找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13220|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。+ o- O( f) O" O% l( _' V
9 ~8 E- s* t! |
一.准备工作
1 r9 t6 F3 s. X3 y5 w6 o0 }7 j0 {6 M- W( g9 u
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0, j/ J: d8 ^( k; _  _8 X
+ M+ H- _, \; z' s0 B* |
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
5 g6 f1 p7 g% q- B8 A
% E7 a1 O+ i, p: Amodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz/ l: b* d. d; u% v% o& [1 B: F

3 x% b; g  W3 Q9 G8 P6 N/ k, QOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs' r) k/ c+ B9 B( Q9 i' R
; R+ m; `5 Q2 ]  U; H, P: t& |
依赖关系:- E( w0 Y1 S% K- Y: S! @7 {
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
% q$ Z  X+ y6 ?  T/ E  g, I" i' e7 c
' B; Q: Y3 ?0 V. J" ]7 @9 Pyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel! y( D) P7 E& y) a
modsecurty依赖的包:pcre httpd-devel libxml2 apr
% K) Q9 @  z, \1 A- l, ?9 Z4 L# G$ _. r2 ]: y! i
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
& }! `4 V( h; g) |二.启用standalone模块并编译
, j2 z$ F5 h9 g; h; v2 N- s- s6 p* ]7 B
下载modsecurity for nginx 解压,进入解压后目录执行:9 C5 M1 Z+ z# S8 Y3 e; x0 V) b

( v+ Y; c# o1 s: m./autogen.sh+ {, J1 ]5 X. J& K# L
./configure --enable-standalone-module --disable-mlogc
- |; \+ O+ R6 \* Q, nmake
% s" Z; ~- o2 z' W. t三.nginx添加modsecurity模块5 c9 a. J' o3 ^) M: ~
7 Y- N( _  |3 u& w, M
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:6 J! n  \% e- p  g: M$ Z1 {" D

. n* ?8 t6 s: [9 u. Z./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine, I. ?  T! P# o$ z: x. R& B
make && make install
1 W- L/ N5 w3 i2 s# ]4 H# g  Q四.添加规则
# m/ r4 A$ I) d  W: e3 c
$ O' B: y5 P( v- k3 r! E8 _1 x' Imodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
5 s& ^" u- u* Y% F
' ^! q: h2 q# {: W1.下载OWASP规则:* p/ j, B8 _6 ^: |

7 Q4 X) ?) `1 \$ \/ \! Bgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
' z' W2 O. Z, u3 [  T; A2 J" k( R6 i6 U4 [) B) g1 `
mv owasp-modsecurity-crs /opt/tengine/conf/- W2 P2 H# x  X& D, ~2 d: ^

+ A* Q, Q" W: ~0 w7 \8 Bcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
) m& I" N2 K- W9 N- Y, d9 h2.启用OWASP规则:
% _! _( Y8 x- m$ p1 [
9 z/ x% t, ^8 R复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。3 ]5 \! ?  ~2 J. u! J
, i3 i3 U: o1 C' i
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on& h6 Q( ]" d) A/ ^
* P- J3 D8 W! y* H# V% p6 L
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。7 P, s- P  v) b9 B* P# v
, K. |6 M9 k7 `3 e1 a
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
7 h% I' X" b7 y% l  FInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf2 n4 ^+ Y* L8 {6 Y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf% E& @9 ^, x0 ?1 C% k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
+ X, H+ K3 p7 bInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf1 Y: V$ f7 g" A' A: u" ?) Y9 E
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf$ Z) A( ?, d$ N( r4 C/ F, N
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf4 Q/ C& `" m) Z$ e# ]8 O9 A! x
五.配置nginx
7 ^/ Z( R  V; x4 v1 C) K
9 [: Y' e9 L+ y* s在需要启用modsecurity的主机的location下面加入下面两行即可:
% @- C3 `& M/ R8 `+ m
+ w) U9 D$ s4 i4 BModSecurityEnabled on;  
2 n# h6 I, ~+ L) r! X  CModSecurityConfig modsecurity.conf;! j5 T- [  w8 r! ]( f
下面是两个示例配置,php虚拟主机:
9 i% f$ a8 R7 y- M% ^3 a
  p  S/ c' \+ ^/ a7 }! x  ^9 C2 wserver {
1 d9 `' [- t, v4 X5 l      listen      80;
. y5 u; d  t, c! h" O      server_name 52os.net www.52os.net;
; ]0 K3 P% J' |# j/ b- @5 M5 Z3 _     # T) a# ]  c2 ^
      location ~ \.php$ {
& T" k" w" K% v0 ^8 V/ P      ModSecurityEnabled on;  
5 f$ @- ~0 ~7 i* e      ModSecurityConfig modsecurity.conf;$ k  r: `. w0 \+ M# p0 _& g

$ ~( G$ z$ E; j& J. |3 U& F* r0 i      root /web/wordpress;. E3 t8 e% V5 l" I
      index index.php index.html index.htm;
1 L' s' h  W0 y. ~! u9 C4 j  
% A/ g' J" C# m$ I      fastcgi_pass   127.0.0.1:9000;( T1 @1 R% R5 d5 \. ]& t
      fastcgi_index  index.php;5 m# I9 n5 ~, e' u6 \  T
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;  J1 O9 Q6 n% o& S6 G1 [
      include        fastcgi_params;
  ^6 u6 ]* F0 N5 }* o      }2 G, Q" ^4 T% y; _: V9 P: B
  }+ m: J: G. a  S3 N5 F
upstream负载均衡:
/ V- ?4 I1 c2 y3 V3 G$ p. z$ l; g6 B5 A& E' R0 ~1 o5 y3 l/ S
upstream 52os.net {
# {& u4 H* A$ T/ h  Y& T    server 192.168.1.100:8080;9 P0 c6 Z, _4 |+ ]
    server 192.168.1.101:8080 backup;
( R+ Y: x) r- h}  K$ F- b5 z' x! |0 B8 n" N

/ D) m1 `  a3 k8 G1 k% Bserver {1 Y! D) a/ o' H9 A$ l8 Y- [
listen 80;
5 v/ x$ i: U. s( z" @' E- pserver_name 52os.net www.52os.net;  b% J- f& A4 I$ r& ?2 Y* G2 b. ]

/ h% j& |' X# Y! J4 Y' H  Nlocation / {: n# h: ?7 i( v) D5 W
    ModSecurityEnabled on;  
# [# T3 r& t' z/ G5 K    ModSecurityConfig modsecurity.conf;  
6 \- G$ e& n+ \  E6 t  V( h
8 ^# ]% ]( H# h% F9 {! y        proxy_pass http://online;2 L9 g' P7 v9 l+ p( T" p
        proxy_redirect         off;, a- \/ p' m) Q$ I4 C0 V. u
        proxy_set_header Host $host;  |+ n+ R/ v& |% K; w! E* U
        proxy_set_header X-Real-IP $remote_addr;
' \. J' S6 V" ^/ T        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
' W& V# G% _0 Q/ R    }  X) z& ]7 l! I
}7 |: w4 P9 d8 e- [5 }5 t+ Q3 v& J
六.测试
- b) I8 ^0 m# P7 E+ j( t5 S/ E5 ]' \( o5 `( w; D) b
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
* u0 b0 P; ^$ }0 P+ K; Q  O% U1 z
0 a" B1 k; h; E<?php. b6 ?3 t& l) B, D* u
    phpinfo();   
