找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10345|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
6 K; ]: T/ G6 |& q6 M# M  e2 v
# B/ t% ?1 Y* f2 A一.准备工作2 [7 K3 M2 W1 V) O% c2 m* ]% D: q

* s: b1 k/ l1 f系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.03 r2 ~+ h7 m  }& l
  j( @# Q- E0 @# d2 n
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
8 p& |( U: E% Q# a
8 E. K& m+ j& B- rmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz% _, `; ]8 o+ W# r6 `
; J. R& l$ l! x) {& {
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
) \/ Q/ ~: r: x7 L8 p
3 c1 a9 L+ v. S' {; ~- v. ^依赖关系:
+ x; H  `: }: I; Btengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:( F; U. q. T" @" [4 ^/ k
: t" A: ~; w1 Y. h# H
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel* @6 {% v/ l; y! h, T8 s
modsecurty依赖的包:pcre httpd-devel libxml2 apr
: [+ g/ G+ }3 y7 h) w6 |
$ P* v6 @: _. C! j1 c8 fyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel$ j+ J, m4 P& B  J5 A+ \
二.启用standalone模块并编译4 v8 `& A( x( c1 O+ M. l4 f
$ D# h  e# a  j
下载modsecurity for nginx 解压,进入解压后目录执行:/ p- c2 L9 X4 B" B, U
1 ]: z6 d7 w# G, n' C
./autogen.sh, i! N/ X+ f7 C4 y+ i% u
./configure --enable-standalone-module --disable-mlogc
' j  `2 O8 E, ^' _8 {make ) E' H# [/ t+ i
三.nginx添加modsecurity模块
6 ?6 t( S7 ?& ^8 S3 d) o, l/ ?; P+ x
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
7 x6 E/ g: S1 O" G0 y9 o: J# m9 z& M* g; w7 Y  h2 i
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine+ q& S! H+ d1 V1 v4 ?$ g
make && make install5 |1 \* v+ @' J7 Y/ X0 J$ S! a
四.添加规则7 G3 X" {% T3 Q  [/ i0 U
; z! G; [4 y9 I, J1 J6 t1 P! l: l2 w
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。: w( p9 }+ z, Q2 I: i

8 ]7 A9 r* g5 s) ]' l5 y1.下载OWASP规则:) x+ u: ~3 e6 V& f! Q8 C

, b0 Q$ Q- M' |9 V$ m7 G* ]git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
3 F8 h& g! `+ h( V+ F. C  W) l
4 s4 R$ o$ x" Bmv owasp-modsecurity-crs /opt/tengine/conf/
( W; J# w, I" L) h3 D7 A
2 W6 Y( e( p8 P, z9 ]cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf3 z* I! y5 A5 e6 ?$ |
2.启用OWASP规则:3 e5 _/ @$ i  B& Y, t
0 ~7 w% U, t0 r# N* V
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
9 U8 f2 G- D" b) J! r3 X# c: u' x* }; k: g: Y
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
9 g4 a3 h' Y" E0 z2 e5 G& E
  i* x  J: f( Z* q5 I5 U  lowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
' T" v% B- J5 S3 E2 y! o6 d. F4 J9 }$ V, r1 }8 O
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf$ {( x( N! W: D3 U3 @6 R
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
. g: d' e9 o: Z0 E( V2 z0 \Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf. p8 T9 c  @4 H; o
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
% y2 f$ y  [  |Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
# ]; i, \4 b( K! CInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf# W' X8 V- t, u7 o$ f) ^) o7 v
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf1 d( ~! d. ]. K4 F9 H( p
五.配置nginx
5 c* I5 ?  e+ m9 ~, U4 W
9 O; u9 ^; z! D4 B$ c+ Y: B/ O在需要启用modsecurity的主机的location下面加入下面两行即可:; {9 n% L, \9 E- D& U0 N3 h

7 J" O  N+ h9 ?; r: N) k: xModSecurityEnabled on;  
) e' X( q3 |' `4 ?) uModSecurityConfig modsecurity.conf;- B0 D. k4 s! F! R" M9 m4 j
下面是两个示例配置,php虚拟主机:
6 Y7 m3 n9 Y- S! @2 S2 ?
& Q; n& i/ U) v, w; d* cserver {) t( w0 j4 J& k* ?+ W6 G; R
      listen      80;
0 B/ {4 g, u1 u      server_name 52os.net www.52os.net;& P5 a6 {& [; @
     
, Y+ ~1 p* t) w# H" f: o: X7 s+ F; U      location ~ \.php$ {3 k9 z* M+ _& z5 y2 q' W% @
      ModSecurityEnabled on;  # a# R; Z5 z3 Y0 X4 h( N( l
      ModSecurityConfig modsecurity.conf;
& X: J- i2 e& H) K" \: E# l8 y- y: \" W$ N/ O
      root /web/wordpress;
) n- C+ |; ]% A  g' ?. ~5 J+ \% X* E      index index.php index.html index.htm;
/ F. w$ X  S  @3 p. m6 d, W  
. Y. |( r% J( k' i; `' T      fastcgi_pass   127.0.0.1:9000;) K; m5 [" x9 _( y4 i4 m
      fastcgi_index  index.php;  E1 r% n3 x0 J
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;7 h; |+ h6 @+ ]1 q6 T2 m
      include        fastcgi_params;
) w6 N1 V2 i% M. M+ p  o3 Y      }
7 f) `. Y* [4 g  z- w. }; |  }
/ |7 F  M$ _: B' B$ \upstream负载均衡:
0 {6 I" T3 n. q6 S% d' g
2 F) ~+ H, s' ?% p4 n) }, oupstream 52os.net {
0 |: L2 W  {! Y- E: s3 {5 h* w    server 192.168.1.100:8080;* e* o( D0 g$ w% m+ B# s+ s
    server 192.168.1.101:8080 backup;
$ w+ |- f# H0 ?+ o' K* |& m/ _}
0 A' J% q4 M; S2 n. `% I9 l9 t5 e6 I7 ^4 Y
server {
( p/ B0 T( M7 G8 v: elisten 80;
1 f8 h/ g# h! u, C0 j, vserver_name 52os.net www.52os.net;
# _. x) X( }- R6 q; Q" q2 q- e0 Z  `' p- Z
location / {, |- e8 ~( w' K( E
    ModSecurityEnabled on;  
1 l( X5 h  ~, [) X8 k    ModSecurityConfig modsecurity.conf;  5 b4 c9 Q3 p5 o
: r2 |; ?: ^/ M9 H
        proxy_pass http://online;) E" t5 j( }# P" [3 M: P
        proxy_redirect         off;
" J1 R5 M3 q" h        proxy_set_header Host $host;$ q% k0 T' {" P- X$ A
        proxy_set_header X-Real-IP $remote_addr;0 X8 T$ E6 U6 ~; N# `
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;3 U! S/ t+ _% F& F$ ]  e
    }6 @1 A, V1 f+ r* o7 F, O
}- g5 S4 T6 y  y5 I2 u5 V
六.测试+ f8 L% Z) ^+ N% s
9 `& a5 I% _7 o8 G
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:! y* ^3 m# s  M6 i% k7 }" _3 Q
; L4 l/ D( E& R& G* h8 d- n
<?php4 G8 \% J5 C. y8 o2 X! X
    phpinfo();    & b* k  z! H" q; x
?>$ P4 |# D: }0 c9 g7 v% k
在浏览器中访问:/ F3 x3 K& M2 a0 Y7 K' U, C$ T
; j& Q0 e9 Z0 X$ g  y
http://www.52os.net/phpinfo.php?id=1 正常显示。
$ A7 Q  I1 T% _4 l- s9 x- y* Vhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。% V! P1 o! x4 `0 d( y' F: m2 O# n
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
% Q2 l! f0 N+ I说明sql注入和xss已经被过滤了$ E8 C/ e0 L/ X+ k
7 M' J! H5 A( I, v8 c
七、安装过程中排错
  x# N, {: I0 y. @  u/ }6 h  U# K) S0 M7 P0 r) N9 K* Q
1.缺少APXS会报错) m; E+ E: D9 @
; r+ ~( T+ n6 K# Y  A: x/ v
configure: looking for Apache module support via DSO through APXS
( ]: _5 H( f& \/ oconfigure: error: couldn't find APXS
( A3 X) f3 |8 j4 g2 D4 Q/ Capxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。3 ^* y  a3 R8 n5 q
解决方法:
, Y! N( Z/ R3 P0 q. o8 n
4 z8 W1 z$ O9 ~& Lyum install httpd-devel
' T/ E6 Q  x. ?* }" o- W2.没有pcre
# L* @9 L, ]8 S2 z! r! W0 s$ C
2 i; N; I9 C3 c% R6 W- ^# p' Iconfigure: *** pcre library not found.4 V; h7 z7 K' r7 T. P+ s& R; @
configure: error: pcre library is required
! o$ {4 E7 q) _+ X解决方法:
% f/ D8 M: N) q. ?
  E/ {, A* R( e+ u- wyum install pcre pcre-devel3 R, {% w9 H# T0 }$ c. J" |2 K
3.没有libxml2, A; B: c2 \4 D9 K# I8 O& V
& U4 d0 h, |* S5 ]! r+ L5 c
* \$ R/ Q# `- x- y& J# e; G. Z
configure: *** xml library not found.
+ W7 i+ m) F* n' Q9 jconfigure: error: libxml2 is required
4 w% H( \3 n* x" Y! W5 P0 h解决方法:
! H% Q+ X: t. {' ?
6 A, Z( y4 }5 i. V* ^7 y8 @yum install  libxml2 libxml2-devel* b7 ]: N; g- D5 H, I8 n
4.执行 /opt/tengine/sbin/nginx -m 时有警告" T8 k4 @3 K; }' r8 H% S
% J  q- L. q5 Q# ^, ^
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
) y7 {* z* P6 H0 w5 x, S- gnginx: [warn] ModSecurity: Loaded APR do not match with compiled!& u. ~3 p2 @7 q
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
4 s! |: q  P+ @' A6 O, x& g$ ]+ B% t/ u- H1 v- d
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
! x" M- A2 T4 ~1 b. T+ @8 n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
  I' B% X. o1 e2 J% ~4 U' r% e2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
" T9 c3 {) |( _2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"9 y3 h$ s( _% L* C
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
9 @" P+ ^: Z. y, X9 W, K% S2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On., t2 E: Q. }* r2 V7 @; T
解决方法,移除低版本的APR (1.3.9)
0 i# J) W0 B: Y- r& r
: w8 ^: ~! o: X* a' H  c: Dyum remove apr
4 V" D2 O- y0 m) |+ R5.Error.log中有: Audit log: Failed to lock global mutex, F! W, \5 k9 b( U( m+ E+ n

# {1 P4 ^1 Z, a8 u6 H8 g2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     7 z) m7 H+ \& a! Q" S5 L
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
9 G+ W+ N9 g0 h4 z) b5 B解决方法:
4 |* I& }4 x0 k编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:7 J( `/ M( b1 q' V

3 l5 R9 k# U2 h1 x1 T% @SecAuditLogDirMode 0777
% J0 s! U1 u# v5 I+ JSecAuditLogFileMode 0550+ j, n3 ^5 e7 f; j) ?; h; ^
SecAuditLogStorageDir /var/log/modsecurity
* [3 p! g. O( Z2 e+ u6 rSecAuditLogType Concurrent" Z7 B. }0 {, ^) T
参考文章:
, l& k1 m: g$ n0 J9 P2 Shttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
* S: l+ {) }" q2 Uhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-3-24 01:36 , Processed in 0.061158 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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