- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我们每周都会进行一次计算机系统漏洞测试,测试题如下:
The below function is part of a program that is running on a 32-bit x86 system; the compiler does not change the order of variables on the stack.
void function(char *input) {
int i = 1;
char buffer[8];
int j = 2;
strcpy(buffer,input);
printf("%x %x %s\n",i,j,buffer);
}
What is the minimum length of a string passed to the function through the input parameter that can crash the application?
a)10 b)11 c)12 d)13
我写了一个 main
函数来调用 void function(...
并使用 gcc -m32 test.c -o test
编译程序,因为我在 64 位计算机上。下面是主要功能:
int main(int argc, char *argv[]) {
function(argv[1]);
return 1;
}
并使用输入进行测试:
~/Dir:./test 1234567
1 2 1234567
~/Dir:./test 12345678
1 2 12345678
~/Dir:./test 123456789
1 2 123456789
*** stack smashing detected ***: <unknown> terminated
Aborted (core dumped)
一旦我输入 123456789
作为参数,就会检测到堆栈粉碎,所以这个问题的答案应该是 9
但没有选择 9
的选项。上面问题的正确答案应该是什么?我如何知道可以使上述应用程序崩溃的最小字符串长度?
最佳答案
你会得到 9 个字符的“Stack smashing detected”,因为你的编译器确实对堆栈上的变量重新排序。 GCC 可以,即使在 -O0
。为防止这种情况,请将变量放在一个结构中。
#include <stdio.h>
#include <string.h>
struct variables {
int i;
char buffer[8];
int j;
};
void function(char *input) {
struct variables s;
s.i = 1;
s.j = 2;
strcpy(s.buffer, input);
printf("%x %x %s\n", s.i, s.j, s.buffer);
}
int main(int argc, char *argv[]) {
function(argv[1]);
return 0;
}
在关闭优化的情况下编译它,否则编译器很可能会优化 s
本身。
$ ./a.out 1234567
1 2 1234567
$ ./a.out 12345678
1 0 12345678
$ ./a.out 123456789
1 39 123456789
$ ./a.out 1234567890
1 3039 1234567890
$ ./a.out 1234567890a
1 613039 1234567890a
$ ./a.out 1234567890ab
1 62613039 1234567890ab
$ ./a.out 1234567890abc
1 62613039 1234567890abc
*** stack smashing detected ***: <unknown> terminated
[2] 6086 abort (core dumped) ./a.out 1234567890abc
现在您可以看到发生了什么。最多 7 个字符,加上空终止符,该字符串适合 8 字节缓冲区。对于 8 个字符,字符串开始溢出到内存中的下一个内容,即 j
。在 32 位小端机器上,组成 j
的字节的值为 {0x02, 0x00, 0x00, 0x00}。对于 8 到 11 个字符,字符串逐渐接管 j
。
在 12 个字符处,空终止符会覆盖内存中 s
之后的任何内容。在我的测试中,内存中的这个字节的值恰好为 0,因此没有发生比覆盖 j
更糟糕的事情。在 13 个字符处,字符串 c
的最后一个字符覆盖该字节,堆栈保护检测到该字节,因为该字节实际上是堆栈金丝雀的一部分。
在我的构建中,导致崩溃的字符数是 13。但是,这是因为 j
之后恰好有一个空字节。鉴于练习的假设,可能使应用程序崩溃所需的字符数为12。此时,strcpy
调用会写入函数的本地存储,这可能是一个未映射的地址。
为了视觉引用,这是 strcpy
调用之前内存的内容:
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| 01 | 00 | 00 | 00 | ?? | ?? | ?? | ?? | ?? | ?? | ?? | ?? | 02 | 00 | 00 | 00 | 00 | ?? |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
^-i ^-buffer ^-j ^-stack canary
如果我用 gcc -O0 -fno-stack-protector
编译,它需要 21 个字节才能在我的平台上实际导致崩溃,大概是因为这是覆盖返回地址所需要的。练习(我没看过,我不知道它有多难):借助调试器并借助汇编代码和一些 x86 ABI 文档,找出其中的内容(帧指针?对齐间隙?)。
关于c - 可能使某些应用程序崩溃的字符串的最小长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57645610/
如何使用 SPListCollection.Add(String, String, String, String, Int32, String, SPListTemplate.QuickLaunchO
我刚刚开始使用 C++ 并且对 C# 有一些经验,所以我有一些一般的编程经验。然而,似乎我马上就被击落了。我试过在谷歌上寻找,以免浪费任何人的时间,但没有结果。 int main(int argc,
这个问题已经有答案了: In Java 8 how do I transform a Map to another Map using a lambda? (8 个回答) Convert a Map>
我正在使用 node + typescript 和集成的 swagger 进行 API 调用。我 Swagger 提出以下要求 http://localhost:3033/employees/sear
我是 C++ 容器模板的新手。我收集了一些记录。每条记录都有一个唯一的名称,以及一个字段/值对列表。将按名称访问记录。字段/值对的顺序很重要。因此我设计如下: typedef string
我需要这两种方法,但j2me没有,我找到了一个replaceall();但这是 replaceall(string,string,string); 第二个方法是SringBuffer但在j2me中它没
If string is an alias of String in the .net framework为什么会发生这种情况,我应该如何解释它: type JustAString = string
我有两个列表(或字符串):一个大,另一个小。 我想检查较大的(A)是否包含小的(B)。 我的期望如下: 案例 1. B 是 A 的子集 A = [1,2,3] B = [1,2] contains(A
我有一个似乎无法解决的小问题。 这里...我有一个像这样创建的输入... var input = $(''); 如果我这样做......一切都很好 $(this).append(input); 如果我
我有以下代码片段 string[] lines = objects.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.No
这可能真的很简单,但我已经坚持了一段时间了。 我正在尝试输出一个字符串,然后输出一个带有两位小数的 double ,后跟另一个字符串,这是我的代码。 System.out.printf("成本:%.2
以下是 Cloud Firestore 列表查询中的示例之一 citiesRef.where("state", ">=", "CA").where("state", "= 字符串,我们在Stack O
我正在尝试检查一个字符串是否包含在另一个字符串中。后面的代码非常简单。我怎样才能在 jquery 中做到这一点? function deleteRow(locName, locID) { if
这个问题在这里已经有了答案: How to implement big int in C++ (14 个答案) 关闭 9 年前。 我有 2 个字符串,都只包含数字。这些数字大于 uint64_t 的
我有一个带有自定义转换器的 Dozer 映射: com.xyz.Customer com.xyz.CustomerDAO customerName
这个问题在这里已经有了答案: How do I compare strings in Java? (23 个回答) 关闭 6 年前。 我想了解字符串池的工作原理以及一个字符串等于另一个字符串的规则是
我已阅读 this问题和其他一些问题。但它们与我的问题有些无关 对于 UILabel 如果你不指定 ? 或 ! 你会得到这样的错误: @IBOutlet property has non-option
这两种方法中哪一种在理论上更快,为什么? (指向字符串的指针必须是常量。) destination[count] 和 *destination++ 之间的确切区别是什么? destination[co
This question already has answers here: Closed 11 years ago. Possible Duplicates: Is String.Format a
我有一个Stream一个文件的,现在我想将相同的单词组合成 Map这很重要,这个词在 Stream 中出现的频率. 我知道我必须使用 collect(Collectors.groupingBy(..)
我是一名优秀的程序员,十分优秀!