找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11058|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。9 o. q7 o4 t; s

7 x/ V  \+ C/ [( S& j一.准备工作* p- D7 a5 K6 D" P- A6 u% _  g
1 H& X, w) c1 A3 D' u% v
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.04 G  x) w; ?- z4 [! R0 t  ^
& h8 ~8 s; v. T$ p( z: G
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
- m* k" h3 |9 q4 T( A5 D0 U& M7 t& p: k: M5 N) m9 O: H2 E. ~
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
( N* o9 s9 ?, {# d, `9 t  l
" i+ D- p. o5 A, d9 `; l! F8 o  FOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
& g% ]3 W' @  [& S7 |9 k8 G7 n
3 t4 V! k/ m9 k5 A. a0 W; f依赖关系:
. S5 g$ j/ `) Dtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
3 T& t; L' |7 K9 W  ~' `9 @/ ?! {! E* n- I( A
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
* J$ C0 p$ H6 b+ h# Fmodsecurty依赖的包:pcre httpd-devel libxml2 apr5 i; G% e* Y8 }! C: G6 U1 d
9 Y3 q6 m$ G) P, o; b. r2 u' {
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel0 ]4 ~7 N( K% b
二.启用standalone模块并编译3 r- f* P, N* Q  x' Y
8 ~" ~0 |9 L! R0 m( _* u9 E' L
下载modsecurity for nginx 解压,进入解压后目录执行:
& m9 X" M; @9 g
# ^/ l2 K1 R4 t( z) ~% A./autogen.sh
% i5 P. ?4 J5 w2 T7 n/ n0 {./configure --enable-standalone-module --disable-mlogc
  n& B" p4 m4 _$ J7 |) u% E8 Kmake
6 E: ~% y8 W- i6 r0 j' c三.nginx添加modsecurity模块
  g6 p+ s8 H+ l& l; h* c: }6 u# D' }' Y( V& x5 H, t9 ~
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:- S/ n, A: V8 l/ ^
  @3 ?8 d; U) X# o3 T
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine* K1 Y+ T1 f/ ~$ N
make && make install$ f2 k+ r: Z6 M% C0 G
四.添加规则2 P8 [) ]. q: {" K- _- H6 W

8 o, U; x8 I2 p# ]7 l* E+ {( N. Zmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
8 B  `2 O* j/ Q; r! r! H
. @5 W* a6 ]% Y5 j5 F1.下载OWASP规则:
9 R/ n" V2 L8 X  [" k1 m" E6 N" B' g
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs& q7 P9 T5 ^2 g
) O5 @; b- o) \* T5 v
mv owasp-modsecurity-crs /opt/tengine/conf/
; P" M: h1 ^3 J" j, L6 f3 M5 k$ z) J( B  L
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
# J/ N" S% M7 J2.启用OWASP规则:# V/ @/ y9 v$ n* L$ X1 c" `0 N

# g3 ?* L6 d* M7 L1 C3 @' _复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。1 `/ e% C: q* G
8 B2 p0 f4 U  @9 _" R5 d& g
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on2 ~6 |% D$ {, b& ^) v+ o9 h
9 m8 m; q  L8 L
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。6 Q% _3 k9 A2 |% Q9 k+ L8 E

/ A5 |0 e/ r/ e' U+ |Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf4 M! O7 N0 b5 \5 `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf1 ?$ C; ^9 Z' N
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf4 C  [; B: J2 s0 p5 F
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
3 E& V+ C0 {$ DInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf$ H  _+ c/ J0 x; S% }
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf" l8 l7 h: b! O# ~/ v( \& n" W
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf  A2 g$ d( l4 Y6 _- x# k
五.配置nginx
" z2 h6 }5 @. a0 {/ j# N/ R& \7 c0 t0 b5 W( C/ _0 G
在需要启用modsecurity的主机的location下面加入下面两行即可:0 j' s3 F1 P# E" h, g
1 p" C2 S; F$ W" K1 U; b/ |# R
ModSecurityEnabled on;  : f0 v( J/ o1 @" y
ModSecurityConfig modsecurity.conf;4 r. {" R/ L3 L0 Q; ]5 J6 S
下面是两个示例配置,php虚拟主机:( l: q5 N6 W: v: ^7 Q% o

- u4 C% H! C5 h! X0 k/ fserver {, [0 p9 W, a' K# ^# }- ~
      listen      80;
: @" B! r$ F% Q+ `% E8 N3 \      server_name 52os.net www.52os.net;
& U1 d+ H! X" S* {0 |, l' I9 X" M     . F; Y7 U2 j4 c7 g* F% B; v5 c
      location ~ \.php$ {. R9 ?5 y% g+ a
      ModSecurityEnabled on;  
  W" Y' b2 P8 A      ModSecurityConfig modsecurity.conf;8 r7 v- ?0 P! o) T+ U" i* x

/ W6 t* [6 G7 B  C! [2 E# o5 e      root /web/wordpress;7 s( j% f' t' b0 u2 t
      index index.php index.html index.htm;/ Q6 z6 m* Q8 C/ A
  , L3 X1 _/ f* I: {3 \2 i7 J5 u
      fastcgi_pass   127.0.0.1:9000;
& U9 K3 x  ?# |8 P( f7 m' @      fastcgi_index  index.php;/ i( |; M. R5 N1 c; X
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
8 W0 r' _; H; B7 a* i2 T      include        fastcgi_params;
; {: f/ m/ s6 W9 R4 `1 @3 r4 z7 @  }      }4 G3 _/ b/ i+ ^8 N0 G5 b
  }. G/ s: D- e7 G8 |: z! Z0 x* J
upstream负载均衡:
& s1 ~( J/ _3 Q8 r4 l* Z, `* ^
# {* S8 r, c# L8 S$ @5 t6 bupstream 52os.net {! U- F6 L  ~9 g
    server 192.168.1.100:8080;
' ^/ k6 I' I" C& Z9 i0 v4 f    server 192.168.1.101:8080 backup;2 D' g0 ?9 ^6 T! v
}/ t. M' S2 M4 q4 T/ s8 n

% a2 \8 ^2 ]6 o6 f, `server {0 ]6 R& @7 ^0 b6 c- N
listen 80;1 V# w/ x- @2 \, o+ T5 Z
server_name 52os.net www.52os.net;
% q& C5 s# L3 W! t
' C- W: t0 y$ |$ g$ J0 a( }location / {* \2 a6 F* T2 q9 Q) W2 e
    ModSecurityEnabled on;  , h: T1 Y- |" t9 i; R  x' E8 U
    ModSecurityConfig modsecurity.conf;  
% s8 U0 W7 [$ \
( }. _7 R% h3 T- q        proxy_pass http://online;
2 s# |0 j+ x/ g% o& P7 C        proxy_redirect         off;
6 ~: t* v' |) s" p2 C        proxy_set_header Host $host;- i" t# m1 k1 W$ |
        proxy_set_header X-Real-IP $remote_addr;6 D6 y) P; y: V: D
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
: D2 i1 e% }7 Y, q    }
! W. T4 v# C5 k2 Q' R2 N/ a}
+ x7 @- n6 I) P  k0 \) f9 d六.测试
) A% ^9 x2 S2 ?3 M+ p' A- ~3 s) ]1 C7 }( Z) ^- O
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
4 v" X; `9 K+ `2 ]/ K  D* p& D# r9 b# @! ^! }; P! u4 m
<?php
  ^1 X4 v( ~- s: ]& U    phpinfo();   
" R4 }& F' B; i# r?>
% C5 R1 R0 U' z. @' q6 j7 r在浏览器中访问:. p3 W1 d* D7 N& W* h+ j& R4 ^
3 G6 t: ^5 t! R
http://www.52os.net/phpinfo.php?id=1 正常显示。8 ]9 p# Y5 y/ U5 a4 p* B
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
6 t' j4 }0 P9 ]; U0 a. Ahttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。7 s' Q) v( `$ Z
说明sql注入和xss已经被过滤了
/ J+ R. g1 [: H6 h: Q8 s; S; ?
) X3 H' |2 r- H0 k# h七、安装过程中排错
8 a. f0 T& K( y  U; g6 ~& h5 d- ^5 a( Q, w! K+ X& b- e  q
1.缺少APXS会报错2 g3 W) U; i* j4 l: d+ e8 j% |: a

0 u& Z6 n9 N5 O5 ~. Z, ^configure: looking for Apache module support via DSO through APXS
2 P% q- J' E/ h6 O, ^configure: error: couldn't find APXS
  s, p9 S. E5 _, Gapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
5 Z) n) t. i# T! E# r" _8 o9 r解决方法:3 z* v  q) [& A2 B0 Y: n
9 {/ J0 }! U0 A; Q7 ?
yum install httpd-devel
5 V/ |$ k4 i, w- @& ]; R3 H2.没有pcre1 X0 {% C2 s+ U$ @
2 R5 e0 Q' }* q) s
configure: *** pcre library not found.- k% `0 T1 w6 J
configure: error: pcre library is required
$ x9 _4 `* p9 ~& `  D% _解决方法:( x8 Y% S. e: R/ H  [

4 R0 S" X  `! b( W' i9 ?4 pyum install pcre pcre-devel
& _8 ^& E1 I; w& I- E+ j9 V. o3.没有libxml2
2 y5 `% P, N6 l  S0 O- q' Y6 C3 X* i+ |2 E

+ B( s5 Y0 t8 B3 Iconfigure: *** xml library not found.% N+ E' e) c6 {+ T6 l$ R
configure: error: libxml2 is required
# ?7 q% L$ t2 ~! W+ D, _2 g$ [; M解决方法:
; x' ]6 ~5 t4 x+ W! h. N3 L1 r4 S) M3 M
yum install  libxml2 libxml2-devel
, D0 E; |) P6 E+ |4.执行 /opt/tengine/sbin/nginx -m 时有警告
7 G- O' q" g; E) X" Z
* O7 P. ]: J. U+ g0 ETengine version: Tengine/2.1.0 (nginx/1.6.2)+ P# l; w  b7 o+ J9 h9 r. q9 n
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!: k  N7 r0 E' L; r# r
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log9 L) b6 I5 k3 D2 S; q. C7 e

8 J7 X9 E' }3 ^8 i1 c2 t' W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
, ^  D+ P- U9 \9 P2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
8 _* w; L7 P7 X8 d2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!& a, O- v- u+ O0 v7 M2 b* V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
  z# t3 P5 ?: Q/ t5 [# W6 w2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"1 h3 c7 y5 Y) r. P4 c
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
( z3 Q' D$ [6 h2 d解决方法,移除低版本的APR (1.3.9)
3 j2 l- B  ~3 ?- u8 k; |! _  G! z' S+ V, n0 ]5 P0 o. }
yum remove apr
! w6 L  g! d2 q" ]/ d& y8 @, |5.Error.log中有: Audit log: Failed to lock global mutex
( p" Y3 i. n7 t1 e, Z1 R6 X- A+ F+ z* \9 H- G. n! _% Z4 {
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
% e% ^$ L# z3 t; U2 }/ j4 Dglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]( t/ `# N; z3 g2 u* L
解决方法:3 R0 e  Y0 Q' L0 W4 n3 h' X
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:. Y2 n% C& m0 M. A8 m8 d
1 W7 ?3 R% z' f+ A7 {4 e2 [% ~* }
SecAuditLogDirMode 07777 l# v$ O7 Q0 }8 P3 G( W
SecAuditLogFileMode 0550$ b( P% i" y" `. _
SecAuditLogStorageDir /var/log/modsecurity) r" H0 Y$ x, A/ [1 U" y
SecAuditLogType Concurrent8 W  n) [3 X* p  O
参考文章:
0 s  b1 H  g" yhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
% K. |  i. y4 P6 m# Shttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-5-16 19:29 , Processed in 0.052352 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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