-
Notifications
You must be signed in to change notification settings - Fork 2
/
insertion_points.go
289 lines (247 loc) · 6.86 KB
/
insertion_points.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package scan
import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"mime"
"mime/multipart"
"net/url"
"strings"
"github.com/pyneda/sukyan/db"
"github.com/pyneda/sukyan/lib"
"github.com/rs/zerolog/log"
)
type InsertionPointType string
const (
InsertionPointTypeParameter InsertionPointType = "parameter"
InsertionPointTypeHeader InsertionPointType = "header"
InsertionPointTypeBody InsertionPointType = "body"
InsertionPointTypeCookie InsertionPointType = "cookie"
InsertionPointTypeURLPath InsertionPointType = "urlpath"
)
type InsertionPoint struct {
Type InsertionPointType
Name string // the name of the parameter/header/cookie
Value string // the current value
ValueType lib.DataType // the type of the value (string, int, float, etc.)
OriginalData string // the original data (URL, header string, body, cookie string) in which this insertion point was found
}
func (i *InsertionPoint) String() string {
return fmt.Sprintf("%s: %s", i.Type, i.Name)
}
// Handle URL parameters
func handleURLParameters(urlData *url.URL) ([]InsertionPoint, error) {
var points []InsertionPoint
// URL parameters
for name, values := range urlData.Query() {
for _, value := range values {
points = append(points, InsertionPoint{
Type: "parameter",
Name: name,
Value: value,
ValueType: lib.GuessDataType(value),
OriginalData: urlData.String(),
})
}
}
return points, nil
}
// Handle URL paths
func handleURLPaths(urlData *url.URL) ([]InsertionPoint, error) {
var points []InsertionPoint
// URL parameters
for _, pathPart := range strings.Split(urlData.Path, "/") {
if pathPart == "" {
continue
}
points = append(points, InsertionPoint{
Type: InsertionPointTypeURLPath,
Name: pathPart,
Value: pathPart,
ValueType: lib.GuessDataType(pathPart),
OriginalData: urlData.String(),
})
}
return points, nil
}
// Handle Headers
func handleHeaders(header map[string][]string) ([]InsertionPoint, error) {
var points []InsertionPoint
for name, values := range header {
if name == "cookie" {
continue
}
for _, value := range values {
points = append(points, InsertionPoint{
Type: InsertionPointTypeHeader,
Name: name,
Value: value,
ValueType: lib.GuessDataType(value),
OriginalData: header[name][0],
})
}
}
return points, nil
}
// Handle Cookies
func handleCookies(header map[string][]string) ([]InsertionPoint, error) {
var points []InsertionPoint
// Cookies
if cookies, ok := header["Cookie"]; ok {
for _, cookieString := range cookies {
cookieValues := strings.Split(cookieString, ";")
for _, cookieValue := range cookieValues {
cookieParts := strings.SplitN(strings.TrimSpace(cookieValue), "=", 2)
if len(cookieParts) == 2 {
points = append(points, InsertionPoint{
Type: InsertionPointTypeCookie,
Name: cookieParts[0],
Value: cookieParts[1],
ValueType: lib.GuessDataType(cookieParts[1]),
OriginalData: cookieString,
})
}
}
}
}
return points, nil
}
// Handle Body parameters
func handleBodyParameters(contentType string, body []byte) ([]InsertionPoint, error) {
var points []InsertionPoint
// URL-encoded body
if strings.Contains(contentType, "application/x-www-form-urlencoded") {
formData, err := url.ParseQuery(string(body))
if err != nil {
return nil, err
}
for name, values := range formData {
for _, value := range values {
points = append(points, InsertionPoint{
Type: InsertionPointTypeBody,
Name: name,
Value: value,
ValueType: lib.GuessDataType(value),
OriginalData: string(body),
})
}
}
}
// JSON body
if strings.Contains(contentType, "application/json") {
var jsonData map[string]interface{}
err := json.Unmarshal(body, &jsonData)
if err != nil {
return nil, err
}
for name, value := range jsonData {
valueStr := fmt.Sprintf("%v", value)
points = append(points, InsertionPoint{
Type: InsertionPointTypeBody,
Name: name,
Value: valueStr,
ValueType: lib.GuessDataType(valueStr),
OriginalData: string(body),
})
}
}
// XML body
if strings.Contains(contentType, "application/xml") {
var xmlData map[string]interface{}
err := xml.Unmarshal(body, &xmlData)
if err != nil {
return nil, err
}
for name, value := range xmlData {
valueStr := fmt.Sprintf("%v", value)
points = append(points, InsertionPoint{
Type: InsertionPointTypeBody,
Name: name,
Value: valueStr,
ValueType: lib.GuessDataType(valueStr),
OriginalData: string(body),
})
}
}
// Multipart form body
// Multipart form body
if strings.Contains(contentType, "multipart/form-data") {
_, params, err := mime.ParseMediaType(contentType)
if err != nil {
return nil, err
}
boundary, ok := params["boundary"]
if !ok {
return nil, errors.New("Content-Type does not contain boundary parameter")
}
mr := multipart.NewReader(strings.NewReader(string(body)), boundary)
form, err := mr.ReadForm(10 << 20) // Max memory 10 MB
if err != nil {
return nil, err
}
for name, values := range form.Value {
for _, value := range values {
points = append(points, InsertionPoint{
Type: InsertionPointTypeBody,
Name: name,
Value: value,
ValueType: lib.GuessDataType(value),
OriginalData: string(body),
})
}
}
}
return points, nil
}
func GetInsertionPoints(history *db.History, scoped []string) ([]InsertionPoint, error) {
var points []InsertionPoint
// Analyze URL
urlData, err := url.Parse(history.URL)
if err != nil {
return nil, err
}
if lib.SliceContains(scoped, "parameters") {
urlPoints, err := handleURLParameters(urlData)
if err != nil {
return nil, err
}
points = append(points, urlPoints...)
}
if lib.SliceContains(scoped, "urlpath") {
urlPathPoints, err := handleURLPaths(urlData)
if err != nil {
return nil, err
}
points = append(points, urlPathPoints...)
}
// Convert datatypes.JSON to http.Header equivalent
headers, err := history.GetRequestHeadersAsMap()
if err != nil {
log.Error().Err(err).Interface("headers", history.RequestHeaders).Msg("Error getting request headers as map")
} else {
if lib.SliceContains(scoped, "headers") {
// Headers
headerPoints, err := handleHeaders(headers)
if err != nil {
return nil, err
}
points = append(points, headerPoints...)
}
if lib.SliceContains(scoped, "cookies") {
// Cookies
cookiePoints, err := handleCookies(headers)
if err != nil {
return nil, err
}
points = append(points, cookiePoints...)
}
}
// Body parameters
bodyPoints, err := handleBodyParameters(history.RequestContentType, history.RequestBody)
if err != nil {
return nil, err
}
points = append(points, bodyPoints...)
return points, nil
}