找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13304|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
6 N$ q! p* H+ P9 S+ k0 M$ `7 N$ r9 y. I
一.准备工作
: v  }. J! j: L" I8 {; h: z; w3 E2 H4 c2 m9 e. Y# j: L+ m
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
/ ~  c! t1 ?( E
# M, @2 l" y0 ?  r1 D: Wtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz. U$ @7 d0 k* W, x& b
; g7 f/ n+ Y& V; V' |
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz. S0 _: A3 `; `# Q9 N, f
0 c( Q/ E1 j3 z0 x; }
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
6 V; y$ O( r& g& J5 V
! ]! b2 q& Z. d% u依赖关系:7 K/ X* o" H0 w. M  s2 U
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:* C/ g2 ^0 d; r) h9 ?' O

7 e2 h6 s+ k  _( c( Wyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
+ F' e% |( d! Umodsecurty依赖的包:pcre httpd-devel libxml2 apr" |5 p1 p8 Y; a+ K
6 O6 I7 w, W# C
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
! q7 v# l9 k: H& \$ v  G6 f二.启用standalone模块并编译7 V8 _" P7 M  P, ~* r

! H) ^# X5 A2 g/ Z2 `下载modsecurity for nginx 解压,进入解压后目录执行:
  w! Q1 |5 a8 h! m  ^( W( k( F9 F, P
./autogen.sh* u, o; g/ M" \
./configure --enable-standalone-module --disable-mlogc1 _% p+ c) F3 _( h& Z
make
! b+ L; N1 h: {/ Q& }( h  S& T三.nginx添加modsecurity模块
) u0 B: j  q3 j% Z8 b8 q& l: K/ r3 s# O" ~" C
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:) K" I- v5 a2 q) K

7 i' W1 b% Q3 E/ f9 R8 q0 V- L8 e./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
% h# ^6 _( ]7 {, m! {5 _make && make install
1 y9 u; A8 m: _( R( O. N% V四.添加规则
8 q9 }+ Q+ y7 q5 s; g3 n1 l/ S( X2 F, ?& {; {
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。) w! J  a# d+ V

, ^5 M5 i6 b% a) C$ O- \2 a! }" }4 K1.下载OWASP规则:& Y. q  y/ M9 u: R7 _5 a
( b4 ~# |& P; c+ b/ s/ N0 Y
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs# c- K2 p7 `' O- w4 O( S; G3 I! k1 H
! G8 d% G: s& E5 E
mv owasp-modsecurity-crs /opt/tengine/conf/
9 c2 t6 h6 u5 O8 j
  ?7 H; H5 ?" s5 _" g4 I2 C/ icd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf' }; I; c6 T0 w. _" j& [
2.启用OWASP规则:, B- s9 J# }4 R( I3 _. q
" r3 y* t. F0 m4 P4 }9 g% @. E
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
7 r8 ^0 H! u- F* U# e4 ~. }' l# E* x2 J
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on' c( ~/ X; X1 j: {. a! W: R- l
: A# O4 I) {% Q" A5 [, ?
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。9 g  W0 S! i. ]$ r1 \

# O3 q( b# t+ u: [Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
9 N7 f6 ~0 T. r0 z; CInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf+ @' W( u% i. X
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
% A# d( X- Z: @) \0 J) N- bInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
+ R7 ~. B' ]4 X$ k3 HInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf- C4 a) {4 @, p4 S; l
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf1 X$ \5 u; a1 R
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
, Y5 ?6 P6 [; C, d五.配置nginx& K  Z7 o8 i  L( K8 u- ]5 y& u7 y" v3 {  h
, O: c7 [7 ^  j3 G
在需要启用modsecurity的主机的location下面加入下面两行即可:) P2 B* w( V4 U( Y6 G

3 B4 P. `+ {8 c- S/ ^* j8 b! zModSecurityEnabled on;  3 G9 D5 E% R/ R1 O
ModSecurityConfig modsecurity.conf;" b- y2 b5 N( P1 j
下面是两个示例配置,php虚拟主机:9 a" ?8 ?/ j9 ?3 c2 R' w
% _* J3 B- V" g& _+ _+ M/ B- B( i
server {1 a( f, u! Q3 ]& m9 [% s+ K" B/ \8 z
      listen      80;
5 }0 H9 V8 O; K      server_name 52os.net www.52os.net;" D5 r+ Z9 n8 Y$ N7 S; }5 [, l! Y
     
; x# w- S3 d1 b9 x      location ~ \.php$ {: g9 P9 q' W9 r; D1 Z7 d
      ModSecurityEnabled on;  
; _& b' c  A5 P. [      ModSecurityConfig modsecurity.conf;
' L, y! Z6 W+ V9 I4 R0 W. S
. z" X: J( s6 Q$ ?7 @8 j      root /web/wordpress;/ B" M5 x% l2 \; O3 i/ [0 P
      index index.php index.html index.htm;+ J. }- D- V' t6 Y5 H! `
  ) B$ a, w- A) X- d
      fastcgi_pass   127.0.0.1:9000;
0 U# ?2 c5 `7 K& f$ w      fastcgi_index  index.php;
! Q+ B6 B/ [2 s4 |* w      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
2 |4 y3 X- l5 x9 m/ B; |+ w      include        fastcgi_params;
7 S! a- y8 ^: V1 v' ?      }5 v$ Y" \& ]# W( ?" G, ^( h
  }8 Q5 o. P- r$ i" y: v
upstream负载均衡:
* r( A/ u/ ?' w: g
* e+ n. O8 v4 N! q: iupstream 52os.net {
( i  M( x. i* h2 _    server 192.168.1.100:8080;
! {0 P7 C4 l% a    server 192.168.1.101:8080 backup;
* R( m; R2 j( ~9 T, E8 w4 Q}) [  E6 ^; i+ _+ j. `* E

+ v. U- X! x* |+ R8 zserver {( ?0 R! t8 y; s; A, I& v: Z
listen 80;
8 m# n8 e  M* k, oserver_name 52os.net www.52os.net;
; n/ u/ l% @7 ]. o: f' |( R
2 @3 [7 L# \1 R/ [2 Alocation / {% _& z0 t' m8 i2 Z' T( m. N% c
    ModSecurityEnabled on;  2 l! g; J5 S* y. b, u, K# E2 M5 l
    ModSecurityConfig modsecurity.conf;  
& }: v9 V+ H3 \9 z7 j
; O) V, Z( L( `) q# A0 w& l0 p        proxy_pass http://online;
2 e6 v7 A# }. H( @  i        proxy_redirect         off;
1 F0 _( c3 a' j9 @        proxy_set_header Host $host;
# S8 c* A, V! l9 q        proxy_set_header X-Real-IP $remote_addr;' o% H( F! }  m: V, ]) L
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;, Q" X. h+ ?4 j6 c( ]& }
    }
& @' T& y' B+ y+ I1 _}9 f1 i  ]) w: ~& {
六.测试* A* s) Y0 ?2 @& O
, G3 @+ o1 M! h
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
* G' }0 B# j% P7 |% L
) o! a( j4 Z. d( I8 u* ^<?php
  L9 ~2 D# t/ a% t    phpinfo();    1 f( i2 G7 ~& E7 o8 k
?>- f2 J6 P) Q5 q) U
在浏览器中访问:
; s2 }# x: S5 ?0 x6 l% b' Z5 Q  e0 P
http://www.52os.net/phpinfo.php?id=1 正常显示。+ {# \, Y' g; I3 }6 V7 B  q; z0 |8 B
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
- t$ ?! V/ x: w% [http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
+ K) V) d% Q6 a说明sql注入和xss已经被过滤了
3 f, P' e& w, o) W2 U, a
: p7 [! E! W0 Q0 K' n2 x七、安装过程中排错
& D$ d, c8 y# w1 r& }, |& X& j, j1 N
) C* n0 v% [" U: P# B" h1.缺少APXS会报错
$ g/ I  h9 B9 Z) y3 N' E+ x: @0 U) X+ r
configure: looking for Apache module support via DSO through APXS
8 @" N( R/ F# o2 X- d) Q1 R% fconfigure: error: couldn't find APXS
" ]4 m, W8 x6 X  papxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
- b4 U- }4 V' }+ E% }* B. q5 }9 g解决方法:
, w2 I5 ?( x) T( O$ ?) n6 M: M, T" G% G3 V# W8 r
yum install httpd-devel+ K- F7 Y  E( S/ b
2.没有pcre, a  T+ o& U& l+ _: p% m% T  q

5 J  A7 D& p5 u. ^. y) Mconfigure: *** pcre library not found.! I% k3 b7 r6 K% x
configure: error: pcre library is required
7 m+ m1 G" a7 p解决方法:
# L( U: \' \) {9 u3 W, p2 H2 ~, ?2 F2 {, M
yum install pcre pcre-devel! |& o0 C  z6 V! c" r
3.没有libxml2
/ {! [9 y4 I7 Z6 C- g: _
( W* W; Y& H; Q" M" a
; C: y9 i3 @+ c# nconfigure: *** xml library not found." r4 |' \& e! v$ f& {9 E
configure: error: libxml2 is required8 U2 q* Z: J: {7 i2 A0 d2 L
解决方法:
0 D7 }. \- T' S5 i
% O" W$ `1 P% B" l% U9 b+ zyum install  libxml2 libxml2-devel
' B) ]/ y, f# ?  e" d6 Z7 R4.执行 /opt/tengine/sbin/nginx -m 时有警告  d  o, V4 {+ I- B# o- g" s
+ d. N- d# H$ {7 y+ |
Tengine version: Tengine/2.1.0 (nginx/1.6.2)! \% O5 D8 j. h" o% S; ~  P
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!$ a+ x) B# e& N6 c8 S9 C/ O+ v
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log: v# _* g# m+ u/ Y# a( x1 P
/ \9 Z/ M: H/ D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
  {0 N* g; U6 r6 G  h0 Z0 x* K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
3 f6 S7 N9 C; f$ M2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!- K: T) ~# @7 v) w) u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"& v4 Z4 G# ?# K7 J+ X8 W+ ?/ `; O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
% E* e( d+ Y2 ~2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
- Y: q( m& Y0 t# q3 m  j2 Q* `解决方法,移除低版本的APR (1.3.9)
# p- W1 e9 D/ i
9 s: J) D% S& {/ xyum remove apr
7 [# H8 B" i6 k/ o/ ?4 A' [6 |5.Error.log中有: Audit log: Failed to lock global mutex
  S3 e8 _  a- H  Z$ |+ G' X2 @) j3 b; \* U$ s: g8 ~& c" ]& P
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
' v6 F( R) x5 `! C( tglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
- s, \" x3 w3 U+ s) [, K解决方法:
; b, z* i# B0 ~+ p9 [8 v编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:  t& |& [2 Z0 O

! ?; N8 F% M; H* Q! i. b0 p  ?; ISecAuditLogDirMode 0777
, x9 t/ v( M8 _0 l9 I) I% sSecAuditLogFileMode 0550
3 w+ T' y8 n( D9 ~7 QSecAuditLogStorageDir /var/log/modsecurity$ f. C6 A( S: _. L
SecAuditLogType Concurrent
! N6 i1 F% }; b5 H- _参考文章:/ u: J  C( C$ k! O! P' j
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
+ k% H8 z" S9 Ghttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-12-3 01:46 , Processed in 0.054444 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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