Skip to content

Commit

Permalink
Fix solver failures in BinEx101
Browse files Browse the repository at this point in the history
Adds more resilient socket code to continue reading from the socket
until the newline are read.

Fixes #25
  • Loading branch information
John E. Rollinson committed Oct 27, 2021
1 parent 2461180 commit d8955c6
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions examples/remote/make/solver/solve.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
#!/usr/bin/python3
import argparse
import socket

parser = argparse.ArgumentParser(description="solve script for 'BinEx101'")
parser.add_argument('--host', default="challenge", help="the host for the instance")
parser.add_argument('--port', type=int, default=5000, help="the port of the instance")
parser.add_argument('--print', action='store_true', help="print flag to stdout rather than saving to file")
args = parser.parse_args()

def recv_until(sock, pattern):
response = sock.recv(4096).decode()
while len(response) < len(pattern) or response[-len(pattern):] != pattern:
response += sock.recv(4096).decode()
return response

target = 2**32 - 1
print("target =", target)
# print("target =", target)
p = 2
q = 0
while p*p < target:
Expand All @@ -16,14 +29,17 @@
exit(-1)

c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c.connect(("challenge", 5000))
c.recv(4096)
c.connect((args.host, args.port))
recv_until(c, ":\r\n")
c.sendall(("%d\n" % p).encode())
c.recv(4096)
recv_until(c, ":\r\n")
c.sendall(("%d\n" % q).encode())
response = c.recv(4096).decode()
response = recv_until(c, "'\r\n")

flag = response.split("'")[-2]

with open('flag', 'w') as f:
f.write(flag)
if args.print:
print(f"flag: {flag}")
else:
with open('flag', 'w') as f:
f.write(flag)

0 comments on commit d8955c6

Please sign in to comment.