itmediaã®ãmain()é¢æ°ã®åã«ã¯ä½ãããã®ãããèªãã§ã¿ãï¼ç¬¬äºåï¼
# ã¯ããã« main()é¢æ°ã®åã«ã¯ä½ãããã®ãã®é£è¼ãèªãã§ããããªãã£ããã¨ã調ã¹ããã¨ã¡ã¢ ä»åã¯ç¬¬äºå printf()ã®ã½ã¼ã¹ã³ã¼ãã§ãã½ã¼ã¹ã³ã¼ããªã¼ãã£ã³ã°ã®ã³ãã身ã«ä»ãã
ã¡ã¢
24:/* Write formatted output to stdout from the format string FORMAT. */ 25:/* VARARGS1 */ 26:int 27:__printf (const char *format, ...) 28:{ 29: va_list arg; va_list: å¯å¤åã®å®å¼æ°ãæ±ãããã®æ å ±ãä¿æããããã®åã 30: int done; 31: 32: va_start (arg, format); 33: done = vfprintf (stdout, format, arg); 34: va_end (arg); 35: 36: return done; 37:} 38: 39:#undef _IO_printf 40:ldbl_strong_alias (__printf, printf); 41:/* This is for libg++. */ 42:ldbl_strong_alias (__printf, _IO_printf);
l.29: va_list: å¯å¤åã®å®å¼æ°ãæ±ãããã®æ å ±ãä¿æããããã®åã
l.32: va_start: va_list ãåæåããå¯å¤åå¼æ°ã®ä½¿ç¨ãéå§ããã
l.34: va_end: å¯å¤åå¼æ°ã®å¦çãçµããã¨ãã«å¼ã³åºãã
é¢æ°åã__printf()ã«ãªã£ã¦ããããããã¯ldbl_strong_alias()ã¨ãããã¯ãã«ãã£ã¦printf()ã¨ããã¨ã¤ãªã¢ã¹ãå®ç¾©ãããããã ã
è¨ã£ã¦ããã¨ãããããããªãã£ãã ãã¯ãã«ãã£ã¦ã¨ã¤ãªã¢ã¹ãå®ç¾©ããã¦ãããããããªã
æ¾ãèªã¿ãã¦ã¿ãã¨ããã¯ãã®ä¸ã§ã¯va_arg()ã«ãã£ã¦å¼æ°ãå¾ã¦ããããã ã
va_arg: å¯å¤åå®å¼æ°ã®å¤ãè¿ãã次ã®å¼æ°ã¸é²ãã
ã¤ã¾ãåºåå ãTTYã®ã¨ãã«ãè¡åä½ã®åºåã«ãªãããã ã
TTYã¯ãã£ã¹ãã¬ã¤ã®ãã¨ã¿ãã
AFL++åããã¦ã¿ã
ã¯ããã«
AFL++ã£ã¦ãã¡ã¶ã¼ãåããã¦ã¿ãã®ã§ãåãããéç¨ã¨çµæã®ã¡ã¢
ç®æ¬¡
åè
IPAã®AFLè³æ qiitaã®è¨äºï¼AFLã§ãã¡ã¸ã³ã°ãã£ã¦ã¿ã
åããã¦ã¿ã
æºå
AFL++ã¤ã³ã¹ãã¼ã«
apt
ã§AFL++ãã¤ã³ã¹ãã¼ã«
$ sudo apt update $ sudo apt install afl++ afl++-clang afl++-doc
RAMãã£ã¹ã¯ä½æ
$ mkdir -p /tmp/afl-ramdisk && chmod 777 /tmp/afl-ramdisk/ $ sudo mount -t tmpfs -o size=512M tmpfs /tmp/afl-ramdisk/ $ cd /tmp/afl-ramdisk/
ãã¹ã対象ã³ã¼ã
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(int argc, char** argv) { char buf[8]; if(read(0, buf, 8) < 1) { exit(1); } printf(buf); exit(0); }
ã³ã³ãã¤ã«
example.cãafl-gcc
ã§ã³ã³ãã¤ã«
$ afl-gcc -o example example.c
ãã¡ã¸ã³ã°
ãã¹ãã±ã¼ã¹ãã¦ã³ãã¼ã
$ wget -O - https://lcamtuf.coredump.cx/afl/releases/afl-latest.tgz | tar zxvf -
ãã¡ã¸ã³ã°ãã¹ãå®è¡
$ afl-fuzz -i afl-2.52b/testcases/others/text/ -o out/ ./example
afl++å®è¡æã®ç»é¢ IPAã®è³æã«ããAFLã®å®è¡æã®é 表示é ç®ã®èª¬æã¨é ç®åãå¤å°éããå 容ã¯åãã ã¨æãããã å®è¡æã®ã·ã¹ãã ã¢ãã¿ã¼ãã¿ãã¨ãCPU1ã®å®è¡ã100%ã«è²¼ãä»ãã¦ãã
HACKING: ç¾ããçè¬åå¼·ã¡ã¢ #1
0x200ã®ããã°ã©ãã³ã°ã®åºç¤ã¯æãåãããèªãã ãã«ããã
0x300ã®ããã°ã©ã ã®èå¼±æ§æ»æããå®éã«åããã¦ããã
Â
overflow_example.cã«ã¤ãã¦è¦ã¦ããã
dump[vvar]ã»ã°ã¡ã³ãã¯ãLinuxã¦ã¼ã¶ã¼ç©ºéããã°ã©ã ã«ããã¦ãé¢æ°å¼ã³åºãã«ã¾ããã£ã¦ä¿åããå¿ è¦ã®ãããã¼ã¿ãæ ¼ç´ããããã«ä½¿ç¨ãããã¡ã¢ãªé åã§ããããã®ã»ã°ã¡ã³ãã¯é常ãèªã¿åãå°ç¨ã¡ã¢ãªï¼ROMï¼é åãã¾ãã¯ãã©ãã·ã¥ã»ã¡ã¢ãªãEEPROMãªã©ã®ä¸æ®çºæ§ã¡ã¢ãªï¼NVMï¼ããã¤ã¹ã«ãããã³ã°ãããã
Understanding Dump[vvar]Segment in Linux User Space Programs (devcodef1.com)ãã
 vDSO (virtual dynamic shared object) ã¯ã注ææ·±ãé¸æããã kernel space ã«ã¼ãã³ç¾¤ããuser space ä¸ã®ã¢ããªã±ã¼ã·ã§ã³ã«ã¨ã¯ã¹ãã¼ãããããã®ãã«ã¼ãã«ã¡ã«ããºã ã§ãããããã«ãããã¢ããªã±ã¼ã·ã§ã³ã¯ããããã«ã¼ãã«ç©ºéã«ã¼ãã³ããã·ã¹ãã ã³ã¼ã«Â ã¤ã³ã¿ãã§ã¼ã¹ã使ç¨ãã¦ããããã®åãã«ã¼ãã«ç©ºéã«ã¼ãã³ãå¼ã³åºãæã«åºæã®ãuser mode ãã ã«ã¼ãã«ã¢ã¼ã ã¸ã®Â ã³ã³ããã¹ãã¹ã¤ãã ã«ããããã©ã¼ãã³ã¹ä½ä¸ãªãã«ãããã»ã¹å ã§å¼ã³åºããã[1][2]
