ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
% e9 B# C, }% m7 b5 @8 ^2 w
- ]) |8 u/ j% y+ I7 p, ~ b
- }6 H6 O: R! j$ d% d/ {( L1 k$ k0 B在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
- e# B$ t. h3 ~# G3 b, c' \* `! s( B2 y: q: T2 t$ x
; z. A" L/ r, F1 `什么是ModSecurity
5 O) O- j' c( ]& I) hModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
# k8 \! ^# \) e# X) H$ } t% M* i+ S7 R1 P) j3 H# q: _4 G; ]% a4 {. a
9 U$ T" [$ @3 ?4 E0 _ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。4 y" c3 L' V4 Y0 L2 C
' a9 Y# r6 j/ u! p) ~- V8 a
/ I+ G8 i0 l. y4 m8 LModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。8 h: l4 t) B) p. e
! u" o* m5 g4 [" k9 x$ a
6 F8 Q* R4 z: f& L
官网: https://www.modsecurity.org/
- {6 u) o4 {# J& M/ O7 y, T j; Z/ R
% e* h3 h3 m$ H7 |; x4 D0 W- @什么是OWASP CRS2 v8 P/ s9 S2 ^8 W* Z. M
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。0 v7 R n( M6 i6 v. n
6 J% h, _# T* y/ o
7 c1 Y, _: O" Y" Q# dModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。! A- _# P( x+ C5 D1 E
: F: g. t, L7 x3 t5 }4 D* X
" c: U2 ?0 z( P
HTTP Protection(HTTP防御)
/ m/ y9 M' z( Z' V$ N/ }HTTP协议和本地定义使用的detectsviolations策略。
- s a/ f+ }' N. r% O# H% A7 ?2 H
! o8 h- f0 \$ K- N9 E! R% Q# \
/ e# W. T& B2 }2 [3 |7 WReal-time Blacklist Lookups(实时黑名单查询)0 [) W4 b! c: P2 \8 y A
利用第三方IP名单。
! S9 i+ Z: h( M `1 R! T1 f
8 l; }, R; B: |+ Y- z9 e: D5 r" ~7 Q4 X+ z* E+ W9 a8 Q/ [
HTTP Denial of Service Protections(HTTP的拒绝服务保护)" Q! p7 W0 l$ Z) m! l6 @
防御HTTP的洪水攻击和HTTP Dos攻击。
\* X" ^' |4 E! ]' Q0 C- ]
; _4 a6 y" E# h5 i! J+ v8 j% s0 A% Q) @. Z6 u2 o* {
Common Web Attacks Protection(常见的Web攻击防护)
* z8 l7 b. p9 T% E7 c检测常见的Web应用程序的安全攻击。
5 p1 N" h E0 x) r( @" P1 @+ V: O" L$ W, [+ w
( x- F# g$ g: ^% A8 \
Automation Detection(自动化检测)# |' u" G l. L6 X' O8 v( i% U
检测机器人,爬虫,扫描仪和其他表面恶意活动。
* V4 ?8 f5 j$ ^5 o) d: i
' e; m6 @; {) J) R: M- B% I) K4 ~" y( Q# D
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
4 a# [- _! a5 \! F o2 c检测通过Web应用程序上传的恶意文件。
( Q1 ^, P7 R1 e) X
! ~# `6 I* A7 O6 |7 L
$ r, z1 M: @& d$ A8 {Tracking Sensitive Data(跟踪敏感数据)% A: E5 f! f+ x( L. q
信用卡通道的使用,并阻止泄漏。
/ r& x# t3 h/ y- l1 C, Y
. @ v, }# D8 n! n* w' R
: W) D2 A6 D D% hTrojan Protection(木马防护)
$ Y; f8 V! M) u: C0 M检测访问木马。8 J! C2 O( I+ d/ Z7 o
6 E3 R0 o- Z3 K; V% w
1 m) i, {. P. u5 i
Identification of Application Defects(应用程序缺陷的鉴定)
& S/ W1 l8 L9 ^% o; J b检测应用程序的错误配置警报。
# K$ _7 E0 x. M$ d
, R. J. i8 k" s
( n+ W% W8 w- O% Y7 iError Detection and Hiding(错误检测和隐藏): p# Q+ w- M& ?; a& I6 @- z) ]
检测伪装服务器发送错误消息。7 w1 \9 S& \4 D+ C) T7 U, C( H
0 }& u: V' [! H' o' o- P- s( ^$ i" c" t+ \2 k' k
安装ModSecurity, n$ i: a! c; m6 i
软件基础环境准备
# Y. ~( x. T9 g下载对应软件包/ Z! A$ a9 _, H7 F
$ cd /root! |; W3 u* F! C, d- h
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'/ e) n# M7 u s# k5 w! G* [6 G* n
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
8 \$ k; R: ~# L- U& A安装Nginx和ModSecurity依赖包9 ]0 G( K2 G. w: Y+ m" O
Centos/RHEL- _4 D- I8 ^) @5 L
" U# \. w! w" v. n- @' D2 J
) ^( u+ V% e [' V% s; Q$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
& Y4 ^4 \! Y9 r1 s4 pUbuntu/Debian0 y& W! K6 {4 K l! u
- U# w- L- P( A7 k5 ^0 w1 D% c; Z, Y9 [0 Q- 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% a) F: B: M; s/ Y. m
编译安装ModSecurity5 `; a# ] P# c: b9 U1 Q% g* v3 ]+ Z
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
- o( z5 O/ Z9 I1 ?
Q. d3 P2 X, e+ w& e0 X* t% [; X3 J9 [( w+ z0 V$ x1 S
方法一:编译为Nginx静态模块2 K0 a0 i( e7 z0 C8 e
8 f1 q. h: b$ _/ ?; f+ F' @
: y2 f! I7 v3 w
编译为独立模块(modsecurity-2.9.1)' _3 Q% `7 e, G
$ tar xzvf modsecurity-2.9.1.tar.gz) p9 Z! b; J; ? N6 x: B1 \
$ cd modsecurity-2.9.1/
# w$ f+ C# i- @1 T) z5 j9 v: Q$ ./autogen.sh: h/ n5 q4 ~9 N5 K! N
$ ./configure --enable-standalone-module --disable-mlogc! u. i" W u, N
$ make
+ b: u' Q" S" O) ^, P2 Q; P; ]6 V编译安装Nginx并添加ModSecurity模块
$ ]# Y) L/ \4 T- l0 i! ^9 C6 K8 r$ tar xzvf nginx-1.9.2.tar.gz& e& g" Q, x7 Y4 @. v
$ cd nginx-1.9.2
* v! p1 w1 M+ r+ S3 L: V/ c) X$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
1 _; z9 s1 F" O6 V$ make && make install
8 d+ }: N, e3 \* y方法二:编译通过ModSecurity-Nginx Connector加载的动态模块 m' S( ]$ u+ z3 \4 B. h
, L, u# g7 [' q f- g0 z) s% _; r f, Z: b- L7 ^! H% q
编译LibModSecurity(modsecurity-3.0)) E G; v6 N8 B
$ cd /root
; s2 F. T* i( c" d: r$ git clone https://github.com/SpiderLabs/ModSecurity
0 D" M7 o% A3 _; ]$ cd ModSecurity
& c: C6 y5 n5 [ \& \# I$ git checkout -b v3/master origin/v3/master2 b$ I& M+ @9 m* y& x
$ sh build.sh' B8 }3 {' T& j3 w+ t$ ~( ]1 R
$ git submodule init" q) y E8 E+ M
$ git submodule update7 p" X% D9 E7 c7 G
$ ./configure) E) ?/ Y4 M% w( D& Q! L- l
$ make* J- S6 H) [ X1 F
$ make install- ]0 F7 u; T6 o/ M8 ?
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。* N/ L- _: s, q8 L
, x8 L, v1 _5 @$ ~! {
. [% R( C; p! N/ H: m R$ ls /usr/local/modsecurity/lib
" U$ z! y' |2 j" T" g* r4 ]3 ]4 Tlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
: n/ ?/ B. `3 a5 x1 P编译安装Nginx并添加ModSecurity-Nginx Connector模块* N" `. C+ q; L8 d# \
使用ModSecurity-Nginx模块来连接LibModSecurity' X9 @( D' Z! t! R) U& c |/ l
! q7 g+ j0 [3 G% R% p9 p, }. \0 @( g$ L6 {! L# Z: S" e
$ cd /root4 v6 x! _' n, [1 T' @
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
6 q( d, D! C2 S. j* Q: F3 ]$ tar xzvf nginx-1.9.2.tar.gz/ U4 M+ Q* Z8 c' @) l* T$ {9 m0 ]
$ cd nginx-1.9.2 u7 c6 v0 [# Y& u
$ ./configure --add-module=/root/modsecurity-nginx, e* T: U5 G0 l' a+ z+ k. W4 V
$ make" v0 c8 s8 E, X# s( u
$ make && make install
2 T8 P! v9 v5 A3 ?6 Q& \8 X: s添加OWASP规则
0 R. I; D$ E- r" T" E; g( p5 }, LModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。$ E) [" p$ _" a
. z6 T" J8 [, k/ I8 L: u, C% t
9 w. S3 J# Q9 h3 M; u( B2 y! q# _下载OWASP规则并生成配置文件2 O5 Z2 Q- v5 W: l! J+ Z
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git( }- N2 @5 X. W$ E/ o6 Y
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/% P/ E" N( p, x2 Q; Y1 k
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs0 Y X) r6 I3 n% W0 O2 x0 I: A
$ cp crs-setup.conf.example crs-setup.conf. Q$ x6 B k. `; [: A
配置OWASP规则1 s2 M+ u8 z3 C# B0 g9 C" \- v
编辑crs-setup.conf文件! l/ j' N) ?! ]9 Q/ {
+ p2 g" S1 F5 u) l4 v. T
, {5 |2 Z$ r4 q: q: \- n7 j5 a
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
+ r! N, W- _ v9 |! q$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
. D' f# m, t+ d2 e! N$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
& w7 @ i9 O" G) ~3 o$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
- R# f* T; w+ q) g默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。( J8 u, I, N. Z; m+ }
1 c$ K, E) p, I
! O8 b3 Y% L: A( d' S6 L% [启用ModSecurity模块和CRS规则( d; ~! B/ z3 A7 [# L3 T6 T/ j
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
. j) I! p. u7 l! Z' }9 b8 |) v7 }/ `! v( h5 i+ u
' l/ n3 Z; z2 i* F
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
; z4 P; M* h- }- {+ g4 F$ G2 u( L6 n" J0 C' w7 [/ r
]- \! ^! Q( M" a7 M3 e
$ cd /root/modsecurity-2.9.1/" x( k' k+ D J7 i* ~1 r$ V
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf ( l7 Y; L2 S6 q4 ^
$ cp unicode.mapping /usr/local/nginx/conf/; C5 Q2 T3 x- L4 d( f# f8 g
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。7 L) `" F% J% Q. m7 _- B
' C5 |3 B8 h9 x0 r1 r3 v
6 i& U# l; c. @; Z/ @/ V# I$ vim /usr/local/nginx/conf/modsecurity.conf
" H4 [. P$ A* _% \SecRuleEngine On( v7 H9 D( O5 n
ModSecurity中几个常用配置说明:9 t' z4 \5 P: Y8 N' X
9 L( }" d( T; ^( H3 Q( t& X
; J* a, E. s% t( `0 N
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
: d6 G& Y9 r# ?2 ~$ T6 Y
8 f7 ~; v& Y& l3 x5 ~
[+ R# e) D% M8 o9 M2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
( s1 C0 D- L1 J/ L& t ^; i9 y
( i) V7 d1 y- b, F' J9 r( }; m- E9 _0 x' g
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。. u/ x4 u# \+ Y
- }! [7 u5 V' [/ R7 A( Y
6 i) H+ v, w( l" Q3 y& P$ t1 {8 b4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
, b( X8 c8 D) w- Y7 x9 ^
3 ?6 h+ p# n8 f$ ]; c' J& {" Y: }* n3 w6 Y$ H. r* c3 L2 y
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
' p6 ?, w B2 t3 b$ s# Z
, ]; G3 ?4 Y1 ]2 F$ ]) b% k' ^$ Z- u4 a, W
3.x版本CRS; z' n2 ], _' k" Y
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs' X. J2 g+ V: f. j; T# N# ^5 F
# 生成例外排除请求的配置文件) f8 o4 W2 P4 a% _
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf5 |& V! T0 {2 v( l
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
- g, G N( H7 ]" c$ cp rules/*.data /usr/local/nginx/conf1 _4 h: {0 H, e* \7 i9 A# O) I
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
9 O x1 q; ~% U& j4 B& g4 e0 e$ _# w; j% L/ e& y; R
4 B' F4 H( r5 i# b) c
$ vim /usr/local/nginx/conf/modsec_includes.conf \! r2 A/ G1 D7 g, ]' [: v
( b: Z' |5 u3 l% [6 d2 y
[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
2 a! E4 Y5 m( i" Y* M6 m1 p8 [9 ?% E7 X
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。( z$ G0 i# E N6 T+ G
7 c$ w1 q) }" U& L
- T7 v. w9 x8 }& S9 R- U2 @
配置Nginx支持Modsecurity
R; B% [/ m& Q5 C启用Modsecurity; Z* F1 z9 w/ |: a- ~
使用静态模块加载的配置方法
" w5 `% d# F& a7 x7 n在需要启用Modsecurity的主机的location下面加入下面两行即可:
# b, j+ l/ y2 d z3 {- r) S4 w0 x6 {) Q! V5 ~! e% `
+ Z+ T4 J5 j3 x4 }
ModSecurityEnabled on;
+ E8 b) M- P6 [# e% VModSecurityConfig modsec_includes.conf;# b+ R8 `+ L7 r! o8 m/ g3 ?
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。3 ^2 t1 R" j% I. D1 c
% ~4 q! `/ x, ]/ m7 `' m, @% a
4 C* \6 y9 Z+ q% V$ vim /usr/local/nginx/conf/nginx.conf, l# U# i. H" {% ]1 ~, u8 s
6 }0 ^& M" Z8 F" l; s& h7 K# j+ O( V
server {3 \1 e& [- n) b4 y$ Y* }( e
listen 80;$ ?: d: i% B$ A/ h6 Y1 l
server_name example.com;
7 @+ V7 n& h g9 T% m0 v F3 @
$ _/ y! Q1 w f R3 N! l9 `& h: Y+ ~; x8 V
location / {" S9 j$ o$ @' G% _# q% ~
ModSecurityEnabled on;
# q- ?7 q9 n& c% }# `# q( p ModSecurityConfig modsec_includes.conf;
" N" P5 r+ N; a4 j2 \( N% j; u root html;7 U+ ?; _8 \( t K/ A& h( G
index index.html index.htm;7 E/ M2 P6 ^/ j5 @3 K
}
. B9 X9 y5 p- I$ c+ s3 }) D `% Q}
! |& n+ f* K2 }4 n0 Z使用动态模块加载的配置方法
& B5 C8 r& V7 C! |. d" K在需要启用Modsecurity的主机的location下面加入下面两行即可:
0 j5 f+ o+ ?6 K8 K: B" j6 R+ s" C/ {/ S! B- ?# ~
& Y& Q8 K" O2 S# } \) f
modsecurity on;' t0 j/ E, J" v! ?1 T( }7 ]
modsecurity_rules_file modsec_includes.conf;" Z* l9 {" j. n1 x
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
' m) e- _$ @' k \. D P Q0 P- E, n, T% v$ O5 C. ?
2 f. H% a6 @; V. }9 M
$ vim /usr/local/nginx/conf/nginx.conf
: x6 V+ v, o: D% t5 \# c. U ^
7 Y& m+ [8 D* z0 N' r2 H, R8 f5 r: t, V' S
server {2 t; y0 C- Z" h. v3 V2 c0 p- `
listen 80;; i* B6 D( w( E- m2 \3 C
server_name localhost mike.hi-linux.com;% H. {$ b+ d k, P. F( Q) T% c
access_log /var/log/nginx/yourdomain.log;
- W* m! L1 _9 D" C) u8 m3 ^# q1 ~- E0 N7 F) F6 \% S4 C
; S- K* y6 t+ {, L
location / {: _8 r! e' g0 i* y
" F. E, C, T+ g( G3 O+ Y) q3 A. _# D+ }- k( D
modsecurity on;
0 B4 i5 T- W7 x modsecurity_rules_file modsec_includes.conf;/ G) L; f+ K6 E0 _' o/ m$ y
root html;
4 p& u; [) B4 t index index.html index.htm;: D9 a; d3 R3 r x# l7 C8 {
}
- U! j1 J- d' g}
: v/ Z" Y/ P' g- P+ C验证Nginx配置文件
( T3 \) ~# {4 R- [; j$ /usr/local/nginx/sbin/nginx -t
9 I1 m% `0 x' N- e0 x& K9 wnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
' K* t/ p( ~) anginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful- e$ Z- s a8 [% d& j/ ?
启动Nginx
' q1 s1 F& d7 G& y$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
0 k, z- c+ |" ^5 X' ~5 ]/ r7 V4 U( _" Y
测试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能够战胜更多复杂/未知/混淆的攻击模式。
/ ^- Z G; ~6 A" r; D |