ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
4 \& h4 F8 R I
% @! O* j0 [% W4 p5 | t
' y; K& P. I' G/ Q6 r3 X在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
$ N6 G& D$ P6 S7 T/ ~0 Z$ d+ D; _9 P& ~9 F
& U5 G8 a P7 e, c+ k什么是ModSecurity
5 B8 q3 K4 ~8 `- |, ~7 LModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
: O- L* E! O: ]8 @7 D5 H8 f/ i
4 y% D& G; g+ H6 y$ `& E' M9 S* I: Y
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
# E- f- q' b0 [( l; K) q. P3 ]
1 X8 _' k: p. S- B
9 X" P$ t$ `; X I1 FModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。% a0 [2 ?$ f1 c# H1 b# `
; o- R1 Z! ^: f( m3 K2 Z! O* v7 {- W
官网: https://www.modsecurity.org/2 _% ^/ A+ b3 L
4 j' s+ D9 ^) d2 U6 q- l
% o( H( q# T4 f+ ]6 v, \( I* H9 W* Q$ q什么是OWASP CRS
0 s4 m0 C2 ^ o9 TOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
6 E. ?( a7 M6 k
$ _' g/ O2 @/ c5 l9 E9 F/ D1 a
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
! B" h0 x- l1 ^) U N: u
8 t; T# G% |! O7 ?1 Z! d* y- p8 D! e% B0 D0 K/ T+ V, [
HTTP Protection(HTTP防御)' G& k: n1 L6 W) r2 H/ Q- b( y
HTTP协议和本地定义使用的detectsviolations策略。
' `) k, c8 x) o3 h/ b! R" t& D0 Y4 O W" |2 S4 D! R
; v# P' X/ r8 } d# }3 c* m$ V5 AReal-time Blacklist Lookups(实时黑名单查询)
3 C# ^" K+ D" W利用第三方IP名单。
; P3 |' o# W; L! `% j* K
, J% C3 f# m1 s* M, K+ f4 y9 c
' ]$ u, [% p" }" u+ wHTTP Denial of Service Protections(HTTP的拒绝服务保护)! i1 ^, E4 `- B1 H1 i, k+ y' W! S
防御HTTP的洪水攻击和HTTP Dos攻击。4 X8 Q, `1 U* d6 f& j6 l
% H0 {2 d! v. r' e
" e$ Q& i7 [: N& v% q- l7 i, S/ [Common Web Attacks Protection(常见的Web攻击防护)6 {+ } W) c$ ?9 F. d1 E; A
检测常见的Web应用程序的安全攻击。4 W" _% F+ k. f% m
' q4 H; b: Z) K9 S0 ^7 p- e6 ~
$ A; p7 T0 L* F, T5 }0 iAutomation Detection(自动化检测)8 M: R5 c( J: ?& S5 j2 q: \
检测机器人,爬虫,扫描仪和其他表面恶意活动。; N( }/ ]# r! e" w
U: `' x) f$ w7 L
1 V9 X1 i4 x7 R6 O( K, [Integration with AV Scanning for File Uploads(文件上传防病毒扫描)4 h7 I- ~2 Z8 ~; G
检测通过Web应用程序上传的恶意文件。
2 ~+ S5 q3 P5 W# s; d) R V8 M7 `! z6 ]- d
5 q$ Y& G0 a e1 e$ N# K/ CTracking Sensitive Data(跟踪敏感数据)$ p* V9 L/ @3 ?2 r: C# E
信用卡通道的使用,并阻止泄漏。
+ i4 x1 L5 Z$ i
5 x: I/ t1 m* u7 ?( a# q7 H/ r6 ]3 K0 F9 w6 R7 |
Trojan Protection(木马防护)
: Z% R# }! | b. h6 j: e检测访问木马。$ G/ P3 s7 y% I3 d! D3 S7 A
4 ?) y' G& |5 j+ l" i' Z; h, U- p! [$ r y& U& B5 z
Identification of Application Defects(应用程序缺陷的鉴定)
* L" @' W0 u4 i* l, [; {' s检测应用程序的错误配置警报。
9 ~2 w: S+ T2 s- k6 L5 O5 e4 k" ?% a* b* A( x, T3 H% d
" w" Z# q- d1 S8 D9 }& F KError Detection and Hiding(错误检测和隐藏)
& Q4 t1 x1 \( y1 q检测伪装服务器发送错误消息。; X) Q# q2 `9 O3 H( R# M9 T/ H f
2 p3 R3 X/ G4 W8 ]
7 D: ?# b" J3 A
安装ModSecurity4 t" [* g* c7 s! J2 I3 Y3 C! v: D
软件基础环境准备
" h: x$ ~, V# n5 g* i0 q4 h下载对应软件包2 c, ~9 S% p& l! Y- b9 y( q7 U, K
$ cd /root
9 [+ G6 w4 h% Q$ K$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'2 @3 L2 f/ a' N; O
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz: X7 ^4 E2 V% R) c# o; b0 U
安装Nginx和ModSecurity依赖包
: ~5 P6 x: K7 F4 t3 x; ECentos/RHEL0 j8 y# f* j% n' c, K3 ~" \
* Z2 m- S" q9 F$ l, S* j6 J8 y
6 B v) ^1 ?4 ]$ |/ M x, k$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel" R! j5 {, I% l/ v9 v% j8 {* I, |
Ubuntu/Debian
$ D5 K0 ` a/ C* P% m% B: {
% q3 Q$ a( ?* b! k
( p; `* z/ u% z+ X3 T- ]$ 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
& E* {: J! |" N+ S2 t编译安装ModSecurity6 L& m. Y7 Q3 ~* ~3 K
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
: S5 _) l: s, f! _4 }( s6 `
0 U8 M- o% M- n1 J( ]+ _3 J
3 J1 C; U. u y( H; j* I0 a方法一:编译为Nginx静态模块. e/ {7 U0 y* p* M1 g1 i7 s! V
, X: R; d+ p* }
+ }9 c! e* S* R' H! e编译为独立模块(modsecurity-2.9.1)
( j$ [9 b8 _0 v; R$ tar xzvf modsecurity-2.9.1.tar.gz
# ^! [6 |% z2 N3 {$ cd modsecurity-2.9.1/
. a( X9 u7 d; B' b7 M3 @1 `% m$ ./autogen.sh
6 q( g. k6 L: |) V$ ./configure --enable-standalone-module --disable-mlogc7 `" r8 s( ~+ w3 v3 r, L
$ make
1 _3 Y* d2 ]& ], o K6 {1 N编译安装Nginx并添加ModSecurity模块+ b! T8 M! o" _8 ]' F5 P
$ tar xzvf nginx-1.9.2.tar.gz: i/ k; G0 c1 L& `8 g4 D* c
$ cd nginx-1.9.2
- M* J) x! u" M, ^) x4 Q$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/* z. y1 s! |2 ^- X
$ make && make install
6 n: ^3 H: M& a方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
H1 [1 K! J+ a" G( t8 [, Y/ E7 y0 |; e* c$ S+ a8 Q
2 L. I3 u3 _0 {3 @0 @编译LibModSecurity(modsecurity-3.0)' Q2 S1 ]6 ~: K8 M) U4 V& ~' R
$ cd /root
4 S9 ]' p9 ]$ o0 i0 I. ]7 q% }$ git clone https://github.com/SpiderLabs/ModSecurity
" F: T3 g7 B3 O, f7 M. g7 ]$ cd ModSecurity1 x; B0 o( J# P1 a
$ git checkout -b v3/master origin/v3/master
; y j0 F; [) c. G( F/ K6 M$ sh build.sh
6 n* w- i$ f; n# F$ git submodule init
1 C. x- K1 L5 ?1 A( S$ git submodule update
! ^# Q5 G1 _# B% r; U, h$ ./configure% E" i% i$ y/ [8 x/ F
$ make( u: L' X2 T9 N* R; K. M/ N \0 F
$ make install
9 G+ l$ d/ W+ i! C: [; I6 ]) ~LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
$ x z. H% v0 Z4 X/ h; g; C3 s' q" ?4 X4 X
' E1 L4 L/ b* ~
$ ls /usr/local/modsecurity/lib
! C- U: G9 Z' B) M | nlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
- F; K. P. Q+ `; c3 ^: |. D编译安装Nginx并添加ModSecurity-Nginx Connector模块
- K, t6 [+ T) `* l6 A使用ModSecurity-Nginx模块来连接LibModSecurity
3 ~. l& A9 g! P$ W. `( ?& \2 e, K3 }; ]8 t, y
6 f. p$ D" i- K W0 P$ cd /root
( g( z# l* `7 S5 n4 f3 v$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx$ f' y2 j' P$ f$ ^! {
$ tar xzvf nginx-1.9.2.tar.gz
9 V1 e- I/ s4 {6 T! M$ cd nginx-1.9.2
5 _( r, r& |, n) `$ ./configure --add-module=/root/modsecurity-nginx
" T; Q2 B4 M3 L0 n$ make: f$ s1 ^% V; K/ v0 i8 T
$ make && make install r8 k7 Y- X& ?3 t J9 T8 C
添加OWASP规则
* }/ L9 g0 ^# b {ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
% F2 b- w+ W6 e4 ] n b* r' y$ d+ k% S6 P$ J E p
3 G" o, N, W4 T6 i- |- X9 q, f6 U. p
下载OWASP规则并生成配置文件
9 ~, k) ?) ]. e2 x! ~2 J( f5 @$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
[( C! r, ?( l3 U$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/; n0 f" ~0 M1 P- y
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs, j- q( E4 u# ?5 i: |' f& K5 r, s# g9 a
$ cp crs-setup.conf.example crs-setup.conf5 d3 R3 h$ ]) _1 ?* H
配置OWASP规则7 j Z2 W7 E/ T# m6 o; [
编辑crs-setup.conf文件
4 t9 `1 |, o% b( m" E# n! ]+ X6 k
- t" F! |5 y2 a* v$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf9 F; P. g4 O! `+ O9 V0 ~
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf- Z8 a" G1 d$ O) O
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf D% }+ c6 [2 ~8 Z, C" L6 Y& j. c
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf5 f- o# a0 \3 d
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。& `2 w+ W M. \! _( k7 R' s2 J
0 ?% t! F. J0 w9 }6 C# h, H( p2 K$ t& i8 V _) d7 f
启用ModSecurity模块和CRS规则
5 |" D+ q+ |8 B( ]$ h复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。( r/ N2 O6 n3 e N
' Q% U& r9 K* b
6 B" `( `3 E0 ]- |' {/ Omodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
& o# X O9 ^( L9 p3 P4 _; a$ {# N' z0 x% T
1 ^% P$ I( ^% l4 _! g
$ cd /root/modsecurity-2.9.1/
! n4 m( N3 o. @$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
% _/ U: h0 a/ [3 n; ` o6 I$ cp unicode.mapping /usr/local/nginx/conf/
& i1 F( Q9 A* w9 k( y将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
/ k, i' z: l0 j1 i3 C8 ^0 w* m+ p2 u3 D1 D
+ I2 N& D$ ~% f. Y, n8 D* Z
$ vim /usr/local/nginx/conf/modsecurity.conf t+ ?4 k! Q( Z* x
SecRuleEngine On
# B2 w7 l% o" b, V! k5 R' U3 \0 Q/ }ModSecurity中几个常用配置说明:5 d) Y- S9 l9 o( B7 D* B" m0 O
6 ` ~8 F0 i. n% r2 w- c
+ H3 l7 P k5 x4 m F" v/ h3 K1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
. g" f% T" C2 B9 {* A
0 C/ Y/ o1 N$ x. o4 e& r+ @* y/ u& C8 b# q# L
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
% l6 n" [* W% g3 w/ U1 A* r% K; z8 s- w3 t
; ^8 g- ^2 C h" ^6 m8 o9 K1 B
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。0 `( y" j( f! k4 W+ T' s H7 Y6 \! g
* s7 B6 v7 J+ L% n, o$ j7 l1 h! b1 W- O% U" j8 t
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。$ k( ]& U6 H% f$ j' M
. e" f% e$ a9 q1 B2 s# u: z/ F5 ?8 c9 W# }4 W2 X* q4 ]
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。% C. q- u) |- c: o
' L' I |& y/ a5 R7 s0 K0 ^
% p+ R1 ? J/ A# f, V6 {, `% V+ ]- ^3.x版本CRS
* c7 O* o/ I9 D" P2 Q$ cd /usr/local/nginx/conf/owasp-modsecurity-crs3 T5 r r1 P1 O7 o# I3 b3 G6 X2 Y
# 生成例外排除请求的配置文件. ~, e' @1 V# _0 X$ J
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf! o k9 ~! ^2 m: V* O: {1 l
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
7 E. z+ Y5 O3 A8 A8 M$ cp rules/*.data /usr/local/nginx/conf
3 Z6 B6 `; f8 p- a: L2 v' p为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
$ T. r- s% Q2 K N p8 \" ?0 c& }' d- F: H8 w: o2 K. `' O5 `/ }
% T) G" W& n* S8 B$ vim /usr/local/nginx/conf/modsec_includes.conf
3 E1 V4 e; a' d; v5 h" F6 o5 ]9 k
[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
" n8 e! v. y, H( `7 F: i( G# W3 ^7 s8 @. D9 N( p, V2 \
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
# d, N# V8 ^2 y( y9 s: |
4 z" [# j( y- a# N8 i
8 F! g; e: G4 p0 R+ K* {0 l0 f配置Nginx支持Modsecurity
1 Y$ T$ W& G0 \/ ~% k9 _6 b启用Modsecurity4 Q+ ^. R8 \# B5 t2 m
使用静态模块加载的配置方法
% }: e8 c! h7 F6 a' E在需要启用Modsecurity的主机的location下面加入下面两行即可:* T4 }' m) ?9 C- j" ~" u
- z3 e# j$ S+ ^; Z7 u! v. d; b5 A& o6 ?
. a- a# ^! n: [4 o5 I, C
ModSecurityEnabled on;
" J6 z" j" m* M* I/ u; x( HModSecurityConfig modsec_includes.conf;
8 k, q# e: T! a修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
0 Y" ~& T8 P* a( G# T$ N3 D) g/ M
3 d' |3 A' x0 b0 {3 J6 b. N' k6 y! P% ~
$ vim /usr/local/nginx/conf/nginx.conf
# [/ b4 b/ I: x1 B+ U! F" L, s6 ]5 i! [
& O0 u5 C" [: u5 F! A2 mserver {
+ v+ v1 h" F5 s6 ^0 h listen 80;, u/ j7 l5 a2 r; l( l3 D' e4 y
server_name example.com;
+ A* I W% C- w4 i* o) u: |2 {$ {. q* D
; O5 y4 m. t+ @3 C
location / {" W6 c( |2 F/ N
ModSecurityEnabled on;2 s& h1 a& O2 N( O* f# L
ModSecurityConfig modsec_includes.conf;* C% _5 t" m1 t6 D: g$ B' R
root html;: h. [0 a7 B6 ?0 o
index index.html index.htm;1 q" t. O \4 I2 E
}
0 }) _; t5 ~; ^! A$ O5 F. a}
6 ?9 N3 X6 f1 o使用动态模块加载的配置方法5 \1 z" H* \7 u' Y; n
在需要启用Modsecurity的主机的location下面加入下面两行即可:1 j. S: v! o2 M- n6 R$ S
) Y' O) [/ r6 [; Q+ |
: l d3 I, w0 n6 P; vmodsecurity on;
' b, j! p- Y" B$ K' t% e* Hmodsecurity_rules_file modsec_includes.conf;. `. \6 L6 H5 B
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。4 N/ A% R L6 {) K
) V, S% D3 Q% B5 T2 W
; |# m9 K K" `4 U) i$ vim /usr/local/nginx/conf/nginx.conf1 N% b5 o9 E3 s3 x* I3 l
' @% l' G; v; o; \* V: }4 X3 Q% J* j0 B/ I f: F
server {
8 |! r) a; x6 c listen 80;
! t# v! Z# N5 k. n, L c$ N C server_name localhost mike.hi-linux.com;
! X u X4 @* t: {( y( v access_log /var/log/nginx/yourdomain.log;6 K0 }3 W2 p* N; K; f' L* }/ l
X$ m: [. l6 D) {1 b
6 P, N. y6 x& n: e- s1 ~ location / {, m/ D& O/ Z& q5 C( P
) ~4 }, h, O( c9 k, h5 @) W4 W+ R
modsecurity on;" B( N; l$ O; i6 k6 e8 r/ _( |
modsecurity_rules_file modsec_includes.conf;- z9 E6 c! `1 j+ u" L
root html;/ t& d5 L; ?! ?. c) D2 g! I
index index.html index.htm;
, F* s1 s) F- Q, i}
; U3 |( N! D( ?$ |8 I}
/ H# Z) g/ v# D2 r8 i+ L. ]验证Nginx配置文件9 ]+ Y, W% C3 h) O: A' B
$ /usr/local/nginx/sbin/nginx -t# Y, v" t! J0 ~0 P. |" t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
9 C: z2 t* P( @nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
0 K. E8 {! C; q! C启动Nginx
1 b& V1 ~6 y0 _# T' s- G& M& h) {0 O$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
+ ~+ B ]4 @& Y( B. P& i ~: Z
! D4 _$ n, u* C测试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能够战胜更多复杂/未知/混淆的攻击模式。
+ R# f d& _* L/ H |