找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12179|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
4 V1 c. N# X7 z4 N& o) |, S- g' T2 k) @$ c8 }
一.准备工作
! l! a; P6 n6 ~! B8 {0 {1 n
) _% l: T& O  s系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.08 v& S& \2 ^8 n+ v4 ?) D- r$ m

5 _9 `. c* |0 b7 u# Ttengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz) s* R% R8 f7 r7 _7 b; V1 X

; [9 i% j6 |* S$ \* c" Nmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
# S- Z3 q+ I: |" f4 H7 U8 F( n* s- J
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs1 {# T% ]; H5 T3 O0 }

9 [" A1 W% f  N$ V依赖关系:7 @1 ^* A1 M; E9 s6 ~, q3 R# U
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
: I/ w% E$ @9 ?5 F) X' p) z
- s7 K5 n6 R3 c  Z" l( b4 C; X) {yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel3 E) f7 D4 V" V$ _
modsecurty依赖的包:pcre httpd-devel libxml2 apr
7 Q9 Q3 N1 }" d7 K
2 o  b; d- w2 A9 z/ D3 _6 h& K+ M2 Lyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
. N- y& U9 D6 S- V  E5 f二.启用standalone模块并编译3 L' [4 D/ S) o3 t: a% K
: J8 X& t! Y8 D! p  p- M+ w3 L' k4 T
下载modsecurity for nginx 解压,进入解压后目录执行:4 O- f; p4 [0 b0 A+ |
# {  f$ b' _5 Y; L% c
./autogen.sh: w2 l/ u  h! T- Q9 W
./configure --enable-standalone-module --disable-mlogc
3 {" E0 A5 o% B7 Z& nmake
5 `4 D5 Y2 u# [2 i4 V三.nginx添加modsecurity模块" M1 K6 r" X( \& S- ^& P- O

+ ]* R) @! r  [在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
( ]0 e: f, Y. b& X; {1 |& Q$ B* U: V* X  Q# ~
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
$ a) a8 i3 J. N3 u4 r, [make && make install
. W! q4 a2 F+ L1 D9 ^+ |四.添加规则  A4 l) C6 G+ L) H0 c: h% A

