İçeriğe geç

GDB ve Normal Olmayan Program Akışı

Giriş

Çok öncesinde sunumlar hazırlayıp öğrencilere kullanımından bahsetmiştim gerçi o zamanalar bende öğrenciydim ancak kullanmadıkça unuttuğumu farkettim ve tekrardan bir göz attım. Göz atarkende buraya yazayim dedim ve yeni bir seri böylece doğmuş oldu. Yine tekrar tekrar söylüyorum ki bir müfredata bağlı kalarak yazmıyorum daha çok kendi hoşuma giden kısımları kaleme alıyorum boşuna "bireysel çöplüğüm" demiyorum buraya.

Başlangıç

O zaman yavaş yavaş başlayalım. İlk durağımız symbol file olacaktır. Bunun ne olduğu hakkında konuşacağız ama öncesinde basit bir hello world yazalım. Ve gdb'den bahsedelim.

┌──(root💀kali)-[~/oscp/gdb]
└─# cat hello.c 
#include <stdio.h>
int main() {
   // printf() displays the string inside quotation
   printf("Hello, World!");
   return 0;
}

Şimdi programı derleyelim ve çalıştıralım.

 hello.c -o hello

┌──(root💀kali)-[~/oscp/gdb]
└─# ./hello
Hello, World!          

Harika, şimdi gelelim asıl sorularımıza gdb nedir. Hello World gibi basit programlardan karmaşık programlara kadar birçok programı analiz edebileceğiniz, çalışma zamanında debuglayabileceğiniz bir program arıyorsanız gdb ile tanışın. Kısaca gdb'yi debuger aracımız olarak özetleyebiliriz.
Peki symbol files nedir? Bu bir tersine mühendislik serisi olmayacak ama buradaki bilgileri tersine mühendislikte kullanırız. Amacımız tersine mühendislik yaparken aslında birçok gizem çözmektir. Makine yada byte kodlardan bir şeyler elde etmeye çalışırız, elimizde symbol dosyaları olsaydı iş biraz değişebilirdi. Symbol dosyaları ile debuglamak odukça kolaydır çünkü bu dosyalar değişkenler, fonksyionlar gibi bir çok anlamlı bilgiyi barındırır. Örnek olarak hello uygulamasını debuglamayı deneyelim. gdb bize bir uyarı verecektir.

┌──(root💀kali)-[~/oscp/gdb]
└─# gdb ./hello         
GNU gdb (Debian 10.1-1.7) 10.1.90.20210103-git
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./hello...
(No debugging symbols found in ./hello)
(gdb) 

(No debugging symbols found in ./hello) eskiden derleyiciler şuanda olduğu gibi değildi, yazılımcılar debuglamak için ekstra araçlar kullanırlardı örnek gdb gibi. Daha anlamlı debug'lamak içn symbol dosyalarına ihtiyaç duyarız. Peki bu dosyaları nasıl elde edeceğiz. Aslında derleme aşamasında bir parametre ile bunu elde edebiliriz.


┌──(root💀kali)-[~/oscp/gdb]
└─# gcc hello.c -o hello2 -ggdb     

┌──(root💀kali)-[~/oscp/gdb]
└─# gdb ./hello2               
GNU gdb (Debian 10.1-1.7) 10.1.90.20210103-git
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./hello2...
(gdb) 

Harika! Reading symbols from ./hello2... Tamam symbol dosyalarını analdık. Debuglamada yardımcıymış. Şimdi teorik bilgiyi bir kenara bırakıp gerçekten uygulama üzerinde görelim. Symbol files'ın olması ya da olmaması nelere sebep oluyormuş?

Şimdi yeni bir dosya oluşturalım ve normal şartlarda çalıştıramadığımız bir fonksiyon tanımlayalım. Biraz bu dosyayla oynayacağız.

┌──(root💀kali)-[~/oscp/gdb]
└─# cat test.c        
#include <stdio.h>
int main() {
   // printf() displays the string inside quotation
   printf("Hello, World!");
   return 0;
}
void test(){
  printf("Hack!");
}

┌──(root💀kali)-[~/oscp/gdb]
└─# gcc test.c -o test

┌──(root💀kali)-[~/oscp/gdb]
└─# gcc test.c -o test2 -ggdb                                                                                    1 ⨯

