找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10485|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
* J7 s/ D8 {7 }1 r; ^: ^3 n* C, g, o* N5 N1 R! k
一.准备工作7 v/ Y7 R9 c/ A( S" n# X5 H
1 ^! j1 ]* Q6 C; w2 U& F& h- C
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
* m/ o2 j- g9 E$ s1 U3 R
! R% `7 S, [8 z( Btengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz8 {7 Z1 _) A) q. O0 |! w6 s% R% n" X
" J0 P0 r, O' ~! H! q
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz$ j2 @8 r; }1 Q3 j. j

( x& I5 o: b! n( J6 n: P. kOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
. Y+ P" F0 ]- `9 W9 T
' }; V" @+ F4 _" A依赖关系:" X/ d- j" s: J7 @
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:, ?7 S" \$ \8 @! M6 X0 c" l3 r
( b9 s4 J+ ~3 m) L/ s- m4 P
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
. ?- R# c2 n' Cmodsecurty依赖的包:pcre httpd-devel libxml2 apr
$ N8 @+ C& A6 v; }$ F
9 u/ E+ R  v: R7 t8 T" myum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
6 Q8 `) R% N) Q/ t: E+ N6 w3 t" t) ?: O9 y, g二.启用standalone模块并编译7 w/ r( _& R6 b. {7 a* n8 N

0 x2 r7 T! w' M# G  b* `. f下载modsecurity for nginx 解压,进入解压后目录执行:& i) w* T7 b* U; y2 b

0 ~! K4 H, z' n5 h./autogen.sh! \/ q* j' w" f2 Y$ a: ^. \* J5 O
./configure --enable-standalone-module --disable-mlogc- |& a" [. T, G8 N: ^
make
* z; _: P7 d* Y3 P1 Y: Q8 E三.nginx添加modsecurity模块
  Y# @& o+ }7 p" \5 n& T* p7 v+ A9 i5 Q" o. d' E
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:, f( Z4 p( f7 v1 L: J
9 [# w( e/ m% B( A8 g% ~& a
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine1 d+ v* o% c; N, o6 g: U
make && make install  w2 w$ F9 @3 a" `# F  D0 o& S3 G
四.添加规则
/ `- k: T/ |2 |! N) @2 Y& p, ]# `8 K% g) d# @9 v' o% Q
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
- X  ^9 M: d4 O
' v' c: S! `$ K' T9 Y) F1.下载OWASP规则:
+ v( v7 n9 w% A6 @& E. t3 d8 e, {3 e3 a( ?
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs" m' e3 a! `( |" O  _9 v) R- j5 [6 G
' u/ C+ B4 A% I1 @0 ]0 O6 o
mv owasp-modsecurity-crs /opt/tengine/conf/
- Y4 }1 z! Z. T
9 j. q. q* S7 ~* c( {$ Ocd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
% P4 O/ C9 W5 M' g4 J4 o6 L2.启用OWASP规则:/ {* o9 _$ t  s4 `6 ^& W* u

$ @* g4 D0 a0 t+ u复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
# E$ f6 d7 y  X# @* f' ]& T+ U# f% b2 _! l4 j2 b2 w
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on2 s" b) B* t, E0 n. S. s* U

6 _% ?) N3 K* o; [owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。9 w" r6 u* J+ c9 S6 y

3 R+ @6 ~  [4 \. wInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf. Q9 @7 `9 W6 I3 Y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
9 M8 N7 J. o( e' @6 s0 v1 a1 XInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
" X2 b# O2 H. l% T* W( `* fInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf& j1 d0 @5 m% l8 L! c/ E) u
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf' x6 i( A6 _1 S- j3 v9 @
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
2 j0 h0 @: i! `" }9 e# ]Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
( B; E* K3 |$ G1 Q, o& c五.配置nginx  Z# K. ^" l" N
/ W2 \" S  w2 |7 b  a) \0 ?% n
在需要启用modsecurity的主机的location下面加入下面两行即可:8 t% ?  L  L, M

3 Z9 n1 _. H$ IModSecurityEnabled on;  
7 Z9 Y. k2 G7 x+ j* b  p+ [0 t+ PModSecurityConfig modsecurity.conf;
1 \4 r( @4 [! S4 N/ h/ j下面是两个示例配置,php虚拟主机:
( J( }2 X% c" a" t1 `; M" y4 m  e
4 @4 j  T$ p$ t: h7 Oserver {$ C3 f5 G6 n. E( X
      listen      80;( _; E. c9 }( y5 k0 Z
      server_name 52os.net www.52os.net;# s6 e- A, S& r( r
     ( o6 b, U- P& G$ z3 M
      location ~ \.php$ {6 E8 Q$ f6 X! `0 D) w" ?2 ?
      ModSecurityEnabled on;  % ]; B8 t/ v( Q; N1 T$ Q$ P
      ModSecurityConfig modsecurity.conf;
+ v: {  N. ?+ Z! B' s' d7 F* K$ D! M* B% x9 C
      root /web/wordpress;* V: U( X" m: I# D% F0 t' p$ k3 Y# M
      index index.php index.html index.htm;
3 ]0 j5 ~+ H0 {+ m# y# U7 \6 h$ A  
6 e' @8 H/ F$ D; r3 [8 q& n  @( k      fastcgi_pass   127.0.0.1:9000;
* P) B$ g+ o9 t9 n      fastcgi_index  index.php;* {  Q; P( ^5 T$ }' W! b
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;3 I: V0 P; h! b
      include        fastcgi_params;
/ J# u+ b' P( O8 D  u8 K6 W      }: K: O% r5 b+ G
  }
) D$ S4 ^, }$ s6 Uupstream负载均衡:
) X+ T" ?4 Z' N; n& P2 x3 d% L; f& D: Z; {8 ~
upstream 52os.net {
3 P4 Q2 ^4 D3 ], ]) x, {    server 192.168.1.100:8080;% R4 ]& d) B$ Y( g' W, k+ `
    server 192.168.1.101:8080 backup;
1 G" g! A. w, @$ a7 m; }}
  y" d6 T; B/ t3 Q$ g2 Z. @' I  B/ e; J9 [# |
server {: Q- |0 W1 R, S) }
listen 80;% u0 p7 N, I6 D: j8 ~  Y. G
server_name 52os.net www.52os.net;
" ?( n) \0 e! T, B; a2 b& S# l& g1 a$ S, J% Z  D$ x# j
location / {& {! m" L* R5 z1 K& A+ H( ^
    ModSecurityEnabled on;  
& X9 F: @! k% l" v8 v& e/ K$ a8 S5 u    ModSecurityConfig modsecurity.conf;  0 r2 E% \# n" S6 O6 c" |

* v# O+ f* ~4 A6 X8 ~/ p& l        proxy_pass http://online;8 J' @, T% s! d0 ~% ?& V2 I5 ?
        proxy_redirect         off;; `& I8 I3 \3 k
        proxy_set_header Host $host;
  l* K# s, s; L8 @( u* s' t' y        proxy_set_header X-Real-IP $remote_addr;( Z0 S2 y8 Q( S9 V9 Y
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;- _7 Z, z3 a' S8 X. X* x% ?% v1 @" k
    }( }5 u6 |2 N: n2 Z
}
% V1 a- k# O$ X. t, C六.测试
1 v# ~' ~5 E! F" g. k" A
! [4 d) l- Y8 O我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
- L; i, G: J" t
; {8 y3 D& W. D' N6 a$ ~7 n<?php  ]7 P) C3 ^" N& \4 r2 K, l
    phpinfo();    7 U# {: A' |: q, ]: k
?>) {7 m) W6 `8 G* ~5 J8 [
在浏览器中访问:2 N3 V: \' |. m4 i0 [6 }
2 [* z" ?/ z3 {8 o" q* y3 a- [# X
http://www.52os.net/phpinfo.php?id=1 正常显示。
1 y* }# I# V/ chttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
, w2 u6 c! Z3 W' ohttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。; C& S; X7 e8 w9 p, I
说明sql注入和xss已经被过滤了2 w! \* C7 {+ L! X% P: u8 k* q3 M

$ O8 T4 r$ s3 w七、安装过程中排错
; l( B% q5 I1 M% l
; C' W, s7 R9 C' b) M" B1.缺少APXS会报错# ]7 X3 j% M0 S6 G+ q
  c9 ]% c$ e* F0 F1 d7 |# A" Q7 q
configure: looking for Apache module support via DSO through APXS" k9 K& L' o- R/ g1 J
configure: error: couldn't find APXS
. b: x& W9 i2 dapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。# w1 n2 `5 J9 o  G% S# G. i/ b
解决方法:' F1 r1 y( f. z+ ^- Q1 |

# R. K" w% u; Z5 R& T( w5 byum install httpd-devel
: g" w4 O9 P2 L0 `2.没有pcre1 W% R/ |: h8 n( }5 ?* l1 N
0 K  a0 |5 q" h9 {
configure: *** pcre library not found./ c2 ?! s8 Z2 v, i1 T8 B
configure: error: pcre library is required. `+ M1 u) R5 R) T
解决方法:
6 _6 p1 n7 W5 ~/ M1 i+ ^' H: _8 j' p# l4 W) D9 ^
yum install pcre pcre-devel
" A! o  o9 D' ^( |3.没有libxml2
2 _3 o  x3 v4 f& I' A, R& G6 A6 O, j2 C) S2 \( H
, ?9 \2 R9 Q2 m4 g+ k6 P: L4 E
configure: *** xml library not found.
! w2 Y; z/ q1 a9 M* @% oconfigure: error: libxml2 is required- `6 g- U' |! h1 G
解决方法:
$ t* A% D. b' z+ n
2 S2 Z, i( ?: ?4 n: f( pyum install  libxml2 libxml2-devel
% T  _1 u! |& r% Q4.执行 /opt/tengine/sbin/nginx -m 时有警告
3 h) }! r' Y# K: X( }
" C( \$ A9 I! Q6 n- X4 p. QTengine version: Tengine/2.1.0 (nginx/1.6.2)
0 C( |2 {8 Z5 t% d+ pnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
: U5 d8 k1 [+ {  ~% f原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log% ^% \1 j" O# \) H' N8 K5 `
; R8 t* X5 R, h& Z$ _" g1 K
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.3 o3 E  l2 i3 g( C, _, _
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"0 x# s3 I) d8 L
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
/ m7 p9 s0 p2 Z- ^2 w2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"* A$ R  F4 p5 [1 P4 _$ v; W! a
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6". @$ l8 w0 H* y6 W
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On./ c/ j. P: p' C( |8 i+ m3 R# |
解决方法,移除低版本的APR (1.3.9)+ e% F/ N* X# `& @

* i* Z1 V6 R& `yum remove apr% \2 \0 i- ]; p
5.Error.log中有: Audit log: Failed to lock global mutex
1 K# d- d8 P& W" |/ {
) f: E, O& d5 |1 @2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     1 w* R1 d9 S, y5 H- S9 m- k
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
  _3 i9 D- k( ?+ P9 V解决方法:
* v( M  ]2 `  E编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
( ?: P4 A/ O$ f/ k
/ ~0 p6 ^1 T4 }4 I! L; A/ V; tSecAuditLogDirMode 07776 ^) p2 h* N: H4 c/ T
SecAuditLogFileMode 0550$ @; o  \+ s7 C& ]/ B' y: f+ f
SecAuditLogStorageDir /var/log/modsecurity8 Y& }" l1 q+ V8 M* `; h5 X
SecAuditLogType Concurrent0 N6 K) n9 L% J
参考文章:7 Z9 g) o+ G1 N# n- ~* t
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX6 V7 ]( z) q- b
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-4-3 17:40 , Processed in 0.069580 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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