找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9662|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
9 X6 V+ G  j, x- I/ t' d/ F; {& e
一.准备工作
$ C3 Z4 `9 r$ Z" b# o6 Z* p% w0 O8 x% n1 I; W$ x6 C) b
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
0 f/ L8 \4 G6 A( f$ y* n) [7 p; \3 v7 B7 n+ ^4 D, J* |
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz0 Z+ [3 M) k! a8 f2 L* U' t
0 r) b4 x' ]0 C' V: t
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
9 t6 p- |& ^  w  n4 e# C0 U' @, D) v8 b* B# \& H
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs  S' k% F$ s8 I. I1 m
1 J" ~+ M% i- s. I. T( O! [; ?
依赖关系:
7 {# J) @3 P1 e+ A/ @tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:( b) H/ F4 S+ w

/ I) Z* A$ F& W4 e3 Q4 |9 \) r, \) ~9 Tyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
; _) o0 h6 z- p+ }4 x: |4 C8 V1 }modsecurty依赖的包:pcre httpd-devel libxml2 apr4 B) |- g: z9 l# |5 T0 q0 x/ K

/ U+ \2 z8 ~5 \  B7 |( tyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel7 j+ m3 X2 n* R/ w6 `4 ~
二.启用standalone模块并编译! K2 i! O8 _8 `+ ?4 H* S

7 r/ A7 v# x9 S. U下载modsecurity for nginx 解压,进入解压后目录执行:
+ e1 S7 m% `: y0 `: o( C7 _" F, ~" z
./autogen.sh
2 o0 `2 `% A5 ]4 k- L& L./configure --enable-standalone-module --disable-mlogc
; ]7 v# Z: ^: Nmake ) k0 J9 V$ X; P4 S: i
三.nginx添加modsecurity模块
& T1 i+ y  t+ b8 @9 W0 O5 G0 v7 @
. Z* d* b) c- k3 i: n/ x在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:! U4 K0 ?: D8 ?) g7 v

' X2 S7 x- s. `, W+ i$ a" n8 b./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
" q0 o5 e; y9 x: Rmake && make install1 `( U4 s0 E9 f1 O! d
四.添加规则  t' r- v, t4 r" @( _0 U; {

