-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
executable file
·72 lines (48 loc) · 1.12 KB
/
solver.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
#!/usr/bin/env python3
import sys
import socket
HOST = sys.argv[1] if len(sys.argv) > 1 else 'localhost'
PORT = 17171
def encrypt(io, data):
io.write(
b'ENCRYPT ' + data.hex().encode() + b'\n',
)
io.flush()
io.read(2)
return bytes.fromhex(
io.readline().strip().decode(),
)
def decrypt(io, data):
io.write(
b'DECRYPT ' + data.hex().encode() + b'\n',
)
io.flush()
io.read(2)
return bytes.fromhex(
io.readline().strip().decode(),
)
def xor(a, b):
return bytes(x ^ y for x, y in zip(a, b))
def attack(io):
ct = encrypt(io, b'\x00' * 32)
pt = decrypt(io, ct[16:])
flag = xor(ct[:16], pt)
flag = b'ptzctf{' + flag + b'}'
return flag
def main():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10)
sock.connect((HOST, PORT))
io = sock.makefile('rwb')
try:
io.readline()
io.readline()
flag = attack(io)
print(flag)
except Exception as e:
print(e)
finally:
io.close()
sock.close()
if __name__ == '__main__':
main()