Skip to content

Commit 44c5fc3

Browse files
RageLtManbusterb
authored andcommitted
Sync build_net_code post module upstream
Fix merge conflicts and add missing lines to framework version of the DotNet compiler example module. Test output to come in PR rapid7#5393
1 parent 0e57808 commit 44c5fc3

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

lib/msf/core/exploit/powershell/dot_net.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def dot_net_compiler(opts = {})
4646
# Configure .NET assemblies required to compile source
4747
assemblies = ["mscorlib.dll", "System.Xml.dll", "System.Data.dll"]
4848
if opts[:assemblies]
49-
opts[:assemblies] = opts[:assemblies].split(',').map {|a| agsub(/\s+/,'')} unless opts[:assemblies].is_a?(Array)
49+
opts[:assemblies] = opts[:assemblies].split(',').map {|a| a.gsub(/\s+/,'')} unless opts[:assemblies].is_a?(Array)
5050
assemblies += opts[:assemblies]
5151
end
5252
assemblies = assemblies.uniq.compact
@@ -111,7 +111,7 @@ def dot_net_compiler(opts = {})
111111
# PS uses .NET 2.0 by default which doesnt work @ present (20120814, RLTM)
112112
# x86 targets also need to be compiled in x86 powershell instance
113113
run_32 = compiler_opts =~ /platform:x86/i ? true : false
114-
if opts[:net_clr] and opts[:net_clr] > 2 # PS before 3.0 natively uses NET 2
114+
if opts[:net_clr] and opts[:net_clr].to_i > 2 # PS before 3.0 natively uses NET 2
115115
return elevate_net_clr(compiler, run_32, opts[:net_clr])
116116
else
117117
return compiler

modules/post/windows/manage/powershell/build_net_code.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ def initialize(info={})
4141
OptString.new('ASSEMBLIES', [
4242
false,
4343
'Any assemblies outside the defaults',
44-
"mscorlib.dll, System.dll, System.Xml.dll, System.Data.dll, System.Net.dll"
44+
"mscorlib.dll, System.dll, System.Xml.dll, System.Data.dll"
4545
]),
46-
OptString.new('OUTPUT_TARGET', [true, 'Name and path of the generated binary, default random, omit extension' ]),
46+
OptString.new('OUTPUT_TARGET', [false, 'Name and path of the generated binary, default random, omit extension' ]),
4747
OptString.new('COMPILER_OPTS', [false, 'Options to pass to compiler', '/optimize']),
4848
OptString.new('CODE_PROVIDER', [true, 'Code provider to use', 'Microsoft.CSharp.CSharpCodeProvider']),
4949

@@ -55,7 +55,7 @@ def initialize(info={})
5555

5656
end
5757

58-
def exploit
58+
def run
5959

6060
# Make sure we meet the requirements before running the script
6161
if !(session.type == "meterpreter" || have_powershell?)
@@ -82,18 +82,22 @@ def exploit
8282
net_com_opts[:cert] = datastore['CERT_PATH']
8383

8484
begin
85-
script = ::File.read(datastore['SOURCE_FILE'])
85+
net_com_opts[:harness] = ::File.read(datastore['SOURCE_FILE'])
86+
script = dot_net_compiler(net_com_opts)
87+
if datastore['Powershell::Post::dry_run']
88+
print_good("Compiler code:\n#{script}")
89+
return
90+
end
8691
rescue => e
8792
print_error(e)
8893
return
8994
end
9095

9196
vprint_good("Writing to #{net_com_opts[:target]}")
92-
9397
# Compress
9498
print_status('Compressing script contents:')
9599
compressed_script = compress_script(script, eof)
96-
100+
=begin
97101
# If the compressed size is > 8100 bytes, launch stager
98102
if (compressed_script.size > 8100)
99103
print_error(" - Compressed size: #{compressed_script.size}")
@@ -107,9 +111,10 @@ def exploit
107111
print_good(" - Compressed size: #{compressed_script.size}")
108112
script = compressed_script
109113
end
110-
114+
=end
111115
# Execute the powershell script
112116
print_status('Executing the script.')
117+
#psh_exec(script,false,true)
113118
cmd_out, running_pids, open_channels = execute_script(script, true)
114119
get_ps_output(cmd_out,eof)
115120
vprint_good( "Cleaning up #{running_pids.join(', ')}" )
@@ -119,7 +124,7 @@ def exploit
119124
# Check for result
120125
begin
121126
size = session.fs.file.stat(net_com_opts[:target].gsub('\\','\\\\')).size
122-
vprint_good("File #{net_com_opts[:target].gsub('\\','\\\\')} found, #{size}kb")
127+
print_good("File #{net_com_opts[:target].gsub('\\','\\\\')} found, #{size}kb")
123128
rescue
124129
print_error("File #{net_com_opts[:target].gsub('\\','\\\\')} not found")
125130
return

0 commit comments

Comments
 (0)