This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate.py
executable file
·70 lines (51 loc) · 1.71 KB
/
generate.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
#!/usr/bin/env python3
import hmac
import json
import hashlib
import os
import sys
PREFIX = "ugra_avoid_the_mines_"
SECRET = b"UQWtzjydRD7W47uA"
SALT_SIZE = 63 - len(SECRET)
def encode_flag(flag):
prefix = flag[:32]
suffix = flag[32:64]
xor_value = 17
encoded_prefix = bytearray()
for c in prefix:
encoded_prefix.append(ord(c) ^ xor_value)
xor_value = (xor_value + 13) % 256
encoded_prefix.append(ord('%') ^ xor_value)
encoded_suffix = bytearray()
for c in suffix:
encoded_suffix.append(ord(c) ^ xor_value)
xor_value = (xor_value + 13) % 256
encoded_suffix.append(ord('%') ^ xor_value)
return (bytes(encoded_prefix), bytes(encoded_suffix))
def get_flag():
user_id = sys.argv[1]
return PREFIX + hmac.new(SECRET, str(user_id).encode(), "sha256").hexdigest()[:SALT_SIZE]
def generate():
if len(sys.argv) < 3:
print("Usage: generate.py user_id target_dir", file=sys.stderr)
sys.exit(1)
target_dir = sys.argv[2]
flag = get_flag()
prefix, suffix = encode_flag(flag)
data = open(os.path.join("private", "mines.exe"), "rb").read()
data = data.replace(b"PREFIX__________________________\0", prefix)
data = data.replace(b"SUFFIX__________________________\0", suffix)
open(os.path.join(target_dir, "mines.exe"), "wb").write(data)
decoded_suffix = flag[32:64]
for i in range(43):
m = hashlib.md5()
m.update(decoded_suffix.encode("utf-8"))
decoded_suffix = m.hexdigest()[:len(decoded_suffix)]
new_flag = flag[:32] + decoded_suffix
json.dump({
"flags": [new_flag],
"substitutions": {},
"urls": []
}, sys.stdout)
if __name__ == "__main__":
generate()