@@ -325,22 +325,22 @@ def self.to_winpe_only(framework, code, opts = {}, arch="x86")
325
325
326
326
# look for section with entry point
327
327
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 ]
331
331
332
332
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 ]
334
334
if ( importsTable - addressOfEntryPoint ) < code . length
335
335
#shift original entry point to prevent tables overwritting
336
336
addressOfEntryPoint = importsTable - code . length + 4
337
337
338
338
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 ' )
340
340
end
341
341
# put this section writable
342
342
characteristics |= 0x8000_0000
343
- newcharacteristics = [ characteristics ] . pack ( 'L ' )
343
+ newcharacteristics = [ characteristics ] . pack ( 'V ' )
344
344
exe [ sec [ 0 ] , newcharacteristics . length ] = newcharacteristics
345
345
end
346
346
end
@@ -633,12 +633,16 @@ def self.replace_msi_buffer(pe, opts)
633
633
634
634
msi = self . get_file_contents ( template )
635
635
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<*' )
638
640
639
641
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
642
646
643
647
until current_secid == -2
644
648
buffer_chain << current_secid
@@ -672,12 +676,12 @@ def self.to_osx_arm_macho(framework, code, opts = {})
672
676
set_template_default ( opts , "template_armle_darwin.bin" )
673
677
674
678
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 } "
676
680
bo = self . find_payload_tag ( mo , "Invalid OSX ArmLE Mach-O template: missing \" PAYLOAD:\" tag" )
677
681
puts "bo is #{ bo . class } :#{ bo . to_s } "
678
682
mo [ bo , code . length ] = code
679
683
puts "mo after is #{ mo . class } :#{ mo . to_s } :#{ mo . length } "
680
- mo
684
+ return mo
681
685
end
682
686
683
687
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)
781
785
782
786
# Check EI_CLASS to determine if the header is 32 or 64 bit
783
787
# 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+ )
786
790
if big_endian
787
791
elf [ 0x44 , 4 ] = [ elf . length ] . pack ( 'N' ) #p_filesz
788
792
elf [ 0x48 , 4 ] = [ elf . length + code . length ] . pack ( 'N' ) #p_memsz
789
793
else # little endian
790
794
elf [ 0x44 , 4 ] = [ elf . length ] . pack ( 'V' ) #p_filesz
791
795
elf [ 0x48 , 4 ] = [ elf . length + code . length ] . pack ( 'V' ) #p_memsz
792
796
end
793
- when 2 , " \x02 " # ELFCLASS64 - 64 bit (ruby 1.8 and 1.9 )
797
+ when 2 # ELFCLASS64 - 64 bit (ruby 1.9+ )
794
798
if big_endian
795
799
elf [ 0x60 , 8 ] = [ elf . length ] . pack ( 'Q>' ) #p_filesz
796
800
elf [ 0x68 , 8 ] = [ elf . length + code . length ] . pack ( 'Q>' ) #p_memsz
797
801
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
800
804
end
801
805
else
802
806
raise RuntimeError , "Invalid ELF template: EI_CLASS value not supported"
@@ -1027,18 +1031,19 @@ def self.to_mem_aspx(framework, code, exeopts = {})
1027
1031
read_replace_script_template ( "to_mem.aspx.template" , hash_sub )
1028
1032
end
1029
1033
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 )
1041
1045
1046
+ hash_sub = rig . to_h
1042
1047
hash_sub [ :b64shellcode ] = Rex ::Text . encode_base64 ( code )
1043
1048
1044
1049
read_replace_script_template ( "to_mem_dotnet.ps1.template" , hash_sub ) . gsub ( /(?<!\r )\n / , "\r \n " )
0 commit comments