简体   繁体   English

缓冲区溢出随机大小的缓冲区? (得到)

[英]Buffer overflow with random size buffer? (gets)

I'm trying to learn more about ways to exploit/prevent buffer overflow in my programs.我正在尝试更多地了解在我的程序中利用/防止缓冲区溢出的方法。 I know that the following code is vulnerable if the size is constant, but what if the size is random every time?我知道如果大小恒定,下面的代码很容易受到攻击,但是如果大小每次都是随机的怎么办? Is there still a way to grab it from the stack and somehow alter the amount of overflow characters dynamically?有没有办法从堆栈中抓取它并以某种方式动态改变溢出字符的数量?

void vulnFunc(int size){
    char buffer[size];
    gets(buffer);
    // Arbitrary code
}

Consider fgets(buf, sizeof(buf)-1, stdin);考虑 fgets(buf, sizeof(buf)-1, stdin);

with stdin and a size that matches your buffer.使用标准输入和与您的缓冲区匹配的大小。 It will be safe.这将是安全的。 There are other possibilities, such as a loop with getc(stdin): when the data becomes larger than your buffer you can realloc().还有其他可能性,例如带有 getc(stdin) 的循环:当数据变得大于缓冲区时,您可以重新分配 ()。

It depends on the variable that is used to represent the array, if its of type char[] or char *.它取决于用于表示数组的变量,如果它的类型是 char[] 或 char *。 let me explain why:让我解释一下原因:

  1. for char[], the variable name represents an array and the sizeof operator returns the size of the array in the memory (number of cell * sizeof(type)), so basicly you can get the number of cells using the following call:对于 char[],变量名表示一个数组, sizeof运算符返回数组在内存中的大小(单元格数 * sizeof(type)),因此基本上您可以使用以下调用获取单元格数:

    sizeof(array)/sizeof(array[0])大小(数组)/大小(数组[0])

  2. for char*, the variable is a pointer which holds the value of the first cell of the array, sizeof(array) in this case will return the size of pointer in memory that is 8Byte for 64bit architecture, it has nothing with the array so you cant get the information from this kind of variable.对于 char*,变量是一个指针,它保存数组的第一个单元格的值,在这种情况下,sizeof(array) 将返回内存中指针的大小,对于 64 位体系结构为 8Byte,它与数组无关您无法从此类变量中获取信息。 Maybe you could store the size of the allocated buffer in memory but I don't know if it suits your needs.也许您可以将分配的缓冲区的大小存储在内存中,但我不知道它是否适合您的需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM