[CryptoHack] Export-grade

이번엔 Alice와 Bob 사이에서 parameter를 협상하는 단계가 있는 것 같다. 한번 살펴보도록 하자.

이렇게 받아오고 있다. 일단 뭔지 모르겠으니 그대로 Bob에게 보내봐야겠다.

Bob이 DH1024를 골랐다.

그대로 Alice에게 보내주면, Alice에게서 p, g, A를, Bob에게서 B를, 그리고 Alice에게서 iv와 encrypted_flag를 한번에 받는다. 이전 문제와 비슷하지만 Eve가 중간에서 parameter를 조작할 수 없는 상황이다.

 

그런데 처음에 Alice와 Bob이 통신하는 부분이 살짝 의심이 간다. DH뒤의 숫자가 왠지 bit일 것 같다는 생각이 들었다.

그래서 Alice의 supported를 바꿔서 Bob에게 보내보았다.

정확했다! DH64로 보냈더니 p와 A, B가 보내 64bit였다.

64bit이므로 Pohlig-Hellman algorithm을 이용한다면 마지막 전수조사에서 충분히 값을 얻어낼 수 있을 것이라고 생각했다. (bit가 커지면 힘듦)

Pohlig-Hellman algorithnm을 이용해서 A=2^a mod p에서 a를 알아냈다.

Pohlig-Hellman Algorithm은 위와 같은 알고리즘으로 이산대수 문제를 푸는 것이다.

따라서 Alice의 개인키 a는 3304556024286719681였다.

위에서 얻은 정보들로 코드를 작성해줬다.

from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import hashlib
import pwn
import json


def is_pkcs7_padded(message):
    padding = message[-message[-1]:]
    return all(padding[i] == len(padding) for i in range(0, len(padding)))


def decrypt_flag(shared_secret: int, iv: str, ciphertext: str):
    # Derive AES key from shared secret
    sha1 = hashlib.sha1()
    sha1.update(str(shared_secret).encode('ascii'))
    key = sha1.digest()[:16]
    # Decrypt flag
    ciphertext = bytes.fromhex(ciphertext)
    iv = bytes.fromhex(iv)
    cipher = AES.new(key, AES.MODE_CBC, iv)
    plaintext = cipher.decrypt(ciphertext)

    if is_pkcs7_padded(plaintext):
        return unpad(plaintext, 16).decode('ascii')
    else:
        return plaintext.decode('ascii')

p=0xde26ab651b92a129
A=0x6c0810650778255a
B=0x623748025c3fc53c
a=3304556024286719681

shared_secret = pow(B,a,p)
iv = "19998a6836895ea4e97df16e4a1e429d"
ciphertext = "dd162c70fd6b5a4b268d8bbf0664906a87ac7cb1b99beeaef1813916c5d4cf03"

print(decrypt_flag(shared_secret, iv, ciphertext))

 

실행결과는 다음과 같았다.

flag를 얻어냈다!

🚩 crypto{d0wn6r4d35_4r3_d4n63r0u5}

 

댓글 달기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

위로 스크롤