你能识别这个 C 代码中的任何漏洞吗
can you identify any vulerabilty in this C code
有人可以帮助我查看这段代码并判断是否存在漏洞吗?我是 C 语言的新手,也刚开始研究道德黑客。
如果有,谁能告诉我这个漏洞以及如何利用它?
我得到了提示
hint: Your task is to inject a command line argument such that function4
gets called, instead of function3
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int function1(int x, int y, int z)
{
int result_func1;
result_func1 = x + y + z;
return result_func1;
}
int function2(int x, int y, char *input_string)
{
int result_func2;
char buffer[20];
strcpy(buffer, input_string);
printf("your input string %s is copied in the buffer \n", input_string);
result_func2 = x - y;
return result_func2;
}
void function3(int result1, int result2)
{
printf("The result of function 1 is %d\n", result1);
printf("The result of function 1 is %d\n", result1);
}
void function4(void)
{
printf("The function never gets called is \n");
exit(-1);
}
int main(int argc, char *argv[])
{
int result1;
int result2;
result1 = function1(5, 10, 15);
result2 = function2(20, 8, argv[1]);
function3(result1, result1);
}
在 function2 的 strcpy 行上有一个简单的缓冲区溢出。如果您提供的参数超过 20 个字符,堆栈将开始被覆盖,您将能够将函数的 EIP 设置为 return 到 function4。
请注意,这只会在当今关闭了针对 bof 的保护的特殊环境中如此容易地工作。
提出正确输入的过程可能类似于
- 找一个调试器(比如 gdb)
- 给这个程序添加一个由 20 个无关紧要的字符组成的参数,然后是 \x01\x02\x03\x04...(实际字节数,而不是 c 的文字字符串)
- 运行它与调试器
- 注意堆栈被覆盖后 EIP 指向的位置,这些是您输入中的字符,您将使用这些字符将 EIP 设置为函数 4 的地址
有人可以帮助我查看这段代码并判断是否存在漏洞吗?我是 C 语言的新手,也刚开始研究道德黑客。 如果有,谁能告诉我这个漏洞以及如何利用它?
我得到了提示
hint: Your task is to inject a command line argument such that
function4
gets called, instead offunction3
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int function1(int x, int y, int z)
{
int result_func1;
result_func1 = x + y + z;
return result_func1;
}
int function2(int x, int y, char *input_string)
{
int result_func2;
char buffer[20];
strcpy(buffer, input_string);
printf("your input string %s is copied in the buffer \n", input_string);
result_func2 = x - y;
return result_func2;
}
void function3(int result1, int result2)
{
printf("The result of function 1 is %d\n", result1);
printf("The result of function 1 is %d\n", result1);
}
void function4(void)
{
printf("The function never gets called is \n");
exit(-1);
}
int main(int argc, char *argv[])
{
int result1;
int result2;
result1 = function1(5, 10, 15);
result2 = function2(20, 8, argv[1]);
function3(result1, result1);
}
在 function2 的 strcpy 行上有一个简单的缓冲区溢出。如果您提供的参数超过 20 个字符,堆栈将开始被覆盖,您将能够将函数的 EIP 设置为 return 到 function4。 请注意,这只会在当今关闭了针对 bof 的保护的特殊环境中如此容易地工作。
提出正确输入的过程可能类似于
- 找一个调试器(比如 gdb)
- 给这个程序添加一个由 20 个无关紧要的字符组成的参数,然后是 \x01\x02\x03\x04...(实际字节数,而不是 c 的文字字符串)
- 运行它与调试器
- 注意堆栈被覆盖后 EIP 指向的位置,这些是您输入中的字符,您将使用这些字符将 EIP 设置为函数 4 的地址