用于修改 ELF 二进制文件动态部分的工具

发布于 2024-09-08 12:51:45 字数 74 浏览 0 评论 0原文

是否有工具可以修改 ELF 二进制文件动态部分中的共享库条目?我想显式修改二进制文件中的共享库依赖项(即用自定义路径替换现有库的路径)

Is there a tool for modifying the shared library entries in the dynamic section of an ELF binary? I would like to explicitly modify the shared library dependencies in my binary (i.e. replace path to existing library with a custom path)

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

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

发布评论

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

评论(4

浮光之海 2024-09-15 12:51:45

用自定义路径替换现有库的路径

如果这是您自己的库,那么您可能会像这样链接它:

$ cc -o prog1 -l/full/path/to/libABC.so prog1.o

而不是正确的:

$ cc -o prog1 -L/full/path/to/ -lABC prog1.o

第一种方法告诉Linux链接器应用程序恰好需要该库,只应该使用该库并且不应该覆盖可能的。第二种方法告诉应用程序需要将安装在系统上某个位置的库,无论是在默认库路径中还是在 $LD_LIBRARY_PATH 指向的路径中(将在运行时查找)。 -L 仅在链接时使用。

否则,不要修补 ELF,而是首先检查是否可以使用符号链接替换库。这是常见的技巧:事后很难修改可执行文件,但更改符号链接点的位置却很容易。

replace path to existing library with a custom path

If this is your own library, then you probably linking it like that:

$ cc -o prog1 -l/full/path/to/libABC.so prog1.o

instead of the proper:

$ cc -o prog1 -L/full/path/to/ -lABC prog1.o

The first approach tells Linux linker that application needs precisely that library, only that library and no override should be possible. Second approach tells that application needs the library which would be installed somewhere on the system, either in the default library path or one pointed by the $LD_LIBRARY_PATH (would be looked up during run-time). -L is used only during link-time.

Otherwise, instead of patching the ELF, first check if you can substitute the library using a symlink. This is the usual trick: it is hard to modify executable afterward, but it is very easy to change where to the symlink points.

寻梦旅人 2024-09-15 12:51:45

patchelf 就是你想要的

$ patchelf --replace-needed LIB_ORIGIN  LIB_NEW  ELF_FILE

看到效果

$ readelf -d ELF_FILE

安装工具很简单:

$ sudo apt-get install patchelf readelf

patchelf is what you want

$ patchelf --replace-needed LIB_ORIGIN  LIB_NEW  ELF_FILE

To see the effect

$ readelf -d ELF_FILE

Install the tools is easy:

$ sudo apt-get install patchelf readelf
囚我心虐我身 2024-09-15 12:51:45

您可能需要检查LD_LIBRARY_PATH环境变量。

You may want to check the LD_LIBRARY_PATH environment variable.

绻影浮沉 2024-09-15 12:51:45

如果您通过 readelf 查看 Linux 中的 .dynsym 部分,您将看到类似以下内容:

1: 0000000000000000   163 FUNC    GLOBAL DEFAULT  UND fseek@GLIBC_2.2.5 (2)

它仅包含库的符号名称。但是,如果您包含动态加载程序信息,您会得到:

libc.so.6 => /lib/libc.so.6 (0x00002ba11da4a000)
    /lib64/ld-linux-x86-64.so.2 (0x00002ba11d82a000)

因此,如上所述,绝对最简单的事情(假设您这样做是为了调试,而不是永远)只是创建一个新会话,导出您的自定义路径在现有的 LD_LIBRARY_PATH 前面,然后从那里开始。

If you look at the .dynsym section in Linux via readelf, you'll just see something like:

1: 0000000000000000   163 FUNC    GLOBAL DEFAULT  UND fseek@GLIBC_2.2.5 (2)

which just contains a symbolic name of the library. However, if you include the dynamic loader info, you get:

libc.so.6 => /lib/libc.so.6 (0x00002ba11da4a000)
    /lib64/ld-linux-x86-64.so.2 (0x00002ba11d82a000)

So as mentioned, the absolute easiest thing to do (assuming you're doing this for debugging, and not forever) would just be to create a new session, export your custom path in front of the existing LD_LIBRARY_PATH, and go from there.

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