首页 > 解决方案 > 有没有办法使用特定的 C 函数/符号作为 nm 的输出

问题描述

我正在尝试分析已编译的文件以用于网络安全学习目的,并希望使用特定功能。

这是输出nm --defined-only ./compiled_file

0000000000200d98 d _DYNAMIC
0000000000200f88 d _GLOBAL_OFFSET_TABLE_
0000000000000ba0 R _IO_stdin_used
0000000000000d64 r __FRAME_END__
0000000000000bfc r __GNU_EH_FRAME_HDR
0000000000201010 D __TMC_END__
0000000000201010 B __bss_start
0000000000201000 D __data_start
00000000000007d0 t __do_global_dtors_aux
0000000000200d90 t __do_global_dtors_aux_fini_array_entry
0000000000201008 D __dso_handle
0000000000200d88 t __frame_dummy_init_array_entry
0000000000200d90 t __init_array_end
0000000000200d88 t __init_array_start
0000000000000b90 T __libc_csu_fini
0000000000000b20 T __libc_csu_init
0000000000201010 D _edata
0000000000201018 B _end
0000000000000b94 T _fini
0000000000000660 T _init
0000000000000710 T _start
0000000000201010 b completed.7696
0000000000201000 W data_start
0000000000000740 t deregister_tm_clones
0000000000000810 t frame_dummy
0000000000000a92 T main
000000000000081a T function_I_would_like_to_use                 \\ << this one
0000000000000780 t register_tm_clones

我主要使用 Python(我对 C/C++ 知之甚少,只是基础知识),所以,自然地,我一直试图在 Python 中使用 ctypes 库来处理这个文件,用该文件创建一个 CDLL 对象,但是据我所知,上面的函数名称都没有出现在对象或其属性的 dir() 中。

我从与此类似的内容开始:https ://book.pythontips.com/en/latest/python_c_extension.html#ctypes然后越陷越深,dir()/__dict__试图找到我认可的东西,但没有运气。

正如你所看到的,我试图在没有任何机器代码知识的情况下对这个 ELF 进行逆向工程,但希望我能在这个过程中学到一些东西哈哈!出于练习的目的,我也宁愿在没有外部(第三方)库的情况下这样做。

的输出file ./compiled_file是:

./compiled_file: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=cf62f3afa6f99f98a863d44932d2e
0f9f8594e71, not stripped

所以,我的主要问题是,如上所述,有没有办法在 Python 或其他方式中调用 , 等nm列出的“已定义”函数之一?objdump我读到 BuildID[sha1] 可用于调试。我可能出于我的目的需要这个吗?

我希望这个问题不要太宽泛。本质上,我主要是在寻找“是”或“否”,也许在正确的方向上稍微点头,至于​​我是否可以使用上面的信息调用该函数!

编辑:

感谢第一个非常快速的回答和评论,我dlopen/dlsym在 C 中一直在搞乱。脚本如下(改编自此处):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>

int main(int argc, char** argv) {
    void *handle;
    void (*func_in_question)(const char*);
    handle = dlopen("./compiled_file", RTLD_LAZY);
    if (!handle) {
        /* fail to load the library */
        fprintf(stderr, "Error: %s\n", dlerror());
        return EXIT_FAILURE;
    }
    *(void**)(&func_in_question) = dlsym(handle, "function_I_would_like_to_use");
    if (!func_in_question) {
        /* no such symbol */
        fprintf(stderr, "Error: %s\n", dlerror());
        dlclose(handle);
        return EXIT_FAILURE;
    }
    func_in_question(argv[1]);
    dlclose(handle);
    return EXIT_SUCCESS;
}

似乎按预期工作,除了它返回:

Error: ./compiled_file: undefined symbol: function_I_would_like_to_use

(事实上​​,同样的错误导致我问这个问题¯\_(ツ)_/¯)

然而,最初的问题已经得到解答。

标签: pythonc++reverse-engineeringsymbolssecurity

解决方案


对的,这是可能的。毕竟,在共享库中导出符号的目的是能够使用它们。在 C 中,您可以通过将库链接到应用程序(实际上不是 python 的选项)或运行时加载库并找到所需的符号(在 linux 上:dlopen、dlsym)来做到这一点。手册页示例显示了如何在 C 中执行此操作。


推荐阅读