ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
7 t, [1 P! @2 h2 ]9 d& N' X B1 B2 v8 y* ?( F, d) d
7 p# K6 s. y% t' G. ^
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
: Q$ u) T; N. h9 s- f- p4 u4 }9 Q
9 N' \% z% G4 _+ y. P
* B" }3 i3 M, s% K3 ?3 M+ _" d9 i9 p什么是ModSecurity/ P5 O. q2 c ]: R. u: w
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。$ z! g' h- ^) k/ a- R
6 J' N. C/ C$ r% @8 }6 N( t/ V! V, f$ X9 S
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。# ^( m4 c/ T4 Z% a3 }2 P! l
8 f5 h7 A A$ j1 n$ M
* T- H9 h! ?4 y" D2 E; n
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
% p0 L+ f( ^9 `; h* k: k3 R8 f- E l
6 o" A$ e) k# D9 W
官网: https://www.modsecurity.org/
! b4 X' R" b/ s+ @) q
/ |' g, {3 q4 [3 ~4 o& x% r/ T; j! v/ ]& V/ Y
什么是OWASP CRS, a: @6 w4 s$ D9 y" p' h
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
' W0 n& q/ W1 _! M1 |: f" e6 k& \# F4 o( w& g4 p9 v$ Y5 ]
4 W* ^) t+ K# ?, pModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。; N+ b5 f9 x' G5 G3 p4 X
1 R3 y1 Y7 c7 y l/ f$ k: S' f) d* o3 N1 B; j7 i
HTTP Protection(HTTP防御)) q* }# i# [! E' i* ?
HTTP协议和本地定义使用的detectsviolations策略。
5 `% q! H+ z$ R
* y; x6 z7 {' u' z, X" N
% N; w& `# r E2 qReal-time Blacklist Lookups(实时黑名单查询)9 I; j5 V8 H# C- m
利用第三方IP名单。" ^/ ~9 l) \8 r: {
! ?% R! n% m3 G7 Y
; R5 K- \3 v, K: }; `8 q# V
HTTP Denial of Service Protections(HTTP的拒绝服务保护)3 z7 z; r* m6 ?* u" A5 {& Q
防御HTTP的洪水攻击和HTTP Dos攻击。
6 E' D7 A( K4 m8 K" _0 L* j
9 G, m! V2 Z5 v6 o$ M- p' z1 D
H+ h8 z- H- O8 w) N: p6 T1 PCommon Web Attacks Protection(常见的Web攻击防护)5 B( s' ^( N( \* E7 F$ y3 h
检测常见的Web应用程序的安全攻击。
9 x8 M2 ]$ y! f, V1 o
: G7 g) e5 ]* K# }, r
, H$ l7 y/ y$ V& o( nAutomation Detection(自动化检测)
. N) I, [5 ^$ P3 F检测机器人,爬虫,扫描仪和其他表面恶意活动。1 N8 h- @1 l6 }" Y% z- [
. z6 V1 Y' t/ O5 S
9 g# o" Z& A) p' TIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
6 q W; x* A2 }! Y: g2 C2 U检测通过Web应用程序上传的恶意文件。0 u+ U! K, d+ C# w
, e4 P% T! X. X& r( J: U
7 Z1 e" D: ]" L' X# R& @Tracking Sensitive Data(跟踪敏感数据)
1 x. C U0 G0 m# ^# x: b信用卡通道的使用,并阻止泄漏。% ^% q3 |, A, }9 Z* [2 o0 P) }
& \- A8 e& D+ c7 A+ l
; Y3 Q2 h9 p- D) J9 \$ q1 c' n
Trojan Protection(木马防护)
|/ y; `: j; m8 W& E7 i: d检测访问木马。
1 d! K, V* q2 a8 h# y3 k3 M" H* A9 o( B0 N. W7 q. c @
6 B" {6 ^* c" h! }. W! cIdentification of Application Defects(应用程序缺陷的鉴定), ` {+ ^4 A; }
检测应用程序的错误配置警报。
9 `4 i# D, i4 |& p$ ?- P7 Q4 L! z( v2 G
4 |7 e G G9 Q* K2 P- Q5 |
Error Detection and Hiding(错误检测和隐藏) ^. o) q# _( X
检测伪装服务器发送错误消息。
* E; ?5 M) ^0 U O
6 E }0 n$ d, q, x7 C
2 a4 r) b3 p* b' ^: r( N安装ModSecurity
; W7 m' I% \: C) ], a4 X软件基础环境准备; p3 N( n9 ` J% ^! w) x3 ~
下载对应软件包- x; k$ \2 m, b/ {1 z1 L
$ cd /root8 O- C# X+ ~* v% q+ I
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'5 H/ n) y' S9 i$ n/ a6 P
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz/ O. E) P% L( Y" Z B# F$ x1 L
安装Nginx和ModSecurity依赖包
! t% e) U# o: uCentos/RHEL
; C4 N2 C) ]5 O/ w) F7 ?9 w: V- a1 f3 R5 e
5 G* Q8 X. m2 L$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
7 z) @& W+ |9 Q2 l. DUbuntu/Debian, a, u- }7 k- O; C1 {
% o9 t: e# }% {$ @) z* J
* V0 d. W( `6 C& _; n% P$ 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
7 t2 C3 ?: o/ |6 d L, Z) R4 E编译安装ModSecurity
3 U4 x+ f8 W! _! d" MNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。* x: m+ ^) b2 o! X6 B
4 L9 Q8 m2 q* b$ K
- w; e0 Z7 @& X8 B; \) N5 X
方法一:编译为Nginx静态模块
1 g+ h2 p# U+ j4 U8 O3 V3 }% K; U" x5 R! Y( `; A" N# Z R. y; X
' Q" Q4 a/ L( W编译为独立模块(modsecurity-2.9.1)+ |4 J9 f, W) C: w1 k
$ tar xzvf modsecurity-2.9.1.tar.gz
9 ~. ~. k- T- `! n$ cd modsecurity-2.9.1/
9 Q6 m& g$ v9 g/ @- q; Y$ ./autogen.sh* x i+ t# ?& i$ N
$ ./configure --enable-standalone-module --disable-mlogc# o& x( m" j) Z r- d
$ make" {' H' W! z* G. u
编译安装Nginx并添加ModSecurity模块' _4 X; q3 g5 T2 w; n$ E5 y
$ tar xzvf nginx-1.9.2.tar.gz. a: n/ b1 ?# H/ B' k5 u K
$ cd nginx-1.9.2
+ Y" h0 H. o) \ a/ a" h$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
/ ~6 |' o6 O0 t b- g$ make && make install
7 o" b8 k( F2 a1 ]方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
2 J3 ~ p, _" v# G$ f! B$ I8 f, `, g+ z w* h# c3 A4 \
- |$ H* H# _" h1 O7 h8 S- d: q
编译LibModSecurity(modsecurity-3.0)
% j k O' |& K5 N- q; j$ cd /root
0 \2 b' h. G w ^8 O" q$ git clone https://github.com/SpiderLabs/ModSecurity
' J# \2 O$ `: o: {$ U6 ^# r) c" l$ cd ModSecurity
8 z7 w# \5 w, m+ ~' |$ git checkout -b v3/master origin/v3/master3 @# L& Y$ W% N, k1 e' l; O$ ^
$ sh build.sh
! L7 `* W/ n$ c- }% H+ g$ git submodule init1 M* b: p( h( [5 d# D6 \% s2 g
$ git submodule update
2 s0 \. W0 @0 F: r6 i; N1 E$ ./configure
% P. N, P( P/ E$ H$ make Q* G4 Y& D; [$ [* H
$ make install& k" A: N' O( ~ F T
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
& c2 O" T* V8 ]: q H# T3 P. x- D9 T) O8 y
2 H" d4 M9 O* l7 U$ ls /usr/local/modsecurity/lib
5 P( f: Z& R* o; l5 a dlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
' v1 I8 v( T/ g4 }+ d编译安装Nginx并添加ModSecurity-Nginx Connector模块
. q- }# C) c' T- ]) X7 U2 _9 [ t5 r( @使用ModSecurity-Nginx模块来连接LibModSecurity
1 X1 ?4 i. ?& @4 i" D- m% G# c
4 Z* z: E g7 @
' s$ c. w! E! z$ cd /root) { s9 }) E# l- V: \
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
8 o/ H# T, g9 q% S$ H$ tar xzvf nginx-1.9.2.tar.gz
+ W! ^8 t- ?, X$ cd nginx-1.9.2
; P! i9 @ D( [+ A$ ./configure --add-module=/root/modsecurity-nginx
0 K. v& H V' r W$ make6 s* }5 L+ x, Z: ^$ C; H8 G
$ make && make install; `( q2 U5 _0 E4 b @# f
添加OWASP规则
1 L. j! R4 ^9 v) _ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
( e& m4 J) S* g8 T; U( w3 s' h" g K/ w) ^: ^5 _, a( a$ x. w3 _3 }
; S1 T! r) C4 v下载OWASP规则并生成配置文件
o% d, _! Q% t; Q: J5 s! p9 Y$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git: Y$ g- O3 D+ W6 G# A
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
6 s* d$ c+ U2 O. F+ H) y9 h$ cd /usr/local/nginx/conf/owasp-modsecurity-crs/ R, v# n! g" l3 k* T! _
$ cp crs-setup.conf.example crs-setup.conf
4 P( E) o4 x3 o, [% S- o* X' e7 m配置OWASP规则
7 s9 _$ p2 z6 w: `# Y, K- {" j编辑crs-setup.conf文件
0 @0 U% u# f9 [8 [) e$ P5 z) J0 G ?4 Q9 V) H
; I0 z3 a/ ?- u9 C: k7 t5 g
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
$ Q$ Y X/ x3 v! |$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
1 F" P$ }2 w( q0 |( v$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
p5 M7 V }( W% g# `8 G: [$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
4 _' ]7 e' S- Z# r默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
3 S; {9 q6 ^( |9 v9 F
7 v* P9 g5 [7 e3 e) B0 r: D9 W3 G- u( r7 x) G- Q
启用ModSecurity模块和CRS规则
! v/ m4 }$ @( T2 H+ A复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
3 {( Z* v) s0 h3 m
4 z; R. B, e6 T& A' L# n$ J
/ _- t* @) W* M" p! D% @, p/ C# Nmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。6 _ x$ f2 g9 Q* ?: M( z: p
, M3 P9 a' u1 @5 F7 F5 K, X; u3 ~9 N% D7 S y
$ cd /root/modsecurity-2.9.1/
# h2 s7 [9 u2 [" B- \. x$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
) J8 a* `: Y" K9 Q% [2 o: D$ cp unicode.mapping /usr/local/nginx/conf/
) |( W- ]: ?3 E! _& l将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
: G4 E$ s0 g' v" B; i/ g" s) S; Z: H2 h+ k- `: i" [* I
! E* Y! c2 r' y$ a) S( p$ vim /usr/local/nginx/conf/modsecurity.conf' ~' O) a# k2 `0 X. T" Q
SecRuleEngine On! ^. }1 p( R' f! |: p0 a
ModSecurity中几个常用配置说明:3 e" Z! A2 g, `( E
/ ]. d% A& p# q7 |5 p4 p
' W2 K2 G4 K$ ]1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。: h) q" T0 X0 X& }% [& ^
- E0 b# `* S" \ ]7 U: K4 ?0 ~" L- L9 m2 u
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
/ C1 N% Q& m- |! w
: | n& ]% q7 {2 Y" j$ E8 V
& M5 j; o4 H# Q, Y' n* U! x# B3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
3 n: V6 y7 l+ _3 u) g, r7 R$ L) M7 y
( ^! R( _5 G; t% N9 b: x5 l' J& l$ A! I& m
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
- p4 e- d6 E7 i5 ]# K# D; k% O# K, H( m: N
3 d% T4 u/ d* K: x- B. [6 e
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。- F- `4 |. h: n
& f- p9 q% n& ]) {* i5 i+ _
: r5 W/ I/ t7 M" a- t( [$ N+ o
3.x版本CRS3 b: P g3 h* u- b- S
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
6 C. H0 M/ C, \9 ?# {# 生成例外排除请求的配置文件: W. s, f( C4 `: S
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf) d' w+ Y% [+ Q$ p
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf% W3 Q& K4 t, Q: _2 c/ k9 R+ Q+ U
$ cp rules/*.data /usr/local/nginx/conf
4 r$ D$ H" L) N( M为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。9 ~2 c. }) J1 ~: [2 F
}5 O L+ b" `. X1 G' D# l3 L/ |( O% L2 Z0 {+ G; y' n C/ Z
$ vim /usr/local/nginx/conf/modsec_includes.conf
p" ?/ z" Y& I9 ]6 D5 L# y
8 J, ~" ?3 M' W! {' w) G[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 [3 X z! I, V" s/ g2 d2 ]- X$ H
* O3 s9 V' t5 G5 o" ]) g7 h注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
) ?: P1 w: _' B8 J; Z6 o
4 Q; C6 P6 s) q+ J# @6 ?3 Z
4 T, ?- v7 B; N, p" z9 D配置Nginx支持Modsecurity Q) l( G7 \: k {7 B, \
启用Modsecurity* |* Y: B0 I& c9 m" q6 X
使用静态模块加载的配置方法
. q0 |* B& x/ C; w* B2 a9 B在需要启用Modsecurity的主机的location下面加入下面两行即可:5 J! i L6 v( _9 B& z
& `# j+ ?3 W2 E+ U5 p3 R
& O0 J0 D; }/ `
ModSecurityEnabled on;& a- O+ S7 e6 s0 Y
ModSecurityConfig modsec_includes.conf;4 ~4 N9 X7 C. |6 f. t; Z& l
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。5 G/ Q) ^# Q& W: {* b7 O
! L# L4 ~- b3 [5 K5 y1 b+ N
; T! ~( O( J8 N; Z% l; x/ t( b# {$ vim /usr/local/nginx/conf/nginx.conf
/ ^0 O& i! @* z) M- r, Z2 |: H8 y. x7 j; @3 p
. P% e; q& L# M% X J8 f* t' m! P! I& [server {
2 | p3 ?: V/ g( {5 |' M4 o3 D listen 80;% e6 f. b6 k S( o) J6 |. O3 ]4 U
server_name example.com;: h; P1 X/ f# ?6 E- ^5 h
0 x$ [& n3 g2 V. Y) r* d
5 G0 L* M* W6 C: J) q! w location / {
- l* M L' Q4 [- K8 b ModSecurityEnabled on;4 T/ @ k) p _
ModSecurityConfig modsec_includes.conf;
6 ~0 z0 e: q% g/ M root html;4 V8 ]* K/ P3 j% J3 d( w# y+ {9 Q- F
index index.html index.htm;/ a) {7 g* q& t2 ]6 R& K" S9 ^
}/ H* k/ h4 t, @, r8 H
}
( y! `( ^- y# r9 l) }$ [ Q W使用动态模块加载的配置方法* r3 ?2 F# z9 h0 B( v" f8 ~) ]
在需要启用Modsecurity的主机的location下面加入下面两行即可:' a# W5 f1 N1 M k, [* R% s
5 K7 q2 ], M! _' g0 U5 g; l
& I( X; G# L3 o% C6 Cmodsecurity on;
8 O$ r: i! J1 i. r$ d$ tmodsecurity_rules_file modsec_includes.conf;
# |5 \" m2 d9 Z; I) m4 h6 v修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。: Q `& m9 W4 I: v2 t& }
$ z1 v1 K: h' h; R+ P; g# N
7 ` I4 E6 \5 I1 U6 c' f5 E
$ vim /usr/local/nginx/conf/nginx.conf. F% B& C4 |6 R. C
6 u' i9 j6 y# |* f- v u+ l4 W# H& h- K3 n3 V
server {
- V8 g6 o3 U/ s% R listen 80;" [; I; J1 K) H. x3 [& C/ Y
server_name localhost mike.hi-linux.com;
/ g' t6 v/ N& S& L$ s access_log /var/log/nginx/yourdomain.log;
! D% E, H ?- ]* ]5 O r- `/ I8 f( o. `: r! Y
9 W/ ~# j1 h# Y4 {5 \( j location / {
4 f5 u) J: r d4 A9 _0 M5 q: h: t0 @ K+ k5 L- v) i7 {
+ j" w( D, e1 S4 L1 w. p+ Y
modsecurity on;
0 k8 P2 N( a- z _# C$ i; J modsecurity_rules_file modsec_includes.conf;
1 q' U* ?& e7 ^* J1 W( c root html;5 E7 k) u1 L9 N5 K
index index.html index.htm;
9 ]5 P5 v2 ?+ b& U, Y/ M8 a}
% l* M2 s. O) v}! A7 u7 L8 O9 B' t4 a- e0 g5 g
验证Nginx配置文件
$ h: \8 O1 H w$ /usr/local/nginx/sbin/nginx -t! C7 V1 ~4 _+ I1 R0 c0 F
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
3 t# V& V3 `/ s+ Z. ?nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful( n$ Z8 a7 @+ e& y& A u1 f! A
启动Nginx
4 i5 H/ Z4 g, r7 u+ H3 e; \* j8 p$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf$ n+ e. L" a1 N. S2 |
) W: \7 A* Z2 A, w, \! T9 u& L$ h5 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 K; }% u( l1 o1 i- ~
|