Skip to content

Commit bd24e7e

Browse files
committed
more cleanups and print output on auto-run
1 parent 63bf93b commit bd24e7e

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

documentation/modules/post/windows/manage/powershell/build_net_code.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ To build and run the code:
1818

1919
```
2020
msf exploit(handler) > use post/windows/manage/powershell/build_net_code
21-
msf post(build_net_code) > set session -1
22-
session => -1
21+
msf post(build_net_code) > set SESSION -1
22+
SESSION => -1
2323
msf post(build_net_code) > show options
2424
2525
Module options (post/windows/manage/powershell/build_net_code):
@@ -55,3 +55,23 @@ E:\metasploit-framework>C:\cygwin64\tmp\aNwCFmmLzlYvPWw.exe
5555
C:\cygwin64\tmp\aNwCFmmLzlYvPWw.exe
5656
Hello, World!
5757
```
58+
59+
You can also run the code automatically:
60+
61+
```
62+
msf exploit(handler) > use post/windows/manage/powershell/build_net_code
63+
msf post(build_net_code) > set SOURCE_FILE /tmp/hello.cs
64+
SOURCE_FILE => /tmp/hello.cs
65+
msf post(build_net_code) > set RUN_BINARY true
66+
RUN_BINARY => true
67+
msf post(build_net_code) > set SESSION -1
68+
SESSION => -1
69+
msf post(build_net_code) > run
70+
71+
[*] Building remote code.
72+
[+] File C:\cygwin64\tmp\QuEQSEifJOe.exe found, 3584kb
73+
[+] Hello, World!
74+
75+
[+] Finished!
76+
[*] Post module execution completed
77+
```

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,22 @@ def initialize(info = {})
3939
[
4040
OptPath.new('SOURCE_FILE', [true, 'Path to source code']),
4141
OptBool.new('RUN_BINARY', [false, 'Execute the generated binary', false]),
42-
OptString.new('ASSEMBLIES', [false, 'Any assemblies outside the defaults',
43-
"mscorlib.dll, System.dll, System.Xml.dll, System.Data.dll" ]),
42+
OptString.new('ASSEMBLIES', [false, 'Any assemblies outside the defaults', "mscorlib.dll, System.dll, System.Xml.dll, System.Data.dll" ]),
4443
OptString.new('OUTPUT_TARGET', [false, 'Name and path of the generated binary, default random, omit extension' ]),
4544
OptString.new('COMPILER_OPTS', [false, 'Options to pass to compiler', '/optimize']),
4645
OptString.new('CODE_PROVIDER', [true, 'Code provider to use', 'Microsoft.CSharp.CSharpCodeProvider'])
47-
], self.class)
46+
], self.class
47+
)
4848
register_advanced_options(
4949
[
5050
OptString.new('NET_CLR_VER', [false, 'Minimum NET CLR version required to compile', '4.0'])
51-
], self.class)
51+
], self.class
52+
)
5253
end
5354

5455
def run
5556
# Make sure we meet the requirements before running the script
56-
if !(session.type == "meterpreter" || have_powershell?)
57+
unless session.type == "meterpreter" || have_powershell?
5758
print_error "Incompatible Environment"
5859
return 0
5960
end
@@ -68,8 +69,9 @@ def run
6869
eof = Rex::Text.rand_text_alpha(8)
6970
env_suffix = Rex::Text.rand_text_alpha(8)
7071
net_com_opts = {}
71-
net_com_opts[:target] = datastore['OUTPUT_TARGET'] ||
72-
session.fs.file.expand_path('%TEMP%') + "\\#{Rex::Text.rand_text_alpha(rand(8) + 8)}.exe"
72+
net_com_opts[:target] =
73+
datastore['OUTPUT_TARGET'] ||
74+
"#{session.fs.file.expand_path('%TEMP%')}\\#{Rex::Text.rand_text_alpha(rand(8) + 8)}.exe"
7375
net_com_opts[:com_opts] = datastore['COMPILER_OPTS']
7476
net_com_opts[:provider] = datastore['CODE_PROVIDER']
7577
net_com_opts[:assemblies] = datastore['ASSEMBLIES']
@@ -110,8 +112,11 @@ def run
110112

111113
# Run the result
112114
if datastore['RUN_BINARY']
113-
session.sys.process.execute(net_com_opts[:target].gsub('\\', '\\\\'),
114-
nil, { 'Hidden' => true, 'Channelized' => true })
115+
cmd_out = session.sys.process.execute(net_com_opts[:target].gsub('\\', '\\\\'),
116+
nil, 'Hidden' => true, 'Channelized' => true)
117+
while (out = cmd_out.channel.read)
118+
print_good out
119+
end
115120
end
116121

117122
print_good 'Finished!'

0 commit comments

Comments
 (0)