ELF en la mira: Hacking y Defensa

Post on 24-May-2015

606 views 2 download

description

ENLI @ UPAEP \’08, Puebla, Mexico.

Transcript of ELF en la mira: Hacking y Defensa

ELF EN LA MIRA: HACKING Y DEFENSAA.Alejandro Hernández Hernándeznitr0us@0hday.orghttp://www.genexx.org/nitrous/

UPAEP @ Puebla, Puebla. 24/Oct/2008

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

¿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.

¿QUE ES ELF? (Cont.)

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

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

INTERFACES O VISTAS

ESTRUCTURA DE ELFElf32_Ehdr

ESTRUCTURA DE ELFElf32_Shdr

ESTRUCTURA DE ELFElf32_Phdr

BLACKHAT

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

“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

Breakdown of malware according to platform

MALWARE FOR UNIX-TYPE SYSTEMS(Cont.)

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

Breakdown of malware according to platform

MALWARE FOR UNIX-TYPE SYSTEMS(Cont.)

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

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.

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

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

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

.text

.data

.bss

0x080482d0

Entrypoint

.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”

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

.text

.data

.bss

nitr0us@hacklab $ ./run_forest_run

Parásito

DEMO

ELF_data_infector.c (nitr0us)

4554invader.c (Electronic Souls)

ES-Malaria (Electronic Souls)

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

DEMO

Hydan (Rakan El-Khalil)

WHITEHAT

SEGURIDAD EN ELF

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

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

DEMO

cryptelf.c (SLACKo)

ELFCrypt.c (JunkCode)

elfsign (skape)

GRAYHAT

FRANK SINATRA

• 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

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

ELF HEADER (Elf32_Ehdr)

¿Y QUE SI…?

ELF PROGRAM HEADER (Elf32_Ehdr)

¿Y QUE SI…?

INTERPRETE

¿Y QUE SI…?

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…

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);

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);

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);

DEMO

ELFsh_crash.esh (nitr0us)

ht_shstrndx_poc.c (nitr0us)

Gizmo (izik)

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.

¿PREGUNTAS? ¿COMENTARIOS?

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.

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.

[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.)

A. Alejandro Hernández Hernándeznitr0us@0hday.orghttp://www.genexx.org/nitrous/

Gracias !