【问题标题】:Buffer overflow - set relevant text to be printed缓冲区溢出 - 设置要打印的相关文本
【发布时间】:2019-01-24 18:25:12
【问题描述】:
#include <unistd.h>

char shellcode[] = "???";

int main(int argc, char* argv[]) {
    int* ret;
    ret = (int*) &ret + 2;
    (*ret) = (int) shellcode;
}

启动此程序后,我必须更改 shellCode 变量以打印今年。 我以ret 指向其先前地址+ 2 的方式理解此代码,并且它具有shellCode 数组开始的地址的值。 但是在这种情况下堆栈看起来如何?我只是不明白main() 中代码的含义以及缓冲区溢出如何在这里工作。

编辑 好吧,我的问题并不像我想象的那么明显。我只能更改shellcode 中的内容,我不能向此代码添加任何内容(更改shellcode 除外)并且输出应为2019(将其视为输出所有其他数字,即2000,数字将被硬编码)

【问题讨论】:

  • 需要指出的是,ret 指向它自己的地址加 2(而不是基于它之前指向的地址)。
  • 我不明白您对main() 中的三行代码的目标是什么,但是获取当前年份的字符串版本,然后将其打印到stdout 非常简单,并且可以通过使用 strftime 函数来完成。

标签: c buffer-overflow shellcode


【解决方案1】:

我必须更改 shellCode 变量以在启动此程序后打印今年。

使用strftime,可以使用以下代码将shellcode 填充为当前年份,(如果shellcode 创建时有足够的空间。):

char shellcode[] = "0000"; //increase space for 4 characters + nul.

...  

time_t t;
struct tm* tm_info;
time(&t);
tm_info = localtime(&t);
strftime(shellcode, 5, "%Y", tm_info); //populate shellcode with year.
printf("%s\n", shellcode);             //write current year to stdout

正如我在 cmets 中提到的,我不确定你在 main 函数中做了什么,但如果你的意图是获取当前年份的整数版本,你可以菊花链 strftime 的字符串结果 使用 strtol 函数将字符串转换为数字:

char *temp = null;
long int year = strtol(shellcode, &temp, 0); 

但一定要检查转换错误。例如:

if(temp == shellcode|| ((year == LONG_MIN || year == LONG_MAX) && errno == ERANGE))
{
    //handle error
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多