基于https://blog.csdn.net/jinking01/article/details/126718873,继续hook的尝试。
需求说明
1、文件监控系统,实现指定目录的文件监控,当存在关键字的文件被打开时拒绝打开并提示某进程危险,正在打开某文件,若文件不存在关键字或者不在监控范围内则提示用户某进程正在打开文件允许放行。
2、网络监控系统,实现指定IP地址的拦截,不允许指定的IP连接和访问,并且提示用户某IP正在访问或连接,处理结果为放行或者拦截。
示例(没有测试代码哈,只提供了hook函数)
file_filter.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dlfcn.h>
#define STRMAXLEN 301
const char path[] = "/home/ok/test"; // 受监控的目录
const char word[] = "passwd"; // 敏感字,在受监控的目录中存在敏感字的文件受到保护
typedef int (*new_open)(const char *pathname, int flags);
int open(const char *pathname, int flags)
{
void *handle = NULL;
new_open old_open = NULL; // 保存成old_open以备调用
// 获得libc.so.6的句柄
handle = dlopen("libc.so.6", RTLD_LAZY);
// 返回open函数在libc.so.6中的加载时的地址
old_open = (new_open)dlsym(handle, "open");
struct stat ststat;
if (stat(pathname, &ststat)==0)
{
// 断是否为文件
if (S_ISREG(ststat.st_mode))
{
// 判断是否为全路径,不为则补全当前路径
char pathbuf[STRMAXLEN]; // 全路径文件名buf
if (pathname[0] != '/')
{
memset(pathbuf, 0, STRMAXLEN);
getcwd(pathbuf, STRMAXLEN);
if (pathbuf[strlen(pathbuf)-1] != '/')
strcat(pathbuf, "/");
strcat(pathbuf, pathname);
}
else
{
strcpy(pathbuf, pathname);
}
// 判断文件是否在监控的目录中
if (strstr(pathbuf, path) == &pathbuf[0])
{// 表明该文件受到监控
FILE *fp = NULL;
char buffer[1024];
memset(buffer, 0, sizeof(buffer));
char strcmd[128];
memset(strcmd, 0, sizeof(strcmd));
snprintf(strcmd, sizeof(strcmd), "grep %s %s", word, pathbuf);
fp = popen(strcmd, "r");
// buffer不为空则存在关键字,拦截
if (fgets(buffer, sizeof(buffer), fp) != NULL)
{
printf("进程%d正在打开%s, 该操作已被拦截\n", getpid(), pathbuf);
pclose(fp);
exit(0);
}
pclose(fp);
}
}
}
printf("进程%d正在打开%s, 已放行\n", getpid(), pathname);
return old_open(pathname, flags);
}
socket_filter.c
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <dlfcn.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
const char URL[] = "115.238.190.240";
typedef int (*new_socket)(int, const struct sockaddr *, socklen_t);
typedef int (*new_accept)(int, struct sockaddr *, socklen_t *);
// 判断接入的网络是否为过滤IP,如果是则提前返回
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
void *handle = NULL;
new_socket old_connect = NULL; // 保存成old_connect以备调用
// 获得libc.so.6的句柄
handle = dlopen("libc.so.6", RTLD_LAZY);
// 返回open函数在libc.so.6中的加载时的地址
old_connect = (new_socket)dlsym(handle, "connect");
char ip[128]; memset(ip, 0, sizeof(ip)); // ip地址
int port = -1; // 端口号
if (AF_INET == addr->sa_family)
{
struct sockaddr_in *sa4 = (struct sockaddr_in *)addr;
inet_ntop(AF_INET, (void *)(struct sockaddr *)&sa4->sin_addr, ip, sizeof(ip));
port = ntohs(sa4->sin_port);
printf("\nAF_INET IP===%s:%d\n", ip, port);
}
if (0 == strcmp(ip, URL))
{
printf("\n===%s netfilter...connect failed!\n", ip);
return -1;
}
printf("\nPID:%d, socket:%d, %s Successfully connected!\n", getpid(), sockfd, ip);
return old_connect(sockfd, addr, addrlen);
}
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
{
void *handle = NULL;
new_accept old_accept = NULL; // 保存old_accept以备调用
// 获得libc.so.6的句柄
handle = dlopen("libc.so.6", RTLD_LAZY);
// 返回accept函数在libc.so.6中的加载时的地址
old_accept = (new_accept)dlsym(handle, "accept");
char ip[128]; memset(ip, 0, sizeof(ip)); // Ip地址
int clientfd = -1; // 客户端socketfd
if (AF_INET == addr->sa_family)
{
struct sockaddr_in *sa4 = (struct sockaddr_in *)addr;
strcpy(ip, inet_ntoa(sa4->sin_addr)); // IP
// 阻止接入
if (0 == strcmp(ip, URL))
{
printf("\nPID:%d ===%s=== netfilter...accept failed!\n", getpid(), ip);
return -1;
}
}
// 放行
clientfd = old_accept(sockfd, addr, addrlen);
printf("\nPID:%d, clientfd:%d, %s Successfully accepted!\n", getpid(), clientfd, ip);
return clientfd;
}
makefile
GC=gcc
all:libmyfilter.so
libmyfilter.so:file_filter.c socket_filter.c
$(GC) -fPIC -shared -o libmyfilter.so file_filter.c socket_filter.c -ldl
clean:
rm -rf *.so
原文链接:https://blog.csdn.net/weixin_45646368/article/details/111403378