åå ã¯ãPwnableåé¡ã«åãçµã¿ã¾ãããCTF ã®ã«ãã´ãªã®ä¸ã§ããä¸çªé£ããã¨è¨ãããã ããã£ã¦ãããªãè¦å´ãã¾ããã
ä»åã¯ãååã® Pwnableåé¡ã§ã使ç¨ãããå®è¡ãã¡ã¤ã«ã®ã»ãã¥ãªãã£æ©æ§ï¼èå¼±æ§ç·©åæè¡ã¨ãè¨ãï¼ã調ã¹ããã¼ã«ã§ãããchecksecãã®æ·±æãããããã¨æãã¾ãã
å®è¡ãã¡ã¤ã«ã®ã»ãã¥ãªãã£æ©æ§ã¨ã¯ããããå®è¡ãã¡ã¤ã«ã«èå¼±æ§ãåå¨ãã¦ããã¨ãã¦ãããã®èå¼±æ§ã«å¯¾ããæ»æãããã«ããããä»çµã¿ã®ãã¨ã§ãã
ä¾ãã°ãã¹ã¿ãã¯ã«ããªã¤ã¯ãé¢æ°éå§æã«ã¹ã¿ãã¯ã«ã©ã³ãã ãªå¤ãæ ¼ç´ãã¦ãããé¢æ°çµäºæã«ããã®å¤ãå¤åãã¦ããªããããã§ãã¯ãã¾ããããã«ãã£ã¦ãã¹ã¿ãã¯ã使ã£ãæ»æãæåããã«ãããããã¨ãåºæ¥ã¾ãã
å®è¡ãã¡ã¤ã«ã®ã»ãã¥ãªãã£æ©æ§ã«ã¯ãããã¤ã種é¡ãããã®ã§ããããã®è©³ç´°ã¨ãå®éã«ãã³ã³ãã¤ã«ããããGDB ã§ç¢ºèªããããã¦ã¿ããã¨æãã¾ãã
ããã§ã¯ããã£ã¦ããã¾ãã
åèæç®
ã¯ããã«
ãã»ãã¥ãªãã£ãã®è¨äºä¸è¦§ã§ããè¯ãã£ããåèã«ãã¦ãã ããã
ã»ãã¥ãªãã£ã®è¨äºä¸è¦§
checksec ã®å
¬å¼ãµã¤ãã¯ä»¥ä¸ã§ãã
github.com
ç°å¢ã¯ãVirtualBoxï¼ParrotOS 6.1 ã§ãã
ããã§ã¯ããã£ã¦ããã¾ãã
checksecã®æºå
checksec ã¯ãå¾æ¥ããã·ã§ã«ã¹ã¯ãªããã§å®è£
ããã¦ãã¾ããããã·ã§ã«ã¹ã¯ãªããçã¨ãã¦ã2.7.xï¼ç¾å¨ã®ææ°çã¯ã2.7.1ï¼ãæçµãªãªã¼ã¹ã§ã以éã® 3.x ããã¯ãGoè¨èªã«ããå®è£
ã«ä»£ããããã§ãã
ä¸ã® URL ã«ã¢ã¯ã»ã¹ãã¦ãå³å´ã«è¦ãã Releases ãã¯ãªãã¯ãã¾ããchecksec.sh-2.7.1.zip ããã¦ã³ãã¼ãã§ãã¾ãã®ã§ãä»»æã®å ´æã«è§£åãã¾ãã解åãããã©ã«ãã®ä¸ã«ãchecksecãã¨ãããã¡ã¤ã«åã®ã·ã§ã«ã¹ã¯ãªãããå
¥ã£ã¦ããã¨æãã¾ãã
ãã¡ã¤ã«ã®ç¢ºèªã¨ããã¼ã¸ã§ã³ã確èªãã¦ã¿ã¾ãã
$ file ../../tools/checksec.sh-2.7.1/checksec
../../tools/checksec.sh-2.7.1/checksec: Bourne-Again shell script, ASCII text executable
$ ../../tools/checksec.sh-2.7.1/checksec --version
checksec v2.7.1, Brian Davis, github.com/slimm609/checksec.sh, Dec 2015
Based off checksec v1.5, Tobias Klein, www.trapkit.de, November 2011
以éã§ã¯ããã® checksec ã使ã£ã¦ããã¾ãã
ç°¡åãªCè¨èªã®ããã°ã©ã ãç¨æãã
å®éã«ãå®è¡ãã¡ã¤ã«ã®ã»ãã¥ãªãã£æ©æ§ãã©ããªã£ã¦ããããè¦ãããã«ãç°¡åãªããã°ã©ã ãç¨æãã¾ãã
ãã®ããã°ã©ã ãå®è¡ããã¨ãã¦ã¼ã¶ãã2åå
¥åãã¦ããã£ã¦ããã®åè¨å¤ãã0 ãã大ããã£ãã 0ï¼æ£å¸¸çµäºï¼ãè¿ãã0 以ä¸ã ã£ãã 1ï¼ç°å¸¸çµäºï¼ãè¿ãã¾ãã
ã½ã¼ã¹ã³ã¼ãã¯ä»¥ä¸ã®éãã§ããã¢ãã¬ã¹ã®ç¢ºèªãå¿
è¦ãªã®ã§ãå
é ã§ãmainé¢æ°ã®ã¢ãã¬ã¹ï¼å®è¡ããããã°ã©ã ã®ã¢ãã¬ã¹ï¼ããã¼ã«ã«å¤æ°ã®ã¢ãã¬ã¹ï¼ã¹ã¿ãã¯ã®ã¢ãã¬ã¹ï¼ãmallocé¢æ°ã§ç¢ºä¿ããã¡ã¢ãªã®ã¢ãã¬ã¹ï¼ãã¼ãã®ã¢ãã¬ã¹ï¼ãmallocé¢æ°ã®ã¢ãã¬ã¹ï¼å
±æã©ã¤ãã©ãªã®ã¢ãã¬ã¹ï¼ã表示ãã¦ãã¾ãã
#include <stdio.h>
#include <stdlib.h>
int sub( int data )
{
int data2;
printf( "input data2: " );
scanf( "%d", &data2 );
return data + data2;
}
int main( int argc, void *argv[] )
{
int ret, data;
char buf[20], *mbuf;
mbuf = malloc( 20 );
printf( " main: %p\n", main );
printf( " buf: %p\n", buf );
printf( " mbuf: %p\n", mbuf );
printf( "malloc: %p\n", malloc );
printf( "input data: " );
scanf( "%d", &data );
ret = sub( data );
printf( "result: %d", ret );
if( ret > 0 )
return 0;
else
return 1;
}
ãã«ãã«ã¯ gcc ã使ãã¾ããv12.2 ã®ããã§ãã
ãã«ããã¦ãå®è¡ãã¦ã¿ã¾ãã1 㨠2 ãå
¥åãã¦ã足ãåããã 3 ã表示ããã¦ãæ£å¸¸çµäºãã¦ãã¾ãã
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/12/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 12.2.0-14' --with-bugurl=file:///usr/share/doc/gcc-12/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-12 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-12-bTRWOB/gcc-12-12.2.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-12-bTRWOB/gcc-12-12.2.0/debian/tmp-gcn/usr --enable-offload-defaulted --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.2.0 (Debian 12.2.0-14)
$ gcc -g -o hello_hello.out hello_hello.c
$ ./hello_hello.out
main: 0x55555555518d
buf: 0x7fffffffdf30
mbuf: 0x5555555592a0
malloc: 0x7ffff7e62860
input data: 1
input data2: 2
result: 3
ãã®ããã°ã©ã ã使ã£ã¦ãå®è¡ãã¡ã¤ã«ã®ã»ãã¥ãªãã£æ©æ§ãè¦ã¦ããã¾ãã
Cè¨èªããã°ã©ã ãããã©ã«ãã§ãã«ãããå®è¡ãã¡ã¤ã«ã®ã»ãã¥ãªãã£æ©æ§ãè¦ã¦ã¿ã
å
ã»ã©ããã«ããã¦å®è¡ãã hello_hello.out ã«å¯¾ãã¦ãchecksec ãå®è¡ãã¦ã¿ããã¨æãã¾ãã1è¡ç®ã¯ãã»ãã¥ãªãã£æ©æ§ã®é
ç®åã§ã2è¡ç®ããã®çµæã§ããä¾ãã°ãã¹ã¿ãã¯ã«ããªã¤ã¯ããã©ã«ãã§ã¯ç¡å¹ã®ããã§ãã
$ ../../tools/checksec.sh-2.7.1/checksec --file=./hello_hello.out
RELRO STACK CANARY NX PIE RPATH RUNPATH Symbols FORTIFY Fortified Fortifiable FILE
Partial RELRO No canary found NX enabled PIE enabled No RPATH No RUNPATH 38 Symbols No 0 1 ./hello_hello.out
ç¾ç¶ã® ASLRï¼Address Space Layout Randomizationï¼ã確èªãã¦ããã¾ãã/proc/sys/kernel/randomize_va_space
ãè¦ãã¨ç¢ºèªã§ãã¾ãã0 ã®å ´åã¯ã©ã³ãã åããã¾ããã1 ã¯ãä¸é¨ãã©ã³ãã åããã¾ãï¼å
±æã©ã¤ãã©ãªãã¹ã¿ãã¯ãªã©ï¼ã2 ã®å ´åã¯å®å
¨ã«ã©ã³ãã åããã¾ãã1 ã®å ´åã«å ãã¦ãbrk() ã§ç®¡çãããã¡ã¢ãªã®éå§ã¢ãã¬ã¹ãã©ã³ãã åãããããã§ãï¼ãã¾ãåãã£ã¦ãªãã§ããã追å ã§ãã¼ãã確ä¿ããã¨ãã®é åï¼ï¼ã
$ cat /proc/sys/kernel/randomize_va_space
2
ã»ãã¥ãªãã£æ©æ§ã«ã¤ãã¦ãç°¡åã«ã¾ã¨ãã¦ããã¾ãã
é
ç® |
å
容 |
RELRO |
RELocation Read Only ã®ãã¨ã§ãNo RELROï¼GOT ã«æ¸ãè¾¼ã¿å¯è½ï¼ãPartial RELROï¼__libc_start_main ãªã©ã®ãããä¸é¨ã® GOT ã¯æ¸ãè¾¼ã¿ç¦æ¢ï¼ãFull RELROï¼GOT ã¯æ¸ãè¾¼ã¿ç¦æ¢ï¼ã®ã©ããã«ãªããå
±æã©ã¤ãã©ãªã®ã¢ãã¬ã¹ãæ ¼ç´ããã¦ãã GOT ãæ¸ãè¾¼ã¿ç¦æ¢ã«ãã |
STACK CANARY |
é¢æ°éå§æã«ã¹ã¿ãã¯ã«ã©ã³ãã ãªå¤ï¼ã«ããªã¤ï¼ãæ ¼ç´ãã¦ãããé¢æ°çµäºæã«ããã®å¤ãå¤åãã¦ããªããããã§ãã¯ããï¼SSP ã¨ãè¨ãï¼ |
NX |
No eXecute ã®ãã¨ã§ãã¹ã¿ãã¯é åã®ã³ã¼ãã®å®è¡ãç¦æ¢ã«ãã |
PIE |
Position Independent Executable ã®ãã¨ã§ããã®å®è¡ãã¡ã¤ã«ãé
ç½®ãããã¢ãã¬ã¹ãã©ã³ãã åãã |
RPATH |
å®è¡ãã¡ã¤ã«ã«å
±æã©ã¤ãã©ãªã®ãµã¼ããããªã¹ããæ ¼ç´ãã¦ãããã¨ç¤ºãï¼æ»æã«å©ç¨ããããã¨ãããã®ã§ãRPATH ãæå¹ãã©ããã表示ãã¦ããï¼ |
RUNPATH |
RPATH ã¨æ©è½ã¯åãã ããLD_LIBRALY_PATHãåªå
ããããããRPATHããå®å
¨ã¨è¨ããã¦ããï¼RUNPATH ãæå¹ãã©ããã表示ãã¦ããï¼ |
Symbols |
å®è¡ãã¡ã¤ã«ã«å«ã¾ãã¦ããã·ã³ãã«æ
å ±ã®æ°ï¼strip ããã¦ãªããã¨ã表示ããï¼ |
FORTIFY |
GCCãGLIBC ã«ãããã»ãã¥ãªãã£æ©è½ãæå¹ãã©ããã示ã |
Fortified |
FORTIFY ã®æ©è½ãæå¹ã«ããé¢æ°ã®æ° |
Fortifiable |
FORTIFY ã®æ©è½æ°ï¼æå¹ã«ã§ããé¢æ°ã®æ°ï¼ |
FILE |
ä»å checksec ã®å¯¾è±¡ã¨ããããã°ã©ã |
RELROã«ã¤ãã¦å
·ä½çã«ç¢ºèªãã
ã¾ããGOTï¼Global Offset Tableï¼ã¨ PLTï¼Procefure Linkage Tableï¼ã«ã¤ãã¦èª¿ã¹ã¾ãã
GOTã¨PLTã«ã¤ãã¦å
·ä½çã«ç¢ºèªãã
å
±æã©ã¤ãã©ãªã®é¢æ°ï¼ä¾ãã°ãprintfé¢æ°ï¼ããããã°ã©ã ããå¼ã³åºãã¨ãã¯ãã¾ããPLT GOT ã¨ã¯ã PLT
GDBï¼gdb-peda å°å
¥æ¸ã¿ï¼ãèµ·åãã¾ããå
é ã® mallocé¢æ°ãã³ã¼ã«ããç´åã§æ¢ã¾ãã¾ããã
$ gdb -q hello_hello.out
no key sequence terminator:
Reading symbols from hello_hello.out...
gdb-peda$ start
Warning: 'set logging off', an alias for the command 'set logging enabled', is deprecated.
Use 'set logging enabled off'.
Warning: 'set logging on', an alias for the command 'set logging enabled', is deprecated.
Use 'set logging enabled on'.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[----------------------------------registers-----------------------------------]
RAX: 0x55555555518d (<main>: push rbp)
RBX: 0x7fffffffe358 --> 0x7fffffffe5d2 ("/home/user/svn/experiment/c/hello_hello.out")
RCX: 0x555555557dd0 --> 0x555555555100 (<__do_global_dtors_aux>: endbr64)
RDX: 0x7fffffffe368 --> 0x7fffffffe5fe ("SHELL=/bin/bash")
RSI: 0x7fffffffe358 --> 0x7fffffffe5d2 ("/home/user/svn/experiment/c/hello_hello.out")
RDI: 0x1
RBP: 0x7fffffffe240 --> 0x1
RSP: 0x7fffffffe200 --> 0x7fffffffe358 --> 0x7fffffffe5d2 ("/home/user/svn/experiment/c/hello_ hello.out")
RIP: 0x55555555519c (<main+15>: mov edi,0x14)
R8 : 0x0
R9 : 0x7ffff7fcf680 (<_dl_fini>: push rbp)
R10: 0x7ffff7fcb878 --> 0xc00120000000e
R11: 0x7ffff7fe1930 (<_dl_audit_preinit>: mov eax,DWORD PTR [rip+0x1b4e2]
R12: 0x0
R13: 0x7fffffffe368 --> 0x7fffffffe5fe ("SHELL=/bin/bash")
R14: 0x555555557dd0 --> 0x555555555100 (<__do_global_dtors_aux>: endbr64)
R15: 0x7ffff7ffd020 --> 0x7ffff7ffe2e0 --> 0x555555554000 --> 0x10102464c457f
EFLAGS: 0x206 (carry PARITY adjust zero sign trap INTERRUPT direction overflow)
[-------------------------------------code-------------------------------------]
0x555555555191 <main+4>: sub rsp,0x40
0x555555555195 <main+8>: mov DWORD PTR [rbp-0x34],edi
0x555555555198 <main+11>: mov QWORD PTR [rbp-0x40],rsi
=> 0x55555555519c <main+15>: mov edi,0x14
0x5555555551a1 <main+20>: call 0x555555555050 <malloc@plt>
0x5555555551a6 <main+25>: mov QWORD PTR [rbp-0x8],rax
0x5555555551aa <main+29>: lea rax,[rip+0xffffffffffffffdc]
0x5555555551b1 <main+36>: mov rsi,rax
[------------------------------------stack-------------------------------------]
0000| 0x7fffffffe200 --> 0x7fffffffe358 --> 0x7fffffffe5d2 ("/home/user/svn/experiment/c/hello _hello.out")
0008| 0x7fffffffe208 --> 0x100000000
0016| 0x7fffffffe210 --> 0x0
0024| 0x7fffffffe218 --> 0x0
0032| 0x7fffffffe220 --> 0x0
0040| 0x7fffffffe228 --> 0x0
0048| 0x7fffffffe230 --> 0x0
0056| 0x7fffffffe238 --> 0x0
[------------------------------------------------------------------------------]
Legend: code, data, rodata, value
Temporary breakpoint 1, main (argc=0x1, argv=0x7fffffffe358) at hello_hello.c:20
20 mbuf = malloc( 20 );
ã¾ããã¡ã¢ãªé
ç½®ã確èªãã¦ããã¾ããéå§ã¢ãã¬ã¹ã¯ã0x555555554000
ã§ãã
$ i proc map
process 319238
Mapped address spaces:
Start Addr End Addr Size Offset Perms objfile
0x555555554000 0x555555555000 0x1000 0x0 r--p /home/user/svn/experiment/c/hello_hello.out
0x555555555000 0x555555556000 0x1000 0x1000 r-xp /home/user/svn/experiment/c/hello_hello.out
0x555555556000 0x555555557000 0x1000 0x2000 r--p /home/user/svn/experiment/c/hello_hello.out
0x555555557000 0x555555558000 0x1000 0x2000 r--p /home/user/svn/experiment/c/hello_hello.out
0x555555558000 0x555555559000 0x1000 0x3000 rw-p /home/user/svn/experiment/c/hello_hello.out
0x7ffff7dc7000 0x7ffff7dca000 0x3000 0x0 rw-p
0x7ffff7dca000 0x7ffff7df0000 0x26000 0x0 r--p /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7df0000 0x7ffff7f45000 0x155000 0x26000 r-xp /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7f45000 0x7ffff7f98000 0x53000 0x17b000 r--p /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7f98000 0x7ffff7f9c000 0x4000 0x1ce000 r--p /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7f9c000 0x7ffff7f9e000 0x2000 0x1d2000 rw-p /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7f9e000 0x7ffff7fab000 0xd000 0x0 rw-p
0x7ffff7fc3000 0x7ffff7fc5000 0x2000 0x0 rw-p
0x7ffff7fc5000 0x7ffff7fc9000 0x4000 0x0 r--p [vvar]
0x7ffff7fc9000 0x7ffff7fcb000 0x2000 0x0 r-xp [vdso]
0x7ffff7fcb000 0x7ffff7fcc000 0x1000 0x0 r--p /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
0x7ffff7fcc000 0x7ffff7ff1000 0x25000 0x1000 r-xp /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
0x7ffff7ff1000 0x7ffff7ffb000 0xa000 0x26000 r--p /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
0x7ffff7ffb000 0x7ffff7ffd000 0x2000 0x30000 r--p /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
0x7ffff7ffd000 0x7ffff7fff000 0x2000 0x32000 rw-p /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
0x7ffffffde000 0x7ffffffff000 0x21000 0x0 rw-p [stack]
mallocé¢æ°ã¯ä¸åº¦ãã使ã£ã¦ãªãã®ã§ã次㮠printfé¢æ°ã§ãGOTãPLT ã«ã¤ãã¦ç¢ºèªãã¾ããéã¢ã»ã³ãã©ã表示ãã¦ãå
¨ä½ãè¦ã¾ãã+20 ã®ã¨ããã§ãmallocé¢æ°ãã³ã¼ã«ãã¦ã+54 ã®ã¨ãã㧠printfé¢æ°ãã³ã¼ã«ãã¦ãã¾ããããã¦ã+81 ã®ã¨ããã§ã2åç®ã® printfé¢æ°ãã³ã¼ã«ãã¦ãã¾ãã
gdb-peda$ disas
Dump of assembler code for function main:
0x000055555555518d <+0>: push rbp
0x000055555555518e <+1>: mov rbp,rsp
0x0000555555555191 <+4>: sub rsp,0x40
0x0000555555555195 <+8>: mov DWORD PTR [rbp-0x34],edi
0x0000555555555198 <+11>: mov QWORD PTR [rbp-0x40],rsi
=> 0x000055555555519c <+15>: mov edi,0x14
0x00005555555551a1 <+20>: call 0x555555555050 <malloc@plt>
0x00005555555551a6 <+25>: mov QWORD PTR [rbp-0x8],rax
0x00005555555551aa <+29>: lea rax,[rip+0xffffffffffffffdc]
0x00005555555551b1 <+36>: mov rsi,rax
0x00005555555551b4 <+39>: lea rax,[rip+0xe5a]
0x00005555555551bb <+46>: mov rdi,rax
0x00005555555551be <+49>: mov eax,0x0
0x00005555555551c3 <+54>: call 0x555555555030 <printf@plt>
0x00005555555551c8 <+59>: lea rax,[rbp-0x30]
0x00005555555551cc <+63>: mov rsi,rax
0x00005555555551cf <+66>: lea rax,[rip+0xe4b]
0x00005555555551d6 <+73>: mov rdi,rax
0x00005555555551d9 <+76>: mov eax,0x0
0x00005555555551de <+81>: call 0x555555555030 <printf@plt>
0x00005555555551e3 <+86>: mov rax,QWORD PTR [rbp-0x8]
0x00005555555551e7 <+90>: mov rsi,rax
0x00005555555551ea <+93>: lea rax,[rip+0xe3c]
0x00005555555551f1 <+100>: mov rdi,rax
0x00005555555551f4 <+103>: mov eax,0x0
0x00005555555551f9 <+108>: call 0x555555555030 <printf@plt>
mallocé¢æ°ããprintfé¢æ°ã¯ã@plt
ã¨ããã®ãä»ãã¦ã¾ããMAPãã¡ã¤ã«ã確èªãã¦ã¿ã¾ãã0x555555554000
ã« 0x1020
ã足ãã¨ãPLT ã®ã¢ãã¬ã¹ã«ãªãã¾ããprintfé¢æ°ã¯ã0x1030
ã¨æ¸ããã¦ãã®ã§ã0x555555554000+0x1030=0x555555555030
ã¨ãªããä¸ã®éã¢ã»ã³ãã©è¡¨ç¤ºã®ã¢ãã¬ã¹ã¨ä¸è´ãã¦ãã¾ãã1ã¤ã® PLT ã®ã¨ã³ããªã 16byte ã¨ãªã£ã¦ãã¾ããPLT ã®é åã¯ãhello_hello.out ã«å«ã¾ãã¦ãã¾ãã
.plt 0x0000000000001020 0x30
*(.plt)
.plt 0x0000000000001020 0x30 /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/Scrt1.o
0x0000000000001030 printf@@GLIBC_2.2.5
0x0000000000001040 __isoc99_scanf@@GLIBC_2.7
ã§ã¯ãprintf@plt
ã«é£ãã å¾ã®ç¶æ
ãè¦ã¦ã¿ã¾ãã16byte ã«æ ¼ç´ããã¦ããã³ã¼ãã¯ã[email protected]
ã«æ ¼ç´ããã¦ããã¢ãã¬ã¹ã¸ã®ã¸ã£ã³ãã¨ã0x555555555020
ã¸ã®ã¸ã£ã³ãã§ããã
[-------------------------------------code-------------------------------------]
0x555555555021: xor eax,0x2fca
0x555555555026: jmp QWORD PTR [rip+0x2fcc]
0x55555555502c: nop DWORD PTR [rax+0x0]
=> 0x555555555030 <printf@plt>: jmp QWORD PTR [rip+0x2fca]
| 0x555555555036 <printf@plt+6>: push 0x0
| 0x55555555503b <printf@plt+11>: jmp 0x555555555020
| 0x555555555040 <__isoc99_scanf@plt>: jmp QWORD PTR [rip+0x2fc2]
| 0x555555555046 <__isoc99_scanf@plt+6>: push 0x1
|-> 0x555555555036 <printf@plt+6>: push 0x0
0x55555555503b <printf@plt+11>: jmp 0x555555555020
0x555555555040 <__isoc99_scanf@plt>: jmp QWORD PTR [rip+0x2fc2]
0x555555555046 <__isoc99_scanf@plt+6>: push 0x1
JUMP is taken
MAPãã¡ã¤ã«ã§ã[email protected]
ã¨æ¸ãããã¢ãã¬ã¹ãè¦ã¦ã¿ã¾ãã_GLOBAL_OFFSET_TABLE_
ã¨æ¸ããã¦ãã¾ãã0x555555554000+0x3fe8=0x555555557fe8
ããã0x555555554000+0x3fe8+0x28=0x555555558010
ã got.plt ã®é åã«ãªãã¾ãã.got
㨠.got.plt
ã¯ã両æ¹ã¨ã GOTé åã¨å¼ã°ããããã§ããã©ã¡ãããhello_hello.out ã«å«ã¾ãã¦ãã¾ãã
.got.plt 0x0000000000003fe8 0x28
*(.got.plt)
.got.plt 0x0000000000003fe8 0x28 /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/Scrt1.o
0x0000000000003fe8 _GLOBAL_OFFSET_TABLE_
*(.igot.plt)
[email protected]
ã«å
¥ã£ã¦ãã¢ãã¬ã¹ã確èªãã¾ããgdb-peda ãä¸ã«è¡¨ç¤ºãã¦ããã¦ããããã«ã0x555555555036
ã® push 0x0
ã§ãã次ã®å½ä»¤ã§ããã1åç®ã¯ã0x555555555020
ã®ã¸ã£ã³ãå
ã§ã¢ãã¬ã¹è§£æ±ºããã¦ã[email protected]
ã« printfé¢æ°ã®ã¢ãã¬ã¹ãæ ¼ç´ãã¦ãããã¯ãã§ããããã«ãã£ã¦ã2åç®ã¯ãç´æ¥ printfé¢æ°ã«ã¸ã£ã³ãã§ããããã«ãªãã¾ãã
gdb-peda$ x/g 0x555555558000
0x555555558000 <printf@got.plt>: 0x0000555555555036
[email protected]
ã«æ ¼ç´ããã¦ããã¢ãã¬ã¹ã«ã¸ã£ã³ããã¦ã¿ã¾ããpush 0x0
ã«é£ã³ã¾ããã
[-------------------------------------code-------------------------------------]
0x555555555026: jmp QWORD PTR [rip+0x2fcc]
0x55555555502c: nop DWORD PTR [rax+0x0]
0x555555555030 <printf@plt>: jmp QWORD PTR [rip+0x2fca]
=> 0x555555555036 <printf@plt+6>: push 0x0
0x55555555503b <printf@plt+11>: jmp 0x555555555020
0x555555555040 <__isoc99_scanf@plt>: jmp QWORD PTR [rip+0x2fc2]
0x555555555046 <__isoc99_scanf@plt+6>: push 0x1
0x55555555504b <__isoc99_scanf@plt+11>: jmp 0x555555555020
ãã®å¾ã0x555555555020
ã«ã¸ã£ã³ããã¦ãã·ã³ã°ã«ã¹ãããå®è¡ã1åããã¨ã以ä¸ã«ãªãã¾ãã1ã¤åã®å½ä»¤ãå¤ã«è¦ãã¾ãï¼0x555555555020
ã ã£ãã®ãã0x555555555021
ã«å¤ãã£ãï¼ããããã¯æ°ã«ããªãã§ããã¾ãã_dl_runtime_resolve_xsave
ã¨ããã¢ãã¬ã¹è§£æ±ºããã¦ãããã¢ã¸ã¥ã¼ã«ã«é£ã¶ããã§ãã
[-------------------------------------code-------------------------------------]
0x55555555501d: add BYTE PTR [rax],al
0x55555555501f: add bh,bh
0x555555555021: xor eax,0x2fca
=> 0x555555555026: jmp QWORD PTR [rip+0x2fcc]
| 0x55555555502c: nop DWORD PTR [rax+0x0]
| 0x555555555030 <printf@plt>: jmp QWORD PTR [rip+0x2fca]
| 0x555555555036 <printf@plt+6>: push 0x0
| 0x55555555503b <printf@plt+11>: jmp 0x555555555020
|-> 0x7ffff7fdd060 <_dl_runtime_resolve_xsave>: push rbx
0x7ffff7fdd061 <_dl_runtime_resolve_xsave+1>: mov rbx,rsp
0x7ffff7fdd064 <_dl_runtime_resolve_xsave+4>: and rsp,0xffffffffffffffc0
0x7ffff7fdd068 <_dl_runtime_resolve_xsave+8>: sub rsp,QWORD PTR [rip+0x1fbe1]
JUMP is taken
ãã®ä¸ã¯ãã¡ãã£ã¨é·ãããªã®ã§ã次㮠printfé¢æ°ã®å¼ã³åºãã¾ã§é²ãã¾ããç´åã¾ã§é²ãã¾ããã
[-------------------------------------code-------------------------------------]
0x5555555551cf <main+66>: lea rax,[rip+0xe4b]
0x5555555551d6 <main+73>: mov rdi,rax
0x5555555551d9 <main+76>: mov eax,0x0
=> 0x5555555551de <main+81>: call 0x555555555030 <printf@plt>
0x5555555551e3 <main+86>: mov rax,QWORD PTR [rbp-0x8]
0x5555555551e7 <main+90>: mov rsi,rax
0x5555555551ea <main+93>: lea rax,[rip+0xe3c]
0x5555555551f1 <main+100>: mov rdi,rax
Guessed arguments:
arg[0]: 0x555555556021 (" buf: %p\n")
arg[1]: 0x7fffffffe210 --> 0x0
ä¸ã«å
¥ãã¾ããå
ã»ã©ã® 1åç®ã® printfé¢æ°ã¨åãã¨ããã«ãã¾ããã
[-------------------------------------code-------------------------------------]
0x555555555021: xor eax,0x2fca
0x555555555026: jmp QWORD PTR [rip+0x2fcc]
0x55555555502c: nop DWORD PTR [rax+0x0]
=> 0x555555555030 <printf@plt>: jmp QWORD PTR [rip+0x2fca]
| 0x555555555036 <printf@plt+6>: push 0x0
| 0x55555555503b <printf@plt+11>: jmp 0x555555555020
| 0x555555555040 <__isoc99_scanf@plt>: jmp QWORD PTR [rip+0x2fc2]
| 0x555555555046 <__isoc99_scanf@plt+6>: push 0x1
|-> 0x7ffff7e1c5b0 <__printf>: sub rsp,0xd8
0x7ffff7e1c5b7 <__printf+7>: mov QWORD PTR [rsp+0x28],rsi
0x7ffff7e1c5bc <__printf+12>: mov QWORD PTR [rsp+0x30],rdx
0x7ffff7e1c5c1 <__printf+17>: mov QWORD PTR [rsp+0x38],rcx
JUMP is taken
åãããã«ã0x555555558000
ã«æ ¼ç´ããã¦ããå¤ãè¦ã¦ã¿ã¾ãããã£ãã¯æ¬¡ã®è¡ã® push 0x0
ã®ã¢ãã¬ã¹ãæ ¼ç´ããã¦ãã¾ããããå¤åãã¦ãã¾ãã
gdb-peda$ x/g 0x555555558000
0x555555558000 <printf@got.plt>: 0x00007ffff7e1c5b0
å
ã»ã©ã® i proc map
ã®åºåã®ä¸é¨ãè²¼ãã¾ãã0x00007ffff7e1c5b0
ã¯ã2è¡ç®ã®å®è¡å¯è½ãª x
ã®ä»ãã¦ãã¨ããã§ãããlibc ãªã®ã§ãprintfé¢æ°ã®å®è£
ãæ ¼ç´ããã¦ããã¨æããã¾ãã
0x7ffff7dca000 0x7ffff7df0000 0x26000 0x0 r--p /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7df0000 0x7ffff7f45000 0x155000 0x26000 r-xp /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7f45000 0x7ffff7f98000 0x53000 0x17b000 r--p /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7f98000 0x7ffff7f9c000 0x4000 0x1ce000 r--p /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7f9c000 0x7ffff7f9e000 0x2000 0x1d2000 rw-p /usr/lib/x86_64-linux-gnu/libc.so.6
ããã¾ã§ã®å
容ãã¾ã¨ãã¾ããã¾ããpltï¼hello_hello.out ã«å«ã¾ãã¦ãã 16byteã®ããã°ã©ã ã ã£ãï¼ã«ã¯ãgot.pltï¼hello_hello.out ã«å«ã¾ãã¦ãããå
±æã©ã¤ãã©ãªãã³ã¼ã«ããããã®ã¢ãã¬ã¹ãæ ¼ç´ãããã¼ãã«ã ã£ãï¼ããã¢ãã¬ã¹ãåå¾ãã¦ã¸ã£ã³ãããå¦çã¨ã1åç®ã® printfé¢æ°ã§å®è¡ããã¦ãã _dl_runtime_resolve_xsave
ã¨ããã¢ãã¬ã¹è§£æ±ºããã¦ãããããªé¢æ°ã«ã¸ã£ã³ãããå¦çãé
ç½®ããã¦ãã¾ããã
2åç®ã® printfé¢æ°ã§ã¯ãåãããã«ãplt ã«å
¥ã£ã¦ãåæ§ã«ãgot.plt ããã¢ãã¬ã¹ãåå¾ãã¾ãããããã® got.plt ã«æ ¼ç´ãããã¢ãã¬ã¹ããprintfé¢æ°ã®ã¢ãã¬ã¹ã«å¤åãã¦ãã¾ããã1åç®ã§ã¢ãã¬ã¹è§£æ±ºãã¦ãgot.plt ã« printfé¢æ°ã®ã¢ãã¬ã¹ãæ ¼ç´ãã¦ãããã®ã§ã2åç®ä»¥éã¯ç´æ¥ printfé¢æ°ã«ã¸ã£ã³ãã§ããããã«ãªã£ãã¨ãããã¨ã§ãã
ä»å確èªããã®ã¯ãplt 㨠got.plt ã§ãããããããGOTé åã¯ãgot 㨠got.plt ã¨ããã»ã¯ã·ã§ã³ã®ãã¨ã§ããplt.got ã¨ããé åãããã¾ãããããã«ã¤ãã¦ã¯ãä»ã¯åãããªãã®ã§ã調æ»ã§ããããããã«è¿½è¨ãã¾ãã
Partial RELROã¨FULL RELROã«ã¤ãã¦
RELRO ã¯ãGOT ã®æ¸ãè¾¼ã¿ãç¦æ¢ããã»ãã¥ãªãã£æ©æ§ã§ãã
ã³ã³ãã¤ã«ãªãã·ã§ã³ã® -Wl,-z,relro
ã¯ããªã³ã«ã« RELROã»ã¯ã·ã§ã³ãä½ãããã«æ示ãã¾ããã¾ããã³ã³ãã¤ã«ãªãã·ã§ã³ã® -Wl,-z,now
ã¯ããªã³ã«ã«ããã°ã©ã èµ·åæã«ãã¹ã¦ã®ã·ã³ãã«ã®è§£æ±ºãè¡ãããæ示ãã¾ãã
以ä¸ã«ã3éãã試ãã¾ãããçµè«ã¨ãã¦ã¯ãchecksec ã®åºåã Full RELRO ã«ããã«ã¯ã-Wl,-z,now
ã ãã§ããã¨ãããã¨ã«ãªãã¾ãã
$ gcc -g -Wl,-z,relro -Wl,-Map=hello_hello_relro.map -o hello_hello_relro.out hello_hello.c
$ ../../tools/checksec.sh-2.7.1/checksec --file=./hello_hello_relro.out
RELRO STACK CANARY NX PIE RPATH RUNPATH Symbols FORTIFY Fortified Fortifiable FILE
Partial RELRO No canary found NX enabled PIE enabled No RPATH No RUNPATH 39 Symbols No 0 1 ./hello_hello_relro.out
$ gcc -g -Wl,-z,now -Wl,-Map=hello_hello_now.map -o hello_hello_now.out hello_hello.c
$ ../../tools/checksec.sh-2.7.1/checksec --file=./hello_hello_now.out
RELRO STACK CANARY NX PIE RPATH RUNPATH Symbols FORTIFY Fortified Fortifiable FILE
Full RELRO No canary found NX enabled PIE enabled No RPATH No RUNPATH 39 Symbols No 0 1 ./hello_hello_now.out
$ gcc -g -Wl,-z,relro -Wl,-z,now -Wl,-Map=hello_hello_relro_now.map -o hello_hello_relro_now.out hello_hello.c
$ ../../tools/checksec.sh-2.7.1/checksec --file=./hello_hello_relro_now.out
RELRO STACK CANARY NX PIE RPATH RUNPATH Symbols FORTIFY Fortified Fortifiable FILE
Full RELRO No canary found NX enabled PIE enabled No RPATH No RUNPATH 39 Symbols No 0 1 ./hello_hello_relro_now.out
æåã«ãã³ã³ãã¤ã«ãªãã·ã§ã³ã® relro ã«ã¤ãã¦èª¿ã¹ã¾ããrelro ãæå®ãã¦ãªãé常ã®ããã°ã©ã ã¨æ¯è¼ãã¾ããMAPãã¡ã¤ã«ã¨éã¢ã»ã³ãã©ã§æ¯è¼ãã¾ãããçµæãè¦ãéããã³ã³ãã¤ã«ãªãã·ã§ã³ã® relro ãä»ãã¦ãå¤åã¯ããã¾ããã§ãããä¸ã®çµæã§ã relro ã¯å½±é¿ãè¦ãããªãã£ãã®ã§ããããããã®ããããã¾ããã
$ objdump -M intel -d hello_hello.out > hello_hello.s
$ objdump -M intel -d hello_hello_relro.out > hello_hello_relro.s
$ diff hello_hello.s hello_hello_relro.s
--- hello_hello.s 2024-09-14 16:37:21.724997508 +0900
+++ hello_hello_relro.s 2024-09-14 16:37:10.970791296 +0900
@@ -1,5 +1,5 @@
-hello_hello.out: file format elf64-x86-64
+hello_hello_relro.out: file format elf64-x86-64
$ diff hello_hello.map hello_hello_relro.map
--- hello_hello.map 2024-09-13 20:24:36.926962157 +0900
+++ hello_hello_relro.map 2024-09-13 22:50:58.807749077 +0900
@@ -6,7 +6,7 @@
As-needed library included to satisfy reference by file (symbol)
-libc.so.6 /tmp/ccVKmQjn.o (malloc@@GLIBC_2.2.5)
+libc.so.6 /tmp/cc5qA2Go.o (malloc@@GLIBC_2.2.5)
Discarded input sections
@@ -19,7 +19,7 @@
.note.gnu.property
0x0000000000000000 0x20 /usr/lib/gcc/x86_64-linux-gnu/12/crtbeginS.o
.note.GNU-stack
- 0x0000000000000000 0x0 /tmp/ccVKmQjn.o
+ 0x0000000000000000 0x0 /tmp/cc5qA2Go.o
.note.GNU-stack
0x0000000000000000 0x0 /usr/lib/gcc/x86_64-linux-gnu/12/crtendS.o
.note.gnu.property
@@ -37,7 +37,7 @@
LOAD /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/Scrt1.o
LOAD /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/crti.o
LOAD /usr/lib/gcc/x86_64-linux-gnu/12/crtbeginS.o
-LOAD /tmp/ccVKmQjn.o
+LOAD /tmp/cc5qA2Go.o
LOAD /usr/lib/gcc/x86_64-linux-gnu/12/libgcc.a
LOAD /usr/lib/gcc/x86_64-linux-gnu/12/libgcc_s.so
START GROUP
@@ -179,7 +179,7 @@
.text 0x0000000000001082 0x0 /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/crti.o
*fill* 0x0000000000001082 0xe
.text 0x0000000000001090 0xb9 /usr/lib/gcc/x86_64-linux-gnu/12/crtbeginS.o
- .text 0x0000000000001149 0x13c /tmp/ccVKmQjn.o
+ .text 0x0000000000001149 0x13c /tmp/cc5qA2Go.o
0x0000000000001149 sub
0x000000000000118d main
.text 0x0000000000001285 0x0 /usr/lib/gcc/x86_64-linux-gnu/12/crtendS.o
@@ -201,7 +201,7 @@
*(.rodata .rodata.* .gnu.linkonce.r.*)
.rodata.cst4 0x0000000000002000 0x4 /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/Scrt1.o
0x0000000000002000 _IO_stdin_used
- .rodata 0x0000000000002004 0x59 /tmp/ccVKmQjn.o
+ .rodata 0x0000000000002004 0x59 /tmp/cc5qA2Go.o
.rodata1
*(.rodata1)
@@ -220,7 +220,7 @@
.eh_frame 0x00000000000020c8 0x40 /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/Scrt1.o
.eh_frame 0x0000000000002108 0x18 /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/Scrt1.o
0x30 (size before relaxing)
- .eh_frame 0x0000000000002120 0x40 /tmp/ccVKmQjn.o
+ .eh_frame 0x0000000000002120 0x40 /tmp/cc5qA2Go.o
0x58 (size before relaxing)
.eh_frame 0x0000000000002160 0x4 /usr/lib/gcc/x86_64-linux-gnu/12/crtendS.o
*(.eh_frame.*)
@@ -334,7 +334,7 @@
.data.rel.local
0x0000000000004018 0x8 /usr/lib/gcc/x86_64-linux-gnu/12/crtbeginS.o
0x0000000000004018 __dso_handle
- .data 0x0000000000004020 0x0 /tmp/ccVKmQjn.o
+ .data 0x0000000000004020 0x0 /tmp/cc5qA2Go.o
.data 0x0000000000004020 0x0 /usr/lib/gcc/x86_64-linux-gnu/12/crtendS.o
.data 0x0000000000004020 0x0 /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/crtn.o
@@ -359,7 +359,7 @@
.bss 0x0000000000004020 0x0 /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/Scrt1.o
.bss 0x0000000000004020 0x0 /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/crti.o
.bss 0x0000000000004020 0x1 /usr/lib/gcc/x86_64-linux-gnu/12/crtbeginS.o
- .bss 0x0000000000004021 0x0 /tmp/ccVKmQjn.o
+ .bss 0x0000000000004021 0x0 /tmp/cc5qA2Go.o
.bss 0x0000000000004021 0x0 /usr/lib/gcc/x86_64-linux-gnu/12/crtendS.o
.bss 0x0000000000004021 0x0 /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/crtn.o
*(COMMON)
@@ -406,7 +406,7 @@
*(.comment)
.comment 0x0000000000000000 0x1f /usr/lib/gcc/x86_64-linux-gnu/12/crtbeginS.o
0x20 (size before relaxing)
- .comment 0x000000000000001f 0x20 /tmp/ccVKmQjn.o
+ .comment 0x000000000000001f 0x20 /tmp/cc5qA2Go.o
.comment 0x000000000000001f 0x20 /usr/lib/gcc/x86_64-linux-gnu/12/crtendS.o
.gnu.build.attributes
@@ -427,29 +427,29 @@
.debug_aranges 0x0000000000000000 0x30
*(.debug_aranges)
.debug_aranges
- 0x0000000000000000 0x30 /tmp/ccVKmQjn.o
+ 0x0000000000000000 0x30 /tmp/cc5qA2Go.o
.debug_pubnames
*(.debug_pubnames)
.debug_info 0x0000000000000000 0x1ad
*(.debug_info .gnu.linkonce.wi.*)
- .debug_info 0x0000000000000000 0x1ad /tmp/ccVKmQjn.o
+ .debug_info 0x0000000000000000 0x1ad /tmp/cc5qA2Go.o
.debug_abbrev 0x0000000000000000 0x10d
*(.debug_abbrev)
- .debug_abbrev 0x0000000000000000 0x10d /tmp/ccVKmQjn.o
+ .debug_abbrev 0x0000000000000000 0x10d /tmp/cc5qA2Go.o
.debug_line 0x0000000000000000 0x93
*(.debug_line .debug_line.* .debug_line_end)
- .debug_line 0x0000000000000000 0x93 /tmp/ccVKmQjn.o
+ .debug_line 0x0000000000000000 0x93 /tmp/cc5qA2Go.o
.debug_frame
*(.debug_frame)
.debug_str 0x0000000000000000 0xdf
*(.debug_str)
- .debug_str 0x0000000000000000 0xdf /tmp/ccVKmQjn.o
+ .debug_str 0x0000000000000000 0xdf /tmp/cc5qA2Go.o
0x11e (size before relaxing)
.debug_loc
@@ -483,7 +483,7 @@
0x0000000000000000 0x7a
*(.debug_line_str)
.debug_line_str
- 0x0000000000000000 0x7a /tmp/ccVKmQjn.o
+ 0x0000000000000000 0x7a /tmp/cc5qA2Go.o
0xb2 (size before relaxing)
.debug_loclists
@@ -511,4 +511,4 @@
*(.note.GNU-stack)
*(.gnu_debuglink)
*(.gnu.lto_*)
-OUTPUT(hello_hello.out elf64-x86-64)
+OUTPUT(hello_hello_relro.out elf64-x86-64)
次ã«ãã³ã³ãã¤ã«ãªãã·ã§ã³ã® now ã«ã¤ãã¦èª¿ã¹ã¾ããnow ãæå®ãã¦ãªãé常ã®ããã°ã©ã ã¨æ¯è¼ãã¾ããMAPãã¡ã¤ã«ãæ¯è¼ããã¨ãããå·®ç°ãå¤ããå
¨é¨ã¯è²¼ãã¾ããããã»ã¯ã·ã§ã³ã®ãµã¤ãºãç°ãªãã¨ããã ãè²¼ãã¾ããdynamic ã¨ããã»ã¯ã·ã§ã³ã¨ãgot ã®ã»ã¯ã·ã§ã³ã®ãµã¤ãºãç°ãªã£ã¦ãã¾ããã
ã¾ããã¡ãã£ã¨åããã«ããã®ã§ãããããããã® .got
以éã®è¡ã«æ³¨ç®ãã¾ããnow ãæå®ããªãæ¹ã¯ã.got
ã 0x3fb8 ããå§ã¾ãã0x30 ã®ãµã¤ãºãããï¼0x3fb8+0x30=0x3fe8ï¼ã次ã«ã.got.plt
ã 0x3fe8 ããå§ã¾ãã0x28 ã®ãµã¤ãºãããã¾ãï¼0x3fe8+0x28=0x4010ï¼ã0x555555554000 ãã 0x555555558000 ã¾ã§ã¯æ¸ãè¾¼ã¿ç¦æ¢ã§ããã0x555555558000 以éã¯æ¸ãè¾¼ã¿å¯è½ã§ããã¤ã¾ãã.got.plt
ã®æ«å°¾ã® 16byte ã ããæ¸ãè¾¼ã¿å¯è½ã¨ãããã¨ã«ãªãã¾ãã
ä¸æ¹ãnow ãæå®ããæ¹ã¯ã.got
ã 0x3fa8 ããå§ã¾ãã0x58 ã®ãµã¤ãºãããã ãã§ãï¼0x3fa8+0x58=0x4000ï¼ã.got.plt
ãããããã«è¦ãã¾ãããããã¯ãµãã»ã¯ã·ã§ã³ã¨ãããããï¼from ChatGPTï¼ã.got
ã®ä¸ã«å«ã¾ãã¦ãã¾ããã¤ã¾ããnow ãæå®ããæ¹ã¯ã.got.plt
ã¨ããã»ã¯ã·ã§ã³ã¯ç¡ããªã£ã¦ãã¦ã.got
ã«å«ã¾ããå½¢ã«ãªã£ã¦ãã¾ãããreadelfã³ãã³ãã® -S ãªãã·ã§ã³ã§ç¢ºèªããã¨ããã.got.plt
ã¨ããã»ã¯ã·ã§ã³ã¯ããã¾ããã§ãããGOTé åã¯ãæ¸ãè¾¼ã¿ç¦æ¢ã¨ãããã¨ã«ãªãã¾ãã
/* hello_hello.map */
.dynamic 0x0000000000003dd8 0x1e0
*(.dynamic)
.dynamic 0x0000000000003dd8 0x1e0 /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/Scrt1.o
0x0000000000003dd8 _DYNAMIC
.got 0x0000000000003fb8 0x30
*(.got)
.got 0x0000000000003fb8 0x30 /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/Scrt1.o
*(.igot)
0x0000000000003fe8 . = DATA_SEGMENT_RELRO_END (., (SIZEOF (.got.plt) >= 0x18)?0x18:0x0)
.got.plt 0x0000000000003fe8 0x28
*(.got.plt)
.got.plt 0x0000000000003fe8 0x28 /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/Scrt1.o
0x0000000000003fe8 _GLOBAL_OFFSET_TABLE_
*(.igot.plt)
.data 0x0000000000004010 0x10
/* hello_hello_now.map */
.dynamic 0x0000000000003db8 0x1f0
*(.dynamic)
.dynamic 0x0000000000003db8 0x1f0 /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/Scrt1.o
0x0000000000003db8 _DYNAMIC
.got 0x0000000000003fa8 0x58
*(.got.plt)
.got.plt 0x0000000000003fa8 0x28 /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/Scrt1.o
0x0000000000003fa8 _GLOBAL_OFFSET_TABLE_
*(.igot.plt)
*(.got)
.got 0x0000000000003fd0 0x30 /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/Scrt1.o
*(.igot)
0x0000000000004000 . = DATA_SEGMENT_RELRO_END (., 0x0)
éã¢ã»ã³ãã©ãæ¯è¼ããã¨ããããå·®ç°ãå¤ãã¦è²¼ãã¾ããããã³ã¼ãã¨ãã¦ã¯ä¸ç·ã§ãã¢ãã¬ã¹ããºã¬ã¦ããã ãã®ããã§ããã
ç¶ãã¦ãã³ã³ãã¤ã«ãªãã·ã§ã³ã® now ãæå®ããæ¹ã GDB ã§ç¢ºèªãã¾ãã1åç®ã® printf@plt
ã¾ã§é²ãã¾ãããå
ã»ã©ã® now ãæå®ããªãã£ãããã°ã©ã ã§ã¯ã0x555555555030
ã§ã[email protected]
ã«æ ¼ç´ããã¦ããã¢ãã¬ã¹ã¯ã次ã®è¡ã® 0x555555555036
ã§ããããnow ãæå®ããããã°ã©ã ã§ã¯ã1åç®ã® printfé¢æ°ããã0x7ffff7e1c5b0
ï¼printfé¢æ°ã®å®è£
ãé
ç½®ãããã¢ãã¬ã¹ï¼ãæ ¼ç´ããã¦ãã¾ãããã³ã³ãã¤ã«ãªãã·ã§ã³ã® now ã¯ãããã°ã©ã èµ·åæã«å
¨ã¦ã®ã·ã³ãã«ã解決ããã¨ããå½±é¿ã確èªã§ãã¾ããã
[-------------------------------------code-------------------------------------]
0x555555555021: xor eax,0x2f8a
0x555555555026: jmp QWORD PTR [rip+0x2f8c]
0x55555555502c: nop DWORD PTR [rax+0x0]
=> 0x555555555030 <printf@plt>: jmp QWORD PTR [rip+0x2f8a]
| 0x555555555036 <printf@plt+6>: push 0x0
| 0x55555555503b <printf@plt+11>: jmp 0x555555555020
| 0x555555555040 <__isoc99_scanf@plt>: jmp QWORD PTR [rip+0x2f82]
| 0x555555555046 <__isoc99_scanf@plt+6>: push 0x1
|-> 0x7ffff7e1c5b0 <__printf>: sub rsp,0xd8
0x7ffff7e1c5b7 <__printf+7>: mov QWORD PTR [rsp+0x28],rsi
0x7ffff7e1c5bc <__printf+12>: mov QWORD PTR [rsp+0x30],rdx
0x7ffff7e1c5c1 <__printf+17>: mov QWORD PTR [rsp+0x38],rcx
JUMP is taken
i proc map ã確èªãã¾ããprintfé¢æ°ã®ã¢ãã¬ã¹ãæ ¼ç´ããã¦ãã 0x555555557fc0
ã¯ãæ¸ãè¾¼ã¿ã®ãã©ã°ããªããèªã¿åãå°ç¨ã«ãªã£ã¦ãã¾ããããã«ãã£ã¦ãGOT ãæ¸ãè¾¼ãæ»æãé²ãã§ããã¨ãããã¨ã«ãªãã¾ãã
gdb-peda$ i proc map
process 397724
Mapped address spaces:
Start Addr End Addr Size Offset Perms objfile
0x555555554000 0x555555555000 0x1000 0x0 r--p /home/user/svn/experiment/c/hello_hello_now.out
0x555555555000 0x555555556000 0x1000 0x1000 r-xp /home/user/svn/experiment/c/hello_hello_now.out
0x555555556000 0x555555557000 0x1000 0x2000 r--p /home/user/svn/experiment/c/hello_hello_now.out
0x555555557000 0x555555558000 0x1000 0x2000 r--p /home/user/svn/experiment/c/hello_hello_now.out
0x555555558000 0x555555559000 0x1000 0x3000 rw-p /home/user/svn/experiment/c/hello_hello_now.out
0x555555559000 0x55555557a000 0x21000 0x0 rw-p [heap]
0x7ffff7dc7000 0x7ffff7dca000 0x3000 0x0 rw-p
0x7ffff7dca000 0x7ffff7df0000 0x26000 0x0 r--p /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7df0000 0x7ffff7f45000 0x155000 0x26000 r-xp /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7f45000 0x7ffff7f98000 0x53000 0x17b000 r--p /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7f98000 0x7ffff7f9c000 0x4000 0x1ce000 r--p /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7f9c000 0x7ffff7f9e000 0x2000 0x1d2000 rw-p /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7f9e000 0x7ffff7fab000 0xd000 0x0 rw-p
0x7ffff7fc3000 0x7ffff7fc5000 0x2000 0x0 rw-p
0x7ffff7fc5000 0x7ffff7fc9000 0x4000 0x0 r--p [vvar]
0x7ffff7fc9000 0x7ffff7fcb000 0x2000 0x0 r-xp [vdso]
0x7ffff7fcb000 0x7ffff7fcc000 0x1000 0x0 r--p /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
0x7ffff7fcc000 0x7ffff7ff1000 0x25000 0x1000 r-xp /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
0x7ffff7ff1000 0x7ffff7ffb000 0xa000 0x26000 r--p /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
0x7ffff7ffb000 0x7ffff7ffd000 0x2000 0x30000 r--p /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
0x7ffff7ffd000 0x7ffff7fff000 0x2000 0x32000 rw-p /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
0x7ffffffde000 0x7ffffffff000 0x21000 0x0 rw-p [stack]
now ãä»ãã¦ãªãããã°ã©ã ã®æ¹ã® got.plt ã®é åã GDB ã§ç¢ºèªãã¾ãã1åç®ã®printfé¢æ°ã«é£ãã ã¨ããã§ãã0x555555558000
ã対象ã®ã¢ãã¬ã¹ã§ãæ¸ãè¾¼ã¿ãã©ã°ãä»ãã¦ãããã¨ã確èªã§ãã¾ãã
[-------------------------------------code-------------------------------------]
0x555555555021: xor eax,0x2fca
0x555555555026: jmp QWORD PTR [rip+0x2fcc]
0x55555555502c: nop DWORD PTR [rax+0x0]
=> 0x555555555030 <printf@plt>: jmp QWORD PTR [rip+0x2fca]
| 0x555555555036 <printf@plt+6>: push 0x0
| 0x55555555503b <printf@plt+11>: jmp 0x555555555020
| 0x555555555040 <__isoc99_scanf@plt>: jmp QWORD PTR [rip+0x2fc2]
| 0x555555555046 <__isoc99_scanf@plt+6>: push 0x1
|-> 0x555555555036 <printf@plt+6>: push 0x0
0x55555555503b <printf@plt+11>: jmp 0x555555555020
0x555555555040 <__isoc99_scanf@plt>: jmp QWORD PTR [rip+0x2fc2]
0x555555555046 <__isoc99_scanf@plt+6>: push 0x1
JUMP is taken
gdb-peda$ i proc map
process 397764
Mapped address spaces:
Start Addr End Addr Size Offset Perms objfile
0x555555554000 0x555555555000 0x1000 0x0 r--p /home/user/svn/experiment/c/hello_hello.out
0x555555555000 0x555555556000 0x1000 0x1000 r-xp /home/user/svn/experiment/c/hello_hello.out
0x555555556000 0x555555557000 0x1000 0x2000 r--p /home/user/svn/experiment/c/hello_hello.out
0x555555557000 0x555555558000 0x1000 0x2000 r--p /home/user/svn/experiment/c/hello_hello.out
0x555555558000 0x555555559000 0x1000 0x3000 rw-p /home/user/svn/experiment/c/hello_hello.out
0x555555559000 0x55555557a000 0x21000 0x0 rw-p [heap]
0x7ffff7dc7000 0x7ffff7dca000 0x3000 0x0 rw-p
0x7ffff7dca000 0x7ffff7df0000 0x26000 0x0 r--p /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7df0000 0x7ffff7f45000 0x155000 0x26000 r-xp /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7f45000 0x7ffff7f98000 0x53000 0x17b000 r--p /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7f98000 0x7ffff7f9c000 0x4000 0x1ce000 r--p /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7f9c000 0x7ffff7f9e000 0x2000 0x1d2000 rw-p /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7f9e000 0x7ffff7fab000 0xd000 0x0 rw-p
0x7ffff7fc3000 0x7ffff7fc5000 0x2000 0x0 rw-p
0x7ffff7fc5000 0x7ffff7fc9000 0x4000 0x0 r--p [vvar]
0x7ffff7fc9000 0x7ffff7fcb000 0x2000 0x0 r-xp [vdso]
0x7ffff7fcb000 0x7ffff7fcc000 0x1000 0x0 r--p /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
0x7ffff7fcc000 0x7ffff7ff1000 0x25000 0x1000 r-xp /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
0x7ffff7ff1000 0x7ffff7ffb000 0xa000 0x26000 r--p /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
0x7ffff7ffb000 0x7ffff7ffd000 0x2000 0x30000 r--p /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
0x7ffff7ffd000 0x7ffff7fff000 0x2000 0x32000 rw-p /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
0x7ffffffde000 0x7ffffffff000 0x21000 0x0 rw-p [stack]
ããã¾ã§ã®èª¿ã¹ã§ãgot
ã®é åã«ã¤ãã¦æ¨æ¸¬ãã§ãã¾ããgot.plt
ã¯å®è¡ä¸ã«ã·ã³ãã«è§£æ±ºãè¡ããä¸é¨ã¯æ¸ãè¾¼ã¿å¯è½ãªé åã§ããããgot
ã¯ããã°ã©ã èµ·åæã«ã·ã³ãã«è§£æ±ºï¼ãªãã±ã¼ã·ã§ã³ï¼ãè¡ãèªã¿åãå°ç¨ã®é åã§ããã¨ãããã¨ã§ãã
ããå°ã確èªãã¦ã¿ã¾ããnow ãæå®ãã¦ããªãæ®éã®ããã°ã©ã ã§ãGDB ãèµ·åãã¦ã以ä¸ã¯ãmallocé¢æ°ã®ä¸ã«å
¥ã£ãã¨ããã§ãã
printfé¢æ°ã¨ã¯ç°ãªãã1åããå¼ã°ããªããããªã®ããæåãããlibc ã®ã¢ãã¬ã¹ãæ ¼ç´ããã¦ãã¾ããã¾ããå°ãä¸ã«è¦ãã¦ãã __cxa_finalize
ã¨ããé¢æ°ããæåãã libc ã®ã¢ãã¬ã¹ãæ ¼ç´ããã¦ãã¾ããããããã0x555555557000
ãã 0x555555558000
ã¾ã§ã¯èªã¿åãå°ç¨ã§ãã
[-------------------------------------code-------------------------------------]
0x555555555040 <__isoc99_scanf@plt>: jmp QWORD PTR [rip+0x2fc2]
0x555555555046 <__isoc99_scanf@plt+6>: push 0x1
0x55555555504b <__isoc99_scanf@plt+11>: jmp 0x555555555020
=> 0x555555555050 <malloc@plt>: jmp QWORD PTR [rip+0x2f7a]
| 0x555555555056 <malloc@plt+6>: xchg ax,ax
| 0x555555555058 <__cxa_finalize@plt>: jmp QWORD PTR [rip+0x2f82]
| 0x55555555505e <__cxa_finalize@plt+6>: xchg ax,ax
| 0x555555555060 <_start>: xor ebp,ebp
|-> 0x7ffff7e62860 <__GI___libc_malloc>: push r12
0x7ffff7e62862 <__GI___libc_malloc+2>: push rbp
0x7ffff7e62863 <__GI___libc_malloc+3>: push rbx
0x7ffff7e62864 <__GI___libc_malloc+4>: mov rbx,rdi
JUMP is taken
gdb-peda$ x/g 0x555555557fe0
0x555555557fe0: 0x00007ffff7e07f40
ã ãã¶é·ããªãã¾ããããPartial RELRO ã«ã¤ãã¦ãã ãã¶åãã£ãæ°ããã¾ããããä¸é¨ã® GOT ãæ¸ãè¾¼ã¿ç¦æ¢ã¨ãããã¨ã§ãããããããããããä¸åº¦ããå¼ã°ããªãé¢æ°ã¯æ¸ãè¾¼ã¿ç¦æ¢ãªã®ããããã¾ãããã§ããã大é¨åã® GOT ã¯æ¸ãè¾¼ã¿å¯è½ãªãã ã¨æãã¾ãã
STACK CANARYï¼SSPï¼ã«ã¤ãã¦å
·ä½çã«ç¢ºèªãã
åé ã§èª¿ã¹ãéããããã©ã«ãã§ã¯ãSTACK CANARYï¼ã¹ã¿ãã¯ã«ããªã¢ï¼ã¯ç¡å¹ã§ãããã¹ã¿ãã¯ä¿è·ã¨ãããã¨ã§ãSSPï¼Stack Smashing Protectionï¼ã¨ãå¼ã°ãã¾ããã¨ããããã¯ãSSP ã®æ段ã®ä¸ã¤ã STACK CANARY ããããã¾ããããããã§ã¯ãSSP ã¨å¼ã¶ãã¨ã«ãã¾ãã
SSP ãæå¹ã«ããæ¹æ³ã¯ãããã¤ãããããã§ãããã¾ãã¯ãä¸çªç°¡åãªãã¤ã«ãã¾ãã-fstack-protector
ãæå®ãã¦ã¿ã¾ãããchecksec ã®çµæããCanary found
ã«å¤ããã¾ãããéã¢ã»ã³ãã©ã確èªããã¨ãããmainé¢æ°ã«ã¯ããã§ãã¯ãå
¥ã£ã¦ãã¾ããããsubé¢æ°ã«ã¯ãã§ãã¯ãå
¥ã£ã¦ãã¾ããã§ããã
$ gcc -g -fstack-protector -Wl,-Map=hello_hello_ssp.map -o hello_hello_ssp.out hello_hello.c
$ ../../tools/checksec.sh-2.7.1/checksec --file=./hello_hello_ssp.out
RELRO STACK CANARY NX PIE RPATH RUNPATH Symbols FORTIFY Fortified Fortifiable FILE
Partial RELRO Canary found NX enabled PIE enabled No RPATH No RUNPATH 40 Symbols No 0 1 ./hello_hello_ssp.out
$ objdump -M intel -d hello_hello_ssp.out > hello_hello_ssp.s
ç¶ãã¦ãããå°ã SSP ãå¼·åãã -fstack-protector-all
ããã£ã¦ã¿ã¾ããchecksec ã¯ä¸ã¨åãçµæã§ããããã¡ãã¯ãmainé¢æ°ã«å ãã¦ãsubé¢æ°ãã¹ã¿ãã¯ã«ããªã¢ã®ãã§ãã¯ãå
¥ã£ã¦ãã¾ããã
$ gcc -g -fstack-protector-all -Wl,-Map=hello_hello_ssp_all.map -o hello_hello_ssp_all.out hello_hello.c
$ ../../tools/checksec.sh-2.7.1/checksec --file=./hello_hello_ssp_all.out
RELRO STACK CANARY NX PIE RPATH RUNPATH Symbols FORTIFY Fortified Fortifiable FILE
Partial RELRO Canary found NX enabled PIE enabled No RPATH No RUNPATH 40 Symbols No 0 1 ./hello_hello_ssp_all.out
$ objdump -M intel -d hello_hello_ssp_all.out > hello_hello_ssp_all.s
subé¢æ°ã® éã¢ã»ã³ãã©ã表示ãã¾ãã1164 ã®è¡ã§ãä¹±æ°ã®å¤ï¼FSã»ã°ã¡ã³ãã®ãªãã»ãã 0x28ï¼ã RAX ã«è¨å®ãã¦ãã¦ã116d ã®è¡ã§ãã¹ã¿ãã¯ã«æ ¼ç´ãã¦ãã¾ããsubé¢æ°ã®çµäºç´åã® 11aa ã®è¡ã§ãã¹ã¿ãã¯ããæ ¼ç´ãã¦ãããå¤ã RDX ã«åãåºãã11ae ã®è¡ã§ãä¹±æ°ã®å¤ã¨ RDX ãå¼ãç®ãã¦ã11ae ã®è¡ã§ãã¤ã³ã¼ã«ï¼ã¹ã¿ãã¯ã«ããªã¢ã«å¤åããªãï¼ãªã leaveå½ä»¤ã«ã¸ã£ã³ãããã¹ã¿ãã¯ã«ããªã¢ã«å¤åããã£ãã __stack_chk_fail@plt
ãå¼ã³åºããããã°ã©ã ãçµäºããã¾ãã
FSã»ã°ã¡ã³ãã¨ã¯ãx86 ã®ããã»ããµãæã¤ã»ã°ã¡ã³ãã¬ã¸ã¹ã¿ã§ç®¡çãããã»ã°ã¡ã³ãï¼ã¡ã¢ãªï¼ã®ãã¨ã ããã§ãã
0000000000001159 <sub>:
1159: 55 push rbp
115a: 48 89 e5 mov rbp,rsp
115d: 48 83 ec 20 sub rsp,0x20
1161: 89 7d ec mov DWORD PTR [rbp-0x14],edi
1164: 64 48 8b 04 25 28 00 mov rax,QWORD PTR fs:0x28
116b: 00 00
116d: 48 89 45 f8 mov QWORD PTR [rbp-0x8],rax
1171: 31 c0 xor eax,eax
1173: 48 8d 05 8a 0e 00 00 lea rax,[rip+0xe8a]
117a: 48 89 c7 mov rdi,rax
117d: b8 00 00 00 00 mov eax,0x0
1182: e8 b9 fe ff ff call 1040 <printf@plt>
1187: 48 8d 45 f4 lea rax,[rbp-0xc]
118b: 48 89 c6 mov rsi,rax
118e: 48 8d 05 7d 0e 00 00 lea rax,[rip+0xe7d]
1195: 48 89 c7 mov rdi,rax
1198: b8 00 00 00 00 mov eax,0x0
119d: e8 ae fe ff ff call 1050 <__isoc99_scanf@plt>
11a2: 8b 55 f4 mov edx,DWORD PTR [rbp-0xc]
11a5: 8b 45 ec mov eax,DWORD PTR [rbp-0x14]
11a8: 01 d0 add eax,edx
11aa: 48 8b 55 f8 mov rdx,QWORD PTR [rbp-0x8]
11ae: 64 48 2b 14 25 28 00 sub rdx,QWORD PTR fs:0x28
11b5: 00 00
11b7: 74 05 je 11be <sub+0x65>
11b9: e8 72 fe ff ff call 1030 <__stack_chk_fail@plt>
11be: c9 leave
11bf: c3 ret
ã¹ã¿ãã¯ã«ããªã¢ï¼SSPï¼ã«ã¤ãã¦ã¯ä»¥ä¸ã§ãã
NXã«ã¤ãã¦å
·ä½çã«ç¢ºèªãã
NXï¼No eXecuteï¼ã¯ãã¡ã¢ãªé åã«ç½®ãããã³ã¼ããå®è¡ã§ããªããã¾ããWindowsã§ã¯ DEPï¼Data Execution Preventionï¼ã¨å¼ã°ããããã§ããããã©ã«ãã§æå¹ã«ãªã£ã¦ãã¾ãããç¡å¹ã«ããã«ã¯ãã³ã³ãã¤ã«ãªãã·ã§ã³ -z execstack
ãä»ããã¨åºæ¥ãããã§ãããã£ã¦ã¿ãã¨ãããNX disabled ã«å¤åãã¾ããã
$ gcc -g -z execstack -Wl,-Map=hello_hello_nx.map -o hello_hello_nx.out hello_hello.c
$ ../../tools/checksec.sh-2.7.1/checksec --file=./hello_hello_nx.out
RELRO STACK CANARY NX PIE RPATH RUNPATH Symbols FORTIFY Fortified Fortifiable FILE
Partial RELRO No canary found NX disabled PIE enabled No RPATH No RUNPATH 39 Symbols No 0 1 ./hello_hello_nx.out
NX disabled ã«ãªã£ãããã°ã©ã ã GDB ã§ç¢ºèªãã¾ããæå¾ã®è¡ã® [stack]
ã«æ³¨ç®ãã¾ããrwxp
ã¨å®è¡å¯è½ã«ãªã£ã¦ãã¾ããã¡ãªã¿ã«ãNX enabled ã®é常ã®ããã°ã©ã ã¯ããã®è¨äºã®åé ã®çµæãè¦ã¦ã¿ãã¨ã0x7ffffffde000 0x7ffffffff000 0x21000 0x0 rw-p [stack]
ã¨ãªã£ã¦ãããå®è¡ã¯ã§ãã¾ããã
$ gdb -q hello_hello_nx.out
gdb-peda$ i proc map
process 398709
Mapped address spaces:
Start Addr End Addr Size Offset Perms objfile
0x555555554000 0x555555555000 0x1000 0x0 r--p /home/user/svn/experiment/c/hello_hello_nx.out
0x555555555000 0x555555556000 0x1000 0x1000 r-xp /home/user/svn/experiment/c/hello_hello_nx.out
0x555555556000 0x555555557000 0x1000 0x2000 r--p /home/user/svn/experiment/c/hello_hello_nx.out
0x555555557000 0x555555558000 0x1000 0x2000 r--p /home/user/svn/experiment/c/hello_hello_nx.out
0x555555558000 0x555555559000 0x1000 0x3000 rw-p /home/user/svn/experiment/c/hello_hello_nx.out
0x7ffff7dc7000 0x7ffff7dca000 0x3000 0x0 rw-p
0x7ffff7dca000 0x7ffff7df0000 0x26000 0x0 r--p /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7df0000 0x7ffff7f45000 0x155000 0x26000 r-xp /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7f45000 0x7ffff7f98000 0x53000 0x17b000 r--p /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7f98000 0x7ffff7f9c000 0x4000 0x1ce000 r--p /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7f9c000 0x7ffff7f9e000 0x2000 0x1d2000 rw-p /usr/lib/x86_64-linux-gnu/libc.so.6
0x7ffff7f9e000 0x7ffff7fab000 0xd000 0x0 rw-p
0x7ffff7fc3000 0x7ffff7fc5000 0x2000 0x0 rw-p
0x7ffff7fc5000 0x7ffff7fc9000 0x4000 0x0 r--p [vvar]
0x7ffff7fc9000 0x7ffff7fcb000 0x2000 0x0 r-xp [vdso]
0x7ffff7fcb000 0x7ffff7fcc000 0x1000 0x0 r--p /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
0x7ffff7fcc000 0x7ffff7ff1000 0x25000 0x1000 r-xp /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
0x7ffff7ff1000 0x7ffff7ffb000 0xa000 0x26000 r--p /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
0x7ffff7ffb000 0x7ffff7ffd000 0x2000 0x30000 r--p /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
0x7ffff7ffd000 0x7ffff7fff000 0x2000 0x32000 rw-p /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
0x7ffffffde000 0x7ffffffff000 0x21000 0x0 rwxp [stack]
NX ã«ã¤ãã¦ã¯ä»¥ä¸ã§ãã
PIEã¨ASLRã«ã¤ãã¦å
·ä½çã«ç¢ºèªãã
ç¾å¨ã¯ãæ®éã«ãã«ãããã¨ãPIE ãæå¹ã«ãªã£ã¦ãã¦ãå®è¡ããããã°ã©ã ã®é
ç½®ã¢ãã¬ã¹ã¯å¤åãã¾ããASLR ã¯ãLinux ãªãé常㯠2 ã«ãªã£ã¦ãããã§ãä¸ã§è¨ã£ãéããå
±æã©ã¤ãã©ãªããã¼ããã¹ã¿ãã¯ã®é
ç½®ã¢ãã¬ã¹ãã©ã³ãã åãã¾ãã
å
·ä½çã«ç¢ºèªãã¦ã¿ã¾ããå®è¡ããããã°ã©ã ãã¹ã¿ãã¯ããã¼ããå
±æã©ã¤ãã©ãªã®ã¢ãã¬ã¹ã®å
¨ã¦ãã確ãã«å¤åãã¦ãã¾ãã
$ ./hello_hello.out
main: 0x558b2bd4018d
buf: 0x7ffdfa3c3df0
mbuf: 0x558b2cd972a0
malloc: 0x7f5973187860
input data: 1
input data2: 2
result: 3
$ ./hello_hello.out
main: 0x563e7e40d18d
buf: 0x7ffc769104a0
mbuf: 0x563e7fd652a0
malloc: 0x7efd5c029860
input data: 2
input data2: 3
result: 5
ASLR ãä¸æçã« 1 ã«å¤æ´ãã¦ã¿ã¾ãã1 ã§ããå
¨ã¦å¤åãã¦ãã¾ãã
$ sudo su
2
kernel.randomize_va_space = 1
1
$ ./hello_hello.out
main: 0x55bf00e6518d
buf: 0x7fffbf786810
mbuf: 0x55bf00e692a0
malloc: 0x7f386ad62860
input data: 1
input data2: 2
result: 3
$ ./hello_hello.out
main: 0x55c26238418d
buf: 0x7fffcde31f50
mbuf: 0x55c2623882a0
malloc: 0x7f3cd7205860
input data: 1
input data2: 3
result: 4
ã§ã¯ã0 ã«ãã¦ãåãããã«ç¢ºèªãã¦ã¿ã¾ããå
¨ã¦ä¸è´ãã¾ãããå®è¡ããã°ã©ã ã¯å¤åããããªï¼ã¨æãã¾ããããä¸è´ãã¾ããã
$ sudo su
kernel.randomize_va_space = 0
0
$ ./hello_hello.out
main: 0x55555555518d
buf: 0x7fffffffdf30
mbuf: 0x5555555592a0
malloc: 0x7ffff7e62860
input data: 1
input data2: 2
result: 3
$ ./hello_hello.out
main: 0x55555555518d
buf: 0x7fffffffdf30
mbuf: 0x5555555592a0
malloc: 0x7ffff7e62860
input data: 3
input data2: 4
result: 7
ASLR ã¯ãå
ã® 2 ã«æ»ãã¦ããã¾ãã
$ sudo su
kernel.randomize_va_space = 2
2
ä¸æ¹ã§ãPIE ãç¡å¹ã«ããå ´åã確èªãã¦ã¿ã¾ãã-no-pie
ãä»ããã¨ãPIE ãç¡å¹ã«ã§ãã¾ãã
checksec ãå®è¡ããã¨ãNo PIE ã¨ãããã¨ã§ãPIE ãç¡å¹åã§ãã¦ãã¾ããå®è¡ãã¦ã¿ãã¨ãä¸ã¨æ¯ã¹ã¦ãããã¶ãå°ããã¢ãã¬ã¹ã«ãªãã¾ãããå®è¡ããããã°ã©ã ã¯åãã¢ãã¬ã¹ã«é
ç½®ããã¦ãã¾ããããã以å¤ã®é åã¯å¤åãã¦ãã¾ãã
ASLR ã 0 ã«ããã¨ãã¯ã-no-pie
ãä»ããªãã¦ããå®è¡ããããã°ã©ã ãåãã¢ãã¬ã¹ã«é
ç½®ããã¾ãããã¢ãã¬ã¹ãè¦ãã¨ãåãã¢ãã¬ã¹ã§ããã大ããªã¢ãã¬ã¹ã«é
ç½®ããã¦ãã¾ããã®ã§ãASLR ã®å¹æã¯åºã¦ãããã¨ãåããã¾ããPIE 㯠ASLR ã 0 ã§ã¯ãªããã¨ãåæã®ã»ãã¥ãªãã£æ©æ§ãªã®ããããã¾ããã
$ gcc -g -no-pie -Wl,-Map=hello_hello_nopie.map -o hello_hello_nopie.out hello_hello.c
$ ../../tools/checksec.sh-2.7.1/checksec --file=./hello_hello_nopie.out
RELRO STACK CANARY NX PIE RPATH RUNPATH Symbols FORTIFY Fortified Fortifiable FILE
Partial RELRO No canary found NX enabled No PIE No RPATH No RUNPATH 37 Symbols No 0 1 ./hello_hello_nopie.out
$ ./hello_hello_nopie.out
main: 0x40118a
buf: 0x7ffeb1e5e4d0
mbuf: 0x5fd2a0
malloc: 0x7fad9d01d860
input data: 1
input data2: 2
result: 3
$ ./hello_hello_nopie.out
main: 0x40118a
buf: 0x7ffd6570c2d0
mbuf: 0x21102a0
malloc: 0x7fb018f7a860
input data: 2
input data2: 3
result: 5
ãããã«
ä»åã¯ãchecksec ã®çµæãç解ããããã«ããããã調ã¹ãããå®éã« GDB ã§å®è¡ãã¦ç¢ºèªããã¾ãããã ãã¶ç解ãé²ãã æ°ããã¾ãã
checksec ã対å¿ãã¦ããªãã»ãã¥ãªãã£æ©æ§ããããããã®ã§ãã¾ãåãã£ãããã®è¨äºã«è¿½è¨ãããã¨æãã¾ãã
ä»åã¯ãChatGPT ã«ãã´ãä½ã£ã¦ãããã¾ããã1æ¥2åã¾ã§ç¡æã¢ã«ã¦ã³ãã§ãä½ã£ã¦ããããããã§ããä»åã¯ãéãã1åã§ããæãã®ç»åãçæãã¦ããã¾ããã
ãé¡ãããå
容ã¯ãchecksec ã¨ãããã¼ã«ã®ãã´ï¼PNGç»åï¼ã 200x200 ã®ãµã¤ãºã§ä½ã£ã¦ãã ãããã§ãï¼ç¬ï¼ãPNGç»åã¨ãé¡ããã¾ããããwebpã¨ããæ¡å¼µåã®ç»åãã¡ã¤ã«ãä½ããã¾ããããªã³ã©ã¤ã³ã®ãã¼ã«ã§PNGç»åã«ç°¡åã«å¤æã§ãã¾ããã
æå¾ã«ãªãã¾ããããã¨ã³ã¸ãã¢ã°ã«ã¼ãã®ã©ã³ãã³ã°ã«åå ä¸ã§ãã
æ°æ¥½ã«ãããã¨ãããããé¡ããããã¾ãð
ä»åã¯ä»¥ä¸ã§ãï¼
æå¾ã¾ã§ãèªã¿ããã ãããããã¨ããããã¾ããã