ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。1 R, S5 }! B2 N: }
; P- p* Q, r( O1 i/ P, Z' D
& r$ b9 Y9 U3 }在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。: ]- Y3 }5 c) P% c3 D
7 b2 x3 J1 C* ]+ Y: i
" T* N+ r) g6 a7 E5 G; c) a5 H
什么是ModSecurity* N! K% a# i! l3 I$ e0 b9 `0 e& I
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。% D+ V3 ?. C( e& b9 k
. q M4 l8 J! q6 O$ U$ s
7 Y# g% I- [+ p0 Y, R7 h5 G2 P
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
( o1 }4 B5 I0 ~" }# v, `! U' w+ b3 E& z6 ^, ?/ Q. B k( u. {& c
! K7 e& `* W- A2 o4 u) @& I0 P
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
7 V9 f- I. N: u# _ z( O
2 b3 ?2 B( D0 k* D
+ S2 k) H8 M$ s官网: https://www.modsecurity.org/" D. n9 b! h* k1 ~. ~; e5 L2 t
4 F. w+ S+ F5 A6 L
- d" F6 [, }7 _0 a R0 V5 @什么是OWASP CRS7 D% g. f4 [& W5 o! O# ?& o
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。0 F; k+ S& j- J1 i6 F( Y
9 F+ ^. T& a) u0 _* R4 i& y- J) j9 W6 o8 @: k; W0 v8 w4 ~
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
& o: ?" L. L2 q* I; ^) B G: r p( w7 z0 e
4 o& B% j( z3 n N+ E( a
HTTP Protection(HTTP防御)) q# h/ S, K3 B- e& X! M
HTTP协议和本地定义使用的detectsviolations策略。
4 M& |( f9 |/ G1 K5 Q6 o; p4 T; V/ j X* ~ T: i1 E' w/ l
2 y/ _ _: `6 g; m, G
Real-time Blacklist Lookups(实时黑名单查询)
. t8 u9 r$ r" a利用第三方IP名单。7 C$ i1 q) F, H! ]! `, l# W3 l
5 J6 q5 n8 K) o& I
2 x5 G/ A# o% v( t5 cHTTP Denial of Service Protections(HTTP的拒绝服务保护)8 q9 S8 n5 v9 m1 F. s4 P: E
防御HTTP的洪水攻击和HTTP Dos攻击。& {/ j/ Q4 L) }# C- f7 f& b; { L
9 f' Y8 o$ R: c* A; e) g7 v/ J) D
Z. `% ?% R0 R5 M. g
Common Web Attacks Protection(常见的Web攻击防护)
/ p; C- T% e) P检测常见的Web应用程序的安全攻击。
, h% R0 c! W) Y7 H+ K5 f3 R1 h( B6 n" G% D
' }# G+ n2 D" }" D& O7 UAutomation Detection(自动化检测)0 `: p2 K9 }) ~( F$ k
检测机器人,爬虫,扫描仪和其他表面恶意活动。+ ]! S F- k5 G* `0 b+ j" v
" I+ X* ]5 o% K! I9 L5 j
; \ g. L# {6 UIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)3 m! N2 p! ^, \2 p3 @" s: {
检测通过Web应用程序上传的恶意文件。& g" n7 k# e/ N5 ^( C; _0 K( H6 u
9 X; A( E5 H& c5 v0 [$ j, }7 E2 L
' h( ]& k1 N1 d. b# C* }5 m# ]Tracking Sensitive Data(跟踪敏感数据)
% u) E1 i: x" p$ } n5 x: B信用卡通道的使用,并阻止泄漏。7 H$ K9 ~/ D/ Z( j# F' O3 o: c: \% O
4 X; B& d* g& R" Z: Z9 |# g7 \" X3 s
Trojan Protection(木马防护)+ d+ B- s4 }1 F n9 K
检测访问木马。; T5 X2 f' M7 c/ r8 j! N
2 i, L7 X& m( k# ^
1 J1 S* ?' W8 P# ]1 GIdentification of Application Defects(应用程序缺陷的鉴定)6 E8 ]: X. Z$ c& p# Y
检测应用程序的错误配置警报。
: c8 j* M, n6 X* ?7 I7 I5 f
. z) E+ F& M" `1 g6 n1 R# K2 \! r4 t+ L" |0 d5 }9 B
Error Detection and Hiding(错误检测和隐藏)
& i9 E" d& w a8 N% n6 n) o2 G8 B检测伪装服务器发送错误消息。
" V: X1 |. r, u, o; s2 t% X' b
4 a ^ R/ s* k; \! e4 s9 A
: n1 G) Z: p' r安装ModSecurity
p! j" Q- s1 B& S软件基础环境准备
- T, W: B# w0 r下载对应软件包
6 {/ D8 p, D$ `1 S% k$ cd /root3 D: ^. K: y/ W. D& S' ?( y6 V
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
4 ~& {- u, Q0 y/ w$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz. i# Z2 h1 S; `9 }( _! d% Z) t
安装Nginx和ModSecurity依赖包( }# j2 d5 ?/ t# X8 t5 {; G; K- O
Centos/RHEL
4 J7 ?9 p: ]7 B T$ ?- e% m
' J7 i0 Y4 T: j: Z/ g
1 c a5 x2 |" X' X9 a$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel, c7 d! i4 u$ w/ [% P
Ubuntu/Debian
z N" r8 O6 k9 [" W* B) D5 D7 G' t' E
! Z4 k" [( J7 [$ 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
% r) e3 T( W& u8 V; h! v" }+ J2 w编译安装ModSecurity/ U- t( h: T- ]/ {
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。9 Q, U6 X8 I& ] F
0 w9 R. M: b" X* H% a
2 q" T" a- h7 q% {方法一:编译为Nginx静态模块
& {7 r+ t" A$ H- |- t5 A3 b( G1 F8 n9 \$ }: e1 U0 u; ]
; g. F+ ]9 _* J% r! e( r* u$ s
编译为独立模块(modsecurity-2.9.1)
* a( _! T0 x2 E& y, |1 |; w$ tar xzvf modsecurity-2.9.1.tar.gz
( m" {& p n* N" ~$ v$ cd modsecurity-2.9.1/$ L* o& c: U1 r2 g
$ ./autogen.sh4 T- t) Q4 S/ l3 N! C: s0 J8 ^
$ ./configure --enable-standalone-module --disable-mlogc
$ Y: C" R$ j6 `7 R5 p' c# O8 R$ make
3 _; n, G: L8 |# Y% {) E# a$ K3 W- S编译安装Nginx并添加ModSecurity模块3 w& R- U; s! }' j& f
$ tar xzvf nginx-1.9.2.tar.gz
1 p& U! L- h$ h3 E S4 Q0 s* ?5 B$ cd nginx-1.9.2. _" p% b+ ]8 F, K$ t5 R
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
- A f/ i4 d6 G1 q7 i$ make && make install- s" A F" J0 K, z
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块* Y; b! Q! }" M, k
0 D7 k! @3 S% w \, C6 h, @) g0 {) E9 Y8 N5 p4 W' U0 L6 [" ]
编译LibModSecurity(modsecurity-3.0)4 J8 F% P" Z- i' z% d/ B
$ cd /root
, n: w* h+ g- K5 B+ X7 o. e$ git clone https://github.com/SpiderLabs/ModSecurity
8 G8 Y F2 u6 h, ~$ cd ModSecurity6 |% C1 k- D6 r+ c
$ git checkout -b v3/master origin/v3/master
0 g# X0 S( m7 m: |+ }6 s& T D$ sh build.sh3 t! x1 o) t9 c! ]# M8 M \
$ git submodule init. w$ F% e6 ]" [3 X3 U, B2 u
$ git submodule update
) T3 _, k) \3 o: E$ ./configure+ \/ I8 U( K3 l; A' v6 g
$ make
- ?& G) e6 \3 ]0 A/ [1 x$ make install
9 q1 {5 Z% C& a3 cLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
! h& V( e' T) E+ S% o
+ b% a1 F/ G1 i8 G6 w
* K5 N0 f* ?7 t0 q# \! m$ ls /usr/local/modsecurity/lib
' e* j$ h1 h2 U: E* ulibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0. R! D8 c8 J- S( d
编译安装Nginx并添加ModSecurity-Nginx Connector模块% f& {2 U1 n% \
使用ModSecurity-Nginx模块来连接LibModSecurity4 [) m6 F: ~% }: L0 x2 R" N/ l7 p
( a6 \8 Q1 D" n
1 I; k" G" r& x W$ cd /root, \( s: o0 @( j. S& u9 W- B
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx" e4 z, H) O0 y r6 g
$ tar xzvf nginx-1.9.2.tar.gz" c) m3 o- q/ A3 w
$ cd nginx-1.9.2! u ]( W+ A1 {6 ~
$ ./configure --add-module=/root/modsecurity-nginx
2 I+ t" M( h' L$ b# B! O$ make1 f7 S7 Z* ~3 G* j
$ make && make install
* U0 n8 }' G/ s/ w1 E添加OWASP规则
/ f* G/ |& h% v) Z- M: p$ T) {) ~ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
; _% \ q3 u- H' v3 i! T
: r M& i' o# `' u L% i3 H$ }" f% m- l' `% w
下载OWASP规则并生成配置文件% h- v2 X7 a" E2 W
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git' R: W& T! [2 S1 Z; f. y$ Q
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
4 Y0 N3 y. W# U$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
7 L3 h' _0 o# s3 \' \) ^$ cp crs-setup.conf.example crs-setup.conf) C) k" h6 v5 V* I3 S$ U
配置OWASP规则
8 `+ N# o* ^' ]4 E: S0 s# K* z编辑crs-setup.conf文件
' S* A! E, _' t Y M6 m8 ?8 M" G! m) ?' V
5 N; X" V6 K, g" v9 Q. S% C m( ]
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
4 q/ W- s( ]- J9 R; W; ~; Z e$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf2 M$ Q* E. ]( e }8 T% s
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
+ }/ V( i' q: q$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
& R f$ W0 |. a$ G9 d* _0 F* p默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。! @. U. T$ U: s+ P D# l
7 Q7 M( _6 ? q
0 K1 z/ e+ B8 @+ k% l% t启用ModSecurity模块和CRS规则$ _9 O# R6 i5 \3 z0 I; O
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。7 M8 j3 Y/ J5 U/ I% z, T
7 X; Z- C5 o/ [* o0 S3 D: X& b z' C) N* W. g# l* h/ r7 W
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
% X7 p$ H2 B& A P S$ V0 _$ g
0 I* N4 Y& ~2 M: c2 `+ g7 W
- G9 c0 ?" g, B5 o! w' h$ t$ cd /root/modsecurity-2.9.1/
+ A( }" y8 t1 o P$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
# [- b3 `/ Q- n! \& Q1 U* ^# a' J3 h$ cp unicode.mapping /usr/local/nginx/conf/5 m3 O0 J) K5 @" z: _
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。$ w# X3 \0 I8 Q4 V
' o. v0 l3 @' L+ q. i* p
! m- `1 f! z. f. u
$ vim /usr/local/nginx/conf/modsecurity.conf A x2 v; D" U! Z; d2 ~
SecRuleEngine On
, r, }4 M1 U3 pModSecurity中几个常用配置说明:
* O$ b: G1 X) I1 t `# d. @& {2 m6 B9 Z9 P% F
- [% N1 x6 W% ~1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
f1 z6 M! I$ K2 G6 D
( n1 C' w f y0 |" v& V% ~
1 s! N2 \; }5 P5 s2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
, o4 n6 ~4 h. w9 a! S) B- d1 |: l# x, J9 {* j3 ^
+ H3 J* h) R) U2 c% j
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。4 M' {0 c; | J) i; E5 @# w" o! D; Z
4 j' h/ }' |2 f% u* w: s% R/ Q3 e+ _. o. z5 i: C4 {* F
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
0 n2 Y" Y) u7 T. X* U, d v% G7 W2 E" p) Q/ j7 m7 A6 [! A
3 B! _6 b0 o" w) ?# Y
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。* \1 A/ k; X. Y& |
/ k& d0 r$ T& | U1 Z
- z6 N4 Z4 m3 Y4 k7 K4 O
3.x版本CRS: @$ n! Z, C: W
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
) g* Y/ O, n. S, ^# W# 生成例外排除请求的配置文件
$ M2 Y! y0 u6 N( D& s6 w* W$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf2 F7 v6 t* k& l, N- L1 z) |
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf7 c6 ~( f0 d5 f( X4 V& J: G
$ cp rules/*.data /usr/local/nginx/conf5 ]! L; `, y- y/ e: u
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。2 M% z4 n i+ v* f4 A% Y
, h) F" c- I- r# N: t4 i @& @/ t* o b
m' J/ u% @* Y( B2 p
$ vim /usr/local/nginx/conf/modsec_includes.conf- P; T2 t% Q Y8 ~! V8 E
0 l. C; s$ ?& S* [5 r% R
[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 & `! G) z, M% ^; Q
9 j" ^& E) T6 e7 M6 h
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。* C @7 T1 \1 d& G8 P
* A* H H% A! R6 p* L, n
' g* r" ]/ h" `1 M9 M配置Nginx支持Modsecurity+ W& |0 Z. N# @
启用Modsecurity% z D" \& G2 f3 w8 g% Z5 e" u1 i
使用静态模块加载的配置方法7 m1 y/ H( C( {2 H- V. R
在需要启用Modsecurity的主机的location下面加入下面两行即可:9 D* I( o9 |1 [. s; `( Y
6 S& Y& k" s+ F0 G6 ]( ^1 I: m% Y y1 F0 X- @
ModSecurityEnabled on;
2 x7 ]9 N4 g+ F: y+ ]" b: T5 n* J# kModSecurityConfig modsec_includes.conf;- a- u9 U/ \" ?2 r/ c& t' t B
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
# y5 h1 `% I* R$ W0 T/ k" q+ _$ V) y5 r, \! e8 q7 i
0 p1 L. N, b" }! {# V$ vim /usr/local/nginx/conf/nginx.conf
) q8 m8 y7 p; f# B/ j4 e' ^ ?$ T) O5 M
D6 r( H8 G/ Z; Y$ b! Z9 Dserver {+ v' o( ]" z$ V. ]# G. m
listen 80;
9 g% V# i7 y0 @( e* i) Y server_name example.com;; j" t2 |! X! p. v
& ^4 `/ q# ]# j8 T) S* \" T
" p5 f* w8 |5 b5 e. X location / {0 c9 W7 ?$ B3 F# S6 U8 O7 }
ModSecurityEnabled on;/ q; i4 l( Y8 D( D
ModSecurityConfig modsec_includes.conf;
/ p I: t6 N9 F9 Q root html;/ N, X- }6 J4 E4 _% r; K2 g
index index.html index.htm;! _+ C1 N% }, W+ m4 I% h
}* i8 [) ^; a7 J1 R0 Y' u
}
3 m+ e9 m1 g$ d9 j0 O8 d- n% j使用动态模块加载的配置方法
" d. c# M/ v0 C4 x- B# ^在需要启用Modsecurity的主机的location下面加入下面两行即可:0 e5 A$ ^1 }/ Y- e+ i
6 m5 X7 ~( @* W' T1 Z
9 x7 `# D( o' M6 S5 F
modsecurity on;
' q* |/ ]5 ]! G$ }( \7 Qmodsecurity_rules_file modsec_includes.conf;: t. [3 E( i8 y4 t) s" u3 x
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
# V3 T$ _) n# U# {7 N
! Y0 w& g) z2 z0 l$ y6 D G# v4 Y* r+ q Y) b* k1 Z4 E7 r$ a
$ vim /usr/local/nginx/conf/nginx.conf
# F8 S2 Q9 ? R8 D' y" t
6 X5 i' A0 e7 k" A7 ^8 [8 k' P2 c2 M6 R* l7 e* c
server {# A- O& C: x6 V5 }/ F2 G- h% L
listen 80;5 t+ g: f6 j/ I" x9 z& k; D( V0 Y
server_name localhost mike.hi-linux.com;0 F% l1 F. n2 |
access_log /var/log/nginx/yourdomain.log;
% @4 o d6 m3 n+ b* j/ B+ l6 y! K
" `, n d9 D+ D% r) m3 `) u" \
- _# r( E2 v% @' l$ [4 | location / {
( p* y; x3 \2 p. J. e. A/ @- g- r; g6 ~" p& Y2 A
& O8 p2 N7 C: v6 |1 p& c/ Q
modsecurity on;
7 u/ n$ L) h U) a( j! Q' ~5 d' F modsecurity_rules_file modsec_includes.conf;
% q3 u4 e# {$ u- d root html;
# v6 @8 b4 r: t5 U index index.html index.htm;
4 F: g, r' _8 F& r: S$ C! B}5 ^, o+ u6 x; V, a
}
3 ]. C5 t$ U5 b验证Nginx配置文件4 B4 `: e8 v% c. p/ r
$ /usr/local/nginx/sbin/nginx -t
2 O' a/ N r, o! P& d/ _nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok+ D$ z7 b* c0 N( ]
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful7 p' `0 J4 |" ?$ u8 _
启动Nginx4 U- @$ I/ V( w0 d. @) Y
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf& Z% M( P' a; m
- P% w6 N4 |" h0 o测试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能够战胜更多复杂/未知/混淆的攻击模式。 / c9 x5 W! G! J% J
|