-
Notifications
You must be signed in to change notification settings - Fork 2
/
iodump.py
executable file
·337 lines (270 loc) · 8.23 KB
/
iodump.py
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/python3
import sys
import time
import math
import struct
import signal
import resource
import ctypes as ct
import multiprocessing
from bcc import BPF
# ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count)
# ssize_t ksys_write(unsigned int fd, const char __user *buf, size_t count)
bpf_text = '''
#include <linux/sched.h>
#define IO_MAX_BUF_SIZE __IO_MAX_BUF_SIZE__
#define IO_PACKET_F_WRITE 1
struct packet {
u32 pid;
u32 fd;
s64 ret;
u64 flags;
char data[IO_MAX_BUF_SIZE];
};
struct rw_args {
unsigned int fd;
char *buf;
};
// use regular array instead percpu array because
// percpu array element size cannot be larger than 3k
BPF_ARRAY(packet_array, struct packet, __NUM_CPUS__);
BPF_HASH(rw_buf, u32, struct rw_args);
BPF_PERF_OUTPUT(events);
int kprobe_ksys_read_write(struct pt_regs *ctx,
unsigned int fd, char *buf, size_t count)
{
u64 pid_tgid = bpf_get_current_pid_tgid();
u32 pid = pid_tgid >> 32;
u32 tid = pid_tgid;
struct rw_args args = { 0 };
if (pid != __PID__)
return 0;
__FD_FILTER__
args.fd = fd;
args.buf = buf;
rw_buf.update(&tid, &args);
return 0;
}
static inline int kretprobe_ksys_read_write(struct pt_regs *ctx, u64 flags)
{
u64 pid_tgid = bpf_get_current_pid_tgid();
u32 pid = pid_tgid >> 32;
u32 tid = pid_tgid;
int n;
s64 ret;
struct packet *packet;
struct rw_args *args;
args = rw_buf.lookup(&tid);
if (args == NULL) {
return 0;
}
rw_buf.delete(&tid);
n = bpf_get_smp_processor_id();
packet = packet_array.lookup(&n);
if (packet == NULL)
return 0;
ret = PT_REGS_RC(ctx);
packet->pid = pid;
packet->fd = args->fd;
packet->ret = ret;
packet->flags = flags;
if (ret < 0) {
events.perf_submit(ctx, packet, offsetof(struct packet, data));
return 0;
}
n = ret;
bpf_probe_read(
&packet->data,
// check size in args to make compiler/validator happy
n > sizeof(packet->data) ? sizeof(packet->data) : n,
args->buf
);
n += offsetof(struct packet, data);
events.perf_submit(
ctx,
packet,
// check size in args to make compiler/validator happy
n > sizeof(*packet) ? sizeof(*packet) : n
);
return 0;
}
int kretprobe_ksys_read(struct pt_regs *ctx)
{
return kretprobe_ksys_read_write(ctx, 0);
}
int kretprobe_ksys_write(struct pt_regs *ctx)
{
return kretprobe_ksys_read_write(ctx, IO_PACKET_F_WRITE);
}
'''
TASK_COMM_LEN = 16
UNIX_PATH_MAX = 108
IO_MAX_BUF_SIZE = 1024 * 50
IO_MAX_QUEUE_LEN = 100
IO_PACKET_F_WRITE = 1
def build_fd_filter(fds):
if len(fds) == 0:
return ''
filter = 'if ('
filter += ' && '.join('fd != %d' % x for x in fds)
filter += ') return 0;'
return filter
def render_text(bpf_text, seg_size, pid, fd_filter):
replaces = {
'__IO_MAX_BUF_SIZE__': seg_size,
'__NUM_CPUS__': multiprocessing.cpu_count(),
'__PID__': pid,
'__FD_FILTER__': fd_filter,
}
for k, v in replaces.items():
bpf_text = bpf_text.replace(k, str(v))
return bpf_text
class Packet(ct.Structure):
_pack_ = 1
_fields_ = [
('pid', ct.c_uint),
('fd', ct.c_uint),
('ret', ct.c_long),
('flags', ct.c_ulong),
# variable length data
]
PCAP_LINK_TYPE = 147 # USER_0
PACKET_SIZE = ct.sizeof(Packet)
packet_count = 0
def parse_event(event, size):
global packet_count
packet_count += 1
packet = ct.cast(event, ct.POINTER(Packet)).contents
if packet.ret < 0:
return packet, b''
event += PACKET_SIZE
size -= PACKET_SIZE
data_len = packet.ret
if data_len > size:
data_len = size
data_type = ct.c_char * data_len
data = ct.cast(event, ct.POINTER(data_type)).contents.raw
return packet, data
def print_header(packet, data):
ts = time.time()
ts = time.strftime('%H:%M:%S', time.localtime(ts)) + '.%03d' % (ts%1 * 1000)
direction = 'WR >>>'
if packet.flags & IO_PACKET_F_WRITE == 0:
direction = 'RD <<<'
line = '%s %s pid %d fd %d' % (ts, direction, packet.pid, packet.fd)
if packet.ret < 0:
line += ' err %d' % packet.ret
else:
line += ' len %d(%d)' % (packet.ret, len(data))
print(line)
def string_output(cpu, event, size):
packet, data = parse_event(event, size)
print_header(packet, data)
print(str(data, encoding='ascii', errors='ignore'), end='', flush=True)
def ascii(c):
if c < 32 or c > 126:
return '.'
return chr(c)
def hex_print(data):
for i in range(0, len(data), 16):
line = '{:04x}'.format(i)
line += ' '
line += '{:<23s}'.format(' '.join('%02x' % x for x in data[i:i+8]))
line += ' '
line += '{:<23s}'.format(' '.join('%02x' % x for x in data[i+8:i+16]))
line += ' '
line += ''.join(ascii(x) for x in data[i:i+16])
print(line)
def hex_output(cpu, event, size):
packet, data = parse_event(event, size)
print_header(packet, data)
hex_print(data)
def pcap_write_header(snaplen, network):
header = struct.pack('=IHHiIII', 0xa1b2c3d4, 2, 4, 0, 0, snaplen, network)
sys.stdout.write(header)
def pcap_write_record(ts_sec, ts_usec, orig_len, data):
header = struct.pack('=IIII', ts_sec, ts_usec, len(data), orig_len)
sys.stdout.write(header)
sys.stdout.write(data)
def pcap_output(cpu, event, size):
packet, data = parse_event(event, size)
# FIXME: also dump error
if packet.ret < 0:
return
ts = time.time()
ts_sec = int(ts)
ts_usec = int((ts % 1) * 10**6)
if packet.flags & IO_PACKET_F_WRITE == 0:
src = 0
dst = packet.pid << 32 | packet.fd
else:
src = packet.pid << 32 | packet.fd
dst = 0
header = struct.pack('>QQ', dst, src)
data = header + data
size = len(header) + packet.ret
pcap_write_record(ts_sec, ts_usec, size, data)
outputs = {
'hex': hex_output,
'string': string_output,
'pcap': pcap_output,
}
def sig_handler(signum, stack):
print('\n%d packets captured' % packet_count, file=sys.stderr)
sys.exit(signum)
def main(args):
fd_filter = build_fd_filter([int(x) for x in args.fd])
text = render_text(bpf_text, args.max_buf_size, args.pid, fd_filter)
if args.bpf:
print(text)
return 0
b = BPF(text=text)
b.attach_kprobe(
event='ksys_read', fn_name='kprobe_ksys_read_write')
b.attach_kretprobe(
event='ksys_read', fn_name='kretprobe_ksys_read')
b.attach_kprobe(
event='ksys_write', fn_name='kprobe_ksys_read_write')
b.attach_kretprobe(
event='ksys_write', fn_name='kretprobe_ksys_write')
npages = args.max_buf_size * args.max_queue_len / resource.getpagesize()
npages = 2 ** math.ceil(math.log(npages, 2))
output_fn = outputs[args.format]
b['events'].open_perf_buffer(output_fn, page_cnt=npages)
signal.signal(signal.SIGINT, sig_handler)
signal.signal(signal.SIGTERM, sig_handler)
if args.format == 'pcap':
sys.stdout = open(args.output, 'wb')
pcap_write_header(args.max_buf_size, PCAP_LINK_TYPE)
else:
sys.stdout = open(args.output, 'w')
print('waiting for data', file=sys.stderr)
while 1:
b.perf_buffer_poll()
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
description='Dump unix domain socket traffic')
parser.add_argument(
'--max-buf-size', type=int, default=IO_MAX_BUF_SIZE,
help='max buffer size for read/write')
parser.add_argument(
'--max-queue-len', type=int, default=IO_MAX_QUEUE_LEN,
help='max len of the dump queue')
parser.add_argument(
'--format', choices=outputs.keys(), default='hex',
help='output format')
parser.add_argument(
'--output', default='/dev/stdout',
help='output file')
parser.add_argument(
'--bpf', action='store_true',
help=argparse.SUPPRESS)
parser.add_argument(
'--fd', action='append', default=[],
help='sniff this file descriptor')
parser.add_argument(
'pid', type=int,
help='sniff this PID')
args = parser.parse_args()
sys.exit(main(args))