2023-12-07 20:25:35 +08:00
|
|
|
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
|
|
|
|
|
|
|
|
|
package injectionutils
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
#cgo CFLAGS: -I./libinjection/src
|
|
|
|
|
|
|
|
|
|
#include <libinjection.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
*/
|
|
|
|
|
import "C"
|
|
|
|
|
import (
|
2023-12-09 11:46:50 +08:00
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/waf/utils"
|
|
|
|
|
"github.com/cespare/xxhash/v2"
|
2023-12-07 20:25:35 +08:00
|
|
|
"net/url"
|
2023-12-09 11:46:50 +08:00
|
|
|
"strconv"
|
2023-12-07 20:25:35 +08:00
|
|
|
"strings"
|
|
|
|
|
"unsafe"
|
|
|
|
|
)
|
|
|
|
|
|
2023-12-09 11:46:50 +08:00
|
|
|
func DetectXSSCache(input string, cacheLife utils.CacheLife) bool {
|
|
|
|
|
var l = len(input)
|
|
|
|
|
|
|
|
|
|
if l == 0 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if cacheLife <= 0 || l < 512 || l > utils.MaxCacheDataSize {
|
|
|
|
|
return DetectXSS(input)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var hash = xxhash.Sum64String(input)
|
|
|
|
|
var key = "WAF@XSS@" + strconv.FormatUint(hash, 10)
|
|
|
|
|
var item = utils.SharedCache.Read(key)
|
|
|
|
|
if item != nil {
|
|
|
|
|
return item.Value == 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = DetectXSS(input)
|
|
|
|
|
if result {
|
|
|
|
|
utils.SharedCache.Write(key, 1, fasttime.Now().Unix()+cacheLife)
|
|
|
|
|
} else {
|
|
|
|
|
utils.SharedCache.Write(key, 0, fasttime.Now().Unix()+cacheLife)
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 10:15:18 +08:00
|
|
|
// DetectXSS detect XSS in string
|
|
|
|
|
func DetectXSS(input string) bool {
|
2023-12-07 20:25:35 +08:00
|
|
|
if len(input) == 0 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 10:15:18 +08:00
|
|
|
if detectXSSOne(input) {
|
2023-12-07 20:25:35 +08:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 兼容 /PATH?URI
|
2023-12-09 11:46:50 +08:00
|
|
|
if (input[0] == '/' || strings.HasPrefix(input, "http://") || strings.HasPrefix(input, "https://")) && len(input) < 1024 {
|
2023-12-07 20:25:35 +08:00
|
|
|
var argsIndex = strings.Index(input, "?")
|
|
|
|
|
if argsIndex > 0 {
|
|
|
|
|
var args = input[argsIndex+1:]
|
|
|
|
|
unescapeArgs, err := url.QueryUnescape(args)
|
|
|
|
|
if err == nil && args != unescapeArgs {
|
2023-12-08 10:15:18 +08:00
|
|
|
return detectXSSOne(args) || detectXSSOne(unescapeArgs)
|
2023-12-07 20:25:35 +08:00
|
|
|
} else {
|
2023-12-08 10:15:18 +08:00
|
|
|
return detectXSSOne(args)
|
2023-12-07 20:25:35 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 10:15:18 +08:00
|
|
|
func detectXSSOne(input string) bool {
|
2023-12-07 20:25:35 +08:00
|
|
|
if len(input) == 0 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var cInput = C.CString(input)
|
|
|
|
|
defer C.free(unsafe.Pointer(cInput))
|
|
|
|
|
|
2023-12-08 10:15:18 +08:00
|
|
|
return C.libinjection_xss(cInput, C.size_t(len(input))) == 1
|
2023-12-07 20:25:35 +08:00
|
|
|
}
|