Elimizde iki tane binary var. Bunları sırası ile gdb aracılığıyla açalım ve fonksiyonlar hakkında bilgi almayı deneyelim.

Symbol dosyaları yokken aşağıdaki gibi bir çıktı alırız.

┌──(root💀kali)-[~/oscp/gdb]
└─# gdb ./test               
GNU gdb (Debian 10.1-1.7) 10.1.90.20210103-git
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./test...
(No debugging symbols found in ./test)
(gdb) info functions
All defined functions:

Non-debugging symbols:
0x0000000000001000  _init
0x0000000000001030  printf@plt
0x0000000000001040  __cxa_finalize@plt
0x0000000000001050  _start
0x0000000000001080  deregister_tm_clones
0x00000000000010b0  register_tm_clones
0x00000000000010f0  __do_global_dtors_aux
0x0000000000001130  frame_dummy
0x0000000000001135  main
0x0000000000001151  test
0x0000000000001170  __libc_csu_init
0x00000000000011d0  __libc_csu_fini
0x00000000000011d4  _fini
(gdb) 

Symbol dosyaları varken ise aşağıdaki gibi bir çıktı alırız.

┌──(root💀kali)-[~/oscp/gdb]┌──(root💀kali)-[~/oscp/gdb]
└─# gdb ./test2
GNU gdb (Debian 10.1-1.7) 10.1.90.20210103-git
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./test2...
(gdb) info functions
All defined functions:

File test.c:
2:  int main();
7:  void test();

Non-debugging symbols:
0x0000000000001000  _init
0x0000000000001030  printf@plt
0x0000000000001040  __cxa_finalize@plt
0x0000000000001050  _start
0x0000000000001080  deregister_tm_clones
0x00000000000010b0  register_tm_clones
0x00000000000010f0  __do_global_dtors_aux
0x0000000000001130  frame_dummy
0x0000000000001170  __libc_csu_init
0x00000000000011d0  __libc_csu_fini
0x00000000000011d4  _fini
(gdb) 

Kaynak kodu görmek istediğimizde list anahtar kelimesini kullanabiliriz. Symbol dosyaları olmadığında aşağıdaki gibi bir çıktı alırız.

(gdb) list 1
No symbol table is loaded.  Use the "file" command.
(gdb) 

Symbol dosyaları olduğunda aşağıdaki gibi bir çıktı alırız.

(gdb) list 1
1   #include <stdio.h>
2   int main() {
3      // printf() displays the string inside quotation
4      printf("Hello, World!");
5      return 0;
6   }
7   void test(){
8     printf("Hack!");
9   }
(gdb) 

Şimdi çalışır durumdayken analiz yapmaya başlayalım. Bunun için programı ilk başta çalıştırmamız gerekecektir ve analizlere symbol dosyası olmayan binary üzernden devam edeceğim.

İsterseni yeni bir kod yazalım ve aşağıdaki projeyi derleyip analiz edelim.

┌──(root💀kali)-[~/oscp/gdb]
└─# cat ornek.c
#include <stdio.h>

void aslaCalismam(){
  printf("Tebrikler!, beni çalıştırabildin.");
}

void calis(){
  printf("Zaten çalışmam gerekiyordu");
}
int main() {
   printf("Deneme Amaçlıdır\n");
   calis();
   return 0;
}

┌──(root💀kali)-[~/oscp/gdb]
└─# gcc ornek.c -o ornek

┌──(root💀kali)-[~/oscp/gdb]
└─# ./ornek             
Deneme Amaçlıdır
Zaten çalışmam gerekiyordu  

Şimdi programı çalıştıralım.

┌──(root💀kali)-[~/oscp/gdb]
└─# gdb ornek 
GNU gdb (Debian 10.1-1.7) 10.1.90.20210103-git
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ornek...
(No debugging symbols found in ornek)
(gdb) info functions
All defined functions:

Non-debugging symbols:
0x0000000000001000  _init
0x0000000000001030  puts@plt
0x0000000000001040  printf@plt
0x0000000000001050  __cxa_finalize@plt
0x0000000000001060  _start
0x0000000000001090  deregister_tm_clones
0x00000000000010c0  register_tm_clones
0x0000000000001100  __do_global_dtors_aux
0x0000000000001140  frame_dummy
0x0000000000001145  aslaCalismam
0x000000000000115d  calis
0x0000000000001175  main
0x00000000000011a0  __libc_csu_init
0x0000000000001200  __libc_csu_fini
0x0000000000001204  _fini
(gdb) run
Starting program: /root/oscp/gdb/ornek 
Deneme Amaçlıdır
Zaten çalışmam gerekiyordu[Inferior 1 (process 5093) exited normally]

