just for now
This commit is contained in:
BIN
week06/easy/__pycache__/pwn_utils.cpython-312.pyc
Normal file
BIN
week06/easy/__pycache__/pwn_utils.cpython-312.pyc
Normal file
Binary file not shown.
@ -1,25 +1,44 @@
|
||||
import socket
|
||||
|
||||
from Crypto.Util.number import getPrime, inverse
|
||||
import re
|
||||
# Fill in the right target here
|
||||
HOST = 'this.is.not.a.valid.domain' # TODO
|
||||
PORT = 0 # TODO
|
||||
HOST = 'netsec.net.in.tum.de' # TODO
|
||||
PORT = 20106 # TODO
|
||||
|
||||
|
||||
def int_to_bytes(m):
|
||||
return m.to_bytes((m.bit_length() + 7) // 8, 'big').decode()
|
||||
|
||||
def modular_inverse(p, phi_q):
|
||||
x = inverse(p, phi_q)
|
||||
return x % phi_q
|
||||
|
||||
def decrypt_message(encrypted, p, q):
|
||||
phi_q = q - 1
|
||||
d = modular_inverse(p, phi_q)
|
||||
message = pow(encrypted, d, q)
|
||||
return message
|
||||
|
||||
|
||||
|
||||
def get_flag():
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
s.connect((HOST, PORT))
|
||||
sf = s.makefile('rw') # we use a file abstraction for the sockets
|
||||
|
||||
message1 = sf.readline().rstrip('\n')
|
||||
# TODO
|
||||
|
||||
m = sf.readline().rstrip('\n')
|
||||
bit_len = int(re.search(r"[0-9]+", m).group())
|
||||
print(f"bit_len is {bit_len}")
|
||||
p = getPrime(bit_len*2)
|
||||
q = getPrime(bit_len*2)
|
||||
sf.write(f'{p};{q}\n')
|
||||
sf.flush()
|
||||
c_m = int(sf.readline().rstrip('\n'))
|
||||
print("C_M: ", c_m)
|
||||
d_m = decrypt_message(c_m, p, q)
|
||||
sf.close()
|
||||
s.close()
|
||||
print(int_to_bytes(d_m))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
27
week06/easy/pwn_utils.py
Normal file
27
week06/easy/pwn_utils.py
Normal file
@ -0,0 +1,27 @@
|
||||
import asyncio
|
||||
|
||||
|
||||
class utils:
|
||||
@staticmethod
|
||||
async def read_line_safe(reader):
|
||||
"""
|
||||
Simple implementation to read a line from an async reader
|
||||
Mimics the original read_line_safe functionality
|
||||
"""
|
||||
try:
|
||||
line = await reader.readline()
|
||||
return line.decode().strip()
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def log_error(e, client_writer=None):
|
||||
"""
|
||||
Basic error logging function
|
||||
"""
|
||||
print(f"Error occurred: {e}")
|
||||
if client_writer:
|
||||
try:
|
||||
client_writer.write(f"Error: {str(e)}\n".encode())
|
||||
except Exception:
|
||||
print("Could not send error to client")
|
@ -46,18 +46,19 @@ async def handle_client(client_reader: StreamReader, client_writer: StreamWriter
|
||||
return
|
||||
match resp.split(';'):
|
||||
case [e, n]:
|
||||
if not e.isdigit() or not n.isdigit():
|
||||
if not e.isdigit() or not n.isdigit(): # has to be not
|
||||
client_writer.write('Invalid input\n'.encode())
|
||||
await client_writer.drain()
|
||||
return
|
||||
e, n = int(e), int(n)
|
||||
if not (no_bits * 2 - 1 <= n.bit_length() <= no_bits * 2):
|
||||
if not (no_bits * 2 - 1 <= n.bit_length() <= no_bits * 2): # has to be not
|
||||
client_writer.write('Wrong bit count\n'.encode())
|
||||
await client_writer.drain()
|
||||
return
|
||||
flag = subprocess.check_output('flag').decode().strip()
|
||||
flag = "flaggy"
|
||||
m = int.from_bytes(flag.encode(), 'big')
|
||||
c = pow(m, e, n)
|
||||
print(c)
|
||||
client_writer.write(f'{c}\n'.encode())
|
||||
case _:
|
||||
client_writer.write('Invalid input\n'.encode())
|
||||
|
36
week06/easy/test.py
Normal file
36
week06/easy/test.py
Normal file
@ -0,0 +1,36 @@
|
||||
import random
|
||||
from Crypto.Util.number import getPrime, inverse
|
||||
from Crypto.Util.number import inverse
|
||||
|
||||
|
||||
def moc_encryption_message(p, q):
|
||||
message = int.from_bytes("flaggy".encode(), 'big')
|
||||
print("original message: ", message)
|
||||
c = pow(message,p,q)
|
||||
return c
|
||||
|
||||
|
||||
def modular_inverse(p, phi_q):
|
||||
x = inverse(p, phi_q)
|
||||
return x % phi_q
|
||||
|
||||
def decrypt_message(encrypted, p, q):
|
||||
phi_q = q - 1
|
||||
d = modular_inverse(p, phi_q)
|
||||
message = pow(encrypted, d, q)
|
||||
return message
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
bit_length = 992
|
||||
o = getPrime(bit_length*2)
|
||||
q = getPrime(bit_length*2)
|
||||
e, n = int(o), int(q)
|
||||
if not (bit_length * 2 - 1 <= n.bit_length() <= bit_length * 2): # has to be not
|
||||
print("wrong bit count")
|
||||
c = moc_encryption_message(o, q)
|
||||
m = decrypt_message(c, o, q)
|
||||
print("decrypted message: ", m)
|
||||
byte_length = (m.bit_length() + 7) // 8
|
||||
decoded_string = m.to_bytes(byte_length, 'big').decode()
|
||||
print("Decrypted_message_to_string: ", decoded_string)
|
Reference in New Issue
Block a user