ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。* n& ^8 c9 |# g& ?7 l4 c$ Q
) B4 V+ [+ T9 W4 D9 B5 F3 H9 v
8 x9 K# O* \* w) g8 o在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。- N2 w7 J# H, O( Z
% r G. `1 d0 U% `
' \7 ? z( x8 b i什么是ModSecurity
. K! K) C0 N; F$ w) ^/ fModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
* S: V" ?9 {7 G
% f6 i% M/ V8 J( U% j! a; P1 p U9 m- ]" y) s
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。* @+ @! I% s9 T5 k2 _$ t _9 q
& x+ L# s/ s% L% j* u* I$ w0 Z" _
* G2 m- S! {8 [9 b
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
. p2 _$ B. p h6 ]3 P9 D0 k
L3 r1 h( ~$ n0 a( w# q) N- I% r7 H0 u3 K. |; n8 d2 G& ?1 b5 p
官网: https://www.modsecurity.org/
. @2 O9 t$ f. D. ?9 y$ j
$ @( J' F+ b, R# s, h5 z6 V/ U, h$ Z
什么是OWASP CRS
; F% k: O8 G* W7 z) f: I8 j) DOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
" e, _& O2 u. v! ^6 J; n: z/ n& w3 D" @. ^: S3 R3 W8 p3 f
& d, H: l6 x0 Q5 ?% _1 m2 D$ d
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
g2 i# n |; Y9 r; ~4 Z& f4 S9 d$ O7 W1 A/ l: u; M
- q; x* \9 O: p M2 I* _HTTP Protection(HTTP防御): |6 q& G) [$ k" g5 E2 S; A5 G
HTTP协议和本地定义使用的detectsviolations策略。& ]5 M2 ?' k+ M+ s
, V) _1 k. y: j$ d! N$ _% o
. S! t7 r5 r: b' J
Real-time Blacklist Lookups(实时黑名单查询)
( M& q* u7 x3 x- I" U {$ i, u' g利用第三方IP名单。
, G" C9 \3 X8 ^
4 N( E1 X0 n5 G# Z
( a3 a2 s8 Y* Z/ A; P0 bHTTP Denial of Service Protections(HTTP的拒绝服务保护)
5 c7 l. Y; U: m C防御HTTP的洪水攻击和HTTP Dos攻击。% ^6 G& |4 E, _. X
G! A. ~# B3 I; Q- [5 b' O
8 t) D$ z; ]4 e& c/ cCommon Web Attacks Protection(常见的Web攻击防护): q7 B: u1 A, @* O
检测常见的Web应用程序的安全攻击。1 u$ U0 `9 \7 D" M
0 k2 R; P6 Z3 S+ O' F" `1 Q
( n7 n, T# n1 c8 T# kAutomation Detection(自动化检测)/ C- _# h7 h$ a9 |" a
检测机器人,爬虫,扫描仪和其他表面恶意活动。5 N* y% t" R. t6 T% v* A; u
0 J" c+ h4 w% e! w8 U. |. T4 X/ r* Q( L: W
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
. F5 Z; {+ S* t$ ?检测通过Web应用程序上传的恶意文件。' O/ h0 P! }0 W( s4 I- B
0 ~, T M; ?2 C+ I
`0 f3 O2 p5 o+ j" E$ |! S2 L g
Tracking Sensitive Data(跟踪敏感数据); q$ y' c: Q' X+ }# E3 H) O
信用卡通道的使用,并阻止泄漏。
. m5 _% B! l$ D+ p+ a! u' C, A3 B% R6 v# P& ^/ _
) C- s9 `% }( P( u6 V0 O( G2 U/ n8 U
Trojan Protection(木马防护)- O# E( d8 f- `) w& z9 w
检测访问木马。
8 N$ O0 H4 w Y, K$ x& C. C5 Q- f; L7 \; O2 i( l/ Q
1 K/ r$ }7 N2 K2 V$ _7 eIdentification of Application Defects(应用程序缺陷的鉴定)3 `% f; T5 j2 a) d5 f4 q- [: z
检测应用程序的错误配置警报。3 D# b+ ^! D ]' c- A( n4 u
5 b$ M: Q5 O' r
4 }' o3 N" j3 ?' }2 e! wError Detection and Hiding(错误检测和隐藏)
8 ?" t9 V6 f; e$ l6 X检测伪装服务器发送错误消息。
: U, f0 K. S+ @, h, w9 {& ?2 n9 _$ _( F- ]2 ^9 V; x
) s# \: R3 L# y( [/ P
安装ModSecurity+ `7 j( c6 V, J- q
软件基础环境准备
6 Z; I1 _9 A2 y/ V9 E6 B下载对应软件包
4 u3 a6 {, d* X3 S- Z* ~6 T$ cd /root* L& ]' |+ R, O7 @7 V6 ~
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
: W3 i) |! E& ?+ Q9 C4 u$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz3 _& d8 _# P& R F' z, y% }, _3 @: v
安装Nginx和ModSecurity依赖包; C* R. ]0 b c4 ^0 ^( a
Centos/RHEL
/ Z. j0 f+ o) X, o7 r
4 ~; x9 q9 k+ _# i7 q6 {6 Z/ N! r2 |
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel0 V# K# q: w2 n Q
Ubuntu/Debian
+ \4 J( H# g2 y+ ^( e' g; t
6 a5 {: ?# c4 r, z6 }; A& m2 ]' I% [ j3 c! c7 e4 s: J6 w) w0 w+ 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. u; D5 z4 r: m g/ t
编译安装ModSecurity
' l# V' [) R5 C- m, r4 V/ n$ ^Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
* y, h4 L( D1 ^1 _8 c, ~5 k
) e" y, M( ]8 Y4 E. I$ h" r& C! s, r8 j
方法一:编译为Nginx静态模块2 T, _) e/ o7 g Y2 y( }5 g
5 Q- f( s$ m4 S$ m6 `
- T( R: M/ ~! Y: X( ^: p编译为独立模块(modsecurity-2.9.1) ^8 V8 S$ `- `$ c
$ tar xzvf modsecurity-2.9.1.tar.gz3 \# _* j# E+ N1 j) ?% I5 d
$ cd modsecurity-2.9.1/
& L" V, G( N/ d( ~# ^$ ./autogen.sh
5 g) R6 a0 n$ p" {* {2 r8 S$ ./configure --enable-standalone-module --disable-mlogc
' H7 X) O5 t- v$ make( x8 N, U. K" [& N/ v! U& Z: Z# A& y
编译安装Nginx并添加ModSecurity模块
9 Y7 U( B# U& N4 W$ tar xzvf nginx-1.9.2.tar.gz! N, m' ^% ]6 K" N
$ cd nginx-1.9.2- H2 O) j, T; J5 [9 M% {
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/8 C3 z$ q7 P/ s; D
$ make && make install
0 r0 M4 i0 L# S方法二:编译通过ModSecurity-Nginx Connector加载的动态模块; {7 t$ u9 B( X$ m
* }4 F r+ P# y$ n) _8 ] {3 ]5 Y4 E5 m) k R" D: T
编译LibModSecurity(modsecurity-3.0)
! S4 m& V5 a. J8 ] A$ cd /root
9 T/ P. y2 `: b3 D) _+ Y: l8 p$ git clone https://github.com/SpiderLabs/ModSecurity: J& M8 ^0 |5 }
$ cd ModSecurity
# u2 B9 G9 U4 A4 C3 N$ git checkout -b v3/master origin/v3/master
2 D7 I; r1 J) U$ }0 K$ sh build.sh. \3 b+ ?* R1 E# _3 I* x( C4 ]% ^
$ git submodule init) u% C6 [0 S+ |' I3 T c7 Z& i* B
$ git submodule update) e" a1 Z# }7 k( j
$ ./configure
9 Z# T7 D4 L2 K5 E- x! Y$ make
* v& u X5 ^: C$ make install3 \+ C! z4 N1 @, Z, o/ V
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。1 k# E5 }3 Y" c, o. _
4 g( N k$ S4 P) `* T% S I8 @, P' \3 X$ R. D' }. y* N
$ ls /usr/local/modsecurity/lib6 }. Z2 ~) h, i2 ~- G% i$ u
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.09 N% i$ x ~( R( o) w. g
编译安装Nginx并添加ModSecurity-Nginx Connector模块3 ]! w; J# }$ D7 l& F- M
使用ModSecurity-Nginx模块来连接LibModSecurity$ I5 J* A' Q- p9 a
( r, x* W4 M( |# f/ c
, V6 j) S/ K, n/ _$ cd /root
8 w. |) X! g+ `& o1 F$ g$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
; z7 B$ [" G# M6 r" Y* P: ~0 d$ tar xzvf nginx-1.9.2.tar.gz
8 y. i9 ?7 t! }$ cd nginx-1.9.2+ x a$ ~. {' s, E3 x) E
$ ./configure --add-module=/root/modsecurity-nginx
7 h+ \' x1 c3 h0 N% W! Y" D2 u$ make" b* ?5 i1 Y1 v6 w p( A z
$ make && make install+ Z! d; u/ o; {
添加OWASP规则 I4 z; O2 U- d4 d; ] _% k. t
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
" i" {1 [; }( k2 V
. {6 f7 ?$ s" d! n% u' N C5 R7 d! W d
下载OWASP规则并生成配置文件
5 r) o o) ]% m0 Y$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git/ q. t( y# D3 D5 Y- u; P& H+ Z
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
1 {5 G7 I4 l& Y0 Y9 j# U! i7 d8 \$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
7 L5 {" B3 [$ V5 ^$ cp crs-setup.conf.example crs-setup.conf
/ X# o8 J" ?. d' K4 V( K; ]* h配置OWASP规则
/ i" q! v3 v0 o! h: o+ c. k编辑crs-setup.conf文件
+ r& }) X3 X( |2 I+ I; j5 d. r
+ k3 A$ f4 ~1 D5 {$ m, L4 [/ q$ T5 j
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf u: a3 S& d0 K
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
) [( Z5 F0 t( d& [9 X" y9 {$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf, @. J5 r( \7 ]2 s
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf/ L! j. ]. ?% o' W: Q# p
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。% n& ` i1 B' E+ Z! o
# X7 M" B( B* q3 J% } M4 j) R& v& p6 k6 Y9 v
启用ModSecurity模块和CRS规则
0 B6 N6 H7 a8 q+ i6 S; m复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
* ~& D/ C8 U9 q3 W/ D0 P% m! s4 J% {
4 w0 a! Y4 ]" @3 K( v+ u
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。! U2 r# r0 [8 S; {8 }
0 c* e8 _0 C8 S# v% z9 k- K" @( Y+ Y: f! Z/ v
$ cd /root/modsecurity-2.9.1/
2 p! K/ l2 o% S4 z5 n$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
- |( `& f- H, X1 [: d$ cp unicode.mapping /usr/local/nginx/conf/4 A6 [1 ? j6 k% d$ U
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
' u8 `+ G. q7 C' U# T0 }7 ]9 i3 h" g- O p
5 J% ^( j" n8 G) i, ~
$ vim /usr/local/nginx/conf/modsecurity.conf
R, ~* |$ i# o! lSecRuleEngine On
2 |% M1 L; {+ U) b. B3 o- H+ KModSecurity中几个常用配置说明:. [: s4 x3 ^" O. f* B$ `9 [- y3 o
: W* B6 e3 z# J$ S5 F4 J9 X# t
% J! L l' M1 q. U* |: l* p/ N
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。, e H+ s- J5 i$ X9 b# h
. L! e# d) c! b- S2 I {# h3 E/ {) L. O0 B7 v+ |1 h9 p
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。( m& g$ ~1 W! Y1 `5 p% s
+ e1 |6 `; b8 d8 u% `: {4 ~# f
" h9 D. T4 T" _& _9 h5 K" }3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。" x7 h( x" Q9 K* Y8 o' W0 m
8 j6 D% N4 N" Z/ _
- c( \6 F& ^. ]6 _+ _4 a4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
$ x6 T4 {4 F9 C/ n' B) T! A/ J" ?* |% c4 M" Z
( p/ K3 q1 k" X/ c. ~" ^& m3 P在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。) d1 m4 s8 D& g5 \
4 K; [- X- U0 d# h/ y1 O/ S
7 {! H" m, N+ p3.x版本CRS
$ p4 w+ D# y- c) @3 v$ cd /usr/local/nginx/conf/owasp-modsecurity-crs( V+ `8 U& I5 L7 ]; ]. L1 L1 R. K
# 生成例外排除请求的配置文件/ V, Q/ J6 l$ o R% T0 G
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
! J/ R. ^1 z0 y6 l& T( N. ^ T$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf G" a! Z* _2 l( x8 `3 W# l
$ cp rules/*.data /usr/local/nginx/conf* p @ d+ N* P, ]( k0 a+ J4 D: O
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。& r; H, ^- b. U- u. ^
6 n5 S; T3 [0 I& d1 O- S
2 e& w; Y$ i4 _6 g" y$ vim /usr/local/nginx/conf/modsec_includes.conf
# t! f7 W, z8 E% j+ ?( _
6 d& B$ G" z6 E8 `+ N[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 @/ }; V3 A+ |1 C
) R) _5 b& a; E; y0 J, ~注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。% D0 a! P j1 B+ y
8 m) ^$ Q) F" q$ s9 \9 }. i2 L2 u/ O: X5 W- m8 Z# _* b/ m
配置Nginx支持Modsecurity5 |. m* B, [6 ~' b7 P
启用Modsecurity
6 I0 l( e& s! S( C使用静态模块加载的配置方法
0 r" S( X- A5 }: F# d5 u在需要启用Modsecurity的主机的location下面加入下面两行即可:
& \, P/ c1 I$ K# l3 e9 T' m# Q$ c, W$ ~0 y$ K
( d4 P$ _( s: [( C& N1 h5 m
ModSecurityEnabled on;! A% s$ {2 A- Z" V: A- p
ModSecurityConfig modsec_includes.conf;4 k% D% o. n, l, p. e2 Z2 l+ K
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
, w0 q- t, a6 G9 ]4 @2 n9 F$ S
8 q" P# @! Z/ g) |
, W* W- _' j `$ vim /usr/local/nginx/conf/nginx.conf
' X& {& P, H& \' \: H) e/ O( @* r
$ F- c. G) C4 z
1 W/ h! I0 U+ a" v( o2 q& d0 zserver {0 M) J; J+ [. P- Z, c. o
listen 80;: ~3 ]( q% D: T+ R6 S) F
server_name example.com;5 u" H9 V) H& @% p" ]! d
1 h! }. S) @; x( |/ v1 S# G: p8 a& I
location / {
4 X5 x7 Z* X8 i" D ModSecurityEnabled on;
" @5 z- u A' R; S- k ModSecurityConfig modsec_includes.conf;$ P& [( C( `6 V% A4 ^
root html;
) A. j1 f" P. u2 n1 u7 C" L2 C index index.html index.htm;
6 S' J+ r0 z3 f) y, s }7 d( ^ f8 W/ j9 e
}
# j4 O8 _5 W) H% V, x使用动态模块加载的配置方法
8 l, m O" z& Z$ s; g9 P7 e在需要启用Modsecurity的主机的location下面加入下面两行即可:
3 Z) w! I, l! u$ U2 p
( f% Y2 k8 Y7 A
0 X: X& ]% Z$ pmodsecurity on;, l/ n% s& {5 e% W0 |: L T" F
modsecurity_rules_file modsec_includes.conf;
- F" M( e2 N0 ]修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。2 O% ]/ R& D/ O# P
0 R' _# ~) X2 `" ]7 {- u& J2 b1 G1 j% C w9 X! S
$ vim /usr/local/nginx/conf/nginx.conf& P! }5 z1 R# l( ^0 e! P
1 A" |; s) g' n, {$ f
L. B- L$ a. C* f# [$ }' v2 T
server {
: w( U# a# s+ p/ X! i5 J. _ listen 80;
9 d, B0 _: x5 d server_name localhost mike.hi-linux.com;& C- W. L. M- [: d# T
access_log /var/log/nginx/yourdomain.log;
, p# u9 D5 K5 b* Z
* z$ W- Z: x6 i7 o8 M; v1 a0 m" l5 E7 L1 h/ h
location / {0 M$ G+ \3 A& ?' l. H
; m/ K: y" j5 R0 i
" x7 B$ p: a9 X) W7 Y# a \3 h
modsecurity on;# U, f% U1 h' j9 E
modsecurity_rules_file modsec_includes.conf;# }0 W" A+ E6 } T6 [* q
root html;
! C# O; Z) d% \2 q# @* t; q% H1 g index index.html index.htm;
% B" X7 n& c" }9 R1 Q5 {}9 u) M' ]1 w$ c* {, ^* p6 t
}
9 H# ~& e5 @0 V5 B9 j验证Nginx配置文件1 _0 J+ m/ t2 b2 [/ F( S
$ /usr/local/nginx/sbin/nginx -t
+ B$ I1 {- m0 x9 T: Mnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
3 U+ Y7 e+ ~; F0 |) Tnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
! v2 I& K6 O5 U启动Nginx
/ n0 x/ h0 d2 P4 w5 J$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf) ]% r, C7 Z1 H! I, M
5 [- v* M6 x" ^- @7 ^" Y测试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能够战胜更多复杂/未知/混淆的攻击模式。 8 |4 M3 L4 Y( b1 [$ `& s. ^
|