Görüldüğü üzere programı normal bir şekilde çalıştıeravildik ancak kodu dikkatli okuduysanız aslaCalismam() isimli bir fonksiyon bulunmaktadır. Bu fonksyionu çalıştırmak istiyoruz. Bunu nasıl yapabiliriz?

Program çalışırken main fonksyionuna break point koyarak başlayalım. Daha sonrasında o esnadaki register'ları kontrol edelim.

The program has no registers now.
(gdb) break main
Breakpoint 1 at 0x555555555179
(gdb) run
Starting program: /root/oscp/gdb/ornek 

Breakpoint 1, 0x0000555555555179 in main ()
(gdb) info registers
rax            0x555555555175      93824992235893
rbx            0x0                 0
rcx            0x7ffff7fae718      140737353803544
rdx            0x7fffffffe448      140737488348232
rsi            0x7fffffffe438      140737488348216
rdi            0x1                 1
rbp            0x7fffffffe340      0x7fffffffe340
rsp            0x7fffffffe340      0x7fffffffe340
r8             0x0                 0
r9             0x7ffff7fe21b0      140737354015152
r10            0x1                 1
r11            0x0                 0
r12            0x555555555060      93824992235616
r13            0x0                 0
r14            0x0                 0
r15            0x0                 0
rip            0x555555555179      0x555555555179 <main+4>
eflags         0x246               [ PF ZF IF ]
cs             0x33                51
ss             0x2b                43
ds             0x0                 0
es             0x0                 0
fs             0x0                 0
gs             0x0                 0

Aslında hiçbir hata yok ancaj biz şuanda x86 mimarisine göre ilerliyoruz. Bu yüzden benimle aynı çıktıyı alıyorsanız programı x64'e göre derlemişsiniz demektir. x86'ya göre tekrar derleyip devam edelim.

64bir bir makinede 32 bit derleme yapabilmek için gcc'nin multilib'ine ihtiyacımız olacaktır.


┌──(root💀kali)-[~/oscp/gdb]
└─# gcc ornek.c -o ornek -m32
In file included from ornek.c:1:
/usr/include/stdio.h:27:10: fatal error: bits/libc-header-start.h: No such file or directory
   27 | #include <bits/libc-header-start.h>
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

┌──(root💀kali)-[~/oscp/gdb]
└─# apt-get install gcc-multilib -y                                                                                                                                                                                                      1 ⨯
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  gcc-10-multilib lib32asan6 lib32atomic1 lib32gcc-10-dev lib32gomp1 lib32itm1 lib32quadmath0 lib32ubsan1 libc6-dev-i386 libc6-dev-x32 libc6-x32 libx32asan6 libx32atomic1 libx32gcc-10-dev libx32gcc-s1 libx32gomp1 libx32itm1
  libx32quadmath0 libx32stdc++6 libx32ubsan1
The following NEW packages will be installed:
  gcc-10-multilib gcc-multilib lib32asan6 lib32atomic1 lib32gcc-10-dev lib32gomp1 lib32itm1 lib32quadmath0 lib32ubsan1 libc6-dev-i386 libc6-dev-x32 libc6-x32 libx32asan6 libx32atomic1 libx32gcc-10-dev libx32gcc-s1 libx32gomp1
  libx32itm1 libx32quadmath0 libx32stdc++6 libx32ubsan1
0 upgraded, 21 newly installed, 0 to remove and 208 not upgraded.
Need to get 17.1 MB of archives.
After this operation, 69.3 MB of additional disk space will be used.
...

┌──(root💀kali)-[~/oscp/gdb]
└─# gcc ornek.c -o ornek -m32      

Evet harika. Başarılı bir şekilde 32bit'e uyun derleme yaptık.

Şimdi main fonksiyonuna break point atalım ve o esnada register'ları kontrol edelim.

ou are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./ornek...
(No debugging symbols found in ./ornek)
(gdb) info functions
All defined functions:

