@@ -340,22 +340,22 @@ def self.to_winpe_only(framework, code, opts={}, arch="x86")
340
340
341
341
# look for section with entry point
342
342
sections_header . each do |sec |
343
- virtualAddress = sec [ 1 ] [ virtualAddress_offset , 0x4 ] . unpack ( 'L ' ) [ 0 ]
344
- sizeOfRawData = sec [ 1 ] [ sizeOfRawData_offset , 0x4 ] . unpack ( 'L ' ) [ 0 ]
345
- characteristics = sec [ 1 ] [ characteristics_offset , 0x4 ] . unpack ( 'L ' ) [ 0 ]
343
+ virtualAddress = sec [ 1 ] [ virtualAddress_offset , 0x4 ] . unpack ( 'V ' ) [ 0 ]
344
+ sizeOfRawData = sec [ 1 ] [ sizeOfRawData_offset , 0x4 ] . unpack ( 'V ' ) [ 0 ]
345
+ characteristics = sec [ 1 ] [ characteristics_offset , 0x4 ] . unpack ( 'V ' ) [ 0 ]
346
346
347
347
if ( virtualAddress ...virtualAddress +sizeOfRawData ) . include? ( addressOfEntryPoint )
348
- importsTable = pe . hdr . opt . DataDirectory [ 8 ..( 8 +4 ) ] . unpack ( 'L ' ) [ 0 ]
348
+ importsTable = pe . hdr . opt . DataDirectory [ 8 ..( 8 +4 ) ] . unpack ( 'V ' ) [ 0 ]
349
349
if ( importsTable - addressOfEntryPoint ) < code . length
350
350
#shift original entry point to prevent tables overwritting
351
351
addressOfEntryPoint = importsTable - ( code . length + 4 )
352
352
353
353
entry_point_offset = pe . _dos_header . v [ 'e_lfanew' ] + entryPoint_offset
354
- exe [ entry_point_offset , 4 ] = [ addressOfEntryPoint ] . pack ( 'L ' )
354
+ exe [ entry_point_offset , 4 ] = [ addressOfEntryPoint ] . pack ( 'V ' )
355
355
end
356
356
# put this section writable
357
357
characteristics |= 0x8000_0000
358
- newcharacteristics = [ characteristics ] . pack ( 'L ' )
358
+ newcharacteristics = [ characteristics ] . pack ( 'V ' )
359
359
exe [ sec [ 0 ] , newcharacteristics . length ] = newcharacteristics
360
360
end
361
361
end
@@ -572,20 +572,20 @@ def self.to_win32pe_service(framework, code, opts={})
572
572
"\x5B \x5B \x61 \x59 \x5A \x51 \xFF \xE0 \x58 \x5F \x5A \x8B \x12 \xEB \x86 \x5D " +
573
573
"\x6A \x00 \x68 \x70 \x69 \x33 \x32 \x68 \x61 \x64 \x76 \x61 \x54 \x68 \x4C \x77 " +
574
574
"\x26 \x07 \xFF \xD5 " +pushed_service_name +"\x89 \xE1 " +
575
- "\x8D \x85 " +[ svcmain_code_offset ] . pack ( '<I ' ) +"\x6A \x00 \x50 \x51 \x89 \xE0 \x6A \x00 \x50 \x68 " +
575
+ "\x8D \x85 " +[ svcmain_code_offset ] . pack ( 'V ' ) +"\x6A \x00 \x50 \x51 \x89 \xE0 \x6A \x00 \x50 \x68 " +
576
576
"\xFA \xF7 \x72 \xCB \xFF \xD5 \x6A \x00 \x68 \xF0 \xB5 \xA2 \x56 \xFF \xD5 \x58 " +
577
577
"\x58 \x58 \x58 \x31 \xC0 \xC3 \xFC \xE8 \x00 \x00 \x00 \x00 \x5D \x81 \xED " +
578
- [ hash_code_offset ] . pack ( '<I ' ) +pushed_service_name +"\x89 \xE1 \x8D " +
579
- "\x85 " +[ svcctrlhandler_code_offset ] . pack ( '<I ' ) +"\x6A \x00 \x50 \x51 \x68 \x0B \xAA \x44 \x52 \xFF \xD5 " +
578
+ [ hash_code_offset ] . pack ( 'V ' ) +pushed_service_name +"\x89 \xE1 \x8D " +
579
+ "\x85 " +[ svcctrlhandler_code_offset ] . pack ( 'V ' ) +"\x6A \x00 \x50 \x51 \x68 \x0B \xAA \x44 \x52 \xFF \xD5 " +
580
580
"\x6A \x00 \x6A \x00 \x6A \x00 \x6A \x00 \x6A \x00 \x6A \x00 \x6A \x04 \x6A \x10 " +
581
581
"\x89 \xE1 \x6A \x00 \x51 \x50 \x68 \xC6 \x55 \x37 \x7D \xFF \xD5 \x31 \xFF \x6A " +
582
582
"\x04 \x68 \x00 \x10 \x00 \x00 \x6A \x54 \x57 \x68 \x58 \xA4 \x53 \xE5 \xFF \xD5 " +
583
583
"\xC7 \x00 \x44 \x00 \x00 \x00 \x8D \x70 \x44 \x57 \x68 \x2E \x65 \x78 \x65 \x68 " +
584
584
"\x6C \x6C \x33 \x32 \x68 \x72 \x75 \x6E \x64 \x89 \xE1 \x56 \x50 \x57 \x57 \x6A " +
585
585
"\x44 \x57 \x57 \x57 \x51 \x57 \x68 \x79 \xCC \x3F \x86 \xFF \xD5 \x8B \x0E \x6A " +
586
- "\x40 \x68 \x00 \x10 \x00 \x00 \x68 " +[ code . length ] . pack ( '<I ' ) +"\x57 \x51 \x68 \xAE \x87 " +
586
+ "\x40 \x68 \x00 \x10 \x00 \x00 \x68 " +[ code . length ] . pack ( 'V ' ) +"\x57 \x51 \x68 \xAE \x87 " +
587
587
"\x92 \x3F \xFF \xD5 \xE8 \x00 \x00 \x00 \x00 \x5A \x89 \xC7 \x8B \x0E \x81 \xC2 " +
588
- [ shellcode_code_offset ] . pack ( '<I ' ) +"\x54 \x68 " +[ code . length ] . pack ( '<I ' ) +
588
+ [ shellcode_code_offset ] . pack ( 'V ' ) +"\x54 \x68 " +[ code . length ] . pack ( 'V ' ) +
589
589
"\x52 \x50 \x51 \x68 \xC5 \xD8 \xBD \xE7 \xFF " +
590
590
"\xD5 \x31 \xC0 \x8B \x0E \x50 \x50 \x50 \x57 \x50 \x50 \x51 \x68 \xC6 \xAC \x9A " +
591
591
"\x79 \xFF \xD5 \x8B \x0E \x51 \x68 \xC6 \x96 \x87 \x52 \xFF \xD5 \x8B \x4E \x04 " +
@@ -654,12 +654,17 @@ def self.replace_msi_buffer(pe, opts)
654
654
msi = fd . read ( fd . stat . size )
655
655
}
656
656
657
- section_size = 2 **( msi [ 30 ..31 ] . unpack ( 's' ) [ 0 ] )
658
- sector_allocation_table = msi [ section_size ..section_size *2 ] . unpack ( 'l*' )
657
+ section_size = 2 **( msi [ 30 ..31 ] . unpack ( 'v' ) [ 0 ] )
658
+
659
+ # This table is one of the few cases where signed values are needed
660
+ sector_allocation_table = msi [ section_size ..section_size *2 ] . unpack ( 'l<*' )
659
661
660
662
buffer_chain = [ ]
661
- current_secid = 5 # This is closely coupled with the template provided and ideally
662
- # would be calculated from the dir stream?
663
+
664
+ # This is closely coupled with the template provided and ideally
665
+ # would be calculated from the dir stream?
666
+ current_secid = 5
667
+
663
668
664
669
until current_secid == -2
665
670
buffer_chain << current_secid
@@ -827,22 +832,22 @@ def self.to_exe_elf(framework, opts, template, code, big_endian=false)
827
832
828
833
# Check EI_CLASS to determine if the header is 32 or 64 bit
829
834
# Use the proper offsets and pack size
830
- case elf [ 4 ]
831
- when 1 , " \x01 " # ELFCLASS32 - 32 bit (ruby 1.8 and 1.9 )
835
+ case elf [ 4 , 1 ] . unpack ( "C" ) . first
836
+ when 1 # ELFCLASS32 - 32 bit (ruby 1.9+ )
832
837
if big_endian
833
838
elf [ 0x44 , 4 ] = [ elf . length ] . pack ( 'N' ) #p_filesz
834
839
elf [ 0x48 , 4 ] = [ elf . length + code . length ] . pack ( 'N' ) #p_memsz
835
840
else # little endian
836
841
elf [ 0x44 , 4 ] = [ elf . length ] . pack ( 'V' ) #p_filesz
837
842
elf [ 0x48 , 4 ] = [ elf . length + code . length ] . pack ( 'V' ) #p_memsz
838
843
end
839
- when 2 , " \x02 " # ELFCLASS64 - 64 bit (ruby 1.8 and 1.9 )
844
+ when 2 # ELFCLASS64 - 64 bit (ruby 1.9+ )
840
845
if big_endian
841
846
elf [ 0x60 , 8 ] = [ elf . length ] . pack ( 'Q>' ) #p_filesz
842
847
elf [ 0x68 , 8 ] = [ elf . length + code . length ] . pack ( 'Q>' ) #p_memsz
843
848
else # little endian
844
- elf [ 0x60 , 8 ] = [ elf . length ] . pack ( 'Q' ) #p_filesz
845
- elf [ 0x68 , 8 ] = [ elf . length + code . length ] . pack ( 'Q' ) #p_memsz
849
+ elf [ 0x60 , 8 ] = [ elf . length ] . pack ( 'Q< ' ) #p_filesz
850
+ elf [ 0x68 , 8 ] = [ elf . length + code . length ] . pack ( 'Q< ' ) #p_memsz
846
851
end
847
852
else
848
853
raise RuntimeError , "Invalid ELF template: EI_CLASS value not supported"
0 commit comments