找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11252|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
% G; H/ Z7 R$ ?* ^, a0 v: \
  A3 _/ }4 q; N4 ]4 ?' U一.准备工作1 \% g; d1 S1 C8 K% m; H

4 `: ]0 B% S: }8 o系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0$ j  _- c  e& @4 V; D2 M7 `
  Z9 T- ?1 X3 [9 r$ s
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
/ I2 F2 n2 _' P) o2 I% p3 s4 U! F2 ~1 z* M2 H; b+ S. D/ @
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
4 ^6 j6 ?& }' d- J
* y/ N# r7 P/ i7 sOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs+ x  H0 z3 x+ d

1 u- X# X, `" S1 s依赖关系:
% A9 @! ^" E; m  R6 Otengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:8 l+ j4 P) i: C

8 f! c+ I5 ]/ p; ~9 Iyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel1 S+ j- k) E/ ]. c/ }$ ?. Q
modsecurty依赖的包:pcre httpd-devel libxml2 apr6 z% ?) N$ }+ p. V

$ W& a8 n! v  r" G( l- {yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel+ o( o5 v: S$ |* C
二.启用standalone模块并编译
% u) r3 m8 F: D! y! G! S* y  k6 m( V) n
下载modsecurity for nginx 解压,进入解压后目录执行:
8 b" K! k; {" m! x, x# ~
: s8 S! p: Q" m8 g./autogen.sh
# u9 v7 t$ P, D$ b./configure --enable-standalone-module --disable-mlogc
6 ]. h2 a7 Q! V. `, M, w- xmake ' i( _' ^6 r6 r  U, s# p7 H
三.nginx添加modsecurity模块9 f! P$ A5 j5 M# p& w3 ]2 |) ?% Y

* f4 R3 J7 g3 n# N9 I在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
# D" C$ Q! @7 D2 w* F1 b7 p5 k
* a  Z/ D# G, `* V5 {0 K./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine. _' I( Q4 u$ Z# y* ?
make && make install* B4 t" _8 O" E! q2 Y! `
四.添加规则
# w  J) f4 f$ e' `! [
* i1 [9 ?8 \& c' umodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。& v. D% v  D: M4 a5 C
# l2 L' u7 V# O: p" ?6 T
1.下载OWASP规则:$ o# p4 U& s  R) M0 X5 i$ K- W

; N' B$ t$ t  z: O& z- Bgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
( `: l/ V( f% E
: d" G  j: B' Y9 Q5 _mv owasp-modsecurity-crs /opt/tengine/conf/
3 R" t8 d) O* j. E
) n) k+ ^4 \7 gcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf. F0 t8 e+ h( J9 z) X
2.启用OWASP规则:1 ^! P3 m, K# p6 s& b9 t" H

9 ~4 A% j$ f& z- a  g$ c. i+ m复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。) i1 a8 \7 C4 b  A

: C+ S* S3 w) g% O. C7 v' u" j编辑modsecurity.conf 文件,将SecRuleEngine设置为 on/ V' m$ N- H7 E& M3 h1 ~6 K

. W# {! ~- U9 Nowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。' ^) ?) L* \* V/ t+ \. y

. B' s2 j9 t# T- ?& S; xInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf' e1 U8 n' g# U/ ?
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
5 q1 F/ }3 y! N6 O# R+ eInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
5 c- @  c* B% s- j! L$ VInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
6 P0 M  F; n, F' o& a' ^9 pInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf6 b1 w; R( s- T& s# o% [$ s
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
7 u2 }- ]  o7 I: v9 D- tInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf- q# {" o+ s% [5 Y. Z: x
五.配置nginx
' F1 \9 j* ]- x% R, f+ O/ r" I( }3 n) Q1 P: P8 u
在需要启用modsecurity的主机的location下面加入下面两行即可:6 [2 `8 I2 _1 \+ C& x2 X, m

* Z% J; _# p! a8 T  sModSecurityEnabled on;  . {) w  p# X( A( M1 x/ M$ j1 V  ^. r
ModSecurityConfig modsecurity.conf;; Q. Q) H6 g) W1 W; y( h; ~
下面是两个示例配置,php虚拟主机:
1 l; r2 _. ~5 {4 y$ W6 ]2 t$ O! @5 p) m" g
server {" F7 A, z; P. A( j; |6 n: q9 i# Z
      listen      80;
6 i9 f: Z$ S3 F3 X+ I' T      server_name 52os.net www.52os.net;; R' z; g$ k( u# H% g
     
  f# n. s0 }1 ?; N5 Q3 J8 \      location ~ \.php$ {7 w5 S5 c! ]. l2 S
      ModSecurityEnabled on;    t- P6 ?6 n2 M/ f' ?; j
      ModSecurityConfig modsecurity.conf;
5 _0 Y# n6 [2 K: M5 h) U& c3 B7 W5 M+ F' H2 u( Z) f& h
      root /web/wordpress;
8 N) t4 o- Z7 s% `0 a5 e      index index.php index.html index.htm;
# Y7 v7 t# V. H* X  A& L/ s  , F9 M# ]$ z, M3 G0 e0 |. m
      fastcgi_pass   127.0.0.1:9000;0 \1 q7 v7 g4 N9 i4 W5 e
      fastcgi_index  index.php;
% Q1 T1 `9 v4 I0 ?: v      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
! ^) S4 E8 t+ i7 T2 a      include        fastcgi_params;% I" z0 E. @1 U0 l" T# }$ T, k
      }
6 D! b' b+ M* [0 X3 x0 }  }/ O9 m) f4 D7 m' y
upstream负载均衡:  w) D- h4 ?9 Y5 ?9 K7 w9 O
  p/ z  g, `; f/ r
upstream 52os.net {
' h; [' ~: Q( |  }  @# F' b    server 192.168.1.100:8080;
: T" c# }$ U* A- q$ m. ?" h    server 192.168.1.101:8080 backup;
4 s  a9 s( J$ H7 Z/ x' v}3 v* [- }) J( }2 V6 F5 b

9 S+ j% b! u# C, l4 K  Userver {
% V1 Y' d; |2 O6 ]* olisten 80;1 [1 l  C2 T' K
server_name 52os.net www.52os.net;
: s* R+ H+ ~7 K& F. x: P, u" `
4 I7 i/ S" A( dlocation / {
. Y5 D6 V& B" T0 H7 r# J6 N# e    ModSecurityEnabled on;  
8 E7 Y/ A" ^/ x# b# W: |    ModSecurityConfig modsecurity.conf;  ! D9 [5 A9 i7 ~2 {% z' v- A2 ^1 Y
4 |! x  O3 l; M( ^! I) Y1 P" Y* B7 F
        proxy_pass http://online;
