-
Notifications
You must be signed in to change notification settings - Fork 1
/
writesnoop.go
150 lines (122 loc) · 3.69 KB
/
writesnoop.go
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
// Copyright © 2018 Jonathan Pentecost <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"encoding/binary"
"flag"
"fmt"
"log"
"os"
"os/signal"
"strconv"
"strings"
"unsafe"
"github.com/iovisor/gobpf/bcc"
)
import "C"
var source = `
#include <linux/sched.h> // task_struct
#include <linux/tracepoint.h> // tracepoint__*__*
struct data_t {
u64 id;
u32 ppid;
u32 pid;
u32 tid;
char comm[16];
char output[255];
};
BPF_PERF_OUTPUT(events);
// args is from /sys/kernel/debug/tracing/events/syscalls/sys_write_enter/format
int do_trace(struct tracepoint__syscalls__sys_enter_write *args){
struct data_t data = {};
struct task_struct *task;
data.id = bpf_get_current_pid_tgid();
data.pid = data.id >> 32; // PID is higher part
task = (struct task_struct *)bpf_get_current_task();
// Some kernels, like Ubuntu 4.13.0-generic, return 0 as the real_parent->tgid.
data.ppid = task->real_parent->tgid;
if (data.pid == $$PID || data.ppid == $$PID) {
// Do nothing
} else {
return 0;
}
data.tid = data.id; // Cast and get the lower part
bpf_get_current_comm(&data.comm, sizeof(data.comm));
bpf_probe_read(&data.output, sizeof(data.output), (void *)args->buf);
events.perf_submit(args, &data, sizeof(data));
return 0;
};
`
// NOTE: This needs to match the exact fields and ordering
// as is in the bpf programs for the 'data_t' struct.
type event struct {
Id uint64
Ppid uint32
Pid uint32
Tid uint32
Command [16]byte
Output [255]byte
}
func main() {
flag.Parse()
if len(flag.Args()) != 1 {
fmt.Printf("requires exactly one argument; a process 'pid' to filter on.\n")
return
}
pid := flag.Args()[0]
// Check that the pid argument is an integer.
if _, err := strconv.Atoi(pid); err != nil {
log.Fatalf("pid %q is not an integer: %v", pid, err)
}
// TODO(vishen): check that the pid exists?
source = strings.Replace(source, "$$PID", pid, -1)
m := bcc.NewModule(source, []string{})
defer m.Close()
// The function here can be any function defined in the BPF source code.
tracepoint, err := m.LoadTracepoint("do_trace")
if err != nil {
log.Fatalf("unable to load tracepoint: %v", err)
}
// This has to be an existing tracepoint.
if err := m.AttachTracepoint("syscalls:sys_enter_write", tracepoint); err != nil {
log.Fatalf("unable to attach tracepoint: %v", err)
}
// These need to be initialised for the BPF program to load and run.
table := bcc.NewTable(m.TableId("events"), m)
channel := make(chan []byte)
perfMap, err := bcc.InitPerfMap(table, channel)
if err != nil {
log.Fatal("unable to init perf map: %v\n", err)
}
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, os.Kill)
go func() {
var e event
for {
data := <-channel
err := binary.Read(bytes.NewBuffer(data), binary.LittleEndian, &e)
if err != nil {
fmt.Printf("failed to decode received data '%s': %s\n", data, err)
continue
}
comm := (*C.char)(unsafe.Pointer(&e.Command))
output := (*C.char)(unsafe.Pointer(&e.Output))
fmt.Printf("%q (%d,%d) OUT >> %q\n", C.GoString(comm), e.Pid, e.Ppid, C.GoString(output))
}
}()
perfMap.Start()
<-sig
perfMap.Stop()
}