( M4 O$ w6 W! o: c9 S+ t; n+ smodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
+ y7 H7 K9 B% J5 X
7 X/ D3 K& E( h: q" @, s1.下载OWASP规则:
! `0 J7 b( x4 |5 [  |5 G' ^9 u/ ]1 V+ G6 k! b* }
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ u1 g( J" g- C6 r& X8 h4 R( v0 f+ D) Q" `0 N
mv owasp-modsecurity-crs /opt/tengine/conf/# o1 @: ]$ h0 f4 l
3 q7 |+ K. G$ {9 v& ?
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf" i/ l5 G, v1 g* Y
2.启用OWASP规则:
1 O5 u1 l7 N* B4 S( M
/ Z1 ?! _, c5 m. K+ p/ L复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
) j* [* E4 m4 H' y, H
( m9 q; M. l! [8 u8 z, ?编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
& L# A9 D) M4 _5 |3 v! [* n, h" @3 Q# b, o) F; b0 `) o
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
' x. q( @( S" [0 F' g" R3 v# W- b4 W! _5 s' R$ O8 g
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
0 N" ~/ G0 j8 X  U1 g3 CInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf- x+ _" Q; q  W3 E
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
0 p( f9 U3 k9 @, _& [Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
2 o6 H" g3 M2 y; B( r5 eInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf) s, \$ o# u9 t+ U, N4 \
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf* d4 o7 p6 m9 t' z/ O
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf/ G9 G6 S: `& C2 E
五.配置nginx1 f- e" r! Z6 u& U% Z* _4 Z: |3 Q0 w

" ^( v* A0 ~, v; n! @# g. p3 v在需要启用modsecurity的主机的location下面加入下面两行即可:2 Z' V$ e' D2 g4 [  s

" _: {- O2 e! l+ _) uModSecurityEnabled on;  ) y; ~( J& t; t* N+ J
ModSecurityConfig modsecurity.conf;
5 }* Q8 O5 J8 z; t: u  c1 \7 L下面是两个示例配置,php虚拟主机:
3 R+ s( d2 i8 l& e8 R9 A7 c+ p3 C
) w4 U/ j) z* j! lserver {% R" `4 Z# P. n$ n( \: C
      listen      80;
" D: ]0 s2 v* g3 J% \0 h      server_name 52os.net www.52os.net;
3 q1 j- x, ~8 I1 M/ q     
; W) T- [- G6 ?5 _      location ~ \.php$ {: T( o- m- a- A, @3 S
      ModSecurityEnabled on;  
( A; D: K3 v9 g, i5 j  Y) V) X      ModSecurityConfig modsecurity.conf;
( x) t7 Q# L+ x2 `: K: B1 K5 c0 y* B" b7 `
      root /web/wordpress;  {3 z; P( L$ H' {
      index index.php index.html index.htm;
1 T1 C+ ~; |& x6 ]/ t( E  ) C/ x, }9 h9 ~, T
      fastcgi_pass   127.0.0.1:9000;
9 l+ ]& w+ Y: F      fastcgi_index  index.php;
* V$ e7 C& Q& }0 K      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;0 e9 x8 l& {' c2 k, n! Y  J
      include        fastcgi_params;" }+ z- m7 T! S' p" m& u& }) n
      }2 F3 j& w- `0 g) u& T6 F
  }
3 X, q* s7 i# Dupstream负载均衡:* ~: |6 N* Q9 X; d* T, l1 L
7 a  Y0 q. a7 w4 j2 D5 [: q
upstream 52os.net {3 G3 x$ V/ x- p- Z$ ]# j, g
    server 192.168.1.100:8080;: Q3 k6 M2 j* d
    server 192.168.1.101:8080 backup;5 y, S$ D5 B# D
}4 F- P% [" N" r8 z. T

* c: U/ t3 i# Hserver {
; t) o. z* I) C: [6 ilisten 80;- h% B6 T% G6 @' }% ?. D! Q
server_name 52os.net www.52os.net;
# ~# Z0 V& |& a. ?! a
/ B" M5 P! c5 l( z$ Ylocation / {
1 O# X7 Z5 D6 W6 M& G    ModSecurityEnabled on;  * o# }: q  Y$ r  Y% V1 _
    ModSecurityConfig modsecurity.conf;  8 U9 r) ]8 o$ I5 X! A( V

$ r& Z2 i  c5 f. r" A        proxy_pass http://online;
) J% c, `" s) X2 j8 `        proxy_redirect         off;& |) C2 c  v* t
        proxy_set_header Host $host;
) J* I; Z$ G  K; ^        proxy_set_header X-Real-IP $remote_addr;
- g$ i1 A1 y" I( S8 z2 O        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;9 x0 i: q, m6 j8 W; m
    }
/ u+ U8 E5 v4 }! h}) V8 [* A0 I- V# B( a7 H5 J% ^# K5 ~( b
六.测试
% @! j: ]# l. k! C
- a( f: N$ r( f9 K% K/ n我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:3 e8 C$ O* b8 U6 M

7 D; N6 v+ n8 y: Z7 s<?php
# F1 m8 ~  L- s9 W5 d    phpinfo();    9 L" E0 d% Y. `$ v8 Z
?>: T/ f, B1 k9 H. ?$ s/ R# i
在浏览器中访问:
' G$ T+ i! Y7 J" ^! m0 X& D. ], _
http://www.52os.net/phpinfo.php?id=1 正常显示。+ Y3 M3 U& n: n, N! y, Q
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
" H6 E% E0 @- }0 J' Q9 F5 t0 uhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
- _( B# g8 g  X  R说明sql注入和xss已经被过滤了
) F/ }6 h. L( U/ T
2 }7 U) V" ]- }" h$ h7 U/ T七、安装过程中排错5 o* |, Y4 R# K7 K- w" j
/ F: E" j. Z& n
1.缺少APXS会报错
7 r5 V) V( i; l' T
2 [0 }* ]1 N/ S& I) \" z) k' wconfigure: looking for Apache module support via DSO through APXS: u; O- l  M' @' ]
configure: error: couldn't find APXS
2 n. n; ^6 k( P8 }) Z: f  Q% o% h  k: Zapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
4 ^2 @0 h, ~) J6 q5 _- G解决方法:
3 O( R+ U# U4 }4 l/ J" S5 S6 }% F  b: A0 l  w  u6 I' [2 J  L8 l" h
yum install httpd-devel6 X! O, z9 M9 t# `
2.没有pcre
1 u1 ^9 [, O/ K3 |9 G0 `& N8 C3 J9 p  f: x1 {6 D
configure: *** pcre library not found.
" y" e3 Q$ y0 W5 G5 v" T3 B- s$ a0 econfigure: error: pcre library is required/ \5 {; W' c2 j/ L, B4 e1 X& a
解决方法:: }# C$ d, w! |  T% b

( a$ Z: N0 r' F+ A, vyum install pcre pcre-devel1 t- Z7 i  y5 F. `5 `0 S$ j
3.没有libxml2
& X; N) l9 D. i9 x& `% \
5 H0 f+ w/ y1 u2 m' W" V
5 t7 [. I: b# y7 g; e# z& c) tconfigure: *** xml library not found.1 E: t8 |( u& |1 W" }- b9 I
configure: error: libxml2 is required$ }0 {* p# `" n: [& G2 N9 i+ ~" |
解决方法:- I8 V7 H4 X; w
: Y. G% F5 m$ z6 l# c/ ?1 k+ k& {
yum install  libxml2 libxml2-devel3 [3 @/ r3 E' ~; Y: f4 `
4.执行 /opt/tengine/sbin/nginx -m 时有警告
# O' f8 @% N$ ^9 b0 n3 \) O+ c' z- J6 ~% n+ N1 ]; o
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
# m' O# `" i7 u- O. hnginx: [warn] ModSecurity: Loaded APR do not match with compiled!5 J6 W* N8 K. v+ X: K1 s4 p5 x8 z
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
3 c; e) p! h7 J
/ T/ G/ p. a; o8 e  h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.' `2 ]9 y8 V. p; ?5 f; F9 |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
6 I% {+ a: l) M5 n! F2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!, K% d) W- d& Q; t1 b* |7 `
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
6 ]# f1 a" \2 j  u. F  W1 J2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
; u0 ?2 f9 u6 e2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.( m1 t. N5 n2 p* v# l4 W
解决方法,移除低版本的APR (1.3.9)
. n7 j8 S! {$ a; {+ J
  M( w9 ?# s* U; yyum remove apr' ^$ O. y4 Q! R
5.Error.log中有: Audit log: Failed to lock global mutex. S7 l7 }% ?7 O' O( v6 g

6 o7 L5 y1 B$ R; }$ g2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
. @  v+ G1 W5 s6 fglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
0 P0 r5 t/ ]  W& _解决方法:1 k+ i# ^% m$ A' A. {
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:, e1 ?* Y' o; Y  X

! E/ x7 p# U: p5 LSecAuditLogDirMode 0777' I; S# S5 c: g9 C  F4 _/ x& U
SecAuditLogFileMode 0550( B# V. P( a: Z8 {4 t- [3 x# I
SecAuditLogStorageDir /var/log/modsecurity
; ]5 {9 K- l) k/ l- USecAuditLogType Concurrent: X2 o4 |. e/ A- O4 W; P6 H- B
参考文章:
: ]: d: n( t& M1 }5 @https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX4 V7 i, l) V4 G( o7 r
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-1-17 22:42 , Processed in 0.034118 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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