ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。4 b* b4 L l- u, J' L
5 M3 @- x( @5 l3 p3 ? y' Y ?6 G4 D; t$ g2 C$ }" y' b4 r
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
, V0 V9 r$ q* u% T$ ~$ V
% R+ ?5 Y, _5 x: B" ~/ ?" N( M9 J
% ^5 V6 y- k: H9 x& }什么是ModSecurity
8 p( ]+ r7 V) F6 `ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。! L7 Z- ^# d5 q- }
9 |1 Q1 |% c* V5 S/ r: R
! S( F" k* W7 G5 v& PModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。/ [: G0 D3 P. g) c* d5 f- G
( f8 I7 k" w9 u: k$ e5 g9 z
4 w7 I# ]$ f' LModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
8 Z% }5 Z! p- x7 J
( K9 o4 h; L0 i! a" d6 A9 B
& k9 x6 i U! V; h官网: https://www.modsecurity.org/4 |2 q' T9 Z7 G9 ^8 o) J) L& @
0 h" J" A# y: `4 p/ A3 P* \; f* [9 x
( `! z) f8 P! f2 t7 u' X- ^3 @) ?
什么是OWASP CRS# m' w: \" a3 L- R' ^7 r: ^
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。- P+ N4 l4 h3 L& J4 ~, X+ L x; B. t
. d! {7 g( X, x4 ^
4 f1 I( Q& ]7 r; t t( Y
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。! Z$ I- l$ C, y4 \
6 g/ j+ {) a. n f
( m( ~; J1 d9 u2 K* M2 A4 oHTTP Protection(HTTP防御)" @2 ? i$ n5 ^4 M8 A. a* ?5 z) U* c
HTTP协议和本地定义使用的detectsviolations策略。8 l; e& F1 N& ^. z
" H `7 l3 E) m2 ?- G ?% [$ _) R1 |2 ?2 W* e$ q$ j6 U/ k
Real-time Blacklist Lookups(实时黑名单查询)
5 E l7 A% y9 s: R! `$ C5 [利用第三方IP名单。$ i9 U7 |! ^9 p+ | Q
+ I: ^ W% P3 e _
: U% J/ r. a8 G% w" B" EHTTP Denial of Service Protections(HTTP的拒绝服务保护)1 c; s) R4 _6 k
防御HTTP的洪水攻击和HTTP Dos攻击。
C4 D' O1 c9 o6 P; e( I, X
8 a, v0 |! F! t' s! ^9 V
. u0 [# M; e$ |9 _* a: _. o+ d, CCommon Web Attacks Protection(常见的Web攻击防护)' i( z/ X/ _' J M! m6 [9 P
检测常见的Web应用程序的安全攻击。
( p1 h6 Q% A, q& l3 E& e) D3 v& G8 ^# w
: @/ y: { b2 W: ^+ ^+ j, t
Automation Detection(自动化检测)# j/ v8 c/ G0 Q9 c0 n' p! e7 O
检测机器人,爬虫,扫描仪和其他表面恶意活动。
6 ?# W% h( x, B! E Q; R& e) ]* N3 o# l6 D, t/ U! Q9 f
4 q$ J* R: G( p2 ?+ QIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)0 s- h" ]- K# P: w2 g" J1 L( X) `$ Y: q
检测通过Web应用程序上传的恶意文件。
# I, i4 C, c* ~) p4 U* q' m0 I# T1 V
7 V2 K" U& _* B a/ D
Tracking Sensitive Data(跟踪敏感数据)* `% A/ H3 A D" }! Q
信用卡通道的使用,并阻止泄漏。2 s2 w4 U* F! |; Q4 r
7 S8 K: Y' T# o
3 w9 s) T7 g* BTrojan Protection(木马防护)9 n! }3 _; X, B2 h: O: _5 m
检测访问木马。
5 S) S# d' B5 v& X) U5 _! {
" d0 K% S0 z2 ]. [
3 F! o) h7 S* _8 u- @; t" f$ BIdentification of Application Defects(应用程序缺陷的鉴定)$ e z" [# {3 `
检测应用程序的错误配置警报。
0 Y' U. U7 D! T: r, a" ^
& a D# x. n( F
/ K- W$ C8 `6 }; jError Detection and Hiding(错误检测和隐藏)1 g: B$ i: \9 [$ Z0 _* I5 W ^& T
检测伪装服务器发送错误消息。
9 ~( a. ~) @/ v0 I* w
5 l5 u4 @. {! n( f
( M& N2 X3 C3 q; j7 C安装ModSecurity/ m: f0 g1 t7 c( t4 l- Y. J
软件基础环境准备% M- J/ ?& `; w6 Q; a- K& A3 Q
下载对应软件包4 c3 O: v( {; d( o. S9 ^0 Q
$ cd /root
' j5 s+ p6 r. \5 _6 Z$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'9 I" D, }2 G5 ~4 f
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
1 x7 E2 x. T2 R$ d8 ]% I# S安装Nginx和ModSecurity依赖包
# n9 I( o9 l5 w: VCentos/RHEL
# h3 O X& |' \: p7 G
' @1 C$ T7 ?: H. ~' V4 s F+ ]2 e- u: q2 c4 i
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
. a8 A7 c1 T3 E6 ZUbuntu/Debian. L5 X2 ~6 a! ^/ h
" S/ Z I" }/ A5 U! e2 [1 G% d$ |+ [) n! h) [9 x* x2 y/ |! A! f& |' g
$ 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
) S0 b& D4 m% f# o2 ?* d# K. ]编译安装ModSecurity
+ h/ J* ?; l- t* r, SNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。/ i% m. b! w/ ~( }1 [
. g+ Q& L' L6 C0 d: `
: C* a0 D. k% P( m方法一:编译为Nginx静态模块
& z+ A5 H n; ^5 a' J* j e6 O4 z; k" b) v4 N1 Q
( M: @' T- [% w+ h5 x' e1 @编译为独立模块(modsecurity-2.9.1)/ y9 g9 {* T3 T; m9 h) n( T6 c I
$ tar xzvf modsecurity-2.9.1.tar.gz! O( Q( v, A q+ p& a! x
$ cd modsecurity-2.9.1/
0 Z) w5 V0 L3 s6 J; C$ ./autogen.sh
2 r2 w% H8 H0 F8 f7 E0 x$ ./configure --enable-standalone-module --disable-mlogc
! [( n6 k {% N) c$ make& Q. L9 {8 h1 z; u
编译安装Nginx并添加ModSecurity模块
, E6 B# y+ N$ \# W/ T; k9 ~$ tar xzvf nginx-1.9.2.tar.gz
' h; w$ M0 a& X$ cd nginx-1.9.2& K- O- n; j: s0 ~; p: K! `* Y
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/6 K& I" E5 B. a4 B
$ make && make install* Y! G9 X/ l# i- F- r1 w2 u
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块# b5 [6 w2 l& b/ g0 v
4 V+ Z, e& ]% J' s: Z# @
% k, M* D) W4 o4 B/ n编译LibModSecurity(modsecurity-3.0)
2 o- w) u# c& p0 q b G$ cd /root; U/ c7 R' w' M5 S& o
$ git clone https://github.com/SpiderLabs/ModSecurity
: v- J. [/ ~/ X# G$ cd ModSecurity& a' N6 w7 J& }5 O
$ git checkout -b v3/master origin/v3/master
- L/ M! B: q' w% C) l9 Y; S$ sh build.sh
2 E, ^- a& }1 r; P( W. D& U$ git submodule init
) @# L9 `( G1 x3 B! y K$ git submodule update% v. T. ^. ^- A ?! T4 z* m
$ ./configure
9 \( y0 _$ \2 k$ make
) M% F9 ]. e& {9 M9 b$ make install
4 C9 {* a9 z2 m/ I* FLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。/ O9 M: E# q1 N" s7 w* A; T( \
4 h X5 ?. Y2 Z% L1 Z* Y2 Q, ?) r/ O: T# a% I' d5 F! @* U
$ ls /usr/local/modsecurity/lib
! C# D( w& H5 Z* {libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0+ g& E D/ I; E$ @. l
编译安装Nginx并添加ModSecurity-Nginx Connector模块
+ o+ |$ P& d* Y. C使用ModSecurity-Nginx模块来连接LibModSecurity
/ s& Y+ Y7 o8 I: E2 [2 X, ]2 o8 d: C7 D/ f8 N/ C# b& ?, I# e( ?
: L4 L+ b. P6 N8 O, @4 a- \5 E3 Z
$ cd /root
. D0 q Z+ R2 U5 N+ w6 j$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx5 Z2 \: {! w& M
$ tar xzvf nginx-1.9.2.tar.gz
% b3 C" a9 V" w/ x8 p/ F. q* l$ cd nginx-1.9.2
( v+ R% m9 b' [6 s& B K! [4 ?! K$ ./configure --add-module=/root/modsecurity-nginx
- r, L: T/ b0 H) Q$ make$ u2 M, y: }: }. Z" _2 W) _" w
$ make && make install" j8 S M6 J* P* X O X
添加OWASP规则
$ S8 D5 O- w3 }* E1 T* LModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。8 p# N+ C+ `1 b' i+ R0 M
' p2 U2 B) ~* }' W! v6 G2 z6 I: d( a$ V o* ]8 L' n# @3 T
下载OWASP规则并生成配置文件
8 O* z$ |, i+ E: x0 N$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
[7 }$ M1 I0 f% J- I$ q+ b, a7 T/ H/ o$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/+ S! M7 r; B8 }! O
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
" G( X7 G; A; t, W2 X% v% S$ cp crs-setup.conf.example crs-setup.conf
9 g% a3 v3 |9 h% W配置OWASP规则
8 m. S, @% J. w) |编辑crs-setup.conf文件# t9 [2 z- O( S2 m
0 T8 P' z# K6 t
& y7 [6 v& D5 Q! [, y( {$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
' J% N6 u' x3 i, D" u- N- S$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf0 Q! i; X6 z. t9 f6 H2 D& s
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
" B) d4 S; b/ ?$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
. k7 Z0 _: Z8 s! i# H默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
% `' |/ \' X7 g9 I3 {! w- h7 Z$ o) A0 d; s1 T
, N' d9 z! h; o4 Q
启用ModSecurity模块和CRS规则# L# [+ \! c1 j! T/ |
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
5 Q6 [, c3 _; F, U. F2 k& i
' U. s/ Z8 h7 b# d( c' X( S9 ^5 e
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
7 m$ g* S' b9 Q% m Z4 P9 q# P1 {# a: [
; u6 {" O% g3 ^$ cd /root/modsecurity-2.9.1/
2 b2 H1 I- y0 {6 R2 P5 h( k$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
- s& I# G1 W- F1 Q z$ cp unicode.mapping /usr/local/nginx/conf/
- L& i( H l) O2 b5 r1 e将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。8 o1 i w6 P5 `: j
( U( C( d r* o; Y6 O8 z1 d: V
5 D; y5 z, R9 U0 ^: S: B$ \
$ vim /usr/local/nginx/conf/modsecurity.conf
* R4 I4 e9 A+ e, P+ q! c4 t/ dSecRuleEngine On4 d9 j, R: Y! t. c! C) S
ModSecurity中几个常用配置说明:, B+ P9 ?+ I# F1 e, E, K& @: q1 W
2 n) @8 n( [, ^* b) [ K
8 k, G+ { r" l1 W# B/ u' ^, o1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
0 w: S9 @+ U" s/ {0 E8 `
* ~$ S- \* @+ K: S2 w9 N8 k5 r. R
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。1 ], e7 @' \" T; @' K( P
5 ^$ ~' }# _7 @& Z
- `$ b, S3 M/ o' M9 s3 C0 a
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。1 T9 z/ h d7 I6 c
. ?6 @9 o b7 ^8 o
6 a* D5 \) y' x9 a4 Y* N1 n4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
/ h* k& _& U+ S' e' n/ z _: W% U: Q7 t2 Z, y2 R9 Y
% C4 C% E: c: N( T; |
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
$ v2 L1 \; o7 N5 d
) M" b* f9 q4 S( A# `6 P( O, u& }5 e* c1 C$ q" }8 L
3.x版本CRS# w- Y; [5 Z" |+ q* @
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
2 s3 |5 w5 U1 o3 B: L7 c+ u# 生成例外排除请求的配置文件2 i1 h# l* [4 z7 f3 Z- U' m
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf# I Q$ P: p+ `6 a
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
5 W# b% B' R# k0 e( z: \+ e: r$ cp rules/*.data /usr/local/nginx/conf
; B3 x1 e: l: {& ]+ \/ Q* Z& [为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。. ~; Q, @+ ?8 o; r# f( s1 J- e) n
. x7 E8 g6 s# }' H5 u2 C9 A3 o3 r1 @7 z+ [' b8 x% V& L) U/ Z
$ vim /usr/local/nginx/conf/modsec_includes.conf2 u# d7 N7 H! {
( s* z0 f: k6 L3 \ s$ T) L5 x[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
/ U e: t) w- F$ o ]" u- c
$ O& i% [" t- }6 I* E: y- w注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
& K7 ]/ v' X* j# e- f0 t8 `' @" E0 x6 J0 c
, V3 H* ~" Z! {2 W5 a9 M配置Nginx支持Modsecurity# q* ^- Y4 U! |6 [" J: |% D; J
启用Modsecurity" S: c3 C" ]8 {+ q- S7 [
使用静态模块加载的配置方法0 u3 n: I3 o' L* V) S
在需要启用Modsecurity的主机的location下面加入下面两行即可:
5 X ?" a) K5 C4 q- J' R
, s H1 {2 d! o" T8 B# {% `4 F9 h5 y, K: d# S( N
ModSecurityEnabled on;
* r" x% o8 p, f8 I8 L5 W4 xModSecurityConfig modsec_includes.conf;+ ~" K; s" K2 Q2 e
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
" ~1 V/ S) o* v& b6 y& Y3 |) v) G% |( B
$ |6 C0 ?+ c% R5 C- z$ k5 U7 P, I2 ~3 g: W: f
$ vim /usr/local/nginx/conf/nginx.conf
5 b$ H/ G2 R% m* l' K+ H% ^! {' I5 }
; ^% g: s2 F! Lserver {
7 H( D- S" t! Z3 p listen 80;0 C7 D5 r6 e0 M; u1 R
server_name example.com;
( \" b ~( {5 a$ h' l
8 h8 X2 k) w& E3 t; y h0 j/ F: `2 C: t+ d7 T! D4 f- I* V
location / {
& t3 R' ?5 k( m ModSecurityEnabled on;
& A9 c- E7 d7 ?$ Q8 p/ y, h- x! e* } ModSecurityConfig modsec_includes.conf;5 M5 n/ V- r7 T7 K
root html;& ]! M$ }% P2 m( x- t0 G
index index.html index.htm;
( G) h$ C5 C, b* E* z3 D/ h }: A }7 h6 [ a4 Z4 B5 f' z
}
; G5 B- l2 r. [使用动态模块加载的配置方法
- g; q% w, d* u% V" o& e% V在需要启用Modsecurity的主机的location下面加入下面两行即可:
" e0 A1 V3 n5 @$ N8 }
, h9 Z) i3 b" y. |# e( u- N) p7 y( S! d- E6 M6 S
modsecurity on;$ p, @4 @* t+ T8 f, B
modsecurity_rules_file modsec_includes.conf;4 Z: B: ]2 n, X$ z5 G
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。- R' T9 K8 @) |- t2 n! P3 t
( L; W2 G0 O6 W( _% ^, S, C
[ ^7 ? O% O( w6 i( S# z( g: o$ vim /usr/local/nginx/conf/nginx.conf7 p$ C" o, L, i( U+ D+ a
* A4 C! E! N3 ~0 M5 l- r# S5 E! O
5 F3 S7 \& X4 {( g
server {; n" B& P1 Y: u/ ^3 ^; i) b
listen 80;2 @) o' e* ?9 g9 I
server_name localhost mike.hi-linux.com;2 P' o( U+ X3 |7 b6 Y
access_log /var/log/nginx/yourdomain.log;4 M5 w1 x1 W5 k9 p6 U" X
4 d1 b# L4 Z9 D$ l# x. m
/ N! o, U/ k- ^; e% q& K location / {( N" L! ?# r z5 T/ E( ?: I0 M
: N! u, Q8 Q7 G' G f
: I. }0 Z1 \1 W' K% i8 v' K modsecurity on;
& @ T. R. h5 R- D7 \0 J, T4 i modsecurity_rules_file modsec_includes.conf;
; `- q. C9 E5 e/ a9 y root html;: v: b5 ]# j- s7 C0 W: Y4 L
index index.html index.htm;
6 ~( x' ^' ~1 U4 {# ]5 v}' y6 ^/ s& x6 b* A, N6 q
}
. _3 G/ g1 q* W. x8 [- W9 z验证Nginx配置文件
( e+ t% b. P4 v8 c$ /usr/local/nginx/sbin/nginx -t7 K2 o. K% s+ y$ P( I8 `$ ~
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok8 A1 G9 X% F$ Z% G/ v! j
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful8 M$ M) m: ?" }) b" l8 ~' G4 ^
启动Nginx
/ j- T& Y! C6 f6 X' I$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf7 a z+ d- s+ V& j$ d; q
+ U9 O# Y# E3 p8 d* 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能够战胜更多复杂/未知/混淆的攻击模式。
+ y# \. `8 ]5 g7 Y7 g( y |