找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11798|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。+ k& o7 j0 s/ z. U2 L5 b2 _
5 u; P' n8 c) e- H1 o2 O/ I
一.准备工作' c0 e/ d( L1 ~# N( y: `: m! C
! o  E" x- I7 z$ ^7 X4 U6 W
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0( \# Y* [2 f0 K/ _+ k' X: }
4 V1 V$ h2 R- j6 Q/ y! x8 B- s6 o
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
: w# ^9 l$ f" n- `& ^4 @/ K8 Y; z  n. I  K9 b0 l3 u
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz  M3 [' h: Q7 N
% r  Q3 R: O* q% W
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
" a( \5 o3 b! Y7 r& w" ]+ a7 u) l& z& S1 F
依赖关系:
: V" B, w/ O4 Ktengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
- w( a5 s8 B! J6 d: i7 D6 L  v' R& R. V9 E0 i2 v7 _' o
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel0 P0 P' ?" }' ~  G- J% r4 d: f
modsecurty依赖的包:pcre httpd-devel libxml2 apr( B* z  M' u3 D9 a: ?+ I# E. ~
/ R7 e. c- t  [
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
; P0 h+ p0 ?9 q% y( c7 P5 k二.启用standalone模块并编译! P/ X7 h& W, V2 b4 J7 s2 i5 G+ x

7 y8 B6 p/ M9 m" m下载modsecurity for nginx 解压,进入解压后目录执行:
/ h, V6 S' R7 u) `
7 @& K% f$ c+ Z+ u, O- Y0 L./autogen.sh
7 Y- Q" V% i, n: p/ N./configure --enable-standalone-module --disable-mlogc$ |" X: p& x( i( V% t8 _! Q
make 2 Z  i& q1 y& ?, ?
三.nginx添加modsecurity模块
( E; R) I. c/ L* I! o5 N9 v* A
9 [# E. C0 t( ?* z; z% T$ I/ E在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
7 S8 t1 P. G2 f: P4 Z3 o7 M& P9 h6 U* H6 n
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine. C3 M% k4 H( o& F) U, R5 r. _) Q4 G
make && make install
3 J$ v: d% n# d& D! q2 B1 c四.添加规则/ i: c2 K' [0 M9 H

: E# B% y2 X, V3 A$ Umodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。. t; G# A) V7 T: O

% u# M7 \0 f; m3 p- u1.下载OWASP规则:6 |$ P! @. E4 L3 S  r
- d: P* a6 Q1 y: ?, \8 M
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
9 `3 G7 W, [4 T( W$ y. P" Q8 O1 X! I; J, p
mv owasp-modsecurity-crs /opt/tengine/conf/9 L: C& |4 }* v# W6 }- R, V' i9 a

9 P  n" a# l. M* Ucd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
0 ]7 v3 t/ b9 U5 }( m2.启用OWASP规则:* @; B9 U2 ?5 C# Y  E* i
2 O+ m$ W+ E7 j
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。" }3 H  H4 @  p1 P
  d. D7 J! i" l' b; `, U1 x( f
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
% Q( ], ], T2 l+ z1 B3 |$ b7 Q& x) t; ~) L9 S6 K6 A' c
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。6 S0 }& h$ b' B: Z: \' `

* K/ N! W8 Z! R2 k; m+ DInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf& A+ p3 l8 n+ W5 @, Y' q+ [& T+ D
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
; u! T5 X& ]+ @/ fInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
* N( S1 x2 Y$ u: X; L. w- a9 T3 g& o0 }  UInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
2 P- Y9 Q- c) HInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
/ Y* r6 A2 g' RInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
4 z9 Q8 y0 ~2 Q, UInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf2 Q% W/ a, c& f5 e
五.配置nginx1 ?. r6 ?$ K& \) @
1 M* n' U: t0 k. ?' I- r. d
在需要启用modsecurity的主机的location下面加入下面两行即可:
" m' v% P; m! G7 Y, U& d
- K) p) x; S( G. t0 bModSecurityEnabled on;  
* Y$ f9 _6 J$ R3 e: P% A( v8 P. LModSecurityConfig modsecurity.conf;2 ~. c; M! ~* u7 o
下面是两个示例配置,php虚拟主机:4 f- h0 u$ V$ T0 E" e
; \& y7 F1 w8 T1 k, |
server {& X) }# B( J" U' H
      listen      80;+ h6 S; b4 ^+ _: s: Y; ~
      server_name 52os.net www.52os.net;# w8 g$ N. t2 g6 m' \* J( X5 S
     
2 S& O$ w6 n( l8 ^" R6 Y+ ?+ J      location ~ \.php$ {" R0 Q% b+ P& y- u- J2 [
      ModSecurityEnabled on;  
: T) y& f, Z) }# X* R3 x( @' k      ModSecurityConfig modsecurity.conf;; Y- h) \# ?" _" [1 s
! n2 q: V) J# v: w
      root /web/wordpress;8 ^8 h2 @$ t3 X- |  y6 h( B4 M
      index index.php index.html index.htm;3 `# i' }# `! f. h+ R9 @" G
  $ J2 K  v5 m6 v
      fastcgi_pass   127.0.0.1:9000;
+ W$ G1 T& s! [( }9 C, Z+ \0 }      fastcgi_index  index.php;
# ?7 ^- G; A: Q- L      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
& W9 X8 D: @5 U# V      include        fastcgi_params;
6 r, i4 j2 C5 f6 O& R  [, |      }
7 g% R  N5 O7 j2 p4 O& C' a, T" g  }
5 a. c4 w; l) p" n& q- Kupstream负载均衡:3 N* u# `9 c: t2 h9 ^/ ~/ U
7 z( e( t3 n' O  s6 v3 b- [, |
upstream 52os.net {; ]2 i! E7 L& ]& l, w
    server 192.168.1.100:8080;
3 Y: h* }7 \, e$ N    server 192.168.1.101:8080 backup;; E- c6 f2 k* I! m1 D
}+ X7 a+ z$ \9 D2 O0 \

