완료됨
최종 솔브 코드 어떻게?

로드맵 강의 천천히 따라가면서 Meet-in-the-middle attack을 실습하고 있습니다.
첨부파일 다운로드 하고 코드 분석, 익스플로잇 설계까지 코드 모두 이해했습니다.

  • 문제 prob.py파일 코드
#!/usr/bin/env python3
from Crypto.Cipher import DES
import signal
import os

if __name__ == "__main__":
    signal.alarm(15)

    with open("flag", "rb") as f:
        flag = f.read()
    
    key = b'Dream_' + os.urandom(4) + b'Hacker'
    key1 = key[:8]
    key2 = key[8:]
    print("4-byte Brute-forcing is easy. But can you do it in 15 seconds?")
    cipher1 = DES.new(key1, DES.MODE_ECB)
    cipher2 = DES.new(key2, DES.MODE_ECB)
    encrypt = lambda x: cipher2.encrypt(cipher1.encrypt(x))
    decrypt = lambda x: cipher1.decrypt(cipher2.decrypt(x))

    print(f"Hint for you :> {encrypt(b'DreamHack_blocks').hex()}")

    msg = bytes.fromhex(input("Send your encrypted message(hex) > "))
    if decrypt(msg) == b'give_me_the_flag':
        print(flag)
    else:
        print("Nope!")
  • 최종 솔브 코드
from pwn import *
from Crypto.Cipher import DES

io = process(["python3", "prob.py"])
# io = remote("host3.dreamhack.games", 18664)

io.recvuntil(b":> ")
hint = bytes.fromhex(io.recvline().decode())

conflict = dict()

for i in range(65536):
    b = i.to_bytes(2, "big")
    cipher = DES.new(b"Dream_" + b, DES.MODE_ECB)
    enc = cipher.encrypt(b"DreamHack_blocks")
    conflict[enc] = b"Dream_" + b

for i in range(65536):
    b = i.to_bytes(2, "big")
    cipher = DES.new(b + b"Hacker", DES.MODE_ECB)
    dec = cipher.decrypt(hint)

    if dec in conflict:
        key1 = conflict[dec]
        key2 = b + b"Hacker"
        break

cipher1 = DES.new(key1, DES.MODE_ECB)
cipher2 = DES.new(key2, DES.MODE_ECB)
encrypt = lambda x: cipher2.encrypt(cipher1.encrypt(x))
assert encrypt(b"DreamHack_blocks") == hint

io.sendlineafter(b'> ', encrypt(b"give_me_the_flag").hex().encode())

flag = eval(io.recvline())
io.close()

print(flag.decode())

강의에서 설명하는 원리와 코드는 이해했는데 이를 환경에 어떻게 적용해서 flag를 얻어야 할지 모르겠습니다.
칼리 리눅스, zsh, pwntools, tqdm, pycryptodome 패키지 모두 설치했습니다.

#암호학 #리눅스 #cryptography
작성자 정보
더 깊이 있는 답변이 필요할 때
드림핵 팀과 멘토에게 직접 문의해 보세요!
답변 1

최종 솔브 코드에는 문제가 없습니다.
로컬에선 flag 파일이 주어지지 않았기 때문에 prob.py 코드의 "flag" 파일을 오픈하는 부분에서 오류가 발생합니다.
만약 로컬에서 테스트하신다면 flag라는 이름의 임의 파일을 추가해주시고,
문제를 푸실 땐 프로세스 접근만 주석처리 되어있는 remote 접근으로 바꿔주시면 해결될 듯 합니다.

2024.08.11. 23:30