ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。. l% s' n! M6 v* X9 t
7 k2 J( F5 W) z! j# Z2 j; o0 k8 p5 S: i* U& Y5 F4 i. g9 L. ]: O1 w
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
G' `9 x# |: m+ Z0 g& y/ |; [& r8 Z2 U& k, }
1 d- j) [3 s j- \. \
什么是ModSecurity
) s a8 ^8 p A4 _+ [- ` LModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。+ A8 r1 ]$ z( f% W( J) A5 ]) Y! s7 g
: S$ Q, q6 t) L3 k0 k- G8 _. l! f$ z, o. x' B8 Q/ _$ l
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。- m( G. K6 b9 @4 r! Y. @/ ?
7 ~ i* Y& H" h" U2 j' H9 F0 O5 S
( v$ s4 I6 c2 v. f+ M4 Q! SModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。+ n& F/ T3 v( x# x4 @
7 p& T6 L" x0 }0 G
9 V7 r4 d6 h$ P% J
官网: https://www.modsecurity.org/+ f' V3 L2 {# P' ?1 b
5 R. ^5 K9 F( p. J' m) a/ {8 ?9 ]. L" G& F
什么是OWASP CRS& `7 C. M1 o! s( m. X
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
9 X8 W' C) z; S- a4 y' [$ d
$ G8 d( l; v& _0 Q. ~0 \( J! b% M: g0 h" h! e; W
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
+ }' m- _# e5 c( \, C! E: c- L# f
1 h7 W0 d, `0 q' O3 v1 J# B/ W2 n/ W/ }& O
HTTP Protection(HTTP防御)
7 P( x# H; F( j& T! A# W5 ?HTTP协议和本地定义使用的detectsviolations策略。' q2 Y. k( p U* F
* y# B) k: u$ T* @, R
1 D( a% `4 S' N1 _- ^8 JReal-time Blacklist Lookups(实时黑名单查询)
: Q5 m+ d! \; J利用第三方IP名单。
4 h# k- n3 Z( J5 G
4 H0 H6 v6 K8 V
! \. t% l; K, r5 e0 SHTTP Denial of Service Protections(HTTP的拒绝服务保护)
: f$ O& w( O) _7 J2 X4 _( d& ^防御HTTP的洪水攻击和HTTP Dos攻击。
# P" J1 Z: t5 a8 t- S# p
* ^% M/ \6 ]/ c1 \ O: l7 J, k$ {' b' X
Common Web Attacks Protection(常见的Web攻击防护) B2 ~0 r p/ c' x% [
检测常见的Web应用程序的安全攻击。
9 s2 X' r; L+ `5 Q7 B* f9 ?# T* d2 a: j8 t- Q; x$ {
8 \, p- b* P6 J+ _ SAutomation Detection(自动化检测)
$ K$ M3 C) ]2 A5 |检测机器人,爬虫,扫描仪和其他表面恶意活动。3 w5 m% D4 ]1 {% R9 M; C5 p/ N
: ` p( z, d/ L, q$ Y4 y# f' [9 W( h3 l3 O* R- @4 Z5 z
Integration with AV Scanning for File Uploads(文件上传防病毒扫描), r5 T. l0 m6 H
检测通过Web应用程序上传的恶意文件。
. O9 k6 X @0 U; E- J0 Y" G: I
3 {& t9 e* X* u' f8 ]2 P& \
0 F. m& Z& u8 p( R" `- GTracking Sensitive Data(跟踪敏感数据)
" U. v8 y8 _8 Q信用卡通道的使用,并阻止泄漏。
* S+ X% W2 B2 D$ C- G# x8 l9 x5 F, F7 f6 ^1 G
% g. j: X5 p$ ^; T8 ]Trojan Protection(木马防护)7 r% \3 E& f% _& s$ f
检测访问木马。
; N1 N# J4 }0 u0 M) W. A$ J L5 x1 N5 Z$ \, e: c) h2 m
1 d; n; `/ m* T- TIdentification of Application Defects(应用程序缺陷的鉴定)
; y( ^! p' j: O0 ]检测应用程序的错误配置警报。( A" x: p5 d: |1 A% H" ?
! Q: t& @( W" x4 `6 n2 ]
. q* x' W- d6 c7 G
Error Detection and Hiding(错误检测和隐藏)
% e0 m ^( _% X9 @ Q检测伪装服务器发送错误消息。
: z" o3 w1 A; `7 Y; v
( _5 S) c3 Z. z ~* c% y/ q. R' R5 ?7 g( s
安装ModSecurity( L# r" }1 X( P+ R4 t
软件基础环境准备
$ C7 z1 I7 X8 V下载对应软件包
- E( j. M, ~2 V5 C, l$ cd /root
0 Y* O5 m, p( f9 m* A$ e+ h$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
/ g0 j" a; w& W% h5 \, C- T$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz* e9 C! w- |+ v. O
安装Nginx和ModSecurity依赖包% M% d/ v; { B) T* q; u
Centos/RHEL$ K- i) x4 ?+ N: d& I# a2 w
& f9 W! m; V& C) Q6 N' u2 I8 l6 G e; S2 D- b4 |4 g" J/ \) H- {
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel, r5 M7 N& T9 o! ]
Ubuntu/Debian5 Z$ T2 W/ |0 U: s2 ^& P$ q
' S8 e u& H) v9 V, D* i! n- C9 w$ z2 ]) a4 D! e' i9 C6 R
$ 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
# j$ F7 C- E7 M1 P5 I编译安装ModSecurity
: l7 n# E6 w4 e+ tNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。- r; g6 T8 i- c8 a; F. w. M+ V
/ E$ I0 v/ f$ H( e: ^1 a
7 m2 ] x" j& Y4 b! W: V& i方法一:编译为Nginx静态模块! H0 h" d6 ^6 q4 A- H5 a
3 [( U$ n, j6 b& d* @" ^
; q# D L3 a% y) M& G编译为独立模块(modsecurity-2.9.1)
* R2 u2 Y N8 b$ tar xzvf modsecurity-2.9.1.tar.gz
/ [8 N+ G0 R; f3 b$ W/ H2 L8 t2 H$ cd modsecurity-2.9.1/9 M. w# N3 y+ r7 r
$ ./autogen.sh
% e7 I# G5 l, M; p# L8 W6 T4 x$ ./configure --enable-standalone-module --disable-mlogc8 A& T, ] a# j$ V" _8 y* o6 o
$ make
" _( K/ c7 Z4 M. |编译安装Nginx并添加ModSecurity模块1 x+ D7 z& b# Z; S
$ tar xzvf nginx-1.9.2.tar.gz
: g# o; l4 @4 K7 Q$ cd nginx-1.9.2
8 l8 B( K8 R! T' Q9 `; I8 @9 L4 a$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
3 [: Y2 D- C7 j" H% }% n+ b$ make && make install' q! P& S6 `; M! M+ K* p% l
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块; g7 @) f7 n; p+ W) h5 _" c
, Q k8 r/ m9 n; `- B( v2 h' S3 F& ~/ F: ^9 s
编译LibModSecurity(modsecurity-3.0)
' N# m+ M; h% n+ X4 m; Y$ e$ cd /root( ?) ]5 M, `2 }) |5 w9 H" q' C
$ git clone https://github.com/SpiderLabs/ModSecurity
* W8 I- {+ E# E% t8 e: p* S5 B2 \$ cd ModSecurity7 H. B+ {9 x6 w1 q4 i
$ git checkout -b v3/master origin/v3/master
3 d* o0 M8 e, H9 j$ sh build.sh$ y0 j6 `- H+ x% E9 r5 r# x2 j
$ git submodule init
8 V3 J& [# d8 c6 I$ git submodule update
& ], `5 |5 k8 ]: ?9 I- i: x$ ./configure6 a/ s# p, E+ F0 @$ A. w
$ make
! e$ [( c0 ]9 z1 y& ~$ make install0 W8 Z, u, G, `
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。. \ y# O- }2 `
' d1 f0 ^% b3 E
) `, r; M1 i2 `+ c# O% }$ ls /usr/local/modsecurity/lib- v% `4 J2 B! S' w
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0& w& K4 C2 h9 @
编译安装Nginx并添加ModSecurity-Nginx Connector模块! A2 @0 z. K4 z, P+ t, _
使用ModSecurity-Nginx模块来连接LibModSecurity1 H* I3 W1 h8 I! A: ~7 |/ g
: c R: _& z, L* Z9 n
/ A* [6 I1 _. E* u$ cd /root* ]7 I4 G6 E' i$ v( ]' @
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx& |& K1 |3 t: ]4 x a
$ tar xzvf nginx-1.9.2.tar.gz
( w* c* Z% A& O8 Q( n$ cd nginx-1.9.2
* u4 U7 y. x \$ \! l6 ?4 A( h$ ./configure --add-module=/root/modsecurity-nginx) n/ Q# d/ f! m- a- t
$ make
6 v3 w4 I- b% S( D" W& w {, C6 R$ make && make install
3 Q4 e, l3 W- |5 K0 Y) K, {2 o' k添加OWASP规则$ I& I5 G g( C' u
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。) s* b; d" ]3 g2 e- h6 B
n; S& \; }1 y- T U2 @) H. E- B" e+ e7 r
下载OWASP规则并生成配置文件
% Y1 d' I% _# V% A4 n$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git' U- L& c4 R$ ]6 ?) u
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
7 l# ^2 z; A; I$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
5 H. a: P: q" k% Q) j$ cp crs-setup.conf.example crs-setup.conf
0 @/ w6 ]% V3 H: b( r4 }' N& E4 U配置OWASP规则6 v# A" A! [- P5 t% B. k9 D
编辑crs-setup.conf文件
; `2 Z' X2 ^) G' V: |
! ?6 {+ X( h Z7 V
" x8 u% G8 C3 s$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
" S# `5 ~3 u9 \$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
3 a# O: ~0 y; Q- C+ d d8 k: p8 n$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf& |$ k, _! d% f+ h( Y
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
" ~' M& O3 w' }( }; F默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。8 i0 b" W) s/ y
7 B2 D( f/ ^. y. p7 n/ D' k7 g* K9 Y% X C
启用ModSecurity模块和CRS规则
5 |+ h! N$ d6 B3 S, U: K复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。. w, O( f1 O/ w' Q5 f
" _) U. D: o% h9 y- ]% q4 [5 D, X+ q, {
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
$ ]/ V a! p# V, y4 b) i/ T7 { N4 b; R" F2 O4 h- u& Z4 g b
- K/ e8 B C( y6 r- c, u$ cd /root/modsecurity-2.9.1/% {$ y# S- {- G" z
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
, R% \( I2 n$ K% m4 j$ cp unicode.mapping /usr/local/nginx/conf/
) z# Q0 W# o) k( d6 M将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。; N2 @4 M9 _7 ?; P, M+ J
) Q6 N% p( W; G: j; [6 j; m
. R* P3 B6 k2 K* |9 U! w! d& S$ W( p
$ vim /usr/local/nginx/conf/modsecurity.conf+ B$ V# F7 L9 q8 t/ T7 }* f% `! c
SecRuleEngine On
4 w" E( X6 T8 P+ ^# x' @, YModSecurity中几个常用配置说明:, X9 e- n# _, `3 A
, v7 I# T9 R$ c9 M& q) i. n' @% H- R/ |
3 ]" ?; X& }0 O! M1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。, V4 N+ I$ T1 v( c* N
2 S+ d& Z6 ~* n. O, x$ S5 n k
9 P+ \$ `. o4 ?% F
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
8 `. T" I6 d0 S/ E2 j1 `/ s0 \) H$ d- n/ Z9 M6 ]5 ?
$ z. S, g* c; z! `+ U4 c5 {
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
! x$ Q. z2 U. H+ k0 I+ }: f# a/ c6 n% v1 E# i* P" N
- t5 U& J( n0 l/ G: I+ D! p+ n
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
. v, n: q* U! N# V3 x' _+ k
5 u7 B5 m3 x7 r
7 N$ e) Z6 } J+ J( h在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。) I# W# f7 J, W9 e, ]* a
4 G3 a. o1 b+ [
, o1 e, _3 h# j- {4 V$ R! u- W- i7 Z8 E3.x版本CRS7 ~7 H' H' N: }" k* F; T) T! T
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs0 u+ j2 v7 t/ L) D" d
# 生成例外排除请求的配置文件
6 ?$ I0 T) a* w D3 P$ i$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
* n0 y/ e: S* ~3 _; b+ l6 N$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf1 C+ z M0 |- l1 `( S) U* `
$ cp rules/*.data /usr/local/nginx/conf
# P+ e6 p# y5 m0 N为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
' l R, |. a6 r& d/ N$ L+ l
7 i* ^/ V0 ?' Q" _5 a8 G6 L/ L/ \# u3 O% e* F
$ vim /usr/local/nginx/conf/modsec_includes.conf& u* O) [2 S- A6 g ]% Y6 y2 s! S7 Y
9 D. M- { f9 _% u# u4 q5 |
[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
) ^1 T$ m5 h. U
( Y/ x- |# y {3 U% H注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
' J* O2 ~( j) K& A# ]7 I% K3 a, K" b* ]) F
3 |) Z4 n$ B1 ?! s' `0 X, @& A, f
配置Nginx支持Modsecurity
0 o& j2 Y1 r( H启用Modsecurity3 ~) {, P3 S+ N; O
使用静态模块加载的配置方法( O! m: N; p% N# G
在需要启用Modsecurity的主机的location下面加入下面两行即可:4 m; B5 L8 t: v
* P: ]9 y h f+ N; F- {
3 ?! R: b" N. Z9 i2 U% M1 uModSecurityEnabled on;! R' e+ m& A/ n. ?: q
ModSecurityConfig modsec_includes.conf;
" y* x+ o, d9 V1 K$ {7 c* q. V# E修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
6 @3 l3 H' g: w4 ]7 U- i+ k5 ?9 i' M- l d* _8 V- L: {
3 D1 t# y/ j3 z) p$ vim /usr/local/nginx/conf/nginx.conf/ U& l4 y& A" M3 x9 x2 A
' o: @2 ?8 i; @7 N
3 H6 o& B8 f9 ` _
server {. c: b h) w1 J- v J0 D f. d
listen 80;% \8 o, i$ ^3 } F5 T4 N( N
server_name example.com;
& s" M. U# t: S, H" z& b2 u* g7 o( f* S
1 o+ ~( f; B' y* [6 c" X location / {' m0 e2 M" h5 g- F
ModSecurityEnabled on;
2 @, G; ]! U7 K# W- w0 z6 \/ o ModSecurityConfig modsec_includes.conf;: N" P5 Z( K! z, C% `9 E
root html;
8 c3 I$ N7 c' H) B }6 e index index.html index.htm;% R( j7 n$ H) w! r) A; A5 s
}5 C, ]" |2 v2 R" r* n# c
}& o1 i/ r2 f! W* d6 s
使用动态模块加载的配置方法
0 E) }6 X* ]2 ?1 ]在需要启用Modsecurity的主机的location下面加入下面两行即可:0 u+ E. w! m7 c0 w
0 O# g4 ]% o/ B* V: N4 b, U
7 n' v4 F1 h# n2 p
modsecurity on;3 n$ m- a( O, J$ p( N7 ?5 T
modsecurity_rules_file modsec_includes.conf;9 Q% d+ B( ?/ Z( |8 ^
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。/ H2 \' L! D. E) s
& c6 ^8 s' t$ m1 i' S' A7 b
) X/ \3 P& M+ w1 Q. O! X. \$ vim /usr/local/nginx/conf/nginx.conf
1 w) ^0 {0 }0 s! q2 `% M, @+ H( \
* J; w, A; z( G% m: V1 Eserver {* ?: U$ T, r) P8 i$ R1 G+ m
listen 80;
/ G& ^; o4 ?$ L3 s7 _ server_name localhost mike.hi-linux.com;
. Z% I6 S* c' T! K access_log /var/log/nginx/yourdomain.log;
! c* [! J, g* j9 e; B: Y1 ^# U9 b& K8 b) L' [- ]4 X. ?
6 N% ], ^2 Z+ D# k& E$ m4 w# n
location / {
' a: L+ X3 F& m P/ C) h6 V( ^; e
) K! v5 b/ J" ^# G2 W$ U4 t! m
& e' _* K7 d4 c+ U modsecurity on;
5 J/ z- n0 [* K modsecurity_rules_file modsec_includes.conf;
! o0 @7 ]: G. z root html;+ h1 z. [3 q9 h7 j5 S
index index.html index.htm;
- Y& ~, ?% a. e% e4 W. m}
3 b8 P7 U# T- o2 P" H5 ?}) o U/ X0 F, f5 W* L) P
验证Nginx配置文件- v$ @. c8 x6 y% C5 {
$ /usr/local/nginx/sbin/nginx -t
0 L( a3 X( |, S. `nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
- T- y# l& X: T/ D6 Onginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful) P C7 o& I( w0 `# W
启动Nginx
" D2 u; v: \5 B I9 J- R3 b! L& m) {$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf0 M4 H7 n& N9 G
; {, `9 R h; ^$ S/ @5 F" `测试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能够战胜更多复杂/未知/混淆的攻击模式。 8 M/ R7 I* u; v0 x. ?+ i5 S9 ]
|