c = "5c4c4c6c4c3c4c3c5c4c4c6c7cbc6c3c7c3c6c8c6cfc7c5c7c4c5cfc6c3c6cfc7c5c7c4c5cfc6c3c7c4c3c0c5cfc6c3c6cdc7c9c5cfc6c3c6c2c3c0c7c9c5cfc6c3c3c4c6cec6c4c5cfc6c3c6cdc7c9c5cfc6c3c6c4c6cfc6c7c5cfc6c3c6c1c6cec6c4c5cfc6c3c6cdc7c9c5cfc6c3c6c3c3c4c3c7c7cdc0ca" cipher = "" for i inrange(len(c)//2): cipher += c[2*i] print(bytes.fromhex(cipher[:-1]))
c = "CCCACACGCAATCAATCCCACACGCTGTATACCCTTCTCTATACCGTACGTACCTTCGCTATATCTCACCTTCTCACGGAATACCTATCCTTATCACTATCCTTATCACCTTCTCAATCACTCACTCAATAAATAACCTTCCCGATATCTAGCTGCCCTTCTATATAAATAACGTGCTTC"
flag = "" for i in c: if(i == "A"): flag += "00" elif(i == "C"): flag += "01" elif(i == "G"): flag += "10" elif(i == "T"): flag += "11"
print(long_to_bytes(int(flag,2)))
#TFCCTF{1_w1ll_g3t_th1s_4s_4_t4tt00_V3ry_s00n}
PADGROUNDS
题目描述:
1 2 3
Welcome to Padgrounds, where every bit counts. Dive into the action and let your skills shine as you unravel the coded enigma.
Note: Make sure your solver works locally before running on remote.
current = 0 limit = 10000 key = os.urandom(16) iv = os.urandom(16) cipher = AES.new(key, AES.MODE_CBC, iv) ct = cipher.encrypt(FLAG) give = base64.b64encode(iv + ct) print(f"Lets see you decode this: {give.decode()}") print("I made my unpad truly random, there is nothing you can do, just give up already")
whileTrue: if current > limit: exit() to_unpad = input() out = custom_unpad(to_unpad) print(out) current+=1
We heard rumors that someone might rig the upcoming elections. We managed to place a backdoor in one of their messaging systems. See what you can make out of it, the world counts on you.
from ecdsa import ellipticcurve from ecdsa.ecdsa import curve_256, generator_256, Public_key, Private_key import random import hashlib from Crypto.Cipher import AES from Crypto.Util.Padding import pad from Crypto.Util.number import long_to_bytes,bytes_to_long
FLAG = b'TFCCTF{REDACTED}' G = generator_256 order = G.order() rand = random.randint(1,order-1) pubkey = Public_key(G, rand*G) privkey = Private_key(pubkey, rand)
messages = [ "Ensure the ballots are ready for the biased count.", "The first batch of votes is in. Proceed as planned.", "Secure the communications. We can't risk exposure.", "Start the rigging process at midnight tonight.", "All systems are go. Confirm the final preparations.", "Intercepted some chatter. Maintain radio silence.", "Verification complete. Everything is on track.", "Monitor the vote counts closely. Report any issues.", "We've gained access. Execute the next step." ]
defthe_random(): defvery_random(length): return''.join(chr((l(a, b, m) % 94) + 33) for _ inrange(length)) defl(a, b, m): nonlocal x result = (a * x + b) % m x = result return result a = 6364136223846793005 b = 1 m = 2 ** 64 x = random.getrandbits(64) r = very_random(128) s = hashlib.sha256(r.encode()).hexdigest() t = hashlib.md5(s.encode()).hexdigest() u = hashlib.sha1(t.encode()).hexdigest() f = lambda q: int(q, 16) c = lambda q: q & ((1 << random.randint(170,200)) - 1) g = very_random(256) h = very_random(256) j = ''.join(chr((ord(k) ^ ord(l)) % 256) for k, l inzip(g, h)) k = hashlib.sha256(r.encode() + j.encode()).hexdigest() n = ''.join(chr((ord(o) ^ ord(p)) % 256) for o, p inzip(j, k)) o = hashlib.md5(n.encode() + k.encode()).hexdigest() p = hashlib.sha1(o.encode() + k.encode()).hexdigest() q = f(p[:40]) aes_key = very_random(16).encode('utf-8') aes_iv = very_random(16).encode('utf-8') aes_cipher = AES.new(aes_key, AES.MODE_CBC, aes_iv) aes_data = pad((p[:32] + o[:32]).encode('utf-8'), AES.block_size) aes_encrypted = aes_cipher.encrypt(aes_data) z = f(hashlib.sha256(aes_encrypted).hexdigest()[:40]) obfuscated_final = lambda a, b: a ^ (b >> 5) result = obfuscated_final(q, z) return c(result)
max_used = 10 used = 0 print('What do you want to do?') print('1. Listen in on conversation') print('2. Submit the info you found') print('3. Get pubkey') whileTrue: data = input()
if data == '1': if used > max_used: print('Intruder spotted, deleting secrets.') exit()
import hashlib from Crypto.Cipher import AES from Crypto.Util.number import * from Pwn4Sage.pwn import * import json
############################################################################## parameters p = 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff K = GF(p) a = K(0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc) b = K(0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b) E = EllipticCurve(K, (a, b)) G = E(0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296, 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5) n = E.order() a = 733113379677 b = 657 m = 2 ** 150 messages = [ "Ensure the ballots are ready for the biased count.", "The first batch of votes is in. Proceed as planned.", "Secure the communications. We can't risk exposure.", "Start the rigging process at midnight tonight.", "All systems are go. Confirm the final preparations.", "Intercepted some chatter. Maintain radio silence.", "Verification complete. Everything is on track.", "Monitor the vote counts closely. Report any issues.", "We've gained access. Execute the next step." ]
############################################################################## part1 get data sh = remote("challs.tfcctf.com", 31633) sh.recvuntil(b"3. Get pubkey\n") res = [] nums = 9 for i inrange(nums): sh.sendline(b"1") res.append(json.loads(sh.recvline().strip().decode().replace("'","\"")))
############################################################################## part2 ECDSA-HNP h = [i["hash"] for i in res] r = [int(i["r"],16) for i in res] s = [int(i["s"],16) for i in res] L2 = Matrix(ZZ,2*nums,2*nums) for i inrange(nums): L2[i,i] = 1 L2[nums,nums] = 2^180 for i inrange(1,nums): L2[0,nums+i] = s[0]*r[i] L2[i,nums+i] = -s[i]*r[0]
ci = h[i]*r[0]-h[0]*r[i] L2[nums,nums+i] = ci L2[nums+i,nums+i] = n L2[:,-nums+1:] *= n
res = L2.LLL()[0][:nums] k0 = abs(res[0]) d = (s[0]*k0-h[0])*inverse(r[0],n) % n
############################################################################## part3 get flag sh.sendline(b"2") sh.sendline(str(d).encode()) sh.recvline() print(sh.recvline())
#TFCCTF{c0Ngr47s_y0u_s4v3d_th3_3lect1Ons}
HELLFIRE PHANTOM
题目描述:
1 2 3 4 5
In the heart of an ancient, cursed forest lies the Phantom's lair, shrouded in hellfire. Legends speak of a powerful artifact hidden within, guarded by spectral flames and arcane riddles. Will you be the one to break the curse and uncover the secrets of the Hellfire Phantom?
Note: Challenge requires a bit of bruteforcing. Solver takes ~10 mins to run on my laptop. Note: sha256sum of the variable secret's value is f8f099dd278c23d44387914def5fc20bc0fb8915d916775e5b4b17275e2107bd, website used is https://emn178.github.io/online-tools/sha256.html, input encoding UTF-8 Note: A person had issue with receiving multiple G points. The one that is used in the challenge ends in 727.
from sage.allimport * import random from Crypto.Util.number import getPrime, isPrime, long_to_bytes, bytes_to_long from Crypto.Util.Padding import pad from Crypto.Cipher import AES from hashlib import sha256
FLAG = b'REDACTED' secret = REDACTED b_curve = REDACTED
p = 1154543773027194978300531105544404721440832315984713424625039 g = 2 shared = pow(g,secret,p)
#p-1 = 2*641*900580166167858797426311314777226771794720995307888786759 p = 1154543773027194978300531105544404721440832315984713424625039 g = 2 shared = 589382223336825905353017404337901190007770052877203421235378
r = 900580166167858797426311314777226771794720995307888786759 t1 = 364745943990966274208559347028377806262751076921350982122 t2 = 31653772803698764866405663325365133454945445328775270048 secret = t2*inverse(t1,r) % r assert sha256(str(secret).encode()).hexdigest() == "f8f099dd278c23d44387914def5fc20bc0fb8915d916775e5b4b17275e2107bd"
######################################################################## part2 secret2 p = 4470115461512684340891257138125051110076800700282905015819080092370422104067183317016903680000000000000001 a = 35220 P = (623096442003276996005526819582785620084071954736463701753970373963912716099078435477704571257942074621357 , 2384627087675194082373873481003992989604314757777638917742582544298077858048626428935086473569758082583040) x,y = P b = (y^2 - (x^3 + a*x)) % p E = EllipticCurve(GF(p), [a,b]) r = 17229968799761915831961648039300568398720910455733592221895507193323 Z = GF(p) G = -E.lift_x(Z(secret)) P = E(P)
n = G.order() ordd = n // r secret2 = (r*P).log(r*G)
F = RealField(3456) x = vector(F, [x1, x2]) y = vector(F, [y1, y2])
for _ inrange(10000): theta = F.random_element(min=-5 * pi, max=5 * pi) R = matrix(F, [[cos(theta), -sin(theta)], [sin(theta), cos(theta)]]) x = R * x y = R * y
In the aftermath, a few masterminds behind the operation managed to escape and have now relaunched their scheme. We've intercepted their encrypted communications, but we need your expertise to decrypt and decipher them.
from ecdsa import ellipticcurve from ecdsa.ecdsa import curve_256, generator_256, Public_key, Private_key import random import hashlib from Crypto.Cipher import AES from Crypto.Util.Padding import pad from Crypto.Util.number import long_to_bytes,bytes_to_long
FLAG = b'TFCCTF{REDACTED}'
G = generator_256 order = G.order() rand = random.randint(1,order-1) pubkey = Public_key(G, rand*G) privkey = Private_key(pubkey, rand)
messages = [ "Initiate phase two, we've secured new access points.", "Encryption keys updated. Proceed with the new protocol.", "Ensure all communications are routed through the secure channel.", "Begin the new vote manipulation sequence at dawn.", "The second wave of votes is in. Validate and proceed.", "Our agents are in place. Execute the next steps discreetly.", "Double-check all encrypted transmissions for vulnerabilities.", "We've detected interference. Activate the countermeasures.", "Final preparations complete. Awaiting your signal.", "The rigging algorithm is updated. Deploy immediately." ]
a = 733113379677 b = 657 m = 2 ** 150 state = random.randint(0,2**150-1) defpatched_random(): global a,state,b,m state = (a * state + b) % m
defthe_random(): patched_random() global state nonce = ((random.getrandbits(106) & (random.getrandbits(106) ^ random.getrandbits(106)) | random.getrandbits(106)) << 150) | state return nonce, state >> 110
max_used = 50 used = 0 print('What do you want to do?') print('1. Listen in on conversation') print('2. Submit the info you found') print('3. Get pubkey') whileTrue: data = input()
if data == '1': if used > max_used: print('Why do you need to sign more than 50 times?') exit()
if key == rand: print('At last, they are gone, as payment you receive the flag.') print(FLAG) exit()
if data == '3':
print('Too easy if i just give it to you right? Try and solve this, i need P.x and Q.x')
# Sage just refused to work in docker, so ill give you the values # from sage.all import GF,EllipticCurve
# curve secp256r1 # p = 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff # a = 0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc # b = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b # F = GF(p) # E = EllipticCurve(F, [a, b]) # G = E((0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296, 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5)) # k1 = REDACTED # k2 = REDACTED # P = k1 * G # Q = k2 * G # print(P,Q) # for i in range(2): # a,b = random # out.append([a,b,a * P + b * Q])
import hashlib from Crypto.Cipher import AES from Crypto.Util.number import * from Pwn4Sage.pwn import * import json
############################################################################## parameters p = 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff K = GF(p) a = K(0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc) b = K(0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b) E = EllipticCurve(K, (a, b)) G = E(0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296, 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5) n = E.order() a = 733113379677 b = 657 m = 2 ** 150 messages = [ "Initiate phase two, we've secured new access points.", "Encryption keys updated. Proceed with the new protocol.", "Ensure all communications are routed through the secure channel.", "Begin the new vote manipulation sequence at dawn.", "The second wave of votes is in. Validate and proceed.", "Our agents are in place. Execute the next steps discreetly.", "Double-check all encrypted transmissions for vulnerabilities.", "We've detected interference. Activate the countermeasures.", "Final preparations complete. Awaiting your signal.", "The rigging algorithm is updated. Deploy immediately." ]
############################################################################## part1 get data sh = remote("challs.tfcctf.com", 30371) sh.recvuntil(b"3. Get pubkey\n") res = [] nums = 10 for i inrange(nums): sh.sendline(b"1") res.append(json.loads(sh.recvline().strip().decode().replace("'","\"")))
############################################################################## part2 LCG-HNP to get state h = [i["leak"]*2^110for i in res] L1 = Matrix(ZZ,2*nums,2*nums) for i inrange(nums): L1[i,i] = 1 L1[nums,nums] = 2^109 for i inrange(nums-1): L1[i,nums+1+i] = a L1[i+1,nums+1+i] = -1
ci = b + a*h[i] - h[i+1] L1[nums,nums+1+i] = ci L1[nums+1+i,nums+1+i] = m L1[:,-nums+1:] *= m res1 = L1.LLL()[0][:nums] state = [i["leak"]*2^110+abs(j) for i,j inzip(res,res1)] assert (state[0]*a + b) % m == state[1]
############################################################################## part3 ECDSA-HNP h = [i["hash"] for i in res] r = [int(i["r"],16) for i in res] s = [int(i["s"],16) for i in res] L2 = Matrix(ZZ,2*nums,2*nums) for i inrange(nums): L2[i,i] = 1 L2[nums,nums] = 2^105 for i inrange(1,nums): L2[0,nums+i] = s[0]*r[i]*2^150 L2[i,nums+i] = -s[i]*r[0]*2^150
ci = h[i]*r[0]-h[0]*r[i]-s[i]*r[0]*state[i]+s[0]*r[i]*state[0] L2[nums,nums+i] = ci L2[nums+i,nums+i] = n L2[:,-nums+1:] *= n
res = L2.LLL()[0][:nums] k0 = abs(res[0])*2^150+state[0] d = (s[0]*k0-h[0])*inverse(r[0],n) % n
############################################################################## part4 get flag sh.sendline(b"2") sh.sendline(str(d).encode()) sh.recvline() print(sh.recvline())
#TFCCTF{y0u_end3D_Th3_c0rruption}
exp(option 3):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
from Crypto.Util.number import *
p = 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff a = 0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc b = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b F = GF(p) E = EllipticCurve(F, [a, b]) G = E((0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296, 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5)) out = [[96782493581107017213287940431906781117775401216029818502421722067004869671128, 18538871412723353247501824844190723767417562653982776603661341114579966744605, (36566315207713245091176405415710241138207929913023468223244125431056706761511,10976749572513729423697994197166601856770738841401887139027157647878298794556)], [90010878197235881814460760613107101266952873141567869967076779247935202848887, 95977587327198575377209621701343555951978156791715802180542478952260780398694, (42891521857367877217539705137361753088078433669344096955944120767916016902990,24699094435692184568058588241787734516749698041094295443543822154915466799108)]]
if(0): dic = {} for w in tqdm(product(chars,repeat=3),total=len(chars)**3): for x in product(chars,repeat=3): w = "".join(list(w)) x = "".join(list(x)) k1 = sha256(w.encode()).digest() k2 = sha256(x.encode()).digest() AES_w = AES.new(k1, AES.MODE_ECB) AES_x = AES.new(k2, AES.MODE_ECB) dic[AES_x.decrypt(AES_w.decrypt(ct))] = (w,x)
for y in tqdm(product(chars,repeat=3),total=len(chars)**3): for z in product(chars,repeat=3): y = "".join(list(y)) z = "".join(list(z)) k3 = sha256(y.encode()).digest() k4 = sha256(z.encode()).digest() AES_y = AES.new(k3, AES.MODE_ECB) AES_z = AES.new(k4, AES.MODE_ECB) if(AES_y.encrypt(AES_z.encrypt(pt)) in dic.keys()): print(dic[AES_y.encrypt(AES_z.encrypt(pt))],y,z)
n = 11570808501273498927205104472079357777144397783547577003261915477370622451850206651910891120280656785986131452685491947610185604965099812695724757402859475642728712507339243719470339385360489167163917896790337311025010411472770004154699635694228288241644459059047022175803135613130088955955784304814651652968093606122165353931816218399854348992145474578604378450397120697338449008564443654507099674564425806985914764451503302534957447420607432031160777343573246284259196721263134079273058943290282037058625166146116257062155250082518648908934265839606175181213963034023613042840174068936799861096078962793675747202733 e = 65537 c = 7173375037180308812692773050925111800516611450262181376565814072240874778848184114081029784942289615261118103256642605595499455054072839201835361613983341298973366881719999836078559255521052298848572778824157749016705221745378832156499718149327219324078487796923208917482260462508048311400560933782289383624341257636666638574026084246212442527379161504510054689077339758167386002420794571246577662116285770044542212097174474572856621921237686119958817024794843805169504594110217925148205714768001753113572920225449523882995273988088672624172009740852821725803438069557080740459068347366098974487213070886509931010623
n = 11570808501273498927205104472079357777144397783547577003261915477370622451850206651910891120280656785986131452685491947610185604965099812695724757402859475642728712507339243719470339385360489167163917896790337311025010411472770004154699635694228288241644459059047022175803135613130088955955784304814651652968093606122165353931816218399854348992145474578604378450397120697338449008564443654507099674564425806985914764451503302534957447420607432031160777343573246284259196721263134079273058943290282037058625166146116257062155250082518648908934265839606175181213963034023613042840174068936799861096078962793675747202733 e = 65537 c = 7173375037180308812692773050925111800516611450262181376565814072240874778848184114081029784942289615261118103256642605595499455054072839201835361613983341298973366881719999836078559255521052298848572778824157749016705221745378832156499718149327219324078487796923208917482260462508048311400560933782289383624341257636666638574026084246212442527379161504510054689077339758167386002420794571246577662116285770044542212097174474572856621921237686119958817024794843805169504594110217925148205714768001753113572920225449523882995273988088672624172009740852821725803438069557080740459068347366098974487213070886509931010623
L = Matrix(ZZ,102,102) for i inrange(100): L[i,i] = 1 L[i,-1] = pow(i+0x1337, e, n) L[-2,-2] = 1 L[-2,-1] = c L[-1,-1] = n L[:,-1:] *= n res = L.LLL() flag = res[0][:-2] for i in flag: print(chr(i), end="")
import os from sage.allimport * set_random_seed(1337) Fp = GF(6143872265871328074704442651454311068421530353607832481181) a, b = Fp.random_element(), Fp.random_element()
flag = (os.getenv('flag') or'crew{submit_this_if_desperate}').encode() s = Fp.from_integer(int.from_bytes(flag[len('crew{'):-len('}')], 'big'))
#aim to construct a matrix of c=a*b%v defconstruct_poly_mul_mat(n,v,b): assert v[-1] == 1#use this after monic
#step1 construct a matrix of d=a*b mat1 = Matrix(ZZ,n,2*n-1) for i inrange(n): for j inrange(n): mat1[i,j+i] = b[j]
#step2 construct a matrix of c=d%v mat2 = Matrix(ZZ,2*n-1,n) for i inrange(n): mat2[i,i] = 1 for i inrange(n,2*n-1): for j inrange(i-n,n): mat2[i,j] = -v[j-(i-n)] init_row = vector(ZZ,n*[0]) for j inrange(i-n): temp = -v[n-1-j]*vector(ZZ,mat2[i-j-1]) init_row += temp for j inrange(n): mat2[i,j] += init_row[j] #step3 multiply them and return return(mat1*mat2)
#################################################### part1 construct modpoly set_random_seed(1337) p = 18315300953692143461 F = GF(p) Fp = GF(p^3) mod = Fp.modulus() a, b = Fp.random_element(), Fp.random_element() aa, bb = a, b s = Fp.random_element()
A = construct_poly_mul_mat(3,mod.list(),a) #print(a*s) #print(vector(F,s.list())*A)
#################################################### part2 construct Lattice shift = 57 out = [50, 32, 83, 12, 49, 34, 81, 101, 46, 108, 106, 57, 105, 115, 102, 51, 67, 34, 124, 15, 125, 117, 51, 124, 38, 10, 30, 76, 125, 27, 89, 14, 50, 93, 88, 56] nums = len(out)//3 h = [out[3*i:3*i+3] for i inrange(nums)] a, b = vector(F,a.list()), vector(F,b.list()) L = Matrix(ZZ,3*nums+1+3*(nums-1),3*nums+1+3*(nums-1)) for i inrange(nums): L[3*i,3*i] = 1 L[3*i+1,3*i+1] = 1 L[3*i+2,3*i+2] = 1 L[3*nums,3*nums] = 2^shift
for i inrange(nums-1): for j inrange(3): L[3*i,3*nums+1+3*i+j] = A[0,j] L[3*i+1,3*nums+1+3*i+j] = A[1,j] L[3*i+2,3*nums+1+3*i+j] = A[2,j]
L[3*i+3,3*nums+1+3*i] = -1 L[3*i+4,3*nums+1+3*i+1] = -1 L[3*i+5,3*nums+1+3*i+2] = -1 si, si_1 = vector(F, [j*2^shift for j in h[i]]), vector(F, [j*2^shift for j in h[i+1]]) ci = si*A + b - si_1 L[3*nums,3*nums+1+3*i] = ZZ(ci[0]) L[3*nums,3*nums+1+3*i+1] = ZZ(ci[1]) L[3*nums,3*nums+1+3*i+2] = ZZ(ci[2])
L[3*nums+1+3*i,3*nums+1+3*i] = p L[3*nums+1+3*i+1,3*nums+1+3*i+1] = p L[3*nums+1+3*i+2,3*nums+1+3*i+2] = p
L[:,-3*(nums-1):] *= p res = L.LLL()[0] s0l = res[:3] print(res)
s0 = [ZZ(i+j*2^shift) for i,j inzip(s0l,out[:3])] s = s0[0]*p^0 + s0[1]*p^1 + s0[2]*p^2 s = Fp.from_integer(s) s = (s-bb)*aa^(-1) print(long_to_bytes(s.to_integer()))
from Crypto.Util.number import getPrime, long_to_bytes, bytes_to_long from secrets import randbelow, choice from hashlib import sha256 from Crypto.Cipher import AES from Crypto.Util.Padding import pad from string import ascii_letters, digits from flag import flag
key = "".join([choice(ascii_letters + digits) for i inrange(150)]).encode()
blocklen = len(key)//5 polsize = 25 maxexp = 3
V, W, X, Y, Z = [bytes_to_long(b) for b in [key[blocklen*i:blocklen*i+blocklen] for i inrange(5)]]
n = getPrime(4096)*getPrime(4096)*getPrime(4096)*getPrime(4096) n = n^2
K = Zmod(n) PP.<v, w, x, y, z> = PolynomialRing(K)
coeffs = [K.random_element() for i inrange(polsize)]
pol = 0 for l inrange(polsize): whileTrue: i, j, k, s, t = [randbelow(maxexp) for _ inrange(5)] if i == j == k == s == t == 0: continue pol = pol + coeffs[l] * v^i * w^j * x^k * y^s * z^t iflen(pol.coefficients()) == l + 1: break
c = pol(V, W, X, Y, Z) pol = pol - c assert pol(V, W, X, Y, Z) == 0
from Crypto.Util.number import * from hashlib import sha256 from Crypto.Cipher import AES
blocklen = 30 K = Zmod(n) PP.<v, w, x, y, z> = PolynomialRing(K) n = pol = c = '92bbb516b6e04ac3c39df6834328028fc4525cd5c97eb220bf7be20d6d6db596'
coeff = pol.coefficients() print(pol.monomials())
L = block_matrix(ZZ,[ [1,Matrix(ZZ,coeff).T], [0,n] ]) for i,j inenumerate(pol.monomials()): L[i,i] *= (256^30)^(9-j.degree()) L[:,-1:] *= n res = L.LLL()
tt = [] for i,j inzip(pol.monomials(),res[0]): tt.append(j // (256^30)^(9-i.degree()))
xx = GCD(tt[-2],tt[-3]) ww = tt[-2] // xx zz = tt[-4] // xx // ww vv = int(sqrt(tt[-7] // xx // zz)) yy = tt[-5] // vv // zz // xx
defchecktoken(key): token = bytes.fromhex(gethexinput("token(hex): ").decode()) iflen(token) % blksize != 0orlen(token) <= 2*blksize: print("token is invalid.") returnNone iv = token[:blksize] tag = token[-blksize:] enc = token[blksize:-blksize]
cipher = AES.new(key, AES.MODE_GCM, nonce=iv) try: dec = unpad(cipher.decrypt_and_verify(enc, tag), blksize) except ValueError: print("tag may be invalid.") returnNone if dec == b"admin": print("congrats, give you the flag.") flag = open('/flag.txt', 'rb').read().strip().decode() print(flag) return1 else: print("you are not admin user.") return0
defmenu(): menu = [ "", "0: get token", "1: check admin token" ] print('\n'.join(menu))
defmain(): key = urandom(blksize) i = 0 banner() try: while (True): menu() opt = getintinput("option(int): ") if opt == 0: result = givetoken(key, i) if result isNone: break curusername, iv, enc, tag = result print(f"token for {curusername} is: \n{iv + enc + tag}") i += 1 elif opt == 1: result = checktoken(key) if result isNoneor result == 1: break else: break except: print("error occured.") exit(1)
from Pwn4Sage.pwn import * from Crypto.Util.number import long_to_bytes as lb from Crypto.Util.number import bytes_to_long as bl from binascii import unhexlify, hexlify import struct from Crypto.Cipher import AES
defbytes_to_polynomial(block, a): poly = 0 # pad to 128 bin_block = bin(bl(block))[2 :].zfill(128) # reverse it to count correctly, wrong don't reverse it lol # bin_block = bin_block[::-1] for i inrange(len(bin_block)): poly += a^i * int(bin_block[i]) return poly
L = struct.pack(">QQ", 0 * 8, len(C1) * 8) C1_p = [bytes_to_polynomial(C1[i], a) for i inrange(len(C1))] C2_p = [bytes_to_polynomial(C2[i], a) for i inrange(len(C2))] C3_p = [bytes_to_polynomial(C3[i], a) for i inrange(len(C3))] T1_p = bytes_to_polynomial(T1, a) T2_p = bytes_to_polynomial(T2, a) L_p = bytes_to_polynomial(L, a) # Here G_1 is already modified to include the tag defmake_poly(C_p,L_p): G = 0 for c in C_p + [L_p]: G += c G *= x return G
defhamming_weight(x): returnsum([int(y) for y inbin(x)[2:]])
whileTrue: print("I can encrypt anything for you! But the bits may get messy") msg=input() if msg=="guess": print("Do you know the secret?") msg=int(input()) if msg==Secret: print("You sure do! Here is your prize:") print(FLAG) exit(0) else: print("Wrong answer!") exit(0) msg=int(msg) enc=pow(msg,3,n) print(hamming_weight(enc)%2)
defhamming_weight(x): returnsum([int(y) for y inbin(x)[2:]])
################################################################################# below is my test defstable_test(a): for i inrange(15): if(hamming_weight(pow(a+i,3)) % 2 != hamming_weight(powmod(a+i,3,n)) % 2): returnFalse returnTrue
a = int(iroot(sig,3)[0]) #sig < n --> a^3 < n
for i inrange(340,-1,-1): a += 2**i if(stable_test(a)): continue else: a -= 2**i
t1 = (a)**3 t2 = (a+15)**3 assert t1 < n and t2 > n