找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9720|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
' B* X9 Z; ~% d0 N9 D7 m2 y: d( n) Z
一.准备工作& |0 f+ h6 R, k- B- D# T5 `$ N* L
5 ~+ {: U3 |: l0 s8 r
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0( }/ C7 [; }: g5 z' z" J$ a! R- M

3 G5 U, k1 M( k. N  T* Dtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz9 m. N: E0 i0 n$ w) D( K, Y: y: R( B

3 p& i# q! M8 K+ Gmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz3 a1 j" A5 T- _6 x- d

: c6 G6 a1 I1 U, X& ~; |" X7 o. ^OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs) g! U* J% X2 e

3 T8 c- t& Q  I- [4 }- o依赖关系:
, I( D, L  _" p6 R9 q$ d( x0 \tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
8 Z/ R, r( C; A6 [5 M
- n( G7 Y( {. [9 w. D) n" Fyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
. ]) j% u, k4 `# l: A) ~8 T. |9 C; ?modsecurty依赖的包:pcre httpd-devel libxml2 apr% D7 k4 {7 O6 b) d4 P

: M% [: z5 u+ U, }$ r. s7 c# U  A, a) qyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
3 ~) D, k: @2 H1 r; Q& J二.启用standalone模块并编译/ `$ G; t7 N9 f3 D# L9 h

* Q6 o3 ~3 u, p* u下载modsecurity for nginx 解压,进入解压后目录执行:4 S2 X& p& c: \# Y- Y

' y$ V0 h8 [+ I# C./autogen.sh4 o. V/ L  P1 v3 g) p4 e4 `
./configure --enable-standalone-module --disable-mlogc
8 |; y6 k% `7 p  Q% X0 Gmake
) t, ]8 J2 r% C三.nginx添加modsecurity模块) X. ^; _- }, W8 f3 u+ p
; a; M+ m7 @, \& L4 O
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
( w. ^1 F! c0 \( Y! q+ l( G
: I) R  C) G& b3 n./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
+ P: @5 h1 o$ y$ rmake && make install
( H* R& Q% G, }5 [2 x, W1 T四.添加规则& t; e/ c3 O0 R+ u& l* l
- N# s  Y- G$ z
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。1 a: K# n. d2 t' b% s' v
  z& d  d8 u; _' h. A, z
1.下载OWASP规则:
3 N8 o' [. @1 ]) |& B, Q! b- g: z8 L( b( j" b2 m
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs/ T) ]5 H7 D/ |6 u: f
& u* Y, x9 x9 B; i( b% P! a
mv owasp-modsecurity-crs /opt/tengine/conf/
3 T) i3 t- w+ L% b: {9 I9 j& N
5 T; t# c5 }# W- @% i  }4 _cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
6 l0 S$ {' v, f3 ~2.启用OWASP规则:
6 n  s9 f. u# _7 s% ?% R  p5 ~% o- `9 G1 f& }) F2 }
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
6 m6 c; I4 K1 B; M1 L& v3 u# Q0 x& I% O
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on0 x9 }. e2 h4 Q8 Q! A

+ e0 L* Q3 p7 M- @$ E: ~" J% Powasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
, Y% }9 F4 V% O% j4 ]& F- z) x4 q: n$ t
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf+ V% a2 I7 i: J- `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf% b3 O; G5 G' e7 s9 L  |
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
1 h) S" x: H( C5 x/ x) _6 XInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf! d! E9 g& z% S& i" e: t
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
" t, f, s' d" kInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
- z- C$ M4 b! E0 K/ d1 qInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
# [3 v$ K/ f2 a" ?, \五.配置nginx
% f8 q5 k% P$ f& s5 y- j2 t8 h- {* u/ Q% T
在需要启用modsecurity的主机的location下面加入下面两行即可:' ?: T5 J# D2 ?. k" Z
7 T# ?1 x. E" Y' b
ModSecurityEnabled on;    ^+ u! d; x) G; ^  x
ModSecurityConfig modsecurity.conf;# ]: I, \3 F: c( C: w" M! b
下面是两个示例配置,php虚拟主机:
  I. k* ~* U% e- u. d
+ _0 J5 t! a% b# R: L0 qserver {
# a4 j/ R9 Z. U" }& K- M4 _      listen      80;
+ s' U, {- i4 K9 {6 q! y0 V4 a      server_name 52os.net www.52os.net;/ Q: ~; ^! F3 a0 E" _: s9 g6 O6 \
     
& ^( V7 P, N$ R      location ~ \.php$ {( ~) o' a# L6 {0 k% W1 B
      ModSecurityEnabled on;  0 B1 K8 w& g/ h- |
      ModSecurityConfig modsecurity.conf;
7 D2 O$ ]" C" k. D9 P* [) v+ T1 j$ _. f) t+ L6 E1 f
      root /web/wordpress;
, t$ l. T" O; G" b      index index.php index.html index.htm;
' V0 E' m6 F  m4 z  
% [+ V2 {5 ]2 i6 `) h      fastcgi_pass   127.0.0.1:9000;3 u% e0 P7 e9 n: y
      fastcgi_index  index.php;