. ]* v9 \% g1 S( G: M2 A/ i        proxy_redirect         off;
* J9 W9 g  U" p( H/ H        proxy_set_header Host $host;  ]9 P7 Q0 \8 w2 L! J  X
        proxy_set_header X-Real-IP $remote_addr;* I- ^5 _3 e! g# y) L
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;: s9 J9 u) C5 x5 [5 {! [
    }
0 W- f7 v- X4 B; @}9 s. [" k9 V  s
六.测试
( p7 Q7 i! I) o
* J3 x9 j3 ]3 k% M+ M我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
0 M$ d$ @; f% H- v3 b8 u, a5 `/ R8 R, k0 {9 a7 x5 m
<?php
7 P+ p, t, g3 R    phpinfo();    " E5 o4 A0 y( X5 ]0 A; {
?>
' T2 t3 c) K/ Q在浏览器中访问:
/ v+ k8 ?, J/ o+ |0 t
% g& s4 Q, N8 ~http://www.52os.net/phpinfo.php?id=1 正常显示。4 i: u  F+ ~# [/ e" ^
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。4 I7 T$ {6 V( `9 W9 O! ?7 t1 T
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。7 h4 |% O6 ?% S3 W  k+ k  z! o2 Q
说明sql注入和xss已经被过滤了0 s; q) m& Q% P" |* B

