ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。5 e; v1 U2 d$ Z! O2 w/ ]( v# C
c& z) M7 c6 b( N: C+ \8 ?# l5 p8 ~0 h
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。1 g4 i2 K( ~' H/ c5 r8 A
) _* R' g; U B' T) N0 G7 Q
' e$ T( c* a+ w+ g! O2 G+ I9 m什么是ModSecurity& | O3 {0 l- c2 t' r, z% E* v
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
" L1 w3 t) Z. ?& P2 u9 r" f
0 D K, {/ Z1 T; S
: |' }) x8 L, T# k4 U! pModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
+ s7 N! m1 A2 N$ Y2 j0 V
3 D3 f( Q; {9 T+ [: H8 W
2 @6 {% d2 M3 f$ U4 a% ^0 ?ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。7 x' t0 R. B9 z1 Z: [2 Y
! m; N# ~( M& U- I. l
$ g8 X' c3 k8 b/ ]" i! B# x官网: https://www.modsecurity.org/- ^, u( i$ M# a/ W5 m
w6 D- T. i# ]& t7 d
, n2 \( |8 _. x
什么是OWASP CRS1 ~ r6 E, x/ {" F w0 Y& n
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。4 C" y/ j6 ?+ P! T/ \
! z# x' H0 k8 T- c9 h( j
' ?/ z& @$ Y' y9 T3 B c8 GModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。$ _( @) G8 O0 ]
1 u: f) V8 q6 Y p: T9 a. ^3 h8 ?7 c. t: [" Z @; |1 H
HTTP Protection(HTTP防御)
. d9 D7 X" h- o9 l+ [HTTP协议和本地定义使用的detectsviolations策略。
8 {7 ?& q: q* |. `0 m1 V: Z* u' o; P3 m1 f
* k8 y6 a% v- M* |: GReal-time Blacklist Lookups(实时黑名单查询)
5 \. ?; C1 `& H7 [利用第三方IP名单。
2 o2 e4 K: N0 l. |" L( F0 b! i2 _" n, R7 G4 r% w& \0 s# y
6 P6 s) K3 j! n) ?0 Q2 G# y
HTTP Denial of Service Protections(HTTP的拒绝服务保护)8 S8 F& `! z) w" g3 _; @; K
防御HTTP的洪水攻击和HTTP Dos攻击。3 W% k& A) g9 l
; y+ S* r0 h6 g' R6 J# y
! S! J# R/ N/ @0 WCommon Web Attacks Protection(常见的Web攻击防护); q* [2 \- \7 u4 h" `+ m* E
检测常见的Web应用程序的安全攻击。" t6 x, A$ K6 D2 r( y
' t7 l' Q# O( D1 b( m
4 k! g% y5 o' iAutomation Detection(自动化检测), N" ^7 `& h2 X4 o3 U% N1 f
检测机器人,爬虫,扫描仪和其他表面恶意活动。9 ]$ ?( v6 W: X, } s. o# [
6 L. y! C! O" U
+ _+ P; s! F3 P `Integration with AV Scanning for File Uploads(文件上传防病毒扫描)7 q+ y3 X( [% p7 ~" ~; |2 |
检测通过Web应用程序上传的恶意文件。7 W S9 y# E# b2 X' k
! {& Z+ d, \* ?, i5 O& ~7 R
- N a! U: f) T) f
Tracking Sensitive Data(跟踪敏感数据)7 J5 h: y9 x. M B( p
信用卡通道的使用,并阻止泄漏。; v* ^+ |9 \( B i5 S8 [
) M& l6 t, Y" l% X! }! L+ v/ @1 `9 B
( ~7 v8 |! \9 a' r
Trojan Protection(木马防护)* ~3 i. h% t4 H7 U/ z, e3 P
检测访问木马。! E# {- M4 \9 `& r+ l; ^
0 I! T' ], n, ~+ z! ?) m
1 [, {7 X8 X! }& G8 R j/ v3 P2 _Identification of Application Defects(应用程序缺陷的鉴定)+ p' i% _" m9 P, u! F! |2 O6 l
检测应用程序的错误配置警报。
; M# S- j6 _6 m& Y, H7 n2 @6 F
u8 u0 X0 U: Q. P3 |
( ^6 Q" ?) ^' T+ L- F7 |Error Detection and Hiding(错误检测和隐藏)
, {8 }+ M, ?3 A! g' l& h检测伪装服务器发送错误消息。9 X* X. y6 Q: @
: E* T! D# G5 [: z: W0 i8 W2 u" E, K9 v) n/ R8 n7 l
安装ModSecurity4 T" A+ M Q" G* C
软件基础环境准备/ K: d0 l! ~) S0 t9 J2 ^
下载对应软件包
) s4 C Y* ]5 Z7 m9 M1 M* A, P$ cd /root( x0 {" [) f* _3 k
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'$ |: C* B6 y* m5 v6 m" l
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
9 n9 O! C: S* C( u$ R2 ~% g安装Nginx和ModSecurity依赖包
! Y- \ F+ j) ^: a# ?. yCentos/RHEL0 S8 J' `( C% t1 f6 A Z. H
' J _ d! s: `4 i9 R
; R5 M6 p5 f' v. t- |; {1 a% B
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
% l9 L1 |6 X6 _9 j, LUbuntu/Debian% @' a% e- S# w1 G
; j- x# J) B3 a
. a3 F* W+ X0 U' x7 w1 ^7 D5 r. L$ 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; F1 v; R4 N' J7 F! z
编译安装ModSecurity! j7 L$ W' n7 }5 w
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
( t8 _/ d, U k: ~7 j' F4 v, d4 ^# U- j6 }- R
8 l, h+ J) H6 ~9 m2 J* K方法一:编译为Nginx静态模块6 J; h1 V% u- \% w
5 w7 T: ~6 {+ e- I
0 Z: B4 g2 e- [* }+ r2 p编译为独立模块(modsecurity-2.9.1)* x4 T8 k" R! }2 i- ?/ q
$ tar xzvf modsecurity-2.9.1.tar.gz/ H' U& @- u* l. w* g% P: |
$ cd modsecurity-2.9.1/- \9 o& E/ ]4 K4 |' v" t& f
$ ./autogen.sh* c/ Q+ F2 ?2 l! ~) `5 f; C+ I
$ ./configure --enable-standalone-module --disable-mlogc
! Q- `: m% Z4 `) J4 ]( k$ make
% m/ ^: \3 O2 U5 M' W0 y编译安装Nginx并添加ModSecurity模块
& l: s L, \( S7 Q$ b/ \$ tar xzvf nginx-1.9.2.tar.gz$ Y* M# C7 Y- l3 x$ e$ F! O4 P* k$ i* e
$ cd nginx-1.9.2, ~0 h, J/ ~; \7 p9 d
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/; |1 g5 X6 s& p$ e6 G+ }: e
$ make && make install/ Q, w- C: M! E) a* q3 Q
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块5 E. P& ]+ x/ s$ F R, q) G. J; ~
; u `9 V9 Z% @; ?+ A x
- ]% M, k* k! A: c+ e0 L编译LibModSecurity(modsecurity-3.0)1 e, q" l8 o- A& z" i. y
$ cd /root# S+ n' T# B. m6 o$ I: |' s) v
$ git clone https://github.com/SpiderLabs/ModSecurity. x8 R- J- U* \; m0 N% y
$ cd ModSecurity
+ _& v6 u( w& D1 }: e6 T) F7 u$ git checkout -b v3/master origin/v3/master
- w# C& ]& [- p" k$ sh build.sh
! _+ j, J9 Q2 n& w; @$ git submodule init
: s& |$ O8 U; i" C& P6 V3 S$ git submodule update! C1 Z. t% Z3 E( ` m# Y
$ ./configure
& a6 w0 {1 @ _& {0 j% s, |" y9 k% z$ make
* r2 j6 q7 [* K4 [9 b: a% N/ l+ P$ make install% S8 \) X9 o0 V* T9 W/ ~# [
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。1 J, K3 o' S/ {! ]7 q
0 Z6 J6 i; \7 l' o5 A
, }( `/ z- m% D8 `
$ ls /usr/local/modsecurity/lib* f: L% g9 _8 C* b8 x% `, w! w' x
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0; a& r! p' H" A$ G) G2 T# s: ]
编译安装Nginx并添加ModSecurity-Nginx Connector模块: e% I7 I/ [+ l, a; b3 S
使用ModSecurity-Nginx模块来连接LibModSecurity& B& `( v( a7 G0 Q# \
) ~, L1 z4 q3 k9 A
+ Q# }& M9 x$ j8 S1 N$ cd /root
/ A/ o2 p9 E) @1 n+ g' K; w( y$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx% l5 t5 m$ l$ s) Z5 G( C5 E
$ tar xzvf nginx-1.9.2.tar.gz
& f w/ _$ `0 x4 q2 S$ cd nginx-1.9.2
9 q; O( g4 H! {$ ./configure --add-module=/root/modsecurity-nginx0 {+ B4 A" B0 A0 x
$ make5 ]' T& J' g1 L4 k7 G: M
$ make && make install6 D( p; Z- ^+ f
添加OWASP规则
; t+ U% y5 o1 a: S$ z* EModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
: ^ y" \/ L9 R% @! U" u1 G1 I! I0 j4 m7 E
) p$ r4 `; d- @8 K5 U8 f& G' ?1 d
下载OWASP规则并生成配置文件. e; z# c* }- x- C- j4 z' {2 j" i' E
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
: a: l7 k. c1 D+ q, {! e$ L9 ^/ _$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
0 O1 y$ x/ S/ b$ cd /usr/local/nginx/conf/owasp-modsecurity-crs, ~( {# w& X1 [* M6 X
$ cp crs-setup.conf.example crs-setup.conf' ]* x4 T, J4 M+ L: k0 r) e* }2 P
配置OWASP规则0 u( Z% T8 C" H# g: E
编辑crs-setup.conf文件
5 q- g+ m6 I4 ?: M% ~
4 }$ M( a' M. M' o S$ P" x" [+ ]/ \
9 y+ u+ P2 E2 C9 C; k$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
1 w9 ~ d# S2 ~' U$ J! r4 D( R! X; f+ `$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
9 _6 i0 @% q, x: ?. u" V2 H. |9 f$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
$ T. r2 S, i0 H$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
9 B: S0 p8 P v, }5 o默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。0 [) x; H# W% ^1 S9 J
# x9 g3 N3 I; m( u* H- J5 M
% g& B$ v% h6 j启用ModSecurity模块和CRS规则
7 T4 Q' _2 J0 C v3 F j复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。+ b" |. g/ o- I4 `8 S* O& N+ }
" Q+ H; z" s# t" a3 c$ {, O
0 K' Q, i1 t; L' amodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。1 c( A9 l9 U( r
+ x$ O- ^. k2 Y' e0 U0 ^3 p5 `( Z' d+ W& }. }5 s1 w! o2 L% k
$ cd /root/modsecurity-2.9.1/
' C3 d" X& h9 o& g0 ~) i: P1 q$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
! i+ A8 p0 W0 ?6 K1 d$ cp unicode.mapping /usr/local/nginx/conf/* I2 \0 ]7 \8 L8 y3 _( ^- e
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。3 S2 n) S8 B1 a2 p+ W4 I0 R
2 Q# G( f8 _$ P# R L; g: j4 f
- [1 |8 S4 L6 e8 p
$ vim /usr/local/nginx/conf/modsecurity.conf
! ~! w. I8 X: ^: J1 B" ]! ?SecRuleEngine On
! g7 B4 O3 O4 G4 q+ T9 yModSecurity中几个常用配置说明:
. s. o( i; o7 k( E& ]! @% H% y, l1 u2 C- x# n) h X
5 r- z- a8 y$ u) T
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。. V T6 l" a% L7 H
8 Y" c9 m" b; Q. o
9 U% z, `( O: {$ c2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。5 }- l" h( f1 ~9 m
8 Q$ l% h/ w I' h7 n& E1 U
A5 F* E/ |! j. L$ {; r
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
9 K9 C7 a: x% r# ~/ a
4 k; U/ T- y# i. L* k
5 @9 h/ ` X" y2 h; z2 i8 M) `% L1 C4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
2 f; ^+ f6 t3 _
- U6 ^8 m9 J8 r5 B5 \3 d5 Y8 h/ n( ~( y% T5 b& l
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。8 h8 A; O) q6 o# O( J4 b
( h+ E) \& a( R4 z; F( W0 Y
# \: x5 Q2 l1 U/ V! d# |3.x版本CRS, x+ D3 {. t$ U$ b
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
8 \$ d: ]1 f2 u! _9 U4 N# 生成例外排除请求的配置文件
0 }5 P& m8 f+ l6 s: X! \" q$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf& p# f8 `7 Z# {" j1 o* j
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf. ^( [# i8 Z& U/ v
$ cp rules/*.data /usr/local/nginx/conf% Y5 c) J! P7 S; ^9 I8 {
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。0 f8 x8 I! E4 L8 i! F( v
" v: Q" `; [' W) x' z
! Z5 l% `3 E7 z. @$ J4 K3 \# A8 q$ vim /usr/local/nginx/conf/modsec_includes.conf' ?- Q7 g% L3 \2 V
2 T( J9 D+ n' o! q[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 ; @ M, U0 w, ]7 j0 N* l
; j7 t3 ^% M& P9 f. C: b注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。) S) E% l! Y* q. A( I$ t8 L, N
1 r' R+ ^$ H' A& R5 t4 m/ Y* j7 S- H V9 y" O+ o
配置Nginx支持Modsecurity
1 s% N) X8 \+ e7 X. k) C* {* L启用Modsecurity
; b3 I! v9 L3 U$ a& H! g使用静态模块加载的配置方法. ]* S) p1 p! v% T; f: J
在需要启用Modsecurity的主机的location下面加入下面两行即可:
+ e, w9 u/ L- r# d' Q' z' n$ {4 C2 s% o" {& K8 K$ }' v
0 g9 {! s* K, S0 Q0 eModSecurityEnabled on;* o/ `' ^6 h: u7 U/ {+ }3 _
ModSecurityConfig modsec_includes.conf;* V# ?5 X1 B) p
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
8 l6 w$ ?" w. R. _: n( p1 e$ o
( w- \. U; s+ H' ?5 P! {: z8 ~
/ g' a L4 X9 b: A9 [7 J' }8 B$ vim /usr/local/nginx/conf/nginx.conf
/ e8 I7 k& s% P- u1 F
& ]3 B4 z, m% Q0 Z8 N. X" [5 ]0 P; O* W7 \1 f
server {
( P8 U( Q- ?2 B' Y6 J1 f/ a& C6 J9 i listen 80;
7 O0 i2 s, j3 A. _* v3 |, y server_name example.com;* o! p' N/ f8 m) M9 T
f0 P6 p4 Y, F9 C: w" C0 B6 g- Q/ T3 E) o5 a
location / {
8 h: s8 a5 w2 f5 _7 r1 k1 H( B( C ModSecurityEnabled on;
( s; ~- L- x2 j+ b% _5 \( W ModSecurityConfig modsec_includes.conf;
) S' d7 N' E* e" J2 K root html;. k; }7 p7 I, B' E1 c
index index.html index.htm;# h I8 z" p# ]0 Z
}) H: }, b4 P; B: o. K
}
! T# _; a, s/ l1 E使用动态模块加载的配置方法# h0 d) o/ @) [8 v l
在需要启用Modsecurity的主机的location下面加入下面两行即可:8 b( B* l+ h( @
0 F( j7 [% G6 Q/ h1 K$ M- N. J9 T6 k2 r% u2 f3 d& r4 d! w2 X/ U
modsecurity on;
% L, S; _, K% S$ M1 Omodsecurity_rules_file modsec_includes.conf;: L6 z5 e1 |7 ]9 _
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
7 e8 i0 D* [1 \- T+ J( x9 q" W0 l
3 @, |: G: E. h, }
6 ~' l! R* u5 P& F/ A$ E$ vim /usr/local/nginx/conf/nginx.conf$ F0 w! C) r: A- m4 ^
( m2 z4 F% r- z* x) [0 n/ I% @
# y5 o0 s( a) c0 [
server {
. Z+ G. ]* h4 r& C, N8 b listen 80;
( E" }& \* @% O/ x u; | server_name localhost mike.hi-linux.com;6 C8 D# y* u) B# c7 W/ y' A% `
access_log /var/log/nginx/yourdomain.log;, f3 `/ u) S. z! `( _) `! W6 M
- W# S! A9 \" |* s) I0 W0 c! x
* b. `" B& t0 i2 @7 f2 ]9 I- S" m! Y# P location / {! W) G- @& t- e2 C4 h
3 X6 t' r& u, E# a
; d7 q/ r/ J9 Z& a) y modsecurity on;! }/ N8 W! [/ ]+ p0 y- [
modsecurity_rules_file modsec_includes.conf;
( ?+ [$ `! G2 e0 H root html;
* m/ T! x c, |" {4 X: E3 h; ]! K index index.html index.htm;: @/ d- R9 s5 r
}7 _, s# L" p2 Q3 F
}
" u% z% s! @: O& n/ m; @' J+ d验证Nginx配置文件9 v" D8 Y6 m) S) d% k( R/ [
$ /usr/local/nginx/sbin/nginx -t
# u4 O# M3 @* \* _$ v1 x0 _nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok& {$ B' U2 |4 D9 C5 M: E- B
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful2 {" q0 T' Z4 p9 O0 i
启动Nginx$ g% H, H3 @2 F/ N3 e5 q" D0 j
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
/ g0 p3 a4 ?7 Z$ n( E
4 |) G& Q1 K% t& Q9 a测试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能够战胜更多复杂/未知/混淆的攻击模式。
. t/ F9 B4 G; l8 p; e) Z |