diff --git a/model/hosts.go b/model/hosts.go index a163486..3c537dc 100644 --- a/model/hosts.go +++ b/model/hosts.go @@ -38,6 +38,7 @@ type Hosts struct { CaptchaJSON string `json:"captcha_json"` //验证码配置 json AntiLeechJSON string `json:"anti_leech_json"` //防盗链配置 json CacheJSON string `json:"cache_json"` //缓存配置 json + DefaultEncoding string `json:"default_encoding"` //默认编码 utf-8 或者 gbk auto字符串自动选择 } type HostsDefense struct { diff --git a/model/request/waf_host_req.go b/model/request/waf_host_req.go index 6c56e57..465f375 100644 --- a/model/request/waf_host_req.go +++ b/model/request/waf_host_req.go @@ -33,6 +33,7 @@ type WafHostAddReq struct { CaptchaJSON string `json:"captcha_json"` //验证码配置 json AntiLeechJSON string `json:"anti_leech_json"` //防盗链配置 json CacheJSON string `json:"cache_json"` //缓存配置 json + DefaultEncoding string `json:"default_encoding"` //默认编码 utf-8 或者 gbk auto字符串自动选择 } type WafHostDelReq struct { @@ -74,6 +75,7 @@ type WafHostEditReq struct { CaptchaJSON string `json:"captcha_json"` //验证码配置 json AntiLeechJSON string `json:"anti_leech_json"` //防盗链配置 json CacheJSON string `json:"cache_json"` //缓存配置 json + DefaultEncoding string `json:"default_encoding"` //默认编码 utf-8 或者 gbk auto字符串自动选择 } type WafHostGuardStatusReq struct { diff --git a/service/waf_service/waf_host.go b/service/waf_service/waf_host.go index 877b77b..4575048 100644 --- a/service/waf_service/waf_host.go +++ b/service/waf_service/waf_host.go @@ -64,6 +64,7 @@ func (receiver *WafHostService) AddApi(wafHostAddReq request.WafHostAddReq) (str CaptchaJSON: wafHostAddReq.CaptchaJSON, AntiLeechJSON: wafHostAddReq.AntiLeechJSON, CacheJSON: wafHostAddReq.CacheJSON, + DefaultEncoding: wafHostAddReq.DefaultEncoding, } global.GWAF_LOCAL_DB.Create(wafHost) return wafHost.Code, nil @@ -118,6 +119,7 @@ func (receiver *WafHostService) ModifyApi(wafHostEditReq request.WafHostEditReq) "CaptchaJSON": wafHostEditReq.CaptchaJSON, "AntiLeechJSON": wafHostEditReq.AntiLeechJSON, "CacheJSON": wafHostEditReq.CacheJSON, + "DefaultEncoding": wafHostEditReq.DefaultEncoding, } err := global.GWAF_LOCAL_DB.Debug().Model(model.Hosts{}).Where("CODE=?", wafHostEditReq.CODE).Updates(hostMap).Error diff --git a/wafdb/patch_sql.go b/wafdb/patch_sql.go index 85c14d8..877e12e 100644 --- a/wafdb/patch_sql.go +++ b/wafdb/patch_sql.go @@ -89,7 +89,13 @@ func pathCoreSql(db *gorm.DB) { } else { zlog.Info("db", "anti_ccs :ip_mode init successfully") } - + //20250519 默认编码设置 + err = db.Exec("UPDATE hosts SET default_encoding='auto' WHERE default_encoding IS NULL").Error + if err != nil { + panic("failed to hosts: default_encoding " + err.Error()) + } else { + zlog.Info("db", "hosts: default_encoding init successfully") + } // 记录结束时间并计算耗时 duration := time.Since(startTime) zlog.Info("create core default value completely", "duration", duration.String()) diff --git a/wafenginecore/content_encoding.go b/wafenginecore/content_encoding.go index 2a3e9ba..a475db2 100644 --- a/wafenginecore/content_encoding.go +++ b/wafenginecore/content_encoding.go @@ -76,7 +76,7 @@ func (waf *WafEngine) compressContent(res *http.Response, isStaticAssist bool, i } // 获取原始内容 -func (waf *WafEngine) getOrgContent(resp *http.Response, isStaticAssist bool) (cntBytes []byte, encodeType string, err error) { +func (waf *WafEngine) getOrgContent(resp *http.Response, isStaticAssist bool, defaultEncoding string) (cntBytes []byte, encodeType string, err error) { bodyBytes, err := io.ReadAll(resp.Body) if err != nil { return bodyBytes, "", fmt.Errorf("读取原始响应体失败: %v", err) @@ -116,7 +116,19 @@ func (waf *WafEngine) getOrgContent(resp *http.Response, isStaticAssist bool) (c contentType := resp.Header.Get("Content-Type") var currentEncoding encoding.Encoding - if strings.Contains(contentType, "charset=") { + if defaultEncoding != "" { + switch strings.ToLower(defaultEncoding) { + case "utf-8", "utf8": + currentEncoding = unicode.UTF8 + charsetNameFinal = "utf-8" + break + case "gbk", "gb2312": + currentEncoding = simplifiedchinese.GBK + charsetNameFinal = "gbk" + break + default: + } + } else if strings.Contains(contentType, "charset=") { charsetName := "" parts := strings.Split(contentType, ";") for _, part := range parts { diff --git a/wafenginecore/wafengine.go b/wafenginecore/wafengine.go index ae46749..ab37f6f 100644 --- a/wafenginecore/wafengine.go +++ b/wafenginecore/wafengine.go @@ -763,7 +763,7 @@ func (waf *WafEngine) modifyResponse() func(*http.Response) error { } } if ldpFlag == true { - orgContentBytes, charsetName, responseEncodingError := waf.getOrgContent(resp, isStaticAssist) + orgContentBytes, charsetName, responseEncodingError := waf.getOrgContent(resp, isStaticAssist, waf.HostTarget[host].Host.DefaultEncoding) if responseEncodingError == nil { newPayload := []byte("" + utils.DeSenText(string(orgContentBytes))) finalCompressBytes, _ := waf.compressContent(resp, isStaticAssist, newPayload, charsetName) @@ -774,12 +774,12 @@ func (waf *WafEngine) modifyResponse() func(*http.Response) error { resp.Header.Set("Content-Length", strconv.FormatInt(int64(len(finalCompressBytes)), 10)) } else { resp.Body = io.NopCloser(bytes.NewBuffer(orgContentBytes)) - zlog.Warn(fmt.Sprintf("识别响应内容编码失败,隐私防护不可用 %v,请求URL: %s", responseEncodingError, r.URL.String())) + zlog.Warn(fmt.Sprintf("识别响应内容编码失败,隐私防护不可用 %v,请求URL: %s ,可以在主机其他设置里面设置默认编码", responseEncodingError, r.URL.String())) } } //编码转换,自动检测网页编码 resp *http.Response - orgContentBytes, charsetName, responseEncodingError := waf.getOrgContent(resp, isStaticAssist) + orgContentBytes, charsetName, responseEncodingError := waf.getOrgContent(resp, isStaticAssist, waf.HostTarget[host].Host.DefaultEncoding) if responseEncodingError == nil { if global.GCONFIG_RECORD_RESP == 1 { if resp.ContentLength < global.GCONFIG_RECORD_MAX_RES_BODY_LENGTH { @@ -821,7 +821,7 @@ func (waf *WafEngine) modifyResponse() func(*http.Response) error { } else { resp.Body = io.NopCloser(bytes.NewBuffer(orgContentBytes)) - zlog.Warn(fmt.Sprintf("识别响应内容编码失败,响应日志,敏感词替换 不可用 %v,请求URL: %s", responseEncodingError, r.URL.String())) + zlog.Warn(fmt.Sprintf("识别响应内容编码失败,响应日志,敏感词替换 不可用 %v,请求URL: %s ,可以在主机其他设置里面设置默认编码", responseEncodingError, r.URL.String())) } }