forked from mechpen/sockdump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sockdump.py
executable file
·518 lines (419 loc) · 14.1 KB
/
sockdump.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#!/usr/bin/python3
import json
import os
import subprocess
import sys
import time
import math
import struct
import signal
import resource
import ctypes as ct
import multiprocessing
from bcc import BPF
bpf_text = """
#include <linux/sched.h>
#include <linux/net.h>
#include <uapi/linux/un.h>
#include <net/af_unix.h>
#include <linux/version.h>
#define SS_MAX_SEG_SIZE __SS_MAX_SEG_SIZE__
#define SS_MAX_SEGS_PER_MSG __SS_MAX_SEGS_PER_MSG__
#define SS_PACKET_F_ERR 1
#define SOCK_PATH_OFFSET \
(offsetof(struct unix_address, name) + offsetof(struct sockaddr_un, sun_path))
struct packet {
u32 pid;
u32 peer_pid;
u32 len;
u32 flags;
char comm[TASK_COMM_LEN];
char path[UNIX_PATH_MAX];
char data[SS_MAX_SEG_SIZE];
};
// 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_PERF_OUTPUT(events);
int probe_unix_socket_sendmsg(struct pt_regs *ctx,
struct socket *sock,
struct msghdr *msg,
size_t len)
{
struct packet *packet;
struct unix_address *addr;
char *buf, *sock_path;
unsigned long path[__PATH_LEN_U64__] = {0};
unsigned int n, match = 0, offset;
struct iov_iter *iter;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,0,0)
const struct iovec *iov;
#else
const struct kvec *iov;
#endif
struct pid *peer_pid;
addr = ((struct unix_sock *)sock->sk)->addr;
if (addr->len > 0) {
sock_path = (char *)addr + SOCK_PATH_OFFSET;
if (*sock_path == 0) {
// abstract sockets start with \\0 and the name comes after
// (they actually have no @ prefix but some tools use that)
bpf_probe_read(&path, __PATH_LEN__ - 1, sock_path + 1);
} else {
bpf_probe_read(&path, __PATH_LEN__, sock_path);
}
__PATH_FILTER__
}
addr = ((struct unix_sock *)((struct unix_sock *)sock->sk)->peer)->addr;
if (match == 0 && addr->len > 0) {
sock_path = (char *)addr + SOCK_PATH_OFFSET;
if (*sock_path == 0) {
// abstract sockets start with \\0 and the name comes after
// (they actually have no @ prefix but some tools use that)
bpf_probe_read(&path, __PATH_LEN__ - 1, sock_path + 1);
} else {
bpf_probe_read(&path, __PATH_LEN__, sock_path);
}
__PATH_FILTER__
}
if (match == 0)
return 0;
n = bpf_get_smp_processor_id();
packet = packet_array.lookup(&n);
if (packet == NULL)
return 0;
packet->pid = bpf_get_current_pid_tgid() >> 32;
bpf_get_current_comm(&packet->comm, sizeof(packet->comm));
bpf_probe_read(&packet->path, UNIX_PATH_MAX, sock_path);
packet->peer_pid = sock->sk->sk_peer_pid->numbers->nr;
__PID_FILTER__
iter = &msg->msg_iter;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,0,0)
if (iter->iter_type == ITER_UBUF) {
packet->len = len;
packet->flags = 0;
buf = iter->ubuf;
n = len;
bpf_probe_read(
&packet->data,
// check size in args to make compiler/validator happy
n > sizeof(packet->data) ? sizeof(packet->data) : n,
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;
}
if (iter->iter_type != ITER_IOVEC || iter->iov_offset != 0) {
#else
if (iter->iov_offset != 0) {
#endif
packet->len = len;
packet->flags = SS_PACKET_F_ERR;
events.perf_submit(ctx, packet, offsetof(struct packet, data));
return 0;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,3,0)
iov = iter->__iov;
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(6,0,0)
iov = iter->iov;
#else
iov = iter->kvec;
#endif
#pragma unroll
for (int i = 0; i < SS_MAX_SEGS_PER_MSG; i++) {
if (i >= iter->nr_segs)
break;
packet->len = iov->iov_len;
packet->flags = 0;
buf = iov->iov_base;
n = iov->iov_len;
bpf_probe_read(
&packet->data,
// check size in args to make compiler/validator happy
n > sizeof(packet->data) ? sizeof(packet->data) : n,
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);
iov++;
}
return 0;
}
"""
TASK_COMM_LEN = 16
UNIX_PATH_MAX = 108
SS_MAX_SEG_SIZE = 1024 * 50
SS_MAX_SEGS_PER_MSG = 10
SS_MAX_SEGS_IN_BUFFER = 100
SS_PACKET_F_ERR = 1
def render_text(bpf_text, seg_size, segs_per_msg, sock_path, pid=None):
path_filter, path_len, path_len_u64 = build_filter(sock_path)
replaces = {
"__SS_MAX_SEG_SIZE__": seg_size,
"__SS_MAX_SEGS_PER_MSG__": segs_per_msg,
"__NUM_CPUS__": multiprocessing.cpu_count(),
"__PATH_LEN__": path_len,
"__PATH_LEN_U64__": max(path_len_u64, 1),
"__PATH_FILTER__": path_filter,
}
if pid:
replaces["__PID_FILTER__"] = (
"if (packet->pid != %s && packet->peer_pid != %s) { return 0; }"
% (pid, pid)
)
else:
replaces["__PID_FILTER__"] = ""
for k, v in replaces.items():
bpf_text = bpf_text.replace(k, str(v))
return bpf_text
def build_filter(sock_path):
sock_path_bytes = sock_path.encode()
# if path ends with * - use prefix-based matching
if sock_path[-1] == "*":
sock_path_bytes = sock_path_bytes[:-1]
elif sock_path[0] == "@":
sock_path_bytes = sock_path_bytes[1:] + b"\0"
else:
sock_path_bytes += b"\0"
path_len = len(sock_path_bytes)
if path_len > UNIX_PATH_MAX:
raise ValueError("invalid path")
# match all paths
if path_len == 0:
return "match = 1;", 0, 0
path_len_u64 = (path_len + 7) // 8
sock_path_bytes += b"\0" * (path_len_u64 * 8 - path_len)
sock_path_u64s = [
struct.unpack("Q", sock_path_bytes[i * 8 : (i + 1) * 8])[0]
for i in range(path_len_u64)
]
filter = "if ("
filter += " && ".join(
"path[{}] == {}".format(i, n) for (i, n) in enumerate(sock_path_u64s)
)
filter += ") match = 1;"
return filter, path_len, path_len_u64
class Packet(ct.Structure):
_pack_ = 1
_fields_ = [
("pid", ct.c_uint),
("peer_pid", ct.c_uint),
("len", ct.c_uint),
("flags", ct.c_uint),
("comm", ct.c_char * TASK_COMM_LEN),
("path", ct.c_char * UNIX_PATH_MAX),
# 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
event += PACKET_SIZE
size -= PACKET_SIZE
data_len = packet.len
if data_len > size:
data_len = size
data_type = ct.c_char * data_len
data = ct.cast(event, ct.POINTER(data_type)).contents
return packet, data
def print_header(packet, data):
ts = time.time()
ts = time.strftime("%H:%M:%S", time.localtime(ts)) + ".%03d" % (ts % 1 * 1000)
print(
"%s >>> process %s [%d -> %d] path %s len %d(%d)"
% (
ts,
packet.comm.decode(),
packet.pid,
packet.peer_pid,
packet.path.decode(),
len(data),
packet.len,
)
)
def string_output(cpu, event, size):
packet, data = parse_event(event, size)
print_header(packet, data)
if packet.flags & SS_PACKET_F_ERR:
print("error")
print(str(data.raw, 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 get_hexstring(data):
chunks = ["{:02x}".format(i) for i in bytes(data)]
return "".join(chunks)
def hexstring_print(data):
chunks = ["\\x{:02x}".format(i) for i in bytes(data)]
print("".join(chunks))
def hex_output(cpu, event, size):
packet, data = parse_event(event, size)
print_header(packet, data)
if packet.flags & SS_PACKET_F_ERR:
print("error")
hex_print(data)
unknown_method = "<unknown>"
stream_method_map = {}
def parse_hexstring_by_ttrpc(hexstring, org_data):
# print(hexstring)
command = "./ttrpc-parser --pppid -f %s" % hexstring
status, output = subprocess.getstatusoutput(command)
if status != 0:
print("run ttrpc-parser failed, output: %s" % {output})
else:
data = json.loads(output)
msg_type = data["msg_type"]
stream_id = data.get("stream_id")
method = data.get("method")
if not method:
method = unknown_method
msg = "\tTaskId: %s, StreamID: %s, MsgType: %s, Flags: %s, Method: %s" % (
data["task_id"],
data["stream_id"],
msg_type,
data["msg_flags"],
method,
)
if method == unknown_method:
msg += ",\toutput: %s, hexstring: %s" % (output, hexstring)
else:
stream_method_map[stream_id] = method
if msg_type == "01":
msg += ",\tReq: %s" % (data["req"])
elif msg_type == "02":
msg += ",\tResp: %s, Status: %s" % (data["resp"], data["status"])
else:
pass
print(msg)
# print(stream_method_map)
def ttcp_output(cpu, event, size):
packet, data = parse_event(event, size)
print_header(packet, data)
if packet.flags & SS_PACKET_F_ERR:
print("error")
hexstring = get_hexstring(data)
parse_hexstring_by_ttrpc(hexstring, data)
def hexstring_output(cpu, event, size):
packet, data = parse_event(event, size)
print_header(packet, data)
if packet.flags & SS_PACKET_F_ERR:
print("error")
hexstring_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)
ts = time.time()
ts_sec = int(ts)
ts_usec = int((ts % 1) * 10**6)
header = struct.pack(">QQ", packet.peer_pid, packet.pid)
data = header + data
size = len(header) + packet.len
pcap_write_record(ts_sec, ts_usec, size, data)
outputs = {
"hex": hex_output,
"hexstring": hexstring_output,
"string": string_output,
"pcap": pcap_output,
"ttcp": ttcp_output,
}
def sig_handler(signum, stack):
print("\n%d packets captured" % packet_count, file=sys.stderr)
sys.exit(signum)
def main(args):
text = render_text(bpf_text, args.seg_size, args.segs_per_msg, args.sock, args.pid)
if args.bpf:
print(text)
return
if args.disassemble:
BPF(text=text, debug=8)
return
b = BPF(text=text)
b.attach_kprobe(event="unix_stream_sendmsg", fn_name="probe_unix_socket_sendmsg")
b.attach_kprobe(event="unix_dgram_sendmsg", fn_name="probe_unix_socket_sendmsg")
npages = args.seg_size * args.segs_in_buffer / 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.seg_size, PCAP_LINK_TYPE)
else:
if args.output != "/dev/stdout":
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(
"--seg-size",
type=int,
default=SS_MAX_SEG_SIZE,
help="max segment size, increase this number"
" if packet size is longer than captured size",
)
parser.add_argument(
"--segs-per-msg",
type=int,
default=SS_MAX_SEGS_PER_MSG,
help="max number of iovec segments",
)
parser.add_argument(
"--segs-in-buffer",
type=int,
default=SS_MAX_SEGS_IN_BUFFER,
help="max number of segs in perf event buffer,"
" increase this number if message is dropped",
)
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("--pid", default=None, help="Pid filter, default no filter")
parser.add_argument("--bpf", action="store_true", help=argparse.SUPPRESS)
parser.add_argument("--disassemble", action="store_true", help=argparse.SUPPRESS)
parser.add_argument(
"sock",
help=" ".join(
[
"unix socket path.",
"Matches all sockets starting with given path if it ends with '*'.",
"Note that the path must be the same string used in the application,",
"instead of the actual file path.",
"If the application used a relative path, the same relative path should be used.",
"If the application runs inside a container, the path inside the container should be used.",
]
),
)
args = parser.parse_args()
main(args)