/ a* s, _7 }/ p! Gmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
% [0 l. w4 B9 h$ h0 x' n! o
! |# w+ i% w/ F7 [& \8 H8 u1.下载OWASP规则:5 h, G9 w9 |1 r- O4 \8 M

5 q3 T& Z5 r. R% W" X% h: Pgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs4 D7 q6 W0 `  d  G
6 D" v# |7 h4 j0 D' P) m
mv owasp-modsecurity-crs /opt/tengine/conf/: t  [4 m4 e3 m2 \" F9 C

0 u4 N5 n1 c' B) b# f+ z1 acd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf3 }: @, }9 H2 z- R; {7 ]' E
2.启用OWASP规则:. s4 m, |2 [  w% y; I

$ M# B' F9 s+ H. w复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。# N9 {) A* Q) w# ]# F
5 `% _  F4 F: z' {: v
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on5 C! J) T4 U) M- M( l

3 C4 t9 Y8 C6 iowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
: }/ Q4 R8 F: @1 M+ r% e/ X) a( Q/ N$ ^  c- B' w6 |$ B
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf# ]7 ~7 O  M/ g# h
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf. H. S' q- D; E
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf$ C/ T# @: U7 ]% U( v$ g( F& t5 \* o
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
, ^- ]. d" |5 y: TInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf: b4 ?4 A: G# m% C4 u
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf" R* E& e. r2 O
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf- z& t9 S: d* g& y9 ]
五.配置nginx/ ^1 S( M5 ?, ^* b8 B
, O( E' K6 R* M5 E( L$ ~+ J  M( p6 \
在需要启用modsecurity的主机的location下面加入下面两行即可:
, l: j+ v( W9 l, K" [& @+ i+ u1 s7 A+ e
ModSecurityEnabled on;  
" U) u% S+ I  e( p' J: `3 j7 YModSecurityConfig modsecurity.conf;
. ^* n1 X: u* ?3 s$ K下面是两个示例配置,php虚拟主机:
( R6 y# t" S$ {; t4 C/ A
9 Q$ j. B8 J: q7 W1 d: Tserver {' J+ B! f( L5 E2 W2 D" ~8 q2 T
      listen      80;/ c' ]1 a% l1 |( r. M9 ]
      server_name 52os.net www.52os.net;
& Y, ~) p8 j7 ~& f' r3 ~     
9 ^2 P4 ^9 H5 O; @) i# r, \6 H      location ~ \.php$ {6 r; [8 G" F3 P' n+ b
      ModSecurityEnabled on;  
, ~0 ]3 @- B- u2 _      ModSecurityConfig modsecurity.conf;
3 [+ X3 A' T' n) e$ h9 S( I. a) j& O# m0 D' f- ], P
      root /web/wordpress;6 u1 }, \  y& ?& K/ a0 q( _
      index index.php index.html index.htm;
0 [2 m: [* p1 m  
" l% g4 @% s" q      fastcgi_pass   127.0.0.1:9000;9 P% Y/ q/ y0 M6 i: S3 @
      fastcgi_index  index.php;. d9 P2 D" T7 a- _9 U- }( f
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;- l: E5 B. ]1 ]( F& V# y
      include        fastcgi_params;
& S3 Q6 }2 @' }% g" T      }
. F/ o3 X6 P. w& b1 C  }
# t5 ~  ]( o$ K0 N; Yupstream负载均衡:
4 ]2 F2 y. i* U3 D4 R  }- u& |
% T; b% m7 C8 q- ~: R* zupstream 52os.net {
: [; H% \4 {9 \, H, |- b0 L( Y    server 192.168.1.100:8080;
  i; ]$ e/ _2 r    server 192.168.1.101:8080 backup;
1 A0 B  }$ q: w( N5 z4 r}
; ~3 S. I/ S5 H3 I. N6 p' r
& ~! Y% y/ Z* M$ T/ E  E! B/ }4 L  j( I( Iserver {
2 h: p0 T; b3 m" I0 u1 P  Flisten 80;
& o  U7 `6 D4 e* P( Sserver_name 52os.net www.52os.net;% z$ f; A( d: b% f

* y& f9 |& [) m5 M2 C8 k) ?8 v* a- N- flocation / {/ ~7 r5 t( o* c6 Z: I- E
    ModSecurityEnabled on;  
) v/ N+ w  t7 f    ModSecurityConfig modsecurity.conf;  & f1 Z* D5 E& s' g3 A& Z1 |

: G/ Q9 E* _; R0 L, a* O" r4 j        proxy_pass http://online;
" }* r; x- t7 E$ [6 z        proxy_redirect         off;* ^4 `% t( W! f% M, s* g& |, u) c
        proxy_set_header Host $host;
8 ], o% r2 t( u6 Z, b) f- V        proxy_set_header X-Real-IP $remote_addr;
) }: O- @% a) j' }$ m        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
2 \7 Q2 x7 }6 y' H" B    }
& P( f: m  n9 O8 i: H}5 a3 o5 a4 M( e6 T
六.测试
# Q: a- p# k$ N4 Z1 h
$ A# I! G) }4 m/ n8 r& ~我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
  k8 ~1 v+ q. d! ]- A) E- f) g* X2 S# y$ e) q
<?php
8 s7 r- G9 p/ k& r4 U( }9 p' z' X    phpinfo();   
/ g2 ]# }0 K4 e0 p5 G# L* H* c?>8 A, g. K7 s" @) ?$ K' [: I
在浏览器中访问:+ a6 g. I8 h5 x

