设为首页 收藏本站
查看: 1176|回复: 0

[经验分享] nginx + modsecurity实现WAF功能

[复制链接]

尚未签到

发表于 2018-11-13 06:30:42 | 显示全部楼层 |阅读模式
  modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
一.准备工作
  系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0

  •   tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
  •   modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
  •   OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
  依赖关系:
  tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel  modsecurty依赖的包:pcre httpd-devel libxml2 apr
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel二.启用standalone模块并编译
  下载modsecurity for nginx 解压,进入解压后目录执行:
./autogen.sh  
./configure --enable-standalone-module --disable-mlogc
  
make
三.nginx添加modsecurity模块
  在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine  
make && make install
四.添加规则
  modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
1.下载OWASP规则:
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs  

  
mv owasp-modsecurity-crs /opt/tengine/conf/
  

  
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
2.启用OWASP规则:
  复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
  编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
  owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf  
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
  
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
  
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
  
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
  
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
  
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
五.配置nginx
  在需要启用modsecurity的主机的location下面加入下面两行即可:
ModSecurityEnabled on;  
ModSecurityConfig modsecurity.conf;
  下面是两个示例配置,php虚拟主机:
server {  
      listen      80;
  
      server_name 52os.net www.52os.net;
  

  
      location ~ \.php$ {
  
      ModSecurityEnabled on;
  
      ModSecurityConfig modsecurity.conf;
  

  
      root /web/wordpress;
  
      index index.php index.html index.htm;
  

  
      fastcgi_pass   127.0.0.1:9000;
  
      fastcgi_index  index.php;
  
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
  
      include        fastcgi_params;
  
      }
  
  }
  upstream负载均衡:
upstream 52os.net {  
    server 192.168.1.100:8080;
  
    server 192.168.1.101:8080 backup;
  
}
  

  
server {
  
listen 80;
  
server_name 52os.net www.52os.net;
  

  
location / {
  
    ModSecurityEnabled on;
  
    ModSecurityConfig modsecurity.conf;
  

  
        proxy_pass http://online;
  
        proxy_redirect         off;
  
        proxy_set_header Host $host;
  
        proxy_set_header X-Real-IP $remote_addr;
  
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  
    }
  
}
六.测试
  我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:

  在浏览器中访问:
http://www.52os.net/phpinfo.php?id=1 正常显示。  
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
  
http://www.52os.net/phpinfo.php?search=alert('xss');  返回403。
  说明sql注入和xss已经被过滤了
七、安装过程中排错
  1.缺少APXS会报错
configure: looking for Apache module support via DSO through APXS  
configure: error: couldn't find APXS
  apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
  解决方法:
yum install httpd-devel  2.没有pcre
configure: *** pcre library not found.  
configure: error: pcre library is required
  解决方法:
yum install pcre pcre-devel  3.没有libxml2
configure: *** xml library not found.  
configure: error: libxml2 is required
  解决方法:
yum install  libxml2 libxml2-devel  4.执行 /opt/tengine/sbin/nginx -m 时有警告
Tengine version: Tengine/2.1.0 (nginx/1.6.2)  
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
  原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.  
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
  
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
  
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
  
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
  
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
  解决方法,移除低版本的APR (1.3.9)
yum remove apr  5.Error.log中有: Audit log: Failed to lock global mutex
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock  
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
  解决方法:
  编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
SecAuditLogDirMode 0777  
SecAuditLogFileMode 0550
  
SecAuditLogStorageDir /var/log/modsecurity
  
SecAuditLogType Concurrent
  参考文章:
  https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
  http://drops.wooyun.org/tips/2614



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-634241-1-1.html 上篇帖子: Nginx的Rewrite设置及示例 下篇帖子: 给已经编译安装了的nginx 添加http_ssl_module模块
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表