블로그 이전했습니다. https://jeongzero.oopy.io/
[pwnable.kr] fix
본문 바로가기
워게임/pwnable.kr

[pwnable.kr] fix

728x90

1. 문제


1) mitigation 확인

2) 문제 확인

쉘코드 문제인듯

3) 코드흐름 파악

int __cdecl main(int argc, const char **argv, const char **envp)
{
  int value; // [esp+8h] [ebp-10h]
  unsigned int index; // [esp+Ch] [ebp-Ch]

  puts("What the hell is wrong with my shellcode??????");
  puts("I just copied and pasted it from shell-storm.org :(");
  puts("Can you fix it for me?");
  index = 0;
  printf("Tell me the byte index to be fixed : ");
  __isoc99_scanf("%d", &index);
  fflush(stdin);
  if ( index <= 0x16 )
  {
    value = 0;
    printf("Tell me the value to be patched : ");
    __isoc99_scanf("%d", &value);
    sc[index] = value;
    shellcode();
  }
  return 0;
}

int shellcode()
{
  char dest; // [esp+Ch] [ebp-1Ch]
  char *retaddr; // [esp+2Ch] [ebp+4h]

  strcpy(&dest, sc);
  retaddr = &dest;
  return puts("get shell");
}

현재 삽입된 쉘코드에서 한바이트만 변경가능하다. shellcode()함수에서 해당 쉘코드를 실행시킬수 있다. 뭐가 문젠지는 디버깅을 해봐야할껏같다.

2. 접근방법


현재 strcpy로 쉘코드를 스택에 넣고 실해을 시킨다.

push eax 부분부터 쫌 이상하다. 원래 push eax → push ebx인데 push eax를 하고 또 push eax를 한다. 저 상태에서 push eax를 진행하면, eax가 스택에 삽입되고 esp가 올라간다.

실제 해보면 push eax는 2번 실행안되고 push ebx가 수행된다.

현재 ebx도 스택주소이므로 push ebx를 하면 esp-4에 ebx에 들어있는 값이 수행된다.

push ebx가 진행되면 어셈이 달라진다. 이 이유는 push ebx가 진행되면 esp는 ebx에 들어있는 값을 가리키게 되는데, 현재 esp(0xffffd09c)에 0xffffd0a4가 들어있고, eip는 스택주소이다. 다음에 수행할 라인이 0xffffd09d인데 이 주소는 아까 push ebx로 인해 스택에 들어가버렸다.

따라서 저렇게 어셈이 바뀌는것이다. push ebx 가 인덱스 16번째여서 저기를 pop esp로 바꿀라 했는데 그러면 esp에 0이 들어가서 안된다. 따라서 push ebx 직전 push eax를 pop esp로 바꾸면, /bin 문자열 주소가 esp로 되는데, 이는 스택의 사이즈를 무제한으로 바꾸면 저 주소도 스택으로 사용가능하여 진행시키면 끝이다.

3. 풀이


  • ulimit -s limited
    fix@pwnable:~$ ulimit -s unlimited; ulimit -a
    core file size          (blocks, -c) 0
    data seg size           (kbytes, -d) 1500000
    scheduling priority             (-e) 0
    file size               (blocks, -f) 120000
    pending signals                 (-i) 380926
    max locked memory       (kbytes, -l) 64
    max memory size         (kbytes, -m) unlimited
    open files                      (-n) 1500
    pipe size            (512 bytes, -p) 8
    POSIX message queues     (bytes, -q) 819200
    real-time priority              (-r) 0
    stack size              (kbytes, -s) unlimited
    cpu time               (seconds, -t) unlimited
    max user processes              (-u) 300
    virtual memory          (kbytes, -v) unlimited
    file locks                      (-x) unlimited
    fix@pwnable:~$

  • index : 15
  • value : 92 (→ 0x5c)

4. 몰랐던 개념


괜히 ssh를 주는게 아니다.

728x90

'워게임 > pwnable.kr' 카테고리의 다른 글

[pwnable.kr] dragon  (0) 2020.10.04
[pwnable.kr] md5 calculator  (0) 2020.09.23
[pwnable.kr] brainfuck  (0) 2020.09.23
[pwnble.kr] leg  (0) 2020.09.06
[pwnable.kr] shellshock  (0) 2020.09.06