-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprivate_keys_extractor.py
More file actions
53 lines (37 loc) · 1.36 KB
/
Copy pathprivate_keys_extractor.py
File metadata and controls
53 lines (37 loc) · 1.36 KB
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
import xml.etree.ElementTree as ET
import argparse
def extract_keys_from_xml(xml_file):
tree = ET.parse(xml_file)
root = tree.getroot()
ecdsa_key = None
rsa_key = None
for key in root.findall('./Keybox/Key'):
key_type = key.get('algorithm')
if key_type == 'ecdsa':
for priv_key in key.findall('PrivateKey'):
ecdsa_key = priv_key.text
elif key_type == 'rsa':
for priv_key in key.findall('PrivateKey'):
rsa_key = priv_key.text
return ecdsa_key, rsa_key
def save_key_to_file(key, filename):
with open(filename, 'w') as file:
file.write(key)
def main():
parser = argparse.ArgumentParser(description="Script to extract private keys from a keybox.")
parser.add_argument('-f', '--file', type=str, required=False, help="XML file containing the keys. Default: keybox.xml")
args = parser.parse_args()
xml_file = 'keybox.xml'
if args.file:
xml_file = args.file
ecdsa_key, rsa_key = extract_keys_from_xml(xml_file)
if ecdsa_key:
save_key_to_file(ecdsa_key, 'ECDSA_rootCA.key')
else:
print("ECDSA key not found in the XML file.")
if rsa_key:
save_key_to_file(rsa_key, 'RSA_rootCA.key')
else:
print("RSA key not found in the XML file.")
if __name__ == "__main__":
main()