-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathshodan.py
More file actions
70 lines (57 loc) · 2.4 KB
/
shodan.py
File metadata and controls
70 lines (57 loc) · 2.4 KB
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
60
61
62
63
64
65
66
67
68
69
# -*- coding: UTF-8 -*-
"""Bot dedicated to Shodan.
"""
from collections import OrderedDict
from ...apis import ShodanAPI
__all__ = ["ShodanBot"]
def reformat(response, hosts=None):
hosts = hosts or {}
for host in response.get('matches', [response]):
ip = host['ip_str']
service = OrderedDict()
service['Port'] = "{}/{}".format(host['transport'], host['port'])
service['Name'] = "{} {}".format(host.get('product', ""), host.get('version', "")).strip()
service['CPE'] = ", ".join(host.get('cpe', []))
if ip in hosts.keys():
hosts[ip]['Services'].append(service)
else:
hosts[ip] = OrderedDict()
hosts[ip]['Location'] = "{} ({})".format(host['location'].get('city') or "?",
host['location'].get('country_code'))
hosts[ip]['Organization'] = host['org']
hosts[ip]['ISP'] = host['isp']
hosts[ip]['Last Update'] = host['timestamp']
hosts[ip]['Hostnames'] = ", ".join(host['hostnames'])
hosts[ip]['ASN'] = host['asn']
hosts[ip]['Services'] = [service]
for data in hosts.values():
data['Services'] = sorted(data['Services'], key=lambda x: int(x['Port'].split("/")[-1]))
return hosts
class ShodanBot(ShodanAPI):
"""
Class for requesting multiple information using the Shodan API.
:param apikey: API key
:param args: JSONBot / API arguments
:param kwargs: JSONBot keyword-arguments
"""
def hosts_from_file(self, ips_path):
"""
Check a list of IP addresses from a given file.
:param ips_path: path to the file with the list of IP addresses or networks (in CIDR notation)
:return: dictionary of hosts found on Shodan
"""
found = {}
with open(ips_path) as f:
for ip in f:
found.update(reformat(self.shodan.host.search("net:%s" % ip.strip()), found))
return found
def hosts_from_list(self, *ips):
"""
Check a list of IP addresses from the given arguments.
:param ips: list of IP addresses or networks (in CIDR notation)
:return: dictionary of hosts found on Shodan
"""
found = {}
for ip in ips:
found.update(reformat(self.shodan.host.search("net:%s" % ip.strip()), found))
return found