找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13065|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
4 p, X7 A% H1 N4 s
9 s4 q" g8 l0 c; ?  l一.准备工作2 n, \3 ~( ^, g( N' h

: C1 [/ Y# f3 }5 c2 }' v# l3 l7 b( v系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
4 n) v& b5 l0 ]. }6 K) N0 h! ?5 v0 {6 h. A; g. [
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz2 l, x, O7 w. X3 b
0 i  z. t( V7 U* T4 m1 j& V$ E+ _* Z9 ^
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz1 p+ D% v% N9 {% U9 Z  `5 ^

( }* r. Y) ^+ M0 v- JOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
( e8 c4 |* P$ e2 i0 v0 ]: N/ d$ @+ D' X- l
依赖关系:$ V! K7 _5 ?. m* r
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
! e5 V% C; w; ?0 H
" A. [2 x  Z, k7 K) P2 T$ R+ j7 Eyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
& D% \+ {) g4 K' }modsecurty依赖的包:pcre httpd-devel libxml2 apr# E. A7 N; n4 h0 N
8 @! K2 K0 Y( j5 }! C) D
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
6 P5 a3 ^9 a) s; \6 Z二.启用standalone模块并编译
7 b- F* ?7 E2 g& @3 S3 H" a5 z
下载modsecurity for nginx 解压,进入解压后目录执行:. n0 ^+ T* S4 K! \  o
( F! e* g  h% J" v' e& ?
./autogen.sh
9 V- j+ a8 ~! u. x+ _- S/ r. w./configure --enable-standalone-module --disable-mlogc5 ^( [7 g1 ^& D) S
make ; }7 s& F9 ?' o7 }" w+ R, [$ f
三.nginx添加modsecurity模块
5 ~. o4 x5 l% D% y1 G' ~- e) p4 y7 ]. U* \
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:% W* ?2 a# i% u

; j' q. N  N7 ]( Z! I./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine1 e& N( q5 r) u2 L3 {/ f
make && make install/ c0 ?! w0 h% o9 i2 f$ {  U7 N
四.添加规则: N: b+ c7 _8 e6 t' i
1 t0 S! e7 N& [5 ^- T$ ]2 B8 r
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
* H7 y7 A  U8 f: L  k$ Q" l; ]! E% ^9 Y  Y0 `" @' w
1.下载OWASP规则:
( p! a7 ~- ~/ P' H2 T  ?. n$ Z* B1 j# x9 f7 t, d+ h
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
, _0 @' j1 I& s- k7 T( f6 u* E2 C
/ T/ O+ r* |6 imv owasp-modsecurity-crs /opt/tengine/conf/5 O$ s$ [6 x( `1 i2 Y

# ^8 G$ D4 t: [( j3 K( Gcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
/ n+ C* ^0 |5 X4 R2.启用OWASP规则:
: l0 ]2 g: x  U! t( m0 N# }5 Q, g0 I0 M5 U0 E3 \$ W( v
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。) T( N; S! f& P( H% @9 w
7 Y1 B$ r8 m5 j3 e! Y
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
; y: X$ @) ^& _1 I9 k) a) @2 V+ U5 S* @+ x, X2 t
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
, r5 |1 y9 T# \9 t9 X" ^' f9 k# ?- n
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf" P0 g, K* A$ G) Y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
6 |# Y5 A4 c. v# p  ^Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf, G+ s+ A8 m2 Y5 t1 l. t8 x8 k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf& Q( w- w( a9 e  X  q; P
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
7 q0 {/ P- D* U" R" E) w) C6 yInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf2 o3 A7 [7 [+ e$ [( U' c: U
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
( b6 |8 Q* ^( e5 u五.配置nginx
3 C9 B; G. p6 N( Q" [
4 B+ F; u5 Z" \9 E在需要启用modsecurity的主机的location下面加入下面两行即可:* R$ o& H2 @+ S& A

1 R& H$ S) D" A( RModSecurityEnabled on;  
( _" L3 m& F7 G9 H- SModSecurityConfig modsecurity.conf;: V3 ~1 {# Q0 {7 C
下面是两个示例配置,php虚拟主机:$ P8 w9 u; T2 w$ y

' u. D) c% W3 p$ G! _server {
* n' o, `1 {% l/ I9 n9 @) a- o, M      listen      80;
+ H5 w; s0 Z& B      server_name 52os.net www.52os.net;3 z. O, Q. t5 U  H4 G3 Z4 _. O3 E
     
  @3 b3 s5 O* U" U. C  E      location ~ \.php$ {# r$ U  d7 W' ?) G
      ModSecurityEnabled on;  
% I' Q5 \- ]  P7 s      ModSecurityConfig modsecurity.conf;5 l! A7 v8 H, v) W' Y8 U
/ Z6 p; H2 w7 V% V: R5 n; y
      root /web/wordpress;
; E3 b7 Y# r- k) }" k      index index.php index.html index.htm;
( Y* ]; m& @  p  
  E. j2 {% f9 s% g* l      fastcgi_pass   127.0.0.1:9000;
$ L4 A; q, v% }& {' v      fastcgi_index  index.php;
+ _$ e* X- T2 e! c3 K% K# z& O, Y+ X      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;' X+ [, @/ d# i& F2 {; v
      include        fastcgi_params;
# d5 B- t) l8 t- X      }5 n, p* @# U/ t+ |: k" U$ |/ F
  }9 {' c( t: I' R; V5 P( I/ _: W
upstream负载均衡:6 R! N0 f3 l# c

! [5 G4 @! l. E( m" M( j' J# x* a1 kupstream 52os.net {# L2 o9 Z* @. S+ n
    server 192.168.1.100:8080;
5 p3 r* @( a) E8 ]  ^    server 192.168.1.101:8080 backup;1 v/ L! R; ?8 }* |5 A% ?4 m: |
}: ?* Z* ^3 _8 x: t( u! x' L& ^
9 Y! o! [+ T9 x, }5 v$ |& X
server {
: [# z+ }. f% B6 W+ p/ Slisten 80;+ c4 H) p8 m" E7 t. ^
server_name 52os.net www.52os.net;: n6 j: S% }1 S  B: g4 i
4 Z& n% u' F! t  X0 K+ D
location / {
  I1 @- |( K2 ]$ r9 Z    ModSecurityEnabled on;  
& o+ b$ N; e( ?/ Z" Y* w- E- F  i    ModSecurityConfig modsecurity.conf;  
5 [2 _* U' R9 v3 M# Y: Q
6 q9 M0 z( R/ n# {; A        proxy_pass http://online;: w- C+ d; |  O( ^3 R7 W' a) W' t
        proxy_redirect         off;
0 d) f' t8 C( j: ]' E        proxy_set_header Host $host;
# |5 t; t. Q7 u: |' U5 x        proxy_set_header X-Real-IP $remote_addr;
' F8 Q2 c: v5 _: R2 S5 r        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
: ?& J$ g) R8 [1 l1 {! m    }7 o( S. f* p- C
}( f3 Z0 ~" ]; U& M8 S* F
六.测试) H5 p+ s/ D8 d0 \0 {' Q

: R7 d7 H7 g/ a& L- b' y我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:6 g1 f# W8 f0 C' K) q

( h& y8 X+ i- {; \" I1 C) j. ]<?php
3 a( x; H( T5 ^# a9 r6 E' v    phpinfo();    ; a6 J7 F, V8 V
?>
3 i/ Z& h, b3 E" I! ~/ k# i# E- z在浏览器中访问:
* m" b# Z: C7 y2 }. W  r: S$ ]0 G0 v+ M3 B" w7 o' A+ s& m# G4 R# l
http://www.52os.net/phpinfo.php?id=1 正常显示。8 k* y6 ?8 }' Q4 K# M5 @
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
1 V# |% K1 |: o1 Q: f6 ^http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。6 l% l/ H) A  [. a) F; N
说明sql注入和xss已经被过滤了  h( C5 g' P* ]0 z4 S& h
% |# d3 P5 }6 a. f. n% b
七、安装过程中排错; B$ i; t& Y- x, C) P

' K: l. @: n2 q7 V1.缺少APXS会报错) i7 P+ D" l& V3 ]. Y1 h) V- s; f

7 D( {" }+ k. ~: Q4 ~configure: looking for Apache module support via DSO through APXS: |2 E  c" [6 C( w$ a  W
configure: error: couldn't find APXS; F3 Z) Q0 ]% V2 y
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。  ?3 v; r/ h5 a/ H8 S; T
解决方法:1 G* O8 ?: w! A! [& ?
& \$ G5 m6 _, P- n
yum install httpd-devel& w" Y& u6 n0 y& A* A
2.没有pcre
# S; }: `; _" y- `# y) o" M# G$ s' a; W
configure: *** pcre library not found.
! ?# m2 B: G/ `configure: error: pcre library is required
( K' O$ B  l' X5 O解决方法:
% ^! \& d4 n" S' Q! a  O/ H5 u5 H$ P8 M; Z  O3 g
yum install pcre pcre-devel
3 a9 ^- ?8 |+ a4 J: l( F* H# I4 n5 N3.没有libxml2" R9 j+ F0 j' G& e5 m

8 e# t4 _/ V! s8 I/ q4 z, \9 c0 N) c0 U6 L! n
configure: *** xml library not found.
- Z( H, F! V$ @* v/ d) ], a# bconfigure: error: libxml2 is required/ S5 @$ w  W* K5 z* s9 x/ A- {8 V/ y9 I
解决方法:/ J: k; P9 s5 O: W+ o9 N
! f6 C% W: H5 C' w7 `4 _8 b
yum install  libxml2 libxml2-devel
  A# _$ a4 W% I& L% P6 l$ b6 z! A* z4.执行 /opt/tengine/sbin/nginx -m 时有警告( K! w1 u) h: b
# p# ~- I0 [" ?7 }5 D
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
9 ^' z2 A" f& y: {* }' I2 E$ Dnginx: [warn] ModSecurity: Loaded APR do not match with compiled!. F' k/ i. \8 I7 n* G2 S: _
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log: v: z' T# V1 R
0 W% S9 Q" p5 w3 J$ H% m$ U% d
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.& |6 N5 d" l$ w6 i$ L+ ]) m) W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9": n' {  J7 e' ^; {" }
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!; S3 Z* F' V. a. o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"5 I# i( A' Y9 U& s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"2 s8 |- r  H" W# S1 y* o
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
" e# j5 O; L( g  M/ [, c解决方法,移除低版本的APR (1.3.9)1 k% m: y' X; ^7 x  j& V3 Y

3 Z& G. b. W% m" r3 @* xyum remove apr3 B$ W! [% Y3 o! S$ ^4 X6 I9 E
5.Error.log中有: Audit log: Failed to lock global mutex
1 n8 T6 b9 y; t# i; `. q) d; E2 ~% D8 V9 s/ X
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock       g0 t8 ~& e. G  I% T, o7 n
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
% ^. v% D7 W$ ~8 R; S: J3 Q解决方法:
1 K. A1 x$ [4 H- k0 k4 S& m, ~6 n编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:- X0 i5 Y. ^% Y9 a0 [/ j
$ j! l( [, h  c9 p
SecAuditLogDirMode 0777
& U, i7 _& A1 w  l/ h* aSecAuditLogFileMode 05502 F  C, z4 y6 d7 z7 Q! N; o
SecAuditLogStorageDir /var/log/modsecurity
. K! G, u  Q( I2 ~& CSecAuditLogType Concurrent5 H8 I  O, s. N  T  _
参考文章:
$ v  O* B( t# Z, whttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX( {* {0 u# u/ p
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-10 04:21 , Processed in 0.067331 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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