ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
! N- k8 {8 t9 f1 ~" D, f
6 `. ]/ V% }: S3 J4 i! R! d9 k
/ n m9 l: ]7 h- H) r; Q! ^# J在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
: T" X/ i8 f7 L6 \ S
* ?% q9 k4 g6 R. ^
* o8 H5 N }, `3 k* |什么是ModSecurity9 V; H1 T- R; g$ J6 Z% ]5 W
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
. T" _' o# F% M) v% \9 p
& u' Z8 P! q% F/ x) j8 d$ c/ _5 J2 X6 a
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
9 w0 u" e( M- T, q2 K) f1 z
/ K+ c. y l c( @3 _8 _7 O9 n7 L2 b( ] a- z' F, i. N. W
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
0 _% G: y$ `, a" N: ]$ s" }1 C, U( p% X, Z
" u; Z/ x4 P! b% r' G
官网: https://www.modsecurity.org/" v4 _ L) Y& Y! t: q
% D& v! X0 i* \ w2 g* B6 w
# `) `1 ~" y4 n1 \, T; g
什么是OWASP CRS+ ~" |- ]3 w/ R
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。) E$ O; `; w6 r( F' }4 ^: {
+ @* y; O2 k O$ b0 T
: a7 }$ l, J) x. ?* Q& l; ]2 ?ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。* u. V# Y% H6 T" P' U! [5 K
- G. \- O( v2 _) B% e: |/ t; p. G3 i. E- Z# K/ h
HTTP Protection(HTTP防御). b( r% m S }7 e8 w7 T# z0 {- b. N
HTTP协议和本地定义使用的detectsviolations策略。1 s6 D2 f3 {- X7 {- F; s
0 j5 t. B% o5 U7 G" {
" n6 M- U1 z# u; v6 F' T' PReal-time Blacklist Lookups(实时黑名单查询)5 |! t0 M3 y9 L5 W0 c9 s
利用第三方IP名单。
, ]6 q4 Q$ ^% c" p# U" \9 }
5 o: x2 i( W/ |3 Y+ ^! K8 n' |3 B/ Q6 q1 Z- e* @. J' P+ c
HTTP Denial of Service Protections(HTTP的拒绝服务保护); Z0 \/ ?4 d& @6 P1 Y, ?
防御HTTP的洪水攻击和HTTP Dos攻击。! i0 Z7 j, ~9 Y7 I2 f# \
2 h' H; K4 {0 ]- o
5 z- P7 U* H+ y/ \5 \Common Web Attacks Protection(常见的Web攻击防护)
# |3 R# c6 K3 o3 l2 ?检测常见的Web应用程序的安全攻击。
# \6 i4 l* r- g, r0 \
: g% w; ]* B' k1 D
- @4 e% ]5 K: Y( q- o( FAutomation Detection(自动化检测), M+ x3 a# X3 o# y7 d3 n
检测机器人,爬虫,扫描仪和其他表面恶意活动。
c5 m/ U0 W3 v0 S$ D' s. l- \: Y, U% I7 H0 _ z [
! X! T' c, S; s) |1 S$ e
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)( Q9 P) E: W5 A8 w8 a: g. O+ m, ^
检测通过Web应用程序上传的恶意文件。
1 l0 J9 R0 f/ k$ P5 Z( C; ^& M5 s
1 {+ B8 ?7 N1 @, s; @9 f0 z" G/ o
* U! H/ k9 K6 M4 q; q6 B) rTracking Sensitive Data(跟踪敏感数据)+ j) I+ |: \# N F8 W! b" n# \
信用卡通道的使用,并阻止泄漏。
H( J! R1 `! F# R, k' z
" E1 S- R5 J) B
! d3 i1 r7 k, gTrojan Protection(木马防护)3 k) ]- v. u( d: K2 K' X0 v' C
检测访问木马。
1 s+ ~" f" s7 D& |0 Q- E% V q# A/ _
; x% n$ C6 D( c, Z" I, N
Identification of Application Defects(应用程序缺陷的鉴定)
: X, o+ O- Z& E7 ^+ Q& I" i检测应用程序的错误配置警报。
! A; _4 u/ V7 C3 o% k. n7 U& h9 D# a' }6 V: B2 h! p1 N' J
. _' v- G5 d8 ~Error Detection and Hiding(错误检测和隐藏)
( z: Y/ k- ?! @* V, Q" J, \检测伪装服务器发送错误消息。
" x' \1 z# z6 ^0 k. g( u, x, K; }$ m0 u7 o8 J0 P H8 f
/ C" b+ l" g. x% Z, t/ x/ x安装ModSecurity7 N- j+ X/ O1 x9 n" Q( e) ]
软件基础环境准备9 k5 O( T2 w! g8 p; [. T" @ `
下载对应软件包
) j, L6 m! H! I5 T4 f( Y' b$ cd /root/ G9 U/ h& D3 O" q" {0 a, T
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
$ h1 Y1 O9 g+ O' h3 ?$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
7 ~5 b7 D% V: o6 |安装Nginx和ModSecurity依赖包
; V) }3 N( f' @6 A# d! y) bCentos/RHEL' s* n* |- P5 R
7 l( {" V8 ^+ R4 S9 U7 B
8 m- G$ H8 d) x6 O8 r+ }" q6 u
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
) W" [1 E* U$ W3 ^- y0 e& i+ TUbuntu/Debian. F0 S, s* u1 ?- ?
- m6 Z D0 }' a+ c8 l0 H+ R# _# p0 z0 j7 I
$ 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++-dev2 R. T j8 }# S+ E9 u2 b# I$ I
编译安装ModSecurity/ N' Z; O$ V7 r( B
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。: R% s2 R9 S. t a
& I p% L, K: V( [1 H5 Z+ S8 o, L
# C) H( a' x. E7 |+ p
方法一:编译为Nginx静态模块3 `- q0 M! r8 F; q {1 Z
0 O2 W& g1 Z" n
7 l; K+ |% a& Q( t3 \3 c! H) d编译为独立模块(modsecurity-2.9.1)
$ ?' ]8 r' [. ^; i$ tar xzvf modsecurity-2.9.1.tar.gz+ k% r; P$ n! l- p9 b- B+ N. n
$ cd modsecurity-2.9.1/' R: g. `8 ~( N% u4 d6 q: {
$ ./autogen.sh1 i- C2 t2 ?! p# H
$ ./configure --enable-standalone-module --disable-mlogc
- }% V& g6 Q+ T" D( |5 B$ make, \: ^* L( v( z$ T- ^
编译安装Nginx并添加ModSecurity模块
5 g& c% C8 X, D# s$ tar xzvf nginx-1.9.2.tar.gz
, n9 ?- S- I6 k$ cd nginx-1.9.2
, k; f3 i; X1 a$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
]. A# q2 y9 j# F0 I$ make && make install
+ q D9 Z3 X: H& T方法二:编译通过ModSecurity-Nginx Connector加载的动态模块& p" E: l' \7 b' f* M" ^
. p4 u q. i( A7 X
( C* Z2 g' }! ]7 Q; n
编译LibModSecurity(modsecurity-3.0)' S9 O8 ~( D5 G8 v% k; L! Q) }3 M
$ cd /root
9 t; Q0 n% s( v4 l# R$ git clone https://github.com/SpiderLabs/ModSecurity( k! C8 M- S$ R8 {5 d! q
$ cd ModSecurity- N n2 J J% O! B% J; D( U
$ git checkout -b v3/master origin/v3/master4 y7 W+ s C! T9 m1 T- N5 g& ?
$ sh build.sh
0 Y3 _4 F5 W9 u% B& t# k; q9 H$ git submodule init5 f+ G; }: m+ p8 ^
$ git submodule update
2 d( G1 q0 p7 Q$ ./configure; X$ H: w1 i h+ v* q
$ make
% j; ]9 @9 L5 l3 I7 m2 K$ make install$ o2 }/ P# ?( B
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。& U7 j" t, R9 q& p
& B5 i8 O9 f( _. J2 U6 C; E! C
+ L0 D+ E" K9 |) \2 P" H$ ls /usr/local/modsecurity/lib
8 h. z1 u% x0 ]3 _) nlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.08 d: _; K1 _0 H1 k" M) m$ m
编译安装Nginx并添加ModSecurity-Nginx Connector模块
( `# F h' k+ h使用ModSecurity-Nginx模块来连接LibModSecurity
% }& ~% W- k# ^, b+ s9 o
/ x8 s9 g4 t7 x5 P; g: ^ C# [( Y. I
$ cd /root: o6 x! |* Q0 T- F
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx+ x; H* M3 x# U- a
$ tar xzvf nginx-1.9.2.tar.gz
3 k4 Z* {. |4 P/ w4 v. V K$ cd nginx-1.9.2
' F/ g T9 Y4 d* u: {7 v- S9 v e$ ./configure --add-module=/root/modsecurity-nginx
4 V2 l* s' c1 X' Y" G' _' W5 f, ?$ make: g" J$ U3 u* b! E# V( b8 a6 Q, Q
$ make && make install4 Y7 t- M1 y8 b8 P
添加OWASP规则
3 Z, w# Y4 ~* _( G1 VModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
& c, J) G3 |* V: _4 b# I1 t5 @. c5 D
& M5 f% }$ y1 C- d下载OWASP规则并生成配置文件
% R7 a/ q v% q( U$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git6 N: `9 R3 m. W
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
! G9 r2 G9 H l) p$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
. y/ e6 l; A. T$ cp crs-setup.conf.example crs-setup.conf$ }) }# K( y9 m
配置OWASP规则
9 r4 z' ?3 P9 @6 J) v7 H& e& Y编辑crs-setup.conf文件
3 ~7 U% H Q( J" p3 z4 P( v& ^: i1 \3 n* p+ Y% O: I) G
7 M# c' C9 Y3 e/ k+ i1 j0 k$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf8 a( G: k' ^- f
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf C4 N: O; F* U6 H* N" ^2 Y
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf( m! s; y# W5 l* o$ B
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf: z% Z" T$ N Q( a! o) I
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
; p2 t6 A; {; R; ]+ _) ?0 L
% O* X0 Z5 W+ S5 b* V T, n" ]7 \- c/ l$ a
启用ModSecurity模块和CRS规则
* `, U/ J1 \( |, B7 v复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
5 l9 O8 p# l. g. P3 t/ Q7 O6 z2 x! g, T8 l) V" V- t
2 i% |6 v. I) A/ ^. R
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。) x) j; _0 \, \+ [' ]* f6 U( w
. {) R; ^3 A- S D3 i& q8 G; w2 F. F# @. a. F9 r1 b w
$ cd /root/modsecurity-2.9.1/
6 ^% N. ?( p. k, j- t3 s# }$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf ; c" t& x2 T6 o: A. ]. \
$ cp unicode.mapping /usr/local/nginx/conf/6 L8 c" F6 p8 t0 m P3 `
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
/ E& n' w7 J6 ]9 z& j8 Q( \5 u4 x
" u4 {( s" q& h6 s1 L( e" i9 }# i) ^
$ vim /usr/local/nginx/conf/modsecurity.conf' w4 I [" O; M' Q; w: m
SecRuleEngine On
" N4 n" h% U# x$ ~ModSecurity中几个常用配置说明:" A. ]% G: _. _0 }! i7 Y
+ S& Y4 ?' |$ Y# E, b! Z& I9 v2 A- Q; G0 ]$ U
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
8 u8 k, e f% E0 k$ l2 K" d; i8 c W
3 ^) e- r5 k7 |- `: r
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。# t: }, N4 s, z; W1 e& ]
. E7 w; d7 C% b3 p. d( W, S z, E- f9 h: G; Z7 L
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
# [- Q* x/ ]5 f' n! r- _4 Z" V
* r d1 m. v# V6 r9 o0 S5 P7 U" h, `# j9 n; k& H$ i, I; s
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
& o. ~! u" M" }5 U8 X& u g- y% G6 X a7 K5 c
' E( Q1 @; J: z9 s在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
: S- ?/ W* \$ [2 n" ^9 N$ d+ p3 K0 M( Q/ I1 e, ?( D* S( K' v6 |' n
: ~ ?# R1 J z6 |4 @ F6 V! | ^6 X a
3.x版本CRS( b" _! B- H8 K. u0 v, `% k5 V
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs) F; w, f x, `4 w* Q# S1 i
# 生成例外排除请求的配置文件
4 ]" a+ l3 Z+ Y# c4 l2 u$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
; h4 H0 l9 U* f$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
) \: Q7 w7 Y7 D6 y6 N$ cp rules/*.data /usr/local/nginx/conf% u2 `) |5 s' r3 f
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
& G& s/ l. T2 e( }, v! j, C: A1 \* B! Z# d% Z1 b0 b
$ V7 F3 d% i; l2 _4 a1 Z [
$ vim /usr/local/nginx/conf/modsec_includes.conf t: P* S) r% ~3 _5 ~5 z* U# Z
& l1 j- z: _- @, b( ^2 i2 g% V[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 : V5 z* a/ ?4 ?! ~7 ~% r- S0 P5 m+ `
/ p. Y& \) @3 X' T1 K# q2 S9 ~注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。1 L) U/ H" L* Q7 Q% S% a
9 x2 l \' c: {: { w( z
8 \: t) p1 E% R. z' i# c
配置Nginx支持Modsecurity+ n4 u$ X0 R" F( ]# X6 r+ A, N
启用Modsecurity
( g" s2 X( R& l l9 B- P( w使用静态模块加载的配置方法
! J8 t) \4 H; `7 j( f. U, z在需要启用Modsecurity的主机的location下面加入下面两行即可:6 f- b1 t: C4 y$ Y
# I% N* |' w( L5 \
+ z4 r |: K& t' QModSecurityEnabled on;
$ m7 c- W3 n7 V; Z# @3 v( ]+ r, LModSecurityConfig modsec_includes.conf;1 T+ X( n0 J% N2 |
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。1 f6 e+ _) b7 ^2 n$ p) b/ X
9 h' f/ s U$ D l8 o8 o$ x
$ x; D3 P" @" U* @$ vim /usr/local/nginx/conf/nginx.conf" F; O0 K- _, N$ w J
4 \5 [6 G9 A4 _
4 ~9 c: V- B8 A4 ? E. F& A- ^% xserver {5 Z9 C# W6 P2 C, w) d
listen 80;0 d( ]5 ], C" I
server_name example.com;/ S' M4 l1 O- [( }: m
; p2 A9 I/ l1 V1 y$ E3 e2 ^, H. m8 k
8 ~9 `/ h# B0 D1 L5 {8 C, m location / {
! t( S6 ]% K2 S ModSecurityEnabled on;# r4 F8 s- n6 c6 Z5 v Y* O+ d3 h
ModSecurityConfig modsec_includes.conf;/ U. `5 x/ u4 I) m
root html;. z9 [( H# P+ T) c
index index.html index.htm;2 _% q+ m" e7 E: L
}
/ ^" w& h# i: ~$ Y}4 v- o) m& s3 }1 @% `- o* x# ?8 ~6 y
使用动态模块加载的配置方法
/ ]) U! x& F( k6 T: B: ^在需要启用Modsecurity的主机的location下面加入下面两行即可:; o, X1 o D" q ?4 X
6 x, k8 G) ]2 x9 S: U" _) G8 @( m3 I( _3 P' p! s
modsecurity on;
$ ~/ X% n, E0 Y4 ]( N3 X7 C5 Kmodsecurity_rules_file modsec_includes.conf;9 i0 {. o( t( z2 i) l6 X$ ~# @' _
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
2 N* _0 V# V* F8 k/ ?6 X/ ?
! R" J3 e9 z: x+ b. x: i5 C' k( C9 d* e# P
$ vim /usr/local/nginx/conf/nginx.conf5 x; L$ ~- X2 w* o3 @+ x9 }& [
% n& j0 U$ e6 Y# s
! r# S. q, d7 pserver {- F- F1 T% M' x8 e# a |
listen 80;
# W( O& P" v5 t0 o1 W1 H. L. b server_name localhost mike.hi-linux.com;7 v! C% A3 Z( H9 G8 ?
access_log /var/log/nginx/yourdomain.log;( D2 t2 A3 A U7 x4 V$ Z% B# j
( N1 r- D- k( X% ^& i/ w" ]0 }% O& \4 H% c) y! x6 V( G6 U
location / {7 l" m$ ~) P' p* |1 j) l! L
" Z; h4 G! E, f3 k) S* p
. J ?( m; C# j& Y4 W modsecurity on;
, i2 t; `9 \" U/ C4 h) ^ modsecurity_rules_file modsec_includes.conf;6 K* u1 w# n- M% A% E& y4 x0 n1 L# ?
root html;. y3 H0 j" v# g5 a0 ~! J
index index.html index.htm;, y! G5 O) e/ P* c. V/ V( u
}* ?" P+ Q' [) C: J* }
}7 i: O* O; i* g" ]4 a! K5 u
验证Nginx配置文件
+ y6 J. a( L# ]5 R2 ]+ _$ /usr/local/nginx/sbin/nginx -t+ v& D0 [$ Y9 D
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
5 k9 `% h+ G; ^: Snginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
" w7 b( V" N- A/ E# s/ a2 q启动Nginx
/ p; O, `, d# y$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
3 z( G- o7 h1 c4 H' O5 E& P; u
( |9 T' ~% c% J% Y; s测试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能够战胜更多复杂/未知/混淆的攻击模式。
2 w. o0 _/ f2 E, @: S: e) h |