C++ Buffer Overflow

前端 未结 6 1224
攒了一身酷
攒了一身酷 2020-12-28 11:13

I\'m trying to teach myself about buffer overflows and exploitation in C++. I\'m an intermediate C++ guy, at best, so bear with me. I\'ve followed a few tutorials, but here\

6条回答
  •  渐次进展
    2020-12-28 11:58

    If you use std::string, you'll find that your program will be much simpler:

    int main()
    {
      bool authenticated = false;
    
      while(!authenticated)
      {
        string username;
        string password;
    
        cout << "Username: ";
        getline(cin, username); // you may want to read input differently
    
        cout << "Pass: ";
        getline(cin, password); // same as above
    
        // you'll need to check cin.fail() to see whether the stream
        // had failed to read data, and exit the loop with "break".
    
        if(username == "admin" && password == "adminpass")
        {
          authenticated = true;
        }
        else
        {
          cout << "Wrong username and password, try again\n";
        }
      }
    
      if(authenticated)
      {
        cout << "Access granted\n";
      }      
    }
    

    Edit:

    With regards to your recent question, I think by default, cin >> string will stop reading at the first whitespace character (i.e. space), so if you input a space, cin will stop before it corrupts any data, and so you don't get the access violation. If you want to be able to read spaces, then you'll need to use getline like I have above so that it will read the entire line of text, spaces included.

提交回复
热议问题