This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
/
agent.go
179 lines (153 loc) · 3.77 KB
/
agent.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
package client
import (
"bufio"
"cloud.google.com/go/firestore"
"context"
"fmt"
"io"
"log"
"os"
"os/exec"
"os/signal"
"syscall"
)
const (
libFuzzerTimeoutExitCode = 77
libFuzzerLeakExitCode = 76
libFuzzerCrashExitCode = 1
libFuzzerOOMExitCode = -9
libFuzzerSuccessExitCode = 0
jqfCrashExitCode = 3
jqfSuccessExitCode = 0
fuzzingInterval = 3600
AgentGeneralError = 1
AgentNoPermissionError = 22
)
var lastLines []string
func appendPrefixToCmd(cmd *exec.Cmd) error {
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
go func() {
merged := io.MultiReader(stderr, stdout)
scanner := bufio.NewScanner(merged)
for scanner.Scan() {
msg := scanner.Text()
fmt.Printf("FUZZER: %s\n", msg)
lastLines = append(lastLines, msg)
if len(lastLines) > 1000 {
lastLines = lastLines[500:]
}
}
}()
return nil
}
func (c *FuzzitClient) transitionToInProgress() error {
ctx := context.Background()
job := Job{}
if c.updateDB {
// transaction doesnt work for now at go client with oauth token
jobRef := c.firestoreClient.Doc(fmt.Sprintf("orgs/%s/targets/%s/jobs/%s", c.Org, c.currentJob.TargetId, c.jobId))
docsnap, err := jobRef.Get(ctx)
if err != nil {
return err
}
err = docsnap.DataTo(&job)
if err != nil {
return err
}
if job.Status == "queued" {
_, err := jobRef.Update(ctx, []firestore.Update{{Path: "status", Value: "in progress"}})
if err != nil {
return err
}
}
}
return nil
}
func (c *FuzzitClient) transitionStatus(status string) error {
ctx := context.Background()
if !c.updateDB {
return nil
}
// transaction doesnt work for now at go client with oauth token
jobRef := c.firestoreClient.Doc(fmt.Sprintf("orgs/%s/targets/%s/jobs/%s", c.Org, c.currentJob.TargetId, c.jobId))
_, err := jobRef.Update(ctx, []firestore.Update{{Path: "status", Value: status}})
if err != nil {
return err
}
return nil
}
func (c *FuzzitClient) RunFuzzer(job Job, jobId string, updateDB bool) error {
if err := c.refreshToken(); err != nil {
return err
}
channel := make(chan os.Signal)
signal.Notify(channel, syscall.SIGTERM)
go func() {
<-channel
log.Println("VM interrupted. shutting down and relaunching...")
os.Exit(0)
}()
c.currentJob = job
c.jobId = jobId
c.updateDB = updateDB
if err := c.transitionToInProgress(); err != nil {
return err
}
if err := os.Mkdir("seed", 0644); err != nil {
return err
}
log.Println("downloading seed")
if err := c.DownloadAndExtractSeed("./seed", c.currentJob.TargetId); err != nil {
if err.Error() == "404 Not Found" {
log.Println("no seed corpus. continue...")
} else {
return err
}
}
log.Println("downloading corpus")
if err := c.DownloadAndExtractCorpus(".", c.currentJob.TargetId); err != nil {
if err.Error() == "404 Not Found" {
log.Println("no generating corpus yet. continue...")
} else {
return err
}
}
if err := createDirIfNotExist("corpus"); err != nil {
return err
}
if jobId != "" {
log.Println("downloading fuzzer")
if err := c.DownloadAndExtractFuzzer(".", c.currentJob.TargetId, jobId); err != nil {
return err
}
log.Println("downloading additional corpus")
if err := c.downloadAndExtract(
"additional-corpus",
fmt.Sprintf("orgs/%s/targets/%s/jobs/%s/additional-corpus", c.Org, c.currentJob.TargetId, c.jobId)); err != nil {
if err.Error() == "404 Not Found" {
log.Println("no additional-corpus. skipping...")
} else {
return err
}
}
}
if err := createDirIfNotExist("additional-corpus"); err != nil {
return err
}
var err error
if c.currentJob.Engine == "jqf" {
err = c.RunJQF()
} else if c.currentJob.Engine == "go-fuzz" {
err = c.runGoFuzz()
} else {
err = c.runLibFuzzer()
}
return err
}