Skip to content

Commit a9f00db

Browse files
ehlewisEkultek
authored andcommitted
Implemented Censys Functionality (#60) (#56 dome)
* Added censys functionality * Added Censys functionality Moved Censys search function into its own file * Updated requirements document to include requests module
1 parent 7a7e587 commit a9f00db

File tree

4 files changed

+175
-12
lines changed

4 files changed

+175
-12
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
*.pyc
22
.idea/*
33
api.p
4-
hosts.txt
4+
hosts.txt
5+
secret.p
6+
uid.p

autosploit.py

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import pickle
2424
import threading
2525
import subprocess
26-
26+
import json
27+
import requests
28+
import censysSearch
2729
import shodan
2830
# idk if you're going to need this since retrying is a decorator (see line 410)
2931
# from retrying import retry
@@ -344,6 +346,7 @@ def targets(clobber=True, hostLimit = -1):
344346
print("[{}]Hosts appended to list at ".format(t.green("+"), hostpath))
345347

346348

349+
347350
# TODO:/
348351
# custom list importing needs to be done here.
349352
# could be possible to import the custom list via argparse
@@ -481,6 +484,7 @@ def try_shodan():
481484
# Make sure a misconfiguration in the MSF settings
482485
# Doesn't execute main menu loop but returns us to the
483486
# appropriate function for handling those settings
487+
484488
if configured is None:
485489
settings()
486490

@@ -493,23 +497,56 @@ def try_shodan():
493497

494498
if action == '1':
495499
usage()
496-
497500
elif action == '2':
498501
hostLimit = -1
499502
limitYN = raw_input("\n[" + t.magenta("?") + "]Limit number of hosts? [y/n]: ").lower()
500503
if limitYN == 'y':
501504
hostLimit = input("\n[" + t.magenta("?") + "]How many?: ")
502-
if not os.path.isfile("hosts.txt"):
503-
targets(True, hostLimit)
504-
else:
505-
append = raw_input(
506-
"\n[" + t.magenta("?") + "]Append hosts to file or overwrite? [A/O]: ").lower()
507-
if append == 'a':
508-
targets(False, hostLimit)
509-
elif append == 'o':
505+
searchOption = input("\nSelect an option:\n1. Search Shodan\n2. Search Censys\n3. Search Shodan and Censys ")
506+
if searchOption == 1:
507+
if not os.path.isfile("hosts.txt"):
510508
targets(True, hostLimit)
511509
else:
512-
print("\n[{}]Unhandled Option.".format(t.red("!")))
510+
append = raw_input(
511+
"\n[" + t.magenta("?") + "]Append hosts to file or overwrite? [A/O]: ").lower()
512+
if append == 'a':
513+
targets(False, hostLimit)
514+
elif append == 'o':
515+
targets(True, hostLimit)
516+
else:
517+
print("\n[{}]Unhandled Option.".format(t.red("!")))
518+
elif searchOption == 2:
519+
if not os.path.isfile("hosts.txt"):
520+
censysSearch.censysTargets(True, hostLimit)
521+
else:
522+
append = raw_input(
523+
"\n[" + t.magenta("?") + "]Append hosts to file or overwrite? [A/O]: ").lower()
524+
if append == 'a':
525+
censysSearch.censysTargets(False, hostLimit)
526+
elif append == 'o':
527+
censysSearch.censysTargets(True, hostLimit)
528+
else:
529+
print("\n[{}]Unhandled Option.".format(t.red("!")))
530+
elif searchOption == 3:
531+
if not os.path.isfile("hosts.txt"):
532+
targets(True, hostLimit)
533+
censysSearch.censysTargets(False, hostLimit)
534+
else:
535+
append = raw_input(
536+
"\n[" + t.magenta("?") + "]Append hosts to file or overwrite? [A/O]: ").lower()
537+
if append == 'a':
538+
targets(False, hostLimit)
539+
censysSearch.censysTargets(False, hostLimit)
540+
elif append == 'o':
541+
targets(True, hostLimit)
542+
censysSearch.censysTargets(False, hostLimit)
543+
else:
544+
print("\n[{}]Unhandled Option.".format(t.red("!")))
545+
546+
else:
547+
print("\n[{}]Unhandled Option.".format(t.red("!")))
548+
549+
513550
elif action == '3':
514551
if not os.path.isfile("hosts.txt"):
515552
import_custom(True)

censysSearch.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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))

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
shodan==1.7.7
22
blessings==1.6.1
3+
requests==2.18.4

0 commit comments

Comments
 (0)