找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10276|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。8 n- N6 N/ D6 Z3 T5 u! d) a
  p( z/ j: p4 t: ^
一.准备工作# X' R3 q5 g* s  {7 A6 ~

0 Y5 j; F' w6 r, `2 Q3 z& T* x8 g系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
/ x5 ~% |2 y$ Y( B" ]$ {/ k0 {# F! A
- h  C: ?# _9 o+ ftengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz7 K- C" f( a% z3 H# u. g# o

5 {" l/ i, l; V. k; r/ Amodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz4 `; l7 V: t: M; q

3 R8 {6 J5 }- G  m% OOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
$ }6 b7 C4 m, r& k& }- k: [# E& n2 d: x
依赖关系:; f) \# p( W+ w$ V0 O1 o
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
6 ?1 l2 j: E4 q" F% q* U% T2 P8 ^* C) c
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
" y" ?3 _" R9 ~0 I" s6 m, `modsecurty依赖的包:pcre httpd-devel libxml2 apr
2 t- f# \- }3 L( t% k2 ]  }; s" V& l- \4 ~7 X5 B, x/ t
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel8 H8 X6 B( }" m: H" \
二.启用standalone模块并编译
8 f7 ?) z) [% `  N4 w) ~: A; k8 R1 p! \" I% s, {
下载modsecurity for nginx 解压,进入解压后目录执行:
% V- r& c$ ^& Q0 q, O8 r2 U5 b+ m7 N6 A6 C3 M% ?% C
./autogen.sh; u; S+ w, r+ H9 b6 W) a2 I$ W
./configure --enable-standalone-module --disable-mlogc
& p9 r) v+ @8 s/ i' F- Tmake
) m- ?$ J2 A- ?0 l3 I; L7 N) D! F三.nginx添加modsecurity模块3 H7 e! T8 T, ?: b  h: W
1 D( {+ Q$ x- p* ]
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:6 m+ Z. D  x; Z5 L
; g. R7 c- ^1 |
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
5 ]3 r* ?  e& M* M& Fmake && make install
# O2 ]/ k/ }9 Y6 z  j四.添加规则3 L2 H* b$ U) S4 L3 Q
, d" u7 y. r/ z- v. F/ [
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
, O9 Y7 W" z6 n% R( m9 K5 Z0 O  a, _9 I# \$ `/ e
1.下载OWASP规则:
# {7 {' G& Z' M# A3 _. R% h3 y2 O" T8 m  q, A
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs) y8 p: \5 L7 T6 E
9 P# x) J- X/ R( o  T7 T# T1 `. x$ i) @
mv owasp-modsecurity-crs /opt/tengine/conf/& J) V9 U5 T: t+ @' [. N

. a4 _" `, ]2 E7 E! n' y. ycd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
& V$ B' w! V& v! p2.启用OWASP规则:
5 Q! V: f. D8 G5 A' g" G, \4 ]  I$ c5 [- W6 g* ]) }7 J
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
+ n' m& \6 b+ x3 [. }8 C# @0 o" }
5 N; J2 w- o4 u1 t编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
  P- o" w. h0 N( k/ W) a
