๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœ’๏ธ Capture The Flag (CTF)

[Patriot CTF 2023] Python XOR Write Up

by A Lim Han 2023. 9. 9.

๐Ÿฆ„ Python XOR Write Up

1.  ๋ฌธ์ œ ํ™•์ธ ํ›„ ์ฒจ๋ถ€ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ

2.  ๋‹ค์šด๋ฐ›์€ XOR.py ํŒŒ์ผ์„ ์—ด์–ด ์ฝ”๋“œ ๋ถ„์„

 

from string import punctuation

# ํŠน์ˆ˜ ๋ฌธ์ž(punctuation)๋ฅผ ๊ฐ€์ ธ์™€ ์•ŒํŒŒ๋ฒณ๊ณผ ํŠน์ˆ˜ ๋ฌธ์ž์˜ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
alphabet = list(punctuation)

# ์•”ํ˜ธ๋ฌธ
data = "bHEC_T]PLKJ{MW{AdW]Y"

def main():
ใ…คใ…ค# ๋ณตํ˜ธํ™” ํ‚ค ์ •์˜
ใ…คใ…คkey = ('')

ใ…คใ…ค# ๋ฐ์ดํ„ฐ๋ฅผ ๋ณตํ˜ธํ™”ํ•˜์—ฌ ํ‰๋ฌธ ์–ป๊ธฐ
ใ…คใ…คdecrypted = ''.join([chr(ord(x) ^ ord(key)) for x in data])

ใ…คใ…ค# ๋ณตํ˜ธํ™”๋œ ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ
ใ…คใ…คprint(decrypted)

# main ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๋ณตํ˜ธํ™” ์ˆ˜ํ–‰
main()

 

++  XOR ์•”ํ˜ธํ™” ํ‚ค๊ฐ€ ๊ณต๊ฐœ๋˜์ง€ X

-->  ์•”ํ˜ธํ™” ํ‚ค๋ฅผ ์•Œ์•„๋‚ด๊ฑฐ๋‚˜, ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋Œ€์ž…ํ•ด๋ณด๋Š” ๋ฐฉ์‹์œผ๋กœ ํ‰๋ฌธ ๋ณต๊ตฌ ๊ฐ€๋Šฅ

3.  XOR ๋ณตํ˜ธํ™”๋ฅผ ์œ„ํ•œ Python ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ

from string import punctuation

alphabet = list(punctuation)
data = "bHEC_T]PLKJ{MW{AdW]Y"

def decrypt_with_key(key):
ใ…คใ…คdecrypted = ''.join([chr(ord(x) ^ ord(key)) for x in data])
ใ…คใ…คreturn decrypted

def main():
ใ…คใ…คfor possible_key in alphabet:  # ๋ชจ๋“  ๊ฐ€๋Šฅํ•œ ํ‚ค ์กฐํ•ฉ์„ ์‹œ๋„
ใ…คใ…คใ…คใ…คdecrypted_data = decrypt_with_key(possible_key)
ใ…คใ…คใ…คใ…คprint(f"Key: {possible_key}, Decrypted Data: {decrypted_data}")

main()

 

 

++  < Line 1 >

: string ๋ชจ๋“ˆ์—์„œ ๋ชจ๋“  ๊ตฌ๋‘์  ๋ฌธ์ž๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š” punctuation ๋ฌธ์ž์—ด์„ ๊ฐ€์ ธ์˜ด

 

++  < Line 2 ~ 3 >

: punctuation ๋ฌธ์ž์—ด์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ alphabet ๋ณ€์ˆ˜์— ์ €์žฅ + ๋ณตํ˜ธํ™”ํ•  ๋ฐ์ดํ„ฐ๋ฅผ data ๋ณ€์ˆ˜์— ์ €์žฅ

 

++  < Line 6 ~ 8 >

: XOR ๋ณตํ˜ธํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•จ์ˆ˜๋กœ, ๋ณตํ˜ธํ™”๋œ ํ‰๋ฌธ์„ ๋ฐ˜ํ™˜

 

