找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12853|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。- v0 G6 s% d5 a$ U9 ?
0 T1 F, r8 j0 Q- G0 @# e8 O  d
一.准备工作! o5 F/ `) g" f
- C  W4 W+ U2 `) L4 E4 U
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.05 J2 _% w- r, E7 c8 v  p; M6 U

2 \2 k# G  @9 T* Z* ~" Z" ]tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz+ b. X8 q2 E* j1 Z
* r! O, M- O, }: B
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
7 K3 @7 H5 L0 K2 j; `6 V5 \7 j6 m4 A% h6 p
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs* _! `" P3 u- d' @
1 T  t, [/ |( A
依赖关系:9 z* p! ~  k, i% q: f
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:2 B5 w) s; J; u

" ~1 q+ L# V1 `% gyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel5 ?8 p4 |$ f  }8 C
modsecurty依赖的包:pcre httpd-devel libxml2 apr
/ G2 g, B* p* @6 g6 @- W- e( d' h7 n# Z/ l# N
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
6 L7 P" C: H8 H, {  o- U9 {二.启用standalone模块并编译
4 R; E% F( _4 s- }8 _, V& |# B/ u0 m8 R
下载modsecurity for nginx 解压,进入解压后目录执行:) \9 ~: _' \1 v4 B, F5 s2 ?

# Q% L9 h( @# F! w" i) Y% `, Z./autogen.sh
6 I2 {. M8 c, x# u! ?( z2 y./configure --enable-standalone-module --disable-mlogc
# C3 D4 O0 D5 Q; W8 ^5 ymake
( G  K% K3 O+ x3 @3 ^8 w三.nginx添加modsecurity模块
5 \0 {' }3 W0 h+ V# Q( T- r4 c$ b! R  l5 p' x6 [
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
8 H# [5 c' u; m" a$ U' L3 S: Z8 d
) j7 J" h8 r4 i% m. ?./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine+ g7 t9 O# K+ E- }3 A
make && make install0 U# L9 a. G( y6 `. I4 C8 _
四.添加规则2 k& ~: l( ~+ |/ s& f

* j4 @" K& \- t/ Vmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
/ D, e, M3 y1 r+ M& l8 e7 p5 o- ?$ w1 q# o3 l: V: ~% [% Z
1.下载OWASP规则:* I3 d9 J( A' t

  j9 c" E5 r/ I8 f" E2 Igit clone https://github.com/SpiderLabs/owasp-modsecurity-crs7 _$ }. V1 K% v' O% Q2 T8 I

; s! R  s6 h1 b+ p$ v, d, l# lmv owasp-modsecurity-crs /opt/tengine/conf/
2 _$ L. O: m  {
+ R# j5 V9 J8 Ncd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
0 ]- _3 M& r9 |1 }, q2.启用OWASP规则:
" o  q- Q- z3 q: B. @
* d& h1 x7 m$ h% f  e: y' ?复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
. i4 |) z; d; d% e" J+ o/ F+ `
! Z% D3 U9 }5 R7 e, y: M编辑modsecurity.conf 文件,将SecRuleEngine设置为 on- U1 T2 g5 Q$ t1 ^4 x" ^8 K

5 _$ t/ ~- g" l/ Q' Rowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。. r( F% O# {. m# O7 S! e; |! a
- ^" P$ A* q* D/ h1 e+ A
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf' }" ~5 `  m3 o: n) g) @) h+ T
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
. W- i; s1 c: eInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
- @9 N( l  ^1 x+ p4 E# FInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf' D: B) k' {' b& p3 ^2 J
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
/ z4 u/ ~7 q* O4 p4 t& aInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
% Z, W. J! N* B9 h, GInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
. T4 s3 a/ m& d; Y, |五.配置nginx
% w/ T% M) D6 q, H2 o7 ?  L! l6 a# x) i; D6 Y
在需要启用modsecurity的主机的location下面加入下面两行即可:
6 V: Q5 s/ K3 v! s
' v) M! [; i. t( O6 eModSecurityEnabled on;  6 y7 P5 e8 `% d$ O8 ]2 l' a
ModSecurityConfig modsecurity.conf;
+ _$ K9 f! C8 [9 W: Z8 \6 ^+ i# V下面是两个示例配置,php虚拟主机:
: j9 E* |+ y/ ?+ Z* c0 Y  L( R) H3 E0 x: g" D- N5 T  R
server {
4 {" c, [0 s! Q* X! H& w' @      listen      80;0 G- X( S0 z, e. n; I. [  l
      server_name 52os.net www.52os.net;
. z- p# _1 G4 T5 S# V+ K! c     
( h& q9 M# w3 l  ?  o* P      location ~ \.php$ {$ ~7 S: P5 Z) X- \
      ModSecurityEnabled on;  + R7 t9 b) ]+ W7 B$ Q5 D/ _6 d
      ModSecurityConfig modsecurity.conf;
1 O0 z0 q! `- c5 K1 B' `
6 R( \) T+ u2 x% d0 l5 _      root /web/wordpress;' D* i& O4 j; M) b7 k. C% q
      index index.php index.html index.htm;) v  P! w( ]2 s) V" t
  
9 |8 ]$ B* w+ G. Q, n      fastcgi_pass   127.0.0.1:9000;
( v3 T  c5 b1 e" \- ]& j& z) ~      fastcgi_index  index.php;
1 \; R1 o1 Q& h( V( L( g$ b      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;/ t: K" F3 X  L4 I) i. F5 [
      include        fastcgi_params;( r. P7 Z5 T; s
      }7 H8 {5 Q; Q5 \
  }3 B: ]& M& Q' P! u2 x: o
upstream负载均衡:
) \8 v+ Y' s- u: p7 ?# A7 H- f6 E7 W- h% z% z
upstream 52os.net {
- u( t% r; R0 y    server 192.168.1.100:8080;* l; M: T- N- E* Q( Z
    server 192.168.1.101:8080 backup;! W8 |9 ?2 Q3 K9 j, d0 [
}
/ b' m. x% b+ g! w
0 s. H4 R( T2 P+ @3 H+ Oserver {8 w& G# V2 M, C( m+ h: u! `3 T2 D& ^4 Q
listen 80;
- Q, X& X4 @2 [) ^server_name 52os.net www.52os.net;
$ B5 O4 [& O8 l8 U) K1 {0 u3 G! x/ V* D% ]9 H3 d8 Q/ H
location / {, e$ T8 T2 d0 f! k
    ModSecurityEnabled on;  0 {) o3 M8 C- |7 m+ {1 ~
    ModSecurityConfig modsecurity.conf;  
1 i) x( }: h3 r$ z
9 c- M/ A$ C3 x7 h/ p3 P, S$ I        proxy_pass http://online;5 H% x, Z( _4 j3 }% w7 s0 T
        proxy_redirect         off;
4 ~. n" s. j/ T. g8 q        proxy_set_header Host $host;  C0 s( [1 n, F' t' X4 \  B
        proxy_set_header X-Real-IP $remote_addr;/ A: M: @+ f+ L
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;: P& o4 @/ B; A0 J, {9 V  c
    }
9 C7 P' h% x& F9 T0 M}
& b% `' M+ [; o2 X: f) r六.测试
9 M* x; Q* u" ]; s% w  v' {0 d1 x; \6 a
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
/ W# K; {& X  q9 y! Z& l! u. n6 h9 A' e4 a
<?php: u% b% n3 T! e$ ^3 ~
    phpinfo();   
- U' t- |/ S; c* K5 P9 L?>6 Q# ^# S0 L5 b6 W/ w
在浏览器中访问:
. a1 Y/ E% l" J( l
! C; m: r$ d! n: J% b+ Yhttp://www.52os.net/phpinfo.php?id=1 正常显示。1 E) F; \) G  l6 n# G+ y
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。. N- Q: S5 \5 b: \2 k$ W" o
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。( D3 x$ y9 m% E2 a; g8 O
说明sql注入和xss已经被过滤了' a- \5 Y) B! A4 W) X9 J  D
  U6 t/ V/ y& U
七、安装过程中排错4 v! w0 K( w! C4 X) N, m8 F& j
% U8 x  g! h/ ?3 ]2 x
1.缺少APXS会报错# V  f# C8 e) r
3 u3 \3 e' C- e$ R6 y) S+ O
configure: looking for Apache module support via DSO through APXS
. `3 X, [, \1 j$ s- {; Mconfigure: error: couldn't find APXS4 X' S4 Q- Z% b, V& P& C5 u+ b
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
; r4 E. j5 l2 Q! x解决方法:. e. P$ {6 C: f- G

: l2 W7 L, r2 o0 qyum install httpd-devel. M- D- J+ x3 k
2.没有pcre7 P. G9 K0 ^+ p- z3 g! v0 ^# ^* j

9 ?: z1 m' r3 ^configure: *** pcre library not found.
( ?. `" t- Y* [& N3 r  c. X3 pconfigure: error: pcre library is required
. `# |! d- \* g解决方法:
# e/ A* A, B8 z" g- a9 P
: u+ T* f, P% h3 iyum install pcre pcre-devel: J/ E3 j% d& r- A$ ]0 e
3.没有libxml2( b0 E$ ?, h3 U1 h
/ D! \  C* x2 Q  {

! H, L2 @$ f  C" dconfigure: *** xml library not found.: U* d; a1 Q4 b
configure: error: libxml2 is required8 a9 N% W/ D9 `/ |. T  }* }& |0 ~
解决方法:
: V% H& W4 d( s2 e$ D9 U) ^+ b3 ?9 I8 _# i+ h! Q! E; g
yum install  libxml2 libxml2-devel; b, p+ E! z* z1 N& m
4.执行 /opt/tengine/sbin/nginx -m 时有警告
: k& r- V0 d7 Q; T( c! V" j, w/ R& z
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
# A$ Z2 X7 A7 g9 Mnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
0 F! x0 @! D( C( A原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log* W) I* y8 X( Y
6 Q! {! j& o- o) C9 x/ g4 o! W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
9 O1 U3 p0 \  ]2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"* \7 X3 h. V, B4 U1 r& B
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
! I1 a2 |; \4 b9 M3 y0 |5 O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"# u( y- u  r* T, T, {: r' k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"1 v7 b% _$ `1 u" j1 b' T4 Z) b- _
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.5 D& Q5 R$ G" p0 ~$ L) r  G/ h
解决方法,移除低版本的APR (1.3.9)
$ S& |' p/ c( d% f
4 E9 [( @9 y' [. ?yum remove apr
6 D0 c2 Y' ?5 T; l2 P8 a9 r) n+ E5.Error.log中有: Audit log: Failed to lock global mutex
& Y. j( c: P$ K) b& p7 i: |2 \8 o7 [0 P+ \
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
1 l# G5 P" w2 ]2 j, u" R9 }4 K+ aglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
$ M2 |0 H* R2 {( M解决方法:: h: d: n  X  H  J2 t7 Q6 Q
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:# H) p2 ]3 @( J3 @2 H" H" ?
# T% S7 U2 E6 k! h/ X
SecAuditLogDirMode 0777" l  Q6 i7 ]" g6 s  i, N* M% @
SecAuditLogFileMode 0550
1 S& J! N) A9 c: \SecAuditLogStorageDir /var/log/modsecurity# F3 R% H' T$ s2 t/ s8 }) U
SecAuditLogType Concurrent
/ O/ \9 v, G% _  v. C0 f( \6 @参考文章:% c& X  X1 s0 s" E
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
# P# [  G: ]- C0 Z, J2 thttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-19 03:39 , Processed in 0.104785 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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