找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12515|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
8 `: d# E$ c2 \) j6 z
1 R0 I1 R9 {' Z( j一.准备工作) G/ C1 e+ ]6 e3 z' p
# u$ c0 m3 \2 Q6 b$ E. d' W1 [
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.08 f4 k# y  O! c! U/ k

# t' C) L: U5 @+ T" Atengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz% T7 L1 Y1 x: N5 ~+ ^
" m; n' Q/ E3 V1 J# e3 e, P
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
2 K) {* r/ f- t* }% H& D: c# Z: k: k) @/ q. Z8 e/ g
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
! A0 }: i7 H. F- O
1 i, f' z+ n6 l依赖关系:
0 l) u) K% }7 }: v. L7 f  D0 Rtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
: L. }/ h1 Q  |9 _% {* Q+ r
2 G# K+ [, R7 Qyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
' s+ R" e" m9 V' s+ R* kmodsecurty依赖的包:pcre httpd-devel libxml2 apr0 d: ~5 D0 m2 e$ I. @" {! Y
# I# Y  p  ^6 M/ ~4 ?
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
$ v9 Z  `' U0 L  N8 `, S. R+ s. S二.启用standalone模块并编译
' l. Y2 m1 x2 Y8 J& x1 f) Y0 z# q! ?3 `3 v* j  J$ V, b( H8 [
下载modsecurity for nginx 解压,进入解压后目录执行:
, e; V  y- f. \6 w; g* P1 e  d. M# T, S* x4 I  ~
./autogen.sh
6 V" H' _- O; d# @0 h, v( y./configure --enable-standalone-module --disable-mlogc& b2 S+ T0 s7 v1 W6 S9 |% H( ^
make ) s9 U1 b. r1 U( H' e2 ~4 J
三.nginx添加modsecurity模块
# J0 P( i% l8 K% `! F: o# W4 `$ \% z) i8 z3 G8 y2 W8 N
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:1 w$ r2 s3 U8 z4 I
, a0 \1 n8 H2 H" Z* a# n7 j) Y
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
+ U# F6 @) c# S& Smake && make install
) Q; n6 [. v4 b* L四.添加规则
2 p9 G/ g1 W7 K: y& J, c
3 ~! s8 F# n$ t9 q" Jmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。7 t0 f7 R0 ~/ \

0 n! L, r! I  T( F! `% p1.下载OWASP规则:
0 f! {. K+ R+ W
: g; s4 w9 v1 \+ ^) _* Jgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
5 |* Q. v) O. b3 V. \1 R, R7 L0 K) J$ G1 \( V4 Y
mv owasp-modsecurity-crs /opt/tengine/conf/- b: X/ f* I6 N8 b1 Y: k! p
' G8 q( @9 G: U  `4 o4 V
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf  w! l1 B! S, s& q" Q8 a9 j
2.启用OWASP规则:
- H3 ?3 f( J2 l8 Q# J: ~8 d( P, [& e# K: S" W. o3 ?$ Q* f* [2 s
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
; {3 f. N; ~' D/ ~: {8 e8 D/ C2 {- X$ V5 C- N. G
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on: d. [1 s* _- r: `% h' c
+ \% |. I- q4 y+ u( J
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
  N+ \4 E+ E! x. n1 X, Y9 y* ~( k# U6 K! C- p5 P
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf5 l% W' p- _  O3 |
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf- ~& G6 M8 C% |* z1 T
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf) D3 a- X6 B: X
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf2 q, W3 p2 I. C" e* ?  |
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf6 f; B# J0 R8 E/ U4 X' g0 F
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
, W" T* G8 f( X0 I1 D7 l6 u! J7 U! gInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
( a" z1 o0 X' A- r7 a' E3 U! R& k五.配置nginx" `; Y5 U; l, A1 ~) ?
! o( m5 X- ~* ^# k: y& n# l
在需要启用modsecurity的主机的location下面加入下面两行即可:
; G" D- `! Q$ n8 H4 l) ^% b0 }5 ~$ M: j
ModSecurityEnabled on;  
! o' ]" M" [4 P1 V* iModSecurityConfig modsecurity.conf;: `4 O- ?. q0 h# Z
下面是两个示例配置,php虚拟主机:
; D8 q9 L' x, g# ^/ D& y) R  v8 m* B3 d& X
server {
0 {' p; C) C0 E      listen      80;9 e4 s& C1 Z, j* M0 X
      server_name 52os.net www.52os.net;
, l. j! ~/ O+ l, k# F2 _     , k3 H6 u- r4 ~  V" P) ?
      location ~ \.php$ {
. k" [3 a6 c; }9 J0 r2 ?      ModSecurityEnabled on;  
( J, c( C: f8 s/ C* A3 F      ModSecurityConfig modsecurity.conf;
3 f" g: L% W; O7 o) g* j' g' C$ _+ H
      root /web/wordpress;% v1 ]3 X* `% h
      index index.php index.html index.htm;
/ H# n) `7 @/ E% S' _; I  $ t4 P0 H: q/ Q6 z2 k
      fastcgi_pass   127.0.0.1:9000;
/ K2 G5 q- G4 @      fastcgi_index  index.php;4 K5 C5 z: g. l9 c' s
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;6 a* D9 V4 c# @2 z4 N
      include        fastcgi_params;( ]9 D4 c; A# _# W
      }
0 C; ^4 E  ?, \7 |3 A8 w  }; M) m2 \) u8 L& g$ o( c! I* u
upstream负载均衡:& P* ]3 y, U3 ~
6 Q7 s2 \. V! U
upstream 52os.net {# Z* |3 p* Q1 m# Y
    server 192.168.1.100:8080;4 ?* m* O! k+ p2 g; z) t% Q
    server 192.168.1.101:8080 backup;; f* p" G8 z: \1 H. E. a# c% c* ~
}, s+ S# @% g3 {
& D4 \; [3 p6 @8 e* N+ a
server {
7 ^% I$ q! S7 j3 n' q1 {listen 80;
6 T2 I( z8 q8 C( Jserver_name 52os.net www.52os.net;6 K' F, D8 e, B

3 ^5 U3 \1 h5 ^9 _5 C' Nlocation / {8 ^! Z2 G$ o- w* v4 d
    ModSecurityEnabled on;  % C- n9 F- N6 h
    ModSecurityConfig modsecurity.conf;  3 c1 I7 E, F: s  }7 \; p, J

0 }% G3 r& [9 j) m        proxy_pass http://online;
  H# Q, Y3 ~9 a0 ^) E        proxy_redirect         off;0 c/ w% n6 i) @$ l- F1 G8 n
        proxy_set_header Host $host;
. ~( u9 _8 D1 v1 C6 P8 z        proxy_set_header X-Real-IP $remote_addr;! h3 A; O4 B7 B& C  V3 l. A
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;% _& K$ c. ^* u, w3 J  B& L
    }2 _. u- a6 B$ ^
}$ }9 v! j  j$ z9 D  z1 K+ B
六.测试
- L& \" @; n' C+ z
5 A) p9 B7 I& m9 A) G6 V' |我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:1 K8 y7 I' A, [; ~+ y2 T+ X
1 R! V) z9 X1 c# Q1 E# ~$ L! |  ]
<?php
3 c2 @$ O+ w$ }, f    phpinfo();      f5 s6 ^& x$ o% B, j
?>* |5 O3 v7 _: u% m0 v% K
在浏览器中访问:
) t2 h) h1 Q% }- r. A
- d+ t8 c* t4 e2 \- H0 Khttp://www.52os.net/phpinfo.php?id=1 正常显示。
3 j  p  f* |9 I0 p! q1 z7 `http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
' d, F- o, A& A$ s) s1 v4 _http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
% ]( w* [' m, Z$ I7 F+ S: ?1 D/ C说明sql注入和xss已经被过滤了
. ?) {: {8 @3 v  o! s; G
. r8 {! k: k* o6 M$ Q/ V7 @七、安装过程中排错2 D" q; r4 f) s
3 e1 [; k6 z* m2 R; D) \7 [
1.缺少APXS会报错
* }  V; n; [" _, g( p- J( u  X, i  m5 H
configure: looking for Apache module support via DSO through APXS; U  x; q3 J9 T) `
configure: error: couldn't find APXS2 ]- b4 a; M) j" L
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
/ c; P  u% }( y7 o' c" H解决方法:( l3 B" ?2 {* x8 ?1 O4 U" i
+ D. U( S* \  Z7 P5 m
yum install httpd-devel4 X, ^+ q  {4 I' z& C
2.没有pcre
+ c' q5 l( H/ |+ O6 A$ ?9 {- ]+ x% l1 _
configure: *** pcre library not found.
! _# j7 u) y4 u( w/ kconfigure: error: pcre library is required: d( y% ]  F$ H
解决方法:3 W- |9 X, t7 Y5 H

: A4 W& e+ X2 r6 uyum install pcre pcre-devel
3 g4 B5 a! u0 V- m3.没有libxml2
) w& N0 d7 [+ A, W: N* c: o- c7 l  ?

6 m: }6 e4 j. r: n7 x5 Y3 oconfigure: *** xml library not found.
/ r: G8 W- V/ P5 P9 C* vconfigure: error: libxml2 is required5 R; ^$ k  o  L8 C; G8 h/ N& |1 r  V
解决方法:# E3 M$ D9 t; A- M: l' t+ w
* w) W) o, E) L7 ^8 Z) n; v
yum install  libxml2 libxml2-devel
. X$ y, ~2 A& N; o  u/ [4.执行 /opt/tengine/sbin/nginx -m 时有警告
, r! U( d' S" z& |9 Q9 [6 @; D
0 l% K# n1 T. ^* N" W. j$ OTengine version: Tengine/2.1.0 (nginx/1.6.2), H+ O) e2 n$ l- a$ D4 d9 A
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
! S9 M) g  r" Z. b原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
5 t$ Z/ W+ D) d) N7 _; y1 J: {% _( m8 N3 }- w% Q4 Z! Q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
1 k: S1 `$ M  u3 Z0 {' n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"! B+ L: V9 H$ V9 |& V0 G
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!" N0 c* f2 y2 s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
9 R$ @  h& J9 h2 E' d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"7 U% {$ y1 v, H3 h
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
. i1 [& D& `; V3 z% M) {0 y解决方法,移除低版本的APR (1.3.9)
  s7 L% k; W8 g/ X5 F. a/ P! X9 @/ R2 o. K
yum remove apr
9 v' D6 ^- K% e' L. H0 }6 S5.Error.log中有: Audit log: Failed to lock global mutex
; }& ?& g6 b* m& q( A4 X+ ]( P+ A4 M) b8 O3 w, I# [
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     - n8 ?7 m, n4 |; p$ {$ q( A* @
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]+ {# i8 P3 T/ v1 X) k& x
解决方法:# A9 `( |* o$ i6 ^, ^8 `+ T5 q
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
* Z4 `4 J* J, N1 y
  {# _( I8 K; Z5 }) N! u8 z# @SecAuditLogDirMode 0777
5 E; E) M1 h0 ?8 l- w% W! S. [SecAuditLogFileMode 0550
! Y, R. J: w$ e' A+ t: QSecAuditLogStorageDir /var/log/modsecurity
$ k! N1 O( J7 ?7 H* zSecAuditLogType Concurrent/ F# C5 j! ~+ B; ~
参考文章:
* {" ^9 y+ l3 t3 }https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
5 S4 p6 M5 w" `4 p! vhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-13 04:54 , Processed in 0.062563 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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