4

我正在尝试自学 C++ 中的缓冲区溢出和利用。我充其量是一个中级 C++ 人,所以请耐心等待。我已经学习了一些教程,但这里有一些示例代码来说明我的问题:

#include <string>
#include <iostream>

using namespace std; 

int main()
{
  begin:
  int authentication = 0;
  char cUsername[10], cPassword[10];
  char cUser[10], cPass[10];

  cout << "Username: ";
  cin >> cUser;

  cout << "Pass: ";
  cin >> cPass;

  strcpy(cUsername, cUser);
  strcpy(cPassword, cPass);

  if(strcmp(cUsername, "admin") == 0 && strcmp(cPassword, "adminpass") == 0)
  {
    authentication = 1;
  }
  if(authentication)
  {
    cout << "Access granted\n";
    cout << (char)authentication;
  } 
  else 
  {
    cout << "Wrong username and password\n";
  }

  system("pause");
  goto begin;
}

我知道这里有各种不好的 juju ,等等......无论如何,当我在andcin << String中输入太多字母(例如大量的A's )时,我只是从 Visual Studio 获得访问冲突。但是,如果我输入 20ish ,然后是一个空格,然后是另一个into ,它会跳过询问我(假设因为在空格字符导致上一次调用返回后它已被填充)并授予我访问权限。cUsercPassAAcUsercPasscin

在什么时候,以及为什么,数据会溢出到“身份验证”中,为什么它只在我有空间而不是在我有一百万个时才会发生A......当我使用空间时,我从来没有遇到过“访问冲突”在输入中cUser

4

6 回答 6

29

我稍微修改了您的程序以使其更具说明性:

#include <iostream>

int main( void )
{
 int authentication = 0;
 char cUsername[ 10 ];
 char cPassword[ 10 ];

 std::cout << "Username: ";
 std::cin >> cUsername;

 std::cout << "Pass: ";
 std::cin >> cPassword;

 if( std::strcmp( cUsername, "admin" ) == 0 && std::strcmp( cPassword, "adminpass" ) == 0 )
 {
  authentication = 1;
 }
 if( authentication )
 {
  std::cout << "Access granted\n";
  std::cout << ( char )authentication;
 }
 else
 {
  std::cout << "Wrong username and password\n";
 }

 return ( 0 );
}

我用 x64 编译器命令行 MS 编译器编译它,没有优化。所以现在我们有一个我们想要“破解”的 exe。我们使用 WinDbg(非常好的调试器)加载程序并查看反汇编(注意,为清楚起见,我提供了完整的调试信息):

00000001`3f1f1710 4883ec68        sub     rsp,68h
00000001`3f1f1714 488b0515db0300  mov     rax,qword ptr [Prototype_Console!__security_cookie (00000001`3f22f230)]
00000001`3f1f171b 4833c4          xor     rax,rsp
00000001`3f1f171e 4889442450      mov     qword ptr [rsp+50h],rax
00000001`3f1f1723 c744243800000000 mov     dword ptr [rsp+38h],0  // This gives us address of "authentication" on stack.
00000001`3f1f172b 488d156e1c0300  lea     rdx,[Prototype_Console!std::_Iosb<int>::end+0x78 (00000001`3f2233a0)]
00000001`3f1f1732 488d0d47f00300  lea     rcx,[Prototype_Console!std::cout (00000001`3f230780)]
00000001`3f1f1739 e8fdf9ffff      call    Prototype_Console!ILT+310(??$?6U?$char_traitsDstdstdYAAEAV?$basic_ostreamDU?$char_traitsDstd (00000001`3f1f113b)
00000001`3f1f173e 488d542428      lea     rdx,[rsp+28h] // This gives us address of "cUsername" on stack.
00000001`3f1f1743 488d0df6f00300  lea     rcx,[Prototype_Console!std::cin (00000001`3f230840)]
00000001`3f1f174a e823faffff      call    Prototype_Console!ILT+365(??$?5DU?$char_traitsDstdstdYAAEAV?$basic_istreamDU?$char_traitsDstd (00000001`3f1f1172)
00000001`3f1f174f 488d153e1c0300  lea     rdx,[Prototype_Console!std::_Iosb<int>::end+0x6c (00000001`3f223394)]
00000001`3f1f1756 488d0d23f00300  lea     rcx,[Prototype_Console!std::cout (00000001`3f230780)]
00000001`3f1f175d e8d9f9ffff      call    Prototype_Console!ILT+310(??$?6U?$char_traitsDstdstdYAAEAV?$basic_ostreamDU?$char_traitsDstd (00000001`3f1f113b)
00000001`3f1f1762 488d542440      lea     rdx,[rsp+40h] // This gives us address of "cPassword" on stack.
00000001`3f1f1767 488d0dd2f00300  lea     rcx,[Prototype_Console!std::cin (00000001`3f230840)]
00000001`3f1f176e e8fff9ffff      call    Prototype_Console!ILT+365(??$?5DU?$char_traitsDstdstdYAAEAV?$basic_istreamDU?$char_traitsDstd (00000001`3f1f1172)
00000001`3f1f1773 488d15321c0300  lea     rdx,[Prototype_Console!std::_Iosb<int>::end+0x84 (00000001`3f2233ac)]
00000001`3f1f177a 488d4c2428      lea     rcx,[rsp+28h]
00000001`3f1f177f e86c420000      call    Prototype_Console!strcmp (00000001`3f1f59f0)
00000001`3f1f1784 85c0            test    eax,eax
00000001`3f1f1786 751d            jne     Prototype_Console!main+0x95 (00000001`3f1f17a5)
00000001`3f1f1788 488d15291c0300  lea     rdx,[Prototype_Console!std::_Iosb<int>::end+0x90 (00000001`3f2233b8)]
00000001`3f1f178f 488d4c2440      lea     rcx,[rsp+40h]
00000001`3f1f1794 e857420000      call    Prototype_Console!strcmp (00000001`3f1f59f0)
00000001`3f1f1799 85c0            test    eax,eax
00000001`3f1f179b 7508            jne     Prototype_Console!main+0x95 (00000001`3f1f17a5)
00000001`3f1f179d c744243801000000 mov     dword ptr [rsp+38h],1
00000001`3f1f17a5 837c243800      cmp     dword ptr [rsp+38h],0
00000001`3f1f17aa 7426            je      Prototype_Console!main+0xc2 (00000001`3f1f17d2)
00000001`3f1f17ac 488d15151c0300  lea     rdx,[Prototype_Console!std::_Iosb<int>::end+0xa0 (00000001`3f2233c8)]
00000001`3f1f17b3 488d0dc6ef0300  lea     rcx,[Prototype_Console!std::cout (00000001`3f230780)]
00000001`3f1f17ba e87cf9ffff      call    Prototype_Console!ILT+310(??$?6U?$char_traitsDstdstdYAAEAV?$basic_ostreamDU?$char_traitsDstd (00000001`3f1f113b)
00000001`3f1f17bf 0fb6542438      movzx   edx,byte ptr [rsp+38h]
00000001`3f1f17c4 488d0db5ef0300  lea     rcx,[Prototype_Console!std::cout (00000001`3f230780)]
00000001`3f1f17cb e825f9ffff      call    Prototype_Console!ILT+240(??$?6U?$char_traitsDstdstdYAAEAV?$basic_ostreamDU?$char_traitsDstd (00000001`3f1f10f5)
00000001`3f1f17d0 eb13            jmp     Prototype_Console!main+0xd5 (00000001`3f1f17e5)
00000001`3f1f17d2 488d15ff1b0300  lea     rdx,[Prototype_Console!std::_Iosb<int>::end+0xb0 (00000001`3f2233d8)]
00000001`3f1f17d9 488d0da0ef0300  lea     rcx,[Prototype_Console!std::cout (00000001`3f230780)]
00000001`3f1f17e0 e856f9ffff      call    Prototype_Console!ILT+310(??$?6U?$char_traitsDstdstdYAAEAV?$basic_ostreamDU?$char_traitsDstd (00000001`3f1f113b)
00000001`3f1f17e5 33c0            xor     eax,eax
00000001`3f1f17e7 488b4c2450      mov     rcx,qword ptr [rsp+50h]
00000001`3f1f17ec 4833cc          xor     rcx,rsp
00000001`3f1f17ef e8bc420000      call    Prototype_Console!__security_check_cookie (00000001`3f1f5ab0)
00000001`3f1f17f4 4883c468        add     rsp,68h
00000001`3f1f17f8 c3              ret

现在,既然我们知道 x64 堆栈是如何工作的,我们就可以开始“hacking”了。RSP是堆栈指针,函数堆栈是高于RSP值的地址(堆栈增长为较小的地址)。所以,我们看到RSP+28h是 where cUsername, RSP+38hisauthenticationRSP+40his cPassword,其中 28h、38h 和 40h 是十六进制偏移量。这是一个小图像来说明:

-----> old RSP value // Stack frame of caller of `main` is above, stack frame of main is below 

      16 bytes of
      "cPassword"
+40h
     8 bytes of "authentication"
+38h
      16 bytes of
      "cUsername"
+28h   


-----> RSP value = old RSP-68h

我们从这里看到了什么?我们看到编译器在 8 字节边界上对齐数据:例如,我们要求为 分配 10 个字节cUsername,但我们得到了 16 个字节 - x64 位堆栈自然地在 8 字节边界上对齐。这意味着为了写入authentication我们需要写入cUsername更多的 16 个字节(符号)。还要注意,编译器放得更cPasswordauthentication——我们不能只authenticationcPassword,覆盖cUsername

所以现在我们运行我们的程序并输入Username: 0123456789abcdef10123456789abcdef= 16 个字节,下一个1将被放入authentication- 对我们来说足够好:

Username: 0123456789abcdef1
Pass: whatever
Access granted
1
于 2012-01-09T02:03:16.193 回答
2

它正在覆盖您的authentication变量。这意味着authentication即使在您的代码检查用户名和密码之前也是如此。要检查这一点,请在检查之前打印出身份验证。

我将进一步扩展:当您键入一个很长的用户名时,该长用户名将由您的strcpy, 复制到cUsername. 该变量cUsername紧随其后authentication,因此被过长的用户名覆盖。

如果您键入一个非常长的用户名,那么(再次)身份验证变量将被覆盖。但现在堆栈中更靠前的项目(例如返回值)也将被覆盖。如果您的程序在堆栈上覆盖得太高,那么它将被严重破坏并且任何事情都可能发生。此时您基本上执行随机代码。

于 2012-01-09T01:10:10.137 回答
1

如果你使用std::string,你会发现你的程序会简单得多:

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";
  }      
}

编辑:

关于您最近的问题,我认为默认情况下,cin >> string将在第一个空格字符(即空格)处停止读取,因此如果您输入空格,cin将在它损坏任何数据之前停止,因此您不会遇到访问冲突. 如果您希望能够读取空格,那么您需要getline像我上面那样使用,以便它可以读取整行文本,包括空格。

于 2012-01-09T01:27:24.380 回答
1

Proposed solution to detect NULL pointer and buffer overflow in memcpy, memset, strcpy before hand and print out the location (file:line) where the problem occurs:

http://htvdanh.blogspot.com/2016/09/proposed-solution-to-detect-null.html

于 2016-10-07T15:03:03.867 回答
0

当你编译一个程序时,编译器决定如何在内存中排列你的数据。如果程序包含未经检查的数组访问,它可能会被利用,因为知道内存中数据排列的恶意用户可以弄清楚如何覆盖关键变量。

但是,C++ 并没有让您完全控制事物在堆栈上的布局方式。局部变量可以以任何顺序出现在内存中。

要了解缓冲区溢出漏洞,您必须反汇编您的程序并深入研究机器代码。这将为您提供堆栈的布局,包括最重要的返回地址。

顺便说一句,“访问冲突”来自您的程序,而不是 Visual Studio。在进入逆向工程之前,您可能需要更多的“正向”工程经验。

于 2012-01-09T01:04:20.520 回答
0

因为您将 char 设置为 10 个位置(包括NULL字符),所以任何更长的内容都会溢出到Authentication. 有很多方法可以解决这个问题,最明显的是让字符变大。其他方法是限制用户在注册时输入的字母数量(假设这是在网站服务器上)。您还可以使用strlen(cUsername)来计算 char 数组的长度,并要求以更少的字符重新输入用户名。
编辑:
好的。所以你想要做的是使用getline(cin,cUser)cin在第一次出现空白时停止读取。getline()将读取带或不带空格的整个字符串。

于 2012-01-09T01:18:04.023 回答