-
Notifications
You must be signed in to change notification settings - Fork 36
/
evaluations.go
213 lines (181 loc) · 6.77 KB
/
evaluations.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
212
213
package main
import (
"fmt"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
)
func (r *Rule) evaluate(resp Response, urlInjection UrlInjection, ruleName string, heuristicsResponse Response, baselineResponse Response) RuleEvaluation {
headersExpected := false
bodyExpected := false
codeExpected := false
lengthExpected := false
heuristicsExpected := map[string]bool{"responsecode": false, "responselength": false, "responsecontent": false, "responseheader": false}
for _, match := range r.Heuristics.BaselineMatches {
heuristicsExpected[strings.ToLower(match)] = true
}
numOfChecks := 0
var ruleEvaluation RuleEvaluation
if r.Expectation.Headers != nil || heuristicsExpected["responseheader"] {
headersExpected = true
numOfChecks += 1
}
if r.Expectation.Contents != nil || heuristicsExpected["responsecontent"] {
bodyExpected = true
numOfChecks += 1
}
if r.Expectation.Codes != nil || heuristicsExpected["responsecode"] {
codeExpected = true
numOfChecks += 1
}
if r.Expectation.Lengths != nil || heuristicsExpected["responselength"] {
lengthExpected = true
numOfChecks += 1
}
if bodyExpected {
if matched := r.evaluateContent(resp.Body, heuristicsResponse, baselineResponse, heuristicsExpected["responsecontent"]); matched {
ruleEvaluation.ChecksMatched += 1
}
}
if codeExpected {
if matched := r.evaluateStatusCode(resp.StatusCode, heuristicsResponse, baselineResponse, heuristicsExpected["responsecode"]); matched {
ruleEvaluation.ChecksMatched += 1
}
}
if headersExpected {
if matched := r.evaluateHeaders(resp.Headers, heuristicsResponse, baselineResponse, heuristicsExpected["responseheader"]); matched {
ruleEvaluation.ChecksMatched += 1
}
}
if lengthExpected {
if matched := r.evaluateContentLength(resp.ContentLength, heuristicsResponse, baselineResponse, heuristicsExpected["responselength"]); matched {
ruleEvaluation.ChecksMatched += 1
}
}
if ruleEvaluation.ChecksMatched > 0 && ruleEvaluation.ChecksMatched >= numOfChecks {
ruleEvaluation.Successful = true
u, err := url.QueryUnescape(urlInjection.InjectedUrl)
if err != nil {
u = urlInjection.InjectedUrl
}
// Sprintf expects format string and arguments so URL encoded values will show up as (MISSING)
// when printed. This will URL decode until fully decoded when printing for readability
for strings.Contains(u, "%") {
decodedUrl, err := url.QueryUnescape(u)
if err != nil {
break
}
u = decodedUrl
}
ruleEvaluation.SuccessMessage = fmt.Sprintf("[%s] successful match for %v\n", ruleName, u)
evaluationResults = append(evaluationResults, EvaluationResult{RuleName: ruleName, RuleDescription: r.Description, InjectedUrl: urlInjection.InjectedUrl})
}
return ruleEvaluation
}
func (r *Rule) evaluateContent(responseContent string, heuristicsResponse Response, baselineResponse Response, heuristicExpected bool) bool {
if heuristicExpected && len(r.Expectation.Contents) == 0 {
if heuristicsResponse.Body == baselineResponse.Body {
// This is a false positive. If the heuristics response, baseline response, and injected response all have the same response content
// It is not an indication of vulnerable functionality
if baselineResponse.Body == responseContent {
return false
}
return true
}
}
for _, content := range r.Expectation.Contents {
if strings.Contains(strings.ToLower(responseContent), strings.ToLower(content)) {
if !heuristicExpected {
return true
}
if heuristicsResponse.Body == baselineResponse.Body {
// This is a false positive. If the heuristics response, baseline response, and injected response all have the same response content
// It is not an indication of vulnerable functionality
if baselineResponse.Body == responseContent {
return false
}
return true
}
}
}
return false
}
func (r *Rule) evaluateHeaders(responseHeaders http.Header, heuristicsResponse Response, baselineResponse Response, heuristicExpected bool) bool {
for header, value := range r.Expectation.Headers {
if strings.Contains(strings.ToLower(responseHeaders.Get(header)), strings.ToLower(value)) {
if !heuristicExpected {
return true
}
if reflect.DeepEqual(heuristicsResponse.Headers, baselineResponse.Headers) {
return true
}
}
}
return false
}
func (r *Rule) evaluateStatusCode(responseCode int, heuristicsResponse Response, baselineResponse Response, heuristicExpected bool) bool {
if heuristicExpected && len(r.Expectation.Codes) == 0 {
if heuristicsResponse.StatusCode == baselineResponse.StatusCode {
// This is a false positive. If the heuristics response, baseline response, and injected response all have the same code
// It is not an indication of vulnerable functionality
if baselineResponse.StatusCode == responseCode {
return false
}
return true
}
}
for _, code := range r.Expectation.Codes {
statusCode, err := strconv.Atoi(code)
if err != nil {
continue
}
if statusCode == responseCode {
if !heuristicExpected {
return true
}
if heuristicsResponse.StatusCode == baselineResponse.StatusCode {
// This is a false positive. If the heuristics response, baseline response, and injected response all have the same code
// It is not an indication of vulnerable functionality
if baselineResponse.StatusCode == responseCode {
return false
}
return true
}
}
}
return false
}
func (r *Rule) evaluateContentLength(responseLength int, heuristicsResponse Response, baselineResponse Response, heuristicExpected bool) bool {
if heuristicExpected && len(r.Expectation.Lengths) == 0 {
if heuristicsMatch := isLengthWithinTenPercent(heuristicsResponse.ContentLength, baselineResponse.ContentLength); heuristicsMatch {
// This is a false positive. If the heuristics response, baseline response, and injected response all have the same length
// It is not an indication of vulnerable functionality
if heuristicsMatch := isLengthWithinTenPercent(baselineResponse.ContentLength, responseLength); heuristicsMatch {
return false
}
return true
}
}
for _, length := range r.Expectation.Lengths {
expectedLength, err := strconv.Atoi(length)
if err != nil {
continue
}
if withinTenPercent := isLengthWithinTenPercent(expectedLength, responseLength); withinTenPercent {
if !heuristicExpected {
return true
}
}
if heuristicsMatch := isLengthWithinTenPercent(heuristicsResponse.ContentLength, baselineResponse.ContentLength); heuristicsMatch {
// This is a false positive. If the heuristics response, baseline response, and injected response all have the same length
// It is not an indication of vulnerable functionality
if heuristicsMatch := isLengthWithinTenPercent(baselineResponse.ContentLength, responseLength); heuristicsMatch {
return false
}
return true
}
}
return false
}