# Filter the common false positive on the last byte by # perturbing another byte and re-checking the padding. if pad == 1and index > 0: crafted[index - 1] ^= 1 still_valid = self.oracle(bytes(crafted), block) crafted[index - 1] ^= 1 ifnot still_valid: continue
intermediate[index] = guess ^ pad found = guess print(f"[+] byte {index:02d} recovered: {intermediate[index]:02x}", flush=True) break
if found isNone: raise RuntimeError(f"failed to recover byte {index}")
returnbytes(intermediate)
defforge_admin_cookie(self) -> str: block = os.urandom(BLOCK_SIZE) print(f"[*] chosen ciphertext block: {block.hex()}", flush=True) intermediate = self.recover_intermediate(block) print(f"[*] intermediate D(C1): {intermediate.hex()}", flush=True) iv = bytes(a ^ b for a, b inzip(intermediate, TARGET_PLAIN)) return (iv + block).hex()
from Crypto.Util.number import * from secrets import flag
p = 9259018534502783714631247560818133078409930397939705162361230465031580254504264713899169170790687716589100652406132800533397486109926387016562663961524649 a = 0 b = 6235467631650349040636525320446729529985562949423449382969614887116983248527693872546808737512375916974084741892428681798937790855872528526403738040908493 c = 4165903654767429195543540819098180314477702137507994424192636596518008877139978822038616746899053449640020812062736993008962585578921635697413459959685760 d = 1889382340373247565387211782596794283852946561870564309251998196824383297786878212641581641540685106266683503654620956037368416192796434147249748216284648 e = 3015564788819504594313842562882781366361783108618226049128986996153057550014499326419988348165744003693083108924831219996703133056523468396967900376388617
defadd(P1, P2): if P1 isNone: return P2
x1, y1 = P1 x2, y2 = P2
l = (y2 - y1) * pow(x2 - x1, -1, p) % p x3 = (l**2 + a * l - b - x1 - x2) % p y3 = (l * (x1 - x3) - y1 - a * x3 - c) % p return (x3, y3)
defdouble(P): if P isNone: returnNone
x, y = P
denom = (2 * y + a * x + c) % p num = (3 * x**2 + 2 * b * x + d - a * y) % p l = (num * pow(denom, -1, p)) % p x3 = (l**2 + a * l - b - 2 * x) % p y3 = (l * (x - x3) - y - a * x3 - c) % p return (x3, y3)
defmul(k, P): Q = None while k: if k & 1: Q = add(Q, P) P = double(P) k >>= 1 return Q
m = bytes_to_long(flag) G = (1244884551970947614719458919805713649754289814760243366205012699871413235954279930743612403791919112394457579170253990713250052822262255880036254772609156, 4579639528751113977115209571728128585569082149696598770106934145500742785077382446292613925719404433141749168427443122707253164477493499731016883616496009) P = mul(m, G) print(P)
defadd(P1, P2): l = (y2 - y1) * pow(x2 - x1, -1, p) % p x3 = (l**2 + a * l - b - x1 - x2) % p y3 = (l * (x1 - x3) - y1 - a * x3 - c) % p
defdouble(P): denom = (2 * y + a * x + c) % p num = (3 * x**2 + 2 * b * x + d - a * y) % p l = (num * pow(denom, -1, p)) % p x3 = (l**2 + a * l - b - 2 * x) % p y3 = (l * (x - x3) - y - a * x3 - c) % p
defRSA(): p = getPrime(512) q = getPrime(512) n = p * q k = getPrime(8) m = bytes_to_long(flag) e = inverse(c,(p-1)*(q-1)) cipher = pow(m,e,n) return n,k,cipher
from sage.allimport * from Crypto.Util.number import * from Crypto.Util.Padding import pad from Crypto.Cipher import AES from secret import flag,x1,x2,x3,y,z1,z2,z3,alpha import hashlib
for _ inrange(10): A, B, C = bytes.fromhex(input('A > ')), bytes.fromhex(input('B > ')), bytes.fromhex(input('C > ')) ha, hb, hc = _H(A), _H(B), _H(C) assert ha == hb == hc
D, E, F = bytes.fromhex(input('D > ')), bytes.fromhex(input('E > ')), bytes.fromhex(input('F > ')) hd, he, hf = _H(ha + D), _H(hb + E), _H(hc + F) assert hd == he == hf
if __name__ == "__main__": challenge() signal.alarm(4) guess_hex = input("secret (hex): ").strip() guess = bytes.fromhex(guess_hex) if guess == secret: print(f"Good! Here is your flag: {FLAG}")
from Crypto.Util.number import getRandomNBitInteger from Crypto.Cipher import AES from Crypto.Util.Padding import pad from hashlib import md5 import signal import os
FLAG = os.getenv("FLAG", "xmctf{fake_flag}") classMLFSR: def__init__(self, n, seed, mask, lfsr=None): self.state = [int(b) for b inf"{seed:0{n}b}"] self.mask_bits = [int(b) for b inf"{mask:0{n}b}"] self.n = n self.lfsr = lfsr
defupdate(self): s = sum([self.state[i] * self.mask_bits[i] for i inrange(self.n)]) & 1 self.state = self.state[1:] + [s]
import patcher from sage.allimport * from Crypto.Cipher import AES from Crypto.Hash import SHA256 from Crypto.Util.Padding import pad from secret import SEED , flag
defrandom_prime(n, proof=None, lbound=2): r""" Return a random prime `p` between ``lbound`` and `n`. The returned prime `p` satisfies ``lbound`` `\leq p \leq n`. The returned prime `p` is chosen uniformly at random from the set of prime numbers less than or equal to `n`. INPUT: - ``n`` -- integer `\geq 2` - ``proof`` -- boolean or ``None`` (default: ``None``); if ``False``, the function uses a pseudo-primality test, which is much faster for really big numbers but does not provide a proof of primality. If ``None``, uses the global default (see :mod:`sage.structure.proof.proof`) - ``lbound`` -- integer; `\geq 2`, lower bound for the chosen primes EXAMPLES:: sage: # needs sage.libs.pari sage: p = random_prime(100000) sage: p.is_prime() True sage: p <= 100000 True sage: random_prime(2) 2 Here we generate a random prime between 100 and 200:: sage: p = random_prime(200, lbound=100) sage: p.is_prime() True sage: 100 <= p <= 200 True If all we care about is finding a pseudo prime, then we can pass in ``proof=False`` :: sage: p = random_prime(200, proof=False, lbound=100) # needs sage.libs.pari sage: p.is_pseudoprime() # needs sage.libs.pari True sage: 100 <= p <= 200 True TESTS:: sage: # needs sage.libs.pari sage: type(random_prime(2)) <class 'sage.rings.integer.Integer'> sage: type(random_prime(100)) <class 'sage.rings.integer.Integer'> sage: random_prime(1, lbound=-2) # caused Sage hang #10112 Traceback (most recent call last): ... ValueError: n must be greater than or equal to 2 sage: random_prime(126, lbound=114) Traceback (most recent call last): ... ValueError: there are no primes between 114 and 126 (inclusive) AUTHORS: - Jon Hanke (2006-08-08): with standard Stein cleanup - Jonathan Bober (2007-03-17) """ # since we do not want current_randstate to get # pulled when you say "from sage.arith.misc import *". from sage.structure.proof.proof import get_flag proof = get_flag(proof, "arithmetic") n = ZZ(n) if n < 2: raise ValueError("n must be greater than or equal to 2") if n < lbound: raise ValueError("n must be at least lbound: %s" % (lbound)) elif n == 2: return n lbound = max(2, lbound) if lbound > 2: if lbound == 3or n <= 2 * lbound - 2: # check for Betrand's postulate (proved by Chebyshev) if lbound < 25or n <= 6 * lbound / 5: # see J. Nagura, Proc. Japan Acad. 28, (1952). 177-181 if lbound < 2010760or n <= 16598 * lbound / 16597: # see L. Schoenfeld, Math. Comp. 30 (1976), no 134, 337-360 if proof: smallest_prime = ZZ(lbound - 1).next_prime() else: smallest_prime = ZZ(lbound - 1).next_probable_prime() if smallest_prime > n: raise ValueError("there are no primes between %s and %s (inclusive)" % (lbound, n))
if proof: prime_test = is_prime else: prime_test = is_pseudoprime randint = Zmod(n).random_element count = 0 whileTrue: # In order to ensure that the returned prime is chosen # uniformly from the set of primes it is necessary to # choose a random number and then test for primality. # The method of choosing a random number and then returning # the closest prime smaller than it would typically not, # for example, return the first of a pair of twin primes. p = randint() count = count + 1
if p < 2: continue
if prime_test(p.lift()): return p, count ##这里魔改了,多返回了个count###################################################################
n = 1359289594911861706114410263039030781889501874535854365263922081700238941971104298775704733565166223684142297360239921080802503206559783832007855750334958326368538063619171609885459927586035302195455632768752776216956554963499448005445126927786242177611054827389185330231903625073278897391670581843035646913248732183168634640757720768465749375619368398241192399373901594871829578828976563808877743744957547821735170186252185438724528024345696208656639600908162373375395068724897936084947703562847353279296559280237330305142321357271051046159817216535038453709580872952011626071015235951428711736008848437349560705229