找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12861|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
: H/ s5 _0 g4 Q5 {5 O7 q! |+ t. y8 _2 g/ F/ r
一.准备工作- E1 D3 G0 _6 N8 Y

8 G) C! X8 ]3 I  t! {) j, L系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
: P3 X. h) J: U$ k
9 d. T& o" n% \% _, k; Utengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
! a9 \9 T+ ^! [" g3 O/ P/ ~! F; Y* R9 l* t  h9 y! @1 U
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz8 I% u' [( n/ u" R  Z9 d

% n. e9 [1 \" ]* lOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
& L& R* M% Q' Y+ B8 m5 t1 r) Y; C
. ~4 x( L0 B: m依赖关系:
/ W! n0 z9 ~9 k  w' Z' Ftengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
/ {; B. P- e/ }
/ @7 P3 t6 s4 Myum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel6 u  m& Z5 g. b2 l" t+ a1 Z
modsecurty依赖的包:pcre httpd-devel libxml2 apr3 a5 h# d$ _! c3 n' q- m4 x
) o+ v5 M4 B2 i
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel/ o3 F3 O. g  p6 B0 L9 l5 p+ H2 |
二.启用standalone模块并编译  R8 k/ e! \! v
" L( D! R: r" j, A' k
下载modsecurity for nginx 解压,进入解压后目录执行:. m- p& M' A/ u. N) {4 _
. T" e" p  a/ P2 L, Q
./autogen.sh
8 J+ \3 K9 Q/ _! D./configure --enable-standalone-module --disable-mlogc$ _- L/ g# _! L
make 8 F+ y0 V1 f4 X* X
三.nginx添加modsecurity模块
7 p1 ~# k# l; I0 {) K! e- T+ P
& R; N& K- n/ I1 J; {. k/ l6 E在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
/ T! h' P8 `2 |" j+ V2 z1 i, P9 Z/ v# M3 ]
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
: J( k' @6 ?% b: I4 J4 ^6 M7 \make && make install9 [% j! _& o6 A# p3 P( G' F  d
四.添加规则8 e; B4 j2 L4 G5 t6 w
9 P& s5 |% b( N2 X" L: d9 p& D4 A
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。) q' C" O7 t2 @- U. X, e4 T
: C' ^5 |5 p6 e% T5 |+ V& |8 T: Z# }/ c
1.下载OWASP规则:+ O  J7 K0 i+ y; P' b4 H

7 @, ^0 l5 l1 }git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
7 ?8 A3 @- r9 U$ l6 C8 c6 B2 X0 C7 v$ a9 E4 l; t  l
mv owasp-modsecurity-crs /opt/tengine/conf// F8 ^/ V/ k. v5 `1 ^! ~
1 l- o! p" m" ]) s' M; Z/ x
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
/ R" W% I1 S- K8 q# l' j! T) Y* J2.启用OWASP规则:
: p* V: ^, S* @# R) z* J5 k; u4 ?% r9 M9 p
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
6 r( y/ c! ~  |$ `% R+ ?8 n8 ]
2 v2 t7 ], l, ?1 |. b$ f编辑modsecurity.conf 文件,将SecRuleEngine设置为 on. J3 f% S' B9 ?! }

: t# I( a3 _3 x- B* a$ Zowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。' U) j, _, P2 q3 M
0 V- s' b. x% D5 a5 E
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
  b9 G$ \' P, f) b0 ?. t- V% ?Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
6 m2 w! s+ k* g. qInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf9 r& i; n- I; g8 z; e8 p
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf7 V9 _. Q0 h: Q% T
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
" b  [0 E6 {  xInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf& W+ t7 t# s; ?
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf# M; @2 O3 A7 Q# c% r
五.配置nginx
  F1 f5 u7 x' p3 w
0 t" ^8 U/ [0 c3 i+ t0 o在需要启用modsecurity的主机的location下面加入下面两行即可:% w$ h, R/ p5 A: |( W
/ M  j- V9 B4 G8 Z
ModSecurityEnabled on;  
& Z. B/ G7 f* @( M$ Y3 @ModSecurityConfig modsecurity.conf;5 |' I4 S+ F1 c$ r
下面是两个示例配置,php虚拟主机:
! S6 i' _) g8 ]7 X
% @0 w: G9 [; n; Fserver {
+ t" x' {7 ~4 t      listen      80;( H( M7 U5 }: M2 M/ j! F
      server_name 52os.net www.52os.net;$ a0 L# ^, f0 K5 Y
     ! d1 a& E- |. a3 x5 W" J- F  t
      location ~ \.php$ {
! s" c) k# t7 [5 @      ModSecurityEnabled on;  2 P  Q& M0 p' b8 d( f
      ModSecurityConfig modsecurity.conf;
$ T1 Z; G4 e3 P% b6 p* b( C7 l# c% b4 s7 x" l
      root /web/wordpress;
" X9 k' L% ]) \) i6 F0 {2 q7 p      index index.php index.html index.htm;
* q4 z% B: L9 a; L7 g; s9 y  1 I! a9 g: l! W3 h; |
      fastcgi_pass   127.0.0.1:9000;4 S1 e1 p7 f* c% C
      fastcgi_index  index.php;
9 M0 z7 k$ D( y2 J6 P5 H      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;8 z9 X. v! K( ~/ ^5 O
      include        fastcgi_params;6 j( ^7 i, c+ y0 F5 L
      }
( s, t) X4 x# X! K7 R! M  }
9 U$ I6 n# m, _+ f7 x0 nupstream负载均衡:
( h& h: c3 x  j! A* N
! c" |8 p1 `, a$ z. ?1 l8 T8 E, o( gupstream 52os.net {
0 M3 i; q5 p' D$ e    server 192.168.1.100:8080;
! p* U+ w/ g1 D7 v) o% O    server 192.168.1.101:8080 backup;  _/ c: z+ x5 k& B" U
}& D  G; R: E$ U

6 T7 A% e* V4 h% Q6 bserver {  i% i0 H2 |- |' v* x$ {
listen 80;7 P, P  o. U' R2 G
server_name 52os.net www.52os.net;% P9 W% ~3 P7 _2 w6 S
. o9 v, O# q) X6 U" W$ z
location / {
) d* r$ o* p$ d( @1 U# N* b    ModSecurityEnabled on;  2 V' D% l) x5 c: r8 O" M* i
    ModSecurityConfig modsecurity.conf;  
* K% w* }) i" A$ v
, ~5 i5 I: X2 m& D2 F        proxy_pass http://online;
! D6 x% F  n3 k( W6 m7 r        proxy_redirect         off;
, P4 J7 b5 h' F6 l( ^6 [        proxy_set_header Host $host;  U: x! T+ {1 D
        proxy_set_header X-Real-IP $remote_addr;
: e% S5 U( r& @, l5 `2 G        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
+ a3 E) i/ J: h    }. K: @2 ~* T* z- r  n0 E# L1 b
}/ s. I  u1 X1 q" e! P
六.测试% g' N- O" O1 D! I' Z: m

! q  C9 U1 x; V4 j我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:; C9 ]2 P" n  l6 C$ J5 T& F: G
6 W6 ^8 C, L3 j0 \
<?php
: v, q: }8 f1 e- B; n# \8 {    phpinfo();   
+ p5 @9 l1 M+ s& Q  V: r$ {7 ^" T?>
0 `8 y# q9 Q7 s, q) Q- j) @在浏览器中访问:
+ K2 s2 t/ c) s- W4 Z8 N5 G. D8 A/ }9 J. D& Q' b3 S/ M8 n
http://www.52os.net/phpinfo.php?id=1 正常显示。
/ w! |$ s2 n. Z" B9 d4 G" Z( Ghttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
4 q7 g* a. C( }- O) F& G+ Bhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。  k# H6 D! }( C6 G3 E
说明sql注入和xss已经被过滤了( Y3 F  t9 I. R* g+ y

4 e. Y1 C- w+ O3 h2 }七、安装过程中排错
# ^& t4 d; h; E
9 ^! n0 p$ A" d/ G3 {8 t7 g1.缺少APXS会报错
) T, {' v; m( v* C1 |0 V6 W* W  g6 I
) G6 p2 K  \; B5 |8 t( e4 {configure: looking for Apache module support via DSO through APXS
1 {3 W- o+ ]* m: q. \, G6 P' Y3 e+ }configure: error: couldn't find APXS: |" G1 ~/ L  o* ~& P
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。6 E  m0 y" }- Y) i9 b
解决方法:
- a% N" f' l$ Q% {6 N. |3 o$ y
/ u9 z- l& s( B1 W; s' R: \: Nyum install httpd-devel
0 g( X/ g" [6 K" Q; c2.没有pcre
9 q1 h- ~$ ~# Z  a5 G2 Q. x( Q8 X2 M4 `5 a$ {. N' y  R1 u
configure: *** pcre library not found.
7 z. e( {( G8 c+ I* Econfigure: error: pcre library is required+ U5 O, C; {  ?  o
解决方法:* [- G) S* e& T0 z
  p7 G( B) l  j0 ?1 i' t" R! A8 b
yum install pcre pcre-devel6 Z1 X4 P* T. Q# B( @; P
3.没有libxml2! K* ^. b5 r5 Z. |/ j7 {2 v

9 A, E) E- s  y% b1 I! C) X9 O& c$ l5 B1 l: \1 l
configure: *** xml library not found.
+ A5 I0 e& |8 j* f2 E; Iconfigure: error: libxml2 is required
: q# X, {& j: R7 d/ ^1 I解决方法:
1 T3 k) l/ k! a/ n' u* t, C0 i
7 I% h" ?$ S0 F6 p8 i/ m0 Myum install  libxml2 libxml2-devel
4 g% s1 _$ {0 l2 K5 N4.执行 /opt/tengine/sbin/nginx -m 时有警告7 T0 R) |2 @. o# T- D! l. b

: p% x  V0 ?( j; ]: NTengine version: Tengine/2.1.0 (nginx/1.6.2)
9 l4 P7 `9 i& G% T+ ?nginx: [warn] ModSecurity: Loaded APR do not match with compiled!% b1 T# o$ Z: |" Q2 X: H
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log: i2 y. T8 G2 v! N5 E# x

' j4 I  m1 `$ G/ N2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.- g$ `% U# c8 q2 }/ v+ v
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
6 Z; d+ [% T  x+ ~& I/ z2 X  n2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!: D5 l4 z7 {5 ?! `  a
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
2 ~( ^: ~% F& M9 S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
( _) P' J9 ?5 B2 V+ i2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
/ e( [, V: Y0 s3 Y解决方法,移除低版本的APR (1.3.9)9 E! \8 d% Q3 k4 \
/ }/ @# ?4 p" V+ K( j1 y
yum remove apr# x. q' {9 W) T, B9 x. H, W
5.Error.log中有: Audit log: Failed to lock global mutex: d/ ?% c# f4 \+ ~8 w0 v- k3 D4 \  ~
+ Z/ m, X  v* f9 Q
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     8 _; T9 z. N9 a# ^4 Z: k
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
9 h  Y7 B$ D9 B解决方法:. l6 Q& L5 X) s9 F
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:$ b' Z: q) j' w3 t- j0 |

) k/ k. T" W" X% w. L1 GSecAuditLogDirMode 0777: X& E( n* J7 ]6 V/ M! t# ]/ a% m. M
SecAuditLogFileMode 0550  {$ w& i) {* @5 `! P7 m2 s
SecAuditLogStorageDir /var/log/modsecurity
+ a7 `1 |  v, dSecAuditLogType Concurrent
- Q! i$ M# l1 v( B  F2 k- |7 S7 e参考文章:
  O7 b' r! g1 G/ o& ?2 |4 Shttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
! f7 A" l; ^( Nhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-19 16:04 , Processed in 0.084589 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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