ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
/ C- [# s/ U2 v+ }9 X
; a, l6 E9 L+ C% \, k( e7 w+ S9 I" h v7 Q# K
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。$ m% k# L' h$ p' e2 x0 {
' C* D& w% a- J4 \( B; X+ e8 n
' g& ?( K1 F% m& o
什么是ModSecurity
7 p# Q3 M1 n6 l7 l- V4 _ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
$ m+ t/ w& s7 x; E, Q4 A6 @5 \. L) x$ r9 D$ L# k, r
L- _0 E$ @+ W& T) M9 sModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。# {% a t: A8 m4 l+ Y) L2 h! C4 q0 j
% [/ Q1 q$ Q8 r+ t [- p
, Y0 K6 g. K' b' ^, d0 P6 UModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
2 m- X# y4 ^/ z3 `6 |. g d0 t, l. D! {0 I `
( r, P& O! @# r# i2 i" d
官网: https://www.modsecurity.org/
" r; M9 B& B! ^( j( `" ]4 r# J: U1 G8 J% q1 j1 @
2 K( c' c& h% A7 H, J" [什么是OWASP CRS( B; e; E! ?2 m1 G) Y/ b6 `4 M) w
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。4 K9 i0 `9 s5 S% S/ P5 {
# A }. b$ ~, u' ~
# Y3 d+ |2 O3 X( EModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。6 v3 I; _, `: F& X: T
$ ~ s4 L3 t/ a0 e. B7 d" L, Z
* J' D, D9 w4 s! f+ O, FHTTP Protection(HTTP防御)& n* U2 D4 W' l. Y
HTTP协议和本地定义使用的detectsviolations策略。8 y5 Z$ W" A/ u# S
" Z- O, f' J8 m' `7 @; p% R! ]
4 |" S, N3 m$ N9 ]( p4 H: IReal-time Blacklist Lookups(实时黑名单查询)1 [6 w' N. r/ K! M4 F: V* L
利用第三方IP名单。3 [- P! C8 a3 v" e' F8 `* A
, X/ h/ ^8 G7 _4 C; y) A2 x
* t' F2 ~, q& C. }4 F# x5 p8 NHTTP Denial of Service Protections(HTTP的拒绝服务保护)/ J3 ~/ ~0 O* \4 ]; \
防御HTTP的洪水攻击和HTTP Dos攻击。 B2 N2 @" z5 ]' d
, e7 m0 S# R3 [4 p
8 a0 N! L7 _$ m' X, P% L6 L( x) ?# QCommon Web Attacks Protection(常见的Web攻击防护)
8 {9 [ C& V. v4 }' R, J检测常见的Web应用程序的安全攻击。6 Z8 N d- z* f2 h+ Z& P
% \- x% ^9 v" {8 q4 z# H
. G# }2 ^; z' z8 YAutomation Detection(自动化检测)
4 ], ~$ z$ _& o8 Q8 w8 [. a检测机器人,爬虫,扫描仪和其他表面恶意活动。
' b# K( S. h7 l; {4 l9 b6 R; M! U9 |' |3 A& l. l6 Y8 G: E' h. B+ q
; V! p% l% @: ~4 ?2 R$ X3 }
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
9 G- `9 e5 |0 _8 y& u* w: _7 p检测通过Web应用程序上传的恶意文件。
( v1 t1 E% H$ b4 p7 r/ O- J! H1 I* J3 ]
- x" A8 \" j/ n; s8 _$ l% h3 ^Tracking Sensitive Data(跟踪敏感数据)
) Q; a/ v! E" Z9 I信用卡通道的使用,并阻止泄漏。
% y! R8 T4 s D; l, q! C& R2 R- C0 l
, \3 s+ n3 F/ d% R6 M3 t kTrojan Protection(木马防护)
& f$ i" T" l1 m" ^6 R5 [- N检测访问木马。
( b- D. S! s2 Q3 k) `+ J
- B. w4 j; B0 _- E
; e0 m! O8 P9 x# o9 m1 g, UIdentification of Application Defects(应用程序缺陷的鉴定)
3 Q+ f0 n" H6 f5 `% W8 H! c检测应用程序的错误配置警报。- [3 A# F( V+ L' U
( m! `% V& V" G2 w9 n4 T3 m8 U6 F* { S4 Z" V2 c" w4 U
Error Detection and Hiding(错误检测和隐藏)- ~ j6 R$ M3 P# A T# ^+ k H
检测伪装服务器发送错误消息。
# K+ e& }5 W3 E
7 z: I" ~) x. r
. d0 H* z4 O8 F) `安装ModSecurity% z) Q) i: P+ l# j
软件基础环境准备2 h5 M+ p4 I& d; t8 t7 s5 s, A/ ]
下载对应软件包
" r; @. Z# k5 i2 j0 F- w( k$ cd /root( v4 f. j1 s3 @1 y
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
3 X! ?4 g1 }9 o M; C) i8 F# e4 ^+ J$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
6 S( n8 b% c$ E8 I安装Nginx和ModSecurity依赖包
; s$ w b8 t" n( g5 n" p" {Centos/RHEL
( M$ y$ X; M9 ^
5 b( l' M8 N) M
+ \1 }3 F/ x# o8 r! V$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
- N0 X+ Y8 R7 u0 HUbuntu/Debian
( c) q! Y1 Z. r* f. g1 w- {8 b
! t! j8 w8 t8 _4 S* X8 [
; {# [% V+ ]# v. D5 w' S$ 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% m. R3 L4 q" U
编译安装ModSecurity
& V& j, n, O; P4 rNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
3 K: I4 P* z2 D) \' g. S. f7 a8 V. R: o g/ M' E* r8 J+ A# I5 M2 D0 n
& I! ], |* |. s7 f
方法一:编译为Nginx静态模块6 O& H# ?$ |# ] @( V7 j
, y6 I% Y4 s+ ~7 n5 W
3 y7 K0 a, G8 ]3 m& V' c
编译为独立模块(modsecurity-2.9.1)
8 `7 f4 k! z0 j8 w2 d$ tar xzvf modsecurity-2.9.1.tar.gz& V. X% Q7 Q6 N: E3 i7 J* |
$ cd modsecurity-2.9.1/
; M, x& c2 t; }0 Z% n3 }$ ./autogen.sh ~! Y1 D9 l, g
$ ./configure --enable-standalone-module --disable-mlogc& ?3 f( e6 a5 [2 i, J- p2 F" s# p1 t
$ make: H# y; G& v7 W) z& ^
编译安装Nginx并添加ModSecurity模块( M# N$ L0 `; Q9 A3 ?
$ tar xzvf nginx-1.9.2.tar.gz/ A' l. K8 C q% }
$ cd nginx-1.9.2
, V! t6 ] E5 n% U$ G6 J2 T G$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/9 \( s) x* I4 O7 n; _
$ make && make install
# ~4 m% l+ u9 _. u1 \ q方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
; n* f W/ K' @; q6 V* ^) c% }7 H" e* K+ g2 R
% d0 l+ T9 L e1 F# P编译LibModSecurity(modsecurity-3.0)8 f# {7 S$ T4 x; l2 w' ~
$ cd /root
' F: L9 j: G9 h" @( ?4 ]$ git clone https://github.com/SpiderLabs/ModSecurity! j: k$ v3 Z" Z
$ cd ModSecurity+ x+ I- E1 ^* g0 b {# G6 o; E
$ git checkout -b v3/master origin/v3/master
0 p# G, T1 e: D0 V u# F$ sh build.sh
. ?/ \" Y; a2 A+ n( S0 g$ git submodule init0 _4 |$ `2 v3 |( j- O: p
$ git submodule update
5 L) c, R; w' F. {6 [ _7 E& M$ ./configure+ L3 o% r# j( W `3 `$ f4 p' x
$ make4 m; `, h1 s) \: O$ @/ d" a
$ make install
; C/ K- }% p; k& x! c% C! XLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。4 N( ?! A4 z9 v" X
/ B$ T; B+ _8 J6 T. P6 \) o7 o2 R) p! `2 |7 l0 ?
$ ls /usr/local/modsecurity/lib0 M$ g, t& N# P9 f( Z
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
3 H! X9 b' t( S2 w: `# d% O编译安装Nginx并添加ModSecurity-Nginx Connector模块
1 P! D8 |: \* Q, o8 e3 |& m3 t& W使用ModSecurity-Nginx模块来连接LibModSecurity
& e% R& u5 r; N" k- J8 K P3 K( u) w) G% y2 g s, |0 }
. p) X9 m8 k8 o3 C
$ cd /root
) U j, r+ F* E; N5 A# Z$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx% t3 q+ M: r$ j$ J: h4 p
$ tar xzvf nginx-1.9.2.tar.gz
* S/ I3 ^- F- o# M3 x7 f+ h( t+ a$ cd nginx-1.9.2
. i& `5 Y2 M2 s, U1 J5 [9 ^$ ./configure --add-module=/root/modsecurity-nginx* `. C4 W* \: R( ?6 J
$ make$ V: x+ r3 `4 `. V9 p) U4 B
$ make && make install
/ }! J- d' T9 q4 U/ |3 [2 g添加OWASP规则0 A; A2 ]3 z N$ o1 z% G X; c
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
8 B" `( q) d( s ]% v
+ u) p7 i. V" y8 |* e ]8 ^+ z8 F/ ?! q- B: q9 [: j& ^4 K9 x
下载OWASP规则并生成配置文件7 C `3 x% p6 `, I6 y m; |6 E
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git$ Q0 G7 O o, o' h. |6 X6 ~
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/+ F0 Z" `9 L7 s1 w* L
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
! S- Q" y( q* E3 _6 d6 A$ cp crs-setup.conf.example crs-setup.conf
- P/ @! k2 D) R6 Z6 C- C7 y2 ]配置OWASP规则/ O H" P0 x7 P- `5 f4 a
编辑crs-setup.conf文件( x' p+ B( c2 T# f9 |: D
( L1 Z/ R- i1 p/ Z4 U
5 n: f2 Z" c t$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf; y! ^2 s. A4 B. h
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
' F+ t3 w, B: C- `6 m9 I9 B+ }$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf+ B+ m) n1 m+ i' D7 x
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf( S) i7 Y! r4 a6 F4 S" T# f
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。# r' u( O/ J: d* T! ?1 `
# w7 ^, }, U+ H. m: |/ H; W
7 l* d$ v# I1 e7 }启用ModSecurity模块和CRS规则
' T, s$ C1 D8 O" X复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: k% p0 o, x* y- i9 ?' ^+ y7 p: `9 X2 q" ?! H
& R, q s l' q% M( \1 [ dmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
8 H6 R. [' u4 `: w5 T: ? y n4 Z" V6 r1 x. R
: a' K1 t0 s5 T$ cd /root/modsecurity-2.9.1/
# [5 Z, I9 Y9 E' @0 m" J$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
4 B0 X' J$ S0 Q2 t1 r$ cp unicode.mapping /usr/local/nginx/conf/
; f. c9 f- M1 K/ d! z将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。& T$ M; j! q0 r9 Y/ l! J- y5 p9 {
3 \# v" l5 Y) K6 D
' ?2 f! g* `( k$ Y8 V' X& g$ vim /usr/local/nginx/conf/modsecurity.conf
8 M6 ?) X) J/ `" y) W# jSecRuleEngine On
8 U6 `* ?# V" r) S2 Q( @+ rModSecurity中几个常用配置说明:4 |% i1 W$ n( C$ ^1 E& b
+ K6 [9 T# W# c2 Y; \1 B- [
) c; ^" S3 k9 b
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。# y; V p. ~# h0 b! g. A
, O D% ?, F. S) d& H6 B
# E6 M2 w f6 B. f2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
2 E8 A. ?, e: q3 |& P3 d* b( g. P, x3 O0 k4 t
' E7 }/ i+ V: f( h' M
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。* U7 F$ S+ m N; p3 J
) m3 F$ W& j- K2 G
9 ]. [6 t' _$ |. x9 [6 k& k4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。" {9 f3 ?4 r2 M3 Y* [7 S/ `
% @5 y& p) s; Y
4 k+ d( G a' O0 K1 v在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。+ i) a' K/ d! g- [
# `. F6 x8 X" Z( Q2 _
3 O; v; H* D+ H% ~* H3.x版本CRS0 y- ?2 H' L1 N+ x. S6 V3 ?/ V' v
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
8 q( j, ^6 R8 t' T% ~# 生成例外排除请求的配置文件
( S+ k i: ?8 a/ t9 q" ^: \7 u) }$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf) G. h! E: L' \
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
$ B; k- ~& ?- E1 G2 k, y/ k: Y$ cp rules/*.data /usr/local/nginx/conf
, x: f% p' }6 f. Y w. T; [为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。5 k& v/ N$ l& n! [3 D( W
1 n* d: h9 `! H! X1 t# D+ U$ C
0 u* q+ S) h& b1 T R$ vim /usr/local/nginx/conf/modsec_includes.conf$ } x: J/ _: P5 d ?. v
2 }9 a3 k/ y" K0 h7 E, V[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 8 A; _: m% o$ u; p( v, v, E; W
1 T: f3 t- X2 |注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
! I/ l5 G# O2 j3 {0 v
5 x" r( x. ]! j" U$ W% M5 ]/ o0 W- L# `
配置Nginx支持Modsecurity! R; x( t0 O2 Q/ w M
启用Modsecurity: X3 ]- P8 q0 J
使用静态模块加载的配置方法/ R% G; P. N* V
在需要启用Modsecurity的主机的location下面加入下面两行即可:1 d7 }8 j( @% S: ]
* K. z K3 b- c. c
: Y8 a+ P% H# C3 x! AModSecurityEnabled on;2 A6 f, z6 I4 I. }$ e( h# u$ _
ModSecurityConfig modsec_includes.conf;2 d. T+ H1 Q. j2 o/ Q
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。8 |: }9 [5 j: x3 F$ U. ~0 _
! P$ f1 s$ k' G9 O# G
( L, F# O( H7 Q' x% L$ vim /usr/local/nginx/conf/nginx.conf F. ~% u1 j8 ?2 q9 E$ q3 X6 M( a/ A
, d- [9 Z* D: ^/ T5 N0 M
^9 M4 J2 @, h5 n* U* Sserver {
3 D8 Q" m* ^$ D* K6 y5 R listen 80;
9 _0 ^0 E! V/ }+ [5 c' H) T4 g server_name example.com; i' M8 H( y7 B6 C0 [2 a
8 X/ {. d7 t- L. W- X5 k/ o, `4 T
3 g' y. F+ ^% y7 }/ q! ]8 Z H% f location / {/ q- `' ]1 b/ `% Y
ModSecurityEnabled on;
& m7 }7 N9 ~! {. S! _. O7 x ModSecurityConfig modsec_includes.conf;6 v, S3 ]" S, R
root html;3 b& G% f+ F& M; p0 {2 j# u
index index.html index.htm;: Y5 B( m8 E: u0 x% e
}
$ B, V+ h- p+ B& a ^3 d# }}
0 z( b1 n2 ^2 l, U& ` G$ f& \使用动态模块加载的配置方法& a. ~9 e: _# ] @, D
在需要启用Modsecurity的主机的location下面加入下面两行即可:9 L/ w5 T; D6 h+ F5 o& V
- `, k/ w: d' B9 t" Q
0 F c& H: [% }) O8 c! Rmodsecurity on;5 G& t5 t& v5 x2 ]2 f4 k
modsecurity_rules_file modsec_includes.conf;
% x( a* p4 w5 O# W, ^8 k修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
6 [/ m* q$ x: y. M
) U9 x7 y. S# j) k, c$ c% t4 P1 r7 t7 N8 w4 q$ |, [9 q, T
$ vim /usr/local/nginx/conf/nginx.conf6 U5 b# i0 C# B" i" {3 ~
1 v1 B6 |. a+ e& o# ?- z) F9 `" s
2 ]! U. G# E2 pserver {
# _" e, q3 M7 ^; e' ~2 c2 }1 K, m$ p listen 80;
4 k5 j4 r; Y6 c- A5 u0 h server_name localhost mike.hi-linux.com;8 n* `" f3 S+ r3 ?2 }
access_log /var/log/nginx/yourdomain.log;
5 O( `% [* d6 x: n* a7 Z# c1 A e2 I- H2 r
) p! \& c+ W0 ^1 E4 i
location / {' M% G4 E$ R0 P" H3 q! x! F
2 U, R! Z4 ^- ^0 q# z: i/ i
& F( G- R0 D0 m+ E- M. L modsecurity on;& M4 n! P! _- t8 q
modsecurity_rules_file modsec_includes.conf;
$ b/ o# Z8 v# c2 u7 ^ root html;8 J1 }$ f. W5 d5 J- F6 l& [0 [
index index.html index.htm;
5 C! M9 {! `. J: Z& g' e}, r9 F7 k0 R# q6 D% e9 Y% I: r. ?
}
! A) B1 B6 I$ @( C5 a8 k验证Nginx配置文件
+ r# h1 I$ R0 w, B, x$ /usr/local/nginx/sbin/nginx -t
5 E! n' V' n z% z, S: {& B. d: _. K: Enginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok) z1 V. v0 g4 u0 P8 l
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
+ C! x7 g! @8 C* @( X' J8 P! z5 P启动Nginx2 `9 x( A2 l9 N- b
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# v. \% v4 ^, W/ c) ^
* [! _$ G% c- f/ }% J3 [测试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能够战胜更多复杂/未知/混淆的攻击模式。
4 C; A/ M7 ?% r! | X |