-
Notifications
You must be signed in to change notification settings - Fork 0
/
sukudo.py
91 lines (67 loc) · 2.64 KB
/
sukudo.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
#!/usr/bin/python
import time
import random
def slowpower(base, power, mod):
accum = 1
for bit in bin(power)[2:]:
if accum == 4:
time.sleep(1.0)
accum = accum*accum % mod
if bit == '1':
accum = accum*base % mod
return accum
SAFEPRIME = 27327395392065156535295708986786204851079528837723780510136102615658941290873291366333982291142196119880072569148310240613294525601423086385684539987530041685746722802143397156977196536022078345249162977312837555444840885304704497622243160036344118163834102383664729922544598824748665205987742128842266020644318535398158529231670365533130718559364239513376190580331938323739895791648429804489417000105677817248741446184689828512402512984453866089594767267742663452532505964888865617589849683809416805726974349474427978691740833753326962760114744967093652541808999389773346317294473742439510326811300031080582618145727L
# generated with gensafeprime.generate(2048)
# https://pypi.python.org/pypi/gensafeprime
GENERATOR = 4
FLAG = ''
def rand_exponent(p):
return random.randrange(1, (p - 1)/2)
def group_check(p, m):
if m <= 0:
return False
elif m >= p:
return False
elif pow(m, (p-1)/2, p) != 1:
return False
else:
return True
import base64, SocketServer, os, sys, hashlib
class ServerHandler(SocketServer.BaseRequestHandler):
def fail(self, message):
self.request.sendall(message + "\nGood-bye.\n")
self.request.close()
return False
def captcha(self):
proof = base64.b64encode(os.urandom(9))
self.request.sendall(proof)
test = self.request.recv(20)
ha = hashlib.sha1()
ha.update(test)
if test[0:12]!=proof or not ha.digest().endswith('\xFF\xFF\xFF'):
self.fail("You're a robot!")
def exponentiate(self):
base = int(self.request.recv(1024))
if not group_check(SAFEPRIME, base):
self.fail("Bad group element.")
result = slowpower(base, self.SECRET, SAFEPRIME)
if result != 4:
self.request.sendall(str(result))
else:
self.request.sendall(FLAG)
def setup(self):
self.SECRET = rand_exponent(SAFEPRIME)
def handle(self):
self.captcha()
while True:
self.exponentiate()
self.request.close()
class ThreadedServer(SocketServer.ForkingMixIn, SocketServer.TCPServer):
pass
if __name__ == "__main__":
HOST = sys.argv[1]
PORT = int(sys.argv[2])
FLAG = open('flag.txt', 'r').read()
server = ThreadedServer((HOST, PORT), ServerHandler)
server.allow_reuse_address = True
server.serve_forever()