使用CDN对网站通用防注入简单解决方案让网站安全坚如磐石

作为一个多年的站长,网站被攻击是常见的问题,一些人,拿起sqlmap 乱跑一通,真是烦不胜烦
那么今天就分享一种简单免费高效的防注入解决方案,无须在服务器安装任何杀毒软件,网站一行代码都不用改,与网站完全分离的有效方法,可以拦截%99的注入攻击。
现在的站长为了隐藏服务器真实ip,大部分都采用了CDN加速,但是CDN有个非常牛X的功能,大概你还不够了解,那就是阿里云CDN边缘脚本
今天不探讨边缘脚本的其他应用场景,仅说一下怎么设置,而后演示一下效果






点击发布到生产环境即可,代码是即时生效的,那么具体的防注入代码为:
args_decode = url_unescape($args)
dian = url_unescape('%27')
sql = '|\/\*.+?\*\/|(and|or)\b.+?(>|<|=|in|like)|sleep\s*?\([\d\.]+?\)|UNION.+?SELECT(\(.+\)|\s+?.+?)|UPDATE(\(.+\)|\s+?.+?)SET|INSERT\s+INTO.+?VALUES|(SELECT|DELETE)(\(.+\)|\s+?.+?\s+?)FROM(\(.+\)|\s+?.+?)|(CREATE|ALTER|DROP|TRUNCATE)\s+(TABLE|DATABASE)'
newsql = concat(dian, sql)
m1 = match_re(args_decode,newsql ,'i')

add_rsp_header('Content-Type', 'text/html;charset=UTF-8')
if m1{
    say('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gbk2312" /><title>网站防火墙</title></head><style>p {	line-height:20px;}ul{ list-style-type:none;}li{ list-style-type:none;}</style><body style=" padding:0; margin:0; font:14px/1.5 Microsoft Yahei, 宋体,sans-serif; color:#555;"><div style="margin: 0 auto; width:1000px; padding-top:70px; overflow:hidden;"><div style="width:300px; float:left; height:200px; background:url(http://404.safedog.cn/images/safedogsite/broswer_logo.jpg) no-repeat 100px 60px;"></div><div style="width:600px; float:left;"><div style=" height:40px; line-height:40px; color:#fff; font-size:16px; overflow:hidden; background:#6bb3f6; padding-left:20px;">网站防火墙</div><div style="border:1px dashed #cdcece; border-top:none; font-size:14px; background:#fff; color:#555; line-height:24px; height:220px; padding:20px 20px 0 20px; overflow-y:auto;background:#f3f7f9;"><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-weight:600; color:#fc4f03;">您的请求带有不合法参数,已被网站管理员设置拦截!</span></p><p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">可能原因:您提交的内容包含危险的攻击请求</p><p style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:1; text-indent:0px;">如何解决:</p><ul style="margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; -qt-list-indent: 1;"><li style=" margin-top:12px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">1)检查提交内容;</li><li style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">2)如网站托管,请联系空间提供商;</li><li style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">3)普通网站访客,请联系网站管理员;</li></ul></div></div></div></body></html>')
}

上面的代码我只发了GET类型的防注入,POST类型的和cookie header类型的自己可以按照样式添加
代码使用灵活比较强,可以自己进行增删改,我比较懒,直接使用了安全狗的拦截页面

那么来看下使用效果



曾经的解决方案是通过openresty lua脚本进行防注入,现在转到CDN流程处,又一次大大减轻了服务器负担,如果你的服务器配置低的可怜,那么就赶快试试这种方法吧!

阿里云cdn边缘脚本地址

如果你的服务器几十核几百G,请忽略

您可能还会对下面的文章感兴趣: