Apuntes Cutter Radare2
Apuntes y toma de contacto con Cutter / radare2
Radare2 es una estupenda herramienta de desensamblado y analisis de aplicaciones.
Lo que se conoce como Reversing / Ingenieria inversa.
Cutter es un nuevo entorno grafico con Radare. Lo que lo hace mucho mas intuitivo
Nota: Gracias a un compañero S0ftD4t que conoci en el congreso Navaja Negra Conference 2019 que me ha picado en el mundo de la Ingenieria Inversa.
Recuerda: En la categoria Apuntes Voy a poner apubtes y notas de los proyectos que tengo en marcha.
No son articulos conpletos. Pero te pueden ayudar a resolver dudas y problemas y tener referencias.
Instalacion Cutter:
cutter https://cutter.re/ Ve a downloads y escoge la imagen que te propone: (La pagina detecta automaticamente tu sistema operativo y te propone la imagen adecuada)
- Radare2 (debian)
https://www.radare.org/n/
apt-get install radare2
Para actualizar a la ultima version:
Descargate e instala el paquete .deb de: https://radare.mikelloc.com/list
En mi caso: https://radare.mikelloc.com/get/4.0.0/radare2_4.0.0_amd64.deb
- Radare2 (debian)
https://www.radare.org/n/
###Compilacion hello word en C (linux) Manual instalacion Compilacion con GCC (GNU Compiler Collection) (Linux/windows) https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html
nano hello_word.c
// hello.c #include <stdio.h> int main() { printf("Hello, world!\n"); return 0; }
Compilo:
jejo@em50l:~/reversing/radare2$ gcc hello_word.c
**Ejecuto:**
jejo@em50l:~/reversing/radare2$ ./a.out Hello, world!
**Analisis del uso de la memoria:**
jejo@em50l:~/reversing/radare2$ size a.out text data bss dec hex filename 1516 600 8 2124 84c a.out jejo@em50l:~/reversing/radare2$
**Analisis de ejecucion con strace:** En sistemas strace/procmon a veces es muy util para encontrar problemas en la ejecucion del archivo.
jejo@em50l:~/reversing/radare2$ strace -a80 ./a.out execve("./a.out", ["./a.out"], 0x7ffc9f32f9a8 /* 50 vars */) = 0 openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3 read(3, "\177ELF\2\1\1\3\0\0\3\0>\0\1\0\0\0\260\34\2\0\0\0\0\0"..., 832) = 832 mmap(NULL, 118098, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7ff355c5b000 openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3 write(1, "Hello, world!\n", 14Hello, world!) = 14 exit_group(0) = ? +++ exited with 0 +++
__________
###Analisis estatico basico de hello_word con radare **En un solo comando:** teclear `radare2 -c "aaaa;afl;v" a.out`
jejo@medion:~/Escritorio/reversing/radare2$ radare2 -c "aaaa;afl;s main;v" a.out
Y aparecera el entorno guiado3:
File Settings Edit View Tools Search Emulate Debug Analyze Help Tab [1] [0x0000063a] ┌─────────────────────────────────────────────────────────────────────────┐-----------------------------------. │[X] Disassembly (pd) [Cache] Off │[X] Functions (afl) [Cache] Off | │ ┌ 23: int main (int argc, char **argv, char **envp); │ 0x00000530 1 42 entry| │ │ ; DATA XREF from entry0 @ 0x54d │ 0x00000560 4 50 -> 40 sym.d| │ │ 0x0000063a 55 push rbp │ 0x000005a0 4 66 -> 57 sym.r| │ │ 0x0000063b 4889e5 mov rbp, rsp │ 0x000005f0 5 58 -> 51 entry| │ │ 0x0000063e 488d3d9f0000. lea rdi, str.Hello__world │ 0x00000630 1 10 entry| │ │ 0x00000645 e8c6feffff call sym.imp.puts ;│ 0x000006d0 1 2 sym._| │ │ 0x0000064a b800000000 mov eax, 0 │ 0x000006d4 1 9 sym._| │ │ 0x0000064f 5d pop rbp │ 0x00000660 4 101 sym._| │ └ 0x00000650 c3 ret │ 0x0000063a 1 23 main | │ 0x00000651 662e0f1f8400. nop word cs:[rax + rax] │ 0x00000510 1 6 sym.i| │ 0x0000065b 0f1f440000 nop dword [rax + rax] │-----------------------------------. │ ┌ 101: sym.__libc_csu_init (int64_t arg1, int64_t arg2, int64_t arg3); │[X] Symbols (isq) [Cache] Off | │ │ ; arg int64_t arg1 @ rdi │ 0x00000238 0 .interp | │ │ ; arg int64_t arg2 @ rsi │ 0x00000254 0 .note.ABI-tag | │ │ ; arg int64_t arg3 @ rdx │ 0x00000274 0 .note.gnu.build-id | │ │ ; DATA XREF from entry0 @ 0x546 │ 0x00000298 0 .gnu.hash | │ │ 0x00000660 4157 push r15 │ 0x000002b8 0 .dynsym | │ │ 0x00000662 4156 push r14 │ 0x00000360 0 .dynstr | │ │ 0x00000664 4989d7 mov r15, rdx ;│ 0x000003e2 0 .gnu.version | │ │ 0x00000667 4155 push r13 │ 0x000003f0 0 .gnu.version_r | │ │ 0x00000669 4154 push r12 │ 0x00000410 0 .rela.dyn | └─────────────────────────────────────────────────────────────────────────┘-----------------------------------'
Menu View => Decompiler
Para los que no se manejan bien con los terminales de texto.
Es como una ventana de texto Pulsar con el raton sobre View y luego en Decompiler
Luego pico en 0x0000063a 1 23 main e intro en la parte/ventana que queramos ver completa.
Nota: las pestañas se pueden cerrar pulsando en [X].
File Settings Edit View Tools Search Emulate Debug Tab [1] [0x0000063a]
┌───────────────────────────────────────────────────────────────────────────────┐
│[X] Decompiler (pdc) [Cache] On │
│ function main () { │
│ // 1 basic blocks │
│ │
│ loc_0x63a: │
│ │
│ //DATA XREF from entry0 @ 0x54d │
│ push rbp │
│ rbp = rsp │
│ rdi = ["Hello, world!" //0x6e4 ; str.Hello__world] ; const char *s │
│ │
│ int puts("Hello, world!") │
│ eax = 0 │
│ //rsp ; rsp │
│ return │
│ (break) │
│ │
└───────────────────────────────────────────────────────────────────────────────┘
###Depuracion basica de hello_word con radare
En un solo comando:
teclear: radare2 -c "aaaa;afl;db main;dc;v" -d a.out
jejo@medion:~/reversing/radare2$ radare2 -c "aaaa;afl;db main;dc;v" -d a.out Process with PID 28979 started... = attach 28979 28979 bin.baddr 0x5556172ff000 Using 0x5556172ff000 asm.bits 64 [x] Analyze all flags starting with sym. and entry0 (aa) [Warning: Invalid range. Use different search.in=? or anal.in=dbg.maps.x Warning: Invalid range. Use different search.in=? or anal.in=dbg.maps.x [x] Analyze function calls (aac) [x] Analyze len bytes of instructions for references (aar)__________
Y aparecera el entorno guiado con el programa arrancado y un breakpoin en la funcion main:
File Settings Edit View Tools Search Emulate Debug Analyze Help Tab [1] [0x5556172ff63a]
┌────────────────────────────────────────────────────────────────────┐-----------------------------------.
│[X] Disassembly (pd) [Cache] Off │[X] Stack (pxq 256@r:[Cache] Off |
│ ;-- rax: │ 0x7ffc44f1de88 0x00007f8f8a37ab97|
│ ;-- rip: │ 0x7ffc44f1de98 0x00007ffc44f1df68|
│ ┌ 23: int main (int argc, char **argv, char **envp); │ 0x7ffc44f1dea8 0x00005556172ff63a|
│ │ ; DATA XREF from entry0 @ 0x5556172ff54d │ 0x7ffc44f1deb8 0xaf1a475dbc25b9c8|
│ │ 0x5556172ff63a b 55 push rbp │ 0x7ffc44f1dec8 0x00007ffc44f1df60|
│ │ 0x5556172ff63b 4889e5 mov rbp, rsp │ 0x7ffc44f1ded8 0x0000000000000000|
│ │ 0x5556172ff63e 488d3d9f0000. lea rdi, str.Hello__world│ 0x7ffc44f1dee8 0xfaa97d6d067bb9c8|
│ │ 0x5556172ff645 e8c6feffff call sym.imp.puts ;│ 0x7ffc44f1def8 0x0000000000000000|
│ │ 0x5556172ff64a b800000000 mov eax, 0 │ 0x7ffc44f1df08 0x00007f8f8a75a733|
│ │ 0x5556172ff64f 5d pop rbp │ 0x7ffc44f1df18 0x000000000005f5d2|
│ └ 0x5556172ff650 c3 ret │-----------------------------------.
│ 0x5556172ff651 662e0f1f8400. nop word cs:[rax + rax] │[X] Registers (dr) [Cache] Off |
│ 0x5556172ff65b 0f1f440000 nop dword [rax + rax] │ rax = 0x5556172ff63a |
│ ;-- rcx: │ rbx = 0x00000000 |
│ ;-- rbp: │ rcx = 0x5556172ff660 |
│ ┌ 101: sym.__libc_csu_init (int64_t arg1, int64_t arg2, int64_t arg3); │ rdx = 0x7ffc44f1df78 |
│ │ ; arg int64_t arg1 @ rdi │ r8 = 0x7f8f8a745d80 |
│ │ ; arg int64_t arg2 @ rsi │ r9 = 0x7f8f8a745d80 |
│ │ ; arg int64_t arg3 @ rdx │ r10 = 0x00000002 |
│ │ ; DATA XREF from entry0 @ 0x5556172ff546 │ r11 = 0xffffffff |
│ │ 0x5556172ff660 4157 push r15 │ r12 = 0x5556172ff530 |
└─────────────────────────────────────────────────────────────────────────┘-----------------------------------'
Tambien podemos probar con: radare2 -c "aaaa;afl;db main;dc;v" -d a.out
###Analisis estatico basico de hello_word con radare (paso a paso)
Paso a Paso
Teclear los siguientes comandos:
1) radare2 a.out
2) aaa
(analizar) 3) afl
(listar funciones 4) s main
(saltar a main) 5) pdf
desensamblar funcion
jejo@em50l:~/reversing/radare2$ radare2 a.out -- We only have bugs, features are an unintended side-effect [0x00000530]> aaaa [Cannot analyze at 0x00000520g with sym. and entry0 (aa) [x] Analyze all flags starting with sym. and entry0 (aa) [Cannot analyze at 0x00000520ac) [x] Analyze function calls (aac) [x] Analyze len bytes of instructions for references (aar) [x] Check for objc references [x] Check for vtables [x] Type matching analysis for all functions (aaft) [x] Propagate noreturn information [x] Use -AA or aaaa to perform additional experimental analysis. [x] Finding function preludes [x] Enable constraint types analysis for variables [0x00000530]> afl 0x00000530 1 42 entry0 0x00000560 4 50 -> 40 sym.deregister_tm_clones 0x000005a0 4 66 -> 57 sym.register_tm_clones 0x000005f0 5 58 -> 51 entry.fini0 0x00000630 1 10 entry.init0 0x000006d0 1 2 sym.__libc_csu_fini 0x000006d4 1 9 sym._fini 0x00000660 4 101 sym.__libc_csu_init 0x0000063a 1 23 main 0x00000510 1 6 sym.imp.puts 0x000004e8 3 23 sym._init 0x00000000 3 97 -> 123 loc.imp._ITM_deregisterTMCloneTable [0x00000530]> s main [0x0000063a]> pdf ┌ 23: int main (int argc, char **argv, char **envp); │ ; DATA XREF from entry0 @ 0x54d │ 0x0000063a 55 push rbp │ 0x0000063b 4889e5 mov rbp, rsp │ 0x0000063e 488d3d9f0000. lea rdi, str.Hello__world ; 0x6e4 ; "Hello, world!" ; const char *s │ 0x00000645 e8c6feffff call sym.imp.puts ; int puts(const char *s) │ 0x0000064a b800000000 mov eax, 0 │ 0x0000064f 5d pop rbp └ 0x00000650 c3 ret [0x0000063a]>
Pulsamos v
(entorno guiado)
Y ya tenemos menus , ponemos cambiar vistas y navegar con las flechas y el raton.
File Settings Edit View Tools Search Emulate Debug Analyze Tab [1] ┌──────────────────────────────────────────────────────────────────────────────────┐ │[X] Disassembly (pd) [Cache] Off │ │ ┌ 23: int main (int argc, char **argv, char **envp); │ │ │ ; DATA XREF from entry0 @ 0x54d │ │ │ 0x0000063a 55 push rbp │ │ │ 0x0000063b 4889e5 mov rbp, rsp │ │ │ 0x0000063e 488d3d9f0000. lea rdi, str.Hello__world ; 0x6e4 ; "Hello│ │ │ 0x00000645 e8c6feffff call sym.imp.puts ;[1] ; int puts(c│ │ │ 0x0000064a b800000000 mov eax, 0 │ │ │ 0x0000064f 5d pop rbp │ │ └ 0x00000650 c3 ret │ └──────────────────────────────────────────────────────────────────────────────────┘__________
**Menu View => Decompiler**
File Settings Edit View Tools Search Emulate Debug Tab [1] [0x0000063a]
┌───────────────────────────────────────────────────────────────────────────────┐
│[X] Decompiler (pdc) [Cache] On │
│ function main () { │
│ // 1 basic blocks │
│ │
│ loc_0x63a: │
│ │
│ //DATA XREF from entry0 @ 0x54d │
│ push rbp │
│ rbp = rsp │
│ rdi = ["Hello, world!" //0x6e4 ; str.Hello__world] ; const char *s │
│ │
│ int puts("Hello, world!") │
│ eax = 0 │
│ //rsp ; rsp │
│ return │
│ (break) │
│ │
└───────────────────────────────────────────────────────────────────────────────┘
Menu View => Decompiler with offsets
File Settings Edit View Tools Search Emulate Debug Analyze Help Tab [1] [0x0000063a]
┌──────────────────────────────────────────────────────────┐──────────────────────────────────────────────────┐
│[X] Decompiler With Offsets (pddo) [Cache] On │[X] Decompiler (pdc) [Cache] On │
│ ;-- rip: │ function main () { │
│ ┌ 23: int main (int argc, char **argv, char **envp); │ // 1 basic blocks │
│ │ ; DATA XREF from entry0 @ 0x54d │ │
│ │ 0x0000063a 55 push rbp │ loc_0x63a: │
│ │ 0x0000063b 4889e5 mov rbp, rsp │ │
│ │ 0x0000063e 488d3d9f0000. lea rdi, str.H│ //DATA XREF from entry0 @ 0x54d │
│ │ 0x00000645 e8c6feffff call sym.imp.p│ push rbp │
│ │ 0x0000064a b800000000 mov eax, 0 │ rbp = rsp │
│ │ 0x0000064f 5d pop rbp │ rdi = ["Hello, world!" //0x6e4 ; str.He│
│ └ 0x00000650 c3 ret │ │
│ 0x00000651 662e0f1f8400. nop word cs:[r│ int puts("Hello, world!") │
│ 0x0000065b 0f1f440000 nop dword [rax│ eax = 0 │
│ ┌ 101: sym.__libc_csu_init (int64_t arg1, int64_t arg2, i│ //rsp ; rsp │
│ │ ; arg int64_t arg1 @ rdi │ return │
│ │ ; arg int64_t arg2 @ rsi │ (break) │
│ │ ; arg int64_t arg3 @ rdx │ │
│ │ ; DATA XREF from entry0 @ 0x546 │ } │
│ │ 0x00000660 4157 push r15 │ │
│ │ 0x00000662 4156 push r14 │ │
│ │ 0x00000664 4989d7 mov r15, rdx │ │
│ │ 0x00000667 4155 push r13 │ │
└──────────────────────────────────────────────────────────┘──────────────────────────────────────────────────┘
Referencias
https://fwhibbit.es/introduccion-reversing-0x00-introduccion https://fwhibbit.es/introduccion-reversing-0x01-introduccion
GCC and Make Compiling, Linking and Building C/C++ Applications https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html
CodeBlocks http://www.codeblocks.org/ https://www3.ntu.edu.sg/home/ehchua/programming/howto/CodeBlocks_HowTo.html
Eclipse IDE https://www.eclipse.org/downloads/packages/
Visual Studio Community https://visualstudio.microsoft.com/es/vs/community/
Visual Studio 2019 https://visualstudio.microsoft.com/downloads/ https://visualstudio.microsoft.com/es/vs/older-downloads/ https://docs.microsoft.com/en-us/visualstudio/install/create-an-offline-installation-of-visual-studio?view=vs-2019
Visual Studio 2015 Community Edition (FREE) https://go.microsoft.com/fwlink/?LinkId=532606&clcid=0x409 (web) https://go.microsoft.com/fwlink/?LinkId=615448&clcid=0x409 (iso) https://docs.microsoft.com/en-us/visualstudio/install/create-an-offline-installation-of-visual-studio?view=vs-2015
ejemplos sencillos en c
https://www.tutorialgateway.org/c-programming-examples/ https://www.dotnetperls.com/Use the Microsoft C++ toolset from the command line https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=vs-2015 https://docs.microsoft.com/en-us/cpp/build/reference/c-cpp-building-reference?view=vs-2015
ejemplos en c# https://www.dotnetperls.com/
Fundación Sadosky (ejercicios explotación de vulnerabilidades) https://github.com/fundacion-sadosky/abopp
Apendices
Entornos de compilacion Windows/Linux
###Compilacion hello word en C (Windows con Microsoft Visual C++ Build Tools)
Documentacion Oficial:
https://docs.microsoft.com/en-us/cpp/build/walkthrough-compile-a-c-program-on-the-command-line?view=vs-2015
Para instalar un entorno de compilacion minimo.(“Microsoft Build Tools”)
puedes usar Microsoft C++ Build Tools 2015 Windows 7 necesita .NET Framework >4.5.1Nota: Este enlace me ha costado sudor. he tenido que sacar el enlace del codigo fuente de: https://github.com/felixrieseberg/windows-build-tools
Para instalar un entorno mas completo: Visual Studio Community (Ojo 12Gb)
Si eres mas de software libre: (mas ligero) - Orwell Dev-C++ (recomiendo: Dev-Cpp MinGW portable(38Mb) y Dev-Cpp TDM-GCC Portable(41Mb)) - CodeBlocks (recomiendo: mingw-nosetup.zip 90Mb ) - Eclipse IDE (Recomiendo: Eclipse Cpp Mars 170Mb ) - Si no quieres de IDE y eres mas de comandos: GCC/MinGW (GNU Compiler Collection) (Linux/windows)
Crear el archivo hello.c
En el ejemplo lo creo en la carpeta c:\kits\reversing
// hello.c #include <stdio.h> int main() { printf("Hello, world!\n"); return 0; }
Compilacion desde linea de comandos. (Paso a Paso).
Teclear los siguientes comandos:
Arrancar Visual C++ 2015 X86/X64 Build Tools Command Prompt
c:\Program Files\Microsoft Visual C++ Build Tools\vcbuildtools.bat
Para configurar el entorno de compilacion.cd c:\kits\reversing
Para ir a la carpeta del archivo.cl hello_word.c
para compilar el archivo hello_world.c y crear hello_word.exehello_word.exe
Para probar que el programa.
c:\>"c:\Program Files\Microsoft Visual C++ Build Tools\vcbuildtools.bat" c:\Program Files\Microsoft Visual C++ Build Tools>cd c:\kits\reversing c:\kits\reversing>cl hello_word.c Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24210 for x86 Copyright (C) Microsoft Corporation. All rights reserved. hello_word.c Microsoft (R) Incremental Linker Version 14.00.24210.0 Copyright (C) Microsoft Corporation. All rights reserved. /out:hello_word.exe hello_word.obj c:\kits\reversing>hello_word.exe hola Mundo
Si quieres mas programas sencillos para compilar y desensamblar: https://www.tutorialgateway.org/c-programming-examples/