找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12577|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
2 y- a- F+ L1 X" A0 J, c( ]
+ o; P( F$ x/ J* {! @0 z. O一.准备工作
2 e5 h  E9 L( x$ r$ _9 ~6 N2 b7 B& J+ c4 c
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
- U6 ~+ K2 T4 v; S
: r& h" Y8 {  j* f' Q4 c% Z0 S3 Jtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
) W: G0 \0 g- f, t: y/ z& q8 D- i5 A  j, M; U6 E" z. n% B
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz- H, q% D1 Z) y& `4 T2 Y4 B( Y
1 i9 Z$ [7 S, ^$ S
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs/ v! l* G6 C* z/ k- U

, p3 {( d* T0 D) ~6 V- D9 X依赖关系:2 ]0 u$ x1 J1 w! }( E5 g& j
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:/ W6 x) X; j8 o$ d( J( M% v

: k* `: V) a2 f$ l) {9 M8 ^yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel; O0 N5 d, b4 w
modsecurty依赖的包:pcre httpd-devel libxml2 apr
$ }+ {+ M0 [3 v$ ~: H# X, l
0 r0 N/ u6 o. j) e6 o/ `yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
9 ], d9 X6 t, Q二.启用standalone模块并编译
/ i2 T" h3 n, O9 y, s% N$ ]0 D/ i0 f! @' n, U
下载modsecurity for nginx 解压,进入解压后目录执行:) X5 [1 ^3 i' f

( a' u+ t2 y- @1 d& @./autogen.sh$ T% f: J& c" S* m
./configure --enable-standalone-module --disable-mlogc0 i. j4 J& T4 Z' b# |& W- r
make
8 n0 u3 V8 a, b3 h9 G1 y# D三.nginx添加modsecurity模块
& v2 B' \0 u0 K0 y: `. `( _/ c
& i! L4 b0 |6 I在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:( E  h$ x" v7 y) g2 M$ ^; k; V
8 r! n1 O3 u# l3 R1 }! M
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine8 T9 w4 Y6 }% R' V8 m
make && make install0 L8 H$ a8 H0 I' J' z% r5 Z! T
四.添加规则
. J( ^+ L8 j2 r0 Z: N1 F5 G0 Y" c0 ^7 ^$ [# G
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。, J0 @% d% m. C! I
3 ^' b5 {: R; J/ J" G5 p: D: @
1.下载OWASP规则:- s9 u7 x" y, ?" \

0 ~* h5 E% w& a/ \git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
* i: G7 {$ W, S/ ]3 k) v; Y) p& B4 O5 x' Y9 |% n" S9 l% V6 P
mv owasp-modsecurity-crs /opt/tengine/conf/
/ l, ~6 B( S0 \* w" R& f" L, H, [( K. I! n
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
6 f  t9 @& k3 G! h1 C% q2.启用OWASP规则:
# [/ M; Z( ~- R3 @; o% |$ S+ Z# [! J# ?5 {2 v/ o
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
  _3 M" q3 P- U5 ~) E
& y8 q5 f! @5 g( w; y编辑modsecurity.conf 文件,将SecRuleEngine设置为 on1 W1 T! c+ s7 L# \! d+ r8 g

, h- _. }5 p* b- Q2 K" ~6 Powasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
  N; u7 t4 r) \/ c$ O. w& f2 n" R% Y
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
- y  p  ~1 E5 g$ f& A: yInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
8 `  z; v+ z8 ~% [, KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf7 p7 o2 C: e+ [
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
/ k5 [3 m' o3 Q% b4 p( L; h" @7 wInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
2 T4 t+ Q# T5 e7 E* ~! MInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf; e9 m% _" U9 F: p/ z1 Z% x
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
% X4 O! u  ~9 A8 t3 T五.配置nginx5 L0 s' \7 J6 x6 a
* q! O0 d9 f& W% J3 A  g6 ]
在需要启用modsecurity的主机的location下面加入下面两行即可:* q, W/ u; Z9 _! b6 C& @

- A6 m6 S) h/ e/ ?ModSecurityEnabled on;  
- E# M8 W$ f( t) a* n" x8 \ModSecurityConfig modsecurity.conf;. {5 ~: U! U  p$ e, b- U  i
下面是两个示例配置,php虚拟主机:6 A+ E# V# H0 {8 M) ^4 x. d

7 {; ~$ ^+ }$ T! i+ L1 y* \server {
! y) {0 r3 ?! o. h      listen      80;6 F8 Q: e' `8 @! J( |: o
      server_name 52os.net www.52os.net;
# b6 _2 c; b7 J# z; j$ A/ k     ' ~5 j+ K& n9 e9 m  n; w
      location ~ \.php$ {2 ~  [& Q- q* J1 l2 R
      ModSecurityEnabled on;  ; ~) }; Q" I. I1 x, n3 ], _! k
      ModSecurityConfig modsecurity.conf;2 }4 b, U- b- E: J# k' R; [
$ m- u$ ?7 @- `8 Q, L" W  V, _
      root /web/wordpress;
3 p" O" U  Z1 [$ g      index index.php index.html index.htm;
! e1 U" Y5 \4 f2 E  ! f8 X" e& s2 V9 U
      fastcgi_pass   127.0.0.1:9000;/ Z6 M& R; d6 K7 y2 D- z9 F
      fastcgi_index  index.php;
% _6 X# k* E5 h* y  K! P# Z      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
  L. q; x2 S7 x' M1 @, Z  H0 k+ M% t      include        fastcgi_params;/ }8 l/ W3 L/ k
      }
) L3 [' z5 y) q4 W1 ]8 R  }1 a3 C9 K+ C" a
upstream负载均衡:
6 u, ^# ?2 \% u1 y7 O0 ~( c) H3 b
5 e3 V# f: b# F6 Iupstream 52os.net {  l$ E1 a1 l* f
    server 192.168.1.100:8080;
; L3 z4 \5 y! o    server 192.168.1.101:8080 backup;
* s7 a+ z- q6 ^* B}
' t/ P5 G& y5 M2 L: r
% ~% e% ~' l, h* d7 Aserver {
7 ]& F: t5 ^# j0 V" Hlisten 80;
3 S" i, L4 X; w4 q: G5 A" Z0 Q& rserver_name 52os.net www.52os.net;
9 @) ]+ Y5 f% ?
* T/ C! G/ m9 z& I/ h3 Glocation / {) j1 I2 E4 O0 c! G) S# J$ N2 V) g; e
    ModSecurityEnabled on;  
4 I& |' o7 n7 F- z2 f    ModSecurityConfig modsecurity.conf;  ' y% g  ~5 M# A3 `

! k* }% d" _7 d        proxy_pass http://online;. s. ]/ G8 m' i3 R4 }$ |! m5 A
        proxy_redirect         off;
  M/ D# U- v  t4 R* K        proxy_set_header Host $host;
8 |) x( t8 b2 x1 M        proxy_set_header X-Real-IP $remote_addr;% P2 V9 Y) L2 B% V7 R' e3 \
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
4 |+ z: a$ W) c- v) d! _# _) l    }
8 W$ ]; _4 J: q3 w( w% R* B' D* U}
8 k- k, A: W9 j, R" p$ t& N六.测试
% X. i. h% c: |1 \+ Y* G; a/ K- `0 t
& G* d* _" G  ]6 _$ S0 _/ r我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
/ K3 j! X+ n. w4 N. y5 a& e7 O7 M  [5 X) v* {, z
<?php
& G, E0 D/ x7 z+ a+ @6 K# _0 Z    phpinfo();   
7 d4 X/ K+ ^. q+ ^3 V?>
0 [0 W4 U  I* G# K3 L' F3 e+ ~+ H& S在浏览器中访问:) Y4 u/ I. `' o# G) C& M1 K- f

9 c+ _+ O% h1 ]& U4 \+ Ehttp://www.52os.net/phpinfo.php?id=1 正常显示。
3 l8 h2 M& R/ d) o9 G; Vhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
5 k# q" u6 r% Q, hhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
, q) t# i* k1 t% B说明sql注入和xss已经被过滤了8 p7 |/ }; g$ A8 q5 g! o

