-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNC_MathBot.py
More file actions
44 lines (37 loc) · 1.13 KB
/
Copy pathNC_MathBot.py
File metadata and controls
44 lines (37 loc) · 1.13 KB
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
# Source - https://codingo.io/ctf/writeup/programming/crikeycon/2017/03/08/crikeycon-2017-fastmath.html
#!/usr/bin/python3
import socket
import re
import time
MAXBUF = 4096
SENTINEL = 'flag'
CTF_BOT = ('IP', PORT)
if __name__ == '__main__':
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(CTF_BOT)
while True:
data = b''
# receive and store data
while True:
chunk = client.recv(MAXBUF)
data += chunk
if len(chunk) < MAXBUF:
break
decoded = data.decode('utf-8')
print(decoded)
if SENTINEL in decoded:
print(decoded)
break
match = re.search('x =.*', decoded)
if not match:
raise ValueError("Invalid expression string")
b = match.group(0)
# print(match)
expression = b[4:]
# print(expression)
result = eval(expression)
print(expression + ' = ' + str(result))
data = str(result).encode('utf-8') + b'\n'
print('Sending: ' + str(result))
client.send(data)
time.sleep(0.8)