forked from koangel/grapeSQLI
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.go
233 lines (204 loc) · 4.65 KB
/
common.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
package gsqli
import (
"log"
"runtime/debug"
"strings"
)
func h5_is_white(ch uint8) bool {
/*
* \t = horizontal tab = 0x09
* \n = newline = 0x0A
* \v = vertical tab = 0x0B
* \f = form feed = 0x0C
* \r = cr = 0x0D
*/
return (ch == '\t' || ch == '\n' || ch == '\v' || ch == '\f' || ch == '\r' || ch == ' ')
}
func memchr(src string, pos int, char byte) int {
if len(src) < pos {
log.Println("XSS detect slice bounds out of range: ", src, pos, char)
debug.PrintStack()
return -1
}
ipos := strings.IndexByte(src[pos:], char)
if ipos == -1 {
return -1
}
return ipos + pos
}
/* memchr2 finds a string of 2 characters inside another string
* This a specialized version of "memmem" or "memchr".
* 'memmem' doesn't exist on all platforms
*
* Porting notes: this is just a special version of
* astring.find("AB")
*
*/
func memchr2(haystack string, c0 string) int {
if len(haystack) < 2 {
return -1
}
return strings.Index(haystack, c0)
}
/**
* memmem might not exist on some systems
*/
func my_memmem(haystack string, needle string) int {
for i := 0; i < len(haystack); i++ {
if haystack[i] == needle[0] && strings.Index(haystack[i:], needle) != -1 {
return i
}
}
return -1
}
/** Find largest string containing certain characters.
*
* C Standard library 'strspn' only works for 'c-strings' (null terminated)
* This works on arbitrary length.
*
* Performance notes:
* not critical
*
* Porting notes:
* if accept is 'ABC', then this function would be similar to
* a_regexp.match(a_str, '[ABC]*'),
*/
func strlenspn(s string, accept string) int {
for i := 0; i < len(s); i++ {
/* likely we can do better by inlining this function
* but this works for now
*/
if strchr(accept, s[i]) == -1 {
return i
}
}
return len(s)
}
func strlencspn(s string, accept string) int {
for i := 0; i < len(s); i++ {
/* likely we can do better by inlining this function
* but this works for now
*/
if strchr(accept, s[i]) != -1 {
return i
}
}
return len(s)
}
func strchr(s string, ch uint8) int {
return strings.IndexByte(s, byte(ch))
}
func char_is_white(ch uint8) bool {
/* ' ' space is 0x32
'\t 0x09 \011 horizontal tab
'\n' 0x0a \012 new line
'\v' 0x0b \013 vertical tab
'\f' 0x0c \014 new page
'\r' 0x0d \015 carriage return
0x00 \000 null (oracle)
0xa0 \240 is Latin-1
*/
return strchr(" \t\n\v\f\r\240\000", ch) != -1
}
/* DANGER DANGER
* This is -very specialized function-
*
* this compares a ALL_UPPER CASE C STRING
* with a *arbitrary memory* + length
*
* Sane people would just make a copy, up-case
* and use a hash table.
*
* Required since libc version uses the current locale
* and is much slower.
*/
func cstrcasecmp(a string, b string) int {
if len(a) > len(b) || len(b) > len(a) {
return -1
}
return strings.Compare(strings.ToUpper(a), strings.ToUpper(b))
}
/**
* Case sensitive string compare.
* Here only to make code more readable
*/
func streqp(a string, b string) int {
return strings.Compare(a, b)
}
// 使用map进行搜索,比bsearch还快
func bsearch_keyword_type(key string) uint8 {
kt, has := szKeywordMap[strings.ToUpper(key)]
if has {
return kt.vtype
}
return 0
}
func is_keyword(key string) bool {
return bsearch_keyword_type(key) > 0
}
func st_clear(s *sqli_token) {
s.count = 0
s.len = 0
s.pos = 0
s.ttype = 0
s.val = ""
}
func st_assign_char(s *sqli_token, stype uint8, pos int, len int, value string) {
/* done to eliminate unused warning */
s.ttype = stype
s.pos = pos
s.len = 1
s.val = value
}
func st_assign(st *sqli_token, stype uint8, pos int, len int, value string) {
MSIZE := 32
last := len
if last >= MSIZE {
last = MSIZE - 1
}
if last < 0 {
debug.PrintStack()
return
}
st.ttype = stype
st.pos = pos
st.len = last
st.val = value[:last]
}
func st_copy(dst, src *sqli_token) {
//reflect.Copy(reflect.ValueOf(dst), reflect.ValueOf(src))
*dst = *src
/*dst.ttype = src.ttype
dst.val = src.val
dst.count = src.count
dst.len = src.len
dst.pos = src.pos
dst.str_close = src.str_close
dst.str_open = src.str_open*/
}
func st_is_arithmetic_op(st *sqli_token) bool {
ch := st.val[0]
return (st.ttype == TYPE_OPERATOR && st.len == 1 &&
(ch == '*' || ch == '/' || ch == '-' || ch == '+' || ch == '%'))
}
func st_is_unary_op(st *sqli_token) bool {
str := st.val
len := st.len
if st.ttype != TYPE_OPERATOR {
return false
}
switch len {
case 1:
return str[0] == '+' || str[0] == '-' || str[0] == '!' || str[0] == '~'
case 2:
return str[0] == '!' && str[1] == '!'
case 3:
return cstrcasecmp("NOT", str[:3]) == 0
default:
return false
}
return false
}
func streq(a, b string) bool {
return strings.Compare(a, b) == 0
}