ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
6 \: w3 J( Y; ?; n
) ~3 q1 _7 {$ |! G
; K8 n# u: k# C! J在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
5 f% x9 o. Q2 ?, b
- I) Y4 P* P& X t6 A9 P$ i' e$ [% |3 _6 @% i
什么是ModSecurity3 i* D9 }2 {6 ?
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
! {1 f$ d# V* q: r3 x' c3 I: g: c% B& A# U- R& [- k. ^8 `
* G6 o) t" J6 l: D9 N
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。( X8 [ N2 u8 r5 u& f c3 n3 h
6 n1 I3 c$ g* }0 r4 p( w2 s3 y6 M. P0 _ j2 e" \9 v h& n
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。! f* b( X, m1 s* e [
1 r- I3 s6 T; Y+ I
/ H% ^ H9 U( Z9 Q" U) R! Y官网: https://www.modsecurity.org/. o$ B4 h9 W( F6 C
% `5 x) |$ [* l" V3 S8 S& [3 @9 |5 I5 A! c- w6 s1 L$ Z
什么是OWASP CRS
% ?: i" T% Y' Q/ `; hOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。6 \2 ^) x! X( n7 E' U; X b% s+ d7 ^
: r3 n! n L" ^* `6 M
; v$ n4 i4 D$ E7 A& g" Q
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。5 I, s7 r& h4 s( L
' B7 u. S$ {* C$ s3 ?/ u
; \: W6 B: d! S& \5 KHTTP Protection(HTTP防御)( _' F. K1 c- A8 @# M; Z8 A
HTTP协议和本地定义使用的detectsviolations策略。, w, A% @( O; j2 K: [
' U" {1 f2 a/ a2 T+ O
) Q+ X- j. B* r8 J: _
Real-time Blacklist Lookups(实时黑名单查询)
6 X+ o, |1 \. ^ c0 u$ D& r. Z利用第三方IP名单。! }+ t' j) ?* o( Q4 ]' c
% d$ Y0 o0 l5 v5 M4 G6 h/ W+ J
* s" B7 {/ \4 m5 ]
HTTP Denial of Service Protections(HTTP的拒绝服务保护), Z+ b& m& T/ U' P( a) _
防御HTTP的洪水攻击和HTTP Dos攻击。
) k9 e2 z4 Z3 U/ ?9 Q1 H: ?
! f1 Z: n" _2 o8 Z1 y% |2 I6 y. D( g2 Q7 l7 u( m- O
Common Web Attacks Protection(常见的Web攻击防护)6 B. V& K$ \3 I4 D( B
检测常见的Web应用程序的安全攻击。& z5 `! u& v. H+ ~
& X: A8 N) W% C+ E& W
2 N0 d+ I- c: g, f& Z, ^
Automation Detection(自动化检测)
' Z" z% U* v7 `; }2 y2 a检测机器人,爬虫,扫描仪和其他表面恶意活动。* o5 N: k8 Y! X) c9 `! B
/ ?" T" c$ G4 a) V4 ]7 T. ]
2 s& m r+ I. H; x- \Integration with AV Scanning for File Uploads(文件上传防病毒扫描)) M" q4 B/ P$ I3 J# Y* j
检测通过Web应用程序上传的恶意文件。
0 [5 i- ^2 U7 t' T8 A
$ C) E: _2 |8 p/ Q3 k9 n& S" v( c& V8 S- D
Tracking Sensitive Data(跟踪敏感数据)
. L: R! o! k3 N5 P) i% q信用卡通道的使用,并阻止泄漏。# e3 Z0 b3 k3 i( X9 r( t
. @. [7 c4 ^2 K" Y# T
1 n- z7 I9 |6 q" R+ j2 F/ E; dTrojan Protection(木马防护)
+ v6 N9 o5 M1 P8 [& [# E检测访问木马。5 C) ]' R# |# W
' M% }: x7 Q2 ~- K/ n% |
! i9 Z2 d) I8 U) w7 y% Z3 _6 r( L
Identification of Application Defects(应用程序缺陷的鉴定)
; Y) ]0 P3 H' Q5 O% k检测应用程序的错误配置警报。; k2 X9 q# \6 g# l/ L" T
: d4 I. S' e0 m8 a& w; |4 }! B
! Y/ o2 h# \# R. j* J. G6 w" b
Error Detection and Hiding(错误检测和隐藏)
! c+ S+ W9 Q2 W9 d! H9 m0 M! W4 X/ [检测伪装服务器发送错误消息。& ?# s; V% C& d" C4 f# B5 O- m7 a
5 W9 Y6 f/ I& t g
6 I) [7 T8 W/ L' p% t4 D% K) i. m, u安装ModSecurity4 \" E5 f, m/ q# O. \
软件基础环境准备
$ `( i1 x/ n) S* M) P& o下载对应软件包5 S5 B( x0 Z, |+ `
$ cd /root
7 |) e3 c \4 ?! [$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'( ? F( ?' g0 ?9 z8 j- 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
2 T; g/ K; \; t2 Y5 C8 ]安装Nginx和ModSecurity依赖包 @* v% U% K3 }9 H/ ]# d0 F
Centos/RHEL
5 L* w B& ?: L! F" r4 C; R! |
- e1 R7 f" @9 {0 D, i% E
! |" H$ i' z+ ?, B$ N: C$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
+ U% a$ T! g! S: Z3 tUbuntu/Debian
4 H' c, i0 U7 f; C; e1 L3 _. z' J1 J: l
7 w3 I0 ?0 M$ V: L$ B9 n. c/ e
$ 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( P* }9 s% N/ { e1 U! I
编译安装ModSecurity
% E$ H3 C3 ]; P, ]9 b( TNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。/ }" a7 N8 Z0 n, K; l9 H0 d2 {! ~
6 z! W, D$ A( s( ]' a
- T# B2 a8 a: K0 {6 `5 Q4 N3 M
方法一:编译为Nginx静态模块8 J/ D+ j' B6 [$ g) [* O$ U
9 f) f" M* q L1 h
+ q5 \. J8 ]& l/ v3 p5 O编译为独立模块(modsecurity-2.9.1)
( r& M3 D0 w& T0 x3 q$ tar xzvf modsecurity-2.9.1.tar.gz4 L" Z" Q& @0 D% M7 x7 h3 A
$ cd modsecurity-2.9.1/0 _* O* N0 k4 \4 z
$ ./autogen.sh; \5 Z1 b1 G7 F( Z/ b# C
$ ./configure --enable-standalone-module --disable-mlogc
1 |+ N. N$ I1 j3 I1 F$ make
: Y- X8 l M2 t0 b' }' O7 ^. s) Z编译安装Nginx并添加ModSecurity模块, M7 A. h2 j6 y
$ tar xzvf nginx-1.9.2.tar.gz/ J+ B$ K* p7 X- O ?: ]+ E
$ cd nginx-1.9.23 y- b! a# A! K" J' S2 S
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/* v5 R" K! p2 L% p( b7 b$ t
$ make && make install' N! X; h, v& w: W' q6 ~' j
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
, X+ p" R5 H$ I* M9 W9 S) g5 a. ~" _ R1 O7 [1 w8 f
& p8 x! H9 h' z4 D+ h编译LibModSecurity(modsecurity-3.0)$ J1 ^0 t) Z+ }7 X7 }
$ cd /root
0 [' y0 @' G( P6 m$ git clone https://github.com/SpiderLabs/ModSecurity
( ?+ a/ v( m, g" ]; }$ cd ModSecurity1 z) N8 \9 o% p D( V: y" h
$ git checkout -b v3/master origin/v3/master: M! u9 E! ]9 s& \5 x, V( \& q
$ sh build.sh
) d6 C# T& f( V) F1 Z& ?$ git submodule init* d0 h* o4 I- w; N. p' T/ M
$ git submodule update
- r; Y4 p; Z, F( @$ Y3 o8 V6 k$ ./configure
+ N0 n: J* d) H5 A! e! l- _, B$ make) c1 ^9 h6 I; }# f7 d
$ make install
" e9 p3 q, h$ g) T! jLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
4 C- t- f! \4 }* L0 U/ c4 U& l/ ^* |
1 p) S. g; L) C ^$ ls /usr/local/modsecurity/lib
8 ~- \' v! x# D: W+ Glibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0$ }* N8 ~: V; U$ z+ z% l" _# @
编译安装Nginx并添加ModSecurity-Nginx Connector模块
2 ^& L3 J, S4 ]' U; H, H使用ModSecurity-Nginx模块来连接LibModSecurity j5 k" _# \5 T0 g8 Z6 d: _
2 H. f1 ^" Q' j2 T
- u! m5 X& Q) i- H1 j$ cd /root6 w" i" G. e V9 O* n/ \; e+ F7 i
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx6 s( Z- b9 H0 c( u
$ tar xzvf nginx-1.9.2.tar.gz
; [ Y& J1 i( z7 ]) q5 O# ]& x9 Z$ cd nginx-1.9.2
6 F, Z+ A6 y8 S$ ./configure --add-module=/root/modsecurity-nginx
1 ]6 F. S' G1 C- n8 h( V$ make
7 p( H$ ^# s: H+ D- k, k' `6 [$ make && make install: L& d0 c! V# j! }0 d2 m7 k
添加OWASP规则
; @# h9 K' a% h& k- FModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。4 p# b$ C- X( C* f% I' g
% t( y# b }6 e2 e4 f5 G- J
, K2 M+ T* \8 l( h' O* {' H8 s
下载OWASP规则并生成配置文件
& w+ ]9 c, F% r& r9 g$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git5 u$ P" I" T8 N" u; @; N4 A
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/ o' _$ } u7 j' [+ ]
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
+ |4 g5 j' R- V$ cp crs-setup.conf.example crs-setup.conf0 e8 K5 \# ^2 b+ K( B
配置OWASP规则
8 J, |7 `) l7 A8 {编辑crs-setup.conf文件/ |" F8 r5 Z, m5 [
/ q& @% J* a) M. p; {. [2 W! n0 y1 z
' Z2 a! h2 s% K |9 X- |
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
: {# A2 z& T2 q2 A0 L9 _$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf* W1 z3 P$ |& X5 T( n) ~, I* _
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf) j- }! P+ s9 j
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
) O* y# D" S: r默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
1 Q" Z9 ?' F% J ?. @. {1 `- h
6 {4 _; p: s6 a7 Y
7 K8 R# t; g" @1 ~启用ModSecurity模块和CRS规则
- |7 [' l+ z9 Y0 a8 @复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
, M" k: y. v1 |8 D/ s1 E @( B- e4 s7 l, W# Y* w
' P& V4 Q3 \6 t# M: p# Smodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
' J; |4 E8 T( r/ r1 K# ^0 i" F. z% J) ? `0 `8 z% G2 k
4 ^; u7 S- V) N8 h: r" e1 `$ cd /root/modsecurity-2.9.1/" f$ H2 o$ B4 R- H
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
: R4 j3 j0 }' U: P6 p6 X$ cp unicode.mapping /usr/local/nginx/conf/0 U. F! R$ g% v$ ~# E8 M
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。1 I0 U1 l0 N6 ]
! {6 T* A0 b" w6 K i/ f0 z! F8 i" K: a% N1 g) Q r5 g- E
$ vim /usr/local/nginx/conf/modsecurity.conf
6 m: Y4 a- p$ g, `0 ], M$ j8 q! {SecRuleEngine On
! v9 r6 d% q" a- \$ ]ModSecurity中几个常用配置说明:1 [" b+ [' ?/ A% H
$ p" Y0 H) }$ i6 _/ w
& D4 z' T8 }& B6 v; M& n5 e1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
& }2 R) K- E9 s9 ?1 f6 P' s2 h. k3 y. x9 ]3 n: p. G) Y
+ q; A. W; h& i/ J( P% m
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。. o: x7 i6 V# A% B
* q6 S/ Q. j2 m4 L# X$ N. u) i
( U" l |! [- X1 t' l
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
: R8 e0 ]) H) ]: Y4 G7 D3 _' U8 p2 E% b+ g5 c5 M3 w1 z3 I
# n' o( L! s2 r! d# T4 R' g3 L2 Y
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。* ` W# S# j; y. D+ r5 Q! w
+ v+ }4 ]0 ^/ q; l1 Y; Q; S* V
. Y' R, w& e# f9 w在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。* M9 k7 b, w7 C! K. S2 a
* v8 b; o# C5 x" P; \. d0 A1 N8 P5 a. {! T" Q
3.x版本CRS6 {4 O0 e: ^9 W; r2 [8 ]
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
) J5 I" S/ T$ G& l b- Z# 生成例外排除请求的配置文件5 i9 u9 [4 d, o" k) R& _
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
4 A+ `8 b9 l7 C) x$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
: i" K! J6 t' _$ y$ cp rules/*.data /usr/local/nginx/conf
' A6 ? K; \0 B" [" B/ Z% \ H) F为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。2 B+ z; z+ B: O
' P# G; n0 y3 O0 q. Z7 a* X& L) \2 v. N6 U
$ vim /usr/local/nginx/conf/modsec_includes.conf, a9 |9 r' }5 [: i3 q
! c0 B3 D" _8 _( o/ V* z[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 $ ~( O$ ]' N/ C3 X; c7 O; \' _1 L
! ]4 l% {# \4 X8 d注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。- A9 U6 v! o) e$ t
$ T( Q8 y! f) b3 z
9 K" [) }- ~( E9 b
配置Nginx支持Modsecurity
: ^- T7 K& v" P启用Modsecurity
: {0 d* ~/ f- Q! J2 Y+ ^0 O使用静态模块加载的配置方法$ y9 V, M* ?4 D5 }- B
在需要启用Modsecurity的主机的location下面加入下面两行即可:
& Q+ n, ?: V) S; B2 s5 R
0 `/ @; V. V6 f- R0 H! J% y6 h8 S
ModSecurityEnabled on;- A1 o2 Z& A1 u k1 j$ E
ModSecurityConfig modsec_includes.conf;
8 K5 y- r8 n! j8 ^9 k" C2 V2 T修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。5 k* d) J3 i; }: ]: r
3 ]6 Z5 t3 W$ c3 i
$ v# u- ~1 F7 l, A! @* S9 S$ vim /usr/local/nginx/conf/nginx.conf& c" T2 H1 i+ y
. f' }+ o) r/ A+ f
- H. K- H% R& I- N4 X+ ^server {" k4 }9 a" s( s3 B# `6 n, I( i( F
listen 80;1 v. s Y! l2 D9 `: `
server_name example.com;
/ r1 M8 R3 S0 q& g9 o9 c4 R& {
7 G. a/ g" B8 c
) D8 l! }4 \% e! l0 Q0 z0 b location / {
5 M; J, Q' t7 q- w; [; F ModSecurityEnabled on;2 @; H% c4 ?+ M# t
ModSecurityConfig modsec_includes.conf;
% T: M9 J/ m0 c' o- K. r$ A root html;1 w+ m$ ~5 E5 Q$ ]( w" Z
index index.html index.htm;7 B4 F7 B2 Z0 q/ B; I2 d
}
) S4 y! d- I: Y. o% c}5 L3 ~- M( k% _% p. w8 J
使用动态模块加载的配置方法/ e" r: S$ a) u6 T p
在需要启用Modsecurity的主机的location下面加入下面两行即可:- [8 x" [( d G. T9 L9 _; `
, u1 b( V- @* `+ i: X& D
! o; k Z/ |/ B( j. M
modsecurity on;
8 p! V- o4 X0 a! p* M. _7 q5 _2 dmodsecurity_rules_file modsec_includes.conf;4 e4 Q0 A+ \" W. y" ]" V7 k w0 j
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
# h. k& M8 q0 I) N6 Q& e3 |+ Q: f0 W
& d e X$ r! g* o+ I
# }! ?( b4 Q( r$ vim /usr/local/nginx/conf/nginx.conf' `, d) U8 ?/ x) M/ p! r
: F1 @ c! [; U# \: w
- \ ~; C" D* gserver {
8 f ^7 ^, n3 W# u6 Q6 U listen 80; Z* S2 B" D- N F
server_name localhost mike.hi-linux.com;
, `$ {% e5 M1 ]: r1 V& x2 o access_log /var/log/nginx/yourdomain.log;
& u1 A, d$ x: d5 ^0 t2 }
5 V: `! R3 E. j/ v, P' x( N! W5 R* W7 {9 a6 t& y7 {
location / {
+ a- @2 A- w5 ^8 H5 w. G; S# }9 [" P Y: S2 P- J
1 s v8 t% H. M- e# |% B
modsecurity on;9 ~ K5 v+ y: Z% S- @% @& b
modsecurity_rules_file modsec_includes.conf;5 S2 T: z3 f" Q- T2 y s! r. {
root html;! h% r, d4 @4 E
index index.html index.htm;, h" X( g% T g M# f
}4 b9 @3 N8 \3 g$ m
}2 ` C; ~* \$ W) X8 U
验证Nginx配置文件. y& M5 l6 W% g
$ /usr/local/nginx/sbin/nginx -t
4 x7 C: t! u# V# R* x" F) I. V: \: anginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok) W. `+ y3 w! n4 A( \( X! r
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful/ L- r5 B1 x1 L! Q
启动Nginx
! r5 J8 T# {, R; _9 o1 n$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
$ p Y; G' K/ j0 ~+ n
/ ^( n6 x V# W1 O测试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能够战胜更多复杂/未知/混淆的攻击模式。
5 O/ }. o7 ?7 T8 E# F" n |