ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。, p# L3 Q5 F3 k+ H, Y- R; |, Y3 Q5 E
: y* Q0 L: F- O
+ F# S; I [8 n在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
" C+ t" ], ~, I3 Z* [; x3 o K' P3 c8 n5 W
' O, I: x: K; v1 V4 d* q
什么是ModSecurity
0 b# q3 V& v; E7 oModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
- ? a; @+ m+ o' g4 ?4 k
W( J$ Y4 `; e% r3 v
]! F+ h! Z' T# l0 S; ~3 [0 TModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。8 f& u0 x& d1 ^7 c) P' ~9 h
, Y1 D6 ~5 N/ d0 b
0 T5 z; g( u* J' M" \6 d1 P/ vModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。! {$ m+ M7 n$ q& p" B4 e' K! |! E/ ^
' S% u: ]3 \- d+ q7 [" Y) G
% l8 Y) A5 _0 i8 M/ e官网: https://www.modsecurity.org/
* i% J+ W; [+ b3 l2 ~; `, `$ H
0 X* I" C7 B* J3 L y0 z
4 _6 u' p4 L( D- R% i什么是OWASP CRS' a9 F' q* x2 r
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。. r# H/ ?; s6 {; g# g( j& D
$ P' a/ i- D2 a: \
9 f$ `& z3 m7 T* B& q& [9 P+ S
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
, x) v4 V. p% s8 H
: V( C7 |9 v+ n! Q% R8 O7 `0 a- @; Q2 X# b; @
HTTP Protection(HTTP防御)
, F) Y, {0 o2 \3 ?HTTP协议和本地定义使用的detectsviolations策略。
7 B4 {& b. @% H3 F6 H; x) H) ^$ ^' `7 s% H9 s$ q4 T: ~# x
; [+ v; K$ y: ?' ^. `
Real-time Blacklist Lookups(实时黑名单查询)8 b' _2 P( j4 H
利用第三方IP名单。
# y4 ~/ W) Y8 w" R1 o0 o, C% |2 \1 {' o5 a3 b1 Y
5 T% n" X. q" A+ d6 ?0 @HTTP Denial of Service Protections(HTTP的拒绝服务保护): e1 S$ p% v% z
防御HTTP的洪水攻击和HTTP Dos攻击。; d5 E; q6 ?: J1 {
. D1 h4 T" n! c
) L/ [8 P9 K7 _: [0 JCommon Web Attacks Protection(常见的Web攻击防护)2 q& R, |0 L) s
检测常见的Web应用程序的安全攻击。) b7 L; _ C; h( [
0 z1 N# s- W+ Q6 T3 T' y y) ^0 f2 T% j/ W' o' J& K
Automation Detection(自动化检测)
8 f# R8 y7 w: S* X7 `- A检测机器人,爬虫,扫描仪和其他表面恶意活动。* r$ H E- S& `! `3 d: H+ {
# r8 h+ n3 R$ N" h8 E
( B& r' {1 Y2 }0 \0 n, hIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)9 M+ p; e! q0 C9 \
检测通过Web应用程序上传的恶意文件。
# ]3 Z( C* W7 X/ Z8 P2 M* y# R2 }( W" O/ x
8 y* S7 M" I( _, r7 _! STracking Sensitive Data(跟踪敏感数据) U& y: E D/ o0 [8 U1 O+ t- [. a
信用卡通道的使用,并阻止泄漏。7 \/ Y) ^9 p% d a+ ]
: B3 K" [ J( ]- E4 k4 d! Q/ d* \) V6 q9 l5 s ^: Y- E
Trojan Protection(木马防护)
8 L0 w0 J2 Y) r7 O4 f4 H检测访问木马。
/ B7 y% {/ k7 z3 E: D k, z; ~, k/ Q9 ]6 J
5 L. z' B0 K4 r* X6 _
Identification of Application Defects(应用程序缺陷的鉴定)" M" ?5 d& @: w# y
检测应用程序的错误配置警报。
( l' ?1 D' C0 o. C
5 m% Y2 z2 a8 L h9 R1 o5 A% `9 B+ ^$ @! p
Error Detection and Hiding(错误检测和隐藏)5 d6 V/ ~: I0 f; Q* u4 w/ P$ j; G
检测伪装服务器发送错误消息。
' f; d) g8 z$ ^1 @* v+ {
; G: _* b$ o* Y% b& r' ~5 h& z J- e/ h, z" E5 Y
安装ModSecurity
1 D5 T4 C; g& r& L- N& b; N1 g& c& s软件基础环境准备
: K! S$ K5 {3 v' _7 I/ S' l5 l, h7 `下载对应软件包
$ L4 Z, A+ C( U" I0 e# ~+ d W$ cd /root
. K! k% M. l3 e$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'6 x M$ Z5 d1 H6 w: F, `
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz6 n% @; z! @: w" H6 m' f
安装Nginx和ModSecurity依赖包
t3 k! w+ ~$ R! W. Q; kCentos/RHEL6 E3 r% [# j( e0 j/ t& L! z% d P8 C
' m9 T, N& Z( F% _3 W; R/ G4 z! O' }1 u# Q8 K5 ~
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel0 ~+ k! y4 |0 D8 R' {. q
Ubuntu/Debian
' _, z9 w- Z5 w1 j4 @4 U$ x" }) F( i+ O6 j7 x5 _4 ^
; Q7 u1 x; Z6 b7 [! V; M$ 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
- {% w3 S2 A; J5 V, u编译安装ModSecurity
/ G3 f f5 [% [4 n% Z n) M0 O3 sNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。9 {* g# L8 t- i, [/ H' j
& ^2 @* [+ _. H
/ ^' [: o# N7 ~5 r; L- i
方法一:编译为Nginx静态模块
2 v" A' D% m$ |/ }6 p! ^' U. E* O
& ?- L W9 | p
5 G! P+ {, S* y; u) x; E$ d( e编译为独立模块(modsecurity-2.9.1)
0 @ s: r: O* Q! U0 Q$ p$ tar xzvf modsecurity-2.9.1.tar.gz
$ B4 u9 L: ]( n4 |7 b. J$ cd modsecurity-2.9.1/$ q3 E* i3 _$ F
$ ./autogen.sh
7 Q, a6 c9 f% T4 J4 F- R1 Q+ q$ ./configure --enable-standalone-module --disable-mlogc
* Y `/ ]. k" h5 [& A$ make
) b( [( M& Q1 K$ N: c& g编译安装Nginx并添加ModSecurity模块
$ _& Q+ P$ _- O p! k* s$ tar xzvf nginx-1.9.2.tar.gz8 m" [$ I: U, u1 y0 V( G+ G
$ cd nginx-1.9.2
' T9 J9 g. Y* M9 S' d3 g! b& i$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/. R1 k* K6 ?, c" r: J
$ make && make install0 {# `' o5 ^4 W; Y
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
# E7 ]* k7 Y' m5 ~
H5 l/ K, O; `4 c( W, }+ k
: f' ~/ @ ~ e( A编译LibModSecurity(modsecurity-3.0)# T& d+ d# G* H$ [
$ cd /root: S0 M: t! _) ~9 Q% I) ]
$ git clone https://github.com/SpiderLabs/ModSecurity
* C7 ~7 Y6 f9 x s$ ?8 w$ cd ModSecurity: D- j3 r3 f' F1 h8 C1 o/ }
$ git checkout -b v3/master origin/v3/master
2 ^5 Z, ~! Z( E ]& g$ sh build.sh- k2 `+ C" S* ^, ?7 Y
$ git submodule init1 T1 t }% N) X' k
$ git submodule update
1 ] i, ]# R. _7 I2 B$ D: Y/ I$ ./configure0 F, C" Z7 V$ P+ K
$ make
G2 w: M, k C* a$ make install
1 v3 L- t" |0 Y; K8 q0 Q; M# wLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。. k- S: o; d2 F) y I+ J2 p6 \
- R6 F5 g! \/ A' I; ]1 G0 k- |
7 T* ?# v6 a6 t4 B
$ ls /usr/local/modsecurity/lib
% s9 I0 [( o7 u, a1 a$ d; xlibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
6 J( v) z4 [" L' p3 Q& C编译安装Nginx并添加ModSecurity-Nginx Connector模块 q" D: l9 \1 d6 O9 r8 n& |0 |
使用ModSecurity-Nginx模块来连接LibModSecurity8 m4 `# t/ S% n6 W
7 i0 g) V+ H) \% Q# t2 U
5 q- C/ P0 X1 y0 b2 U$ cd /root) m/ B6 N8 p* m& R" a
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
& x! e$ r" _* A0 s8 e$ n$ tar xzvf nginx-1.9.2.tar.gz
" b, F/ C' y3 Y1 X% e' f R$ cd nginx-1.9.28 W* j0 t) N. U1 k: o, ^/ `
$ ./configure --add-module=/root/modsecurity-nginx
# D1 W; j7 q* u4 T$ make. L+ |1 n0 I+ }
$ make && make install
+ t6 \5 r" H! M: c6 [0 I( y D6 |添加OWASP规则/ R/ w$ g2 g; g6 C. P/ e
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。: w o0 X5 k) k6 R$ q0 Q$ @$ v
6 B% J1 C# q: i8 Y# V3 O
+ U! x0 Y3 X D) }3 a下载OWASP规则并生成配置文件
f- _) ?0 a) g, w j( Y$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git$ K: j4 V" v( r" g
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
# K e$ Y. V g$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
) A9 C# ^6 O! P+ Y0 A3 I7 o$ cp crs-setup.conf.example crs-setup.conf! z: n4 E$ Q. S9 s
配置OWASP规则+ @6 K( X. L; ~* m
编辑crs-setup.conf文件: r1 Z: i8 \) a( [" A( L N
% E2 B4 ^9 |! N+ f$ y/ ^
' Z( D$ Z: l1 y# T! X
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
2 k8 u! ^: F+ H; E( c$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf* `: x7 I8 V3 \- ?
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf( d' u2 L( u% R0 y) Z, E
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf! E+ a) |( k" M+ ?
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
2 L. ~2 ~1 \0 A* {9 x& B0 {: E" ~# G( O8 k1 ]
' N3 A4 B6 a1 U6 V& p% `
启用ModSecurity模块和CRS规则/ w7 o6 |( T, V' z
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。, @" y# f6 {; i
% k( E1 m" L9 @+ F, Y, B, _, C$ u* S$ S P; b- ]' n" g; V
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。0 H5 Z* S1 n8 f' w5 x& n
5 b- I$ d- y; O% g% d
2 F, r0 ]% m7 B ?+ d* W$ cd /root/modsecurity-2.9.1/, v) s5 ^& a" H7 \: ]
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf ' h& {, M1 q. t* K ]
$ cp unicode.mapping /usr/local/nginx/conf/
& l; F6 R. d S! r5 z, H7 y3 r" u5 Y将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
5 r; T% K+ _" T0 P! Y# r
5 H, M. D* k+ q7 O2 N+ D% ^; G2 J: N t @; ?2 D. W$ m7 l+ c
$ vim /usr/local/nginx/conf/modsecurity.conf7 B" m2 l9 g+ A; q2 P4 O! b; o
SecRuleEngine On' m5 L" F; t p1 Z2 d3 y' A
ModSecurity中几个常用配置说明:$ `6 g8 a" z; I# a) o
# L( P. c5 a8 i" {9 h7 l+ y/ @; i% a- T
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
! \, s! s! U8 D4 M2 {5 C& V6 w, m3 g5 A9 u4 z5 Z
" L8 A3 F$ a# |3 N& U2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
4 k& Y o# R3 j3 m7 `) b8 B
! L: i j Z- H9 s! H
6 O1 a. [6 Z3 g: `+ y' I) ?# k* U3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。0 K" _- |* R: y+ B" g
8 E- r( E. f( q% D3 d
1 [& j2 K7 }4 C" m
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
" k/ {. q2 H! l. r$ s6 C, U( N: b1 e6 q. W
$ |" Y! [; y3 `, N f, l
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
( H5 { o X6 [9 O$ o" Q8 R# U# \' |9 O& T; t9 [! {3 W( L6 _
# k2 e/ c3 k: g/ ?" K+ X! T
3.x版本CRS
! G. g) b' w0 m# ~. `$ cd /usr/local/nginx/conf/owasp-modsecurity-crs x0 O8 o7 @0 }9 A
# 生成例外排除请求的配置文件
6 x8 Y! `/ w( E$ E5 R# w' x$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
3 w$ G. P) l( [( w* g* i _, q1 p$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf8 i/ g$ v; I& q
$ cp rules/*.data /usr/local/nginx/conf
/ O( K. v5 R1 e为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
% r e' E' h; G& p" }) X9 U$ K2 \: k |5 c. ?7 f% P6 a
" q( F( l) W5 }: M/ U4 A* w5 [
$ vim /usr/local/nginx/conf/modsec_includes.conf5 y# S3 X% }9 ?" z$ M
- C5 [; ]& A! @0 X4 Q) v
[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
; |: X; C( T5 u* e6 g- M- h3 [6 e
2 l7 c& S" z$ k7 y# ^注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。9 }+ S- g7 T( v4 v' p) i) w Q4 \2 N
# \: r6 d' @& E8 i* ~7 X: F& T% y% B+ y( j) P7 |& k
配置Nginx支持Modsecurity" i3 E7 r, H% b2 ]0 d( Z
启用Modsecurity
3 }7 K' E3 n7 B4 v$ W; a$ J4 [使用静态模块加载的配置方法
* k+ s. Y& h2 p! D9 f- u* u1 b在需要启用Modsecurity的主机的location下面加入下面两行即可: W5 T7 N; [. J, r! P( W
# }' F: ?3 B: ^: E! A
! p( m0 B9 X3 V, u8 W! P
ModSecurityEnabled on;
* C( ^/ `8 H5 T0 f1 RModSecurityConfig modsec_includes.conf;
$ `& _) _! S$ q) b修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
2 d, ^. a% @- B: R1 w6 v
, x$ s4 y2 W# x/ Z( m. D+ q* s7 l4 m0 M) L2 H0 v( b
$ vim /usr/local/nginx/conf/nginx.conf
! {6 v/ z1 d7 a% J. K( l8 `7 ]6 c% w3 S. k/ [
$ i: N4 H3 V/ N+ d3 |! T! ~: A* `server { x- p, S0 H/ B, b. B+ c
listen 80;9 x4 l4 B9 ?/ [7 h9 Y4 q2 G4 V% ?
server_name example.com;
5 D9 [- E2 ^1 M& m) t7 B: L3 x* ] D8 v8 ]& y# N+ \7 q# E
m, w6 m, M3 c9 L( L location / {
+ }1 ?3 P/ k. Y. ]& U; ]" I- [ ModSecurityEnabled on;
; f o0 [4 G: a) n' N ModSecurityConfig modsec_includes.conf;) g n6 j+ f" a6 B) K6 J* M9 I
root html;
" {* t. q. o& L8 z+ x7 y index index.html index.htm;; b8 Q# f9 y- j$ K& A
} i; h4 L: {) [1 a* @
}
* @* z) K& q$ M+ R! u+ D* z使用动态模块加载的配置方法: ^* l; m) h! i/ \0 {+ v
在需要启用Modsecurity的主机的location下面加入下面两行即可:
/ [; N+ ?( F( B9 W4 F* d
9 z& J" C( l' C& u. A% P+ Q7 ]* ]1 F1 u7 C9 q
modsecurity on;
% ^1 e. Z. U; K ~/ Hmodsecurity_rules_file modsec_includes.conf;$ b4 H1 D$ C7 {1 j# h3 M+ t1 |' G
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。3 k5 V1 a l" \
: y1 N* M! i8 E' v' E% r3 C
/ ]! u) J, G0 D% g5 z. \' g
$ vim /usr/local/nginx/conf/nginx.conf
/ A4 { d& t+ N2 h: p9 u5 x1 ?& K9 s. G! U5 r6 U
- e- k( z$ b! e! D G9 u
server {
% t/ {) z8 Q, x0 p listen 80;
$ z# b8 Z3 v+ l5 b" l/ V server_name localhost mike.hi-linux.com;& f' R2 u( } x0 h1 `* K
access_log /var/log/nginx/yourdomain.log;, }, c& T* I( m; B! d1 \
$ a: [$ I, F* [# w& i' b0 _- ]/ E5 ~ m, a) B
location / {# o+ n' w% E( R
, f) M# B5 i6 X7 ~
+ [/ r. a J' } modsecurity on;
) @& r6 k% ?1 `7 A. C modsecurity_rules_file modsec_includes.conf; l" }) _' {- C: e% V" P/ _
root html;+ A: ]; a- L4 e8 z e W% i
index index.html index.htm;
# X0 ^/ C. G- _% h, F}0 ^+ V) p- [* ]/ y' H
}& [, @8 ]$ ]% X3 c/ c1 b7 n9 f; t
验证Nginx配置文件
4 H# [0 E; c( e( G& J$ /usr/local/nginx/sbin/nginx -t
+ m) A: U9 \, K' ynginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok9 g0 |2 X. R) c# k0 O
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
9 }- y. |5 J8 t# D4 i" a% j- M启动Nginx
5 |" m& p+ r% D1 Y& A. P* b+ i! X& A$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf, j7 R) _9 b& d5 d' `- [5 c- N3 Z
; i: q/ J: j: \, b9 j* Y: z测试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能够战胜更多复杂/未知/混淆的攻击模式。
, Q3 N4 L7 e: W7 e! G- z& l2 J& M! T |