ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
+ G' {/ m' s$ C1 l
, z4 K y+ ~$ i# I1 D4 J: Q, t# l9 i+ j; O6 G( Z0 r2 q& ^
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
3 T p$ m L. }
" Q2 B+ ?: X1 f) e0 r( k- l6 k& K- H! m: I0 G4 b
什么是ModSecurity
4 [3 W9 @" Q7 \ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。( H1 |$ U {, ~/ O" Y8 x% \- d) W+ I
9 c' W0 Q: A3 J
+ a' |* l( l* x. t4 HModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。' n& \( \3 ^% g3 ~
: x, i9 ^, o/ }% [4 y: {8 F+ P) L' i
6 X* a4 e `0 g- V% A" dModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。$ \" O8 R( X/ f2 n) l
# o# Y& ~/ K4 m: h8 m V) l' \% G& M* d5 M/ Y
官网: https://www.modsecurity.org/
9 l0 E* c2 A1 o0 V0 m* Z* L8 g5 t3 }
. {+ Z# g s" v( S% t5 {: `( v
% b0 ^( M/ P5 a2 r$ C5 Q5 v什么是OWASP CRS* I, \2 S: R% E& N' v; r5 K+ ~
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
- n1 ?5 ]; g, L$ f# N
9 U4 V* [7 S( j# Z% g' r* P Q/ W2 V1 ~8 j4 E p
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。$ [. f) s# a# s/ y+ V
5 t; [3 I$ Y% ]% u
# v- `5 G$ X% ~5 H1 pHTTP Protection(HTTP防御)
: S/ n. Y, A; N4 ?( Z. BHTTP协议和本地定义使用的detectsviolations策略。# [7 Y) J" y3 C1 ~& h) U; H G6 ]7 ^* _8 L
. r# x. c% k& A9 i3 T
! K- {8 T. z- P" [+ E" ]Real-time Blacklist Lookups(实时黑名单查询)' @- ]! a6 u2 P+ G+ ^+ T
利用第三方IP名单。+ s% ]- f2 z6 ?# w; P0 [8 x
7 i$ Q" G1 x6 j1 c7 K( x t1 T. [# J- d& I) @
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
9 h3 ?: A/ [* u) G/ L8 I防御HTTP的洪水攻击和HTTP Dos攻击。0 f7 c+ H, |6 g3 K8 X
" n* V: s$ ^/ G. G* X. f. j, R S& z' P4 w A/ Y2 t8 u5 y
Common Web Attacks Protection(常见的Web攻击防护)
a$ `! o# g8 v- E1 D& [检测常见的Web应用程序的安全攻击。6 R3 \1 l( Y, n3 S+ i1 o$ @! y
5 ^" r) a! R3 ]. R0 ?: [
4 J% j8 F# ]4 b8 A' H; o# }Automation Detection(自动化检测). s; S4 |" \& W L, w$ ?
检测机器人,爬虫,扫描仪和其他表面恶意活动。
" r' ]% a! S: d- V5 l
. \! x& k5 d6 y$ W. i; Y$ w) {% Z* y3 p$ g' k) `3 l$ o
Integration with AV Scanning for File Uploads(文件上传防病毒扫描): w# w. R3 ~& @
检测通过Web应用程序上传的恶意文件。
8 o9 Y! W. e' p3 L8 B r2 p. b
0 ]+ L: S% _9 e5 J4 X% D5 s- g" z
% w7 Y1 \/ U& b, BTracking Sensitive Data(跟踪敏感数据)
0 V+ H- n' P: v( a1 Z! {0 b7 c. z2 y信用卡通道的使用,并阻止泄漏。
7 w0 o* P1 C x# j; D+ g
8 N" E I" P* S& ?3 ^
4 f# u9 x( `* t I" ^/ tTrojan Protection(木马防护)
, ?) i1 z2 _+ R$ v! B+ h3 \检测访问木马。
- Z0 E3 J0 f' y/ V4 ?/ U) T& K; L- u% Q: f6 I7 C2 U4 X
8 z3 p' p* `+ ?$ k p# H" C! qIdentification of Application Defects(应用程序缺陷的鉴定), j, `5 r, p- r6 Y7 E. d; d
检测应用程序的错误配置警报。
$ n1 y4 ~9 d/ ]! J
+ K3 X# d6 W. [% M
; _/ u; R, ]; M+ E9 \1 E8 kError Detection and Hiding(错误检测和隐藏)+ x' v: `6 y0 k7 }9 A* w
检测伪装服务器发送错误消息。
' q* \9 d% r7 K1 J+ C0 q& h) H% t4 F2 a7 L+ o- y
/ x" |1 A$ J6 L9 L$ r: D安装ModSecurity
c+ `9 S8 n) p0 b( w3 M3 c8 O软件基础环境准备
1 G$ f* |! p5 Q1 E3 _4 g下载对应软件包7 k2 }1 ]" S& w2 D. r g
$ cd /root+ b. P' E* }; W# A! a8 a
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
~3 }5 L& E8 ?& j, @' o% s7 K6 v: c$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
8 A# _9 t0 U1 K+ A" P安装Nginx和ModSecurity依赖包/ P9 q d0 F u: Q- `
Centos/RHEL0 w$ a$ Z$ C8 n4 m: C# e" R
9 J0 @2 I6 s. L# ^% }6 D
, q" l, @1 ]; {% O
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel' T6 P' `) `3 B9 ~' F5 [- c
Ubuntu/Debian% x: Z% L! M0 z/ @6 y# e) {, H V [- r
' J9 ^9 |. F" ~
( j4 n9 X+ m0 ], M/ n4 p/ k. V$ 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
5 h" D# \ \& e1 L编译安装ModSecurity
0 n1 E% N6 O( R" t& B# ONginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
9 j: j& |# N9 y6 |7 l2 s+ h: q3 U9 D1 \ j" k* z
. N" B6 U* A8 ^5 M0 n$ |方法一:编译为Nginx静态模块
: f9 i0 t' l+ M
) B+ T6 E7 r, Y* r: l3 U1 Z1 p9 H$ e5 m& M0 m7 G* ?
编译为独立模块(modsecurity-2.9.1): `' |, Q) H( u% c
$ tar xzvf modsecurity-2.9.1.tar.gz9 v, y" D& @2 D# Q' u
$ cd modsecurity-2.9.1/3 Q+ v+ p3 F8 A: h8 E" m9 h4 n# \
$ ./autogen.sh |; N/ |$ ]7 j+ g/ S: @* E5 d
$ ./configure --enable-standalone-module --disable-mlogc" x3 q" G9 v$ C
$ make% i/ S1 e" b0 o' ]1 u
编译安装Nginx并添加ModSecurity模块; q8 ^5 F5 A+ _2 u: a/ `2 u+ b
$ tar xzvf nginx-1.9.2.tar.gz
2 i6 ?. m& X. h/ D Q. U$ cd nginx-1.9.2
1 U* F& ^+ E$ L# p+ X z! Z% G$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/6 p+ a& k% Q. P! Z3 `
$ make && make install B" k# w: c s
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块; M" W$ i9 P" Z% E5 k' X
) r2 k) \' n b6 k& v
8 Y) H/ R9 j' g; e ~- @2 n3 M编译LibModSecurity(modsecurity-3.0)
; O1 h- F/ z0 a$ cd /root
9 `6 o% \8 z% {3 X3 P0 O9 s$ git clone https://github.com/SpiderLabs/ModSecurity$ P( z4 z. f6 `: k0 c6 d
$ cd ModSecurity. c, b5 A& M0 k
$ git checkout -b v3/master origin/v3/master/ |* H: q( p4 Z* j0 V9 z+ y# m
$ sh build.sh
% R5 l$ v5 t' E/ z$ git submodule init
' `. p4 G) u( i7 U! Y ]. y1 ~$ git submodule update
+ P/ E+ j! l1 g' s+ p% Q" C$ ./configure
9 g& w( m5 g. \: h$ make0 {7 ?: g, }3 }$ x! Q8 G
$ make install6 v8 m( c5 a' P
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
7 L0 D6 f% S% a3 [) v2 I7 B; t7 K1 x5 [
& A( e' [8 `+ J! E! P: q3 O
$ ls /usr/local/modsecurity/lib% ~* I' B" i1 R8 m
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0# ~" A4 t7 [' H5 {! i% K2 _
编译安装Nginx并添加ModSecurity-Nginx Connector模块. b5 f$ P- n) k( }) L
使用ModSecurity-Nginx模块来连接LibModSecurity* Q, J7 {- }4 D- S, L* P* [/ z7 U
$ l) f! g# `. R5 r6 W$ @* U0 c ^
& ~3 {4 P- n# E! ?5 d3 H1 ~$ cd /root; v. [, k$ Q+ y# w
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
, u% v! ]: F6 _' n$ tar xzvf nginx-1.9.2.tar.gz
H j3 o$ w7 w9 C, G$ }2 \3 i$ cd nginx-1.9.2
9 A0 D$ R- }5 E! W" t! u( b$ ./configure --add-module=/root/modsecurity-nginx6 {# O$ u# g G
$ make% ~5 h" f# \5 ?5 T0 t
$ make && make install5 ?3 x6 Y/ U2 v! S# {8 T
添加OWASP规则
/ {* F% k) w- C+ G1 L& xModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。4 i- m7 ^, i) R4 y
: U! P# x) u# O5 w) g) q5 C
0 [4 [7 Z+ q: ]) g/ h下载OWASP规则并生成配置文件2 [/ c/ t. B- \9 p% i; y
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git9 r+ \# n o7 a# p$ r' ]5 @
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
* c2 f& B! v: B8 a/ a' z" f J$ cd /usr/local/nginx/conf/owasp-modsecurity-crs3 B, E8 M6 t/ q
$ cp crs-setup.conf.example crs-setup.conf3 t# m3 K$ {5 `+ ^, n J) ~
配置OWASP规则1 ]; J( S' m# B
编辑crs-setup.conf文件/ e" j& J. i& K
( \! h4 P' k4 h1 I4 f
: ]! L* c& f+ ?2 D$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
+ n7 e# u) e% {8 E9 O$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
( m5 i. R! p0 I' P8 L8 R, e$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
, O6 ~4 ]: H) b" p5 N$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
) |1 f$ R- g- K2 S# O默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。+ k, m/ t9 e* ]$ t6 p; x Q/ W
" N, [& U k, ]
' D2 @- V7 U8 g* y# h% F
启用ModSecurity模块和CRS规则
3 \! X$ t' h u: r! c' T' D复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; l5 w1 @" [+ G$ @7 _$ [
& L, b4 a% x% A0 a& j2 K0 m7 J: x2 {6 K: Y4 \& y
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。1 I, u. F t, J7 F7 a; b
5 {0 P* h' D) M: i# k; P/ H. D9 z: x
( t3 G/ M% V8 i" G
$ cd /root/modsecurity-2.9.1/- L- S2 ?" X. g
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf ! Q5 M8 H1 a) e/ l$ Q; h7 R$ X- c
$ cp unicode.mapping /usr/local/nginx/conf/
# O. r! Y0 o" _, f t. Q- `将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
* a o4 y' }4 F, `0 } A- B$ B! b( @' }$ Q" A* @+ @4 b- C+ \
1 F* ` U9 i$ `9 p, y' e$ vim /usr/local/nginx/conf/modsecurity.conf5 C+ }2 O+ N0 x4 h5 M
SecRuleEngine On
6 {% b- q- S: j, [+ B$ l6 z* R7 ]ModSecurity中几个常用配置说明:2 x1 K7 {' G; ?6 C+ h$ B
0 y3 O0 q/ O4 K; q. C3 A
& Q1 t6 A- V9 _) }/ U( A
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
" e/ s9 y9 T7 j1 I" L1 i* q3 @/ d
( m# v8 H) d t7 b1 h
G6 p9 h; p) h+ w: X- Q1 g# e2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
4 l( S; C1 N3 J- W# @8 y z3 W/ Z1 f7 T# c
; r# a( |% B+ J/ F0 Y( z( n3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
- X; q* H, F- D' [ R- f* p. G. `
( ?0 _2 a0 f4 y7 v; l( o
8 L3 k: q4 `, ^8 a6 H6 w4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
' M6 b" h3 g6 p( I! m
7 W7 G7 E4 J1 a8 S8 M
i3 l3 D4 N( M( q7 j& D9 N在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。' e W: Y$ t1 s* q2 D5 r0 N/ E" }
+ e; F4 f6 _; r: q
5 {5 z. H7 l4 d# C+ F5 C3.x版本CRS" K# o, y; g6 R. m6 T j$ }
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
+ h6 m/ s1 u1 {* H# 生成例外排除请求的配置文件4 R" [3 I" m! k* N" h
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
) L" U& t* j! r$ K% _' r& k' ?+ @$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf+ r: [- C1 O0 ^: Q
$ cp rules/*.data /usr/local/nginx/conf
5 h6 r4 Z" Q$ G+ K- V! H# G3 }) C为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。- H0 }, ~9 Y1 w
# j; J: |/ I; j
! Z4 H9 B( w) f7 D/ A1 A. ]$ vim /usr/local/nginx/conf/modsec_includes.conf
# ?! @% o5 X6 y. H- I8 v% V4 y' `9 b
" s' Y H/ a0 p4 d[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 ! [# B3 e- N) t8 F2 H* r
5 `; ^4 v5 R" l" {# h' B R) B
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
5 j4 {- G+ D% ?# B+ s6 l
6 J; y% S: _* X5 p2 U+ V! |
- Q* Q3 p1 _1 G配置Nginx支持Modsecurity
, x7 h/ o) C, k% v+ g启用Modsecurity# K2 [6 c% x7 v( E6 T
使用静态模块加载的配置方法
, K5 I0 I" r- w b& [& L在需要启用Modsecurity的主机的location下面加入下面两行即可:
0 m* L* ^+ c9 g- D5 j
8 D; M" L/ T; c7 o* h1 ^& }( g" Y/ I! z) ^; U2 B( \0 Z
ModSecurityEnabled on;8 I1 h( ]6 u- R+ Y! i
ModSecurityConfig modsec_includes.conf;7 w* p! x W- Z# q/ u( m
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。" f1 d7 [/ a2 X$ }5 E5 U
0 V, n8 r4 O+ ]1 d* p
0 p2 A$ _$ I3 B; O8 S4 v. N" f
$ vim /usr/local/nginx/conf/nginx.conf
l" ]0 t. Y; S7 q5 k6 ?) A( Z- A
$ R, J" g; \1 {9 F) x
- K& Z+ ~, z. @" S" W- |/ J+ Bserver {' q6 N' r' _& z1 |* f f3 Y; r) @
listen 80;
7 a4 A g* Y8 t& K server_name example.com;
) T, v l& Z' Z* L* S9 g, S+ G3 b7 Y7 S$ G
& i8 ?! ^4 R) n" O& U7 V) l location / {
# ^6 O9 a( K8 a: f- k ModSecurityEnabled on;
3 _8 S( a G( ~' R% } ModSecurityConfig modsec_includes.conf;- A; I1 n: ?3 o5 v# \* m7 ] w
root html;9 v8 R1 m0 o3 b: n, Q
index index.html index.htm;
7 N8 m- a ?% Q6 N, c* M1 A$ ` }( |' X/ B4 B2 b, j5 s
}
0 a p9 T" z& a2 K( r" P使用动态模块加载的配置方法9 x) k! x# S; W/ f
在需要启用Modsecurity的主机的location下面加入下面两行即可:
$ u8 Z* ~! e; Y1 c; _
5 @& c+ ~2 `- r
% N2 h& }3 I- {9 Smodsecurity on;+ E6 U$ }9 Q; B Q" T9 I
modsecurity_rules_file modsec_includes.conf;8 i" i& Z5 C. ?" e1 N7 u4 \
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
' o. W9 T, s( `. q2 ^
7 ]7 M/ W1 r, r1 \: k x4 b6 y' s2 J9 c0 p6 ~0 P
$ vim /usr/local/nginx/conf/nginx.conf5 u/ J2 S+ l; B8 X# t
0 t4 @7 D. P# T& P' r$ I
/ r9 W6 g2 {+ s) Fserver {
' ?% `6 X: J. t5 x listen 80;9 z: i, `% ]/ R; c5 P' b4 c
server_name localhost mike.hi-linux.com;
( U( ]6 o( ]) P& ^" E$ F access_log /var/log/nginx/yourdomain.log;) ^$ O+ d6 y3 P4 k4 P
9 R1 M& l1 ^5 D2 [
& c- i/ {& ]2 m9 o1 C
location / { b9 n- T7 C: a P
( L+ l, ~+ X3 E
: a; S" ^6 W* N M* x8 [ modsecurity on;
3 ?* p$ U% V% ^$ S modsecurity_rules_file modsec_includes.conf;, S& b. F: ~1 @: l3 o0 T
root html;/ j( j6 B" K5 }( }0 m" W3 R5 G
index index.html index.htm;
- v. `6 e8 K! ?& W! f. h}
) f* M. M0 }- v6 U& t8 s5 l}
8 v% J) a6 `- C9 N: I; B验证Nginx配置文件
% q6 M! e" R% {7 b1 g$ /usr/local/nginx/sbin/nginx -t& [) k( v2 `- V% e( _
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok1 H# ~* C6 {3 l8 u l5 q
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful& u' x3 ?. j" |) |% ~/ e, s; P0 [% Y
启动Nginx2 S( Q* x- H8 Q" D8 {) v( K
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf: {$ x) x) q5 |4 [. T
# |+ ?7 O: f& t- `; E! H) Z Z3 l8 d测试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能够战胜更多复杂/未知/混淆的攻击模式。 % k* a7 y" W& u* M; z9 ?% C' `
|