waf

package
v0.7.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: Apache-2.0 Imports: 35 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package waf 实现基于 Coraza 与 OWASP 核心规则集(CRS)的 Web 应用防火墙, 以实例形式供 Web 服务的站点引用。

Index

Constants

View Source
const (
	RuleCRS    = "crs"
	RuleCustom = "custom"
)

规则集类型

View Source
const (
	ModeBlock  = "block"
	ModeDetect = "detect"
)

工作模式

View Source
const (
	// BodyLimitReject 直接拒绝(413),与 Coraza 默认行为一致
	BodyLimitReject = "reject"
	// BodyLimitPartial 只检查前面不超过上限的部分,其余内容原样转发,适合上传大文件的站点
	BodyLimitPartial = "partial"
)

请求体超过检查上限时的处理方式

View Source
const ID = "waf"

ID 是模块标识。

Variables

This section is empty.

Functions

func CRSFiles

func CRSFiles() []string

CRSFiles 返回内置 CRS 的规则文件列表。

Types

type Instance

type Instance struct {
	Key  string `json:"key"`
	Name string `json:"name"`
	// Group 为列表中的分组名称,只影响管理界面的展示,留空为未分组
	Group  string `json:"group"`
	Enable bool   `json:"enable"`
	Mode   string `json:"mode"`
	// Paranoia 为 CRS 偏执等级 1~4,越高越严格、误报越多
	Paranoia          int       `json:"paranoia"`
	InboundThreshold  int       `json:"inboundThreshold"`
	OutboundThreshold int       `json:"outboundThreshold"`
	RuleSets          []RuleSet `json:"ruleSets"`
	// BodyLimitMB 是请求体检查上限(MB),0 表示默认 12.5 MB
	BodyLimitMB int `json:"bodyLimitMB"`
	// BodyLimitAction 是请求体超过上限时的处理方式:reject(默认)或 partial
	BodyLimitAction string `json:"bodyLimitAction"`
	// SkipBodyPaths 是不检查请求体的路径前缀,每行一个(如 /api/upload);请求头、URL 与参数仍照常检查
	SkipBodyPaths string `json:"skipBodyPaths"`
	// WebDAV 为 true 时放行 WebDAV 与上传用到的方法、请求头与任意内容类型,供网盘、WebDAV 等站点使用
	WebDAV bool `json:"webdav"`
	// RegexPrefilter 控制是否启用 Coraza 的正则预过滤(SecRxPreFilter):先用字面量快速排除不可能命中的正则,
	// 可降低约 16% 的检测 CPU。nil(存量配置)视为开启;已知默认防护等级下 CRS 回归用例 942220 #2 的一条样例会漏判,关闭可恢复完整检测
	RegexPrefilter *bool `json:"regexPrefilter"`
	// SafeLine 为接入雷池检测引擎的设置,可与规则组同时使用:先经雷池检测,再经规则组检查
	SafeLine SafeLine `json:"safeline"`
	Remark   string   `json:"remark"`
}

Instance 是一个 WAF 实例。

func (*Instance) Validate

func (in *Instance) Validate() error

Validate 校验实例并补全默认值。

type InstanceInfo

type InstanceInfo struct {
	Key  string `json:"key"`
	Name string `json:"name"`
}

InstanceInfo 是供其他模块选择实例的简要信息。

type InstanceView

type InstanceView struct {
	Instance
	Error   string `json:"error,omitempty"`
	Blocked int64  `json:"blocked"`
	Matched int64  `json:"matched"`
	// SafeLineError 为最近一次雷池检测失败的原因,正常时为空;SafeLineErrors 为累计失败次数
	SafeLineError  string `json:"safelineError,omitempty"`
	SafeLineErrors int64  `json:"safelineErrors"`
}

InstanceView 是带统计的实例。

type Module

type Module struct {
	// contains filtered or unexported fields
}

Module 是 WAF 模块。

func New

func New() *Module

New 创建模块。

func (*Module) HasInstance added in v0.6.3

func (m *Module) HasInstance(key string) bool

HasInstance 判断实例是否存在,供 Web 服务批量设置 WAF 引用时校验。

func (*Module) ID

func (m *Module) ID() string

ID 实现 module.Module。

func (*Module) ImportLucky

func (m *Module) ImportLucky(doc luckyimport.Document, opts luckyimport.Options) (*luckyimport.Report, error)

ImportLucky 实现 luckyimport.Importer。沿用 Lucky 的实例 key,使 Web 服务站点的引用保持有效。

func (*Module) Init

func (m *Module) Init(env module.Env) error

Init 实现 module.Module。

func (*Module) Instances

func (m *Module) Instances() []InstanceInfo

Instances 返回实例列表。

func (*Module) Middleware

func (m *Module) Middleware(key string, next http.Handler) http.Handler

Middleware 返回经过指定实例检查的处理器。实例不存在、停用或编译失败时直接放行, 每次请求时读取最新实例,修改 WAF 配置后无需重启 Web 服务。

func (*Module) Routes

func (m *Module) Routes(r module.Router)

Routes 实现 module.Module。

func (*Module) Start

func (m *Module) Start(context.Context) error

Start 实现 module.Module。

func (*Module) Stop

func (m *Module) Stop() error

Stop 实现 module.Module。

func (*Module) Summary

func (m *Module) Summary() []module.SummaryItem

Summary 实现 module.Summarizer。

func (*Module) Title

func (m *Module) Title() string

Title 实现 module.Module。

type RuleSet

type RuleSet struct {
	Enable bool   `json:"enable"`
	Remark string `json:"remark"`
	Type   string `json:"type"`
	// Param:CRS 类型为规则文件名列表(每行一个,留空表示全部);自定义类型为 SecLang 指令
	Param string `json:"param"`
}

RuleSet 是实例中的一组规则。

type SafeLine added in v0.6.3

type SafeLine struct {
	Enable bool `json:"enable"`
	// Addr 为检测引擎地址:host:port,或 unix: 开头的 Unix 套接字路径(如 unix:/data/safeline/resources/detector/snserver.sock)
	Addr string `json:"addr"`
	// TimeoutMs 为单次检测的超时(毫秒),0 表示默认 1000
	TimeoutMs int `json:"timeoutMs"`
	// BodyLimitKB 为送检的请求体上限(KB),超出部分不送检、原样转发给后端,0 表示默认 1024
	BodyLimitKB int `json:"bodyLimitKB"`
	// FailClosed 为 true 时检测引擎不可用(连接失败、超时)则拒绝请求并返回 503;默认放行,避免雷池故障拖垮站点
	FailClosed bool `json:"failClosed"`
}

SafeLine 是接入长亭雷池检测引擎(safeline-detector)的设置。雷池由用户自行部署, Gately 只把请求送去检测并按结果放行或拦截,入口、证书、认证与封禁仍由 Gately 负责。

type SafeLineTestResult added in v0.6.3

type SafeLineTestResult struct {
	// LatencyMs 为心跳往返耗时(毫秒)
	LatencyMs float64 `json:"latencyMs"`
	// Blocked 表示示例攻击请求被检测为攻击,说明检测引擎工作正常
	Blocked bool `json:"blocked"`
	// EventID 为示例攻击在雷池中的事件 ID
	EventID string `json:"eventId,omitempty"`
}

SafeLineTestResult 是测试雷池连接的结果。

func TestSafeLine added in v0.6.3

func TestSafeLine(s SafeLine) (*SafeLineTestResult, error)

TestSafeLine 测试检测引擎:先发心跳确认连接,再送检一个示例 SQL 注入请求,确认检测引擎能识别攻击。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL