-
Notifications
You must be signed in to change notification settings - Fork 7
/
sqli_token.go
108 lines (94 loc) · 2.34 KB
/
sqli_token.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
package libinjection
import "strings"
type sqliToken struct {
// position and length of token in original string
pos int
len int
// count: in type 'v', used for number of opening '@', but maybe used in other contexts
count int
category byte
strOpen byte
strClose byte
val string
}
const (
maxTokens = 5
tokenSize = 32
)
// Look forward for doubling of delimiter
//
// case 'foo' 'bar' -> foo' 'bar
//
// ending quote is not duplicated (i.e. escaped)
// since it's the wrong or EOL
func (t *sqliToken) parseStringCore(s string, length, pos, offset int, delimiter byte) int {
// offset is to skip the perhaps first quote char
var (
str = s[pos+offset:]
)
if offset > 0 {
// this is real quote
t.strOpen = delimiter
} else {
// this was a simulated quote
t.strOpen = byteNull
}
for {
index := strings.IndexByte(str, delimiter)
if index != -1 {
str = str[index:]
}
switch {
case index == -1:
// string ended with no trailing quote
// assign what we have
t.assign(sqliTokenTypeString, pos+offset, length-pos-offset, s[pos+offset:])
t.strClose = byteNull
return length
case isBackslashEscaped(s[pos+offset : pos+offset+strings.Index(s[pos+offset:], str)]):
// keep going, move ahead one character
str = str[1:]
continue
case isDoubleDelimiterEscaped(str):
// keep going, move ahead two characters
str = str[2:]
continue
default:
// hey it's a normal string
t.assign(sqliTokenTypeString, pos+offset, len(s[pos+offset:])-len(str), s[pos+offset:])
t.strClose = delimiter
return len(s) - len(str) + 1
}
}
}
func (t *sqliToken) assign(tokenType byte, pos, length int, value string) {
var last int
if length < tokenSize {
last = length
} else {
last = tokenSize - 1
}
t.category = tokenType
t.pos = pos
t.len = last
t.val = value[:last]
}
func (t *sqliToken) isUnaryOp() bool {
if t.category != sqliTokenTypeOperator {
return false
}
switch t.len {
case 1:
return t.val[0] == '+' || t.val[0] == '-' || t.val[0] == '!' || t.val[0] == '~'
case 2:
return t.val[0] == '!' && t.val[1] == '!'
case 3:
return toUpperCmp("NOT", t.val[:3])
default:
return false
}
}
func (t *sqliToken) isArithmeticOp() bool {
return t.category == sqliTokenTypeOperator && t.len == 1 &&
(t.val[0] == '*' || t.val[0] == '/' || t.val[0] == '+' || t.val[0] == '-' || t.val[0] == '%')
}