ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
& M" B# F \$ X* ?5 _, I' i
/ X5 D$ z5 `. k, ]) P# p3 |
" z! f7 U! y) x5 Z9 J在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。: p) W4 {5 `5 d4 [. ~
! b0 R$ e7 D+ o# k) X8 s+ ]( O
: A3 u& b9 F( w) m* h1 }2 R% p
什么是ModSecurity
4 z, ]! \: i" W) S% m$ ^( U9 UModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
; z$ A1 Z$ }' D; Z3 N3 D4 v4 V9 ?) [0 H, \, ^+ y2 m) H7 [
1 x K6 x- |4 dModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
( g, }( T! @3 d% [: }: l1 E
" i/ D# a1 y* P1 ~' Y# G" x& ?, Q. p. \/ t. L f
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
# L3 u( F4 Y& C
' }% r' s7 T, }9 W* |) G# P- }" t- g& ~ @9 D
官网: https://www.modsecurity.org/
; k0 o& Q6 t5 I M/ g$ H9 S! L) p1 V. C7 j
: b( k# X! h6 t Y5 m4 }+ t5 ]1 Z
什么是OWASP CRS6 |( Y, @* t1 b! K* T. G( @! R
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
: [- R" W2 m# g) a- k3 R4 i9 @( ^* R- P2 R8 y2 P1 b$ {& \
& F9 A; Y: x2 A8 B
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。, c- s# c( D% b- A
$ s. F/ B$ b' z% G) ^. u5 k
: C5 j- c# a) O% d& @HTTP Protection(HTTP防御)
1 @" F$ m9 v# eHTTP协议和本地定义使用的detectsviolations策略。
+ D2 i$ Y3 W# _ J' J; |. J0 O t6 ]8 }
2 P9 Q' D6 G! [/ ^/ Z9 HReal-time Blacklist Lookups(实时黑名单查询)2 V0 F# g( i; I8 G$ U. g
利用第三方IP名单。 L5 f/ i" p0 d" e- M2 y5 B
+ ]. p8 D- n7 @- n* k* N
; G, S, B% g- VHTTP Denial of Service Protections(HTTP的拒绝服务保护)
5 v3 p# L0 H$ |8 S" z7 ?9 U防御HTTP的洪水攻击和HTTP Dos攻击。. L }. h, `/ v; \3 }; Q A
0 E9 }* ~& a% E; D3 f- I5 y0 m% \
; ^# x6 R9 O6 b. w
Common Web Attacks Protection(常见的Web攻击防护)- V7 G9 G% U; a" |5 b: a
检测常见的Web应用程序的安全攻击。
' j1 e% k- {+ Q+ M j/ ], a q1 _$ h4 f! b7 G$ j
# d5 N4 {+ J5 Y+ ^5 h1 ?% l+ JAutomation Detection(自动化检测)
$ k! a3 O2 X" y& n% m' [检测机器人,爬虫,扫描仪和其他表面恶意活动。
& C. A+ Y, t4 j' g* |9 R( I" R$ e k$ \+ p j" @
/ l; r; v0 [1 q' M! M* [
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)9 ^; U- r' H3 T: ~
检测通过Web应用程序上传的恶意文件。8 K& ?' S$ l1 s6 \0 O1 ]
; v0 S5 y. g" }8 n/ D
7 q g' M$ j( B. {1 x
Tracking Sensitive Data(跟踪敏感数据), u3 s, x2 ?" O2 Y: a- g
信用卡通道的使用,并阻止泄漏。
3 N# [9 i+ E' H( Q- N, y2 }
1 h8 ~$ O' A7 E) X* x& s2 i( o" N9 I t7 R- H3 K
Trojan Protection(木马防护)
3 w. x0 V4 N3 n检测访问木马。+ o4 J5 |, e- W. {/ T+ t
9 h' g: H7 X; U! w! y
* @" D9 M& w O( [Identification of Application Defects(应用程序缺陷的鉴定)! V% i: b8 @- { Z G+ [
检测应用程序的错误配置警报。1 H C. w# w. U/ X+ e0 I2 a
7 I6 c+ Q+ ^, Y9 u
7 g" a. b; w' R' M1 _Error Detection and Hiding(错误检测和隐藏)9 N# d- m. s4 P9 T4 Q2 _
检测伪装服务器发送错误消息。& Z; q) M S. [$ q5 r3 S
1 k5 J# R, {. A" A9 ]# L F! m4 a5 Z }: K7 I \: C
安装ModSecurity
! M/ c; }0 ]$ b& g% d) C软件基础环境准备* v: g' E8 o. o* R) i5 W' U& O
下载对应软件包
6 G& H j! J: D% @$ cd /root
- Z, q" A" [3 ?. R$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'5 A, O. u, Z a3 z) @
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz( [5 |$ B( ~) [, i( g$ h
安装Nginx和ModSecurity依赖包
$ @1 T8 a; ]6 nCentos/RHEL
]3 I- m/ f* G: U, B A/ o; B5 z
5 \* e: f1 ]& ^" q& n$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel8 x' `) r" P. W& ^# ~& e
Ubuntu/Debian( m; S1 _1 R# X; M
6 K. G/ t" e; k3 h6 B p3 {! Q9 i$ B0 {- j6 S: U
$ 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 q+ d6 x% V9 ]- ?2 g4 S5 J& H
编译安装ModSecurity
2 O* s6 k6 Q, @3 x; @Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。5 u- D* T) x9 T. j. i4 F/ I0 h
* I: e' ^" K( e0 O+ K: F, j) H! d) n+ F+ Q0 ?$ k1 B
方法一:编译为Nginx静态模块& K6 @+ @& {1 `% x7 ]7 E' b$ f
* T) Y, L3 @/ H7 n& t
9 C! D: ^8 k& r( R& k
编译为独立模块(modsecurity-2.9.1)
7 f& T! T: i- B1 |* p1 G$ tar xzvf modsecurity-2.9.1.tar.gz N" W3 H, A& n+ c7 s y
$ cd modsecurity-2.9.1// o0 P6 Y: d6 Y7 _ i
$ ./autogen.sh3 X; l' [8 d: d! l2 L
$ ./configure --enable-standalone-module --disable-mlogc' q- ~6 l* M9 c5 \' J
$ make, z4 V- B" W& t( |' {) p. R2 }9 I7 x
编译安装Nginx并添加ModSecurity模块4 M# e8 C3 I7 d5 I7 c, V! p- E. ~
$ tar xzvf nginx-1.9.2.tar.gz" Q! U' a, _/ Y B$ }/ D
$ cd nginx-1.9.22 @4 j2 r# w: e% p" v9 r
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
7 h _1 V) T) m/ f$ make && make install+ z6 |4 c5 v1 D! ^5 f1 d5 ^
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块0 u2 q8 J. i. L0 Z, |
. Y# x8 b) z R( u
' C7 i& f4 w/ t5 F# v* f编译LibModSecurity(modsecurity-3.0)
0 \+ s; T8 ^) u$ cd /root
3 x! X9 g# G T/ Y- ^2 A5 A$ git clone https://github.com/SpiderLabs/ModSecurity/ v( ]- e6 m, b; z0 G9 ~) r: }1 f! x8 C. w
$ cd ModSecurity+ S' f) T3 |# `; u1 R
$ git checkout -b v3/master origin/v3/master
7 L# H# u6 g: K- ~0 X- u$ sh build.sh
" @+ G' d1 h$ e/ P- o) I$ git submodule init4 n( y; n. b- ^& i' q
$ git submodule update# X' ?, I' t( \' Q
$ ./configure3 A; W, a+ R4 t& h! I2 O+ \. [1 h
$ make
/ g( f3 I) z6 {- i" h$ R( s$ make install" [+ E7 s8 X1 Y7 g
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。& T/ X! z4 h* o- z( F- t3 ]& h
O/ _* o% }8 {5 x9 L) Y
4 S( m2 R+ t$ n! \. i% ]& @! a$ ls /usr/local/modsecurity/lib
: D& }7 i9 U' Clibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0+ f4 @5 S# v2 J" [7 `* a
编译安装Nginx并添加ModSecurity-Nginx Connector模块
U6 o8 {/ |8 i. }使用ModSecurity-Nginx模块来连接LibModSecurity& T+ o; `/ e" k; \& L
9 V' s5 |; {) `8 X+ ]) J a! B( Y
6 z3 u$ R+ O! ~. _5 y% c$ cd /root
# b8 o( S6 f1 s4 `9 ?; F" P7 }$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
% e q7 ] j/ k3 G: r$ tar xzvf nginx-1.9.2.tar.gz/ |4 e% g" T. L3 I
$ cd nginx-1.9.2
; y6 ^; B: D3 T- K6 r1 i- H$ ./configure --add-module=/root/modsecurity-nginx5 ]: P* k. o0 {8 p
$ make
! }: k" }0 o5 w7 Y0 J/ Q K$ make && make install+ E6 L: ]% ]4 Y
添加OWASP规则
$ y) P! L$ z2 I, c/ ^! IModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。) l$ R; e7 Q& V, Q6 H u, ]
5 o' v* W5 E. ^% u. e
4 k- |/ Z7 O0 m- [% h下载OWASP规则并生成配置文件3 ^0 J% n7 s5 F; a0 q
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git- G5 R% \# p" @
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
% C: ~ [/ c. X7 d2 e7 i$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
! a/ R6 x. r2 g- U9 {# ~: m$ cp crs-setup.conf.example crs-setup.conf" o+ a4 J, ^" @% s! B. ~ k$ H! s
配置OWASP规则) X9 p! N! s! F$ ~
编辑crs-setup.conf文件, u* R+ N* K. v" w& w8 A
" J& @7 g; j. G& g4 c' X# k V" O1 h
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf. W9 T2 \1 S" y: a
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf3 T! Z$ B" e9 e
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
8 s9 Q: S y d4 B$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf1 R/ C+ z, A7 v* D
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
6 y1 T7 K! |- r8 X9 D$ d6 G, U& ^2 q% |# z, f
2 |% P( a+ [+ E8 P6 L
启用ModSecurity模块和CRS规则" j! m/ A, z% J% v! g# C1 e
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。7 Q9 c+ v9 q/ @6 u" `0 a3 u. t4 r
& T$ |/ ~2 V C @
' Z& @6 h8 Q+ _* F- a" Ymodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。* l* n/ q9 N/ f5 Q
8 V; H* r F$ B) H7 s5 s C$ @) S- P
* k' ?- \3 j! |0 _6 |$ cd /root/modsecurity-2.9.1/
\5 M6 x& i. z" f$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf : s+ M( w& U0 ]( h6 Y: L
$ cp unicode.mapping /usr/local/nginx/conf/- G% x" v- `$ \* X' c
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
3 q) u! Z. ]# k" h& `# f, s2 G$ ~; K1 Y- u" L
$ W1 A, t5 m% G
$ vim /usr/local/nginx/conf/modsecurity.conf
6 F) d$ Z( k6 J2 ^' M6 MSecRuleEngine On1 h) }/ X! v4 j; m3 L+ N
ModSecurity中几个常用配置说明:
! A/ B3 ~( V; t' T3 b9 n* H) b; d
( G1 t% n8 k5 [0 j
: T5 O( m9 g! H1 B' e1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。3 @* d4 B6 H# T/ @$ G( V+ }
. A, P4 F! b) Y1 V0 {4 Q
" l7 x) P# N# Z# i2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。& l, c B& _4 @
7 E1 o* p; x) h1 L1 |7 D6 G, r& F8 m! F
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
" D8 A% p9 A2 A1 h1 O0 A: {
2 s9 h* E+ T" B+ S: E; i6 u: e+ ^% S5 @. @" b: D5 j+ v5 s5 Y( B4 y% w' b
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。# i0 Y. P; v/ I1 u1 P
M- f" [: ]. C9 y+ p2 g4 m
' h7 ` f9 y) o1 \
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
# q/ t) b" g) [: Y( h
- g5 O5 R* Z: b+ N# z( x2 l3 n W7 {: _+ A! J! @5 O
3.x版本CRS
& R U; X) l' n; j$ cd /usr/local/nginx/conf/owasp-modsecurity-crs: d G8 j3 d1 h8 ^% D
# 生成例外排除请求的配置文件
/ Q' B7 p0 T e$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
6 w, y' c; Q7 [. q2 F( _$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf* `! V) l9 W, `( @" J8 C
$ cp rules/*.data /usr/local/nginx/conf8 g/ X I$ V. a* A! ?. J7 Y0 [
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
# r# V" B8 x) ]& E) d$ R% \4 m" p' |% P
! @/ C' m0 X$ h' J5 ?, P* y$ vim /usr/local/nginx/conf/modsec_includes.conf; w; s$ l2 f. T- f2 b) Y, [
% j7 W* d, E. f* K& s4 h. h[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
& f- P- f0 j- H7 |& D" S1 h( O) K; x. p+ H' T% V' Q
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。2 u; F! H9 p4 {% `! n
, p: E: v: T6 t1 l
" {3 G$ L9 {" ~! _) r4 g4 t N: C配置Nginx支持Modsecurity
% o* v% J8 U, \+ n启用Modsecurity3 c3 t, l: T4 k# t$ J* l) X. y
使用静态模块加载的配置方法4 h( {* I' ]" E; ?5 y! V! _8 J
在需要启用Modsecurity的主机的location下面加入下面两行即可:
/ K7 f3 h. i# t# Z6 R: U: D8 M: I2 U' }. ~ c( u8 n$ U4 t. a$ F
+ L5 T$ i# X# |: m: r$ kModSecurityEnabled on;
0 {7 m# E) j5 @: M! G* kModSecurityConfig modsec_includes.conf;, H0 Z5 b/ C& T3 b* G
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
+ B, q' L4 H. G; {
; _# A- R2 d- A1 m% A' s. w" `* G4 m& r! y. t9 Z
$ vim /usr/local/nginx/conf/nginx.conf
% `# Y/ D! n) H+ o- z5 @; L
. ?; x; E# I! E7 b/ Q
% a& `9 `" _9 l) X/ U* N( rserver {& X6 r3 e, V, h0 n/ e+ l, U
listen 80;3 ^4 I! m* W1 j, q/ {" {
server_name example.com;
# h' |+ G* }( I6 D8 y4 Y" C3 Z) R" m2 I! u/ F7 x( v) A7 h' L
. M, z* H5 X7 @ z
location / {
, {0 U5 U+ `$ w( c/ X9 \# i ModSecurityEnabled on;$ j7 i1 k$ M# q' B
ModSecurityConfig modsec_includes.conf;# I# K) {6 B, l, T
root html;$ n" X% \+ k1 H. b. g
index index.html index.htm;
& X" z6 f" U& R- l, _( d6 o }/ M! y0 ^+ L* ^6 P; c* E0 v
}
+ B" W% E9 J4 w0 E7 y N( \使用动态模块加载的配置方法
( A: c$ J; |- y9 f( q. ]2 c) @% ?在需要启用Modsecurity的主机的location下面加入下面两行即可:
# ^% s. F: x# e: [; K# p, K9 {+ h6 p$ g- |+ n
5 c- o$ o: j" S
modsecurity on;
2 i& B( y) E( e+ c- ~modsecurity_rules_file modsec_includes.conf;
8 y3 Y; ]1 x2 g. o, T/ m9 k修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。5 ]3 c, l) I6 o5 }4 L& E- w
) c% h& ~3 f9 X0 \$ g3 a$ c
7 P( l0 g# w4 M$ vim /usr/local/nginx/conf/nginx.conf
3 I+ S" ~7 l, }* b( I6 `! N( O" X, q. o5 r5 |
% S( B/ J' e# F" B0 J) C
server {
; _% ~; S: X2 j8 M0 O0 s listen 80;
" ~" @! j' O/ g0 o5 N" s server_name localhost mike.hi-linux.com;
0 E' \+ R& @8 v5 ] access_log /var/log/nginx/yourdomain.log;( J% v, L* S8 W9 u% \1 [3 A( w, V
) R) d0 [4 z* Q
3 i; }5 W7 I% O6 ]7 h8 ~ location / {* ~2 U) u+ h$ ]8 w6 ], g. ]0 e. Y
6 S+ a- _# m( v* ^8 h. q
% h3 \ F: L+ J& }6 h modsecurity on;
/ Z( u8 `7 b6 ?+ |. _ modsecurity_rules_file modsec_includes.conf;3 E9 D( Q9 s# [5 C
root html;
' }$ Y: \; }) L8 P# Q index index.html index.htm;
3 _% c' I N. |- c; a}
6 J0 R" l$ u7 R}
1 k% M" Z2 j$ c& g h- K4 j验证Nginx配置文件
( `1 v( b/ f7 Q2 _; C; o+ v$ /usr/local/nginx/sbin/nginx -t
8 W% k! x4 ~: O# _5 q: a; `nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
& R& w# o' h2 w4 x' Wnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
7 }: h; R; E4 @: t, b( [启动Nginx
, C1 l7 z" G* W8 H6 ^/ E! K- t$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
$ A/ h2 a0 \7 ~: ]" i7 O' R% N6 A2 p2 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能够战胜更多复杂/未知/混淆的攻击模式。 % r) }3 o5 \2 _; G: S
|