ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
! X! E1 F- d4 F9 R" _8 g2 l& s5 D, L& G- S: H& ^* e; J" Y
0 t) Z' W' a5 Y- r7 q+ L在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
9 w! w4 y$ o" N1 I
0 i; o0 P. k. m: g8 Y9 ~
8 B, Z; ~. I5 U6 Z什么是ModSecurity
& e# y3 X0 c3 O8 U% E: i+ Y1 yModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
. `- V3 a9 P' w X
6 \% d/ t" s: w! D; b4 a# B" M! W
) i+ q5 H6 M! qModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
; S5 q* O4 {' `* H4 E+ I5 U* E9 ?( w- \9 F
2 a. O3 ~7 O* L V! X/ L
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
; Q+ u* w2 r1 ] o+ g _% Z9 e6 J1 ^7 C, B
4 g$ d) k# d$ [' K
官网: https://www.modsecurity.org/
5 K6 i; Q7 \! K# z! D
2 V+ Z& n& b {8 A2 J. m% z7 {- F8 ? A r# Z2 @' U
什么是OWASP CRS5 g3 \# F8 \) z" o9 H: r
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。: z1 ]" F3 o" N. A6 ~* P( R
- Q2 ] U& K0 o: M+ |% i" M
4 u% ^% v# K9 R8 B0 o3 @ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。7 B" H/ C) P" Q: S8 }& }
6 Y/ V8 k. Q! W- _
" L! d' X# n0 L$ H6 A
HTTP Protection(HTTP防御)
( t( ~: T0 d! q% EHTTP协议和本地定义使用的detectsviolations策略。
, _1 d; e: a8 j% r" g H
0 X+ v. v$ {7 q+ M0 P8 E! k! O/ a& R8 K% f% p! }
Real-time Blacklist Lookups(实时黑名单查询)
t! k5 b- o- D3 e1 b& M% r* E利用第三方IP名单。! M7 J/ M- g7 m* n1 E2 {8 l1 H! K
" I" H* l D& \
# q% {, x* F% d! z9 a6 G! x( l3 u+ b
HTTP Denial of Service Protections(HTTP的拒绝服务保护)( f9 h8 q7 ^) K7 J0 B' E8 G
防御HTTP的洪水攻击和HTTP Dos攻击。
- g) i" U6 ]$ {! m6 E6 s4 k2 J5 m: u f8 a
, h" Y. H% J; ACommon Web Attacks Protection(常见的Web攻击防护); w, [$ c P" ?: m! w: u
检测常见的Web应用程序的安全攻击。6 j5 w; q6 i p9 p" ^- t k
! n7 B5 g2 Z6 c- f4 s3 I l3 _9 }$ ?. u; J8 K; V% @* C* r
Automation Detection(自动化检测)0 r4 {+ [6 B2 y# s
检测机器人,爬虫,扫描仪和其他表面恶意活动。
0 N9 }9 x8 T5 m4 I4 ]: B; o9 X- y1 }6 z5 H+ p6 @2 w: t5 D. \% m
( ]" Z( I# Q2 N% V6 `
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
0 c8 T2 S2 D# C2 `! X1 | s检测通过Web应用程序上传的恶意文件。
. n8 J7 v7 R" X2 R8 T' P8 L7 G
3 G, u( N5 q3 P' B( M
! C( R; b7 _4 zTracking Sensitive Data(跟踪敏感数据)6 T! h5 D3 ` R3 m/ \1 e- G+ [6 Y
信用卡通道的使用,并阻止泄漏。
1 S; `, R& Q5 O8 u& x+ s% q2 Z: ~* R( t g @" g
/ F" w3 t, o. l/ ^) H; |- i
Trojan Protection(木马防护)6 D2 l. `7 d' X5 s% d0 T! [! }2 n
检测访问木马。
* S/ L; V% `' C4 K1 ^0 v
+ _# Y8 i" Y. K4 Y! @" U( i
/ Q, u2 N! X' xIdentification of Application Defects(应用程序缺陷的鉴定)
' I# }/ y$ b9 D# I/ p0 }检测应用程序的错误配置警报。! u. O+ _1 c9 P. @" }* z
8 I7 V: Z# H' \5 Y* [! T3 F8 W9 a% ?0 d5 r8 ?7 ~+ X- [+ X, ^
Error Detection and Hiding(错误检测和隐藏)3 I8 }. g1 [- h, K
检测伪装服务器发送错误消息。
; O; D/ P2 Y: E6 i% V
2 U7 F4 I* W" a8 ~0 X9 y9 V' ]* b& d1 W' h( r
安装ModSecurity. w8 Z# @; @0 {: b& p ~3 s8 |
软件基础环境准备" `. h8 e+ k0 r2 d( m* K
下载对应软件包1 _9 X6 S4 z' p2 m6 Z7 K
$ cd /root
: n. h- I/ @. u5 M$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
; Z$ V# x- o8 ?6 h+ {2 \$ j# 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
- @; D% Y# \: C6 g4 o* f安装Nginx和ModSecurity依赖包3 B& s/ ^' C6 F; H4 i, |
Centos/RHEL2 d6 x2 J5 _- F/ S/ c
. L& {2 k: d% @) ]/ u, y! s2 B% g/ R$ @5 V/ U
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel0 T. w! v5 S$ c0 U$ c
Ubuntu/Debian
8 U) p/ W$ B, O2 `& ?$ {% C; D- R; ^. [; C+ G) u
* A, }) j$ K7 d; a8 O$ 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++-dev8 B6 c3 d! q: g9 Y2 p- {$ ~
编译安装ModSecurity5 K3 T" w- h+ c4 }! j( K
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
; j8 S3 H# q$ f% y5 O' ]+ L9 t. R9 K( r0 m4 a
5 i1 D' @+ T" D+ u8 }' f) i方法一:编译为Nginx静态模块! U! k/ {! k6 e+ L. ]7 L6 \7 k- m
! U2 ^: ^* a+ }3 `# [" s( L' t; f+ M3 [! d
编译为独立模块(modsecurity-2.9.1)) A( A0 A0 i2 ^& Z( Z5 Q
$ tar xzvf modsecurity-2.9.1.tar.gz) \( q4 b" d o
$ cd modsecurity-2.9.1/, h3 V% b4 \3 ?/ r8 Q9 A
$ ./autogen.sh/ M2 g8 u8 X8 i! O
$ ./configure --enable-standalone-module --disable-mlogc
* ~8 E+ u3 z6 d; h) b0 I$ make6 P5 s i! m, E- ]
编译安装Nginx并添加ModSecurity模块) l& `$ p+ {8 [+ _3 W9 |
$ tar xzvf nginx-1.9.2.tar.gz) C8 Y k6 r& A# F4 l
$ cd nginx-1.9.2
% m. x6 o7 p) V; O* [ q7 j$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
( P9 {& P! T2 A; r9 i$ make && make install
/ B4 I: L8 |" [1 _5 c. T方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
, |, j) d7 \/ ]
l5 n- Y+ D9 Q% b, W2 r* B; S" e2 o3 M% K& ]' X
编译LibModSecurity(modsecurity-3.0)
; H7 {& A- f- o. l5 m* C" g$ cd /root- H7 g, s2 H: C# e- @- F: |
$ git clone https://github.com/SpiderLabs/ModSecurity
7 I2 h! y) J9 ^8 h0 W+ m% i% }+ U7 y$ cd ModSecurity
/ u6 ]+ n2 D! S' |, T# u8 x( W" {$ git checkout -b v3/master origin/v3/master
F5 ?2 i: m$ x- [7 o+ @$ L$ sh build.sh
: H: g. U7 E5 `& j, A$ git submodule init, P, u) V! b( j2 ?
$ git submodule update
6 G- |( a/ M7 L, I T, y$ ./configure4 w: v( L% {* m
$ make; O" f: T& A, }
$ make install F1 M0 U; v* J6 J
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。6 u- Y( H' `: P% H5 ~
, F \( ~3 l" b& J+ d. g+ n3 x6 h
$ ls /usr/local/modsecurity/lib
- N* U' k& r7 d9 o& _4 ~. W' P" olibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.03 J4 R; j3 Z+ X7 r1 P9 d
编译安装Nginx并添加ModSecurity-Nginx Connector模块
' {4 g3 R9 p8 q6 p使用ModSecurity-Nginx模块来连接LibModSecurity
+ X6 y2 c! w/ ]5 B
) f8 O5 H2 A* {6 C9 v9 j+ S# k" K2 \0 q0 f
$ cd /root! {. t. l G! q
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx: B! W* a! P) N2 e/ A" b# z
$ tar xzvf nginx-1.9.2.tar.gz
" f* |2 ?; X8 |+ l; b( Y/ H! v$ cd nginx-1.9.2% `+ G- b; S7 G
$ ./configure --add-module=/root/modsecurity-nginx
" q( }4 G1 o/ W9 A1 d1 j% _7 R$ make6 c/ V. a9 o* l* m
$ make && make install6 C6 q6 [/ ^/ F6 l. V
添加OWASP规则
( h/ ^4 w/ t/ u7 m' SModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
( @" G4 f' G# g% O& x! q# ?$ `. _0 f t9 ^. }3 b& j
7 @( z0 B1 P6 z$ D8 J( \' ^4 P下载OWASP规则并生成配置文件. l) G7 e7 R {7 ^" T# d% |7 P
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git G3 I# }! q6 t$ O N u
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/ X0 N4 a' w3 N& }7 O$ J
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
% r+ G! X! Z, B" u$ cp crs-setup.conf.example crs-setup.conf+ c3 _# L- }) j% J! O1 A- Y, M& N7 i
配置OWASP规则
* N3 [5 L% Q+ N; ]# k* t编辑crs-setup.conf文件
# O+ H6 O, B* x' S
3 s7 Z" C4 y3 s1 g% ?$ C+ `2 u) h( G( I# ]
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf. J8 c _/ p9 H. [2 o5 T
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
], O' N9 \1 |) L" n$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf% L9 \' U" `6 }9 k! {
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf3 ^+ o- s$ k9 O8 w. b/ ^( b
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。) w0 S/ V3 @4 G. r2 D+ K9 ~& J
h8 v- C* Z5 x% h& E" \( c! }
0 H' u# ]2 a1 i/ h
启用ModSecurity模块和CRS规则
( w' Y) I0 D; X2 l% Z' |复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: H. Z, v% b& V3 f0 W0 C' W/ S3 I$ l/ {$ J" J+ G
' E: x: J" h |8 C* Ymodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
# O3 _: A% R6 I( z/ d8 f
: P/ C; ]/ @/ b
6 V4 g% K- ]) v( W" ~- X. T. M6 ~: y/ B$ cd /root/modsecurity-2.9.1/
* K( ]' g! y' [$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
6 a$ ~- K+ q0 ^& j" ~$ cp unicode.mapping /usr/local/nginx/conf/
8 s1 b- d" T2 M9 o* j# Q! t将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
& I) V- P! _; U# i4 o! U& \0 m3 g7 I; P$ _ |+ Z- l
- C* U4 Y, _2 W! \ W7 T
$ vim /usr/local/nginx/conf/modsecurity.conf
) U3 {: \1 J- {6 P1 E7 I2 ~* ?SecRuleEngine On+ d v+ I% f4 g
ModSecurity中几个常用配置说明:
. |+ L& T- {* T" i! Q Z3 h9 n- S) s5 }
, ^5 Q' U& b4 [- o
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。" c/ D3 f5 g$ ]" @
9 R4 V( ?# S: F
; M) K* V2 c& H; g2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。: N) ]. F# M$ l4 X$ E4 z
% ?; K" ^ B- a/ P* h0 Y( {" I
6 v6 m4 c+ ?& I/ s: D# s; y) z0 i/ Y3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
2 g3 i$ o; |$ x7 s' ?8 ?# w
- |3 D9 X: S/ V% ^. W; G& D: l5 ^( t
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
F0 r6 u$ }; T2 y# Y, G( G0 G4 a4 c7 E1 o% x* }1 I
$ ?0 k# m- T" k2 d3 s9 D在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。, Z- S' h Q- X" @1 ]+ W
# x7 G( y2 e* n2 o" F/ Q
& \! |$ p1 v2 o2 ~2 c
3.x版本CRS) c/ F9 @+ H3 ^4 m/ m
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs3 I0 h* u/ }0 A5 l8 N% \/ G
# 生成例外排除请求的配置文件
+ h" {' M, s/ J$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf0 x2 a' K6 ], o5 `! C
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf0 k y7 r/ z: [5 z
$ cp rules/*.data /usr/local/nginx/conf8 e) y2 C0 A1 ?- j c
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
) U" R9 U3 @, f* a2 K) W) U/ P: J# n8 H- ~
5 s! U% y/ H0 y) M! m5 v' f: e4 n
$ vim /usr/local/nginx/conf/modsec_includes.conf/ _5 G7 o7 b7 ]& E& k
- M# |. }" n: ?5 V2 }! @
[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
- `7 A. T: t; l0 F! }3 s" f7 Y6 r8 T2 `: F
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。 i* |& Z- |( c7 N1 u- T! z
7 P; I& K0 V" P( ^2 w/ I$ Z& V
( P3 q- m) a- K! {% ~7 l配置Nginx支持Modsecurity
: `/ j. T+ f; L8 I+ |启用Modsecurity
5 n6 g8 S5 c9 R0 b$ F使用静态模块加载的配置方法9 w( x7 b" f0 Z) T
在需要启用Modsecurity的主机的location下面加入下面两行即可:
Y9 e8 H' _ t9 L# I# v- w4 J* _6 \1 x9 H6 {8 l) x
+ H' [& }3 C) r7 a
ModSecurityEnabled on;, p4 F- G6 B, B3 B/ Y5 E
ModSecurityConfig modsec_includes.conf;$ Z$ c6 j4 n5 A4 |
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。' A) W! U/ v) A+ V# }$ B
* N; R! x( O( i4 S
8 W2 B) ^0 e+ P& X' g. e
$ vim /usr/local/nginx/conf/nginx.conf% S ]: p s2 A, j
8 u' E+ p6 Y4 T4 f7 X
: \. {2 u, Q. ]: l# g+ Hserver {( W: M" T c7 _
listen 80;
& j8 V* d# {3 D: \7 p% X7 V+ F6 y server_name example.com;) g0 r9 S: S3 d' b
- T; G; ?/ L# ~
& o; V5 a1 s) ~- a8 P location / {
* [3 u; V' I# b( A" }# W- y2 l3 P ModSecurityEnabled on;
" h0 o" c1 q; c& M4 m9 a4 M ModSecurityConfig modsec_includes.conf;
7 r! S+ `' [. A* q+ ]* J) L" T: d root html;
! U3 }& g* x. p$ I$ U# X" O index index.html index.htm;
+ q; Y. i% i1 u4 z; ?/ x8 D" {) S }
3 ~& n. Y% m3 H. c}
! X6 v0 x6 {* E' A _使用动态模块加载的配置方法! A- g d' D" k0 B
在需要启用Modsecurity的主机的location下面加入下面两行即可:
; Q# P" C$ `/ J: H' P6 k
0 ^ B5 L$ D3 @
/ I7 b- e8 V" mmodsecurity on;5 w4 ~1 f* {9 c
modsecurity_rules_file modsec_includes.conf;1 h- a/ l( s1 F8 g& F1 i
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
* b/ ]3 n4 Q/ a- ?5 p' q$ C
! `8 @9 P+ L- v% @$ X. m; L( v. T% w D6 i- b/ s4 `6 Z( Q9 F8 f+ _
$ vim /usr/local/nginx/conf/nginx.conf( ?- H- [& ]9 k& D3 @- n% s; k* K! A
5 S+ R& l0 j; i( n: a2 |+ B
7 B. E+ _/ X' [server {
* t2 ?- L! S6 i u listen 80;" d, y; h# R: v/ L. d8 Z {! h
server_name localhost mike.hi-linux.com;1 B! c( E- U7 w6 n7 p$ P& O
access_log /var/log/nginx/yourdomain.log;
, r R8 R: H( l! y; q% O7 ^( D- o6 z9 i1 b8 m* y9 o
y! `4 s$ c1 c& u9 S1 o location / {" e$ c5 y3 C' g, I) i& g* c5 R: J5 K
6 p' U) e1 S2 ^6 S
. \6 b5 u2 L, c. u* u3 z8 V2 w modsecurity on;
- {; g' m( S- u. v/ i# b modsecurity_rules_file modsec_includes.conf;% o* u. ^: a/ G* p+ y0 m$ ~
root html;
9 y/ f1 z, r0 }* t index index.html index.htm;
6 p5 w. M2 M# f* a# O}9 ?; o; g% ~' W3 D& Q+ E. I& ]
}
5 F1 C( q* F, o# r9 D% _8 g验证Nginx配置文件8 g% C( L. W1 N
$ /usr/local/nginx/sbin/nginx -t
0 V& w: m6 X# Z/ onginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
" Y) Z2 H% l# I( Unginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
5 H6 H' ^- D6 Y1 ~! e, d启动Nginx% S) ?5 e- m' _6 R2 e
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf; h7 H" W( S H# z
( R" f1 h1 A2 J+ |- A) J) y测试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能够战胜更多复杂/未知/混淆的攻击模式。
`4 j+ p) _* }. t E2 j |