ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。7 l ]/ F3 h+ u$ J3 ]
/ c$ e& h+ o. _/ a) B S8 I8 E6 o& _& W4 y7 W! ^4 Q
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
( X/ N2 @" y; D' Y" J
, |6 L: [7 b) ~; l% t7 Q& d" u5 W0 G3 S* c x
什么是ModSecurity
0 ~) B# f: q8 N" E% x, hModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
3 x9 @! g! P' O, A( A
! s8 Y2 ?3 k. m* m4 d; e$ c/ N% ?, G" J9 V
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。' ~5 q" `5 L, N9 N9 l* F; C
* p4 z' ^5 K4 J$ { Q: s3 l) P, W
) J, E5 K7 a- ]/ Y6 u+ @0 vModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
$ J |5 ?0 x; L1 t8 v0 O/ n* B9 x8 r# |; q
1 h* ~& ]4 W( q
官网: https://www.modsecurity.org/
0 O0 ~& V6 C d6 S! f, X
/ D. u/ X) z9 v3 k+ U$ o5 J7 ~. z( y [7 g+ f& R4 G
什么是OWASP CRS- K; q+ E/ A4 v* W6 y8 o$ A
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。) w2 }+ f: C8 p
3 l F, s1 u g2 v" C& ]7 t. Z* ^8 w& B
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。6 r* ^5 B Y% R6 ~8 r
1 E# A0 y6 u( [, M. E6 K t G+ ~2 Z3 @& Y2 v, @3 o) l
HTTP Protection(HTTP防御)4 [0 e: q* g. T2 Y
HTTP协议和本地定义使用的detectsviolations策略。. e6 F5 X' W/ `3 u
/ q- R Y$ U7 w/ D; O0 l
% Z& a+ t! C% Y IReal-time Blacklist Lookups(实时黑名单查询)
+ b$ S/ \. m; C+ C- b7 M利用第三方IP名单。
5 C, C8 [( h% r; B1 m' Y( T. a4 k j8 ]6 q8 r# [& B8 D$ | w4 _
; X8 [4 ]) B) Z% ~0 _
HTTP Denial of Service Protections(HTTP的拒绝服务保护)! Q' V* V4 ~, A' Q4 s4 ~1 T
防御HTTP的洪水攻击和HTTP Dos攻击。5 n0 q! c3 u3 v* Q) X& C. V
. x9 M( U ~' Z% v: W/ ^# I! r7 C0 i& R. O4 I( S- u. u
Common Web Attacks Protection(常见的Web攻击防护)
5 G* u; |8 `' p3 s" e! a& d检测常见的Web应用程序的安全攻击。2 L9 h: J6 v! C5 \8 P: Y2 \
& j4 P6 `$ v% g8 p P$ c0 ], J$ {. K+ l2 f3 M4 V
Automation Detection(自动化检测)
% ?3 x; W% L; k6 r: b. e检测机器人,爬虫,扫描仪和其他表面恶意活动。
" S' Z7 K+ k: w: ~
( G) P; c/ @ G, g& C& r
3 S+ `7 e5 G/ D! r8 b) O. [Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
( {3 u3 x: u( J/ F) |检测通过Web应用程序上传的恶意文件。/ [. V; o6 N. i% f
$ W% w* U" S- X6 a
- _. N$ O" v- G% d+ I' R- ~5 i4 ?Tracking Sensitive Data(跟踪敏感数据)
- V. W Y- p7 X+ [# f7 x信用卡通道的使用,并阻止泄漏。
, \$ F' G9 q7 @
* ?8 ?) `, ?; m; `! ?# y2 g8 H2 a
, G1 f6 ? s; t' B ]% F7 |3 MTrojan Protection(木马防护)# o; N2 P9 T; S4 u
检测访问木马。
7 I! L8 j& t% J$ I' O9 w% n6 J* T/ @8 C r1 s9 Y1 D/ |4 m+ l2 U
* B' T0 Z( j6 w3 {6 n) D7 a
Identification of Application Defects(应用程序缺陷的鉴定)8 [1 L' T, g3 a0 C) x4 R& v
检测应用程序的错误配置警报。
0 d$ b( W0 t& X' i( [, X& @) r5 s3 A2 j8 k! L |5 Z; P9 _' Y S
1 @0 U6 ?" v( S" Z
Error Detection and Hiding(错误检测和隐藏)4 F* V- _& f; ^& _& n9 R
检测伪装服务器发送错误消息。
; j/ M# D ^- z4 Z4 z
+ N$ I* c% B+ U1 h( j% j
8 t2 f& l5 d( `安装ModSecurity* _) F# ?) D I1 x
软件基础环境准备% ]8 f3 i* J) o9 K# A) K- r
下载对应软件包. j# ?+ T+ a( h+ y" h0 X
$ cd /root
; H3 g# T, ]7 o$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'$ V; n2 s# y) H& B
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz! M" ~' B9 y( I! g
安装Nginx和ModSecurity依赖包
$ Y. }* v$ G2 n M Z* ]2 W$ aCentos/RHEL
* n& ^3 ]3 W/ a( q6 e# Q) [& W s- b$ L+ [- m, _' N
# Z; M- e b! a% J9 b2 {3 M/ }$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel q0 \6 ?6 i. z7 q* s$ o9 {2 F( k
Ubuntu/Debian, r% G" [6 |; f8 f3 m0 H9 z
4 g" B7 m- Y6 x( f6 U+ s
6 ]9 m% g$ v3 }! M( @% O0 x5 E: 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++-dev
, [) k3 ?8 y9 `# b" q7 S编译安装ModSecurity( {; ^7 c! M" w/ D6 A3 D7 G
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
. |1 N* ~7 U% W% B7 y8 ` X R2 W% x# a9 l1 E1 u4 q
$ Y+ S6 H1 H. O/ m' a( Z8 p
方法一:编译为Nginx静态模块
9 _8 b+ X F% w ~/ ?+ U+ B: \9 h. c0 F8 c; h/ a
! n( i0 }' j6 a1 z( X5 ?% v编译为独立模块(modsecurity-2.9.1)9 T- e( h( ?( o( p0 ]* ^
$ tar xzvf modsecurity-2.9.1.tar.gz
$ w1 Z# `& t) ~# E9 u% J& F# E0 p$ cd modsecurity-2.9.1/- n* o5 Q# J5 }" O
$ ./autogen.sh+ u/ q3 b1 a- i! _
$ ./configure --enable-standalone-module --disable-mlogc* {4 Q! j( h. t# X0 a1 ^! V. r
$ make
1 n& e* W o3 h2 J6 V3 K编译安装Nginx并添加ModSecurity模块4 L. K8 X; g ?% r2 @4 W+ E
$ tar xzvf nginx-1.9.2.tar.gz1 g( _" } O6 I/ u, E
$ cd nginx-1.9.2
j. G6 U( P d, V& ]7 w$ d$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
* s" W6 p0 Q" A7 _$ make && make install5 h8 t5 ^& J; w8 k1 t: Z5 m
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
" r5 H+ ~, m. T- J$ T
2 j/ L, Z+ w' X# u* y- ?9 \9 F1 c2 N# C1 t0 Y! N& m5 d( H
编译LibModSecurity(modsecurity-3.0), n. P# k I# b4 x4 ^9 q
$ cd /root$ u% Z+ ?9 A& n: |+ a
$ git clone https://github.com/SpiderLabs/ModSecurity3 F( C/ S, C& k0 t
$ cd ModSecurity
# b7 ~9 o# G) R& R$ git checkout -b v3/master origin/v3/master
* e- F3 n5 Y- P- T7 v+ N$ sh build.sh
4 S3 x# l: P/ F$ e# ~$ git submodule init( K- I4 P2 _6 o0 V; Z
$ git submodule update
, Z5 a$ c7 e* J! f' P$ ./configure
+ d; x& y/ m- ~4 x7 w$ make2 t- n3 g" S9 F( s
$ make install
0 |" M8 ^3 r) a* Z5 W+ XLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
( `1 q1 V0 e4 i# ^! S/ C. K) n
. j8 {( }3 G" [4 p5 @) ^
+ d7 @( e. O; {$ Q( a4 p$ ls /usr/local/modsecurity/lib1 V, g+ x% C% Z Y+ N' e" b; X
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
6 S3 I6 f6 M( _! y9 H5 e* _) N编译安装Nginx并添加ModSecurity-Nginx Connector模块/ @& P4 d% o' ^4 F- @4 p
使用ModSecurity-Nginx模块来连接LibModSecurity
- s) K) J1 g; x7 d7 \4 ?& c& E
* c! f4 O2 N$ L9 z$ cd /root
7 E/ y, W- t" W- F, s$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
; P3 O6 C$ n7 E& _! N& Q4 I$ tar xzvf nginx-1.9.2.tar.gz Q+ O. Y% O: g
$ cd nginx-1.9.2
# T0 b- C" m! [' D& |4 e; z$ ./configure --add-module=/root/modsecurity-nginx
* N" n/ j( v4 I* m4 e1 g& h$ make
) C) ^7 \+ b$ ^7 u# o& W. @$ make && make install. j1 n3 k4 i0 X: x, K- l% a: |: J
添加OWASP规则
5 n+ l) |9 n/ S, EModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
3 `5 m; s6 ~1 y" R% S0 Q# t* x3 u9 Z$ O) ?
, `, w9 x" Z) B
下载OWASP规则并生成配置文件7 S2 E/ `" Y# g4 l1 f: E) r; i
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git1 x; Z. O0 |; B* S
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/' ?7 t3 H6 I; q
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
% y( i5 o, P+ d4 k/ Y- Y8 |. S, t$ cp crs-setup.conf.example crs-setup.conf
" U0 c& L) I1 N$ m配置OWASP规则( ^$ [7 K+ Z6 {4 h& ?: f
编辑crs-setup.conf文件
* V" p' L& S6 l$ ]# A, C
* T, H! r, s. S( S$ k+ _$ G, S! F' {- d
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf0 u h: ]$ `) ^" @, V. B
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf5 m: v7 X7 v# X) j3 |+ Z
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf, {8 C: A# d5 ~! ~
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf {% S* |; q7 k
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
% k: A# e* [3 s! J) @7 R3 D6 }5 [6 t1 y3 t) x/ @: r9 J: R
Y; |, H# E' ]4 q
启用ModSecurity模块和CRS规则
, d9 k5 u7 b7 E2 ~. {5 E4 S* ]复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。( u) [# ~) ~! Z/ v: m! v# @
+ F6 F) G% x' }
% K8 G+ v4 B/ s/ \modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
8 f" `9 l- A3 P+ O2 s' ]" u5 \: G( Q0 Q# l0 A3 D p( _" J8 U1 @) A6 P
7 R1 l5 W4 h# y/ y2 P$ a
$ cd /root/modsecurity-2.9.1/# y$ {/ }' H4 Q
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
+ V6 R, i' [# I, t) u+ t$ cp unicode.mapping /usr/local/nginx/conf/
; f( K3 y; E) A" G( W) d将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。, ]4 `0 @4 Y/ Z) W' W( g
+ P& C) j/ P8 E( }5 B7 [" z
, W4 J: x" E' v$ vim /usr/local/nginx/conf/modsecurity.conf( \: A6 L, \, P" H' d8 o& W; R
SecRuleEngine On
$ x" ~# f# V6 M3 uModSecurity中几个常用配置说明:
3 ^( a8 a' ?2 _2 a: u
; g) L" N6 E- ^- J* B# l& c
+ \! Y( |# ?. y$ d" A& L# n1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
7 x1 O; y! j+ z; D8 s. O8 ?% h2 Q0 q/ f- }$ n. D( ]6 t1 z
$ i O; p9 m' R9 l$ i, @2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
) P, X' D# l1 u# c1 y5 N F5 T6 W5 u B0 @* T; q
+ T( i2 S0 }2 }% u4 Y* R3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
+ t0 g( y! z4 _& A, z) y4 l: g$ F
8 g" k% g' J4 U! A& L1 ?: N* r5 }! I" |
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
# ]9 g( D g/ i, B) C& s- S! m0 k# L1 y S
1 r4 m- o8 f( } Y/ k: J
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
: y. q- t( A# p2 |! a$ F
9 ^6 S5 E7 ^6 e" i% G! g, k
`/ i7 y0 f3 t. m3.x版本CRS
$ D8 p* [3 Q& r$ cd /usr/local/nginx/conf/owasp-modsecurity-crs2 s- R+ }0 b( Y3 d# a5 x0 |; \
# 生成例外排除请求的配置文件
/ n% {, u( n8 l0 ^! K% {$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
' O/ s2 s8 J6 V; z) O1 v$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf; D& D! A9 J; i& O5 V' ]; r
$ cp rules/*.data /usr/local/nginx/conf
1 F5 Z; ?/ ^$ i为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。0 z3 R) \" }+ Q- x: b
5 `! _; d0 a! x. K6 [0 E
e/ n, E( ~, ?0 g4 F$ vim /usr/local/nginx/conf/modsec_includes.conf8 @ g' ]; n! f, A0 _
* C* ~# F8 E+ x0 B% b9 E: U[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 4 Q+ g0 ~: F+ K, Z. q* w
. J0 N3 E8 n5 T! d1 M" p! D注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。7 ^2 Z; g3 O! m* N# c
. `9 W; [+ P+ ?' ~! m/ q
: z# ~+ M f, W/ ^7 w配置Nginx支持Modsecurity' J1 R+ L; s( B) R A: v
启用Modsecurity; j$ F8 E% t# O, s0 D& {
使用静态模块加载的配置方法
8 f. }3 F) b! r0 x& K! Q在需要启用Modsecurity的主机的location下面加入下面两行即可:, t; [7 B. X# J: }7 l. T# G
7 Y' Q$ e' V8 Y& ?% `$ \, W2 F5 S# Q
# _* K- ^7 g. I# lModSecurityEnabled on;' }9 k0 |# m& A Q
ModSecurityConfig modsec_includes.conf;
+ R0 c- g, f2 x修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
9 C7 G2 D, Z1 C* ]' e9 _7 \4 m5 a5 Y! r
& |4 F3 W9 ?. }+ l/ g# z _! w2 L! f
$ vim /usr/local/nginx/conf/nginx.conf. P& B; |/ c' X9 n& ^
3 K" M( p/ f% J4 h9 d6 Y
$ Z1 w0 \7 Z* Mserver {
- b, ]) y& u- |7 T listen 80;4 r ]/ P* l7 W) ?, w% Z/ j$ V
server_name example.com;3 e4 F( v, o% C; o7 V- W; |# O' U3 y
2 u C* c. r2 m
% L5 V( A$ i. q/ j! V- t
location / {
$ ~, f5 a8 A6 v: N! c ModSecurityEnabled on;
" m7 z2 f% h* d( v ModSecurityConfig modsec_includes.conf;0 D/ q/ k' B L7 j" I* p9 n Z6 D1 n
root html; j, A) d2 p) g" h2 G9 C$ U9 ^% S
index index.html index.htm;
) t7 `' o* j/ b. f" D7 ^ }. w! g9 ~9 Y. R# W9 Q) ?- _' m
}
$ i' e. U7 @, u+ G" ^8 T! ^使用动态模块加载的配置方法/ D& j' l) ?) b- ^" R! P
在需要启用Modsecurity的主机的location下面加入下面两行即可:
0 g3 ]. S# a7 S7 D5 a/ w# P& ?2 P6 [" `9 ]# H0 g% d I# ?& e6 u
; D' T& X+ c7 C7 r9 Q$ b- G6 r
modsecurity on;' T& u; C+ j2 Q. I8 b% O
modsecurity_rules_file modsec_includes.conf;
: H& Z: m; E% [& {修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。. b0 p& O: `8 T% j+ O% S% B
# j, M- P8 ^) T1 I
6 o; ]7 y- u. D( I4 |8 r) G+ h$ vim /usr/local/nginx/conf/nginx.conf1 W" ]. ^$ O2 ?& y/ ]. L% E# O6 P
# g: E. A# J( E0 Z# I, h
F3 Z8 Q$ R; C2 g' Y2 eserver {; F# J% r T) l. _- z% c* R
listen 80;7 \8 J/ ]$ C" e- e3 n: M& `" b
server_name localhost mike.hi-linux.com;& M8 a6 Y J: h0 @( u: @
access_log /var/log/nginx/yourdomain.log;
1 @1 @7 {' m* i: \" @) k/ u7 G/ d) B& u$ b" L1 u2 K
5 ^* w5 S3 U5 v
location / {
9 v* X4 r& q; K2 Z% w' E3 U" n# e- B) b8 V$ U. H
: v& i P& N; T }# C& x
modsecurity on;5 a4 C3 a( U# j) b! Z& b C- _
modsecurity_rules_file modsec_includes.conf;' }0 W T6 Q( h3 N
root html;6 r6 r. a' \) O. [2 g' T
index index.html index.htm;
' E5 i, M" i9 {}# r* w, K+ d) c" h$ o
}, e7 y6 A D. ]9 m
验证Nginx配置文件
+ R4 G. ^0 T% U4 t( a; Y$ /usr/local/nginx/sbin/nginx -t
, i+ ^( [8 J" Z) d# h$ Knginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
8 V0 }0 i# O3 Y9 C& s, xnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful) q1 Z9 z( r5 q \' A4 J8 y
启动Nginx
- i' m! ~$ v$ A9 M' [% R' d( L& O$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf7 g+ i7 `: {; @- B
& `% j. C y! C$ P* `测试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 S I5 Z2 i2 c% N1 H l# s |