File: get_code_signing_ca.py

package info (click to toggle)
osslsigncode 2.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,216 kB
  • sloc: ansic: 12,466; python: 1,001; sh: 74; makefile: 12; javascript: 1
file content (52 lines) | stat: -rwxr-xr-x 1,827 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python3
# © 2024 Michal Trojnara
# This script downloads Microsoft code signing certificates
# Tor is required for this script to work
# Redirect the script output to a PEM file

from sys import stderr
from time import sleep
from csv import reader
from requests import get
from requests.exceptions import RequestException
from concurrent.futures import ThreadPoolExecutor
from re import search

def download_cert(hash):
    for attempt in range(10):
        if attempt > 0:
            sleep(10)
        try:
            creds = f'{attempt}{hash}:{attempt}{hash}'
            proxies = dict(https=f'socks5://{creds}@127.0.0.1:9050')

            url = f'https://crt.sh/?sha1={hash}&match=='
            resp = get(url, proxies=proxies)
            resp.raise_for_status()

            m = search(r'\bid=(\d+)\b', resp.content.decode('ascii', 'replace'))
            id = m.group(1)

            url = f'https://crt.sh/?d={id}'
            resp = get(url, proxies=proxies)
            resp.raise_for_status()

            print('.', file=stderr, end='')
            stderr.flush()
            return resp.content.decode('utf-8', 'replace')
        except Exception as e:
            print(f'\n{url} attempt {attempt}: {e}', file=stderr)
    print('\nGiving up on', hash, file=stderr)

resp = get('https://ccadb.my.salesforce-sites.com/microsoft/IncludedCACertificateReportForMSFTCSV')
resp.raise_for_status()
lines = resp.content.decode('utf-8').splitlines()[1:]
hashes = [row[4] for row in reader(lines)
    if row[0] != 'Disabled'
        or row[4] == 'F38406E540D7A9D90CB4A9479299640FFB6DF9E224ECC7A01C0D9558D8DAD77D']
with ThreadPoolExecutor(max_workers=10) as executor:
    certs = executor.map(download_cert, hashes)
for cert in certs:
    if cert is not None:
        print(cert)
print('\nDone', file=stderr)