ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
% j1 w& y" r7 _" w' @
3 g1 N3 m4 d M9 _0 |- ~
/ D7 t: X( J" b$ j5 F" W* [; _& E在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。0 K& `. n# l% W8 c0 p% D7 `
0 K3 {7 l* O7 p9 K. h
: _% @( h: H7 @什么是ModSecurity* \( ~# G+ r7 i
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
( M" X7 T) q/ t) L. R. V6 q0 _
/ Z# B- [+ A* g
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。2 y z& Y" l$ C6 |' h! m. R0 `
( s! z3 [6 S+ m+ n1 ^
9 ?. i) K% U- z3 Z1 N UModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。7 y+ o- k+ y$ O ?; ~$ X! t# P
8 U! U# ~2 C. n8 n& T8 p! Q P+ @
; S }/ m! z* V' q9 S官网: https://www.modsecurity.org/8 ]1 [- e& [/ O& y/ x1 T
$ v# B& t5 Y1 I+ e, C6 i
U0 w% `; [% f) x什么是OWASP CRS
2 O/ {, y8 S) E% i& x( z' ~' S) i* MOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
+ N h4 S2 [0 |
$ w6 e/ s' [: F$ E" B% u/ V2 u3 g. \9 I7 _0 K. P! {+ _
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。5 m5 c* J4 d/ u# u; f! l" R
' }8 L" [, W9 Y& m+ m2 ]- N
' F. l/ t m& B+ e! q& S" KHTTP Protection(HTTP防御)
/ X5 S: E' o0 h! ~1 w" {9 D# D d+ ?HTTP协议和本地定义使用的detectsviolations策略。7 K2 ^; n& H) ]- X1 L
5 f9 t0 B! M2 J$ A4 K) K7 I. q- K# a
Real-time Blacklist Lookups(实时黑名单查询) \. ], X- \4 _5 ]4 C4 z2 J
利用第三方IP名单。
; P' }5 E5 V! }. S: L( u
6 \: B& e) }& N) o g0 ]8 n% m' [) A
HTTP Denial of Service Protections(HTTP的拒绝服务保护)! q0 d% H1 ~: Y0 C0 t4 b4 j
防御HTTP的洪水攻击和HTTP Dos攻击。6 M4 l' ?+ U( c5 O4 O% M
2 v6 V' b5 }8 k3 m' B0 b" h2 }9 ~% U+ t+ e/ `4 d
Common Web Attacks Protection(常见的Web攻击防护)
9 [) t) o( s% M9 k7 K检测常见的Web应用程序的安全攻击。
/ f7 B" e j3 Z1 k. z; I# y$ D9 c7 v+ @: H7 f1 _ f
% N7 Q* Q1 D4 k9 uAutomation Detection(自动化检测)
6 w9 u% j% ]0 {1 U检测机器人,爬虫,扫描仪和其他表面恶意活动。+ Y: V+ N0 r& W4 ~5 v* N
6 d4 q: |6 w& i. M% I A; D
4 v5 r8 k& D7 K/ i9 V- k
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
6 `, M# D: P+ R5 g9 `% e检测通过Web应用程序上传的恶意文件。
8 i$ z, Z% O3 `
: j" h$ J9 o- m/ b6 O+ b+ r2 ?' x) h8 h+ q, ` O
Tracking Sensitive Data(跟踪敏感数据)
3 s. Y3 J% @' c信用卡通道的使用,并阻止泄漏。
% z* s# g" J$ }# G- U. E3 E) Q
; d0 d% G0 U9 j J: \* o @6 T4 v9 k9 `% c
Trojan Protection(木马防护)5 B9 U& U! r1 E& Y
检测访问木马。
) A. q" a. V& Y
% l! W* E- y+ g9 s8 q2 r
# F4 M# N n6 n) J" cIdentification of Application Defects(应用程序缺陷的鉴定)
5 @: B5 H. o2 t% F$ g检测应用程序的错误配置警报。
/ K9 ^% o% G* k, j& c! M i& f9 r j
% G8 K1 x! l" h4 SError Detection and Hiding(错误检测和隐藏)( m- f! y" Z# X
检测伪装服务器发送错误消息。/ Q: I9 E0 @' l5 t
4 i; @9 l6 @. U5 _. L- c
" S& l( Q3 C- I* l4 @$ z" `* W, L
安装ModSecurity
1 ?/ Z0 F' r! ]软件基础环境准备
& Z* Y2 s7 D2 O& S下载对应软件包2 ]/ g# D& Z* p& o; j% I4 s
$ cd /root/ U0 d; X! J0 T3 O
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'0 S, [& A9 m1 D9 U5 O; i0 |
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
. M" f7 Z" I* E安装Nginx和ModSecurity依赖包* e; g; V' n8 m( z& ~
Centos/RHEL
, w+ |1 c2 e$ N& Z- W* v- b6 |2 x- K7 |' L$ _
! T; M7 T/ u: J8 I( E/ }4 ?$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel) E5 k7 D- M E; w# F2 h5 u$ c
Ubuntu/Debian
8 ?7 U0 @% F3 B' A; D s. _ j. f4 @3 v: o
: v' X- q% q. |7 b& C$ 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- r- K: ?! {3 e! g
编译安装ModSecurity) B# J& P2 a' m
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。$ \3 h7 |! w( G
+ p/ Q0 k- J# Q y( E. a- x1 S r# e3 _/ r5 L: p
方法一:编译为Nginx静态模块
7 q. x/ Y% `3 G! S- _ Y; |# Q3 S" {5 Y, f( u
) ]" _% a" S" p' }
编译为独立模块(modsecurity-2.9.1) b; Q9 A# [6 G' k2 g& W! v
$ tar xzvf modsecurity-2.9.1.tar.gz
! R0 q/ g" Y' b$ cd modsecurity-2.9.1/
/ _, N+ Q g2 @& N, m' k. ^& A$ ./autogen.sh
" |7 w0 [: f: m$ ./configure --enable-standalone-module --disable-mlogc
5 F- k; h% H" r0 N7 C! ]' B$ d: ?$ make
( u" B! N4 w6 g5 F编译安装Nginx并添加ModSecurity模块
% g4 O8 a4 B5 Q9 `$ tar xzvf nginx-1.9.2.tar.gz" l( C& b2 A7 h ^) X8 _+ y
$ cd nginx-1.9.2$ x n4 m; f/ N" s: k
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/; V+ @# P# P! u5 {1 K
$ make && make install) f/ ]2 m* B+ u' _ r
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
- \6 h' K5 ]8 G/ U: r, U$ O: q! ~" ^0 z/ C# z' F5 |% P
7 a+ ]) r0 n$ r) o8 G9 P/ J
编译LibModSecurity(modsecurity-3.0)4 U5 [! A F" b- }
$ cd /root/ V6 h9 \4 t9 `# t/ y8 [
$ git clone https://github.com/SpiderLabs/ModSecurity
9 Y* S5 u4 l" E4 x; f, A$ cd ModSecurity2 U5 E c( T3 S4 Y
$ git checkout -b v3/master origin/v3/master
* Q& d+ A2 h5 ^2 D4 L L* B$ sh build.sh
* Q. n( d. K3 V0 F$ git submodule init
0 j% F& r* x+ M5 u+ z$ git submodule update7 ?" ^# o+ b* k6 u' x# E4 {
$ ./configure* w* C) I) z3 x! V3 v
$ make/ z! }, l2 p! ]" L7 A3 P: W% u
$ make install3 _. L( s8 I% @: B3 K7 D- u. v
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。, v0 @1 i% s8 t& S0 j4 d
; t* }6 d( A7 v
3 e! P2 s6 X: X: N
$ ls /usr/local/modsecurity/lib" y* ~8 ?+ B4 O% I1 M. x9 l' X
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
7 ~; d' f1 v9 c* d0 X1 Z8 ~编译安装Nginx并添加ModSecurity-Nginx Connector模块
! X4 J' w" i) \6 {使用ModSecurity-Nginx模块来连接LibModSecurity
" g0 o/ |, i" ]9 u5 \, M) N
) X# E% e+ i9 s1 V( b8 j4 \
5 q; V2 n! I9 f6 I/ \4 c: v$ cd /root5 G+ ]# ?1 L0 g* p1 O0 F; ? k7 L
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
" F) l e9 M: b9 _$ tar xzvf nginx-1.9.2.tar.gz3 W- R* M M0 j
$ cd nginx-1.9.2
+ O/ r+ V5 ]1 r% L) y9 [; a$ ./configure --add-module=/root/modsecurity-nginx
# g& @$ K M, W1 M- G# p9 _1 C$ make, z& ?) @! P0 V' d+ Z
$ make && make install
* x# P: J# l) u, g7 V+ ^! |# p添加OWASP规则
8 f6 ~* D8 i( N$ l6 j% ^9 SModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
" ?$ ?4 t& q" G ?! a0 m# M
& e$ g e! Q) C" @: Q# [) s* \ e$ H
下载OWASP规则并生成配置文件1 `( s5 |9 D- D
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
. Q" y" d. Y6 R( R) P7 W1 [5 k- y$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
2 `5 a8 o2 v. Q. B$ cd /usr/local/nginx/conf/owasp-modsecurity-crs' h7 t) q. h6 p1 M) S
$ cp crs-setup.conf.example crs-setup.conf& o: P; j* T- ]: B1 g( g! w
配置OWASP规则4 Y# ]8 P3 V1 F: ~
编辑crs-setup.conf文件& z* A0 ~6 r% j+ g0 p5 r- V; R
1 I2 z6 i: I) a: U3 C8 q( T' j
4 s2 e5 _+ ^# r6 ?" ?# p$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
: p. @( W$ F$ N6 U+ ]% t$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf$ @! Q# E1 t, [9 a8 r0 c$ P4 g
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
) _1 a9 s: Q4 k+ N4 k. e3 J$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf/ {9 c. P |7 u8 S1 _
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。8 B* P0 u, }) E9 R! C5 w
* s* E6 P. n8 l4 I5 a0 U
: m0 V$ G9 [ C% p启用ModSecurity模块和CRS规则# d& N6 W5 n6 @: v8 b- u. m' \
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
7 H8 N+ q& _0 q* v* | u2 y% t i7 S" c6 r
8 T5 v }: @8 `
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。 m( q& G6 o; l, e: u) u$ \3 F9 C5 A% P
) r# s7 \; j5 H* S$ z/ D' E, ]$ D" ]0 y; z' ~ W
$ cd /root/modsecurity-2.9.1/. n% W( w5 j) E9 X% \
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf # M+ Y+ _3 N+ B( N7 K
$ cp unicode.mapping /usr/local/nginx/conf/* R% _; M" u% C l/ t; s) \0 s" P* T
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。. o3 W3 }$ \4 q, \$ ]6 Z$ o z
. e6 F2 L- A6 [% o
, N) u1 b1 \/ k }$ vim /usr/local/nginx/conf/modsecurity.conf
( B( @) A' f7 Z# |7 U4 w7 t, f& vSecRuleEngine On
) T* p8 f, i7 w$ a2 iModSecurity中几个常用配置说明:1 c8 V ? W+ j& ^ f& Y7 T) q
" e. m2 B" f/ A5 d& f5 ~/ l
0 m! @, b' `: ~: M; `1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
4 e; ^6 c( p# ~# S! @" a
6 C, N$ s4 |( j# S5 T$ y. J: [8 M
# G6 u/ L7 v8 G) H$ M, x) M2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
7 h% y5 z7 H6 j/ E' h' S' R4 D6 T# E2 p; @" J l0 Q# }
2 i! s" L% ?# S
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
7 Q7 A5 o/ `" M, Y0 {% ^
6 K: B. ~2 F# I5 U! J& Y. z9 h& w5 D2 p. n' G1 d
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
) z$ D# T$ M0 n, J" J' \: g5 h* B$ X, `
) s# f9 ?6 C' p7 e0 ^3 |
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。3 J. \1 T' G/ R1 [ C2 M m O' G3 p
4 \. @+ h2 l p' a- G8 S" G4 q1 _- T
3.x版本CRS
' O5 U1 k- p+ P8 l& h$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
. z7 _" a- e h( ^% ~8 u- r* a# 生成例外排除请求的配置文件6 U: y6 N6 ~7 F2 N& ?* x7 P
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
6 D% u7 P4 w) F0 }: ^$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
) u8 b" B# E$ c L* { Z! ]$ cp rules/*.data /usr/local/nginx/conf/ z6 C! S( P, A; q; U" ^
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
' A5 R; d6 w) n' N' W
4 u# M. W3 @. j
9 u, D0 P7 i' P5 L1 p2 k$ vim /usr/local/nginx/conf/modsec_includes.conf6 |0 F1 t4 ?. G3 K9 ^
- q5 d( c& G; f% R
[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
1 H/ V% {$ z: x4 a& z7 }/ y
- x3 _' v/ r" I9 e' d注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
1 }$ d5 O T& f
' ]4 X7 q& v2 F4 f
5 O6 g- @ S% r& I配置Nginx支持Modsecurity4 F& J* d: u5 L
启用Modsecurity
. Q0 D# W( F. e5 G使用静态模块加载的配置方法+ r# E+ r. T7 ?
在需要启用Modsecurity的主机的location下面加入下面两行即可:# N+ d9 C& s9 ~
7 ]7 O6 T8 t+ m+ X1 S. H# g d( L, V8 A7 U+ C
ModSecurityEnabled on;
' [6 D4 x9 h. A2 KModSecurityConfig modsec_includes.conf;/ Z! v4 @' a" ^4 P3 q+ X# c
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
8 y$ k% C5 I4 i( r* |/ _$ Q9 a# E# i( X! ^9 V' g, ]4 g
4 C" U. I9 `* T: T
$ vim /usr/local/nginx/conf/nginx.conf' m0 a. {4 f9 ~0 k& e
( \* h, `" x+ a" C% j0 Q3 _3 ~" N0 ?% {: ~: z+ A+ {7 ~
server {
2 k6 k+ z- Q2 Z& R listen 80;
5 z9 F. l# T4 Q5 ?- P server_name example.com;
8 T; i3 ?8 K8 F; A4 L5 g9 ` Y& ~
+ T& Q8 V4 \" E6 W, V
location / {
4 ~3 k4 M) p+ ~2 z ModSecurityEnabled on;
2 ]- y j2 ^) y3 n7 A: l' @6 t$ X ModSecurityConfig modsec_includes.conf;. f W9 \' h) j# s+ h
root html;9 `2 J3 O) l$ p$ L R
index index.html index.htm;- x0 w: |" I. \ \! c
}
$ s6 |* T1 U l9 {( Z}$ P# M5 d: U6 y, g/ d1 e* A2 B
使用动态模块加载的配置方法" n7 Z2 E0 w( g* i3 R4 M* b+ }/ P
在需要启用Modsecurity的主机的location下面加入下面两行即可:
1 \$ E A! K; F+ L- s( v: @% g0 h# n% Q# }
0 z7 E1 O' v+ a2 \& X# L: V* lmodsecurity on;
+ d; [1 I4 x Z, D; gmodsecurity_rules_file modsec_includes.conf;
; `: R* T; V; O( i' `' V修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
0 E# G0 q# |$ y$ P) m$ E
3 t2 Q8 d1 a& Q& \2 c0 Y
+ K0 `; H) p, W9 w0 p& A. H1 O$ vim /usr/local/nginx/conf/nginx.conf) h6 J9 L( J1 Z7 I n: `& X
% C3 c( Z5 \5 P5 O9 A/ b& P) B
/ R5 n* |6 A* T! u8 }
server {* S2 h$ R5 L9 @- K* d: v0 F5 w
listen 80;
1 O$ P; O9 U3 I6 u4 O3 ~) p; j8 w server_name localhost mike.hi-linux.com;
2 s" r' Q( E3 g3 A' b# V I! t access_log /var/log/nginx/yourdomain.log;
' }/ K% z/ R/ n- t# O
6 c: c3 g4 m7 H8 l4 A5 T
- F' [$ w `' [) e9 e location / {. }5 R- [3 T2 a3 Y! L0 w
7 D4 f) b- `7 }& P3 K! w! ? Q4 ]$ R
modsecurity on;5 Q% Z1 ~, w# `' [, D
modsecurity_rules_file modsec_includes.conf;
( R; Y& C) u0 O: m) S( Q+ o# Q root html;: ?" q! O4 C% v1 d
index index.html index.htm;1 T+ g v$ P" M+ I
}. \% z4 o& `, C+ u( Z
}
8 n) O/ S6 k( \4 ~" T' p验证Nginx配置文件
3 V: U8 G4 F1 s j$ /usr/local/nginx/sbin/nginx -t. G& R8 B2 @, _; W; \! _
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
8 Y* W$ y2 | v$ w$ q6 Rnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful6 e5 u! ^9 H- v, S; F" m; s4 x
启动Nginx
8 a! }: l5 K8 g8 B4 h$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# i; }3 h7 _3 \2 V" n: _
7 b% D. H$ c! W/ d1 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能够战胜更多复杂/未知/混淆的攻击模式。
5 Q$ e7 w3 m8 e) z1 l |