ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。. w- [3 g6 H) A8 ]
' k! x3 _1 Z- u- i3 N D: |+ U# U
* f/ e8 `% s6 F. [* z/ R' i/ F) { q在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
* r) s" q8 p4 I# `) c/ V
3 \+ o" I Y7 Q- s3 |
( T! Q8 i" G- a2 f什么是ModSecurity. H: H- m/ C) M- r. o1 i3 ]
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。; s4 A5 m. |: s1 @3 Q" j! l9 l
$ z5 G1 I9 o+ c m
/ s6 E8 m+ E) a' w/ z
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
- Q9 I* o) u3 f+ o+ x, [# ^) s8 u4 O, x
, p( Y* J6 ?5 z# ~0 }4 a* |ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
# Z0 U6 G1 c! a/ ^) @+ T9 _# W4 C
* r n: _- x* y/ t$ ~3 p
官网: https://www.modsecurity.org/% m' R: J' i! n
) m7 {% \( {" h) d. X2 ~0 k$ v' w0 u) ?+ R: C6 |" f& A) d
什么是OWASP CRS
" z! k0 k6 U" l* D- |3 z4 DOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。; O8 h# f' ^4 @, H: v
! v1 p2 b, O! v% d& |; @
/ e4 A/ z0 ]- [4 C# t, q2 GModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。/ J; y5 k) b P
4 D% N( e; f. p) C; F- G6 N( [2 f( @# k
% |( l; n2 C3 p; \HTTP Protection(HTTP防御)+ f+ r* n( @0 P) n* l. }
HTTP协议和本地定义使用的detectsviolations策略。/ b6 H- W4 ^! R
% \7 i7 k9 C8 j4 O' R/ N
2 S& E8 K0 y# o5 {+ u0 W) X9 lReal-time Blacklist Lookups(实时黑名单查询)
% G2 G) v5 h! @) P* P# C2 T利用第三方IP名单。
# U0 a% x e8 b2 x
& u. c$ N/ G8 r/ e3 i9 Z5 X1 ^7 T f g! i7 H7 ^5 [
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
6 h& P/ g6 G; a( K8 u9 ~/ m防御HTTP的洪水攻击和HTTP Dos攻击。
; e0 ?6 Q/ }9 i E
7 j5 B. ]3 k' A) c/ Z+ i
) h* u- _4 Z O5 y6 }Common Web Attacks Protection(常见的Web攻击防护)- X6 g9 @$ v" j `
检测常见的Web应用程序的安全攻击。
% p7 ~* p% [2 }: V% u2 s* y
* @3 g. a# u1 o' ~. E
! ^/ S9 V8 L: h, nAutomation Detection(自动化检测)
+ P- x) |- n g P# k8 W5 t. d' \/ t0 M检测机器人,爬虫,扫描仪和其他表面恶意活动。
% E# H4 ^" U; a5 D6 C- k" ~9 ?) W+ q: F2 G3 J A
* K6 v" p( m* u+ L" m3 d) V8 ]Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
_2 |& ~' M9 X! r7 ` C h$ B检测通过Web应用程序上传的恶意文件。0 H( c" ?, r* a0 ]/ S s
' c. |& j! ^6 }6 u$ q( Z0 E# @
! @; x8 Z2 [4 a4 FTracking Sensitive Data(跟踪敏感数据)' p! ]+ i+ o, V A& E: V
信用卡通道的使用,并阻止泄漏。
0 l) i2 P% S$ b2 f% C3 g" y
" j0 F7 Z6 \+ ^ e. G8 n
1 w" W. `% ~; Y4 O @, p- lTrojan Protection(木马防护)
- @ g" N5 y. D4 o/ {检测访问木马。
+ q4 F8 l1 o6 T
/ g- K* M z- n. } V8 @: a2 m& Q+ G# `, C+ I$ {
Identification of Application Defects(应用程序缺陷的鉴定) }; w2 Y9 C- `- H- U
检测应用程序的错误配置警报。
6 }% T+ s+ k& @) T; b# {, ]0 N( r3 R4 S" V- [1 k
0 d7 D# v- R- ?7 t, v" w9 a' H
Error Detection and Hiding(错误检测和隐藏)7 b7 N( V% Z4 I
检测伪装服务器发送错误消息。& M6 L0 R8 O) ~% Y4 Q5 C
% [& m1 o( |8 E6 f x- I
0 o* c0 `+ u4 R# u* _3 d
安装ModSecurity, S r, C& _' n8 i5 D
软件基础环境准备
& H* q- Z; e4 y& Y" J下载对应软件包
( y d% _8 b2 O2 Q5 |" I% i$ cd /root
! H( N4 h- N9 E, X. x3 w# N& b- Z$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
" |0 T& Y2 g1 {' N2 N: V4 h' x$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
2 `" ~: _- o: t4 R( D' }安装Nginx和ModSecurity依赖包7 E1 J5 n' i1 a0 P8 q. d
Centos/RHEL* w" M" Z$ x+ s5 w! X: n5 P3 e
/ ^3 i% C5 D5 a; r5 Z& t4 o% H, |$ f# b# [" k7 J A
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
' a7 S5 V! w2 E1 Q: S' x" aUbuntu/Debian6 d! y6 J6 h+ }1 t
7 I+ U2 \% T* L* E
2 U& |9 n# o; G5 y3 W# H! ]$ 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
) M! m4 x" N0 q ]4 o' B+ Z编译安装ModSecurity0 i$ W4 q6 }0 I) l8 e) |
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。 @; S" @# L# X: _
& R1 b; |" R. ^ N
9 \7 V- Y: P7 z8 @; H( X
方法一:编译为Nginx静态模块5 z% H a& _) {6 y+ d% f l# P
- S' Q2 ^/ F) @$ \6 s5 c6 l
! q0 q, R, C3 C3 |1 |" z. D
编译为独立模块(modsecurity-2.9.1)* W! S& O- U/ @( ~" n: J
$ tar xzvf modsecurity-2.9.1.tar.gz
/ P5 P' w* m9 w1 r7 D9 [* u$ cd modsecurity-2.9.1/
2 h; T3 ~2 u3 u3 O$ ./autogen.sh
! W+ a% Z$ U5 r; C8 z1 t$ K7 O$ ./configure --enable-standalone-module --disable-mlogc
7 i+ [+ {8 S9 x# x" l; Q9 p$ make
2 C; H: @0 y& P0 Z' H编译安装Nginx并添加ModSecurity模块2 Q4 y& Y1 a/ h$ M" R
$ tar xzvf nginx-1.9.2.tar.gz
) k0 @1 C( F: W# [7 H0 ]; \$ cd nginx-1.9.2
) {1 w; U& C( K+ B5 [0 z, L$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
7 B8 G; @% Q, C8 o% b' E0 Y% ?$ make && make install
- t2 G$ G" J- \- U& ?7 i2 a方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
' H' Y, _! m' ?# q1 ^6 w. c2 ^) y+ v- t; [
2 z; W, x! _( O
编译LibModSecurity(modsecurity-3.0)
( t( p* u5 t a; c N# V$ cd /root
3 K5 h B/ }! W# d* P$ git clone https://github.com/SpiderLabs/ModSecurity
; ?: O$ {% r5 h f: j+ D# c1 ?$ cd ModSecurity
" x D) @% ~2 x3 ^4 N5 b) C" D$ git checkout -b v3/master origin/v3/master+ e) D' n; ?1 m6 P9 r, s) W
$ sh build.sh2 n# Q& e: f7 g) ~5 b0 r
$ git submodule init! c% i5 z. y5 w3 _" V8 G
$ git submodule update
1 r1 } L9 ~7 x; c; A2 D; K$ ./configure
/ t) |2 s- M, f2 n7 h. O) R' F$ make4 d( h- F! ?5 x3 D9 B
$ make install
. Q2 K U" N1 j3 ]. C5 sLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
2 D2 S' L/ x% N* ]/ |7 F
! ]$ a/ o3 ~% m( p, U$ k/ ]* t
& {5 I [ b ?* i3 q8 \$ ls /usr/local/modsecurity/lib6 {2 d4 g9 r0 \' r2 \0 X# O( V, w+ ^
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
4 @' n% R' Q8 U编译安装Nginx并添加ModSecurity-Nginx Connector模块
* B' w6 ?5 y4 d8 N: `1 ~% v使用ModSecurity-Nginx模块来连接LibModSecurity' ]7 }( F3 `8 |# v
7 [1 K6 I4 V X2 T/ e X5 z/ u! Y* x( d5 F8 u" U& J5 {/ S
$ cd /root3 Z5 v, A |9 b4 | E& P& d8 M
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
* Y- t* {! l% O: H8 W$ tar xzvf nginx-1.9.2.tar.gz
2 ~. u+ a3 t- a- }3 X6 I+ D$ cd nginx-1.9.24 i& u! S% H: z @' p0 s
$ ./configure --add-module=/root/modsecurity-nginx
6 @/ X f8 s: B2 E/ A$ make
( {( l9 \. C! b9 a; ~9 C$ make && make install
0 i9 a1 H# I. S2 f% S添加OWASP规则! [! V/ P( M, Z1 C* |& r+ L* @! X
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
* f$ j6 X' a p1 b4 ]8 O3 b
! d1 L& O, }. l8 q$ x9 ^3 C& b4 g b# H5 z. y6 D6 ~
下载OWASP规则并生成配置文件
3 R* b T' q' r4 G3 u5 ?$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git, F4 l5 `$ F# [7 L3 L9 K6 I
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/- U0 F1 O$ z6 o- F- Q
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs I' j$ X7 v2 P' U% ?- I$ k u3 L2 K
$ cp crs-setup.conf.example crs-setup.conf
" ]: O" r% u9 ~2 u; b/ V% F# A5 X配置OWASP规则/ w6 e/ r# t. o
编辑crs-setup.conf文件) s# R0 q# `1 f
$ I4 u6 U3 }; Y9 X$ s( |: w
& U8 O3 O; J# _, I' S- q2 z$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf% W- I! i1 b/ w+ {5 W& }/ k& K
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf: j& P) w6 ?" H+ [
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf% d& `9 E0 k, |# l
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
# x* |" ]9 J+ T: y/ w- k$ @$ @默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。* J) n4 w' \& r& a6 e: B
# q8 t+ |/ _$ X n
# F( D6 T2 R7 ]; D# M: k+ A; D启用ModSecurity模块和CRS规则
0 {! A' n* `& o8 u8 d复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
, q+ F' ~% y/ J0 z# d2 X u- Z: M: e4 j; Q. a4 r, m$ Z
1 W# d$ W- g0 o: B' r# fmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
2 u/ J! V+ c$ A( _+ n
; t7 _7 i: p1 G" W% a' D9 P8 x$ o8 S& l* Y, J
$ cd /root/modsecurity-2.9.1/
: ^. j- g8 r( ^$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf 0 u/ ^# y. s) M, ?3 X- ]
$ cp unicode.mapping /usr/local/nginx/conf/) j7 y; X2 D8 F4 A2 C
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。! e4 q. V+ \/ c/ M) |
- ]0 P; ? Z# ~( L
; _: T2 q2 R' j' c2 H$ `8 I1 v$ vim /usr/local/nginx/conf/modsecurity.conf& X5 x1 }( W4 f( c. X- q. `
SecRuleEngine On
$ w. `% [/ f7 }/ D' U' XModSecurity中几个常用配置说明:. f1 |6 o4 _* U( ?9 @3 ~) W6 V
3 z! {9 L; d( j" j
: }0 o# m" A- f9 y+ v, m8 ~
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
& B9 W( a5 w2 z5 d; e+ q2 n0 J; |$ H+ _
. {# Q! B( g: Q5 j1 i2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
. k/ Q2 u1 a/ Z& I( }) w
, Q5 N9 c1 d+ d0 \) W G5 N) f$ V2 K6 m! q- h1 V
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
' L: B0 J. E4 u
" u8 P5 U: t' C! [7 }+ F% _+ J' J8 r, ?" U3 ?( p9 F/ r$ p
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
" l# M3 E; V! l) X! V8 ?' ]4 e
/ L6 k4 Q2 N f+ t1 u3 A/ C" Q2 t1 ?3 _& a( C
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。, o- e+ u" s, r- C; ^
4 i J9 w. [" f- g
2 b0 n$ G. R: n$ R+ l3.x版本CRS
e6 ^& m( h9 \$ cd /usr/local/nginx/conf/owasp-modsecurity-crs5 b: Z# m+ F, S* y& U/ F, z1 T
# 生成例外排除请求的配置文件
' Z# m$ q9 j/ e! I( G G9 y( j6 M& i$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
$ P) v/ A! E) ?( M W2 X$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf9 O7 v: U) A' z- X/ T
$ cp rules/*.data /usr/local/nginx/conf
2 L, S- b1 D2 y! `9 V7 w1 i2 }; |为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
! T( r) }1 u1 T; f9 z e, a7 g4 D( l# [; p
2 V& u4 @, [7 P1 P5 |& _5 P
$ vim /usr/local/nginx/conf/modsec_includes.conf$ q+ Z b6 R# v! M8 E6 |; Z4 |
- n" [2 i0 v: l" o: i6 h W& J
[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
+ Z9 H9 d. c: d; d
/ a3 i9 e9 }/ e% B) E N注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。, L5 V' X* G+ N
( S6 m+ L8 O5 q( E1 M, j
* S0 e2 I0 Z6 P {
配置Nginx支持Modsecurity- V/ ~4 K7 g* ]) e, { `2 Z
启用Modsecurity* o9 |1 T1 O1 c; |6 J8 t! p
使用静态模块加载的配置方法
- O }- C- X9 @& Y在需要启用Modsecurity的主机的location下面加入下面两行即可:% u! `1 C) M/ Y$ X I) O
, S; Q2 A2 K" ~8 f# x- W
$ @6 P+ g/ D" g, BModSecurityEnabled on;
' j3 g/ _4 n; P0 a2 b% @* p# [- c0 TModSecurityConfig modsec_includes.conf;. T. g/ Z! k f6 Q
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
1 s3 H" f9 ^) r( n/ w2 t4 C& w
. z! M. p5 O2 Q" p$ i$ vim /usr/local/nginx/conf/nginx.conf
! V0 `7 [, T+ K8 Z' C- Q7 j
! {. F- f5 L* U6 Y( V7 X5 k, I4 H% V! p5 }
server {
$ x. n# H+ M4 u) m( F listen 80;9 \* l Y1 L8 q. ] \4 ~
server_name example.com;7 k( d7 s. {! M$ T6 _
6 E5 p9 e' H% X/ H5 H
1 w- O4 g" M- j, t/ g location / {
2 g8 n2 [! P5 H( B1 S6 `3 y+ { ModSecurityEnabled on;* \# p' [ N1 ?% W
ModSecurityConfig modsec_includes.conf;+ d5 E' h! m9 N
root html;
" @# X8 ]% f7 U5 y1 X/ n index index.html index.htm;
' X4 O* M, ]8 I' s R" l }7 ~( S8 B) a8 I2 c/ n2 l
} N! Z# n' q2 W% A7 v
使用动态模块加载的配置方法/ o$ Y6 T6 |+ ^0 C! i m) j
在需要启用Modsecurity的主机的location下面加入下面两行即可:, K+ i2 P0 T/ N
8 l; l! r+ A: U1 M
" P6 t: y) B0 c8 Ymodsecurity on;8 x1 {7 b$ }* Q" J; _# X2 u, g
modsecurity_rules_file modsec_includes.conf;
' f5 X# x4 C( P4 F9 U修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
( }, h# e7 m6 ^! l: O8 D. l. g4 |" X9 P6 |. w& G) l+ s, m" B) Z
! \5 z* t" T' V+ u$ vim /usr/local/nginx/conf/nginx.conf
! j" v7 f/ e, Y2 e' S" C# Y" C( X" |" }
' o: e$ P- O {1 Z" t
* ~; Z6 u- @0 s, J4 |3 R7 c1 tserver {
: K8 ^* C7 c M listen 80;7 @0 p5 E: C0 d6 [
server_name localhost mike.hi-linux.com;1 a3 d: S( a; K
access_log /var/log/nginx/yourdomain.log;. U" `) B6 M1 E2 \" A
: u4 x' v7 }- v* t+ H$ c, ]- G5 j& D$ V* y4 r8 r
location / { d( ^5 c& E* Z5 l
# j9 u. o3 O8 O1 _0 U9 U. o; L$ L- J0 @9 H& e% L7 T
modsecurity on;+ I% u2 K8 L7 ~
modsecurity_rules_file modsec_includes.conf;% r' y/ O1 y: Y* b6 \
root html;
4 T" m% X f5 q2 ]) _2 X% E) b; s index index.html index.htm;
. ?9 D4 Y& E8 y: m- V}" R! K+ i W/ e
}& @: [2 t& I+ ^! |8 Q6 a
验证Nginx配置文件
* q( F, s; q J" m! L" {/ U. w6 P$ /usr/local/nginx/sbin/nginx -t% B0 q) o7 l9 [! X- W
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok4 f* Y6 c( Q( d5 S) W0 y
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful3 `" v3 {6 p e- s% `+ ?0 U
启动Nginx
* V$ m& }# [0 [0 }$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf- z6 u$ k' n! V8 x
, r# d+ g4 P/ A- `2 T4 J测试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能够战胜更多复杂/未知/混淆的攻击模式。
; P* T) X2 u2 J; o: K |