Skip to content

Commit 23de819

Browse files
authored
Create solve.py
1 parent 09f18de commit 23de819

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

Cryptography/My Box/solve.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from pwn import remote, context
2+
import random
3+
import string
4+
import time
5+
6+
def substitute(s1, sbox, key):
7+
sbox = sbox
8+
key = key
9+
s = []
10+
11+
for value in s1:
12+
r = key
13+
while value:
14+
r = sbox[r]
15+
value -= 1
16+
s.append(r)
17+
18+
return bytes(s).hex()
19+
20+
21+
host, port = "0.cloud.chals.io", 31202
22+
context.log_level = "debug"
23+
24+
io = remote(host, port)
25+
26+
io.recvuntil(b">")
27+
io.sendline(b"1")
28+
seed = int(time.time())
29+
io.recvuntil(b"flag: ")
30+
enc = io.recvline().strip().decode()
31+
chunks = []
32+
33+
for i in range(0, len(enc), 2):
34+
chunks.append(enc[i:i+2])
35+
36+
for trial in range(seed-0x100, seed+0x100):
37+
random.seed(trial)
38+
sbox = list(range(256))
39+
random.shuffle(sbox)
40+
41+
charset = string.printable
42+
mapping = {}
43+
44+
for key in range(0xff):
45+
for value in charset:
46+
r = substitute(value.encode(), sbox, key)
47+
mapping[r] = value
48+
49+
flag = ""
50+
try:
51+
for chunk in chunks:
52+
flag += mapping[chunk]
53+
except KeyError:
54+
pass
55+
56+
if "csean-ctf" in flag:
57+
print(flag)
58+
break
59+
60+
61+
io.interactive()

0 commit comments

Comments
 (0)