47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
import requests
|
|
|
|
class Censys:
|
|
def __init__(self, uid, secret):
|
|
self.url = 'https://censys.io/api/v1'
|
|
self.uid = uid
|
|
self.secret = secret
|
|
self.login()
|
|
self.ipv4 = []
|
|
|
|
def login(self):
|
|
r = requests.get(self.url + "/data", auth=(self.uid, self.secret))
|
|
if r.status_code != 200:
|
|
print("Wrong creds for Censys")
|
|
sys.exit(1)
|
|
return True
|
|
|
|
def build_query_ipv4(self, targets):
|
|
query = ""
|
|
for t in targets:
|
|
query += "ip:[" + t['start'] + " TO " + t['end'] + "]"
|
|
query += " OR "
|
|
return query[:-4]
|
|
|
|
def search_ipv4(self, query):
|
|
r = requests.post(self.url + "/search/ipv4", json={'query': query}, auth=(self.uid, self.secret))
|
|
data = r.json()
|
|
self.parse_ipv4(data)
|
|
if data['status'] == 'ok':
|
|
count = data['metadata']['count']
|
|
pages = data['metadata']['pages']
|
|
for page in range(2, pages + 1):
|
|
r = requests.post(self.url + "/search/ipv4", json={'query': query, 'page' : page}, auth=(self.uid, self.secret))
|
|
data = r.json()
|
|
self.parse_ipv4(data)
|
|
return self.ipv4
|
|
|
|
def parse_ipv4(self, data):
|
|
for host in data['results']:
|
|
r = requests.get(self.url + "/view/ipv4/" + host['ip'], auth=(self.uid, self.secret))
|
|
data = r.json()
|
|
try:
|
|
vhosts = data['443']['https']['tls']['certificate']['parsed']['names']
|
|
except:
|
|
vhosts = []
|
|
self.ipv4.append({'ip': host['ip'], 'protocols': host['protocols'], 'vhosts': vhosts})
|
|
return True |