-
Notifications
You must be signed in to change notification settings - Fork 13
/
fuzzer.go
197 lines (168 loc) · 4.87 KB
/
fuzzer.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
package client
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"syscall"
"github.com/richo/roving/types"
)
var invalidFuzzerNames *regexp.Regexp
func init() {
invalidFuzzerNames = regexp.MustCompile("[^a-zA-Z0-9_-]")
}
// Fuzzer runs an AFL fuzzer by shelling out to `afl-fuzz`.
// It keeps track of the fuzzer's process, and of its
// progress using an AflFileManager.
type Fuzzer struct {
Id string
fileManager *types.AflFileManager
started bool
cmd *exec.Cmd
}
// run starts the fuzzer and sets up its output pipes.
// Once the fuzz command has started, run should never return
// unless something goes wrong with the command.
func (f *Fuzzer) run() error {
var err error
cwd, err := os.Getwd()
if err != nil {
log.Fatalf("Couldn't get cwd: %s", err)
}
log.Printf("Starting fuzzer in %s", cwd)
cmd := f.cmd
log.Printf("%s %s", cmd.Path, strings.Join(cmd.Args, " "))
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatalf("Couldn't get stdout handle: %s", err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
log.Fatalf("Couldn't get stderr handle: %s", err)
}
if err := cmd.Start(); err != nil {
log.Fatalf("Couldn't start fuzzer: %s", err)
}
go func() {
io.Copy(os.Stdout, stdout)
}()
go func() {
io.Copy(os.Stderr, stderr)
}()
log.Printf("Started fuzzer")
f.started = true
return cmd.Wait()
}
// stop pauses the fuzz process by sending it a SIGSTOP.
func (f *Fuzzer) stop() {
log.Printf("Stopping the fuzzer")
f.cmd.Process.Signal(syscall.SIGSTOP)
}
// start restarts the fuzz process after it has been stopped
// by sending it a SIGCONT.
func (f *Fuzzer) start() {
log.Printf("Starting the fuzzer")
f.cmd.Process.Signal(syscall.SIGCONT)
}
// hasBegunFuzzing returns whether the fuzz command process has
// started fuzzing. This is distinct from "running". hasBegunFuzzing
// does not test for the process liveness, but instead whether the
// fuzz process has made it past the initialization phase and has
// begun the actual task of fuzzing.
func (f *Fuzzer) hasBegunFuzzing() bool {
_, err := os.Stat(f.fileManager.FuzzerStatsPath())
return !os.IsNotExist(err)
}
// ReadState returns the State of the Fuzzer.
func (f *Fuzzer) ReadState() (types.State, error) {
aflOutput, err := f.fileManager.ReadOutput()
if err != nil {
return types.State{}, err
}
stats, err := f.fileManager.ReadFuzzerStats()
if err != nil {
return types.State{}, err
}
return types.State{
Id: f.Id,
Stats: *stats,
AflOutput: aflOutput,
}, nil
}
// newAFLFuzzer returns a new fuzzer.
func newAFLFuzzer(targetCommand []string, workdir string, dictPath string, timeoutMs int, memLimitMb int) Fuzzer {
name, err := os.Hostname()
if err != nil {
log.Fatal("Couldn't get hostname", err)
}
id := mkFuzzerId(name)
fileManager := types.NewAflFileManagerWithFuzzerId(workdir, id)
fuzzCmd := aflFuzzCmd(
id,
targetCommand,
fileManager.OutputDirToPassIntoAfl(),
fileManager.InputDir(),
dictPath,
aflFuzzPath(),
timeoutMs,
memLimitMb,
)
return Fuzzer{
Id: id,
fileManager: fileManager,
started: false,
cmd: fuzzCmd,
}
}
// aflFuzzPath returns the path to afl-fuzz. It first looks for an env var
// called `AFL`, which should be the path to the dir that afl-fuzz is in.
// If it does not find this var then it defaults to `afl-fuzz` and hopes
// that this is in PATH.
func aflFuzzPath() string {
root := os.Getenv("AFL")
if root == "" {
return "afl-fuzz"
}
return fmt.Sprintf("%s/afl-fuzz", root)
}
// aflFuzzCmd constucts an afl-fuzz Cmd out of the given options.
func aflFuzzCmd(fuzzerId string, targetCommand []string, outputPath string, inputPath string, dictPath string, aflFuzzPath string, timeoutMs int, memLimitMb int) *exec.Cmd {
cmdFlags := []string{
"-S", fuzzerId,
"-o", outputPath,
"-i", inputPath,
}
if timeoutMs != 0 {
cmdFlags = append(cmdFlags, "-t", strconv.Itoa(timeoutMs))
}
if memLimitMb != 0 {
cmdFlags = append(cmdFlags, "-m", strconv.Itoa(memLimitMb))
}
if dictPath != "" {
cmdFlags = append(cmdFlags, "-x", dictPath)
}
cmdFullArgs := append(cmdFlags, targetCommand...)
c := exec.Command(aflFuzzPath, cmdFullArgs...)
return c
}
// mkFuzzerId builds a fuzzerId out of a hostname and a random 4 char hexstring.
// It replaces non-alphanumeric chars in the hostname with underscores, and
// truncates it to 27 chars.
func mkFuzzerId(hostname string) string {
validHostname := invalidFuzzerNames.ReplaceAllString(hostname, "_")
// Max AFL fuzzer ID length is 32:
// https://github.com/mirrorer/afl/blob/2fb5a3482ec27b593c57258baae7089ebdc89043/afl-fuzz.c#L7456
//
// Our fuzzer ID is ${hostname}-xxxx, so the hostname portion can
// be max 32 - 5 = 27 chars.
maxHostnameLen := 27
if len(hostname) > maxHostnameLen {
hostname = hostname[0:maxHostnameLen]
}
number := types.RandInt() & 0xffff
return fmt.Sprintf("%s-%x", validHostname, number)
}