端口Win32 DLL钩到Linux [英] Port Win32 DLL hook to Linux

查看:113
本文介绍了端口Win32 DLL钩到Linux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序(NWShader)挂钩到第二个程序的OpenGL调用(NWN)做后处理效果和whatnot。

I have a program (NWShader) which hooks into a second program's OpenGL calls (NWN) to do post-processing effects and whatnot.

NWShader最初是为Windows(通常是现代版本(win32))构建的,并使用DLL导出(获取Windows加载它并获取一些OpenGL函数)和Detours挂钩到其他功能)。我使用的技巧,Win将检查当前目录中的任何DLL之前检查sysdir,所以它加载我的。我有DLL重定向与此方法:

NWShader was originally built for Windows, generally modern versions (win32), and uses both DLL exports (to get Windows to load it and grab some OpenGL functions) and Detours (to hook into other functions). I'm using the trick where Win will look in the current directory for any DLLs before checking the sysdir, so it loads mine. I have on DLL that redirects with this method:

#pragma comment(linker, "/export:oldFunc=nwshader.newFunc)

将它们发送到我自己的DLL中的不同的命名函数,然后进行任何处理,并调用从系统DLL的原始函数。

To send them to a different named function in my own DLL. I then do any processing and call the original function from the system DLL.

我需要端口NWShader到Linux(NWN存在两种口味)。据我所知,我需要做的是一个共享库(.so文件)如果这是在NWN可执行文件之前预加载(我发现一个shell脚本来处理这个),我的函数将被调用。唯一的问题是我需要调用原始函数(我会使用各种DLL动态加载方法,我想),需要能够做Detour的钩子的内部函数。

I need to port NWShader to Linux (NWN exists in both flavors). As far as I can tell, what I need to make is a shared library (.so file). If this is preloaded before the NWN executable (I found a shell script to handle this), my functions will be called. The only problem is I need to call the original function (I would use various DLL dynamic loading methods for this, I think) and need to be able to do Detour-like hooking of internal functions.

目前我在Ubuntu 9.10 x64(带有32位编译器标志)。我在Google上找不到很多帮助,但我不知道* nix社区是指什么,我可以编写C ++,但是我更习惯于Windows。作为OpenGL,唯一需要修改为与Linux兼容的部分是挂钩代码和调用。有没有一个简单的方法来做到这一点,或者它将涉及重新创建Detours和动态加载原始的函数地址?

At the moment I'm building on Ubuntu 9.10 x64 (with the 32-bit compiler flags). I haven't been able to find much on Google to help with this, but I don't know exactly what the *nix community refers to it as. I can code C++, but I'm more used to Windows. Being OpenGL, the only part the needs modified to be compatible with Linux is the hooking code and the calls. Is there a simple and easy way to do this, or will it involve recreating Detours and dynamically loading the original function addresses?

推荐答案

库预加载可以通过 LD_PRELOAD 完成。从那里你想看看 dlopen dlsym 调用以获得原始库中的实际函数。这是所有如果你想手工做的事情。

The library preloading can be done through LD_PRELOAD. From there you want to look at the dlopen and dlsym calls to get at the actual functions in the original library. This is all if you want to do things by hand.

你也可以看看修改 ltrace 这样你就提供了函数hook(通过 -e 标志),让letrrt为你处理簿记。

You can also look at modifying ltrace in a way such that you provide the functions to hook (via the -e flag) and let ltrace handle the bookkeeping for you.

用手做的例子:

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

int (*orig_puts)(const char *);

int puts (const char * str) {
    void * handle = dlopen("/lib/libc.so.6", RTLD_NOW | RTLD_GLOBAL);
    orig_puts = dlsym(handle,"puts");
    fprintf (stderr,"I have hooked your puts\n");
    return orig_puts(str);
}

使用

#include <stdio.h>

int main () {
    puts ("Hello World");
    return 0;
}

您会得到以下结果:

$ ./a.out
Hello World
$ LD_PRELOAD=./libhook ./a.out
I have hooked your puts
Hello World

这篇关于端口Win32 DLL钩到Linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