๊ธ€ ์ž‘์„ฑ์ž: heogi
์ด ๋ฌธ์ œ๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฌธ์ž์—ด ์ž…๋ ฅ์„ ๋ฐ›์•„ ์ •ํ•ด์ง„ ๋ฐฉ๋ฒ•์œผ๋กœ ์ž…๋ ฅ๊ฐ’์„ ๊ฒ€์ฆํ•˜์—ฌ correct ๋˜๋Š” wrong์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.  ํ•ด๋‹น ๋ฐ”์ด๋„ˆ๋ฆฌ๋ฅผ ๋ถ„์„ํ•˜์—ฌ correct๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ž…๋ ฅ๊ฐ’์„ ์•Œ์•„๋‚ด์„ธ์š”.

1. ํ”„๋กœ๊ทธ๋žจ ๋ถ„์„

์ฃผ์–ด์ง„ ํ”„๋กœ๊ทธ๋žจ์„ IDA๋ฅผ ํ†ตํ•ด ๋ถ„์„์„ ์ง„ํ–‰ํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

int __fastcall main(int argc, const char **argv, const char **envp)
{
  char v4[256]; // [rsp+20h] [rbp-118h] BYREF

  memset(v4, 0, sizeof(v4));
  sub_1400011C0("Input : ", argv, envp);
  sub_140001220("%256s", v4);
  if ( (unsigned int)sub_140001000(v4) )
    puts("Correct");
  else
    puts("Wrong");
  return 0;
}

์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ ๊ฐ’์„ ๋ฐ›์•„ sub_140001000 ํ•จ์ˆ˜๋กœ ์ „๋‹ฌํ•œ๋‹ค.

ํ•ด๋‹น ํ•จ์ˆ˜ ์‹คํ–‰ ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ Correct ๋˜๋Š” Wrong ์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

2. sub_140001000 ํ•จ์ˆ˜ ๋ถ„์„

__int64 __fastcall sub_140001000(__int64 a1)
{
  int i; // [rsp+0h] [rbp-18h]

  for ( i = 0; (unsigned __int64)i < 28; ++i )
  {
    if ( ((unsigned __int8)(16 * *(_BYTE *)(a1 + i)) | ((int)*(unsigned __int8 *)(a1 + i) >> 4)) != byte_140003000[i] )
      return '\0';
  }
  return '\x01';
}

์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ ๋ฌธ์ž์™€ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜์—ฌ byte_14000300์— ์žˆ๋Š” ๋ฌธ์ž์™€ ๋น„๊ตํ•˜์—ฌ 0 ๋˜๋Š” 1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

byte_14000300์— ๋‹ด๊ฒจ์žˆ๋Š” ๊ฐ’์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

3.  ์กฐ๊ฑด ๋ถ„์„

if๋ฌธ์•ˆ์˜ ์กฐ๊ฑด์„ ๋ถ„์„ํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•ํƒœ์ด๋‹ค.

16 * a1[i] | a1[i] >> 4 == byte_14000300[i]

ํ•ด๋‹น ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฐ’์„ ์ฐพ๊ธฐ ์œ„ํ•ด ์‹œ๋„ํ•ด๋ดค์ง€๋งŒ ๊ฒฐ๊ณผ ๊ฐ’์ด ๋‚˜์˜ค์ง€ ์•Š์•˜๋‹ค.

IDA์—์„œ Pseudo Code๋ฅผ ํ†ตํ•ด ํ™•์ธํ•œ ์กฐ๊ฑด์€ ์œ„์™€ ๊ฐ™์ง€๋งŒ ์‹ค์ œ Assembly๋ฅผ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

movsxd  rax, [rsp+18h+var_18]
mov     rcx, [rsp+18h+arg_0]
movzx   eax, byte ptr [rcx+rax]
sar     eax, 4
movsxd  rcx, [rsp+18h+var_18]
mov     rdx, [rsp+18h+arg_0]
movzx   ecx, byte ptr [rdx+rcx]
shl     ecx, 4
and     ecx, 0F0h
or      eax, ecx
movsxd  rcx, [rsp+18h+var_18]
lea     rdx, byte_140003000
movzx   ecx, byte ptr [rdx+rcx]
cmp     eax, ecx
jz      short loc_140001063

Assembly๋กœ ํ™•์ธํ•œ ์กฐ๊ฑด์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

a[i] << 4 & 0xF0 | a1[i] >> 4

4. Exploit

ํ•ด๋‹น ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฐ’์„ ์ฐพ๋Š” ๊ฐ’์„ ascii ๊ฐ’ ๋‚ด ์—์„œ ๋ฌด์ž‘์œ„ ๋Œ€์ž…์„ ์ง„ํ–‰ํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ Flag๋ฅผ ์ฐพ๋Š”๋‹ค.

#python3 ex.py
#byte_140003000 DATA
x = "36, 39, 19, 198, 198, 19, 22, 230, 71, 245, 38, 150, 71, 245, 70, 39, 19, 38, 38 , 198, 86, 245, 195, 195, 245, 227, 227, 0".replace(" ","").split(",")

flag = []

for j in x:
	for i in range(0,127):	
		lshift = int(i) << 4 & 0xF0
		rshift = int(i) >> 4
		result = rshift | lshift
		if(result == int(j)):
			flag.append(chr(i))

print("flag : "+"DH{"+"".join(flag)+"}")

'๐Ÿ›ก๏ธCTF > DreamHack' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Secure Mail  (0) 2024.01.03
Shell_Basic  (0) 2023.10.08
rev-basic-3  (0) 2023.09.19
[BOB CTF 8th] - FileStroage  (0) 2022.09.13
[BOB CTF 8th] - Summer Fan  (0) 2022.09.13