-
Notifications
You must be signed in to change notification settings - Fork 19
/
censys.py
70 lines (66 loc) · 2.34 KB
/
censys.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
60
61
62
63
64
65
66
67
68
69
70
import sys
import os
import json
import requests
import time
import math
from Queue import Queue
import re
class Censys(object):
def __init__(self):
self.API_URL = "https://www.censys.io/api/v1"
self.UID = None
self.SECRET = 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.UID = config['UID']
self.SECRET = config['SECRET']
else:
print "you must config your UID and SECRET for the first time"
sys.exit()
def searchIP(self, query, pages, queue, STOP_ME):
if self.UID == None or self.SECRET == None:
print "please config your UID and SECRET"
sys.exit()
for page in range(1, pages+1):
start_time = time.time()
data = {
"query":query,
"page":page,
"fields":["ip", "autonomous_system.name"]
}
try:
res = requests.post(self.API_URL + "/search/ipv4", data=json.dumps(data), auth=(self.UID, self.SECRET))
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["results"])
for result in result_iter:
if re.search(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", result["ip"]):
queue.put(result["ip"])
end_time = time.time()
used_time = end_time - start_time
if used_time > 2.5:
pass
else:
time.sleep(math.ceil(used_time))
STOP_ME[0] = True