-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeepass_keyfile_generator.py
99 lines (82 loc) · 3.42 KB
/
keepass_keyfile_generator.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
#!/usr/bin/env python
# KeePass KeyFile Generator
# Author: Florian Zeeb
# License: MIT
# Version: 1.1 @ 2023-04-26
# Version: 1.0 @ 2021-11-17
# Source: https://github.com/florianzeeb/keepass_keyfile_generator/
import os
import sys
import platform
import time
import datetime
import math
import secrets
import hashlib
from win32_setctime import setctime #pip install win32-setctime
# define max keyfiles
run_numbers = 10
# define script file and script path
script_file = os.path.realpath(__file__)
script_path = os.path.dirname(sys.argv[0])
script_path_absolute = os.path.abspath(script_path)
# calculcate max digits of integer: run_numbers
max_digits = int(math.log10(run_numbers))+1
# define the template
source_template = r'''<?xml version="1.0" encoding="utf-8"?>
<KeyFile>
<Meta>
<Version>2.0</Version>
</Meta>
<Key>
<Data Hash="#KEY01##">
#KEY02## #KEY03## #KEY04## #KEY05##
#KEY06## #KEY07## #KEY08## #KEY09##
</Data>
</Key>
</KeyFile>'''
# loop per run_number
for x in range(0, run_numbers):
# read template for run
source_template_output = source_template
# define random files
secret_2 = secrets.token_hex(4).upper()
secret_3 = secrets.token_hex(4).upper()
secret_4 = secrets.token_hex(4).upper()
secret_5 = secrets.token_hex(4).upper()
secret_6 = secrets.token_hex(4).upper()
secret_7 = secrets.token_hex(4).upper()
secret_8 = secrets.token_hex(4).upper()
secret_9 = secrets.token_hex(4).upper()
# create hash of created keys
keys_to_hash = secret_2 + secret_3 + secret_4 + secret_5 + secret_6 + secret_7 + secret_8 + secret_9
secret_1 = hashlib.sha256(bytes.fromhex(keys_to_hash)).hexdigest()[:8].upper()
# replace key template with secrets
source_template_output = source_template_output.replace('#KEY01##', secret_1)
source_template_output = source_template_output.replace('#KEY02##', secret_2)
source_template_output = source_template_output.replace('#KEY03##', secret_3)
source_template_output = source_template_output.replace('#KEY04##', secret_4)
source_template_output = source_template_output.replace('#KEY05##', secret_5)
source_template_output = source_template_output.replace('#KEY06##', secret_6)
source_template_output = source_template_output.replace('#KEY07##', secret_7)
source_template_output = source_template_output.replace('#KEY08##', secret_8)
source_template_output = source_template_output.replace('#KEY09##', secret_9)
# add leading 0
x_fix = str(x).zfill(max_digits)
# define output file
target_template_file = f"keepass.kdbx.{x_fix}.keyx"
target = script_path_absolute + "\\" + target_template_file
# write data to target file
source_template_file = open(target, "w")
source_template_file.writelines(source_template_output)
source_template_file.close()
# change file timestamp: last update date, last access date
date = datetime.datetime(year=datetime.datetime.now().year, month=1, day=1, hour=10, minute=00, second=00)
modTime = time.mktime(date.timetuple())
os.utime(target, (modTime, modTime))
# change file timestamp: create date (for windows OS)
if platform.system() == 'Windows':
setctime(target, modTime)
# output
print("Files created: {}".format(run_numbers))
print("Files location: " + script_path_absolute + "\\")