【问题标题】:Hacking: how to perform buffer overflow attack?黑客:如何执行缓冲区溢出攻击?
【发布时间】:2016-06-06 02:10:28
【问题描述】:

我被困在一个黑客练习中。

程序在执行时显示:

Build your own string!

Usage:
  ./4 length command...

Each command consist of a single character followed by its index.

Example:
  ./4 11 h0 e1 l2 l3 o4 w6 o7 r8 l9 d10

如何使用命令行参数执行缓冲区溢出攻击?

atoi 将字符串参数 str 转换为整数(int 类型)。

memset 将字符 c(一个无符号字符)复制到参数 str 所指向的字符串的前 n 个字符。

感谢您的洞察力!

#include <alloca.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

static void usage(const char *argv0) {
        printf("Build your own string!\n");
        printf("\n");
        printf("Usage:\n");
        printf("  %s length command...\n", argv0);
        printf("\n");
        printf("Each command consist of a single character followed by its index.\n");
        printf("\n");
        printf("Example:\n");
        printf("  %s 11 h0 e1 l2 l3 o4 w6 o7 r8 l9 d10\n", argv0);
        exit(1);
}

int main(int argc, char **argv) {
        char *buffer;
        unsigned short buffersize, i, index, length;

        if (argc < 2) usage(argv[0]);

        length = atoi(argv[1]);
        if (length <= 0) {
                fprintf(stderr, "bad length\n");
                return 1;
        }

        buffersize = length + 1;
        buffer = alloca(buffersize);
        memset(buffer, ' ', buffersize);
        buffer[buffersize - 1] = 0;

        for (i = 2; i < argc; i++) {
                if (strlen(argv[i]) < 2) {
                        fprintf(stderr, "bad command \"%s\"\n", argv[i]);
                        return 1;
                }

                index = atoi(argv[i] + 1);
                if (index >= length) {
                        fprintf(stderr, "bad index in command \"%s\"\n", argv[i]);
                        return 1;
                }

                buffer[index] = argv[i][0];
        }

        printf("%s\n", buffer);
        return 0;
}

【问题讨论】:

  • 您被否决了,因为您的问题本质上是“为我做作业”。你应该问一个关于你不明白的具体问题。
  • @Adam 我不明白如何使用命令行参数来执行缓冲区溢出。代码看起来很安全。
  • 它不安全,因为它写入缓冲区而不检查长度。它很容易被利用,因为该缓冲区是用alloca 分配的。
  • 你被另一个学生淘汰了:stackoverflow.com/questions/35671279/…

标签: c buffer-overflow


【解决方案1】:

据我所知,缓冲区溢出是您覆盖缓冲区内存地址以执行代码或简单地指向另一个地址的好功能!在您的脚本中,我相信要进入下一个级别,您必须将内存地址指向 printf("%s\n", buffer);。为此,请使用 gdb 调试代码并将内存地址覆盖到 printf 函数的许多位。一个不错的教程是http://www.tenouk.com/Bufferoverflowc/Bufferoverflow4.html。但是您应该阅读有关 c 编程和源代码中所有易受攻击的代码的信息。哪个是printf("im很脆弱");

【讨论】:

    猜你喜欢
    • 2021-12-03
    • 2011-11-12
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    相关资源
    最近更新 更多