-
Notifications
You must be signed in to change notification settings - Fork 2
/
censys_subdomain_finder.py
190 lines (167 loc) · 7.65 KB
/
censys_subdomain_finder.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import requests
import censysparser
from useragents import UserAgents
import argparse
try:
import simplejson as json
except ImportError:
import json
import socket
class SearchNonApiCensys(object):
def __init__(self, domain, limit):
self.domain = self.cleanDomain(domain)
self.urlhost = ""
self.urlcert = ""
self.page = ""
self.resultshosts = ""
self.resultcerts = ""
self.total_resultshosts = ""
self.total_resultscerts = ""
self.server = 'censys.io'
self.ips = []
self.hostnamesall = []
self.limit = limit
self.ua = UserAgents()
def cleanDomain(self, domain):
"""
@remove wwww.
"""
# TODO: this is primitive way of getting rid of the protocol
# needs a more advanced way.
if("www." in domain):
domain = domain.split(".")
if(len(domain) > 2):
domain.pop(0)
return '.'.join(domain)
return domain
def do_searchhosturl(self):
try:
headers = {'user-agent': self.ua.get_user_agent(), '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(f'Error occurred in the Censys module downloading pages from Censys - IP search: + {e}')
def do_searchcertificateurl(self):
try:
headers = {'user-agent': self.ua.get_user_agent(), 'Accept': '*/*', 'Referer': self.urlcert}
responsecert = requests.get(self.urlcert, headers=headers)
self.resultcerts = responsecert.text
self.total_resultscerts += self.resultcerts
except Exception as e:
print(f'Error occurred in the Censys module downloading pages from Censys - certificates search: {e}')
def process(self):
try:
self.urlhost = 'https://' + self.server + '/ipv4/_search?q=' + str(self.domain) + '&page=1'
self.urlcert = 'https://' + self.server + '/certificates/_search?q=' + str(self.domain) + '&page=1'
self.do_searchhosturl()
self.do_searchcertificateurl()
counter = 2
pages = censysparser.Parser(self)
totalpages = pages.search_totalpageshosts()
pagestosearch = int(self.limit / 25) # 25 results/page
if totalpages is None:
totalpages = 0
if totalpages <= pagestosearch:
while counter <= totalpages:
try:
self.page = str(counter)
self.urlhost = 'https://' + self.server + '/ipv4/_search?q=' + str(self.domain) + '&page=' + str(
self.page)
print('\tSearching IP results page [{0}]'.format(self.page))
self.do_searchhosturl()
counter += 1
except Exception as e:
print(f'Error occurred in the Censys module requesting the pages: {e}')
else:
while counter <= pagestosearch:
try:
self.page = str(counter)
self.urlhost = 'https://' + self.server + '/ipv4/_search?q=' + str(self.domain) + '&page=' + str(
self.page)
print(f'\tSearching results page {self.page}.')
self.do_searchhosturl()
counter += 1
except Exception as e:
print(f'Error occurred in the Censys module requesting the pages: {e}')
counter = 2
totalpages = pages.search_totalpagescerts()
if totalpages is None:
totalpages = 0
if totalpages <= pagestosearch:
while counter <= totalpages:
try:
self.page = str(counter)
self.urlhost = 'https://' + self.server + '/certificates/_search?q=' + str(
self.domain) + '&page=' + str(self.page)
print(f'\tSearching certificates results page {self.page}.')
self.do_searchcertificateurl()
counter += 1
except Exception as e:
print(f'Error occurred in the Censys module requesting the pages: {e}')
else:
while counter <= pagestosearch:
try:
self.page = str(counter)
self.urlhost = 'https://' + self.server + '/ipv4/_search?q=' + str(self.domain) + '&page=' + str(
self.page)
print('\tSearching IP results page ' + self.page + '.')
self.do_searchhosturl()
counter += 1
except Exception as e:
print(f'Error occurred in the Censys module requesting the pages: {e}')
# Space in terminal display
print("\n")
except Exception as e:
print(f'Error occurred in the main Censys module: {e}')
def get_subdomains(self):
try:
ips = self.get_ipaddresses()
headers = {'user-agent': self.ua.get_user_agent(), 'Accept': '*/*', 'Referer': self.urlcert}
response = requests.post('https://censys.io/ipv4/getdns', json={'ips': ips}, headers=headers)
responsejson = response.json()
domainsfromcensys = []
for key, jdata in responsejson.items():
if jdata is not None:
domainsfromcensys.append(jdata)
else:
pass
matchingdomains = [s for s in domainsfromcensys if str(self.domain) in s]
self.hostnamesall.extend(matchingdomains)
hostnamesfromcerts = censysparser.Parser(self)
self.hostnamesall.extend(hostnamesfromcerts.search_hostnamesfromcerts())
subdomains = list(set(self.hostnamesall)) # Filters unique values
subdomain_dict = []
for sub in subdomains:
subdomain_dict.append({
"subdomain":sub,
"ip":self.resolve_ip(sub),
"domain":self.domain
})
return subdomain_dict
except Exception as e:
print(f'Error occurred in the Censys module - hostname search: {e}')
def get_ipaddresses(self):
try:
ips = censysparser.Parser(self)
self.ips = ips.search_ipaddresses()
return self.ips
except Exception as e:
print(f'Error occurred in the main Censys module - IP address search: {e}')
def resolve_ip(self, host):
"""
@ given the host return the IP
"""
try:
return socket.gethostbyname(host)
except socket.gaierror:
return "404"
if __name__=="__main__":
parser = argparse.ArgumentParser(prog="None censys subdomain finder")
parser.add_argument('-d', '--d', help='Help', required=True)
parser.add_argument('-l', '--l', help='Limit', type=int, required=False, default=500)
args = parser.parse_args()
cen = SearchNonApiCensys(args.d, args.l)
cen.process()
subdomains = cen.get_subdomains()
print(json.dumps(subdomains, indent=4, sort_keys=True))