@@ -147,6 +147,18 @@ def self.to_executable(framework, arch, plat, code = '', opts = {})
147
147
nil
148
148
end
149
149
150
+ def self . clear_dynamic_base ( exe , pe )
151
+ c_bits = ( "%32d" %pe . hdr . opt . DllCharacteristics . to_s ( 2 ) ) . split ( '' ) . map { |e | e . to_i } . reverse
152
+ c_bits [ 6 ] = 0 # DYNAMIC_BASE
153
+ new_dllcharacteristics = c_bits . reverse . join . to_i ( 2 )
154
+
155
+ # PE Header Pointer offset = 60d
156
+ # SizeOfOptionalHeader offset = 94h
157
+ dll_ch_offset = exe [ 60 , 4 ] . unpack ( 'h4' ) [ 0 ] . reverse . hex + 94
158
+ exe [ dll_ch_offset , 2 ] = [ new_dllcharacteristics ] . pack ( "v" )
159
+ exe
160
+ end
161
+
150
162
def self . to_win32pe ( framework , code , opts = { } )
151
163
152
164
# For backward compatability, this is roughly equivalent to 'exe-small' fmt
@@ -169,12 +181,6 @@ def self.to_win32pe(framework, code, opts = {})
169
181
fsize = File . size ( opts [ :template ] )
170
182
pe = Rex ::PeParsey ::Pe . new_from_file ( opts [ :template ] , true )
171
183
172
- # DYNAMIC_BASE modification
173
- original_dllcharacteristics = pe . hdr . opt . DllCharacteristics
174
- c_bits = ( "%32d" %original_dllcharacteristics . to_s ( 2 ) ) . split ( '' ) . map { |e | e . to_i } . reverse
175
- c_bits [ 6 ] = 0 # DYNAMIC_BASE
176
- new_dllcharacteristics = c_bits . reverse . join . to_i ( 2 )
177
-
178
184
text = nil
179
185
pe . sections . each { |sec | text = sec if sec . name == ".text" }
180
186
@@ -185,7 +191,7 @@ def self.to_win32pe(framework, code, opts = {})
185
191
:template => opts [ :template ] ,
186
192
:arch => :x86
187
193
} )
188
- injector . generate_pe
194
+ return injector . generate_pe
189
195
end
190
196
191
197
raise RuntimeError , "No .text section found in the template" unless text
@@ -284,15 +290,12 @@ def self.to_win32pe(framework, code, opts = {})
284
290
tds = pe . hdr . file . TimeDateStamp
285
291
exe [ exe . index ( [ tds ] . pack ( 'V' ) ) , 4 ] = [ tds - rand ( 0x1000000 ) ] . pack ( "V" )
286
292
287
- # Patch dll characteristics
288
- dll_ch_offset = exe [ 60 , 4 ] . unpack ( 'h4' ) [ 0 ] . reverse . hex + 94
289
- exe [ dll_ch_offset , 2 ] = [ new_dllcharacteristics ] . pack ( "v" )
290
-
291
293
cks = pe . hdr . opt . CheckSum
292
294
unless cks == 0
293
295
exe [ exe . index ( [ cks ] . pack ( 'V' ) ) , 4 ] = [ 0 ] . pack ( "V" )
294
296
end
295
297
298
+ exe = clear_dynamic_base ( exe , pe )
296
299
pe . close
297
300
298
301
exe
@@ -359,6 +362,7 @@ def self.to_winpe_only(framework, code, opts = {}, arch="x86")
359
362
# put the shellcode at the entry point, overwriting template
360
363
entryPoint_file_offset = pe . rva_to_file_offset ( addressOfEntryPoint )
361
364
exe [ entryPoint_file_offset , code . length ] = code
365
+ exe = clear_dynamic_base ( exe , pe )
362
366
exe
363
367
end
364
368
@@ -440,7 +444,6 @@ def self.string_to_pushes(string)
440
444
end
441
445
442
446
def self . exe_sub_method ( code , opts = { } )
443
-
444
447
pe = self . get_file_contents ( opts [ :template ] )
445
448
446
449
case opts [ :exe_type ]
@@ -506,7 +509,7 @@ def self.to_win64pe(framework, code, opts = {})
506
509
:template => opts [ :template ] ,
507
510
:arch => :x64
508
511
} )
509
- injector . generate_pe
512
+ return injector . generate_pe
510
513
end
511
514
opts [ :exe_type ] = :exe_sub
512
515
exe_sub_method ( code , opts )
0 commit comments