ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
# S% x( T& L' \5 Q4 H
' ^) p1 ]' {+ H& o8 j
% G+ S7 X8 L2 N, w ]& p) s在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。) Y5 j; P" c( h4 m: e) y" @
: f; u+ M* T4 z5 x3 U" u" f
6 Y/ w5 z" ?0 b1 I5 r P什么是ModSecurity
# k: c# R* p# D; I: w, BModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。2 L' ~' j$ _0 b* F' L8 \/ g
$ U0 b X3 o y
7 O4 j0 B2 v: M; v& k1 HModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。) K- Z2 I; P0 p# F5 i) F6 l
, m& U" D3 `. a' m- `& B5 C. C
! _8 g3 r+ J$ U' d" @5 F) w; ?ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
$ v0 x; U2 {+ C1 l' X: k
4 g. B9 C- [1 y l' V) v- N+ r- v& e4 u: i
官网: https://www.modsecurity.org/: l! e1 J6 n0 I" s+ E3 N1 k
2 r5 C Y7 r* W' `% }. m' [. f: q% `, q+ o1 E% l' u' O
什么是OWASP CRS5 {3 m- V: e4 E4 P: f0 ?
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。 x5 X/ X$ S$ ?& N e3 g3 F
4 |) f7 T/ c+ g6 G! O0 O) l
4 u$ K) E. x0 j
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
! u" U; {4 [4 c& n P* F* c7 _
& r% O4 [$ v# z W$ m: Q1 B: ^* @4 \* l# n& Z- W. k4 e
HTTP Protection(HTTP防御)5 C) t8 }( O2 _, v6 ~
HTTP协议和本地定义使用的detectsviolations策略。
5 y4 p* }# i B: G. x# j
( { L: ]7 i% o1 m* e$ c5 i
* L9 ]" b: { U- k# _Real-time Blacklist Lookups(实时黑名单查询)
' o1 w: ~, I8 D" o4 H P利用第三方IP名单。
4 {7 H! q* @! M/ [( h% W, o
- a; Z* i6 J, k' W5 D, X8 q2 o" S- }& L) u
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
9 Y* d- G& G) u/ f, V防御HTTP的洪水攻击和HTTP Dos攻击。
! |. B4 j" u M: c/ J$ k, g2 D4 @/ A/ q4 }
% I: O8 H' Z$ p3 |; N
Common Web Attacks Protection(常见的Web攻击防护)
+ N" p- A( P, X1 e检测常见的Web应用程序的安全攻击。! x$ \# g2 c7 H: x
" A2 }/ J0 Q: _9 f. o5 ]
5 x e$ ]- A/ j3 }7 m& R3 ]) H* o" KAutomation Detection(自动化检测)
4 O. K! S' L9 N1 P+ W$ Y" x7 q检测机器人,爬虫,扫描仪和其他表面恶意活动。
* [5 x# o* r5 J" z
& ~. j* K3 L4 s; D" u' s0 p5 k8 b3 e1 |7 ?" u% K4 Y% }- v
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)) b* i4 [" V3 ]; \
检测通过Web应用程序上传的恶意文件。
: p( A& J2 e! ^* B. |( r9 P, o3 I: _, I" d* }6 i- g% F; ?! i5 T0 d
6 L' g" M2 X& x4 V8 J1 ZTracking Sensitive Data(跟踪敏感数据)
* {5 n# `" n! V, r信用卡通道的使用,并阻止泄漏。" z2 g) U& q5 q9 c, z- J0 P* @& s
4 t3 p' M2 C0 Q$ @2 c
% n* v, H% V- ]6 j$ z3 E; [Trojan Protection(木马防护)
* i+ w4 I/ u. w( a! D检测访问木马。
h( C0 F* b# @/ _
* L. }0 v# s$ `0 _
: ?3 f8 O% }5 |$ |! w. W4 o1 m: ?Identification of Application Defects(应用程序缺陷的鉴定)
- {' F& }/ s! D9 ?! ]检测应用程序的错误配置警报。) M4 j$ W9 S j
2 |7 t3 t" ^) t+ o; i( A; n
1 S" p: M2 V* U" Z0 \/ Q
Error Detection and Hiding(错误检测和隐藏)
7 c2 R& U( b4 h. d1 \" L' X0 s检测伪装服务器发送错误消息。) R' X9 t9 X( ~7 j2 A
% L; G9 V t! \5 p' L. X
; O: ?$ n& F2 |8 d' y% F3 R+ E安装ModSecurity* Z e, \, S5 Q% ~. K
软件基础环境准备
8 Q! F- p8 o1 ~1 F* y3 a5 `8 |, r4 r下载对应软件包& K3 n. V% [2 V/ \+ q
$ cd /root
; W: G, M, W* B3 `1 G7 b, I$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'9 C3 E: @- d; w
$ 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 ~, a9 ^. D7 L: h安装Nginx和ModSecurity依赖包
- B: j' A& ]! yCentos/RHEL
) [- t, l/ t( ^8 ~) H' s
- P1 H K; | ^8 d4 U0 T
' b; @( R {" C' ~& B5 h$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
) G' j! F1 b, o M7 ^# A8 \Ubuntu/Debian
7 C+ l6 l( i5 w# I5 T5 S- g% }' ^5 m7 a6 C
) n' Q5 X s6 P- \0 k }. @( \$ 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
& L% T/ d1 ?" Y编译安装ModSecurity
) U# }3 N' Z8 v: h6 Y- s. l- `Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
# L: i) \, P/ m% F/ D7 Y' E# R0 d( ?9 z0 [+ e6 I3 ^4 n1 B% W& C4 C
% n# r( t( F# U5 X
方法一:编译为Nginx静态模块
& ?$ ^+ R8 d* H( w
7 l! I! P* {3 L5 J4 Y4 N! P( `
8 V: N# l3 R9 t' x编译为独立模块(modsecurity-2.9.1)6 U" ^. b u0 L- j7 G; n
$ tar xzvf modsecurity-2.9.1.tar.gz5 |; x" G4 M ?' i+ ^* ~" i: @% T8 D5 r
$ cd modsecurity-2.9.1/
; }. G+ n2 q% B; C3 b- g8 r" d& M8 q$ ./autogen.sh
0 g0 t4 b" y* X: P0 {% W$ ./configure --enable-standalone-module --disable-mlogc
9 V+ u# X# E: B: y3 I ~$ make
: n) t, R [+ Y编译安装Nginx并添加ModSecurity模块8 P3 {& J& h- |* x
$ tar xzvf nginx-1.9.2.tar.gz
, a a% `' L, @+ J: D$ cd nginx-1.9.21 G- }2 R" N5 B1 T/ L7 v% r" m
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/: g2 n5 S6 ?# n& ~+ y
$ make && make install+ ]2 V$ Y7 w+ x. w; E
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
{$ x8 O" S( [3 H. G0 {% e+ S% b
! \) ~+ Y: _" |% Z) y( k! o {) V1 x
编译LibModSecurity(modsecurity-3.0)
2 z. h' I! p5 o: {: X5 ?$ cd /root' J9 d: v3 D, ?" j$ i) W
$ git clone https://github.com/SpiderLabs/ModSecurity8 M" i3 m& A7 X+ w1 @+ Q& ?
$ cd ModSecurity! A$ j( m7 F6 A6 l1 _9 O, I2 d
$ git checkout -b v3/master origin/v3/master- j! O7 c# T$ M- k! L0 o8 Y: ~
$ sh build.sh/ ~+ U: O6 x/ o% a f9 o
$ git submodule init
; t* Q( l; D" P$ L' v$ git submodule update
: Y! j0 m5 e- `/ X1 N" ^6 [% c5 g$ ./configure
3 @ X; Z$ H! u3 [$ make
3 `; |8 f0 |8 E( @2 k( u8 h8 F5 z' q$ make install3 ?7 l0 \! A- F6 k2 I. ^
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
+ y0 `# F( \6 g5 q1 @$ x
5 g! z, f0 ~( \1 G; \. {- [6 h- L4 K0 Z) d, T) e) ^
$ ls /usr/local/modsecurity/lib& d' Q+ S' H' ^7 R2 A* t
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0' }& P! t( h; f: T" i+ m
编译安装Nginx并添加ModSecurity-Nginx Connector模块
; j! ]$ z7 W8 F9 q0 e; J( |使用ModSecurity-Nginx模块来连接LibModSecurity* T6 N* a7 h$ B
/ |6 Z" j7 ~( R% }; L' }
5 J2 Y U, r0 q$ \( Y' {, A$ cd /root
2 E' ^% y; |- [2 P$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx! ~- B8 `5 w! l6 _6 K
$ tar xzvf nginx-1.9.2.tar.gz& C5 { R$ Y6 P# f
$ cd nginx-1.9.24 E7 A4 Z; A9 V1 S5 e% ~
$ ./configure --add-module=/root/modsecurity-nginx, p* m3 J+ R3 r, F# E) \$ y
$ make
. \6 g- M& k, w) J0 ]2 U, s. L$ make && make install7 J1 E) T" f* Q7 B* O, X) o
添加OWASP规则
) _9 {( V, ]9 ~8 oModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
4 p3 ]7 a, q3 X, n7 i h: N5 C1 _; n. e5 c+ @9 c( v* O1 h
9 o/ W7 C0 D/ b* W
下载OWASP规则并生成配置文件
" F: `. t( C0 ^) b/ R8 n3 ^$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git$ |( ]) E3 W# d
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
- J( Z$ m: h; ]' S" N* r$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
\, E9 ^; }- y9 R G* q$ cp crs-setup.conf.example crs-setup.conf
/ r& Y. W" \; e配置OWASP规则
9 F' o- [* b9 v' W/ }* T+ B, Z2 W编辑crs-setup.conf文件7 s q1 E! w: J% y
, z* ]; Z3 m# }( N
0 E: `& D8 g7 E$ t% p8 b* |$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf& y Z2 B. U/ j
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
9 }, f( y) D' M$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf5 f3 V$ s4 Z; |
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf. D6 f% j; H, v: _8 a0 d" }( F3 d# x
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。/ L2 C. o, O7 k* M
9 i8 U, U0 w4 q! y% J9 C2 c0 u( @# f8 D
启用ModSecurity模块和CRS规则
+ G- l/ u6 }" f% h3 }) l复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。9 f( P; y- k' d6 L% l
" X" d/ B" s6 j
+ J- h% ?4 L. L
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
& K1 E8 z: q2 u9 B" S% [6 p# T9 s3 O- t5 H& X# L- F
: J" w/ C+ l6 z, Y, _1 }
$ cd /root/modsecurity-2.9.1/! ` b: }& Z& M) W
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf # `6 j+ g7 ~ k* u- e: N* L- p
$ cp unicode.mapping /usr/local/nginx/conf/# q+ g7 c, _+ d' L% g
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
- z& O" L. x* P r
) V. ]: S3 e9 m- a4 s. I$ ~4 ]. L# N% s3 T
$ vim /usr/local/nginx/conf/modsecurity.conf. }: j8 U( S2 L( M- s2 |4 o: r
SecRuleEngine On
. E0 H+ r, K) N4 p6 nModSecurity中几个常用配置说明:: B& s4 ]* g% r
0 K" d1 B9 l+ w2 i k# ?
+ E( T$ g. |( Q- {/ L' I1 B0 p1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
9 _+ ]: J( k! F( T$ o) X# b, `5 L: A4 m9 T8 Y; n s7 |
. ^/ w5 n9 I ?& l
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
7 A h: G; j4 A+ F
2 P! B. \# n% `! x5 ]
4 b R" q u+ ]9 ~! o- L0 }0 [3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。 s1 t' w+ c6 y- A
' Z: F) Y! ?* U7 q5 @" s G" B& v3 E, G' ~2 k% Z
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。. X: {# M2 i, d' [
" I+ o/ @9 O I6 ]( [( G
% P6 A6 D3 Q5 v g, M3 K( N
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。! X+ r2 H% [' @! b/ m. E% E
9 t0 m' t$ L' t. L/ d' _
2 Y2 u6 N' T& m w( Y
3.x版本CRS1 Q& @$ j f- F7 l9 m4 v9 _
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
& p- |8 X3 E6 w& @# 生成例外排除请求的配置文件6 M3 n9 |+ O+ ^1 i
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf) t+ o3 \6 h4 N+ V
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
5 p) S% P' S T5 q' ~' N$ cp rules/*.data /usr/local/nginx/conf
* I/ U" D6 W- N为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。 H" p4 n; i% ^5 x2 o) [; i ~9 x
5 F, N: y3 O' \. R- S4 y' U; B1 u1 C( p5 O k( j
$ vim /usr/local/nginx/conf/modsec_includes.conf' `( {2 U+ i+ I& v1 [; `- u
/ s) D9 H: l3 ?; ]8 A1 v8 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
$ b! i6 s. \" ~! T4 q5 q; t5 ^5 H4 i& m9 A) I1 j! a, w# e1 s4 `
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。# v+ X/ F S9 W/ g
7 q& J' ^; Q) F8 _, X
( O7 k; M8 ]+ j
配置Nginx支持Modsecurity
( l/ S, v: m* W1 f4 t8 |启用Modsecurity8 e1 H. C5 u' Q
使用静态模块加载的配置方法5 |. J. S( m5 b" u' C) A
在需要启用Modsecurity的主机的location下面加入下面两行即可:
# `3 p, Q7 d R2 z7 f
% y9 L/ P! N. m+ \$ I X% Y+ s9 m3 x& k% D% j# ]: z1 X
ModSecurityEnabled on;
% {! ~% C* X0 X1 @/ l" W) M% m# [ModSecurityConfig modsec_includes.conf;1 @8 o* ~( X- S6 V7 {( E- F
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。8 s( T5 [/ Q# k0 R; X* f
7 V& G9 P$ b8 z0 i* ^ r
0 I, ~& T9 S* w) V7 h: F; }$ vim /usr/local/nginx/conf/nginx.conf( {! F& e9 S+ ~8 y) Q# t
$ u5 A& X! x# l& P8 Y1 k% M
5 w# a3 J0 C/ ]( g' P# Lserver {
: T: s1 ^- _! B$ B7 F2 h listen 80;
9 A3 O* I6 G [5 z" } server_name example.com;0 ?8 J/ j, A7 o# B
: b: J" w( G p S* m: B9 r, |) p" S8 z, s0 `
location / {. i6 \/ ?0 z. y+ o
ModSecurityEnabled on;
& R1 _2 m5 L% g ModSecurityConfig modsec_includes.conf;
% R- @; y( Z( Q1 j6 @) }! R root html;# S' X4 v, b5 t
index index.html index.htm;
9 m; }5 t" u. c4 \; p }8 @/ B3 J( u3 H: p! Q0 \
}
& X, @* b# d% t0 w) \0 P/ K使用动态模块加载的配置方法( [1 N s( r" q) i7 W* |# b
在需要启用Modsecurity的主机的location下面加入下面两行即可:$ u+ P8 Y& G( \9 A, g# ]
! F# j. B6 u8 g; M
- h+ f1 }0 h2 f( d( T6 f7 Gmodsecurity on;
; q2 U4 y0 L, C/ Nmodsecurity_rules_file modsec_includes.conf;
% R# B; x8 d3 [. H7 a% r. O修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
! H9 G" B! \' u8 x7 M- l/ U) Y5 z
# I+ d! W8 D- X$ vim /usr/local/nginx/conf/nginx.conf
) ?1 p: ]( a7 w! F0 Q |" U' M4 a! B5 v
" g3 C# Q4 C) E0 |9 i* H: r6 @3 kserver {$ }$ Z8 h' o3 [( m
listen 80;
; \( v; }1 p$ W( U server_name localhost mike.hi-linux.com;/ N: `2 T0 g7 r, X8 G
access_log /var/log/nginx/yourdomain.log;
0 Z; H) n4 y' L8 t
* Z- A2 G. A/ [( T$ q4 {5 H N: L. ]" _* A4 o; }4 t0 _
location / {
) {8 t8 g. h* f' V# V( b0 c$ t y; }0 |7 R3 d- f
7 C/ c5 |2 ?4 v0 l modsecurity on;5 z7 r* t7 ?5 f
modsecurity_rules_file modsec_includes.conf;
' p# V) g4 P/ q4 ]% O root html;; D0 G" ~ [/ d& [7 G% R
index index.html index.htm;. {. c0 H- O: K7 J
}
4 U0 Y) L" K5 f} ~$ i8 R5 u v
验证Nginx配置文件5 [, W) Z- E8 X2 w5 x3 K; T
$ /usr/local/nginx/sbin/nginx -t
! n/ j6 Z7 l8 y; V* cnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok$ l+ d: c! a/ A; K2 G
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
% I' X- i6 {9 Y5 j启动Nginx+ A/ r5 H- m/ M+ a/ u m9 S; g* ?
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf+ n0 }0 W% r' [
* Y/ h$ q+ M. q) `6 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能够战胜更多复杂/未知/混淆的攻击模式。
; I+ ]: i/ b; ~/ E3 y |