【问题标题】:What does this syntax *((unsigned int *)(buffer+i)) mean in C这个语法 *((unsigned int *)(buffer+i)) 在 C 中是什么意思
【发布时间】:2021-05-12 22:25:08
【问题描述】:

这是代码:

char *command, *buffer;

command = (char *) malloc(200);
bzero(command, 200);

strcpy(command, "./notesearch \'");
buffer = command + strlen(command);
for(int i=0; i < 160; i+=4) {
    *((unsigned int *)(buffer+i)) = ret; // What does this syntax mean?
}

您可以在此处获取完整代码 => https://raw.githubusercontent.com/intere/hacking/master/booksrc/exploit_notesearch.c

请帮助我,我是初学者。

【问题讨论】:

  • 什么是缓冲区?
  • buffer的类型是什么?
  • 这需要太多的猜测。请提供更多上下文,可能是minimal reproducible example
  • 该代码假定sizeof(unsigned int)4。 C 不保证这一点,而且并非总是如此。 (在 Windows 中并非如此。)
  • 该代码假定缓冲区的地址与unsigned int 适当对齐。我想知道是否真的是这样。即使不是,它仍然可以在 x86 和 x64 上运行,尽管速度较慢。它会在其他机器上彻底失败。

标签: c buffer low-level-code


【解决方案1】:

从内到外阅读。这里我们必须假设buffer 是指向某个内存区域或数组元素的指针。 你有:

  • buffer + 1 ==> 下一个内存位置或下一个数组元素的地址
  • (unsigned int *)(buffer+i) ==> 将结果指针转换为 unsigned int 类型的指针。
  • *((unsigned int *)(buffer+i)) ==> 取消引用 unsigned int 指出的(获取值)。
  • *((unsigned int *)(buffer+i)) = ret; ==> 将值赋给变量ret

在 C 中,计算表达式时,总是从内到外。

【讨论】:

    【解决方案2】:

    这会将unsigned int ret 写入地址buffer+i

    *((unsigned int *)(buffer+i)) = ret
    
    • buffer+ichar*(指向 char 的指针)
    • (unsigned int *)(buffer+i) 中的 (unsigned int *) 将指向 char 的指针转换为指向 unsigned int 的指针。这称为演员表
    • 最后* 将此指针取消引用到unsigned int 并将ret 写入该地址。

    请注意,根据您的硬件架构,这可能会因为对齐问题而失败。

    【讨论】:

      猜你喜欢
      • 2016-09-06
      • 1970-01-01
      • 1970-01-01
      • 2018-05-22
      • 2011-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-23
      相关资源
      最近更新 更多