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

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

[Dream Hack - Crypto] Pyploit

๋ฐ˜์‘ํ˜•

pdb.set_trace() ํ•จ์ˆ˜

pdb.set_trace()๋Š” Python์˜ ๋””๋ฒ„๊น… ๋ชจ๋“ˆ์ธ pdb๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ์˜ ์‹คํ–‰์„ ์ค‘๋‹จํ•˜๊ณ  ๋Œ€ํ™”ํ˜• ๋””๋ฒ„๊น… ์„ธ์…˜์„ ์‹œ์ž‘ํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค. ์ด ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ํ•ด๋‹น ๋ถ€๋ถ„์— ๋„๋‹ฌํ–ˆ์„ ๋•Œ, ํ”„๋กœ๊ทธ๋žจ์˜ ์‹คํ–‰์ด ์ผ์‹œ์ ์œผ๋กœ ์ค‘๋‹จ๋˜๊ณ  ๊ฐœ๋ฐœ์ž๊ฐ€ ์ฝ”๋“œ๋ฅผ ๋‹จ๊ณ„๋ณ„๋กœ ์‹คํ–‰ํ•˜๊ฑฐ๋‚˜ ๋ณ€์ˆ˜์˜ ๊ฐ’์„ ํ™•์ธํ•˜๋Š” ๋“ฑ์˜ ๋””๋ฒ„๊น… ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

UTF-8

UTF-8(Unicode Transformation Format - 8-bit)์€ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž ์ง‘ํ•ฉ์„ ํ‘œํ˜„ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ€๋ณ€ ๊ธธ์ด ๋ฌธ์ž ์ธ์ฝ”๋”ฉ ๋ฐฉ๋ฒ•์ด๋‹ค. ์•ŒํŒŒ๋ฒณ๊ณผ ์ˆซ์ž์˜ ๊ฒฝ์šฐ, UTF-8 ์ธ์ฝ”๋”ฉ ์‹œ 1 byte๋ฅผ ์ฐจ์ง€ํ•˜๋ฏ€๋กœ 1์”ฉ ์ฆ๊ฐ€ํ•˜๊ณ , ํ•œ๊ธ€์˜ ๊ฒฝ์šฐ 3 byte๋ฅผ ์ฐจ์ง€ํ•˜๋ฏ€๋กœ 3์”ฉ ์ฆ๊ฐ€ํ•œ๋‹ค.

 

๋ฌธ์ œ ํ•ด์„

1. Encrypt

์ž…๋ ฅ ๋ฐ›์€ text๋ฅผ 12๊ธ€์ž์”ฉ ์ž˜๋ผ์„œ ๋ฐฐ์—ด์— ๋„ฃ๊ณ  ๋ฐฐ์—ด ์ˆœ์„œ๋Œ€๋กœ ์•”ํ˜ธํ™”ํ•˜์—ฌ / ๋’ค์— ์ด์–ด ๋ถ™์ธ๋‹ค.

## text = '0123456789101112'
def trans_enc(text):
    text = split_string(text) ## text = ['012345678910', '1112']
    result = ''
    for i in text:
        result = f'{result}/{encryption(enc_int(i),pub1,pub2)}' ## result = '/{text[0]์•”ํ˜ธ๋ฌธ}/{text[1]์•”ํ˜ธ๋ฌธ}'
    return result

 

 

์•ž์˜ ๋ฐฐ์—ด์—์„œ ํ•˜๋‚˜์”ฉ ์ „๋‹ฌ ๋ฐ›์•„, ํ•œ๊ธ€์ž ํ•œ๊ธ€์ž utf-8๋กœ ์ธ์ฝ”๋”ฉํ•œ๋‹ค.

์ธ์ฝ”๋”ฉํ•œ ๊ธ€์ž๋ฅผ 5๊ธ€์ž 16์ง„์ˆ˜๋กœ ๋‚˜ํƒ€๋‚ธ ํ›„ ์ด์–ด ๋ถ™์ด๊ณ  ๊ฐ€์žฅ ์•ž์— ‘1’์„ ๋”ํ•œ๋‹ค.

def enc_int(text):
    result = ''
    for i in range(0,len(text)):
        try:
            enc_code = str(int(text[i].encode('UTF-8').hex(),16))
		## text[0]:0 => 49
		## text[1]:1 => 50
        except:
            enc_code = str(int('?'.encode('UTF-8').hex(), 16))
        if len(enc_code) <5:
            enc_code = '0'*(5-len(enc_code)) + enc_code
		## 49 => '00049'
		## 50 => '00050'
        result = result + enc_code
		## 0004900050
    result = '1' + result
	## 10004900050
    return int(result)

์ด ์ˆซ์ž๋ฅผ RSA ๊ณต๊ฐœํ‚ค๋กœ ์•”ํ˜ธํ™”ํ•œ๋‹ค.

 

2. Decrypt

RSA ๋น„๋ฐ€ํ‚ค๋กœ ๋ณตํ˜ธํ™” ํ›„, dec_int ํ•จ์ˆ˜์—์„œ ๋ฌธ์ž์—ด 5์”ฉ ์ž˜๋ผ์„œ utf-8๋กœ ๋””์ฝ”๋”ฉํ•œ๋‹ค.

def trans_dec(code):
    code = code.split('/')
    result = ''
    for i in range(1,len(code)):
        result = result + dec_int(decryption(int(code[i]),priv,pub2))
    return result

def dec_int(code):
    code = str(code)
    result = ''
    code = code[1:len(code)]
    for i in range(0,len(code),5):
        try:
            rs = (bytes.fromhex(hex(int(code[i:i+5]))[2:])).decode('UTF-8')
        except:
            rs = '?'
        result = result + rs
    return result



3. main

encrypt ๊ณผ์ •์—์„œ dict์— {์•”ํ˜ธํ™”๋œ ํ…์ŠคํŠธ : ํ•ด๋‹น ํ‰๋ฌธ}์ด ์ €์žฅ๋œ๋‹ค.

decrypt ๊ณผ์ •์—์„œ ๋ณตํ˜ธํ™”๋œ ํ…์ŠคํŠธ๊ฐ€ ์ €์žฅ๋œ ํ‰๋ฌธ๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ํ”„๋กœ๊ทธ๋žจ์ด ์ค‘๋‹จ๋˜๊ณ  ๋””๋ฒ„๊น… ๋ชจ๋“œ๋กœ ์ „ํ™˜ํ•œ๋‹ค.

if inp == '1':
    print('ํ…์ŠคํŠธ: ', end='')
    a = input() 
    rs = trans_enc(a)
    ## dict[์•”ํ˜ธ๋ฌธ] = ํ‰๋ฌธ
    dict[rs] = a
    print(rs)
elif inp == '2':
	print('Code: ', end='')
	a = input()
	## ๋ณตํ˜ธ๋ฌธ = trans_dec(์•”ํ˜ธ๋ฌธ)
	rs = trans_dec(a)
	print(f"{pub2 = }")
	print(rs)
	if a in dict: 
		if dict[a] != rs:
			print('ERROR!')
			pdb.set_trace()

 

๋ฌธ์ œ ํ’€์ด

utf-8์€ ์ธ์ฝ”๋”ฉ ์‹œ ์˜์–ด, ์ˆซ์ž ⇒ 1byte, ํ•œ๊ธ€ ⇒ 3byte๋ฅผ ์ฐจ์ง€ํ•œ๋‹ค.

๋”ฐ๋ผ์„œ 1byte ๋กœ ๋‚˜ํƒ€๋‚ด์ง€ ๋ชปํ•˜๋Š” ๋ฌธ์ž๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๊ฒฝ์šฐ ์œ„์˜ ์ •์ƒ์ ์œผ๋กœ ๋ณตํ˜ธํ™”๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค.

