ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。' y1 J! m) E7 g: t1 p; L& `
" l0 _' J- G' i1 G; g: W: L
0 k4 M( y" o1 x W t' L
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。) l, y( X/ W( v- ^
) K& T6 g: A. R- I, \7 X2 M
h, e5 T6 w5 s9 ]什么是ModSecurity$ ~0 Q8 ^; Q+ i# l9 N7 s1 \
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
" J8 S. u6 O: i- w/ |2 R. c8 o/ ~- d5 G# }( z& ^4 K. i1 {
2 N' j' I% Z7 c+ o D) ^1 G3 YModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
" O: [4 O( `& W( z2 D2 |
+ r. O+ t! x/ H+ a# A( k! z
1 N8 G5 d0 Y$ H2 uModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
- S. g) ?/ \; _, m# D' r6 k7 `6 u2 z+ L! ~
5 k/ y9 @' r5 ^9 n官网: https://www.modsecurity.org/
% A9 B: x" a& w$ O& L5 |2 }, e/ k m. Z6 I0 q2 x+ j
- {; t+ }" d* m
什么是OWASP CRS, n; A7 Y8 t& q
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
% O \6 g/ \5 N& d2 e, x, S2 G- `+ `+ w" X* Y- a# u! C5 K
6 U. ^+ u, v: G' Q! G
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。9 |9 z; A5 q" \& O7 v, n
+ X3 A. O% U6 c# m
- [- ^0 @- i. a8 K1 B0 kHTTP Protection(HTTP防御)4 {& [1 L9 X9 {* R( M1 Y) U, C
HTTP协议和本地定义使用的detectsviolations策略。
- t3 g3 y- i# z: `. @( V. W4 s) q7 W$ @8 n2 a
/ {/ O2 I1 J/ V
Real-time Blacklist Lookups(实时黑名单查询)
0 U; N f4 }) L; g) A) W利用第三方IP名单。
& Q- f/ `2 w8 I3 @ j" U5 {
. o& N' Q s" C4 k6 E- |. i( h
, y' h8 k. } A4 I7 ^0 OHTTP Denial of Service Protections(HTTP的拒绝服务保护)
$ Y0 k7 O4 B; V( p M0 c$ K5 O- b防御HTTP的洪水攻击和HTTP Dos攻击。
% C$ ^0 ?& r. A a, h; _9 `( [2 ]5 p
5 X$ U, G6 i. R; \' G7 P& U0 q$ u7 g3 K) B; }5 N
Common Web Attacks Protection(常见的Web攻击防护)1 C0 B. T, j) \& Q2 d3 g" ]1 T
检测常见的Web应用程序的安全攻击。, ~) }9 ?8 V0 j3 ?: @, W% m
; b& a1 q' L M; g! ?8 ~
% y) h8 A2 Y3 GAutomation Detection(自动化检测). f$ c; V. y& E2 s3 }
检测机器人,爬虫,扫描仪和其他表面恶意活动。
H3 P/ F! Y2 B! K
% H; s9 {4 ?$ l6 V6 B) P- G( w8 B. C
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
* I8 M3 s# w$ y- L6 t q检测通过Web应用程序上传的恶意文件。
: D$ ?- O- M, a9 s& n9 A
3 W' c: V8 U* p. n- t$ J" i
* W/ K! l1 H5 V9 ]4 ]* W$ E/ sTracking Sensitive Data(跟踪敏感数据)! X+ g) R+ S" z7 u1 I
信用卡通道的使用,并阻止泄漏。2 E! Z8 j% ~- M9 T9 ~3 k
* n# j8 w! {! R: N/ ^! b
$ o7 D) U& ^- J a: X
Trojan Protection(木马防护)
& Y! S- f4 _7 g! A' w检测访问木马。
: h( z/ w! M* Y; n6 W" D/ @6 v. z
4 h# R( x l4 m5 J& }4 M$ w( l. b6 M# `2 P
Identification of Application Defects(应用程序缺陷的鉴定)
! ]3 A! S3 e- ?, D/ s9 U检测应用程序的错误配置警报。) Q. H- W I' |( e6 s+ Y& d
4 G, Z. h- G- d9 {9 D8 y
% x0 ~" h+ b+ G. x5 {Error Detection and Hiding(错误检测和隐藏)1 x$ h& `) G7 P. T2 E$ O, i: p
检测伪装服务器发送错误消息。
# M3 m; ?7 Q9 q9 Y
; r7 ?& ?3 h* c1 x3 Q0 U4 q, f
6 V! t2 q2 ~, O& L4 Z9 |: ~ n$ W安装ModSecurity4 b! P+ k* U* g# h) h d: g
软件基础环境准备
+ D2 B6 {) n) a# r, R/ E下载对应软件包
1 R3 F, ^2 [1 r8 u$ cd /root! q3 D% P- P5 u7 n
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
7 U0 }! A9 |5 i- K/ A) n$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz, z4 C/ ~! H: j0 k- ]
安装Nginx和ModSecurity依赖包& A! Z2 u: b/ `& _6 A
Centos/RHEL
% M6 ` ?3 Y P( P, J, X" C1 f6 ^1 s3 L7 e
t0 `# C# ^0 z$ y9 L0 P
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
' U- @4 k& V8 ^+ VUbuntu/Debian
* d' h$ i% {0 i8 f4 S/ {* ?% u/ B3 F) I
/ D' b4 c6 B" ]9 L ~$ 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++-dev9 q/ w& N. q) M; T% b% k" h1 D; K7 c
编译安装ModSecurity6 Y6 @; ^8 j* h+ k4 D) t
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
) j; o* E1 Z- a1 S3 _1 K* C) @; H w
$ T9 x A- H7 c8 O8 k; W1 s6 }' c( E$ J7 p/ E$ a) A: C* i
方法一:编译为Nginx静态模块+ O% \- |* N/ o+ e) ?
& y$ Y5 J5 c* s/ v4 r
& z1 p" T9 p* P6 ~编译为独立模块(modsecurity-2.9.1)9 f3 V8 q! e7 F5 @) P4 [8 A5 ^
$ tar xzvf modsecurity-2.9.1.tar.gz
. O- l; t& P6 t* N5 H& M8 b1 A$ cd modsecurity-2.9.1/$ G6 P, g8 F! t+ C* f
$ ./autogen.sh1 _5 k8 }: g: F& }* v% w$ U
$ ./configure --enable-standalone-module --disable-mlogc5 ?. N( h0 N, f$ l
$ make) s/ w$ a2 }7 o' [ H0 H
编译安装Nginx并添加ModSecurity模块6 R6 z* e7 {+ I! e
$ tar xzvf nginx-1.9.2.tar.gz
# R7 K1 O+ l- y6 e. D( G0 ^ `$ cd nginx-1.9.24 @2 v* }8 l, a6 L
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
. f9 b& j# _* k# ^& z/ _$ make && make install, O2 \; p+ [; R5 {5 L
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
& L4 C0 @( A; L2 F: f; a# |
0 O. o( R1 s0 k! f u
9 a% R4 ]$ o: V% g% r编译LibModSecurity(modsecurity-3.0)' j0 o! O3 h7 `" ~& J& Q
$ cd /root
; Z' a8 Q N0 L2 c) a; `8 V6 \9 i$ git clone https://github.com/SpiderLabs/ModSecurity3 L' e& G: X9 [% m9 `
$ cd ModSecurity2 c4 K& f v! X K9 q" X) k2 ~
$ git checkout -b v3/master origin/v3/master5 {6 I1 l: _( x
$ sh build.sh+ r" o% |4 m+ b' e, g* C
$ git submodule init
5 u% u0 m7 }: x/ y* L t- I: j; E$ git submodule update) N. p) o, s9 ?: L, ] b g1 ^
$ ./configure
a2 v$ I& `: q G6 V9 K8 ]$ make
: ?0 t- U7 V) a: P! z( J$ make install1 k: E8 t, i$ L1 p: s
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。9 }2 ^9 w, F0 n3 U" Y$ [
, _3 b% P m/ p: B0 |
* ?( y. p: L( F; Y3 q6 t$ ls /usr/local/modsecurity/lib
- j; a4 c( {7 X3 Zlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0! F8 j7 |% B( I# H( E
编译安装Nginx并添加ModSecurity-Nginx Connector模块
' |2 t0 b8 H% ~) l$ F7 @- i使用ModSecurity-Nginx模块来连接LibModSecurity" O7 ~8 L# b! b0 e3 [% b
; `; g6 S, h7 ~' k3 N- h+ b/ G7 H* ^4 U
$ cd /root: s8 u5 Z! j$ Q6 b" b& g/ Y1 p
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
3 X: S$ m3 G: ~3 b# o! j9 V$ tar xzvf nginx-1.9.2.tar.gz
1 T6 T5 c! B6 `9 P/ y$ cd nginx-1.9.2
# B3 u: k. p( w+ i$ ./configure --add-module=/root/modsecurity-nginx" I0 Q! s+ y7 Y! T5 z# ]
$ make/ _. S8 m8 m* O& l) q, B+ ?
$ make && make install
3 f3 H! _" z; z& Z2 A0 s添加OWASP规则
0 @7 Y0 H7 K1 J1 IModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
+ i$ U) B- Q. v6 ?' f [' T: \9 T6 H6 _; O1 `0 o
% o+ [1 C! [* x9 M下载OWASP规则并生成配置文件
% p4 J1 h2 ]4 i3 Q) q4 q1 h, M$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
6 h9 ?; D$ r% @9 I0 R( A$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/# D" m, e3 J0 G( `! V
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
6 n8 V$ `" W$ y$ n$ cp crs-setup.conf.example crs-setup.conf
8 [0 ~, \6 n) M- g1 C0 A配置OWASP规则1 v% J7 |5 x% x" @
编辑crs-setup.conf文件
8 G9 }; {! O4 K" t" ?
. a/ ^3 b7 O& o* k& M1 q$ f8 M K7 ?
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf- q: U! j0 D' m' J
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
% L( \5 d, u6 S1 c$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf E" R0 l( O/ b0 ~1 b$ d9 _+ P, B
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf. {" Q% e; j6 ^' I7 Y7 {2 _* K6 ~( {& W
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。3 T0 q- y1 B. I: P* ?6 B; [, b# z
1 D0 Q! m5 U& W0 A5 u' G7 R8 _& X8 F
' C, A: R. i- \5 N3 ?, Z5 V5 f
启用ModSecurity模块和CRS规则4 j+ M4 L& g w' h2 `" U6 V
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。1 h+ }4 N. X' y; J7 b |
/ q- \4 U: u4 H4 x) [1 A1 v6 s' t/ S' H
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。6 F0 x+ _9 {9 B4 N m! | a
' V; s6 z' X6 Y, x
! p' F9 u- L% R- h/ Y0 N$ cd /root/modsecurity-2.9.1/4 A* K0 l' d' \, P
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf % ~6 P4 ~. C/ _% |( c
$ cp unicode.mapping /usr/local/nginx/conf/
8 P, E% c+ k3 i. g# d' Y将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
5 N3 y# H# M0 E
: U, `9 E* k9 [3 X. f, g- D# @9 g2 b+ K
$ vim /usr/local/nginx/conf/modsecurity.conf6 ~ W+ `, T; l1 F
SecRuleEngine On @" I, k% `* y" h ?7 F0 S
ModSecurity中几个常用配置说明:0 j; X. w- }6 ~4 w+ A
1 \; Q9 |8 x7 K- k) Z8 {
9 M7 n: n5 _% E% P: E: ^5 E) }1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。! O3 Y( C5 d7 ^% Z% u# {
/ K: Y1 C9 h3 _" t6 m2 \ Y3 ?
8 x9 K9 ?; P0 T2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。* G- }2 }/ b I. R
/ n) m# A. p& S0 k0 ?0 v
1 H9 |2 \! v# r* }/ H8 T3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
2 W2 C& g. p- @8 R8 Y/ A0 q( ~& f( K1 l! }% k v, _
% n' e% o# l" a! s& a
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。1 h( w# Q- P- v- {% G+ R
# [" x! A9 ^( \0 J) R9 D) |+ M7 [9 ~- `. T
4 ^0 y& N: b$ D4 g6 J在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
! Q& \2 M0 W5 x9 {- {7 c; O
' k( k6 S8 D) R q1 O5 \' `
6 S& o4 w+ r) f0 K1 W0 `) U2 ~3.x版本CRS6 H- l# d- a7 \1 o
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
; W1 o% b" k- Y# 生成例外排除请求的配置文件' |$ s! D( c5 E8 @/ h
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf0 N- b- u& b* @
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
1 ]/ P" i: j# k- c% P5 _- Z$ cp rules/*.data /usr/local/nginx/conf$ X1 x6 `/ I5 r' y) ]0 l x3 S
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。8 b) c+ p2 S: y0 A" A* x* p9 [
/ q" Z$ R8 L; k; `+ |
0 e7 d: J, l! S
$ vim /usr/local/nginx/conf/modsec_includes.conf
+ m8 p; V; ~+ c& Z8 h* M% F/ e! i5 U4 g" e# Q* K# K4 c: I# M
[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
. H+ \ f4 }- V
5 Q# N) L( T% Z1 Q: N注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。 d l# ?& }( m. s; I
8 l& g! E: e2 w9 q
0 \+ n$ K8 }0 x) h; q+ U( d配置Nginx支持Modsecurity1 D3 ^3 W2 q/ W* P) t% i! X
启用Modsecurity( g# y2 E$ [0 J6 b5 w
使用静态模块加载的配置方法
% q4 i- A2 f+ }- _9 M Z q在需要启用Modsecurity的主机的location下面加入下面两行即可:3 k# c8 `: N$ u, P
3 K2 L$ J! l% ?; ~5 _5 U
, @6 ^% h& y1 N- R% PModSecurityEnabled on;# m' j1 o8 Z$ B
ModSecurityConfig modsec_includes.conf;& I m' p) N" |) }- t) N: U }
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
7 @6 [ G T! B" ~* S5 g( @' ^! F' U# k; w- _
& Z) A a( U% ^7 |8 j0 c" _
$ vim /usr/local/nginx/conf/nginx.conf1 d- T5 V. A3 P
a9 h/ N+ r/ N/ s7 Y
5 F5 \( y9 {% A2 |server {, ~- w6 Y- X7 ^9 U% c7 V$ ?
listen 80;
e b9 T. v+ \: A: E& q server_name example.com;, w* F. y( e; F
3 d4 o/ k: n5 ?" |
% h4 E0 V0 s3 F. x$ O0 A4 d# N9 U
location / {" }" w# @0 H% {, t
ModSecurityEnabled on;" h+ F' |* ?4 ^( t* x/ W
ModSecurityConfig modsec_includes.conf;
, L+ \, n6 u; Z1 w) J root html;: |7 _; T1 p6 J" }* e$ v
index index.html index.htm;9 |# y, }- ^' F: ~( j( C# ^
}
& e/ \ z5 S6 m/ P0 l8 R0 ~}
* e0 U. p' U9 Z. h6 ~8 ^. P6 M使用动态模块加载的配置方法
2 w1 l: i8 Y% P% c9 S! X, T在需要启用Modsecurity的主机的location下面加入下面两行即可:
2 j3 p' o# u6 r0 [' v0 { p5 v1 d0 \5 J, Z4 T4 N7 {
8 A8 X* [9 _: S0 ^ |8 t
modsecurity on;
0 O4 A+ |/ p3 v- O$ smodsecurity_rules_file modsec_includes.conf;4 i6 }' o4 r; j$ b: C# A$ ]6 r$ j
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。2 M/ P- q( H3 ^: l& C3 ~ m: R6 d% [
( ?. s& Q! N& S+ b7 B6 o5 ~8 R+ b0 M1 i
$ vim /usr/local/nginx/conf/nginx.conf
: T* [% q6 k$ V$ H3 [* w
$ g4 Z3 Z8 C, m" m8 i2 l4 }4 [% s
/ a+ S& G/ ?8 i0 Wserver {0 g+ ~ S- s+ b
listen 80;
7 C0 P- W* {& N! [ server_name localhost mike.hi-linux.com;
0 \) E" i. x& p* } access_log /var/log/nginx/yourdomain.log;
) u/ J, f! I; p$ s$ Q
" w3 q9 v. `5 R- v2 J9 x; b. M B9 H. x! h" v
location / {
v" t. H: d0 X5 K
+ E; ?% o& n1 g- u6 y. W1 C) B3 A
6 R" P3 z9 {1 X6 a' m modsecurity on;+ r& N3 @8 ]( O0 M: |! E' k
modsecurity_rules_file modsec_includes.conf;, N: H* u! _- d8 u
root html;, m: H% b" ]$ v# Z# ^4 b$ l
index index.html index.htm;
7 N8 N! u. p1 {& } Z0 f6 @}) V6 T6 _9 \; M1 t7 ~6 Z
}# r: Q- ?/ i; ~
验证Nginx配置文件- H" F# |8 I- K$ r" |, _6 M
$ /usr/local/nginx/sbin/nginx -t! _0 n* e$ L- @+ z
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
$ b& d" ] j1 E9 V: \' Bnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
9 H" _# k( H3 t; ~2 ?% }启动Nginx' s9 J* s$ Z2 ?. X3 H
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
7 V+ Y, F0 ^+ f: I6 U" O9 @6 `* o0 [2 D$ q
测试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能够战胜更多复杂/未知/混淆的攻击模式。 + q& X, F! B" f. c, o. E$ x9 Z
|