ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
4 ~7 ^' e" I+ R3 b2 m
3 }/ ~1 {0 w# }, i# J1 o3 H% `2 i4 d
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
) P7 k- D8 l# c, H
5 F s% j* K" ~1 L2 c
' _& E3 x; U0 y4 S3 |' }什么是ModSecurity" n5 ~- }+ e8 Q- T6 z) U; ?) D$ O) }
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
- s7 c" I$ c+ s5 y4 K7 B0 E0 W* E
% `* w0 W. \. Q3 E0 X1 E& D# Z2 rModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
8 b& _* I T! d3 m/ ~4 l) E, g/ V n2 ]+ K! v
5 w C. o8 r" D. Z0 ~
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。- q! g$ ? ]2 X1 W; p- S
/ G% P$ H" _8 b( _8 t2 n/ s( n, L4 P5 D& ]
官网: https://www.modsecurity.org// ~2 Q: ]& H" J/ H, g4 T
; k3 h# T9 F6 v# c/ }6 q
; B f% o+ g8 r什么是OWASP CRS
3 Z5 z1 I2 P. \& ~0 E/ p TOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。+ B- ~, T' N7 B- q/ M9 ]
7 P2 \0 N. U) F) u8 T8 f# w/ V* @' g* P+ u, Y
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。; h0 {. X/ t" g& Y
& R% h/ u/ j& X) @7 G3 ^" I: e9 y: P8 Q% R! Z
HTTP Protection(HTTP防御)1 r, U. u8 @ p7 j9 K3 J, O
HTTP协议和本地定义使用的detectsviolations策略。7 Z- V1 h$ a9 p" J" l+ d
$ F- w6 M, \# T
) c$ ^- A6 }& U8 FReal-time Blacklist Lookups(实时黑名单查询)
I+ @0 }. p: w% ?( e利用第三方IP名单。6 z d1 ~* v, e X
, q# y0 w0 @0 a* d: y: t
; I) ^9 D2 F4 \/ c. hHTTP Denial of Service Protections(HTTP的拒绝服务保护)) a- u! w* D2 o- U& n, O- u
防御HTTP的洪水攻击和HTTP Dos攻击。+ H3 ?! X3 _6 K1 J9 Z7 n
; v1 A5 T7 Z$ W1 c( ?' E" X/ r! E' u# I! M# H) B
Common Web Attacks Protection(常见的Web攻击防护)
3 V: `; v3 A5 P" l+ G( \- _6 f检测常见的Web应用程序的安全攻击。; P" i2 ~1 K5 S7 B! e0 K# L4 c
/ m# }: [& S7 G ?# G& M* e* ]
! w' ]0 z" d9 k: \: K$ c* n# nAutomation Detection(自动化检测): `% u# Y* R7 Y' f) _: z' M
检测机器人,爬虫,扫描仪和其他表面恶意活动。
% y) Z5 A. {4 G2 B& I5 Z3 V. y3 }- s- J5 q8 V
- W' O, ~0 Q+ G+ {
Integration with AV Scanning for File Uploads(文件上传防病毒扫描): N+ p' ^8 l9 A3 A
检测通过Web应用程序上传的恶意文件。# R4 ?4 K; _% F Q; ^7 H7 ~! w
0 \* j' ^% v( ^( t& ?! c) G2 e/ L% r8 \8 B
Tracking Sensitive Data(跟踪敏感数据)
+ w6 R3 Q! a; c; Z, N+ }信用卡通道的使用,并阻止泄漏。: u6 w, F: H; E* X0 D
% `, d: |) h, ?0 p0 E: c
$ j5 Q: A( Q: J; _3 eTrojan Protection(木马防护)- N' Z: p1 n& f5 z9 B6 h! a: z
检测访问木马。 } c7 x0 U7 M
- e( C0 x5 U9 Z9 W) V$ s* z& a5 @/ q1 V1 d
Identification of Application Defects(应用程序缺陷的鉴定); s" v4 Y! c6 O/ M0 F5 e6 N) b
检测应用程序的错误配置警报。& i: H, }. S( e$ |0 ~! E
$ a/ Y" r" L0 U- u( D. ~% U# \9 W$ V
- ~5 _7 B5 K. w. |3 ~
Error Detection and Hiding(错误检测和隐藏)- T: q) ^4 E+ F* M. d9 |
检测伪装服务器发送错误消息。# B0 n9 Z, |$ M, b& Q6 f4 F7 S
- M/ Z" j8 {# \) o* ?, U/ a, Q+ s; e% S
安装ModSecurity
+ Z' q6 r$ l. @ b2 |软件基础环境准备
! k7 n& k- u" F" U- R( A下载对应软件包
% ?7 v! R; ?6 k5 k' W" D! v5 l$ cd /root
5 J: \: ]* i" m! G$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'( C) G0 K+ |' c: ?' h* x, Y
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz: u) D3 \( \4 z) U
安装Nginx和ModSecurity依赖包
4 P# X# W) |9 W# w0 e5 ECentos/RHEL
2 @% r. O7 a3 X1 V! [, r( _
. S& i0 J3 N- o7 \8 a" K4 p1 i h, d0 ^3 x+ C) M2 [
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
+ a: [' o5 f" Y$ m. {4 X$ v- tUbuntu/Debian
* U1 _- U" W G' o( ?3 O! w5 _0 n! B
A8 T7 x) p m" ^1 N* @$ 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
3 R3 y/ T' H& y/ S0 @7 m编译安装ModSecurity6 \: b5 \) K: x
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。* T R0 M1 z, e: r# _; d* p4 `7 r
" e$ P9 G9 n6 ?0 n
: k0 F5 B) x- f% {方法一:编译为Nginx静态模块5 r) L& K) R1 X+ {
3 l& Z/ g! s, r- m) ^+ C1 b; j) `7 C( _; _* T7 Y
编译为独立模块(modsecurity-2.9.1)0 }4 u# d+ t' r/ u6 Q- {8 M- O
$ tar xzvf modsecurity-2.9.1.tar.gz5 v$ g% c# T% O9 ^' U0 D
$ cd modsecurity-2.9.1/
1 G* J' t5 D, z9 w$ ./autogen.sh
" k5 n% k' ]1 [, _$ ./configure --enable-standalone-module --disable-mlogc& j, Q9 M Z& Z# a, B, p1 L, ?
$ make/ f* _& o' U+ m
编译安装Nginx并添加ModSecurity模块$ U9 u9 f. M/ l
$ tar xzvf nginx-1.9.2.tar.gz& d/ C+ A$ q) j( R
$ cd nginx-1.9.2
- F6 ^4 r6 I3 ?7 @$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
2 @$ B1 Z* ?7 J5 r$ make && make install
' l9 Y" v3 p4 {; n+ X; Y5 S# B/ r方法二:编译通过ModSecurity-Nginx Connector加载的动态模块6 g* L. |# A( c* f0 w4 c
+ }$ @7 E# }9 n d4 R1 X! \
$ m+ q# I4 T/ i9 g1 V' ?编译LibModSecurity(modsecurity-3.0)' q: P5 Z8 v. @ N+ h
$ cd /root
- ]; j- f' p3 _6 \4 j% @$ git clone https://github.com/SpiderLabs/ModSecurity
/ C1 C% p- b4 D1 G( ~6 ^% {$ cd ModSecurity; M+ I" N$ T# o$ Q
$ git checkout -b v3/master origin/v3/master
/ N' G' U/ s$ d9 B$ sh build.sh& G3 W# v' T% x( K9 B
$ git submodule init) X3 y% w5 ^5 b& T0 M. j5 J
$ git submodule update7 E0 Q, A% P3 z) r$ }
$ ./configure8 z) N+ l; W" L$ _$ @) |8 W) w d
$ make4 u! M5 D1 H* A4 F
$ make install
4 I4 K: l" O: [7 L/ j3 b. z$ b! O- JLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
5 J# @ v t. Q, D: I9 s0 b
# ^6 x6 t9 |. K* S0 f5 {5 c+ A2 T# D8 ?1 E5 }' @
$ ls /usr/local/modsecurity/lib$ W3 O; n B; ~/ f8 E/ s
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.01 c+ o z) d7 o6 Q- M" X
编译安装Nginx并添加ModSecurity-Nginx Connector模块7 w! G* X% B. S' u- a- h% V
使用ModSecurity-Nginx模块来连接LibModSecurity3 L% v# c; j: V @5 b; }( c. A
& d! ^0 I& x) E$ h( \& j0 c
% i6 R" Z- F' e) B
$ cd /root
% I- Y# }9 d9 i& g p4 i1 {$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx- L& T p0 X+ E7 D
$ tar xzvf nginx-1.9.2.tar.gz
1 ~ o0 A4 e1 t3 E7 I* c$ cd nginx-1.9.2
, {2 P) m5 c% H3 v1 V$ ./configure --add-module=/root/modsecurity-nginx
" k, _+ D+ W$ ^+ B: i! j G. L2 h$ make
3 @- L, l4 q0 D/ k$ g$ make && make install0 C' W+ o* o- z2 R5 x
添加OWASP规则# {9 L3 C. h2 F. h
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
' i# B" `3 S1 @& [; e8 K7 y3 U7 T' Y U% @
; I, H. X' E9 I3 T: L
下载OWASP规则并生成配置文件
% g8 G3 V. w+ A$ w. V- N$ L$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git2 e+ |0 L5 J" a
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
9 S1 n/ m( g4 P) r$ cd /usr/local/nginx/conf/owasp-modsecurity-crs/ l4 f7 c* _% @0 [" g- k N8 }6 ?
$ cp crs-setup.conf.example crs-setup.conf
8 i) {+ J8 e6 z配置OWASP规则( c' T# L2 Y$ d1 }/ c
编辑crs-setup.conf文件$ w! }- _* w8 W0 [3 a, Y
$ t8 R) G. S$ _' _5 W) D: x! f
/ }& C$ u! r, X
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
7 I3 v$ z) N& `# h& ]0 [4 [9 @1 c; ]$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf" W- B* g$ m/ V( L o
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
% j4 F# q5 b6 L$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
7 O0 c' J4 ^$ R% }* p默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。; |0 H4 I! } n+ R( I' F
' K) |/ q5 N+ N
1 v7 z+ f. f( e- Y, _. p8 I# G; s1 }启用ModSecurity模块和CRS规则
- K4 s8 e* S0 p) H, e* h复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。/ X# z9 D! m1 H+ c$ W
4 }: J! ]# c2 S H; `) V5 e, D
_9 W: c+ U- Y/ Z, h7 f
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。2 Y$ N/ J* V2 \! E( A8 t! } F
" O% M# u4 g( g' l: u* [( p# t; G3 A+ Q) Q' U# x+ j! U# }
$ cd /root/modsecurity-2.9.1/+ H6 _0 \# O! R: e
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
3 L& z) d/ X2 `3 H$ cp unicode.mapping /usr/local/nginx/conf/, N* m. E5 X2 s2 K
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。3 q" ?7 \* R: W$ t
w( P3 _- h* p- v4 |# M
0 d8 k$ @8 S$ L" t: ~$ vim /usr/local/nginx/conf/modsecurity.conf4 C8 N. y) \6 c+ T# k" o- `
SecRuleEngine On2 P( ]% c, w Q$ k, Q
ModSecurity中几个常用配置说明:. x* j h& B+ y2 M5 ~' j
: Y9 X) b7 q5 B9 C4 k9 Y K, Z" R; F l! o
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
, g6 S7 u% }5 O& r6 |9 z
. s/ s( V' }( t& D# e F) C) v! x6 |$ |& t
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
3 z( Y8 M; r+ m& A1 d* J4 l3 Z
8 T# d" K5 R& k: ~* N$ j* l
: [0 g+ }( ?: _/ r- X; S3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
2 {. ]9 p) d4 ?. N. E3 h
: y+ S* y4 P* W4 Z, O1 [. @
! e0 n4 M2 O E9 W& ?4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。6 O& u4 I0 x4 |9 J$ X1 A; B9 n$ w
, X9 ]0 K$ o) S$ c$ Z/ i6 {
0 H) s; J' | @ W. W
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
* E# z. t4 T' V; m% x v' I
& ]; p1 K7 N8 j' j2 t
/ q% j" F: Y4 E. K8 O" [3.x版本CRS
2 C/ y! t. z' r$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
+ y' }. n, i1 B# n: d/ \# 生成例外排除请求的配置文件
8 }2 Q0 D3 @7 y9 _! r$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf5 W/ s5 l0 h: l L
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
- r" K- d0 \% V5 R Q$ cp rules/*.data /usr/local/nginx/conf C' T Z7 d" ?4 i7 `$ q
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。% F9 q7 K& a2 ?2 e5 I, Z
( b0 e O( Y$ H9 i% _ r! E$ g$ {( @. A4 K+ [
$ vim /usr/local/nginx/conf/modsec_includes.conf
( z8 @* ~) W2 c$ x$ {# u7 Z, U* r! }) Z: t6 J! w" {3 @
[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 & K3 J% b1 {; X9 C: q& u: E
6 F! V3 S& Y1 ^, O8 y N: f
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。( N, x, H4 r0 Z2 B3 G0 y: x
6 U! C% R. _: a' ~6 ~; U; c* Q4 ` w) i! U7 r: F
配置Nginx支持Modsecurity
* Q8 `( N4 H& k1 N3 W启用Modsecurity. H. ?7 O: C' x |% r; V! z
使用静态模块加载的配置方法7 M h$ T$ l$ b' x5 L
在需要启用Modsecurity的主机的location下面加入下面两行即可:
9 l+ ]3 c( q- L- X
1 v6 {& ?. S, F: f# d G7 v
$ z5 }0 @% b% @" AModSecurityEnabled on;
& W, x' p0 M! ZModSecurityConfig modsec_includes.conf;
% w& {$ t: R. i& q, Y, s1 ^修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
+ w* o9 |; x5 Q# I8 S8 C1 o$ }# h
1 @8 w+ u" E8 D% a5 h5 H$ N9 ?$ P. u- E9 S5 i
$ vim /usr/local/nginx/conf/nginx.conf: K: x5 }/ f& b$ f3 i
1 Y9 b' A4 R% i6 h, s: a) J4 b0 f$ G* ]$ b% V9 b2 R6 |; i
server {
' ^* q0 K8 n1 q2 a listen 80;
) b$ |. B9 t4 J6 K, g server_name example.com;
2 i' L* E3 f, u$ t4 @6 ? X7 E/ p, m Z# R& Z; q+ o/ }7 D
9 v3 @) b4 ^- h0 r location / {! ^& z1 Q; ]8 U- z
ModSecurityEnabled on;3 J; A% n2 q% o/ m4 S. {# m" V
ModSecurityConfig modsec_includes.conf;, s' I# k- ?1 b. w3 Y
root html;0 ^3 H8 J: G$ Y1 c8 m: C+ X
index index.html index.htm;
$ S3 n" y; | X% C* p }
* |9 V' R1 x% W}8 E3 d/ b# \' K7 N* Q' Q
使用动态模块加载的配置方法' h9 Q8 g; |+ x6 m: ~7 C0 H+ \
在需要启用Modsecurity的主机的location下面加入下面两行即可:
! u; y" s# C( K0 { c: q4 H5 e* `: d9 R7 e- [
$ C, \% R1 E. v6 S4 e" _
modsecurity on;
5 K8 a9 t& ?3 s! Q1 L' Fmodsecurity_rules_file modsec_includes.conf;
+ e$ f" S* {' I# y9 B9 S; m- e修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
. ^- h/ _# e$ m* d* x k( x8 Q. a, @! y; A0 J, R: d
3 r1 y* x( Q3 G
$ vim /usr/local/nginx/conf/nginx.conf0 d- U, K9 h: W' a# s
3 y& K8 V1 X4 y: ~6 A' W
# J8 Z- t* w$ N, R7 s/ Xserver {
; @: P8 Q. k& y6 B V listen 80;
5 x$ q, b3 U5 T9 E6 { server_name localhost mike.hi-linux.com;' N7 S/ A0 V$ _- y
access_log /var/log/nginx/yourdomain.log;, ?" p* K1 B4 L1 n; u2 B1 c
; M7 t+ r; `" l1 H" {( l; r
6 k/ s v- U! N
location / {+ `+ l# V* Y' M8 q# `1 e# B. V
1 Y% @5 ~ T- }- P
. E. y3 ]# X* P! u modsecurity on;
: P1 L% R( U) ~9 a1 U, v, {% f modsecurity_rules_file modsec_includes.conf;
) x3 J/ N5 N6 q root html;
/ f2 q5 D2 ?( V' } index index.html index.htm;
! R' w- r. }! l% t( U, W}
/ j2 T- H( c( @$ b0 J}# E. ^+ [: G) Y8 Q
验证Nginx配置文件
: s) K* }6 ~/ }7 M$ /usr/local/nginx/sbin/nginx -t
, Y1 q6 k4 v+ a: i. p$ snginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok) q. q" c( b b" K* y
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful' ]) u% Q- O" i" P4 l5 k
启动Nginx' Y5 H! y+ \# q8 n. w6 A. B. G2 m: O2 x
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
3 e9 w( l8 O2 J+ O% F8 @: ^3 P/ H, } K# u% N# l2 l# Q7 g
测试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能够战胜更多复杂/未知/混淆的攻击模式。 + l1 v7 O, f) L0 z
|