【问题标题】:call to sprintf crashing in assembly调用 sprintf 在程序集中崩溃
【发布时间】:2014-05-04 23:30:01
【问题描述】:

我正在尝试调用sprintf 来格式化字符串并将结果存储在堆栈变量中。但是,我的尝试惨遭失败,并立即崩溃。

sub esp, 0x100                                  ;Allocate 256 bytes on the stack.
push dword[RequestedFile]                       ;push string2
push dword[Host]                                ;push string1
push dword[GetHeader]                           ;push format   "String1: %s, String2: %s"
push dword[ebp - 0x04]                          ;push buffer/stack variable
call [sprintf]                                  ;store string in buffer
add esp, 0x10                                   ;restore stack

push dword[ebp - 0x04]                          ;push the stack variable.
push StringFormat                               ;push the format
call [printf]                                   ;print the new string.
add esp, 0x08                                   ;restore the stack

add esp, 0x100                                  ;destroy the stack variable.

任何想法我做错了什么?

【问题讨论】:

  • 它在哪里崩溃?你的add esp, 0x04不应该是add esp, 0x08吗?如果您可以访问 C 编译器,您可能希望查看 C 中的等效示例并查看它生成的内容。
  • 它在调用sprintf 时崩溃是的,你是对的,它应该是0x08。已修复,但在调用 sprintf 时仍会崩溃。我尝试了 gcc explorer: gcc.godbolt.org-m32 标志,但我不明白 lea 和它使用的指令。我更喜欢使用push。它似乎也不叫sprintf
  • lea 是一个非常基本、重要的指令。如果您打算使用组装,您应该熟悉它。

标签: assembly x86 fasm


【解决方案1】:

您使用[ebp-4] 就好像它是指向缓冲区的指针,而实际上它只是缓冲区最后 4 个字节中的随机内存垃圾(假设尚未从堆栈中分配任何其他内容)。如果您想继续使用[ebp-4],您还需要从堆栈中分配它并将其初始化为地址。例如:

sub esp, 0x104                  ;Allocate 256 bytes buffer and 4 bytes pointer
mov dword[ebp - 0x04], esp      ;store address of buffer in local variable
push dword[RequestedFile]       ;push string2
push dword[Host]                ;push string1
push dword[GetHeader]           ;push format   "String1: %s, String2: %s"
push dword[ebp - 0x04]          ;push buffer/stack variable
call [sprintf]                  ;store string in buffer
add esp, 0x10                   ;restore stack

push dword[ebp - 0x04]          ;push the stack variable.
push StringFormat               ;push the format
call [printf]                   ;print the new string.
add esp, 0x08                   ;restore the stack

add esp, 0x104                  ;destroy the stack variables.

【讨论】:

  • 对我不起作用。不过我会+1。这说得通。编辑:工作。没有改变任何东西:S
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-13
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多