找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12325|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。/ i# J) Y7 x9 D
( N5 j* Y) y6 V) n% V3 {( u
一.准备工作, e  K$ x6 C( a! l" x
& F6 C6 ?# l7 j; m: s
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
+ S& {& a/ W; o6 D$ h% {5 |9 E% j( Q! I; m+ F
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz' A9 X" _; _. W$ T

4 `" x$ c4 f9 `. ?9 A8 Omodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz, q3 c5 i' {  @: i$ R
) r% K/ q5 o" U9 V6 h1 I
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs, R6 }. [& Z3 I9 [1 l
& z" Z1 t5 v3 o$ w8 |
依赖关系:
1 q  L. J- J, ]# ~tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:4 Q& ^( X( N2 L

' E8 s% D, S: J) ?+ \% U+ ]yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel. x9 E  y) B* _% }, Q: \" |0 F
modsecurty依赖的包:pcre httpd-devel libxml2 apr( z& g8 n& M2 _
9 S3 X& a4 m5 l3 ?9 ]8 r
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel5 Q: g8 G. N  m7 T
二.启用standalone模块并编译
6 J5 @! e+ \3 ?6 n. D& j9 ?
+ Q9 V2 M$ }! w: r$ f4 i  T! p下载modsecurity for nginx 解压,进入解压后目录执行:
% p- w' _+ @! m0 I( k4 U. f) p6 L5 U% n7 E' ^7 V, |$ C
./autogen.sh2 N  o$ s( ]% y
./configure --enable-standalone-module --disable-mlogc
, b! V2 c8 n6 j5 K; q* `make
8 m$ P& h6 ~& z1 J三.nginx添加modsecurity模块0 b  e- q- Z7 W' O

/ y$ p5 q2 p& Z1 M2 V在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:& V7 M0 ~' ]+ |
/ Z4 Z- b/ `  z& i! y. p7 Q- `
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
8 C2 R* ^7 b" o; ]& L. |make && make install8 e8 f4 T6 G/ b8 t  m& ^8 O$ y- S+ P
四.添加规则
7 z2 \5 u6 I, |' G& m" r- A1 C' ^/ A' E% P! s2 b
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。; A1 b' B+ ]- P( c9 k) c
- m6 }  `! U/ \, {# Z
1.下载OWASP规则:# a. t+ n. \; u$ @" n6 J3 a
( L5 b; S3 q1 `
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs6 a3 _+ L2 U* D

5 n, h% Y, _1 D/ @6 umv owasp-modsecurity-crs /opt/tengine/conf/. F$ i- S5 Z0 _6 S. u( q' f
1 S: _% S) q* p6 T
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf2 B" C/ A* n6 `9 ^0 I2 F
2.启用OWASP规则:
& i& o1 N# U8 S+ n. t$ i! M/ `, Z# j
( ?+ S* ]: [8 g' ]) ]+ N复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。, N9 |' P: [$ I( {' }# k& d
+ T2 l3 y5 I7 ^- v9 n
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
5 v+ i+ F8 a: Y6 f! {. z
# [0 }- ?3 D7 v8 ?. H; k  w1 cowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
! D8 Q# Q# f1 |- ?8 P! H, _
. N* R- {% i7 p! U: q5 DInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf* ?( w- g: }9 |# [0 c$ u
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf9 R0 ?0 W  [, T$ i4 g
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
) c2 }: N- [) |$ j; \' M  X" WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
2 a. g0 y- E) T/ w; [Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf9 l- r0 S" E& ~0 @! [9 C( z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf: l# S" {. D* W$ P9 C
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf" F7 O9 C5 I" V
五.配置nginx. ~  E' ?) N. l3 C

9 S, A, e* M& Z/ ~在需要启用modsecurity的主机的location下面加入下面两行即可:% e* _( O  w; {/ J/ D9 [

) U$ g9 h3 H! wModSecurityEnabled on;  
9 h% W% Y# h- B% b" F4 s$ PModSecurityConfig modsecurity.conf;
! y$ ?! x  G  p+ L& Q下面是两个示例配置,php虚拟主机:
3 b, J! P% l9 M
% e" ^* s- s) g( `$ E7 }) F9 hserver {
4 ?' A4 S; B$ s5 O8 |4 m      listen      80;
" ~* P- T2 _) Z" {- Z7 J      server_name 52os.net www.52os.net;
0 A/ w7 A! X( {- L" l       w& e; i0 r' y) b1 F
      location ~ \.php$ {
9 G$ j6 j5 n0 ]8 t' A      ModSecurityEnabled on;  
0 @0 [/ i; O: ]3 ~; |( M: k% O      ModSecurityConfig modsecurity.conf;
0 R0 {/ y6 D7 I. D, D
. b/ f; c% B' F      root /web/wordpress;* e3 T/ i) `" P2 g% O
      index index.php index.html index.htm;! p5 C5 [5 T  l/ e  q, j
  
' S; u8 Y$ Y6 `& L6 F/ ]8 W* Z      fastcgi_pass   127.0.0.1:9000;9 t8 T# B; S" ~7 t7 ]. M- e
      fastcgi_index  index.php;
, c6 S- }3 X3 |4 f/ D      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;: `& T& }5 Q9 }/ K
      include        fastcgi_params;
6 R; V! a2 U) c! N      }; Y% @7 a0 d3 S. h  {
  }  m* A0 ]* B. _  Y6 {; I
upstream负载均衡:
' j( v* V6 t  U2 R, G$ N9 L3 o6 Z1 g
upstream 52os.net {% ~( ^$ u/ A3 W+ t: s/ p
    server 192.168.1.100:8080;0 V& e& O" @2 r
    server 192.168.1.101:8080 backup;* B2 Y: w* S& ?) x% f$ f3 r0 R
}4 q: O6 i. s! L. ?
% j7 b, c9 y& J
server {! n1 h7 L8 [- j  W# B
listen 80;
9 D; R' N1 ]% Z" pserver_name 52os.net www.52os.net;& t6 P$ i  E8 U7 J# E+ f

! ?# q, w' {" C4 ^- \7 slocation / {' `4 I4 L! L; s. ?, o3 v9 q! M' `0 v
    ModSecurityEnabled on;  
$ p. u- |* d1 C' y    ModSecurityConfig modsecurity.conf;  
; p6 n' o; D2 m3 N
3 K; y; C' d0 a& i( _& N8 l        proxy_pass http://online;
& M/ k! g: P4 {( p) M$ m        proxy_redirect         off;3 b* X/ g) w( N2 w/ Y$ ]7 O
        proxy_set_header Host $host;# i& j. |1 z4 C
        proxy_set_header X-Real-IP $remote_addr;
$ n" a3 a' H# o0 n' u' ~1 t( R/ }        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
0 |- ?5 s- v7 q$ ^& d$ p! v    }& Q- U0 _4 t) v" v
}! T: n7 S7 H7 G! l' p; A  U0 e
六.测试$ w: B0 y6 b8 b

1 A& k' {0 |% C- r( ]( Q我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
  ~6 g4 a5 u# G. @( Y9 B$ t6 P. a% |
<?php( V( _# r/ `7 @
    phpinfo();   
* H3 j. }$ i9 ]; u8 h?>" \$ Y- U2 ~! e# E/ o1 j
在浏览器中访问:
% O& f7 l0 {, x4 y. m  n8 k: Z* r3 c  N+ X( E+ y( G( o2 g8 U& q- E
http://www.52os.net/phpinfo.php?id=1 正常显示。9 t4 q& ~5 B+ P7 w( Q+ z
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。3 [  _( m4 X* z* d1 v
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
  R. U2 |2 Y7 H! A$ s* q说明sql注入和xss已经被过滤了
) M$ X. \) r: A6 u  z# s, L, r; J4 E/ q% ]; A' a
七、安装过程中排错4 g1 w6 b2 W: e, i1 v3 v! n
! |& h/ a$ T$ D( e
1.缺少APXS会报错/ w$ f" d; c: T; u# |: K# C
6 _0 f6 q; _+ c* l
configure: looking for Apache module support via DSO through APXS  V  `, k* b: {: k: y/ A1 c" y
configure: error: couldn't find APXS
/ m& H; o$ Y6 r% H  ]apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
! C' ]! [# M0 p( V9 T8 s# u解决方法:/ d/ a) e1 c3 w, _6 L3 p2 K

, [# A2 _9 P" g* _+ c  C2 H. Yyum install httpd-devel& O2 i7 s/ ~! n  N& z
2.没有pcre
- E) D" N, @/ ~1 \1 ]3 Q* ^1 ^4 F% h/ f1 Q, c5 e: _
configure: *** pcre library not found.1 v7 ?& }( F- r( x- [
configure: error: pcre library is required' o( e' p+ e: X# E' t: J2 D# ]7 x7 Q
解决方法:
- N, X( w: Q1 w
- f/ x: J+ D5 q. {* G. ^yum install pcre pcre-devel
. d6 ^) l$ S% ?$ D/ g( ~3.没有libxml2/ K' }$ z5 g" v% Y5 i' y! q# X

, C% R& Q: P! \
! I! ~1 x# U# C  Gconfigure: *** xml library not found.
. p  v7 i7 p1 J; Q# m$ i& P1 ?configure: error: libxml2 is required
8 a8 b, A; ]7 t( s+ H解决方法:
9 w4 G/ b# v* c/ x" L. x9 ?$ b% m6 ^7 B
yum install  libxml2 libxml2-devel' I2 U5 z0 p! V) _: M6 S; b8 A1 B5 `
4.执行 /opt/tengine/sbin/nginx -m 时有警告. z. T: n- \, A* x

: R0 g* Q& T- Y9 Q# Y4 YTengine version: Tengine/2.1.0 (nginx/1.6.2)
/ j( ?2 M1 {$ r/ l1 G; Knginx: [warn] ModSecurity: Loaded APR do not match with compiled!
* J) H; ]2 M2 C原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
& }9 l) C8 M- Q! {( Q% N- }2 T+ P! \4 i0 ^
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.0 Y* L/ I$ S/ ^. J+ u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9": I" Z% x8 g5 N$ D
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!1 s- s8 q$ M) j3 j" x
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05": Z" P6 e  A/ f) \8 G# Y& ~! S
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
; i5 h9 K( j  p7 Y) n2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
& d+ O* R3 w( V& h# I* k' u解决方法,移除低版本的APR (1.3.9)% A) Q2 r* x. K6 G
# i! |8 R$ |2 d, ?- h2 S+ o! @5 H: ~
yum remove apr- q5 H4 i6 V  V
5.Error.log中有: Audit log: Failed to lock global mutex" ]  l9 Z1 Q% [7 i' U7 a: ]" D- F5 s
  L7 J' ^0 Z; ?3 u
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
$ g5 v2 I  j9 q4 y1 M! f3 p( mglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]* G1 E& W5 ~, `" p+ d4 A; ^
解决方法:
6 u$ v: d/ s% _) z) }编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:0 q: e8 d& L. E: i6 P/ Q! M' c

, p. S4 j" P8 K" j) R6 ^2 bSecAuditLogDirMode 07776 b0 N  w/ J: K( H
SecAuditLogFileMode 05500 Q* n5 f0 K. l" @
SecAuditLogStorageDir /var/log/modsecurity
' Z5 b( \- e" M; `' ~SecAuditLogType Concurrent
. @& f  i& G( S1 k& X) ]/ H( ?+ M参考文章:
& ^4 H1 H0 [" ^0 P; uhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX) ^7 N) W7 `$ `' ?. }7 q, R4 l
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-27 00:23 , Processed in 0.087529 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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