gpt4 book ai didi

c++ - Safari 和 getaddrinfo 设置

转载 作者:太空宇宙 更新时间:2023-11-04 14:16:22 27 4
gpt4 key购买 nike

我编写了一个小型动态库,用于插入对 getaddrinfo 和连接的调用。我在 Firefox 和 Safari 上使用 DYLD_INSERT_LIBRARIES 插入这个库来劫持对 www.apple.com 的请求并将它们发送到 www.microsoft.com。该代码适用于 Firefox,但当 Safari 调用我插入的函数时,它会忽略重定向。

我想知道是否有人了解 Safari 忽略重定向的原因,以及是否有办法(可能通过 addrinfo 标志)让 Safari 像 Firefox 一样运行。我的代码 list 如下。

// -*- mode: c++ -*-

#include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <dlfcn.h>
#include <dns_sd.h>
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
#include <cstring>
#include <string>

typedef int (*connect_type) (int, struct sockaddr const*, socklen_t);
typedef int (*getaddrinfo_type)(
const char*, const char*, struct addrinfo const*, addrinfo**);

connect_type real_connect = 0;
getaddrinfo_type real_getaddrinfo = 0;

int
do_getaddrinfo_impl(const char* nodename, const char* servname,
struct addrinfo const* hints, struct addrinfo** ppai)
{
int result = real_getaddrinfo (nodename, servname, hints, ppai);
if(result != 0) {
fprintf(stderr, "! real_getaddrinfo error on %s: %s\n", nodename, gai_strerror(result));
}

return result;
}

int
do_getaddrinfo(const char* nodename, const char* servname,
struct addrinfo const* hints, struct addrinfo** ppai)
{
if (0 == nodename) {
return do_getaddrinfo_impl(nodename, servname, hints, ppai);
}

if (hints->ai_family == AF_INET6) {
printf ("# do_getaddrinfo : IPv6 resolution\n");
return do_getaddrinfo_impl(nodename, servname, hints, ppai);
}

if ( hints->ai_flags & AI_PASSIVE
|| hints->ai_flags & AI_CANONNAME
|| hints->ai_flags & AI_NUMERICHOST
|| hints->ai_flags & AI_NUMERICSERV) {
printf ("# do_getaddrinfo : unsupported resolution flags\n");
return do_getaddrinfo_impl (nodename, servname, hints, ppai);
}

if ((hints->ai_flags & AI_V4MAPPED) && !(hints->ai_flags & AI_ALL)) {
printf ("# do_getaddrinfo : unsupported IPv6-mapping\n");
return do_getaddrinfo_impl (nodename, servname, hints, ppai);
}

// redirect requests for www.apple.com to microsoft at 65.55.57.80 (0x41373950)
if(0 == strcmp(nodename, "www.apple.com")) {
addrinfo* pai = (addrinfo*)calloc (1, sizeof (addrinfo));

pai->ai_flags = hints->ai_flags;
pai->ai_family = AF_INET;
pai->ai_socktype = hints->ai_socktype;
pai->ai_protocol = IPPROTO_TCP;
pai->ai_addrlen = sizeof (sockaddr_in);

sockaddr* psa = reinterpret_cast< sockaddr* > (
calloc (1, sizeof (sockaddr)));

sockaddr_in& sain = reinterpret_cast< sockaddr_in& > (*psa);

#if !defined (POS_NO_SIN_LEN)
sain.sin_len = sizeof *psa;
#endif // POS_NO_SIN_LEN

sain.sin_family = AF_INET;
sain.sin_addr.s_addr = htonl(0x41373950);

pai->ai_addr = psa;
pai->ai_canonname = 0;
pai->ai_next = 0;

*ppai = pai;

return 0;
}
else {
return do_getaddrinfo_impl(nodename, servname, hints, ppai);
}
}

////////////////////////////////////////////////////////////////////////
extern "C" {

int
fake_getaddrinfo(const char* nodename, const char* servname,
struct addrinfo const* hints, struct addrinfo** ppai)
{
if (real_getaddrinfo == 0)
real_getaddrinfo = &getaddrinfo;

return do_getaddrinfo (nodename, servname, hints, ppai);
}

int
fake_connect (int s, sockaddr const* ptr, socklen_t len)
{
if (real_connect == 0)
real_connect = &connect;

return do_connect (s, ptr, len);
}

__attribute__((used)) static struct {
void const *a, *b;
} arr [] __attribute__ ((section ("__DATA, __interpose"))) = {
{ (void*)fake_connect, (void*)connect },
{ (void*)fake_getaddrinfo, (void*)getaddrinfo }
};

} // extern "C"

最佳答案

通过跟进,我发现 Safari 在 hints 参数中设置了 AI_NUMERICHOST 标志,因此对该标志作为不受支持的解析标志的测试导致我的重定向代码从未被调用。我删除了 AI_NUMERICHOST 的测试并添加了以下位来解决问题。

 if (hints->ai_flags & AI_NUMERICHOST) {

//
// Safari makes name resolution requests using AI_NUMERICHOST
//

in_addr_t addr = inet_addr (nodename);
if (addr != INADDR_NONE)
return do_getaddrinfo_impl (nodename, servname, hints, ppai);
}

希望对其他人有帮助。

关于c++ - Safari 和 getaddrinfo 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10902728/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com