找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12303|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。3 i# x; L* `8 p2 J
6 t4 \: |1 f- p2 X6 Y
一.准备工作0 L+ U. l% v. g  j4 O

7 Q, l0 M/ C: w- E0 l系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
0 [2 S5 i) I7 Q9 F' f# w9 a
. z. |6 A) T6 |7 e2 R# V8 D/ dtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz: c/ x5 O! ^( x* e, o1 ?, v

/ p& {/ l! ~8 Emodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz. R1 l- e! r9 F1 d$ m- j8 l% m& M

* q3 M  Q9 L+ B! v/ pOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
: Y- D* T& U6 ~, K& ^- M
: _& E# E' j4 ]3 Y1 T依赖关系:
5 [3 w# D' `. C1 f. O( G+ }/ H0 Stengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:( F/ u; J5 L4 y* G
, S  b! A  S* N9 a1 `
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
- D2 U9 T) F- f* Emodsecurty依赖的包:pcre httpd-devel libxml2 apr( \8 F/ C6 y5 [' X" @

/ r. [; D) A0 z) D  M: Kyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel, z8 H" y1 e$ Q, W6 g
二.启用standalone模块并编译
  |& v1 ^% ^' r9 j. k1 m. g5 \
& q; \$ T: }2 \( Z下载modsecurity for nginx 解压,进入解压后目录执行:' v: ^* f$ @6 V- M% f
' L$ j2 P% ?% t8 z
./autogen.sh
) i2 D8 a6 N  x& m5 ~./configure --enable-standalone-module --disable-mlogc1 t& R" l4 x5 |! ~0 r% j' X
make ' G) j  P3 k7 J. d( X% N
三.nginx添加modsecurity模块4 T% B: T$ \# u; j$ g1 Z: w, k' [* f

$ [/ x- q$ I* E3 U在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
: s+ s( i" O8 o( A4 w; x% Q/ Q% U* l4 _+ q# U  O0 {  u1 q
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
4 I, U" @6 o+ L( z0 j( Pmake && make install
- e$ U7 F, e; J- d2 g& n" u% Y四.添加规则$ {) h/ y9 H5 p# z3 }: a

& i! p- P! ]( S2 ]1 O% x  ^modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
6 M1 K! A9 `! R4 ]
7 S  h! P$ W: J0 X7 L1.下载OWASP规则:
8 I# H; i1 ~( V! {/ h. E: \' q8 R+ m
* Y: A4 C! m0 t' c- ]git clone https://github.com/SpiderLabs/owasp-modsecurity-crs# T  b! ]( c; D+ i/ Y0 v! p* n

# _+ }8 t) J$ b1 D: m0 nmv owasp-modsecurity-crs /opt/tengine/conf/
: H* m  P+ f, e* h0 b' P1 @  ^8 x4 U+ o1 M  [- x2 O' L* L* v
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
3 u0 E& G& R6 p+ Q# i) N# W- n2.启用OWASP规则:
8 g( b6 O3 M- U8 h/ R! r
$ k( W0 f) d2 i& j" l1 \. K4 G: ^1 {1 N! s复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。" x$ g  z3 W, W* Y+ d& _

8 U. g2 ^) w, \' M7 {0 l" D编辑modsecurity.conf 文件,将SecRuleEngine设置为 on0 R# A) ?2 n* r$ h- M
* E- t$ q# T+ I" y, x) l
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
4 o, G' X3 \6 O; D8 H3 G! [2 v# T2 J0 |
  l* _" h! q4 ]! ?  y" j5 {6 }# F/ m: XInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf7 c* G6 n( N4 c: y: m' A
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf& h" u4 f4 V) {2 x
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
3 H5 C6 G+ y  W( D/ j/ ]Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
# E# J- f# @. bInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf6 ~) {8 i% e8 r6 @* O) J' x/ Q
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf, t9 W$ G% F; g0 C
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
% p# T4 `( t5 {9 s& ^5 g五.配置nginx& \( c6 l9 H6 l2 z

( o0 x  k% _% P9 i0 ~3 K* ]; H在需要启用modsecurity的主机的location下面加入下面两行即可:
% |% w+ `! \3 e% |4 }+ A0 N2 D! Z" \4 G% o% H
ModSecurityEnabled on;  
/ e+ Q2 Y5 w: G( }% K  yModSecurityConfig modsecurity.conf;
$ M3 ]9 ~8 L# k; A! ]5 R下面是两个示例配置,php虚拟主机:
4 F+ O3 @. l1 o) @) _8 Q
: i* ~" p+ g7 i: C8 Y: |server {: G" a  X, ^$ n; y: b, I; W. B( q
      listen      80;, k4 b& o4 l1 B; \
      server_name 52os.net www.52os.net;6 f0 F! f  v$ G/ o: e5 u) P
     
" D0 D" Z7 d% A9 f      location ~ \.php$ {5 ^( V/ r- G: H3 k6 k  ~7 P
      ModSecurityEnabled on;  
* O% G$ `- z5 o; J5 T      ModSecurityConfig modsecurity.conf;
/ n8 l+ q0 O' }8 Y% g; t5 Q6 g2 v9 Y( q0 ?0 T1 K
      root /web/wordpress;7 E/ D9 ^2 ^1 g1 Q. [
      index index.php index.html index.htm;
; b# |; ?" o7 A# Y, i# \  
; B, C7 m, d- x+ Y7 n8 u% ~      fastcgi_pass   127.0.0.1:9000;
" D: Q: Q+ p! S& N" }      fastcgi_index  index.php;
7 P9 U' L# H# \0 Y" c! ^) g  X' h      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;+ t- |& O3 v* ]! F& |
      include        fastcgi_params;( Y2 B8 R, a+ c* _- m0 \- E
      }1 r0 C6 D: ~; n: q% n' i
  }
" h. E+ b  I. }9 W2 Xupstream负载均衡:# `' G* @& D" D" ?9 S

6 w/ X6 H5 A( Nupstream 52os.net {
* \6 C9 v  x' G* o    server 192.168.1.100:8080;
5 ^; X/ F7 [% @% H0 G' r8 y7 j# P    server 192.168.1.101:8080 backup;
) I7 t6 H7 F# u  {" @}
4 W, ^: G' v" _0 [. q7 l2 l( B+ Q% O: _) m4 _
server {0 D; h) u- Q7 q9 B5 C. E
listen 80;
& |% z% h# k! R& Q- m7 o6 n8 F" p) G+ s$ Aserver_name 52os.net www.52os.net;
: V0 m( ?6 k: M* H; b3 l/ w  |! r  I. r8 l  k$ D0 ~+ S
location / {
" @1 ~3 i' g; u! F* f9 K    ModSecurityEnabled on;  ( \- f# [3 o9 i5 P8 r, ^2 v
    ModSecurityConfig modsecurity.conf;  
2 [* o3 w4 x  L- ]9 }. _9 u; r1 b$ s3 u( g
        proxy_pass http://online;3 l  i' r% i- R: o9 A; N  z5 _
        proxy_redirect         off;
# h2 l5 ~6 q1 a. N% R0 c        proxy_set_header Host $host;! ^; V* `( v- ~5 Z# j1 [
        proxy_set_header X-Real-IP $remote_addr;) m; F% Y7 W: `3 D8 `: h8 ^% W
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
! @8 q- f! V  W! d    }3 ]4 b. ?7 }2 h- H
}6 X) s$ G/ V. w5 q4 U% [1 |- G
六.测试
3 b5 i9 M1 J/ F; M3 q: e2 e+ u
- V( r" ^* _: b  z3 N: L9 Q我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
; `2 Z) R( g+ {! r4 T  c
+ b$ A4 J! A# O: A$ {/ {. ^<?php
3 }+ M: F/ ^9 t( ^    phpinfo();    , {# \5 K, r/ \4 n" b1 v3 O9 ?- U
?>
$ b8 x' o: f  o& g+ C" [4 M在浏览器中访问:; x9 @( d% \+ N
0 P- W) Q# }* _* q" O
http://www.52os.net/phpinfo.php?id=1 正常显示。
2 D& X6 c3 V% U/ V$ E) @) P, Fhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。& D% P: a: {+ Y
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
8 b' L$ c6 L( c; p& G0 I$ p说明sql注入和xss已经被过滤了& r( l& s, S2 L. e$ y1 s

5 s' S/ u$ @9 `: W+ w4 _, ]七、安装过程中排错
" W0 c1 @  ]; \% k$ U+ H3 ^( M
  _  |; U* k5 a( B2 X! r" W2 h: j1.缺少APXS会报错, o- ]/ q3 H. t0 u3 b7 U' q
% [' u9 M) o* u/ D4 w  |* a/ {
configure: looking for Apache module support via DSO through APXS9 x0 b9 k: P! D# m5 l8 j: k$ Q
configure: error: couldn't find APXS0 U# s7 A( F. d4 B/ _& A5 `+ p
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。+ E) J0 Q% j% Y; i- S, K" }* P
解决方法:
" N8 \' ~- ~/ C0 P! c/ R, F, R) l( L
yum install httpd-devel
( T6 a+ C1 S) d% c" K, K0 g2.没有pcre4 u% h1 M7 g: y1 S; J

' A  J" Y3 C5 u1 |7 Z1 xconfigure: *** pcre library not found./ k4 l7 R9 W1 B6 i; t! q
configure: error: pcre library is required4 Q; V! D( x7 C8 d* n+ p: [
解决方法:
+ Q. d6 F' F3 H8 z
/ w/ F: l: u0 s* z7 A$ h+ Oyum install pcre pcre-devel
) F7 L2 j6 O' F( H& e7 L- _3.没有libxml2
/ v( a% [! g) m+ C: H( T  C6 x1 f" x; Q0 G: J; y9 X  a, e2 b

) N2 A, s/ r6 w8 `9 @configure: *** xml library not found.
: g: j& c/ M! m: t8 s% X0 j2 Vconfigure: error: libxml2 is required
( a! }" z: O/ P) @! G$ l# ~解决方法:
; O: n1 K7 b- I  v( Q
: \) \( u$ y7 D* |yum install  libxml2 libxml2-devel* s* y6 k  ~5 B$ O. X. g
4.执行 /opt/tengine/sbin/nginx -m 时有警告
( k* p! V* y6 I) I* F" i/ |' ]2 S4 c6 j/ o
Tengine version: Tengine/2.1.0 (nginx/1.6.2)3 b1 B1 N/ i  m( P( b  H' J+ A" t
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!! Z& O) o: J. i4 i( r" B: w
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log( @$ T9 K/ B+ O" @( r
. V- @! B. v- A9 h6 t6 P
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.6 y0 T3 S/ i0 j2 `6 }) |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
( M, ~6 J: m- {2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!% ~' y& v* b( Z5 q9 ~1 c" Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
0 T2 u9 h5 C+ g9 u' `5 X2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"0 F# I3 Y2 }, X! c
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On." |% G7 e# @5 V% y9 U9 F2 v5 p
解决方法,移除低版本的APR (1.3.9)
0 w5 a+ d, l3 S3 a$ I
2 r# w# c0 D6 @  e! Vyum remove apr% t' d+ I, R- w) q& M; }+ b" {; O6 h% {
5.Error.log中有: Audit log: Failed to lock global mutex: J5 ]7 t& J" i/ q8 m

5 }6 |* m: c9 E4 X3 L+ V5 Q8 n# l2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
: ?6 t6 `/ [+ {8 ?! q& jglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
2 ~  d4 Z' d3 n# C9 W解决方法:
$ @$ q* f/ h9 L5 b/ k# I( T) P编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:2 m- T5 @% g" [( b
6 l# ?9 F( Z% u1 A6 _
SecAuditLogDirMode 0777
/ x# C4 e9 l' hSecAuditLogFileMode 05505 n) T" r# }* s- Q, R
SecAuditLogStorageDir /var/log/modsecurity1 j. L- ^$ }" [( V% n' b
SecAuditLogType Concurrent2 Y5 k$ e: ^; z( F
参考文章:1 p* r& a  |# e& V' c
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
8 H+ {7 L9 I4 f1 p2 J; uhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-25 02:10 , Processed in 0.066398 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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