ã»ç°ãªãé¢æ°ã®å¤æ°ã®ããã·ã¥é ã¯OSã«ãã£ã¦ç°ãªãÂ
ã»åãé¢æ°å ã®å¤æ°ã®ããã·ã¥é ã¯ã³ã³ãã¤ã©ã«ãã£ã¦ç°ãªãÂ
é¢ç½ãããªè¨äºãè¦ã¤ããã®ã§æ©æ¢°å¦ç¿ç°å¢ãæ´å
æ°ã«ãªãè¨äºãè¦ã¤ããã®ã§ãwinãã¹ã¯ãããã«æ©æ¢°å¦ç¿ç°å¢ãç¨æãã¦ã¿ã
Â
ä¸ç¹ãè¨äºã§ã¯Ubuntu24.04ãå ¥ãã¦ãããã©aptã®ã¬ãã¸ããªã¨ãã§ã¨ã©ã¼è¸ã¿ãããªã®ã§ãUbuntu-22.04ã«ãã
Â
Dockerã常æèµ·åç¶æ ã«ããã¨ãã¾ã§ãã£ã
ã¡ã¢ãªãµã¤ãºããã£ã¹ã¯ãµã¤ãºã®å¢å ã¯ãã£ã¦ãªã
itmediaã®ãmain()é¢æ°ã®åã«ã¯ä½ãããã®ãããèªãã§ã¿ãï¼ç¬¬ååï¼
itmediaã®ãmain()é¢æ°ã®åã«ã¯ä½ãããã®ããã®é£è¼ãèªãã§ããããªãã£ããã¨ãã調ã¹ããã¨ãã¡ã¢ãã
Â
第ååã¯" OSã®ã·ã¹ãã ã³ã¼ã«ã®å¼ã³åºãã¨ã¯ï¼ãã¤ããªã¨ãã£ã¿ã®ä½¿ãæ¹"
Â
ãããªãååã®è¨äºã®éä¸ããå§ã¾ã£ã¦ã¦ãã©ããã£ããéã¢ã»ã³ãã«ã®ãã®è¡ã«ãã©ãã¤ãã®ãå¿ãã¦ãã
以ä¸ãæãåºããªããå®è¡ãããã第ååã®é ã®ã¨ããã«è¡ãã
Â
å é ããã¢ã»ã³ããªè¿½ã£ã¦ã¿ãããª
https://image.itmedia.co.jp/l/im/ait/articles/1703/01/l_r20_Hello04-01.PNG
Â
ï¼__write_nocancel()ã__kernel_vsyscall()ãå¼ãã§ãã¿ããã ãã©ããããããä½ãªã®ãï¼
__write_nocancel()ã¯writeã·ã¹ãã ã³ã¼ã«ãå¼ã¶APIå ã®é¢æ°ã£ã½ã
nocancel()ã¯ã¹ã¬ãããã£ã³ã»ã«ãã¤ã³ãã§ãªãé¢æ°ã¨ãããã¨ããã
noncancelã®åã®writeã¯ãã£ã³ã»ã«ãã¤ã³ãã¨ãããã¨ããª
pthread_cancelã£ã¦é¢æ°ã§ãã£ã³ã»ã«ã§ããã¿ãã
pthreadãPOSIXã¹ã¬ããã£ã¦ããPOSIXæ¨æºã®ã¹ã¬ããã¨ã®ãã¨ï¼ï¼ï¼
ãããã¸ãã¯ããã«ãã¹ã¬ããããã°ã©ãã³ã°ãåå¼·ããå¿ è¦ããããã
(gdb) where #0 0x00110416 in __kernel_vsyscall () #1 0x08053d92 in __write_nocancel () #2 0x08067671 in _IO_new_file_write () #3 0x0806819b in _IO_new_do_write () #4 0x080683ea in _IO_new_file_overflow () #5 0x080673f4 in _IO_new_file_xsputn () #6 0x08059738 in vfprintf () #7 0x08049381 in printf () #8 0x080482e2 in main (argc=1, argv=0xbffffc14) at hello.c:5 (gdb)
Â
hexeditã®/使ã£ãæ¤ç´¢ã¯ãå ¨ææ¤ç´¢ãããªãã¦ã«ã¼ã½ã«ä»¥éã®æ¤ç´¢ãªããã
æ¤ç´¢ãããç®æéãéããç¶æ ã§æ¤ç´¢ãã¦ãnot foundã£ã¦è¨ããã¦è¬ã ã£ã
Â
0x80d6750ã¯ã©ãã ï¼
objdump -d hello | less ã§æ¤ç´¢ãã¦ããã®ã¢ãã¬ã¹ã¯å ¥ã£ã¦ãªãã£ãã
ã¡ãªã¿ã«ã0x110414<__kernel_vsyscall>ãã©ããªã®ãæ°ã«ãªã
helloã確ä¿ããã¡ã¢ãªã®ç¯å²å¤ãªæ°ããã¦ã
Â
Â
itmediaã®ãmain()é¢æ°ã®åã«ã¯ä½ãããã®ãããèªãã§ã¿ãï¼ç¬¬ä¸åï¼
itmediaã®ãmain()é¢æ°ã®åã«ã¯ä½ãããã®ããã®é£è¼ãèªãã§ããããªãã£ããã¨ãã調ã¹ããã¨ãã¡ã¢ãã
ï¼ä½è ãµãã¼ããã¼ã¸ï¼
Â
Â
break mainã§runãããã¨ãbreakãã¦ãã®ãâã®è¡ãªã®ãããããããª
breakãè²¼ãããç®æã£ã¦ã©ãããè¦åã«ãªã£ã¦ããã ããï¼
https://image.itmedia.co.jp/ait/articles/1703/01/r20_Hello03-01.PNG
Â
gdbã«ã¤ãã¦èª¿ã¹ãã¨ä½¿ãæ¹ã°ããåºã¦ãã¦ãåçããããããª
Â
break function ã ã¨functionã®ã¨ã³ããªã¼ãã¤ã³ãã§ãã¬ã¤ã¯ããããã
ã¨ãããã¨ã¯ãã¹ã¿ãã¯ãã¤ã³ã¿ããã¼ã¹ãã¤ã³ã¿æ´æ°å¾ãé¢æ°ã®ã¨ã³ããªã¼ãã¤ã³ãã£ã¦ãã¨ï¼kprobeãuprobeãåãã¿ã¤ãã³ã°ãªã®ããªï¼
break function
function ã®ã¨ã³ããªã«ãã¬ã¼ã¯ãã¤ã³ããè¨å®ãã¾ãã
https://image.itmedia.co.jp/l/im/ait/articles/1703/01/l_r20_Hello03-08.PNG
Â
ã¢ã»ã³ãã©ã§æ·±ãã¨ãæ½ã£ã¦ããã®ã¯ãbreakè²¼ã£ã¦å§ãã¦é¢æ°å¼ã³åºãè¦ã¤ãã¦stepiã§å¼ã³åºãå ã®é¢æ°ã«é£ãã§ã¯breakè²¼ã£ã¦ãç¹°ãè¿ãã¦ããã¿ããã
Â
itmediaã®ãmain()é¢æ°ã®åã«ã¯ä½ãããã®ãããèªãã§ã¿ãï¼ç¬¬äºåï¼
itmediaã®ãmain()é¢æ°ã®åã«ã¯ä½ãããã®ããã®é£è¼ãèªãã§ããããªãã£ããã¨ãã調ã¹ããã¨ãã¡ã¢ãã
ï¼ä½è ãµãã¼ããã¼ã¸ï¼
Â
第äºåã¯"ãHello Worldï¼ãã®ä¸»å½¹printf()ã®å é¨åä½ããããã¬GDBã§è¿½ã"
Â
gdbserverã使ã話ãåºã¦ãã
https://www.codeproject.com/KB/mcpp/remote_debugging/3.jpg
Â
Â
printfã®éã¢ã»ã³ãã«çµæã®ãã¼ã¢ããã¯leaãããããªãã£ã
movã¯ã©ãã«ãæå®ããã¨ãã©ãã«ã®å ã«ããä¸èº«ãã³ãã¼ããã
leaã¯ãã¢ãã¬ã¹ã®å ã®ä¸èº«ã§ã¯ãªããã¢ãã¬ã¹ãã®ãã®ãã³ãã¼ããã
movã¯ä¸èº«ã®å¤ãleaã¯ã¢ãã¬ã¹ãã³ãã¼ããã¿ãã
Â
ãã¼ã¢ããã¯leaveãç¥ããªããª
調ã¹ã¦ã¿ãã¨ã
leave
å½ä»¤ã¯ã以ä¸ã®ï¼ã¤ã®å½ä»¤ãçµã¿åãããã®ã¨åçã®å¦çããã¾ããmov esp, ebp pop ebp
https://vanya.jp.net/os/x86call/#enterleave:~:text=leave%E5%91%BD%E4%BB%A4%E3%81%AF,ebp%0Apop%20ebp
ã¨ã®ãã¨ã
retã®åã«å¿ è¦ãªå¦çã¿ãã
retã¯ã¹ã¿ãã¯ã«ç©ã¾ãã¦ãã¢ãã¬ã¹ããå¼ã³åºããã¨ã«æ»ãå½ä»¤
ä¸ã§ãåç §ãã以ä¸ã®ãµã¤ããé¢æ°å¼ã³åºãã«ã¤ãã¦ããããããã£ã
x86ã¢ã»ã³ããªè¨èªã§ã®é¢æ°ã³ã¼ã« (vanya.jp.net)
Â
Â