找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12481|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
9 M2 o; k5 A4 [& c; r+ m! {
1 H. G! n( q8 \4 }! `6 N一.准备工作
: A( B' H. c. T
. f; Q: x. l. B) X2 }" t系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0. K  o/ E9 H, q! l" Z0 g. l
6 t/ z' j0 m! s) M6 d4 E5 ^
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz* A) M5 l0 ]' n4 y3 s  `. e
2 U# `" d# |6 X" f/ C5 r9 a- k5 Y
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
" G2 o5 {1 F. n2 F1 e# U
- L* Q+ p' T5 S2 VOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs) e- \6 h- a$ K. ^; o; m

/ W" r7 P0 g. o6 I" J; J: [; o依赖关系:$ u' z5 J" Z- V9 }) M# X
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:! [+ f3 U. n) f$ i+ K
8 E3 [6 P: m# {5 ~2 U; V8 m
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
$ Q7 D; S, d8 S: @modsecurty依赖的包:pcre httpd-devel libxml2 apr5 b& ~, `9 n) x
* j$ r9 d- w: U3 A1 ^
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
* t) F/ |: P2 y+ [( s二.启用standalone模块并编译% c' {; q; I' ]5 m, Z
. I( W9 C. r( |
下载modsecurity for nginx 解压,进入解压后目录执行:
- D7 h6 b1 I7 G& N1 |+ `  Y
9 A( {5 ^1 j, G5 C) l./autogen.sh
0 E$ b9 o: \% J1 T7 n5 X./configure --enable-standalone-module --disable-mlogc9 B5 V% @% j3 X
make ( }) ~% L2 M% A" C+ T
三.nginx添加modsecurity模块
  U! j" P- _& V' `' ~
, L/ E; X0 s, q. q在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
# Q$ \/ q- a4 I* a; V3 z3 ]
3 _  ]) p# d& @& w0 [./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine, i2 e$ u# ~$ c' t1 D, G
make && make install+ L: K; h4 t- T4 |6 c% v% X' v
四.添加规则
, E9 a! F3 o, A4 q
. t& J* A1 X- r' ]modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。" w( ]* M" z: r

6 @' n, `/ T, g/ F8 s1.下载OWASP规则:
2 Q( ^5 I5 z7 U, d% `: D# ^0 o5 ]6 e
$ a4 g+ J9 l5 O( b0 lgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs9 |: v! {8 o. E* A( p- u. E
% S) c* y8 f' b
mv owasp-modsecurity-crs /opt/tengine/conf/
! P. L) x, G  [6 X6 w6 ^0 \% Z% x- @* T. X
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
$ }* [& \" F4 s+ i- m% M  H8 b( i1 y2.启用OWASP规则:2 L% i- m) I& D
* d) @5 Y+ a6 K% _6 v* t
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
" N* q# ?" B! B9 j! ]2 S, q" K3 I7 j6 K8 r. B! j  D
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on: ^2 L. _8 V3 v

9 U: u: O6 _, aowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
* L: ?+ w9 N) Y5 H: t$ c1 l
8 b9 T/ e' H5 nInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf" |# W+ J9 W) p2 k# x
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
! i. [* V4 G/ @* I4 k+ F( i; X9 wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
* P! C3 _4 D; b5 W" G7 \Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf7 [& [0 v2 m* v4 G1 p
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf& R# G! C, \0 b7 W2 L
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf( }+ u/ A, I) \- {4 a+ K. D: ]
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
; U* B7 T  x9 N6 g$ c" b五.配置nginx+ O. B  F- i# I
; j! c/ w* r. C9 }& U6 }3 {
在需要启用modsecurity的主机的location下面加入下面两行即可:
: i9 N! T' O8 o  d" J, E: P; w* P+ ~) |1 ~
ModSecurityEnabled on;  4 w; Q1 F9 I" M0 j
ModSecurityConfig modsecurity.conf;( @- O4 g, X) V1 Y/ i1 E% U5 g" A" m
下面是两个示例配置,php虚拟主机:
+ n1 w& Q) ]  e; l
+ }+ `5 k5 M& [# Pserver {) C- E  e, U2 i; R& H6 j7 L- F
      listen      80;2 ?- w, U% u& M1 U2 b
      server_name 52os.net www.52os.net;+ ?. [8 H* q9 e! J6 L  w2 C
     9 n; d  L% p& U* C" G/ }9 e, p* {
      location ~ \.php$ {
: \, t! K2 V0 x; U% n. A& u" W      ModSecurityEnabled on;  / E6 }( n+ F" J2 D3 C
      ModSecurityConfig modsecurity.conf;) X+ J0 I0 B% m( ?+ g6 p& l
/ `3 y; i1 S8 h! b) [/ c! U
      root /web/wordpress;
. ^3 @3 R+ d2 V+ H. A) \" J      index index.php index.html index.htm;
% y, }# u8 s+ N  U- n# j( t  & A- @, m3 B3 [" M8 |
      fastcgi_pass   127.0.0.1:9000;
# M) c3 h# V( t/ {0 x! w      fastcgi_index  index.php;" u3 m2 u, {/ x3 _/ g
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
+ s8 x0 u2 J: I0 d8 O      include        fastcgi_params;
: J, C" ]2 p2 ?0 o9 w      }
6 r7 w+ ^0 A) \/ B- e- H5 e  }5 f$ k1 p+ F9 u2 ?1 M' Z
upstream负载均衡:
- W3 \2 X8 T$ ^
9 S+ V% P4 U8 a0 p) i' z& Dupstream 52os.net {
$ M$ v( S3 ~( q& G  _+ |# W3 X7 h    server 192.168.1.100:8080;
, u9 B9 `* [6 P9 b. y  F    server 192.168.1.101:8080 backup;% N, i0 D# I/ w  t. |/ K8 b! K
}" e/ e# w4 _* ?& m  W% Z% ]

* D, u# b; I9 ]' p; Q; S, j. hserver {
" {$ v: \5 J% ^2 Wlisten 80;
7 w0 a' L& o( H' oserver_name 52os.net www.52os.net;) ^. A# L( D. T

* N. u* Y; `$ ?6 ]% [) llocation / {
  Q, O9 d. K+ Q: a0 P( R    ModSecurityEnabled on;  
* V0 f6 Y8 x, j* W    ModSecurityConfig modsecurity.conf;  9 }* C5 H$ N4 o; R: C

7 v! ]9 w: \9 A        proxy_pass http://online;
, Q8 u. w$ n0 {0 N/ d        proxy_redirect         off;3 g5 J  u( F( t% h, B
        proxy_set_header Host $host;* z- e: @  h# J1 U. z; ]0 L
        proxy_set_header X-Real-IP $remote_addr;
& q4 {. p# A5 X% k# n# ~# |        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;4 }5 Z8 e# A, X8 X" F3 G7 M) O
    }' p! e6 s$ m+ x8 ^* s
}1 e' J& S% X9 U9 l8 t
六.测试
/ r1 C- V7 v3 m+ _
2 v, {; o) X1 Z6 {. g% e我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
3 \  g$ u. m/ E) p9 {  u2 S7 s
1 ~1 w2 T6 k% }  f5 E<?php3 ]0 h. r- o5 C5 Y) t' N' N8 T
    phpinfo();   
) @& S. x  m& G* p; D" Z?>8 ?7 ^" b4 W) f
在浏览器中访问:6 u& ?/ E- H/ \1 D
7 {) d8 ^1 V4 X  j+ Z0 \, u' N4 H
http://www.52os.net/phpinfo.php?id=1 正常显示。
. [/ B. W9 E$ u: G! g  Ehttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。. R8 L% `/ _# {7 R+ F
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
* P" l4 C1 q4 Y/ y; n1 R( h3 F" U3 v( Y说明sql注入和xss已经被过滤了
' j4 k& e- g) U# u. i% ?+ H9 `$ s+ J
9 V/ @+ F5 [0 A七、安装过程中排错
' A3 ^* b' [' Z6 f1 F
7 V5 L" g, ~3 c# \! l1.缺少APXS会报错  Q; \: l9 C! r' l

! e& D: w- ~! u& b9 \# i' fconfigure: looking for Apache module support via DSO through APXS8 c3 \3 F, e, `% z! A
configure: error: couldn't find APXS0 Z9 N5 e  R4 ^& G: u& E' j6 K. ~
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。  x, t' S( k8 n. c, `6 t0 m
解决方法:
- ?( j2 v7 l$ j* j1 ~1 H$ |/ _9 S  f: I
yum install httpd-devel
+ K9 }. ?. g- C; h1 g/ ~! c2.没有pcre
. W+ d- J5 D$ j  @4 x
$ n( {) j/ o! T9 k0 n6 _configure: *** pcre library not found.
5 |3 \( j* B  `6 p0 s1 @  F5 pconfigure: error: pcre library is required( p* c8 [5 {* E" i
解决方法:
$ x6 [) V  h& c
. N% }7 l1 t& Myum install pcre pcre-devel- e- G1 q6 ?9 g2 s7 H2 }* `
3.没有libxml22 k6 {5 @3 q- P' a7 C

- f* n0 r- f( w% F+ T
. ~2 q  F3 J) l# \3 Mconfigure: *** xml library not found.
% P. k0 H% u6 Kconfigure: error: libxml2 is required1 V/ c( ^4 i# |4 Y9 n2 O+ g
解决方法:
; M. `+ m. k! E3 h0 ^; W) d4 W; ^8 X) b3 y3 q! ^- s
yum install  libxml2 libxml2-devel
. p" C: `% z, V" r/ G4.执行 /opt/tengine/sbin/nginx -m 时有警告
) \' b' j6 B' n7 T: L- }1 Z  _9 ?7 K
Tengine version: Tengine/2.1.0 (nginx/1.6.2): O% v! W3 V' J' u
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
1 U  J/ p6 f# T! L8 T9 n原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
) \, I- k! J3 o9 Z! ~6 ^. B& H- U
* R3 }$ ?4 W% T( Y; U9 o* O  G$ w3 x2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
  p, D4 W6 V+ i2 i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
  r9 U% `: O: D% E5 ]) O2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!6 m& {' I+ s' [% B0 i2 V( D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05". b3 s, r1 T+ ~+ g: X# E. T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
( A/ ]! i8 D* t) X9 P2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.) z" S9 g' w" a8 y* r
解决方法,移除低版本的APR (1.3.9)& S, t# q9 q+ _( k0 i

# I6 k$ n6 z; r+ a% @  v' [: Qyum remove apr
% s! I4 e+ }, K7 r. @8 e" ]5.Error.log中有: Audit log: Failed to lock global mutex
. c. d: D1 Y4 P9 v7 @( w' p5 l5 ~' p' p) y* O2 D1 N: U) m
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     ( z# e' c* @4 M2 \6 E& U
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]- `3 n1 i% R( [8 C- [% V5 j
解决方法:9 L' ?; U5 R( O/ M2 Z+ e$ @
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:$ K: M4 S4 d& h' [" G3 c3 V, a
- z2 _5 F9 F: x
SecAuditLogDirMode 0777
# `- |+ ~& S1 V5 L& Z; ]SecAuditLogFileMode 05500 _/ `% I% L* M) o0 a5 D8 a. ~4 z0 Y
SecAuditLogStorageDir /var/log/modsecurity7 Y! k) ]; O$ v; ]
SecAuditLogType Concurrent
* j% t* u! e; I# C参考文章:
' x/ @9 J" p2 f: c5 ohttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
1 [# k9 @( s5 fhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-9 10:59 , Processed in 0.061788 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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