ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。8 \* v2 S7 s3 C' s& L7 i8 V' o- g N
% r! s. W4 C2 ?0 M$ N
$ { K( d/ Q5 m# q* Z5 R
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。! t0 f, l6 L2 z; E* Z
w+ y: o: c: `. ^3 T; u) r
6 B, y" e0 n; D- h, B什么是ModSecurity1 O9 X( r- s! F& H* H: G1 P# V
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。( x f6 |! w9 j8 M0 e3 ]8 t& c9 n% y
; [! z' I+ n6 T/ K, s% |- F" @2 g2 Q! Y5 e3 M$ X
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
$ Q0 B0 k& p# }, _' b4 G/ c# i0 Y! p g! `8 P+ `6 F z
4 }6 g0 Q b* _4 Y
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。2 ?6 y/ K* ?6 a+ m: v8 `
8 a7 o/ T8 k* m
7 J d; w Y. k& Y
官网: https://www.modsecurity.org/2 M5 ]( Q9 R; e
" W2 @. g |/ ~; P4 P% u! M
9 k* A! I. P# j. i& d3 A什么是OWASP CRS
- X) o- {- @6 F( EOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。; C1 B0 @0 p+ j& A6 Y/ H) @
' o7 m v# S- }9 Y5 b
2 | J; }' A7 l9 _$ P4 `ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。; a1 R' h2 f& c0 C0 N
& E2 }1 U) s9 M* Z) _. B9 M$ l: F& j$ G/ e2 [
HTTP Protection(HTTP防御)" u' L O3 E5 c& e
HTTP协议和本地定义使用的detectsviolations策略。( r* v% b v0 P# D6 h
% g7 p6 Q+ o' t) O! y2 i
7 r6 T8 {. P$ K/ \- _9 o' h" d
Real-time Blacklist Lookups(实时黑名单查询)
0 Z* X1 W! I1 J2 ]4 F1 |# y6 T5 G利用第三方IP名单。
; i* w: z/ q" R7 i9 ]6 L
9 [- j5 v0 e7 f/ b) E" a0 @& W3 c. X) |, m- g R7 M
HTTP Denial of Service Protections(HTTP的拒绝服务保护)+ {/ J3 ~7 Q6 w5 s' v
防御HTTP的洪水攻击和HTTP Dos攻击。" O/ ~( ]/ d6 I# M3 ]- \9 y
$ _! I5 ]/ G3 v, K/ V/ \( {- ^/ C- [2 v! a
Common Web Attacks Protection(常见的Web攻击防护)' }5 ?' C4 ~3 `
检测常见的Web应用程序的安全攻击。
7 r8 v' [& X$ e0 o+ \' h3 I# F. [) A0 X, C# U
; @9 ?+ z& B* }* y, o/ ]8 eAutomation Detection(自动化检测)9 ^+ P& B9 b ]8 \9 n6 {) A; u
检测机器人,爬虫,扫描仪和其他表面恶意活动。
' r* M* d4 ~: X9 E+ Q7 u8 `2 H W1 j9 a8 O5 i
1 a& {6 N/ u& S9 v$ t% }Integration with AV Scanning for File Uploads(文件上传防病毒扫描), e: D/ {. M' R& W! Y8 ^! |8 z
检测通过Web应用程序上传的恶意文件。; Y* r; G V1 q# b5 Y
j9 ]! Z- b- v' l" ?4 D$ z9 n
n- u" k' @1 j5 c% M2 c$ O" `# S8 w6 zTracking Sensitive Data(跟踪敏感数据)
1 Y3 w0 p2 {' ?# ~信用卡通道的使用,并阻止泄漏。+ A1 D% ?9 r+ y0 r# ~: t, x
8 q. m' u: b' `+ k+ S0 o3 ]6 F8 o- Y b3 j6 D( I
Trojan Protection(木马防护)7 G1 r1 V7 j& `. n# g. F: R0 J0 {
检测访问木马。
9 P+ P; n! W0 a2 B) B* j; q) @6 G. ?
- `+ ~; T' |" K2 K& F' j! K
Identification of Application Defects(应用程序缺陷的鉴定)
6 a% S( j$ n+ _5 c: \; f检测应用程序的错误配置警报。# Y" B8 O- Q' G1 C3 [! L' Q, J( M, V
% g4 ^6 B C% j, ? } C- Z
. r" R! e5 H7 A# P( m# O9 T# NError Detection and Hiding(错误检测和隐藏)
; g+ z( L+ U0 E' t0 a5 }: F5 n" o检测伪装服务器发送错误消息。
% P4 i" [" D b! s2 T1 }
. m! B& t0 X, D2 e# q* f" d& [) R0 J" A1 Y# O( J6 a
安装ModSecurity |7 m. n5 o$ d. R/ n1 b7 p0 g* d
软件基础环境准备. P0 W, v3 V' _: o! g! m
下载对应软件包
& Q, U& n. t R- p- M3 e$ cd /root
/ g& z# w8 U) m: g7 a3 K$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'" M# c- ~) L& w2 K
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
& J3 a( y K8 W) L" t$ ~: i2 H安装Nginx和ModSecurity依赖包
7 ], @! W- r p9 K+ ~! ECentos/RHEL
4 S! m, m I! o% F, g, r
& H8 K5 S3 I8 N B8 ?0 E0 h. |1 p( u! ^9 |9 R9 k9 v; a4 L
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
! q1 t3 ^- T6 ~Ubuntu/Debian
9 a: B2 }# h9 m- U5 x, h0 S9 A* f, @# F$ u- S3 |" _
8 l2 R& K' h/ U9 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
( S$ L! `: c+ r' _7 T1 p H, k5 ^编译安装ModSecurity+ S: \$ `- w. n3 q* p
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
. i) J( {# z0 f
4 K1 v- T; e- d, q1 R4 N/ M/ q: i* r) d- h( S' o
方法一:编译为Nginx静态模块/ Y9 G; X. }* Q$ y( n$ e6 ?
3 C- G( @3 s) g0 o2 I+ A0 z6 S0 F8 a2 S% I! j: {
编译为独立模块(modsecurity-2.9.1)
% `6 W# Z8 o* I2 w: p$ tar xzvf modsecurity-2.9.1.tar.gz/ _5 n b2 d p" H$ Q
$ cd modsecurity-2.9.1/
. Y2 R& L( |0 P$ ./autogen.sh( P8 r5 t8 V% i3 A" p0 y3 E
$ ./configure --enable-standalone-module --disable-mlogc
P' A. _/ U' G7 @/ b* a$ make# P$ a8 N2 K; x. |. N, r
编译安装Nginx并添加ModSecurity模块
! n: z9 C! {3 n$ tar xzvf nginx-1.9.2.tar.gz, r" l) d4 S1 U/ z* f4 Z: d
$ cd nginx-1.9.2) K' T+ u; s' m2 D9 U" u5 [
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
5 j) h" B! ^8 B$ k1 f( V1 ?# L$ make && make install
+ F" X0 D) L* J$ \6 p方法二:编译通过ModSecurity-Nginx Connector加载的动态模块/ V. z& `+ S8 o+ B
( J- {( d) @) w8 b4 X( S- P
1 v2 k1 u* P1 b, j M8 L }编译LibModSecurity(modsecurity-3.0)
" c) h9 w7 @0 A$ J# J$ cd /root3 C1 R, ]$ F5 K5 n% r4 @" s
$ git clone https://github.com/SpiderLabs/ModSecurity# j# _! u R8 H9 E* C( L
$ cd ModSecurity1 w5 D$ ]7 M$ D) D6 d
$ git checkout -b v3/master origin/v3/master; v( n7 G/ |' p6 ?8 G1 r
$ sh build.sh
- [5 L# J8 i9 }; A( p. O$ git submodule init
2 O! @4 l7 {8 D! W7 s H( P$ git submodule update) |; J2 Q5 l2 t {6 r
$ ./configure, h* y9 M& ?7 r4 A* ^
$ make& ]% @' g, j9 v- a% O% F7 J
$ make install
/ Z/ w C! S* F4 f" h4 K$ K% uLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。2 y5 A8 r G! t7 k5 v
" P q ^+ P- I& B& a
+ l8 w( d7 C+ B! b9 V$ ls /usr/local/modsecurity/lib
$ b$ `7 A3 M7 g7 \0 ?) Mlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.00 b6 B# T2 `; S
编译安装Nginx并添加ModSecurity-Nginx Connector模块% G" x3 K# i/ h
使用ModSecurity-Nginx模块来连接LibModSecurity- \9 M* x% D$ R1 o
T4 m7 R% K! v* P
# _: x( v* I% i5 a2 Y
$ cd /root
3 z' v Z8 ]. I. L$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx# Z: d' y Y0 N
$ tar xzvf nginx-1.9.2.tar.gz" A+ K/ p- d i# {
$ cd nginx-1.9.2
0 z/ ^+ S; P& X5 ?7 J$ ./configure --add-module=/root/modsecurity-nginx+ m& p' S8 A, Q) C! m& B9 Y
$ make
: y2 v% R! Z; g4 H) d% ?/ {5 Z$ make && make install
3 C6 U+ _0 }( W( Z( e0 z8 D* V添加OWASP规则& d. `$ ^( s" l2 F% d: e
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。3 W6 E# I/ ^ z- T4 K+ h
. Y8 C% ~3 T5 V7 G8 ~5 q4 U7 \4 P$ ?/ a1 M
下载OWASP规则并生成配置文件. i1 ]. h+ i* X" k% e
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git( M% S: y2 Y: J% z9 U6 q Z! l
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/: {4 r# N$ Q4 S d$ }9 N
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs7 _4 L. t$ C2 ~; s Q( }+ @
$ cp crs-setup.conf.example crs-setup.conf- h7 E* I# W7 M' m& F( j3 S7 S5 K9 h
配置OWASP规则* g" e% r, p9 ?. ~% Z
编辑crs-setup.conf文件: V* G/ c+ c0 _, ^9 r$ D
" O( X) Z3 Q3 [ |5 J5 L
7 q# p* z' M: W$ m/ p
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
: s6 x( }' S6 \5 ~, O$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
% S) {# B8 f. I5 P: v: V$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
R; J2 B0 \. c0 J3 v2 I1 ^$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf! r m i7 P1 @
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
7 X4 Z0 c9 _. Y+ \) j% k. t% L0 u B, G5 c$ K I# y: f, o
& ~% y* W; s3 |7 S% |& R4 f
启用ModSecurity模块和CRS规则
4 L) Y, _3 q- C5 l复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
/ r P# r7 |* |5 J
* G! Q" t i l& y" j' a6 }% U2 B) x
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
4 \# x$ S9 J. v( u
4 m2 u5 G8 @# B1 `, m3 ]
: h: R# w7 Z. c8 r' g S, @$ cd /root/modsecurity-2.9.1/5 W3 w e, s) P& L: Q; A
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
+ j& Z6 @( |/ D# [! G$ cp unicode.mapping /usr/local/nginx/conf/5 L5 T8 N. Z# v- Q. E2 @+ Z1 Y
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
8 i* t4 E8 b+ d3 k3 ~0 K9 U4 t, }4 M2 i7 R3 ]% s2 p0 `
- n# Z: B1 c/ A# q b$ e* y' q/ p
$ vim /usr/local/nginx/conf/modsecurity.conf& j" Z$ D' k* _1 r7 i
SecRuleEngine On
n1 u+ o+ c% L# X5 `3 _ModSecurity中几个常用配置说明:
9 B7 z/ w, C2 l: ]7 d/ S
9 ^- P3 h4 v9 O/ m
6 |' I# p5 ]; P$ H1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。6 _* \7 B. w$ r3 P' @8 x7 ^% J5 t# p
: c; Q* Q: _$ E. g
Y% Q( l+ _+ C, B2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。" i) h% _( i- @5 M4 [
2 Y% q5 b! t5 |
# ?' X# s6 O% G6 y) S2 r& ?/ j
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。2 ]. l! e. S8 W! A) n2 R& @
# J6 X1 ?7 X5 O1 [ W# |! W
8 G( _2 @2 L8 Y# G5 k# X- ?4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。 V- i. p, M9 C) N* d
0 ], c: q% l' {$ z! U5 I, s
2 h2 H4 V. d; P; _' ~在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
$ B- Y6 O. t! T3 H* e. l1 \" D- n& |) P. R( x' V
T4 Y$ ~/ u) b
3.x版本CRS- V; a& k$ h& o* s7 i, q5 k/ \
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs4 G: M1 x A/ |# J9 J) g* a$ q2 u
# 生成例外排除请求的配置文件6 p! \ |4 _9 j9 q* U
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
$ b5 S# D' [( V7 L; u' l$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf' I- R ~6 N1 M5 x2 Y3 ~+ Y, S
$ cp rules/*.data /usr/local/nginx/conf
* K$ k4 c) _2 I7 ?- [/ o6 l- r5 v为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
7 Y; { W# x) z# M0 n% A
" A# j- l2 T) c q- M" R& E" S' I6 A9 h0 T, u- ~8 k; Q
$ vim /usr/local/nginx/conf/modsec_includes.conf+ ^& _- z; ^4 p# K. s
, g4 ~5 o9 O3 w/ E' V* b u- g- O# @' N5 e[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 ' T2 g3 q$ R& f. \) \$ G
' S4 C1 ] z9 `
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。- `0 ~% t: p% ]. W
* b, r/ C0 l2 ^; m, x6 d$ f% [8 }$ I
配置Nginx支持Modsecurity8 e8 d6 r' m/ _, K
启用Modsecurity
3 G! S8 w0 o! F C9 a1 H7 x使用静态模块加载的配置方法
& s7 R+ ~9 |' H1 I; o# q在需要启用Modsecurity的主机的location下面加入下面两行即可:
+ a, E, P$ V d& L# x! c9 |5 y- X+ Q8 R" b
7 C0 Y7 I) ~1 E' B8 KModSecurityEnabled on;' I5 d; |' {; v7 ~5 O
ModSecurityConfig modsec_includes.conf;- f8 ~0 C E, w8 {
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
8 v+ B8 E5 K7 t" p" S' D; b9 W, s% p. O; v3 A( e
% ?2 f) E& N; b8 Q8 B i$ vim /usr/local/nginx/conf/nginx.conf
" W- H5 [9 d/ M8 s) H3 L
# [3 y- ]+ t: B" l( I( `! j f- T4 `0 w% B5 M6 r) N7 m& a& K
server {" @. w, }4 y# m0 h. w& j1 F. K
listen 80;
8 S5 ^- @% o# H/ ?+ i7 L6 l9 X, h9 ~ server_name example.com;
( C% k, g% w) z. H, {% O$ O2 u& g# t. q! |
Q6 |, }$ h) L B R( k
location / {5 L) e% C: m7 q0 X: z
ModSecurityEnabled on;, B& N4 ?( u9 b l1 b; |
ModSecurityConfig modsec_includes.conf; O6 p! n( S, m# M/ s) P) n% o' @
root html;$ T; z$ j. l7 E3 S Y: w
index index.html index.htm;) s1 U3 M. K7 [4 N% Q3 l
} [+ d( v4 f2 s0 v& y% c# J
}
9 k8 X) q$ m2 t; D$ \使用动态模块加载的配置方法
9 o9 I2 S X5 o在需要启用Modsecurity的主机的location下面加入下面两行即可:
& A6 ~8 g. ]/ `* h
1 I% ?2 E S/ K* D
0 @) s1 ~& j5 b6 I% _* |/ Tmodsecurity on;+ m' m" Q0 l1 U( N- Y, o
modsecurity_rules_file modsec_includes.conf;
3 T/ \. S5 ]! Z) ~修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。! j, h% O' Y7 r# L) m' b
! J0 D$ u8 F) O3 k* Q3 T
- j9 B& x9 L4 S/ d+ m$ vim /usr/local/nginx/conf/nginx.conf
: {1 S8 ~6 z' Z8 {( A1 Y! U/ j) U# H7 j1 ~! a; I7 V( O6 n: d
, l4 ~' Z1 |1 \5 h$ T* ] ^& x5 `server {; z; i$ F3 u7 J7 p( o
listen 80;
* A' z5 E( s7 X) J server_name localhost mike.hi-linux.com;* ]( `0 B4 c0 ~8 Q3 x
access_log /var/log/nginx/yourdomain.log;
1 B, S" K/ n( d; P+ W( j0 A; t h. C: e
3 M/ O1 j' |+ p; s8 J location / {. Z! _- V: `! c9 X0 j. l, u8 f; }
' d3 @' ^, ^8 [+ e9 H; ~: B
) g5 J2 F5 d% o0 [# x! \ modsecurity on;/ {* O8 v; n# A# [
modsecurity_rules_file modsec_includes.conf;# @. F* {9 n+ x8 K* B3 c, O4 l6 Z
root html;
* U2 v! ]. G6 m9 |" O% r( Q index index.html index.htm;" z; i, Z: ^8 {) b5 T# N
}+ a- o) r9 R- x5 A3 W0 m
}
0 ]4 h8 `# S% ^: n8 j. z/ [验证Nginx配置文件0 |% Q& S! x: }
$ /usr/local/nginx/sbin/nginx -t
, N% K: a% o' g! }2 F' s0 S6 v4 gnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok5 o w7 K9 Y: a$ A
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
8 c' D8 s& d4 E启动Nginx/ m! _0 |7 @; e$ \
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
1 E' B; w( V* H- J! ]& Q
/ A6 \! ^9 b7 S7 i/ J- g3 V测试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能够战胜更多复杂/未知/混淆的攻击模式。 * }$ s* |* p8 [# a( t: K
|