找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12804|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。- @, k1 W! a2 ]) k. j9 R; [7 _
4 @# y3 D+ h& \
一.准备工作  o6 }& i+ y& L, x: v, f) U4 `

, I# _% C4 f8 l' B  I6 S4 P系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
' u  {5 S/ V. Z# m' k& f
9 M+ K5 W# K( Ltengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
8 `  H% m+ n% i0 ?: E# B6 K0 e; ^& V- a8 f( k, d
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz2 H- w& p4 u6 s  W) ]; u9 E/ N+ U
  s/ w4 E9 h6 b$ v: g6 z& t- Z7 V
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
9 x1 [8 Y( ~- ^- O; A8 w3 |# B1 |- d/ j! ^& F3 j: w) D' {7 i/ V
依赖关系:/ O2 ?  d' e& J: o
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:' N4 t% H0 |0 v5 M* ?
: d, K  U4 t3 _
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel, T6 Q$ w/ U4 `% x  s$ G3 y
modsecurty依赖的包:pcre httpd-devel libxml2 apr( j" ~% |0 u& u+ r

+ }1 m, T! _  z9 Byum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel: b# ^5 m7 e: B* p, r0 V
二.启用standalone模块并编译4 J3 O, Q+ R9 U+ P
; {# }3 {- @0 K6 A. Z6 d; d
下载modsecurity for nginx 解压,进入解压后目录执行:
% {6 ^3 [, X- a  _# V. F
% w& {: h8 g% R: D* B1 g  {7 @./autogen.sh
# {4 B9 `5 a; h8 B$ C./configure --enable-standalone-module --disable-mlogc1 ^) {7 c5 N2 M9 E( U
make
2 B  x, M1 O; B6 U/ Q1 s9 f三.nginx添加modsecurity模块: d7 u, H5 a% P0 i" x! T$ W

) s$ |& Y( f3 s# ?+ Y9 d在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
* `+ R, M4 Q6 L7 b# d- D& X0 ^/ l' J- ?( c+ q
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine) t& a3 m9 z- C: Z
make && make install! {( t. }8 I! g4 b  k* ^. Q9 G
四.添加规则
$ ?3 i) [! x2 m1 m/ Q% b) y3 @* C' U8 u: ?0 b' p; o
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
) f2 F# Y( w! B$ h& [3 |) u2 a) }7 L8 W3 K& h
1.下载OWASP规则:
& P$ n! R& C. c9 x  ^9 s. w' x% L+ e& r: {
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
3 P) n% b; [2 m/ f$ N4 M8 j& n9 \9 F( N4 v4 d' p9 Q3 R
mv owasp-modsecurity-crs /opt/tengine/conf/, E9 ~2 I5 F5 r- B3 V# n. q
3 |- b/ U4 I+ J3 `- b
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf3 x9 ~! d+ _, o5 p( W; {; j
2.启用OWASP规则:+ Z$ Q" a8 l  v
2 p0 S( |6 M- E. `& f# E( {
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
% [8 s5 g2 @7 e" c  l+ y- i3 B# L5 Y  `4 c, l
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
. B) ^* }( |+ P+ }, m4 {
; K" I5 J- [( Q2 f5 Aowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。% u6 q; D* I1 m4 m2 I- V: P( W

# \- K& c) E0 U6 Z  NInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf5 B- m7 B6 l$ N: u6 i1 Q: g5 T, `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf3 S) ?3 D/ Z( L. ~' d' Z5 l
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf; {) X, Y) j) W- z3 H* u; W
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
& P9 I- ], @2 k: I) _2 eInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
' y5 X$ d+ F% b* y" s& v- R* O% }( B  fInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf& A: p9 [1 x7 ~6 P# V: Y1 ]
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
( s; ^* x7 O* X% ]* Q+ A- ?五.配置nginx
& v; d& t2 c1 k! h
* ?7 D" Y& ^% n3 r* |9 ~1 ^在需要启用modsecurity的主机的location下面加入下面两行即可:
- G7 d6 H" h) I/ l1 [) A$ Q; X% T3 {! s% t
ModSecurityEnabled on;  
/ H" I. A& j7 a- b3 `, pModSecurityConfig modsecurity.conf;
) u; H- d* d  H& w下面是两个示例配置,php虚拟主机:
* ?4 g- g7 C0 L5 y7 n  t+ ^1 ^2 k% F% ?# b! u0 u
server {
9 D; L7 g1 D' i( o/ H1 P; L$ d& a      listen      80;
( c/ x4 ^7 z5 G) _8 e# r6 `/ j+ i      server_name 52os.net www.52os.net;* E' O5 y! F2 M0 t0 q( a' v
     
/ ?# e4 J) B+ D$ ^- ^; g      location ~ \.php$ {
8 d' f7 R: b, g8 s  `      ModSecurityEnabled on;  
3 r6 S/ r0 L; x3 o, d      ModSecurityConfig modsecurity.conf;+ M' Q' r2 R- v( b8 M

- n( v! M; W% O) x5 f      root /web/wordpress;" \: Q& ^1 L) t7 {
      index index.php index.html index.htm;6 p! S: A" a- r: R  H# D# l
  ( D& Z+ U7 @2 [
      fastcgi_pass   127.0.0.1:9000;5 K9 Y) ^) M9 a. y1 t* c/ z0 T" H
      fastcgi_index  index.php;
2 c0 w" F7 E! I5 e- q; O      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
3 ?& U+ t7 l) V/ `      include        fastcgi_params;" C/ x9 u2 ]! @4 H4 N% Z6 T
      }
2 L4 O" Y; b' E" x% y  }2 w! {1 Y' f7 B3 e: N
upstream负载均衡:
- P$ _* Q) p, t# [4 O4 L& D- G3 @7 `) W& _0 ~+ O
upstream 52os.net {
& u/ H8 C' m% M" h/ \1 E    server 192.168.1.100:8080;
+ J" x  ^' q  T# N' H" F    server 192.168.1.101:8080 backup;
, W/ z2 t, K/ e7 B}3 C! ^: G" k: T: z: A" t1 W
" c' f; k  D: y0 ?0 m: m$ p* x
server {8 E3 h" t! H7 _% p* {$ u
listen 80;
# T( @0 i6 T) wserver_name 52os.net www.52os.net;
9 ~' L9 ]' q; ], [1 e3 [/ i$ T3 U0 C' I: I0 y2 j3 o
location / {
- Q  g% n2 M& X: Q* `: L    ModSecurityEnabled on;  
5 f' h: R+ \8 d    ModSecurityConfig modsecurity.conf;  
7 H  P* h; T+ K3 y: ~5 o' [" @8 B0 h# F- j3 L/ ~! A+ f
        proxy_pass http://online;
0 W4 _9 e- `0 m8 U        proxy_redirect         off;8 M' E, m- X$ t5 c2 h& e! y
        proxy_set_header Host $host;) d$ _# W2 _; Q  |3 `  H6 R  }
        proxy_set_header X-Real-IP $remote_addr;
- b' E7 w7 y" w5 A/ a6 ^" W& S" @1 }        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
% P  U3 C( u4 @* A7 N% T    }
" I& f2 O4 z) D( E! Y- h}
5 c* o' G: P/ R9 h; z六.测试
/ E# |1 [4 R( g8 |: Y* l# [0 f( f7 Y6 u5 Y* r
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:: r5 P+ @" l" M) B$ w( @  |2 l
% Z: T  {7 Z( A8 G
<?php3 {7 ~& s( j- i: N9 O, z
    phpinfo();   
3 X) \" \) {3 N# M) Q" `) Y+ z?>  x4 X0 V8 C$ a. R8 P  [' a  G. N9 r
在浏览器中访问:, v: A9 @/ V) e; U
& b# ~/ {) A+ a4 \# d" T0 w
http://www.52os.net/phpinfo.php?id=1 正常显示。8 h* R& x6 E' Z$ q$ f4 v
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
0 }" v: a1 f/ }- W# E4 [4 N# Y& mhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。9 o5 ]) f6 v8 L/ x
说明sql注入和xss已经被过滤了- l# e. ~5 J6 T  F

$ M) o# `7 r8 o* {七、安装过程中排错' K7 M3 |. i/ s) I! |$ c; w: P5 m
4 v! |; ^+ E+ c* q/ L1 c6 Y2 T! ]
1.缺少APXS会报错
* ?  @) L. y" B5 ]' M* U
8 c1 b* P5 V* S1 yconfigure: looking for Apache module support via DSO through APXS
+ v4 g+ g: @- ?4 h) z4 y2 s% }configure: error: couldn't find APXS
* g! m  c8 O$ wapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
4 x. F+ N5 |/ a- |+ d; O9 L! G解决方法:! I& U  t5 W$ Q9 p# ~

# [) ]' [; `  q3 [yum install httpd-devel
8 Y/ _3 c* p+ m% U  ], ^$ s8 r2.没有pcre
) v+ Q. L: j+ x4 D
. S  _) O( v7 l8 `configure: *** pcre library not found.
# O7 r2 h  f$ f, o; ]4 j3 {configure: error: pcre library is required9 y# g' r- \6 i/ u+ ?( l
解决方法:
! y" |' \. e- V4 s4 D  z5 c' s: Z5 ^+ f; o: C% o
yum install pcre pcre-devel
3 n6 v) E7 Z$ c- u3.没有libxml25 ^; o2 {4 w8 @

5 b/ @4 \/ }. o& n! J4 s% v% Q3 I; k8 B4 Z" z0 L; g$ @: C- }
configure: *** xml library not found.- {, F9 k1 v: o1 t# j0 L
configure: error: libxml2 is required# t8 J+ X+ u. e7 v7 x
解决方法:
# r' E% R- Z$ c+ w
, Y# K$ B5 g+ l0 A/ Syum install  libxml2 libxml2-devel9 H. e6 D- L6 U: t$ X8 Q
4.执行 /opt/tengine/sbin/nginx -m 时有警告
+ C* y1 b, ^1 G5 d% c3 D( f
6 b7 _5 ^  o( i/ V: h- H# fTengine version: Tengine/2.1.0 (nginx/1.6.2)/ V4 s) s4 D0 o6 ~
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
4 W: O( @9 V: Y+ _原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log% g2 J0 @) ~/ ~5 F6 z7 m
1 y* r2 o: ]2 y) s6 v) W" }
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.$ a9 c; s0 I: A5 @! s" ]: V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"3 ]/ F6 Z) E2 p* n# w
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!1 A# L1 h! b; P+ V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"5 E+ k. p$ P6 e" v2 b. A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"/ }* f( J; ]9 t8 b- ?0 A
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.9 z+ w' g& I% x
解决方法,移除低版本的APR (1.3.9)
! [4 e& `$ u/ X, |2 v. Q0 M9 l. }
2 f: |0 x1 G  @7 q; n/ Q2 @. x6 ^yum remove apr- l# L: X9 e" N! E$ c
5.Error.log中有: Audit log: Failed to lock global mutex
' k, h) u, J2 z3 e0 h  U
8 \9 Y7 \+ ?' E* b4 ~8 R  ?2 V2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
. A. D( x& C) J& Eglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
. }" E3 V7 ?" i. J4 X( w解决方法:
  \6 G% h& B# n6 s( [* c" |! R编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:6 U2 K( m1 ~' G

2 [, Q3 I7 ?2 \! u1 H; ^+ C" Y) VSecAuditLogDirMode 0777
& f7 L5 A- j' aSecAuditLogFileMode 0550) U- F, s! P( X, o$ ~3 `% F
SecAuditLogStorageDir /var/log/modsecurity
5 a5 @$ D: `. `: QSecAuditLogType Concurrent% K6 F9 y) K4 \, h
参考文章:
3 d8 k6 t1 R1 L6 s% P* Ehttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
1 u7 A- d1 {  e2 M: `; ihttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-13 04:14 , Processed in 0.066563 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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