I am currently investigating a compiled C program. I know that it makes several network requests due to several calls to socket
, gethostbyname
and connect
throughout the code. Furthermore, I know it is making a GET
request because i have run strings
on the program and found one.
I would like to run this program so that I can investigate it without it making any network calls; however to do this I would have to simulate a get request just with the functions given.
I have the following code, which I have compiled and added to 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;
}
Which appears to work, but I can make little sense of what it prints to and receives from the socket.
I am also open to other solutions to the problem.