picoCTF 2023 SRA

preview_player
Показать описание

from pwn import *
import primefac
from itertools import combinations

# function to generate all the sub lists
def sub_lists (l):
# initializing empty list
comb = []

#Iterating till length of list
for i in range(1,len(l)+1):
# Generating sub list
comb += [list(j) for j in combinations(l, i)]
# Returning list
return comb

def divisors(phi):
print("Give me the divisors of ", phi-1)
# this is dangerous, but I'm trusting me
return(eval(input()))

# connect to the server
# get ciphertext
# get d
print("cipher=",ciphertext)
print("d=",d)
# since d is e^-1 mod (p-1)*(q-1), d*e=k*(p-1)*(q-1)+1
# so (p-1)*(q-1)*k = d*e-1
factors=divisors(d*65537)
combos = sub_lists(factors)
primes = set()
# try all possible subsets of the list of factors as the factors of (p-1)
for l in combos:
product = 1
# multiply them together to get p-1
for k in l:
product = product * k
# if the right length and adding 1 yields a prime, then maybe
print(primes)
primelist = list(primes)
# try all possible prime pairs
for p in primelist:
for q in primelist:
n=p*q
# decode with this possible n
plain = pow(ciphertext,d,n)
try:
plaintext = long_to_bytes(plain)
# see if it corresponds to text and if so, try it
except:
continue
Рекомендации по теме
Комментарии
Автор

I just wanted to say i’m so grateful for this writeup, i spent like 3 days on the problem back in the ctf but couldnt solve it

rzn