Non-debugging symbols:
0x00001000  _init
0x00001030  printf@plt
0x00001040  puts@plt
0x00001050  __libc_start_main@plt
0x00001060  __cxa_finalize@plt
0x00001070  _start
0x000010b0  __x86.get_pc_thunk.bx
0x000010c0  deregister_tm_clones
0x00001100  register_tm_clones
0x00001150  __do_global_dtors_aux
0x000011a0  frame_dummy
0x000011a5  __x86.get_pc_thunk.dx
0x000011a9  aslaCalismam
0x000011d4  calis
0x000011ff  main
0x00001240  __x86.get_pc_thunk.ax
0x00001250  __libc_csu_init
0x000012b0  __libc_csu_fini
0x000012b1  __x86.get_pc_thunk.bp
0x000012b8  _fini
(gdb) break main
Breakpoint 1 at 0x120e
(gdb) run
Starting program: /root/oscp/gdb/ornek 

Breakpoint 1, 0x5655620e in main ()
(gdb) info registers
eax            0xf7fb2ae8          -134534424
ecx            0xffffd4c0          -11072
edx            0xffffd4f4          -11020
ebx            0x0                 0
esp            0xffffd4a0          0xffffd4a0
ebp            0xffffd4a8          0xffffd4a8
esi            0xf7fb0000          -134545408
edi            0xf7fb0000          -134545408
eip            0x5655620e          0x5655620e <main+15>
eflags         0x282               [ SF IF ]
cs             0x23                35
ss             0x2b                43
ds             0x2b                43
es             0x2b                43
fs             0x0                 0
gs             0x63                99

Register'ları düzgün bir şekilde görüntüleyebildik. Şidmi main fonksiyonun içine girelim.

(gdb) disassemble main
Dump of assembler code for function main:
   0x565561ff <+0>:   lea    0x4(%esp),%ecx
   0x56556203 <+4>:   and    $0xfffffff0,%esp
   0x56556206 <+7>:   push   -0x4(%ecx)
   0x56556209 <+10>:  push   %ebp
   0x5655620a <+11>:  mov    %esp,%ebp
   0x5655620c <+13>:  push   %ebx
   0x5655620d <+14>:  push   %ecx
=> 0x5655620e <+15>:   call   0x56556240 <__x86.get_pc_thunk.ax>
   0x56556213 <+20>:  add    $0x2ded,%eax
   0x56556218 <+25>:  sub    $0xc,%esp
   0x5655621b <+28>:  lea    -0x1fb4(%eax),%edx
   0x56556221 <+34>:  push   %edx
   0x56556222 <+35>:  mov    %eax,%ebx
   0x56556224 <+37>:  call   0x56556040 <puts@plt>
   0x56556229 <+42>:  add    $0x10,%esp
   0x5655622c <+45>:  call   0x565561d4 <calis>
   0x56556231 <+50>:  mov    $0x0,%eax
   0x56556236 <+55>:  lea    -0x8(%ebp),%esp
   0x56556239 <+58>:  pop    %ecx
   0x5655623a <+59>:  pop    %ebx
   0x5655623b <+60>:  pop    %ebp
   0x5655623c <+61>:  lea    -0x4(%ecx),%esp
   0x5655623f <+64>:  ret    
End of assembler dump.
(gdb) 

Break pointimizi 0x5655620e adresinde görüyoruz. Ancak intel syntax'ına uygun bir şekilde dump almadık. Bunu düzeltip tekrar okuma işlemi yapalım.

(gdb) set disassembly-flavor intel
(gdb) disassemble main
Dump of assembler code for function main:
   0x565561ff <+0>:   lea    ecx,[esp+0x4]
   0x56556203 <+4>:   and    esp,0xfffffff0
   0x56556206 <+7>:   push   DWORD PTR [ecx-0x4]
   0x56556209 <+10>:  push   ebp
   0x5655620a <+11>:  mov    ebp,esp
   0x5655620c <+13>:  push   ebx
   0x5655620d <+14>:  push   ecx
=> 0x5655620e <+15>:   call   0x56556240 <__x86.get_pc_thunk.ax>
   0x56556213 <+20>:  add    eax,0x2ded
   0x56556218 <+25>:  sub    esp,0xc
   0x5655621b <+28>:  lea    edx,[eax-0x1fb4]
   0x56556221 <+34>:  push   edx
   0x56556222 <+35>:  mov    ebx,eax
   0x56556224 <+37>:  call   0x56556040 <puts@plt>
   0x56556229 <+42>:  add    esp,0x10
   0x5655622c <+45>:  call   0x565561d4 <calis>
   0x56556231 <+50>:  mov    eax,0x0
   0x56556236 <+55>:  lea    esp,[ebp-0x8]
   0x56556239 <+58>:  pop    ecx
   0x5655623a <+59>:  pop    ebx
   0x5655623b <+60>:  pop    ebp
   0x5655623c <+61>:  lea    esp,[ecx-0x4]
   0x5655623f <+64>:  ret    