% x8 s8 Q8 ?2 ^. W2 m1 D% b1 u1 F6 z      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;( e, u$ O8 m- @0 [6 o: m
      include        fastcgi_params;
. R: c  _- A: l% _& `- T& x      }
& K- r6 }5 w" k/ G  }
2 G& x9 g# [6 t: b& y0 Iupstream负载均衡:& H' N0 ]0 [% ?$ q( a* h
. |( T$ D; l- q1 ^2 h4 U; O5 ?" |
upstream 52os.net {
: c% c; l# L. X: b    server 192.168.1.100:8080;
0 D" a& D; w* w% R    server 192.168.1.101:8080 backup;2 m% S8 z6 i; N* v" p
}+ f* j! W' t1 G6 z- O& q

' @* D9 P2 v0 Z9 k4 s/ dserver {
& s/ _' t' M% `! P5 W$ Blisten 80;$ v3 U4 G2 C( h
server_name 52os.net www.52os.net;8 A. l! x" V0 S* g2 A

) l9 K! P6 M* s, }location / {
+ f6 q& ^  y" J: Z( w1 @    ModSecurityEnabled on;  
! T; Q: e! M' d; B6 X5 f0 r  P    ModSecurityConfig modsecurity.conf;  1 w; I3 u3 h& p: E) m

& \* m4 r+ j8 O! E; a$ _- `" i        proxy_pass http://online;1 u1 B9 z1 Z% V3 S
        proxy_redirect         off;
# i' T* e. B3 p# c$ o        proxy_set_header Host $host;& U! T) O/ \6 a1 j! [# r$ f
        proxy_set_header X-Real-IP $remote_addr;
4 F! R/ `5 y  K; }, t  C        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;, ]  y4 ^4 d% J" Q' G  q
    }' f' q+ i2 T4 a5 F& Z6 J. V; d
}% m9 N; X4 H5 y# O7 q+ ^% i7 D
六.测试$ @3 q5 A% Y! L

9 s# a+ l- q6 v; X我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:% t) k: W7 R+ ^0 M: ?6 j+ E

