-
Notifications
You must be signed in to change notification settings - Fork 55
/
chall.py
134 lines (99 loc) · 3.34 KB
/
chall.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
# challenge source code
# Env: Python 3.10
import os
from Crypto.Util.number import long_to_bytes, bytes_to_long
from Crypto.Util.Padding import pad, unpad
from magic_box import *
banner = """
Traveler, choose one God to talk with.
1. Negligent God
2. Softhearted God
3. Harsh God
Other. exit()
"""
def Negligent_God():
print("Who are u?")
your_name = input("> name: ")
your_pass = your_name + "Open the door!"
print("Uh, I don't care who you are. Just show me your magic power.")
algo = input("> algo: ")
mode = input("> mode: ")
assert algo in algos
assert mode in modes
if algo == "AES":
blocksize = 16
else:
blocksize = 8
keys = bytes.fromhex(input("> hex keys: "))
magic_box = Magic_box(algo, mode, keys)
if magic_box.auto_enc(pad(your_pass.encode(), blocksize)) == pad(your_pass.encode(), blocksize):
print("Well done")
print(open("./flag1", "r").read())
else:
print("I'm sorry, maybe something is wrong with your name? Um, I just say it casually.")
def Softhearted_God():
print("Who are u?")
your_name = input("> name: ")
print("Oh, what a poor guy from another world. You are allowed to show me your magic power block by block.")
algo = input("> algo: ")
mode = input("> mode: ")
assert algo in algos
assert mode in modes
block_num = 10
if algo == "AES":
blocksize = 16
else:
blocksize = 8
your_pass = os.urandom(block_num*blocksize)
print(f"[+] you_pass : {your_pass.hex()}")
for i in range(block_num):
print(f"[+] rounds {i}")
keys = bytes.fromhex(input("> hex keys: "))
magic_box = Magic_box(algo, mode, keys)
if magic_box.auto_enc(your_pass)[i*blocksize:(i+1)*blocksize] != your_pass[i*blocksize:(i+1)*blocksize]:
print("You're not a pure guy from another world!")
return
print("Well done")
print(open("./flag2", "r").read())
def Harsh_God():
print("Treat no exceptions. You're not allowed to select the keys directly.")
print("Anyway, this time I want to encrypt the message twice.")
your_name = input("> name: ")
algo = input("> algo: ")
mode = input("> mode: ")
assert algo in algos
assert mode in modes
if algo == "AES":
blocksize = 16
else:
blocksize = 8
your_pass = bytes.fromhex(input("> hex msg (at least 16 bytes): "))
assert len(your_pass) >= 16 and len(your_pass) % blocksize == 0, "Invalid msg length"
your_key = long_to_bytes(crc128(your_pass), 16)[:blocksize]
if mode != "ECB":
iv = os.urandom(blocksize)
magic_box = Magic_box(algo, mode, your_key+iv)
else:
magic_box = Magic_box(algo, mode, your_key)
if magic_box.auto_enc(magic_box.auto_enc(your_pass)) == your_pass:
print("What a miracle!")
print(open("./flag3", "r").read())
else:
print("Out, Right Now!")
if __name__ == "__main__":
while True:
print(banner)
try:
choice = int(input("> choice: "))
except:
print("Must be a digit number")
continue
if choice == 1:
Negligent_God()
elif choice == 2:
Softhearted_God()
elif choice == 3:
Harsh_God()
else:
print("Byebye!")
exit()