ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
4 O1 I' n* t* {/ s. m2 Z
% G( O/ Q% A" y) Q/ J% l
" x& r5 i) Z) F* ]; Q在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。 j" y& c! e! n5 a' j
1 {0 D7 u J$ d4 Q1 `
: Z# j3 b* H& ^3 ?6 T) V' o& d7 j什么是ModSecurity9 I; T( B; i/ h8 }9 {* |- _
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
& i( j' T( r$ [' D+ h
! w. i& L% z+ y3 C0 ]" P r# H; R! N7 p u* {, _
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。0 y$ j+ R1 O/ X. p9 t
) x" }4 |/ l6 L; A3 R. z7 ^
7 O0 {% |3 d. ~ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。4 {9 r) O5 [6 |7 ?8 Z
. [2 H" T& T I0 h- T" `) J" |0 ?7 a3 y5 x. f4 m) r0 q
官网: https://www.modsecurity.org/* E- j+ c3 l6 S$ |5 y
. I8 M* P) W7 E4 U1 g7 Y9 s/ J
" B* Y/ r1 X6 }5 l' F0 F
什么是OWASP CRS
6 r7 _2 D; k' ?3 w) w$ POWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。. O7 R6 L' u6 c9 |
& l9 _3 [" y3 Y1 `* @; w
5 a/ ?4 U% w, K( G2 fModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。! |' x' H" Z( P6 ~
J5 F4 i$ ~9 U2 s* B
9 }/ U5 S# l: u: `* DHTTP Protection(HTTP防御)
# y( r; @: u3 n/ p( x0 B* ]HTTP协议和本地定义使用的detectsviolations策略。: ~( e# v! a( [
8 T: R) |. E& Y: x. e
% V t- f; G5 ~% _3 Y8 F
Real-time Blacklist Lookups(实时黑名单查询). a) O+ }) T/ w. z3 v
利用第三方IP名单。: ?$ \% p$ B, m2 [9 y
9 q3 s1 G$ @5 ~6 c3 t6 @+ c8 m4 N" f P) x
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
. |( [: L3 n/ l8 M. K防御HTTP的洪水攻击和HTTP Dos攻击。
7 o0 U3 q& [" \; t! @- `8 F$ P
" ~% y7 Y' l% j `
' v- X# S/ z Y0 y# S- l( kCommon Web Attacks Protection(常见的Web攻击防护)7 h( T9 Z8 z1 }( m
检测常见的Web应用程序的安全攻击。' ^: w- L1 n/ z b% c5 }+ k
, e5 h0 @) @1 d- h. Z
1 @& U9 v# n. G% hAutomation Detection(自动化检测)- |8 @! M, H- ?7 q, y) a# F. K
检测机器人,爬虫,扫描仪和其他表面恶意活动。( g+ X" P, Q) p
2 h. _& H* W$ {% m4 ]; q& c' S9 ]
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
& u* s: x$ P% _- J( P9 l, {0 A检测通过Web应用程序上传的恶意文件。
# s% Q, o4 x, {4 B5 g
+ \% T& E, ?$ h1 F. H% ~
! q( y3 B& d/ u* m* z8 oTracking Sensitive Data(跟踪敏感数据)
) H2 H# ]% K% ~- m信用卡通道的使用,并阻止泄漏。' O2 X# ?' z6 w# J* ?
3 u: Y4 ]- s- N V+ [0 d6 t2 k; O6 n
Trojan Protection(木马防护)( t- E- o8 G, |3 i& {
检测访问木马。& W" }: m! b. V$ D$ y
( o. j% g& d' x0 Q* B, Z5 B& p* h) I2 s1 P
Identification of Application Defects(应用程序缺陷的鉴定)
: ~' l, `2 M" V. |6 Y2 e检测应用程序的错误配置警报。1 V) c' ?, N1 H! U* Y8 b
3 T) V) l; u q% {* \% b% E( ]. D% D8 o" h9 z# s6 Z$ |
Error Detection and Hiding(错误检测和隐藏)3 [# W) i& |4 b9 ^8 K$ ^! d
检测伪装服务器发送错误消息。; t7 S3 W8 B1 i( A) w
+ j, p( R& k8 g. c- {( k
) q) f( ]" X* ` J( j6 }安装ModSecurity& ^! v- ^6 ]* @' i0 B2 a
软件基础环境准备
3 t& E4 n* C: C$ z* W下载对应软件包9 r% t% I G+ R! \
$ cd /root
" c* ~( `+ D4 X: k5 M8 }$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
{2 w& R: w8 [* P0 A$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
v& ?3 [2 Q! W5 u9 U5 d' W+ a* m1 x安装Nginx和ModSecurity依赖包3 M( t- T+ @2 | F
Centos/RHEL
) K* a2 _/ w; o3 ]7 _7 l' N! M8 S% I' C6 R8 r2 R
% |( v$ v- y% f) ~
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
: Q# D. M: b0 Q: `! mUbuntu/Debian( {, H/ c7 N0 F' @' r* F$ b
2 F1 Z1 H; b) v6 X1 F. R: ~3 ]9 m& x
$ 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++-dev4 |3 B* w5 S4 F
编译安装ModSecurity
: i$ w9 ?0 V( }# f: dNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
" y N* R+ T9 n% E3 r# D; R' F6 p9 Z9 l
9 L- | B4 \$ q7 `) l
方法一:编译为Nginx静态模块
& @% } ?; k! N/ y; ~/ X! b
+ x! ^# E" }* m/ o8 x
& W H @8 W% Q% R- A编译为独立模块(modsecurity-2.9.1)
" H6 [4 v5 @' i# d( U, M$ tar xzvf modsecurity-2.9.1.tar.gz& j# Q5 y4 n1 z. C; B. ]
$ cd modsecurity-2.9.1/
' K2 v1 y4 @- x3 G9 u C$ ./autogen.sh
+ x- H8 v" t: p) T5 [$ ./configure --enable-standalone-module --disable-mlogc
- D4 [! e! p! A$ make, `; _7 S t) C+ R0 G" _7 q' l
编译安装Nginx并添加ModSecurity模块
0 I* V9 Q: o8 n% y0 {8 J$ tar xzvf nginx-1.9.2.tar.gz# G5 o: X! r' w' [& g8 e, N
$ cd nginx-1.9.2
# \; l. S! F! z( w$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/5 M; J, E# V$ U9 @
$ make && make install+ s2 J; d3 |0 d+ m, Y8 T1 ^
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
! g% Q C4 x) ^; G k* w6 o" e0 n) T+ u7 v, t+ \3 x8 q
1 k, e# N5 g9 n
编译LibModSecurity(modsecurity-3.0)
* h/ ?7 O) H( j5 S4 g" j* h9 I8 G0 f$ cd /root) j+ ~) A! {7 f
$ git clone https://github.com/SpiderLabs/ModSecurity
9 S; O @" Z" n' `! r6 }$ cd ModSecurity; C" {% x Q" `, G% b# H
$ git checkout -b v3/master origin/v3/master' Q4 k5 H: Q/ x' ]8 E W9 }- G
$ sh build.sh
# J6 p: {8 ? }6 n$ git submodule init0 E8 c+ Z# l) i8 A5 V' O
$ git submodule update8 v5 B, P/ @% {2 Y/ d- L P
$ ./configure
# U' z' B) w8 E# j1 j/ G, Y2 s$ make; Q1 J# p1 O& V# g6 f
$ make install
. f4 O6 W3 B- A, S M fLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
5 M$ t3 W- }$ U8 y: [. b: @( D, |& u, c! r/ E& j4 m
Z+ ~: w1 a+ u7 Q, j6 B4 L& b$ ls /usr/local/modsecurity/lib
z5 @! t- u, _. F4 W2 `libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.04 ~, b8 T' B6 A$ k8 @
编译安装Nginx并添加ModSecurity-Nginx Connector模块
7 h. U& i$ E2 @- n5 k+ \使用ModSecurity-Nginx模块来连接LibModSecurity$ Y. T A @$ _# P% \! q- @( V
! A' e; }" ^) o4 l
8 l+ d5 F N- r! i) P0 H) J9 V$ cd /root
9 W9 o# J% @! I3 l. o, q- ]$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx0 b u. k2 ]* E' D
$ tar xzvf nginx-1.9.2.tar.gz% `+ _( s; ~) h/ }* D
$ cd nginx-1.9.2- F: z( {1 |9 N! ]& w! `
$ ./configure --add-module=/root/modsecurity-nginx
% \) j9 I& n! `% r. I$ p$ make
; b% [5 K: m. j: Q+ n' q$ make && make install% e2 v# l9 x+ W' a1 Y; o7 `. e7 Q
添加OWASP规则 k+ p S4 ~! c6 |8 p5 G1 U
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。. s" W+ o$ D! X2 {0 W0 T) p0 J
! n; \) l: o8 d( U& t7 k) |5 g3 f" t6 l9 [' Y5 A* C% Z
下载OWASP规则并生成配置文件" G, ~& M1 [) r. G) Z) \
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
9 @4 O& n, r Y; E) |+ V/ a7 i$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/% a( Y) a4 h$ L8 ^# J( Q: Y
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs6 F% N' x, O/ v& Z! `' U
$ cp crs-setup.conf.example crs-setup.conf
; A; o4 P. M% P+ Y- A Z配置OWASP规则
4 j1 P; ?: } O5 a- f! |0 \$ L编辑crs-setup.conf文件6 z. q0 ` d5 G5 f1 Z: d
& U$ K" T7 L. o5 |
N6 X2 h; L! m5 k$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf8 X" X5 g& r9 O# p# W- Z
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
7 b0 T0 l3 ~, ]3 l! }: W5 o. x$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf( ]$ X X; {+ }( b% c- ]; Z P
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf5 {" b# }2 f Z" N9 j) A9 r
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
; |9 p" v8 l9 G# r7 v, Q' H" K4 {( `% e c# D2 d
- w8 W/ Y3 h5 m, [
启用ModSecurity模块和CRS规则
7 E+ `! D8 Y2 }! a* N复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
/ }# ]: y! l: n _
6 o% B. r# t$ j9 I
3 B" L4 A z+ C ]modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
/ F0 `8 m$ |; S7 A1 m* ?. C% E- p0 R* K O. c
! b4 J0 }* c0 ~$ cd /root/modsecurity-2.9.1/
! Q. A" J h$ B, N) b, Z/ T1 p& s$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
/ Q1 q2 g/ G& ]% i' W0 u$ cp unicode.mapping /usr/local/nginx/conf/# M3 N; S- ]2 l4 l* r( \8 k+ m2 w# ?, l
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
; ~7 u2 C* t3 L0 @$ l& x
1 }$ w* u+ f# B6 M( a K8 W4 t& `8 V% _+ x i7 U9 m
$ vim /usr/local/nginx/conf/modsecurity.conf
0 w# f& T8 W- o6 LSecRuleEngine On
5 D% `* f/ B! |3 y) @+ yModSecurity中几个常用配置说明:/ s( `! r- @6 J
2 z- ^ X, l! ^# o6 U
/ D1 {! |+ _# m6 A1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
. y7 |1 p& ?5 r% T& J" e) g- [# b. O' v8 y
( t% x! d; P+ g7 U. m0 e
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。4 ]4 t' }: l3 M- {' E
1 D4 B6 S9 h$ S9 v* L ]. c
8 \8 X* C; I8 h+ ~# ^# [" N
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
, i$ p6 D0 p; d; g E% T) S2 o1 z, p+ j
Q( j# i& G- V7 c# w4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
3 o0 c. ~! I: @/ h: S R7 z- t0 O6 {, `% I9 m0 O
+ d6 H$ T3 o. G! i1 @6 w& M* h4 b
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。' h. F! ]4 g2 z
u# ^/ [' d% T$ o% C
* u% h P& X/ w# Y3.x版本CRS* B" M" j$ c% _; ^) z
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
8 Z3 U/ U. F1 Y6 \- ~7 x- C7 `5 ?# 生成例外排除请求的配置文件5 D4 E Y# @" [# L6 q1 ?
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf4 r' Y8 u8 |2 u0 e) w$ V
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf6 }! _- ~% H2 @ O% f
$ cp rules/*.data /usr/local/nginx/conf7 _6 j( ^& U& h4 p8 _$ c% T
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。' S$ L! }1 A6 V6 f; G
) i, c# u7 J( Q; C$ D) V; F8 w9 @
9 n C8 o r, O; [0 p l$ vim /usr/local/nginx/conf/modsec_includes.conf5 n& x% o1 h" |, n2 E: ^+ {
! W6 O6 O" c9 Z8 K3 b# J0 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
5 M v; t/ J" \# [- ~! j
6 ]4 G8 x3 p' W; j) W2 D注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
/ F1 U' J7 E) l0 V
1 V: x' r4 a7 K" m$ d" s' b( V) R6 `7 x7 I
配置Nginx支持Modsecurity$ Q- E! V! u# _- ?/ w
启用Modsecurity
! V8 G) U+ B! G8 d+ o8 X' q2 H) L使用静态模块加载的配置方法
: b- t& y. J1 R i* m5 P在需要启用Modsecurity的主机的location下面加入下面两行即可:3 R* \$ i& `& v' ]+ b
" l+ A K2 s+ A8 U2 a6 Y p4 `+ X
7 `' W$ a/ V3 d4 K7 |2 P! dModSecurityEnabled on;
- b1 N( r2 \; ]ModSecurityConfig modsec_includes.conf;
+ ]' h8 p3 H! P. ^9 N4 b, [: Z修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。5 Y7 D- f% l. O
( |7 R- g8 p4 e
7 T2 ?+ f, {$ c( L7 n6 r \& {$ vim /usr/local/nginx/conf/nginx.conf
. x8 E$ Q$ f7 s7 a" } ]9 p& _+ r4 ^3 f# n/ k/ Q) j) G5 h
. A( Y$ d" {+ F4 m# {server {
3 T& c! Q6 v2 J% m5 C( ]( i# E5 e4 ^ listen 80;
$ \: K6 _& G/ f# R8 R* k server_name example.com;4 G% g8 \2 ~" f
# [ A# \- p8 ?# ]2 D, b4 Y! i& x) B! B
location / {8 C4 ?! S V( c3 w5 H6 I
ModSecurityEnabled on;
e* H5 j# [+ J D+ J+ u ModSecurityConfig modsec_includes.conf;
* j& [; V$ q3 V v" K root html;6 j3 o4 k% K! d1 Q/ x# A
index index.html index.htm;
% T& }9 [% e; k, Z }
8 u0 [& i" c0 e}2 L2 \& g4 N1 o
使用动态模块加载的配置方法/ h5 v7 @+ V3 U/ q+ d! }( g p
在需要启用Modsecurity的主机的location下面加入下面两行即可:7 F& t* O. C6 E) }: Q& Y2 O* v
: C# g. ^8 l7 W& K
6 q$ Z8 L/ K6 T6 T7 {3 Tmodsecurity on;7 U' r; Q1 _) W5 ?; K% o* C
modsecurity_rules_file modsec_includes.conf;
2 E( x1 i U- Q* _; M. |' u6 T修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
! [3 N. n4 Z7 m7 F0 t* ]& x
( k' E m4 K' j1 X- {
6 D4 b6 i$ R* y' s9 L# ]$ vim /usr/local/nginx/conf/nginx.conf- i& u1 y7 \ z0 L
( W, I1 d& ]: H& A, m( V% u
0 L2 G( P* j: @3 d+ Mserver {* @$ H) H% B) j; f
listen 80;+ X+ S8 b6 ]' j4 [& H
server_name localhost mike.hi-linux.com;" b4 D; ]/ S; t5 j, X8 ~/ Y8 l6 j5 R
access_log /var/log/nginx/yourdomain.log;
7 |8 j! F, M# r' Q( x+ f. @
. V- i- Z; U8 y1 m
; A/ V' Z7 P/ L4 p' M. {& w location / {' s; F% E3 x% h& q. |% Y
3 W4 Y3 ~7 J+ _. a5 X( P; Y
! A! E1 q, p! M( T7 Y" i) ]; s modsecurity on;
6 C& y& w; ]/ b5 `9 B0 c modsecurity_rules_file modsec_includes.conf;
# Y7 F+ w9 H% o4 D7 ? root html;
+ d! R: I( M9 Y: y& Z6 s/ Z% `( D index index.html index.htm;
7 X) a2 ?) ~/ F5 ]" }: _}% P$ q% L8 ]$ z; l" {/ p% \3 S
}
( \( `& N& m9 I验证Nginx配置文件
( R! L( X0 R' Z, p" a) H6 t' ~$ /usr/local/nginx/sbin/nginx -t' m- Z. e' r0 \; ^3 m+ ~
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
* {) B9 B& E4 s/ Snginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
& v& p8 D6 H& M; y5 M- s% P5 _. M P启动Nginx' ~4 U, i1 T' J3 v$ a7 }
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
$ B; a! ~/ w( a! q
- Y6 a4 Y6 D. s/ ~9 X4 @4 T测试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能够战胜更多复杂/未知/混淆的攻击模式。 + s2 l- @8 U% D- C9 |, _( }
|