找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9198|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
% ]' m  m$ K2 J/ p) [- L' H* B6 O/ S( ~& U: M. }
一.准备工作7 B- {* I4 B* ~, B$ b- r5 A9 V2 ~1 w
7 }+ T% k% B8 t! s0 f1 L7 o- s
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
5 |8 F+ w8 {, x! r
" ]& w1 J- j2 d+ D# |- stengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz' {, z5 Y, _7 e; Y* ~9 ]
/ g. e/ k( l9 d; ~
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
8 {) K6 M# `# R9 a2 r& r6 ~# h2 V1 }  [9 P7 j" u
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
: z" O, K; r1 ^
3 e4 P! X0 H8 w% w依赖关系:
4 v$ K8 r7 A/ Y3 |0 ktengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:* P* q( K6 `# p) e9 E  W4 R8 M
7 x4 a: s8 e" H- C0 {5 V
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel) q# P7 s! K+ z; _
modsecurty依赖的包:pcre httpd-devel libxml2 apr
: R+ v! U5 [4 q. m% X1 k9 E8 _5 m$ |- s0 }" m+ L* H3 W
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel  \' x% h! C4 s+ ?8 W6 v; M! q
二.启用standalone模块并编译
+ I. P0 N7 }  r1 X! L4 v0 _
% O8 j5 V( ^: o! y+ \( N下载modsecurity for nginx 解压,进入解压后目录执行:) i% k1 {' [4 W8 [5 U8 L' A* Q

. K8 L9 q2 W7 m' x/ r./autogen.sh0 F% Z) m; u- i. _) y# M2 c- ?
./configure --enable-standalone-module --disable-mlogc
& ~* U5 Z1 L0 Bmake
9 H5 z$ ^. M; q4 z4 y三.nginx添加modsecurity模块; l' n" H9 W0 I3 g+ g7 N2 N

$ S# X3 y5 `- a在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:- o! j6 c1 f5 A4 a

+ Y- U& ^- v9 U0 p6 T- O./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine. f* A) h1 t" x: A6 _
make && make install. s5 w% T; T$ v! j, ]. c7 }* f
四.添加规则1 d7 G0 E" r) H. V( c% g6 T$ z
- L7 n" u3 q7 a- D; E4 ?  x
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
7 J9 h/ i8 K: t6 E2 R& V# h0 @# X4 M( ^$ ?  B, H* G
1.下载OWASP规则:/ v% q2 k. A2 `2 B+ i* U; F( R

8 U2 L) y/ h4 \1 N# S* ^$ J0 _git clone https://github.com/SpiderLabs/owasp-modsecurity-crs  i9 i* U1 D5 X6 Q+ M4 U2 O, I) v
: p: w" N6 U& e6 \$ `' e% O
mv owasp-modsecurity-crs /opt/tengine/conf/. f1 j$ z) a$ s

