forked from s0m30ne/pymsf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshodan.py
53 lines (50 loc) · 1.84 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
import sys
import os
import json
import requests
from Queue import Queue
import re
class Shodan(object):
def __init__(self):
self.API_URL = "https://api.shodan.io/shodan/host/search"
self.API_KEY = None
def getConfig(self):
if os.path.exists("config.txt"):
config = {}
fp = open("config.txt", "r")
for eachLine in fp:
if "=" in eachLine:
eachConfig = eachLine.split("=")
config[eachConfig[0].strip()] = eachConfig[1].strip()
self.API_KEY = config['API_KEY']
else:
print "you must config your API_KEY for the first time"
sys.exit()
def searchIP(self, query, pages, queue, STOP_ME):
if self.API_KEY == None:
print "please config your API_KEY"
sys.exit()
for page in range(1, pages+1):
try:
res = requests.get("%s?key=%s&query=%s&page=%s" % (self.API_URL, self.API_KEY, query, page))
except:
print "request error"
continue
else:
try:
results = res.json
if not isinstance(results, dict):
results = res.json()
except:
print "json decode error !"
continue
else:
if results.has_key("error"):
print "error occurred: %s" % results["error"]
sys.exit(1)
else:
result_iter = iter(results["matches"])
for result in result_iter:
if re.search(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", result["ip_str"]):
queue.put(result["ip_str"])
STOP_ME[0] = True