gpt4 book ai didi

c - 怎么用 smashed stack 启动代码?

转载 作者:太空宇宙 更新时间:2023-11-04 04:30:45 25 4
gpt4 key购买 nike

我对 C 语言有非常初步的了解(尽管我确实了解一般的编程概念)。我的任务是创建一个缓冲区溢出,它会产生一些东西(比如访问未授权区域、免费资金等),而不仅仅是让程序崩溃。

我已经尝试过不同大小的缓冲区,并且总能使程序“崩溃”,但我无法让它启动任何代码(即/bin/su)。我处理这个问题不正确吗?

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <limits.h>
#include <string.h>
#define BUFSIZE 20

int main() {
int month=12;
int day=31;
int year=2016;
int confirm = 0;
double dollars = 5.00;
char *sitenum="97871";
char acctnum[BUFSIZE];

printf("Welcome to the Acme AP-AR System. This is the Accounts Receivable module. \n");
/* Gathering date information */
printf("Please enter the month of transaction as an integer value (2 digits). \n");
printf("For example, July would be 07, December would be 12. Please input the month: ");
for (;;) { /* Start of month input validation loop */
scanf("%d", &month);
if(month>=1 && month<=12) {
printf("Validated. \n");
break;
}
else {
printf("Please enter a value between 1 and 12! \n");
continue;
}
} /* End of month input validation loop */
printf("\nPlease enter the day of transaction as an integer value (2 digits). \n");
printf("For example, the 3rd would be 03, the 25th would be 25. Please input the day: ");
for (;;) { /* Start of day input validation loop */
scanf("%d", &day);
if(day>=1 && day<=31) {
printf("Validated. \n");
break;
}
else {
printf("Please enter a value between 1 and 31! \n");
continue;
}
} /* End of day input validation loop */

/* Gathering sender account number */
printf("\nPlease enter the sender Account Number: ");
scanf("%s", acctnum);

/* Gathering transaction amount */
printf("\nPlease enter the USD amount (including cents) received: $ ");
scanf("%lf", &dollars);

/* Confirming data entry */
printf("\nTransaction information.\n Date: %d-%d-%d \n", month,day,year);
printf("Account: %s-%s \n", sitenum, acctnum);
printf(" Amount: $ %.2lf \n", dollars);
printf("\nProcess transaction information? (Yes=1/No=0) ");
for (;;) { /* Start of confirmation validation loop */
scanf("%d", &confirm);
if(confirm==1) {
printf("Transaction processed. \n");
break;
}
else {
printf("Transaction voided! \n");
break;
}
} /* End of confirmation validation loop */

return (EXIT_SUCCESS);
}

执行时,如果您输入 25 个字符表示月份中的第几天,程序将一直执行到结束。只有在最后一次输入之后,它才会因堆栈粉碎错误而终止。恐怕我正在尝试做一些无法完成的事情,但是一天(从字面上看,过去 8 小时)的 Google 搜索并没有产生我能够使用的示例。

有人可以将我推向不同的方向,使我更接近我想要实现的目标吗?谢谢。

最佳答案

您需要深入了解目标架构(x86、x86-64 等)才能实现这一目标。一种典型的方法是仔细构造缓冲区溢出的内容,以便它 1) 包含您希望在输入数据被重新解释为机器指令时运行的代码,以及 2) 覆盖堆栈帧的返回地址以便它跳转进入您的代码,而不是返回到调用函数。

我不愿意提供实际执行此操作的代码,但确实有可能做到。

编辑:顺便说一句,我不认为该作业旨在要求实际运行任意代码。我根据您发布的代码猜测您应该只覆盖堆栈的一部分,这样看起来您正在访问不同的“sitenum”。这绝对是可能的,因为 sitenum 指针将存储在堆栈中的 acctnum 之后(至少通常是这样)。因此,如果您仔细设计缓冲区溢出,您可以将 sitenum 指针更改为指向其他地方。例如,(假设 sitenum 指针紧跟在堆栈中的 acctnum 之后),您可以向 acctnum 输入 1 个额外字符,空终止字符将覆盖 sitenum 指针的最低有效字节,这很可能指向一个然后不同的位置。

但在我看来,这是一个糟糕的分配,因为 1) 堆栈可以根据大量因素进行不同的安排,以及 2) 大多数现代开发环境将默认添加运行时检查以防止这种堆栈损坏.例如,在 MS Visual C++ 中,您必须特意禁用基本运行时检查和缓冲区安全检查功能以避免异常。

无论如何,希望对您有所帮助。

关于c - 怎么用 smashed stack 启动代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36536612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com