7 ]# T! X# s; N  s) ^) C2 Gcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf9 F9 g7 |. P8 S' y9 Z) c
2.启用OWASP规则:: f/ w3 q7 K' x1 ^5 C2 w
" J6 m8 j  a" s+ Z. ~
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
3 `! e4 h7 R: K+ k. _( A0 i+ c% s' t2 H5 `" B
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on. m% c$ K, f% t: g
+ d+ N  G$ ~1 R5 y  I, l: w
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
! N. S  V) Q* i- G: D0 a- p: \" c) A2 |  m9 Q6 V5 ?
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf7 ~& S* O1 \+ z" o' @, F/ `) K
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
8 M6 S# b3 H! bInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
3 S0 Q! e, V  QInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
8 e/ c0 Q' I# ~) ~0 E7 VInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf! A. u+ V- e8 ?7 X: K0 B  T9 e
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
+ J3 o/ }7 e: {# NInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf. @! L$ j0 x( V. G+ N, ~
五.配置nginx
* ~) U1 `; f9 o4 U% _" `( d
! q. l1 A3 S! q( J. q* \在需要启用modsecurity的主机的location下面加入下面两行即可:/ r* Y3 E- J* h; ], ?# [% L

- P" i# @  F6 u" @ModSecurityEnabled on;  3 U. M1 U3 V' Q. [3 |
ModSecurityConfig modsecurity.conf;
; e) G8 v! f) i) g+ C1 n' {下面是两个示例配置,php虚拟主机:
# Z/ f$ T1 S" @8 l1 p8 T9 k6 u- D
1 W% o2 H3 @( W- Z. u  \3 Iserver {. l2 J4 m* ]6 F, u( I& @( s
      listen      80;
1 V; u8 V7 N; i  U      server_name 52os.net www.52os.net;
8 t5 m& Z5 L: T& ^: R     9 H6 }; n! q6 ?5 Y  Z, u
      location ~ \.php$ {+ a4 V$ O2 G5 v  R" C
      ModSecurityEnabled on;  $ M2 f: G, G5 v% D0 L2 L5 E
      ModSecurityConfig modsecurity.conf;2 j- D- G% F8 f- M& x% s

. q' Z$ r1 l: t* @      root /web/wordpress;4 w+ ]6 y7 X' r$ s! u( i
      index index.php index.html index.htm;6 z5 c* Y1 K* ^- S. n$ S4 D) B
    E/ i/ N7 K. m5 C% v
      fastcgi_pass   127.0.0.1:9000;
- F* d9 y; s$ L. V! t5 D      fastcgi_index  index.php;" T' A$ [; P7 t1 I
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;9 H  D, w2 Z9 L9 k
      include        fastcgi_params;  U* ^" Q0 T6 R) K6 @
      }& q8 g8 d* J* ?$ q& \$ i8 K: N
  }
% b& O5 x+ _- T3 Cupstream负载均衡:
; ?* k3 o. |' B% _! t  v# C
* r! C& w9 A2 k  m: `1 Cupstream 52os.net {/ H3 h$ }) ~+ M
    server 192.168.1.100:8080;+ n& w) w9 p9 J% K' ]( O
    server 192.168.1.101:8080 backup;
5 F) G0 u) \) Z0 y5 [}% V9 g6 H# g+ ]" g
/ s; Q# _# |5 [6 ^# ~% c
server {
  F' U* b( b+ J& Nlisten 80;/ ~9 Y5 A6 C: e9 O, W% y( [3 u
server_name 52os.net www.52os.net;
0 C% ^" d6 ~4 N9 l/ J" m; g+ S) ~- o1 N
location / {
4 _2 A; q5 G' f3 I3 b# ?! L    ModSecurityEnabled on;  2 t$ e4 j  q* \! @+ v5 q
    ModSecurityConfig modsecurity.conf;  # T* F8 _, V) x0 s% k5 {( Q8 e8 Q

( s( y; n. W# M( E: z        proxy_pass http://online;
9 m9 _) g# |2 [9 v        proxy_redirect         off;$ Z/ ?0 \$ q7 d" d- G/ @/ E
        proxy_set_header Host $host;
$ f9 E/ {" @1 R5 |        proxy_set_header X-Real-IP $remote_addr;; c( h7 C  e( s' H7 e5 H+ |
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
5 E5 o" k) v( i$ d+ u8 y    }
! C- v( e  ~2 }, W; _- Y}
/ N" W3 H! N2 n, O  X% o0 H六.测试. R9 ~+ o) H$ d

$ S! P& ?5 a, D我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
; n7 G7 [! F0 _; u8 _& f7 s+ F) _5 j. G- J
<?php& u) ^/ M( x" M+ L  A
    phpinfo();    0 s7 d. v& j; U& u1 H
?>& ^( V/ b" Y# B
在浏览器中访问:
! M; ^( s) C5 d# C* P' r5 b
& n+ Z9 v& r( ^' }* C7 Vhttp://www.52os.net/phpinfo.php?id=1 正常显示。/ l/ W1 S: J% I0 T" {4 F& Y" ]
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。7 A  U) v8 S2 W1 W: W
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。3 R- e7 C( W# s) H( M
说明sql注入和xss已经被过滤了8 v; T) K, x7 k6 N3 q2 }' q5 J# O

1 x7 F' v- F/ X  i2 V& z6 p& j七、安装过程中排错( t+ i' I$ b( C' i+ b

: `% {# v* I5 k, Z* ~1.缺少APXS会报错, h2 H8 d& h( S. Q/ ?6 l+ Q

9 K; l( ]) z( a! s& ]0 Z  qconfigure: looking for Apache module support via DSO through APXS
* L+ U0 w( @. W  e1 u& yconfigure: error: couldn't find APXS( n8 y$ J$ X! \1 k# F# o2 k
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
5 C3 t0 g* K' R* X3 y5 T0 f5 l解决方法:
! d0 B" K4 [: R& _& \
2 w) C( f' p3 ?( M* R8 _yum install httpd-devel
8 @; g% A7 @" v  m( I+ {9 D2.没有pcre
6 x1 u) W& g/ [9 t
& t$ G( p( f% r9 Z3 G+ |configure: *** pcre library not found.& l; {9 f: b! P  O5 X( A* [
configure: error: pcre library is required
9 f7 G# q3 a. k/ B! o3 @解决方法:4 U8 g+ `+ w: O; U0 _3 {

" q: e: C7 X! R: \) x! Pyum install pcre pcre-devel
+ b* L: x! s: ^9 D  T( o9 }3.没有libxml23 C: s" I* d. H' Y4 R

5 `% Q+ z. P9 V  \7 V1 s
9 G7 d5 y( U* T: Iconfigure: *** xml library not found.; [: T3 ^9 o8 e3 l' v
configure: error: libxml2 is required# w" [+ @( j1 u" A; H
解决方法:
: a6 k: P+ R6 A/ L
! C) W1 Y' f) Uyum install  libxml2 libxml2-devel
4 Y* I% f; K; h- o0 L$ t4.执行 /opt/tengine/sbin/nginx -m 时有警告
* G$ q/ S) ?1 h5 O% s+ r* [! s% L: n& T9 n9 Z! O& `
Tengine version: Tengine/2.1.0 (nginx/1.6.2)% A% V" G) ]- C" J8 g8 Z/ q4 x
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
2 U& Z! d3 t; S原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
( \6 [* B# b! w. R
) S& y# r4 }7 o9 \7 J* I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured." G0 B( }3 ?& i; @, s% w
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
- d& ?+ _! [) E, v+ P" d" L$ y, c2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!' y. Z  j, m* o0 k6 {3 X4 J
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"6 b. p7 B0 A, o( u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
5 j# C' ~5 X) c7 y, n: D& h& l2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.8 g. t3 `5 F. n
解决方法,移除低版本的APR (1.3.9)
2 B* i  o+ \' L7 ^( @3 i" b$ i6 x6 B9 z
! G# J. ~- q: w8 T) s6 R8 i* Cyum remove apr
; G7 ?/ W  o% w7 S( ^5 ^+ f; W5.Error.log中有: Audit log: Failed to lock global mutex
) P" x) F' \. J  D0 h  h, L( C% a. }2 N! ^/ K
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
7 Y# R3 G5 |( \2 a. j4 Bglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
3 p+ q' U2 L: X6 b# J- U* V" x解决方法:7 q( ~4 e6 J% k" D
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:7 ^$ l: U! O3 f& B. U4 C  j
/ F' Q+ S  f0 T, I4 v
SecAuditLogDirMode 0777
; h$ l/ O' p* T, r* ASecAuditLogFileMode 05500 P. b9 V1 V- h. x" }3 U( B
SecAuditLogStorageDir /var/log/modsecurity
* `6 Y# i4 w; x9 pSecAuditLogType Concurrent) D5 V" }, c( [1 u
参考文章:
/ f& K! u0 }5 V2 `8 phttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX; K9 N( }6 M) O0 u( O0 ?
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-11-24 17:36 , Processed in 0.065378 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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