-
Notifications
You must be signed in to change notification settings - Fork 36
/
main.go
210 lines (181 loc) · 5.27 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
package main
import (
"flag"
"fmt"
"github.com/fatih/color"
"net/http"
"net/url"
"os"
"sync"
"time"
)
type Rule struct {
Description string `mapstructure:"description"`
Injections []string `mapstructure:"injections"`
ExtraParams []string `mapstructure:"extraParams"`
Expectation ExpectedResponse `mapstructure:"expectation"`
Heuristics HeuristicsRule `mapstructure:"heuristics"`
}
type HeuristicsRule struct {
Injection string `mapstructure:"injection"`
BaselineMatches []string `mapstructure:"baselineMatches"`
}
type ExpectedResponse struct {
Contents []string `mapstructure:"responseContents"`
Codes []string `mapstructure:"responseCodes"`
Headers map[string]string `mapstructure:"responseHeaders"`
Lengths []string `mapstructure:"responseLength"`
}
type UrlInjection struct {
BaselineUrl string
InjectedUrl string
HeuristicsUrl string
}
type Response struct {
StatusCode int
Body string
Headers http.Header
ContentLength int
}
type RuleEvaluation struct {
ChecksMatched int
SuccessMessage string
Successful bool
}
type EvaluationResult struct {
RuleName string
RuleDescription string
InjectedUrl string
}
type Task struct {
UrlInjection UrlInjection
RuleData Rule
RuleName string
}
var failedRequestsSent int
var successfulRequestsSent int
var config Config
var opts CliOptions
var evaluationResults []EvaluationResult
var responseCache map[string]Response
var printGreen = color.New(color.FgGreen).PrintfFunc()
var printRed = color.New(color.FgRed).FprintfFunc()
var printCyan = color.New(color.FgCyan).FprintfFunc()
var startTime = time.Now()
func main() {
err := verifyFlags(&opts)
if err != nil {
fmt.Println(err)
flag.Usage()
os.Exit(1)
}
if err := loadConfig(opts.ConfigFile); err != nil {
fmt.Println("Failed loading config:", err)
os.Exit(1)
}
urls, err := getUrlsFromFile()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Create HTTP Transport and Client after parsing flags
createClient()
if !opts.SilentMode {
printCyan(os.Stderr, "There are %v unique URL/Query String combinations. Time to inject each query string, 1 at a time!\n", len(urls))
}
tasks := make(chan Task)
var wg sync.WaitGroup
startTime := time.Now()
for i := 0; i < opts.Concurrency; i++ {
wg.Add(1)
go func() {
for task := range tasks {
task.execute()
}
wg.Done()
}()
}
for _, u := range urls {
for rule, ruleData := range config.Rules {
fullUrl, err := url.Parse(u)
// If URL can't be parsed, ignore and move on
if err != nil {
if opts.Debug {
printRed(os.Stderr, "[%v] error parsing URL or query parameters for %v\n", rule, u)
}
continue
}
injectedUrls, err := getInjectedUrls(fullUrl, ruleData)
if err != nil {
if opts.Debug {
printRed(os.Stderr, "[%v] error parsing URL or query parameters for %v\n", rule, u)
}
continue
}
if injectedUrls == nil {
continue
}
for _, injectedUrl := range injectedUrls {
tasks <- Task{RuleName: rule, RuleData: ruleData, UrlInjection: injectedUrl}
}
}
}
close(tasks)
wg.Wait()
secondsElapsed := time.Since(startTime).Seconds()
printCyan(os.Stderr, "Evaluations complete! %v successful requests sent (%v failed): %v requests per second\n", successfulRequestsSent, failedRequestsSent, int(float64(successfulRequestsSent)/secondsElapsed))
}
func (t Task) execute() {
resp, err := sendRequest(t.UrlInjection.InjectedUrl)
if err != nil {
failedRequestsSent += 1
if opts.Debug {
printRed(os.Stderr, "error sending HTTP request to %v: %v\n", t.UrlInjection.InjectedUrl, err)
}
return
}
successfulRequestsSent += 1
heuristicsResponse := Response{}
baselineResponse := Response{}
if t.RuleData.Heuristics.Injection != "" {
// Check if the baseline URL has already been requested to avoid duplicate requests
if response, ok := responseCache[t.UrlInjection.BaselineUrl]; ok {
baselineResponse = response
} else {
baselineResponse, err = sendRequest(t.UrlInjection.BaselineUrl)
if err != nil {
failedRequestsSent += 1
if opts.Debug {
printRed(os.Stderr, "error sending HTTP request to %v: %v\n", t.UrlInjection.BaselineUrl, err)
}
}
successfulRequestsSent += 1
}
heuristicsResponse, err = sendRequest(t.UrlInjection.HeuristicsUrl)
if err != nil {
failedRequestsSent += 1
if opts.Debug {
printRed(os.Stderr, "error sending HTTP request to %v: %v\n", t.UrlInjection.HeuristicsUrl, err)
}
}
successfulRequestsSent += 1
}
// Send an update every 1,000 requests
if !opts.SilentMode {
totalRequestsSent := successfulRequestsSent + failedRequestsSent
if totalRequestsSent%1000 == 0 {
secondsElapsed := time.Since(startTime).Seconds()
fmt.Fprintf(os.Stderr, "%v requests sent (%v failed): %v requests per second\n", totalRequestsSent, failedRequestsSent, int(float64(successfulRequestsSent)/secondsElapsed))
}
}
ruleEvaluation := t.RuleData.evaluate(resp, t.UrlInjection, t.RuleName, heuristicsResponse, baselineResponse)
if ruleEvaluation.Successful {
printGreen(ruleEvaluation.SuccessMessage)
if opts.ToSlack {
err = sendSlackMessage(ruleEvaluation.SuccessMessage)
if err != nil && opts.Debug {
printRed(os.Stderr, "error sending Slack message: %v\n", err)
}
}
}
}