找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13016|回复: 0

nginx配合modsecurity实现WAF功能

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

9 l& y1 C& S% `% R一.准备工作
. k+ V0 G+ f) k# h3 C4 Z3 ]
- f7 Y% F4 @7 m' n" `系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.06 ~: D- u/ h# L; u6 v6 Q
2 ?! s, V, {. J1 t" m) ~& ?+ c
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
/ A2 [( j( s3 `: P4 j
8 ]  y: Z7 \2 ~$ T/ \) z, |. hmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
( }9 ?  G, F& C* S5 @. q# o
/ D6 Z; y! e) z& h" u9 ROWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs4 q) S/ O. f. i" e
0 y( B% a6 J# f
依赖关系:
, V  E  R; ?) T$ Q3 F, ~tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:% }. P6 M2 j  Z+ S) E$ @

7 l/ H& Z* M2 Pyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel9 c9 e, t$ N" r* n
modsecurty依赖的包:pcre httpd-devel libxml2 apr* c3 R% K' ?/ k6 I, ^

' y& k  z4 b% S% q" ~8 D+ jyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
* j: t' x, y4 P& y4 K5 n& `二.启用standalone模块并编译
1 T1 d8 y- l7 p/ N- w( ?3 V0 S" D
0 Y! u$ K6 X6 I7 ~, T; d1 u下载modsecurity for nginx 解压,进入解压后目录执行:
2 A4 O/ Q  W4 U5 ~
; I$ C! k  d& F& i7 {8 }& o./autogen.sh
  }& f6 _$ `3 }7 I  T./configure --enable-standalone-module --disable-mlogc
0 h& Y( Y- I( w$ _( vmake
* l  [( ]3 W$ P; B* r* _# d7 i" y三.nginx添加modsecurity模块! R, \3 r0 J0 J. ]; X, m2 K0 x8 o

) Y# j/ }, [" `( i在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:* J9 m0 e$ M5 k
+ n+ O/ W9 q$ |, @  o
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
, r" n) i; V  F; s* p# ]% hmake && make install; \' w8 y+ s. b6 t' ~* U# z
四.添加规则
1 Y7 x: y7 N' e- @% c! J$ s9 d. g5 ]0 J7 j* y, A' m! x
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。7 e# E( P. f3 u" k# \  |
' L4 I6 I- @# s' f# r5 g# t3 d: M* H9 R: C
1.下载OWASP规则:7 R7 x, x: M6 G5 M$ k& o+ t* S
( ?- [5 R  O3 v+ s
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
5 j4 J; O1 J. t: b% Q  [- t" H$ M" [! ]( ^
mv owasp-modsecurity-crs /opt/tengine/conf/" i! I! W+ X$ t
& g' N4 H- j  V" L& f8 P7 o
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
- ]& y3 X" ]/ m2.启用OWASP规则:
$ m+ Z, `6 S; O+ X6 A# r7 N, l3 _9 K( `! Y7 e. O$ P% O5 B1 G6 d5 _+ {' U
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。' E+ L/ H' i% l2 H) r0 U
7 B+ m, R% h) ]  V
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on- H8 M1 D% b' f5 z2 X
, Q) H, z  \# |  G" l  f
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。" U2 H  U" G0 r) d; J5 G7 i

( J6 R5 G1 u. w) {! SInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf) D" o' n. [* U: \( s
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf- w' T) J. A& G8 b& f4 c4 R
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf! g  G+ C- r4 M, c$ z' r$ Q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
* Q( k* {) \% {) YInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
: I0 m2 A: P8 f" [: w, R/ z: |Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
5 f( Z4 K8 V6 g# uInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf( I. p7 z+ |/ T" ^' t
五.配置nginx% Y% T3 |1 W6 i$ g- s
: J# C# l/ P2 |& `
在需要启用modsecurity的主机的location下面加入下面两行即可:2 f9 R1 k# ?5 S% Y

5 K, C) L$ D8 U- {ModSecurityEnabled on;  % ^" A8 q: n) Z$ P
ModSecurityConfig modsecurity.conf;
7 D' c& m3 X( F) e下面是两个示例配置,php虚拟主机:
0 {! M3 _9 P: @5 k% C
7 Z0 A! h+ K% B% }* B4 Tserver {5 P! D! m" z  R0 M9 n- P; p& `
      listen      80;
( F: i  S, e4 M2 L0 N      server_name 52os.net www.52os.net;
$ N9 B! X5 A4 Y3 l4 x     * V* @' w/ ^" }" p' a, O
      location ~ \.php$ {( m! [3 Z& ^0 Y8 A6 k) s9 V
      ModSecurityEnabled on;    C6 V: U& _2 b* N0 k% w
      ModSecurityConfig modsecurity.conf;
+ i5 f4 v( h" |" _
0 W2 }9 E" j' f/ r      root /web/wordpress;& _4 D9 S: i- s
      index index.php index.html index.htm;8 G* i: }8 P. p; N- `% A
  
" d1 F, c9 Z% X& E- b. k      fastcgi_pass   127.0.0.1:9000;
5 L. U4 d! ?' V3 k$ c3 t      fastcgi_index  index.php;  R( r) ~3 [, D4 F
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
9 C  _' ]5 u3 r# q1 {      include        fastcgi_params;
( @8 s5 U& w( d9 R) b      }
6 v2 ?+ x4 t7 o6 ?: {  }6 U1 s7 f. v! c9 q3 l' R; S
upstream负载均衡:
( ?: G' c' }, b3 V. ~- L; b4 A, u! _4 l
upstream 52os.net {# v6 i, I+ T6 W# f" b) i* m% b
    server 192.168.1.100:8080;
5 O8 Z( S1 s, @6 p! ?    server 192.168.1.101:8080 backup;
3 z, C/ e. G, G}" l- E% F9 D! p0 v% g2 T  @

8 q  l+ F0 g( k& M9 W" fserver {
( y4 h* p1 C* J1 W  V2 ?  i0 Flisten 80;
* R* V: ~6 u+ }+ A5 R7 jserver_name 52os.net www.52os.net;& j2 x# K0 o" n7 w8 v
8 @1 f4 B7 h9 b* c; P: Q
location / {( p" l8 x( l: m, G0 e" u
    ModSecurityEnabled on;  * \6 o; Q/ m- ?: ^1 }
    ModSecurityConfig modsecurity.conf;  
+ o- N6 P4 s8 l1 t; P1 R5 S( R
2 z! @: J5 _0 H) _; r+ v        proxy_pass http://online;
1 h* d+ L0 w6 [: g. k& I2 n        proxy_redirect         off;. P- h- @. f( s$ l7 S! `4 i
        proxy_set_header Host $host;3 _4 x) }0 V5 a  m/ l; a' |3 i
        proxy_set_header X-Real-IP $remote_addr;% x; T( r4 s! Y( v" N+ j* Y" ^( C
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
6 }( w+ i, C$ `: a4 i5 ^    }
5 ~  |8 I, t3 @( [- S1 O5 j}# }. j7 J; L7 Y' d7 E8 n3 w0 J4 N
六.测试
9 y$ L9 a" \" p! p% P' a' [( I/ }2 v( u# t4 x2 j& B9 M7 B
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:$ p7 H- v. X( J
3 k2 q5 A: x# O6 j4 l4 A- {! l
<?php
+ N- x( P! h9 i( x0 W9 P9 e- O( r    phpinfo();   
! V0 l3 O* c. n/ ]% h/ o?>
& Q( u+ J/ f* p$ ^在浏览器中访问:
/ |2 m( f9 V( e# C1 `% c2 _# j- B% `0 }
http://www.52os.net/phpinfo.php?id=1 正常显示。
) u1 ^. S4 @! V2 E# z# Ghttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
; C7 A4 p, q3 }http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。5 R7 w" m' m9 O2 J
说明sql注入和xss已经被过滤了
1 W2 R' l9 g3 K. O5 u" [. j
! X- P4 L" |# A七、安装过程中排错1 e* e* c. X7 l
8 ~& \7 n6 V5 L0 A) \) U6 X  Y% B
1.缺少APXS会报错
# w1 D+ N5 a, E5 l( E
# U* E9 N; r9 z8 B( l6 g0 ~4 E2 Sconfigure: looking for Apache module support via DSO through APXS5 w2 A. ~! b9 D* Q+ u9 _/ w
configure: error: couldn't find APXS) i1 n9 o. u) n) G) y2 z; ]
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。/ x/ R( t' @: q8 d& c: L
解决方法:+ p' _" V8 U6 j, G
! B" d( l7 b5 `7 Z0 Z" v
yum install httpd-devel
! E- e: O# d7 J+ K2.没有pcre
  u! X( |0 |& r* }" o1 c+ F: {$ w
# }& p# c6 P1 f, C4 W( Pconfigure: *** pcre library not found.
0 ^$ x1 p. f! ?2 N* bconfigure: error: pcre library is required- e$ b$ I4 _7 ^- B6 W, `/ \! J2 x
解决方法:
' ?$ M/ ~; J, i$ ]: r! o
$ ^% p1 V( z( P: R7 Nyum install pcre pcre-devel! f8 q6 c3 P* w1 X
3.没有libxml24 D% _. G8 ]; \9 B; m: T' w
3 P* ~# k8 `) o7 _. _

: m5 L$ u, u2 k& x4 A+ G6 e* jconfigure: *** xml library not found.7 V, O) o" D9 G* V
configure: error: libxml2 is required. Z! c6 ?: i% D9 k
解决方法:
3 |$ z, t- a) e, o2 _8 v8 `  e1 m
2 ]) E% r7 ^4 Xyum install  libxml2 libxml2-devel
; S( A/ v7 S9 ~0 |8 s+ M/ V6 h4.执行 /opt/tengine/sbin/nginx -m 时有警告
: D5 D5 X+ ^! J( i5 _' ~- \* V- Q3 w2 Y9 b. f/ g5 W- s
Tengine version: Tengine/2.1.0 (nginx/1.6.2); f' W7 Y6 Q4 r0 a
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!! s, j, A. Q. \' ^9 J9 F; n
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
- u& v, o3 _3 B" ?/ W
' R4 I! l, U4 {2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
$ e2 l6 w/ C& Z& y) G- a2 d% d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
7 _1 ?; R0 A2 u% L( o3 m2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
. S7 f1 s5 _0 T! K4 J. }/ v6 k2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
9 z$ k4 w8 _/ {  ?# d+ a$ D. W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
! O, {) A( R- _' S* d7 K6 @8 e2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
: c( g; r! S6 o2 [$ J5 b5 i解决方法,移除低版本的APR (1.3.9)
. {1 d2 p) U/ s1 g0 J
4 g  ^* b; q9 R5 P  n, ?4 m7 c" u4 j& Iyum remove apr
- J: ]" j7 e% N! n. W2 L7 N5.Error.log中有: Audit log: Failed to lock global mutex: A4 ?) q& I2 ]/ U

) k" H( I4 k) Z1 E2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
' z; \" P5 |! M+ U8 \0 }* s9 z/ _( Wglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]. l. O4 `1 C7 O& l
解决方法:9 W" f- L! L0 B; T$ M
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:& L6 ^$ `0 D1 f
0 S- r3 x8 O" f3 w* b. i) B
SecAuditLogDirMode 0777. i' z$ g2 o% {$ M5 D
SecAuditLogFileMode 0550
! V  ?' B$ n( F- m* n- BSecAuditLogStorageDir /var/log/modsecurity7 t+ c5 e0 @8 O5 m" |
SecAuditLogType Concurrent
) P* m  Y3 w2 W, l' g参考文章:
4 p) q9 j7 q) K0 s) {. g1 S, o0 |https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
6 W$ \$ n& A( V! \7 R8 v6 Zhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-3 20:56 , Processed in 0.074382 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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