ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。% z! y O( a9 L+ M8 B" _
1 T" t9 M" E. E! g
9 H# L+ o) B: |5 p m- j在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。* N: |. D9 A" L( z2 Y: D
9 W1 S$ p% B, y k9 T! u
2 ^3 \# i# y9 O) d( [+ p( I! I! @什么是ModSecurity1 o% o0 g: |$ L t- t5 b
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
& t5 b3 G, [2 P2 m
) B! b4 O/ D: s# G; R) d( J# Z) A
2 U# @# P3 {/ VModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
{4 a/ J2 w0 X0 D: a8 A' n% C" Z' a- t; N$ |
" i& n4 V( M" t+ `: @3 XModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
$ @* { T; i. s# I. g) [' k8 Z4 c" h% @- }- R- U( G. J8 ], k3 K& g
/ Q7 a# P7 v( {官网: https://www.modsecurity.org/
' s3 l" r# b; ^) Q: o5 Y" T E' \% J" i6 @! h1 Y5 P
' Z0 z' H% n# a& `% z; H7 X8 r7 i
什么是OWASP CRS
1 v8 i, V g1 `) l! K4 _OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
M8 W; t: ^" t4 A L
: s' R" S( U& b8 P% B
# [( U9 R3 Q% P: _1 e$ a+ J% `. F: dModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
2 a2 A6 g" t/ [$ S% N Z
# a. z) Q( r9 _+ G a
$ [8 C: q* v+ L6 _; uHTTP Protection(HTTP防御)
5 U; u: D" y4 X) ^! cHTTP协议和本地定义使用的detectsviolations策略。' C K; l& z+ O" e) l
7 i0 @& u9 U3 M; Z! h; a% f) t& p( X# P
Real-time Blacklist Lookups(实时黑名单查询)7 B; E6 }# Z+ M
利用第三方IP名单。
2 h# A; P" z D) F) p6 I0 D7 x# M; {' a6 I0 \( B0 w9 l
3 J- K, I1 G: W7 h" RHTTP Denial of Service Protections(HTTP的拒绝服务保护)) w# ]' _7 c5 x8 w4 A
防御HTTP的洪水攻击和HTTP Dos攻击。
, ]3 o5 ^& X4 q8 n) K
" U D Z X, c, S: G) ]3 \0 a
/ p# o; D, h/ e9 z* t; UCommon Web Attacks Protection(常见的Web攻击防护)
* d/ P: l0 T5 f" E检测常见的Web应用程序的安全攻击。- w; q# Z% z# D! y. T; G- M
1 E0 G" T) }0 O! Z* F) J/ S( ?8 h- v- m. y3 f4 ^
Automation Detection(自动化检测)
V! |. ~% |8 h+ ^9 e检测机器人,爬虫,扫描仪和其他表面恶意活动。5 M, U: n$ m* Y$ J# Q7 a
. Q4 w u2 e' Q& b4 @, M) ^
; G H& _$ Z1 jIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)% M! h9 ~1 I8 v7 z% l
检测通过Web应用程序上传的恶意文件。
$ c, ?2 w% T3 k& c5 B/ z1 w- t% w* K" ^% h4 v" k" ? x
; q2 L6 k+ F/ M x& I3 W) m2 k) b
Tracking Sensitive Data(跟踪敏感数据)' }* z) y( Y0 R% j8 |
信用卡通道的使用,并阻止泄漏。5 a& w) ~ s7 h# O" Y3 m
; y2 l' r" a2 M4 c4 p
( U p x- ?& H! l5 r WTrojan Protection(木马防护)
# D" o& r% Q) G z; C% u9 u/ J$ d0 h检测访问木马。
. w) R3 [) t" m9 I2 M8 Z& @" \# N% H" F6 v* \; \4 M% X
) y0 q! b' r' I( {
Identification of Application Defects(应用程序缺陷的鉴定)% V! w8 V- H/ R8 U8 b+ [
检测应用程序的错误配置警报。
) x, h& U: ^/ {% Z3 u& H+ I8 G$ w& Q0 [' G8 h* S2 s
6 \0 C( x( g2 z+ G! X* h9 \& G
Error Detection and Hiding(错误检测和隐藏)
) U' v) O' Z* ^' I; I0 h检测伪装服务器发送错误消息。6 I M3 B0 C% h+ {2 r
$ n! }% b% R% L5 h& D
3 o+ J, G2 E; B( [& } l安装ModSecurity
3 e5 ]" U0 m) L4 C软件基础环境准备
; d0 Z! I! R# }8 Z3 F5 y8 a下载对应软件包
& S& B$ T9 n) O& _* o& }/ q5 t$ cd /root
5 E9 a2 A# ^( }, A" g5 b O$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
& N, k# v4 D. |$ s+ U$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
4 k5 I3 W/ \1 l4 A) o安装Nginx和ModSecurity依赖包
1 h% M4 R' K7 u1 {. N3 ^3 YCentos/RHEL
2 l* V# M/ I) @+ b" U. O4 p5 U0 v2 g, ~
0 D0 j2 w a6 T$ G
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel: Y5 \0 g- m4 `4 F# f" [1 w
Ubuntu/Debian
7 d& R0 B. T, M$ t( J/ Y5 {
, V6 U5 @; W) O1 V: ^1 s% v" s+ D q6 N. |; R6 n5 K
$ 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
' B$ n! X& i) R7 r D0 t; {编译安装ModSecurity7 y0 w9 w4 W1 n2 U( c) P) R( k
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
0 [' f, _8 t u6 N3 e' V/ F; [, J D3 o2 b" A5 g+ i
% i0 O/ q. s' {, H3 ^: k方法一:编译为Nginx静态模块0 j7 N$ ?- b; z6 Y K9 @% O7 c; H
1 J/ E1 Z! x* u) O' I! Q
3 M4 x# v, E/ w) J3 U1 x: K" o编译为独立模块(modsecurity-2.9.1)/ s# ]3 j' P t4 R9 j( @. |0 N3 Y3 A
$ tar xzvf modsecurity-2.9.1.tar.gz6 f$ G* S) p2 W- {, L7 S
$ cd modsecurity-2.9.1/- T0 P& l* q' F" O2 k) [
$ ./autogen.sh
( D; k& s+ i) [' V, a3 c$ ./configure --enable-standalone-module --disable-mlogc
& P4 P% p/ j8 M+ O$ make4 k8 I, r b, a, Z
编译安装Nginx并添加ModSecurity模块
- b1 s' O* E4 U6 t) F, m, t$ tar xzvf nginx-1.9.2.tar.gz6 b, ^4 h$ i( T4 p! E9 [# n4 d
$ cd nginx-1.9.23 Z! O7 ?! K% f F9 K7 V( W
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
9 d9 J' z0 F* L( g* W& y e$ make && make install
7 o6 \/ s8 z2 v( ^. `/ N方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
5 @8 R- T H: U5 w ]2 X% J- U3 Y9 b2 i
6 \, c& F8 i. S. x' g/ x7 Y
编译LibModSecurity(modsecurity-3.0)% c* W. ^! I* s
$ cd /root$ B- y* H# [# a5 L9 O, e% v
$ git clone https://github.com/SpiderLabs/ModSecurity
/ g) l4 ?7 v% o5 l: K8 {6 ]1 `, b) y$ cd ModSecurity
7 A% @4 e7 z! I/ i$ git checkout -b v3/master origin/v3/master
. m8 E0 i- q- V2 U) Z$ sh build.sh6 p1 d) J$ n5 J
$ git submodule init. l* x) t7 e# R2 ?( L9 V( d
$ git submodule update
0 D! P$ U/ k4 c4 ?5 f, N. [! |8 N6 ?" l$ ./configure
2 w/ t% n3 k# m+ u$ make
, c! ?$ n+ Q* u s4 N& m7 \, {$ make install% e- ]' I- ?9 X5 r' |6 Z- z, h" {
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。: l' N+ h5 ~) y1 X0 }2 ]/ ]; @
( \5 B8 [. {7 q D- s9 J
) W2 W% `! [) |0 c" O
$ ls /usr/local/modsecurity/lib
7 `! _% a0 l& blibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
% F, o8 l& X! ]1 Y& i; I) t0 X编译安装Nginx并添加ModSecurity-Nginx Connector模块% \* u0 {$ V2 j! G, U
使用ModSecurity-Nginx模块来连接LibModSecurity
" v7 d$ ~+ ^3 U6 O/ M+ {' s! o$ @7 J" s: W3 ]# S F/ S
( _! S: ]5 S4 A5 w$ cd /root
" A0 M) z6 v0 w$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx2 P7 n! ?1 E1 G/ g$ ?, x" S9 o
$ tar xzvf nginx-1.9.2.tar.gz
! l/ l r" Y& p8 k, ~$ cd nginx-1.9.2
5 N% `$ u# e, G! A$ ./configure --add-module=/root/modsecurity-nginx
- }, E; T0 M( \- e. H# E9 ?$ make9 N2 f* _$ `+ K8 H" K' N& k! K4 [
$ make && make install
/ H/ J) R* N6 t+ p& S+ b2 G添加OWASP规则0 A7 A6 K, C( {, a
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。* x# X3 O7 l+ T/ ]) g' c7 |- W
+ i! p& F9 H: S$ V
; j' P( y; ^( E7 o% D下载OWASP规则并生成配置文件- p+ L9 e7 O% X# l% ? S
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git Z$ J2 k' W e; t, P8 v) D
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/2 O2 W: a0 J/ I$ \2 c0 T8 C- W
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs1 R5 A4 e" M+ a ]2 m
$ cp crs-setup.conf.example crs-setup.conf
( X2 }; \, L4 _& A3 n% k' p配置OWASP规则# o4 p2 e! E+ z- E& j4 F
编辑crs-setup.conf文件
) b9 O$ i/ {& I( s$ m
7 E0 H, ^% i" S+ ]1 s. F) |, p, f+ L; F. T$ v" i. {# w) T3 S* M
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf. Y) u5 B" |5 ]$ B" I J4 x4 Y
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf% U7 L; l) h4 c. C1 { N+ I
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf& U5 n7 G- J! F) F O- L
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
6 @/ c( v5 ?- A z2 m默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
) ~5 I2 W0 s' H* H+ _& j; Y4 X' \+ ]- ]- v! q( ?; s- G
9 }+ M& `# F" }" z {, ^+ w S
启用ModSecurity模块和CRS规则
2 C' K/ w: K! b复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; z6 A% X2 m+ ~8 E. E) g% X
. W! ?& x/ }7 ^3 C+ e+ S
, L0 g: U7 }: r9 f& ~2 {
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
. M2 |# g1 w4 ? O4 |. S
- n1 b# z% \/ ]! ^$ u4 q# f- o9 Z; M& s* O" l' k9 {+ {
$ cd /root/modsecurity-2.9.1/
* n! G- m% E% P e$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf P0 M0 q4 Q$ Q* a% v4 T0 \9 R
$ cp unicode.mapping /usr/local/nginx/conf/* ?+ S. a9 w, L8 R
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
@/ V/ y+ L6 y" z) ^
& T, B* N5 r* P% k4 z# f. o7 h/ a$ l8 ^1 O7 o+ q
$ vim /usr/local/nginx/conf/modsecurity.conf
/ E* k; I% J$ dSecRuleEngine On
3 R( N% y7 R* H4 W+ D* k/ |5 @4 L4 _ModSecurity中几个常用配置说明:5 S( q9 t; R% {, V* i
# }7 d5 B* n/ i1 n# n1 ?! D; C) o S% x
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。1 m8 s2 v" X& K% q& _# S1 @
5 N) F2 x8 f7 [3 H
' w3 m5 O q' S6 e2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。. h# T) g" f& m T! W9 a' D+ r
: D8 ?# {% C4 ]' N' x" P3 r2 _- [9 l. X3 a0 N) P" e
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。9 \$ S: Q5 U, M2 a$ f
: v; q4 O! A4 u U1 r& s0 p
1 }+ _6 z& I' e1 P8 C, Y4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。0 @7 M$ w- t1 l* l2 R! h4 B8 f5 q5 A& h
; ?1 S# r* O0 r( v8 d( m3 p
, z: s* `3 }- n在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
8 X3 |$ l: R( q6 a/ O( q* d9 e; f7 |6 F
* f7 P1 a9 s0 n8 f3 l; ^3.x版本CRS
# K& s h' r) N5 H$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
6 i9 Z% ^. S3 ~+ O0 i9 z# 生成例外排除请求的配置文件
# E' d/ y. T( X4 I$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
0 k4 n6 e0 l2 X( e9 j$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf- ], j; e$ Z0 H
$ cp rules/*.data /usr/local/nginx/conf2 b, |: T1 e6 P' q& v6 d
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
) O, h! y4 A6 y, R0 F
' Z4 p' o$ T A f9 d
' x* t8 E9 I: M& r& W5 _$ i' ]: b: M$ vim /usr/local/nginx/conf/modsec_includes.conf
+ v; z& t% Q. E Q! E6 U5 H2 d4 \
3 N$ D: k1 e% f+ E3 s5 c[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 7 ^3 y& K7 S9 D& i O; S q
* f! s7 k- F# m1 `4 T
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。. [1 `- }3 W5 n7 B
5 a5 p/ b8 K6 `' m; V6 R
5 @* D, c9 b( n- H' Z k1 u( n
配置Nginx支持Modsecurity0 a; W q3 e) l: r$ ^' A3 Z" Y
启用Modsecurity
$ R* Q' @6 n# r9 y; G6 \( A使用静态模块加载的配置方法
4 H/ @) B: }; x+ ~/ z1 r. ?3 Z( r, r在需要启用Modsecurity的主机的location下面加入下面两行即可:
, J: l. M2 [/ x' ^5 ?
' r0 G0 j$ R5 e5 @, p) k7 N8 e! _6 E' Y+ k3 f+ _
ModSecurityEnabled on;5 v% {# H, z0 d! @+ K: J. b
ModSecurityConfig modsec_includes.conf;
1 ?8 [& {+ E: B+ ]9 X修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。7 y- K) T# C* [) b4 b
+ E/ s! C* g& x# h+ X2 _8 c
$ g7 E ]" ?% p& z! Z1 s6 ^% o$ vim /usr/local/nginx/conf/nginx.conf; i; x$ a! ^5 w. N% K' b
, a7 z$ U1 [4 {! f% I: Z
7 |7 p* F0 ?2 v' M! y$ e. y
server {$ ~% u6 X- q) M0 A' s, q
listen 80;
8 k' j! a0 B* `) d server_name example.com;
( l. G2 J$ E2 U- I7 w' s: d% z
4 F* ~- t F0 e! p$ V- x! m# P) r% G3 i% }
location / {* h0 F! ?2 U8 p: k2 Y
ModSecurityEnabled on;
7 g/ o6 A1 @; ?8 `- V. u$ p. @ ModSecurityConfig modsec_includes.conf;& R4 J7 q" \# T
root html;* ^/ E# }7 J* ~
index index.html index.htm;$ ]8 X7 u+ I' r, W4 H! d8 j) F8 d
}
( S1 r; ^6 \+ y# t" o: `; z}
0 q+ W+ s/ ?0 i0 d0 A使用动态模块加载的配置方法
+ ~% G8 W: ?9 N1 \在需要启用Modsecurity的主机的location下面加入下面两行即可:
' ^3 Y- E$ J+ Y" A! L
2 ?$ g, |' W; [1 f4 T& S; {3 C1 U( W; c+ G1 V- F8 C
modsecurity on;: V2 _5 v3 t* a3 O1 ?# i. _: f
modsecurity_rules_file modsec_includes.conf;
! b& C6 b% y" g# f' R- s- \修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。9 T3 t/ p% {3 w- r# T& R
0 W/ J% M+ d' {
5 _, Z, L- W! K8 S3 ]$ vim /usr/local/nginx/conf/nginx.conf
, f! r% g# C6 ?. O8 X( h2 `' t* S
7 R4 w' x& _5 T2 V6 z
! q; I3 n; y$ z- A, _server {
( s% X0 g8 Y1 q: l a listen 80;
8 b# j# M5 d! D- { server_name localhost mike.hi-linux.com;. R1 }" x( M, r2 ^* N
access_log /var/log/nginx/yourdomain.log;1 Z1 q% |& i3 w# |
: M" o9 t6 r5 J
2 o X+ ]0 @" f" {' S$ a3 U% n) N location / {1 W# G8 _8 y1 m/ |! U# v5 J, f
5 p; _$ a8 Y3 r( |0 }
6 ?- Y; W. A1 i- V! ?( Y9 V modsecurity on;5 b+ z; K4 a4 F5 h8 F+ S
modsecurity_rules_file modsec_includes.conf; `( n/ R& L. w5 W+ r$ W
root html;, j) ?0 U9 u1 Y* [' f; [' a# J1 p7 D
index index.html index.htm;
$ }6 T# G' k, ^9 }1 ^}
* M$ B! y4 N* n6 \- z, c9 L}5 ~9 z" H1 l; g! C) K; i
验证Nginx配置文件 H3 r5 X2 {7 t- K/ g `+ U# B: a6 Q
$ /usr/local/nginx/sbin/nginx -t
$ L0 Z# w4 i! G2 H! c8 gnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
: D0 X- E- i* F; |, K6 [. f" ?; Snginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful+ e+ X' D: f! X- _! S3 d0 o% a+ C
启动Nginx6 K0 a" W$ a7 }3 c5 m
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# t" n* J5 e6 D& v7 V4 z
% J6 C B1 f) q) B1 Q7 H测试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能够战胜更多复杂/未知/混淆的攻击模式。 + A. V" @/ x$ r3 [9 w! k' @
|