-
Notifications
You must be signed in to change notification settings - Fork 59
/
sql_injection.go
76 lines (73 loc) · 2 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
package uadmin
import (
"net"
"net/http"
"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 {
var err error
user := GetUserFromRequest(r)
if user == nil {
user = &User{}
}
ip := r.RemoteAddr
if ip, _, err = net.SplitHostPort(ip); err != nil {
ip = r.RemoteAddr
}
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, ")") {
Trail(CRITICAL, errMsg, "functions", key)
return true
}
// Case 7 - Sapce
if strings.Contains(key, " ") {
Trail(CRITICAL, errMsg, "space", key)
return true
}
// Case 8 - Escaping
if strings.Contains(key, "'") || strings.Contains(key, "`") {
Trail(CRITICAL, errMsg, "escaping", key)
return true
}
// Case 9 - Escaping
if strings.Contains(key, "'") || strings.Contains(key, "`") {
Trail(CRITICAL, errMsg, "escaping", key)
return true
}
} else if value != "" {
// We are depending on gorm in here
}
return false
}