Code Analysis
The server generates n = pq from two 512-bit primes with p, q ≡ 3 (mod 4) and sets e = 65537, but it never prints n. It then runs an oracle loop: each integer we send is reduced modulo n and passed to f. Sending anything non-numeric ends the loop. The server then prints a random m and gives us one chance to send an s with s ^ e == m (mod n), which is an RSA signature on m.
#!/usr/local/bin/python
from Crypto.Util.number import getPrime as gP
from random import choice, randint
p, q = gP(512), gP(512)
while p % 4 != 3:
p = gP(512)
while q % 4 != 3:
q = gP(512)
n = p * q
e = 0x10001
f = lambda x: ((choice([-1,1]) * pow(x, (p + 1) // 4, p)) * pow(q, -1, p) * q + (choice([-1,1]) * pow(x, (q + 1) // 4, q)) % q * pow(p, -1, q) * p) % n
while True:
try:
l = int(input(">>> ")) % n
print(f(l))
except:
break
m = randint(0, n - 1)
print(f"{m = }")
s = int(input(">>> ")) % n
if pow(s,e,n) == m:
print(open("flag.txt", "r").read())
else:
print("Wrong signature!")
exit(1)
Forging that signature requires d = e^-1 mod φ(n), which means factoring n, a modulus we don’t even know. The oracle f makes both possible.
What f Computes
f is Rabin decryption: it returns one of the four square roots of its input modulo n, chosen at random.
In the Rabin cryptosystem, with , and a message is encrypted as . To decrypt, compute and . These are square roots of : for a quadratic residue ,
since by Euler’s criterion, and likewise for modulo . The Chinese Remainder Theorem combines and into the four square roots of modulo :
Reducing modulo and confirms it. This is f term for term, with choice([-1,1]) picking the two signs.
Approach
Factoring with a Nontrivial Square Root
Suppose but . Then is a nontrivial factor of .
Since , the gcd can’t be , because that would mean , i.e. . It can’t be either: then would have to divide , giving . So is a proper factor of .
Applying It to the Oracle
Send 1 repeatedly. The square roots of 1 modulo n are 1, n - 1, and a nontrivial pair r, n - r, where r ≡ 1 (mod p) and r ≡ -1 (mod q) or vice versa. A handful of queries returns all four, and they give us two things:
- The modulus: the largest root is
n - 1, son = max + 1. - A factor:
r² ≡ 1² (mod n)withr ≢ ±1, sogcd(r - 1, n)isporq.
From there it’s ordinary RSA: compute φ(n) = (p - 1)(q - 1) and d = e^-1 mod φ(n), then sign with s = m^d mod n.
Solve Script
The script collects the four roots of 1 (kept sorted, so roots[3] = n - 1 and roots[1] is a nontrivial root), recovers n, p, and q, then ends the loop with an empty line, reads m, and sends the signature.
from gmpy2 import gcd
from pwn import remote
HOST = "localhost"
PORT = 5000
roots = [1]
r = remote(HOST, PORT)
while len(roots) != 4:
r.recvuntil(b">>> ")
print("[+] Sending 1...")
r.sendline(b"1")
root = int(r.recvline().decode().strip())
if root not in roots:
roots.append(root)
roots.sort()
e = 0x10001
n = roots[3] + 1
print(f"n = {n}")
p = gcd(n, roots[1] - 1)
q = n // p
print(f"p = {p}")
print(f"q = {q}")
phi = (p - 1) * (q - 1)
d = pow(e, -1, phi)
print("[+] Sending new line...")
r.recvuntil(b">>> ")
r.sendline()
r.recvuntil(b"m = ")
m = int(r.recvline().decode())
print(f"m = {m}")
s = pow(m, d, n)
print(f"[+] Sending {s}...")
r.recvuntil(b">>> ")
r.sendline(str(s).encode())
print("[+] Receiving flag...")
flag = r.recvline().decode()
print(flag)
r.close()
Output against a local instance:
[+] Opening connection to localhost on port 5000: Done
[+] Sending 1...
[+] Sending 1...
[+] Sending 1...
n = 102158757137872916867519766834251684680047080108910074901777550131565777310052381355408685441380906174902194389651936971918079142514341344986536557700349342855709010720096500612844096577634994457758191078087559505428005017143300341895702315693422527077074674520562744825189085172771541834290284724932796247137
p = 10163522920815193387671976660840292677514223838397007185548238301805475721185337212771040101993181206973914926605448877804601073462575997864003985519501587
q = 10051510478581081606816003866233207332805671564707596688598801911593133850882133619150492881301535790157976089632071341184154817859781248461832663913422651
[+] Sending new line...
m = 69658260043215692590250091981534290110388076236342117084734605806418301103036788945521753348693372927910451044699812147577274943781451661026087890509391642874395312011061447410862485470172866938424634783637941556372609290407166960301132559716419616688059423325224356360929771063686014399474759145390905138043
[+] Sending 74056118309961038073972813492756342976782322595880800751732257050666722540225553010191159763211827036933043555007400580560995838881529582750376928674075315503481813059719582965375503460851839296729674414145294555072883273134114588315237802862397233964056941755099569279706120000564880811770351902708022831520...
[+] Receiving flag...
FLAG
[*] Closed connection to localhost port 5000