End of assembler dump.

0x5655622c adresinde görüldüğü üzere calis simli fonksyion çağrılmaktadır. Bu esnada return adres olarak 0x56556231 adresi stack alanına eklenecektir.

Şimdi kodlarımızı ilerletelim ve görelim. Kısa bir bilgi aşağıda verilmiştir.

step => adres adres ilerleme
stepi => adres adres ilerleme ama fonksyionların içine girilmez direkt geçilir.
continue => sonraki break poin'e kadar gidilir

İlk olarak calis kısmınada bir break point koyalım. calis fonksiyonuna girdiğimizde 0x56556231 adresinin stack alanına geldiğini görebilmekteyiz.


┌──(root💀kali)-[~/oscp/gdb]
└─# gdb ./ornek
GNU gdb (Debian 10.1-1.7) 10.1.90.20210103-git
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./ornek...
(No debugging symbols found in ./ornek)
(gdb) break main
Breakpoint 1 at 0x120e
(gdb) disassemble main
Dump of assembler code for function main:
   0x000011ff <+0>:   lea    0x4(%esp),%ecx
   0x00001203 <+4>:   and    $0xfffffff0,%esp
   0x00001206 <+7>:   push   -0x4(%ecx)
   0x00001209 <+10>:  push   %ebp
   0x0000120a <+11>:  mov    %esp,%ebp
   0x0000120c <+13>:  push   %ebx
   0x0000120d <+14>:  push   %ecx
   0x0000120e <+15>:  call   0x1240 <__x86.get_pc_thunk.ax>
   0x00001213 <+20>:  add    $0x2ded,%eax
   0x00001218 <+25>:  sub    $0xc,%esp
   0x0000121b <+28>:  lea    -0x1fb4(%eax),%edx
   0x00001221 <+34>:  push   %edx
   0x00001222 <+35>:  mov    %eax,%ebx
   0x00001224 <+37>:  call   0x1040 <puts@plt>
   0x00001229 <+42>:  add    $0x10,%esp
   0x0000122c <+45>:  call   0x11d4 <calis>
   0x00001231 <+50>:  mov    $0x0,%eax
   0x00001236 <+55>:  lea    -0x8(%ebp),%esp
   0x00001239 <+58>:  pop    %ecx
   0x0000123a <+59>:  pop    %ebx
   0x0000123b <+60>:  pop    %ebp
   0x0000123c <+61>:  lea    -0x4(%ecx),%esp
   0x0000123f <+64>:  ret    
End of assembler dump.
(gdb) break calis
Breakpoint 2 at 0x11d8
(gdb) run
Starting program: /root/oscp/gdb/ornek 

Breakpoint 1, 0x5655620e in main ()
(gdb) stepi
0x56556240 in __x86.get_pc_thunk.ax ()
(gdb) disassemble main
Dump of assembler code for function main:
   0x565561ff <+0>:   lea    0x4(%esp),%ecx
   0x56556203 <+4>:   and    $0xfffffff0,%esp
   0x56556206 <+7>:   push   -0x4(%ecx)
   0x56556209 <+10>:  push   %ebp
   0x5655620a <+11>:  mov    %esp,%ebp
   0x5655620c <+13>:  push   %ebx
   0x5655620d <+14>:  push   %ecx
   0x5655620e <+15>:  call   0x56556240 <__x86.get_pc_thunk.ax>
   0x56556213 <+20>:  add    $0x2ded,%eax
   0x56556218 <+25>:  sub    $0xc,%esp
   0x5655621b <+28>:  lea    -0x1fb4(%eax),%edx
   0x56556221 <+34>:  push   %edx
   0x56556222 <+35>:  mov    %eax,%ebx
   0x56556224 <+37>:  call   0x56556040 <puts@plt>
   0x56556229 <+42>:  add    $0x10,%esp
   0x5655622c <+45>:  call   0x565561d4 <calis>
   0x56556231 <+50>:  mov    $0x0,%eax
   0x56556236 <+55>:  lea    -0x8(%ebp),%esp
   0x56556239 <+58>:  pop    %ecx
   0x5655623a <+59>:  pop    %ebx
   0x5655623b <+60>:  pop    %ebp
   0x5655623c <+61>:  lea    -0x4(%ecx),%esp
   0x5655623f <+64>:  ret    