" r2 x- |3 E- h3 J* V+ e$ J% iserver {, F3 C& C2 p5 m7 l
listen 80;9 O3 `; Y9 K+ @  a4 F
server_name 52os.net www.52os.net;
# [4 J& t2 l& ]: Q" M  Y/ A' X0 ~4 I* ^5 h# X
location / {
, T. ?6 O' u) W/ _/ K  Z# q/ m5 z2 e    ModSecurityEnabled on;  ) T: {$ n/ j; a4 M! s
    ModSecurityConfig modsecurity.conf;  8 U/ K0 r1 O" @" ~2 t

7 N) G: M. H$ Q( x9 ^        proxy_pass http://online;
2 J+ C; \" o! K; P3 [% Q( K: N+ U! a        proxy_redirect         off;! {8 ~6 r0 }' G$ D! d: X" U' I- ^- n
        proxy_set_header Host $host;- v* [; Z% P! _5 V) I6 j
        proxy_set_header X-Real-IP $remote_addr;
& W5 H% ^" I! r8 O9 V+ r        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;! _6 v, b) I' H, t' Y/ q  o
    }
. @, j6 S% b4 u2 R2 E}% e1 j6 w8 F. k. Z
六.测试# U' \" U* M0 J

+ ^: c  m4 [3 l5 I2 f; I; `我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:4 V" K* f- c% b. ?. @; y* Z8 C

