본문 바로가기
Security/DreamHack

[Dreamhack] ROT128 #852

by 고간디 2024. 4. 3.
#!/usr/bin/env python3

hex_list = [(hex(i)[2:].zfill(2).upper()) for i in range(256)]

with open('flag.png', 'rb') as f:
    plain_s = f.read()

plain_list = [hex(i)[2:].zfill(2).upper() for i in plain_s]

enc_list = list(range(len(plain_list)))

for i in range(len(plain_list)):
    enc_list[i] = hex_list[(hex_list.index(plain_list[i]) + 128) % 256]

enc_list = ''.join(enc_list)

with open('encfile', 'w', encoding='utf-8') as f:
    f.write(enc_list)

flag가 적혀있을 것으로 추정되는 flag.png의 hex 값들을 128자씩 밀어서 encfile에 utf-8 인코딩을 해놓은 것 같다.

encfile에는 아주아주 긴 헥스값들이 구분없이 나열되어 있다.

 

좀 찾아보니까 카이사르 암호화 방식인가 그렇다고 한다.

 


with open('(Directory)\encfile', 'r', encoding='utf-8') as v:
    encfile = v.read()
v.close()

enc_list = [encfile[i:i+2] for i in range(0, len(encfile), 2)]

hex_list = [(hex(i)[2:].zfill(2).upper()) for i in range(256)]

plain_list = [hex_list[(hex_list.index(enc_list[i]) - 128) % 256] for i in range(len(enc_list))]

# for i in range(len(enc_list)):
#     plain_list[i] = hex(int(enc_list[i], 16))[2:].zfill(2).upper()


flag = bytes.fromhex(''.join(plain_list))

with open('(Directory)/flag.png', 'wb') as f:
    f.write(flag)
f.close

주어진 encfile의 헥스값들을 이용해 역연산하여 flag.png를 복구하는 프로그램을 Python을 사용해 만들어봤다.

문제 파일이 들어가있는 폴더에서 png 복구 프로그램을 만들었는데 환경 변수 문제 때문인지 생성이 자꾸 안 되었다.

encfile도 읽어들이지 못했는데 경로를 절대 경로로 설정해주니까 잘 작동되더라

 


아무튼 실행해서 역연산을 수행 후 생성된 flag.png 이미지를 열어보면 이렇게 플래그를 읽을 수 있게 된다.

728x90
반응형

댓글