Reverse engineering: disassembly
Transcript
Reverse engineering: disassembly
Università degli Studi di Milano Facoltà di Scienze e Tecnologie Dipartimento di Informatica Reverse engineering: disassembly Srdjan Matic <[email protected]> Aristide Fattori <[email protected]> A.A. 2013–2014 Srdjan Matic, Aristide Fattori Reverse engineering: disassembly A.A. 2013–2014 1 / 18 Tecniche statiche Disassembly Cos’è? stream of bytes disassembly sequence of assembly instructions Esempio $ echo -ne "\x89\x04\x24\xc3" | ndisasm -u 00000000 890424 mov [esp],eax 00000003 C3 ret Srdjan Matic, Aristide Fattori Reverse engineering: disassembly A.A. 2013–2014 2 / 18 Tecniche statiche Disassembly Indecidibilità della separazione tra codice e dati Intel x86: 248/256 byte rappresentano l’inizio di un’istruzione valida Esempio ... 0x81 0xc3 0x90 0x90 0x90 0x90 . . . se consideriamo il byte 0x81 come codice: $ echo -ne \x81\xc3\x90\x90\x90\x90 | ndisasm -b32 00000000 81C390909090 add ebx,0x90909090 se consideriamo il byte 0x81 come data e il byte 0xc3 come codice: $ echo -ne \xc3\x90\x90\x90\x90 | ndisasm -b32 00000000 C3 ret 00000001 90 nop 00000002 90 nop 00000003 90 nop 00000004 90 nop Srdjan Matic, Aristide Fattori Reverse engineering: disassembly A.A. 2013–2014 3 / 18 Disassembly Problema: esplosione di complessità 1 2 3 4 5 6 7 8 9 10 11 12 13 Srdjan Matic, Aristide Fattori int main(int argc, char ∗∗argv) { int num; scanf(”%d”, &num); if(num == 1234) printf(”ok\n”); else printf(”no\n”); return 0; } Reverse engineering: disassembly A.A. 2013–2014 4 / 18 Disassembly Problema: esplosione di complessità push mov sub call call call leave ret pushl jmp add jmp push jmp jmp push jmp jmp push jmp jmp push jmp xor pop mov and push push push push push push push push call hlt nop nop push mov %ebp %esp,%ebp $0x8,%esp 8048304 8048360 8048490 0x80495dc *0x80495e0 %al,(%eax) *0x80495e4 $0x0 8048290 *0x80495e8 $0x8 8048290 *0x80495ec $0x10 8048290 *0x80495f0 $0x18 8048290 %ebp,%ebp %esi %esp,%ecx $-16,%esp %eax %esp %edx $0x80483e0 $0x8048430 %ecx %esi $0x8048384 80482c0 %ebp %esp,%ebp push sub call pop add mov test je call pop pop leave ret nop ... push mov sub cmpb je jmp add mov call mov mov test jne movb leave ret nop push mov sub mov test je mov test je movl call Srdjan Matic, Aristide Fattori %ebx $0x4,%esp 8048310 %ebx $0x12c8,%ebx -4(%ebx),%edx %edx,%edx 8048326 80482d0 %eax %ebx leave ret nop push mov sub and mov add add shr shl sub lea %ebp mov %esp,%ebp movl $0x8,%esp call mov $0x0,0x8049600 804834b cmp 804835d jne $0x4,%eax movl %eax,0x80495fc call jmp *%edx movl 0x80495fc,%eax call (%eax),%edx mov %edx,%edx leave 8048341 ret $0x1,0x8049600 nop ... push mov %ebp push %esp,%ebp push $0x8,%esp push 0x8049508,%eax call %eax,%eax add 8048381 sub $0x0,%eax lea %eax,%eax lea 8048381 sub $0x8049508,(%esp)sar *%eax lea cmp je %ebp lea %esp,%ebp call $0x18,%esp dec $-16,%esp cmp $0x0,%eax jne $0xf,%eax lea $0xf,%eax call $0x4,%eax add $0x4,%eax pop %eax,%esp pop -4(%ebp),%eax pop %eax,0x4(%esp) pop $0x80484e8,(%esp)ret 80482b0 lea -4(%ebp),%eax push $0x4d2,%eax mov 80483cb push $0x80484eb,(%esp)push 80482a0 push 80483d7 call $0x80484ee,(%esp)add 80482a0 sub $0x0,%eax call lea lea sub %ebp sar %esp,%ebp mov %edi je %esi xor %ebx mov 8048483 lea $0x11ed,%ebx lea $0xc,%esp inc -224(%ebx),%eax call -224(%ebx),%edi add %edi,%eax cmp $0x2,%eax jne Reverse engineering: disassembly -1(%eax),%esi $-1,%esi 8048419 0x0(%esi),%esi *(%edi,%esi,4) %esi $-1,%esi 8048410 0x0(%esi),%esi 80484c4 $0xc,%esp %ebx %esi %edi %ebp 0x0(%esi),%esi %ebp %esp,%ebp %edi %esi %ebx 8048483 $0x119d,%ebx $0xc,%esp 8048278 -224(%ebx),%eax -224(%ebx),%edx %edx,%eax $0x2,%eax %eax,-16(%ebp) 804847b %edi,%edi %edx,%esi 0x0(%esi),%esi 0x0(%edi),%edi %edi *(%esi) $0x4,%esi %edi,-16(%ebp) 8048470 add pop pop pop pop ret mov ret nop ... push mov push sub mov cmp je mov lea lea call mov sub cmp jne pop pop pop ret nop ... push mov push sub call pop add call pop pop leave ret $0xc,%esp %ebx %esi %edi %ebp (%esp),%ebx %ebp %esp,%ebp %ebx $0x4,%esp 0x80494f8,%eax $-1,%eax 80484bd $0x80494f8,%ebx 0x0(%esi),%esi 0x0(%edi),%edi *%eax -4(%ebx),%eax $0x4,%ebx $-1,%eax 80484b0 %eax %ebx %ebp %ebp %esp,%ebp %ebx $0x4,%esp 80484d0 %ebx $0x1108,%ebx 8048330 %ecx %ebx A.A. 2013–2014 4 / 18 Disassembly Instruction set istruzioni a lunghezza fissa ogni istruzione inizia ad un indirizzo multiplo della sua lunghezza istruzioni a lunghezza variabile disassembly difficoltoso il processo di disassembly può “desincronizzarsi” Srdjan Matic, Aristide Fattori Reverse engineering: disassembly A.A. 2013–2014 5 / 18 Disassembly Instruction set istruzioni a lunghezza fissa ogni istruzione inizia ad un indirizzo multiplo della sua lunghezza istruzioni a lunghezza variabile disassembly difficoltoso il processo di disassembly può “desincronizzarsi” Problemi interposizione di dati e istruzioni nello stesso address space (come distinguerli?) istruzioni di lunghezza variabile trasferimenti di controllo indiretti (function pointer, dynamic linking, jump table, . . . ) Srdjan Matic, Aristide Fattori Reverse engineering: disassembly A.A. 2013–2014 5 / 18 Disassembly Problemi2 Nella fase di compilazione si perdono: nomi delle variabili informazioni sui tipi concetto di “blocco” macro commenti Problemi3 riconoscere i limiti delle funzioni distinguere i parametri delle funzioni Srdjan Matic, Aristide Fattori Reverse engineering: disassembly A.A. 2013–2014 6 / 18 Disassembly Sintassi AT&T vs Intel $ objdump -M att -d /bin/ls ... push %ebp xor %ecx,%ecx mov %esp,%ebp sub $0x8,%esp mov %ebx,(%esp) mov 0x8(%ebp),%ebx mov %esi,0x4(%esp) mov 0xc(%ebp),%esi mov (%ebx),%edx mov 0x4(%ebx),%eax xor 0x4(%esi),%eax xor (%esi),%edx or %edx,%eax je 8049c60 <exit@plt+0x13c> mov %ecx,%eax mov (%esp),%ebx mov 0x4(%esp),%esi mov %ebp,%esp pop %ebp ret Srdjan Matic, Aristide Fattori $ objdump -M intel -d /bin/ls ... push ebp xor ecx,ecx mov ebp,esp sub esp,0x8 mov DWORD PTR [esp],ebx mov ebx,DWORD PTR [ebp+0x8] mov DWORD PTR [esp+0x4],esi mov esi,DWORD PTR [ebp+0xc] mov edx,DWORD PTR [ebx] mov eax,DWORD PTR [ebx+0x4] xor eax,DWORD PTR [esi+0x4] xor edx,DWORD PTR [esi] or eax,edx je 8049c60 <exit@plt+0x13c> mov eax,ecx mov ebx,DWORD PTR [esp] mov esi,DWORD PTR [esp+0x4] mov esp,ebp pop ebp ret Reverse engineering: disassembly A.A. 2013–2014 7 / 18 Disassembly Esempio 08048350 08048355 08048357 08048358 0804835F 08048366 0804836B 08048370 08048375 0804837A 0804837C 08048381 B8 FF C3 48 6F BA B9 BB B8 CD B8 C3 66 83 04 08 D0 65 72 0E 58 00 04 80 00 6C 6C 00 83 00 00 6C 64 00 04 00 00 6F 20 57 21 0A 0D 00 08 00 00 00 00 00 mov $0x8048366,%eax call *%eax ret (data) (data) mov $0xe,%edx mov $0x8048358,%ecx mov $0x0,%ebx mov $0x4,%eax int $0x80 mov $0x0,%eax ret Tecniche utilizzate linear sweep disassembly recursive traversal disassembly Srdjan Matic, Aristide Fattori Reverse engineering: disassembly A.A. 2013–2014 8 / 18 Linear sweep disassembly (e.g., objdump) inizia al primo byte del segmento testo procedi decodificando un’istruzione dopo l’altra $ objdump -D file assunzione: istruzioni memorizzate in locazioni adiacenti 08048350 08048355 08048357 08048358 08048359 0804835A 0804835B 0804835C 0804835D 08048360 08048362 08048365 0804836A 08048370 08048375 0804837A 0804837C 08048381 Srdjan Matic, Aristide Fattori B8 FF C3 48 65 6C 6C 6F 20 72 64 0D 00 BB B8 CD B8 C3 66 83 04 08 D0 57 6C 21 BA B9 00 04 80 00 6F 0A 0E 58 00 00 00 83 00 00 00 04 08 00 00 00 00 00 mov $0x8048366,%eax call *%eax ret dec %eax gs insb (%dx),%es:(%edi) insb (%dx),%es:(%edi) outsl %ds:(%esi),(%dx) and %dl,0x6f(%edi) jb 0x80483ce and %ecx,%fs:(%edx) or $0xeba,%eax add %bh,0x8(%ecx) mov $0x0,%ebx mov $0x4,%eax int $0x80 mov $0x0,%eax ret Reverse engineering: disassembly A.A. 2013–2014 9 / 18 Linear sweep disassembly (e.g., objdump) inizia al primo byte del segmento testo procedi decodificando un’istruzione dopo l’altra $ objdump -D file assunzione: istruzioni memorizzate in locazioni adiacenti 08048350 08048355 08048357 08048358 08048359 0804835A 0804835B 0804835C 0804835D 08048360 08048362 08048365 0804836A 08048370 08048375 0804837A 0804837C 08048381 Srdjan Matic, Aristide Fattori B8 FF C3 48 65 6C 6C 6F 20 72 64 0D 00 BB B8 CD B8 C3 66 83 04 08 D0 57 6C 21 BA B9 00 04 80 00 6F 0A 0E 58 00 00 00 83 00 00 00 04 08 00 00 00 00 00 mov $0x8048366,%eax call *%eax ret dec %eax IA-32 disassembly gs insb (%dx),%es:(%edi)è self-repairing insb (%dx),%es:(%edi) outsl %ds:(%esi),(%dx) and %dl,0x6f(%edi) jb 0x80483ce and %ecx,%fs:(%edx) or $0xeba,%eax add %bh,0x8(%ecx) mov $0x0,%ebx mov $0x4,%eax int $0x80 mov $0x0,%eax ret Reverse engineering: disassembly A.A. 2013–2014 9 / 18 Linear sweep disassembly IA-32: self-repairing disassembly Srdjan Matic, Aristide Fattori Reverse engineering: disassembly A.A. 2013–2014 10 / 18 Recursive traversal disassembly (e.g., IDA Pro) 1 2 inizia dall’entry point quando incontri un’istruzione di branch: determina possibili successori riprendi il disassemby a questi indirizzi 08048350 08048355 08048357 Srdjan Matic, Aristide Fattori B8 66 83 04 08 FF D0 C3 mov $0x8048366,%eax call *%eax ret Reverse engineering: disassembly A.A. 2013–2014 11 / 18 Control flow graph Cos’è? struttura fondamentale in program analysis grafo orientato in cui: nodi: computazioni (basic block) archi: possibili flussi di esecuzione Basic block sequenza non interrompibile di istruzioni singolo entry point singolo exit point Srdjan Matic, Aristide Fattori Reverse engineering: disassembly A.A. 2013–2014 12 / 18 B1: if n<=0 goto B2 False 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 int find max(int ∗v, int n) { unsigned int max, i; int ris; if(n <= 0) ris = −1; else { max = v[0]; i = 1; while(i < n) { if(v[i] > max) max = v[i]; i = i + 1; } ris = max; } return ris; True B3: max := v[0] i = 1 B2: ris := -1 B4: if i<n goto B5 True False B6: ris := max B5: if v[i]>max goto B7 True B7: max := v[i] False } B9: ret ris B8: i := i + 1 Call graph Cos’è? grafo orientato C = (R, F ) i nodi rappresentano procedure gli archi sono relazioni chiamante-chiamato se (ri , rj ) ∈ R, nel corso di ri si ha almeno una chiamata a rj Srdjan Matic, Aristide Fattori Reverse engineering: disassembly A.A. 2013–2014 14 / 18 Call graph Cos’è? grafo orientato C = (R, F ) i nodi rappresentano procedure gli archi sono relazioni chiamante-chiamato se (ri , rj ) ∈ R, nel corso di ri si ha almeno una chiamata a rj Esempio Srdjan Matic, Aristide Fattori P1 P3 P2 P4 Reverse engineering: disassembly A.A. 2013–2014 14 / 18 Hands on! $ wget http://security.di.unimi.it/sicurezza1314/demo{,.c} Memorabilia Regola #1 di IDA Everything you see and you do, you are working on the database. Changes are NOT automatically reflected to the binary. Regola #2 di IDA There are plenty of things you can do. But there is no undo. sub xxxx: a function at address xxxx loc xxxx: an instruction at address xxxx (byte|word|dword) xxxx: data at address xxxx var xx: local variable at EBP-xx arg n: argument at EBP+8+n https://www.hex-rays.com/products/ida/support/ freefiles/IDA_Pro_Shortcuts.pdf Srdjan Matic, Aristide Fattori Reverse engineering: disassembly A.A. 2013–2014 16 / 18 Homework #3 http://security.di.unimi.it/sicurezza1314/homeworks/homework3.tar.gz Due Oct 23 @ 23:59:59 Utilizzando IDA (!hexrays), reversare gli ELF scaricati. Ogni programma contiene una chiave che può essere individuata staticamente e confermata eseguendo il programma e fornendo in input la chiave. Scrivere una breve relazione (pdf/txt), contenente, ∀ programma: la chiave individuata come l’avete individuata/estratta descrizione alto livello del programma Srdjan Matic, Aristide Fattori Reverse engineering: disassembly A.A. 2013–2014 17 / 18 Bibliografia & Utilities G. Vigna Malware Detection, chapter Static Disassembly and Code Analysis C. Eagle The IDA Pro Book: The Unofficial Guide to the World’s Most Popular Disassembler Intel 64 and IA-32 Architectures Software Developers Manual Volume 2 (2A, 2B & 2C): Instruction Set Reference, A-Z http://ref.x86asm.net/coder32.html Srdjan Matic, Aristide Fattori Reverse engineering: disassembly A.A. 2013–2014 18 / 18
Documenti analoghi
Reverse engineering: disassembly
Reverse engineering: disassembly
Roberto Paleari
L`assembler INTEL IA32
EBP:
puntatore
alla
testa
del
frame
di
afvazione
ESP:
puntatore
alla
testa
della
pila
EIP:
instruc>on
pointer
CS,
...,
GS:
seleJori
...
Linux Assembly Language - Appunti
; write function
mov
ebx, STDOUT
; Arg1: file descriptor
mov
ecx, msg1
mov
edx, MSG1
int
80h
; syscall kernel to write
pop
ecx
; Ripristino ecx
mov
edx, [esp+8]