ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。7 j! \4 N0 h5 O, p6 V
: _2 c: r# a5 i" k4 N8 h
1 `! ~; E1 [& F" s% L# @3 O在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。3 \$ S4 r, N! x8 h+ l, Z
7 I* n. w9 {, i* z) ~( {2 L0 l
4 K) F, N) @1 c, b4 W+ f什么是ModSecurity
0 r* @4 M- h+ O; W* f: h+ Y4 ZModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
& A& k# Z! ~' p5 w; h6 d4 l* u& L- Y0 h* q- d5 m& T
6 Z5 l# V1 \9 MModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。6 b3 O9 x8 u0 `" U) i
0 o9 U3 \! p8 ~
# X' O+ {# c% e. r6 lModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
% C% A0 t/ G* Q; V" m+ f6 u( ^& Z( M# J# l4 b9 V% U- r: @, r& m
; R( s3 }5 l; {3 Z) {% ^3 E
官网: https://www.modsecurity.org/% }) |4 J$ ?& l; K" E7 A
0 @* R6 m6 |( }6 H( i. y
; h i( U9 S% R* B; J% t: n9 x什么是OWASP CRS
: O9 l) g$ V& JOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。: v- }. }8 I) ~
* _( n. R' r8 j, M
- P" ]$ E9 z3 u- R% tModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
$ u, }, M0 t; f8 {" G- T6 B L7 c. d/ X# t( O" h: W: s( }' P) q
5 {, y4 \! f) i- L+ ~. P3 F7 SHTTP Protection(HTTP防御)1 W* T8 @" E- T- L7 r5 G
HTTP协议和本地定义使用的detectsviolations策略。4 c, f6 C# Z+ { `( n
* |0 D3 T) S- P, x: O+ e) T0 J2 ~
# z K# j' A; u7 u; }8 gReal-time Blacklist Lookups(实时黑名单查询)
! n! ^( W8 V+ ^, i8 J+ t利用第三方IP名单。* O- [& M" a! ?1 ^* s
' W* w4 s- g# j( r9 E5 f! f0 k, n0 V$ T1 [
HTTP Denial of Service Protections(HTTP的拒绝服务保护). w- \3 g' n7 `: r
防御HTTP的洪水攻击和HTTP Dos攻击。
~4 f- e( s$ k1 a2 u. S, d6 H% ]8 m H2 f
& X: q6 U6 o0 Q9 H" l5 F
Common Web Attacks Protection(常见的Web攻击防护)8 w3 Q( q) T% T" U# N, w
检测常见的Web应用程序的安全攻击。/ Q: u' ~* R, c3 `2 {
# f" H" ~0 ?0 y/ K) ~* M- d0 L8 a4 k l% c& B. Y
Automation Detection(自动化检测)) U2 X# {$ i% a7 G
检测机器人,爬虫,扫描仪和其他表面恶意活动。% A3 D j/ _( m$ L8 C# ^ D3 q
; h/ z/ _/ R/ C3 w) }2 `4 f5 u5 M- y" a$ J( R0 H6 B. o# q
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
" |! S8 A! Z( N( H3 d检测通过Web应用程序上传的恶意文件。
. j" S& S* z. _+ ^0 n2 o) A* v
4 P. D6 p6 e+ M% P7 S" H2 `* B- X( }* {% a
Tracking Sensitive Data(跟踪敏感数据)
- @) O' M8 u' I' i, ^3 _3 c4 \% Z) d信用卡通道的使用,并阻止泄漏。
- z& p6 T2 n2 Z9 o8 s5 N, h5 u, s" m3 m* R
* g" I+ z& D# [+ Y3 u% D
Trojan Protection(木马防护)" z( U. L/ e$ }( R
检测访问木马。+ q8 j' {& y" N% D; Q" E
4 o% v: r2 |/ b; p1 e
0 K5 q$ c _( I- ]1 \Identification of Application Defects(应用程序缺陷的鉴定)$ j# A( C" y% V; m7 P9 |1 [5 q" T# A; n
检测应用程序的错误配置警报。, x! f8 p! T+ ?7 v- S' Y2 B3 @
6 `3 C) }/ b* F% t- i0 m/ a
8 K* E, G& G6 m* BError Detection and Hiding(错误检测和隐藏)
2 T8 d6 D! f8 X2 T& l2 k检测伪装服务器发送错误消息。7 e9 [; B6 i0 O, G& u! r
9 L3 X, t6 B V' I. Y7 @0 L+ W3 T) O7 m9 m
安装ModSecurity
' R0 s0 r* `* _- r& j5 {+ M/ [2 k0 \软件基础环境准备: n+ e$ z/ I$ w1 |/ l& N- o
下载对应软件包 q) g6 ]: i! L. l t/ N3 a4 y; Y
$ cd /root
: R3 t( t1 V6 X4 G ?9 b; \$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'+ q: Y: p' ~/ _$ v, o# S
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
( {0 l' l0 ~: U: \) t+ D1 @" M! ?安装Nginx和ModSecurity依赖包
' r9 h( ^4 L8 b+ v) d4 u) YCentos/RHEL
$ u+ ^- s. G1 Z: M; ?! F4 Y7 c1 q9 `' Z% p/ p+ F
j1 t! Z! i2 B. v* @$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel7 R- [4 l D9 L4 F
Ubuntu/Debian
( ]4 [7 ~6 ]/ q; J. ~
# Q" P! k" t8 X7 |( G, Y2 e
4 ^ c1 y! e L6 `# _ t$ 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++-dev5 x$ T2 q6 b4 n& `/ ^, Z" o
编译安装ModSecurity
! G% V" Y/ H$ x) i$ ^/ }. ?Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。% w" k. d0 S l5 ~4 }) Y% J! z4 ?
& N. {1 N# d2 i% c& a7 N3 Q1 q. N: B! ~! H; s
方法一:编译为Nginx静态模块) l# Q6 \# t7 F& S- F# |* F! d
8 ~& s( f% `7 J3 K9 H. m5 h: ` Y& L; {; c: M/ O2 x- f6 P$ c
编译为独立模块(modsecurity-2.9.1)0 u6 P! t5 | {- b! j) U
$ tar xzvf modsecurity-2.9.1.tar.gz
n2 V$ v, Y ]( P9 Q$ cd modsecurity-2.9.1/' ]3 j$ \- w5 w" O' K7 @' e
$ ./autogen.sh4 t) t5 o' o8 `9 ?3 `& I
$ ./configure --enable-standalone-module --disable-mlogc
$ A# S5 a8 m1 [( W9 Y$ make; e3 X8 c L/ `" `
编译安装Nginx并添加ModSecurity模块
0 P2 m4 G- w8 t, l* _, r& Q/ H$ tar xzvf nginx-1.9.2.tar.gz1 S5 r% K: {0 i' b% g1 s4 W. T
$ cd nginx-1.9.23 w- X' D, }- S( @% k
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
# q s% F- U9 L D0 _( D T5 h- s; M$ make && make install
, J: L8 T! R# n( c- A方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
0 {1 {* T+ Q! e0 K
) |" Q0 I4 q$ Q3 e: \: S* Y0 J: R
+ I' n! u9 `2 o3 D; g编译LibModSecurity(modsecurity-3.0)) ^' S4 Z* g. f% i/ i5 r8 C
$ cd /root
; B& V9 k+ q. }$ git clone https://github.com/SpiderLabs/ModSecurity
5 }! v# {) E4 T" [" [1 D$ cd ModSecurity! y' N& C! i% Y2 s* {
$ git checkout -b v3/master origin/v3/master
- G7 D6 u7 ~3 J7 G) B) q3 {$ sh build.sh9 L. W7 @6 v3 @
$ git submodule init2 d. u; F* v, W* O; k1 ^0 F7 V
$ git submodule update
/ s9 ]1 L; n+ v: }# F$ V, ]$ ./configure' ?9 u* ~4 p7 y0 S. H, G
$ make. p3 M9 r1 X3 |* z, u( T1 _+ S9 y
$ make install
& F' ]9 X/ ]2 V+ N" a5 ^LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。9 r7 a7 I9 Q# u
7 G/ ]- V- T1 Y; T5 o7 r
' _. A$ E% {( K: Z
$ ls /usr/local/modsecurity/lib, I" ^* Q- N( [& Q
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
$ W: c& k% e& i) i+ U编译安装Nginx并添加ModSecurity-Nginx Connector模块
2 Z6 \* d# K& L使用ModSecurity-Nginx模块来连接LibModSecurity
1 w9 o6 \0 [$ |# G* c- m
) N1 F1 G/ _. d; }1 F! n: T5 ]+ M0 s9 T! f% O; r! N
$ cd /root
7 h/ \" d4 \6 l4 P$ v$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
. Q9 _8 @) O" [* D$ tar xzvf nginx-1.9.2.tar.gz
3 p. |$ r [! i; K$ cd nginx-1.9.2# _: p+ K, v# V9 U) r
$ ./configure --add-module=/root/modsecurity-nginx
3 n% q3 Q6 ]) }$ make
. b1 w0 _; D( L) T$ make && make install
" v+ V+ Y, q3 [8 N* k添加OWASP规则1 D/ V3 v4 v) F6 w/ j
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
?/ D: O! x3 M# k7 t$ T# N; v9 U* Y" x9 U, D
) [8 d* h( X; d/ l% T9 P2 }下载OWASP规则并生成配置文件
0 F( R9 q. ^5 ^9 Z- e* h$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
. ^) x8 l( a0 i0 v$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
4 U/ K7 f# h. X; P% B$ cd /usr/local/nginx/conf/owasp-modsecurity-crs% I, w8 r Y; J% A: ^% m
$ cp crs-setup.conf.example crs-setup.conf
) a. w3 G. s9 i: L" P配置OWASP规则) ? C* s! U1 c, p
编辑crs-setup.conf文件
) m* Y# `% T Y; @9 {
R5 M R" J" g+ i
+ `, b" E' Y/ I. x4 q1 ]- T$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
& m4 e9 T% t' P$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf1 L% S6 Z5 X0 O
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
; }4 _3 F6 G, K# X- r$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf' c( q9 R; a4 X
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
- R# p4 C! P; k' ?: u1 `9 r- G4 P# T4 U
7 \4 w7 f7 o) p, J) w- Y- n启用ModSecurity模块和CRS规则% K/ @1 j$ i* L2 X3 I% v0 J
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
! e# P* J+ f8 a- o6 Y. D' D+ c( ]: F1 H/ k! v' V' [$ \
) N8 t% w, p( t: I1 L
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。# P% H: m+ M2 r# @0 t U, F
# ?; F" z8 D! `, i
4 f% o+ @- R- c$ cd /root/modsecurity-2.9.1/
/ H1 t% G8 }# \0 \% V$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf " F! T+ @6 B; ~' W4 @2 c/ @
$ cp unicode.mapping /usr/local/nginx/conf/
8 A1 q6 W/ n, Q将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。 T* ~/ F$ u# y2 l
' i( z, O3 A) B+ y
/ A1 S/ b; h) I% J- W$ vim /usr/local/nginx/conf/modsecurity.conf
& t! c% F# P) M* O! Z0 ]SecRuleEngine On2 [2 w5 J# i' c
ModSecurity中几个常用配置说明:% c. @) Y5 f, t: A
" x& D8 K2 o$ J* v; @
& R9 x" ?) P2 h, p0 c
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。. ~' y+ K' e8 g+ y8 S" `
' S) I: x" i, z4 Y2 g0 Q+ A+ A
7 n, f: a0 f$ t4 o% |2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。; {7 C' A1 C1 V' o( W2 G
% V! K2 O: f+ O, P* C. v
1 S! r- X# U; d4 f$ Z
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。7 s: w' y3 g- Q" ]* m/ a" h* M
5 ]. X0 d6 ^, t) W8 R4 y! S5 s' [2 v; f5 g5 u; h. r4 X4 A
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
+ I$ a3 m, N# Y4 j: N3 \
% Z- ~+ }6 Q" h4 z2 U9 V- G5 a% e2 |7 _4 \
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
4 V; _* J K! t* p6 S9 I
* Q6 M/ q# ^. ]$ x: l3 m3 J7 k
- ~( h' l1 | w$ W3.x版本CRS6 [6 [5 W* W: O
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
" [1 O; D2 N1 P# 生成例外排除请求的配置文件
# y' c1 ~' l* N- P6 r9 L$ _$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf9 r/ x1 u& K, u3 ^5 h7 o r
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf3 U: e, p8 F* F- |
$ cp rules/*.data /usr/local/nginx/conf
9 Q9 [: Z+ @7 o* i$ U7 M" F为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
' B# _6 Z4 `+ K
( u; W* l4 M5 \/ @8 r) P5 {. K, k* X9 l; u4 }; F: O8 {
$ vim /usr/local/nginx/conf/modsec_includes.conf
. P' ^' E; f7 ^, }. ]+ A; p
: V3 Q( @' j: k- r: ^$ y8 d2 E/ T: `[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 % s/ B1 A$ F H! C, s
# H: `* P; z) A$ S注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
5 k! A0 U7 X) ~+ s: l8 U. c
: E& `' D* m0 h4 x" A: T3 }7 c
2 |# _/ X: {' J: [配置Nginx支持Modsecurity, O4 v2 x: J# P0 k# t1 M7 Z
启用Modsecurity" C7 P g5 e' @: _2 @3 J2 Z
使用静态模块加载的配置方法$ _- L! O7 g) D; m
在需要启用Modsecurity的主机的location下面加入下面两行即可:7 ^7 k3 O6 i; J1 {8 H
1 |4 N1 G0 U9 W0 N: E1 L% O j5 Q
$ a {- U# ~2 k8 y7 C4 nModSecurityEnabled on;
$ N+ T3 \0 x. m. F5 mModSecurityConfig modsec_includes.conf;# S( E2 { s( ~" ? H0 @+ j" a7 H
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
6 R6 y0 [' c$ d9 `3 @; Q* v# L; t9 m3 V6 O7 n3 @2 b K
7 S/ r. x) J3 j2 t7 t: i
$ vim /usr/local/nginx/conf/nginx.conf r0 `) \& B2 k0 U+ P8 |! [; m, m r% d
$ ~- }9 k5 t/ y8 y) f3 W9 b' X( J- d6 w# P) @
server {, b; n2 M% w+ o8 p
listen 80;
+ m$ q. s" B0 o$ d* A server_name example.com;
& A$ s4 \8 p1 c2 Y1 a
6 t4 q4 z8 L+ g8 M! E' D' @, S
* ~: t) _2 s/ Q: ~3 V location / {
& a4 ?6 _4 M' r: H% q5 X# b ModSecurityEnabled on;
# ]7 }" [, r' l0 x: T7 Q ModSecurityConfig modsec_includes.conf;
( `, l [. D' l2 o8 N root html;, h& j1 F# b) S- R, F. f
index index.html index.htm;4 I8 Q' z c; G5 B2 a+ H# u
}
* g% Z2 A' o- p- ~7 |0 n& s}
1 i+ k+ e+ `8 E. I4 p( b8 h使用动态模块加载的配置方法8 m+ Z! u/ @$ H0 R1 B* V' g
在需要启用Modsecurity的主机的location下面加入下面两行即可:
) P$ V2 A. [2 d; s. j& v! m3 B ?& r- V! N$ b ~
0 v8 h* p R8 D v" n; |modsecurity on;. A$ w; l4 e- n1 j
modsecurity_rules_file modsec_includes.conf;
) @- i; ~# c/ h7 r# E) v修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。" ]9 q' A. E, o1 \
3 i! b# B& F. T: Y6 z$ c8 x1 g+ B8 Z/ `( a! ^8 Z! X
$ vim /usr/local/nginx/conf/nginx.conf% l9 V# w z; Q7 I7 B
+ O" H; ~3 k' x7 s2 c. g, Y
. r6 _5 ~! |$ p1 a$ h# @server {+ Q! G- h; O8 H* g7 H& R( G# r
listen 80;7 i$ I8 o, L" }/ ?4 j1 _, {' {
server_name localhost mike.hi-linux.com;
! Y9 f3 h: y: ]1 X! b# [ A access_log /var/log/nginx/yourdomain.log;1 f* n' N/ y9 E$ z3 z/ b
' Q g' p$ V( N! ]3 G6 j! W
) U. A3 w4 q2 s& \ location / {
! B4 C d) K9 V s! {! }- V+ R3 M' Y& X+ s5 d* c9 _/ k/ M
# o2 E6 c5 T0 Y0 r: P6 d2 |4 d
modsecurity on;
5 R2 ^8 G, P* O6 o modsecurity_rules_file modsec_includes.conf;
' A: S( x# a- A# g5 W. n root html;+ x& I* F* }5 D8 W6 x
index index.html index.htm;) n+ _: U3 f) W% z
}% z1 V1 J. {9 K4 _# S H! y$ b
}
; c5 z4 s. @" u7 x! s& U2 S验证Nginx配置文件
! v2 Y8 {6 j- H5 J) `" f3 j j$ /usr/local/nginx/sbin/nginx -t; V* }) E0 H4 h5 }8 x$ k
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok/ F; h S5 Z! O' M$ ?
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful; y2 p/ E f3 Q) h
启动Nginx' o( T& J! Y! A& d. u
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf& t% ?8 j& X8 Z6 d1 `5 e, R
/ @+ {& z; o8 W- Q5 @4 m# e
测试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能够战胜更多复杂/未知/混淆的攻击模式。
3 F* Q B: g2 ^' B; Q |