找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12240|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。% x* h' w3 [. W
5 [6 V+ b% q/ U+ D
一.准备工作7 n: h6 h5 ]+ c( G7 t
4 y* Z4 m* g; l. \2 v4 u" ^
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
& A. l* P8 X9 X6 ^' K8 @* K0 ?/ [" |
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
* \( m3 L2 }8 |  q3 E# D- n( F1 J0 g
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
: G; D+ L9 k% }1 n1 @$ I
; u: N4 a. x' R9 s5 M; L( cOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs5 D) _. S4 r. V1 D

/ o+ i2 k* }: J5 U* e# n4 C依赖关系:' l4 b. ~: P% \
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
# i' o* `8 S) d& c
4 R, Q9 @; ]: n$ }4 B' U/ kyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel/ \5 N" A1 ~) y
modsecurty依赖的包:pcre httpd-devel libxml2 apr
: k$ `$ k1 \- `' O. G* W
! y8 @2 x) G; ~8 A+ y6 s: Z, _" fyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel# n6 L2 o) h8 X& q
二.启用standalone模块并编译+ r: W2 x* O; i! a

% k( M- y3 \0 ~2 j% @0 ]( f$ F下载modsecurity for nginx 解压,进入解压后目录执行:3 P8 y: g2 ^2 j" L

; F8 F) R3 l* i4 t./autogen.sh  U% `2 J) f2 O% I! d
./configure --enable-standalone-module --disable-mlogc6 r0 d6 P0 a' Q2 E( k
make 8 E# E! ]/ z# r3 l- U
三.nginx添加modsecurity模块  N, ^4 p' T8 \: p( N0 r
$ C' Z# f) U' `. {; o8 |
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
3 U) m( y# W2 H+ i
- U( R; h  f1 N3 e./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine1 B" M! q+ e' g
make && make install
' j5 f5 Y. q6 E+ Y四.添加规则0 l% O9 j3 _& f: X: k$ a

8 M5 \" e- j# U# @: K5 Hmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。- Y0 a% a" \" \9 v8 @1 E9 ^: l

; z9 m: {) ~; c% J# L1.下载OWASP规则:) U3 o: O' r- ^
6 ]) x0 N* y9 D, d7 @: x
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs$ B2 n' e$ Q1 {/ @( ]; m
8 h% t' k! J* `5 [
mv owasp-modsecurity-crs /opt/tengine/conf/& R, z8 q5 s+ i. L

4 N; X/ s0 E3 e4 N$ R* [cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
- n2 i- B& X1 q) g2 \) Q2.启用OWASP规则:+ Q$ o0 u& \9 G0 d! ~
" b; }+ j: G8 C0 x4 n
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。" F7 O8 Y, t6 @* C$ _& u! @6 y
, W, p2 [/ }, F- A; y/ Q% P; Z
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on$ |5 x$ Z4 A5 w$ T+ J3 ?4 H

  p6 K; g0 c, w9 v" ~4 uowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。! K8 D+ M5 e1 d- g  y

