找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12841|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
  B5 n8 X$ ~+ S7 R
5 t# W: H% D: w; e4 a/ z$ p; n7 s7 s一.准备工作1 @2 D+ \) X- c8 {, }6 d

) k+ F4 C" ]/ r4 `/ X' l' t$ h& ]系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
3 v$ N; @2 _0 q$ z0 J+ u; X( a: n: c
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
! K5 f8 O: @' J$ @6 D3 i6 K0 D! U3 r9 f, g& {
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
6 X- x. {8 J! N! D- _1 _" I1 l" L! Q# H  R
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs. U. F: ?& V) D
" ~2 Q9 r. H& N# E  h2 L
依赖关系:4 q+ U- u3 v+ v4 o6 R
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
- }9 H' z* p' i) l( u" X- I0 F2 E  D1 c- B  w8 v4 m* R
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel+ }% D4 s9 d4 M$ ^3 g0 L- N& s& t
modsecurty依赖的包:pcre httpd-devel libxml2 apr( U; P  B0 v7 a% m- l7 c2 _$ L, B" c
% T2 j# b, v5 \! E" C
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
4 k$ P! F2 C. l' D1 D二.启用standalone模块并编译
% q# r, T; C. d7 I: d8 b; `( @8 S
下载modsecurity for nginx 解压,进入解压后目录执行:
: Y* w9 j- ]+ z
# ?! T8 j/ h& Q8 ~- r) H7 @./autogen.sh! ?# l: d/ M- E+ B3 `, Q# [
./configure --enable-standalone-module --disable-mlogc
' v" ]8 n& i1 _: Tmake ; w4 H+ n2 ]% ]+ I3 ^8 B
三.nginx添加modsecurity模块3 h1 r- v9 v4 ^5 X- A) s: @

