Automation Systems Group
Secure Software Programming and Vulnerability Analysis
Christopher Kruegel chris@auto.tuwien.ac.at http://www.auto.tuwien.ac.at/~chris
Secure Software Programming 2 Automation Systems Group
Secure Software Programming and Vulnerability Analysis Christopher - - PDF document
Automation Systems Group Secure Software Programming and Vulnerability Analysis Christopher Kruegel chris@auto.tuwien.ac.at http://www.auto.tuwien.ac.at/~chris Automation Systems Group Buffer Overflows Secure Software Programming 2
Automation Systems Group
Secure Software Programming 2 Automation Systems Group
Secure Software Programming 3 Automation Systems Group
Secure Software Programming 4 Automation Systems Group
– coding flaws because of
– mostly relevant for C / C++ programs – not in languages with automatic memory management – these use
Secure Software Programming 5 Automation Systems Group
Secure Software Programming 6 Automation Systems Group
Secure Software Programming 7 Automation Systems Group
– Stack segment
– Data segment
– Code (Text) segment
Top of Memory
Secure Software Programming 8 Automation Systems Group
–
–
–
– Phrack 57
– stack, heap, BSS (e.g., PLT)
Secure Software Programming 9 Automation Systems Group
– Intel, Motorola, SPARC, MIPS
– stack pointer – SP – points to last stack element or first free slot
– pushed on top of stack as consequence of function calls – address of current frame stored in processor register
– used to conveniently reference local variables
Secure Software Programming 10 Automation Systems Group
caller code
callee code
Secure Software Programming 11 Automation Systems Group
strcpy, strcat, gets, fgets, sprintf ..
Secure Software Programming 12 Automation Systems Group
Secure Software Programming 13 Automation Systems Group
Secure Software Programming 14 Automation Systems Group
Secure Software Programming 15 Automation Systems Group
void main(int argc, char **argv) { char *name[2]; name[0] = “/bin/sh“; name[1] = NULL; execve(name[0], &name[0], &name[1]); exit(0); } int execve(char *file, char *argv[], char *env[])
“/bin/sh“
“/bin/sh“, NULL
NULL
Secure Software Programming 16 Automation Systems Group
Secure Software Programming 17 Automation Systems Group
Secure Software Programming 18 Automation Systems Group
Secure Software Programming 19 Automation Systems Group
Secure Software Programming 20 Automation Systems Group
popl %esi jmp call_addr Shell Code call jmp_addr + 1 /bin/sh0000
Secure Software Programming 21 Automation Systems Group
Secure Software Programming 22 Automation Systems Group
Secure Software Programming 23 Automation Systems Group
Secure Software Programming 24 Automation Systems Group
Secure Software Programming 25 Automation Systems Group
unsigned long get_sp(void) { __asm__(“movl %esp, %eax“); }
Secure Software Programming 26 Automation Systems Group
Secure Software Programming 27 Automation Systems Group
Secure Software Programming 28 Automation Systems Group
Secure Software Programming 29 Automation Systems Group
Secure Software Programming 30 Automation Systems Group
int main() { jmp_buf env; int i; if (setjmp(env) != 0) { printf(”i = %d\n", i); exit(0); } else { printf(”i = %d\n", i); f1(env); } return 0; } void f2(jmp_buf e) { if (check == error) { longjmp(e, ERROR2); /* unreachable */ } else return; } void f1(jmp_buf e) { if (check == error) { longjmp(e, ERROR1); /* unreachable */ } else f2(e); }
Secure Software Programming 31 Automation Systems Group
typedef int __jmp_buf[6]; # define JB_BX 0 # define JB_SI 1 # define JB_DI 2 # define JB_BP 3 # define JB_SP 4 # define JB_PC 5 # define JB_SIZE 24 /* Calling environment, plus possibly a saved signal mask. */ typedef struct __jmp_buf_tag { __jmp_buf __jmpbuf; /* Calling environment. */ int __mask_was_saved; /* Saved the signal mask? */ __sigset_t __saved_mask; /* Saved signal mask. */ } jmp_buf[1]; Secure Software Programming 32 Automation Systems Group
longjmp(env, i) -> movl i, %eax /* return i */ movl env.__jmpbuf[JB_BP], %ebp /* restore base ptr */ movl env.__jmpbuf[JB_SP], %esp /* restore stack ptr */ jmp (env.__jmpbuf[JB_PC]) /* jump to stored PC */
Secure Software Programming 33 Automation Systems Group
Secure Software Programming 34 Automation Systems Group
– locate shell code in memory, NULL bytes, NOP sledge