选择性调用写包装器

发布于 2024-11-17 11:38:26 字数 419 浏览 8 评论 0原文

我已经拦截了 write 库函数来重定向写入,但只想对 uae 包装器进行一些写入,而其他(用于写入套接字)应该转到原始 libc 函数。已尝试使用 dlsym 但似乎不起作用。

已使用 LD-PRELOAD 环境变量

希望得到帮助

编辑: 部分代码

int call_execute()
{
.....
    static ssize_t (*real_write)(int,const void*,size_t) = NULL;
...

    real_write= (size_t(*)(int,const void*,size_t)dlsym(RTLD_NEXT,"write");

...
    real_write(sockfd,argcalls[i],strlen(argcalls[i]));

}

I have intercepted the write library function to redirect writes but want only a few writes to uae the wrapper whereas others(used for writing to sockets) should go to the original libc function. Have tried using dlsym but does no seem to work.

have used the LD-PRELOAD environment variable

Would appreciate help

An Edit:
A portion of the code

int call_execute()
{
.....
    static ssize_t (*real_write)(int,const void*,size_t) = NULL;
...

    real_write= (size_t(*)(int,const void*,size_t)dlsym(RTLD_NEXT,"write");

...
    real_write(sockfd,argcalls[i],strlen(argcalls[i]));

}

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

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

发布评论

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

评论(2

硪扪都還晓 2024-11-24 11:38:26
#include <unistd.h>
#include <stdio.h>
#include <dlfcn.h>

typedef ssize_t (*readf)(int, void *, size_t);

ssize_t
read(int fd, void *buf, size_t count) {
    readf p = dlsym(RTLD_NEXT, "read");
    printf("passing read with %d bytes\n", count);
    return p(fd, buf, count);
}

$gcc -W -Wall -shared -o /tmp/libpre.so test.c -ldl

$env LD_PRELOAD=/tmp/libpre.so cat /dev/null

上面应该产生如下输出:

传递 read 32768 字节

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

typedef ssize_t (*readf)(int, void *, size_t);

ssize_t
read(int fd, void *buf, size_t count) {
    readf p = dlsym(RTLD_NEXT, "read");
    printf("passing read with %d bytes\n", count);
    return p(fd, buf, count);
}

$gcc -W -Wall -shared -o /tmp/libpre.so test.c -ldl

$env LD_PRELOAD=/tmp/libpre.so cat /dev/null

Above should produce output like this:

passing read with 32768 bytes

樱花坊 2024-11-24 11:38:26

如果 dlsym(RTLD_NEXT, "write") 不返回 libc 函数,您可以显式声明您想要的库,例如

void *handle = dlopen("libc.so.6", RTLD_LAZY);
if (!handle) puts(dlerror()), exit(1);
typeof(&write) real_write = dlsym(handle, "write");

- 但如果您没有在首先在主程序中放置并定义您的写入包装器 - 然后 RTLD_NEXT 应该可以工作。

If dlsym(RTLD_NEXT, "write") doesn't return the libc function, you can explicitly state the library you want, e. g.

void *handle = dlopen("libc.so.6", RTLD_LAZY);
if (!handle) puts(dlerror()), exit(1);
typeof(&write) real_write = dlsym(handle, "write");

- but easier perhaps it would be if you didn't use LD_PRELOAD in the first place and defined your write wrapper in the main program - then RTLD_NEXT should work.

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