-
Notifications
You must be signed in to change notification settings - Fork 0
/
CertEx.py
121 lines (92 loc) · 2.63 KB
/
CertEx.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
#!/usr/bin/env python
LOGFILE = "LOGFILE"
TMPCERTFILE = "tmp.pem"
import os
from optparse import OptionParser
import subprocess
def getFileEndings(p="."):
fel = []
tmp = ""
for e in os.walk(p, True):
for ending in e[2]:
tmp = ending.rpartition(".")[2]
if not(tmp in fel):
fel.append(tmp)
return fel
def createLogfile():
file = open(LOGFILE, 'w+')
return file
def createCertFile(dir = "."):
if not(os.path.isdir(dir)):
os.mkdir(dir)
file = open(dir + "/" + TMPCERTFILE, 'w+')
return file
def createOutPutFIle(p):
datei = open("./" + p + ".txt", "w")
return datei
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def parseDigResultToKey(digResult):
foundSome = False;
digResult = digResult.rpartition("CERT")[2]
digResult = digResult.partition(url)[0]
digResult = digResult.partition(";")[0]
digResult = digResult.split()
key = ""
key2 = ""
for str in digResult:
if(len(str)>10):
foundSome = True
key +=str
else:
if(foundSome):
if(len(str) > 7):
key +=str
break
counter = 0
for c in key:
key2 += c
if(counter == 63):
key2 += "\n"
counter = 0
else:
counter += 1
key3 = "-----BEGIN CERTIFICATE----- \n"
key3 += key2
key3 += "\n-----END CERTIFICATE-----"
return key3
#!/usr/bin/env python
parser = OptionParser()
parser.add_option("-p", "--path", dest="path", help="path u want tu save the digResult")
parser.add_option("-d", "--domain", dest="domain", help="domain u want to scan")
(optionen, args) = parser.parse_args()
PATH = optionen.path
domain = optionen.domain
if (domain is None or len(url) == 0): # check if domain is given
parser.error('domain is required')
logfile = createLogfile()
digResult = subprocess.check_output(["dig", "any" , domain])
logfile.write(digResult)
logfile.close()
key = parseDigResultToKey(digResult)
f = createCertFile()
f.write(key)
f.close()
tmpcert = ""
try:
tmpcert = subprocess.check_output(["openssl", "x509", "-text", "-in", TMPCERTFILE])
except subprocess.CalledProcessError, e:
print"Keiner Zertifikat gefunden... Falls Sie sich sicher sind, dass ein valides Zertifikat existiert."
#os.system("rm" + tmpcert)
os.remove(TMPCERTFILE)
if(len(tmpcert) != 0):
if(PATH is None):
print(tmpcert)
else:
f = createOutPutFIle(PATH)
f.write(tmpcert)
f.close()