: B/ w2 u" v; {2 Q. T9 F+ k% N3 _: q<?php
* w# [: a& L7 s    phpinfo();   
) b$ ?; X1 i; f! y?>
! u! T& _6 a! P6 [( ~6 q在浏览器中访问:& S, N9 Y+ R) \1 _3 L

1 S1 i" \5 i5 u# ~! whttp://www.52os.net/phpinfo.php?id=1 正常显示。4 R% u" \+ w; p  Z+ N# s4 c3 _& d3 h
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。6 P' ^/ L7 `' m' L
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。' T; \  [# l9 y5 y( Y$ f7 [
说明sql注入和xss已经被过滤了
' K) A! ]; t' A7 c1 e  C% L- q7 J3 j; k+ G
七、安装过程中排错
: i5 N9 G1 {8 J  Y" I+ ?/ J: E% @+ ]6 y. C9 T
1.缺少APXS会报错
# x2 N  g3 Z/ j& l" f; d& z# `% Y7 R% d1 s* b
configure: looking for Apache module support via DSO through APXS
( D2 j5 I3 Q4 H7 B, }. n( pconfigure: error: couldn't find APXS) Z( d) e6 j! E
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
! y, n( n: g" Q( }0 f: |: Z) M, r解决方法:5 o3 ]/ r  \( |/ p
' Z# }2 a0 l, Z; |/ K$ U0 j
yum install httpd-devel
9 ?. G# b- D/ W  @* ^4 ]2.没有pcre
6 @! _# i/ E; C1 i" X6 S1 i
  g( Q) g/ g4 {+ A* j5 `5 ~configure: *** pcre library not found." `. @7 A" X) n7 }& Q/ o; m+ q
configure: error: pcre library is required7 B* s5 j8 A, T& S& |3 I9 v  p0 b' p
解决方法:% ]9 c! P4 G5 n! {+ n2 E% n
7 D0 g3 g+ w$ x& x; o0 e
yum install pcre pcre-devel3 b0 H9 Z. A+ H: x' A
3.没有libxml2
" R0 t, S: L' N( b$ }* D- B
1 W- r( x% c, n" [) P# n- Y
# P* p, e* n7 Y" d& s; oconfigure: *** xml library not found.
5 {4 o4 B% F( z2 d! E) c+ w+ f7 H+ oconfigure: error: libxml2 is required
; a: M6 ?! `4 l2 |) ]" b解决方法:
8 i, `: R8 ?( r& r( c5 q: a! _7 n
) f3 p$ M4 o! D$ e4 S8 ryum install  libxml2 libxml2-devel
; m$ A$ [4 T# J, l; R2 X4.执行 /opt/tengine/sbin/nginx -m 时有警告
, S) [3 b% G4 T! K) P% F' C1 N. F- O* F! c
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
, d6 k; R3 D, ?' unginx: [warn] ModSecurity: Loaded APR do not match with compiled!
: x0 r. `5 e+ m5 y1 X& @( J; P/ \原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
* M$ F- \4 ?; w9 u- v& i: m* ~9 P2 \: P' Y. I5 Y9 ~# I9 J
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
: ]7 V. f& k) s: f6 v3 y5 \2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"3 _+ ], s5 |, b; ^8 i, N* [
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!  M1 [  \: _, r. ^+ _
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05": w( P; t, ]- R1 l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
! z* z; k2 q' P# L2 m2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.' k( N) C% P3 j) D9 F
解决方法,移除低版本的APR (1.3.9); o- H3 k0 ^4 L
% ^% p1 y) ]2 \% p3 N& _' V
yum remove apr
) c- G( E8 O& [: h8 A. L5.Error.log中有: Audit log: Failed to lock global mutex. j" G; F4 x( R. Z3 U
3 r2 m$ B; g1 }9 [) {
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     2 O7 F1 `+ O3 S7 d6 [4 e
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
  R/ q  I9 F% K' z7 O解决方法:& U0 r& a9 g  l$ s6 [
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:0 |! F8 @1 r5 P

6 e, e" ~: }1 Z" ?SecAuditLogDirMode 0777% A  A& u( v' R$ \* r7 `4 x" h( m
SecAuditLogFileMode 0550: h5 |3 z6 M" j! O# q
SecAuditLogStorageDir /var/log/modsecurity( o' |; D3 E  [& i8 L7 V) i  b* s
SecAuditLogType Concurrent! Q8 N% `; {& q
参考文章:
4 I4 V7 r$ M: T2 P* Chttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX( P- A/ H% R' ~8 R$ u; [1 l" D9 O- M
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-14 07:06 , Processed in 0.075673 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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