ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
4 ^: G' ~! v. i( O2 ^
1 P9 O( T7 o4 L$ t9 n5 @7 [4 i _7 D. W' u+ Q6 |8 y7 t. G
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
+ ]% J0 F, K* O3 z" K7 t; \( `% ?3 B8 P' `. X4 u/ c1 [1 w$ _
9 `( {: R% B) Q' d+ M# b什么是ModSecurity
1 d+ c" s" }6 l' @ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
0 `5 ^+ I# F* P. W) m3 Z: h, J
4 L. `, M. o3 k& [) j( Q& `5 F$ U) t1 M* D* T: L, M* Z6 w
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
: w# |3 a( N; g& @' S; q u* L
7 i& j4 H( Z2 T; G2 g" p2 [3 N) ?1 W2 `) @5 D
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
) Y3 M# k) ^2 p
& U- h. N/ F/ ], m
' M; U4 ^+ D% Q' f4 j9 F官网: https://www.modsecurity.org/' J( _. d6 e$ r* C6 }
$ R3 Q" A# @ z2 v" S* ~
5 x* Q1 R! Z( V2 j$ K2 d1 A5 j$ |2 H0 H什么是OWASP CRS& E6 K7 _0 c0 Y. f4 M* `# S9 @
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
1 w4 w% d! j: M# H% G4 ]7 k6 G% k( F# o
* g0 ^& ?- {; }6 T+ lModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。1 S- b8 G+ {8 i. k# ^
. [& Z) |9 W, z
2 Y' Q2 _& r: }1 {HTTP Protection(HTTP防御)
8 O" h H! t# M) EHTTP协议和本地定义使用的detectsviolations策略。
% B$ N6 M, e3 l x' o
' v1 H- C% ~8 \: ~/ }* Q/ j$ u+ z4 y& r! t% i
Real-time Blacklist Lookups(实时黑名单查询)
; W. ~% Z/ a/ K( x) d8 C利用第三方IP名单。
: V% f0 _3 k% n; q& u1 F5 ~
4 g F# X j0 n/ q
% l2 |# ?; m1 e9 j, d- J+ O$ V ^HTTP Denial of Service Protections(HTTP的拒绝服务保护): i; F8 \) r) W) ?0 w+ n
防御HTTP的洪水攻击和HTTP Dos攻击。
" a; O; k ], \- ?& C! c: l" y" q4 Y4 w
. ` c% m# b. tCommon Web Attacks Protection(常见的Web攻击防护)$ T) D4 s B6 l! R
检测常见的Web应用程序的安全攻击。
* z2 n# f, @& \* Z( j
( P6 E( J7 `+ s+ C' {
' w! k3 `; ^2 w- I4 ZAutomation Detection(自动化检测)
. i9 t4 Y" Z y7 L检测机器人,爬虫,扫描仪和其他表面恶意活动。& e5 f0 D6 |/ F
a5 U, Q# e* _$ \1 ~3 E7 P8 p8 h2 M
/ q- e, Y; C# m* h! _$ t1 sIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
% f" g, o$ v$ _9 e( ]检测通过Web应用程序上传的恶意文件。
' x% \2 {( L, l1 g9 l; n8 \' \" r1 O( o( ?- u$ X: l$ I
1 G+ l2 h& g. `5 [$ C- V4 E: ?3 RTracking Sensitive Data(跟踪敏感数据), Z+ k x l2 i" _
信用卡通道的使用,并阻止泄漏。
" k7 F4 A- Y) ^# _/ E, {+ _: o1 h8 h7 I
/ a4 i6 _2 n2 n; L$ _
Trojan Protection(木马防护)
( W8 C# Z( O3 p检测访问木马。
. H% q( D5 I, v7 G, Q2 c0 O: K3 O" b6 X" }* X/ S' |
9 v, s+ o) f" c
Identification of Application Defects(应用程序缺陷的鉴定)+ u2 F. O9 R V
检测应用程序的错误配置警报。
- B. e+ b+ I, G2 a: L e0 s6 h! r( b% O. T0 V# a% Q# f3 j
+ N$ o5 @# n# g
Error Detection and Hiding(错误检测和隐藏)
p C5 Y; ]# r% M4 g/ g/ U! L+ a1 w2 r p7 y检测伪装服务器发送错误消息。' m/ }" q$ R* a# ?8 R
2 [4 L1 M% T( D( [+ n' H: q" |
Y9 r( t* @! H- m% d' I* O$ h
安装ModSecurity' I( z% X) ~# }5 ^
软件基础环境准备
( g. H6 x+ e5 C6 V( Z2 p. f; q下载对应软件包. G. W* r( K: p+ j$ v3 r
$ cd /root
# p/ c4 w0 E6 W5 _7 @2 |$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz': `* K) b7 \: Q% _7 G
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
9 A/ H9 K; R" z9 m安装Nginx和ModSecurity依赖包
8 ~2 k$ o* }; m8 d1 \7 ECentos/RHEL( x" ~' z7 H$ j* H7 ?; P. l
9 L5 f) ^- }4 C5 G# T; n
9 Y+ D" r8 Y- P$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
2 J6 e( a/ l; M. N0 T+ i. TUbuntu/Debian8 n o/ V7 N+ Q5 l2 `" W
4 \6 |' v5 @2 ?! @
6 r$ G S# k2 J7 n S
$ 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# M7 J1 l. J6 \8 ?$ D0 M+ E
编译安装ModSecurity0 @; r# \& ^, x( y2 G: a
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。! t h* b c+ ~" [; S9 B! ~7 @% C8 |
8 w# Y5 s, l( D4 y# R# u$ i
+ J2 c1 A; e, p& L* G. D方法一:编译为Nginx静态模块 \* d# s3 \9 P3 M
: {! T2 R v2 u# G- J" a
5 E3 ]) @, |9 T- ~9 b h编译为独立模块(modsecurity-2.9.1) K" @- x; t( @4 I. A; k
$ tar xzvf modsecurity-2.9.1.tar.gz
8 T" c+ {. R; V& _- _0 e$ cd modsecurity-2.9.1/" n0 q1 x2 d# V% n6 f% O
$ ./autogen.sh, O- D8 h+ R1 F% X5 u) }
$ ./configure --enable-standalone-module --disable-mlogc1 K3 V$ ^2 E& a8 e( [
$ make1 q' s; `" ^- J* p1 j" x
编译安装Nginx并添加ModSecurity模块
. x+ l3 ^: z, @/ K$ tar xzvf nginx-1.9.2.tar.gz" N+ p( _+ n0 |/ V& i+ b* \# ~
$ cd nginx-1.9.2
1 T' \ [% @5 y% o9 w$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/) a5 {, t! T) k" _ x' m
$ make && make install
# _1 a) M1 Y5 Q% ]方法二:编译通过ModSecurity-Nginx Connector加载的动态模块9 l7 c8 r7 ^5 \1 T; \
9 z0 P" U g, ~6 Y% `5 F: p% j) r- v1 @" q" \( f
编译LibModSecurity(modsecurity-3.0)
* _1 F- u |( m4 C. @$ cd /root
" | A( Z( P. C- ^% v( e$ git clone https://github.com/SpiderLabs/ModSecurity- c; R% K3 p& V! B
$ cd ModSecurity
4 F2 d3 E6 S, N5 h6 u% `1 `# k$ git checkout -b v3/master origin/v3/master
) Y1 u s5 F$ f7 ]$ sh build.sh
1 W% }: y' l0 A, L) L/ F$ git submodule init- g1 U+ R, Y6 e' j* W) D: m, q1 s
$ git submodule update
# L( l4 b) e1 \+ d' p! t$ ./configure" j' b) [ K) W9 L- f5 a
$ make
# A4 D8 i7 h _$ s8 Q8 j3 s: \$ make install9 W: ?9 g/ @! f2 `, S
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。/ ^8 |3 L( E4 f6 {, I
% ~$ Y$ ~% f! y% B" h% w7 }2 X, m1 b2 ~% H
$ ls /usr/local/modsecurity/lib5 h# K) t3 @3 \+ ]# U& w: |' f
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0: t }2 N* h c% O" i
编译安装Nginx并添加ModSecurity-Nginx Connector模块+ l- E3 e2 R- r5 C" V9 c
使用ModSecurity-Nginx模块来连接LibModSecurity s. q- j! g! G! u! v/ x! x |0 u4 X
7 E( Y4 x0 X+ U8 D3 J
7 i. V# u: w2 c, W( X$ cd /root" f& a# |; O) _) ]1 G$ r2 A
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx* ^; r- Z3 R5 a
$ tar xzvf nginx-1.9.2.tar.gz
3 ~: V' p' y( b. t m$ G. e/ V4 t$ cd nginx-1.9.2
4 E" Y( h M/ E+ R* x$ ./configure --add-module=/root/modsecurity-nginx
/ P8 d9 K& `! M# o; l0 t$ make
4 z& ~+ q0 `- L2 R+ |$ make && make install
5 ~ }- \: e+ S% g/ `, B+ u' q添加OWASP规则( Q$ |1 f& h% d1 C3 z
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
1 F3 r# t/ |7 C
3 ]& @9 f( R) @" K2 R6 f% o. e' _% \/ O
下载OWASP规则并生成配置文件
8 x7 p% }, X0 b- I( d* x0 K" l$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git, Q4 J. I4 P ]' A5 S% `9 H' A
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/9 C& u2 ^ {( l; w6 Z
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
1 D/ y. ]: q- H' M k1 g$ cp crs-setup.conf.example crs-setup.conf) {, L! o0 B9 |0 @9 j
配置OWASP规则6 t% r9 u) _ J @% M1 l; v( ?. S
编辑crs-setup.conf文件
8 Q$ t, P) \7 D5 S- f. T; o6 Z' \! v! a+ ]5 \! x
: E6 a& ^& U$ P$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf9 L+ w- W1 E ]6 s3 J
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf# N# v) u; G0 M# Q3 N v9 n9 t; w8 [
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
3 L4 v* j1 J; Q4 B5 ]3 Q: ~$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
3 J a7 {' [1 r默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。6 u" w- [8 T/ b. S& u! @8 Z5 |9 r
. v$ o! O0 [, ^
: d U) Z8 y5 w启用ModSecurity模块和CRS规则
0 ?- a! y7 Y! {. e复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
1 ]! P1 S% T: i0 J# z8 Z2 q* k# W6 D0 l7 t+ T
9 K( j% E" l" H/ ~7 kmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
0 s, s' v5 o/ P+ n9 M/ v# @! G! W" {' Z {# j% V
: M4 f% _. N( \! C2 D) g0 R4 z
$ cd /root/modsecurity-2.9.1/
* Y* h* V' R1 v$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf ! C8 z0 B3 T: q ~4 u! ~
$ cp unicode.mapping /usr/local/nginx/conf/+ `$ |0 r0 B' l
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
# w6 h1 E0 L8 v- a. U; Y+ @4 R' B
( n( n. C$ F1 W' S0 h% K r
- [% I8 |! o$ H. R$ vim /usr/local/nginx/conf/modsecurity.conf/ d# `+ \6 | Y/ o) y
SecRuleEngine On
' q1 ~; g8 j( o2 t' L( aModSecurity中几个常用配置说明:
- | L* l+ p: ~. W; x" H" A* B/ }$ @# B1 j9 k
6 E; r! F. M7 w! E5 S
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
/ b7 c& ~0 ]1 W) t4 b2 C
0 e+ V; i" k% J2 r- D
* }+ N8 k2 H+ A: Z# Z2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
X0 o4 u7 u. B8 s" {3 j) q. m" M
0 Q- m3 x4 U% M4 E h: y7 I# L. w* d! G" x, m) z
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。5 N1 ^4 o, C( \1 H7 M( X
) H- O3 _1 M) H" } U% A
8 N" D' {/ X# ?' z, O/ L- v& n4 }4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
* X" a7 W( x: F9 B K' i% I5 o
3 p! h* F( J/ Q4 T: q& T5 d8 s) @" @; S
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
* G# Z6 X1 M' Z( h3 ?# c9 y8 I! i5 i4 j. T# h" u$ i a8 G
6 M0 V1 y: g0 \! a
3.x版本CRS( _1 c5 Q$ x9 N
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs2 @4 U# A9 b( r0 o1 S
# 生成例外排除请求的配置文件( K3 s4 F3 ^2 Q$ S+ }1 h. D; F
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf2 y3 R* ~* `7 H5 B$ N& P/ c, _
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf. T/ W H. F4 L9 w4 M% J% ^
$ cp rules/*.data /usr/local/nginx/conf6 C, U5 g! s# l( l
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。5 X7 }4 i* K$ ]( ]
. u7 s& E* ]& r$ c9 {4 A* y
5 @* F! d) f/ A- C; h) U8 t$ vim /usr/local/nginx/conf/modsec_includes.conf
7 X1 T# F6 o* o
) l( I: Q5 |3 f; i( n2 o0 |[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
& K& E. @# W. i7 i6 h$ v( ?/ u8 H2 }' }$ O3 w
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。6 v B! O' c/ v. N
; B r6 `+ S- J3 I$ X6 c
+ e6 S H4 f2 z
配置Nginx支持Modsecurity, O0 I. ` g' ?' E. ^
启用Modsecurity9 T, {: O4 }: n1 T
使用静态模块加载的配置方法
# K/ G( l7 | ?在需要启用Modsecurity的主机的location下面加入下面两行即可:+ \/ u% u! t5 a
' i. n( M" A5 z& {" ?9 g6 y( H! s- C% k# _( Q7 D* @
ModSecurityEnabled on;
# ]3 I4 K' {/ j+ S( K& LModSecurityConfig modsec_includes.conf;' a' R$ w% h4 ~/ ^" E/ a& u
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
6 K' |4 M9 z5 x' `" }' g
" s4 N/ v3 |- v4 K. v
6 p8 k2 N( U/ B# H$ vim /usr/local/nginx/conf/nginx.conf0 z; U0 x3 }: | |' m3 [! ?/ W* y3 ?
, n( G4 s! i/ x' ^4 Y, a7 c
. W3 Y2 b5 F. U2 \
server {; e* R c* d$ m# ]
listen 80;5 N# p9 l& K; @- }% Y3 e
server_name example.com;
9 z, n: Q* q2 R8 a( e- X' Q' N9 L' t: m' `; R5 B1 g
7 T3 q; K! P7 @/ V7 j" i
location / {
7 e/ i6 {# g, U2 f* W+ }& K ModSecurityEnabled on;- y6 t" ^; O* a! B/ N- ?
ModSecurityConfig modsec_includes.conf;# n2 Z: _, @8 G
root html; _: }, G# ^; { q6 F7 E" v/ ^" d1 A
index index.html index.htm;* q$ N5 V4 A1 ]& L J1 Z$ x
}
0 y3 o) ?% ]; f7 p0 r4 \}
. M4 t* K0 a0 U" g8 S" F) x3 j使用动态模块加载的配置方法 n# M. }9 z- q& h; \! R i
在需要启用Modsecurity的主机的location下面加入下面两行即可:( G5 k y8 w" }- y7 B2 Q9 d5 {
8 T3 d$ T0 E9 U- z+ u4 \: J: k9 S- h
1 G; [" b& W/ @9 J: w2 |! }modsecurity on;
# M4 l7 a2 x t( U3 W7 {modsecurity_rules_file modsec_includes.conf;3 `) V9 B$ p& p6 B, s
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。6 @4 d7 R( t! d( h3 T0 e% f, @0 H9 [
. r; M1 L9 `- q w6 v; L- ~
5 c/ A1 t: [. [9 d$ vim /usr/local/nginx/conf/nginx.conf/ c. |2 V, }9 c/ t
- |, i( [" F* w
! _+ v2 C R3 k; O2 Nserver {+ O# J, m5 |. x3 g) f
listen 80; P6 G2 [/ |: F* y. _/ E2 M
server_name localhost mike.hi-linux.com; j" j) K/ Q& \
access_log /var/log/nginx/yourdomain.log;0 q8 ]" u; R4 u* ?) k
$ R7 m% |! g$ I- O
& M& K0 r: p! }% ~/ W
location / {
. f. e+ P* U6 {$ W; `. F
7 H% h7 l% T! L% U d) W4 G6 ?# ^4 A
modsecurity on;
8 [: h. X; ]' I# Z, G" M7 o modsecurity_rules_file modsec_includes.conf;
9 c% l6 J3 h) s# }1 T, k0 | U0 f) @) D root html;
, e# b, t6 R+ M5 j+ Z) | Z index index.html index.htm;
. \; b0 S( Q4 C3 I/ Z0 z}8 N8 b) ]0 j: V, j0 P) n
}
% a7 l2 d5 J Z' C+ p# l验证Nginx配置文件6 m5 T5 K; ~ y$ j. R( Y
$ /usr/local/nginx/sbin/nginx -t6 u/ m. d- R9 c6 K
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok4 E! `6 U, B8 p: U
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
& M0 W) X' T- r2 \' v0 k启动Nginx* S( |' E, i O# |8 `0 ^' [ l
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf }, v! k/ Z" U9 ^0 x, m1 l) s
& `8 B X% V, d5 N2 {测试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能够战胜更多复杂/未知/混淆的攻击模式。
' z9 v% M9 [% i6 u' x |