ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
/ A# M4 E% @4 a
' F, f, `- O; L/ F1 H5 U; `- ]7 F
" B: _2 C o. ^- a% h在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
" U: |9 t( a7 P: X5 N3 v3 W' o. |+ O3 J5 z2 z+ L% {
) Y# q: X c5 ^8 J# A什么是ModSecurity
: p1 x1 J% \: y) ?" e6 I3 m$ gModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。, d* Z0 ?- q. ~3 j& y+ ?; l
' z0 |6 U6 i, ~ w( z
: {: G$ o( B- } G
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。7 c' f& k6 n8 O# @* e) e" P: S
( x1 ~/ V0 h. h4 R
% R' t5 O" W3 I. c( m: X
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。; p; E' @/ ?( e( ^$ i/ O) c( o
* N+ a1 K8 ~- L9 _ Z
- O) |% ?5 W8 f$ M* h官网: https://www.modsecurity.org/
0 I D' ?" \' n% ]: t$ V' m3 E
3 \) B9 W0 k# ]# N/ X3 S+ n7 Q0 P5 [# A a) u
什么是OWASP CRS
# Z2 g4 M* a3 b3 D) s4 Y$ xOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
+ m! H4 g6 ^ u- ?; `
9 M% V, g. m+ b- t! G |& H
1 T. x* w! k+ g: DModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。* i2 K3 t3 ^- g6 p
$ E% O$ h$ q/ g% i6 L& n
; s2 S' K- W) h0 U
HTTP Protection(HTTP防御)6 u5 {6 p1 r1 S: V, R3 @+ W! A# f( Y
HTTP协议和本地定义使用的detectsviolations策略。% Z) v% Z. d x
" R. E5 K; l$ X
" B, l4 _2 [+ r$ N7 RReal-time Blacklist Lookups(实时黑名单查询)& P: b, k6 x( C/ k- X3 O
利用第三方IP名单。
& w: U9 y2 V- Y. l! t& V
- Z. F5 U8 {0 ~
& M7 r5 A7 p, h$ e& ]0 E0 j' PHTTP Denial of Service Protections(HTTP的拒绝服务保护)( f: j; T; Z* b
防御HTTP的洪水攻击和HTTP Dos攻击。
- z3 T N: i7 F, w4 L2 A: ?2 k5 b }) Z( `
7 }8 {( I6 v) M( G/ z# p% xCommon Web Attacks Protection(常见的Web攻击防护)7 s( O8 ?" }) v. b
检测常见的Web应用程序的安全攻击。
2 w3 D8 a+ c2 P; E) y, ~
& w: c2 J! b5 G1 u0 o. X# u# c8 s- T. H- s
Automation Detection(自动化检测)8 P) [9 w2 B; e: q/ {
检测机器人,爬虫,扫描仪和其他表面恶意活动。
0 a A$ G- G" G# [
7 T+ ^9 w) u4 f. U: h' Q( L$ A: X! W# J2 ^( Z( ~8 g L
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)2 @; F( N* Y" C/ ?* o, S
检测通过Web应用程序上传的恶意文件。
' {+ s$ T4 u' o5 D9 @$ t7 N/ \9 j8 S
8 {6 V. Q2 ` d5 M) I
Tracking Sensitive Data(跟踪敏感数据)* R- S5 Q! O/ e+ Q/ Q% j: i3 p
信用卡通道的使用,并阻止泄漏。
( o3 [# m/ |$ M0 _1 o4 k; {" ?
7 Y* q$ h( e4 E) R7 @- N8 N# }0 M# R, \' \* Z6 A* x3 p
Trojan Protection(木马防护)
3 C; S# a5 Q8 d/ a) C+ x" Q$ ~检测访问木马。4 G n, o2 w) v3 g- q, I# V$ Y
- S1 C( Y- Y7 A9 R( A
( S- B- q# t3 D9 f. ^/ I$ w
Identification of Application Defects(应用程序缺陷的鉴定)
7 S+ |8 L+ d' }8 r检测应用程序的错误配置警报。
8 d, j% x" z* S/ A3 T# X* k# j% {' n& Y. z" p: j
) t1 S# Y+ l& Q2 r- }
Error Detection and Hiding(错误检测和隐藏)4 |' i# Y$ p/ v
检测伪装服务器发送错误消息。
$ H, b- d: q1 y% x' m; H, m
/ P' R: G: V7 o/ Y
# `' R+ a6 d1 G- x1 K2 {& `6 b安装ModSecurity5 m K8 C0 Y& u, v
软件基础环境准备" Z7 u" W2 B7 I& o' D5 n
下载对应软件包
! f5 \$ r2 t; z( B$ cd /root! X8 U4 y1 S6 h1 n b! l; ^
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'. U c8 A! l! @# [* v
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
* r0 ]. G `& h d! Z. u. m& t! \ W安装Nginx和ModSecurity依赖包
- d/ b4 L `/ ^' r6 tCentos/RHEL
1 T3 n4 o- W: [* s% Y: w. l' A
' o" O0 ?- t; I
' q+ J" r# q5 r$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel' l( Z) U( s1 x1 b8 v
Ubuntu/Debian" N e6 Z5 _! v2 u) B
" ~2 V1 P' [8 m- r2 N: v# w- A. G/ T7 @. ~
$ 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++-dev1 {* a: ?9 ^8 {9 L
编译安装ModSecurity! y) e6 p: q# o$ S+ R( G4 y
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。& z9 r0 R& T( v* S* e9 U
( C2 ?3 @* l" u' ]
0 e$ o4 w8 W2 a1 M+ X5 Q8 M$ Z0 W2 f; g方法一:编译为Nginx静态模块( S# s5 e% ^, J2 L0 S7 R
9 R+ W% j4 U7 x' M, z$ S/ k
* X" D# C! @9 ~( \& G8 b% d3 @
编译为独立模块(modsecurity-2.9.1)2 ]' ^2 l, l- R. V- u0 \1 e8 f
$ tar xzvf modsecurity-2.9.1.tar.gz
6 S( G- k! L r$ cd modsecurity-2.9.1/
1 A/ k t. U: R( r1 [/ b$ ./autogen.sh* A: \9 y* x. i- w# }: D/ l# _* l
$ ./configure --enable-standalone-module --disable-mlogc7 ]4 ~+ O5 h0 n# z" T. i/ G
$ make, l" k$ J. k. A. E; ~
编译安装Nginx并添加ModSecurity模块
$ j" U8 A. P8 R0 C$ tar xzvf nginx-1.9.2.tar.gz
6 G$ k; F) t9 Z$ cd nginx-1.9.2
9 D1 u( ?5 ^; k& y$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/# A- ]; \7 ~. ]. m! T% g- T$ o
$ make && make install
1 k1 [) a# T( [5 o* l方法二:编译通过ModSecurity-Nginx Connector加载的动态模块* v: Z$ M$ n/ ~, ]/ c' ?" J' I7 M
+ O4 `8 H( c" ?1 B
1 E; W' m! r x( @' e; w编译LibModSecurity(modsecurity-3.0)
5 R; @- g3 r' B; P; c$ cd /root1 g' U. c! M" L3 b- p8 [9 T$ y
$ git clone https://github.com/SpiderLabs/ModSecurity
; Q% g- x- F a$ cd ModSecurity) `' h ^5 U+ A6 A; w
$ git checkout -b v3/master origin/v3/master
4 Q1 p7 R$ h, v: l( i* f! N$ sh build.sh
6 F2 s$ [% e0 D* j n0 @ u$ git submodule init
) m5 ~( j0 a! {1 d$ git submodule update
% q3 {( p ~1 @ I, m3 @$ ./configure- }! z9 O' j' \- s
$ make
. k" D' Y, W. ~$ make install
9 d% l$ l/ [/ I( ?% [; f) T# Q* kLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
4 g( x5 S j$ u _! ~* T" g
?8 [, ]7 g7 v' W2 H5 y
% x8 W4 [$ H' g7 A+ a$ ls /usr/local/modsecurity/lib u7 _7 ?$ ~0 K# P
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0) e( S. }. U/ [7 q) `( |* O
编译安装Nginx并添加ModSecurity-Nginx Connector模块
8 s U8 O+ R) D! c" k/ a使用ModSecurity-Nginx模块来连接LibModSecurity$ r2 Y1 T6 r% \+ ^" B7 L' h
7 f" F% W' m9 w2 y' f/ D. q ?$ }( C: ]- b- h* \, x: a( \- ?
$ cd /root
2 D4 O2 L5 X% X# J- l7 P$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx1 {. D1 y! K5 X" j# y5 I7 }2 [
$ tar xzvf nginx-1.9.2.tar.gz
+ E& _5 n3 I$ i$ a# }6 K$ cd nginx-1.9.2
* x- o4 A3 E8 @- m3 O$ ./configure --add-module=/root/modsecurity-nginx A) @ u5 f8 T% s* |
$ make# b; g+ W/ y w
$ make && make install& A" t6 b2 P2 Z1 u* w
添加OWASP规则# `( |& y g: P$ N& U4 R+ r6 n, J
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。2 \# w2 n( f6 e
- a+ |1 T# N% a; [# r8 S2 I9 ^: Y: v, i% D; O3 S& G4 u
下载OWASP规则并生成配置文件
! y% v+ t, Z. w3 O. ?$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
" V( q2 L; j+ ?# j! U8 S; J$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
: R! l3 C6 g- I: r# b9 r$ cd /usr/local/nginx/conf/owasp-modsecurity-crs- j/ f, y- T3 ^, q& [- k2 v
$ cp crs-setup.conf.example crs-setup.conf
, q$ @: h& s8 h$ N配置OWASP规则
1 o+ A1 D) H/ Z编辑crs-setup.conf文件# i: L. V: D0 p
' \6 ]0 S7 S- x( r: B+ J
F% C: O$ r, l: j3 n6 Y- {$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf3 c& F0 f+ J! n4 s$ j
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
$ A2 j; F4 z. G( _8 Z* Y$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
0 x ^+ H" V5 s- I, k2 f$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
7 {, X6 o" ^' Q8 k" a4 Z9 i: q默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
! u" B3 H. b, A$ J+ M
3 V+ T; q$ h% Y# a% g6 B
% X; Q) |) \8 C0 O: o) x" ]启用ModSecurity模块和CRS规则6 } B* Z9 I7 a; m8 }" o
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。7 \1 c! x+ U' _! \5 \% E5 m3 B
# [7 a# C2 |+ a# m
4 t5 z0 R! e& p8 w* R: H! ]
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
8 m! T. i" y. C1 W6 G8 H
! S4 m0 m* k9 d0 ~4 n
' ~7 w# X1 K9 ?! d$ cd /root/modsecurity-2.9.1/) l9 T. q& i* {. S$ P0 C
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
* g6 ?% |* \* f0 R5 F+ l/ W9 R$ cp unicode.mapping /usr/local/nginx/conf/
9 T+ V) t- A1 p, g y: y4 G6 F) ^将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
$ P' `1 S& \4 V
0 n% q$ v2 n9 m+ Z' E+ j
- J5 h7 q4 M/ X4 j$ vim /usr/local/nginx/conf/modsecurity.conf1 n; c9 }9 G1 A; d9 t) u5 T( c& f
SecRuleEngine On; Y5 O2 j% u( C3 h" ]$ s
ModSecurity中几个常用配置说明:
: W. q9 r+ w5 a( R8 u
, u" s/ P* T" h1 t& C
4 _! P; t$ ^# \# W, I/ a1 \. Y7 G1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
: q# H- M5 _% Z+ O7 f3 {! d
7 J% W2 w7 a: V( Y" t+ V- d+ D1 j, }- n) M
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
( c7 B6 J4 M4 W: s9 H0 N
& i+ X7 O/ Z1 X
! P6 r# f v# d3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。1 a' `2 C3 B: W% X
5 X' b! l; U( k& u V0 H W( s
/ k3 l9 y% r8 O( a' ~! C, u4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。1 h, c7 a( n& p7 n8 b1 I9 c5 r: a
c1 U; N* ]9 O1 V4 s& q$ y4 e& b1 v9 q4 v, K' W8 P! I
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。" B% n5 ^$ c9 F
( D3 i' H5 k2 G5 j
* \* I- A+ U% K; S& J( g- C- D& ^3.x版本CRS" l! V: `" L7 T: {- ^% y
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs% ?/ t0 B- R5 j# ?: r: W; c) K& X3 l# A
# 生成例外排除请求的配置文件1 d" O2 ^4 K% a7 ^- G4 A
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf2 R- E& i' w8 a; D8 y5 s
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
% ?' t8 `3 r2 _, b& q% z0 k$ cp rules/*.data /usr/local/nginx/conf
5 @: }% y) \8 @4 u为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
2 B+ {9 {6 K4 r& u. q
/ ^3 p* n# a2 O9 w7 @
, k1 ` G% s# B$ `5 a( B$ vim /usr/local/nginx/conf/modsec_includes.conf* U- k' U# P& [8 f- J
/ {: Y K) g4 s[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 4 k0 w$ V2 p' B' T; T
* S! A* q+ B5 u! P0 F+ O; y
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。* }3 w! |$ |6 }+ S O9 ^, O
5 q8 r' t ]# k1 b7 s' u
. a: a3 k6 y$ _
配置Nginx支持Modsecurity
5 j) U7 q( B1 n9 `" x. s& j( {+ R启用Modsecurity
{+ E8 W7 m- t使用静态模块加载的配置方法
% i$ D2 M. M- E6 y在需要启用Modsecurity的主机的location下面加入下面两行即可:
$ J$ G6 ]# K7 n. |! _! [/ S. c
5 `( @- y7 f4 I- h+ y- u3 y9 _% y9 q
ModSecurityEnabled on;' y; E5 B# `" A; G7 p: R4 m6 K( K
ModSecurityConfig modsec_includes.conf;( B/ E$ J2 T. l- ^5 t0 a
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。0 o0 W+ l4 X8 f w' |
2 I8 \" D5 A, i# I
- W, O6 k9 ]1 G4 E9 H
$ vim /usr/local/nginx/conf/nginx.conf
3 X G2 y( H4 ^6 B5 s, |7 N2 s P0 K1 @$ d( J
* K; C+ ]' ?' S9 J) i3 Q8 @- x+ Y
server {
c- J, L! }4 c: i) ] listen 80;) S5 j- Z% |) q- D( N( _ G
server_name example.com;& E9 Z3 D( o5 h c8 R
. f3 A V5 V6 h: t# }' @; W
, x$ p6 F+ ?0 D/ p. q/ b P( M
location / {
4 q: i; E0 @9 p+ Y$ z6 l ModSecurityEnabled on;0 A z3 b$ @ i6 x6 {: ^8 I
ModSecurityConfig modsec_includes.conf;
7 j9 Z7 H& ]0 J( m root html;
9 y* |3 w" b0 n$ x3 k! N) x index index.html index.htm;
& G& F& ~( ^ d# z }
2 G. H) p" d# z; x G* c3 C}
+ G4 A2 f- o; ^& `5 i- s2 n使用动态模块加载的配置方法
. V) j, i+ C; m2 H9 g在需要启用Modsecurity的主机的location下面加入下面两行即可:
8 |- \6 d6 M4 B: w. J# O9 B& U- E+ l" g- U) Z7 [ f9 ~( A4 E
1 Y# J) L/ p7 E( B# h5 i; V5 I& y
modsecurity on;
$ p4 s2 n+ e* W' y* t/ omodsecurity_rules_file modsec_includes.conf;2 l* q0 R7 B; q8 g T
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。, G0 u. I/ p& |3 u) Z/ }
$ ^, X' k$ G/ w# f
9 u/ Z* z5 L. n9 K3 f" E# m$ vim /usr/local/nginx/conf/nginx.conf9 y0 x0 w0 _0 G% t& f1 M
$ ^! V$ O$ Z4 r/ s. `/ U
4 \. [: D8 D+ A2 [server {8 e0 t" _* l7 p
listen 80;6 `, c7 T7 O' }0 [- y
server_name localhost mike.hi-linux.com;. d/ ]% S i- _* e4 h, }4 D2 m9 t% Y: S# V
access_log /var/log/nginx/yourdomain.log;9 t2 s, W2 [+ C% ^& f5 r, K
) q/ H( A% j* W% E( \
4 v, v4 H9 x, }4 y
location / {
" T( J. s4 N: S# p$ `3 v+ y0 u4 `# L! N/ \, W' @
9 [, E# z# o/ N modsecurity on; F9 f) G) p6 z, h+ h) Z5 Y. F
modsecurity_rules_file modsec_includes.conf;
' u! I8 }* o; v$ |7 B& l7 _& H root html;, X K" |# v3 L' M X
index index.html index.htm;6 u) |7 F; g1 w/ Z
}9 l5 ]3 \, o: h( o5 J$ T
}9 f- G# Q' Z: w& V
验证Nginx配置文件
" u: {9 q: D" {! p) B4 s$ /usr/local/nginx/sbin/nginx -t/ F7 p- t+ @) f! m# d6 I- f
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok' e9 m: b) L4 K; J2 q
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
9 k: A& \9 P( t5 n# b启动Nginx
5 Y: ? ^# Y- G* H0 h$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
! k ?+ e6 h: {) d; ?$ b7 o; x3 D. i) }
测试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能够战胜更多复杂/未知/混淆的攻击模式。 * J( \% M+ t3 o, e2 G- ]" g: M
|