我目前正在研究一个已编译的 C 程序。我知道由于多次调用socket
,gethostbyname
并connect
贯穿整个代码,它会发出多个网络请求。此外,我知道它正在发出GET
请求,因为我已经strings
在程序上运行并找到了一个。
我想运行这个程序,这样我就可以在不进行任何网络调用的情况下对其进行调查;但是要做到这一点,我必须只使用给定的函数来模拟获取请求。
我有以下代码,我已编译并添加到LD_PRELOAD
:
#include <netdb.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
int socket(int domain, int type, int protocol) {
fprintf(stderr, "socket(%d, %d, %d)\n", domain, type, protocol);
// TODO Return actual socket which contains request
return 1;
}
struct hostent HOST;
struct hostent *gethostbyname(const char*name) {
fprintf(stderr, "gethostbyname(%s)\n", name);
return &HOST;
}
int connect(int sockfd, const struct sockaddr *addr, socklen_t addr_len) {
int name_len = addr_len - sizeof(struct sockaddr);
fprintf(stderr, "Connecting to: %*.s\n", name_len, addr->sa_data);
return 0;
}
这似乎可行,但我无法理解它打印到套接字和从套接字接收的内容。
我也对这个问题的其他解决方案持开放态度。