++  < Line 10 ~ 13 >

: alphabet ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ๋ฌธ์ž๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ํ‚ค ์กฐํ•ฉ์„ ์‹œ๋„  -->  ๋ฐ์ดํ„ฐ ๋ณตํ˜ธํ™” ํ›„ decrypted_data ๋ณ€์ˆ˜์— ์ €์žฅ๋œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅ

4.  ์ž‘์„ฑํ•œ Python ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰  -->   FLAG ํ˜•์‹์˜ ํ‰๋ฌธ ๋ฐœ๊ฒฌ

Key: !, Decrypted Data: Cidb~u|qmjkZlvZ`Ev|x
Key: ", Decrypted Data: @jga}vrnihYouYcFu{
Key: #, Decrypted Data: Akf`|w~sohiXntXbGt~z
Key: $, Decrypted Data: Flag{python_is_e@sy}
Key: %, Decrypted Data: Gm`fzqxuino^hr^dArx|
Key: &, Decrypted Data: Dnceyr{vjml]kq]gBq{
Key: ', Decrypted Data: Eobdxszwklm\jp\fCpz~
Key: (, Decrypted Data: J`mkw|uxdcbSeSiLuq
Key: ), Decrypted Data: Kaljv}tyebcRd~RhM~tp
Key: *, Decrypted Data: Hboiu~wzfa`Qg}QkN}ws
Key: +, Decrypted Data: Icnhtv{g`aPf|PjO|vr
Key: ,, Decrypted Data: Ndiosxq|`gfWa{WmH{qu
Key: -, Decrypted Data: Oehnryp}afgV`zVlIzpt
Key: ., Decrypted Data: Lfkmqzs~bedUcyUoJysw
Key: /, Decrypted Data: Mgjlp{rcdeTbxTnKxrv
Key: :, Decrypted Data: XryengjvqpAwmA{^mgc
Key: ;, Decrypted Data: Ys~xdofkwpq@vl@z_lfb
Key: <, Decrypted Data: ^tychalpwvGqkG}Xkae
Key: =, Decrypted Data: _ux~bi`mqvwFpjF|Yj`d
Key: >, Decrypted Data: \v{}ajcnrutEsiEZicg
Key: ?, Decrypted Data: ]wz|`kbostuDrhD~[hbf
Key: @, Decrypted Data: 
;$
Key: [, Decrypted Data: 9  ?
Key: \, Decrypted Data: >''8
&
&9
๏ฟฝ
Key: ^, Decrypted Data: <
%	%:	
Key: _, Decrypted Data: =๏ฟฝ$$
Key: `, Decrypted Data: (%#?4=0,+*-7!7=9
Key: {, Decrypted Data: 3>8$/&+701๏ฟฝ6,๏ฟฝ:,&"
Key: |, Decrypted Data: 49?#(!,0761+=+!%
Key: }, Decrypted Data: 58>") -1670*<* $
Key: ~, Decrypted Data: 6;=!*#.2543)?)#'

 

5.  ๋‹ค์‹œ ๋ฌธ์ œ ํ™”๋ฉด์œผ๋กœ ๋Œ์•„์™€ ๋ฐœ๊ฒฌํ•œ FLAG ์ž…๋ ฅ  -->  ๋ฌธ์ œ ํ•ด๊ฒฐ ์„ฑ๊ณต

 

'โœ’๏ธ Capture The Flag (CTF)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Cake CTF 2023] Country DB - 92 Write Up  (0) 2023.11.11
[CSAW CTF 2023] Baby's First Write Up  (0) 2023.09.16
[DownUnder CTF 2023] ๐• Write Up  (0) 2023.09.04
[HSpace CTF 2023] HSpace Free Board Write Up  (0) 2023.09.02
[Hero CTF v5] Hyper Loop Write Up  (0) 2023.05.14