How are buffer overflows used to exploit computers?
How is one able to execute arbitrary code simply by causing stack or heap overflows?
I understand that
Virtually all modern processors when calling a subroutine, pushes the return address on the same area as the local data(stack). For routines that doesn't check the upper limit of a variable(on particular the strcpy function), instruction address redirection(buffer overflow) can occur.
void make(char *me)
{
char sandwich[4]; // local data, this is in stack. the buffer for data is too small
strcpy(sandwich, me);
puts(sandwich);
// implicit "return;" the return instruction(RET on Intel) instructs the processor to implicitly pop an address from stack then resume execution on that address
}
void main()
{
// calling a subroutine (CALL on Intel) implicitly instructs the processor to push the next instruction's address(getchar line) on stack before jumping to make.
make("Love Not War");
getchar();
puts("This will not execute. The address to next instruction(getchar) gets overwritten with Not War");
}
"Also, must the 3rd party's malicious code be written in the target processors assembly language?" Yes
Stack overflow can occur from normally running program, example is recursive routines(function that calls itself) with overlooked terminating condition. The stack area will get filled with numerous local variables on stack plus the returning addresses.