|
| 1 | +#!/usr/bin/env python2.7 |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import time |
| 5 | +import pickle |
| 6 | +import threading |
| 7 | +import subprocess |
| 8 | +import json |
| 9 | +import requests |
| 10 | +import autosploit |
| 11 | +from blessings import Terminal |
| 12 | + |
| 13 | +t = Terminal() |
| 14 | + |
| 15 | +def censysTargets(clobber=True, hostLimit=-1): |
| 16 | + """Function to gather target host(s) from Censys.""" |
| 17 | + global query |
| 18 | + global stop_animation |
| 19 | + API_URL = "https://censys.io/api/v1/search/ipv4" |
| 20 | + UID = "" |
| 21 | + SECRET = "" |
| 22 | + |
| 23 | + print("\033[H\033[J") # Clear terminal |
| 24 | + autosploit.logo() |
| 25 | + |
| 26 | + if not os.path.isfile("uid.p"): |
| 27 | + print("[{}]Please provide your Censys API ID.".format(t.green("+"))) |
| 28 | + |
| 29 | + UID = raw_input("API ID: ") |
| 30 | + pickle.dump(UID, open("uid.p", "wb")) |
| 31 | + path = os.path.abspath("uid.p") |
| 32 | + print("[{}]\nYour API ID has been saved to {}".format(t.green("+"), path)) |
| 33 | + |
| 34 | + else: |
| 35 | + try: |
| 36 | + UID = pickle.load(open("uid.p", "rb")) |
| 37 | + except IOError as e: |
| 38 | + print("\n[{}]Critical. An IO error was raised while attempting to read API data.\n{}".format( |
| 39 | + t.red("!"), e)) |
| 40 | + |
| 41 | + path = os.path.abspath("uid.p") |
| 42 | + print("\n[{}]Your API ID was loaded from {}".format(t.green("+"), path)) |
| 43 | + |
| 44 | + if not os.path.isfile("secret.p"): |
| 45 | + print("[{}]Please provide your Censys Secret key.".format(t.green("+"))) |
| 46 | + |
| 47 | + SECRET = raw_input("Secret key: ") |
| 48 | + pickle.dump(UID, open("secret.p", "wb")) |
| 49 | + path = os.path.abspath("secret.p") |
| 50 | + print("[{}]\nYour Secret key has been saved to {}".format(t.green("+"), path)) |
| 51 | + |
| 52 | + else: |
| 53 | + try: |
| 54 | + SECRET = pickle.load(open("secret.p", "rb")) |
| 55 | + except IOError as e: |
| 56 | + print("\n[{}]Critical. An IO error was raised while attempting to read Secret key data.\n{}".format( |
| 57 | + t.red("!"), e)) |
| 58 | + |
| 59 | + path = os.path.abspath("secret.p") |
| 60 | + print("\n[{}]Your Secret key was loaded from {}".format(t.green("+"), path)) |
| 61 | + |
| 62 | + print("[{}]Please provide your platform specific search query.".format(t.green("+"))) |
| 63 | + print("[{}]I.E. 'IIS' will return a list of IPs belonging to IIS servers.".format( |
| 64 | + t.green("+"))) |
| 65 | + |
| 66 | + # /TODO: |
| 67 | + # fix this, seems to be some issues with it, I could be wrong though |
| 68 | + while True: |
| 69 | + query = raw_input("\n<" + t.cyan("PLATFORM") + ">$ ") |
| 70 | + if query == "": |
| 71 | + print("[{}]Query cannot be null.".format(t.red("!"))) |
| 72 | + else: |
| 73 | + break |
| 74 | + params = {'query' : query} |
| 75 | + print("[{}]Please stand by while results are being collected...\n\n\n".format( |
| 76 | + t.green("+"))) |
| 77 | + time.sleep(1) |
| 78 | + |
| 79 | + try: |
| 80 | + response = requests.post(API_URL, json = params, auth=(UID, SECRET)) |
| 81 | + except Exception as e: |
| 82 | + print("\n[{}]Critical. An error was raised with the following error message.\n".format(t.red("!"))) |
| 83 | + |
| 84 | + if response.status_code != 200: |
| 85 | + print(result.json()["error"]) |
| 86 | + sys.exit(1) |
| 87 | + |
| 88 | + result = response.json() |
| 89 | + |
| 90 | + thread = threading.Thread(target=autosploit.animation, args=("collecting results", )) |
| 91 | + thread.daemon = True |
| 92 | + thread.start() |
| 93 | + |
| 94 | + # TODO:/ |
| 95 | + # edit the clobber function to work properly |
| 96 | + if clobber: |
| 97 | + with open('hosts.txt', 'wb') as log: |
| 98 | + for _ in xrange(autosploit.toolbar_width): |
| 99 | + time.sleep(0.1) |
| 100 | + for service in result['results']: |
| 101 | + if hostLimit > 0 or hostLimit < 0: |
| 102 | + log.write("{}{}".format(service['ip'], os.linesep)) |
| 103 | + hostLimit -= 1 |
| 104 | + else: |
| 105 | + break |
| 106 | + autosploit.hostpath = os.path.abspath("hosts.txt") |
| 107 | + autosploit.stop_animation = True |
| 108 | + print("\n\n\n[{}]Done.".format(t.green("+"))) |
| 109 | + print("[{}]Host list saved to {}".format(t.green("+"), autosploit.hostpath)) |
| 110 | + else: |
| 111 | + with open("hosts.txt", "ab") as log: |
| 112 | + for i in xrange(autosploit.toolbar_width): |
| 113 | + time.sleep(0.1) |
| 114 | + for service in result['results']: |
| 115 | + if hostLimit > 0 or hostLimit < 0: |
| 116 | + log.write("{}{}".format(service['ip'], os.linesep)) |
| 117 | + hostLimit -= 1 |
| 118 | + else: |
| 119 | + break |
| 120 | + autosploit.hostpath = os.path.abspath("hosts.txt") |
| 121 | + autosploit.stop_animation = True |
| 122 | + print("\n\n\n[{}]Done.".format(t.green("+"))) |
| 123 | + print("[{}]Hosts appended to list at ".format(t.green("+"), autosploit.hostpath)) |
0 commit comments