6 Q9 z3 n% P) U' @. X?>( F& R' I. @" a9 O. X! O1 X6 i7 T! X
在浏览器中访问:4 Y1 k  B, ~9 d+ y& ]
- ?! Z/ b% X2 f
http://www.52os.net/phpinfo.php?id=1 正常显示。4 @& {' W; m! o* z! u, }
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。7 H# I* @6 \) Q0 E( S3 }
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
7 @: G: f* O* m' [9 M5 P. P说明sql注入和xss已经被过滤了! d7 Z' ~+ w6 }" Y' c" ~
1 c/ t2 F2 V: O( `3 o; ?* j% p0 H- ^
七、安装过程中排错
7 {/ b) P8 |, T- O5 a: U5 N0 l# j6 h% d, t
1.缺少APXS会报错
# R0 N7 ~+ X3 H' z" F2 t2 Q, m, a% x$ \& z' o$ Z
configure: looking for Apache module support via DSO through APXS* |- i, W- E  k( Q: `6 h2 ]
configure: error: couldn't find APXS  |, P; F+ b- c  D" \) \8 [: K- \
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
7 v. F/ E7 O  }1 ?8 e) ]! T0 y解决方法:
, V2 M/ Z0 u9 K5 v1 t6 X8 p8 Z- V# E* v5 ~0 q2 p
yum install httpd-devel: ]# m# M- D8 U3 x9 q; C" k3 ^* \
2.没有pcre
, a/ n6 E$ w# }) d6 e% s# K" ]* X
configure: *** pcre library not found.$ A$ `3 [3 o% D5 s7 Y. K
configure: error: pcre library is required
9 |/ y2 b9 I) p- ~* |' I解决方法:7 m7 J$ m2 d7 V+ @

: g( Y& e1 i9 g5 nyum install pcre pcre-devel" q8 ^4 X. h' ~' U" O6 S- b& V% W1 U
3.没有libxml2
. X. v! z+ V: \0 M& {# N; V! X9 |$ H( u' ~5 H6 t$ }

( y/ |5 _* F; J5 p$ [configure: *** xml library not found.1 {! Q# O6 R5 L
configure: error: libxml2 is required
0 W$ b1 |% F; E# u& s7 |; a解决方法:
# u& B. P# W3 f! `; R2 X! x) [8 s: G4 B, L$ S2 [: L! B% }
yum install  libxml2 libxml2-devel6 S. H  M; n+ c8 y6 w+ {
4.执行 /opt/tengine/sbin/nginx -m 时有警告
- V# c; f8 s$ t7 v- A; ]7 X4 c
. b& C# V  L3 Y( p* _% E' qTengine version: Tengine/2.1.0 (nginx/1.6.2)
$ u# q+ a" p# e- Ynginx: [warn] ModSecurity: Loaded APR do not match with compiled!
8 x& l' O1 r$ ~2 `! Y9 s原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log( R0 k9 a% s  [7 |- c2 ?: k( B

! `" R6 }& t8 e" O/ ?+ r2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured." _5 R" X4 t% U7 I+ |* H8 ~
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"/ y! k* p! N! A: ?9 q
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
2 s3 e& l$ O# g. N& @0 t2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
0 N, j3 l% r" `: u" P4 m4 W" L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
6 }2 b% y5 W& C; R: h" N2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.( d( R) N9 P1 g! z6 X7 N
解决方法,移除低版本的APR (1.3.9)
) C7 u; s/ ]' L, N% U
  i& e3 X: p3 Jyum remove apr2 |7 B) G6 Z: K# O! ?! q$ |0 N
5.Error.log中有: Audit log: Failed to lock global mutex. X  W/ H9 K, |; ^0 N- K" |! F
# v9 x" z; E- P, Y: i
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
% g0 Z, Z; q0 Z- n) R: g& }) f6 oglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
) I9 I# H( u, j8 H解决方法:
3 [- C( H7 N- \( |; z+ j编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:2 {" X4 n' l( x

6 O* m- ?0 j) Z. ^" I+ SSecAuditLogDirMode 0777% c( I( F6 E4 T9 y
SecAuditLogFileMode 0550
# r) Z" v7 ~& G! ySecAuditLogStorageDir /var/log/modsecurity
, z  G( A: F+ Q  b- RSecAuditLogType Concurrent
) K' E; T+ F- H& {# w参考文章:
$ U: P9 U1 e7 U0 O# ?! A5 Rhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX# o# }& Z% w, B3 v& b9 Y
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-25 12:08 , Processed in 0.074637 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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