5 Q  E0 o4 E$ V. P! K七、安装过程中排错1 w' @; k, D) y' P: }; J3 U  a
; P+ N4 J3 c/ q' u$ a
1.缺少APXS会报错! l5 J% h3 ~  W; e2 H8 m# X0 a
5 \( b$ ]+ F* h8 h. ?4 C
configure: looking for Apache module support via DSO through APXS
/ E) |. P7 V9 r/ ?; ]/ h. econfigure: error: couldn't find APXS
5 L1 s0 B* O% Wapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
! F% ]0 U  N# n: @. R解决方法:  [; o, \( R. e/ B& i. O! w, ^' n# s
, Z/ g  F. f% E: H$ F  \
yum install httpd-devel
/ q$ S. u3 ^, }2.没有pcre& [4 M- y0 ]% }# q- p& q9 w" d8 {4 l9 z. ]
9 B) n0 {2 h) D% N1 L1 k
configure: *** pcre library not found.
" k* z! k2 ~1 V% m( ~5 F  R: Oconfigure: error: pcre library is required& _& t0 \5 d2 _5 G) p  U
解决方法:
9 v0 `1 F$ t0 b$ A
. F5 y: ]1 d, q  U8 Fyum install pcre pcre-devel! N# {# y, N) m3 A: T
3.没有libxml2
0 V. y7 P! O- Q1 r  @7 X
8 b7 T" r& T! e) p
: ?6 L3 [+ l- @1 X8 D! f: g3 G( d+ V- yconfigure: *** xml library not found.8 F4 F1 M& O0 I+ t+ Q
configure: error: libxml2 is required+ X+ f' R( i9 f
解决方法:2 l+ k0 T' I% R* G) y) p# ?$ b
$ Z; @7 i/ O9 p+ ]- Y0 F" j; g3 j- G
yum install  libxml2 libxml2-devel9 s6 m0 X% u3 x  B( w
4.执行 /opt/tengine/sbin/nginx -m 时有警告5 t# w% F% |# ]! m
+ {/ T% u: r1 J; a/ c0 ~$ w
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
; ^; V: T9 D4 M( U- \* _% d7 Vnginx: [warn] ModSecurity: Loaded APR do not match with compiled!8 j" W& m; y* Y* j. y
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
3 ]+ n$ P, r& s, R0 y5 E  ^: M0 H- g7 s/ t0 e: X  R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
) L. x- D: K/ U- S  {2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
& ]8 S3 B4 C; ~$ z+ t2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
5 q0 h3 b1 ~. i0 T* J2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"" w8 Y/ @1 A( V1 B+ t0 Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"/ c) @2 m0 ]7 S, S" b  c: R) q
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.' o. t" E, H' H8 B' `# L1 _+ ]/ e
解决方法,移除低版本的APR (1.3.9)
+ d9 C8 y- `( s; O9 A  I2 ~8 B+ u, N
yum remove apr5 ?, R  i7 r9 M) Q6 M
5.Error.log中有: Audit log: Failed to lock global mutex
3 ~; \$ Q4 r! v
1 j  Z. j1 I) G, H) v+ [2 d2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
3 |9 J, X6 G. `# f4 v  t6 vglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
. D$ H1 \' l& _2 ^( M: Z3 j解决方法:/ a+ T8 _/ R" L/ h2 W: F
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:" m6 a; o. O. r

- N% i! V8 d4 r: USecAuditLogDirMode 0777
5 c4 a8 z2 p( V# `. MSecAuditLogFileMode 05506 D* d+ E* m% d8 B- @0 s
SecAuditLogStorageDir /var/log/modsecurity
: c4 N( {6 S# h# KSecAuditLogType Concurrent
4 ~& P/ b) t# E+ W参考文章:9 P5 Z3 v+ b' M1 J* E# g, u5 s
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
+ d- ]; ~( h" j- Q* _http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-19 02:27 , Processed in 0.051466 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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