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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/*
|
| 2 |
+
* network_filter.c - LD_PRELOAD library to filter network connections
|
| 3 |
+
* 编译: gcc -shared -fPIC -o network_filter.so network_filter.c -ldl
|
| 4 |
+
* 使用: LD_PRELOAD=./network_filter.so ALLOWED_HOSTS="127.0.0.1,api.openai.com" your_program
|
| 5 |
+
*/
|
| 6 |
+
|
| 7 |
+
#define _GNU_SOURCE
|
| 8 |
+
#include <dlfcn.h>
|
| 9 |
+
#include <stdio.h>
|
| 10 |
+
#include <stdlib.h>
|
| 11 |
+
#include <string.h>
|
| 12 |
+
#include <sys/socket.h>
|
| 13 |
+
#include <netinet/in.h>
|
| 14 |
+
#include <arpa/inet.h>
|
| 15 |
+
#include <netdb.h>
|
| 16 |
+
#include <errno.h>
|
| 17 |
+
#include <unistd.h>
|
| 18 |
+
|
| 19 |
+
// 函数指针类型定义
|
| 20 |
+
typedef int (*original_connect_t)(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
|
| 21 |
+
typedef int (*original_socket_t)(int domain, int type, int protocol);
|
| 22 |
+
typedef ssize_t (*original_send_t)(int sockfd, const void *buf, size_t len, int flags);
|
| 23 |
+
typedef ssize_t (*original_sendto_t)(int sockfd, const void *buf, size_t len, int flags,
|
| 24 |
+
const struct sockaddr *dest_addr, socklen_t addrlen);
|
| 25 |
+
|
| 26 |
+
// 全局变量
|
| 27 |
+
static original_connect_t original_connect = NULL;
|
| 28 |
+
static original_socket_t original_socket = NULL;
|
| 29 |
+
static original_send_t original_send = NULL;
|
| 30 |
+
static original_sendto_t original_sendto = NULL;
|
| 31 |
+
|
| 32 |
+
static char allowed_hosts[1024] = {0};
|
| 33 |
+
static int filter_initialized = 0;
|
| 34 |
+
|
| 35 |
+
// 初始化过滤器
|
| 36 |
+
static void init_filter() {
|
| 37 |
+
if (filter_initialized) return;
|
| 38 |
+
|
| 39 |
+
const char* env_hosts = getenv("ALLOWED_HOSTS");
|
| 40 |
+
if (env_hosts) {
|
| 41 |
+
strncpy(allowed_hosts, env_hosts, sizeof(allowed_hosts) - 1);
|
| 42 |
+
} else {
|
| 43 |
+
// 默认允许的主机
|
| 44 |
+
strcpy(allowed_hosts, "127.0.0.1,::1,localhost");
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
filter_initialized = 1;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
// 检查IP地址是否被允许
|
| 51 |
+
static int is_ip_allowed(const char* ip_str) {
|
| 52 |
+
init_filter();
|
| 53 |
+
|
| 54 |
+
char* hosts_copy = strdup(allowed_hosts);
|
| 55 |
+
char* token = strtok(hosts_copy, ",");
|
| 56 |
+
|
| 57 |
+
while (token != NULL) {
|
| 58 |
+
// 去除前后空格
|
| 59 |
+
while (*token == ' ') token++;
|
| 60 |
+
char* end = token + strlen(token) - 1;
|
| 61 |
+
while (end > token && *end == ' ') *end-- = '\0';
|
| 62 |
+
|
| 63 |
+
// 检查是否是IP地址直接匹配
|
| 64 |
+
if (strcmp(token, ip_str) == 0) {
|
| 65 |
+
free(hosts_copy);
|
| 66 |
+
return 1;
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
// 如果是域名,解析并比较
|
| 70 |
+
struct hostent* host_entry = gethostbyname(token);
|
| 71 |
+
if (host_entry != NULL) {
|
| 72 |
+
for (int i = 0; host_entry->h_addr_list[i] != NULL; i++) {
|
| 73 |
+
char* resolved_ip = inet_ntoa(*((struct in_addr*)host_entry->h_addr_list[i]));
|
| 74 |
+
if (strcmp(resolved_ip, ip_str) == 0) {
|
| 75 |
+
free(hosts_copy);
|
| 76 |
+
return 1;
|
| 77 |
+
}
|
| 78 |
+
}
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
token = strtok(NULL, ",");
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
free(hosts_copy);
|
| 85 |
+
return 0;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
// 检查连接地址是否被允许
|
| 89 |
+
static int is_connection_allowed(const struct sockaddr *addr) {
|
| 90 |
+
if (addr->sa_family == AF_INET) {
|
| 91 |
+
struct sockaddr_in* sin = (struct sockaddr_in*)addr;
|
| 92 |
+
char ip_str[INET_ADDRSTRLEN];
|
| 93 |
+
inet_ntop(AF_INET, &(sin->sin_addr), ip_str, INET_ADDRSTRLEN);
|
| 94 |
+
|
| 95 |
+
int allowed = is_ip_allowed(ip_str);
|
| 96 |
+
if (!allowed) {
|
| 97 |
+
fprintf(stderr, "[NETWORK_FILTER] Blocked connection to %s:%d\n",
|
| 98 |
+
ip_str, ntohs(sin->sin_port));
|
| 99 |
+
} else {
|
| 100 |
+
fprintf(stderr, "[NETWORK_FILTER] Allowed connection to %s:%d\n",
|
| 101 |
+
ip_str, ntohs(sin->sin_port));
|
| 102 |
+
}
|
| 103 |
+
return allowed;
|
| 104 |
+
} else if (addr->sa_family == AF_INET6) {
|
| 105 |
+
struct sockaddr_in6* sin6 = (struct sockaddr_in6*)addr;
|
| 106 |
+
char ip_str[INET6_ADDRSTRLEN];
|
| 107 |
+
inet_ntop(AF_INET6, &(sin6->sin6_addr), ip_str, INET6_ADDRSTRLEN);
|
| 108 |
+
|
| 109 |
+
int allowed = is_ip_allowed(ip_str);
|
| 110 |
+
if (!allowed) {
|
| 111 |
+
fprintf(stderr, "[NETWORK_FILTER] Blocked IPv6 connection to [%s]:%d\n",
|
| 112 |
+
ip_str, ntohs(sin6->sin6_port));
|
| 113 |
+
} else {
|
| 114 |
+
fprintf(stderr, "[NETWORK_FILTER] Allowed IPv6 connection to [%s]:%d\n",
|
| 115 |
+
ip_str, ntohs(sin6->sin6_port));
|
| 116 |
+
}
|
| 117 |
+
return allowed;
|
| 118 |
+
} else if (addr->sa_family == AF_UNIX) {
|
| 119 |
+
// 允许 Unix domain socket
|
| 120 |
+
return 1;
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
// 其他协议族默认阻止
|
| 124 |
+
fprintf(stderr, "[NETWORK_FILTER] Blocked connection to unknown address family %d\n",
|
| 125 |
+
addr->sa_family);
|
| 126 |
+
return 0;
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
// 拦截 connect 系统调用
|
| 130 |
+
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
|
| 131 |
+
// 获取原始函数指针
|
| 132 |
+
if (!original_connect) {
|
| 133 |
+
original_connect = (original_connect_t)dlsym(RTLD_NEXT, "connect");
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
// 检查连接是否被允许
|
| 137 |
+
if (!is_connection_allowed(addr)) {
|
| 138 |
+
errno = EPERM;
|
| 139 |
+
return -1;
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
// 调用原始的 connect 函数
|
| 143 |
+
return original_connect(sockfd, addr, addrlen);
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
// 拦截 socket 系统调用(可选,用于记录)
|
| 147 |
+
int socket(int domain, int type, int protocol) {
|
| 148 |
+
if (!original_socket) {
|
| 149 |
+
original_socket = (original_socket_t)dlsym(RTLD_NEXT, "socket");
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
const char* domain_str = "unknown";
|
| 153 |
+
switch (domain) {
|
| 154 |
+
case AF_INET: domain_str = "IPv4"; break;
|
| 155 |
+
case AF_INET6: domain_str = "IPv6"; break;
|
| 156 |
+
case AF_UNIX: domain_str = "Unix"; break;
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
const char* type_str = (type & SOCK_STREAM) ? "TCP" :
|
| 160 |
+
(type & SOCK_DGRAM) ? "UDP" : "other";
|
| 161 |
+
|
| 162 |
+
int result = original_socket(domain, type, protocol);
|
| 163 |
+
if (result >= 0) {
|
| 164 |
+
fprintf(stderr, "[NETWORK_FILTER] Created socket: %s %s (fd=%d)\n",
|
| 165 |
+
domain_str, type_str, result);
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
return result;
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
// 拦截 sendto 系统调用(用于 UDP)
|
| 172 |
+
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
|
| 173 |
+
const struct sockaddr *dest_addr, socklen_t addrlen) {
|
| 174 |
+
if (!original_sendto) {
|
| 175 |
+
original_sendto = (original_sendto_t)dlsym(RTLD_NEXT, "sendto");
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
// 如果指定了目标地址,检查是否允许
|
| 179 |
+
if (dest_addr && !is_connection_allowed(dest_addr)) {
|
| 180 |
+
errno = EPERM;
|
| 181 |
+
return -1;
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
return original_sendto(sockfd, buf, len, flags, dest_addr, addrlen);
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
// 库构造函数,在加载时调用
|
| 188 |
+
__attribute__((constructor))
|
| 189 |
+
static void network_filter_init() {
|
| 190 |
+
init_filter();
|
| 191 |
+
}
|
| 192 |
+
|