-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShodan.py
59 lines (50 loc) Β· 1.85 KB
/
Shodan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from shodan import Shodan as _Shodan
from shodan import APIError
from json import load
import logging
from Logging import log_setup
class Shodan:
log = log_setup("Shodan")
logger = logging.getLogger("urllib3.connectionpool")
logger.setLevel(logging.ERROR)
def __init__(self, host=""):
self.host = host
self.api = None
self.error = None
self.cancel = None
self.get_api()
def get_api(self):
# If user didn't pass API key from the command line arguments, search in config.config file.
with open("config.config", "r") as f:
api = load(f).get("api")
try:
self.api = _Shodan(api)
except APIError as e:
if str(e) == "Invalid API key":
Shodan.log.error("Passed invalid Shodan API key.")
self.error = True
if not self.api:
self.cancel, self.error = True, True
def get_hostnames(self):
try:
ip_info = self.api.host(self.host)
return ip_info.get("hostnames")
except APIError as e:
if str(e) == "Invalid API key":
Shodan.log.error("Passed invalid Shodan API key.")
self.error = True
def get_vulns(self):
try:
ip_info = self.api.host(self.host)
return ip_info.get("vulns")
except APIError as e:
if str(e) == "Invalid API key":
Shodan.log.error("Passed invalid Shodan API key.")
self.error = True
def stream(self):
try:
for banner in self.api.stream.ports([3306, 27017, 9200, 9042]):
host, port = banner.get("ip_str"), banner.get("port")
yield host, port
except Exception as e:
Shodan.log.error(f"Shodan Stream: SOMETHING WENT WRONG.\n{e}\n")