End of assembler dump.
(gdb) continue
Continuing.
Deneme Amaçlıdır

Breakpoint 2, 0x565561d8 in calis ()
(gdb) info frame
Stack level 0, frame at 0xffffd4a0:
 eip = 0x565561d8 in calis; saved eip = 0x56556231
 called by frame at 0xffffd4c0
 Arglist at 0xffffd498, args: 
 Locals at 0xffffd498, Previous frame's sp is 0xffffd4a0
 Saved registers:
  ebp at 0xffffd498, eip at 0xffffd49c
(gdb) x/30x 0xffffd498
0xffffd498: 0xffffd4a8  0x56556231  0xffffd4c0  0x00000000
0xffffd4a8: 0x00000000  0xf7de9e46  0xf7fb0000  0xf7fb0000
0xffffd4b8: 0x00000000  0xf7de9e46  0x00000001  0xffffd564
0xffffd4c8: 0xffffd56c  0xffffd4f4  0xffffd504  0xf7ffdb40
0xffffd4d8: 0xf7fca410  0xf7fb0000  0x00000001  0x00000000
0xffffd4e8: 0xffffd548  0x00000000  0xf7ffd000  0x00000000
0xffffd4f8: 0xf7fb0000  0xf7fb0000  0x00000000  0x165acff0
0xffffd508: 0x54c8b1e0  0x00000000
(gdb) 

Bu kısım aslında main fonksiyonu içerisinde çağırılan calis fonksiyonu tamamlanıca main fonksyionunda devam edeceği kısmı belirtmektedir. Şimdi bu alanın üstüne yazalım.

0xffffd498: 0xffffd4a8  0x56556231  0xffffd4c0  0x00000000

Kısmına baktığımızda 0x56556231 kısmının 0xffffd49c alanında saklandığını görebiliyoruz.

Kısca baştan aşağıya aşağıdaki çıktıya bakın. Demek istediğimi anlayacaksınız RETR kısmında aslında stack'e ilk atılan adres en son olarak (lifo) dışarı çıkartılmaktadır.


┌──(root💀kali)-[~/oscp/gdb]
└─# gdb ./ornek              
GNU gdb (Debian 10.1-1.7) 10.1.90.20210103-git
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./ornek...
(No debugging symbols found in ./ornek)
(gdb) info functions
All defined functions:

Non-debugging symbols:
0x00001000  _init
0x00001030  printf@plt
0x00001040  puts@plt
0x00001050  __libc_start_main@plt
0x00001060  __cxa_finalize@plt
0x00001070  _start
0x000010b0  __x86.get_pc_thunk.bx
0x000010c0  deregister_tm_clones
0x00001100  register_tm_clones
0x00001150  __do_global_dtors_aux
0x000011a0  frame_dummy
0x000011a5  __x86.get_pc_thunk.dx
0x000011a9  aslaCalismam
0x000011d4  calis
0x000011ff  main
0x00001240  __x86.get_pc_thunk.ax
0x00001250  __libc_csu_init
0x000012b0  __libc_csu_fini
0x000012b1  __x86.get_pc_thunk.bp
0x000012b8  _fini
(gdb) break main
Breakpoint 1 at 0x120e
(gdb) run
Starting program: /root/oscp/gdb/ornek 

Breakpoint 1, 0x5655620e in main ()
(gdb) disassemble main
Dump of assembler code for function main:
   0x565561ff <+0>:   lea    0x4(%esp),%ecx
   0x56556203 <+4>:   and    $0xfffffff0,%esp
   0x56556206 <+7>:   push   -0x4(%ecx)
   0x56556209 <+10>:  push   %ebp
   0x5655620a <+11>:  mov    %esp,%ebp
   0x5655620c <+13>:  push   %ebx
   0x5655620d <+14>:  push   %ecx
