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

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

[Dream Hack - Crypto] Private Storage

๋ฐ˜์‘ํ˜•

RC4 ์•”ํ˜ธํ™” ๊ณผ์ •

RC4๋Š” 256 byte ๋น„๋ฐ€ํ‚ค ๋Œ€์นญํ‚ค ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ, ๋™์ผํ•œ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”์‹œ์ง€๋ฅผ ์•”ํ˜ธํ™”ํ•˜๊ณ  ๋ณตํ˜ธํ™”ํ•œ๋‹ค. ์•”ํ˜ธํ™” ๊ณผ์ •์€ KSA(Key Scheduling Algorithm)์™€ PRGA(Pseudo-Random Generation Algorithm) ๋‘ ๊ฐ€์ง€ ์ฃผ์š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค.

 

ํ‚ค ์ŠคํŠธ๋ฆผ ์ƒ์„ฑ

https://www.youtube.com/watch?v=kfdvlaOD1ig&t=172s

S๋ฐฐ์—ด: 0๋ถ€ํ„ฐ 255๊นŒ์ง€ ๊ฐ’์„ ๊ฐ€์ง.
K๋ฐฐ์—ด: key ๊ฐ’์„ ๊ฐ€์ง.
KSA ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด ํ‚ค ์Šค์ผ€์ค„๋ง ์ˆ˜ํ–‰.
PRGA ์•Œ๊ณ ์ฆ˜์„ ํ†ตํ•ด ํ‚ค์ŠคํŠธ๋ฆผ ๋„์ถœ. 

์•”ํ˜ธํ™”

์•”ํ˜ธ๋ฌธ = ํ‰๋ฌธ xor ํ‚ค์ŠคํŠธ๋ฆผ

๋ณตํ˜ธํ™”

ํ‰๋ฌธ = ์•”ํ˜ธ๋ฌธ xor ํ‚ค์ŠคํŠธ๋ฆผ

 

 

๋ฌธ์ œ ํ’€์ด

 

RC4์—์„œ๋Š” ๋™์ผํ•œ key๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ, ๋™์ผํ•œ ํ‚ค์ŠคํŠธ๋ฆผ์ด ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๋˜, ์•”ํ˜ธ๋ฌธ = ํ‰๋ฌธ xor ํ‚ค์ŠคํŠธ๋ฆผ ์ด๋ฏ€๋กœ ํ•˜๋‚˜์˜ ์•”ํ˜ธ๋ฌธ, ํ‰๋ฌธ ์Œ์œผ๋กœ ํ‚ค์ŠคํŠธ๋ฆผ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

## ํ‰๋ฌธ
praw1 = "and now your song is on repeat and i an dancing on to your heartbeat and when you're gone, i fell incomplete so if you want the truth i just wanna be part of your symphony"
plain1 = zlib.compress(praw1.encode())
print(f'{plain1 = }')

## ๋‹ค์šด๋กœ๋“œ ํ•œ ์•”ํ˜ธ๋ฌธ
encd1 = "79w2m1cCcODPMUDPnb26dT1K1R7LSH6pIX5sqELToLdbJFguTlCLt1qhYjo1n3+/Xn0JjKcOHKdjBtihKAj5c3B42+uTlwBBagyiBIAp++cXV2xzjI8lpC2HT7F1vc7fikGP0EEF+MMPqh7/uZiGqqhHeRQYcdCAqQ=="
cipher1 = base64.b64decode(encd1)
print(f'{cipher1 = }')

## ํ‚ค์ŠคํŠธ๋ฆผ 
keystream = bytes([plain1[i] ^ cipher1[i] for i in range(0, len(plain1))])
print(f'{keystream = }')

 

ํ‚ค์ŠคํŠธ๋ฆผ์„ ๊ตฌํ–ˆ์œผ๋ฏ€๋กœ flag (= flag์•”ํ˜ธ๋ฌธ xor ํ‚ค์ŠคํŠธ๋ฆผ)๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

encd_flag = '79xw2pGnwu5JGddMSGhq02ZfYmmJOwoVULyEJPHDaw=='
enc_flag = base64.b64decode(encd_flag)
press_flag = bytes([enc_flag[i] ^ keystream[i] for i in range(0, len(enc_flag)) ])
flag = zlib.decompress(press_flag)
print(flag)

 

 

 

์ฐธ๊ณ 

https://minseosavestheworld.tistory.com/173 
https://www.piolink.com/kr/service/Security-Analysis.php?bbsCode=security&vType=view&idx=90

๋ฐ˜์‘ํ˜•