ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
- o: E+ R! Q+ }
8 O3 ?0 G. x% x/ H- f6 O8 _- ?1 p! P. c t1 ]8 P/ l ~) h" b
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
6 Q) k4 s% W" K4 G- Y2 U9 P( f/ i. Q) S2 \5 K
$ q# z2 E; a9 n) a7 c' Q6 K1 L
什么是ModSecurity
: w9 k7 C) K+ M" ~8 C# bModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。; @7 _1 O) w7 v- S* L& m, D
; ]1 v& t/ D% A4 n" C; D" T8 I3 `3 e/ s _2 x* W" {
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。) g7 F4 g9 z6 D% d5 p
. ~) L( n8 \ n8 u
, p, g& f4 ~2 T, x' g
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
3 I: D s) D o" s& C/ L9 w2 V$ ~$ }3 m' I& ~/ [/ t
) p. b; t' H! g3 c6 L
官网: https://www.modsecurity.org/
$ K- y/ u G! A' Q: E9 G6 c6 ^; S
j& b( p3 b6 s1 {- \8 i ~什么是OWASP CRS1 y5 w* M* E/ _1 |. Z* d2 E k
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
; y0 M% K$ L# j) r/ W/ \' H ^: N' w+ O: N8 @3 p
& P$ F5 t$ E5 i# ?5 j i6 m0 \ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。0 Q( M! i5 e n# O8 T2 X h
) _5 l( u0 y+ F" K' j
% w; l) H. u7 }; p9 B
HTTP Protection(HTTP防御)) _5 p) C. K: b J0 y- u
HTTP协议和本地定义使用的detectsviolations策略。: z) \5 |$ j5 L0 h( c6 c2 W
& `% {0 P$ a+ ]& B) p5 K4 p
, x0 G, G, I4 V' f0 v ^5 nReal-time Blacklist Lookups(实时黑名单查询)
% T' G6 S. b& l- V5 I$ l0 I利用第三方IP名单。
( i. B4 R, W7 M8 X& u( V7 Q* E
! d1 j8 X+ T7 x: ^; v: }6 i
! j8 ]) U2 Q2 t0 E. eHTTP Denial of Service Protections(HTTP的拒绝服务保护)
$ a/ P& {; {* f$ v$ B- |$ n防御HTTP的洪水攻击和HTTP Dos攻击。4 o$ U0 r0 S' V; g- t7 Y* ~% p
! m* A" U2 [, P) g8 R$ M- g
3 M% I! q2 b+ vCommon Web Attacks Protection(常见的Web攻击防护)
& d* W H- ~9 z" @检测常见的Web应用程序的安全攻击。
0 R3 P% @3 O; R0 X% G8 R6 }
+ i" Z2 j; H1 m# s. W; B, l, K
6 M, L8 D( N$ ]9 BAutomation Detection(自动化检测)
0 o( }$ E! c7 S0 l9 T检测机器人,爬虫,扫描仪和其他表面恶意活动。
! W* G p Y2 F. s
8 i/ e" `! r0 B- ]& @
/ ?; s; @8 A' B! GIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
3 A# l% r. b5 G; V2 z* K检测通过Web应用程序上传的恶意文件。
! d' q: y9 W7 l) |$ @9 B1 L0 {# K* x
. V( C' h2 r9 P' K- ~Tracking Sensitive Data(跟踪敏感数据)
8 [% e2 ^6 p4 Z# I' [1 a7 u信用卡通道的使用,并阻止泄漏。
! K' E' G) q. R# k
- q1 G" c: E6 L7 h3 w) R1 _% G) ^; ~1 g; q8 f- y
Trojan Protection(木马防护). S! k; G% x. P
检测访问木马。
$ f. j) `& Z+ m, \/ V: O& R& m2 P! x7 i* P
% F7 s% `* p$ d. _/ X4 D) Q: CIdentification of Application Defects(应用程序缺陷的鉴定)1 z2 X% u' L, F0 }" S" J0 O2 y
检测应用程序的错误配置警报。
. S- V0 J0 x! [9 A2 _( S& |& U7 V' w. C0 D, e [' y
/ a' |; W7 M o4 X% wError Detection and Hiding(错误检测和隐藏)9 J4 @& y( D' W' ^( B( l% a7 I
检测伪装服务器发送错误消息。
* i# k7 T4 f- p) l( ~! F$ E! A# C$ [
7 W" P- |. P7 V% N" M
安装ModSecurity7 k) ~3 Y' ~: c4 ^& V, {
软件基础环境准备
+ T# s& [0 Q8 K7 I1 ^9 r& e下载对应软件包7 [4 M9 p6 l& T* ^. _7 F
$ cd /root
1 V- D: I9 X9 l" I7 F: E9 B( q: R$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz' U5 y0 w L t* l4 i( T" w
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
3 m1 r$ P7 b" ] D, I, i3 W安装Nginx和ModSecurity依赖包
+ D, ]) V/ X. E( v+ x, RCentos/RHEL
/ H0 T3 s6 X3 e7 ]: b
: U* \, l1 Z* p3 t I% [
" h) }2 ?6 i$ B7 t7 E$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
, Z1 [4 A' d3 T |/ ?% c7 D+ p9 xUbuntu/Debian" }% l2 g$ C: b( f( @% z
+ [4 q- S+ i: u. l- C$ g- d; n
" u7 R$ L! Q, ~8 U9 C, p' v' f
$ 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
& f8 ~3 Y7 ?& b$ }. [( |编译安装ModSecurity
, o8 _) |1 V, L! \Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。" n. k1 q* f6 ^
: u7 N9 B' ?: H: m' ]. r. ~: c5 l# A$ R7 f8 Y# u% n
方法一:编译为Nginx静态模块8 `$ I m: i' s- Q. O, r
: |" w* H$ U! v/ j& V, _4 ]
) F! a3 x! { i% Z4 V编译为独立模块(modsecurity-2.9.1)! t+ _: q5 m# x& g) j* r+ i# R
$ tar xzvf modsecurity-2.9.1.tar.gz( D4 O' `* ^; S5 E+ D, |! q
$ cd modsecurity-2.9.1/
) R. @* K; Y" e! f1 ?0 Y$ Y3 f$ ./autogen.sh& R3 v( ~0 I7 L! g- I
$ ./configure --enable-standalone-module --disable-mlogc! C1 c9 K: |4 K8 C1 ~4 I: X$ k
$ make& u: v6 O# g" L# @% f+ d
编译安装Nginx并添加ModSecurity模块
9 }! K4 n, _ M3 l$ u. _8 t; b$ tar xzvf nginx-1.9.2.tar.gz
- e2 }$ X: q( a* p: x+ x$ cd nginx-1.9.2
2 ]8 X. c0 j: P- Q1 Z; v$ j$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/) R* e1 d4 n) {4 O4 D E. r
$ make && make install- [# T% P' w. e- l
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
3 j! o4 }6 y4 s2 C
; v4 D% ?- U) o" T4 [9 A
) m# }( Y+ [* b9 d2 w' Y编译LibModSecurity(modsecurity-3.0)) l! Q$ \# M7 ^' S; m" j+ W6 _
$ cd /root
7 I, y( |5 X4 X7 W1 U$ git clone https://github.com/SpiderLabs/ModSecurity
5 p' e5 c. M/ ~/ o* U$ cd ModSecurity- s; y4 c# u" U/ p& |. j5 D
$ git checkout -b v3/master origin/v3/master
4 A: _$ f0 w" K' U$ sh build.sh# D; A% T$ ^' ~/ b) v: t! p
$ git submodule init
3 Z8 r: F! l+ J+ R% U9 T; Z$ git submodule update, O8 c6 Y" S9 i n5 D/ `
$ ./configure0 E, \) m& R2 J# ~* ^
$ make
) T* L N# q7 F& b4 }$ make install
) s. N2 N5 @" l4 {8 C7 iLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
% z/ m, L* K3 s' d. ]7 Q f+ E# V1 T; H2 H
- F! A9 D: l8 w7 ?( k" m
$ ls /usr/local/modsecurity/lib
0 w6 D5 u3 T$ T$ Wlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0" ^# n; D2 r$ `
编译安装Nginx并添加ModSecurity-Nginx Connector模块* x, w2 u& |% Z1 ~& |
使用ModSecurity-Nginx模块来连接LibModSecurity9 i, u) ~9 v8 b1 m
" n- C% }% {# t" F! i9 ^. t
% B' T/ _" l5 ]7 |& ^$ cd /root
6 h& s5 w6 M5 n9 O9 k$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
6 B m% M( E% R6 Z$ tar xzvf nginx-1.9.2.tar.gz
/ n; d' l z0 Y. w: u0 _$ cd nginx-1.9.2. A4 c& `) C4 f* l% A
$ ./configure --add-module=/root/modsecurity-nginx
" \$ ], ~4 H- o# ~6 W9 F3 B$ make( t+ y6 @& Y) Z: j" S
$ make && make install$ w3 o* w( ]" {
添加OWASP规则6 r! s+ ^! ], r: g+ n9 ~
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。% ^) S: r7 c& y) J8 G+ b
+ |- T$ Q" G1 _3 a8 ~8 L+ ~% p; T
; l) L3 p& X' a0 w% X) B9 z8 {下载OWASP规则并生成配置文件* \9 ?/ N+ Q2 k+ s- J9 X% k
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
4 v6 o, s8 n2 {! q6 d: @$ C$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
R* J2 @- K$ ?! s& m d. h$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
. m% Z, n- m0 S" t$ cp crs-setup.conf.example crs-setup.conf
3 M& l3 y- p. w配置OWASP规则
( j9 v# {# C4 d0 U编辑crs-setup.conf文件
- \- Z0 J$ D* l q
2 I9 w8 n6 M- M3 v: a( h" l) s' r. W0 M
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
; w$ Z$ ?' u) I) F- c8 `! `, M$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
6 S- _4 Y9 U( }! }) z$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf/ o$ _1 R# I3 U0 B* c4 E
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
# t `* h8 ]4 Q默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
* [) R" B: w! S
, G9 S( E/ O3 a3 P2 C2 g0 H8 L
. e* `+ I% T' S( K启用ModSecurity模块和CRS规则
2 ^6 c! T. m2 S% @7 t6 [复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; u1 Q% Z: D) ~$ q2 C# T, B
# ?" P7 o) h# ^' m7 `2 C' Q+ r% z4 `
/ ~& l3 [3 W( M8 e# S! z
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
/ f# Z' e& r. V3 t) U% G1 j `; a8 D) `1 w
* E. j0 {: a; T7 V) @5 q7 J) D5 F4 T
$ cd /root/modsecurity-2.9.1/
: Q4 {* i# k: A' h4 J1 G; y$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf & U8 \( {- K8 b8 ]
$ cp unicode.mapping /usr/local/nginx/conf/
% X0 s* o& A1 C% H. f0 K7 W+ m% ]将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。 f. t4 L2 L3 n; X/ {& e
/ u* e" X7 N1 [! W" H
t1 P$ _# X- J9 i& V% V$ vim /usr/local/nginx/conf/modsecurity.conf$ d5 v4 E9 m) v. ^/ t' H6 p# `
SecRuleEngine On8 \8 C4 z1 f! w' ^0 _! Y
ModSecurity中几个常用配置说明:
1 J' Q+ R0 T! O3 l; b: E5 t
: O @0 O$ v: {5 [# ]1 F9 f5 f7 q, k4 C) ~
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。4 ?0 i" I. P1 _% S! T; P
w# S0 I- Z0 y
# B6 v( X) z( H) D
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。7 u a3 q; W( h2 J u% \& e; ]- Y( x
9 u2 i2 O, ?- ]" I" n
9 Z' p L+ Z# g6 J6 C3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
; P6 `$ [3 q( N! G3 E1 p& [( G9 Y9 L+ a% f$ u" J5 }" p0 C6 i
. l/ G( ^# M1 P9 ^" i9 t0 w2 q4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。; E% _: g. O M
/ L% M" ?% U% {
3 O+ h$ }7 L1 T+ s) [在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
( P& a7 m' {6 b, g
$ ?/ s( {0 W0 _0 Z! n3 d2 I; L4 t0 O9 U# Y
3.x版本CRS
( U) e t, E6 I8 L6 _. } {; c$ cd /usr/local/nginx/conf/owasp-modsecurity-crs! i1 t+ _+ |7 _7 N. V. ^
# 生成例外排除请求的配置文件. Q' I% J7 x, a) J( G4 v, m! e' x# s
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf. O0 F* R; @9 z3 F- S: s
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf* w% H: ^$ M8 z2 Q* v% J
$ cp rules/*.data /usr/local/nginx/conf
2 q) B3 X3 t" `, D5 X& }为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
9 E6 d* a. R6 y7 h1 v& O: ]4 [8 j/ L
6 U3 B. ~$ S2 U; N' s$ vim /usr/local/nginx/conf/modsec_includes.conf1 L& Z6 I( i* K# z: y
4 E8 W2 L E+ j- h0 H7 s
[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
, v2 g: Z- }* I: ]- v5 H
, O' X: H' j+ Q" K2 C0 q注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。# Z/ k* Y1 M, B* H/ {0 \3 l
~/ N! @ h; \2 A
" r' ?6 }/ @ e# |5 X配置Nginx支持Modsecurity `5 d% g2 }- J/ y
启用Modsecurity
- g. ~; E: ]4 b$ y0 J8 e使用静态模块加载的配置方法9 L+ z: |1 e v' q/ u5 b" f" }
在需要启用Modsecurity的主机的location下面加入下面两行即可:
0 l( w" V+ }6 r9 L/ j2 m* Y0 A# q; R6 f2 R
3 G$ U' m& R3 B& n+ h, a/ n% A( FModSecurityEnabled on;5 \% j; h U& Y8 m% g# h$ e
ModSecurityConfig modsec_includes.conf;* B; _; N* }# G7 p
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
! n5 j5 b9 h" S5 N7 D% Y$ E( w$ P/ S( o3 T: y& o! w
; ^* }/ y) G: t$ vim /usr/local/nginx/conf/nginx.conf
; E+ e, n+ g( v% O4 {( |6 y! J! Z/ {6 e) J- V" o! \
, C0 ]/ v- L3 W" W4 b9 Bserver {
' S" T6 v) H6 P5 V, H4 X- X listen 80;$ l2 q( F" p* [, _7 N
server_name example.com;
' v' D b. G. d/ z: M" l! y/ W5 Q) k7 p
4 q# b) W5 Q& e$ F: B1 T. D h
location / {
5 q i' e9 a! Q: a7 G# [3 i ModSecurityEnabled on;
- r& ?. m" Y* w7 V. b ModSecurityConfig modsec_includes.conf;- d. Y% r# l& x$ Y/ ?/ t% I
root html;$ t: n" @/ S# n$ c! b. V0 D
index index.html index.htm;6 U X9 _9 y6 h1 F0 u( z0 e' ?
}& n' `. V/ k$ \& X! M0 }! z0 _
}7 o( Q5 v% |: \* n4 \- c0 S
使用动态模块加载的配置方法
. \& h4 n1 r2 E- K0 N( X2 F在需要启用Modsecurity的主机的location下面加入下面两行即可:
3 y$ n X& j; e6 k8 q2 \. O, F# {1 {; x5 X" I+ j' c4 f* Z! y
$ }. z- s& P# E, j' b3 T8 |
modsecurity on;; ^; ]. @1 r3 t
modsecurity_rules_file modsec_includes.conf;
* T# M9 o0 k/ G1 E0 r1 `修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。2 a7 `% y* Y* `7 v8 e C! G) q) ?& i4 Z
/ A; y9 y( w v# |" P
$ k$ e. b% K/ q" T
$ vim /usr/local/nginx/conf/nginx.conf
0 u' h2 ?: i- ^3 E1 R! g, E) D7 m8 a. `; l+ {5 K
8 F9 |6 x3 [* X* Y7 p& d9 F
server {
+ f8 N0 H- I- q( l+ m* M: b) | listen 80;( G& |( x( k4 b% _
server_name localhost mike.hi-linux.com;# X* b5 e0 a U) g9 s( M& U
access_log /var/log/nginx/yourdomain.log;
6 ^( w+ a& d3 \& ]1 |$ e; [9 D- e' ]& B2 r* a- G2 s$ B. d. b A1 Y
7 l+ C2 j2 S/ e3 \2 Q location / {8 Q. a1 V8 ~- `& x' S" l' J
3 O% w) P% {7 [- x/ F
* N H8 ]! I T) q, [4 n/ ?( P" A
modsecurity on;1 A. L" H" Y0 h% Y
modsecurity_rules_file modsec_includes.conf;
; J2 c; n# N% c* n2 _" \- y r root html;
6 S) S; }5 y4 { index index.html index.htm;2 C2 w9 |: r! v2 g
}
+ ^( j/ v1 B# u) O}8 ^' L* k& Z+ p, H% a
验证Nginx配置文件
( ]) G, K' g U$ /usr/local/nginx/sbin/nginx -t
! f; F3 D& Z" T9 ?- ynginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok5 e$ L+ V& h; C; ]6 V
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
; L4 }1 ^( R8 C& E启动Nginx, a/ d, W1 l; Q. S4 S! p
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
9 D5 T( y) n% A. P
7 O" ^" y0 s9 O5 L# f2 R测试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能够战胜更多复杂/未知/混淆的攻击模式。 3 X1 ~4 q( T5 L! q
|