找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12908|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。$ M' w4 x+ \; g0 E/ s+ Z2 y; ^4 O
' p6 j) W3 S1 m0 [# ]1 |. b! ^# o
  W0 Q) c& e1 W& H; g
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
7 W  L$ ~/ c# F6 B
1 O! f) D2 f0 w% t0 P% ]: r) e
  _% j* e8 \9 b! E; |什么是ModSecurity, w5 E1 L6 e3 t: K
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
& L& P# h* L# l7 v" f- x, [4 d
$ ]7 Z5 t; P$ T, C
: ^9 |* Q4 k8 ~1 iModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。6 H! z0 g2 |, U' ]' Y# f6 t) W

6 M1 `1 \4 I! }" C
6 {4 g2 p0 q9 H. U3 d" t3 uModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。( T  o9 W4 l# |1 K# k8 U* d
3 W! R5 r  B5 U' u. o# P5 R) n

& H# U; j$ J& C$ m; k! [官网: https://www.modsecurity.org/
8 L, W" z. N3 [1 ^, d" l
" W7 o- w; a9 A) q( G! ~4 O- [( f0 }, a9 Q6 m
什么是OWASP CRS
. P6 ~8 d2 x+ c6 VOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。3 T  I: j5 c: h2 @8 e

$ |3 y' p, T8 W+ K/ p; [- F# x0 U
4 k, L0 y8 p- x& l7 f2 m4 rModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
# @& h6 f. G/ Z1 @4 i& e; ~8 W" P+ X6 C0 }: y$ o5 |; v1 N; ?4 n
# I3 J6 ?- n1 f
HTTP Protection(HTTP防御). R: ]" A' t1 b% c8 D
HTTP协议和本地定义使用的detectsviolations策略。5 o7 X0 A" ?; A; o, e, _
3 Y- i, K) j; i0 L( m

% [8 u4 ^3 n( s( zReal-time Blacklist Lookups(实时黑名单查询)
1 C: I# s7 [% D5 E8 L利用第三方IP名单。/ Y4 ^+ m% o* B4 c) D  M0 w$ ?7 C
" H2 o( d4 K. B6 V; N* h
8 k! C: ]$ c1 }- w" ?2 d+ ]
HTTP Denial of Service Protections(HTTP的拒绝服务保护)7 K1 }; d3 X7 p  }8 S0 t1 G9 t
防御HTTP的洪水攻击和HTTP Dos攻击。% D% p0 C+ h6 R( W  [% Z

  h8 l" ]7 e1 i' c- F3 h
% P" w6 {" T$ @# g( ZCommon Web Attacks Protection(常见的Web攻击防护)
- f% |, l, [6 i' {2 B% I检测常见的Web应用程序的安全攻击。( M- H: H2 `, k7 Q( z

+ a9 Q5 Q! \9 N) f3 d4 E: e3 G% e) _  w# f  p7 a
Automation Detection(自动化检测)
1 j5 [) F$ f. O检测机器人,爬虫,扫描仪和其他表面恶意活动。
2 A5 j5 F% j- F& ]" p7 M; W7 S+ Y8 U

. l* g7 Y7 A! @' I  g$ KIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)6 e  D3 ?7 N4 F6 j# a
检测通过Web应用程序上传的恶意文件。
: _8 Z8 _0 @/ B- v
+ ~9 y4 e; z( K5 {* N' ]( W. a' C3 B6 S
Tracking Sensitive Data(跟踪敏感数据)% Y: D! `- S9 S& i
信用卡通道的使用,并阻止泄漏。
9 f: o( P4 N# q4 b9 K3 y, ?" [; A
9 v6 i) i( @0 P  V/ j- q
" S" x% J: o* k* v5 fTrojan Protection(木马防护)
/ N; I# C; A3 W/ ?2 K检测访问木马。" y( ?: M% p5 y# J* `
1 D) z* U$ k7 J6 x
$ `; ?- @* |! @9 Y8 V: _* ^
Identification of Application Defects(应用程序缺陷的鉴定)
! \# V6 c8 d( r检测应用程序的错误配置警报。4 m; t' G  u. }1 ~5 c, d4 l

, {. j# R$ o( T3 [- @4 \% A2 M  m1 _) N7 h" V
Error Detection and Hiding(错误检测和隐藏)
8 Z8 Y' u8 L  @8 V" f+ M0 ^检测伪装服务器发送错误消息。  b! G- D0 G8 p" R" g4 m8 n6 k
& j6 \2 I  H9 R/ x  r

- I: {/ @, b5 {, C; ~) r安装ModSecurity) y- ?3 Z8 @* w9 m/ O) ^
软件基础环境准备
. Z. ]% d) m& ~下载对应软件包
, i. o  _" Z. h3 B+ k4 X( m$ cd /root
8 f6 e$ R) y8 E8 u! J( A4 j$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'$ D* N) n3 X' Z8 Z7 h
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz/ Y. S5 z5 S5 N& m
安装Nginx和ModSecurity依赖包
, g: P; b* X/ m8 R" gCentos/RHEL
; u8 Q$ X( S# M
7 f- @0 U5 u& y' w4 V
5 o' L" J4 @) C" E" n) A$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel- `$ ~. ^  `3 m' e0 Q
Ubuntu/Debian9 [! C5 c' f# i- [9 n9 R
- c6 d/ a1 O, L% D9 S

. j1 S2 i. `! t* J9 W$ apt-get install libreadline-dev libncurses5-dev libssl-dev perl make build-essential git  libpcre3 libpcre3-dev libtool autoconf apache2-dev libxml2 libxml2-dev libcurl4-openssl-dev g++ flex bison curl doxygen libyajl-dev libgeoip-dev dh-autoreconf libpcre++-dev
1 V3 R# g+ s7 M3 U6 _编译安装ModSecurity
4 T: O' r6 r1 Q3 t* QNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
; Q/ K* u1 x0 r% q" k+ ]3 o6 \) P8 V. D8 O$ H( s4 M
3 o( i+ O* x/ I; F) I5 ^# O
方法一:编译为Nginx静态模块) f4 n/ L+ {  f

* D8 r. A8 `/ b/ L1 O( ~. Q+ c" x3 e$ q7 `1 ]/ l
编译为独立模块(modsecurity-2.9.1)1 m# k6 `$ H$ O2 F' W
$ tar xzvf modsecurity-2.9.1.tar.gz
3 Z2 k  K1 D. Y' c$ o4 x$ cd modsecurity-2.9.1/" C( @2 l" L- u, I/ [( U5 J4 \
$ ./autogen.sh
6 Z& S) U9 _+ h& V$ m$ ./configure --enable-standalone-module --disable-mlogc  l/ y" X8 G. a2 g" v  y
$ make' m! u5 W0 w* \) n: O
编译安装Nginx并添加ModSecurity模块
. }1 ?- ~% K+ N/ k5 y: v$ tar xzvf nginx-1.9.2.tar.gz% ^# O6 r1 C! |" r* p8 e/ D& K: d
$ cd nginx-1.9.21 O3 B6 E, J6 e8 I! T0 F' i+ T/ v) q
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
" L: r2 i, T. Q# N6 P$ make && make install
9 [% ~- |0 `" N  k方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
$ U/ q1 ]0 ^* K5 k! T- B6 S2 H" p: A" t0 }& A* A
) w) u2 i9 Y$ C3 y1 D9 B
编译LibModSecurity(modsecurity-3.0)
: j# ^) ?% Q, M% T3 d0 O) U$ cd /root' }8 \  |' X6 z& n
$ git clone https://github.com/SpiderLabs/ModSecurity
* |5 M3 c  g: ^8 U/ Q$ K$ cd ModSecurity. i9 m4 r3 ?2 F1 T) n6 u
$ git checkout -b v3/master origin/v3/master; s+ Q! f' A9 Y
$ sh build.sh0 A$ X% Y, V, W4 |! ^% g6 \! B
$ git submodule init% ~( Y+ E" \# Q
$ git submodule update; ]( L. Y) \% w3 r2 r  p
$ ./configure  Q2 r0 ]. ~0 l# I0 [, |
$ make
3 B. b3 @  [) V/ H' ?$ make install& F' L  h; d0 e8 O4 d6 e
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
! N" y& p* \$ z) L! W5 R  F# q. B1 _0 u

; H4 n  k3 w" Y$ ls /usr/local/modsecurity/lib
& W7 Q0 l4 p1 \libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
- A. D2 y* b3 F; D( }# v编译安装Nginx并添加ModSecurity-Nginx Connector模块
7 o* f, X  Q. E# E* M9 b" m使用ModSecurity-Nginx模块来连接LibModSecurity$ A- t  j$ x5 F  D

2 _( ~; x5 ~- m/ ]4 O* w9 U4 Z! x7 }5 G& K8 e
$ cd /root
! X  X! s% s# _  f, l0 a$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
/ F7 U% J7 c4 ~$ tar xzvf nginx-1.9.2.tar.gz( E  H, K$ J) S" R# W
$ cd nginx-1.9.2
6 z) I$ j" E$ ]2 [- m+ O0 u$ ./configure --add-module=/root/modsecurity-nginx- O+ A2 F% f1 ~) h- a
$ make
4 a/ a8 d% b! a5 H3 g$ make && make install" Q- h7 z4 }& w4 g; p
添加OWASP规则5 T9 H% E8 N) g  ~
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
6 `1 B3 \+ i7 q8 o0 w* ^+ R1 @. p7 N$ a0 {! U
9 m' a; H/ a7 H( n6 o3 R* P
下载OWASP规则并生成配置文件
( F/ ~6 }) j, Q6 d6 I4 X$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git' c! `1 b3 b* c& ?5 s7 X
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/+ v2 u* p. b2 I" a& H9 C0 Q
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
5 C2 h: B0 Y8 F( Y) v$ cp crs-setup.conf.example  crs-setup.conf
9 u. E, B9 q1 a( s  F配置OWASP规则) h( H& z6 `* ~2 z/ C, L
编辑crs-setup.conf文件$ J8 k; y2 y, a# z* P7 f! f

" E' _! }# D3 K1 V4 `1 f4 |. C# Z! s
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf4 u$ N0 Z  S2 U
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
  X/ J: w# ~' R% B3 B- i2 t$ I$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
$ y" ]2 B, e; ^0 q; X2 l) C  f$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
+ Y/ @- x: k; y# l3 K1 n* T默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。4 X; c9 Y1 d4 x: G7 e) \
% t, ?. E2 A5 [6 Y

2 g, H2 I7 B# g0 K5 g+ F启用ModSecurity模块和CRS规则  ^: P8 O2 v5 x0 y6 |
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。8 C1 X; a, y% \  f7 p# n& j3 m9 K% f, i
5 F" t2 g0 ?' M5 r
6 l! S  H7 |8 d+ ]0 {
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。5 |. e# x3 N$ P0 q- f& p

2 p% ]- O9 ~2 I: Z) P, B% z+ H# T( ?+ [$ J9 H7 c+ M
$ cd /root/modsecurity-2.9.1/
+ s# u+ O5 X. w5 e$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
0 @: a. T4 l3 ^4 S& A$ cp unicode.mapping  /usr/local/nginx/conf/1 X) g) o6 @& ~0 ]- ?2 [
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。4 c" d! _) A; Y; M( z5 b

  k1 _& t3 P) [8 [: ~4 L3 u7 ]* B
! i/ d, J9 M3 N% T% ]- |, r5 \! J$ vim /usr/local/nginx/conf/modsecurity.conf
6 z; O4 X* l) [/ N$ n9 J7 y5 tSecRuleEngine On
3 ^7 t  w' b% _: cModSecurity中几个常用配置说明:
$ ^1 b  p# ?/ f
8 h1 v- g8 d) d* Q0 @( @' K" u, \: q: M; t
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
# E$ C( g- m) t- Q- m4 I4 z* w- n# y& u( i4 v8 H/ b2 r

7 k7 K4 O! ]. ]+ u; u2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。8 D& m: m: _0 d4 m  q

