2018-11-03 07:04:20 +08:00
|
|
|
import requests
|
2018-12-28 08:49:56 +08:00
|
|
|
from parsers import censysparser
|
2018-12-18 13:21:05 +08:00
|
|
|
from discovery.constants import *
|
2018-12-16 11:07:37 +08:00
|
|
|
|
2018-12-28 08:49:56 +08:00
|
|
|
|
2018-11-03 07:04:20 +08:00
|
|
|
class search_censys:
|
|
|
|
|
2018-12-16 11:07:37 +08:00
|
|
|
def __init__(self, word):
|
2018-11-03 07:04:20 +08:00
|
|
|
self.word = word
|
2018-12-23 05:49:59 +08:00
|
|
|
self.urlhost = ""
|
|
|
|
self.urlcert = ""
|
2018-12-16 11:07:37 +08:00
|
|
|
self.page = ""
|
2018-12-23 05:49:59 +08:00
|
|
|
self.resultshosts = ""
|
|
|
|
self.resultcerts = ""
|
|
|
|
self.total_resultshosts = ""
|
|
|
|
self.total_resultscerts = ""
|
2018-12-16 11:07:37 +08:00
|
|
|
self.server = "censys.io"
|
2018-12-23 05:49:59 +08:00
|
|
|
self.ips = []
|
|
|
|
self.hostnamesall = []
|
|
|
|
|
|
|
|
def do_searchhosturl(self):
|
|
|
|
try:
|
|
|
|
headers = {'user-agent': getUserAgent(), 'Accept':'*/*','Referer': self.urlhost}
|
|
|
|
responsehost = requests.get(self.urlhost, headers=headers)
|
|
|
|
self.resultshosts = responsehost.text
|
|
|
|
self.total_resultshosts += self.resultshosts
|
|
|
|
except Exception as e:
|
|
|
|
print("Error occurred in the Censys module downloading pages from Censys - IP search: " + str(e))
|
2018-12-16 11:07:37 +08:00
|
|
|
|
2018-12-23 05:49:59 +08:00
|
|
|
def do_searchcertificateurl(self):
|
2018-11-03 07:04:20 +08:00
|
|
|
try:
|
2018-12-23 05:49:59 +08:00
|
|
|
headers = {'user-agent': getUserAgent(), 'Accept':'*/*','Referer': self.urlcert}
|
|
|
|
responsecert = requests.get(self.urlcert, headers=headers)
|
|
|
|
self.resultcerts = responsecert.text
|
|
|
|
self.total_resultscerts += self.resultcerts
|
2018-11-23 05:20:06 +08:00
|
|
|
except Exception as e:
|
2018-12-23 05:49:59 +08:00
|
|
|
print("Error occurred in the Censys module downloading pages from Censys - certificates search: " + str(e))
|
2018-11-03 07:04:20 +08:00
|
|
|
|
2018-12-07 00:32:25 +08:00
|
|
|
def process(self):
|
2018-12-23 05:49:59 +08:00
|
|
|
try:
|
|
|
|
self.urlhost = "https://" + self.server + "/ipv4/_search?q=" + str(self.word) + "&page=1"
|
|
|
|
self.urlcert = "https://"+ self.server + "/certificates/_search?q=" + str(self.word) + "&page=1"
|
|
|
|
self.do_searchhosturl()
|
|
|
|
self.do_searchcertificateurl()
|
|
|
|
counter = 2
|
|
|
|
pages = censysparser.parser(self)
|
|
|
|
totalpages = pages.search_numberofpageshosts()
|
|
|
|
while counter <= totalpages:
|
|
|
|
try:
|
2018-12-28 08:49:56 +08:00
|
|
|
self.page = str(counter)
|
2018-12-23 05:49:59 +08:00
|
|
|
self.urlhost = "https://" + self.server + "/ipv4/_search?q=" + str(self.word) + "&page=" + str(self.page)
|
|
|
|
print("\tSearching Censys IP results page " + self.page + "...")
|
|
|
|
self.do_searchhosturl()
|
2018-12-28 08:49:56 +08:00
|
|
|
counter += 1
|
2018-12-23 05:49:59 +08:00
|
|
|
except Exception as e:
|
|
|
|
print("Error occurred in the Censys module requesting the pages: " + str(e))
|
|
|
|
counter = 2
|
|
|
|
totalpages = pages.search_numberofpagescerts()
|
|
|
|
while counter <= totalpages:
|
|
|
|
try:
|
|
|
|
self.page = str(counter)
|
|
|
|
self.urlhost = "https://" + self.server + "/certificates/_search?q=" + str(self.word) + "&page=" + str(self.page)
|
|
|
|
print("\tSearching Censys certificates results page " + self.page + "...")
|
|
|
|
self.do_searchcertificateurl()
|
|
|
|
counter += 1
|
|
|
|
except Exception as e:
|
|
|
|
print("Error occurred in the Censys module requesting the pages: " + str(e))
|
|
|
|
except Exception as e:
|
|
|
|
print("Error occurred in the main Censys module: " + str(e))
|
2018-11-03 07:04:20 +08:00
|
|
|
|
|
|
|
def get_hostnames(self):
|
|
|
|
try:
|
2018-12-23 05:49:59 +08:00
|
|
|
ips = self.get_ipaddresses()
|
|
|
|
headers = {'user-agent': getUserAgent(), 'Accept':'*/*','Referer': self.urlcert}
|
|
|
|
response = requests.post("https://censys.io/ipv4/getdns", json={"ips": ips}, headers=headers)
|
|
|
|
responsejson = response.json()
|
|
|
|
for key, jdata in responsejson.items():
|
|
|
|
if jdata is not None:
|
|
|
|
self.hostnamesall.append(jdata)
|
|
|
|
else:
|
|
|
|
pass
|
|
|
|
hostnamesfromcerts = censysparser.parser(self)
|
|
|
|
self.hostnamesall.extend(hostnamesfromcerts.search_hostnamesfromcerts())
|
|
|
|
return self.hostnamesall
|
2018-11-23 05:20:06 +08:00
|
|
|
except Exception as e:
|
2018-12-23 05:49:59 +08:00
|
|
|
print("Error occurred in the Censys module - hostname search: " + str(e))
|
2018-11-03 07:04:20 +08:00
|
|
|
|
|
|
|
def get_ipaddresses(self):
|
|
|
|
try:
|
|
|
|
ips = censysparser.parser(self)
|
2018-12-23 05:49:59 +08:00
|
|
|
self.ips = ips.search_ipaddresses()
|
|
|
|
return self.ips
|
2018-11-23 05:20:06 +08:00
|
|
|
except Exception as e:
|
2018-12-23 05:49:59 +08:00
|
|
|
print("Error occurred in the main Censys module - IP address search: " + str(e))
|
|
|
|
|