ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。9 [' S. T! w: q+ Y/ p: |
$ t e! ]# ~! P4 b% |; M
. x3 A( f1 y7 P) q5 H. p
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
) T8 Q6 K; x- h* j. K; S! {; U* [* ]3 ]5 |. X" i1 V
3 I5 B7 x0 z6 ~/ a( c5 a0 A: V$ ]: _
什么是ModSecurity; x1 R( g/ s7 T: K2 l' w' A
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。! N h! D. g1 ^' p
1 p3 B$ z7 L/ _" S& e$ v' p+ A* k5 F/ J
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。& \8 y' U$ ^: m/ A
7 p2 A5 _9 L. f4 }0 P& N
4 O% w9 q+ C- g3 KModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。4 _$ I1 J/ `2 ~/ X/ p
; D9 l! ?! G' p/ ^4 R- M- R3 r# o; y9 r9 i% G& A z7 p6 A
官网: https://www.modsecurity.org/
" }; p0 s/ f! P7 v y* v7 j; k
* }9 a1 W, n3 P) |* m5 h# t1 ^7 h" s' v+ x/ j9 T
什么是OWASP CRS
! t* a7 y5 o- Z/ t; \0 E9 wOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。# W0 ^9 Z) d" G& Z
" c3 H. n* Y& }
1 R2 L6 J3 u5 |$ Y3 bModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。; T; e$ ]: U7 i# H$ X; _
- A7 n4 E% J1 d7 ?& h6 i* w G. t6 X; V: I9 s( _
HTTP Protection(HTTP防御). s% y' W. ^5 h+ ^# _% g5 d1 j7 Y
HTTP协议和本地定义使用的detectsviolations策略。2 a1 p5 d( P( p/ p& W) ^ D
( q7 ]# b) b( D) j
0 J% J$ i7 } d: w/ y
Real-time Blacklist Lookups(实时黑名单查询)& p0 b- \' }6 L- N6 V
利用第三方IP名单。8 b- B9 ?* e* `2 j; @3 v3 u
# z+ e2 D6 `- u* z4 r9 }7 t
$ @+ u% i5 f4 F1 N
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
( G3 {4 o$ ?/ Q) q6 q" L& j防御HTTP的洪水攻击和HTTP Dos攻击。# h1 U' W8 b% R" m
" V% y* ]6 C( A6 j1 M
% D, o6 e6 s% M& K& V* v: j
Common Web Attacks Protection(常见的Web攻击防护)
& E+ e' r+ p! O2 E9 ` ^检测常见的Web应用程序的安全攻击。- F7 X" o; f; \" e2 Q; b4 f
, a' q# C1 \3 v- i1 J
% W% d0 t2 P) ?& y g! Z0 K9 h' X
Automation Detection(自动化检测)! g/ y" `2 T4 k
检测机器人,爬虫,扫描仪和其他表面恶意活动。0 ]9 T' u0 U- d% m0 k
- g/ |4 h. e$ A7 E1 y
/ [& g. J% V1 z' ?4 m
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)2 ]5 H9 Z9 r# X( L
检测通过Web应用程序上传的恶意文件。* Q* b$ |6 I4 [: s' D7 T+ S
; H8 e) D4 h9 S2 g! U
7 |8 e2 y+ [. g5 D1 a, N* sTracking Sensitive Data(跟踪敏感数据)
2 m1 @5 H/ w" u" _' s: R信用卡通道的使用,并阻止泄漏。2 S! i2 ^8 v+ o1 H# z. f
; @$ x6 U- x1 \% A! S+ P; j5 y3 A) Y" q4 [
Trojan Protection(木马防护)7 m# @. g. Z3 I% D
检测访问木马。
' @! q: q% B7 Z: C/ j( B$ o1 D( R0 k/ {+ P* H
7 _0 Q. A8 b; hIdentification of Application Defects(应用程序缺陷的鉴定)4 ^4 m7 b2 r0 s0 `; |
检测应用程序的错误配置警报。
/ e5 M- e0 V2 l8 f7 P7 h! e- J3 i! ^; \
/ Z m/ c9 t# k, Y+ z( _6 sError Detection and Hiding(错误检测和隐藏)
) h) ^6 T( W; H- J U6 k检测伪装服务器发送错误消息。
) E3 ]/ v7 N3 o8 b& a M% U2 V1 g0 t) K8 x
: z8 d% I- [5 l- I0 Z ^7 U2 A0 c* ]
安装ModSecurity
( f: p1 L; g0 d }' Y" u2 ?软件基础环境准备
% u1 }+ }% I& z# m# x5 R下载对应软件包
, r! f8 O4 ?3 \2 ?! K$ cd /root
; e" k: H0 g1 B4 f+ ^# [; g$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
) p" h: b: z& ~0 e$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
1 Y4 h ~; u5 [安装Nginx和ModSecurity依赖包5 [( f3 d* ]; N4 N' r! u$ W: G
Centos/RHEL$ E; T }+ ^3 F2 N" \0 M
5 X! q$ R3 u7 o7 X- q2 O$ c. e: H2 _& f- }3 h X. D
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel, X: [ o8 n4 l
Ubuntu/Debian* ^$ f7 f3 x8 j
$ `7 Y. o/ V* u0 ]- E2 }! d6 K
# P3 I; k Z# \2 w' k5 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++-dev0 N" Q" O9 M" W i
编译安装ModSecurity
* Y! U# E1 R* s& K, L9 R: }Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。4 O' a+ i/ P A" B# @) M
" D& @ g+ a6 t2 U3 v$ A
0 d: h8 a# C3 {方法一:编译为Nginx静态模块& F8 L. E; M3 W& S& g o2 u; K
' E4 G& z: G" Z9 x! W8 Q; d2 u: W' ^
. d f7 g1 J; K U( x1 e. S$ N编译为独立模块(modsecurity-2.9.1)
8 H8 S5 X' q/ G$ K1 @2 v2 J+ Y$ tar xzvf modsecurity-2.9.1.tar.gz" }) a Z H3 u Q* u! K9 v6 i) C2 {
$ cd modsecurity-2.9.1/# C0 w' ]' w- { x- C% N8 K
$ ./autogen.sh9 S- t" Y% X1 G- }
$ ./configure --enable-standalone-module --disable-mlogc
' s2 l9 k5 S* q2 |+ B- y$ make
6 T! Y+ F) j( W$ c9 l8 w编译安装Nginx并添加ModSecurity模块
% ?$ N* P# {$ h+ ^, M. ~$ tar xzvf nginx-1.9.2.tar.gz
/ H7 I: O# h: A0 ^, M; x4 U B! F$ cd nginx-1.9.2
* ^! q G: ^9 ?0 V4 r: g6 t$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
1 _3 ~. C) s1 T# M$ make && make install
0 C5 H$ E6 N4 ~ ]9 X L6 |) G方法二:编译通过ModSecurity-Nginx Connector加载的动态模块6 S$ ?% L4 Z* h1 ^ N
$ v: L7 [4 N+ n) @5 E! I4 m. V
" Z: S* i- T; |! F; ^! Q: K
编译LibModSecurity(modsecurity-3.0)
O% x& z* l P. J2 N4 g$ cd /root
5 l; E1 o/ A9 @$ o' [ }$ git clone https://github.com/SpiderLabs/ModSecurity3 ~& t3 @( F+ G- d* r; P3 A' d" n
$ cd ModSecurity. z, f0 e1 K+ A6 }5 G
$ git checkout -b v3/master origin/v3/master
4 l0 ]# \, H% Y) J( f$ sh build.sh
) N2 I) y( y1 Y& N6 X# P$ git submodule init
. s: s( N& T$ {9 l% d; f$ git submodule update
' p0 e! Q L/ H* ^$ ./configure( w. I0 J0 O# b3 k- P
$ make
; |9 h* K7 |6 t% m$ make install
) K0 h% f$ f1 Q5 s* P' g% \LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。1 {- R+ t4 f" \
; J9 k+ }' P, e
) y! F' y# G6 Z% r% [5 ]
$ ls /usr/local/modsecurity/lib
. b' t/ q7 C/ W A% R7 Tlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
; u% d; G( Y; x编译安装Nginx并添加ModSecurity-Nginx Connector模块! \2 _( b/ A: @' s( }
使用ModSecurity-Nginx模块来连接LibModSecurity7 |, b1 j. k, g5 X. A
Q0 i+ o* v4 d- }3 ?8 c% s- X
% ~: v9 f% u& F* b( [$ cd /root4 p: q+ O a; H+ a9 G
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
# Q% a; ^8 N+ g. x$ tar xzvf nginx-1.9.2.tar.gz% ^8 I6 I9 q! d% F9 [
$ cd nginx-1.9.25 r2 y5 Y7 D, O$ n! e B
$ ./configure --add-module=/root/modsecurity-nginx9 o! t2 s- x: Q# N# [9 }
$ make
/ L9 I9 O9 e( d% ^# v, J7 w ~$ make && make install/ P6 c; T* j9 Q1 M' ]/ V& T$ n+ L7 a
添加OWASP规则; p! e# j' Z k4 A5 ?% |: a& a
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
9 M: g1 t6 q" b, N* ?4 {( a9 e7 ]* a" {4 @6 n
# G/ f6 l) _- ]) F9 O
下载OWASP规则并生成配置文件/ }4 o1 Z% e! s7 Z2 }# u4 }
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
4 N0 w; i) ]. s- q7 O: @/ o2 \$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/, b. t( ?/ e7 f' \; c8 J
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
' x( x5 r6 ~: E/ |" y$ cp crs-setup.conf.example crs-setup.conf( T" i8 O) `8 C9 U( c' n
配置OWASP规则
* J* a2 C& { D2 c6 ?9 R7 Y编辑crs-setup.conf文件% T+ ~- `8 Q0 r
9 R5 g% d; W7 N0 q3 `" x+ Y) a* ]/ ^# E' \
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf" H, k8 p8 o: {+ o8 t
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf" D7 O; l, C: g3 b, D L0 D$ N# C/ q
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf1 v9 b4 d' h# V, c3 R
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
3 {2 C) y6 h+ |2 n$ s默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。) q+ W% |; H8 D8 { V4 T
; k- Y) V. K$ ^
7 l9 a# H5 u8 m: G! @启用ModSecurity模块和CRS规则
4 w& c0 s, |. y( ~& K5 @1 Y复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
; m c4 l5 d }" K2 T+ b4 ~8 c
1 J" u4 [# i- M1 f" l5 o- [4 @" j; M! c: v
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。( e! z3 B2 N% o' A6 X s9 \
1 V$ V. b- k, }7 v
1 R4 x6 Q9 D6 @& d* `$ cd /root/modsecurity-2.9.1/
7 ~* A, K4 l3 [, }2 D6 ]) Q& `$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
& i$ V# ?; O8 }: V& y4 \$ cp unicode.mapping /usr/local/nginx/conf/6 D3 L6 G# i+ B; s+ C( ^2 _5 E9 a
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
% r( z% u1 a- {) R& N$ ^% y7 W& E- A5 V' {5 t
% W' _6 Q) C# r+ W6 ^/ d5 d* V$ vim /usr/local/nginx/conf/modsecurity.conf+ x2 U0 G0 O1 U- K" o+ {$ {
SecRuleEngine On- c& ~4 n. {. H% g" g8 f& s3 Z& E& m
ModSecurity中几个常用配置说明:* T, X" R3 W0 w
A( K# `0 I2 p% _
5 H8 t2 _& p7 X% P
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
; O. P$ s; Q1 r6 p* } e% h: ~! }! q( y" d1 E/ h3 Q$ S
" t' y. `% K7 F; h7 _1 U! E2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。: O- d' y1 y6 }' X
" P- F" p! U! m8 g5 g$ f
. U$ T0 g0 A- ]4 ~2 \
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。- u7 D. ]6 W- Q8 e0 R
* ^+ j! E+ d, e8 k. ]5 A
{7 W$ @$ @, ?( D, v- f: j4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
- }! |9 q! G& ?# S/ f- a4 U( ]0 @+ O9 |0 H; W
6 \' s+ Y" L# e, c( U1 Q
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
0 {6 Z- A5 {- J
8 k8 [' t' E$ V. E: w* O7 V" I, S8 }# {# t( {
3.x版本CRS
5 Q3 w2 l; t4 Z1 h* v$ U$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
' o2 q$ @# }, _! }- |) Y* l1 q# 生成例外排除请求的配置文件 G: E4 t. a, k. b9 |4 u Q4 s. F3 ?
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf. L4 F8 i q8 N" U3 N
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf' `) ^. ~, w) E
$ cp rules/*.data /usr/local/nginx/conf: K& K ~3 z4 c. A; t9 H2 p
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
3 X# y- A& D* U2 w4 K+ g% d
$ U' F1 V% t( o- k4 l
9 U- X& k* z( j$ vim /usr/local/nginx/conf/modsec_includes.conf( k: O2 p( m1 V) a1 s- `1 w5 M
* _9 d/ B, H* G8 c; j[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 x" d; {' R4 {% T3 W2 Z+ M; `$ e
) P6 R& \7 c, B4 H& f z8 l注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
6 j" q5 [8 H& J' r6 a- `9 I, n$ h0 Y* F* ]5 [8 t
0 C3 X/ J: r+ b p( m/ h. ^! I. K4 Q
配置Nginx支持Modsecurity' I1 c/ K( W# q4 B( J
启用Modsecurity' D( @6 o( d5 Q+ _
使用静态模块加载的配置方法
# V7 B* t& \2 e" j8 p0 K在需要启用Modsecurity的主机的location下面加入下面两行即可:6 a3 h4 Q% G8 ]5 h; ?" ?
2 K* u* v p" h; w1 b" l7 B- C5 j- J% h
ModSecurityEnabled on;
. O% \# R1 x8 o5 g; vModSecurityConfig modsec_includes.conf;
6 \! @0 I& s& q) E+ _! ?/ ^# @修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。% F, \6 m' o4 m8 ?5 X
5 _# |, T' H( Z
5 d) j0 z4 f; v. h3 b, J$ vim /usr/local/nginx/conf/nginx.conf: [+ U* Z6 T$ P& Q. _
/ S$ e. ?* S) U5 p& w& ^8 F+ X
/ J1 S# ^$ d3 x' Pserver {
5 X8 A* c1 s$ N# J( q1 G listen 80;
6 T4 G) S: A* l1 i0 j% w8 L% O server_name example.com;
& F8 D" i0 k J" y6 ~! l2 f- g. `# j( A2 L0 c7 c; p! a! n! d$ ^
2 p" _4 m2 r+ H9 H
location / {
, |2 }8 |, B' h6 C ModSecurityEnabled on;9 H, A; C. L, J( g; J& V) m0 b
ModSecurityConfig modsec_includes.conf;9 C( n3 i! Z. e
root html;; ? {7 ^4 a/ O- k$ |
index index.html index.htm;5 t/ u/ D2 U. W# ~5 X1 I7 s. k
}
0 D) ]" \) R1 D1 `2 Q6 Z}1 d( I9 S" J# {. L) N$ K4 q8 W3 V
使用动态模块加载的配置方法
k: X1 R I4 j/ ^" z6 a* {& |在需要启用Modsecurity的主机的location下面加入下面两行即可:1 {! B7 ~ X( O$ `' @2 Y! g9 Y
3 @9 Z" Z( J8 Z) R; @) ?% i! ^% G5 f: _2 P; \
modsecurity on;
: f7 @7 \! b! A. x7 _modsecurity_rules_file modsec_includes.conf;
) E8 X5 i1 |3 H* \修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。# h, x$ G, d m0 @0 t
1 [9 ^ ~9 J4 b, d2 K& ^8 n
) s! P7 M$ K, Q$ vim /usr/local/nginx/conf/nginx.conf
- r7 {) p A. Y: L" O- C1 t5 J5 ?. ^9 ~. W
d& @6 q7 T( G! _4 ~5 r
server {" M9 j/ V" C& k ]
listen 80;
" D' z/ H# v Z7 `2 `1 ]( W% E server_name localhost mike.hi-linux.com;! g6 U! n, z. v8 f K2 D' W
access_log /var/log/nginx/yourdomain.log;/ {( f' b3 [8 t9 X7 q& ?7 ^! c
; U h; o( G0 _ l. J
4 f% S1 G) U) N5 Q# _ location / {
$ k, ~. r! k9 P2 T4 }& r' O# `" U( L4 P* I; V6 Q
& \/ r& P1 ]5 ~" q e+ Z modsecurity on;- K& W- B4 l4 C c
modsecurity_rules_file modsec_includes.conf;) W. [; E j, p% O- q
root html;1 k, T n o0 x- _- X; g" H9 |
index index.html index.htm;& a8 H. K, W& Z& ]! _* E
}
) N7 D* u g; f7 P}
7 Z8 [/ c( {& y验证Nginx配置文件
- L0 m7 M! O' N$ /usr/local/nginx/sbin/nginx -t9 d2 F9 ?5 H( Q- d) D; y
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok$ O3 k% D+ J! R7 m! M& K# T
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful7 j' V. T4 v0 K4 [
启动Nginx
- y X/ A- g/ [# Y7 W4 u$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf3 s2 q$ J, C7 G& p9 j
D0 t0 A; y" U9 n测试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能够战胜更多复杂/未知/混淆的攻击模式。
" m+ q( X3 T- a7 N |