0 u4 U5 q+ ]7 `  K. l: b. D' X在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:& c  s# X% {- i, W' ~4 h5 [. h1 S# |
7 u) a/ y* `. ]: m! {
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
* K0 K, Z! a- [* P3 \" t6 N2 ^make && make install
- D9 a/ \" \1 U+ L6 c四.添加规则0 k% b# w% v. y+ h; f
7 O, Y% G4 z! A" w. ~+ a6 y
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
- e3 H+ f8 a1 M% B. m9 d7 S0 p$ `9 I% R- h, |  N- v
1.下载OWASP规则:; ?7 t) O2 o! o* a& h+ Y# h1 l: L
" ~( L" z* Y9 h* U/ R! j2 @
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
. E* t8 N5 R9 O) ^: o  Q, h. z4 e/ g
mv owasp-modsecurity-crs /opt/tengine/conf/+ R6 L3 @; ^9 Y& U  A( Q* B

# \* M! |* u1 m7 F2 }# b6 M- m: ycd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf5 Y" W; Y$ r& C" @
2.启用OWASP规则:
' S4 D4 @7 j# k: Q
/ j2 l' i8 m5 i( M* h) H5 V复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。8 @8 ], {3 q# o8 m. \  \8 J
" s# a& `0 d1 R6 v  e
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on' _4 z6 J$ [3 C& l) U

' a/ d# @7 i; }% T, ~owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。; _, ~* n& u6 ^0 t" p  L- d0 x. H

, M& G8 }7 o* |0 H. p7 Y" rInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
% f5 L# a. g% w0 o3 P' X$ PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf0 k% k  W9 M' f
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf3 f3 e3 P6 Z7 Z. [& j  e3 E$ J
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
, E; G1 i7 Q! WInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
3 P9 x! q$ L& n7 O/ YInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
' H/ I. {1 w1 }/ VInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
: e6 W+ v/ j8 e+ k6 U6 Y4 R五.配置nginx
+ A1 H4 b4 `: x4 A7 ]- C
" V8 {) a2 A# ^3 a/ e在需要启用modsecurity的主机的location下面加入下面两行即可:
2 S: F; J8 p8 j. `2 k* D' ?
- d& h8 x- u* c0 jModSecurityEnabled on;  
( U5 y9 s: U6 H+ d- ^+ |* KModSecurityConfig modsecurity.conf;
6 ^$ F/ L0 r0 L9 Q下面是两个示例配置,php虚拟主机:
& \- _3 {# S6 I8 D: v
3 Z) d4 W: g2 c  Dserver {  G9 E0 P4 b. {( S0 d8 q7 u
      listen      80;
4 j5 I0 i+ K" K+ H      server_name 52os.net www.52os.net;
: r5 Q" m6 S. ~     7 R* f" x: M; a; o& I  S
      location ~ \.php$ {
( G7 l& X, f6 o8 L7 R2 E. Y+ B      ModSecurityEnabled on;  # U% R8 }& n4 S, E6 y
      ModSecurityConfig modsecurity.conf;8 K, h" n6 _# O
& X9 G/ V7 F( H- z# f+ c
      root /web/wordpress;
/ ^6 q/ W$ q% H( W      index index.php index.html index.htm;
' D; {  o8 M' A" W  
8 s: i% G  G' M. A* Z      fastcgi_pass   127.0.0.1:9000;( [8 |7 \7 C% y' o6 Y7 |
      fastcgi_index  index.php;8 ]! f- t: @6 J- b! Y
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;- I! y% j- W, ~' u
      include        fastcgi_params;$ F% Q- }& _' O$ B. u1 x! l
      }
* k* [) z" Z) e  }% `5 `3 l# m7 I
upstream负载均衡:6 \: T/ a7 \; x6 _

0 f. o/ F/ \% k- A9 Nupstream 52os.net {
; ^, X" K* F* r$ d3 F- z/ k    server 192.168.1.100:8080;
1 [, Y; h% {& V6 l, x) o    server 192.168.1.101:8080 backup;
+ c4 b: Y" q7 j. a}  m) @' Z: I7 g. ?9 `9 l
6 J1 e4 C# Q! d1 B1 H9 G5 \
server {
$ F% r$ h6 g( P) ^listen 80;7 i- k; b2 l+ i
server_name 52os.net www.52os.net;
& {% F% I- G* q, P1 D( v5 S- ]: G  a6 ^
location / {; @' s0 n" {4 B2 H' V: f  K
    ModSecurityEnabled on;  9 Q  n( @2 O& h
    ModSecurityConfig modsecurity.conf;  
/ ^( T2 B) ^* r4 u2 h- O3 N  R; C7 D* b* j
        proxy_pass http://online;
" `( f+ ^! C7 \) |/ M: U! O: Q* k        proxy_redirect         off;
/ M7 {3 f1 Z+ }1 a/ ]8 f        proxy_set_header Host $host;7 C; Q2 L& v* Y8 P. t/ \
        proxy_set_header X-Real-IP $remote_addr;: `1 h4 J) p& M& k" O% D
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;( E) ]" R: k' B9 \, P
    }
3 u: u8 ]* ]# P/ z" R' s. y}
6 u' i' W3 H! Y: \8 q六.测试( Z, f  |! S0 r
* F9 P( r3 G1 d# W& d
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
- g" `* \$ q! D/ x" d7 G1 u2 H8 X
( S7 Y# [' y6 K3 ?<?php
4 C2 a/ v8 p, L5 K$ }) Y: I    phpinfo();    + e% T* Y, ?7 s
?>
: T+ g0 Y+ k2 M3 G& V在浏览器中访问:, w/ b# y8 p* L3 x

5 R! v7 w- {. q* y" z3 E9 d" I1 lhttp://www.52os.net/phpinfo.php?id=1 正常显示。
. m, w4 d5 [5 W5 c9 p8 Fhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。- Q- _8 B5 E# i
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。& |- L- g9 n7 t3 S3 b
说明sql注入和xss已经被过滤了
4 N* F% [1 M# @3 Q( E' ?6 ?/ g' i" @3 d# D2 |$ s1 `, o
七、安装过程中排错7 a( ~" _( y. x+ N

' l$ S6 q1 W; }1.缺少APXS会报错1 N' Z% `+ X/ x! G+ u

5 @; N4 x0 `. a( Z: Xconfigure: looking for Apache module support via DSO through APXS
8 L  T# n2 |* E0 k7 ]# i- Hconfigure: error: couldn't find APXS
& D8 U/ F$ M. i! e0 z; bapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
+ `/ t! n9 Y7 H  F  L2 ?* B8 z解决方法:2 j3 m( V$ u6 [' }. v+ L
& d) [; S5 y) B0 r2 I, Z
yum install httpd-devel3 N( l2 g+ g# o. u+ X/ k' Q
2.没有pcre
7 K. J) ?) n' D+ E# m1 O# n% W# y$ y$ c- S5 t; W
configure: *** pcre library not found.! o, T. Y+ P4 z6 y! S- Z6 o
configure: error: pcre library is required
1 M( s& }* F: [7 J' `解决方法:
' w0 E1 l7 R( |" y7 j2 u0 m7 S3 I& p- V) d) G& p
yum install pcre pcre-devel
; B# ~1 t+ q! U7 I2 |+ K$ A3.没有libxml2. F/ _. \+ S1 f# w. \

  R( y5 ~* M! O- q( t, i4 s
  ?1 o$ Y3 k+ ?configure: *** xml library not found.
7 O" z/ h4 E1 H. Vconfigure: error: libxml2 is required
+ A  O2 Z* {8 h) k  a! M解决方法:. v# N- M6 t! h  |

- S6 E2 Q' }7 g3 G( |yum install  libxml2 libxml2-devel
( j' t  F, j! d8 s, ^4.执行 /opt/tengine/sbin/nginx -m 时有警告
- k$ v1 q6 N+ h3 e4 G$ h3 {1 s
- c9 A6 M  S% |* iTengine version: Tengine/2.1.0 (nginx/1.6.2)/ q! f' O9 a/ N! x/ O* N  w4 w
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!& K+ u& M9 @$ t! D$ r. H
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
. K9 _; p3 H, A7 t5 Y, M+ a" J) E7 }" j. E
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured., E+ p$ N6 @! @$ k3 u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
6 a% ^- P3 H9 `$ q8 w2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!% E6 p! v$ q2 R  r4 e
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"1 \/ E: N7 n& V; M. j. W+ D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"% {9 k2 @% }5 y5 }! F( z) F: d
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
+ K% x. ]3 ^: @( F0 _) [7 `解决方法,移除低版本的APR (1.3.9); M8 e. D- ^. _& r9 }

# p, [" v  ?3 k" _4 d( Qyum remove apr2 D1 _; {% g( j# y! t: |9 G
5.Error.log中有: Audit log: Failed to lock global mutex3 M1 d' m2 Z1 q) Z1 I1 x

' F1 `+ e  @+ Z; e2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     - B  a" Z% P. N! ~/ q- c
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
2 P7 z4 R+ r5 n% {  u! t解决方法:
9 `3 M$ l% U  O. Z编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:; W$ y2 S: c* k3 |

' u9 n, n& R# a7 w; KSecAuditLogDirMode 0777
8 m" \- @$ U" w0 y7 M8 A/ ^SecAuditLogFileMode 0550' B4 |) S: N9 v& k
SecAuditLogStorageDir /var/log/modsecurity- z0 T! f) R9 H" a
SecAuditLogType Concurrent# R* D- a) P" Z9 i  B
参考文章:$ C6 I- j4 u7 ]  K0 x7 A
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
/ A! c2 k2 S: C$ t) k3 W$ l0 Bhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-17 18:20 , Processed in 0.071469 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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