( n. E( |0 |! P* F; x, A. y) N* i
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
% X& }1 A! ?$ M% _+ k; i7 W! {
. t2 E1 o/ J( ~' i- I% n! L- H4 W1 s, [% d; Y
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。. b4 {4 o; P% m- c$ M; q3 ~
5 Z$ S: X  V6 X1 {
; [; T$ ^# T! `
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
2 \/ Y& e3 a$ F% G* L% c5 y0 p! S( i

3 y* Q, ?- y- S' l' L, B+ i' U3.x版本CRS
) O! q$ f- u% w9 S" R1 L0 k$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
5 h; ?( [% b/ b. {& E# 生成例外排除请求的配置文件
4 A. ]/ ^. T0 _$ v$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
, t  |2 I6 K8 G& J6 `4 [$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf2 y- _; {! |& `0 {) P' V
$ cp rules/*.data /usr/local/nginx/conf/ x9 Q+ {3 J' T5 G# Y
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
% v4 R2 l5 u: B9 Q3 _! i, q! B
0 i2 [  K4 j+ k7 z4 R
, [- P( u) U; p$ p- K; m$ vim /usr/local/nginx/conf/modsec_includes.conf
" u! r* I1 }; Z
+ s* _, ^, T* R3 C* N) l
[Bash shell] 纯文本查看 复制代码
include modsecurity.conf
include owasp-modsecurity-crs/crs-setup.conf
include owasp-modsecurity-crs/rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
include owasp-modsecurity-crs/rules/REQUEST-901-INITIALIZATION.conf
Include owasp-modsecurity-crs/rules/REQUEST-903.9002-WORDPRESS-EXCLUSION-RULES.conf
include owasp-modsecurity-crs/rules/REQUEST-905-COMMON-EXCEPTIONS.conf
include owasp-modsecurity-crs/rules/REQUEST-910-IP-REPUTATION.conf
include owasp-modsecurity-crs/rules/REQUEST-911-METHOD-ENFORCEMENT.conf
include owasp-modsecurity-crs/rules/REQUEST-912-DOS-PROTECTION.conf
include owasp-modsecurity-crs/rules/REQUEST-913-SCANNER-DETECTION.conf
include owasp-modsecurity-crs/rules/REQUEST-920-PROTOCOL-ENFORCEMENT.conf
include owasp-modsecurity-crs/rules/REQUEST-921-PROTOCOL-ATTACK.conf
include owasp-modsecurity-crs/rules/REQUEST-930-APPLICATION-ATTACK-LFI.conf
include owasp-modsecurity-crs/rules/REQUEST-931-APPLICATION-ATTACK-RFI.conf
include owasp-modsecurity-crs/rules/REQUEST-932-APPLICATION-ATTACK-RCE.conf
include owasp-modsecurity-crs/rules/REQUEST-933-APPLICATION-ATTACK-PHP.conf
include owasp-modsecurity-crs/rules/REQUEST-941-APPLICATION-ATTACK-XSS.conf
include owasp-modsecurity-crs/rules/REQUEST-942-APPLICATION-ATTACK-SQLI.conf
include owasp-modsecurity-crs/rules/REQUEST-943-APPLICATION-ATTACK-SESSION-FIXATION.conf
include owasp-modsecurity-crs/rules/REQUEST-949-BLOCKING-EVALUATION.conf
include owasp-modsecurity-crs/rules/RESPONSE-950-DATA-LEAKAGES.conf
include owasp-modsecurity-crs/rules/RESPONSE-951-DATA-LEAKAGES-SQL.conf
include owasp-modsecurity-crs/rules/RESPONSE-952-DATA-LEAKAGES-JAVA.conf
include owasp-modsecurity-crs/rules/RESPONSE-953-DATA-LEAKAGES-PHP.conf
include owasp-modsecurity-crs/rules/RESPONSE-954-DATA-LEAKAGES-IIS.conf
include owasp-modsecurity-crs/rules/RESPONSE-959-BLOCKING-EVALUATION.conf
include owasp-modsecurity-crs/rules/RESPONSE-980-CORRELATION.conf
include owasp-modsecurity-crs/rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf

  C# j$ M" \0 A- c4 D
( ]' \3 H3 r$ m9 F注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
; T  l6 D' F) B' F( q# j! f4 L5 _

6 `1 Z4 m. d9 j3 Q; g& |. m配置Nginx支持Modsecurity
3 x0 |  S6 M6 W启用Modsecurity, h/ W# J- c1 O/ L& r
使用静态模块加载的配置方法
, z1 b3 a5 U- h7 n' |& x在需要启用Modsecurity的主机的location下面加入下面两行即可:
0 Q0 g+ i8 o% x! H2 A1 \! T. k! a+ U* q  R
6 |! W0 s6 q; e$ d; T: _. E7 n
ModSecurityEnabled on;
; e% K% j$ r) N, e+ V  h- k0 AModSecurityConfig modsec_includes.conf;& j6 E" z2 a' c" X
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
) K& p6 j) V; h% S& d" f, H& U) l4 l: U; s, E3 z' s
( S1 Z9 C  s" F0 ~
$ vim /usr/local/nginx/conf/nginx.conf' I: R* o7 u. d3 @0 c

7 @: V. t  r1 s' f8 G+ I. a8 B: H, W/ o  U
server {
% S, W$ s& [! S3 z6 T, O  listen       80;/ ]& W  F, L% x% T) v* S& U
  server_name  example.com;& [; V: L" n4 N7 ^% N

8 m2 q; M3 ^' f! G, {( W1 u
5 {5 K0 S% q; t# e. }  location / {, w$ c1 X5 H, f5 b
    ModSecurityEnabled on;
# V7 G8 c0 i; [- Z9 _4 ]' S    ModSecurityConfig modsec_includes.conf;5 A1 l: o" J1 ?; A+ {; Z! V2 R
    root   html;
/ x' X; A. v( o" ~6 P8 v% \% L3 Y    index  index.html index.htm;
9 I' _2 u4 D: G  E: ~, }* F9 l7 \  }" [( H' S) b$ `% t5 K
}2 |5 {) J5 U% E6 m
使用动态模块加载的配置方法
3 W, ~+ _# i1 E2 b- S# A在需要启用Modsecurity的主机的location下面加入下面两行即可:
' T+ `! o7 c% J, E4 o% _/ I# K' S. T" c8 z$ `& A! \5 {

- r: ~1 P1 X/ gmodsecurity on;! d% b; C& R( ~! y: [) k; y: J
modsecurity_rules_file modsec_includes.conf;0 x. ]+ N+ N" @7 |* J# X7 V" X
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。8 t9 W, E% P: J

7 g# p5 W$ s" `+ |/ f
, Y1 L8 ]$ ~5 {/ r  |& z/ o$ vim /usr/local/nginx/conf/nginx.conf8 R! T* x% V- ?( B7 ?$ Z6 @
9 T- X0 t, K, H. K) _

6 l: z0 W/ |" Z4 ]' G, Q- Vserver {
0 m; h7 p' I# b: U2 K2 }; w  listen  80;
& }; l- ^! @% U: u) r" i  server_name localhost mike.hi-linux.com;7 Y8 r% ]( l& O& s: J
  access_log /var/log/nginx/yourdomain.log;
  \- D" B- d5 z3 _2 j( g! f0 g3 x# s7 k0 c! K' E  o
! m/ O7 H, b3 b* o
  location / {- T  O2 t% }9 X+ K4 B( \( c( S6 l

* Q. \' n5 P6 ^) ^0 z2 S3 L3 u8 v9 ~% H$ `+ e
  modsecurity on;5 C' S! i( R; d8 d
  modsecurity_rules_file modsec_includes.conf;; J$ e# |) |. n4 `3 V- B
  root   html;
5 s3 }: r) ?: m% o  index  index.html index.htm;
& p$ R! \$ f2 J+ E8 V}7 |2 u. E& ^, T- I& U
}( V, C2 o) O# c9 C
验证Nginx配置文件
' _2 `) K0 Y$ k$ /usr/local/nginx/sbin/nginx -t) U' j9 n  C4 O
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok0 Q8 a! M- l$ V% c
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful) y# W0 s5 T! o  f7 C- Y" z, a
启动Nginx
1 G1 a7 g  W. }& X6 R; G8 m$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf8 }3 u+ Y) H/ D
' J% @  H; c2 W$ F& `$ T

测试Modsecurity

ModSecurity现在已经成功配置了OWASP的规则。现在我们将测试对一些最常见的Web应用攻击。来测试ModSecurity是否挡住了攻击。这里我们启用了XSS和SQL注入的过滤规则,下面的例子中不正常的请求会直接返回403。

在浏览器中访问默认首页,会看到Nginx默认的欢迎页:

[/url]

这时我们在网址后面自己加上正常参数,例如: 。同样会看到Nginx默认的欢迎页:

[url=http://img.colabug.com/2017/06/842f48f203c6c2cd30144f29b57af97a.png]

接下来,我们在前面正常参数的基础上再加上  ,整个请求变成:

[/url]

就会看到Nginx返回403 Forbidden的信息了,说明Modsecurity成功拦截了此请求。再来看一个的例子,同样会被Modsecurity拦截。

[url=http://img.colabug.com/2017/06/246ce28e95310a32f791893d4f5c55ca.png]

查看Modsecurity日志

[url=http://img.colabug.com/2017/06/ae44dcb58b8a4a0ea761317e398b3101.png][/url]

所有命中规则的外部攻击均会存在modsec_audit.log,用户可以对这个文件中记录进行审计。Log文件位置在modsecurity.conf中SecAuditLog选项配置,Linux默认在 /var/log/modsec_audit.log 。

$ cat /usr/local/nginx/conf/modsecurity.confSecAuditLog /var/log/modsec_audit.log

Modsecurity主要是规则验证(验证已知漏洞),Nginx下还有另一个功能强大的WAF模块Naxsi。Naxsi最大特点是可以设置学习模式,抓取您的网站产生必要的白名单,以避免误报!Naxsi不依赖于预先定义的签名,Naxsi能够战胜更多复杂/未知/混淆的攻击模式。

' H5 K8 {, @+ c5 j& j

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-30 09:10 , Processed in 0.057667 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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