-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (119 loc) · 3.2 KB
/
main.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
package main
import "C"
import (
"flag"
"fmt"
"io/ioutil"
"log"
"log/syslog"
"os"
"path/filepath"
"strings"
"syscall"
"unsafe"
bpf "github.com/aquasecurity/libbpfgo"
)
const (
INT_SIZE = 4
COMM_SIZE = 16
FILENAME_SIZE = 1024
PROBE_PATH = "/usr/libexec/unlinksnoop/probe.o"
)
type unlinkEvent struct {
pid int
comm, filename string
}
var files *string = flag.String("f", "", "Comma-separated list of filenames to filter")
var syslogServer *string = flag.String("s", "", "Syslog server (tcp/514) where to send logs")
func (e unlinkEvent) String() string {
return fmt.Sprintf("%v,%v,%v", e.comm, e.pid, e.filename)
}
func setupLogging() *log.Logger {
cmdline, err := ioutil.ReadFile("/proc/cmdline")
if err != nil {
log.Printf("failed to read /proc/cmdline: %v", err)
}
parts := strings.Fields(string(cmdline))
var logServer = *syslogServer
for _, p := range parts {
if strings.HasPrefix(p, "unlinksnoop.syslog=") {
s := strings.Split(p, "=")
if len(s) == 2 {
logServer = s[1]
}
}
}
if len(logServer) > 0 {
syslogWriter, err := syslog.Dial("tcp", logServer+":514", syslog.LOG_WARNING|syslog.LOG_DAEMON, "unlinksnoop")
if err == nil {
return log.New(syslogWriter, "", 0)
}
}
return log.Default()
}
func main() {
flag.Parse()
filenames := strings.Split(*files, ",")
arg0 := filepath.Base(os.Args[0])
if os.Getppid() == 1 && arg0[0] != '@' {
binary, err := ioutil.ReadFile("/proc/self/exe")
if err != nil {
log.Fatalf("failed to read in current executable into memory: %v", err)
}
if err = ioutil.WriteFile("/dev/shm/@"+arg0, binary, 0755); err != nil {
log.Fatalf("failed to copy executable to /dev/shm: %v", err)
}
err = syscall.Exec("/dev/shm/@unlinksnoop", append([]string{"@unlinksnoop"}, os.Args[1:]...), nil)
if err != nil {
log.Fatalf("failed to reexectue: %v", err)
}
} else {
logger := setupLogging()
bpfModule, err := bpf.NewModuleFromFile(PROBE_PATH)
if err != nil {
logger.Fatalf("failed to create BPF module: %v", err)
}
defer bpfModule.Close()
bpfModule.BPFLoadObject()
prog, err := bpfModule.GetProgram("handle_unlink")
if err != nil {
logger.Fatalf("failed to load BPF proogram: %v", err)
}
_, err = prog.AttachTracepoint("syscalls", "sys_enter_unlinkat")
if err != nil {
logger.Fatalf("failed to attach BPF program to tracepoint: %v", err)
}
eventsChannel := make(chan []byte)
ring, err := bpfModule.InitRingBuf("events", eventsChannel)
if err != nil {
logger.Fatalf("failed to initialize BPF ring buffer: %v", err)
}
ring.Start()
defer func() {
ring.Stop()
ring.Close()
}()
logger.Println("comm,pid,filename")
for {
buf := <-eventsChannel
var event unlinkEvent
var pid *C.int
var comm, filename *C.char
pid = (*C.int)(unsafe.Pointer(&buf[0]))
comm = (*C.char)(unsafe.Pointer(&buf[INT_SIZE]))
filename = (*C.char)(unsafe.Pointer(&buf[INT_SIZE+COMM_SIZE]))
event.pid = int(*pid)
event.comm = C.GoString(comm)
event.filename = C.GoString(filename)
if len(filenames) > 0 {
for _, f := range filenames {
if strings.Contains(event.filename, f) {
logger.Println(event)
}
}
} else {
logger.Println(event)
}
}
}
}