找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13081|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
, ~6 d0 P# I0 S$ ^, p  {! H9 u* j: ]8 k; ]" N- `! ~) G$ }
一.准备工作6 P  w* H9 U2 S+ v

- g' n% x+ p3 i$ n( D$ R系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
4 j' i3 y9 U, ^6 c5 r$ Q% l2 L  _( n, O) I/ k& Q7 G
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
- j% _6 z0 @$ S/ _# v5 v# ]9 |; i& \+ Z3 [
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
& D/ r" n& @+ V' n% _, V( j
! E8 b6 n4 B  v6 a2 O9 gOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
& ~! |- L, m4 h) E
( ^& c. R$ j2 R依赖关系:
7 x# F- D8 P( _4 b4 K  q. ?, Gtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:% p9 N/ T1 t; T- ]: K
2 H' p0 h. M$ N6 p( r! O- y  I0 ^
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
" d: D3 c% Q3 _modsecurty依赖的包:pcre httpd-devel libxml2 apr
; N5 Y5 Z& B. U( O& `; S5 s; M9 ]( t- L' M8 z) \# P
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
/ o. b' B3 [! u7 O6 Y二.启用standalone模块并编译# C1 \0 ]- O- u+ T" V! B

8 b4 B1 n8 ?6 k0 R下载modsecurity for nginx 解压,进入解压后目录执行:
! C5 p" g7 M/ ]# N
% e6 S  [$ E3 l* v' B./autogen.sh/ S* l! o+ J5 o4 j* Y: A2 ]! P
./configure --enable-standalone-module --disable-mlogc
- w: F, h- r. B) G4 \& t5 [: Amake
: ^& r3 j  q3 n三.nginx添加modsecurity模块
! j. S3 K2 X. @/ x+ T5 A) `! M5 Z" t; A2 j
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
& B$ a5 k- ~- A: D- m7 i5 M3 m
1 u& c6 x( P: C7 [./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine# t- [( q' R1 b2 B" V' m8 v
make && make install7 ^9 B  k' `) u7 _
四.添加规则
% b$ ~1 l9 c! f; A4 m1 H- ]# P) D3 D8 ^, H! w
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
& c9 o% V& h% x$ g7 \" R# q7 g2 `8 E9 `" a/ ?% d1 [
1.下载OWASP规则:
3 [; F, P& C# Q5 t( i1 l: z5 }# z/ p/ A9 t  l. A% k
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs  I: W+ T/ ~  I8 T3 \
; t& K) k: D& J! b& e2 B' j
mv owasp-modsecurity-crs /opt/tengine/conf/5 g3 ^$ S2 v, K
2 `5 S. u6 c) x/ G2 H
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf: \( c1 z+ G* [. H+ k
2.启用OWASP规则:
8 K4 V+ R/ [! z- M  q* i
3 G2 U6 n) j# G: c# Q" ]复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。4 I' j- z% ?9 o' _0 O2 U2 b0 m
( p7 s8 q! [. a3 q) g; q2 b
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on# |! H, b5 E* x' ]2 u; B% A$ O

: y9 w& G. G. o' ^! T# \5 `owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。/ Q1 p$ s+ W" [: P1 O$ e* w% ~/ \

8 h$ _; F5 W( I" g+ V3 K1 eInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
( [4 W1 ]! V9 s- g1 sInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf& P0 t5 \4 p. A- v* @% n" X6 c7 {
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
! m1 p) Q2 w) ?Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
! [" m$ s0 k3 ]- h8 _, }- l* CInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
. @% R0 ?! C+ {7 N) ^* @6 GInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf) T3 ?( X3 l! a3 W8 w/ U( C9 u3 p
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
* \6 _: m$ \( C$ Y, |, j) j五.配置nginx
. K9 `5 ~, U3 d6 K) O, |, c/ ~# K& T% ]/ \, |
在需要启用modsecurity的主机的location下面加入下面两行即可:8 O& ]4 i7 F+ z+ D2 j/ b
2 h+ K- }9 z. Y' T+ s+ ~
ModSecurityEnabled on;  : f6 d# v$ [# L# L2 L- v
ModSecurityConfig modsecurity.conf;0 i7 L5 [: {1 U( t2 r
下面是两个示例配置,php虚拟主机:
3 {- E9 B9 _3 b5 x; {% ?
8 F7 w; {! C; \& ~' vserver {" T" H7 l' R7 N! G, {' v* \
      listen      80;
0 i+ E% Z( J2 k& Q: ]      server_name 52os.net www.52os.net;
2 k, X4 r1 i  l8 r. |     
6 w9 V1 T$ Z  o  \! \' c! Z0 J      location ~ \.php$ {; g# j  N. F* x+ V
      ModSecurityEnabled on;  
4 E8 l! n9 a: f3 G# [6 F" U" }$ Q; G      ModSecurityConfig modsecurity.conf;
/ T* B+ g% H; }& H+ @
/ ?8 g1 \" h6 F9 }. C: T4 |      root /web/wordpress;9 |6 g' L' X* }9 y; R
      index index.php index.html index.htm;
; w' Y0 _6 D& g7 c& @# n  
7 g( K# r, H7 r& }- p( H- Y$ t      fastcgi_pass   127.0.0.1:9000;
1 e+ |+ n0 r2 }" b: H  ]6 ^      fastcgi_index  index.php;/ b5 @. j9 `# j6 a3 ^
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
# e% W; J0 s0 J& ]5 G: o      include        fastcgi_params;
  P% T, l' h4 v8 l- Y1 {8 b      }
9 c# S3 a5 B! E  }
7 `9 [' C+ x1 V! oupstream负载均衡:
4 T. x* @6 S5 S- f0 e( h! d9 P# J, k# m. |& A' v# q
upstream 52os.net {
% E8 \. e+ j6 ]2 m% U" n    server 192.168.1.100:8080;8 D1 ~' W. p4 X! s5 D; n- U" [9 Z
    server 192.168.1.101:8080 backup;2 h1 N- k' f1 C5 n- V+ ^$ B4 L
}
+ b9 W, l; R6 M0 ~: |" E! q- J2 M
1 p: Z( {+ V4 o$ y6 G$ P2 `server {
) Q# E0 Q8 N# r( o2 I2 L1 N* blisten 80;0 v0 x9 J) W. W. u- i, @, J
server_name 52os.net www.52os.net;+ u. u& w& D9 w+ G

4 D; @! Q& p. mlocation / {
. p$ T' T- u- S# ^# u    ModSecurityEnabled on;  9 t. E4 h2 T0 r
    ModSecurityConfig modsecurity.conf;  2 Z# L. E9 {# K- i3 K' _$ w
: C7 E0 O' h# n2 C
        proxy_pass http://online;3 t% a' Z" H5 N! m9 \: w  e9 L* B! c
        proxy_redirect         off;# y8 T% n7 u, a1 J
        proxy_set_header Host $host;
6 W1 q! e$ j+ o7 G7 f3 u9 x        proxy_set_header X-Real-IP $remote_addr;, p2 R! K" c; \1 Q% R
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
' R. \: K1 G* Q    }
0 P- a5 r+ j; l% o" q}3 p; s) }1 O3 a
六.测试4 T7 p8 s+ [6 u

6 g% `: c/ W5 N我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:  \  g) W# c; V% b3 @* d

! x( }: G/ c9 ]5 `7 q<?php
6 }  t% b8 S- ^    phpinfo();    ) U" z! ?2 P% O: a# s4 z
?>$ d) J- L6 s- P% k: l/ S, h6 p3 l/ y* y
在浏览器中访问:
0 O: b! w2 v8 T% x8 t
/ |  E8 D3 U: O/ g: d5 w% m7 [" Chttp://www.52os.net/phpinfo.php?id=1 正常显示。
0 `2 @: U  _9 Z9 g8 [( B0 _http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
: S& l( f- V& H/ @/ D9 g$ Nhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
  Q# ^, b3 }( M- t4 F说明sql注入和xss已经被过滤了. E8 `, O# ?' [0 j2 n; x
/ e# n' q% O) K& B
七、安装过程中排错+ _# X+ E) X3 a- ~* ?- C+ N

, j( t! W$ j8 f8 u+ ^( r1.缺少APXS会报错, `6 g! {2 W- U
) B4 Q+ X0 e8 p4 y+ y
configure: looking for Apache module support via DSO through APXS
* m2 n* _5 N4 J/ _0 cconfigure: error: couldn't find APXS$ p8 C# [* o; [- B! z- g
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
7 r( D$ |0 {. S, G" j8 s解决方法:/ {/ S; K$ x) A* f
* y7 r1 B5 N' X& s/ C
yum install httpd-devel
! |, v8 z% l$ O5 K6 u, P' R- K2.没有pcre
$ U, `7 y' V# Y7 V+ |9 o% O6 Z, e/ ~1 _# B: v3 S* n4 b  ^
configure: *** pcre library not found.
" \: A9 L/ y8 h% |configure: error: pcre library is required
$ A4 r7 X3 i: [解决方法:
' x/ y/ ]7 [! C1 f% F/ H% A% t% S# T6 ?. m, F9 s1 S
yum install pcre pcre-devel5 G  z2 S6 `/ E) T/ J1 O1 N
3.没有libxml2( k) r$ o1 j' V  v! u
1 r  |/ p6 v6 \8 l( c

- R' g/ R3 s$ P% S% V' Kconfigure: *** xml library not found.$ Q- n, u5 H: F8 g6 e( B& I) o% P+ y& N
configure: error: libxml2 is required
- |- z  ~! I7 k/ _- g解决方法:8 c9 O0 S) O" I9 X3 R  i# F

# c  g: M6 g  e4 byum install  libxml2 libxml2-devel
& L/ J& X. N8 b5 Z4 q# b- r' [4.执行 /opt/tengine/sbin/nginx -m 时有警告
8 G! @: [" b' ~! W  m5 K' e) e0 Q% o3 T, b
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
- g) f5 ~5 X' P. L7 P# A$ G# N( Ynginx: [warn] ModSecurity: Loaded APR do not match with compiled!
, [6 u1 p5 {1 c( W: n& o! x( o0 v原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log% N. ^; z" s+ Q$ n0 `
; X' R3 G! w  X% n+ l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
/ r! A1 O: S- M2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"/ k$ A) d- Y7 {1 M8 T8 W
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!) ~. I+ B/ H' S% b
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
: |% r: J& {; z# a/ ?; F: }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"9 j" w- f6 W# n* V' Z! j. Y2 K
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.  b& B2 ~- s# f/ }
解决方法,移除低版本的APR (1.3.9)6 u7 o# S/ r0 Q/ I
" x$ v4 p0 k5 Y, C8 j
yum remove apr1 h2 r& F* w# Z. w5 c
5.Error.log中有: Audit log: Failed to lock global mutex
* J, m9 e0 A% K' u  S2 Y; O$ ^4 Q5 B0 a; a' I1 b. j
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
1 d! `+ C' O, T3 p3 ]3 X: O2 c, Mglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"], m# h! @  Q3 ?; x2 w
解决方法:
4 D+ `* ]  o- o: Y1 y, P3 E编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
/ p9 J" X5 z; B/ \: x# R7 l
2 I) N4 D6 a* i" hSecAuditLogDirMode 0777) z6 W3 _/ Z( {) Q; [8 V: Y
SecAuditLogFileMode 0550) A5 t6 P2 j' G( J% l/ I% R
SecAuditLogStorageDir /var/log/modsecurity, Y1 s3 @2 \9 f% x& R
SecAuditLogType Concurrent
6 \$ ^. p( V2 |. D1 q参考文章:
6 f" a. H$ N9 |  r& g+ bhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
9 e% F0 N( C) h7 vhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-12 04:28 , Processed in 0.070093 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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