" Z( y6 x% g: m" M( X3 Jowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
& D. a) ^* \8 b2 ]6 q
& m& [9 S1 n2 m) `) uInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf% x) ~9 i4 C- i: W5 |/ K
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf5 a2 R6 R6 s- l# E+ P$ X. @
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf0 N: P  ^# o% u6 f
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf+ _4 x+ H) g9 T: S6 N0 H
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf0 {: ^+ t( m' ?* m* K
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
7 X  A$ P7 v% r) g) w  N6 ?! KInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf# W- o( R( |# ^6 V# a: n* d
五.配置nginx
8 O. }9 Z5 g% _6 t1 P/ [3 T9 I- P# [; e
在需要启用modsecurity的主机的location下面加入下面两行即可:
+ H: ]  x& ^0 \& f! j! ]) z
6 i, u4 F1 ~8 ], ?% NModSecurityEnabled on;  : E0 O- w3 ]6 m) j
ModSecurityConfig modsecurity.conf;/ X5 f* u* z& g6 a" @+ H. F$ y
下面是两个示例配置,php虚拟主机:# }. T; V, U: y
3 \7 I) _/ y+ y/ p
server {
5 X6 G: o7 @- g8 D0 K- w: s. {      listen      80;
; v" T6 |! d9 \+ @/ w6 v) w: f      server_name 52os.net www.52os.net;
% q; ^" ?2 R2 g3 q: Z: y/ v" B     ) K' m8 L  \: `4 N
      location ~ \.php$ {
; s5 D3 o0 P8 P5 o7 R; @, t      ModSecurityEnabled on;  ( K! p: Q+ G3 I8 B
      ModSecurityConfig modsecurity.conf;+ C8 S8 G/ p" h: J

. Z7 i' s7 j! b0 ^: u1 I$ D8 z      root /web/wordpress;2 x1 o9 E3 X2 z$ q
      index index.php index.html index.htm;
5 \% @$ k- l6 U% z  i2 K* N" E  
( p8 L' B) p+ o# a. U      fastcgi_pass   127.0.0.1:9000;
5 F' q+ g/ U4 w+ R3 M      fastcgi_index  index.php;/ G9 o5 n! L6 E" |0 x& N
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
: Q5 \4 G: W" Q$ d) _5 Q. C      include        fastcgi_params;
5 @! [8 M* x2 b4 a: ~      }
, |) v2 k- e+ L: P; k9 w  }8 o& ^6 h* m9 ^( _! f. a6 x6 i
upstream负载均衡:- {) S! M: f. b

0 o7 F) b, B* x3 y/ e8 ^upstream 52os.net {' I2 j9 C, j% e- L+ u6 b
    server 192.168.1.100:8080;8 {. @9 z! \. \+ p1 R+ v# k
    server 192.168.1.101:8080 backup;
. y( }& W1 }/ }8 A}7 ^& O) u. y8 C' i2 _' T
2 M/ d- [/ l3 I4 N
server {/ l. A3 w& B. q; e- ]* @& ]
listen 80;$ Y! @! e2 X* w# E8 m/ ?
server_name 52os.net www.52os.net;- ^) q" `3 H3 Z
1 f; S% }& T) u7 h4 c% b7 X. M
location / {3 b6 @% k0 e* e8 x/ U3 {) E
    ModSecurityEnabled on;  0 J; }2 D: K+ x4 p8 F# j' R
    ModSecurityConfig modsecurity.conf;  % e9 ]) t4 w- O! F5 o7 x& `* m
' [& b- w& ?/ R
        proxy_pass http://online;
9 E+ r, `% m, I+ F1 `        proxy_redirect         off;
; _* ^+ F- t) b! l; v2 }+ @' H+ [        proxy_set_header Host $host;
/ R9 ?) K* K8 f% K0 h( e! T        proxy_set_header X-Real-IP $remote_addr;
2 K; n5 Q: e9 |- I8 }$ [+ z- A        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
6 r7 Z% k0 c+ ^! U( L/ z    }/ t0 _, a, c' Y' e/ k9 z* K1 L" h
}0 S, }/ Q% ^) F$ p
六.测试
1 D8 \. z5 P2 {9 _/ @$ P1 h3 t- b! `2 O1 ?
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:. d! r( \$ d+ H" M. K
2 Y' ^3 V  M5 r& h4 P1 H6 `
<?php
: H. b6 B% X( G) H* k    phpinfo();   
) K8 S- [5 Y7 ~" h: w4 ??>
( \5 C" w1 A8 b. J$ ~在浏览器中访问:4 D' u. m* g( H& t- t
& \# W  t. P) z, T
http://www.52os.net/phpinfo.php?id=1 正常显示。1 |/ R! ~/ L( @* B& q
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。; v7 Q8 X1 c" V1 _
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
1 `8 B( H) X8 a7 K$ ]. L- T说明sql注入和xss已经被过滤了
1 W/ o2 T& G) N3 s3 }" T) a
' ~* [- x2 s' Z8 @9 o七、安装过程中排错2 d& G% b: h; C+ L
& o& ~4 H# d  S+ |6 c
1.缺少APXS会报错/ W) ~, V8 f" n8 w: `& C" S7 v

5 e- Y. y8 a# A! O: M2 n+ @- @9 Econfigure: looking for Apache module support via DSO through APXS0 i! f/ W0 Y: W5 I3 A
configure: error: couldn't find APXS
: _& U; m$ \- N, f/ b" x4 n7 c$ l' Aapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。9 \9 Z2 R: f. J% x- D3 ]4 Z
解决方法:
9 R& m4 j7 M5 F# c# h+ Y0 v# ~7 r6 A' G, k, o) {
yum install httpd-devel
; i: e) j0 u7 M% l( m2.没有pcre
& r8 a1 K/ q' g8 a2 |% y2 L) k+ K1 |4 g8 l3 N' r
configure: *** pcre library not found.! r! V' `, v0 P9 ~0 [( J
configure: error: pcre library is required: C) l' s' S( ~* |
解决方法:
3 j! P5 K% O3 E7 Y; p0 L( I/ y1 ^' ]* g
yum install pcre pcre-devel2 M7 _4 B7 m. @' o7 a
3.没有libxml2
3 O: U' B. O3 o3 C9 r* C7 H6 ]  H6 G+ ~) J& V

- b! o) z1 G7 i: zconfigure: *** xml library not found.
" R" R2 m% w, B; H# ^& d  n* x# F* jconfigure: error: libxml2 is required
# _7 s% I+ e; W- b- B解决方法:) z0 z9 f. h& |

, C3 l% _  F1 E0 U+ P5 h6 Pyum install  libxml2 libxml2-devel
; Y1 M) b+ {8 R+ O' C4.执行 /opt/tengine/sbin/nginx -m 时有警告
0 U: W6 m# R; q4 E1 {% y
6 D7 d* L: S, l4 v9 oTengine version: Tengine/2.1.0 (nginx/1.6.2)$ m' s' N' F" g1 }) Y, y) X4 X/ O
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!3 w; z% {: U7 q. H. _/ w5 o5 L
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
4 p# h& ?: D/ c, O& ]% O0 t- V; u5 W" W' Y& p1 @
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.9 W( m  A' I% q; O9 R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
5 I( [) N# Z; N4 e2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!6 {. C9 T. y6 G8 t$ G0 k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"4 M- U" G: `1 `& U1 N( ~2 U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
  Y& d# y: d( f2 I2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
% ]! O6 c5 \1 l* v) C" S解决方法,移除低版本的APR (1.3.9)$ T  M( n* F( h* z

0 I8 ?, n) s( p/ O3 y% j' w+ Uyum remove apr' }# M0 G7 F( H6 e. y7 I- A0 l" D8 I" g
5.Error.log中有: Audit log: Failed to lock global mutex6 d3 w' M' X& {
" V5 {* x2 r; F' \; R
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     " D  |( `. J& }( O
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
. ~' U5 I) p# r" R: z. n$ Q2 y解决方法:
' l: t* y0 e, J1 k. w编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
& z9 W# O% B; A  K7 y) B' a1 P5 J; w' B3 @, R
SecAuditLogDirMode 0777
! x  h6 b* q( D3 U' u' E/ ?SecAuditLogFileMode 0550& T7 u! D# i5 w& ~; b
SecAuditLogStorageDir /var/log/modsecurity
! s4 g; k6 R9 W; s5 qSecAuditLogType Concurrent
/ E; _/ V" F3 z9 f) K: u) `4 n参考文章:
! @/ E1 C; J  M; `0 \4 qhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
: O' a1 l5 ?9 Z* a8 F$ Khttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-3-18 15:43 , Processed in 0.050995 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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