Skip to content

Commit f5c4d69

Browse files
authored
feat(filter): Add support for capturing traffic based on user ID (#233)
1 parent 924c6fa commit f5c4d69

21 files changed

+98
-3
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ Multiple interfaces:
7676
sudo ptcpdump -i eth0 -i lo
7777
```
7878

79-
Filter by process:
79+
Filter by process or user:
8080

8181
```
8282
sudo ptcpdump -i any --pid 1234 --pid 233 -f
8383
sudo ptcpdump -i any --pname curl
84+
sudo ptcpdump -i any --uid 1000
8485
```
8586

8687
Capture by process via run target program:
@@ -146,6 +147,7 @@ With `-v`:
146147
13:44:41.529003 eth0 In IP (tos 0x4, ttl 45, id 45428, offset 0, flags [DF], proto TCP (6), length 52)
147148
139.178.84.217.443 > 172.19.0.2.42606: Flags [.], cksum 0x5284, seq 3173118145, ack 1385712707, win 118, options [nop,nop,TS val 134560683 ecr 1627716996], length 0
148149
Process (pid 553587, cmd /usr/bin/wget, args wget kernel.org)
150+
User (uid 1000)
149151
ParentProc (pid 553296, cmd /bin/sh, args sh)
150152
Container (name test, id d9028334568bf75a5a084963a8f98f78c56bba7f45f823b3780a135b71b91e95, image docker.io/library/alpine:3.18, labels {"io.cri-containerd.kind":"container","io.kubernetes.container.name":"test","io.kubernetes.pod.name":"test","io.kubernetes.pod.namespace":"default","io.kubernetes.pod.uid":"9e4bc54b-de48-4b1c-8b9e-54709f67ed0c"})
151153
Pod (name test, namespace default, UID 9e4bc54b-de48-4b1c-8b9e-54709f67ed0c, labels {"run":"test"}, annotations {"kubernetes.io/config.seen":"2024-07-21T12:41:00.460249620Z","kubernetes.io/config.source":"api"})
@@ -277,7 +279,7 @@ Flags:
277279
--container-id string Filter by container id (only TCP and UDP packets are supported)
278280
--container-name string Filter by container name (only TCP and UDP packets are supported)
279281
--containerd-address string Address of containerd service (default "/run/containerd/containerd.sock")
280-
--context strings Specify which context information to include in the output (default [process,parentproc,container,pod])
282+
--context strings Specify which context information to include in the output (default [process,thread,parentproc,user,container,pod])
281283
--count Print only on stdout the packet count when reading capture file instead of parsing/printing the packets
282284
--cri-runtime-address string Address of CRI container runtime service (default: uses in order the first successful one of [/var/run/dockershim.sock, /var/run/cri-dockerd.sock, /run/crio/crio.sock, /run/containerd/containerd.sock])
283285
--delay-before-handle-packet-events duration Delay some durations before handle packet events
@@ -311,6 +313,7 @@ Flags:
311313
-c, --receive-count uint Exit after receiving count packets
312314
-s, --snapshot-length uint32 Snarf snaplen bytes of data from each packet rather than the default of 262144 bytes (default 262144)
313315
--time-stamp-precision string When capturing, set the time stamp precision for the capture to the format (default "micro")
316+
--uid uints Filter by user IDs (only TCP and UDP packets are supported) (default [])
314317
-v, --verbose count When parsing and printing, produce (slightly more) verbose output
315318
--version Print the ptcpdump and libpcap version strings and exit
316319
-w, --write-file string Write the raw packets to file rather than parsing and printing them out. They can later be printed with the -r option. Standard output is used if file is '-'. e.g. ptcpdump.pcapng
@@ -334,6 +337,7 @@ Flags:
334337
| -r *-* || |
335338
| --pid *process_id* | ||
336339
| --pname *process_name* | ||
340+
| --uid *user_id* | ||
337341
| --container-id *container_id* | ||
338342
| --container-name *container_name* | ||
339343
| --pod-name *pod_name.namespace* | ||

bpf/bpf.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type BPF struct {
4242
type Options struct {
4343
haveFilter uint8
4444
pids []uint32
45+
uids []uint32
4546
comm [16]int8
4647
filterComm uint8
4748
followForks uint8
@@ -517,6 +518,14 @@ func (b *BPF) applyFilters() error {
517518
}
518519
}
519520

521+
log.Infof("start to update FilterUidMap with %+v", opts.pids)
522+
for _, uid := range opts.uids {
523+
uid := uid
524+
if err := b.objs.BpfMaps.FilterUidMap.Update(uid, value, ebpf.UpdateAny); err != nil {
525+
return fmt.Errorf("update FilterUidMap: %w", err)
526+
}
527+
}
528+
520529
log.Infof("start to update FilterPidnsMap with %+v", opts.pidnsIds)
521530
for _, id := range opts.pidnsIds {
522531
id := id
@@ -565,6 +574,16 @@ func (opts *Options) WithPids(pids []uint) *Options {
565574
return opts
566575
}
567576

577+
func (opts *Options) WithUids(uids []uint) *Options {
578+
for _, id := range uids {
579+
opts.uids = append(opts.uids, uint32(id))
580+
}
581+
if len(opts.uids) > 0 {
582+
opts.haveFilter = 1
583+
}
584+
return opts
585+
}
586+
568587
func (opts *Options) WithComm(comm string) *Options {
569588
opts.comm = [16]int8{}
570589
if len(comm) > 0 {

bpf/bpf_arm64_bpfel.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bpf/bpf_arm64_bpfel.o

14 KB
Binary file not shown.

bpf/bpf_legacy_arm64_bpfel.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bpf/bpf_legacy_arm64_bpfel.o

6.66 KB
Binary file not shown.

bpf/bpf_legacy_x86_bpfel.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bpf/bpf_legacy_x86_bpfel.o

6.66 KB
Binary file not shown.

bpf/bpf_no_tracing_arm64_bpfel.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bpf/bpf_no_tracing_arm64_bpfel.o

7.89 KB
Binary file not shown.

0 commit comments

Comments
 (0)