可以使用“dlopen”加载和映射静态 ELF 可执行文件(不是库)吗?

发布于 2025-01-09 09:32:18 字数 359 浏览 2 评论 0原文

如果文件anexe是静态编译的,没有符号,没有导出,ELF,可以使用dlopen("anexe", RTLD_LAZY)将其映射到内存吗?

我的目标不是能够引用符号——没有符号。相反,我的目标是能够在给定地址(固定的,而不是 PIC)的情况下调用其函数,并在给定地址的情况下读取其数据。我本来计划通过 mmap 完成这一切,但意识到 dlopen 虽然不是为此设计的,但也许能够为我完成这一切。

dlopen可以这样用吗?

当我尝试时,我得到dlerror无法动态加载可执行文件 - 有任何解决方法吗?

If file anexe is static compiled, no symbols, no exports, ELF, can dlopen("anexe", RTLD_LAZY) be used to map it into memory?

My goal isn't to be able to reference symbols -- there are no symbols. Rather, my goal is to be able to call its functions given their addresses (which are fixed, not PIC), and to read its data given address. I was planning on doing this all via mmap, but realized that dlopen, though not designed for thhis, might be able to do it all for me.

Can dlopen be used that way?

When I try it, I get dlerror: cannot dynamically load executable - is there any workaround for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天暗了我发光 2025-01-16 09:32:18

如果文件 anexe 是静态编译的,没有符号,没有导出,ELF,可以使用 dlopen("anexe", RTLD_LAZY) 将其映射到内存吗?

不(但见下文)。

我的目标不是能够引用符号——没有符号。相反,我的目标是能够在给定地址(固定的,而不是 PIC)的情况下调用其函数,并在给定地址的情况下读取其数据。

请注意,非 PIE 可执行文件只能加载到其链接的地址。您可以通过检查程序头来找到哪个地址(以编程方式或使用 readelf -Wl)。

一旦您知道它应该加载到哪个地址(并假设该地址尚未被您自己的可执行文件占用),那么您可以执行一系列 mmap 调用来将该可执行文件“加载”到内存中,之后你可以调用它的函数并读取它的数据。

一个复杂的问题是可执行文件的初始化程序不会运行(您没有调用它的 _start,您也不想这样做),因此其中的函数可能无法正确运行,可能会崩溃等

。例如,给定此二进制文件:

$ echo "int main() { return 0; }" | gcc -xc - -static -no-pie -o a.out
$ readelf -Wl a.out | egrep 'Type|LOAD'

  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  LOAD           0x000000 0x0000000000400000 0x0000000000400000 0x000518 0x000518 R   0x1000
  LOAD           0x001000 0x0000000000401000 0x0000000000401000 0x07fd01 0x07fd01 R E 0x1000
  LOAD           0x081000 0x0000000000481000 0x0000000000481000 0x026660 0x026660 R   0x1000
  LOAD           0x0a7928 0x00000000004a8928 0x00000000004a8928 0x0059c8 0x007298 RW  0x1000

您需要使用 MAP_FIXED 执行 4 个 mmap 调用(每个 LOAD 段调用一次)。 mmap 的地址、文件偏移量、大小和保护参数是显而易见的。

最后一段的 mmap 参数可能不是:您需要将地址和偏移量向下舍入到 Align,并将大小扩展 0x928来解释该舍入。

If file anexe is static compiled, no symbols, no exports, ELF, can dlopen("anexe", RTLD_LAZY) be used to map it into memory?

No (but see below).

My goal isn't to be able to reference symbols -- there are no symbols. Rather, my goal is to be able to call its functions given their addresses (which are fixed, not PIC), and to read its data given address.

Note that a non-PIE executable can only be loaded at the address it was linked at. You can find which address that is by examining its program headers (programmatically, or with readelf -Wl).

Once you know which address it should be loaded at (and assuming that address is not already occupied by your own executable), then you can perform a series of mmap calls to "load" that executable into memory, and after that you can call its functions and read its data.

One complication is that the executable's initializers will not have run (you didn't call its _start, nor would you want to), and so functions in it may not run correctly, may crash, etc.

For example, given this binary:

$ echo "int main() { return 0; }" | gcc -xc - -static -no-pie -o a.out
$ readelf -Wl a.out | egrep 'Type|LOAD'

  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  LOAD           0x000000 0x0000000000400000 0x0000000000400000 0x000518 0x000518 R   0x1000
  LOAD           0x001000 0x0000000000401000 0x0000000000401000 0x07fd01 0x07fd01 R E 0x1000
  LOAD           0x081000 0x0000000000481000 0x0000000000481000 0x026660 0x026660 R   0x1000
  LOAD           0x0a7928 0x00000000004a8928 0x00000000004a8928 0x0059c8 0x007298 RW  0x1000

you would need to perform 4 mmap calls (one for each LOAD segment), with MAP_FIXED. The address, file offset, size and protection arguments for mmap are obvious.

The mmap arguments for the last segment may not be: you would need to round address and offset down to Align, and extend the size by 0x928 to account for that rounding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文