SLAE x86 - Assignment 0x3
Egg Hunter Shellcode
Objectives
- Study about egg hunter shellcode
- Create a working demo of the egg hunter
- Should be configurable of different payloads
Overview
Egg hunter shellcode is a helper shellcode that will scan the memory pages to search for a predefined pattern called egg
.
The egg is appended in the main shellcode that will be executed during the exploitation.
It is useful when there’s a limited shellcode size for a vulnerable application’s input.
The egg hunter shellcode can then be used as an alternative since it is small, but the main shellcode should be put in other inputs to find it in the memory.
This research paper
presented several implementations for both Linux and Windows
Dissecting the shellcode
Below is the overview of the shellcode utilizing access(2)
syscall:
|
|
For the first three lines, the registers are being prepared for the next instructions. See comments in the code below for inline explanation:
mov ebx,0x50905090 ; 0x50905090 is the egg with equivalent instructions of:
; nops; push eax; nops; eax
; The egg is saved into ebx will then be use to compare
; in instructions lines 13 and 14 of full code
xor ecx,ecx ; clear the value of ecx; ecx xor ecx = 0
mul ecx ; clear eax and edx
; multiplying 0x0 with eax will result to 0x0 for
; both eax and edx, see https://www.felixcloutier.com/x86/mul
Then the next lines prepare registers for the access(2) syscall
or dx,0xfff ; 0x0 or 0xfff = 0xfff, means dx = 0xfff = 4095
inc edx ; makes edx = 4096
pusha ; push registers to stack,
; this is to save the values and then use them later
; since the next line will alter ebx
lea ebx,[edx+0x4] ; ebx = address to be validated
; in this case is edx + 0x4
; 4 bytes is added since 8 bytes will be validated in single swoop
; which means edx - 0x4 is within the range as well
System call access(2), then the return value is stored in eax. More details in: https://man7.org/linux/man-pages/man2/access.2.html
mov al,0x21 ; 0x21 is the code for access(2)
int 0x80 ; syscall access(address_to_be_validated, NULL)
The next lines are series of conditions to check if the current page contains the egg
cmp al,0xf2 ; The return value in eax is compared to 0xf2,
; the value for EFAULT - the page is inaccessible.
; If the value are the same, zero flag will be set.
popa ; restore the previous registers state before line 6
jz 0x9 ; If zero flag is set (page is inaccessible)
; next instruction will be jump back to line 4
; which will increment the dx to check the next page.
; Otherwise, if page is accessible, execution flow will proceed
cmp [edx],ebx ; Checks if the egg is found in the current page
jnz 0xe ; if not, jump to line 5 which will iterate through the current page
; else proceed with the execution flow.
cmp [edx+0x4],ebx ; Checks again if the next 4 bytes contains the egg again
; since it should be prepended twice
jnz 0xe ; if not, same conditions with line 14
jmp edx ; if all conditions are met, jump to the current page
Recall
The egghunter shellcode contains the following stages:
- initialization of registers
- alignment of page to be validated
- inspections of the page by means of several conditions
Implementation
The below nasm code is the implementation of egghunter shellcode based on the understanding of the concept. This is not tested in several systems and not guaranteed to be working at all times:
|
|
Dynamic Configuration
The simple Ruby script below will dynamically use shellcodes that will be passed into the -p
or --payload
flag (default: ‘bin/sh’). Also, there are 4 choices which egghunter shellcode to be used: 3 implementations of skape, and the egghunter from above section.
|
|
Also, a shellcode tester C program below is needed to test this setup since the main shellcode with the egg is stored somewhere in the memory while the program executes the egghunter:
|
|
Script usage and testing
Below demo shows the options for the Ruby script then usage of default payload and a sample HelloWorldShellcode-Stack
payload from the course material.
This blog post has been created for completing the requirements of the SecurityTube Linux Assembly Expert certification: http://securitytube-training.com/online-courses/securitytube-linux-assembly-expert/
Student ID: SLAE - 1558