forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_injection.go
81 lines (78 loc) · 2.29 KB
/
sql_injection.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
package uadmin
import (
"net/http"
"regexp"
"strings"
)
// SQLInjection is the function to check for SQL injection attacks.
// Parameters:
//
// -key: column_name, table name
// -value: WHERE key(OP)value, SET key=value, VALUES (key,key...)
//
// return true for sql injection attempt and false for safe requests
func SQLInjection(r *http.Request, key, value string) bool {
user := GetUserFromRequest(r)
if user == nil {
user = &User{}
}
ip := GetRemoteIP(r)
errMsg := "SQL Injection attempt (%s '%s'). User:" + user.Username + " IP:" + ip
if key != "" {
// Case 1 - Comment injection
if strings.Contains(key, "--") || strings.Contains(key, "#") {
Trail(CRITICAL, errMsg, "comment injection", key)
return true
}
// Case 2 - Comment injection
if strings.Contains(key, "/*") || strings.Contains(key, "*/") {
Trail(CRITICAL, errMsg, "comment injection", key)
return true
}
// Case 3 - Stacking
if strings.Contains(key, ";") {
Trail(CRITICAL, errMsg, "stacking", key)
return true
}
// Case 4 - HEX Injection
if strings.Contains(key, "0x") {
Trail(CRITICAL, errMsg, "hex injection", key)
return true
}
// Case 5 - Concatenation
if strings.Contains(key, "+") || strings.Contains(key, "||") {
Trail(CRITICAL, errMsg, "concatenation", key)
return true
}
// Case 6 - Functions
if strings.Contains(key, "(") || strings.Contains(key, ")") {
// Skip allows functions
if match, _ := regexp.MatchString(`DATE\([a-z_]*\)`, key); match {
} else if match, _ := regexp.MatchString(`YEAR\([a-z_]*\)`, key); match {
} else if match, _ := regexp.MatchString(`MONTH\([a-z_]*\)`, key); match {
} else if match, _ := regexp.MatchString(`DAY\([a-z_]*\)`, key); match {
} else {
Trail(CRITICAL, errMsg, "functions", key)
return true
}
}
// Case 7 - Space
if strings.Contains(key, " ") {
Trail(CRITICAL, errMsg, "space", key)
return true
}
// Case 8 - Escaping
if strings.Contains(key, "'") || strings.Contains(key, "`") || strings.Contains(key, "\"") {
Trail(CRITICAL, errMsg, "escaping", key)
return true
}
// Case 9 - Escaping
if strings.Contains(value, "'") || strings.Contains(value, "`") {
Trail(CRITICAL, errMsg, "escaping", value)
return true
}
}
// if value != ""
// We are depending on gorm in here
return false
}