简体   繁体   English

如何修补 3rd party.so 文件,以访问非导出符号 (C++)

[英]How to patch 3rd party .so file, to access non-exported symbol (C++)

I have some binary .fic files in a proprietary format, I have a wd250hf64.so from this vendor that contains a C++ method CComposanteHyperFile::HExporteXML(wchar_t* const path)我有一些专有格式的二进制.fic文件,我有一个来自该供应商的wd250hf64.so ,其中包含 C++ 方法CComposanteHyperFile::HExporteXML(wchar_t* const path)

that I can see using nm我可以看到使用 nm

$ nm  --demangle   wd250hf64.so  --defined-only 


0000000000118c90 t CComposanteHyperFile::HExporteXML(wchar_t const*)

the unmangled version _ZN20CComposanteHyperFile11HExporteXMLEPKw is identical to what I have using my local g++ version未损坏的版本_ZN20CComposanteHyperFile11HExporteXMLEPKw与我使用本地 g++ 版本的版本相同

readelf gives readelf 给

readelf -Ws wd250hf64.so  | grep _ZN20CComposanteHyperFile11HExporteXMLEPK

 19684: 0000000000118c90   119 FUNC    LOCAL  DEFAULT   11 _ZN20CComposanteHyperFile11HExporteXMLEPKw

now I try writing a very simple program现在我尝试编写一个非常简单的程序

class CComposanteHyperFile {
    public:
    static void  HExporteXML(wchar_t const*);
};



int main() {
    CComposanteHyperFile::HExporteXML(L"file.fic");
    return 0;
}

but when I compile it with g++ toto.cpp -L. -l:wd250hf64.so但是当我用g++ toto.cpp -L. -l:wd250hf64.so g++ toto.cpp -L. -l:wd250hf64.so

I got toto.cpp:(.text+0x10): undefined reference to 'CComposanteHyperFile::HExporteXML(wchar_t const*)'我得到了toto.cpp:(.text+0x10): undefined reference to 'CComposanteHyperFile::HExporteXML(wchar_t const*)'

I don't have more luck with dlopen我对dlopen没有更多的运气

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

int
main(int argc, char **argv)
{
    void *handle;
    void (*exportXML)(wchar_t const*);
    char *error;

   handle = dlopen("wd250hf64.so", RTLD_LAZY);
   *(void **) (&exportXML) = dlsym(handle, "_ZN20CComposanteHyperFile11HExporteXMLEPKw");

   if ((error = dlerror()) != NULL)  {
        fprintf(stderr, "%s\n", error);
        exit(EXIT_FAILURE);
    }

    dlclose(handle);
    exit(EXIT_SUCCESS);
}
gcc -rdynamic -o foo toto.c -ldl
./foo
wd250hf64.so: undefined symbol: _ZN20CComposanteHyperFile11HExporteXMLEPKw

I understand that as it is not shown by nm with --extern-only it may be that this symbol is not "exported" and so it's not supposed to work normally我知道,由于 nm 没有用--extern-only显示它,可能是这个符号没有“导出”,所以它不应该正常工作

my question is我的问题是

What is the hackish way of making the program to compile, by all means, even if it means manually patching the.so file?无论如何,即使这意味着手动修补.so文件,使程序编译的黑客方式是什么?

If you really want to be able to get at that symbol in any way possible, you could try to get its address relative to that of a known exported symbol, assuming that they're in the same section.如果您真的希望能够以任何可能的方式获取该符号,则可以尝试获取其相对于已知导出符号的地址,假设它们位于同一部分中。 For example, I made a simple dummy library with one exported function and one non-exported function.例如,我制作了一个简单的虚拟库,其中一个导出的 function 和一个未导出的 function。

0x0000000000003890    12 FUNC    GLOBAL DEFAULT    16 function_we_exported
0x00000000000038a0    12 FUNC    LOCAL  HIDDEN     16 function_we_forgot_to_export

Because this library was contrived just for this answer, the non-exported function happens to be right next to it, 0x10 after function_we_exported .因为这个库只是为了这个答案而设计的,所以未导出的 function 恰好在它旁边,在function_we_exported之后的0x10处。 Using this information, we can hackily do this.使用这些信息,我们可以巧妙地做到这一点。

const auto address = reinterpret_cast<char*>(dlsym(library, "function_we_exported"));
reinterpret_cast<your_function_type>(address + 0x10)(...);

As you can probably tell, this is pretty hacky, but if you have no other choice, I suppose this can be a way.正如您可能会说的那样,这很hacky,但是如果您别无选择,我想这可能是一种方法。 Another way may be to patch the library and forcibly export it.另一种方法可能是修补库并强制导出它。 Considering that they show up as GLOBAL / LOCAL and DEFAULT / HIDDEN , it's probably a flip of a flag or something, but I don't know how to do that at the moment.考虑到它们显示为GLOBAL / LOCALDEFAULT / HIDDEN ,这可能是一个标志或其他东西的翻转,但我现在不知道该怎么做。 I'll update this answer if I do.如果我这样做,我会更新这个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM