local route={} function route.get_btwaf_captcha_base64() local ip=ngx.ctx.ip local token = ngx.md5(ngx.ctx.ip..'base64') local count,_ = ngx.shared.btwaf:get(token) if count then if ngx.ctx.retry<10 then ngx.ctx.retry=10 end if count > ngx.ctx.retry then local safe_count,_ = ngx.shared.drop_sum:get(ip) if not safe_count then ngx.shared.drop_sum:set(ip,1,86400) safe_count = 1 else ngx.shared.drop_sum:incr(ip,1) end local lock_time = (Config['retry_time'] * safe_count) if lock_time > 86400 then lock_time = 86400 end ngx.shared.drop_ip:set(ngx.ctx.ip,ngx.ctx.retry+1,lock_time) ngx.ctx.is_type='cc' ngx.var.waf2monitor_blocked="人机验证封锁IP|验证码验证-达到验证次数阈值已封锁IP" IpInfo.lan_ip('cc','防火墙获取验证码接口遭到该IP攻击: '..ngx.ctx.cycle..'秒内累计超过'..Config['retry']..'次请求,封锁' .. lock_time .. '秒') else ngx.var.waf2monitor_blocked="验证码验证接口" ngx.shared.btwaf:incr(token,1) end else ngx.shared.btwaf:set(token,1,Config['retry_cycle']) end ngx.var.waf2monitor_blocked="返回验证码" math.randomseed(tostring(os.time()):reverse():sub(1, 6)) local n1 = math.random(1,200) ngx.shared.btwaf:set(ip..'__captcha',BTWAF_RULES.captcha_num2[tostring(n1)],180) local file_name=BTWAF_INC..'/captcha/'..n1..'_'..BTWAF_RULES.captcha_num2[tostring(n1)]..'.png' local data=Public.re_png(file_name) return Public.get_return_state(true,ngx.encode_base64(data)) end function route.toASCII2(str,type) local temp_1='' for i = 1, #str do local temp_2=string.byte(string.sub(str,i,i)) if temp_1=='' then if type==1 then temp_1=string.char(temp_2+1) else temp_1=string.char(temp_2) end else if type==1 then temp_1=temp_1..string.char(temp_2+1) else temp_1=temp_1..string.char(temp_2) end end end return temp_1 end --@name获取Ua中的系统信息 --@param string Ua --@return table 返回系统信息 function route.getUAField(t) local tab={} local android=string.find(t,"Android") local iphone=string.find(t,"Mac") local windows=string.find(t,"Windows") if android then tab["platform"]="android" tab["ismobiledevice"]="true" end if iphone then local ipho = string.find(t,"iPhone") local mac = string.find(t,"Macintosh") if ipho then tab["platform"]="iOS" tab["ismobiledevice"]="iphone" end if mac then tab["platform"]="Mac" tab["ismobiledevice"]="PC" end end if windows then tab["platform"]="Windows" tab["ismobiledevice"]="flase" end if Public.arrlen(tab) == 0 then tab["platform"]="Other" tab["ismobiledevice"]="flase" end return tab end function route.toASCII(str,type) local temp_1='' for i = 1, #str do local temp_2=string.byte(string.sub(str,i,i)) if temp_1=='' then if type==1 then temp_1=temp_2+1 else temp_1=temp_2 end else if type==1 then temp_1=temp_1..temp_2+1 else temp_1=temp_1..temp_2 end end end return temp_1 end function route.send_Verification() local ip=ngx.ctx.ip local request_header=ngx.ctx.request_header local token = ngx.md5(ip) local count,_ = ngx.shared.btwaf:get(token) if count then if ngx.ctx.retry<10 then ngx.ctx.retry=10 end if count > ngx.ctx.retry then local safe_count,_ = ngx.shared.drop_sum:get(ip) if not safe_count then ngx.shared.drop_sum:set(ip,1,86400) safe_count = 1 else ngx.shared.drop_sum:incr(ip,1) end local lock_time = (Config['retry_time'] * safe_count) if lock_time > 86400 then lock_time = 86400 end ngx.shared.drop_ip:set(ip,ngx.ctx.retry+1,lock_time) ngx.var.waf2monitor_blocked="人机验证封锁IP|人机验证-达到验证次数阈值已封锁IP" ngx.ctx.is_type='cc' IpInfo.lan_ip('cc','人机验证页面遭到该IP攻击: '..Config['retry_cycle']..'秒内累计超过'..ngx.ctx.retry..'次请求,封锁' .. lock_time .. '秒') else ngx.shared.btwaf:incr(token,1) end else ngx.shared.btwaf:set(token,1,Config['retry_cycle']) end if not ngx.ctx.ua then ngx.ctx.ua='Mozilla/5.0 (Windows NT 10.0; Win64; x64)' end local ua_type=route.getUAField(ngx.ctx.ua) if ua_type["platform"] == "android" or ua_type["platform"]=="iOS" then local jsbody= string.format([[
此为人机校验,请输入验证码继续访问
此为人机校验,请输入验证码来继续访问 (PS: 如需关闭此功能请在防火墙中关闭增强模式 ):
正在进行人机识别,请稍等……
本站受宝塔防御系统保护
正在进行人机识别,请稍等……
本站受宝塔防御系统保护
Browser recognition in progress, please wait……
This station is protected by the BT.CN defense system
]],'%','%','%','%',ngx.md5(ip),os.time(),ngx.md5(ip),ngx.md5(ngx.ctx.ua)) ngx.header.content_type = "text/html;charset=utf8" ngx.header.Cache_Control = "no-store" ngx.status=403 ngx.say(jsbody) ngx.exit(403) end end function route.Verification_auth_btwaf() local ip=ngx.ctx.ip local uri_request_args=ngx.ctx.get_uri_args local request_header=ngx.ctx.request_header local server_name=ngx.ctx.server_name local token = ngx.md5(ip..'auth') local count,_ = ngx.shared.btwaf:get(token) if count then if ngx.ctx.retry<10 then ngx.ctx.retry=10 end if count > ngx.ctx.retry then local safe_count,_ = ngx.shared.drop_sum:get(ip) if not safe_count then ngx.shared.drop_sum:set(ip,1,86400) safe_count = 1 else ngx.shared.drop_sum:incr(ip,1) end local lock_time = (Config['retry_time'] * safe_count) if lock_time > 86400 then lock_time = 86400 end ngx.shared.drop_ip:set(ip,ngx.ctx.retry+1,lock_time) ngx.var.waf2monitor_blocked="人机验证封锁IP|达到验证次数阈值已封锁IP" ngx.ctx.is_type='cc' IpInfo.lan_ip('cc','防火墙验证码接口遭到该IP攻击: '..ngx.ctx.cycle..'秒内累计超过'..ngx.ctx.retry..'次请求,封锁' .. lock_time .. '秒') else ngx.var.waf2monitor_blocked="验证码验证失败" ngx.shared.btwaf:incr(token,1) end else ngx.shared.btwaf:set(token,1,Config['retry_cycle']) end local num2=ngx.shared.btwaf:get(ip..'__captcha') if num2 ==nil then return Public.get_return_state(false,'验证码已经过期') end if uri_request_args['captcha'] then if num2 ==string.lower(uri_request_args['captcha']) then local token='' if ngx.ctx.ua~=nil then token=ngx.md5(ip..ngx.ctx.ua..server_name..'code'..ngx.ctx.today) else token=ngx.md5(ip..server_name..'code'..ngx.ctx.today) end ngx.var.waf2monitor_blocked="验证码验证成功" local jwt_value=ngx.md5(os.time()..ip) ngx.shared.btwaf:set(token,jwt_value,7200) ngx.header.Set_Cookie =token.."="..jwt_value..';' return Public.get_return_state(true,'验证成功') else return Public.get_return_state(false,'验证码错误') end end return Public.get_return_state(false,'请填写验证码') end function route.yanzhengip(type) local ip=ngx.ctx.ip local uri_request_args=ngx.ctx.get_uri_args local request_header=ngx.ctx.request_header local server_name=ngx.ctx.server_name if type=="browser" then if uri_request_args['key'] ==nil or uri_request_args['value']==nil or uri_request_args['type']==nil or uri_request_args['fingerprint']==nil then route.send_Verification_renji(type) end local fingerprint=route.toASCII2(uri_request_args['fingerprint'],1) if uri_request_args['key'] == ngx.md5(ip) and uri_request_args['value']==route.toASCII2(ngx.md5(ngx.ctx.ua),1) and uri_request_args['type']=='96c4e20a0e951f471d32dae103e83881' then local token='' if ngx.ctx.ua~=nil then token=ngx.md5(ip..ngx.ctx.ua..server_name..type..ngx.ctx.today) else token=ngx.md5(ip..server_name..type..ngx.ctx.today) end ngx.shared.btwaf_data:set(fingerprint,ngx.md5(ip..ngx.ctx.ua),7200) local expires = ngx.cookie_time(os.time()+7200) ngx.shared.btwaf:set(token,fingerprint,7200) ngx.var.waf2monitor_blocked="人机验证成功" ngx.header.Set_Cookie =token.."="..fingerprint.."; expires=" .. expires .. "; httponly; path=/" ngx.header.content_type = "application/json;" ngx.header.Cache_Control = "no-store" ngx.status = 200 ngx.say(fingerprint) ngx.exit(200) else route.send_Verification_renji(type) end elseif type=='renji' then if uri_request_args['key'] ==nil or uri_request_args['value']==nil or uri_request_args['type']==nil then route.send_Verification_renji(type) end if uri_request_args['key'] == ngx.md5(ip) and uri_request_args['value']==ngx.md5(route.toASCII(ngx.md5(ngx.ctx.ua),0)) and uri_request_args['type']=='96c4e20a0e951f471d32dae103e83881' then local token='' if ngx.ctx.ua~=nil then token=ngx.md5(ip..ngx.ctx.ua..server_name..type..ngx.ctx.today) else token=ngx.md5(ip..server_name..type..ngx.ctx.today) end local jwt_value=ngx.md5(os.time()..ip) ngx.var.waf2monitor_blocked="人机验证成功" ngx.shared.btwaf_data:set(jwt_value,ngx.md5(ip..ngx.ctx.ua),7200) local expires = ngx.cookie_time(os.time()+7200) ngx.shared.btwaf:set(token,jwt_value,7200) ngx.header.Set_Cookie =token.."="..jwt_value.."; expires=" .. expires .. "; httponly; path=/" ngx.header.content_type = "application/json;" ngx.header.Cache_Control = "no-store" ngx.status = 200 ngx.say(token) ngx.exit(200) else route.send_Verification_renji(type) end elseif type=='huadong' then if uri_request_args['key'] ==nil or uri_request_args['value']==nil or uri_request_args['type']==nil then route.send_Verification_renji(type) end if uri_request_args['key'] == ngx.md5(ip) and uri_request_args['value']==ngx.md5(route.toASCII(ngx.md5(ngx.ctx.ua),1)) and uri_request_args['type']=='ad82060c2e67cc7e2cc47552a4fc1242' then local token='' if ngx.ctx.ua~=nil then token=ngx.md5(ip..ngx.ctx.ua..server_name..type..ngx.ctx.today) else token=ngx.md5(ip..server_name..type..ngx.ctx.today) end ngx.var.waf2monitor_blocked="人机验证成功" local jwt_value=ngx.md5(os.time()..ip) ngx.shared.btwaf_data:set(jwt_value,ngx.md5(ip..ngx.ctx.ua),7200) local expires = ngx.cookie_time(os.time()+7200) ngx.shared.btwaf:set(token,jwt_value,7200) ngx.header.Set_Cookie =token.."="..jwt_value.."; expires=" .. expires .. "; httponly" ngx.header.content_type = "application/json;" ngx.header.Cache_Control = "no-store" ngx.status = 200 ngx.say(token) ngx.exit(200) else route.send_Verification_renji(type) end end end function route.get_btwaf_drop_ip() local data = ngx.shared.drop_ip:get_keys(0) return data end function route.add_temporary_ip() local uri_request_args=ngx.ctx.get_uri_args if not uri_request_args['ip'] or not Public.is_ipaddr(uri_request_args['ip']) then return Public.get_return_state(false,'格式错误') end if not uri_request_args['timeout'] or tonumber(uri_request_args['timeout'])==nil then return Public.get_return_state(false,'超时时间错误') end local ip=uri_request_args['ip'] local timeout=tonumber(uri_request_args['timeout']) if ngx.shared.drop_ip:get(ip) then return Public.get_return_state(false,'ip已经在封锁列表中') end ngx.shared.drop_ip:set(ip,1,timeout) return Public.get_return_state(true,'添加成功') end function route.remove_btwaf_drop_ip() local uri_request_args=ngx.ctx.get_uri_args if not uri_request_args['ip'] or not Public.is_ipaddr(uri_request_args['ip']) then return Public.get_return_state(true,'格式错误') end local token2 = ngx.md5(uri_request_args['ip'] .. '_' ..'return_cc_url') local token3 = ngx.md5(uri_request_args['ip'] .. '_' ..'header_cc') ngx.shared.btwaf_data:delete(token3) ngx.shared.btwaf:delete(token2) ngx.shared.btwaf:delete(ngx.md5(uri_request_args['ip'])) ngx.shared.drop_ip:delete(uri_request_args['ip']) ngx.shared.btwaf:delete(ngx.md5(uri_request_args['ip'])) ngx.shared.btwaf:delete(ngx.md5(uri_request_args['ip']..ngx.ctx.today)) ngx.shared.btwaf:delete(ngx.md5(uri_request_args['ip']..'base64')) ngx.shared.btwaf_data:delete(uri_request_args['ip']) ngx.shared.btwaf_data:delete(uri_request_args['ip']..'_san') ngx.shared.drop_sum:delete(uri_request_args['ip']) ngx.shared.drop_sum:delete(uri_request_args['ip']..ngx.ctx.today) return Public.get_return_state(true,uri_request_args['ip'] .. '已解封') end function route.clean_btwaf_drop_ip() local data = route.get_btwaf_drop_ip() for _,value in ipairs(data) do ngx.shared.btwaf:delete(ngx.md5(value..ngx.ctx.today)) ngx.shared.btwaf:delete(ngx.md5(value..'base64')) ngx.shared.btwaf:delete(ngx.md5(value)) ngx.shared.btwaf_data:delete(value) ngx.shared.btwaf:delete(ngx.md5(value)) ngx.shared.drop_ip:delete(value) local token2 = ngx.md5(value .. '_' ..'return_cc_url') local token3 = ngx.md5(value .. '_' ..'header_cc') ngx.shared.btwaf_data:delete(token3) ngx.shared.btwaf:delete(token2) ngx.shared.btwaf_data:delete(value..'_san') ngx.shared.drop_sum:delete(value) ngx.shared.drop_sum:delete(value..ngx.ctx.today) end return Public.get_return_state(true,'已解封所有IP') end function route.clean_spider_status() for i,v in pairs(Site_config) do ngx.shared.spider:delete(i.."spider_no") ngx.shared.spider:delete(i.."spider") end end function route.yanzhengjs(type) local ip=ngx.ctx.ip local request_header=ngx.ctx.request_header if type=='renji' then local jsbody22='var cx=cx||function(p,j){var h={},m=h.lib={},n=m.Base=function(){function a(){}return{extend:function(d){a.prototype=this;var c=new a;d&&c.mixIn(d);c.$super=this;return c},create:function(){var a=this.extend();a.init.apply(a,arguments);return a},init:function(){},mixIn:function(a){for(var c in a)a.hasOwnProperty(c)&&(this[c]=a[c]);a.hasOwnProperty("toString")&&(this.toString=a.toString)},clone:function(){return this.$super.extend(this)}}}(),b=m.WordArray=n.extend({init:function(a,d){a=this.words=a||[];this.sigBytes=d!=j?d:4*a.length},toString:function(a){return(a||q).stringify(this)},concat:function(a){var d=this.words,c=a.words,g=this.sigBytes,a=a.sigBytes;this.clamp();if(g%4)for(var f=0;f>>2]|=(c[f>>>2]>>>24-8*(f%4)&255)<<24-8*((g+f)%4);else if(65535