워게임/CryptoHack
[Cryptohack] RSA (Modulus Inutilis, Everything is Big)
- -
Modulus Inutilis
문제
내 소수들은 지금 충분히 커야 합니다! |
풀이
더보기
#!/usr/bin/env python3
from Crypto.Util.number import getPrime, inverse, bytes_to_long, long_to_bytes
e = 3
d = -1
while d == -1:
p = getPrime(1024)
q = getPrime(1024)
phi = (p - 1) * (q - 1)
d = inverse(e, phi)
n = p * q
flag = b"XXXXXXXXXXXXXXXXXXXXXXX"
pt = bytes_to_long(flag)
ct = pow(pt, e, n)
print(f"n = {n}")
print(f"e = {e}")
print(f"ct = {ct}")
pt = pow(ct, d, n)
decrypted = long_to_bytes(pt)
assert decrypted == flag
전에 올렸던 문제에서 e=1일때의 경우를 봤었다.
그러면 현재는 e의 값이 3이기때문에
ct = (평문 ^ e) mod n
ct의 세제곱근 = 평문 mod n
이때 평문값이 n보다 적기 때문에
ct의 세제곱근이 평문이라는걸 알 수 있다.
from gmpy2 import iroot
from Crypto.Util.number import long_to_bytes
n = 17258212916191948536348548470938004244269544560039009244721959293554822498047075403658429865201816363311805874117705688359853941515579440852166618074161313773416434156467811969628473425365608002907061241714688204565170146117869742910273064909154666642642308154422770994836108669814632309362483307560217924183202838588431342622551598499747369771295105890359290073146330677383341121242366368309126850094371525078749496850520075015636716490087482193603562501577348571256210991732071282478547626856068209192987351212490642903450263288650415552403935705444809043563866466823492258216747445926536608548665086042098252335883
e = 3
ct = 243251053617903760309941844835411292373350655973075480264001352919865180151222189820473358411037759381328642957324889519192337152355302808400638052620580409813222660643570085177957
pl = iroot(ct,e)[0]
print(long_to_bytes(pl))
플래그값은 crypto{N33d_m04R_p4dd1ng}이다.
Everything is Big
문제
우리 회사에는 슈퍼컴퓨터가 있기 때문에 대량의 숫자를 선택하여 암호화를 안전하게 보호할 수 있습니다! |
풀이
더보기
from Crypto.Util.number import getPrime, bytes_to_long
FLAG = b"crypto{?????????????????????????}"
m = bytes_to_long(FLAG)
def get_huge_RSA():
p = getPrime(1024)
q = getPrime(1024)
N = p*q
phi = (p-1)*(q-1)
while True:
d = getPrime(256)
e = pow(d,-1,phi)
if e.bit_length() == N.bit_length():
break
return N,e
N, e = get_huge_RSA()
c = pow(m, e, N)
print(f'N = {hex(N)}')
print(f'e = {hex(e)}')
print(f'c = {hex(c)}')
먼저 코드를 보게되면 d의 값이 256비트 소수로 p나 q보다 작은걸 알수 있다.
https://cryptohack.gitbook.io/cryptobook/untitled/low-private-component-attacks/wieners-attack
Wiener`s attack이라는 공격은 d의값이 작으면 가능한 공격이라고 한다.
참고로 pip로 owiener라는 라이브러리를 제공하니 이를 이용하게 된다면
import owiener
from Crypto.Util.number import long_to_bytes
N = 0x8da7d2ec7bf9b322a539afb9962d4d2ebeb3e3d449d709b80a51dc680a14c87ffa863edfc7b5a2a542a0fa610febe2d967b58ae714c46a6eccb44cd5c90d1cf5e271224aa3367e5a13305f2744e2e56059b17bf520c95d521d34fdad3b0c12e7821a3169aa900c711e6923ca1a26c71fc5ac8a9ff8c878164e2434c724b68b508a030f86211c1307b6f90c0cd489a27fdc5e6190f6193447e0441a49edde165cf6074994ea260a21ea1fc7e2dfb038df437f02b9ddb7b5244a9620c8eca858865e83bab3413135e76a54ee718f4e431c29d3cb6e353a75d74f831bed2cc7bdce553f25b617b3bdd9ef901e249e43545c91b0cd8798b27804d61926e317a2b745
e = 0x86d357db4e1b60a2e9f9f25e2db15204c820b6e8d8d04d29db168c890bc8a6c1e31b9316c9680174e128515a00256b775a1a8ccca9c6936f1b4c2298c03032cda4dd8eca1145828d31466bf56bfcf0c6a8b4a1b2fb27de7a57fae7430048d7590734b2f05b6443ad60d89606802409d2fa4c6767ad42bffae01a8ef1364418362e133fa7b2770af64a68ad50ad8d2bd5cebb99ceb13368fb31a6e7503e753f8638e21a96af1b6498c18578ba89b98d70fa482ad137d28fe701b4b77baa25d5e84c81b26ee9bddf8cbb51a071c60dd57714de379cd4bc14932809ba18524a0a18e4133665cfc46e2c4fcfbc28e0a0957e5513a7307c422b87a6182d0b6a074b4d
c = 0x6a2f2e401a54eeb5dab1e6d5d80e92a6ca189049e22844c825012b8f0578f95b269b19644c7c8af3d544840d380ed75fdf86844aa8976622fa0501eaec0e5a1a5ab09d3d1037e55501c4e270060470c9f4019ced6c4e67673843daf2fd71c64f3dd8939ae322f2b79d283b3382052d076ebe9bb50b0042f1f7dd7beadf0f5686926ade9fc8370283ead781a21896e7a878d99e77c3bb1f470401062c0e0327fd85da1cf12901635f1df310e8f8c7d87aff5a01dbbecd739cd8f36462060d0eb237af8d613e2d9cebb67d612bcfc353ef2cd44b7ac85e471287eb04ae9b388b66ea8eb32429ae96dba5da8206894fa8c58a7440a127fceb5717a2eaa3c29f25f7
d = owiener.attack(e, N)
pt=long_to_bytes(pow(c, d, N))
print(pt)
플래그 값은 crypto{s0m3th1ng5_c4n_b3_t00_b1g}이다.
'워게임 > CryptoHack' 카테고리의 다른 글
[Cryptohack] RSA (Infinite Descent) (0) | 2023.05.21 |
---|---|
[Cryptohack] RSA (Crossed Wires, Everything is Still Big) (0) | 2023.05.20 |
[Cryptohack] RSA (Manyprime, Salty) (0) | 2023.05.18 |
[Cryptohack] RSA (Monoprime, Square Eyes) (0) | 2023.05.17 |
[Cryptohack] RSA (Factoring, Inferius Prime) (0) | 2023.05.16 |
Contents