This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
sandy.go
175 lines (144 loc) · 3.62 KB
/
sandy.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
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"github.com/gobwas/glob"
"os"
"os/exec"
"strings"
"syscall"
)
type Request struct {
path string
syscall string
allowed bool
}
func requestPermission(path string) (Request, error) {
scanner := bufio.NewScanner(os.Stdin)
fmt.Println(fmt.Sprintf("Wanting to READ %s [y/n]", path))
for scanner.Scan() {
input := strings.ToLower(scanner.Text())
if input == "y" {
break
}
if scanner.Text() == "n" {
return Request{path, "READ", false}, nil
}
// Make a sounds
fmt.Printf("\a")
}
return Request{path, "READ", true}, nil
}
func Exec(bin string, args, allowedPatterns, blockedPatterns []string) (map[string]Request, error) {
var regs syscall.PtraceRegs
reqs := make(map[string]Request)
cmd := exec.Command(bin, args...)
cmd.Stderr = nil
cmd.Stdin = nil
cmd.Stdout = nil
cmd.SysProcAttr = &syscall.SysProcAttr{
Ptrace: true,
// TODO Pdeathsig a linux only
Pdeathsig: syscall.SIGKILL,
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
return nil, fmt.Errorf("error while starting: %w", err)
}
_ = cmd.Wait()
pid := cmd.Process.Pid
for {
err := syscall.PtraceGetRegs(pid, ®s)
if err != nil {
break
}
// https://stackoverflow.com/questions/33431994/extracting-system-call-name-and-arguments-using-ptrace
if regs.Orig_rax == 0 {
// TODO this is a cross-x barrier
path, err := os.Readlink(fmt.Sprintf("/proc/%d/fd/%d", pid, regs.Rdi))
if err != nil {
return nil, err
}
for _, pattern := range allowedPatterns {
g := glob.MustCompile(pattern)
matched := g.Match(path)
if matched {
matchedReq := Request{path, "READ", true}
reqs[path] = matchedReq
}
}
for _, pattern := range blockedPatterns {
g := glob.MustCompile(pattern)
matched := g.Match(path)
if matched {
matchedReq := Request{path, "READ", false}
reqs[path] = matchedReq
}
}
req, ok := reqs[path]
if !ok {
req, err := requestPermission(path)
if err != nil {
return nil, err
}
reqs[req.path] = req
// Throw and exit the command
if !req.allowed {
return nil, errors.New(fmt.Sprintf("Blocked %s on %s", req.syscall, req.path))
}
} else {
// Throw and exit the command
if !req.allowed {
return nil, errors.New(fmt.Sprintf("Blocked %s on %s", req.syscall, req.path))
}
}
}
err = syscall.PtraceSyscall(pid, 0)
if err != nil {
return nil, err
}
_, err = syscall.Wait4(pid, nil, 0, nil)
if err != nil {
return nil, err
}
}
return reqs, nil
}
type arrayFlags []string
func (i *arrayFlags) String() string {
return ""
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
// sandyUsage func is the usage handler for the sandy command
func sandyUsage() {
fmt.Printf("Usage: %s [OPTIONS] command\n", os.Args[0])
flag.PrintDefaults()
}
func main() {
var allowedPattern arrayFlags
var blockedPattern arrayFlags
// TODO add sane defaults like libc etc
allowedPattern = append(allowedPattern, "")
// overriding the Usage handler
flag.Usage = sandyUsage
flag.Var(&blockedPattern, "n", "A glob pattern for automatically blocking file reads.\nFor example, \"/etc/password.txt\" or \"*.txt\".")
flag.Var(&allowedPattern, "y", "A glob pattern for automatically allowing file reads.\nExpected format is same as -n.")
showHelp := flag.Bool("h", false, "Print Usage.")
flag.Parse()
if flag.NArg() < 1 || *showHelp {
flag.Usage()
return
}
args := flag.Args()
_, err := Exec(args[0], args[1:], allowedPattern, blockedPattern)
if err != nil {
fmt.Println(err)
}
}