30.12.2012 Views

num 10 - UnderAttHack

num 10 - UnderAttHack

num 10 - UnderAttHack

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

n.<strong>10</strong><br />

Ora ecco come è strutturato in C un ELF Header:<br />

Il Codice auto-modificante o Self-Modifiying Code<br />

typedef struct {<br />

unsigned char e_ident[16]; /* Magic <strong>num</strong>ber and other info */<br />

Elf32_Half e_type; /* Object file type */<br />

Elf32_Half e_machine; /* Architecture */<br />

Elf32_Word e_version; /* Object file version */<br />

Elf32_Addr e_entry; /* Entry point virtual address */<br />

Elf32_Off e_phoff; /* Program header table file offset */<br />

Elf32_Off e_shoff; /* Section header table file offset */<br />

Elf32_Word e_flags; /* Processor-specific flags */<br />

Elf32_Half e_ehsize; /* ELF header size in bytes */<br />

Elf32_Half e_phentsize; /* Program header table entry size */<br />

Elf32_Half e_ph<strong>num</strong>; /* Program header table entry count */<br />

Elf32_Half e_shentsize; /* Section header table entry size */<br />

Elf32_Half e_sh<strong>num</strong>; /* Section header table entry count */<br />

Elf32_Half e_shstrndx; /* Section header string table index */<br />

} Elf32_Ehdr;<br />

Come tutti sappiamo l’EP o Entry Point è l’entry del programma cioè l’indirizzo della prima istruzione della<br />

sezione .text.<br />

Ora un piccolo esempio dell’utilizzo di queste conoscenze è proposto nel source elfinj.c<br />

Di fatto il programma non fa altro che aprire il programma passato tramite argv[1] e leggere il contenuto di<br />

tutto il file per poi fare un cast esplicito a Elf32_Ehdr* che è la struttura di un ELF.<br />

Tramite di essa possiamo ricavare l’entry point.<br />

elf->e_entry<br />

Poi per sapere l’offset fisico del EP basterà fare un,<br />

elf->e_entry & 0x00000FFF<br />

per prendere gli ultimi 3 <strong>num</strong>eri dell’address.<br />

Poi possiamo usare pwrite per scrivere direttamente a quell’offset senza usare funzioni tipo fseek.<br />

pwrite( file_descriptor, buffer, size, elf->e_entry & 0x00000FFF );<br />

0.2 Text non Writable<br />

Come qualcuno può essersi accorto dopo aver guardato un po’ gli ELF header, o usato un po’ readelf , la<br />

sezione .text non è writeable, ma solo AX ( allocable, executable ).<br />

Ciò vuol dire che non ci possiamo scrivere codice nostro in run-time.<br />

Quindi bisogna trovare un modo per ovviare a questo problema.<br />

Un aiuto ci è dato da mprotect , prima di tutto troviamo le pagine dove è localizzata la .text , quindi basterà<br />

fare un:<br />

unsigned page = (unsigned)&_start & ~( getpagesize() - 1 );<br />

Per rendere la .text writeable, oppure mettere un altro indirizzo per essere più specifici.<br />

http://underatthack.org 16<br />

underatthack@gmail.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!