-
Notifications
You must be signed in to change notification settings - Fork 1
/
challenge.py
148 lines (107 loc) · 3.41 KB
/
challenge.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python3
from binascii import hexlify
from binascii import unhexlify
import logging
import sys
import os
from curve25519 import Private, Public
import nacl.secret
import hmac
import hashlib
logger = logging.getLogger('challenge')
def ReadLine(reader):
data = b''
while not data.endswith(b'\n'):
cur = reader.read(1)
data += cur
if cur == b'':
return data
return data[:-1]
def WriteLine(writer, msg):
writer.write(msg + b'\n')
writer.flush()
def ReadBin(reader):
return unhexlify(ReadLine(reader))
def WriteBin(writer, data):
WriteLine(writer, hexlify(data))
def ComputeProof(key, data):
return hmac.new(key, data, digestmod=hashlib.sha256).digest()
def VerifyProof(key, data, proof):
return hmac.compare_digest(ComputeProof(key, data), proof)
def Handshake(password, reader, writer):
myPrivateKey = Private()
myNonce = os.urandom(32)
WriteBin(writer, myPrivateKey.get_public().serialize())
WriteBin(writer, myNonce)
theirPublicKey = ReadBin(reader)
theirNonce = ReadBin(reader)
if myNonce == theirNonce:
return None
if theirPublicKey in (b'\x00'*32, b'\x01' + (b'\x00' * 31)):
return None
theirPublicKey = Public(theirPublicKey)
sharedKey = myPrivateKey.get_shared_key(theirPublicKey)
myProof = ComputeProof(sharedKey, theirNonce + password)
WriteBin(writer, myProof)
theirProof = ReadBin(reader)
if not VerifyProof(sharedKey, myNonce + password, theirProof):
return None
return sharedKey
def Server(password, flag, reader, writer):
sharedKey = Handshake(password, reader, writer)
if sharedKey is None:
WriteLine(writer, b'Error: nope.')
return 1
mySecretBox = nacl.secret.SecretBox(sharedKey)
WriteBin(writer, mySecretBox.encrypt(b"AUTHENTICATED"))
while 1:
cmd = mySecretBox.decrypt(ReadBin(reader))
if cmd == b'help':
rsp = b'help|exit|whoami|getflag'
elif cmd == b'exit':
return 0
elif cmd == b'whoami':
rsp = b'root'
elif cmd == b'getflag':
rsp = flag
else:
return 1
WriteBin(writer, mySecretBox.encrypt(rsp))
def Client(password, reader, writer):
sharedKey = Handshake(password, reader, writer)
if sharedKey is None:
WriteLine(writer, b'Error: nope.')
return 1
mySecretBox = nacl.secret.SecretBox(sharedKey)
line = mySecretBox.decrypt(ReadBin(reader))
if line != b"AUTHENTICATED":
WriteLine(writer, b'Error: nope.')
return 1
WriteBin(writer, mySecretBox.encrypt(b"whoami"))
line = mySecretBox.decrypt(ReadBin(reader))
if line != b'root':
return 1
WriteBin(writer, mySecretBox.encrypt(b"exit"))
return 0
def Challenge(password, flag, reader, writer):
try:
server_or_client = ReadLine(reader)
is_server = server_or_client[0] in b'sS'
is_client = server_or_client[0] in b'cC'
if is_server:
return Server(password, flag, reader, writer)
elif is_client:
return Client(password, reader, writer)
else:
WriteLine(writer, b'Error: Select if you want to speak to the (s)erver or (c)lient.')
return 1
except Exception as e:
WriteLine(writer, b'Error')
return 1
def main():
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
password, flag = map(lambda f: open(f, 'rb').read().strip(), sys.argv[1:3])
assert(flag.startswith(b'CTF{') and flag.endswith(b'}'))
return Challenge(password, flag, sys.stdin.buffer, sys.stdout.buffer)
if __name__ == '__main__':
sys.exit(main())