1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include <stdio.h> // 23byte shellcode from http://shell-storm.org/shellcode/files/shellcode-827.php char sc[] = "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69" "\x6e\x89\xe3\x50\x53\x89\xe1\xb0\x0b\xcd\x80"; void shellcode(){ // a buffer we are about to exploit! char buf[20]; // prepare shellcode on executable stack! strcpy(buf, sc); // overwrite return address! *(int*)(buf+32) = buf; printf("get shell\n"); } int main(){ printf("What the hell is wrong with my shellcode??????\n"); printf("I just copied and pasted it from shell-storm.org :(\n"); printf("Can you fix it for me?\n"); unsigned int index=0; printf("Tell me the byte index to be fixed : "); scanf("%d", &index); fflush(stdin); if(index > 22) return 0; int fix=0; printf("Tell me the value to be patched : "); scanf("%d", &fix); // patching my shellcode sc[index] = fix; // this should work.. shellcode(); return 0; } | cs |
이 문제는 sc의 index를 골라서 fix할 수 있다. 한 곳만 수정할 수 있는데 수정한 쉘코드로 마지막에 ret을 한다.
저 쉘코드를 한번 보자
https://www.onlinedisassembler.com/odaweb/
예전에부터 담아 놨던 사이트인데 옵코드를 알아서 해석해준다.
처음 이 쉘코드를 보고 생각한 것은
그냥 있는 그대로 입력해주는 것이였다
첫번째가 31이니 수정하지 않고 31을 입력하는것이다.
이렇게 나온다.
쉘코드는 제대로 들어가는데 마지막에 badㄸ면서 안된다.
그다음은 수정을 하기 위해 여러가지를 막 바꿔봤다.
opcode list라고 검색하면
http://sparksandflames.com/files/x86InstructionChart.html
해당 사이트가 나온다. 여길 보면 수정할 때 도움이 된다.
그래서 어딜 수정하느냐
네이버 블로그 인데 아주 설명이 잘 되어있다. (엄청 큰 도움이 된 블로그)
처음에 eax를 0으로 만드는데 이부분을 바꾸려니 2바이트만으론 부족했다...
그리고 mov 명령을 건드릴게 없었고 그러던중 push eax 이부분이 execve("/bin/sh", "/bin/sh", NULL);
여기서 NULL을 담당하는데 저부분을 바꾸면된다. pop esp를 하게되면 esp를 pop하고 밑에 다시 push ebp를 하는데
잘 들어가게된다
pop esp를 통해 esp를 pop하고 push ebx로 다시 넣는다.
스택상태는 이렇다. 사실 이문제는 위에 네이버 블로그만 참고하면 풀 수 있을 듯하다.
pop esp의 옵코드는 5c이다 5c는 십진수로 92이니
이렇게 입력을 해주면 끝!!!
'WARGAME > Pwnable.kr' 카테고리의 다른 글
pwnablekr codemap (0) | 2017.07.06 |
---|---|
pwnable.kr dragon (0) | 2017.01.11 |
pwnable.kr coin1 (0) | 2017.01.09 |
pwnable.kr simple login (0) | 2017.01.06 |
pwnable.kr passcode (0) | 2016.03.29 |