-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMLextract.py
209 lines (168 loc) · 6.62 KB
/
MLextract.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
#!/usr/bin/env python
import os
import sys
import base64
import subprocess
import re
import glob
fingerprints = [re]
totalCerts = 0
certNr = 1
# First, make sure that the version of openssl we are using supports the cms command
# the version supplied with OS X doesn't - you would need to get a different one in this case
# e.g. through Homebrew
opensslbin = "/usr/local/Cellar/[email protected]/1.1.1h/bin/openssl"
def main( filename ):
global certNr
out, err = execute( opensslbin + " cms" )
if err.decode().find( "'cms' is an invalid command" ) != -1:
print( "The version of OpenSSL you are using doesn't support the CMS command" )
print( "You need to get a version that does (e.g. from Homebrew)" )
exit( 1 )
# remove old master list
if os.path.exists( "masterList.pem" ):
os.remove( "masterList.pem" )
# Get a list of all the file paths that ends with .txt from in specified directory
fileList = glob.glob('*.pem')
# Iterate over the list of filepaths & remove each file.
for filePath in fileList:
try:
os.remove(filePath)
except:
print("Error while deleting file : ", filePath)
# Identify Type of file - either LDIF or MasterList (CMS)
if filename.lower().endswith( ".ldif" ):
# Read and parse LDIF File
masterLists = readAndExtractLDIFFile( filename )
elif filename.lower().endswith( ".ml" ):
masterLists = readInMasterListFile( filename )
print( f"Read in {len(masterLists)} masterlist files" )
for index, ml in enumerate(masterLists):
certNr = 1
print( "-----------------------------------" )
print( f"Verifying and extracting MasterList {index}")
extractCertsFromMasterlist( ml )
print( "====================================" )
print( f"Created MasterList.pem containing {totalCerts} certificates")
print( f"Created {totalCerts} files including self signed CSCA and linked certificates")
def readAndExtractLDIFFile( file ):
adding = False
certs = []
with open(file, "r") as inf:
for line in inf:
if line.startswith( "CscaMasterListData:: "):
cert = line[21:]
adding = True
elif not line.startswith(" ") and adding == True:
adding = False
certs.append( cert )
cert = ""
elif adding == True:
cert += line
if cert != "":
certs.append( cert )
print( f"Read {len(certs)} certs" )
masterLists = []
for index, cert in enumerate(certs):
data = base64.b64decode(cert)
masterLists.append( data )
return masterLists
def readInMasterListFile( file ):
with open(file, "rb") as inf:
data = inf.read()
return [data]
def extractCertsFromMasterlist( masterList ):
global totalCerts
# Run openssl cms to verify and extract the signed data
cmd = f"{opensslbin} cms -inform der -noverify -verify"
(signedData, err) = execute( cmd, masterList )
if err.decode("utf8").strip() != "Verification successful":
print( f"[{err.decode('utf8')}]" )
raise Exception( "Verification of Masterlist data failed" )
print( "MasterList Verification successful" )
certList = extractPEMCertificates( signedData )
print( "Removing duplicates")
uniqueCerts = [x for x in certList if uniqueHash(x)]
print( f"Removed {len(certList)-len(uniqueCerts)} duplicate certificates")
totalCerts += len(uniqueCerts)
# Append to masterList.pem
with open("masterList.pem", "ab") as f:
for c in uniqueCerts:
f.write(c)
# Write each cert to directory
(CN,err) = execute( f"{opensslbin} x509 -noout -issuer", c )
certfilename = CN.decode("utf8").strip().replace("/", "-")
i = 0
while os.path.exists("{certfilename}.pem"):
i += 1
certfilename = "{CN.decode('utf8').strip().replace('/', '-')-str(i)}"
with open( f"{certfilename}.pem", "wb") as certfile:
print( f"Saving certificate {certfilename}.pem", end="\r" )
certfile.write(c)
certfile.close()
f.close()
def extractPEMCertificates( signedData ):
global certNr
print( "Extracting all certificates from payload" )
cmd = f"{opensslbin} asn1parse -inform der -i"
(data, err) = execute( cmd, signedData )
lines = data.decode("utf8").strip().split( "\n" )
valid = False
certs = []
certCount = len([i for i in lines if "d=2" in i])
for line in lines:
if re.search( r":d=1", line ):
valid = False
if re.search( r"d=1.*SET", line ):
valid = True
if re.search( r"d=2", line ) and valid:
# Parse line
match = re.search( r"^ *([0-9]*).*hl= *([0-9]*).*l= *([0-9]*).*", line)
if match:
print( f"Extracting cert {certNr} of {certCount}", end="\r" )
certNr += 1
offset = int(match.group(1))
header_len = int(match.group(2))
octet_len = int(match.group(3))
# Extract PEM certificate
data = signedData[offset : offset+header_len+octet_len]
(cert,err) = execute( f"{opensslbin} x509 -inform der -outform pem", data )
certs.append(cert)
else:
print( "Failed match")
print( f"\nExtracted {len(certs)} certs")
return certs
def uniqueHash( cert ):
(data,err) = execute( f"{opensslbin} x509 -hash -fingerprint -inform PEM -noout", cert )
items = data.decode("utf8").split("\n")
hash = items[0].strip()
fingerprint = items[1].strip()
if fingerprint not in fingerprints:
fingerprints.append( fingerprint )
return True
#print( f"Found duplicate hash - {hash}")
return False
def writeToDisk(name, data):
with open( name, "wb" ) as f:
f.write(data)
def removeFromDisk(name):
try:
os.remove(name)
except:
pass
def execute(cmd, data = None, empty=False):
res = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if data != None:
res.stdin.write(data)
res.stdin.close()
out = res.stdout.read()
err = res.stderr.read()
return (out, err)
if __name__ == "__main__":
if len(sys.argv) != 2:
print( "Invalid number of parameters: ")
print( "" )
print( "Usage - python MLextract.py [masterlist .ml file|icao .ldif file]")
print( "" )
exit(1)
main( sys.argv[1] )