找回密码
 立即注册
查看: 5300|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。/ F) ]" c7 ~0 i$ J+ z9 P) T

2 @) E$ m1 L  b/ C- I一.准备工作
9 \; Z& i7 l$ C/ Q1 [
0 r5 |9 V6 v4 M系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.03 r: i3 S" H& G. i2 O
4 m: Z2 ^; t. j6 u- Y
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz6 Z7 J4 z( N( l. f/ |
0 P7 c" K* ]! u& S3 a
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
! `4 M2 y* ^$ r0 g5 M2 t
. B- H# c, P! }; }/ C" c7 J1 \* UOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
: {/ T7 r. B% o- C9 g+ ^) H  E' z
依赖关系:- G' i' [/ [- d; P* y: y1 g
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:1 t! a) ~5 w: r

3 I* t  i" F+ \& vyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel3 p/ t6 [, x! Q8 g2 o# E
modsecurty依赖的包:pcre httpd-devel libxml2 apr$ D- ~1 D6 H& [/ G/ S) U

6 F2 k& v& A  T: U2 t0 Oyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
9 J1 d7 c- m  P, q9 N二.启用standalone模块并编译
- F' x1 b  ^' D. A) b5 m
& w+ I& }: f0 p) W/ M+ a下载modsecurity for nginx 解压,进入解压后目录执行:+ f+ N& E- n& z
  @% G; t# D9 k8 j1 O1 ~3 M
./autogen.sh
/ Z& @6 n3 e' q5 v& g# R7 l./configure --enable-standalone-module --disable-mlogc3 `2 J  Y- `! W: P1 ]+ y3 n
make 6 a) Q0 I0 {) A/ s
三.nginx添加modsecurity模块
4 t% b' o2 c* I6 L9 H5 L
2 H1 e# a) k4 ?. \在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
; O2 W/ R- F5 j9 X: ~  O
* _' }  z# Q- t5 G0 ?4 ]8 N./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
2 _; ?1 V3 k, g9 I$ q' I. Lmake && make install
7 i: U5 f+ e5 P  o, t/ ?& l四.添加规则5 g% {3 h" K; E
0 q+ M9 O$ m# i
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。% [/ C  r( z2 y+ X& T
  a: U" X& e$ u0 ], f, E7 F
1.下载OWASP规则:
8 D7 V: S9 L8 R* y, g; w
8 ?- Z' l5 i5 f$ egit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
* K: r/ p& X4 r+ m/ e; Z7 k" _
mv owasp-modsecurity-crs /opt/tengine/conf/
9 k. R5 F1 a7 M. W# z0 \4 k- c+ H& _
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf. L& g5 d2 v& A# }; B) i- u: `. u
2.启用OWASP规则:2 X& o$ C: o7 _, B+ i9 A

- P7 ]6 W5 s- d0 |' L" u+ R& n复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
" b" g! C" t5 z2 f9 _/ k9 n. P% U, {8 S: J" p! C
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on' E9 j& Y) y" w7 V, W" Z" i5 x

0 n+ @1 p- A: Powasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。6 u+ j2 c; a8 t! z
2 I& f  s8 M0 k+ U5 F
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf. T7 a* w8 }3 e- q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf. o- o. K+ t" B5 F4 N1 H
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf9 D- z4 |! \3 ?8 [1 v% ~+ h
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
; Q- L! V9 i; B- \1 \+ TInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
& L0 m6 A+ ?! K9 ]Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
. Q2 [$ x7 I9 tInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
; e& ^, i/ _# r五.配置nginx9 e; L8 l3 [# m# _1 H' Y/ M

' \/ q8 @# A4 g* r. V在需要启用modsecurity的主机的location下面加入下面两行即可:- Q. c) G. u* ]9 |; |( F( Q( ^

( ]8 l) T' m- bModSecurityEnabled on;  3 J$ h" u6 A1 w- F2 k2 k
ModSecurityConfig modsecurity.conf;' U) m1 R5 v% c) R8 p
下面是两个示例配置,php虚拟主机:
6 V' N1 B4 @/ L4 q$ i$ x
. k6 h- `/ [9 pserver {
8 Q  G: u% W5 C0 L' n; A      listen      80;/ g3 d( x/ M5 i" E# g
      server_name 52os.net www.52os.net;# `" ?0 a& N# p9 v) [
     , W! t2 F1 ^5 T& ~; t
      location ~ \.php$ {
" b# U& t( Q! s! ]! g/ X. s      ModSecurityEnabled on;  1 W8 M1 I# R1 w) A; \, G+ \8 o
      ModSecurityConfig modsecurity.conf;* Y5 e3 U' v2 z2 P
3 R2 [: s+ c8 e! J( p* L' D. e
      root /web/wordpress;
9 Z( x$ z* [8 V5 D      index index.php index.html index.htm;
* A0 U* f& k1 [7 y  ' P0 i" h3 ^( _0 \9 q
      fastcgi_pass   127.0.0.1:9000;
- d' J  ?, N; `6 `9 `4 ~      fastcgi_index  index.php;: f& I2 o- o+ [$ p  A$ f7 c
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;# M. H8 x3 o) G9 ~: L$ E
      include        fastcgi_params;% m# |1 Q8 c+ l) E/ \
      }
7 x" R( N8 F7 }+ g  }8 V) O/ l$ F  }
6 Y7 C" Z: q2 {# X7 dupstream负载均衡:
% v7 c+ w9 S% ^3 F) U1 K2 B# B$ E* S: y6 ]3 `0 B  Z% w
upstream 52os.net {9 J% `; o/ Q1 r$ a) k6 P4 B
    server 192.168.1.100:8080;
/ {+ z5 E, x- y) n8 q' @3 i! g    server 192.168.1.101:8080 backup;) r$ t/ ]8 C" D2 F; H5 f& C
}
2 ?5 p/ d% [  |) v& v8 t' x8 @  \: T5 ]+ s! w9 ^
server {
7 ^9 A5 P  P& Y7 Dlisten 80;
+ F: m, i" [9 x# \) Dserver_name 52os.net www.52os.net;) k- `5 q# r3 s% Z3 V& Q" `9 h1 v0 f

  g) g. o& R3 `  c# glocation / {3 y# s9 e* L" }; r/ `7 b
    ModSecurityEnabled on;  1 M5 t5 j9 W3 l% J9 b' H
    ModSecurityConfig modsecurity.conf;  8 m6 J" w" t- S, a

( |. T( }4 ?: T! z8 |2 N0 H        proxy_pass http://online;
/ q8 l/ y6 h6 q1 c        proxy_redirect         off;. _0 ]% {# B! y: j7 n
        proxy_set_header Host $host;" W: F  B: W& X; |$ j) X& w
        proxy_set_header X-Real-IP $remote_addr;
/ p; P# y0 O: m: b        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
/ n" j- o6 E# `6 a    }
- @. n0 [8 y" a/ V}5 \" f$ d' Q& ]
六.测试
0 ~" o6 H8 E" g$ G9 t& x0 H6 A6 j/ S5 N; v
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
9 X9 a& f$ X8 i5 n4 I5 M. T
$ p/ v$ y9 z5 P4 K<?php5 j  o3 S4 s: V
    phpinfo();    7 H: r. I" f  f! {7 Q( Z
?>" D: U: F; e7 ?8 ]6 a6 l
在浏览器中访问:; r5 g1 m  o+ G' Q

+ g1 N4 p# x2 Y+ g$ Z' d+ M' Shttp://www.52os.net/phpinfo.php?id=1 正常显示。
2 J, @4 c4 g  Z9 |, w4 N) fhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。  {6 r' K* E" _
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。! n0 l. g" R* k! y
说明sql注入和xss已经被过滤了1 i  F$ _2 K- D: I8 L( G
4 j0 \% g# Y7 @) S/ j
七、安装过程中排错- j" v5 _. ^* Z3 q% H/ l6 b* R

. G" d! w3 N0 V3 k6 x1.缺少APXS会报错/ |! w5 }; q' I+ @3 g! B7 ^

# d9 c9 L' c9 x4 n8 G) vconfigure: looking for Apache module support via DSO through APXS
2 J# W- i3 r1 k" x4 i, V/ Qconfigure: error: couldn't find APXS0 q1 T! c& T2 j& e  R4 m4 B9 A+ ^
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。, ^" M( x/ H+ T' s5 W3 S2 h4 t
解决方法:
0 S( |+ m" A2 w* x3 }& E, M% _" r0 n' X
yum install httpd-devel% g2 x# h' d* C; j$ f- a
2.没有pcre
+ O  o8 x) A& P& w  `) Q, j  i! T0 x+ P
configure: *** pcre library not found.
/ C+ A2 |7 w7 S" M% s  |configure: error: pcre library is required+ X! [( f: _2 ?. h
解决方法:
: ]3 \; l7 @; x! z! k: f% O
- v8 v# S0 _3 |! K9 yyum install pcre pcre-devel
, I. f* x6 f% D6 W* j7 l3.没有libxml24 k( C; d8 |' }: F4 L, ^
2 {, g+ E+ |. V; H' h* t
( n! I: q1 U8 I4 x7 E
configure: *** xml library not found.
3 @# X' p2 Z3 |% V3 a: z& B* hconfigure: error: libxml2 is required0 B8 t1 E- i) {" M3 G7 G
解决方法:  l4 c' t7 D  z) [. V

4 x4 E4 s0 M# A5 \yum install  libxml2 libxml2-devel! G" f! {; P( M% x4 I: @
4.执行 /opt/tengine/sbin/nginx -m 时有警告
' }! L& ^: O8 p1 u7 j3 l3 x$ f7 `/ O+ G/ ]$ Z) [
Tengine version: Tengine/2.1.0 (nginx/1.6.2)% [$ Y5 C5 L* m8 k$ K0 O
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!# s9 g4 J( a7 z; O% h1 T9 p
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log' D5 o; b" k: L" Y
/ f1 A  O/ k! u0 N* Q' m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
3 B! x! b+ x) L4 A# U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"( W7 X5 }$ w( w1 m
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
: @$ J6 b9 W. o! m% V2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05", J0 r( i: L) S. @6 v
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"3 a" D3 g( }+ R* C
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.* H9 D' }6 C9 C: E! \% u/ r# Z
解决方法,移除低版本的APR (1.3.9)1 y7 }; v% D5 b% a! d/ Y7 T
- ]- Z0 o2 S9 y2 B5 y
yum remove apr
  O# J3 T: Q1 j5 T; Z5.Error.log中有: Audit log: Failed to lock global mutex# o9 w1 {" {" T9 L' _0 f
, A. d0 \4 r7 p! J# e
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
# D, j, X" k, G! Uglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]  x+ e5 i/ ~' I- W% n- d6 P
解决方法:
* U( |1 D: e5 G6 Z9 f编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:2 B  f/ Y! Y+ f
9 m" r: c: k) J0 F
SecAuditLogDirMode 0777$ X; ]: n) a/ W- V7 F/ U
SecAuditLogFileMode 05501 g8 b' d- o9 u5 S1 F3 E& f
SecAuditLogStorageDir /var/log/modsecurity
! P  g: y- D3 @7 M' }SecAuditLogType Concurrent
) O9 U9 i, h3 t' W  C参考文章:9 N4 d$ n" u1 {3 o6 v0 J
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX# i! V3 F, X1 o. `
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-4-20 11:05 , Processed in 0.657909 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

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