// shellcode_x86.c
#include <windows.h> // Only include if using Windows API
// Declare EntryPoint with __declspec(naked) to prevent prologue/epilogue
void __declspec(naked) EntryPoint() {
__asm {
// Your x86 shellcode here. Example:
// Get the module handle of kernel32.dll. This is *essential* for resolving
// Windows API functions dynamically.
push esi ; Save ESI
push edi ; Save EDI
xor esi, esi ; ESI will be the counter
next_module:
mov eax, [fs:30h] ; TEB address in FS segment at offset 0x30 (Process Environment Block)
mov eax, [eax + 0ch] ; EAX = PEB (Process Environment Block)
mov eax, [eax + 1ch] ; EAX = PEB_LDR_DATA
mov eax, [eax + 08h] ; EAX = InMemoryOrderModuleList.Flink (first module)
next_module2:
mov edi, [eax + 20h] ; EDI = BaseDllName.Data
mov edx, [eax + 18h] ; EDX = BaseDllName.Length
xor ecx, ecx
compare_strings:
mov bl, [edi + ecx] ; Put a char from dll name in BL
mov bh, kernel32[ecx]; Put a char from kernel32 string in BH
cmp bl, bh ; Compare them
jnz next_module3 ; If they are not the same, go to next module
inc ecx ; Increment counter
cmp ecx, edx ; If counter is greater or equal to dll name length, compare done
jl compare_strings ; If not, compare next chars
push eax ; Save module address (kernel32)
jmp resolve_functions
next_module3:
mov eax, [eax] ; EAX = InMemoryOrderModuleList.Flink (next module)
cmp esi, eax
je next_module ; Loop modules if we are back to the first
mov esi, eax ; Save the pointer of the current module
jmp next_module2 ; Go to first list element to continue the dll name comparation
kernel32:
db 'kernel32.dll', 0
resolve_functions:
//Now we have the module of the DLL at the top of the stack. Let's resolve
//GetProcAddress.
//Get the EAT
pop ebx ; EBX = Base Address of Kernel32
mov eax, [ebx + 0x3C] ; EAX = Offset of PE Header
add eax, ebx ; EAX = Address of PE Header
mov eax, [eax + 0x78] ; EAX = Offset of Export Directory RVA
cmp eax, 0 ; Make sure there is an export table
jz end ; If not, return
add eax, ebx ; EAX = Address of Export Directory
mov ecx, [eax + 0x1C] ; ECX = Number of functions
mov edx, [eax + 0x20] ; EDX = Address of the array of names RVA
add edx, ebx ; EDX = Address of the array of names
//Let's find the function GetProcAddress
xor esi, esi ; Index for our functions, starting with 0
next_function:
push edx ; Save address of names table
push ecx ; Save counter of exported functions
mov edi, [edx + esi * 4] ; EDI = Address of current function name RVA
add edi, ebx ; EDI = Address of current function name
xor ecx, ecx ; Zero counter used to compare strings
compare_GPA:
mov al, [edi + ecx] ; Move the char of the current function name to AL
mov ah, GetProcAddress[ecx] ; Move the char of "GetProcAddress" string to AH
cmp al, ah ; Compare the two chars
jnz next_function2 ; Check next function if they are not the same
inc ecx ; Increment counter
cmp cl, 16 ; Check counter for the string length
jl compare_GPA ; Go to next chars if length is not met
pop ecx ; Restore number of functions
pop edx ; Restore address of names array
mov eax, [eax + 0x24] ; EAX = Address of Ordinal table RVA
add eax, ebx ; EAX = Address of Ordinal table
mov esi, [eax + esi * 2] ; ESI = GetProcAddress Ordinal
xor eax, eax ; Zero EAX register to get ready to move to Address Table
mov eax, [ebx + 0x3C] ; EAX = Offset of PE Header
add eax, ebx ; EAX = Address of PE Header
mov eax, [eax + 0x78] ; EAX = Offset of Export Directory RVA
add eax, ebx ; EAX = Address of Export Directory
mov eax, [eax + 0x1C] ; EAX = Number of functions
sub esi, 1
xor edi, edi
mov edi, [eax + 0x28]
add edi, ebx
mov edi, [edi + esi * 4] ;Address of GetProcAddress RVA
add edi, ebx ; EDI = GetProcAddress Address
push edi ; Push address in stack
jmp get_api_functions ;Get the other api functions
next_function2:
pop ecx ; Restore the ECX value
pop edx ; Restore address of the names table
inc esi ; Increment to look at next function
loop next_function ; Loop
GetProcAddress:
db 'GetProcAddress', 0
get_api_functions:
//Now let's load the rest of the functions
push esp ; Save the stack
push 0x74657247 ; push "GetT"
push 0x74737953 ; push "ySte"
push 0x6d657473 ; push "mets"
push esp ; "GetSystemTime"
push ebx
call dword ptr[esp + 0x20]
mov dword ptr[GetSystemTime_ptr], eax ; Save address GetSystemTime in memory
add esp, 12 ; Clean the stack
push esp ; Save the stack
push 0x41657469 ; push "Atie"
push 0x7373656d ; push "ssem"
push 0x4d616572 ; push "Mrae"
push 0x466f7243 ; push "FroC"
push esp ; "CreateProcessA"
push ebx
call dword ptr[esp + 0x24] ; Call GetProcAddress("CreateProcessA")
mov dword ptr[CreateProcessA_ptr], eax ; Save address CreateProcessA in memory
add esp, 20 ; Clean the stack
push esp ; Save the stack
push 0x64616f4c ; push "daoL"
push 0x6c6c6432 ; push "lld2"
push 0x32737255 ; push "2srU"
push esp ; "LoadLibraryA"
push ebx
call dword ptr[esp + 0x20]
mov dword ptr[LoadLibraryA_ptr], eax ; Save address LoadLibraryA in memory
add esp, 12 ; Clean the stack
push esp ; Save the stack
push 0x64657254 ; push "derT"
push 0x64616572 ; push "daer"
push 0x006c6966 ; push " fil"
push esp ; "ReadFile"
push ebx
call dword ptr[esp + 0x20] ; Call GetProcAddress("ReadFile")
mov dword ptr[ReadFile_ptr], eax ; Save address ReadFile in memory
add esp, 12 ; Clean the stack
push esp ; Save the stack
push 0x65746972 ; push "etir"
push 0x57657469 ; push "Weti"
push 0x656c6946 ; push "eliF"
push esp
push ebx
call dword ptr[esp + 0x24] ; Call GetProcAddress("WriteFile")
mov dword ptr[WriteFile_ptr], eax ; Save address WriteFile_ptr in memory
add esp, 20
push esp ; Save the stack
push 0x736f6c43 ; push "solC"
push 0x65736f6c ; push "esol"
push 0x6e6f4365 ; push "nofCe"
push esp
push ebx
call dword ptr[esp + 0x20]
mov dword ptr[CloseHandle_ptr], eax
add esp, 12
push esp ; Save the stack
push 0x646e4574;push "dnet"
push 0x69735865;push "isXe"
push 0x41737365;push "Asse"
push 0x636f7250;push "corP"
push esp
push ebx
call dword ptr[esp + 0x24] ; Call GetProcAddress("CreateProcessA")
mov dword ptr[ExitProcess_ptr], eax
add esp, 20
//Here you can put code to execute the functions that we resolved
// For Example, let's use CreateProcessA to launch notepad.exe
push esi //save esi
push edi //save edi
push 0 ; dwCreationFlags
push 0 ; lpEnvironment
push 0 ; lpCurrentDirectory
push 0 ; lpStartupInfo
push 0 ; lpThreadAttributes
push 0 ; lpProcessAttributes
push 0 ; bInheritHandles
push 0 ; lpProcessInformation
push esp ; lpCommandLine
push notepad_cmd ; lpApplicationName
mov eax, dword ptr[CreateProcessA_ptr]
call eax ; Launch Notepad
add esp, 36
pop edi
pop esi
push 0 ; uExitCode = 0
mov eax, dword ptr[ExitProcess_ptr]
call eax
end:
pop edi //restore edi
pop esi //restore esi
ret
GetSystemTime_ptr:
dd 0
CreateProcessA_ptr:
dd 0
LoadLibraryA_ptr:
dd 0
ReadFile_ptr:
dd 0
WriteFile_ptr:
dd 0
CloseHandle_ptr:
dd 0
ExitProcess_ptr:
dd 0
notepad_cmd:
db 'notepad.exe', 0
}
}
#pragma comment (linker, "/ENTRY:EntryPoint")
#pragma comment (linker, "/NODEFAULTLIB")