From a89c02fd103876e894e45bf78a3cb2d57b258c1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Wed, 16 Nov 2022 15:01:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=8F=98=E9=87=8F=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0${cname}=EF=BC=8CWAF=20checkpoint=E5=A2=9E=E5=8A=A0cna?= =?UTF-8?q?me=E5=92=8CisCNAME?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/http_request.go | 2 ++ internal/waf/checkpoints/request_cname.go | 22 +++++++++++++++++ internal/waf/checkpoints/request_is_cname.go | 26 ++++++++++++++++++++ internal/waf/checkpoints/utils.go | 16 ++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 internal/waf/checkpoints/request_cname.go create mode 100644 internal/waf/checkpoints/request_is_cname.go diff --git a/internal/nodes/http_request.go b/internal/nodes/http_request.go index 8eda8f4..945e682 100644 --- a/internal/nodes/http_request.go +++ b/internal/nodes/http_request.go @@ -758,6 +758,8 @@ func (this *HTTPRequest) Format(source string) string { return strconv.FormatInt(this.requestFromTime.Unix(), 10) case "host": return this.ReqHost + case "cname": + return this.ReqServer.CNameDomain case "referer": return this.RawReq.Referer() case "referer.host": diff --git a/internal/waf/checkpoints/request_cname.go b/internal/waf/checkpoints/request_cname.go new file mode 100644 index 0000000..82c0c2f --- /dev/null +++ b/internal/waf/checkpoints/request_cname.go @@ -0,0 +1,22 @@ +package checkpoints + +import ( + "github.com/TeaOSLab/EdgeNode/internal/waf/requests" + "github.com/iwind/TeaGo/maps" +) + +type RequestCNAMECheckpoint struct { + Checkpoint +} + +func (this *RequestCNAMECheckpoint) RequestValue(req requests.Request, param string, options maps.Map, ruleId int64) (value interface{}, hasRequestBody bool, sysErr error, userErr error) { + value = req.Format("${cname}") + return +} + +func (this *RequestCNAMECheckpoint) ResponseValue(req requests.Request, resp *requests.Response, param string, options maps.Map, ruleId int64) (value interface{}, hasRequestBody bool, sysErr error, userErr error) { + if this.IsRequest() { + return this.RequestValue(req, param, options, ruleId) + } + return +} diff --git a/internal/waf/checkpoints/request_is_cname.go b/internal/waf/checkpoints/request_is_cname.go new file mode 100644 index 0000000..0328c27 --- /dev/null +++ b/internal/waf/checkpoints/request_is_cname.go @@ -0,0 +1,26 @@ +package checkpoints + +import ( + "github.com/TeaOSLab/EdgeNode/internal/waf/requests" + "github.com/iwind/TeaGo/maps" +) + +type RequestIsCNAMECheckpoint struct { + Checkpoint +} + +func (this *RequestIsCNAMECheckpoint) RequestValue(req requests.Request, param string, options maps.Map, ruleId int64) (value interface{}, hasRequestBody bool, sysErr error, userErr error) { + if req.Format("${cname}") == req.Format("${host}") { + value = 1 + } else { + value = 0 + } + return +} + +func (this *RequestIsCNAMECheckpoint) ResponseValue(req requests.Request, resp *requests.Response, param string, options maps.Map, ruleId int64) (value interface{}, hasRequestBody bool, sysErr error, userErr error) { + if this.IsRequest() { + return this.RequestValue(req, param, options, ruleId) + } + return +} diff --git a/internal/waf/checkpoints/utils.go b/internal/waf/checkpoints/utils.go index dbf954f..b1c2406 100644 --- a/internal/waf/checkpoints/utils.go +++ b/internal/waf/checkpoints/utils.go @@ -146,6 +146,22 @@ var AllCheckpoints = []*CheckpointDefinition{ Instance: new(RequestHostCheckpoint), Priority: 100, }, + { + Name: "CNAME", + Prefix: "cname", + Description: "当前网站服务CNAME,比如38b48e4f.goedge.cn", + HasParams: false, + Instance: new(RequestCNAMECheckpoint), + Priority: 100, + }, + { + Name: "是否为CNAME", + Prefix: "isCNAME", + Description: "是否为CNAME,值为1(是)或0(否)", + HasParams: false, + Instance: new(RequestIsCNAMECheckpoint), + Priority: 100, + }, { Name: "请求来源URL", Prefix: "referer",