-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshodanscan.py
More file actions
executable file
·119 lines (101 loc) · 4.82 KB
/
Copy pathshodanscan.py
File metadata and controls
executable file
·119 lines (101 loc) · 4.82 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/python3
import requests
import json
import os
import signal
import argparse
import shodan
header = '''
███████╗██╗ ██╗ ██████╗ ██████╗ █████╗ ███╗ ██╗███████╗ ██████╗ █████╗ ███╗ ██╗
██╔════╝██║ ██║██╔═══██╗██╔══██╗██╔══██╗████╗ ██║██╔════╝██╔════╝██╔══██╗████╗ ██║
███████╗███████║██║ ██║██║ ██║███████║██╔██╗ ██║███████╗██║ ███████║██╔██╗ ██║
╚════██║██╔══██║██║ ██║██║ ██║██╔══██║██║╚██╗██║╚════██║██║ ██╔══██║██║╚██╗██║
███████║██║ ██║╚██████╔╝██████╔╝██║ ██║██║ ╚████║███████║╚██████╗██║ ██║██║ ╚████║
╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝ ╚═════╝╚═╝ ╚═╝╚═╝ ╚═══╝
'''
parser = argparse.ArgumentParser(description='Search a list of IP addresses across Shodan.io using the restful api')
parser.add_argument('-a', '--api', help='API key file', required=True)
parser.add_argument('-f', '--file', help='File containing list of IP addresses to search')
parser.add_argument('--single', help='Search a single IP')
parser.add_argument('-q', '--query', help='Perform shodan query on string')
parser.add_argument('-d', '--dns', nargs='?', help='Perform shodan dns query on specified domain or reverse dns on IPs in file specified with "-f"')
parser.add_argument('-o', '--outfile', help='File to write output to')
args = parser.parse_args()
def main():
ip_file=""
print(header)
if args.file:
ip_file = open(args.file, "r")
api_file = open(args.api, "r")
if not args.file and not args.single and not args.query and not args.dns:
parser.print_help()
exit()
SHODAN_API_KEY = api_file.readline().rstrip('\n')
api = shodan.Shodan(SHODAN_API_KEY)
search(ip_file, SHODAN_API_KEY)
def search(ip_file, SHODAN_API_KEY):
if args.dns and not args.file:
domain = args.dns
url = "https://api.shodan.io/dns/domain/" + domain + "?key=" + SHODAN_API_KEY
request = requests.get(url)
txt = request.text
print("\n" + args.dns)
parsed = json.loads(txt)
if args.outfile:
outfile = open(args.outfile, "w")
output = json.dumps(parsed, indent=2, sort_keys=True)
outfile.write(output)
else:
print(json.dumps(parsed, indent=2, sort_keys=True))
if args.query:
query = args.query
url = "https://api.shodan.io/shodan/host/search?key=" + SHODAN_API_KEY + "&query=" + query
request = requests.get(url)
txt = request.text
print("\n" + args.query)
parsed = json.loads(txt)
if args.outfile:
outfile = open(args.outfile, "w")
output = json.dumps(parsed, indent=2, sort_keys=True)
outfile.write(output)
else:
print(json.dumps(parsed, indent=2, sort_keys=True))
if args.single:
url = "https://api.shodan.io/shodan/host/" + args.single + "?key=" + SHODAN_API_KEY
request = requests.get(url)
txt = request.text
print("\n" + args.single)
parsed = json.loads(txt)
if args.outfile:
outfile = open(args.outfile, "w")
output = json.dumps(parsed, indent=2, sort_keys=True)
outfile.write(output)
else:
print(json.dumps(parsed, indent=2, sort_keys=True))
if args.file and args.dns is None:
for ip in ip_file:
url = "https://api.shodan.io/dns/reverse?ips=" + ip + "&key=" + SHODAN_API_KEY
request = requests.get(url)
txt = request.text
print("\n" + ip)
parsed = json.loads(txt)
if args.outfile:
outfile = open(args.outfile, "w")
output = json.dumps(parsed, indent=2, sort_keys=True)
outfile.write(output)
else:
print(json.dumps(parsed, indent=2, sort_keys=True))
if args.file and not args.dns:
for ip in ip_file:
url = "https://api.shodan.io/shodan/host/" + ip + "?key=" + SHODAN_API_KEY
request = requests.get(url)
txt = request.text
print("\n" + ip )
parsed = json.loads(txt)
if args.outfile:
outfile = open(args.outfile, "w")
output = json.dumps(parsed, indent=2, sort_keys=True)
outfile.write(output)
else:
print(json.dumps(parsed, indent=2, sort_keys=True))
main()