找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9631|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
. O# d9 u! F% [; i( C
# u. @7 _$ k" |, p3 |一.准备工作
. u; ^# |% r2 e$ z* u  z& r
5 i9 ]% O' x6 b8 `) ~系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
# I& p. i) u$ u8 H
2 X4 b* a  }" I- A5 _  p* R1 Q8 a- otengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
' c. ]1 P# v' g( h8 r1 o3 O$ l  g
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
: D( w) w% ^, i( H# n, l
7 {: W+ p; R/ ^2 K4 c( BOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs( C! f6 n9 U( l1 K: D" r& c- n
% V: D( g( c+ r- s( h
依赖关系:
9 J, e9 X8 p6 {2 |, Qtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:. k( w" o. L- J, r3 M5 v- L: ~

5 ^# g; K; F/ ^# W# ~, K% Yyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel' N4 c+ j& Y+ m% X/ Z" W
modsecurty依赖的包:pcre httpd-devel libxml2 apr
4 e3 h7 z5 L: ^2 Q. D
1 z3 c4 \( t% X% Uyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel, H, N' s+ f  v- ]- C
二.启用standalone模块并编译3 F4 r3 g2 s3 x0 j
6 |' ~0 ]( p6 K" y; w3 Q: A
下载modsecurity for nginx 解压,进入解压后目录执行:6 h3 x, d6 V8 y: j) G/ D! e
4 ^8 Z! l1 D5 L  e
./autogen.sh5 a  k  P2 [' U8 n4 w  p2 \% m5 T
./configure --enable-standalone-module --disable-mlogc7 N0 X& ]/ \7 e
make # F0 X( _4 B5 T$ Q' p2 Z" ?
三.nginx添加modsecurity模块3 U5 @; w2 {# I; d
/ h$ h1 h* ?" @( d1 G0 Z1 v
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
% f9 ~5 ~+ {) {. o+ K3 A8 G& c: X4 K
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine1 Z8 P* U6 j1 q$ y( }( Z
make && make install) x: p7 {% r6 L, P# m0 g9 j
四.添加规则
. R3 c/ x, Q1 a. L' C' U, x. j3 ]( {# J8 r
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。0 L- K, V! ?3 c+ n! L7 b
: a; ]" l0 L$ R
1.下载OWASP规则:, T2 N: V6 Z  ~

- f0 G; p1 O3 |git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
2 M4 X* D) y# t# q& f4 z9 k% x0 t$ a$ v) p
mv owasp-modsecurity-crs /opt/tengine/conf/
* G, k0 z0 T5 R: O) R* B3 s" F+ b5 f$ q0 _8 v# `( A# z
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf1 y) v7 C% X  e6 r8 b% [0 a
2.启用OWASP规则:
" @8 n" F% C$ g2 ?3 `
  N  X7 i2 Z1 h1 b) Y1 ?4 {复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。% C( B( `) v6 G( y+ O( z& L0 F
1 z: L+ u" X" b
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
1 x2 `9 P; m  n
9 C3 `1 U6 o. P% I0 f9 s- Yowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
. v' n4 r# T: @4 L! @" t1 K8 m) E& e5 \1 l
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
6 G7 O" ?. i' \0 sInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf) L  ^+ x. H6 E( @+ {
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf8 l- X/ B- H$ C& f
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
* J  D2 |! \$ M6 n. D1 s" jInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
* G( \( }- t4 ~, U  e3 L: d- pInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
# N% a: r" l; P: X: ]  e6 E9 Y, v1 QInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf/ |) C/ ]: f& G' a7 H* p9 `
五.配置nginx: l* ?3 a4 y' |1 O6 z! H

) Y! [0 ]2 S$ l/ j/ _在需要启用modsecurity的主机的location下面加入下面两行即可:
3 J# M' y7 J2 _/ _) h5 L! p
1 Z5 w4 Z1 J$ f0 w+ c3 BModSecurityEnabled on;  
% |, d. A4 b- _% S0 cModSecurityConfig modsecurity.conf;) m" @/ i- ~. s2 N7 A
下面是两个示例配置,php虚拟主机:& s# @+ O2 ^  O+ o" z! F1 e9 Q
: Y' v- c' \2 I( o: {
server {. k8 g* I! [$ o1 }
      listen      80;
7 i. k) W; z; s& m+ u( n      server_name 52os.net www.52os.net;/ q, H& k$ a9 q; g
     
4 T4 D& y+ U0 a      location ~ \.php$ {
& b- w  E! a% _' {8 _- n      ModSecurityEnabled on;  
5 G& F9 y# b8 x8 E5 X) r      ModSecurityConfig modsecurity.conf;
6 h8 w1 p7 V6 m2 a
, I3 a4 U2 i, Z4 x* b& u0 E      root /web/wordpress;% R. n6 X% X3 c/ r; `
      index index.php index.html index.htm;$ D+ o- d: a. O/ a( `
  
; Z3 U* k1 c8 o) z5 t" @      fastcgi_pass   127.0.0.1:9000;1 `0 k5 Y/ D; x% _8 a3 o6 g  [
      fastcgi_index  index.php;* }4 l  I5 C- P& k8 |6 W4 j) {' J* y5 k5 l
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;0 J' ]# p+ I! j7 c+ b6 R
      include        fastcgi_params;4 c, m7 T# t: e0 r
      }$ f/ m8 c" Q2 ?2 H, C3 _
  }; L8 a/ \4 ?  H
upstream负载均衡:7 k% a& ]7 f7 \% [/ m5 C, p$ v9 |- Z2 l
" k. O* b/ r2 E( C* S
upstream 52os.net {
' N$ a: j  Y$ s& S    server 192.168.1.100:8080;
& P( B: O2 T1 ]  Y    server 192.168.1.101:8080 backup;
9 b2 p+ {( }' A. S1 [}" ^2 b+ b  m' t( k: A, K1 V$ g

+ h9 E3 @& C- n9 h0 h6 lserver {! E0 M& m+ O9 [9 y% _
listen 80;
& N( s3 D7 L5 v4 |  D* Bserver_name 52os.net www.52os.net;
" G7 g8 s( f" h( j. v
) {  D! S$ I5 l! q& Y  [location / {
5 P4 J  r& j: u7 W/ p+ U, A; M, ]    ModSecurityEnabled on;  3 S7 v1 |6 P# G. V5 t
    ModSecurityConfig modsecurity.conf;  3 n  G+ w. s; O+ W

9 P3 d7 A: U3 [* @. I        proxy_pass http://online;# a, {/ J+ G1 b% U  A
        proxy_redirect         off;
3 E& a- A( b0 G1 h        proxy_set_header Host $host;) G/ L% E% o  i( y( a* z
        proxy_set_header X-Real-IP $remote_addr;
; U: ?8 x; U6 `- o* h& V2 F        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;( v+ ^7 Q7 x; B5 C  e- o, v% E
    }
: n( E2 p+ F! X( X0 L. ?* d& O4 b}
3 o" l+ b/ }. R, i) _六.测试
' \2 y$ p8 |2 D$ u
* o, c5 z' B% ]/ u/ G* {我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
7 `; `  Y3 @8 ^# j
4 |- }3 K' N- d<?php
4 O$ R5 J% \$ R( `3 x    phpinfo();    ) b  l( F0 ^( A
?>/ K; `6 m* X2 q
在浏览器中访问:
0 G8 [$ M$ W  r0 _# U0 }! z. M2 |' I2 r
http://www.52os.net/phpinfo.php?id=1 正常显示。
$ g, F2 c0 M$ s* F1 t" ~! lhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
) I9 k# \# ?4 `" O# \http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。+ u6 Y% M7 n' c' @
说明sql注入和xss已经被过滤了3 C4 t( t6 }. {( q

; _& H/ P3 n5 Z; g; E% u! _6 t七、安装过程中排错+ ^3 E0 o+ W% B3 A: q

0 ]. {8 z! [8 j) p9 c7 F0 Y1.缺少APXS会报错
( c& V/ Q1 u4 S) k$ i+ t- S  O
. L: S% O' T5 [  D) Econfigure: looking for Apache module support via DSO through APXS1 z% {& n5 p5 S. d# W) N
configure: error: couldn't find APXS
% w' g0 Z! Y4 @" F& Uapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。+ @/ n- t1 }7 ^
解决方法:
+ v4 e$ _: d4 i; t5 G- M% a' U1 P3 c' J
yum install httpd-devel6 a* Q1 q" `2 V
2.没有pcre
; S6 {8 j9 B6 {$ w
( w3 q, X% P* ]& S" s  P: ?configure: *** pcre library not found.! Y2 q+ w) t# Q, d$ _3 r* k
configure: error: pcre library is required$ m/ {& t% o9 O% ?% x! e
解决方法:
4 b, R7 i; O) U! F
. `) ~- M& T- Q5 ryum install pcre pcre-devel
- J+ ^$ a8 h. U6 v3 @3.没有libxml2
' ]! [" ^, V0 ~& M$ U7 D7 m3 C7 ]# M0 u' q

+ `8 P6 d* \7 `( [3 m  V: |9 Pconfigure: *** xml library not found.
' |. O; D. g6 m+ |configure: error: libxml2 is required
, F- G2 d% `- @解决方法:
' D) r* m  y: ?! L1 B, G$ T8 F* _
yum install  libxml2 libxml2-devel
$ X* H0 y# t; O. a4.执行 /opt/tengine/sbin/nginx -m 时有警告
  Z1 O% h- B" o& J
1 I2 E4 j! H: @% ^) }Tengine version: Tengine/2.1.0 (nginx/1.6.2)( R' a- N4 f* `+ |8 D
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!& T: G7 {% ~( R
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
0 @& A. T0 y5 Y5 r- ^
+ ?+ R" d* U) l" F2 g8 i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.4 y' s( J9 n! `3 i
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"2 h4 Q6 Y! B* V: J3 [, o' f9 s
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
+ Z* u. W# F& O0 _: e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
$ f2 o% B+ ?& V9 Q$ a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
% y0 G" I; j9 Z9 N1 T1 p0 K- |* y$ n2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
. x: P4 e# z$ `0 e/ `% V' R解决方法,移除低版本的APR (1.3.9)" j% i( ~$ l1 \& R; c% _; U
! A: O" f5 E# b
yum remove apr# e2 ~# A0 O7 }" q' a& z% t
5.Error.log中有: Audit log: Failed to lock global mutex% u* A7 w; g" }9 x- |9 g
3 ?% Y! g+ ^! ]! V/ y
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
) s3 n- K  u$ Z. jglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
0 [7 e4 G6 J) U/ s8 ~% y! c2 z解决方法:& ~3 z4 q; \2 q  l% ~% y, N  ]
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
* O8 |. z5 v% X1 L/ D6 z' k+ e7 r: `# b' |0 _: R
SecAuditLogDirMode 0777
9 r4 R4 W- c: S& q% h, H& j. [$ ^SecAuditLogFileMode 0550
1 f7 B3 l- Y: B8 ]! B. K* vSecAuditLogStorageDir /var/log/modsecurity
8 s8 ~+ n2 x/ F* ~( T3 JSecAuditLogType Concurrent, A% ~! G% _# H. D  ~
参考文章:# s. f  a: T1 c* ]/ I
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
. F9 O% v8 j3 {- m' n% G: chttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-1-14 13:12 , Processed in 0.048635 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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