-
Notifications
You must be signed in to change notification settings - Fork 5
/
certlookup.py
289 lines (235 loc) · 7.19 KB
/
certlookup.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/python
'''
certlookup.py
Copyright 2011 Xavier Mendez ([email protected])
reportIP function shamesly copied from: http://phreakmonkey.com/index.php/archives/100
certlookup is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.
certlookup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
'''
import sys
import socket
import getopt
from xml.dom import minidom
try:
from M2Crypto import SSL
except ImportError:
sys.stderr.write('You need M2Crypto module')
sys.stderr.flush()
sys.exit(2)
class MyException(Exception):
pass
class CertLookup:
def __init__(self, ip_list, verbose = False):
self.ip_list = ip_list
self.len_list = len(ip_list)
self.current = 0
self.verbose = verbose
def __iter__(self):
return self
def next(self):
if self.current >= self.len_list:
raise StopIteration
else:
host, port = self.ip_list[self.current]
ip = self.verify_ip(host)
self.current += 1
ns = None
try:
ns = self.resolve_cert(self.reportIP(ip, int(port)), ip)
except MyException,e:
sys.stderr.write('Error: %s\n' % e)
sys.stderr.flush()
else:
return (ns, ip)
def reportIP(self, IPaddress, port):
"""
Shamesly copied from: http://phreakmonkey.com/index.php/archives/100
A quick hack to probe for SSL Certificate Information
Requres python & m2crypto
2007.09.26 - phreakmonkey.com / phreakmonkey at gmail
"""
ctx = SSL.Context()
ctx.set_allow_unknown_ca(True)
ctx.set_verify(SSL.verify_none, 1)
conn = SSL.Connection(ctx)
conn.postConnectionCheck = None
timeout = SSL.timeout(15)
conn.set_socket_read_timeout(timeout)
conn.set_socket_write_timeout(timeout)
try:
if self.verbose:
sys.stderr.write('Connecting '+IPaddress+'.\n')
sys.stderr.flush()
conn.connect((IPaddress, port))
except KeyboardInterrupt:
raise KeyboardInterrupt
except TypeError:
raise MyException('connect: Malformed address specified')
except socket.gaierror:
raise MyException('connect: No address associated with hostname')
except socket.error:
raise MyException('timeout connecting with hostname.')
except SSL.SSLError:
raise MyException('SSL_HANDSHAKE_FAILED.')
if self.verbose:
sys.stderr.write('Retrieving cert info.\n')
sys.stderr.flush()
cert = conn.get_peer_cert()
try:
cissuer = cert.get_issuer().as_text()
except SSL.SSLError:
raise MyException('Error: No Valid Cert Presented.')
conn.close
return cert
def resolve_cert(self, cert, ip):
cCN = ""
try:
cCN = cert.get_subject().CN
if self.verbose:
sys.stderr.write('Obtained cert\'s CN attribute: %s\n' % cCN)
sys.stderr.flush()
except AttributeError:
raise MyException('Error: Cert without CN attribute.')
try:
resolving = socket.gethostbyname(cCN)
if self.verbose:
sys.stderr.write('Looking up CN name, result: %s\n' % resolving)
sys.stderr.flush()
if resolving == ip:
return cCN.lower()
else:
if self.verbose:
sys.stderr.write('Error: Cert not issued for this IP\n')
sys.stderr.flush()
return None
except socket.gaierror:
raise MyException('DNS Error: No address associated with hostname %s.' % cCN)
return None
def verify_ip(self, ip):
try:
socket.inet_aton(ip)
except socket.error:
raise MyException("You must supply a valid IP address")
return ip
@staticmethod
def to_magictree(hostlist):
def create_xml_element(parent, caption, text):
# Create a <xxx> element
doc = minidom.Document()
el = doc.createElement(caption)
parent.appendChild(el)
# Give the <xxx> element some text
ptext = doc.createTextNode(text)
el.appendChild(ptext)
return el
doc = minidom.Document()
#<magictree class="MtBranchObject">
node_mt = doc.createElement("magictree")
node_mt.setAttribute("class", "MtBranchObject")
#<testdata class="MtBranchObject">
node_td = doc.createElement("testdata")
node_td.setAttribute("class", "MtBranchObject")
node_mt.appendChild(node_td)
#<host>209.85.146.105
for cn, ip in hostlist:
node_h = create_xml_element(node_td, "host", str(ip))
create_xml_element(node_h, "hostname", str(cn))
return node_mt
@staticmethod
def about():
print """
certlookup - is a tool for performing reverse IP lookups interrogating SSL servers for certificate's CN attribute.
Although is normally used with command-line arguments, it also has a magic tree mode of operation for reading lookup
requests from a file and outputing in XML.
Coded by Xavier Mendez aka Javi ([email protected])
"""
def main():
def usage():
print """
Usage: certlookup.py [--help] [--verbose] -h <ip> [-i=$in] [-p <port>] [--mtree=$out]
Examples:
$ python certlookup.py -h 69.58.181.89
www.verisign.com (69.58.181.89)
$ echo -e "69.58.181.89\\t443" > /tmp/a
$ python certlookup.py -i /tmp/a
www.verisign.com (69.58.181.89)
MagicTree integration:
certlookup.py -i=$i --mtree=$out
The input file must be a list of: ip<TAB>port
"""
sys.exit(0)
# command line options variables
verbose = False
host = ""
port = 443
mtree_file_name = None
input_file_name = None
# get command line options
try:
opts, args = getopt.getopt(sys.argv[1:], "vh:p:i:", ["help","verbose","mtree="])
except getopt.GetoptError, err:
usage()
for o, a in opts:
if o in ("-v", "--verbose"):
verbose = True
elif o in ("-i",):
input_file_name = a
elif o in ("--mtree",):
mtree_file_name = a
elif o in ("--help",):
CertLookup.about()
sys.exit(0)
elif o in ("-h",):
host = a
elif o in ("-p",):
port = int(a)
else:
assert False, "unhandled option"
if len(sys.argv) < 2 or (host=="" and input_file_name is None):
usage()
# file or -h input
l = []
try:
if input_file_name is not None:
ips = [i.split('\t') for i in open(input_file_name, 'r')]
else:
ips = [[host, port]]
for ns, ip in CertLookup(ips, verbose):
print "%s (%s)" % (ns, ip)
l.append((ns, ip))
except IOError:
sys.stderr.write('Error: reading input list\n')
sys.stderr.flush()
sys.exit(2)
except KeyboardInterrupt:
sys.exit(2)
except ValueError:
sys.stderr.write('Error: Incorrect input list format. It should be: ip<tab>port\n')
sys.stderr.flush()
except MyException,e:
sys.stderr.write('Error: %s\n' % e)
sys.stderr.flush()
except Exception,e:
sys.stderr.write('Unhandled exception!')
sys.stderr.flush()
sys.exit(2)
# magictree output
if mtree_file_name is not None:
f = None
try:
f = open(mtree_file_name, 'w')
CertLookup.to_magictree(l).writexml(f)
except IOError:
sys.stderr.write('Error: writing magictree XML file.\n')
sys.stderr.flush()
sys.exit(2)
finally:
if f: f.close()
if __name__ == "__main__":
main()