๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿด‍โ˜ ๏ธ CTF ๐Ÿด‍โ˜ ๏ธ/๐Ÿงฎ ์•”ํ˜ธํ•™ ๐Ÿงฎ

[Dream Hack - Crypto] ROT128

๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ํ•ด์„

  1. flag.png ๋ฅผ ์ œ๊ณต๋œ ์ฝ”๋“œ๋กœ ์•”ํ˜ธํ™”ํ•˜์—ฌ encfile ์„ ์ž‘์„ฑํ•˜์˜€๋‹ค.
  2. png ํŒŒ์ผ์„ 128byte ๋งŒํผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฐ€์–ด์„œ utf-8๋กœ ์ธ์ฝ”๋”ฉ ๋œ ์•”ํ˜ธํ™” ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.
  3. ์•”ํ˜ธํ™”๋œ ํŒŒ์ผ์„ byte ๋‹จ์œ„๋กœ ์ฝ์–ด์™€ ๋‹ค์‹œ ์™ผ์ชฝ์œผ๋กœ 128byte ๋งŒํผ ๋ฐ€์–ด bin ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.

 

์ฝ”๋“œ ํ•ด์„

# ์ดˆ๊ธฐ 0๋ถ€ํ„ฐ 255๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ 16์ง„์ˆ˜๋กœ ํ‘œํ˜„ํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
hex_list = [(hex(i)[2:].zfill(2).upper()) for i in range(256)]
# print(hex_list)

# 'flag.png' ํŒŒ์ผ์„ ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ชจ๋“œ๋กœ ์—ด์–ด์„œ ๋‚ด์šฉ์„ ์ฝ์–ด์˜ต๋‹ˆ๋‹ค.
with open('flag.png', 'rb') as f:
    plain_s = f.read()

# 'plain_s' ๋ฐ”์ดํŠธ ์‹œํ€€์Šค๋ฅผ 16์ง„์ˆ˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
plain_list = [hex(i)[2:].zfill(2).upper() for i in plain_s]
# print(plain_list)

# ์•”ํ˜ธํ™”๋œ ๊ฐ’์„ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ ์ธ๋ฑ์Šค ๊ฐ’์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
enc_list = list(range(len(plain_list)))

# ๊ฐ๊ฐ์˜ 16์ง„์ˆ˜ ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด์„œ ์•„๋ž˜์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
for i in range(len(plain_list)):
    hex_b = plain_list[i]  # ํ˜„์žฌ 16์ง„์ˆ˜ ๋ฌธ์ž์—ด
    index = hex_list.index(hex_b)  # ํ•ด๋‹น 16์ง„์ˆ˜ ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.
    enc_list[i] = hex_list[(index + 128) % len(hex_list)]  # ์•”ํ˜ธํ™”๋œ ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๊ณ  ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

# ์•”ํ˜ธํ™”๋œ ๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค.
enc_list = ''.join(enc_list)

# 'encfile'์ด๋ผ๋Š” ํŒŒ์ผ์„ UTF-8 ์ธ์ฝ”๋”ฉ์œผ๋กœ ์—ด์–ด์„œ ์•”ํ˜ธํ™”๋œ ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
with open('encfile_test', 'w', encoding='utf-8') as f:
    f.write(enc_list)

 

ํ’€์ด ์ฝ”๋“œ

# 0๋ถ€ํ„ฐ 255๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ 16์ง„์ˆ˜๋กœ ํ‘œํ˜„ํ•œ ๋ฌธ์ž์—ด ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
hex_list = [hex(i)[2:].zfill(2).upper() for i in range(256)]
print(hex_list)

# 'encfile' ํŒŒ์ผ์„ UTF-8 ์ธ์ฝ”๋”ฉ์œผ๋กœ ์ฝ๊ธฐ ๋ชจ๋“œ๋กœ ์—ด๊ณ  ๋‚ด์šฉ์„ 'enc_s'์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
with open('encfile', 'r', encoding='utf-8') as f:
    enc_s = f.read()

# 'enc_s'์˜ ๋‚ด์šฉ์„ ๋‘ ๊ธ€์ž์”ฉ ๋Š์–ด ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
enc_list = [enc_s[i:i+2] for i in range(0, len(enc_s), 2)]

# ๋””์ฝ”๋”ฉ๋œ ์ธ๋ฑ์Šค๋ฅผ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ 'dec_list'๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
dec_list = list(range(len(enc_list)))

# 'enc_list'์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
for i in range(len(enc_list)):
    # ํ˜„์žฌ ๋‘ ๊ธ€์ž ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ 'hex_list'์—์„œ ์ฐพ์Šต๋‹ˆ๋‹ค.
    index = hex_list.index(enc_list[i])
    
    # ์ธ๋ฑ์Šค๋ฅผ 128์„ ๋นผ๊ณ  ๋ฆฌ์ŠคํŠธ ๊ธธ์ด๋กœ ๋‚˜๋ˆ  ๋””์ฝ”๋”ฉํ•ฉ๋‹ˆ๋‹ค.
    dec_list[i] = hex_list[(index - 128) % len(hex_list)]

# ๋””์ฝ”๋”ฉ๋œ ์ธ๋ฑ์Šค ๋ฆฌ์ŠคํŠธ๋ฅผ 16์ง„์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ”์ดํŠธ ๊ฐ์ฒด๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
dec_s = bytes(int(dec_list[i], 16) for i in range(len(dec_list)))

# ๋ฐ”์ด๋„ˆ๋ฆฌ ์“ฐ๊ธฐ ๋ชจ๋“œ๋กœ 'decfile.png' ํŒŒ์ผ์„ ์—ด๊ณ  ๋ฐ”์ดํŠธ ๊ฐ์ฒด๋ฅผ ์”๋‹ˆ๋‹ค.
with open('decfile.png', 'wb') as f:
    f.write(dec_s)

 

๊ฒฐ๊ณผ

๋ฐ˜์‘ํ˜•