找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13281|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
8 j  p1 {& g  f8 t
0 `" b4 ^& P/ X6 l- j一.准备工作
8 n7 \5 H* y: v2 \* M8 u$ D. c7 k) H
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0. L- b& t$ P" R! N

. x! K; M4 e5 o6 F( Itengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz2 C0 i! g4 h* d  ~* N; e& |
0 ^* W' k9 r2 m- \+ L) m$ X5 G
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
& g9 j$ T* g: z  U
- L1 b- b6 A) s( ZOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
; o8 q' @$ _" O
2 N# @6 `7 ]4 i) j# ~依赖关系:
3 a: w2 _1 M7 l' D: E$ Xtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
5 }# e$ I8 r8 U' y/ a  i# J( D
5 T+ h2 z& L( r5 E9 C) T# p" Cyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel7 d$ ]" M8 ?9 N2 ^' m7 ?  Z; [3 }
modsecurty依赖的包:pcre httpd-devel libxml2 apr
/ w6 |  Z- f* x9 e( o
- j+ y6 i1 Q/ F3 C) C0 }yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel8 h* ?$ ]' `6 l& q& x8 L) Y1 H
二.启用standalone模块并编译4 V' E6 q/ i. k/ b, ]8 o; ?6 l
0 K, W, V$ V! d! v
下载modsecurity for nginx 解压,进入解压后目录执行:. m; Q  x  E3 i2 v$ l; `

/ F! O( M% H* X( L9 Y3 c- U./autogen.sh2 q( W+ w* p( d- ^4 B
./configure --enable-standalone-module --disable-mlogc
; c, Y, f7 ?/ ~7 A, P3 z0 Wmake
8 J) ], @9 K. e三.nginx添加modsecurity模块% k1 _9 W4 _8 i

( D5 j& q4 [* e9 H在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
7 O4 N& P5 o, N- B# J! e% M5 m; e, o8 W. ?/ ^* C
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine- Y& w$ |( Q9 V
make && make install, z8 W1 S" z: s8 M& V' b
四.添加规则
+ g% x. m$ k( s, a5 d' L2 y. X/ m# B( }, N7 l
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。( o' v3 [) ^* M* e, |8 G, q* s
) C, A" P* @3 _  D; K6 q1 X
1.下载OWASP规则:, n7 `6 e# Q8 B

( x, _" S. M. c( }# g* agit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
1 I9 l% ~9 q6 _& m* W
; Y7 J# |( T/ t9 Z  z& C0 h8 mmv owasp-modsecurity-crs /opt/tengine/conf/4 v: p0 Y9 x- D1 I

5 R2 R* q2 M; ?9 _& wcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf3 g7 c' d) E3 M' }1 l+ m6 t
2.启用OWASP规则:1 h- ?% n" o- h

