ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。; Y8 w% f8 O' V# E) n' ^
$ \/ C* X [7 r3 N0 f0 U, W# [
* o: g, B9 O7 q8 R! a
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。$ n' E- H0 c! H2 |( K) k( }1 `
0 i ^. d5 @) Y5 X
7 ^" [) R% S- Y, k$ b什么是ModSecurity6 X' e1 Y- |% O; A( R8 c
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
+ g% U3 x) f8 \2 h) [8 Z- m, S2 i' F) C9 c/ `( U, r
- {- |+ y. m5 a$ c+ s. D, m
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
% }, R1 `0 {; A+ i3 h
* H' l6 B6 ? @$ K" w
; X& h. N8 w$ u4 o' y7 ~ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
* R9 V8 @$ g3 R4 N* s
# t% a5 s1 E J: @
" q/ R- z; `9 Y4 L官网: https://www.modsecurity.org/, L) I+ n# q# k% D \1 j
" B9 Y9 R) c9 K; N/ d1 m
/ F4 O# p8 \# J: \ ~
什么是OWASP CRS
2 U& I# t- ~6 r' k AOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。% d R1 ~$ u" d3 D; H6 l2 _: `
- a, @! d- a; R: r( x# R
: |) D( g+ y# Y3 A- D& b5 Q jModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。7 M! `; D* @( t9 w" R+ O2 x& ^+ K' t
|( ?( y/ V% R
1 g! a. n9 q+ \" a. g2 M1 F& EHTTP Protection(HTTP防御)6 o, x9 s3 b# d+ F3 S/ l# A; s$ t
HTTP协议和本地定义使用的detectsviolations策略。
5 _: L3 |8 p) A( v+ [/ ]2 p* i- ?( j/ A6 O9 H/ u' l
6 ^& i$ C" x3 b; b* G( DReal-time Blacklist Lookups(实时黑名单查询)
* ]- Y: V5 F; H1 Z" v利用第三方IP名单。
! @0 E# h7 n# C$ N+ f# i U( Z a2 l# [6 k( `; @: ?, h
0 l# v, l2 w3 b2 O! B. X5 y a
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
0 ]3 T, A: u8 W* W' |防御HTTP的洪水攻击和HTTP Dos攻击。! o- \6 f$ R1 U6 Y
& l7 U2 h, U2 y Q+ g* j, W
! Y% Z2 y9 ?% X8 V+ f3 l1 M$ q- \
Common Web Attacks Protection(常见的Web攻击防护)# e- w" a* D/ A2 L
检测常见的Web应用程序的安全攻击。/ m+ D" l+ t( W8 M6 h: _
) m; g$ y' C I* R2 I1 T) V5 i
" I+ u2 B1 G+ k( {Automation Detection(自动化检测)
* f! l8 x- q- r, B% }6 ~检测机器人,爬虫,扫描仪和其他表面恶意活动。
5 B' z( L# D+ w$ R
1 Z& J" b, ]$ o$ ~9 _
" P6 X7 A6 H* ?0 G) P4 u4 L( hIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)" @8 D3 e5 y! w9 I8 m4 r
检测通过Web应用程序上传的恶意文件。$ [5 \1 w& }4 @# A4 g9 ^
G) I( l; K: K" p! j, A ~ @) P ~2 R' o/ J8 g/ n U; Y
Tracking Sensitive Data(跟踪敏感数据)
~2 P U' L9 v6 m X* e. S% x信用卡通道的使用,并阻止泄漏。
; J7 a& S( Z: k8 o" e/ ? G! T7 Z
, C1 F& b g4 O; s0 L# @5 KTrojan Protection(木马防护)
% r8 |7 F5 a1 {$ a- g检测访问木马。2 t4 L! j1 Q1 ^
; N4 U) w: h3 ]/ f4 H8 A3 |' W9 C2 h$ i
Identification of Application Defects(应用程序缺陷的鉴定)2 `7 n) Z9 f/ M) Z/ K, X9 C. E
检测应用程序的错误配置警报。
B* q( \( X% L9 K3 j
5 I; ^4 p4 t/ d+ x; W" s$ m) }
" d0 P1 D: Q+ s1 d$ R4 R GError Detection and Hiding(错误检测和隐藏)
: M' H. e# S2 ~. ^检测伪装服务器发送错误消息。
4 w7 L/ h9 D$ M# t' H6 P- `+ X( c% p" V# d
' B' u) k, A8 C% \9 B安装ModSecurity
, t6 X4 K5 R8 I9 l: o软件基础环境准备0 a, Y0 S) u0 S5 E+ i
下载对应软件包, P* O" H( C& [$ [ S$ ]) M0 K
$ cd /root" @2 u7 `# m% I+ X& `+ V
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
6 z% L& R, s4 W \; Z7 T$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
& I3 O4 j7 [. G5 C& i安装Nginx和ModSecurity依赖包9 }) q1 ~ [) y, d
Centos/RHEL$ ~6 V& g# A7 ? @& c1 g
Z1 S Z: ]0 b- F$ {& }8 T7 B+ L% R
7 ^9 r' o; m" G4 w) R- t
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel& p$ a% H( s0 o
Ubuntu/Debian
: a T& ?$ @# u4 f2 x( x9 k% b" U2 n+ d2 R
$ o) K2 h* [$ v' h @$ 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/ r% u: f/ E( ]& \) |+ B
编译安装ModSecurity8 d( B' V4 K9 T/ d* i3 s
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。 d8 R6 x; D6 G; i9 }% x
9 D+ e" ~+ }5 \+ D0 m+ F2 l7 C
. ~* G1 s* [7 {/ V9 t方法一:编译为Nginx静态模块
" i, |: x1 D- ]. Y8 W' V
2 {% i) `& E4 K: l; V" H: j; {; {! m X
编译为独立模块(modsecurity-2.9.1)5 n. _5 G! ^6 d- I1 J
$ tar xzvf modsecurity-2.9.1.tar.gz e- V$ I: a$ U# n
$ cd modsecurity-2.9.1/
+ X, _ @% @- K9 c# o7 S; |$ ./autogen.sh
T6 {( B" t* \( O! G$ ./configure --enable-standalone-module --disable-mlogc% Z! {/ G; t, e( Y$ @- H
$ make
) A; e2 S9 N1 y9 W* v编译安装Nginx并添加ModSecurity模块6 h1 g! h9 l8 \& `7 N' F
$ tar xzvf nginx-1.9.2.tar.gz
8 e/ i% s; r! O; l, ?5 C( c$ cd nginx-1.9.2
& N% F( [& H0 K: d2 g$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/9 t4 g8 J ~" i
$ make && make install
- H+ r7 k6 p. D% F$ n方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
1 V2 M4 @5 E* u3 K
j3 G- \& N& F$ @( O/ Q2 ]
1 y9 N2 O. ^0 T6 v2 p编译LibModSecurity(modsecurity-3.0)
3 T4 C# k% z6 [/ K/ [2 M$ cd /root
) z# Q g# z4 P5 k: a! b: ^9 _2 Q$ git clone https://github.com/SpiderLabs/ModSecurity! [# `9 s4 a. l6 V" ]/ q3 E0 z: x5 |
$ cd ModSecurity$ V8 I, [, Q8 R# @' o- Z
$ git checkout -b v3/master origin/v3/master
7 A* q! b4 _- Z$ sh build.sh
/ a9 Q3 m$ {2 r+ ]4 ? r/ h! z$ git submodule init+ z) E* ^( L! `7 g/ M
$ git submodule update
2 q& {0 J* ^ m6 [# a: |# ~1 K$ ./configure
% [2 P- ?2 A/ B& r$ make5 U$ a6 y, k4 @1 b, P* l
$ make install- S7 x ?7 I& r# ]$ M: {3 F
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。+ Q6 n4 N9 G- k- `; k0 x, F
4 `7 X3 `# i7 P6 @: p
' y) ^' O# m: r. {& e; i$ ls /usr/local/modsecurity/lib# g6 ~( e4 Z) F8 R! s
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.08 v& z; s# S. u3 ]' \ A
编译安装Nginx并添加ModSecurity-Nginx Connector模块
( p/ j2 p/ v5 [! J. @使用ModSecurity-Nginx模块来连接LibModSecurity
& H& D W9 |! [) q' f1 R) \3 W( p6 y1 Z6 ^0 A9 F) w
# t( g8 j I5 L, F: V5 A
$ cd /root2 q! D. ^# |4 u+ v( @
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx+ t; X n) E* R: F
$ tar xzvf nginx-1.9.2.tar.gz) q9 x- K# W% {+ K
$ cd nginx-1.9.2
$ o) Z u* U3 a$ X$ ./configure --add-module=/root/modsecurity-nginx; m/ k" j/ p5 E# S; F
$ make$ Q# p0 A& I5 Y
$ make && make install
" B: r% }6 R- x3 U添加OWASP规则- C" R! Q6 u- p0 I% D/ o& e
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。9 A# P+ \1 j1 a8 _
3 e$ S, ~. Q% R. A2 W
3 R8 u0 X+ d' A6 Q
下载OWASP规则并生成配置文件
% C+ A6 }+ p/ P4 T: \- Y$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git; N4 c7 e+ O, h5 v
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
7 Z* k( M8 X$ [" p" u$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
; u2 h0 Z; {- P$ cp crs-setup.conf.example crs-setup.conf! x+ j0 r# h9 u+ k2 E
配置OWASP规则; k7 d5 b, ?) R* M b
编辑crs-setup.conf文件
" t1 J. x! x/ Z1 J. @
2 n! h$ {4 B+ a1 x! n1 k' L4 s; t" d0 t+ s/ m
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
% k, L2 V! N* D, q$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf. Y# D5 E, g/ `
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
! W8 p0 J8 b4 w$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf: o# m- s0 ?8 d7 Y1 A1 c- r
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
% F5 F: B! i% w8 O8 p8 a/ l: n! }4 L: Y+ |+ H
2 c( G& y8 N, ?# z/ C$ j" N启用ModSecurity模块和CRS规则; z; r8 }. [ s7 j* a3 u1 d3 f
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
# z# {+ e' K* L
* e! B$ d& U& d
- e( }0 a" h5 J5 K h& n* I" m; rmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。9 h5 ]) f a2 }! f" O" b( |
# n! k2 p, q4 O$ _% @; J- }5 s1 e' g& S; P
$ cd /root/modsecurity-2.9.1/1 | D2 t9 n+ A$ s: k
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
' D& P( b! {! U3 N9 a: a$ cp unicode.mapping /usr/local/nginx/conf/. b& G$ _( d- P! u3 |# s: U
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。7 [ `) e9 J' }3 F% X( E: T
! I, r- n9 t& O! W6 `. Z/ s' W. t) p$ |, C: r
$ vim /usr/local/nginx/conf/modsecurity.conf6 o& v; u2 K t. \
SecRuleEngine On! B: F$ W7 C5 l1 K) I; A3 U
ModSecurity中几个常用配置说明:: q8 u) Z' }1 }+ b0 [
) ~, D. f; l9 Y1 T3 A$ \7 ?! M6 X
" y9 T. V; b' Y& a) d& q3 W1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。! g+ ^# d1 P4 E
2 [: v' k5 ]) ?
' g# @# C# S3 s2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
1 N2 ]$ E2 K ^' a- x6 f: @8 z( j+ Y, {5 B/ t2 D
- E+ p5 y' ]3 r
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。, N( V7 }7 K( T8 |7 y7 t/ r
5 U# @4 }) N' b! b' p$ h7 j" U$ k
1 k) d+ v8 l2 u. O/ y5 [
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。$ E7 N$ P+ v" |! X, n( W
0 ]- Y; a3 H7 {' F8 {7 y0 o) T- L: l) m0 r0 e5 @
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
. H) f' t3 }# E' D5 K8 i7 J( k$ ^# V
6 N6 n9 x1 W& a8 |/ \/ E# {# J B4 f
3.x版本CRS
9 a4 W s# C# w: m& q$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
# a7 y" \7 q. g1 U& H! m4 `# 生成例外排除请求的配置文件* x2 e/ q' y1 ~+ [/ J
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
5 n, p9 M0 _5 Z$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf3 y0 ^9 g. y% B1 g z& j
$ cp rules/*.data /usr/local/nginx/conf" E( B4 C8 s; }3 d q
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
) A0 P7 C+ O' K* G- l/ U5 w; l
8 ?$ `+ }6 o( p/ ^/ m+ y2 L/ z* m( T. M" I' A4 A9 G
$ vim /usr/local/nginx/conf/modsec_includes.conf
3 k; Z9 q( ?2 q1 G8 o- T1 {2 \& f7 Y0 h& c( m& S+ {; w
[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
7 p. B% f, ?& [5 |4 q& {+ h
: W, ?5 V- X q4 N注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
4 t! d) O: x$ n$ o
2 j7 [8 o6 ~2 J0 a7 ?1 D5 }7 _2 |# m B6 `
配置Nginx支持Modsecurity0 ]; _4 V6 {0 {, P" g: {
启用Modsecurity/ R; g& D- A7 `5 U8 y0 r4 t
使用静态模块加载的配置方法; N2 W/ ~4 R# O( {7 _& e5 a
在需要启用Modsecurity的主机的location下面加入下面两行即可:4 P6 z* \1 q$ M. Q, d. h+ A; _
. _: r) `! Q' i$ T6 R# X6 b/ h, p, ^2 h9 p i7 m
ModSecurityEnabled on;
/ d$ m0 x# N8 G. i/ D3 uModSecurityConfig modsec_includes.conf;
7 g7 ?: I3 b1 A2 W/ i2 w修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
% k) p) U6 j9 V) H! `/ _
2 X+ E9 C, f1 L5 S) R! G
7 _& V; B! S3 m2 G$ vim /usr/local/nginx/conf/nginx.conf9 Q0 |1 T, ^5 D% Z. C! v; E
( s4 L, G/ i( e6 ^- e4 t: J. u
& h& h4 v5 Z# n8 | y/ F6 ^server {& f `0 O( c, f1 G1 i
listen 80;: c1 P6 r# b: H; ~# X1 K4 I
server_name example.com;( E5 a! C3 F( ?7 q# y' }% L
& n" u+ t5 {3 u9 z0 u; D4 B+ q% C9 U
location / {& H8 J$ ?/ r* A! Q% J
ModSecurityEnabled on;6 ]' ^ E) V5 U3 O' W; I' l/ a& `
ModSecurityConfig modsec_includes.conf;
) Z& l( G) O$ R. x4 w1 l. \ root html;
7 G8 [7 P( u" L index index.html index.htm;
% ~* s* f# h- |. g$ y f$ v- ` }
3 y, ]& N; T' o6 Z4 _# e}. h; m% ]5 x: T$ o5 J& H
使用动态模块加载的配置方法
8 C7 i- y* d/ I5 `0 U/ R在需要启用Modsecurity的主机的location下面加入下面两行即可:
1 u% Y+ g( \8 A7 t' k7 _& Q b, `9 ~
7 i( T! k- h3 Omodsecurity on;
J {; [. {4 k* J+ b) |0 D. ^modsecurity_rules_file modsec_includes.conf;
; r' h% N& `4 p( e( M修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
3 `$ r* J# N# b8 M, i9 A" h4 z. B( Q# I: z+ o6 V; v1 A6 c4 c
. k5 ]. ]6 N y0 ?1 a
$ vim /usr/local/nginx/conf/nginx.conf1 C( ~4 ^& O4 ~% \
9 R. ?0 r0 n# `" C! G. M
) ]3 d- X1 \) U* p3 _
server {- J! A- f% a( t& O5 x
listen 80;
( H. p- X, R# U J! _6 I0 Q server_name localhost mike.hi-linux.com;
$ w% I) |; S) C8 `* ] access_log /var/log/nginx/yourdomain.log;
5 I/ U6 T2 |6 z$ X+ Y6 t+ Q$ _2 e8 j) j0 X
4 `' {1 }/ V6 y: O location / {
8 X3 _; _) c5 Z# P+ H2 v2 A& u% O. r$ Z" g- n
- N- l- m/ O0 q9 Z9 c3 Q modsecurity on;
6 g) \$ X7 P2 T1 z modsecurity_rules_file modsec_includes.conf;
! r8 Y5 g7 a+ F! N6 X) I! q, j% f0 p root html;9 _# |1 i" {6 R% K! l( a
index index.html index.htm;
8 N' D+ h& s! h1 ?9 c1 B}
# [5 R' {' X, q}
' c, H! o7 c4 O! Q) K5 A( z验证Nginx配置文件 W0 ?+ k" t- ]6 {; _
$ /usr/local/nginx/sbin/nginx -t
0 U" c W% D% c( Rnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
4 o& a3 [) A* \8 w/ A5 B5 l Onginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful- O$ A! n* _6 \; h2 X% j/ J; X
启动Nginx. V* {+ S, y6 G) n- u0 @' e
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf& O ]6 p# Q% e% E' d2 o
6 i& Y! L2 f% `9 C8 Y* R
测试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能够战胜更多复杂/未知/混淆的攻击模式。
4 {6 E4 I% C9 x( H# E% B |