ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。" ^9 z- S& ^% v3 i# T
9 I1 C, h% a7 d4 B* x5 V6 o) ], o2 f2 ]3 V) O- Z2 K
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。/ }+ u5 l. c" g( I. t4 y, N
& ?' `: V; r: O' \% D. _7 C |# \+ K3 Y9 U+ H$ J# A
什么是ModSecurity
$ T& F7 D( \6 i2 ]$ w3 @% }ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。: Q3 @) r3 Q+ g0 O% a
5 i% j% i1 ~. U/ s7 ~; E9 S
4 Y( p" N2 R0 P% J% s; e
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。( o- T( t0 `# g) k9 J' Q0 E
( T& h. t, j" K* l# h, b5 x5 A) l4 `$ |; M/ S
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
, b) p2 S" s# n! c6 r% [* a( V# L2 f) S
" }) J8 F% V. @. m官网: https://www.modsecurity.org/
' M) O8 w% g( D' h) ^- H5 b. R6 ^
$ g* y4 k" @8 e9 M. u
什么是OWASP CRS
. b, A) p& R; ?1 }) n3 ^6 ^& YOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
+ h; B& S. X# S% f0 u! N1 ?' y. p" z8 ^! t9 T+ G/ u
' ^9 E1 ~' \$ g
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。2 l% Z8 Z. x( [8 A3 e
* ~, H5 F! r; R1 {" C8 a( h! _
2 r: }& `7 P& {8 h, I6 s8 d/ k2 YHTTP Protection(HTTP防御)4 y- u( i" s# L6 ?( d
HTTP协议和本地定义使用的detectsviolations策略。: ?* L) r7 \" l
8 I f* ]( H: C/ D( j2 @5 g* ~
$ A6 Q ~9 M: n% bReal-time Blacklist Lookups(实时黑名单查询) O" q, d% N& n. Y
利用第三方IP名单。
/ L) v* R9 Z& z' }6 `. v6 y5 @, c1 ~
6 E: U0 a2 V A+ r; H" Y
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
3 f' _; T- S/ S2 c* }防御HTTP的洪水攻击和HTTP Dos攻击。 N7 r) c: v" I: x( ~% Z- u2 M" Q
& g1 j. \& {8 b1 i2 ~1 @+ [) H& O3 N/ z2 l$ k2 f" q
Common Web Attacks Protection(常见的Web攻击防护)
. I0 l4 r8 O$ @+ a检测常见的Web应用程序的安全攻击。
+ z" c. a4 X6 d$ u- }0 |4 q7 ~3 [" W8 q
0 D1 t% v I& f, X. A
Automation Detection(自动化检测)
7 ]( S0 K3 g* T) s: ^. Q检测机器人,爬虫,扫描仪和其他表面恶意活动。! l# l' ^! H2 }3 I# K
9 Z z! N# f5 w& O
* \- M; N: _* t0 L" fIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
; v2 |2 Z! ^, U2 m; X! r* `! f检测通过Web应用程序上传的恶意文件。. a/ W1 v' t: L0 C" c$ y Z1 ^
, v; G9 ^! g6 M9 E: w
9 K- o U9 u" b% {, B3 `
Tracking Sensitive Data(跟踪敏感数据)
J' e) o# i1 R* W& n" [3 Y A信用卡通道的使用,并阻止泄漏。
7 i7 B, ^& m% U8 ^- V0 c
& N% v& J& V4 U# l3 ?, u
, S" A3 F" Z% ETrojan Protection(木马防护)0 B' n- i. U* Y' P( Z) _( b5 G" e
检测访问木马。% q4 x% q+ N, G) c4 Y0 E
" s, c' y: }) [, E8 ^& Y
$ |0 _6 X: O+ V+ K4 PIdentification of Application Defects(应用程序缺陷的鉴定)# x0 |6 o R" i! J$ n
检测应用程序的错误配置警报。. @" o' j+ Q" K: i( ^
+ l, z+ h, M2 ]
( a* p+ |$ p( k5 m. q
Error Detection and Hiding(错误检测和隐藏)4 t: P4 l# B+ | B( R' ]
检测伪装服务器发送错误消息。
. a0 W' k' L, J- Z& D1 q) v+ V, [( @# }0 g6 u& F
9 ~9 h& [0 I- t) x' K) s5 R安装ModSecurity
* J& e, Q9 t1 Q2 [3 K5 i v软件基础环境准备
4 }3 K5 E- w/ s下载对应软件包6 a4 w% v3 B3 |2 f' T& a2 |7 \
$ cd /root
9 b/ B4 \% H7 y# i6 B" J$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'! R4 ~' e5 H Y u% K
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz# E! r2 U6 o- i4 a
安装Nginx和ModSecurity依赖包% `4 S/ \8 T6 s/ u; e0 _, X; {
Centos/RHEL
1 D0 u0 B' y9 _( g0 w$ k- ]0 U: I# C9 z" X5 Q
3 `) z* C! x; Z2 r$ b' G$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
; u2 N' j0 }4 NUbuntu/Debian
2 ~" J. S8 B2 @: ?! J. ]$ C" _+ [6 X6 e) h/ [8 B9 b1 t D
* [/ {$ S0 C; `6 S- \: c3 V6 m6 L$ 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
; q2 T/ c+ i& @1 N编译安装ModSecurity8 i( \* ^( L- V/ K9 e
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
8 ~0 \3 _6 m8 e* y0 j( E8 d
# a/ P" a" }% D4 o5 t$ V
* A3 B o* ~: t7 N方法一:编译为Nginx静态模块 W6 }. t& e- j
8 d& w/ {. r; W& r g
/ y& }1 ~6 T( w5 a
编译为独立模块(modsecurity-2.9.1)
2 O3 Z# B+ P9 E( G2 X' B$ tar xzvf modsecurity-2.9.1.tar.gz
0 ]4 y! L* ~7 f' `" P* b3 w$ cd modsecurity-2.9.1/: G7 [; l- Z) S6 g/ k- y ^+ V
$ ./autogen.sh
. ]: d' I, |9 w! h$ ./configure --enable-standalone-module --disable-mlogc
& [7 C+ F9 l' A4 x; B1 C1 H$ make" I- ]4 P+ |" D& u0 B. y0 \! f2 q
编译安装Nginx并添加ModSecurity模块 J7 q0 b; G" |# b! v
$ tar xzvf nginx-1.9.2.tar.gz, p8 P% O6 T L5 K7 q; d" F
$ cd nginx-1.9.2
4 q" x) {. B7 G' E" Z- X. g- P$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
) G4 E, n# ~2 t$ make && make install, F2 \8 o/ f# E4 V% F
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
. g' o- a2 i/ ?$ X5 ^7 F
s- `$ l c! Z8 v6 H A6 ?8 V E9 k( C% x) A8 s) r
编译LibModSecurity(modsecurity-3.0)3 O% N9 y7 J2 o. m
$ cd /root
5 X/ t* ]- X+ b! n. J# ^. w$ git clone https://github.com/SpiderLabs/ModSecurity
! ~, ^+ M% D# ]# P$ cd ModSecurity
& }! A G3 G3 z5 K _$ git checkout -b v3/master origin/v3/master/ A7 C3 w5 f- L: \# t: m* V
$ sh build.sh! Y+ N* o" H8 d9 Q5 M
$ git submodule init
\* D4 I- E+ ~& E% _$ git submodule update/ S0 A/ F* V4 t: O" v/ ~) o( a
$ ./configure1 O1 v: A8 D6 U% e9 z) @. y
$ make3 x. W* U& l( D c- G* F4 {
$ make install
$ m4 \! }( R2 c# |2 D% @" lLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。9 c# W9 v' }5 Q4 p- }
7 _% n: ]* Y5 g2 G/ E" ], g) [; m( X O0 b: @: }3 _% ~; z
$ ls /usr/local/modsecurity/lib& X# S# c+ F3 P% P
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
( ^' L. ~8 z5 Z. ^4 T: [编译安装Nginx并添加ModSecurity-Nginx Connector模块0 `( s. g5 e/ q/ K
使用ModSecurity-Nginx模块来连接LibModSecurity
. \: s0 X/ }8 z* J
% r. w0 Z- k9 C6 S+ z+ M
7 ^; ?" |7 v9 s7 Z3 r$ cd /root
9 u7 d4 u7 g+ O% O$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx1 m/ a: g" K% ]# i6 X
$ tar xzvf nginx-1.9.2.tar.gz
& L) c+ M$ f* t: t x$ cd nginx-1.9.24 N. n' z* O8 d
$ ./configure --add-module=/root/modsecurity-nginx
& [) ^% U$ { b3 A" {& J$ make
+ Z/ O8 C& C' q' A" `8 ]$ make && make install
% C. N' m1 {. g( |5 p添加OWASP规则
2 d8 n; [6 H) p" g* gModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。6 v6 y# |! a- u* Q0 ?6 I
1 [7 A3 E5 D) f
2 v' ~9 ^* A5 m S7 X: [& q& D2 _( P
下载OWASP规则并生成配置文件 w! W) n4 _: E7 I# P0 R
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git" x- U+ j/ [" w8 ^1 }; b0 \/ q6 n4 g
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
" H N' x* P. Q2 @$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
: J6 f& E& B/ ^$ cp crs-setup.conf.example crs-setup.conf4 G, h# j' w2 P
配置OWASP规则
% E; H# N8 `6 v# t5 j) Y编辑crs-setup.conf文件# |& ?6 g1 _, ?
3 [, g7 x, _8 I' W
/ S4 [1 y. I5 j0 l6 V8 w. J3 {
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf: w: U2 ?" i# F" N+ \: v# J( y
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
; v. |& s0 r# z( f" t b$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
6 O9 v" v0 Q' ]" A$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
0 }( G/ O3 `$ B: A/ d默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
% c8 w5 a/ u3 F& G/ K% r/ u0 Z# F: w/ S* |/ ~' g7 K
% D3 B1 D% h8 R
启用ModSecurity模块和CRS规则
/ O/ J; }! q5 R# B复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
$ A) ]/ ~6 V2 X% b: W5 x `. D* n+ c0 F1 t' r0 l: M& T
) P3 ?/ P0 J8 l( S3 t$ j, k* a
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。# p1 f) [0 q" g/ ] S' L$ I
, T. D7 K6 \0 W% C# c
) A' V4 S+ o6 |1 [# u& N$ H0 I$ cd /root/modsecurity-2.9.1/
/ X" k" E6 `0 e- E- v E$ H$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf {8 P2 F5 o8 k1 [, @
$ cp unicode.mapping /usr/local/nginx/conf/
! _; `$ r4 H$ ?$ V, E" J$ D7 a' Z将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。2 W" Z" g4 ]2 e& M8 y
- N6 M3 \7 x; E5 l% C
7 `% y5 L% d) h9 q4 q t" w" s$ vim /usr/local/nginx/conf/modsecurity.conf' m8 J4 K$ q4 l+ g u
SecRuleEngine On
) J1 _" d& B: a. O1 M: C& YModSecurity中几个常用配置说明:2 o" w2 R r0 _ v5 s j- m6 |; n
3 h, o8 _2 k0 W, W$ F( j$ I
) U$ m% l* [2 \' @: ?1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
. K7 T! S2 D) R! Q$ F0 K* |& I. s' L& H
3 _2 ~( B! f9 a* l5 h; h
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。. n0 }9 R- c- S; z: `+ @0 D" \
; j& z1 x8 ` n y$ r
4 Z d2 z2 Z' W
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。9 x- @4 m1 w3 Y
2 g6 k% Q1 C! m. M$ J7 _* M& n
- B1 Y; Y) j4 p4 G3 u2 s4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
T. R3 s2 ^: C% i& y$ j- ?: k# e9 [: _; d
6 u8 I: e+ ^/ c" j: e
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
) n4 P' `- y4 h* A! C; {: j
9 a0 T- f, \' ]
9 W- |% `/ r1 k. V+ S# N3.x版本CRS( t2 U- ^, x4 u$ \. O
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
: q: F8 G) Q' G& q: P# 生成例外排除请求的配置文件
! @- V, X' i+ W$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf7 B2 Y" W4 Y* M4 H* r- G) p
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf' n4 x) W, `6 k P. j- B1 u4 M
$ cp rules/*.data /usr/local/nginx/conf
2 M- p: T2 K4 ^9 N为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
2 ^, y! g [; {0 j1 ~- H+ O0 u
8 P& R3 e' c% G4 [) L
' U) i: \+ A' P q$ vim /usr/local/nginx/conf/modsec_includes.conf
% @7 A ?) R- Q$ `* K1 ^! N+ ~2 N) M
[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 " A1 N) d$ m/ q- b! d4 g1 [7 A; ~
! {* H: {. y- C% Y) O2 z
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。, Y4 W) {' {, q
1 y A2 q Y( T( K( B
& ?/ m$ \% B5 J8 a5 s& `' v
配置Nginx支持Modsecurity
. o* `2 h7 q& I8 ?启用Modsecurity0 L% d8 g* ?) R) |7 i8 H
使用静态模块加载的配置方法
" _$ |! z, R- ~在需要启用Modsecurity的主机的location下面加入下面两行即可:$ [) b7 K2 Y. T. K t( N
) c, \& w+ b7 R
, u& \. e2 n5 ^7 c8 R. lModSecurityEnabled on;; A4 F! D1 _9 n C8 l. s7 J9 X
ModSecurityConfig modsec_includes.conf;" ?0 O1 u3 \/ o
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
/ M+ Y5 r: n) s
7 x9 q5 m* D6 Y; ^: }- D* q6 G, k, U3 P. m
$ vim /usr/local/nginx/conf/nginx.conf
* \( G" E; q# i& H0 V4 Z5 |! B$ V
" @4 i/ W1 z6 ]3 y2 d/ ^8 { }0 f
# A2 Y6 P/ Z- J' I6 y9 Fserver {7 h, @: S( T/ z1 k
listen 80;6 N$ Y9 C/ y; j. E% K
server_name example.com;3 I, h6 Q. Z/ @; P6 }& S
2 o& H. _! z6 l; k: {) i' ~' H
" ]9 a% z$ m8 Y6 @! C location / {/ q2 t" d. y6 t; S! p
ModSecurityEnabled on;, E5 }8 M2 J5 u4 \
ModSecurityConfig modsec_includes.conf;/ r9 z8 Q) i$ X8 ~$ @: `9 [
root html;, I( |; R3 }6 e1 y% H$ ~
index index.html index.htm;4 r U _8 t: i, m, I8 a. A. C
}
% T7 f. F/ p6 O}
' E0 L8 w& M' K3 B: m5 T( c9 @使用动态模块加载的配置方法' k& z4 u) r* ~7 u
在需要启用Modsecurity的主机的location下面加入下面两行即可:
- g1 z. T; }- a( ^& t+ S8 D! W& k0 O j- u
; `9 p0 o' ~# @9 |: ?0 f
modsecurity on;) C+ ]. N2 L9 T* q
modsecurity_rules_file modsec_includes.conf;& V, B) ^1 b* i" Y1 l! X, q
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。& J9 p6 Y# K. G9 D8 Z9 a
! B5 q8 p# b* r( H1 J v* F5 q) ~& |; f& f6 }0 ?2 P* G
$ vim /usr/local/nginx/conf/nginx.conf1 ~: p( c) g7 H! f( z; @* G$ W2 P" Z
# V3 e; \. h# t) U4 l! E; j3 l* h" [* P9 t4 o
server {
6 C' h' ?% B" K2 h$ |$ O( O listen 80;
9 H* B, O2 U) b ~0 Y& v server_name localhost mike.hi-linux.com;% W, Z( q1 o- t1 B0 D
access_log /var/log/nginx/yourdomain.log;
8 G' i: \0 U+ v* x& N/ ?5 u% E/ t% A7 @0 f0 k
7 |2 Y' r3 F0 Y. w- z: Z5 [
location / {
" I, d) x6 t* ?
. _1 k. P% L: P H5 H1 a3 @: f( j
/ {+ K2 w# h6 ~$ P& J) F modsecurity on;7 u# t, t% T% N6 {2 L% w, [
modsecurity_rules_file modsec_includes.conf;
+ W( b5 x9 F1 \$ u7 M5 ^ root html;# @5 T- K# a( ~+ s$ H9 _) w
index index.html index.htm;
' }' W9 p ?! o" C* e$ k}
' }6 t+ _' I5 f" B}0 [7 g: Y; D: j
验证Nginx配置文件- e/ v6 l* J/ B6 K: g7 `, b
$ /usr/local/nginx/sbin/nginx -t
5 O4 s" ?+ r5 _4 D& s% {nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok, c0 F7 J. d4 _! r: g- d
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
: p+ D) B' h1 T启动Nginx
' O s) N) R2 s) E$ m/ b$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf7 ?( [, F( h& n5 k1 x7 i
: M4 M' s* ^8 R( K& h3 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能够战胜更多复杂/未知/混淆的攻击模式。 + K$ `. P3 O, {8 X0 g- r
|