$ X1 |( ~0 L3 J( }复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
5 ^/ I: N( E3 m0 I+ w# z- |0 N3 N9 j9 l2 G
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on' X) U! o( T  G' |6 P" [5 `3 G

4 x- ?2 m, ?' @/ Iowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。2 }7 N2 p$ D/ Z" R$ |6 a
! j9 C; z# h1 r% A  E" k. ^
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf. B( J; o$ q! z6 d8 \
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf: F* P0 _- T. F- ?$ i3 i
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
2 C! b; M. b) }3 A4 f9 j# ZInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
; Q. S" x0 s' V( D" V) fInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
* C8 d7 l$ ^/ |Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
! u" I' w3 W0 ~Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf9 G! g4 K; k/ E' v  _7 d. V
五.配置nginx. j: n% x5 W: b3 Q
7 g7 i4 n; S1 P& n# c
在需要启用modsecurity的主机的location下面加入下面两行即可:/ {) `4 T+ Y! _

% W, ^6 W, E4 J- P# b3 MModSecurityEnabled on;  4 Q, V4 ?) ~. N1 U
ModSecurityConfig modsecurity.conf;9 |$ n4 l* b$ A# `: F' }
下面是两个示例配置,php虚拟主机:: ]( S8 @# }: E( Q& v

! o' V1 E; Q+ u' d& Userver {) j$ Z# E, z5 [7 K3 _
      listen      80;/ X3 ^3 u9 U. Z1 @# T# F% u
      server_name 52os.net www.52os.net;
$ l& K0 J+ b1 F/ h9 D& r7 @5 f     
2 |' ~/ f9 E& S0 i0 U3 `      location ~ \.php$ {. \: I" I. b8 N( h  ?& f& u- `' l1 p
      ModSecurityEnabled on;  ' G- k. I- n: p
      ModSecurityConfig modsecurity.conf;
  e- C% p2 f% b! I- B" V' q
( i; b# a+ H0 V4 b! t      root /web/wordpress;" j* B% ^2 ]3 t5 j; F
      index index.php index.html index.htm;
! F7 O3 p- k( N+ g* m  3 _7 E  T; S% y" m9 M% k* F
      fastcgi_pass   127.0.0.1:9000;
1 w2 i# {' p4 E      fastcgi_index  index.php;( ~8 M  B9 @5 d" i+ `
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;, l' d; O) ?& ~' Y% F) e9 O2 ^0 n6 n
      include        fastcgi_params;) E- X0 V5 ?7 m) D4 a
      }
" l! x: }0 z. U  }  S" M2 ^. x) @; ?3 z
upstream负载均衡:- v( @& c4 G( D) G/ f; o% @
$ f! g9 m" i& P, {* [, Y
upstream 52os.net {8 u% g1 O9 |+ F+ Y: @, r
    server 192.168.1.100:8080;
" q( X" a7 [; F/ q6 f/ a    server 192.168.1.101:8080 backup;% ^, G/ |; s  O& c2 n- B" t* ?9 F
}! b* }! X6 E+ }- m' D. ^1 d5 z
$ x' {2 @$ I8 O/ z9 x# w
server {
. {$ A" n2 W" ^' a' t; rlisten 80;  n8 {4 s+ `- u4 y5 v
server_name 52os.net www.52os.net;
  _8 e9 U$ L7 q  x5 H/ q3 ^2 r! P4 O+ j7 V; }9 D1 [
location / {5 d0 y' ^, b  M3 P- s
    ModSecurityEnabled on;  9 e+ V# B' k/ U, U
    ModSecurityConfig modsecurity.conf;  $ o/ P) L- c+ |$ y- r

. z" o' v% `" O5 z        proxy_pass http://online;7 X- \# ]7 X) G$ Q" U9 J) Q& k
        proxy_redirect         off;! _$ x9 U+ m6 p9 k
        proxy_set_header Host $host;
! a1 M( I9 _4 h2 b% n2 c- K        proxy_set_header X-Real-IP $remote_addr;( p/ a. ?3 Z" }) o  n2 D
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
/ k3 `! C4 n4 `& p    }
# ]8 t' _+ q$ {}$ K6 X" }6 B; o: m( B% D7 g
六.测试
0 U$ {0 c; f& h1 R/ s; f
) c) |3 r7 K3 O# c9 E* m我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:' j8 z) i3 W+ i* ]  `5 o& U
1 a  j# R9 j' D% \. {5 B
<?php
7 Q; t& ]8 n. f' a0 {4 E" G    phpinfo();    ! T+ U, e& ]  p8 b# \- p) ]9 V, a
?>
; N6 e/ P) Y: f" o8 w4 ]9 ~在浏览器中访问:
9 H% d& D6 ]# I/ M* g& l7 ?% N% B# W. N  L; Q6 n- H: h% x
http://www.52os.net/phpinfo.php?id=1 正常显示。  D) d: V1 H3 x4 N+ Q3 ?% Q
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
% ?3 l  ~, `6 A3 c& G; {9 |; ~: ?* Shttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
7 K' H' I6 ?$ v+ Y说明sql注入和xss已经被过滤了3 t* i# x% e4 @
9 ^, `' w. _* E9 ~& v7 C, G" ^
七、安装过程中排错% P# R% \7 l4 l

& T. A" I* M# \+ W1.缺少APXS会报错& k" F; E- \( `4 t0 I& ~+ I$ P
. ?0 H+ J& E* Q% N4 p" a- i, `: Q
configure: looking for Apache module support via DSO through APXS2 E* s/ k+ l& j. _9 u" ]7 _$ {
configure: error: couldn't find APXS7 V5 t1 A" |& [% D# U# x0 a
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。* |# @' S4 u! @# q' l
解决方法:
, M; p& \+ k% `: k- K/ B8 u
3 J# q9 a3 ?' r4 f6 syum install httpd-devel
( I0 P1 R/ o# a* }6 {+ G/ D2.没有pcre. R0 i* }; n2 ]6 O& E
6 O$ {  n& w% y0 K4 K) t
configure: *** pcre library not found.
" _" v1 h" X* x7 Hconfigure: error: pcre library is required
% J5 [0 ]4 ?4 j; {解决方法:
+ X- G9 g; Z0 N4 N2 y
3 K/ k4 v+ A' X0 ~- xyum install pcre pcre-devel
0 M3 F9 _; Z- O7 P& L/ G) y, h' f6 `3.没有libxml2# u% i/ |( z% S' h. d1 ]6 o" t
/ C/ M0 S- E% D
7 j. q4 }+ e5 }
configure: *** xml library not found.
6 H& h! I% l) aconfigure: error: libxml2 is required9 X: L! A9 c  v1 {6 D1 p' z: b
解决方法:* A, B6 T. {0 u! ~
$ r' _  N7 b3 q
yum install  libxml2 libxml2-devel
- a! [$ r6 Q$ A4.执行 /opt/tengine/sbin/nginx -m 时有警告3 U3 r% g! U0 E$ Q6 ~, E7 ^+ t5 o3 Q
2 P+ A7 s& K# s5 [" h6 }
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
$ ^% \/ a9 q! J/ U1 @nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
4 B, Q; U8 D$ ^! W8 ^原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
- d- Q# s2 j: v2 b; E1 ~
/ N% l; B- w0 T1 L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.- S4 A0 p' F& p$ ?: N: C
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
. _* U  C6 ]- A+ ^) ~7 k$ f, Q; W+ `8 H1 `2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
( t# O' t" [- w0 x2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
9 u* z& ~# D" {/ l  p& q2 }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
) s9 I# ]4 H4 j% ]+ z2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
! ]& r( V3 k% Y5 u解决方法,移除低版本的APR (1.3.9)
6 B& h" i, \5 C% c7 u
' O9 M. t1 ], [; i2 w+ Kyum remove apr" M! B- `8 |$ \  P
5.Error.log中有: Audit log: Failed to lock global mutex
: H) S3 I+ O0 Z8 a' T* {, {/ k* t$ [2 H. C/ U7 H
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
6 ?/ r$ V! x9 H. F2 j3 p! Uglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
: w9 a% s4 I6 L! Z7 n- R1 Z  x解决方法:: e: |% r6 X+ U) h' u
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
* {9 F% M2 n: E& Y- ?- Y
9 M4 x! w+ k- \% qSecAuditLogDirMode 07774 \/ U3 K5 A' Y" U. d$ u
SecAuditLogFileMode 0550/ ?/ o/ K: |& e4 o
SecAuditLogStorageDir /var/log/modsecurity
4 U9 m8 i& l- \9 VSecAuditLogType Concurrent) I' d, S/ f) e' `! v/ I& X
参考文章:9 ?" K2 D+ p% s6 w  J' x& p- \# w
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX3 g! j' u4 v$ g' o5 J
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-12-1 18:39 , Processed in 0.052422 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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