Skip to content

Commit

Permalink
some starter code
Browse files Browse the repository at this point in the history
  • Loading branch information
Spamakin committed Mar 14, 2024
1 parent 3954022 commit a687fc0
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 7 deletions.
1 change: 1 addition & 0 deletions chals/crypto/diagonal/challenge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ flags:
- sigpwny{mult1d1m3n510n4l}
files:
- public.py
- starter.py
hints:
- Play with some matrices, see how the exponent affects the entries, get some equations going
state: hidden
20 changes: 19 additions & 1 deletion chals/crypto/diagonal/server.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import numpy as np
import signal
import time
import random

FLAG = "sigpwny{mult1d1m3n510n4l}"


def inputs():
print("[D] Define a 3 x 3 Integer Matrix M")
M = [
Expand All @@ -18,9 +21,21 @@ def inputs():
return None
return M

def handler(signum, frame):
raise Exception("[D] Matrix Mult took too long, try something simple")

def fun(M_, d):
M = np.matrix(M_, dtype='object')
M = M**d

signal.signal(signal.SIGALRM, handler)
signal.alarm(5) # raise alarm after 5 seconds
try:
M = M**d
except:
print("[D] Matrix Mult took too long, try something simple")
return None

signal.alarm(0) # disable the alarm

# [\\\]
# [ \\]
Expand All @@ -37,6 +52,9 @@ def main():
return

res = fun(M, secret)
if res == None:
print("[D] You tried something weird...")
return
print()
print(f"[D] Have fun: {res}")
print()
Expand Down
34 changes: 28 additions & 6 deletions chals/crypto/diagonal/sol.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
from pwn import *
import sympy as sp

def main():

res = 18669237451167230340
conn = remote("chal.cryptoctf.sigpwny.com", 5001)

conn.recvline() # [D] Welcome
conn.recvline() # [D] Define a 3 x 3 Integer Matrix M

# Send matrix
conn.sendline(b'1')
conn.sendline(b'1')
conn.sendline(b'1')
conn.sendline(b'0')
conn.sendline(b'1')
conn.sendline(b'1')
conn.sendline(b'0')
conn.sendline(b'0')
conn.sendline(b'1')

conn.recvline()
# Here is number the server gives
res = int(conn.recvline().decode("utf-8")[13:].strip())
conn.recvline()
print(f"Challenge Accepted: {res}")

# Some solutions
disc = (25 - 4 * (6 - 2*int(res)))**(0.5)
sec_ = (-5 + disc) // 2
print(int(sec_))
# TODO: Put some writing in here
# disc = (25 - 4 * (6 - 2*int(res)))**(0.5)
# secret = (-5 + disc) // 2

n = sp.Symbol('n')
print(sp.solve(3 + 2*n + (n**2 + n) / 2 - res, n)[1])

secret = sp.solve(3 + 2*n + (n**2 + n) / 2 - res, n)[1]

conn.sendline(str(int(secret)).encode()) # Send answer
print(conn.recvline().decode("utf-8")[22:].strip()) # Print flag

if __name__ == "__main__":
main()
53 changes: 53 additions & 0 deletions chals/crypto/diagonal/starter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from pwn import *

def main():

# Here is some starter code for those unfamiliar with pwntools
# I am not good at pwntools, so there may be better ways to do this
# But this is how I know how to do things

# Connect to remote server
conn = remote("chal.cryptoctf.sigpwny.com", 5001)

conn.recvline() # [D] Welcome
conn.recvline() # [D] Define a 3 x 3 Integer Matrix M

# Send matrix here
# Replace the 0/1's with the numbers you want
conn.sendline(b'1')
conn.sendline(b'0')
conn.sendline(b'0')
conn.sendline(b'0')
conn.sendline(b'1')
conn.sendline(b'0')
conn.sendline(b'0')
conn.sendline(b'0')
conn.sendline(b'1')

conn.recvline()
# Here is number the server gives
res = int(conn.recvline().decode("utf-8")[13:].strip())
print(f"Challenge Accepted: {res}")


# TODO: Write some code to recover the secret from res!
#
# Bytes:
# pwntools reads and sends bytes
# .encode turns a string into a bytestring
# .decode("utf-8") turns a bytestring into a string
#
# Sending and Receiving:
# conn.sendline(b'#')
# output = conn.recvline()
# These do what you think
#
# Some basic string manipulation such as .strip() and slicing helps
# Use alot of print statements in debugging this
# Try some stuff locally as well
# Have a working local solution *before* pwntools-ifying it
#
# pwntools is really useful, I recommend you learn it

if __name__ == "__main__":
main()

0 comments on commit a687fc0

Please sign in to comment.