【问题标题】:inserting code into elf将代码插入精灵
【发布时间】:2023-03-26 04:36:01
【问题描述】:

我想在汇编中编写代码,将自己置于给定的 ELF 中。我的代码如下:

func_start:
; Getting file descriptor, and additional code here
mov eax, 4; Write Sys_call
mov ebx, [fileDesc]
mov ecx, func_start
mov edx, func_end - func_start
func_end:

但我也希望文件(在版本之后)能够做同样的事情,但为此我必须将我的代码编写为与位置无关。为了在运行时获取 func_start 标签的地址,我所做的一切都失败了。

有什么想法吗?

编辑:我实际上要问的是:如何以与位置无关的方式使用我的标签?

【问题讨论】:

标签: assembly x86 elf


【解决方案1】:

当你调用一个函数时,指令指针被压入堆栈;您可以像这样利用它:

func_start:
    ; Call uses a relative address so it's fine
    call real_start
pushed_addr:
    ; We don't want to recurse infinitely once real_start returns
    ret
real_start:
    ; Get the pushed instruction pointer from the stack
    mov ecx, dword [esp]
    ; It points to pushed_addr, adjust it so that it points to func_start
    sub ecx, pushed_addr-func_start

    ; Now the address of func_start should be in ecx
    ; (The length is still func_end - func_start, of course)

    ; This returns to pushed_addr, which returns to wherever func_start was called
    ret
func_end:

我测试了这是否正确地获取了地址,但我实际上并没有尝试移动代码 - 希望我没有错过任何会搞砸的东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    相关资源
    最近更新 更多