Triple DES

문제

데이터 암호화 표준은 AES의 선구자였으며, 결제 카드 산업과 같은 일부 느린 분야에서 여전히 널리 사용되고 있다.
이 문제는 보안 블록 암호가 가져서는 안 되는 DES의 이상한 약점을 보여준다.

풀이

더보기
from Crypto.Cipher import DES3
from Crypto.Util.Padding import pad


IV = os.urandom(8)
FLAG = ?


def xor(a, b):
    # xor 2 bytestrings, repeating the 2nd one if necessary
    return bytes(x ^ y for x,y in zip(a, b * (1 + len(a) // len(b))))



@chal.route('/triple_des/encrypt/<key>/<plaintext>/')
def encrypt(key, plaintext):
    try:
        key = bytes.fromhex(key)
        plaintext = bytes.fromhex(plaintext)
        plaintext = xor(plaintext, IV)

        cipher = DES3.new(key, DES3.MODE_ECB)
        ciphertext = cipher.encrypt(plaintext)
        ciphertext = xor(ciphertext, IV)

        return {"ciphertext": ciphertext.hex()}

    except ValueError as e:
        return {"error": str(e)}


@chal.route('/triple_des/encrypt_flag/<key>/')
def encrypt_flag(key):
    return encrypt(key, pad(FLAG.encode(), 8).hex())

 

코드를 보면 

IV값은 8바이트로 임의의 값으로 이루어 져있고

 

encrypt함수를 보게 되면 키값과 평문을 받고 바이트화 시킨 후,

평문과 IV을 xor연산 후, 평문에 저장한다.

 

그리고 cipher를 보면 키와 des에 ecb모드로 암호화를 시킨다.

그리고 암호문 변수에 평문과 cipher와 암호화를 시킨후

암호문과 IV값과 xor연산을 하고, 암호문을 출력 시킨다.

 

그리고 encrypt_flag는 키값을 받고

키값, 플래그를 인코딩 시킨후 8바이트 단위로 패딩 시킨후 암호화 시키고,

해당 암호화된 값을 반환한다.

 

먼저 DES의 취약점 중 취약키가 있으며,

'취약키'와 '준 취약키'가 있으며 몇몇 특정된 키들이 있다고 한다.

이들은 암호화와 복호화 모드에서 동일하게 작동된다고 한다.

 

https://en.wikipedia.org/wiki/Weak_key#:~:text=undisciplined%20machine%20operators.-,Weak%20keys%20in%20DES,-%5Bedit%5D

.

 

Weak key - Wikipedia

From Wikipedia, the free encyclopedia In cryptography, a weak key is a key, which, used with a specific cipher, makes the cipher behave in some undesirable way. Weak keys usually represent a very small fraction of the overall keyspace, which usually means

en.wikipedia.org

 

해당 위키사이트에서 취약한 키들의 모아 놓은 것이 있어
대입을 해보거나, 둘을 섞어 사용하여

키중 0x0000000000000000과 0xFFFFFFFFFFFFFFFF를 붙여서 키를 대입하였다.

 먼저 키를 대입하여 암호화된 플래그값을 얻었고,

해당 암호화 문을 얻어, 이를 사이트에서 제공하는 HEX ENCODER에 넣어

 

플래그 값을 얻었다.

플래그값은 crypto{n0t_4ll_k3ys_4r3_g00d_k3ys}이다.

 

 

복사했습니다!