/
main.go
211 lines (190 loc) · 4.85 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
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
package main
import (
"bytes"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"syscall"
"sort"
"strings"
)
const (
EventFork = 1
EventVFork = 2
EventClone = 3
EventExec = 4
)
var ignore = []string{
"/tmp",
"/proc",
}
func waitEvent(status syscall.WaitStatus) uint32 {
return (uint32(status)>>16) & 0xff
}
func wait(pid int) (cpid int, status syscall.WaitStatus, err error) {
cpid, err = syscall.Wait4(pid, &status, syscall.WALL, nil)
if err != nil {
return 0, 0, err
}
return cpid, status, nil
}
// traceSyscalls executes a command exec with the arguments args and calls
// the function callback for every syscall executed by the command's process.
func traceSyscalls(proc *os.Process, state func(pid int, regs *syscall.PtraceRegs)) {
// flags used with ptrace
const flags = syscall.PTRACE_O_TRACEVFORK |
syscall.PTRACE_O_TRACEFORK |
syscall.PTRACE_O_TRACECLONE |
syscall.PTRACE_O_TRACEEXEC |
syscall.PTRACE_O_TRACESYSGOOD
if err := syscall.PtraceSetOptions(proc.Pid, flags); err != nil {
log.Fatal("PtrageSetOptions", err)
}
if err := syscall.PtraceSyscall(proc.Pid, 0); err != nil {
log.Fatalf("PtraceCont: %v", err)
}
for {
signal := 0
pid, status, err := wait(-1)
if err != nil {
break
}
if status.Exited() || status.Signaled() {
continue
}
if status.Stopped() {
switch waitEvent(status) {
case EventFork, EventVFork, EventClone, EventExec:
if cpid, err := syscall.PtraceGetEventMsg(pid); err == nil {
log.Printf("process %d created new process %d", pid, cpid)
}
default:
if stopSignal := status.StopSignal(); stopSignal&0x7f != syscall.SIGTRAP {
signal = int(stopSignal)
}
var regs syscall.PtraceRegs
if syscall.PtraceGetRegs(pid, ®s) == nil {
state(pid, ®s)
}
}
}
syscall.PtraceSyscall(pid, signal)
}
}
// getString returns the C string stored at the process' memory address addr.
func getString(pid int, addr uintptr) string {
var buffer [4096]byte
if _, err := syscall.PtracePeekData(pid, addr, buffer[:]); err == nil {
if i := bytes.IndexByte(buffer[:], 0); i >= 0 && i < len(buffer) {
return string(buffer[:i])
}
}
return ""
}
// getLink returns the path to which the process' openend file descriptor
// fd belongs.
func getLink(pid int, fd int) string {
if link, err := os.Readlink(fmt.Sprintf("/proc/%d/fd/%d", pid, fd)); err == nil {
return link
}
return ""
}
// getCwd returns the process' current working directory.
func getCwd(pid int) string {
if link, err := os.Readlink(fmt.Sprintf("/proc/%d/cwd", pid)); err == nil {
return link
}
return ""
}
// inside returs true if path lies inside dir.
func inside(path, dir string) bool {
path = path + "/"
if !strings.HasSuffix(dir, "/") {
dir = dir + "/"
}
return strings.HasPrefix(path, dir)
}
// ignorePath returs true if path lies in any directory of the ignore list.
func ignorePath(path string) bool {
for _, dir := range ignore {
if inside(path, dir) {
return true
}
}
return false
}
func main() {
runtime.GOMAXPROCS(1)
runtime.LockOSThread()
if len(os.Args) <= 1 {
fmt.Fprintf(os.Stderr, "Call: %s COMMAND\n", os.Args[0])
return
}
proc, err := os.StartProcess(os.Args[1], os.Args[1:], &os.ProcAttr{
Dir: ".",
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
Sys: &syscall.SysProcAttr{
Ptrace: true,
Pdeathsig: syscall.SIGCHLD,
},
})
if err != nil {
log.Fatal(err)
}
_, status, err := wait(proc.Pid)
if err != nil {
log.Fatalf("wait: %v", err)
}
if !status.Stopped() || status.StopSignal() != syscall.SIGTRAP {
log.Fatalf("status: got %#x, want %#x", status, 0x57f)
}
paths := make(map[string]bool)
// Step 1: add the process executable path.
if link, err := os.Readlink(fmt.Sprintf("/proc/%d/exe", proc.Pid)); err == nil {
paths[link] = true
}
// Step 2: trace syscalls and add all used paths.
traceSyscalls(proc, func(pid int, regs *syscall.PtraceRegs) {
call, work, path := getSyscall(regs), "", ""
switch syscallType[call] {
case SyscallPath:
path = getString(pid, getArgAddr(regs, 0))
case SyscallFile:
path = getLink(pid, getArgInt(regs, 0))
case SyscallFilePath:
work = getLink(pid, getArgInt(regs, 0))
path = getString(pid, getArgAddr(regs, 1))
default:
return
}
if path == "" || path == "." || path == ".." {
return
}
if !filepath.IsAbs(path) {
if work == "" {
work = getCwd(pid)
}
path = filepath.Join(work, path)
}
log.Printf("%q", path)
paths[path] = true
})
// Remove paths that do not exist or shall be ignored.
for path := range paths {
if _, err := os.Stat(path); err != nil || ignorePath(path) {
log.Printf("ignoring %q", path)
delete(paths, path)
}
}
// Sort paths to make the output look nice.
list := make([]string, 0, len(paths))
for path := range paths {
list = append(list, path)
}
sort.StringSlice(list).Sort()
for _, path := range list {
fmt.Println(path)
}
}