Created
November 29, 2018 03:56
-
-
Save marche147/205a10e1d8ab2cea2cf2653ed0847484 to your computer and use it in GitHub Desktop.
BCTF 2018 guess_number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import ast | |
import telnetlib | |
#HOST, PORT = 'localhost', 9999 | |
HOST, PORT = '60.205.223.220', 9999 | |
s = socket.socket() | |
s.connect((HOST, PORT)) | |
f = s.makefile('rw', 0) | |
def recv_until(f, delim='\n'): | |
buf = '' | |
while not buf.endswith(delim): | |
buf += f.read(1) | |
return buf | |
p = 1461501637330902918203684832716283019655932542983 | |
k = 10 | |
def solve_hnp(t, u): | |
# http://www.isg.rhul.ac.uk/~sdg/igor-slides.pdf | |
M = Matrix(RationalField(), 23, 23) | |
for i in xrange(22): | |
M[i, i] = p | |
M[22, i] = t[i] | |
M[22, 22] = 1 / (2 ** (k + 1)) | |
def babai(A, w): | |
''' http://sage-support.narkive.com/HLuYldXC/closest-vector-from-a-lattice ''' | |
C = max(max(row) for row in A.rows()) | |
B = matrix([list(row) + [0] for row in A.rows()] + [list(w) + [C]]) | |
B = B.LLL(delta=0.9) | |
return w - vector(B.rows()[-1][:-1]) | |
closest = babai(M, vector(u + [0])) | |
return (closest[-1] * (2 ** (k + 1))) % p | |
for i in xrange(5): | |
t = ast.literal_eval(f.readline().strip()) | |
u = ast.literal_eval(f.readline().strip()) | |
alpha = solve_hnp(t, u) | |
recv_until(f, 'number: ') | |
s.send(str(alpha) + '\n') | |
t = telnetlib.Telnet() | |
t.sock = s | |
t.interact() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment