defPollards_p_1(N): a = 2 n = 2 whileTrue: a = pow(a, n, N) res = gmpy2.gcd(a-1, N) if res != 1and res != N: print ('n ='), n print ('p ='), res return res n += 1 e = 0x10001 n = ....#省略 enc = ....#省略 p = Pollards_p_1(n) q = n // p assert p*q == n d = gmpy2.invert(e, (p-1)*(q-1)) m = pow(enc, d, n) print(long_to_bytes(m))
REPEAT——pwn(*)
0829
考点:ret2libc
1.exploit
from pwn import * from LibcSearcher import *
#sh = process('../vuln') sh = remote('1.container.jingsai.apicon.cn',31547) elf = ELF('../vuln')
from gmpy2 import isqrt, invert from sympy import isprime
n = 90938392456291254525076945024729747747760465730974669418706248227861886309375529663735627415245417634394729586919547147792773340180198391198314430274518933727253498184961383171260516587457792799707287325039224408079898775410581161837617160304998792211064631247289074677708936697313549567484094343014332084339 e = 65537 c = 45364797306235971944260226800635151351008146862295270630931485461400677931474845001179392584827173887899973869918030404844235023951265629425837039670402124263530793496501526611946694844071734068320021250788698711162051512227685780168559259017820474609757591818839612837707915716391578907729532072964242156512
x = isqrt(n) # 一个开平方整数 whilenot (isprime(x) and isprime(n//x)): x-=1 p = x q = n // x # “//” 是整除 # 保证p和q也是素数
phi_n = (p - 1) * (q - 1) # ϕ(n)=(p−1)(q−1)
d = invert(e, phi_n) #d 为 e 关于模 ϕ(n) 的逆元
m = pow(c, d, n)
print(m)
# Convert the number to bytes b_res = m.to_bytes((m.bit_length() + 7) // 8, 'big') # ->结果 = m转化为字节形式(位长 + 7 // 8 = 字节长度,大端模式)
# Decode the bytes to a string using UTF-8 encoding str_res = b_res.decode('utf-8')
print(str_res)
modulus——crypto
方法一:
from Crypto.Util.number import * from decimal import Decimal, getcontext
# 已知的值写在这里,由于太多就省略了
# 使用中国剩余定理求解C N = n1 * n2 * n3 N1 = N // n1 N2 = N // n2 N3 = N // n3 u1 = inverse(N1, n1) u2 = inverse(N2, n2) u3 = inverse(N3, n3)
C = (c1 * u1 * N1 + c2 * u2 * N2 + c3 * u3 * N3) % N
# Step 1: Recover the key from the known prefix key = bytearray() for i inrange(len(known_prefix)): key.append(known_prefix[i] ^ enc[i]) print(key)
# Now you have the first few bytes of the key. Let's attempt to decrypt the rest of the message. # As the key is only 6 bytes, we need only the first 6 bytes to decrypt the entire message.
decrypted = bytearray() for i inrange(len(enc)): decrypted.append(enc[i] ^ key[i % 6])