Skip to content

Commit da27f08

Browse files
authored
Merge pull request #69 from NullArray/updates
Updates the way the API key is loaded
2 parents 4c1783c + 778bcce commit da27f08

File tree

6 files changed

+130
-6
lines changed

6 files changed

+130
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ api.p
44
hosts.txt
55
secret.p
66
uid.p
7+
etc/tokens/*

api_calls/censys.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,20 @@
1111

1212
class CensysAPIHook(object):
1313

14+
"""
15+
Censys API hook
16+
"""
17+
1418
def __init__(self, identity, token, query):
1519
self.id = identity
1620
self.token = token
1721
self.query = query
1822
self.host_file = HOST_FILE
1923

2024
def censys(self):
25+
"""
26+
connect to the Censys API and pull all IP addresses from the provided query
27+
"""
2128
discovered_censys_hosts = set()
2229
try:
2330
req = requests.post(API_URLS["censys"], auth=(self.id, self.token), json={"query": self.query})

api_calls/shodan.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@
1313

1414
class ShodanAPIHook(object):
1515

16+
"""
17+
Shodan API hook, saves us from having to install another dependency
18+
"""
19+
1620
def __init__(self, token, query, proxy=None):
1721
self.token = token
1822
self.query = query
1923
self.proxy = proxy
2024
self.host_file = HOST_FILE
2125

2226
def shodan(self):
27+
"""
28+
connect to the API and grab all IP addresses associated with the provided query
29+
"""
2330
discovered_shodan_hosts = set()
2431
try:
2532
req = requests.get(API_URLS["shodan"].format(query=self.query, token=self.token))

api_calls/zoomeye.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515

1616
class ZoomEyeAPIHook(object):
1717

18+
"""
19+
API hook for the ZoomEye API, in order to connect you need to provide a phone number
20+
so we're going to use some 'lifted' credentials to login for us
21+
"""
22+
1823
def __init__(self, query):
1924
self.query = query
2025
self.host_file = HOST_FILE
@@ -23,6 +28,9 @@ def __init__(self, query):
2328

2429
@staticmethod
2530
def __decode(filepath):
31+
"""
32+
we all know what this does
33+
"""
2634
with open(filepath) as f:
2735
data = f.read()
2836
token, n = data.split(":")
@@ -31,6 +39,11 @@ def __decode(filepath):
3139
return token.strip()
3240

3341
def __get_auth(self):
42+
"""
43+
get the authorization for the authentication token, you have to login
44+
before you can access the API, this is where the 'lifted' creds come into
45+
play.
46+
"""
3447
username = self.__decode(self.user_file)
3548
password = self.__decode(self.pass_file)
3649
data = {"username": username, "password": password}
@@ -39,6 +52,10 @@ def __get_auth(self):
3952
return token
4053

4154
def zoomeye(self):
55+
"""
56+
connect to the API and pull all the IP addresses that are associated with the
57+
given query
58+
"""
4259
discovered_zoomeye_hosts = set()
4360
try:
4461
token = self.__get_auth()
@@ -48,8 +65,8 @@ def zoomeye(self):
4865
_json_data = req.json()
4966
for item in _json_data["matches"]:
5067
if len(item["ip"]) > 1:
51-
# TODO:/ grab all the IP addresses when there's more then 1
52-
discovered_zoomeye_hosts.add(str(item["ip"][0]))
68+
for ip in item["ip"]:
69+
discovered_zoomeye_hosts.add(ip)
5370
else:
5471
discovered_zoomeye_hosts.add(str(item["ip"][0]))
5572
write_to_file(discovered_zoomeye_hosts, self.host_file)

autosploit.py

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from lib.settings import (
3535
validate_ip_addr,
3636
check_services,
37+
load_api_keys,
3738
PLATFORM_PROMPT,
3839
AUTOSPLOIT_PROMPT,
3940
AUTOSPLOIT_TERM_OPTS
@@ -556,6 +557,63 @@ def try_shodan():
556557

557558
if __name__ == "__main__":
558559

560+
'''from api_calls import (
561+
shodan,
562+
censys,
563+
zoomeye
564+
)
565+
from lib.settings import (
566+
load_api_keys,
567+
API_URLS,
568+
AUTOSPLOIT_PROMPT
569+
)
570+
571+
from lib.output import (
572+
prompt,
573+
info,
574+
warning
575+
)
576+
577+
tokens = load_api_keys()
578+
579+
possible_apis = API_URLS.keys()
580+
581+
def get_query():
582+
query = prompt("enter your search query")
583+
return query
584+
585+
selected = False
586+
info_msg = "searching {} API with query '{}'"
587+
info("pick a search engine")
588+
for i, api in enumerate(sorted(possible_apis), start=1):
589+
print("{}. {}".format(i, api))
590+
591+
while not selected:
592+
choice = raw_input(AUTOSPLOIT_PROMPT)
593+
try:
594+
choice = int(choice)
595+
if choice == 1:
596+
selected = True
597+
query = get_query()
598+
info(info_msg.format("Shodan", query))
599+
censys.CensysAPIHook(tokens["censys"][1], tokens["censys"][0], query).censys()
600+
elif choice == 2:
601+
selected = True
602+
query = get_query()
603+
info(info_msg.format("Censys", query))
604+
shodan.ShodanAPIHook(tokens["shodan"][0], query).shodan()
605+
elif choice == 3:
606+
query = get_query()
607+
selected = True
608+
info("ZoomEye token will be loaded automatically")
609+
info(info_msg.format("Zoomeye", query))
610+
zoomeye.ZoomEyeAPIHook(query).zoomeye()
611+
else:
612+
warning("choice must be between 1-{}".format(len(API_URLS.keys())))
613+
except:
614+
warning("choice must be integer not string")'''
615+
616+
559617
logo()
560618

561619
if len(sys.argv) > 1:
@@ -605,20 +663,17 @@ def start_apache():
605663
start_ap = prompt("Start Apache Service? [Y]es/[N]o")
606664
if start_ap == 'y':
607665
start_apache()
608-
time.sleep(1.5)
609-
610666
elif start_ap == 'n':
611667
error("AutoSploit's MSF related operations require this service to be active.")
612668
error("Aborted.")
613-
time.sleep(1.5)
614669
sys.exit(0)
615670
else:
616671
warning("Unhandled Option. Defaulting to starting the service.")
617672
start_apache()
618-
time.sleep(1.5)
619673

620674
# We will check if the shodan api key has been saved before, if not we are going to prompt
621675
# for it and save it to a file
676+
# load_api_keys()
622677
if not os.path.isfile("api.p"):
623678
info("Please provide your Shodan.io API key.")
624679

lib/settings.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
import psutil
66

77
import lib.output
8+
import api_calls
89

910

1011
HOST_FILE = "{}/hosts.txt".format(os.getcwd())
1112
START_POSTGRESQL_PATH = "{}/etc/scripts/start_postgre.sh".format(os.getcwd())
1213
START_APACHE_PATH = "{}/etc/scripts/start_apache.sh".format(os.getcwd())
1314
PLATFORM_PROMPT = "\n{}@\033[36mPLATFORM\033[0m$ ".format(getpass.getuser())
1415
AUTOSPLOIT_PROMPT = "\n\033[31m{}\033[0m@\033[36mautosploit\033[0m# ".format(getpass.getuser())
16+
API_KEYS = {
17+
"censys": ("{}/etc/tokens/censys.key".format(os.getcwd()), "{}/etc/tokens/censys.id".format(os.getcwd())),
18+
"shodan": ("{}/etc/tokens/shodan.key".format(os.getcwd()), )
19+
}
1520
API_URLS = {
1621
"shodan": "https://api.shodan.io/shodan/host/search?key={token}&query={query}",
1722
"censys": "https://censys.io/api/v1/search/ipv4",
@@ -73,3 +78,35 @@ def write_to_file(data_to_write, filename, mode="a+"):
7378
log.write(data_to_write)
7479
lib.output.info("successfully wrote info to '{}'".format(filename))
7580
return filename
81+
82+
83+
def load_api_keys(path="{}/etc/tokens".format(os.getcwd())):
84+
85+
"""
86+
load the API keys from their .key files
87+
"""
88+
89+
def makedir(dir):
90+
"""
91+
make the directory if it does not exist
92+
"""
93+
if not os.path.exists(dir):
94+
os.mkdir(dir)
95+
96+
makedir(path)
97+
for key in API_KEYS.keys():
98+
if not os.path.isfile(API_KEYS[key][0]):
99+
access_token = lib.output.prompt("enter your {} API token".format(key.title()), lowercase=False)
100+
if key.lower() == "censys":
101+
identity = lib.output.prompt("enter your {} ID".format(key.title()), lowercase=False)
102+
with open(API_KEYS[key][1], "a+") as log:
103+
log.write(identity)
104+
with open(API_KEYS[key][0], "a+") as log:
105+
log.write(access_token.strip())
106+
else:
107+
lib.output.info("{} API token loaded from {}".format(key.title(), API_KEYS[key][0]))
108+
api_tokens = {
109+
"censys": (open(API_KEYS["censys"][0]).read(), open(API_KEYS["censys"][1]).read()),
110+
"shodan": (open(API_KEYS["shodan"][0]).read(), )
111+
}
112+
return api_tokens

0 commit comments

Comments
 (0)