找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13103|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。. D  d+ l. i( }, I, n1 O

) ^  W& \2 X# F2 |/ z* R+ S一.准备工作, h8 [+ l+ p0 e- ]1 E6 F
# R' K" g( s3 X% `7 p
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
+ g/ t3 x# x) @2 }! L
9 |" a2 O; |8 {- N" `- Qtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz+ Z  x6 A- `* a1 n* ^3 V
8 d* ~+ I. u/ T1 o
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
0 |2 O5 I$ g9 Q( [4 i0 E" p- m5 j& _  X8 ^- R# k) x
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
/ ?, x) o; e: V4 s" R( f& ^: l
% F8 m0 U7 I2 o& D; ]" M5 U0 x依赖关系:
6 y1 H2 x2 T9 A; `8 l+ otengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:' M9 R- H  v' W) ?; _' ]8 _& k: Y) y
' c* Q& ~3 ~8 V) L9 q
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
% C& F) o* x) q' B# L( h; q: L5 J: l! M$ Smodsecurty依赖的包:pcre httpd-devel libxml2 apr1 t* k, K" {# B0 B

/ |* i' k6 R: J; S+ E" myum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
$ b5 v) a5 P, V) Y+ @二.启用standalone模块并编译
' G4 Q6 @; T! H8 P6 s% D" |
6 m, W& t8 R9 `- g下载modsecurity for nginx 解压,进入解压后目录执行:+ a0 m/ L  ?" M; _2 Y
. k" K" T/ l: O' `( \
./autogen.sh! r% U7 u9 x& F# N
./configure --enable-standalone-module --disable-mlogc
; [8 ?4 C  K) I. f5 @make
9 Z; Z7 D  B3 A三.nginx添加modsecurity模块  ]# g. g  A9 `
  O( k/ l$ R. X7 |1 ?9 P/ B8 g
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
7 K; t7 s9 ?) T* \% v( Q5 J: _
8 S" Q2 n: O7 {$ E0 |, {./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
9 w0 R7 Z( R( |9 j1 U- r4 Hmake && make install# ~; M) S+ o& q
四.添加规则
' {/ q6 N7 A9 s! h' }" A
5 C( {/ o/ v' D% x, y4 G! }/ fmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。8 c5 F4 g! v" u% z$ Y  j- d3 T

4 _8 h4 F# H4 r7 P- G9 u1.下载OWASP规则:
" ^4 @$ h' T/ G. Q% ]+ o* Z7 f  q) W0 G5 f" l" Z
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
4 E  J3 Y+ [9 g- r% g1 U0 b- ?! d* e1 x  [: B
mv owasp-modsecurity-crs /opt/tengine/conf/" G7 G/ V: U" H3 U
) v- P; F3 d  Q3 \4 a$ l
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf# `2 [* B6 j# K) {$ M" R
2.启用OWASP规则:
9 m1 _6 d) o0 d
; I5 Q/ _1 a5 V4 Q0 l) `复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。* E/ z! H1 A; h" g9 r* V
! \) t3 E; l+ M/ [- H& O) G4 S
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
. B0 u$ p* L: z* ~3 F5 s! b/ v* M6 q% Z+ d% i0 _- v3 Z
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
' Z4 Q7 m3 J" \1 N4 z/ f3 M0 Q. e, e0 t" x5 [5 F5 E
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf( b. ^6 H& I% x& [5 n# H  }
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
' @7 E/ Q( O* vInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
, Q, j2 T9 n+ P) dInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
; r  P7 i5 l* M: g- |7 h! KInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
7 y7 A+ s% C% a9 BInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
. `, a& Q4 q5 G: a/ X% ~Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf' u" j1 S7 y+ s  W  Q! J
五.配置nginx
5 Y, N9 m8 R4 M2 [
- y) b! C) W& s$ y- k在需要启用modsecurity的主机的location下面加入下面两行即可:
0 o4 ^; e; Q/ O7 _3 P0 m* U1 N0 w+ ?+ T' t" Y3 i( Z' q/ z
ModSecurityEnabled on;  : Z8 p. u  J' r) j
ModSecurityConfig modsecurity.conf;5 ~/ }+ I( |! [: {4 H" M, o- s
下面是两个示例配置,php虚拟主机:  D* C, R$ `0 v. b. [* h

/ A% P# S/ ?* n9 Userver {
8 M  \/ I3 J! P. W      listen      80;1 l# |" y+ k8 ^' V9 \* }; Q
      server_name 52os.net www.52os.net;! t. g$ q: g( H& _) T( O. C& H  B
     
4 a7 |! c3 h1 f" V; E+ g3 O      location ~ \.php$ {
* B. v6 P& U/ u; a" @1 \/ n      ModSecurityEnabled on;  
$ |3 ?9 {1 l9 N      ModSecurityConfig modsecurity.conf;
. e) t7 X9 R5 z; R$ Q4 @6 ^, E4 O9 K/ r
      root /web/wordpress;
2 ?7 J7 o, S, E/ _* q      index index.php index.html index.htm;
) g; B  L( i: i6 u& `+ o6 b  ; N5 m6 N2 h# A; q7 G* D5 G
      fastcgi_pass   127.0.0.1:9000;# I) Y" e" V& b$ Y1 J! V
      fastcgi_index  index.php;
$ H( K# k5 K- L      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
' d# X; d$ o7 ^0 a& T      include        fastcgi_params;' j8 A8 ?- ~8 P3 U
      }
! E& r7 A6 |3 @  }2 f# U& L- G/ @3 ~* ^
upstream负载均衡:$ f4 }- O7 q/ u0 w
  W+ _: G' Y2 J  r8 X" H& L$ S
upstream 52os.net {% u7 Z6 j; J. F3 B, I. Q$ K
    server 192.168.1.100:8080;* ^8 B; q+ \. x0 v5 h* P
    server 192.168.1.101:8080 backup;
! T  Q' Y3 C% o: B: W6 G3 T: b1 E}
' D) P/ g& O, k* w& y6 H" H+ X( C
0 L" W  J3 o# d6 R3 r9 {server {9 F  `9 c8 q+ v
listen 80;- r2 D/ b/ G! z# Y. G& T( e2 F
server_name 52os.net www.52os.net;
& e, ?- C  |; Y3 T! s4 m
  {* P' a% N# C* e+ Q8 ]7 `' Klocation / {
" ~; y$ \3 m+ o1 t! V( a    ModSecurityEnabled on;  1 ^/ ]; |' g/ H+ N+ |
    ModSecurityConfig modsecurity.conf;  
; Q0 y  d# U/ P4 o: p9 P- G9 S, I' l- h, c9 X
        proxy_pass http://online;
4 Z$ c; O- x# C7 i0 _# X# c        proxy_redirect         off;7 v' s5 ^! L, J8 i3 J" K
        proxy_set_header Host $host;$ {1 Z& M1 I- B0 f8 b8 i
        proxy_set_header X-Real-IP $remote_addr;
5 F; o( S0 ~- R: l' w        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;9 ~$ {4 L. l: o6 A
    }
+ P6 h' A" `1 Z: F4 p, G}
5 \& l* Q- c( V1 R六.测试
/ K7 k7 Q  g: `) a9 ^3 j. f& o8 h1 m" ~7 ]7 k
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:7 t+ @2 J6 e4 V3 y& x: G6 }- M
4 t# H. Q0 B( g9 W6 q4 r) y# W
<?php4 m2 J( r/ y1 ?% f
    phpinfo();   
7 S; u& G$ L8 R/ _* I?># i4 Y  n$ T) n
在浏览器中访问:
1 n4 I; U6 M  y0 I6 v( C# B  J1 m  A
http://www.52os.net/phpinfo.php?id=1 正常显示。
# o; Z% R! z5 o2 Q  h" Khttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
$ y$ T, i& e3 a/ q" Mhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
5 b, V. B; n" \1 t- B% D说明sql注入和xss已经被过滤了
% h4 L/ q' t, ?2 \& j7 j: Q) F5 Y* t1 P- t3 q0 k: ^
七、安装过程中排错
: }' d' o: V; j& q' r* X
* V7 U) a. Q0 Z- C2 g# ~' j1.缺少APXS会报错) \+ A! i$ _/ M' J3 x

5 [# h  u# C9 A: t7 e$ I/ uconfigure: looking for Apache module support via DSO through APXS0 x$ M! {  u3 I* i
configure: error: couldn't find APXS
& c' k1 k- e, u2 y& l8 O& a5 {apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
6 ]+ a3 i/ ~% U解决方法:
+ U/ s( h9 c( n1 R9 q' S
4 V3 ]. f+ n9 x4 M8 ?yum install httpd-devel  U) Z( [4 m4 p  Y* v
2.没有pcre
& ]: _3 C  B4 Q% D$ y7 N; Y8 n6 O- l2 v7 X: _
configure: *** pcre library not found.. a1 E+ p7 w4 P  K/ S+ x
configure: error: pcre library is required. H, U& _( B( S( S0 h" A) \
解决方法:) [* v  Z$ o6 |

3 U: v6 N- R/ y* Z9 nyum install pcre pcre-devel
6 F0 B3 j2 |: Z! k* E) C3.没有libxml2( y- M! ~# W1 q- N, k8 p+ Q8 ~# \5 V- t

& R" T, k7 |+ W+ d8 Y- H* v3 q' j4 l1 ~/ m
configure: *** xml library not found.: g# s5 r3 i( p
configure: error: libxml2 is required
) ]3 e: A7 o* a' U+ S/ M4 N解决方法:" R! _( D* ]5 J, {% I( N% a; w" E

1 p3 i) b1 \/ J2 w" [8 l$ Pyum install  libxml2 libxml2-devel' \! z: d/ [( H& E+ E& Y6 [5 S6 ]
4.执行 /opt/tengine/sbin/nginx -m 时有警告" L, @8 Y3 T7 t. n  e; x
8 r! O8 g& g6 ^8 |5 g
Tengine version: Tengine/2.1.0 (nginx/1.6.2)3 [0 d1 d8 [# C9 y( b, r4 n
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
9 F: r0 A$ q2 a原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log0 r: O0 N: x( E; @4 ?; ^; }
1 p0 t, ~- [! M0 d( R4 I9 T' d
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.: d- Y  G( R( y0 A+ D7 o* [
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
9 m# X- d# z7 {. O2 }2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
8 D7 o0 ?6 X6 a8 N6 N8 S' k' S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
& I( y' l" E' i# q0 u2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
  e( ]* H$ Y& |2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.* F1 ?0 E2 c- a2 Z4 ^- p. G
解决方法,移除低版本的APR (1.3.9). D" b3 D, _' }8 v$ q0 y' S5 e

. u4 ~. G* l' Yyum remove apr
' X0 j7 G. B; H4 ^3 f% R& J5.Error.log中有: Audit log: Failed to lock global mutex
1 ^- d+ o0 V3 P! I1 ~2 d& F9 U) f  Y% C5 O9 e
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
6 n8 |0 |4 L" r$ Pglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]1 G* T, E. O7 d1 q4 f$ c. b( H
解决方法:
3 K6 F3 F8 J" Z6 l编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
! V2 f. X' p3 Q; `/ G1 g$ s; d) C
SecAuditLogDirMode 0777, Q) L" K' S, s, d1 w
SecAuditLogFileMode 0550
/ T, L" s. W9 _* m. Q& RSecAuditLogStorageDir /var/log/modsecurity# d9 n3 f# Z# n* t+ o1 v4 I
SecAuditLogType Concurrent
5 I7 _, e, \+ a9 K0 \/ U' r8 Q# K参考文章:
7 [7 M) d& j3 V- d( Nhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX" o0 B' p8 \) J( O( X" }
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-15 01:37 , Processed in 0.054448 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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