#include <iostream> #include <cstring> using namespace std; int main (int argc, char** argv) { if (argc != 3) { return 1; } char user[10]; char pass[10]; int granted(0); strcpy(user, argv[1]); strcpy(pass, argv[2]); if (strcmp(user, "admin") == 0 && strcmp(pass, "pw") == 0) { granted = 1; } if (granted) { cout << "Access granted\n"; } else { cout << "Access denied\n"; } return 0; }
If you are using gcc, your need to disable stack protection:
g++ -fno-stack-protector -o overflow main.cxx
If you choose your user name long enough you cause an overflow and change the value of the variable "granted". In my case the user name needs to be at least 13 characters long.
~ » ./overflow abc 123 Access denied ~ » ./overflow admin 123 Access denied ~ » ./overflow admin pw Access granted ~ » ./overflow AAAAAAAAAAAAA AA Access granted
If you build your program with debugging symbols, you can print the value of the variables. For the last case, you will have an output like (granted != 0):
(gdb) print user $5 = "AAAAAAAAAA" (gdb) print pass $6 = "AA\000\000\000\000\000\000\220\006" (gdb) print granted $7 = 65
This very simple way of exploiting overflows can be avoided very easily, e.g. length checks, and only works if you disable stack protection. However, it shows the very basic idea behind overflow exploitation and should convince you to write code that is overflow safe.
No comments:
Post a Comment