|
|
@@ -1432,11 +1432,11 @@ int sockops_cb(struct bpf_sock_ops *skops) {
|
|
|
// 将socket添加到sockhash map
|
|
|
__u32 cookie = bpf_get_socket_cookie(skops);
|
|
|
bpf_sock_hash_update(skops, &sockhash, &tuple, BPF_ANY);
|
|
|
-
|
|
|
+
|
|
|
// 同时将socket添加到sockmap(用于sk_msg程序)
|
|
|
bpf_sock_map_update(skops, &sk_msg_map, &cookie, BPF_ANY);
|
|
|
|
|
|
- bpf_printk("sockops: added socket to sockhash and sockmap, cookie=%u\n", cookie);
|
|
|
+// bpf_printk("sockops: added socket to sockhash and sockmap, cookie=%u\n", cookie);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
@@ -1445,58 +1445,58 @@ int sockops_cb(struct bpf_sock_ops *skops) {
|
|
|
SEC("cgroup/skb")
|
|
|
int http_request_handler(struct __sk_buff *skb) {
|
|
|
// 检查数据包长度
|
|
|
- if (skb->len < 4) {
|
|
|
- return 1; // 允许通过
|
|
|
- }
|
|
|
-
|
|
|
- // 读取前几个字节来检查是否是HTTP请求
|
|
|
- char buf[100] = {};
|
|
|
- if (bpf_skb_load_bytes(skb, 0, buf, 100) < 0) {
|
|
|
- return 1; // 允许通过
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- // 跳过IP头部(20字节)和TCP头部(20字节),直接读取应用层数据
|
|
|
- // IP头部长度 = (buf[0] & 0x0F) * 4 = 5 * 4 = 20字节
|
|
|
- // TCP头部长度 = (buf[32] >> 4) * 4,但通常也是20字节
|
|
|
- int ip_header_len = (buf[0] & 0x0F) * 4;
|
|
|
- int tcp_header_len = 20; // 通常TCP头部是20字节
|
|
|
- int app_data_offset = ip_header_len + tcp_header_len;
|
|
|
-
|
|
|
- // 检查是否有足够的应用层数据
|
|
|
- if (skb->len < app_data_offset + 4) {
|
|
|
- return 1; // 允许通过
|
|
|
- }
|
|
|
-
|
|
|
- // 读取应用层数据的前几个字节
|
|
|
- char app_data[100] = {};
|
|
|
- if (bpf_skb_load_bytes(skb, app_data_offset, app_data, 100) < 0) {
|
|
|
- return 1; // 允许通过
|
|
|
- }
|
|
|
-
|
|
|
- // 检查是否是HTTP请求
|
|
|
- if ((app_data[0] == 'G' && app_data[1] == 'E' && app_data[2] == 'T' && app_data[3] == ' ') ||
|
|
|
- (app_data[0] == 'P' && app_data[1] == 'O' && app_data[2] == 'S' && app_data[3] == 'T') ||
|
|
|
- (app_data[0] == 'P' && app_data[1] == 'U' && app_data[2] == 'T' && app_data[3] == ' ') ||
|
|
|
- (app_data[0] == 'D' && app_data[1] == 'E' && app_data[2] == 'L' && app_data[3] == 'E') ||
|
|
|
- (app_data[0] == 'H' && app_data[1] == 'E' && app_data[2] == 'A' && app_data[3] == 'D') ||
|
|
|
- (app_data[0] == 'O' && app_data[1] == 'P' && app_data[2] == 'T' && app_data[3] == 'I')) {
|
|
|
-
|
|
|
-// bpf_printk("=== HTTP REQUEST FOUND ===\n");
|
|
|
-// bpf_printk("IP header len: %d, TCP header len: %d\n", ip_header_len, tcp_header_len);
|
|
|
-// bpf_printk("App data offset: %d\n", app_data_offset);
|
|
|
-// bpf_printk("Content: %s\n", app_data);
|
|
|
-// bpf_printk("=== END HTTP REQUEST ===\n");
|
|
|
-
|
|
|
- // 标记这个socket需要修改HTTP请求
|
|
|
- // 通过http_modify_flags map传递信息给stream_verdict程序
|
|
|
- __u32 cookie = bpf_get_socket_cookie(skb);
|
|
|
- __u32 flag = 1; // 标记需要修改
|
|
|
- if (bpf_map_update_elem(&http_modify_flags, &cookie, &flag, BPF_ANY) == 0) {
|
|
|
- bpf_printk("Marked socket %u for HTTP request modification\n", cookie);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+// if (skb->len < 4) {
|
|
|
+// return 1; // 允许通过
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 读取前几个字节来检查是否是HTTP请求
|
|
|
+// char buf[100] = {};
|
|
|
+// if (bpf_skb_load_bytes(skb, 0, buf, 100) < 0) {
|
|
|
+// return 1; // 允许通过
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// // 跳过IP头部(20字节)和TCP头部(20字节),直接读取应用层数据
|
|
|
+// // IP头部长度 = (buf[0] & 0x0F) * 4 = 5 * 4 = 20字节
|
|
|
+// // TCP头部长度 = (buf[32] >> 4) * 4,但通常也是20字节
|
|
|
+// int ip_header_len = (buf[0] & 0x0F) * 4;
|
|
|
+// int tcp_header_len = 20; // 通常TCP头部是20字节
|
|
|
+// int app_data_offset = ip_header_len + tcp_header_len;
|
|
|
+//
|
|
|
+// // 检查是否有足够的应用层数据
|
|
|
+// if (skb->len < app_data_offset + 4) {
|
|
|
+// return 1; // 允许通过
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 读取应用层数据的前几个字节
|
|
|
+// char app_data[100] = {};
|
|
|
+// if (bpf_skb_load_bytes(skb, app_data_offset, app_data, 100) < 0) {
|
|
|
+// return 1; // 允许通过
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 检查是否是HTTP请求
|
|
|
+// if ((app_data[0] == 'G' && app_data[1] == 'E' && app_data[2] == 'T' && app_data[3] == ' ') ||
|
|
|
+// (app_data[0] == 'P' && app_data[1] == 'O' && app_data[2] == 'S' && app_data[3] == 'T') ||
|
|
|
+// (app_data[0] == 'P' && app_data[1] == 'U' && app_data[2] == 'T' && app_data[3] == ' ') ||
|
|
|
+// (app_data[0] == 'D' && app_data[1] == 'E' && app_data[2] == 'L' && app_data[3] == 'E') ||
|
|
|
+// (app_data[0] == 'H' && app_data[1] == 'E' && app_data[2] == 'A' && app_data[3] == 'D') ||
|
|
|
+// (app_data[0] == 'O' && app_data[1] == 'P' && app_data[2] == 'T' && app_data[3] == 'I')) {
|
|
|
+//
|
|
|
+//// bpf_printk("=== HTTP REQUEST FOUND ===\n");
|
|
|
+//// bpf_printk("IP header len: %d, TCP header len: %d\n", ip_header_len, tcp_header_len);
|
|
|
+//// bpf_printk("App data offset: %d\n", app_data_offset);
|
|
|
+//// bpf_printk("Content: %s\n", app_data);
|
|
|
+//// bpf_printk("=== END HTTP REQUEST ===\n");
|
|
|
+//
|
|
|
+// // 标记这个socket需要修改HTTP请求
|
|
|
+// // 通过http_modify_flags map传递信息给stream_verdict程序
|
|
|
+// __u32 cookie = bpf_get_socket_cookie(skb);
|
|
|
+// __u32 flag = 1; // 标记需要修改
|
|
|
+// if (bpf_map_update_elem(&http_modify_flags, &cookie, &flag, BPF_ANY) == 0) {
|
|
|
+// bpf_printk("Marked socket %u for HTTP request modification\n", cookie);
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
return 1; // 允许数据包通过
|
|
|
}
|
|
|
|
|
|
@@ -1514,13 +1514,16 @@ int sk_msg_handler(struct sk_msg_md *msg)
|
|
|
return SK_PASS;
|
|
|
}
|
|
|
|
|
|
- // todo 进程过滤
|
|
|
__u32 tgid = (__u32) (bpf_get_current_pid_tgid() >> 32);
|
|
|
struct ebpf_proc_info *proc_info = bpf_map_lookup_elem(&proc_info_map, &tgid);
|
|
|
if (!proc_info) {
|
|
|
return SK_PASS;
|
|
|
}
|
|
|
|
|
|
+ if (!mk_header_in_sk_msg(proc_info->code_type)) {
|
|
|
+// bpf_printk("not allowd");
|
|
|
+ return SK_PASS;
|
|
|
+ }
|
|
|
|
|
|
__u32 max_buf = msg->size < MAX_L7_IOVEC_BUF_SIZE ? msg->size : MAX_L7_IOVEC_BUF_SIZE;
|
|
|
|