8 c  W0 |0 W0 K3 J% uhttp://www.52os.net/phpinfo.php?id=1 正常显示。
% _0 D5 n- b! d5 s' Ahttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
$ `3 j7 a* _& a+ C6 b* Fhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。3 e" ~4 D# X) O
说明sql注入和xss已经被过滤了9 E( l3 {' y5 G! w* S. D
; r  G9 b# u3 p  a0 G4 h+ v$ h$ E; b
七、安装过程中排错
8 F8 D  y- G- H  [  a; Z* s3 q# i" x6 T
1.缺少APXS会报错
! y% D* L  {$ m6 T4 t1 U; G- M7 A1 K4 O- V" \' C
configure: looking for Apache module support via DSO through APXS
6 E4 c/ n6 y6 l7 @& O8 Tconfigure: error: couldn't find APXS
# Y, ]* G( _! h; i) E: Oapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
% p. Z! O) ?6 x) n" ?$ d解决方法:
1 E$ S. R$ E6 z( [
" P, `: i7 N" U; r4 Z0 nyum install httpd-devel
, \8 I% P9 o, h, ^2.没有pcre  ]7 k: U# c) B

6 Y' R0 y# R6 }# s4 f3 bconfigure: *** pcre library not found.' u! |1 l9 W6 M' Y& j
configure: error: pcre library is required
4 [; M, g/ J$ n- P8 c# d( K解决方法:
' ], I2 g! p  O3 m2 t/ j0 |- O, H8 \' k1 t  S7 Y7 y0 J
yum install pcre pcre-devel/ @$ f/ L. ]4 J% W' i
3.没有libxml2
. t' Q7 I) o/ R; B
3 K3 t. S6 S; G3 c: c: \3 I6 y9 |( @7 [9 Q) `* _" ^0 U4 ~/ t
configure: *** xml library not found.
& Y# y/ m& \: i& W( E1 Mconfigure: error: libxml2 is required
6 ?4 r- _+ h9 i# F& ^/ B* e解决方法:! ?' e; R( ^; V5 N0 K

' r& \7 B# D# [" ryum install  libxml2 libxml2-devel
  T3 n) O/ G  |7 f0 Q0 l4 l( s4.执行 /opt/tengine/sbin/nginx -m 时有警告1 D" F1 t$ p# V
( H/ J; h! R9 J/ w' g
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
. Y* B4 @- h  ^5 F$ t* gnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
6 w' m- D, N7 x5 ~$ p' K原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log1 n3 }: |! i: s* F, A
% \( S! k/ m, N. O2 N- T) a
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.5 ^0 Z1 n: \0 S; y" Y: E) }
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
5 k6 k. p1 Z6 x- H3 ^) }6 o5 J9 W/ J2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
3 d# Q+ y! k6 ?  L. u5 g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
: H) Q$ x" H1 x2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
+ j3 E/ Q: @3 G* `" _8 R1 c2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On./ |7 o3 k9 B! e
解决方法,移除低版本的APR (1.3.9)  _) C# H6 W7 C- R; g

* s) O* C! v$ g1 qyum remove apr
# H/ M4 r/ ~( s1 _) z5.Error.log中有: Audit log: Failed to lock global mutex
' g9 `# r' k2 j( H8 n1 Y8 D6 d. Q. H  S4 l1 o8 k: ~
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
5 |( e% f4 U/ R& x% G4 Q: Hglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]  p6 Q( ]+ o( u% q5 @5 }! S& l! X
解决方法:$ E( `: X  @8 I, Q2 e$ a
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:! X! N5 v( q; n, Z0 V- Z
+ y+ U9 C! k% ^: ]
SecAuditLogDirMode 0777
. @& I: R' {1 g( J% _) N" ESecAuditLogFileMode 05508 k- u) O' @2 ?3 I+ @
SecAuditLogStorageDir /var/log/modsecurity
6 Q% u# W, b; \3 F2 LSecAuditLogType Concurrent
- Z1 ?. z; W$ z; n7 I8 ~参考文章:
$ u6 J; T0 a3 O$ _5 X) w# Shttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
3 q0 ?5 c8 X  [, f. U0 chttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-15 21:16 , Processed in 0.049014 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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