% c: k5 O. h; K9 @5 TInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf$ [- q5 Y# O& l+ a9 f+ H  N
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
& ]4 W9 }" `# mInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf% _9 M  j, q  x2 E; s
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
" d; P9 H7 H: jInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
$ S1 E0 C* ?# h: RInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf  [1 y5 A; ^- B& r# N* d$ ]  Q
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
: w7 a3 o2 j$ N0 O2 X# Q9 o五.配置nginx
& |% {) V9 g5 A7 Z2 b5 j, e# H$ D- N5 W
在需要启用modsecurity的主机的location下面加入下面两行即可:/ e* P" m6 P; D8 h) F, T
$ e7 x/ F5 y1 _+ q9 U& y
ModSecurityEnabled on;  ! u! L" n7 F1 w9 V  x
ModSecurityConfig modsecurity.conf;
3 R; N) C( T2 z1 A) `% o7 i& a下面是两个示例配置,php虚拟主机:- s2 }6 @& |+ S) P" |: D: a

! m& a4 a: R8 tserver {  H/ {$ p4 r5 s# _/ w
      listen      80;" q. j4 h* g4 m: y1 O
      server_name 52os.net www.52os.net;
( c% y0 ], f! P: ^     & \" @7 T5 `/ }3 }4 G& u7 C
      location ~ \.php$ {
) T" N  x5 }4 g3 w3 V4 u9 _' n) b      ModSecurityEnabled on;  
2 l5 j; `: d% ~5 T* E6 x0 [      ModSecurityConfig modsecurity.conf;7 c6 x/ a, ~  i- y1 `
+ M9 B: r' x- ?9 D# T, u; e
      root /web/wordpress;" a  x# d6 x# u3 i+ i0 f0 D
      index index.php index.html index.htm;
3 G+ U! }6 e; Z' \  0 f# y# C1 \+ s9 w
      fastcgi_pass   127.0.0.1:9000;) u- {, E/ f% d1 G1 A. S9 \, k
      fastcgi_index  index.php;
- c2 K2 |$ x, s; K& ]" D0 U      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;" Q0 `" \* G8 C; M' m8 }8 E
      include        fastcgi_params;
6 K) P  Y. ]( E6 n6 Q" W      }5 T0 y' w1 ?( m; f
  }
7 s' B0 e; }3 C' y7 D( Kupstream负载均衡:
8 w( o" P5 T% K/ ]. b4 |7 G- s& x+ U" y, M6 Y2 m
upstream 52os.net {
; W: z9 t! m+ E& e$ j! s9 I    server 192.168.1.100:8080;- ^2 D2 w0 U+ {( A2 T
    server 192.168.1.101:8080 backup;: Y$ ^' ~: @1 M8 n& i+ _5 X. r
}8 b- U7 P) X0 x' L$ o
) I$ e2 W2 D" r3 x
server {
/ z; m; \$ ~+ x: O) zlisten 80;7 j0 S& e) m( O7 t9 L. M3 ~) e3 u
server_name 52os.net www.52os.net;
4 ?, ^# f2 G# e0 c# G4 A5 \$ Q1 R. A5 ^  _# R2 ?- L; e2 o
location / {
4 |& G; b4 o% b1 p0 S    ModSecurityEnabled on;  ) `+ D" s/ r4 d- \3 \% s+ b- ]5 W
    ModSecurityConfig modsecurity.conf;  3 |; \+ p4 o% \" Z& _
" _, _9 d, v0 P5 B( B6 b# Z
        proxy_pass http://online;& S$ X/ J7 Z2 R9 _  R6 M
        proxy_redirect         off;
