ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。- \5 Q! T& ?- k+ B' c, [
: I0 I$ D& x" Z' m9 ]6 O- P2 G/ q! b% ?" C
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。. e- K& E/ n* W; R* m
0 a1 X! j9 U' @! d* T( B. o5 Z. u
% _* v9 s, ^3 z! Z9 D6 r
什么是ModSecurity/ K5 Z( J) i/ z. f
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
3 m0 I* G4 |9 S
# Y6 |" v5 W' R" v1 u) A4 ?* Q. [! ?
5 T8 t% A8 `' J, s# ~ p: |- pModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
# X1 p* s9 w8 ~5 M1 i! f& W
! L$ T, k" J5 @% ~: I$ \: c4 r- }
2 t5 s5 r( ^; T# UModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
0 u- ~& h6 D! y' n6 T) N4 A Y) Y
* G7 t0 K9 R+ h+ t# x2 H$ e$ {8 U# b( E& }( `' m. l
官网: https://www.modsecurity.org/
# N/ ]4 _7 _# `0 ^6 l, Y3 D, B
5 S: }: B# T* |, Q4 u
. I1 s7 M& y" p& V+ v什么是OWASP CRS7 D6 l& I/ C' U7 r ]4 q
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。: b6 z* Y. F- w/ @, g# d0 }
! F& B- d1 G; n0 L8 h
, Q2 `: |* }! N
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
% F7 }3 N; z8 J* S1 p. z2 d* z
6 h# |; [9 i, r8 j! B9 v, e8 {9 X. O# l2 X, W' d
HTTP Protection(HTTP防御)1 n+ w; f- o/ `5 U
HTTP协议和本地定义使用的detectsviolations策略。& T% }& K; Z4 ?* \9 r/ s. Z4 T
9 Q8 G$ h& H1 \1 U' {& q$ o; Q' }+ W5 Q J% W$ Y% ^7 G1 e% d: }8 Y
Real-time Blacklist Lookups(实时黑名单查询)
) G9 ^( ?+ g8 o* _* I+ L利用第三方IP名单。
5 [% y F7 Y/ I4 V
3 y4 v. n G5 i0 r1 q3 D9 I3 `
[: H1 l0 P# e1 H4 H: fHTTP Denial of Service Protections(HTTP的拒绝服务保护)9 P% G2 p2 N/ Y/ t4 U' V% u
防御HTTP的洪水攻击和HTTP Dos攻击。
* M+ R& ~6 P, E, T; L7 q$ W1 H! [9 Z s0 w6 q6 m7 e/ q
0 {2 t ~ d. [, YCommon Web Attacks Protection(常见的Web攻击防护)0 |2 \: K8 ?) B. q+ e
检测常见的Web应用程序的安全攻击。+ R, L& Q/ W. G; x& s3 R& g( J
! p9 z3 y% A& I5 e2 ^+ T$ B5 G) ?( V
Automation Detection(自动化检测)
3 m3 ?2 u: Q2 K检测机器人,爬虫,扫描仪和其他表面恶意活动。# ^4 \/ T$ o) j, y7 i
8 G# V& @. @0 X, [) e: g9 y3 J w1 B, h$ s+ o% l3 x" [6 y
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
1 s# t! S' [2 q, f d: A# x' r1 e检测通过Web应用程序上传的恶意文件。& ?3 [" H9 Q4 n* i3 h2 M& T" a* J
+ I1 k+ t- r, n
1 U5 f0 z# R5 ^. sTracking Sensitive Data(跟踪敏感数据), v$ h' t$ I Y$ e
信用卡通道的使用,并阻止泄漏。4 Z9 a% i* O( }$ K
0 o, X' S' [- T8 f$ z5 E! M4 W) s7 m+ F
* t" Y/ t- P2 s$ E& s. w3 p/ g7 u1 tTrojan Protection(木马防护)
; U! f8 {. @& I9 E3 o+ @: r6 U检测访问木马。& d2 R; v, A$ M9 E# N
$ P% Q9 X9 c* R. f2 ]7 ]
- \' z/ x2 ^- T! n; u$ |% ]& r
Identification of Application Defects(应用程序缺陷的鉴定)2 Z3 D3 {, K3 d$ z& }
检测应用程序的错误配置警报。
' N/ u+ C. ]3 X4 d& d' n# j" W: j' o4 w3 i7 S& M; b: J
$ t8 ]4 W2 u' k' QError Detection and Hiding(错误检测和隐藏)
& B. P i$ f" ?! X5 \检测伪装服务器发送错误消息。
4 ?6 K& O# z6 C2 Q( C0 D. {) o
* p- e1 `. h( R7 a# a4 x
; r# k J& U) _, U/ E安装ModSecurity+ P4 A9 A7 S, { `1 T5 _
软件基础环境准备
% a, U% M& z! e4 J) ?6 d. Z下载对应软件包& T; n x& H* x/ l1 ^
$ cd /root
; h0 J, w ?, \! _4 y0 p( R7 C$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
2 r; ]. E3 ]1 \$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz5 Y2 Q1 p0 J+ p3 D
安装Nginx和ModSecurity依赖包
* n4 Q% v/ W4 t _ h* C% N. sCentos/RHEL" P1 T( X7 h/ @2 ]
8 C$ h% y) L( u! ~$ f5 l' a* @4 e0 E( W6 f K! j
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
0 l& F( @5 I7 ]5 n& b9 e2 p& dUbuntu/Debian
( x5 c& @2 u% n) d1 z! y4 J, U7 t$ \( m( K2 E6 m/ ]
3 o+ \: U/ Q' c- |" d% N' h/ b# t
$ 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
% k$ R5 N4 d) R, k# W+ R编译安装ModSecurity' F: S9 U, z Y9 L: e# \! p+ e0 i
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。1 a2 W+ J- d& k% y+ j
P- K% |3 b; b# J9 ?0 Z! ?3 s: C( V5 X8 n; ?
方法一:编译为Nginx静态模块" h' g4 V2 L1 |" k* m: h
, {5 ^% K$ H9 ?! G+ ^! o1 S# T. _7 q( C. L8 D3 n6 ~( C
编译为独立模块(modsecurity-2.9.1)1 z7 a) y3 l5 b! v
$ tar xzvf modsecurity-2.9.1.tar.gz
1 J) ^1 d1 i# X7 L: x. n0 ]$ cd modsecurity-2.9.1/
. B# ?4 Q9 f7 Q3 k5 R0 w$ ./autogen.sh
) H' L% E6 f2 m0 H( o3 M: P$ ./configure --enable-standalone-module --disable-mlogc2 `0 l# I1 G, J% M
$ make% Z7 E7 T5 H. }4 y
编译安装Nginx并添加ModSecurity模块
, K/ a0 S9 k, e3 L! z* ~. Y: Y$ tar xzvf nginx-1.9.2.tar.gz# S" \. c+ {# B' l3 `
$ cd nginx-1.9.29 I0 S" g/ c, b! R& N" U$ c1 N& B7 V
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/$ n! _" \! g) ? d
$ make && make install
; ?$ ]3 w/ f. c, s' g' p7 M1 X方法二:编译通过ModSecurity-Nginx Connector加载的动态模块. u4 D- s1 ?) y/ c" P
9 j$ m5 N/ g+ H( D! f) S h
4 ]$ ~6 {+ ^0 ~编译LibModSecurity(modsecurity-3.0)( P$ b* w @$ U. c
$ cd /root
9 A0 ?' f1 x) p9 F, k% k' Z, c8 r$ git clone https://github.com/SpiderLabs/ModSecurity) y! `, o3 G- T/ k) U9 v
$ cd ModSecurity
|9 M, g+ @$ F$ git checkout -b v3/master origin/v3/master7 h) I, h8 K/ e- Y: q2 ]
$ sh build.sh. v0 Y, X' G, J) D
$ git submodule init
: w' {" Q0 S8 }9 y" e+ b8 c$ git submodule update& G8 ^1 U! z% }1 {1 L) a1 n6 M% k
$ ./configure+ P& A* Y2 i8 @. A- c( }
$ make; ^, |/ X- E' l4 |/ U
$ make install! l# L, r( ^1 m1 C) r2 ]3 t
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
: B7 ]. p7 _+ h6 W3 M4 w/ s; D3 c6 K: D" ~
# ?9 g' k' @) U0 i. M$ ls /usr/local/modsecurity/lib6 ~3 a* I' ?* s, s5 ~" e
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0& D8 f+ K! ?+ z |
编译安装Nginx并添加ModSecurity-Nginx Connector模块
H2 Z! L3 [- B: i% T# V使用ModSecurity-Nginx模块来连接LibModSecurity
% L( j, L( E2 W- V, h$ H5 I8 Z! C9 y& f' q
9 T1 G, s1 x$ s0 Y$ cd /root
4 t# g% s, e( Q) H2 s- J- ]/ p$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
, N: H+ P0 Z; M: G5 T" p# G4 F$ tar xzvf nginx-1.9.2.tar.gz
, x9 M4 l) o }$ cd nginx-1.9.20 a6 M f3 C6 a' v
$ ./configure --add-module=/root/modsecurity-nginx7 Y6 d. Q" H, h/ }# F
$ make
$ q2 D+ F/ y$ o$ make && make install
* }) K1 ]: j7 n' e添加OWASP规则
* N$ {% }' Y. [7 QModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
* l9 ~; V0 }2 [9 I- y. ^2 O) [. N+ h `6 i
: Q! |8 M) e1 _3 D+ i: u5 X
下载OWASP规则并生成配置文件
6 [2 o5 _/ A% o7 ~& F, X2 d$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git! N) l" H9 o. F
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
$ W; q) J7 k1 W' n9 w) U' [$ cd /usr/local/nginx/conf/owasp-modsecurity-crs- d& z" Y' {- z( C# u V+ {$ T
$ cp crs-setup.conf.example crs-setup.conf
3 N$ \5 S" `' \. h7 p& [配置OWASP规则+ N: |$ c% W4 w
编辑crs-setup.conf文件1 ~2 y! e6 K9 u# V
0 z+ r/ R* V- O
$ h$ ]' ]7 ]% C! {: d$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf+ y' h# N/ l7 x$ ^: l1 B' H
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
' Q, U" U e( Y& y$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
5 E9 Q7 y2 P: X _* V$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf8 j% j7 S/ p ?1 w- l
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
0 q0 n3 Y! p/ W! o- }8 Q& ^; z+ }. D: E- |6 ^; `
& W/ m" ^8 l7 U+ ^; @# m$ B" {启用ModSecurity模块和CRS规则
' ~) U# ?$ q! v2 L g2 q' c复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
- Q0 c% g/ e5 M9 N- j% K9 U: s. @: I; j: A$ h* K
* g3 O% |# S3 r: r5 ]modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。& `& Q1 M4 X9 v! T/ S! ^. H0 x* D
1 [: e8 p3 N1 Z- Y
1 I3 ~. u7 {3 M r+ {/ L4 M$ cd /root/modsecurity-2.9.1/. T1 g' }- V1 _7 @$ P0 H4 I
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
/ i+ h1 b' f- g, `$ cp unicode.mapping /usr/local/nginx/conf/
9 y/ J/ E0 V* r e; J1 c2 @5 ~将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。2 m+ N4 i7 `$ ?: B) c5 ?
" W7 j' C6 |; _) `8 g
, a$ d1 \! ^! J$ N c/ G2 C
$ vim /usr/local/nginx/conf/modsecurity.conf
6 p, S+ M/ \" d0 H0 y: E/ ISecRuleEngine On* G; x# ^ n1 h5 f! r/ B! G
ModSecurity中几个常用配置说明:: x' f5 o/ E A) O' R8 V. m
, |6 k. C9 ]6 H) p
& k" r: V; Q$ G( I1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。: l0 B# `- D' @. A
2 o% M5 i# W0 H4 P, X G; s
- b( y9 t/ W s( Y$ D, z2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。: C; t1 `* g3 m8 h: k% x* }: D( Y
: E. I3 H: Y6 e1 _2 y
! a( [" T* @! |1 S
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。; S/ a9 U( L6 Z v- j
1 f' k, A/ L; e u
' \ S% q V2 f; i2 k" {
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。0 G+ ] I2 r! L' W) p7 s: u; Y& n/ s
+ _4 r& l9 c# n4 @. Z
& Y7 u. P. o& C3 X( j: `1 }在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
( A9 n( ?& N9 _! n* j2 H, d
7 ?* M" B+ ~6 O4 u$ r$ `! v: m3 ]9 ~. j$ m4 ?
3.x版本CRS& D1 {1 ]$ m9 r
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs% d4 v0 S" k& V9 T4 ?% y% [
# 生成例外排除请求的配置文件
2 W m" a5 |6 W1 O, O) @% M$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
; H: ]6 j4 e8 @4 O9 h. i$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf2 i3 r: d/ ^) A6 i3 E- d
$ cp rules/*.data /usr/local/nginx/conf
% s% B4 D: x* P8 P5 Q为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。" A" J6 D$ J' t4 g" q
( ^3 z& `" H/ h' t
0 G8 [' m; y. v* K2 m0 u: l/ L$ vim /usr/local/nginx/conf/modsec_includes.conf
% \6 G# ]+ q7 v+ p1 f; u# F
- D( s1 c1 m4 B9 D4 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 ! a! t) W% n+ G: I3 r# G' o2 A% p
5 L/ \9 T, D" _& r% u& t
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。" m! Z; z3 p$ B/ E- q
+ [5 U5 e m3 b8 n8 ]& |' Y' V4 c2 R( i1 C9 y
配置Nginx支持Modsecurity! O; A, I) { P+ v
启用Modsecurity
8 \5 F1 J' A9 w% N8 a2 K& G使用静态模块加载的配置方法1 C) Q! v3 s9 h0 S( j" @
在需要启用Modsecurity的主机的location下面加入下面两行即可:
6 q4 |& X9 g# ~+ p0 x5 _! _9 S) P& k6 D/ f4 a
" g8 l; K9 d- V" D4 |& I
ModSecurityEnabled on;
0 D. T6 v* [! eModSecurityConfig modsec_includes.conf;
8 Y8 Z N0 E/ p2 \- L修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
j5 k3 M8 B9 I2 \; O( v/ b. \ R- r! U
7 ^' I, W; @7 i" X3 y" k- y$ vim /usr/local/nginx/conf/nginx.conf1 U8 z9 [4 `, S6 X5 W9 L# q
4 y. }/ ~8 n) E7 `
- E4 Q- \' O5 F5 C' a
server {9 t/ z, H" Q' [" N: J
listen 80;2 ~! a4 M. F7 N2 y
server_name example.com;
6 H: I p1 K9 I# T# _8 q: D9 N5 Y+ { Q+ ?
; l; y# t! [* x' i8 _
location / {
& F1 L+ P8 p9 @ ModSecurityEnabled on;
; ~3 Y6 m5 J3 Y* ^0 r! g! l ModSecurityConfig modsec_includes.conf;
9 c+ }4 G! W9 Y/ ^/ U7 g3 x root html;4 x$ l( m' h5 x1 C, L n6 U
index index.html index.htm;' k& L! h6 T) { `8 r9 W$ o
}
4 U0 J% J4 q o E}
2 C, W& q1 j: r7 `" R使用动态模块加载的配置方法
2 O0 }, Q* W* N3 s4 k, M, { ?在需要启用Modsecurity的主机的location下面加入下面两行即可:& N5 {" m5 P- a
% G( h# k" x! a* B6 d# l
4 V+ u& H2 _- r$ w# K! G1 imodsecurity on;( j1 t: e% q8 j
modsecurity_rules_file modsec_includes.conf;
4 t# u) C4 n/ H2 P5 k: v& L! \修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。* E5 I+ o" t8 p/ _) [
( i6 }: Q3 p+ W
+ K6 ^6 I/ z0 c; G- K$ vim /usr/local/nginx/conf/nginx.conf
% C; ~% s0 P& M9 ~0 S8 N4 y- Z
- ]1 u; n3 l( D( g! v" r5 y4 n3 k
7 u% z) h, d( nserver {
$ }* N$ P9 j( \+ F. W listen 80;
- R4 T" h% w8 i) A server_name localhost mike.hi-linux.com;
O! P: a% \5 s1 D; B9 n access_log /var/log/nginx/yourdomain.log;1 M# Y- D; i% \* m* u
- ~7 H5 o/ T E" n* c# y3 J: A9 S* @1 w; B
" v, [2 G& s7 ~) M$ Y; }$ i4 \ location / {
8 M2 y- m- t* `8 K: d3 W# H
* h/ Q5 v7 X( M7 ] H. R1 N5 A: M) S* }6 q: |8 D6 M
modsecurity on;
# I0 ] f; C3 Z: }' p modsecurity_rules_file modsec_includes.conf;1 n% x+ t2 U2 F/ _: E+ `
root html;
3 }0 s! h" H& a( H9 D7 Z% r7 U8 d index index.html index.htm;
6 x6 `4 I$ d' t8 ?}% G$ Z" y, q- k
}$ I( V& j* x- _ j8 P/ A& n8 A
验证Nginx配置文件
D& G% O2 ^6 @3 x2 E$ /usr/local/nginx/sbin/nginx -t
4 z; a i# U7 w, C. d. Inginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
3 f6 h9 B" x F& q/ i: z( Mnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
2 r+ {( o2 z1 i7 C: Z6 ~启动Nginx
( E' C: a% b1 L D$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf6 r" W( m) n$ O: N1 J' y p, O9 J
: o; j; u6 H& 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能够战胜更多复杂/未知/混淆的攻击模式。
O: s$ c& n7 [& L6 J, }; V/ C$ T6 | |