ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
4 w- g# {3 c5 p
7 e) p' L. \ i# _3 M8 p2 G/ Y) g. f$ S. g4 H
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
0 l8 {* y, Q/ I; _/ w' H
$ ~* H. R# C1 @- p+ G3 C8 ?& s" Y9 f5 X2 b* L/ R1 ?
什么是ModSecurity
! x9 R3 R* M2 GModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。3 T" j3 R* b: e+ ]6 K
( A/ T" t5 o5 O" C
6 p! i& B, a" q3 HModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
3 g* H( y- }* Q7 i& A
, b/ j2 u A/ I8 Q& h7 u6 q
; o7 k. ~0 h/ D% |" y( tModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
) i- \; S; |7 [8 J7 ^; ?
0 r) x: U0 H( n3 i
' L* M0 q5 b6 E+ q5 S' j7 }, R官网: https://www.modsecurity.org/ u7 ?; L( e2 `4 m
' y, E$ c8 h# E; U3 z% T6 O; S# U% m- i+ j
什么是OWASP CRS8 A6 {( ]4 z; V& }
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。 f. S3 l6 P, V V4 w
" R6 G9 c/ a k
' o8 _7 O- S3 H4 w/ G" N5 FModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
* \# d; }5 I$ M! G( S3 [0 t+ Y
3 l1 q# s( d$ |
9 p! z+ T# |8 c1 W. t sHTTP Protection(HTTP防御)! `1 w& D& @6 Z7 r: p
HTTP协议和本地定义使用的detectsviolations策略。1 J1 Y4 y( T D1 s- ] |0 B2 Q+ B0 m" o
. z3 v% u9 c* A( ~; F* y3 x5 K1 n5 @; ]
Real-time Blacklist Lookups(实时黑名单查询)
\' u( d: w, A( V' |2 [利用第三方IP名单。; o+ }6 p# ^5 W9 I! i* Q6 d
7 h L' W+ I- y( ]
. d8 r" I3 p6 f; z% W
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
7 w$ \6 @$ \' _/ N( y. M防御HTTP的洪水攻击和HTTP Dos攻击。4 z) s( ]4 x3 z/ l1 W: N2 Y
! X0 g6 U( H% @# |+ Y3 p
5 N/ y1 x6 A" z8 j1 ?2 a0 c
Common Web Attacks Protection(常见的Web攻击防护)
4 j+ P0 F( l& y9 u" F3 {4 V检测常见的Web应用程序的安全攻击。
0 H7 ^" H/ h) K9 R0 j! _
: S( W* Q7 x6 v8 p
$ u2 x. |$ G, x+ _" { KAutomation Detection(自动化检测)9 x7 F0 X! B8 s
检测机器人,爬虫,扫描仪和其他表面恶意活动。, B4 c& B2 z3 d, B5 M/ C
' J( O7 R+ t# o+ R6 `
6 L3 R- ^( |) ^, H' U; @% H
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
9 \- T$ Y# Z7 {. q检测通过Web应用程序上传的恶意文件。6 E: p! B) g- \
7 m/ X* t! H! w! \
$ j8 W6 W/ Y" V# H% Y! VTracking Sensitive Data(跟踪敏感数据)
+ {9 c! `! A5 r9 H! p- h信用卡通道的使用,并阻止泄漏。
- ~3 w# |/ o8 D$ u
, _$ B1 [* G+ t4 \3 }, n* k. O3 s0 N% u) z+ M, n
Trojan Protection(木马防护): m2 k, K) E4 b! K# ?
检测访问木马。
/ W# b* e6 d, ~: a- ]2 k# h+ Z% W2 T; k- H% R
& O# t4 R/ b' u, HIdentification of Application Defects(应用程序缺陷的鉴定)
4 t( c- o. r) \5 _ ]检测应用程序的错误配置警报。' E" T1 {7 N' l G! _# X7 ?
+ t }% h& c5 h
- l3 P+ @! z1 L3 t# jError Detection and Hiding(错误检测和隐藏)
Q! U; M0 a+ {9 [/ u检测伪装服务器发送错误消息。3 [6 ]7 M) H3 m! d! O- q1 V
& n2 C0 @# G8 N. x$ L$ L A! F
8 R. S' w) Z/ J3 \2 J/ J) t7 p
安装ModSecurity$ `) `/ @( k% @
软件基础环境准备' {" g5 D1 B9 S
下载对应软件包2 t% u6 b l& k. l) K
$ cd /root
" v, Z- ^. _ V* D e$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'2 J. S) [, s) X1 h j
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz4 J; P4 U5 n) M8 w
安装Nginx和ModSecurity依赖包1 C0 Z( Y9 X! V) g5 O: z0 ~! h+ H
Centos/RHEL
- m* r3 t1 d* \6 T2 z1 B
0 t: r1 E3 L1 o) [# ?: N s) V$ n7 U# ?/ W# ^
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel# {% g9 e9 I# F) C3 p/ v
Ubuntu/Debian: B- Z5 I$ e% g. W2 q
7 a# q" x* i2 [: f, T$ J- V1 A* _5 Y, b3 ^
$ 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! F: v6 G9 h6 T/ b' a9 p. w
编译安装ModSecurity) `5 D& K2 F5 b$ k$ f
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。3 B3 @+ y2 i* g) M9 Y
& m, o( ^0 V: V+ _# T9 s; x1 x
4 D7 L! M5 j: [7 q. _方法一:编译为Nginx静态模块! N$ o% o, X" J7 y3 L
* n) N0 D! v9 j2 h9 F: D+ a) @6 C, l
. M$ B4 f- u, u5 E% K+ K
编译为独立模块(modsecurity-2.9.1)# q, W0 P1 [, j P. b) ^7 G
$ tar xzvf modsecurity-2.9.1.tar.gz$ k* P4 N Q) Z% k3 V: V
$ cd modsecurity-2.9.1/
3 n3 |) l% s0 T2 d$ ./autogen.sh
5 n# I/ ]- [! U2 u7 |8 `8 F# d$ ./configure --enable-standalone-module --disable-mlogc% U" ]0 _$ o- ?3 A& O
$ make9 r h. m$ X5 {3 z+ w
编译安装Nginx并添加ModSecurity模块2 _/ N: I$ b/ r- [
$ tar xzvf nginx-1.9.2.tar.gz. _ y) _' n5 P1 H& O. F5 D1 u4 J5 s$ y
$ cd nginx-1.9.2
- |- y# |# B4 j, N' Q+ S% F$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/- R, |+ b' ^6 v
$ make && make install$ N0 \ L/ f/ H6 c1 i5 M) r
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
! X( A9 ?8 P0 Z6 \" j2 `1 f( B9 w3 ?7 b1 U& B) H
3 F. P' t6 x% @7 k
编译LibModSecurity(modsecurity-3.0)
* V* y, a: K: X5 j$ cd /root3 \6 p& ~0 q5 I/ Z$ [; N
$ git clone https://github.com/SpiderLabs/ModSecurity1 T# l2 V. S/ \/ t7 K
$ cd ModSecurity4 C) Z5 t( d! c& |
$ git checkout -b v3/master origin/v3/master
+ u2 l( a5 X$ y0 [$ sh build.sh& ?( s1 r4 p% [
$ git submodule init
5 g3 h- X$ j9 L$ git submodule update; A% F7 s" E2 }* J! E3 j
$ ./configure
7 H) i6 J$ k6 h* {. j f$ make
; G( z4 w f c: {" X1 k$ v$ make install
7 I0 x6 d/ v# M2 K% I" ]* h* u2 _" vLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
1 V h9 w. M: o6 ~
/ a7 M3 K% R @" [. `2 H) e5 w
; p$ A; Q$ B6 G/ `5 v5 x$ ls /usr/local/modsecurity/lib
& |4 _% E2 p" e c5 ]5 c5 T2 llibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
. ~7 Q; E& T, K) D( V9 L编译安装Nginx并添加ModSecurity-Nginx Connector模块
. l. V! m j1 j2 n使用ModSecurity-Nginx模块来连接LibModSecurity
R; w) d9 G, ~
5 z6 z- F6 n. y, X b: X7 C4 J: l
1 |$ w" X+ W) u) f/ G* a! a$ cd /root5 x. O; E; v3 B0 H5 Q/ @& T
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx. g& D! M+ n. E. B n9 F
$ tar xzvf nginx-1.9.2.tar.gz+ R& o: {% F/ p1 {
$ cd nginx-1.9.2
2 j& g5 ]6 ^& r4 f1 t- @+ b6 B f2 ]$ ./configure --add-module=/root/modsecurity-nginx
" R( z+ v3 ^+ T" p% p% c; M W$ make
2 h5 q$ u! C3 g$ P K: g/ Q: O$ make && make install i! w- ^8 q4 j& A' r
添加OWASP规则
$ I* k; \+ W- h+ m. |ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。7 {, N5 C1 l) r* C. }% l- ?
! E$ y9 m% o( q& M* f2 p
; d8 J/ n, ~1 n' _+ ?" y9 V; v下载OWASP规则并生成配置文件& Y0 `0 f5 v5 e: K& ]. P
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
1 [% L; ^8 _0 b/ F8 g) g2 m2 t& g$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/9 L+ y- p9 }( o. B
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs3 r) G+ \' z- y o# F/ G' Z2 q4 u
$ cp crs-setup.conf.example crs-setup.conf
% q3 F3 {3 |$ v( Y. P* ^配置OWASP规则
; e. s/ E. R c; j编辑crs-setup.conf文件7 H2 {# I p% |, a, k5 a `
& q( [4 C% T) x3 \0 X) @
2 B& _% r$ E5 p6 v2 s9 C- H# h2 ?$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
4 X5 Z+ z2 K8 q! u; G8 X$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
; ~0 Y+ ~6 x1 K! z- S2 d1 B2 ]$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf0 E; \1 R% Q7 b" }" ?+ l
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
# J6 T& } `4 ]" H2 W' g0 \9 `# k6 H默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。* j) _) ~& l, F, z+ b
' W; Q5 ?# x; N6 g' l% |1 T6 i4 w
7 v1 U0 S! g3 w, B" T" F2 ?# j- E启用ModSecurity模块和CRS规则3 g- ~; V$ B9 y6 k
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
/ v, y1 F& W* A) h; j% I. G' q$ i
; w6 a/ v3 q' q4 S) P
3 X. v; X2 k: jmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。2 h* U! W, |" ]
9 @$ z5 r- ^! l o
) g I+ g7 s4 w9 E. ~5 u
$ cd /root/modsecurity-2.9.1/
0 q8 W0 q4 D) u$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf 0 B7 s9 k& E. O2 m7 }; d
$ cp unicode.mapping /usr/local/nginx/conf/
8 f7 T% y" s/ ^ a4 ?将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。7 p L1 p5 V& g+ X4 b2 ?
) h c* T2 g& v; O) j) @2 w7 l
: {# w8 L. I; Y: o3 Q; b$ vim /usr/local/nginx/conf/modsecurity.conf
2 e6 X2 X2 T* w1 U' ?SecRuleEngine On1 M: _% s6 S0 P1 N
ModSecurity中几个常用配置说明:
( S8 ?6 |, \! m6 ~: N' `
. b6 ]" ^) [2 \ g/ M
! R( j+ T. A! n1 u# x1 V) W0 ^1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。. q$ U; I- |* w! J) r2 y) _
. k( j& \# O2 h+ P7 X+ d( I+ i7 D, p$ H6 B5 s; G$ m n
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
0 l4 x6 u2 W& Q+ T4 ~1 x* [4 z4 \1 J8 l5 ~- G+ T
1 f' X& i4 x) L. w9 B+ q5 d# C3 u' @
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
" l0 W, U1 t+ X7 u$ s6 _6 x5 `' B* m1 h7 Q
3 Z9 g6 y- k- p6 o, }) ~6 S1 ]( j
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。 i7 r+ [* z- ~ |
1 c% T: ]+ N3 G9 S. t; S" `
) O. q( w# Z+ h5 P" l/ \' F在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。1 P }/ d1 O$ `+ P
1 _3 q# T) T" H$ Z6 ]1 @" t; M! k/ Z4 |9 k* o8 v" P' A5 k
3.x版本CRS( @& @7 @ O$ i/ q
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
; F7 O% C: Q/ S, s9 `& [- Y" L# 生成例外排除请求的配置文件
% u" J. [4 T1 O! ~4 m v! h' d$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf. H/ E# J3 ~' g6 q; |9 g) f
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
# D3 t6 i- T3 l4 u/ y/ L$ cp rules/*.data /usr/local/nginx/conf
! z' z6 v, V) t2 {+ V为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
, \9 b; B( e& z: M# \3 q
3 g0 R5 a& e4 L' @' B; F8 k% F) t' ?/ r6 T0 A. L
$ vim /usr/local/nginx/conf/modsec_includes.conf" n* _ v! o; G5 R+ I# \% D
( N- B/ U+ s; h+ V9 o+ W
[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 9 s" ~) k4 b+ k0 R: ?6 W$ s: H8 P: W
0 U& \- ~+ e, q- l1 r1 s
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
7 X* ~6 ~0 `0 S+ W {, C6 U" u4 J0 V6 ]3 `* i
; E, p" q2 M' q4 _# g配置Nginx支持Modsecurity
' ? h% {" z1 T3 A& N启用Modsecurity/ m( |( D* Q' S; x5 _' Q* y" t
使用静态模块加载的配置方法! L6 h' ~4 K! ^: @/ ]& [
在需要启用Modsecurity的主机的location下面加入下面两行即可:. R# u. G9 b# d5 R( v) r0 h* x: c+ y
+ V `! |" x% Z- ?7 J% Z- i
* N/ ?9 e3 x) V7 ?5 Y; |8 T
ModSecurityEnabled on;& X ^0 N+ X1 g$ c- V
ModSecurityConfig modsec_includes.conf;+ o# w/ T* z& S5 u5 _
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
' {0 i; z. B. w c' w2 {5 `( x; `# W) c
; O3 F7 B, g8 u& n* I6 q: u
$ vim /usr/local/nginx/conf/nginx.conf; |- g, y. C/ E6 C, V1 _9 ]* w
) {+ z" t: ]$ g( e+ T: H3 z2 x) h/ d! L, ]# [, f
server {+ X; ~' I/ ?5 Q0 o4 R1 Y& {! u
listen 80;, G% m5 ^1 H4 ]
server_name example.com;9 U' f+ H( o3 M4 Y
/ R7 M) l7 t4 _& ?" o
/ n: b+ A O+ p X+ b location / {, n7 V1 L6 J5 Y4 }. b0 ~
ModSecurityEnabled on;
6 W% {1 [: F i$ ]$ J. Z+ @7 q ModSecurityConfig modsec_includes.conf;
. o" u% g- Y3 D/ g9 L' q root html;7 C2 ]; |; `& X6 r9 \: N
index index.html index.htm;: g. I1 ~$ _& x' G9 Q- Y. m
}$ t p5 i# V. E0 M
}) r6 q/ n$ G( u9 u' M' Y
使用动态模块加载的配置方法* b: k7 @' h$ G; g
在需要启用Modsecurity的主机的location下面加入下面两行即可:! o# B3 K, N& f
1 S% h8 @% e1 _3 X9 n: F1 A9 ~+ f# ~" ~; @/ W/ q* {
modsecurity on;
' U) a3 t( }( |+ B* Y! C1 D* ~modsecurity_rules_file modsec_includes.conf;. h6 ]: X2 K* A$ {+ v: `
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
; @& V6 D" i+ W$ w+ ?4 N6 V N4 X0 I0 H8 f x: K! q9 K, S
& s8 X R+ b2 S3 o/ P5 Z* u" B
$ vim /usr/local/nginx/conf/nginx.conf
) G$ [0 l# v0 b! X$ L0 c9 y7 e3 B& n4 l. ~$ M; s- w1 ?
/ u% A! {! z$ M/ J2 ^! t* u3 qserver {
4 T' |, u* N/ I9 v$ Y: y+ G6 c' v listen 80;+ J+ k+ z6 `8 o: f
server_name localhost mike.hi-linux.com;
. i$ M- Z* ~9 _. L5 t6 I% N. E2 B access_log /var/log/nginx/yourdomain.log;
4 A# Q A- ^& J/ U& a0 _3 n4 U% R, j# t. L& r- a
# n6 \; ?% k1 e7 ]: x; x
location / {7 o. d1 ^$ ]1 K+ ?! _) J; g3 {
( P6 y& G ]6 y! V* U8 s# Q! U! r! z6 ` @ G
modsecurity on;
) f# i6 Y4 `; S5 b modsecurity_rules_file modsec_includes.conf;
& v. V; L9 T5 r6 P3 f% Z root html;+ F2 _6 t; p c4 @% l* T' o) G
index index.html index.htm;! x. u! c. l( |4 p" s# | t) F( `
}! e& z3 d; n2 T% r5 x
}
5 G# O5 C% g; o& a5 [" h验证Nginx配置文件/ e! k4 e' ^9 B9 C7 _# ?( i
$ /usr/local/nginx/sbin/nginx -t# V* v3 q" P# ?5 U' g% c
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
8 i; h P" d8 P1 A3 z, S- ]nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful+ A( y0 X1 T, B
启动Nginx
- C3 }0 E! {/ B4 ^; P8 T; e4 W$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
4 r5 f' \5 k/ _3 d& K4 G2 h' p2 N: _# \8 ]. b+ a a$ b! 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能够战胜更多复杂/未知/混淆的攻击模式。
5 x& C V" d' r* K5 J5 W8 [ G9 a7 b |