Skip to content

Commit e330916

Browse files
committed
Pull out common stuff in Util::EXE/MsfVenom tests
1 parent ffb28fe commit e330916

File tree

5 files changed

+45
-58
lines changed

5 files changed

+45
-58
lines changed

lib/msf/util/exe.rb

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,30 +1993,35 @@ def self.to_executable_fmt(framework, arch, plat, code, fmt, exeopts)
19931993
when 'elf'
19941994
if (not plat or (plat.index(Msf::Module::Platform::Linux)))
19951995
output = case arch
1996-
when ARCH_X86,nil then Msf::Util::EXE.to_linux_x86_elf(framework, code, exeopts)
1997-
when ARCH_X86_64 then Msf::Util::EXE.to_linux_x64_elf(framework, code, exeopts)
1998-
when ARCH_X64 then Msf::Util::EXE.to_linux_x64_elf(framework, code, exeopts)
1999-
when ARCH_ARMLE then Msf::Util::EXE.to_linux_armle_elf(framework, code, exeopts)
2000-
when ARCH_MIPSBE then Msf::Util::EXE.to_linux_mipsbe_elf(framework, code, exeopts)
2001-
when ARCH_MIPSLE then Msf::Util::EXE.to_linux_mipsle_elf(framework, code, exeopts)
1996+
when ARCH_X86,nil then to_linux_x86_elf(framework, code, exeopts)
1997+
when ARCH_X86_64 then to_linux_x64_elf(framework, code, exeopts)
1998+
when ARCH_X64 then to_linux_x64_elf(framework, code, exeopts)
1999+
when ARCH_ARMLE then to_linux_armle_elf(framework, code, exeopts)
2000+
when ARCH_MIPSBE then to_linux_mipsbe_elf(framework, code, exeopts)
2001+
when ARCH_MIPSLE then to_linux_mipsle_elf(framework, code, exeopts)
20022002
end
20032003
elsif(plat and (plat.index(Msf::Module::Platform::BSD)))
20042004
output = case arch
20052005
when ARCH_X86,nil then Msf::Util::EXE.to_bsd_x86_elf(framework, code, exeopts)
20062006
end
20072007
elsif(plat and (plat.index(Msf::Module::Platform::Solaris)))
20082008
output = case arch
2009-
when ARCH_X86,nil then Msf::Util::EXE.to_solaris_x86_elf(framework, code, exeopts)
2009+
when ARCH_X86,nil then to_solaris_x86_elf(framework, code, exeopts)
20102010
end
20112011
end
20122012

2013+
# this should really be 'jar'
2014+
when 'java'
2015+
2016+
2017+
20132018
when 'macho'
20142019
output = case arch
2015-
when ARCH_X86,nil then Msf::Util::EXE.to_osx_x86_macho(framework, code, exeopts)
2016-
when ARCH_X86_64 then Msf::Util::EXE.to_osx_x64_macho(framework, code, exeopts)
2017-
when ARCH_X64 then Msf::Util::EXE.to_osx_x64_macho(framework, code, exeopts)
2018-
when ARCH_ARMLE then Msf::Util::EXE.to_osx_arm_macho(framework, code, exeopts)
2019-
when ARCH_PPC then Msf::Util::EXE.to_osx_ppc_macho(framework, code, exeopts)
2020+
when ARCH_X86,nil then to_osx_x86_macho(framework, code, exeopts)
2021+
when ARCH_X86_64 then to_osx_x64_macho(framework, code, exeopts)
2022+
when ARCH_X64 then to_osx_x64_macho(framework, code, exeopts)
2023+
when ARCH_ARMLE then to_osx_arm_macho(framework, code, exeopts)
2024+
when ARCH_PPC then to_osx_ppc_macho(framework, code, exeopts)
20202025
end
20212026

20222027
when 'vba'

msfconsole

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class OptsConsole
100100
options['DatabaseMigrationPaths'] ||= []
101101
options['DatabaseMigrationPaths'] << m
102102
end
103-
103+
104104
opts.on("-e", "--environment <production|development>", "Specify the database environment to load from the YAML") do |m|
105105
options['DatabaseEnv'] = m
106106
end

msfvenom

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ class MsfVenom
430430
# possible
431431
when "war"
432432
exe = ::Msf::Util::EXE.to_executable_fmt(framework, @opts[:arch], @opts[:platform], payload_raw, @opts[:format], exeopts)
433-
if (!exe and payload.platform.platforms.index(::Msf::Module::Platform::Java))
433+
if (!exe && payload.respond_to?(:generate_war))
434434
exe = payload.generate_war.pack
435435
else
436436
exe = ::Msf::Util::EXE.to_jsp_war(exe)
@@ -441,7 +441,7 @@ class MsfVenom
441441
# payload if possible
442442
when "java"
443443
exe = ::Msf::Util::EXE.to_executable_fmt(framework, @opts[:arch], @opts[:platform], payload_raw, @opts[:format], exeopts)
444-
if(!exe and payload.platform.platforms.index(::Msf::Module::Platform::Java))
444+
if (!exe && payload.respond_to?(:generate_jar))
445445
exe = payload.generate_jar.pack
446446
end
447447
if exe
@@ -451,7 +451,8 @@ class MsfVenom
451451
end
452452

