ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。+ f, J2 s/ q8 S, B
9 P3 n* R2 H+ @- l( o; I
- b3 ^( ~# Y$ E
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。* {/ ?$ g! U- Q% y0 g
a6 B" R/ I9 H/ Y, e
: h7 G E3 a7 i1 O& [! p什么是ModSecurity" \& g+ Z- v V0 F' F! Y5 w
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。; q) I8 ]6 @4 y7 ~
, \3 ?. p5 J$ C- y6 t& O0 ?% Q3 U5 K0 P: ~: P b$ l
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
4 R' h* s* ]2 H
4 r! c R2 o% y! n3 {; O
& I f% B- V3 E* A& A" rModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。( g: N1 @" G) U/ N0 E: v" P
- `9 \" ?) C. N% `
% ?1 P: D$ Y: W9 {$ b" }官网: https://www.modsecurity.org/
! R' y/ P( C) W1 b- o6 ^! X+ v2 {8 t+ J: v
$ E& w, Q1 _* k; w/ K2 m; N8 ~什么是OWASP CRS
" O7 l1 B+ l' {1 V# D! QOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
5 ^( @9 Z* I3 a; c7 p5 e1 {0 b% S9 V) d
8 D% }$ ?- I) D6 b: s" ?% _+ MModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。" f4 T' Q9 F0 s/ h. q# [( T
a! X! e) x) t) b
7 H* |0 `& ] @. C
HTTP Protection(HTTP防御)/ o- g% o6 o, }+ V" c; I2 A z
HTTP协议和本地定义使用的detectsviolations策略。1 B* Z! F1 D8 G, Z& a+ W, d
\, b% ]/ C6 h4 G+ j. i" m: I: a* d8 n- I# ^3 ^7 T: ]7 _
Real-time Blacklist Lookups(实时黑名单查询)# D! e& x% Z5 _+ r
利用第三方IP名单。; D. h1 Y) u5 v: F3 t) X
' Q% N4 B1 R: i
# @3 y& Y, e9 @& gHTTP Denial of Service Protections(HTTP的拒绝服务保护); c E' ]3 u) m* u4 o! K
防御HTTP的洪水攻击和HTTP Dos攻击。2 I/ g$ @ [7 r3 A6 a% u
& _5 m" z! P# t; Q. V
1 h: i5 r2 A2 h8 t: L$ z3 ]! PCommon Web Attacks Protection(常见的Web攻击防护)
7 ]! E" b/ I# B8 ~+ h8 E检测常见的Web应用程序的安全攻击。
& a2 u7 O5 X2 @1 F% g/ c, k
' R/ A3 I/ [! E Y8 x Q* o' Y! |9 _8 s1 _& @
Automation Detection(自动化检测)) f4 w: r& I- T. T3 l) `
检测机器人,爬虫,扫描仪和其他表面恶意活动。/ w( P4 t8 A F0 W d
1 C8 ^' Z, x' P( b4 y+ u* |8 B4 p% ?0 r" H7 U9 @' `" q. M: X
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
% O7 J' w, }+ X( L( e' m检测通过Web应用程序上传的恶意文件。
% S$ A6 p4 D1 n$ f/ p
, M0 _2 J: B3 {1 E! C- F0 B
- P, i4 V: B8 j- U0 STracking Sensitive Data(跟踪敏感数据)
$ P2 ]* w7 ^+ E5 J% @" Z$ ^" m$ c信用卡通道的使用,并阻止泄漏。
! w$ R5 T8 v" R0 {6 f+ W: l% Q6 O0 l9 A& @) g( S) w3 a
7 f2 n/ @6 p7 bTrojan Protection(木马防护)7 T: N9 g0 h/ {5 O; O% _6 n
检测访问木马。
. V! ~- J2 o: X* j% H" T1 O4 c- [0 i* Y$ ?( \
: O9 {$ Y, Q: w, Y' Z8 {* `Identification of Application Defects(应用程序缺陷的鉴定)2 E. n6 j1 D; ]6 ~. u/ h ^/ H/ Y# i
检测应用程序的错误配置警报。
7 g% @9 _* r) d# T
; z7 c- Q. q) K, A3 @9 k1 F3 P6 V( A X8 u4 p
Error Detection and Hiding(错误检测和隐藏)$ ^# y! J" }8 i7 D: ?
检测伪装服务器发送错误消息。0 B2 m) ^% K; h; h4 v
8 P1 e8 D2 G1 f4 ]
4 w$ y0 ]2 \ H, G- e! A6 h安装ModSecurity# u( i- s/ F: |- b2 c8 W+ P
软件基础环境准备# r, S: s' M) a' Z( N- \
下载对应软件包
, c7 I) }7 [1 ?% G4 ^$ cd /root
" S$ b& S7 {3 l3 j8 `& U$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'9 @9 C9 w! z6 \8 i( R% Z
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz9 A O8 M6 C9 ^. D; U1 J
安装Nginx和ModSecurity依赖包0 q' f) U5 c4 ]; {
Centos/RHEL, ?/ M: O \: B. |& i% V
6 w5 z6 V$ B7 [7 M* _- u7 \4 }# h# |5 w/ Y. u5 I
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
5 n& u& F! i4 ]9 a% |# s' Z( gUbuntu/Debian
% |5 u4 S5 l; j- G$ x9 d- I( q" m0 h: R# z
, G( B5 |7 H) }* ?" d$ 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
: D9 R( \: X! n/ c V; Z1 P编译安装ModSecurity1 G6 }5 s% v0 t+ k0 r' ]
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。& K F9 C, G. Z( U! ~
" O5 w! y1 Q( N2 E; f7 m) K T! [2 F+ X& U# q% y, A
方法一:编译为Nginx静态模块
3 O) ?; W2 d$ n3 w5 C( G2 z A$ Q
' e6 Z+ W. K7 G7 Y# \& |" ]
编译为独立模块(modsecurity-2.9.1)6 t, v0 B4 @, ?, j5 A: r2 U
$ tar xzvf modsecurity-2.9.1.tar.gz
% Q# \, {6 W4 L: C V, f$ cd modsecurity-2.9.1/# I S& s' n2 ?4 d, w+ C4 {7 m
$ ./autogen.sh
0 ~6 ^; e- R) M1 m5 X1 L' S$ ./configure --enable-standalone-module --disable-mlogc7 v; @% V% `; z/ Q4 t
$ make8 }$ z* g" @: \5 y2 u
编译安装Nginx并添加ModSecurity模块
/ M. T& }9 l0 ~( Q$ tar xzvf nginx-1.9.2.tar.gz" h% |; g z2 ~% P
$ cd nginx-1.9.2
5 ^( Z7 k0 V: C3 g v% A S- h9 J* s$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/2 p+ d' J; y* p0 r' J+ Z; {
$ make && make install7 N: E- c1 t4 c1 N( N) W- o# W
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块3 B/ D* x& J- I: d
4 H4 H; T/ |* x; ]8 D
$ X z) H3 r: [9 v0 P% j
编译LibModSecurity(modsecurity-3.0)" ^7 N* p/ {! {' }- g& u; o# S0 w
$ cd /root
+ \, a6 F& P$ m/ }; U! f& O$ git clone https://github.com/SpiderLabs/ModSecurity
! N7 F9 r% a. j; S$ cd ModSecurity* F8 N2 a+ s/ z. {
$ git checkout -b v3/master origin/v3/master
9 H' b! U3 i2 z& ?% f N$ sh build.sh o1 E! N* B6 Y
$ git submodule init9 @4 U: O$ Q1 B4 e5 g+ j
$ git submodule update Z9 b. ~ x" i& a+ E* F# j
$ ./configure
( w( t0 X' i0 h& a* h- g$ make
$ S3 r% p- h3 e" H$ make install
3 p" P4 A, A# | m# J) e& hLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。6 z( ~1 R/ @1 x. \* b a) d% L
) W# q$ X0 \9 F) A* D0 j- M
* S x5 a& K% S) N3 R) X/ M
$ ls /usr/local/modsecurity/lib
3 e5 }+ K1 Q' t/ V# `0 Zlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.01 T$ _* u# O9 ^0 ]3 C7 l
编译安装Nginx并添加ModSecurity-Nginx Connector模块
: a5 y8 N0 q1 J1 U% g/ k使用ModSecurity-Nginx模块来连接LibModSecurity7 v8 \6 y1 d1 Z
: y9 [5 V1 j, d2 ^% E
9 o/ S/ N0 ^+ Y. T
$ cd /root
3 y+ |% }# q4 X: s2 a3 _$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx4 O( v6 n% m7 o9 F, K
$ tar xzvf nginx-1.9.2.tar.gz
% _7 {0 ` p; i$ K6 I9 _6 L2 | G& \$ cd nginx-1.9.2
( B1 \9 \ u! v' H0 T$ ./configure --add-module=/root/modsecurity-nginx7 M4 A. V* c) r' {! w, f3 n0 m' ?
$ make& v+ h$ n! c6 T5 \
$ make && make install U. @( M6 @7 Y/ s6 {
添加OWASP规则) l2 r K* C. f
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
( q2 k, A) k9 `! N% d: p' I+ s. y/ N
5 F- O, P# j" J; J6 K" H
下载OWASP规则并生成配置文件
) l8 ?; p- b2 f( i, X- c; I+ h: `$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
5 P: S) z2 K$ V$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
0 L6 H6 ?) c1 d! `- i8 D0 B$ ~$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
: g4 k p5 ^( Z7 G: W) A( B$ cp crs-setup.conf.example crs-setup.conf
; B' e! w- L$ t0 D- Q配置OWASP规则
) R+ {4 A3 Y' {4 e编辑crs-setup.conf文件
) m% V9 F; M' \& c$ M9 }2 f2 D0 `0 y/ q8 P( m( C
2 F* d" c# L+ B+ D8 f1 B9 A+ W
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf" G& u Z* h: x0 x! P1 j
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf& p" N$ ^* b5 S$ o, X" y6 \
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
/ y$ o# u1 m. Y" B- x$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf- F$ C, T7 G+ x% j m* }6 }4 P
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
' w+ x- m9 e7 s0 Z& {8 R% T8 x+ X8 `; d; I! p- F, V
6 H. C$ S, J5 k
启用ModSecurity模块和CRS规则0 j1 D4 q9 f0 i5 N% [
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。5 j3 R* v. J) k) ~: R
. q: F2 h6 \" B
C K3 _* Q: t4 m/ |- v9 V9 L4 Lmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
% e8 x1 z6 k C4 S* X( e: s5 J7 U" q3 L* Q4 i
' P: U0 `1 V. P$ cd /root/modsecurity-2.9.1/
' k. V3 f" m* T! w& A! S$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
2 K& Z) v7 j4 U6 U( V! d4 [$ cp unicode.mapping /usr/local/nginx/conf/
# A) m9 T8 s. _9 D3 _/ D将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。8 k I8 Y ]6 F9 Z" P
, W& {0 \8 L7 _
9 \7 x6 z2 w, r, B. j1 U, P! P$ vim /usr/local/nginx/conf/modsecurity.conf! X3 O {( z& m7 ^
SecRuleEngine On" z# @. `6 ~4 z: B$ b
ModSecurity中几个常用配置说明:; F2 ^7 o8 B) h2 }
, ^! y- r2 e% `0 f! |# E1 \
" H/ b3 U% [ I' e2 ?# b) S7 X1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
0 b/ S2 N* a" ~. `: z2 l# }( D/ i( ?, v# ], }
- S( S Q/ ^0 h4 A8 ^$ @$ }% q3 D
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。" c/ i; H/ m. L4 S. H
6 |5 X3 ]- d& |3 h Q
( m% E/ @" E7 O$ \+ O" g" V' _7 `3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。/ i1 k; ^! Q3 o ?. F
# a6 T/ V, {/ M( a# [$ b P
, A8 }6 e- `0 i
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
5 C4 C" `3 ?, p
+ H; K% K! P! t5 D6 Z7 N# ?1 A0 J% _, \/ C: m" \
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。3 P% s$ U+ V- K( y2 [' F$ x
, }! Q9 M* V* z; ]2 i l
! w4 n4 f7 L$ s, I3.x版本CRS T1 d4 ?/ a& }& t3 A# H& T; Z6 A
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
8 \4 v) o1 ^% m+ ~# 生成例外排除请求的配置文件
' G u. r! }* R0 \( \$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
& b: ]+ e* g4 _$ h5 E; B- g$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf+ B. A+ I, N: L/ F: _ R" i
$ cp rules/*.data /usr/local/nginx/conf
; }4 y! N5 d* Z2 O; H+ G% \7 Y为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。* z; t7 \2 U5 ?5 T- k
( I, R ^' n3 }5 U8 z
5 O+ Y8 s9 n, h4 V M$ vim /usr/local/nginx/conf/modsec_includes.conf r& R* Z# r& Z$ `) Q" I4 w( P( [
5 P0 W' R( l; T1 J" ]( i[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 5 H; X& p6 [5 ^- K
- r- C( W; z X+ G/ p- b
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
/ h/ O) @# l- v: f K; H
2 z0 R. S+ b6 u {" C$ m: G9 T1 v: h' U1 U) [
配置Nginx支持Modsecurity; b" q9 F" ?, i& R, ]5 Z' N' V
启用Modsecurity
. R- @$ t3 @5 g/ g使用静态模块加载的配置方法: |" ]9 z2 F% C) D* ]; w5 Y: a
在需要启用Modsecurity的主机的location下面加入下面两行即可:
$ n+ n9 H9 t9 \5 _
; }, u- u v d9 c& d3 [9 E
$ B- j1 y3 O H2 }$ XModSecurityEnabled on;, \8 O, [( v* I8 F/ i0 f% m, A
ModSecurityConfig modsec_includes.conf;
" k* a1 `* D2 W修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。/ U! v' U& O0 U, }
1 P- \; K0 i8 I: r, M7 V
3 \- g8 p: P, V% S6 c% n& M
$ vim /usr/local/nginx/conf/nginx.conf
! b7 K& \3 c- b# q# |& x* w/ j5 @/ U1 `7 m9 j" O
; a9 s) c& F0 X* R% @3 s" a* L- mserver {( ^2 a- R# C) L( v1 [: d* B% ^/ A6 w3 u
listen 80;
) e- {8 h6 h# Y. g y! d$ X2 H; {9 W server_name example.com;
1 ]6 } [. J9 c6 W/ Y; o
& t6 K8 E' i* N6 i( P6 R7 W) d5 ]( Y) f( R% W: \- Z6 j! N1 O
location / {
% {0 T3 s' ~+ M4 ]1 {5 [ ModSecurityEnabled on;: ~; g* ~) P" E, I4 g4 j) u/ V- p
ModSecurityConfig modsec_includes.conf;6 t0 X/ Y! D7 f8 O3 u
root html;# @2 p7 A0 L* j
index index.html index.htm;1 `1 K+ V1 n/ ~: z, B% U1 s+ |
}9 j2 k1 g8 H% W' s: i1 a
}
5 W+ {+ \5 f! X. ^! R, U _) H, i, P使用动态模块加载的配置方法9 K, j; [! Q! {" s" m: |8 [
在需要启用Modsecurity的主机的location下面加入下面两行即可:
! Q/ M. W# ]2 j7 {
1 c4 |% S2 o3 T7 E7 Z! K" p' a) I! Y# c$ f
modsecurity on;
8 D/ g7 K: y3 w9 f/ L1 Cmodsecurity_rules_file modsec_includes.conf;
) E' h0 h- R8 C, l- ^修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。% I# ~' S; u) `: ?/ G/ a3 J. J3 ^7 W
- ^) c' n" F( t( f. O/ U( J3 c! g% _ _% Z; U5 W
$ vim /usr/local/nginx/conf/nginx.conf- E) a! p B, K
- g% z4 Q0 x" p. H d5 N4 }3 b* M
1 D" ?7 ]7 x* z8 A& v' iserver {% E* U" |" ~7 @2 ]* `# M) n4 r
listen 80;6 n* x" e6 U! r% u/ J( l
server_name localhost mike.hi-linux.com;
9 b, k7 z) P) Q; D9 Z5 c. a access_log /var/log/nginx/yourdomain.log;
& a. m3 d! d( ]1 W' G
2 T1 F* i! h' ] Y' m, G8 ~) e3 U/ T. Q8 X
location / {
$ h# `) m% L( m9 G& J3 S t$ I4 J: a* R" m0 L$ Y: c. e% D3 Z3 R
# J1 Z6 ~5 G3 }# M' u modsecurity on;0 y. [* `& b/ q' O& |' m5 l8 {
modsecurity_rules_file modsec_includes.conf;" B8 C( i$ {0 c$ t4 F- w6 ]- B" x
root html;3 w F& m9 |, A5 i% X$ ?6 P" ?0 ~- u0 w
index index.html index.htm;* r6 L! u/ F; r. W1 t- }, {% Q* U6 j5 B
}$ B1 F* K: d0 |# }3 s e+ w
}7 z1 @; L9 j- V2 P( K- ~" p! R: j
验证Nginx配置文件
+ U( q$ w2 J6 d. \4 C4 G8 c, f& R$ /usr/local/nginx/sbin/nginx -t
! Z4 u5 ]( |1 L/ u `nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
9 I4 m+ ]: y! v. h+ @$ ]7 rnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful, e5 r* ^$ H a, c1 [
启动Nginx
2 o7 `% d1 a* f& r, @ u0 o1 f- u$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
) F4 H N7 x* s& h- w6 K0 `# z
! _" s l+ f. x9 I9 u2 u测试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能够战胜更多复杂/未知/混淆的攻击模式。 % G& {! u- ^& p1 U
|