251 lines
6.0 KiB
Go
251 lines
6.0 KiB
Go
package middleware
|
|
|
|
import (
|
|
"log"
|
|
"path"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/beego/beego/v2/core/logs"
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
"github.com/beego/beego/v2/server/web/context"
|
|
)
|
|
|
|
func init() {
|
|
logs.Register("filterconsole", func() logs.Logger {
|
|
return &probeFilterLogger{inner: logs.NewConsole()}
|
|
})
|
|
}
|
|
|
|
type probeFilterLogger struct {
|
|
inner logs.Logger
|
|
}
|
|
|
|
func (l *probeFilterLogger) Init(config string) error { return l.inner.Init(config) }
|
|
|
|
func (l *probeFilterLogger) WriteMsg(lm *logs.LogMsg) error {
|
|
if shouldSuppressAccessLogMsg(lm.Msg) {
|
|
return nil
|
|
}
|
|
return l.inner.WriteMsg(lm)
|
|
}
|
|
|
|
func (l *probeFilterLogger) Destroy() { l.inner.Destroy() }
|
|
func (l *probeFilterLogger) Flush() { l.inner.Flush() }
|
|
|
|
func (l *probeFilterLogger) SetFormatter(f logs.LogFormatter) { l.inner.SetFormatter(f) }
|
|
|
|
var (
|
|
ansiColorRe = regexp.MustCompile(`\x1b\[[0-9;]*m`)
|
|
accessStatusRe = regexp.MustCompile(`\|\s*(\d{3})\s*\|`)
|
|
accessMethodPath = regexp.MustCompile(`\|\s*(GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS)\s+(\S+)`)
|
|
)
|
|
|
|
func shouldSuppressAccessLogMsg(msg string) bool {
|
|
clean := ansiColorRe.ReplaceAllString(msg, "")
|
|
m := accessMethodPath.FindStringSubmatch(clean)
|
|
if len(m) < 3 {
|
|
return false
|
|
}
|
|
p := path.Clean(m[2])
|
|
status := 0
|
|
if sm := accessStatusRe.FindStringSubmatch(clean); len(sm) == 2 {
|
|
status, _ = strconv.Atoi(sm[1])
|
|
}
|
|
|
|
if p == "/" && status == 404 {
|
|
recordRootNoise()
|
|
return true
|
|
}
|
|
if IsScannerProbe(p) {
|
|
recordProbeNoise()
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
var scannerProbeFiles = map[string]struct{}{
|
|
".env": {},
|
|
"env": {},
|
|
"env.js": {},
|
|
"env.txt": {},
|
|
"config.js": {},
|
|
"config.json": {},
|
|
"config.yaml": {},
|
|
"config.yml": {},
|
|
"config.php": {},
|
|
"credentials": {},
|
|
"credentials.json": {},
|
|
"credentials.ini": {},
|
|
"secrets.json": {},
|
|
"lithic.json": {},
|
|
"sardine.json": {},
|
|
"database.sql": {},
|
|
"phpinfo": {},
|
|
"phpinfo.php": {},
|
|
"info.php": {},
|
|
"wp-config.php": {},
|
|
"wp-config.php.bak": {},
|
|
"wp-login.php": {},
|
|
"docker-compose.yml": {},
|
|
"docker-compose.yaml": {},
|
|
"composer.json": {},
|
|
"gitconfig": {},
|
|
"runtime-config.js": {},
|
|
"settings.json": {},
|
|
"index.js": {},
|
|
"aws.yml": {},
|
|
"aws.json": {},
|
|
}
|
|
|
|
var scannerProbeDirs = []string{
|
|
"/.git",
|
|
"/.svn",
|
|
"/.aws",
|
|
"/.ssh",
|
|
"/vendor/composer",
|
|
"/wp-content",
|
|
"/wp-admin",
|
|
"/middleware",
|
|
"/routers",
|
|
"/controllers",
|
|
"/models",
|
|
"/services",
|
|
"/pkg",
|
|
"/scripts",
|
|
"/conf",
|
|
"/database",
|
|
"/docs",
|
|
"/cmd",
|
|
"/version",
|
|
}
|
|
|
|
func IsScannerProbe(rawPath string) bool {
|
|
p := strings.ToLower(path.Clean("/" + strings.TrimPrefix(rawPath, "/")))
|
|
if p == "/" {
|
|
return false
|
|
}
|
|
base := path.Base(p)
|
|
if _, ok := scannerProbeFiles[base]; ok {
|
|
return true
|
|
}
|
|
if base == ".env" || base == "env" ||
|
|
strings.HasPrefix(base, ".env.") || strings.HasSuffix(base, ".env") {
|
|
return true
|
|
}
|
|
for _, dir := range scannerProbeDirs {
|
|
if p == dir || strings.HasPrefix(p, dir+"/") {
|
|
return true
|
|
}
|
|
}
|
|
switch path.Ext(base) {
|
|
case ".go", ".vue", ".ts", ".sql", ".conf":
|
|
return true
|
|
}
|
|
if strings.HasPrefix(base, "wp-") && strings.HasSuffix(base, ".bak") {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
const probeSummaryInterval = 10 * time.Minute
|
|
|
|
var probeLogState = struct {
|
|
mu sync.Mutex
|
|
suppressed int
|
|
lastSummary time.Time
|
|
rootSuppressed int
|
|
lastRootSummary time.Time
|
|
leakLogged map[string]time.Time
|
|
}{
|
|
leakLogged: make(map[string]time.Time),
|
|
}
|
|
|
|
type probeAccessLogFilter struct {
|
|
fallback beego.FilterHandler
|
|
}
|
|
|
|
func NewSecurityProbeLogFilter(fallback beego.FilterHandler) beego.FilterHandler {
|
|
return &probeAccessLogFilter{fallback: fallback}
|
|
}
|
|
|
|
func (f *probeAccessLogFilter) Filter(ctx *context.Context) bool {
|
|
p := path.Clean(ctx.Request.URL.Path)
|
|
status := ctx.ResponseWriter.Status
|
|
|
|
if p == "/" && status == 404 {
|
|
recordRootNoise()
|
|
return true
|
|
}
|
|
|
|
if !IsScannerProbe(p) {
|
|
if f.fallback != nil {
|
|
return f.fallback.Filter(ctx)
|
|
}
|
|
return false
|
|
}
|
|
|
|
spaFallback, _ := ctx.Input.GetData("spa_fallback").(bool)
|
|
contentType := strings.TrimSpace(ctx.ResponseWriter.Header().Get("Content-Type"))
|
|
|
|
if status >= 200 && status < 300 && !spaFallback && contentType != "" {
|
|
recordProbeLeak(ctx, status, contentType)
|
|
} else {
|
|
recordProbeNoise()
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func recordProbeNoise() {
|
|
probeLogState.mu.Lock()
|
|
defer probeLogState.mu.Unlock()
|
|
|
|
probeLogState.suppressed++
|
|
if !probeLogState.lastSummary.IsZero() &&
|
|
time.Since(probeLogState.lastSummary) < probeSummaryInterval {
|
|
return
|
|
}
|
|
count := probeLogState.suppressed
|
|
probeLogState.suppressed = 0
|
|
probeLogState.lastSummary = time.Now()
|
|
log.Printf("[security] 已忽略 %d 次敏感文件探测请求(.env/config/credentials 等),未发现泄漏", count)
|
|
}
|
|
|
|
func recordRootNoise() {
|
|
probeLogState.mu.Lock()
|
|
defer probeLogState.mu.Unlock()
|
|
|
|
probeLogState.rootSuppressed++
|
|
if !probeLogState.lastRootSummary.IsZero() &&
|
|
time.Since(probeLogState.lastRootSummary) < probeSummaryInterval {
|
|
return
|
|
}
|
|
count := probeLogState.rootSuppressed
|
|
probeLogState.rootSuppressed = 0
|
|
probeLogState.lastRootSummary = time.Now()
|
|
log.Printf("[http] 已忽略 %d 次根路径 404(当前 Host 未绑定站点),如需排查请用正式域名访问", count)
|
|
}
|
|
|
|
func recordProbeLeak(ctx *context.Context, status int, contentType string) {
|
|
key := ctx.Request.Method + " " + ctx.Request.URL.Path
|
|
now := time.Now()
|
|
|
|
probeLogState.mu.Lock()
|
|
last := probeLogState.leakLogged[key]
|
|
if !last.IsZero() && now.Sub(last) < probeSummaryInterval {
|
|
probeLogState.mu.Unlock()
|
|
return
|
|
}
|
|
probeLogState.leakLogged[key] = now
|
|
probeLogState.mu.Unlock()
|
|
|
|
log.Printf(
|
|
"[security][leak] 敏感文件探测命中可访问资源: %s %s -> %d (Content-Type: %s),疑似泄漏,请立即检查该路径是否应公开",
|
|
ctx.Request.Method, ctx.Request.URL.Path, status, contentType,
|
|
)
|
|
}
|