forked from yanapermana/ctf-2016
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insomni_hack_teaser_bring_the_noise_solver.sage
126 lines (101 loc) · 2.57 KB
/
insomni_hack_teaser_bring_the_noise_solver.sage
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
from collections import defaultdict
import hashlib
import itertools
import string
import socket
import struct
def md5(s):
m = hashlib.md5()
m.update(s)
return m.hexdigest()
def powork(challenge):
letters = string.printable
for c in itertools.combinations_with_replacement(letters, 4):
s = ''.join(c)
if md5(s).startswith(challenge):
return s.encode('hex')
# socket
sock = socket.socket()
sock.connect(('bringthenoise.insomnihack.ch',1111))
print '[*] Connect ...'
# grab challenge
r = sock.recv(4096)
if 'Challenge = ' in r:
challenge = r.replace('Challenge = ','').rstrip()
print '[*] Challenge: {0}'.format(challenge)
# proof of work
powork_r = powork(challenge)
print '[*] Proof of work: {0}'.format(powork_r)
# send powork
sock.send(powork_r.decode('hex')+'\n')
# see response
r = sock.recv(4096)
if ',' in r:
data = [int(x) for x in r if x.isdigit()]
result = data[len(data)-1]
del data[len(data)-1]
coefs = data
print '[*] Coefs: {0}'.format(coefs)
print '[*] Result: {0}'.format(result)
# see equation
r = sock.recv(4096)
print r
equation = r.split('\n')
del equation[len(equation)-1]
del equation[len(equation)-1]
print '[*] Equation: {0}'.format(equation)
# find solution
print '[*] Find solution ...'
def get_common_element(L):
d = defaultdict(int)
for i in L:
d[i] += 1
result = max(d.iteritems(), key=lambda x: x[1])
return result
def chunks(l, n):
n = max(1, n)
return [l[i:i + n] for i in range(0, len(l), n)]
sol_st = []
a,b,c,d,e,f = var('a,b,c,d,e,f')
sol = 'abcdef'
equs = equation
equu = chunks(equs, 6)
for i in xrange(2):
equ = equu[i]
cdd_dt = []
cnt = 0
for r in xrange(len(equ)):
if cnt == 6:
break
else:
dt = [int(x) for x in equ[r] if x.isdigit()]
res = dt[len(dt)-1]
del dt[len(dt)-1]
coefs = dt
poly = ''
for i in xrange(len(coefs)):
poly += '{0}*{1} + '.format(str(coefs[i]), sol[i])
cdd = '{0} + v + 8 == {2}'.format(poly[0:len(poly)-3], i,str(res))
cdd_dt.append(cdd)
cnt += 1
bforce = open('/ctf/insomnihack2016/crypto/bring_the_noise/66abc.txt')
for l in bforce:
l = list(l.rstrip())
nl = ['-1' if x == '2' else x for x in l ]
listing = ''
for i in xrange(len(cdd_dt)):
listing += '{0}, '.format(cdd_dt[i].replace('v', nl[i]))
exec('eqn = [{0}]'.format(listing[0:len(listing)-2]))
solution = solve_mod(eqn, 8)
if len(solution) > 0:
for x in solution:
sol_st.append(x)
sol, y = get_common_element(sol_st)
sol = ', '.join([ str(x) for x in sol ])
# send solution
print '[*] Solution: {0}'.format(sol)
sock.send(sol+'\n')
# see response
r = sock.recv(4096)
print r
sock.close()