ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
: G9 S: R' c+ I* M+ E5 D3 W, ]: ]& S% X% i3 C8 r0 }
5 T9 A- d* m8 W在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
8 [8 y7 A; }2 O3 r$ E, o
9 T6 U7 C+ i: |, u) K' z4 B/ T4 e8 l( i1 E* c8 z, a$ z+ J
什么是ModSecurity
8 o! i3 ]) M8 W9 u2 C0 TModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。8 {& C% }- q, r, I4 u! ]% r
( d7 ?. |, n& V. S
' \; N5 D& |6 p+ Y" OModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。1 P8 E5 l3 J- H6 n$ o4 y$ d
, T$ r( d0 [, h
8 y1 L8 q& @- |. S$ x2 WModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。. i. t, g. `( @. q Z
( k i' R: T- ?, D% |8 [
2 @4 U! G+ u Z官网: https://www.modsecurity.org/
" x5 ^& g1 ~1 q
5 s$ C! m# G$ }( c9 H* j- ?
* O0 j A/ M; n- k8 L8 p) P什么是OWASP CRS
0 t6 \" w6 n' o. G3 f! HOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。1 F/ ]( k( @5 U
! F4 x% u) J% m, [8 C
, i" B( k' ^4 X% {! v- uModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。 I. G. t$ T' e2 \& L
' ~/ S4 Q' |" M& R; D$ T2 R9 b
% L' h: Y6 H' e- ^HTTP Protection(HTTP防御)3 X4 u; c8 |4 V2 ]
HTTP协议和本地定义使用的detectsviolations策略。. ]' E) W+ \* r* e+ y
; P% w/ R* y7 [* m
0 h1 i/ p4 T2 V# ZReal-time Blacklist Lookups(实时黑名单查询)
5 W9 R S' H9 }0 p& @0 L& u/ R利用第三方IP名单。
7 _6 ?* u4 ` @$ Q* n8 d/ X8 Y2 z G
: s. n& l7 g0 m0 B4 W
HTTP Denial of Service Protections(HTTP的拒绝服务保护)2 ~4 A; J `( { b: Z' D0 p$ N8 x) V
防御HTTP的洪水攻击和HTTP Dos攻击。
* g7 {3 ?! ^$ {5 m# m" r# D; p2 I9 S2 d# K* u3 j" C, R. p' F+ @% {8 ~
- z5 M3 c5 X* \0 e3 lCommon Web Attacks Protection(常见的Web攻击防护)0 ^. |# \7 }+ e2 A: K- |0 G p2 h. l9 F
检测常见的Web应用程序的安全攻击。# B4 |# _0 K. H5 Y$ [
2 T2 d$ L6 [4 S4 r2 ^
: d0 g; L2 g# \9 M
Automation Detection(自动化检测)
; o1 ?: _5 G1 h/ |& D检测机器人,爬虫,扫描仪和其他表面恶意活动。
( q5 _. p5 A9 ^$ E( u
* M0 L! e, t: z( g, \+ w k4 s7 W5 G- R- v2 ^5 r/ \7 F
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
. e3 i u. y, f, @检测通过Web应用程序上传的恶意文件。8 c$ q! t2 S! W. y( q& W
- a3 v$ o: G" q) e2 }: P7 Y, C/ a% O6 D9 U# s- l2 `
Tracking Sensitive Data(跟踪敏感数据)' x2 s: S$ U+ A# Y; }) k$ y Q
信用卡通道的使用,并阻止泄漏。
) n) M7 E8 V$ c- ]! v) e( E
! F/ d3 l: l9 D& G; ^
' r8 J5 T+ d1 ~/ ^! kTrojan Protection(木马防护)- x4 ~( ]2 { P, `& x
检测访问木马。8 S2 e6 {( w+ R5 z$ G: D
0 X- }* k) N H0 s* B; ~; Z6 E: x( p6 |9 c* L( V
Identification of Application Defects(应用程序缺陷的鉴定)0 r4 d: r2 x! i% ~' W
检测应用程序的错误配置警报。+ S" D/ P, p; Q6 Z3 t! m' M
, k$ H5 l& X. k6 s( n; e2 c- `+ p6 ^
' Q6 e, |# N( n: L) N: ~) B
Error Detection and Hiding(错误检测和隐藏)
8 X& Q: c4 h0 G! y4 A检测伪装服务器发送错误消息。. t/ j& z5 ]7 e
4 B- H. E7 N2 i8 D7 X' U: \! r' w4 d
安装ModSecurity* g# I% ^8 i2 Z' y5 j
软件基础环境准备9 Y; o. ]* Q" I4 H
下载对应软件包
8 C2 c. U, I! B# m* ^& K+ F' p0 [% s$ cd /root
3 b$ J8 K& {# w7 q/ _2 M2 h4 m3 O$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'+ `" ]4 e' g- u, o/ D
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz" J1 c7 E, j3 s2 P, c
安装Nginx和ModSecurity依赖包$ a& {3 w0 x6 ]7 E7 H6 R @
Centos/RHEL
" M& f% o0 Q& F# @; m( l9 I/ {% O9 G( S) f1 W; i
9 O7 u) |2 f( ]* w" z: G0 I! I: ]$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
% y, A2 F) B( T0 u' L( k: b3 g' ZUbuntu/Debian
7 g- e# r. C4 _0 }0 K/ z F: Y2 E; x) C: v9 Z' ~# z0 W' ^
& f, k/ z5 @; y' b0 ~
$ 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++-dev7 H/ N, v* _1 J$ U+ v
编译安装ModSecurity
6 ^5 l3 M2 n- a D7 X: RNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
* s5 j$ c, l, ]0 h2 {) L: s* k/ w5 m5 [# E; A( K
1 r$ C6 U6 Y) I M4 Q9 d7 f! o方法一:编译为Nginx静态模块
2 ?0 d+ }, _' Q1 N: D
" e( b/ j R) M" M s* }5 T$ Q( l& d+ O2 m% Z: y o( M
编译为独立模块(modsecurity-2.9.1)5 s. S# F- |4 ]9 J' U! Q
$ tar xzvf modsecurity-2.9.1.tar.gz
* B6 d% o9 b- K3 e+ E# V$ u$ cd modsecurity-2.9.1/
5 H8 O$ \% c( O/ }+ `# r2 L$ ./autogen.sh9 W# j, l+ {0 j F( v2 [
$ ./configure --enable-standalone-module --disable-mlogc, }# ^+ z; {: n7 U) N6 Q
$ make
6 H/ y: v9 P9 N: n编译安装Nginx并添加ModSecurity模块0 L u4 [! D! ~2 H- B) K+ I
$ tar xzvf nginx-1.9.2.tar.gz
' ^+ `. P; s3 N5 f$ cd nginx-1.9.2* I, \: B; t8 _& {9 }4 @$ U
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/ X" t$ {# i$ L5 F6 J% E% \
$ make && make install
( i" f9 l N9 S7 o6 i5 z; _方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
4 L6 w0 ]" n3 j4 }! n) Q* H4 N5 ]$ i! C- }( X/ A
# ]3 C( ~( M1 K. h2 J; R
编译LibModSecurity(modsecurity-3.0)) z& ^* `" Y: R( M' T+ h
$ cd /root3 v, p6 a1 A, t2 Q0 {: I
$ git clone https://github.com/SpiderLabs/ModSecurity5 t: m8 A9 M1 l+ {
$ cd ModSecurity
/ S$ j1 ?. C/ \) y$ git checkout -b v3/master origin/v3/master- l+ Q* `7 s4 G. {$ F1 ^
$ sh build.sh
# H2 `3 \5 W" W7 S+ n$ git submodule init3 c4 b/ o3 h& K L- S- |% P
$ git submodule update7 B# a" c" |8 V' R. M1 ?$ I5 V+ C! L& c
$ ./configure
- r+ ^& Y" x8 [" C, j$ make
- L) N4 @2 G7 t0 Y6 D+ ~; g$ make install. {( y$ e3 |, A) {0 |7 G
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
* O) T& v7 ~1 J9 {6 e$ k( U$ u+ E/ B& p' {# J+ V y
; I" A' W! ?! H+ V6 R) ]
$ ls /usr/local/modsecurity/lib
% ? B% h" y0 t9 Wlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0- f! d+ G8 c8 ~! X' I
编译安装Nginx并添加ModSecurity-Nginx Connector模块
9 R5 g2 E( x: j# F4 x. I使用ModSecurity-Nginx模块来连接LibModSecurity, v! K3 Q; n4 w
( y+ Z0 t' I" J) {7 |# @& m& e# ], C6 u* [8 ^" h
$ cd /root
0 p7 T) i8 K$ ]# l$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
1 _8 f+ x7 l1 y% D+ i4 q2 ]$ tar xzvf nginx-1.9.2.tar.gz
. z4 `/ v, q0 m0 B8 j$ O$ cd nginx-1.9.23 ]2 x0 d- h4 v% D! S/ o$ V( }5 c; T
$ ./configure --add-module=/root/modsecurity-nginx
% \5 p( G! N$ \/ x$ make0 c% D0 O9 c& @$ ?; D- n
$ make && make install
( B( Q, x) f9 L添加OWASP规则3 R) _' A0 v& j g2 J$ q
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
! T' E3 T& a" L9 W; n
' y8 q- \2 q4 l \" x9 R
- ?1 K$ E# O0 I! g/ L5 P* Y下载OWASP规则并生成配置文件$ W$ w( N4 \! ~3 P9 P% a
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
5 h7 K2 O) F+ |& ^+ Y1 h$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
' G+ ?7 l+ x7 I% z+ W4 _* D$ cd /usr/local/nginx/conf/owasp-modsecurity-crs8 C! V' x* L! f+ w
$ cp crs-setup.conf.example crs-setup.conf0 R/ E) ?! ~' A/ I
配置OWASP规则
. s6 y- h9 U; G5 c" P/ ?1 B编辑crs-setup.conf文件; h) L2 l" w' k- R
- o" _- k7 l! v
) f1 l& H$ U6 T9 e0 l% }2 h$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf0 l# H" N5 E6 E- u! ?
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf# m: c; j+ r5 o/ {
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
3 T# Z' S7 C3 E# V1 ^/ G8 j) K7 ]$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
' C. l6 z3 S6 W0 v, p/ {* U. V默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
8 ], Y. C1 d# A& L( k9 `: S) h( K: |% l+ d2 z7 M' p5 T) t
$ Q7 w8 V5 x' x# ?5 ]
启用ModSecurity模块和CRS规则1 V7 s% U9 {6 h7 l7 y
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。% x w# E, _: q7 j @
6 j* A) B0 L5 G4 C1 N( `3 C- i
* z1 I0 i3 i3 N Q2 Vmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。- F) L/ D" Y: ^" d/ y
! n3 x$ C7 J5 `* u' c* }% Q5 J
p% D& }' r, K' C$ cd /root/modsecurity-2.9.1/, Z5 `" F4 T2 z- u2 B: s% e( C0 S! l
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
. v+ ^: {; H4 V0 W/ H* m: w$ cp unicode.mapping /usr/local/nginx/conf/- s |+ Q/ Z( f* R: S/ r
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。2 ~% `4 [- s/ Y- S8 A
7 k+ c. L- \( K& ?. Y- a5 L& D. l. E* {
$ vim /usr/local/nginx/conf/modsecurity.conf! F' C' A+ j- j0 v
SecRuleEngine On$ O' }. n8 A# g6 Z7 R; s
ModSecurity中几个常用配置说明:# T3 ], z( b9 l/ H0 U' W- Q
+ ^% U/ S5 G* X6 B
1 u5 l8 m4 v/ ^" G) o+ e4 c% n1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。3 Y( s0 A, _% Y# X" V$ t
9 H2 ?+ m" b: G" t) E
3 S2 N% G8 j( R' W/ ^2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
0 @8 h8 e% L+ K8 m2 x9 x6 j; M9 H9 x* b" z$ x+ H+ ~7 \3 _- |" X8 j
8 ]/ H5 Q8 g, _4 P3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
% W$ e& |: U6 C0 ~% v6 S8 L; n8 K+ }! G7 ~0 C1 _& j8 ?( s
- M) l4 z: y. F/ @$ ]4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
7 c) }5 l I$ g
, r$ l( V7 h3 A9 M- k* S7 z& u7 Y! d/ `6 t
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
; V* W4 h- K8 {% e! ?2 _- O8 c, g8 f; {* h
8 H1 e! _/ `, R+ o3.x版本CRS
8 v; I0 i* q7 }# b# }5 d/ {$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
1 Z, y7 v& ^5 Q# g: h# 生成例外排除请求的配置文件7 t n6 N- E' [9 N6 ]& A: }, k
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf7 r+ R4 Q7 A* M2 X. F$ ?- g
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
8 ^4 f7 Q- n! [% n$ cp rules/*.data /usr/local/nginx/conf4 L. Y8 D6 ~/ t8 H: Y. d/ c: F
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
5 w! C9 P$ n0 q r6 h; b9 T L. g
! K3 J' q, X" N8 _$ y, M, Y5 }3 B0 a, K% n. @* j" r
$ vim /usr/local/nginx/conf/modsec_includes.conf
+ {! p3 R4 L. u5 s7 S5 x4 t! h. S! j; Y% v. q. O
[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 * Z- n- f7 {& `
9 I- z x+ N9 t+ P' A1 ]5 P! A- |6 n
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。2 f. B# [: l6 @7 p
+ C" \5 j+ |0 t# V0 L) b$ ]/ z9 e* a- W! ~8 w: c; M) s3 x
配置Nginx支持Modsecurity
0 v$ D* t4 N/ @# X0 d1 X启用Modsecurity, \6 c, \8 `! `* m4 ?" {
使用静态模块加载的配置方法
/ [7 u% W1 x! Q K8 c3 [: y; O在需要启用Modsecurity的主机的location下面加入下面两行即可:& \, H- l' _% K% z' r2 O% ^1 t4 d
9 s2 x, [: d g2 H9 i! M2 F% \4 `5 m! t4 K. A& F# _ r
ModSecurityEnabled on;
. [- U$ s1 S" l. m; c$ D! S$ UModSecurityConfig modsec_includes.conf;
% f& v) z& h! }6 k0 V, K9 l修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。- \! _* C# _+ R
1 h' {% O* N6 r* _
" ]2 h) `5 L; e( b" n$ vim /usr/local/nginx/conf/nginx.conf
1 @( m! ^' C4 n. r6 o$ f0 s q, z6 _4 J- d( p
, h9 n% B9 `1 F- c7 ~server {
4 ~0 }( `% T4 w5 y% F2 M listen 80;# J' C ^. I# \8 w9 L3 v
server_name example.com;4 e. d7 v8 o' E b
; h/ N9 A: ?- v1 z' X
: I7 n/ d J8 F3 K: M6 J location / {
' h2 N: B6 _7 g- U H ModSecurityEnabled on;
" I; h3 j! }6 e8 g6 S( x! h* \ ModSecurityConfig modsec_includes.conf;* `, }9 ^, P. g
root html;
, Q* Z4 Y2 q! p8 B index index.html index.htm;2 i. @' f) M. i+ L P: D
}& ?/ u- E4 O- R/ F: V
}8 E( C7 ? U/ |- \
使用动态模块加载的配置方法
7 c, s% W, a7 t# b7 r8 z在需要启用Modsecurity的主机的location下面加入下面两行即可:
( a$ _5 K9 Z$ q; J! c& C3 O
5 F# J1 i5 a0 h( T( R8 I1 X; e
' R& s$ f e0 e: I$ V0 tmodsecurity on;
/ S) ^9 z/ a+ T: l$ l/ a" Kmodsecurity_rules_file modsec_includes.conf;; V: m7 c# j. @: N3 K
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。6 |* m! I9 M6 S: s- h0 \- C5 X
! e# [/ e$ Y( V! I+ ?
( f) u7 _+ m7 `$ I; Z; L+ h$ vim /usr/local/nginx/conf/nginx.conf
# P5 h# @5 h/ B# B K- H# W2 y! a0 ]: A4 }
1 A2 R$ X, W3 F' P. Bserver {/ A+ k4 c* R, f Y* ~5 e% K
listen 80;
( k6 ^; x* [2 ]/ c9 ?0 h server_name localhost mike.hi-linux.com;
% x$ H$ D' N: A+ W access_log /var/log/nginx/yourdomain.log;) b: g5 u9 S. w' C
. Z9 w" D* p6 q L( v1 k# S" F3 J
. O7 z2 }' E& J' G9 x) G! O location / {- B- ^) X: P8 ~, O9 s$ U
, x* d$ B" U/ Q. V; Z2 w8 J7 E) R& ]8 d
modsecurity on;
6 ^" Z% Q4 f$ W% x n5 ^ modsecurity_rules_file modsec_includes.conf;
6 C/ k8 Y0 y2 f+ M6 x8 U: u root html;
8 I4 T* |0 K4 o% W# _2 o' t index index.html index.htm;. Y$ w+ A) X1 F5 Z
}( }% ]' n1 h' X- ^( ]
}
& A2 g2 }3 {# ^6 Q, j* Y7 c# s! D验证Nginx配置文件9 x2 C6 R( [$ ?$ R$ v
$ /usr/local/nginx/sbin/nginx -t8 ^$ f5 N0 P- E$ C' X
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok) k* V+ V) ~5 o9 Z+ x1 X
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# k, m. C. w! G" h0 x/ K* O启动Nginx3 g4 D1 g& _# @2 u
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf* j5 I, p4 U. x! \
& e7 U$ o; ?! h$ I7 A) m3 j I
测试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能够战胜更多复杂/未知/混淆的攻击模式。 . N H' p: T0 G1 Q. m2 t
|