453453
when *::Msf::Simple::Buffer.transform_formats
454-
@out.write ::Msf::Simple::Buffer.transform(payload_raw, @opts[:format])
454+
buf = ::Msf::Simple::Buffer.transform(payload_raw, @opts[:format])
455+
@out.write buf
455456

456457
when *::Msf::Util::EXE.to_executable_fmt_formats
457458
exe = ::Msf::Util::EXE.to_executable_fmt(framework, @opts[:arch], @opts[:platform], payload_raw, @opts[:format], exeopts)

spec/lib/msf/util/exe_spec.rb

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require 'msf/core'
44
require 'msf/base/simple'
5+
require 'spec_helper'
56

67
describe Msf::Util::EXE do
78

@@ -28,41 +29,9 @@
2829
bin.should == nil
2930
end
3031

31-
platform_format_map = {
32-
"windows" => [
33-
{ :format => "dll", :arch => "x86", :file_fp => /PE32 .*DLL/ },
34-
{ :format => "dll", :arch => "x64", :file_fp => /PE32\+.*DLL/ },
35-
{ :format => "exe", :arch => "x86", :file_fp => /PE32 / },
36-
{ :format => "exe", :arch => "x64", :file_fp => /PE32\+/ },
37-
{ :format => "exe-small", :arch => "x86", :file_fp => /PE32 / },
38-
# No template for 64-bit exe-small. That's fine, we probably
39-
# don't need one.
40-
#{ :format => "exe-small", :arch => "x64", :file_fp => /PE32\+/ },
41-
{ :format => "exe-only", :arch => "x86", :file_fp => /PE32 / },
42-
{ :format => "exe-only", :arch => "x64", :file_fp => /PE32\+ / },
43-
],
44-
"linux" => [
45-
{ :format => "elf", :arch => "x86", :file_fp => /ELF 32.*SYSV/ },
46-
{ :format => "elf", :arch => "x64", :file_fp => /ELF 64.*SYSV/ },
47-
{ :format => "elf", :arch => "armle", :file_fp => /ELF 32.*ARM/ },
48-
{ :format => "elf", :arch => "mipsbe", :file_fp => /ELF 32-bit MSB executable, MIPS/ },
49-
{ :format => "elf", :arch => "mipsle", :file_fp => /ELF 32-bit LSB executable, MIPS/ },
50-
],
51-
"bsd" => [
52-
{ :format => "elf", :arch => "x86", :file_fp => /ELF 32.*BSD/ },
53-
],
54-
"solaris" => [
55-
{ :format => "elf", :arch => "x86", :file_fp => /ELF 32/ },
56-
],
57-
"osx" => [
58-
{ :format => "macho", :arch => "x86", :file_fp => /Mach-O.*i386/ },
59-
{ :format => "macho", :arch => "x64", :file_fp => /Mach-O 64/ },
60-
{ :format => "macho", :arch => "armle", :file_fp => /Mach-O.*acorn/, :pending => true },
61-
{ :format => "macho", :arch => "ppc", :file_fp => /Mach-O.*ppc/, :pending => true },
62-
]
63-
}
64-
65-
platform_format_map.each do |plat, formats|
32+
include_context 'Msf::Util::Exe'
33+
34+
@platform_format_map.each do |plat, formats|
6635
context "with platform=#{plat}" do
6736
let(:platform) do
6837
Msf::Module::PlatformList.transform(plat)
@@ -95,12 +64,7 @@
9564
bin = subject.to_executable_fmt($framework, arch, platform, "\xcc", fmt, {})
9665
bin.should be_a String
9766

98-
f = IO.popen("file -","w+")
99-
f.write(bin)
100-
f.close_write
101-
fp = f.read
102-
f.close
103-
fp.should =~ format_hash[:file_fp] if format_hash[:file_fp]
67+
verify_bin_fingerprint(format_hash, bin)
10468
end
10569

10670
end

spec/msfvenom_spec.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
end
3434

3535
let(:framework) { @framework }
36-
3736
describe "#dump_encoders" do
3837
it "should list known encoders" do
3938
dump = venom.dump_encoders
@@ -140,6 +139,8 @@
140139
end
141140

142141
describe "#generate" do
142+
include_context 'Msf::Util::Exe'
143+
143144
before { venom.parse_args(args) }
144145

145146
context "with 'exe' format" do
@@ -165,6 +166,22 @@
165166
end
166167
end
167168

169+
@platform_format_map.each do |plat, formats|
170+
formats.each do |format_hash|
171+
context "with format=#{format_hash[:format]} platform=#{plat} arch=#{format_hash[:arch]}" do
172+
# This will build executables with no payload. They won't work
173+
# of course, but at least we can see that it is producing the
174+
# correct file format for the given arch and platform.
175+
let(:args) { %W! -p - -f #{format_hash[:format]} -a #{format_hash[:arch]} --platform #{plat} ! }
176+
it "should print a #{format_hash[:format]} to stdout" do
177+
venom.generate
178+
output = stdout.string
179+
verify_bin_fingerprint(format_hash, output)
180+
end
181+
end
182+
end
183+
end
184+
168185
end
169186

170187
end

0 commit comments

Comments
 (0)