a : 0x61 = 97
b : 0x62 = 98
c : 0x63 = 99
d : 0x64 = 100
e : 0x65 = 101
f : 0x66 = 102
g : 0x67 = 103
h : 0x68 = 104
i : 0x69 = 105
j : 0x6a = 106
k : 0x6b = 107
l : 0x6c = 108
๊ฐ€ : 0xeab080 = 15380608
๋‚˜ : 0xeb8298 = 15434392
๋‹ค : 0xeb8ba4 = 15436708
๋ผ : 0xeb9dbc = 15441340
๋งˆ : 0xeba788 = 15443848
๋ฐ” : 0xebb094 = 15446164
์‚ฌ : 0xec82ac = 15499948
์•„ : 0xec9584 = 15504772
์ž : 0xec9e90 = 15507088
์ฐจ : 0xecb0a8 = 15511720
์นด : 0xecb9b4 = 15514036
ํƒ€ : 0xed8380 = 15565696

ํ’€์ด ๊ณผ์ •

index: 1
Text: ๊ฐ€๋‚˜
/598488141536003528937991342277812969594460037458976406411306987398371767871152324069185247369810247466716214298268157645904433257393630126816085125324998334358419830253078647083044478756888067242842442981599649234410189005820632806388170826324164284969229

index: 2
Code: /598488141536003528937991342277812969594460037458976406411306987398371767871152324069185247369810247466716214298268157645904433257393630126816085125324998334358419830253078647083044478756888067242842442981599649234410189005820632806388170826324164284969229
??????
ERROR!

> /Pyploit.py(171)<module>()
-> menu()
(Pdb) FLAG = open("flag", "rb").read()
(Pdb) print(FLAG)

FLAG

FLAG = b'''
    ====flag====
    /4169516977559141081420826333847051927984265743299875567490339976276808209650300529475755536470874853912690388607937181977191116578234739314891772399481363013915209182518065457286205336688760565151349998694036987898686806899816104590747921701744527122320799
    /713909456513734051357955795040285242928601328904738436368843600390557929196251633640024222489745886875019858304358381622277886831669417429034355351264317775938578700512672608464284133726006261056690563439038151624148052728350758677357080652896117984304740
    /1916429712962618825833430943238183430158385336548491924013081058254482927665145457057967755896375861819413487649194257943965985222253532476313372425008869515119711071870668839818439320941580292051550818330587519743459287118428425189601914420872360958937717
    ====flag====
    
    ====????====
    3459196519943177904674538869459802391686903930008099623840714440954105103866974530427491791539652312167071194984882404537184791492302191598249023703238755689855282318760841260371780027787306307347005882960480731661123451511067907327477975625962114393048577
    ====????====
    
    ====????====
    5002545618242553738882017165095261691689496951783856857046844572064283202968575616766550367230111513802315744422177460306136186205793078706764455760474880834542508211610934828951364974082674456983498680285836457429726248883571914707670547864655320242561399
    ====????====
'''

์‹คํ–‰์ฝ”๋“œ

priv = 3459196519943177904674538869459802391686903930008099623840714440954105103866974530427491791539652312167071194984882404537184791492302191598249023703238755689855282318760841260371780027787306307347005882960480731661123451511067907327477975625962114393048577
pub2 = 5002545618242553738882017165095261691689496951783856857046844572064283202968575616766550367230111513802315744422177460306136186205793078706764455760474880834542508211610934828951364974082674456983498680285836457429726248883571914707670547864655320242561399
a = '/4169516977559141081420826333847051927984265743299875567490339976276808209650300529475755536470874853912690388607937181977191116578234739314891772399481363013915209182518065457286205336688760565151349998694036987898686806899816104590747921701744527122320799/713909456513734051357955795040285242928601328904738436368843600390557929196251633640024222489745886875019858304358381622277886831669417429034355351264317775938578700512672608464284133726006261056690563439038151624148052728350758677357080652896117984304740/1916429712962618825833430943238183430158385336548491924013081058254482927665145457057967755896375861819413487649194257943965985222253532476313372425008869515119711071870668839818439320941580292051550818330587519743459287118428425189601914420872360958937717'
rs = trans_dec(a)
print(rs)
๋ฐ˜์‘ํ˜•