ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
" \; H. F+ v* f' ?9 q0 w! V/ Y
& q" S, w# R& l9 U
" O, ~ }$ r2 V% s5 n; d2 `在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
) Q% G1 J) }$ Q' y$ c
/ r0 n3 J- P, A: l( z
1 o) s( }( G% f1 M' S) |- w什么是ModSecurity
4 u* }, o6 P8 L) }7 G2 GModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
3 r) \) G' F- J2 n! K# M: m2 r9 d+ w9 I" ]9 x$ D
. a, T. ?7 c- H7 F# _' ?
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。8 G+ n( ?1 ?9 T9 q2 a, R
0 C/ m8 F' [8 j: T2 N
6 s+ u5 v' ^& N2 R$ T. ZModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
/ z7 {( M% s$ D
* _5 C3 @5 t% x4 k: L& y6 W& |/ W' S7 K# n
官网: https://www.modsecurity.org/; |& A5 b9 v, \8 B& u x
$ \2 z/ j( \) F' w& [& u/ g/ h' _8 w: P! b3 U- o1 l6 t# j% h5 V" c
什么是OWASP CRS
- s$ A6 |8 \0 E: I5 Z4 dOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。. m/ m2 X9 X4 m; f4 H
4 j3 w7 ?8 f* i/ r9 y$ y
* E2 G4 x$ C& E
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
+ x2 X- b1 }( ?8 a) G6 M3 @) s! D8 S, S( Y$ N/ I" Z
3 H6 \" L* n' h% a$ C2 Z
HTTP Protection(HTTP防御)8 \! ^$ r* ?- A5 O3 |# C1 C: x- A
HTTP协议和本地定义使用的detectsviolations策略。7 [' f I3 {- E9 X" w
) x0 B0 V" N& t
; G4 s1 ~; e& z+ X3 u4 m; C
Real-time Blacklist Lookups(实时黑名单查询)& ^9 E. l- C4 ^- w# r5 O
利用第三方IP名单。
* a2 Q" z% z3 Z! }" M& J A( W+ I
; b7 `9 n+ ~' q* h
/ c9 r) g- x5 A% V2 j) ~HTTP Denial of Service Protections(HTTP的拒绝服务保护)
( T, o6 t2 W) c1 w防御HTTP的洪水攻击和HTTP Dos攻击。
I5 ]% {- G8 R4 P. t. G% y: w2 L# f
) g9 ~. y( p8 _1 ~4 p. \9 Q$ K
Common Web Attacks Protection(常见的Web攻击防护)
. r; F }. u% x+ M检测常见的Web应用程序的安全攻击。5 |8 Z" }) R3 Z' |; {4 [6 a1 ^
2 S9 y% m: A' \3 a B( J* N+ i; b1 A3 D# H1 u
Automation Detection(自动化检测)
! o0 m/ ^7 e/ \8 v5 f: o1 z检测机器人,爬虫,扫描仪和其他表面恶意活动。/ `1 a# e. }0 T
$ f: q' }# x9 A( h! K0 \! h7 y
5 q; x+ E+ l) ?4 BIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
* a9 L' T; d( Q检测通过Web应用程序上传的恶意文件。: e8 c1 O/ }7 x0 h8 A0 T
6 N) J& o7 F5 R+ |) S1 x5 w7 F
* K% s, s. e* b! V
Tracking Sensitive Data(跟踪敏感数据)2 O4 j: W8 ?7 t. K. s3 d7 f c
信用卡通道的使用,并阻止泄漏。
3 w& |/ r4 ]3 {. u: R9 X0 O' W% b5 m& B) s% p: a
6 s9 D9 v; T3 v* c# J/ u
Trojan Protection(木马防护)) G! s* ]+ s5 \: l
检测访问木马。) U" i3 M% }, _8 }, k
: B$ H1 ` C6 \% G: j- f P" x7 `7 r$ k
Identification of Application Defects(应用程序缺陷的鉴定), g0 d1 U, Q+ ^- v+ b
检测应用程序的错误配置警报。7 F! u% U+ l5 D5 H4 U- e
/ J) p2 Z6 E3 h( h) U
~# I' Q; [* J( @3 VError Detection and Hiding(错误检测和隐藏)
7 ~2 E% R9 ?* K ~/ v" W检测伪装服务器发送错误消息。% z( H6 H/ g, w- `( g4 Q) c+ x
. Y( ]' c2 g; L- o2 E# l
+ Y# g/ m8 ?7 L' u安装ModSecurity
0 S5 j( P1 K& V/ g软件基础环境准备+ j w: i* Q0 A- |4 @
下载对应软件包
( h+ s& p0 e* {+ u9 K( n& L- J$ cd /root
% H$ s5 U0 e1 h) ^! ]$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'5 W/ T& } [# a: q6 ^
$ 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) Z/ O, t* j! ^' ?! v" J5 @: l+ x安装Nginx和ModSecurity依赖包. @2 p! [# Q: P0 b& Y5 T
Centos/RHEL( R3 X9 ~! \# v5 n& q1 H
3 ?3 K0 c" {% g, z0 g7 @
. J" [8 Y9 K7 D9 \( k9 a2 n( N. \5 n$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
1 k W/ q- T3 q$ O+ F5 `) IUbuntu/Debian
7 L# |: ~- T; \: o+ J$ L0 i) R! {& N8 c( y$ C
* n% F, u. Y* ^0 t9 l0 B9 ?$ 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 M7 l/ _ c# p编译安装ModSecurity& {+ N2 s4 d0 \ D; E! ~! Z
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。, g/ f* q t8 R8 ?* r$ E
5 C; t+ O- M5 X9 K" u) ^0 K1 F3 j) _0 [8 \5 V- r _) _0 t; S+ F
方法一:编译为Nginx静态模块
1 H: c3 @& o' t: H; A6 R! ~. I. h. Z1 m& F" @' n
( ~, r% S2 }7 Y" I! |4 ?, _编译为独立模块(modsecurity-2.9.1)- }9 y# C, I. ^4 l3 y3 j6 r, Z0 h& y
$ tar xzvf modsecurity-2.9.1.tar.gz
4 p u# l3 d3 i5 P7 U# P( Y' f# u$ cd modsecurity-2.9.1/
W F$ N! o$ E$ ./autogen.sh; I+ Q' M1 K1 v, @4 w, B, t8 h
$ ./configure --enable-standalone-module --disable-mlogc6 u5 H c" y8 j: _
$ make
0 h4 s/ Y, \" i8 E! a编译安装Nginx并添加ModSecurity模块
) v- f0 n: t3 Q X- {$ tar xzvf nginx-1.9.2.tar.gz
9 E9 g$ H* P( O, o- w3 ]$ cd nginx-1.9.2
R) L( [+ Y0 l' Z. h9 R }3 C$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/! G3 T2 h8 u' T; i( w: r2 U
$ make && make install
B. y n/ V& g0 |8 M方法二:编译通过ModSecurity-Nginx Connector加载的动态模块 D' ~4 [. @7 t; {- e4 L
) z& x0 i' k3 s d5 Y1 I1 I
8 H4 C* h+ B* C" h) J3 N编译LibModSecurity(modsecurity-3.0)+ z0 i+ E- N/ c; o+ V
$ cd /root4 g4 [ Z% C5 V1 x8 i0 m" _6 Q: H
$ git clone https://github.com/SpiderLabs/ModSecurity0 s1 Y& M8 E g1 A2 u- {$ ^
$ cd ModSecurity
2 P- z: u' O Q" N$ git checkout -b v3/master origin/v3/master) |* l: K- m- o% K# t4 z
$ sh build.sh9 z3 c* N1 N% U9 X9 [3 |
$ git submodule init: R( |2 h% \) J) o6 c5 Z# q4 F; ]! \
$ git submodule update# ] d+ ?6 J4 j! P0 u9 B5 q
$ ./configure
' |' p, N/ D+ }- }7 _" ?$ make
/ T4 c: G% E7 h$ g/ n. K: k$ make install
- Y/ I, v3 @& k2 K4 _0 c$ X2 t, gLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
+ `: c y9 Q/ w" l+ z- x& M4 `" o0 j8 W
, E6 [! E: l, \1 F8 |9 ? g
$ ls /usr/local/modsecurity/lib1 Y5 v7 n9 Q+ y% G1 s3 j. O9 g
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.08 d) Z5 L, _- r1 c7 |2 V
编译安装Nginx并添加ModSecurity-Nginx Connector模块* @+ a' u" w1 y% ~, G
使用ModSecurity-Nginx模块来连接LibModSecurity m! S. E$ |# m% D' H* X
3 Z5 u9 s1 r; N) q2 E+ ]
9 O2 k7 d6 b1 d5 c# t$ cd /root# y- T d1 c9 ~: L' y {
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx9 F. ?1 t; r$ c3 W I
$ tar xzvf nginx-1.9.2.tar.gz
, O# ?3 z8 i& x& T1 T+ k8 \$ cd nginx-1.9.21 e l' I w5 x% J& U" W- h
$ ./configure --add-module=/root/modsecurity-nginx
- g; d# u9 J% X! F$ Z( c! {$ make
. R9 W! d+ { B1 j& m( {1 w& Q$ make && make install
- ^7 E0 f) R7 r& s# G# b# T添加OWASP规则
% J2 I* O2 p$ H8 G2 o+ p0 \2 r6 XModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
) n( Q T, x/ `6 y, T( N% t6 v0 d2 m* O
1 K- h1 J1 U! M" U
下载OWASP规则并生成配置文件
9 J' k6 D* F* g$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git! c! e4 H+ J$ l' @
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
2 _5 y% _ R5 A. F6 z& L2 V$ cd /usr/local/nginx/conf/owasp-modsecurity-crs. H8 O; m7 k$ W% t: N8 _
$ cp crs-setup.conf.example crs-setup.conf8 d4 a, O6 Y& _6 b+ o: R6 U
配置OWASP规则
1 p: K- u, f \/ E# w+ z编辑crs-setup.conf文件
K8 I) ~: H2 J# a2 w
* A) G4 h4 m7 l/ P( m2 V$ q) N8 O; ?/ y u5 J6 r: W' E! f" i
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf, B- T! b+ v5 f) O% M6 w- y$ U
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
9 A+ O0 E$ @ ]5 s9 d1 `3 T% {$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
, Q' b1 Y1 T; }) y$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
$ }1 V& r1 A5 t6 s8 u默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
: q5 G8 B. e& r" s9 k( |
- Q* s F& e0 ~% n* L) A6 @- Z h0 r0 x2 w- B2 H
启用ModSecurity模块和CRS规则: |$ D9 V, @5 R) v+ a' ?* d
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
" m( u$ a/ Y7 E; G, O) D2 w2 y+ Z8 u! c5 p6 X5 \
# H v2 T* c6 [# E
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
2 _; j& S$ ~# y/ [* ?9 _1 H$ Y
" X' k7 c7 F6 T; W. B9 P( E* D( @) a$ n: M# X: C1 Y
$ cd /root/modsecurity-2.9.1/5 X* |2 `* g$ g1 K( n9 f
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf : _! j/ L+ i7 T8 S' b6 E
$ cp unicode.mapping /usr/local/nginx/conf/( u* D* p6 }0 e: G# a
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
# H! J F1 H" R7 f% z4 F: A
3 o4 ?+ P1 b5 D8 x f! t- s! X9 D. m) w2 n: T& g% ^4 y& Q& J
$ vim /usr/local/nginx/conf/modsecurity.conf
~6 d+ t; @# a0 l8 u! x0 lSecRuleEngine On
8 p1 e% X) y" b: X \ o! O: p; @" h, lModSecurity中几个常用配置说明:
3 y5 ^' t( \) {1 e) c
; R% g' g* q" T8 h, J
9 r; S8 u& h7 d& z$ i/ u+ a1 ^; F$ a1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。0 ~, R8 B: L+ a! `+ Y! v
+ I" }$ ?- P8 l7 j3 k. h" g( |3 E, }, ^' ]" r; q. i" I
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
: O- l4 D' Q7 _; ?% @3 i/ X4 }4 U& |2 }, G2 j* I% W$ y
& m0 r# @. T3 G8 a9 g3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
3 l% n# P& L6 k; L7 T7 B& w1 U9 X; n
! \ R6 d) P. e0 P) I' R: k" T; Q4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
% P% \3 f* k/ @$ x8 l
( d0 V: [6 D& |# _* Z8 h/ ^0 ^3 {2 |3 A5 b& D
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。/ P& L/ j1 s$ J! X( v
3 ?8 e' d k" ^1 y
4 X8 m9 \5 u1 R% x2 e
3.x版本CRS2 D8 d: r" ?/ m: F
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
( T& _, B( c3 H$ D; e' ^; }% R# 生成例外排除请求的配置文件
p: D4 i; x3 n0 o, w' Z8 U% ^$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf2 O0 h. U- O1 {6 j, C
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
' H9 e; d$ l. [3 e5 d$ cp rules/*.data /usr/local/nginx/conf9 r' H1 ?6 ] U; c0 K
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
. { Z$ k7 X+ K2 [, o5 t
: m# C% l; M4 b
- M; P5 t g' [3 E$ vim /usr/local/nginx/conf/modsec_includes.conf
7 x/ w0 G: h1 D2 M7 D9 ?- H9 k
) s, F) I7 L2 v/ F[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 3 F. `3 E, d" r9 c0 ]' }) E1 d
0 J3 e0 G" Q( A3 h: [' Y注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。# z; P1 u' W p
8 R) h0 m; | m8 o9 Q9 e- x: z
# ?* w3 B# g- U5 _配置Nginx支持Modsecurity
3 R$ b! `% ]4 b启用Modsecurity
) s4 u: V7 Y$ ?) l, T; {使用静态模块加载的配置方法0 ^ ^* m4 i' A# L& w
在需要启用Modsecurity的主机的location下面加入下面两行即可:5 {8 {( N* K" u: m
( i1 }1 v6 F P. K
+ U2 m& _0 _' v' u4 {+ XModSecurityEnabled on;
, K; [1 {6 l8 QModSecurityConfig modsec_includes.conf;* R( D3 L1 J! ^; S$ S
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。! s5 N7 F, A$ K; V( I, s1 o! y
4 [ y* v* \$ t" y9 m: y/ L) m
) {1 O1 E8 Q- O, Z# H7 n& X( N$ vim /usr/local/nginx/conf/nginx.conf
+ Z) j" ~# f. I( ], P+ G
0 L9 W1 V9 m% ?
- X7 C" T6 P0 Z0 v- n: c" `) H ^ ~server {
. x/ b6 `* U) s' i& ~ listen 80;2 Y0 D$ [9 z8 `
server_name example.com;
+ t- S/ J' a4 }. e0 D
0 L& A1 h( K* G
J0 C s3 j: B2 c location / {# _( |: c& m* {
ModSecurityEnabled on;
3 w. r [% [1 e; |0 h" ^. e ModSecurityConfig modsec_includes.conf;, K8 o. ~' Q; D, x. H: V' x
root html;! X( f: D* x: w. {% @
index index.html index.htm;
" C) p& m1 `$ H% O* a }
1 _ n- s( Q# O& M}- X' }7 Q- t7 x9 Y8 E8 x& c" W+ y
使用动态模块加载的配置方法% ? w; o8 L' ]( G
在需要启用Modsecurity的主机的location下面加入下面两行即可:
8 o; w9 h& \/ d# u4 [( M' Z
7 N& w5 E# S2 S. t
n5 A; k% [% b& @modsecurity on;
/ N9 _/ ^+ ?6 }modsecurity_rules_file modsec_includes.conf;8 g0 a! h! C3 w D
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
0 \- \7 d8 S% c. A7 k# c0 R* G( ^, Y. M% n; |
( Y+ V( Z( o; [, |9 h! L
$ vim /usr/local/nginx/conf/nginx.conf
( }+ Q1 F M+ c1 j
' h, q9 N k; f6 P2 i
) _! u% b- d. b$ i, f6 R* kserver {
7 u2 e9 Q/ `' U3 W9 i4 i listen 80;0 x, T. n3 s/ G/ E; w/ _
server_name localhost mike.hi-linux.com;
( H- m& `1 Z' d- [5 ]1 | access_log /var/log/nginx/yourdomain.log;
P" r2 Z- v. v9 w1 @1 } v3 d! \3 P1 ?, W! k/ G' A
8 ?" v: r( v/ f, K
location / {
6 r$ ?# W9 D5 i' t% x
9 x n* {: B& U5 O8 X: f! }& H
1 p% x: m) N# e( E modsecurity on;
/ r6 Z) h/ }7 o1 x5 { modsecurity_rules_file modsec_includes.conf;
+ X2 V r1 }* R( r; Y* a2 ] P root html;# M0 b' q5 M0 \% p
index index.html index.htm;3 y4 f0 n6 k7 V
}
9 w6 u' ]- z+ @2 l: D6 y}. \6 Q1 D4 \/ D3 j* f
验证Nginx配置文件
1 X I& P% b( @2 W$ /usr/local/nginx/sbin/nginx -t
: m) n' l; v8 [0 k* Q# @. Ynginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
; b$ P. a3 K$ W3 q4 `( O! nnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful1 O3 `; f/ W3 A* b
启动Nginx
( i, s" Y8 Z# t0 k7 v$ W) y" T1 _$ ^$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf' u& w' l: N6 k( u! j9 ~# B' L+ b
( `/ @* ~( U: r% ?2 B测试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.logModsecurity主要是规则验证(验证已知漏洞),Nginx下还有另一个功能强大的WAF模块Naxsi。Naxsi最大特点是可以设置学习模式,抓取您的网站产生必要的白名单,以避免误报!Naxsi不依赖于预先定义的签名,Naxsi能够战胜更多复杂/未知/混淆的攻击模式。
! {. z _2 c7 j" W Y0 w0 } |