0 w! [6 P8 h. q# f! K! |7 i! I! i<?php
, x5 V+ Y- p1 J/ ~    phpinfo();   
" y. c" q2 o2 M# ^3 X/ R; r?>5 T' v) i" C- f+ |# U
在浏览器中访问:* Y& D' E- n" Z9 s2 e  G1 F- m

7 @; _  [4 G5 G% t: V+ l0 Ehttp://www.52os.net/phpinfo.php?id=1 正常显示。' i+ X/ K& o4 @/ j
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
9 n0 `2 V! i0 p, g  U$ T7 h3 j' Lhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。" f. _# X% v3 \/ n' }0 q
说明sql注入和xss已经被过滤了% {) U& O. ?( |; [- R' G, K2 S
- @! F/ N! H) K0 U4 f* ~
七、安装过程中排错
# }8 o2 J' g4 w' P2 Y% p& h1 ~( r/ ?* i
5 y' L) e  J5 P( v# n2 \1.缺少APXS会报错( N# j/ C# _1 m" r! a
- ]0 l; t; {0 V* p
configure: looking for Apache module support via DSO through APXS
9 g. x- M$ o. ], Y/ wconfigure: error: couldn't find APXS
! v/ E/ C2 ^4 l2 m  d' Capxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
( h: _% @0 J/ z解决方法:! a: e& ^  I6 l9 Z
* {* q  @- r7 g- |4 i; Z
yum install httpd-devel
: V% F3 r8 r# R0 y; S2.没有pcre
3 D# K3 v2 f! u- X  _- Y  s0 v- C* I* s2 _$ }$ a$ {
configure: *** pcre library not found.
# J# L. ~$ w( ~) jconfigure: error: pcre library is required
4 t9 \( }& ]9 G$ V' ?$ B解决方法:+ a8 ~" R( b$ q7 H% R

$ r! W+ C9 _# ^yum install pcre pcre-devel
1 F& n; x# v# X- k$ e! n3.没有libxml2
3 O9 u6 a! ]  [4 v
+ V- b/ W9 _3 s$ \0 f0 _& Z# c  K0 D% i. n4 j& X) c0 s. L4 V
configure: *** xml library not found.% z$ f# n6 x, K0 l. z. i
configure: error: libxml2 is required: z! D- Z. ~, `' s2 ?
解决方法:
8 o1 m! i4 l& r. M$ y: @
. W9 G; r$ v+ `$ b' U& X2 w# K7 tyum install  libxml2 libxml2-devel
% }9 k- M& u' Q, R/ |3 l: ?4.执行 /opt/tengine/sbin/nginx -m 时有警告, r) k8 I; m! J" P& J

, G& e* k: N7 }4 dTengine version: Tengine/2.1.0 (nginx/1.6.2)
7 B1 i' ^+ {! P5 P) lnginx: [warn] ModSecurity: Loaded APR do not match with compiled!/ l; d% X+ A+ X
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
" @: c, y, e, B0 P4 X8 R' w; ^0 y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.$ M3 l2 g0 C3 [. Z* ?$ {
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"; j/ P* A! k0 b) }: W$ G
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!$ \$ j) Z4 l  n2 O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
' }6 w4 s* _. k  Y, S1 g2 F2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"2 J% @5 \' u3 k; q
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.- P7 r& l0 a5 y' e+ I4 c
解决方法,移除低版本的APR (1.3.9)% \+ T& b2 i, T" B
  o: [# g% _: G7 C
yum remove apr
1 t* O0 \3 Y2 b8 W% J! ]5.Error.log中有: Audit log: Failed to lock global mutex
2 r9 c4 V$ w3 `2 j
' s* x0 R- B" q) f( U2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     , U. R* [% E, y7 J3 Z! Z
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
% a/ t3 `: r% a1 V7 ?4 d3 T解决方法:* P+ K  O. ]0 I; u8 l( Y
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:6 ^( z4 w; x" k9 j6 d9 W

8 p: v/ j4 d5 j4 ySecAuditLogDirMode 0777
% ^8 {6 {) [. J5 sSecAuditLogFileMode 05503 ]2 ?$ u  w  k* ?6 |
SecAuditLogStorageDir /var/log/modsecurity
8 B  z: y2 v. Y5 tSecAuditLogType Concurrent. R5 Q- L% p0 u7 W! U# {. }% {
参考文章:
5 T/ b5 B9 v2 |0 qhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
# j7 `  y2 b' O1 _1 g* |2 Q" Nhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-1-25 17:07 , Processed in 0.047272 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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