ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
6 |' v& f9 |3 z
; \1 Y2 U3 v Z; h0 N/ N+ c4 D: x" ?- z9 |
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。$ a' _3 L* r" u3 L N
R" a; y( H8 w* P D
3 Q7 {& K: ?* G/ v! Q什么是ModSecurity$ G! X: L! u1 w4 I A( @. G: d
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
& f1 z7 N( E$ W$ D* Y* w( K# n
2 L, ^1 Q2 `) ?; M4 ]! v* J. z5 o. {" t: J; {2 S
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
9 {/ v; G1 i3 J
" G" f4 H" i$ h+ k
' D* B7 T+ b- Z+ X- ~3 r: ^ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
( F* L& F6 o; _: T3 u1 }) b" { Y% f; I+ a
+ f+ E" l: C* t) Z' _! f官网: https://www.modsecurity.org/
$ X/ M) f. w* ^4 ^" j6 |. |9 P* W
$ G, P6 o T5 E" q3 F% a. v2 f- _2 P% c( a- o- @- ]; H$ j
什么是OWASP CRS
, ?1 {8 U; K( qOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。: V! K( h6 N2 z0 |, o6 S
% x, H: R/ i* B) i S
) s2 e( p7 g* o8 @. D3 |ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
5 r' v/ u# ^* K& u, f. i
8 j7 v1 a: C( N6 K3 t9 T% Y. i( V6 Q8 e& i2 c! |
HTTP Protection(HTTP防御)4 A7 N* s# [3 u3 |' `' H* n
HTTP协议和本地定义使用的detectsviolations策略。
, k% A! i! Y# h. s
4 {& M6 f6 [' A. P/ I% o; n4 X+ }% Y0 c* s
Real-time Blacklist Lookups(实时黑名单查询); W1 |# o- c/ K; X# q1 I
利用第三方IP名单。
* n, r. Q7 @# Z% K M s6 [% T, a; W% m1 f) y- O
1 t2 Z! m* x: o( g+ M1 ~! e6 E# a6 V; L
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
" X: L8 \; I2 H防御HTTP的洪水攻击和HTTP Dos攻击。
; e$ q5 H9 W% {* l$ j0 U' u8 l* E5 k& r' L. P
: ^8 m+ k" m D# o4 }& O% g
Common Web Attacks Protection(常见的Web攻击防护)
8 a- i8 A- t# A9 _" a检测常见的Web应用程序的安全攻击。: L: u: p: O2 t0 I& p, t
1 t8 U& O [% {
3 g- {- \; L+ g# \9 SAutomation Detection(自动化检测)% u F* t( @! v6 M" n
检测机器人,爬虫,扫描仪和其他表面恶意活动。
+ G% ~2 t* f, K. M R
! Q8 u: k: S& e. e7 G& b
" C" h0 ~9 Z" ?) HIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
7 k8 m" L4 w* I5 A9 `: ?检测通过Web应用程序上传的恶意文件。- |' L8 c5 f" I; F
. @/ {+ \9 I; C* N/ b7 k+ j! y; P( I; |$ d
Tracking Sensitive Data(跟踪敏感数据)
7 F5 M' W; ?# N8 I0 _ ^2 |信用卡通道的使用,并阻止泄漏。
& W M1 g6 S. ^8 t6 T
. j. N M& |4 G6 f* K4 M7 f2 n& F3 A+ E
Trojan Protection(木马防护)
: y7 w$ ?: c$ h. R检测访问木马。/ }+ P4 F. a1 c+ Q4 @0 I$ U
, I5 G" A0 d8 W" x
; \- f* F- |: y* c) }+ lIdentification of Application Defects(应用程序缺陷的鉴定)
' h% g P. W& {7 Z1 R; R检测应用程序的错误配置警报。
0 `, ?: |6 S y6 [. B4 Y _+ Z, p0 h5 @. n2 c- h
% Q8 i, o; f0 u) F+ r& f+ ?1 UError Detection and Hiding(错误检测和隐藏)
7 A* K( N1 R" g: s检测伪装服务器发送错误消息。
( }% c. `- L4 I% @/ w: u
$ i* `+ k- X3 W# D$ t2 V: l8 @0 X f$ m* o4 _5 H
安装ModSecurity
" \* f+ c7 `- y! F; V1 K; g) R软件基础环境准备& }+ ~( g2 P" m8 u
下载对应软件包/ Z: w( a5 E! Y# |3 l# [
$ cd /root
' w" {2 b( {5 W( i/ e$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
6 V1 z# k3 S4 D3 n' T$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
- J: J0 C) i! K3 Z- b安装Nginx和ModSecurity依赖包# d4 a; D3 P) t9 G# a0 a. R8 {! {
Centos/RHEL1 K9 ]! O! L- n* |8 z# D
: e7 P; `7 Z, U2 Q5 f7 `" o; H i0 ?
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel/ ~0 Q$ v5 b/ |
Ubuntu/Debian
2 z$ k4 I% H8 o* T
9 p9 S* t" v4 U4 O0 a2 }$ g( X! c- E/ }1 E/ c1 g
$ 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, g9 s+ H! U# V$ f2 B" q
编译安装ModSecurity
8 g3 u- g* Y3 t; u$ |, P$ _( kNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
4 ^* q% Q+ x0 `- r2 p) C1 H0 s0 E5 L0 z1 C. O3 T
' V% |/ i7 w% r! B2 ?6 D) M
方法一:编译为Nginx静态模块2 J7 x. W: S$ K( o/ R3 W. f0 w3 S
% \2 u, {- ?: l9 I/ u! \% b5 g' y0 r' X% B3 k: E" @" N. U1 P
编译为独立模块(modsecurity-2.9.1)
5 N0 W, K$ {: ^" e! X G7 F$ tar xzvf modsecurity-2.9.1.tar.gz4 L" K6 H- y# f2 q1 i+ _
$ cd modsecurity-2.9.1/
3 O9 G P. u" |$ ./autogen.sh! p1 _1 n* J) ^0 ^% J( x
$ ./configure --enable-standalone-module --disable-mlogc
8 S0 B2 t0 M- b. g8 i7 y2 h$ make6 M6 D& y. _4 x3 ~& ^( Z2 [+ v
编译安装Nginx并添加ModSecurity模块
3 @; R: f/ k+ {. T! D/ n$ tar xzvf nginx-1.9.2.tar.gz' w! S1 M0 M5 y5 K, G
$ cd nginx-1.9.2" R1 v! \# V+ c$ ]
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/* R+ k8 U" B: J* x& }& V. Q; A# s
$ make && make install
, T; @2 P6 Q8 a0 B$ g- m5 e方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
- t' ^6 x% G. @! |
1 Z8 R2 J, y$ S3 M/ O8 w+ @$ v( h3 A3 I, x. T
编译LibModSecurity(modsecurity-3.0)
# R$ ~1 |9 D& S9 ~$ cd /root' s8 V+ d6 Y( K
$ git clone https://github.com/SpiderLabs/ModSecurity
9 t' j+ `6 t6 u" a7 U4 Z& u$ `$ cd ModSecurity6 {+ E$ J( z* _2 s
$ git checkout -b v3/master origin/v3/master$ I, t; L/ T( F9 t3 b
$ sh build.sh/ ^' b) x% ^; [5 L
$ git submodule init
4 w9 s5 l% Q9 _8 F* `' Z7 f# m$ git submodule update
! @$ w7 L/ n. ^' ~% n$ ./configure8 ~% I8 v+ |$ n" ^9 Y
$ make
* O# |: D3 v% }& Q3 L, f; j$ make install+ i# g. E* t. b- W3 }: V
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。" Z5 N1 F$ f/ a( I
' T6 |- j( r* }& m7 a+ D! U+ e
, z' v5 ?# J9 B2 D# g$ ls /usr/local/modsecurity/lib
2 A. A4 }1 q3 O8 l% S! Y: h5 [# alibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.03 w! ?% I& V- a! A
编译安装Nginx并添加ModSecurity-Nginx Connector模块* _" e1 O. _7 N1 S/ K, M, u
使用ModSecurity-Nginx模块来连接LibModSecurity; D* {# E" Y0 ]$ T
0 S' L# O, `* k N) M4 \
2 B( X( ?; m/ n" F. E5 M" b- F8 h
$ cd /root
2 j5 v1 A0 W1 L$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx9 ` Z I1 a. O
$ tar xzvf nginx-1.9.2.tar.gz
8 L8 n- K, o& d- P3 s% f5 ?$ cd nginx-1.9.2
2 t; c! E6 Z# N: {5 |0 T$ ./configure --add-module=/root/modsecurity-nginx2 R7 |9 H, `8 @' _+ {
$ make
y3 `* R0 p. n# N: m$ make && make install
Z8 \+ a- _$ |添加OWASP规则! P+ E0 g! M' x4 L
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。% W1 i% _1 T5 m
8 v3 r1 C1 O9 q6 p, Z" ?2 i4 K& Z- M f$ K% M, v3 Z$ b, H
下载OWASP规则并生成配置文件3 H" o3 ~' Q( g4 f% v
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git, P3 U% Y7 f0 Q+ D6 m/ {$ r
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
4 m3 i3 s: L# W1 F$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
8 _- [1 A: X5 F$ cp crs-setup.conf.example crs-setup.conf/ t; ?7 q+ F4 E) \! Y' f
配置OWASP规则
3 d& E0 m% X% K& e. v编辑crs-setup.conf文件& a' o4 d6 S0 X7 p2 }* V
) `. K; ?5 ]5 x L7 \, V6 T+ U2 ]
2 _* d2 J6 q* r' ?0 A5 a
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
3 A5 E2 w V# v% y$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf! R! \3 i* ^2 O3 m; S2 c8 S9 a
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf1 U Y+ r5 B: E3 r6 n* ^6 V
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
/ f8 ^8 Q; e% K6 C, N默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
6 z" y: G, U* \0 e8 G) a2 J/ k# B) x% _: X- ?0 q' M
% f# p- v( m: J% w启用ModSecurity模块和CRS规则 ^, t- L6 k% Z/ u u8 Q7 F
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。6 v; [6 d! q, w
' b8 E8 ?* c+ v. h
$ b( d8 E; ? S; Y9 w% \modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
5 {$ t( F( T, K" t' E
! l; B2 B9 u& B" Z% _" E2 T8 l8 p) O+ E6 K9 B; t! R5 H
$ cd /root/modsecurity-2.9.1/, u4 ]8 n' B, s! b p2 m
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
) O7 K9 Y+ C C3 o* d) T$ cp unicode.mapping /usr/local/nginx/conf/3 E# q+ Z, i7 Z( J1 F+ ^5 a7 s0 t" v
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
& L4 u, t: O Z) i. W
5 o3 l0 ]% ?% ~: M1 G0 e: v. W( L r
$ vim /usr/local/nginx/conf/modsecurity.conf( `2 m4 f7 {6 D+ K" t, e
SecRuleEngine On
8 Z* t* Q8 @" U3 j3 q- ?/ W4 DModSecurity中几个常用配置说明:0 b9 u# R$ x4 \0 W( j
`& w) ?0 e9 b1 c& X; K3 Q) a8 K- r- a5 T: A$ ?
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。2 j6 m; M) x9 T: R
8 g, O5 o3 s% |3 O
, q) E! M! y$ q, N# J1 r2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。1 L3 V- \/ n' a7 j- F1 ?
) }" x' Z; J' F' `* I( m# [( ^5 H* t( H( h1 p4 p) v. x
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。 C8 Q# y5 n) c& e
/ B$ Y4 h& _1 K
% ]5 m5 N8 h( t. \6 i$ J; z4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
3 d# c% z0 m9 Z1 z' Z1 C7 d m1 ^) r! Q! v9 h. A; X7 C
0 x! s0 b& [- H2 [5 e. Z4 k9 z在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。2 D8 e$ b+ i0 U8 z' S
, q X% b3 h9 a, p' [
+ \+ I4 v9 |. S
3.x版本CRS
) P1 H- j- I1 |3 {$ cd /usr/local/nginx/conf/owasp-modsecurity-crs+ P8 N' t/ S1 T5 t
# 生成例外排除请求的配置文件
4 d) k) \- c* r: G, A! E$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
9 m# F( q& O7 B9 T: u; Y$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf2 ^) N! v3 @9 b9 X4 [, v, R3 b
$ cp rules/*.data /usr/local/nginx/conf
* I% Q, q& b$ R- \3 {0 t为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
: `3 v; t2 I! S" y5 _3 l) l/ T, t4 _0 |( C
* A3 d+ C, M7 R% b* r6 M$ vim /usr/local/nginx/conf/modsec_includes.conf( E! L' _# @6 p% [# |" O
V r& r. b. Y0 I# s! 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 % C; c2 W& n$ t/ |% X8 d
" N8 v8 X; }* t% _1 X+ _9 _注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。/ u4 Y& n6 z9 D, H1 c/ X0 _' g
; k9 r1 Y; \, Q. x
/ X, Q5 Z2 i/ F9 I) o) ^! Q& z配置Nginx支持Modsecurity
, D9 i+ W3 h4 l& s" y+ R* |, d: i2 ]启用Modsecurity# Q+ k. x: T% g7 n/ I
使用静态模块加载的配置方法
( O! q3 B' Q( q2 e# }在需要启用Modsecurity的主机的location下面加入下面两行即可:
2 F( Z& b: @7 I C7 ]) b8 ], D) T+ T4 U+ `: V; `0 N3 C
: t" w% M% Z3 _9 \
ModSecurityEnabled on;! p% G+ V1 _' s4 u7 h+ M' F- g" r
ModSecurityConfig modsec_includes.conf;
+ L; g1 B3 T8 Q: o- p) `& w3 Y修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。7 {% L w8 D' y L* U
5 c( ?1 Z, N/ @* ~' `* ^+ W2 Q% ^* F8 ]: G6 N7 x9 ?" U; r, k! H0 a
$ vim /usr/local/nginx/conf/nginx.conf
, [" |$ {) f5 Z! }' ?1 P
S `$ u( |( n! r- a5 \
4 L7 K ^, g; i2 @" Bserver {- s0 |- A3 S, x: d+ F- ~
listen 80;+ \1 |' G/ k% w) W7 Z9 }
server_name example.com;7 U+ H7 d( D% C
' A$ C* i9 [' a0 s& n( v
: R6 u7 ~% V( L& U
location / {% W5 J$ [7 \% u! Q) T
ModSecurityEnabled on;
. H) h* O3 @6 a. i9 k% A ModSecurityConfig modsec_includes.conf;
# J @$ S; a+ ?1 ^; U, {8 ` root html;
1 C* f, w' X) S$ v2 \ index index.html index.htm;' P4 u6 Z( ]) I
}8 Q9 L- Y# w0 d: t" P( [% x; N& `
}
; O$ k$ K- s2 T8 j4 x使用动态模块加载的配置方法; o0 m( ]0 a; f" s/ w, D, v
在需要启用Modsecurity的主机的location下面加入下面两行即可:
z7 g$ N7 s+ y. i& o: U6 O+ l4 K* k/ ]& n5 V! ]7 \
8 ?" }: p7 s! V& Qmodsecurity on;
6 G* s4 Z0 L/ |- lmodsecurity_rules_file modsec_includes.conf;% m: {+ E5 Y* Z0 G0 D, a
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。. ?6 _/ I/ k8 o9 Z2 w8 ]* w
- b. J+ i6 O2 Q) O4 W" b
& a4 O* {9 m7 ] W0 d3 n; b$ vim /usr/local/nginx/conf/nginx.conf% M" g3 X! l5 W. `. V
, g/ W! g8 J. _( m
9 k$ h8 a# h4 A# ~0 `0 ?/ C |
server {4 [4 a6 x2 m8 n& c: f/ Y
listen 80;
- K T$ l A9 X ^8 S" o server_name localhost mike.hi-linux.com;* a! P1 u, o% e4 V
access_log /var/log/nginx/yourdomain.log;. n3 N4 [( c0 h; P% H
3 o* D C! B9 r t
3 U( W$ D% }2 f9 ]8 s4 z- g; u location / {4 _' M3 t9 `8 U3 m0 ^, a, g
' H6 I8 Z# [- V; A7 m5 ?; ?2 Y
% P8 s x' \' J; f modsecurity on;
- H( U; t" v6 @; h' L. V" G# t. H3 l modsecurity_rules_file modsec_includes.conf;
2 K: u) [; Z* v& r& n4 n root html;( O7 r/ v" B3 ]: E' W1 ~( n
index index.html index.htm;. h5 u9 i: d3 e. `1 z; |5 T, d
}
* G4 F: L/ _5 x/ J/ R+ D4 V- u l}& }( t, S1 O- @, f5 E$ E9 ]
验证Nginx配置文件
( p# y" _1 l7 o$ /usr/local/nginx/sbin/nginx -t9 ?1 b I: G! F+ u' y8 L5 \( S* i
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok( Z2 A+ z7 W @3 {1 Y- N
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful7 q/ e* {$ ?2 b! r& C
启动Nginx
, j3 C' }1 t3 V9 u6 Y. n8 v$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
; ?* h0 h7 F- @* j! j- M
4 ?$ [& e4 k1 S/ {6 I7 }测试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能够战胜更多复杂/未知/混淆的攻击模式。
; m$ h! y! }+ K7 n* X |