, l" ^8 n' N# ~        proxy_set_header Host $host;6 J+ F/ h8 F' D
        proxy_set_header X-Real-IP $remote_addr;& ^# v9 \5 h+ j" _( ^$ ~8 t
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
# T5 e0 T# a2 F7 s' k    }
) C8 v; ~$ C" C! t! T0 D}1 p% i9 s" J; j) E0 g
六.测试
( i* h. z( Z6 O4 `2 i# w+ o7 P: n" H& y0 a% y
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:  ^/ t! B* @$ T) g7 B# c
4 q3 Y6 a7 e1 F' P' `  l
<?php
2 \, y5 {& \# w    phpinfo();    5 I$ q! b  ]& \! w1 n
?>
$ }  g# v: J& @% M' N在浏览器中访问:3 y$ W4 X  w; I; C$ @
. u' K) h: H4 T. m# {) F. v. |/ I
http://www.52os.net/phpinfo.php?id=1 正常显示。* \0 y8 Q$ g' I
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
3 N$ b; e3 o% l; E' ^http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
; V1 u5 M8 o9 G说明sql注入和xss已经被过滤了
" b' n5 l4 h( R: y) E, ?; U7 U2 t' ~0 x5 g3 i# E1 f! `
七、安装过程中排错
- B: R2 J( N. U8 ~
3 z5 ~- W8 U$ B4 f$ C; I) M1.缺少APXS会报错7 U. E* `- L: C+ _3 u$ g6 s! v% ?# G* w

: [' q; p, D  s- h* k; {configure: looking for Apache module support via DSO through APXS
9 X7 {3 d5 i1 s9 W8 b+ P2 l+ l" oconfigure: error: couldn't find APXS
7 Y5 P( p. g. i! S! Y  D7 d/ zapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。' M& q5 h3 Q" U- `+ o' ^  y' E1 j
解决方法:7 u4 X, i1 D" n& M5 D2 T6 l, r* i
3 _, Z2 A, l8 ?! {1 S6 H  {
yum install httpd-devel
! H4 N: a' ~: k0 i2.没有pcre
/ r8 i( O# {6 o% t
2 p! P5 K; A% ?! xconfigure: *** pcre library not found.3 {# z4 J9 F, K3 u. ?
configure: error: pcre library is required
- ^; l2 h( h$ u解决方法:1 }& Z% s$ ^8 j- T: o

( {% e+ e+ F# W: v* \! @" c& xyum install pcre pcre-devel
3 @8 X9 z* e. H6 a2 s3.没有libxml2
# ]+ B& U* f/ R  g) E8 b: L5 b1 I* U0 M2 Z, W' H* R' A

' K& W2 r. \% z9 s7 i2 J" \configure: *** xml library not found.
) u' e* S) x1 y9 D- xconfigure: error: libxml2 is required1 K7 K& ~! S( R8 O2 Q( _
解决方法:/ C2 E! H5 u$ B
: X5 C" p4 {3 C  @
yum install  libxml2 libxml2-devel
7 d" o0 [9 }4 m$ ?* W# t+ Y4.执行 /opt/tengine/sbin/nginx -m 时有警告
, D  ?5 \! A: w# E) I1 U6 i7 n* F% e, _( Y& ]2 J
Tengine version: Tengine/2.1.0 (nginx/1.6.2)/ n% R* K$ \4 k
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!1 W; O9 O. R3 \* Z) i* H
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log8 H/ R, |$ w' I
- F  k! Q( v: ~' S! g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
: r& s0 H' A. }2 S+ M2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"; p- a. b& @2 L! n- B' L
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!/ o1 T* n! ~5 F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
; @$ s4 L) r" t) c; ~1 M2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
, @9 }1 b' \" w7 m+ J& W5 E2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
8 h5 @# i/ K" u) X6 e) Q3 j解决方法,移除低版本的APR (1.3.9)+ M  c( v' Y" A- j6 x) Z
' d$ v1 r0 W) }% A0 ~, H# u& P
yum remove apr
/ q2 v7 Z2 C* Q' e0 ^5.Error.log中有: Audit log: Failed to lock global mutex, v: l" v2 B5 ?6 Y; M& {
# E3 E" ^2 P6 s# w
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
+ U% _: z6 H$ ~7 F# [+ Q+ ~7 `global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]% j4 ^5 M7 B1 w$ @
解决方法:
# l  \2 s6 W. h& x) F编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
, S7 z. B" _2 D% N% g3 i$ G; M- l4 u( Z% i2 c- D1 c
SecAuditLogDirMode 0777, R: }; k7 q& d: a
SecAuditLogFileMode 05500 x- J( L, k5 n$ t
SecAuditLogStorageDir /var/log/modsecurity6 |0 R- |0 I5 D( a6 h. Z
SecAuditLogType Concurrent
$ q7 ^0 u( z- Y& W参考文章:
' R* h) v5 Y. Nhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX% q$ R4 }9 q" a; B. }! h) i! X
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-18 13:36 , Processed in 0.108293 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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