=> 0x5655620e <+15>:   call   0x56556240 <__x86.get_pc_thunk.ax>
   0x56556213 <+20>:  add    $0x2ded,%eax
   0x56556218 <+25>:  sub    $0xc,%esp
   0x5655621b <+28>:  lea    -0x1fb4(%eax),%edx
   0x56556221 <+34>:  push   %edx
   0x56556222 <+35>:  mov    %eax,%ebx
   0x56556224 <+37>:  call   0x56556040 <puts@plt>
   0x56556229 <+42>:  add    $0x10,%esp
   0x5655622c <+45>:  call   0x565561d4 <calis>
   0x56556231 <+50>:  mov    $0x0,%eax
   0x56556236 <+55>:  lea    -0x8(%ebp),%esp
   0x56556239 <+58>:  pop    %ecx
   0x5655623a <+59>:  pop    %ebx
   0x5655623b <+60>:  pop    %ebp
   0x5655623c <+61>:  lea    -0x4(%ecx),%esp
   0x5655623f <+64>:  ret    
End of assembler dump.
(gdb) break calis
Breakpoint 2 at 0x565561d8
(gdb) continue
Continuing.
Deneme Amaçlıdır

Breakpoint 2, 0x565561d8 in calis ()
(gdb) disassemble calis
Dump of assembler code for function calis:
   0x565561d4 <+0>:   push   %ebp
   0x565561d5 <+1>:   mov    %esp,%ebp
   0x565561d7 <+3>:   push   %ebx
=> 0x565561d8 <+4>:    sub    $0x4,%esp
   0x565561db <+7>:   call   0x56556240 <__x86.get_pc_thunk.ax>
   0x565561e0 <+12>:  add    $0x2e20,%eax
   0x565561e5 <+17>:  sub    $0xc,%esp
   0x565561e8 <+20>:  lea    -0x1fd2(%eax),%edx
   0x565561ee <+26>:  push   %edx
   0x565561ef <+27>:  mov    %eax,%ebx
   0x565561f1 <+29>:  call   0x56556030 <printf@plt>
   0x565561f6 <+34>:  add    $0x10,%esp
   0x565561f9 <+37>:  nop
   0x565561fa <+38>:  mov    -0x4(%ebp),%ebx
   0x565561fd <+41>:  leave  
   0x565561fe <+42>:  ret    
End of assembler dump.
(gdb) break *0x565561fe
Breakpoint 3 at 0x565561fe
(gdb) continue
Continuing.

Breakpoint 3, 0x565561fe in calis ()
(gdb) x/x $esp
0xffffd49c: 0x56556231
(gdb) disassemble main
Dump of assembler code for function main:
   0x565561ff <+0>:   lea    0x4(%esp),%ecx
   0x56556203 <+4>:   and    $0xfffffff0,%esp
   0x56556206 <+7>:   push   -0x4(%ecx)
   0x56556209 <+10>:  push   %ebp
   0x5655620a <+11>:  mov    %esp,%ebp
   0x5655620c <+13>:  push   %ebx
   0x5655620d <+14>:  push   %ecx
   0x5655620e <+15>:  call   0x56556240 <__x86.get_pc_thunk.ax>
   0x56556213 <+20>:  add    $0x2ded,%eax
   0x56556218 <+25>:  sub    $0xc,%esp
   0x5655621b <+28>:  lea    -0x1fb4(%eax),%edx
   0x56556221 <+34>:  push   %edx
   0x56556222 <+35>:  mov    %eax,%ebx
   0x56556224 <+37>:  call   0x56556040 <puts@plt>
   0x56556229 <+42>:  add    $0x10,%esp
   0x5655622c <+45>:  call   0x565561d4 <calis>
   0x56556231 <+50>:  mov    $0x0,%eax
   0x56556236 <+55>:  lea    -0x8(%ebp),%esp
   0x56556239 <+58>:  pop    %ecx
   0x5655623a <+59>:  pop    %ebx
   0x5655623b <+60>:  pop    %ebp
   0x5655623c <+61>:  lea    -0x4(%ecx),%esp
   0x5655623f <+64>:  ret    
End of assembler dump.
(gdb) 

Bir sonraki yazımda bu adreslerle oynayarak aslında temel buffer overflowda yaptığımız eip gibi register'ların üstüne yazarak programın normal akışını nasıl değiştirebildiğimizden bahsedeceğim. Bir tane ubuntu 14 edinin...

Kategori:Buffer OverflowReverse Engineering

İlk Yorumu Siz Yapın

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir