Reverse engineering: executable file format



Documenti analoghi
Reverse engineering: executable file format

Dynamic Linking. Introduzione Creazione di una libreria dinamica Uso di una libreria dinamica

Reverse engineering: disassembly

ToolChain: Come Generare Applicazioni in Linguaggio Macchina

Stack-based buffer overflow

La protezione dai memory error exploit

Formato degli Eseguibili e Strumenti Avanzati di Compilazione

Introduzione a GCC: GNU Compiler Collection

Fondamenti di Informatica e Laboratorio T-AB T-16 Progetti su più file. Funzioni come parametro. Parametri del main

Esercitazione E7 Immagini

Reverse engineering: disassembly

Librerie. Laboratorio di Informatica Antonio Monteleone 28

Processi in Linux. Stru/ura file eseguibili

ESP/EBP. ESP Frame func()

Sistema operativo: Gestione della memoria

Stack-based buffer overflow

Guida all utilizzo del compilatore lcc-win32 per creare una applicazione console

Kernel and User space

ASM: catena di compilazione

Introduzione alla programmazione in C

Tecniche di traduzione da C ad assembly Note generali

Bloodshed Dev-C++ è l IDE usato durante le esercitazioni/laboratorio. IDE = Integrated Development Environment

Assembler di Spim. Assembler di SPIM. Struttura di un programma assembler. Direttive

Hardware di un Computer

Le command line di Java

[

Makefile. Un target in generale e' un file. Usando.PHONY: target specifichiamo a make che il target non e' un file

Laboratorio di Programmazione 1. Docente: dr. Damiano Macedonio Lezione 18 31/03/2014

Introduzione a Dev-C++

Una prima applicazione in C per l utilizzo delle socket

VARIABILI LOCALI E GLOBALI (ESTERNE)

INTRODUZIONE, LINGUAGGIO, HANDS ON. Giuseppe Cirillo

Il compilatore Dev-C++

AXO. Operativo. Architetture dei Calcolatori e Sistema. programmazione di sistema

Linguaggio C Compilazione, linking e preprocessor

Strumenti per l Analisi Statica e Dinamica di Eseguibili

L ambiente di simulazione SPIM

Alessandro Pellegrini

I puntatori e l allocazione dinamica di memoria

Calcolatori Elettronici Parte X: l'assemblatore as88

Corso Programmazione 1 Capitolo 01: Concetti Elementari

Header. Unità 9. Corso di Laboratorio di Informatica Ingegneria Clinica BCLR. Domenico Daniele Bloisi

MIPS Instruction Set 2


Antifork Research, Inc. HACKERS RESEARCH VIRTUAL LAB. Shellcode Advanced. Angelo Dell'Aera Security Date 2004 Ancona 29/04/2004

Programmazione C Massimo Callisto De Donato massimo.callisto@unicam.it

Alessandro Pellegrini

AXO Architettura dei Calcolatori e Sistema Operativo. processo di assemblaggio

Funzioni. Il modello console. Interfaccia in modalità console

CALCOLATORI ELETTRONICI II

FASE DEBUGGING: Compiler Linker. controllando che la voce Genera le informazioni per il debug cioè. "Generate debugging information"

costruttori e distruttori

Linguaggi di Programmazione

Il Software e Il Sistema Operativo. Prof. Francesco Accarino IIS Altiero Spinelli A.S. 09/10

Funzioni in C. Violetta Lonati

Dipartimento di Sistemi e Informatica Università degli Studi di Firenze. Dev-C++ Ing. Michele Banci 27/03/2007 1

Lezione 1: L architettura LC-3 Laboratorio di Elementi di Architettura e Sistemi Operativi 10 Marzo 2014

Preprocessing, compilazione ed esecuzione. Utilizzando strumenti GNU...

Compilatore risorse display grafico LCD serie IEC-line

Fondamenti di Informatica 2

Corso di Informatica (Programmazione) Lezione 6 (31 ottobre 2008)

Strutture. Strutture e Unioni. Definizione di strutture (2) Definizione di strutture (1)

Lezione 10: Librerie e compilazione separata

Appunti del corso di Informatica 1 (IN110 Fondamenti) 6 Introduzione al linguaggio C

Sistemi Operativi 1. Mattia Monga. a.a. 2013/14. Dip. di Informatica Università degli Studi di Milano, Italia

strutturare dati e codice

Gli array. Gli array. Gli array. Classi di memorizzazione per array. Inizializzazione esplicita degli array. Array e puntatori

Sistemi Operativi. Esercitazione 2 Compilazione, Makefile e Processi

Dev C++ Prerequisiti. Note di utilizzo. Utilizzo di Windows Effettuare il download da Internet Compilazione di un programma

Laboratorio di Calcolatori 1 Corso di Laurea in Fisica A.A. 2006/2007

Nascita di Java. Che cos e Java? Caratteristiche di Java. Java: linguaggio a oggetti

INFORMATICA 1 L. Mezzalira

Introduzione. Laboratorio di Calcolo Corso di Laurea in Fisica. Università degli Studi di Roma La Sapienza

Introduzione ai linguaggi di programmazione

Architettura del Set di Istruzioni (ISA)

Sistemi Operativi MECCANISMI E POLITICHE DI PROTEZIONE. D. Talia - UNICAL. Sistemi Operativi 13.1

MECCANISMI E POLITICHE DI PROTEZIONE 13.1

Capitolo Quarto...2 Le direttive di assemblaggio di ASM Premessa Program Location Counter e direttiva ORG

Appunti del corso di Informatica 1. 6 Introduzione al linguaggio C

Corso di INFORMATICA 2 (Matematica e Applicazioni)

CREARE UNA LIBRERIA IN C

Il Software. Il software del PC. Il BIOS

Laboratorio di Sistemi Operativi Cognome Nome Mat.

Il simulatore SPIM SPIM

Gestione della memoria. Paginazione Segmentazione Segmentazione con paginazione

GLUI: GLUT-based User Interface

Struttura del Micro Filesystem (µfs)

Gestione dei File in C

NOZIONI BASE PER ESERCITAZIONI

Perché il linguaggio C?

Link e permessi. Corso di Laurea Triennale in Ingegneria delle TLC e dell Automazione. Corso di Sistemi Operativi A. A

Introduzione al linguaggio C Gli array

Java: Compilatore e Interprete

Dal linguaggio macchina al linguaggio C

Introduzione ecos. Agenda. Giovanni Perbellini

Calcolatori Ele,ronici Lezione 6 17/11/2011. Emiliano Casalicchio

La genealogia di Windows. Windows NT e Windows 95/98. Dimensioni del codice. Parte IX. Windows

Parte IX. Windows. Sistemi Operativi - prof. Silvio Salza - a.a IX - 1

Transcript:

Università degli Studi di Milano Facoltà di Scienze Matematiche, Fisiche e Naturali Reverse engineering: executable file format Roberto Paleari <roberto@security.dico.unimi.it> Emanuele Passerini <ema@security.dico.unimi.it> A.A. 2008 2009 Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 1 / 37

Sommario 1 Introduzione 2 Struttura file eseguibili Formato ELF Formato PE 3 Esempi di reversing Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 2 / 37

Sommario 1 Introduzione 2 Struttura file eseguibili Formato ELF Formato PE 3 Esempi di reversing Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 2 / 37

Sommario 1 Introduzione 2 Struttura file eseguibili Formato ELF Formato PE 3 Esempi di reversing Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 2 / 37

Introduzione Reverse engineering [1] reverse engineering is the process of extracting the knowledge or design blueprints from anything man-made contesti differenti (e.g., elettronica, informatica,... ) Software reverse engineering: perché? studiare architettura interna (dal sorgente) ricostruire codice sorgente (dal binario) modificare comportamento (programmi closed-source) comprendere l attività di rete (protocolli proprietari) Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 3 / 37

Introduzione Reverse engineering [1] reverse engineering is the process of extracting the knowledge or design blueprints from anything man-made contesti differenti (e.g., elettronica, informatica,... ) Software reverse engineering: perché? studiare architettura interna (dal sorgente) ricostruire codice sorgente (dal binario) modificare comportamento (programmi closed-source) comprendere l attività di rete (protocolli proprietari) Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 3 / 37

Software reverse engineering Applicazioni Come? plagiarismo interoperabilità. Alcuni esempi: Samba protocollo SMB WINE Windows API ReactOS Windows XP/2003 OpenOffice Microsoft Office malware analysis cracking 1 tecniche statiche 2 tecniche dinamiche Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 4 / 37

Software reverse engineering Tecniche statiche il programma non viene eseguito documentazione codice sorgente analisi di stringhe, simboli, funzioni di libreria disassembly e analisi codice assembly Tecniche dinamiche esecuzione (monitorata) dell applicazione interazioni con l ambiente (e.g., file system, rete, registro) interazioni con il sistema operativo (system call) debugging Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 5 / 37

Analisi di codice binario Tecniche statiche 1 analisi meta-informazioni dell eseguibile funzioni di libreria utilizzate simboli informazioni di debugging... 2 disassembly call graph analisi codice assembly... Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 6 / 37

Struttura file eseguibili file eseguibili moderni hanno una struttura complessa Linux Executable and Linkable Format (ELF) Windows Portable Executable (PE) Un po di storia... MS-DOS formato COM no header, no meta-data solo codice e dati, in un unico segmento problemi max 0xff00 byte non rilocabile niente dynamic linking... Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 7 / 37

Struttura file eseguibili file eseguibili moderni hanno una struttura complessa Linux Executable and Linkable Format (ELF) Windows Portable Executable (PE) Un po di storia... MS-DOS formato COM no header, no meta-data solo codice e dati, in un unico segmento problemi max 0xff00 byte non rilocabile niente dynamic linking... Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 7 / 37

ELF: Executable and Linkable Format Tipologie di file ELF [4] 1 relocatable 2 executable 3 shared object Esempio roby@gandalf:/tmp$ gcc -c test.c -o test.o roby@gandalf:/tmp$ file test.o test.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV) roby@gandalf:/tmp$ gcc test.c -o test roby@gandalf:/tmp$ file test test: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV) roby@gandalf:/tmp$ gcc -shared test.c -o test.so roby@gandalf:/tmp$ file test.so test.so: ELF 32-bit LSB shared object, Intel 80386, version 1 (SYSV) Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 8 / 37

ELF: Executable and Linkable Format Struttura file ELF ELF header Program header table Section 1... Section n Informazioni sull organizzazione del file... Section header table ELF executable file Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 9 / 37

ELF: Executable and Linkable Format Struttura file ELF ELF header Program header table Section 1... Section n Dettagli su come creare l immagine in memoria... Section header table ELF executable file Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 9 / 37

ELF: Executable and Linkable Format Struttura file ELF ELF header Program header table Section 1... Section n... Contengono i dati effettivi (e.g., istruzioni, dati, symbol table) Section header table ELF executable file Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 9 / 37

ELF: Executable and Linkable Format Struttura file ELF ELF header Program header table Section 1... Section n Informazioni relative alle sezioni (e.g., nome, dimensione)... Section header table ELF executable file Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 9 / 37

ELF: Executable and Linkable Format ELF header Elf32 Ehdr in /usr/include/linux/elf.h magic number (\x7felf), ELF type & version architettura & endianness entry point (virtual address) offset (nel file) di program & section header table... Esempio roby@gandalf:/tmp$ readelf -h test ELF Header: Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 Type: EXEC (Executable file) Machine: Intel 80386 Version: 0x1 Entry point address: 0x80482f0 Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 10 / 37

ELF: Executable and Linkable Format Section header table Array di Elf32 Shdr in /usr/include/linux/elf.h specifica, per ogni sezione nome (i.e., indice nella section header string table) posizione nel file e memoria dimensione... Esempio roby@gandalf:/tmp$ objdump -h test Idx Name Size VMA LMA File off Algn 0.interp 00000013 08048114 08048114 00000114 2**0 CONTENTS, ALLOC, LOAD, READONLY, DATA 12.text 0000017c 080482f0 080482f0 000002f0 2**4 CONTENTS, ALLOC, LOAD, READONLY, CODE 22.data 00000008 080495a4 080495a4 000005a4 2**2 CONTENTS, ALLOC, LOAD, DATA 23.bss 00000008 080495ac 080495ac 000005ac 2**2 ALLOC Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 11 / 37

ELF: Executable and Linkable Format Program header table Array di Elf32 Phdr in /usr/include/linux/elf.h array di strutture, ciascuna delle quali definisce Esempio un segmento (i.e., 1 + sezioni in memoria); oppure, altre informazioni necessarie per organizzare l immagine in memoria roby@gandalf:/tmp$ objdump -p test Program Header: PHDR off 0x00000034 vaddr 0x08048034 paddr 0x08048034 align 2**2 filesz 0x000000e0 memsz 0x000000e0 flags r-x INTERP off 0x00000114 vaddr 0x08048114 paddr 0x08048114 align 2**0 filesz 0x00000013 memsz 0x00000013 flags r-- LOAD off 0x00000000 vaddr 0x08048000 paddr 0x08048000 align 2**12 filesz 0x000004a4 memsz 0x000004a4 flags r-x LOAD off 0x000004a4 vaddr 0x080494a4 paddr 0x080494a4 align 2**12 filesz 0x00000108 memsz 0x00000110 flags rw- DYNAMIC off 0x000004b8 vaddr 0x080494b8 paddr 0x080494b8 align 2**2 filesz 0x000000d0 memsz 0x000000d0 flags rw- NOTE off 0x00000128 vaddr 0x08048128 paddr 0x08048128 align 2**2 filesz 0x00000020 memsz 0x00000020 flags r-- STACK off 0x00000000 vaddr 0x00000000 paddr 0x00000000 align 2**2 filesz 0x00000000 memsz 0x00000000 flags rw- Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 12 / 37

ELF: Executable and Linkable Format Symbol table Esempio sezione con una entry per simbolo debugging, dynamic linking, relocation,... roby@gandalf:/tmp$ readelf -s test Symbol table.dynsym contains 5 entries: Num: Value Size Type Bind Vis Ndx Name 0: 00000000 0 NOTYPE LOCAL DEFAULT UND 3: 00000000 460 FUNC GLOBAL DEFAULT UND puts@glibc_2.0 (2) 4: 0804848c 4 OBJECT GLOBAL DEFAULT 15 _IO_stdin_used Symbol table.symtab contains 74 entries: Num: Value Size Type Bind Vis Ndx Name 0: 00000000 0 NOTYPE LOCAL DEFAULT UND 1: 08048114 0 SECTION LOCAL DEFAULT 1 70: 080495ac 0 NOTYPE GLOBAL DEFAULT ABS _edata 71: 0804843a 0 FUNC GLOBAL HIDDEN 13 i686.get_pc_thunk.bx 72: 080483a4 38 FUNC GLOBAL DEFAULT 13 main 73: 08048274 0 FUNC GLOBAL DEFAULT 11 _init Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 13 / 37

ELF: Executable and Linkable Format Symbol table Esempio sezione con una entry per simbolo debugging, dynamic linking, relocation,... roby@gandalf:/tmp$ readelf -S test grep \.symtab [34].symtab SYMTAB roby@gandalf:/tmp$ strip test roby@gandalf:/tmp$ readelf -s test Symbol table.dynsym contains 5 entries: Num: Value Size Type Bind Vis Ndx Name 0: 00000000 0 NOTYPE LOCAL DEFAULT UND 3: 00000000 460 FUNC GLOBAL DEFAULT UND puts@glibc_2.0 (2) 4: 0804848c 4 OBJECT GLOBAL DEFAULT 15 _IO_stdin_used roby@gandalf:/tmp$ readelf -S test grep \.symtab roby@gandalf:/tmp$ Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 13 / 37

ELF: supporto al dynamic linking Dynamic linking parte delle operazioni di linking vengono rimandate a runtime facilita l aggiornamento delle shared library consente caricamento delle librerie a runtime overhead a runtime superiore rispetto a linking statico simboli linkati dinamicamente devono essere risolti al volo Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 14 / 37

ELF: supporto al dynamic linking GOT e PLT: cosa sono? Global Offset Table e Procedure Linkage Table sezioni (.got e.plt) presenti in eseguibili che utilizzano dynamic linking PLT aggiunge un livello di indirettezza alle chiamate a funzione, consentendo anche il lazy binding dei relativi indirizzi Esempio roby@gandalf:~$ objdump -h $(which ls) Sections: Idx Name Size VMA LMA File off Algn 11.plt 00000610 08049538 08049538 00001538 2**2 CONTENTS, ALLOC, LOAD, READONLY, CODE 21.got 00000008 0805b5d4 0805b5d4 000125d4 2**2 CONTENTS, ALLOC, LOAD, DATA Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 15 / 37

ELF: supporto al dynamic linking GOT e PLT: come funzionano? Chiamata ad una funzione shared 1 chiamata f() shared chiamata ad una entry della PLT 2 PLT: jmp indiretto ad una entry della GOT che conterrà: la prima volta, l indirizzo di una entry della PLT che trasferisce il controllo al linker, per risoluzione e aggiornamento della GOT successivamente, l indirizzo effettivo della shared function Esempio roby@gandalf:/tmp$ objdump -d test 0804830c <puts@plt>: 804830c: ff 25 08 96 04 08 jmp *0x8049608 8048312: 68 18 00 00 00 push $0x18 8048317: e9 b0 ff ff ff jmp 80482cc <_init+0x30> 080483d4 <main>: 80483ec: e8 1b ff ff ff call 804830c <puts@plt> Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 16 / 37

ELF: supporto al dynamic linking Esempio.got e.plt call <puts@plt>.text <puts@plt>+0: jmp *<GOT+n> <puts@plt>+6: linker.plt <GOT+n>: <puts@plt+6>.got Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 17 / 37

ELF: supporto al dynamic linking Esempio.got e.plt call <puts@plt>.text 1 <puts@plt>+0: jmp *<GOT+n> <puts@plt>+6: linker.plt <GOT+n>: <puts@plt+6>.got Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 17 / 37

ELF: supporto al dynamic linking Esempio.got e.plt call <puts@plt>.text 1 <puts@plt>+0: jmp *<GOT+n> <puts@plt>+6: linker.plt 2 <GOT+n>: <puts@plt+6>.got Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 17 / 37

ELF: supporto al dynamic linking Esempio.got e.plt call <puts@plt>.text 1 <puts@plt>+0: jmp *<GOT+n> <puts@plt>+6: linker.plt 2 <GOT+n>: <puts>.got Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 17 / 37

ELF: supporto al dynamic linking Esempio.got e.plt 3 call <puts@plt>.text <puts@plt>+0: jmp *<GOT+n> <puts@plt>+6: linker.plt <GOT+n>: <puts>.got Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 17 / 37

ELF: supporto al dynamic linking Lazy binding: performance $ time (mplayer >/dev/null) real 0m0.039s user 0m0.024s sys 0m0.012s $ time (LD_BIND_NOW=1 mplayer >/dev/null) real 0m0.141s user 0m0.100s sys 0m0.016s Esempio: dump librerie e dynamic loader $ ldd /tmp/test linux-gate.so.1 => (0xb7faf000) libc.so.6 => /lib/i686/cmov/libc.so.6 (0xb7e43000) /lib/ld-linux.so.2 (0xb7fb0000) [15:35:02] roby@gandalf:/tmp$ readelf./test -x.interp Hex dump of section.interp : 0x08048114 2f6c6962 2f6c642d 6c696e75 782e736f /lib/ld-linux.so 0x08048124 2e3200.2. Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 18 / 37

PE: Portable Executable Tipologie di file PE [2, 3] 1 executable 2 object 3 DLL 4 COM files, OCX controls, CLP applets,.net executables 5 device driver 6... Esempio roby@gandalf:/tmp$ file test.exe test.exe: PE32 executable for MS Windows (console) Intel 80386 32-bit roby@gandalf:/tmp$ file test.o test.o: 80386 COFF executable not stripped - version 30821 roby@gandalf:/tmp$ file test.dll test.dll: PE32 executable for MS Windows (DLL) (console) Intel roby@gandalf:/tmp$ file test.sys test.sys: PE32 executable for MS Windows (DLL) (native) Intel Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 19 / 37

PE: Portable Executable Struttura file PE DOS header DOS stub PE header Section table.text section.data section Per retrocompatibilità...... PE executable file Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 20 / 37

PE: Portable Executable Struttura file PE DOS header DOS stub PE header Section table.text section.data section... Contiene informazioni sull organizzazione del file... PE executable file Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 20 / 37

PE: Portable Executable Struttura file PE DOS header DOS stub PE header Section table.text section.data section... Contengono i dati effettivi (e.g., istruzioni, dati, rilocazione)... PE executable file Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 20 / 37

PE: Portable Executable Struttura file PE DOS header DOS stub PE header Section table.text section.data section... Informazioni relative alle sezioni (e.g., nome, dimensione)... PE executable file Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 20 / 37

PE: Portable Executable Introduzione modulo: file PE caricato in memoria (e riorganizzato dal loader) RVA: Relative Virtual Address indirizzo relativo al base address dell immagine in memoria RVA file offset strutture definite in winnt.h Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 21 / 37

PE: Portable Executable DOS header & stub DOS header typedef struct IMAGE DOS HEADER { WORD e magic; LONG e lfanew; } IMAGE DOS HEADER, PIMAGE DOS HEADER; 64 byte, magic number \x4d\x5a ( MZ ) e lfanew: offset PE header segue DOS stub DOS stub This program cannot be run in DOS mode Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 22 / 37

PE: Portable Executable PE header typedef struct IMAGE NT HEADERS { DWORD Signature; // \x50\x45\x00\x00 ( PE\x00\x00 ) IMAGE FILE HEADER FileHeader; IMAGE OPTIONAL HEADER OptionalHeader; } IMAGE NT HEADERS32, PIMAGE NT HEADERS32; typedef struct IMAGE FILE HEADER { WORD Machine; WORD NumberOfSections; } IMAGE FILE HEADER, PIMAGE FILE HEADER; typedef struct IMAGE OPTIONAL HEADER { // non e opzionale! DWORD AddressOfEntryPoint; DWORD ImageBase; DWORD SectionAlignment; DWORD FileAlignment; WORD Subsystem; IMAGE DATA DIRECTORY DataDirectory[IMAGE NUMBEROF DIRECTORY ENTRIES]; } IMAGE OPTIONAL HEADER32, PIMAGE OPTIONAL HEADER32; Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 23 / 37

PE: Portable Executable Section table typedef struct IMAGE SECTION HEADER { BYTE Name[IMAGE SIZEOF SHORT NAME]; union { DWORD PhysicalAddress; DWORD VirtualSize; } Misc; DWORD VirtualAddress; DWORD SizeOfRawData; DWORD PointerToRawData; DWORD Characteristics; } IMAGE SECTION HEADER, PIMAGE SECTION HEADER; Alcune sezioni 1 executable code section (.text) (di solito, AddressOfEntryPoint.text) 2 data sections (.data,.rdata,.bss,... ) 3.rsrc,.edata,.reloc,.debug,... Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 24 / 37

PE: Portable Executable Import section (.idata) OriginalFirstThunk thunk 1 h 1 name 1 thunk 1 TimeDateStamp thunk 2 thunk n h 2 name 2 thunk 2 thunk n Name h n name n FirstThunk = IMAGE IMPORT DESCRIPTOR = import name table = IMAGE IMPORT BY NAME = import address table Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 25 / 37

PE: Portable Executable Import section (.idata) OriginalFirstThunk thunk 1 h 1 name 1 &name 1 TimeDateStamp thunk 2 thunk n h 2 name 2 &name 2 &name n Name h n name n FirstThunk = IMAGE IMPORT DESCRIPTOR = import name table = IMAGE IMPORT BY NAME = import address table Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 25 / 37

PE: Portable Executable Import section (.idata) invocazione di funzioni importate 0040142d: call 004015c0 < LoadLibraryA@4> 004015c0 < LoadLibraryA@4>: 004015c0: jmp 0x00405098 richiede 1 call + 1 jmp jump stubs.got differenze? 01007105: call 0x0100108c solo 1 call declspace(ddlimport) Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 26 / 37

PE: Portable Executable Import section (.idata) invocazione di funzioni importate 0040142d: call 004015c0 < LoadLibraryA@4> 004015c0 < LoadLibraryA@4>: 004015c0: jmp 0x00405098 richiede 1 call + 1 jmp jump stubs.got differenze? 01007105: call 0x0100108c solo 1 call declspace(ddlimport) Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 26 / 37

PE: Portable Executable Esercitazione Obiettivo iniettare codice in un binario PE sfruttare l allineamento delle sezioni su file e memoria iniettare in.text il codice iniettato deve semplicemente stampare una stringa procedimento: 1 scrivere codice macchina alla fine di.text 2 modificare AddressOfEntryPoint 3 prima di concludere il codice iniettato, restituire il controllo all entry point originale strumenti: 1 editor esadecimale (bvi, biew,... ) 2 objdump Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 27 / 37

Reverse engineering: alcuni esempi Variabili 1 #include <string.h> 2 3 int global; 4 5 int main(int argc, char argv) 6 { 7 int local; 8 char v[256]; 9 10 local = 10; 11 global = 11; 12 13 v[5] = a ; 14 15 return 0; 16 } 1 <main>: 2 3 8048374: lea 0x4(%esp),%ecx 4 8048378: and $0xfffffff0,%esp 5 804837b: pushl -0x4(%ecx) 6 804837e: push %ebp 7 804837f: mov %esp,%ebp 8 8048381: push %ecx 9 8048382: sub $0x110,%esp 10 8048388: movl $0xa, 0x8(%ebp) 11 804838f: movl $0xb,0x8049590 12 8048399: movb $0x61, 0x103(%ebp) 13 80483a0: mov $0x0,%eax 14 80483a5: add $0x110,%esp 15 80483ab: pop %ecx 16 80483ac: pop %ebp 17 80483ad: lea -0x4(%ecx),%esp 18 80483b0: ret 19 Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 28 / 37

Reverse engineering: alcuni esempi Chiamata a funzione 1 int sum(int a, int b) 2 { 3 int c; 4 5 c = a + b; 6 return c; 7 } 8 9 int main(int argc, char argv) 10 { 11 int a; 12 13 a = sum(2,3); 14 return 0; 15 } 1 <sum>: 2 push %ebp 3 mov %esp,%ebp 4 sub $0x10,%esp 5 mov 0xc(%ebp),%edx 6 mov 0x8(%ebp),%eax 7 add %edx,%eax 8 mov %eax, 0x4(%ebp) 9 mov 0x4(%ebp),%eax 10 leave 11 ret 12 13 <main>: 14 15 sub $0x18,%esp 16 movl $0x3,0x4(%esp) 17 movl $0x2,(%esp) 18 call <sum> 19 mov %eax, 0x8(%ebp) 20 Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 29 / 37

Reverse engineering: alcuni esempi Costrutto if 1 #include <stdio.h> 2 3 int main(int argc, char argv) 4 { 5 if (argc < 2) { 6 printf(! syntax error.\n ); 7 } else { 8 printf( ok.\n ); 9 } 10 11 return 0; 12 } 1 <main>: 2 3 80483b7: mov %eax, 0x8(%ebp) 4 80483ba: cmpl $0x1,(%ecx) 5 80483bd: jg 80483cd <main+0x29> 6 80483bf: movl $0x80484b0,(%esp) 7 80483c6: call 80482d4 <puts@plt> 8 80483cb: jmp 80483d9 <main+0x35> 9 80483cd: movl $0x80484c0,(%esp) 10 80483d4: call 80482d4 <puts@plt> 11 80483d9: mov $0x0,%eax 12 Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 30 / 37

Reverse engineering: alcuni esempi Costrutto while 1 #include <stdio.h> 2 3 int main(int argc, char argv) 4 { 5 int i; 6 7 i = 0; 8 while (i < 10) { 9 printf( %02d\n, i); 10 i += 1; 11 } 12 13 return 0; 14 15 } 1 <main>: 2 3 80483b5: movl $0x0, 0x8(%ebp) 4 80483bc: jmp 80483d5 <main+0x31> 5 80483be: mov 0x8(%ebp),%eax 6 80483c1: mov %eax,0x4(%esp) 7 80483c5: movl $0x80484b0,(%esp) 8 80483cc: call 80482d8 <printf@plt> 9 80483d1: addl $0x1, 0x8(%ebp) 10 80483d5: cmpl $0x9, 0x8(%ebp) 11 80483d9: jle 80483be <main+0x1a> 12 80483db: mov $0x0,%eax 13 Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 31 / 37

Reverse engineering: alcuni esempi Costrutto for 1 #include <stdio.h> 2 3 int main(int argc, char argv) 4 { 5 int i; 6 7 for(i=0; i<10; i++) { 8 printf( %02d\n, i); 9 } 10 11 return 0; 12 13 } 1 <main>: 2 3 80483b5: movl $0x0, 0x8(%ebp) 4 80483bc: jmp 80483d5 <main+0x31> 5 80483be: mov 0x8(%ebp),%eax 6 80483c1: mov %eax,0x4(%esp) 7 80483c5: movl $0x80484b0,(%esp) 8 80483cc: call 80482d8 <printf@plt> 9 80483d1: addl $0x1, 0x8(%ebp) 10 80483d5: cmpl $0x9, 0x8(%ebp) 11 80483d9: jle 80483be <main+0x1a> 12 80483db: mov $0x0,%eax 13 Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 32 / 37

Reverse engineering: alcuni esempi Costrutto switch 1 int main(int argc, char argv) 2 { 3 int a; 4 5 a = argc; 6 7 switch(a) { 8 case 0: 9 printf( case 0\n ); break; 10 case 1: 11 printf( case 1\n ); break; 12 13 case 4: 14 printf( case 4\n ); break; 15 default: 16 printf( default\n ); break; 17 } 18 19 return 0; 20 } 1 <main>: 2 3 80483b7: mov %eax, 0x8(%ebp) 4 80483ba: cmpl $0x4, 0x8(%ebp) 5 80483be: ja 8048414 <main+0x70> 6 80483c0: mov 0x8(%ebp),%eax 7 80483c3: shl $0x2,%eax 8 80483c6: mov 0x8048520(%eax),%eax 9 80483cc: jmp %eax 10 80483ce: movl $0x80484f0,(%esp) 11 80483d5: call 80482d4 <puts@plt> 12 13 8048406: movl $0x804850c,(%esp) 14 804840d: call 80482d4 <puts@plt> 15 8048412: jmp 8048420 <main+0x7c> 16 8048414: movl $0x8048513,(%esp) 17 804841b: call 80482d4 <puts@plt> 18 8048420: mov $0x0,%eax 19 Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 33 / 37

Esercizi 1 scrivere un applicazione che, ricevuto in input un eseguibile ELF, determina il suo entry point 2 ricompilare gli esempi C riportati nelle slide precedenti variando il livello di ottimizzazione del compilatore (GCC: flag -O); osservare come varia il codice assembly generato 3 modificare l applicazione del punto 1 in modo da determinare l indirizzo della procedura main() 4 esempio C su costrutto switch: è possibile fare in modo che venga eseguito il case 0? (senza modificare il codice!) Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 34 / 37

Prossima lezione tecniche di disassembly installare IDA Pro! versione freeware: http://www.hex-rays.com/idapro/idadownfreeware.htm Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 35 / 37

Bibliografia I E. Eilam. Reversing: Secrets of Reverse Engineering. Wiley Publishing, Inc., 2005. M. Pietrek. An In-Depth Look into the Win32 Portable Executable File Format Part 1. MSDN Magazine, 2002. M. Pietrek. An In-Depth Look into the Win32 Portable Executable File Format Part 2. MSDN Magazine, 2002. TIS Committee. Tool Interface Standard (TIS) Executable and Linking Format (ELF) Specification Version 1.2. May 1995. Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 36 / 37

Domande? Roberto Paleari, Emanuele Passerini Reverse engineering: executable file format A.A. 2008 2009 37 / 37