( h& u3 E) N6 H  D七、安装过程中排错
! A+ H. l( F2 t7 V* x' b
( x2 T! t: P# Q* P/ |1.缺少APXS会报错
" I# t: Z+ _, a* i% }  T  E% A5 w0 P4 G6 a
configure: looking for Apache module support via DSO through APXS, b8 m& O5 x5 {* A8 E
configure: error: couldn't find APXS& y0 H0 x  D$ U, D+ g
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
6 r5 p+ p3 w) K9 Q$ S6 a+ k解决方法:
6 \% F+ m$ Y' ~+ S9 h7 `( s+ B8 b% \8 [; Z, w
yum install httpd-devel
. y4 J( h( ]/ v" E( {2.没有pcre
% `4 u! g9 b7 G# v" J6 g3 I6 U' e
/ ^( m8 D2 k! A5 v1 y0 ]+ zconfigure: *** pcre library not found.$ Z( n% Q+ w. h- @. p/ s8 F
configure: error: pcre library is required
2 O, I; D2 ^1 \8 n) k) p( t: |" B* w解决方法:7 E* B* P4 k6 w2 B- v6 H+ a! e7 U

6 D% N. f; s4 a; P) P' z4 ayum install pcre pcre-devel
0 P; S; S6 }, v# o% G. A3.没有libxml2
$ g: H6 N  S: ^) Z$ k# @
2 H7 G" j/ J/ F- a8 _
% y: v4 j0 l2 K# |+ E, r- ?configure: *** xml library not found.4 B- v9 ^+ B- ^3 X% Q. M) u, {
configure: error: libxml2 is required
9 C% |+ Q& A* d/ |( j  R# w解决方法:2 e0 H& u9 B9 L9 ]

: P- e, W$ x( q3 `+ Byum install  libxml2 libxml2-devel
# x3 `( N$ w" y' M, C+ ]; t: Q4.执行 /opt/tengine/sbin/nginx -m 时有警告: ?5 |# W5 [& k" J8 Z& B' x

/ y0 u# H) \1 J" TTengine version: Tengine/2.1.0 (nginx/1.6.2)/ E7 ^/ W, q  R/ |$ y
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
+ M' `  h" }2 b2 {; q$ D$ W# s原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
1 |) {, x$ J7 w, Z3 j% ~& U2 D# M: H2 y9 H2 }
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.2 a2 ]& j) R/ e- F0 N( X  \! U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
8 h1 @3 e- G+ I1 J2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
/ c7 M0 [4 U; |0 C* L6 b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
6 D! y* q- [" ^6 B7 {: `, A$ e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"5 d7 c6 n8 _( E! h' Q/ I
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
( y7 s# p" j* i8 }) M8 Q解决方法,移除低版本的APR (1.3.9)/ H5 I$ k7 J! H) g( H6 m

/ o6 \% Y3 L( n/ hyum remove apr! l# i" \; ^0 b; _- o7 a* a- o; T5 X
5.Error.log中有: Audit log: Failed to lock global mutex
% r( s) G3 d6 ^
8 \! h: j/ _4 U2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     & s% q. S, E8 v0 u1 Q& }: A
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
' E  M4 v$ \6 O% j解决方法:1 R& W/ R: N9 q' R# n5 k
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
5 s0 |$ }3 X6 m- x. I9 ?
0 {& s7 K& c/ Y4 P: O+ i* K( KSecAuditLogDirMode 0777
7 @8 f6 |' o! j( {SecAuditLogFileMode 0550
4 `: y. D5 c2 F  d- WSecAuditLogStorageDir /var/log/modsecurity
6 X7 I' O7 j3 z; Y0 T6 RSecAuditLogType Concurrent
: @' @! v9 n( r$ H参考文章:0 N2 v* ~6 _/ O
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX- {- c% F1 M5 J) E2 o4 d: i
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|第一站论坛 ( 蜀ICP备06004864号-6 )

GMT+8, 2026-6-2 22:55 , Processed in 0.104983 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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