找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10170|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
) ^' z8 k( j) D- h8 A! j2 ]7 B: v$ u0 t" Z
一.准备工作4 l# D- ]& l9 u1 _

+ L: `: @8 P; T7 l  o% W系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.01 E/ J7 w7 o. S; u; l- F7 a: p7 q2 }

$ I: g5 e' J/ e- C1 V/ Ntengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz) R9 P" W: l; c& n8 M7 L  Z3 k9 {! g

5 ~1 M" n# w/ }/ {  zmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
3 I; [6 c2 |) s1 e
, W3 }- \! L) B1 `- F+ vOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs0 l3 V8 U* r4 M  d+ {4 x! u

9 ]- r& X7 b5 X, x' X* \6 X依赖关系:
2 ]& }2 Y2 c2 B2 e) J( |# V& Stengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:& ?7 _. ]7 h+ {) p; M, e( ]5 g! [$ p/ L
1 }+ N7 M  c7 }( Z3 [
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel7 i; G5 C  y  L! |, T
modsecurty依赖的包:pcre httpd-devel libxml2 apr, y: V2 U& s5 q3 |! F

) T7 u. ~1 V2 }3 T* a) Nyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
4 G. M' C. A$ ~: u二.启用standalone模块并编译
0 b1 g& D5 P) y- j9 W- O% [! T/ [% P9 @
下载modsecurity for nginx 解压,进入解压后目录执行:
4 k1 |+ ?1 I0 G) i! |6 E, f0 f' E
/ x. C" n# [# ~* U./autogen.sh
) R5 {0 Q) K: d* j, }./configure --enable-standalone-module --disable-mlogc6 R/ x+ U& X% q1 x
make
# ~4 q4 T( F: ~8 {三.nginx添加modsecurity模块
) l) Y" d1 t; e; M+ w" ^! I# [# L! |* f1 G9 E$ {7 d
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:  ~" \& D; d+ X# X
  r- m  F7 A9 ~. O
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine) q5 B, x6 @/ x! c# P
make && make install
, O/ N8 Q! g; F3 I3 f- Q/ c四.添加规则; S% e! M4 M# H, ]* \# W: N5 o
+ R# x1 H2 @+ ]: a
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。' W  a' z( L# K4 O% |# I8 _* K+ b
6 `6 C; G, k: F+ Y
1.下载OWASP规则:
& x2 U! o! y! D" b2 a% c, m  b/ u' [3 m- e. d: j
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
, k% p" D: q$ v8 |& R$ j/ g( E( {& N: z* S; r7 W# z; H+ a
mv owasp-modsecurity-crs /opt/tengine/conf/
5 L# W2 i( q1 r! q% }$ @5 Y7 ~0 J% k3 d8 U2 k$ `% l3 O/ f& ?  G
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf$ e# }7 f5 L! [- \8 l8 o3 n" u; }
2.启用OWASP规则:
4 m+ N0 T' Q: b' a( L5 b- z5 v( a1 J* \
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
; H. b8 i# Q9 l" O$ `9 X% a% k/ {1 a: C& W2 i8 ~+ B( I4 ]' A
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on" g1 P4 ~" r1 ~$ ]  ?! e+ D4 I1 S

. R% e  D% |8 ?) k+ x; t( yowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。4 [5 K4 P3 k; z3 ]

4 P, [! Y/ n+ [Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf/ a; l7 k$ M5 t3 k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
7 j: S  I' R9 PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
2 [8 I& T9 S) x, i6 KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
9 p) R' D; {; F" ~( Z8 I$ }Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf: N. P1 |6 f/ B( {  v% G
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
# t4 N% ^5 s; S) j' A$ tInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
& k4 Y! D1 k1 o: f$ W五.配置nginx0 {; H# Z: y3 h7 ^  C2 a" k

6 x7 l5 k) u/ i- m8 w) Q& s在需要启用modsecurity的主机的location下面加入下面两行即可:+ x( t( t4 H+ y0 b* l2 F5 m% \

0 l0 W9 E- I8 M+ X# HModSecurityEnabled on;  
  J+ r2 n% ^( P4 v) P' TModSecurityConfig modsecurity.conf;
4 T5 t; J' B7 J6 x2 D7 x$ O6 |下面是两个示例配置,php虚拟主机:3 I' J* O/ ~! |4 w6 F
8 ?3 Q6 ?7 M$ R  d4 o
server {
& `8 E: p0 c% G$ P1 a1 J      listen      80;
2 V6 ~: o6 Q" Q0 R5 h      server_name 52os.net www.52os.net;
5 i$ C5 S2 P5 D# G9 s     ' x( w5 j3 K1 o& N
      location ~ \.php$ {
; @: h$ }! g# m      ModSecurityEnabled on;  
2 C# \+ s* C2 W5 ]      ModSecurityConfig modsecurity.conf;
* V$ W- y2 Q+ o& N$ F5 ?, c
1 a) Q. y, r* |* a6 w      root /web/wordpress;! c9 Q8 [% A$ _7 H+ g) o
      index index.php index.html index.htm;) t7 R! z* Y, K7 `/ R! {& Y$ T
  + j1 ?  F  ^! R; Y
      fastcgi_pass   127.0.0.1:9000;
* s1 B8 v8 Y* a1 I  ]3 b      fastcgi_index  index.php;
, y5 j' v" M; A* i      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;2 C5 J$ b- q) H3 W- y6 ?1 T# d) U
      include        fastcgi_params;
$ E) V' ?, `+ f: l  L; l      }+ w( z' w: H" g
  }
5 S; a' {% k5 x( m3 n  o: `& Iupstream负载均衡:
! j6 Q' l+ N+ \
% u& m0 {6 _$ q3 R8 E- Mupstream 52os.net {$ i8 [* y! m/ f/ ]9 ]
    server 192.168.1.100:8080;( a1 O4 u7 e" P$ P
    server 192.168.1.101:8080 backup;* [: g6 _" C. t$ P, I7 l7 y. L
}
3 D8 F+ F7 {6 d% {9 b3 ~
9 `4 ^- p( x6 H, gserver {
. `6 k" {6 [! m2 H' xlisten 80;. |" C" _" h$ V9 n8 M
server_name 52os.net www.52os.net;
$ W* H. \, O3 R' C* `. q7 n* o( V6 }' ]
location / {
3 p5 M# y& S; M    ModSecurityEnabled on;  
  r1 v$ Y! x2 |( Y    ModSecurityConfig modsecurity.conf;  
4 v( [0 W& ~: I* C* ?
8 r& o' e, M% }: w$ _        proxy_pass http://online;2 U3 F  r, x+ j2 k2 C3 t
        proxy_redirect         off;
+ l8 `( d  j- r        proxy_set_header Host $host;6 X, q$ Q: H9 G* K9 i
        proxy_set_header X-Real-IP $remote_addr;/ |2 }8 ^* G, I; L! D! n
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;9 p2 [2 }" ~2 l! B+ T
    }
3 ]2 b+ @" y# m3 y# a% c}
& |' K) K7 o8 U. u" M六.测试
( F7 k3 f8 `+ L6 D. A5 E$ Z
* N# f2 V/ v" w! A2 C( K我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
& H% @. }: E1 O& F# L7 G" Q4 m+ I: ~
<?php
, P* c: Y& w4 K3 i+ Y1 C6 x    phpinfo();    & F4 f- ~/ k% Q0 b
?>
2 a. ?" r  \' {6 @+ f9 N在浏览器中访问:, E# j& u3 k) {6 C
1 }0 \, {: [) m, \  v* l
http://www.52os.net/phpinfo.php?id=1 正常显示。
, _7 f8 q/ q9 y- v9 g( mhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。  l8 G5 W" q, v5 j; _3 q
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。  a& l# f: G- k0 [: w) X1 m% k; C
说明sql注入和xss已经被过滤了
7 X4 K5 s. n* N6 x. k4 n7 T4 ^- [1 }. N5 _! D+ W# {
七、安装过程中排错$ m) U) Y  z( q* Z5 q1 |

, y5 T6 x5 I3 O! K9 ?/ d. K1.缺少APXS会报错. O* @3 ]( m( o0 k% n; W

2 z. E: a& e. dconfigure: looking for Apache module support via DSO through APXS
; o: c# V7 U5 e1 q, ?configure: error: couldn't find APXS
; l- M3 A" s/ Q' Qapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
+ \5 ^# U6 g" B# `$ {, s, q: U解决方法:
) B9 ^7 ~* {# |+ r1 u
% P& U0 b5 c/ {$ k6 s" W" @+ Hyum install httpd-devel* D+ C- _* f& `; V9 f  C5 b
2.没有pcre9 i; ^  v9 p$ d. b, I# ]
, Z9 D) q% ~; n- k
configure: *** pcre library not found.
: x* w4 Q( R# Z8 ~1 V, `$ v( uconfigure: error: pcre library is required0 l# u/ p9 \, `
解决方法:4 a% J7 s/ n) v' y; V, {- T
/ |1 j. N: L4 ^- a
yum install pcre pcre-devel
: K& P6 R' P; ]$ Z3.没有libxml22 u3 f& m/ q0 t; S# s1 d

: R. d' a2 t# A' G' p
' L; M$ ?  n6 D+ l0 x9 |configure: *** xml library not found.
0 D9 ?6 ?& G3 e$ I6 z8 }: qconfigure: error: libxml2 is required8 \) |4 i( r4 M' B3 h
解决方法:
9 v( t, m* x- j1 J! V  J
: L7 Y2 ]0 e6 h: J8 j6 n; wyum install  libxml2 libxml2-devel8 }9 P2 G1 {, o$ ]7 C
4.执行 /opt/tengine/sbin/nginx -m 时有警告
( @* W# o) f* P+ Z; d- j' }
' C: w5 w6 b: V# }/ s( K8 v! h- fTengine version: Tengine/2.1.0 (nginx/1.6.2)5 P+ {7 F. c( w
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
4 W$ F4 E" g8 x( U原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
2 t' |% ]  I( v" Q! m: C4 y9 m$ j2 M% q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.; n8 e* @. v! V- X! x/ l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
- V* \, L( _+ l0 s( f- o9 b) j' ?2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!  R% @& |$ G/ X% g  n6 }$ j
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
8 C6 s. v9 [* B7 U+ I5 ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
5 i9 R6 o* ^3 ~4 E  X  z/ ?  L5 X+ p2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
: `% `7 T" o1 P# A* i4 U解决方法,移除低版本的APR (1.3.9)4 K" P; e: O0 K1 {. h0 @: y, b- b
3 n! S- ?" h6 ~
yum remove apr
7 {! W9 D) c; ?4 |5 o% t5.Error.log中有: Audit log: Failed to lock global mutex4 g$ G: N, q7 e* q5 i6 t: t) g
6 ]! Q( K/ \* I% u& @. R
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
* E$ a  O* H1 wglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]/ N! l0 X3 y: C. m9 f! Z
解决方法:
0 F0 M7 M6 g8 N2 S* z  s, b编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
: B- y- e: {2 M( q7 ]7 w0 A
8 p- b2 ~9 Q6 Z" V& k6 ^& NSecAuditLogDirMode 0777
6 w3 {3 _- f6 c% `SecAuditLogFileMode 0550
5 J  D! D5 s& @1 k- J5 T$ uSecAuditLogStorageDir /var/log/modsecurity
* I$ A* A! ?9 V' SSecAuditLogType Concurrent6 p* R, \: G2 ~$ W* K
参考文章:
! L/ i& n: g9 k2 o3 lhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX: k7 S" E8 Z9 h& ?  ]7 j* s7 |
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-3-8 12:40 , Processed in 0.109367 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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