-
Notifications
You must be signed in to change notification settings - Fork 5
/
proctorserver.py
70 lines (51 loc) · 2.05 KB
/
proctorserver.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
#!/usr/bin/env python
import os
import datetime
import subprocess
from select import select
import SocketServer
from dotenv import load_dotenv
load_dotenv(os.path.join(os.path.dirname(__file__), '.env'))
import shellphish_qemu
from rex import QuickCrash
# fuck it
from farnsworth.models import *
class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
pass
class Handler(SocketServer.BaseRequestHandler):
def handle(self):
global cb, cs_name
p = subprocess.Popen([shellphish_qemu.qemu_path('cgc-base'), cb], stdin=subprocess.PIPE, stdout=self.request.fileno(), stderr=self.request.fileno())
p.poll()
test = ""
while p.returncode is None:
r, _, _ = select([self.request], [], [], 0.05)
if r:
b = self.request.recv(1024)
test += b
p.stdin.write(b)
print "polling"
p.poll()
if p.returncode == 0:
self.request.sendall("Finished test, inserting now...\n")
cs = ChallengeSet.select().where(ChallengeSet.name == cs_name)
# first we have to make a fake job
job = Job.create(cs=cs, completed_at=datetime.datetime.now(), worker="garbage")
Test.create(cs=cs, job=job, blob=test)
self.request.sendall("Test inserted!\n")
else:
self.request.sendall("Found a crash, inserting now...\n")
qc = QuickCrash(cb, test)
self.request.sendall("appears to be of type " + qc.kind + "\n")
cs = ChallengeSet.select().where(ChallengeSet.name == cs_name)
# first we have to make a fake job
job = Job.create(cs=cs, completed_at=datetime.datetime.now(), worker="garbage")
Crash.create(cs=cs, job=job, blob=test, kind=qc.kind)
self.request.sendall("Crash inserted!\n")
import sys
cb = sys.argv[1]
cs_name = sys.argv[2]
host = "0.0.0.0"
port = 13370 + int(cs_name)
ThreadedTCPServer.allow_reuse_address = True
ThreadedTCPServer((host, port), Handler).serve_forever()