ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。0 a7 U7 H0 Q6 M9 d
7 ]; @2 o, y% K$ D: i1 X. ]
' p6 V# h% s+ ^2 z$ U
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。0 L L; B$ \) r+ q0 V
1 c1 C. x7 b7 H& h6 _
2 C& t; h- V! I" a什么是ModSecurity8 a4 y* H5 l/ `) N# R
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
' X9 a3 J0 k- q7 J" ]
7 D( d/ B. J! R; F1 E% V+ M
6 F. G1 K8 M% ~3 ?8 [ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
, Q, z2 h% [) [+ v/ d% U; `& x2 k1 }
* S4 y; [/ `( e0 W! pModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
8 d9 c5 t9 W1 w, W$ _3 r! ]% q# U2 g2 u& {- e3 T# i
. V8 w( s% s! m* \8 [3 c% W
官网: https://www.modsecurity.org/3 P9 Q, t4 l9 G |. R5 T; ]- l
6 o) F: f8 E/ x4 P4 W [/ k4 P6 k; S F4 a |
什么是OWASP CRS: |$ x9 {' X. \5 `4 P
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。+ X$ l$ i3 g' k8 i
9 x6 O$ U. p: `
* }, u9 P4 Y- p2 M' h
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。# k3 j, g& G6 L
6 [$ e$ ~: L! ]& T8 |+ z$ @
0 l* s6 Q' f" y, v) f Y" l
HTTP Protection(HTTP防御), F( K- ]" M/ j" V7 U
HTTP协议和本地定义使用的detectsviolations策略。
& K, p& f: t7 Z1 f4 s5 R$ A4 _( F/ |& W
, E5 V( G. H2 S* G: ?7 f( p1 S$ mReal-time Blacklist Lookups(实时黑名单查询)8 T0 ]! q: q% K2 ^( b
利用第三方IP名单。) t3 J5 y' b/ d% M* u. B
8 ]" U6 Q6 F. u5 A; U- y9 U
( V, q8 c% @2 fHTTP Denial of Service Protections(HTTP的拒绝服务保护)7 j' p: J% y+ T
防御HTTP的洪水攻击和HTTP Dos攻击。
* U: O' r) o2 p% b7 P4 `
g, H% D# A$ z3 ^4 k; q
- m4 P/ n1 Z; a% m- R- z5 a+ T. ^- s" |Common Web Attacks Protection(常见的Web攻击防护)' _0 j0 Z( F# j. ^: H% s
检测常见的Web应用程序的安全攻击。
2 |3 T, b+ f' }& B3 P
9 q" }) G6 T a& S7 h- K. q+ @! Z9 E( t# b4 C" p2 g1 n T9 _
Automation Detection(自动化检测)
2 Z: F8 o# N U# D检测机器人,爬虫,扫描仪和其他表面恶意活动。4 T+ h& R/ ]& {4 J7 p! e4 z q9 R
) c; n- y* z+ L% c& f! E, S# a
& c! }, j$ e# R$ D: @Integration with AV Scanning for File Uploads(文件上传防病毒扫描); z0 C! K' Y$ m
检测通过Web应用程序上传的恶意文件。2 _" n( P' c8 f5 z
) J& S; t @" |1 I5 k4 \. B: b% s! n" j1 N0 |, A" f+ X
Tracking Sensitive Data(跟踪敏感数据)1 L. {! |1 d5 D: [* c( C
信用卡通道的使用,并阻止泄漏。
" T7 Y: B8 D9 y: Z) M* ~; A; H
% Y: a7 ]' h. {7 q0 M$ N: `4 }* W0 _& P" m# i% T+ i3 P
Trojan Protection(木马防护)' q& N# O, b( b+ F
检测访问木马。" ?* u& u. v m' L
5 I6 O" e6 I* t$ d# H- Z6 P2 B$ |- n9 E6 f. g9 U8 r( _- t8 i
Identification of Application Defects(应用程序缺陷的鉴定)
% r1 @+ }, ?7 A0 A6 G! D检测应用程序的错误配置警报。
0 K) R4 I9 P# X# o4 E
7 L4 x0 v3 A2 o2 q8 \' b/ S* {9 O# O" D7 |( I$ V# Q
Error Detection and Hiding(错误检测和隐藏)
) B4 A" s ?# I检测伪装服务器发送错误消息。4 h4 w3 E* w) Q8 t9 u r5 W
- G% z) a5 A5 H% k, q& I( Z/ S% |6 o! E9 c
安装ModSecurity& p8 M) Y9 V+ w4 i% L
软件基础环境准备: J7 c5 s& h8 V+ x u8 P, z$ d
下载对应软件包1 T2 }: {& c1 @5 I
$ cd /root
9 {1 r# W" ^- l+ e; J$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'% j: A: y* F: U
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
/ {7 M; a3 P- J5 n) } ` s安装Nginx和ModSecurity依赖包4 \5 `# g8 g1 u- D2 A& Z: O$ Y
Centos/RHEL
# }. i2 j$ [4 g8 u. @( N4 _# q/ g7 h9 F9 s6 [
" ~9 w0 \2 F& m+ c
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
" L8 X. g1 `- ^) h4 B* v6 t2 u1 v$ wUbuntu/Debian3 K9 }' R9 X* n ]
4 O+ ^% ]0 s5 @2 Y) ^5 E
1 J9 p0 ~; k7 h, ^) c$ 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; v/ v. ^* v+ C& }
编译安装ModSecurity
8 l( j2 {. ^ A2 C% g: GNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
, y! [- P$ b6 Z3 N8 U8 d
, s+ p5 v: ?: F+ v) | Z, V9 }% \
1 D( c) J* k5 {, ?方法一:编译为Nginx静态模块0 ^! N( k- s5 o: w0 o# B* k: F$ k
5 ^% p W( S) J4 C" j) M
; I7 I9 X7 t7 U3 k; Z% `9 z编译为独立模块(modsecurity-2.9.1)
) e" I! z& @8 ]$ tar xzvf modsecurity-2.9.1.tar.gz
* A; X' U' q9 ~8 H# o5 }( I" T: A0 d$ cd modsecurity-2.9.1/
4 _- p5 K- m) O( y; U c6 q$ ./autogen.sh
$ K. R" M# n; g3 w$ ./configure --enable-standalone-module --disable-mlogc0 j, C' e, {9 `1 q2 x6 x
$ make
. M/ t/ f7 i# N; L* C8 A编译安装Nginx并添加ModSecurity模块6 O. x$ C: K8 W( d. F
$ tar xzvf nginx-1.9.2.tar.gz0 h' j' {% `9 x
$ cd nginx-1.9.2
* H" {7 {. ?* n$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
$ K: v9 \; j9 N: f$ make && make install/ M' v7 }$ m2 J" ?1 t- ?5 ?
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块7 C: K1 V/ @: J9 M8 \! }$ g
4 L2 b* o% m* J0 ^& @5 d1 S$ A1 \2 G
编译LibModSecurity(modsecurity-3.0)
. K7 V9 }+ ?! Z2 V$ cd /root
/ m2 ~5 t* C* v% Y$ git clone https://github.com/SpiderLabs/ModSecurity
2 x1 H2 b% x6 Z! [$ cd ModSecurity( e# }- P( ^; {0 ~7 {
$ git checkout -b v3/master origin/v3/master
' f$ i* J; J. R- U4 P# o) u1 K$ sh build.sh* N2 r+ @- Z) b9 E
$ git submodule init- J2 i5 d$ U1 z' O" z1 f- ]
$ git submodule update J' j2 p3 W: m4 p+ k
$ ./configure" P* p3 h9 Y) {) C. \2 |6 t2 o: n
$ make- U5 x( t# y5 f, J; V, l# I7 ~
$ make install, v4 j. Q8 ?# l4 C/ k9 r
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
6 m6 g& t; b5 w" Q' q( ?3 \
4 R7 ~4 o3 Z1 {# G/ t+ Q. B7 V+ o. y& X
$ ls /usr/local/modsecurity/lib8 w; R! o9 w0 I( j0 O
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.04 Q; h* z! a. \+ w
编译安装Nginx并添加ModSecurity-Nginx Connector模块0 z( p$ ?7 l, d! m8 _6 L$ i
使用ModSecurity-Nginx模块来连接LibModSecurity
2 K: U: u& c t+ \/ e9 d( L5 \# L n
5 K& x; D+ N4 ^/ X5 s
$ cd /root3 S; h4 L! v0 k3 H
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
* E& a* O& R. q$ l4 }$ tar xzvf nginx-1.9.2.tar.gz
3 V7 N. x: L4 a# f7 Z x& o) W% S$ cd nginx-1.9.2
) E0 F9 U* [5 @+ k4 X. f ~$ ./configure --add-module=/root/modsecurity-nginx
, x% d% e. f, g4 c/ G7 v% @$ make7 ^1 j, F4 `. ?/ R2 p: F
$ make && make install
6 O# K& p9 ?- M4 t& ^! W/ J添加OWASP规则
* _+ l, w6 D& u0 S- b6 o8 JModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。- A- M* c! O+ r5 O4 P
- R3 s8 D3 r$ d
, F! f( \. R- I$ C下载OWASP规则并生成配置文件2 l% j( v& p7 H4 ^. n
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git$ i3 r$ V- v! _' T: [. C* c
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/% ?/ y& X+ L0 `% h
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
6 j- A8 \7 ?: @+ ]7 H5 t: [$ cp crs-setup.conf.example crs-setup.conf
( p# l/ q4 t8 `3 I2 b9 K配置OWASP规则- j4 V! ^0 c( Y' R, S4 u+ }4 j
编辑crs-setup.conf文件
9 A, x! P# b V5 b5 b8 r$ N" T! W! {8 M, w1 e7 r
$ N* L1 r: o5 V- u- ~
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
|% G/ `! }" J) ^! L$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
1 S2 `' b7 J! a$ _ f$ e4 O# C$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf' a8 k( X1 G; Y4 C [/ G$ }
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf" d& d5 V- N! K' f: x
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。) z, ]. ]/ k3 ]% u# ^+ I; J5 d3 V
/ U% j/ r& V& u* N& e1 o( n/ L6 h/ d0 H! z
启用ModSecurity模块和CRS规则
5 b0 Y* K1 }' S# w复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。. L. d; a* m* O$ F- [4 i
! n% p# Z; t- h( c( q4 }) n
& R" F# l7 d; s( dmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。6 e o0 l( n6 \- a- b
2 ?! N( E, z# ^1 ^* o
; T. j( P+ s. D8 s }, d$ cd /root/modsecurity-2.9.1/
. D" d+ i0 T7 w1 {1 I! M& h$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf / o! H) |! g" h5 N) V
$ cp unicode.mapping /usr/local/nginx/conf/
. ]$ ?$ X; w4 R; {2 r% n将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。0 w }" l% \# N( @$ f
, | u' H: z1 w7 V0 p# @
" C0 {2 g( c+ N2 b; G; O$ vim /usr/local/nginx/conf/modsecurity.conf b5 C# S) e; N: b! E6 b& r
SecRuleEngine On: {9 V0 m# j% q1 A6 u& ^) W& T5 p
ModSecurity中几个常用配置说明:$ x! u' D: }3 F7 |( R
! h2 D9 c* |8 K& X+ D( G2 Q' T
# d1 B2 m1 a$ E! }. |1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。1 q3 T6 p$ I6 |( m D
' p% N% T) \: q
+ Q; G6 Z, z/ J$ ~: d9 [4 s2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。" G- H% o% R3 M- `
0 A$ B& r3 y" N
3 \6 ~- f+ c. ?+ o
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
8 O- C7 q) m9 H# E+ h; Z
; |( O" t. v) ~
& F8 K, w, M6 ^% x4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。* F8 Q* A1 G$ b! x
, Q3 x4 y `. E( ~ k
* D, o- J. g9 i) @0 \) W
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。, v; I$ ]: x8 s' `
' [8 o9 X7 Q5 y; G, ]7 t- p2 ~+ c, g. L# Q& ?2 K/ S: A1 Y
3.x版本CRS M5 i0 p) g) W
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs* ?" u6 Z- g$ V0 E9 \
# 生成例外排除请求的配置文件9 t' u* j3 m! _+ V7 P$ Z/ e5 }
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
! R5 T, J; i6 |4 B" }' l- K$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf0 A3 u* E0 {+ Q( y
$ cp rules/*.data /usr/local/nginx/conf
/ Z: m; I. ]& s4 Z为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。; r. I' a" \0 D7 D. h
) q7 p: m c) P1 c& c6 K
5 a2 Q6 `6 V8 h9 u. Y! k- k/ D
$ vim /usr/local/nginx/conf/modsec_includes.conf$ R# F& U* ]# N/ i9 Y& f' w- D
0 [( q3 v: M* [9 G9 `
[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 9 B% y+ b" V2 y
9 i4 G# F. I0 E- F; p: A6 l3 Y注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。6 L; }0 \. k% d2 [; ?( d9 ~. X1 M
. H5 \: @% }7 r" U
" h- r" A+ n$ A% H+ ^/ R
配置Nginx支持Modsecurity
! ?% E; N6 L' R启用Modsecurity$ |% h6 j: C" @
使用静态模块加载的配置方法. Y7 P; T% s1 S _1 K
在需要启用Modsecurity的主机的location下面加入下面两行即可:# r/ o# H% L( x* k4 v& c
1 \. J0 j o* e
% u+ v! |' u* Z8 @% oModSecurityEnabled on;8 Y# x9 _- x) y5 ^. P
ModSecurityConfig modsec_includes.conf;
0 M( X8 i; {; W/ ^0 Q修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。" I& i- _# j1 [. [4 Q; I" d
! P( V9 _2 o5 a' k l& O
: h! ^5 U9 T% B5 @# `/ L0 ^2 {
$ vim /usr/local/nginx/conf/nginx.conf
/ }" W( ^* p+ h0 A
" G& e" a: g8 ~. a+ {6 Z/ ^% u7 N' h7 h2 p" X1 w$ e- F6 R M
server {
( r# q+ d! T+ X8 }8 W1 F( ^ listen 80;) N4 L, F8 g. r+ ], _0 [
server_name example.com;2 M2 C2 D/ \3 Z# ~" Q. W; s
* d1 @) J4 `+ b% |) l
; J2 y: [2 R$ I: p location / {
0 g% `# `' V' X2 |, R! Y ModSecurityEnabled on;% f' n, W; Y+ v$ O
ModSecurityConfig modsec_includes.conf;
: y4 u0 y) H7 |7 v# ] root html;" H2 S0 L. q3 t% ]6 Y- }$ W" S
index index.html index.htm;
* c- d! l Z5 H }
1 {2 B, l1 i4 ~( z4 e& n0 s- r}
1 H. A8 w" I: K4 a* c使用动态模块加载的配置方法
" A+ ]- M1 A& p1 K/ p! j6 z在需要启用Modsecurity的主机的location下面加入下面两行即可:4 F% R0 D; S& v/ X, ^1 a6 t# {5 ^
9 Z0 V5 _: \: t4 |8 h
. t: I( u- Z; j" y6 ^0 Fmodsecurity on;
6 Z2 {8 T2 g; v9 t. R/ `modsecurity_rules_file modsec_includes.conf;
$ K5 u5 m# ? c4 m' i) k$ R修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
* U C4 w! l' L$ {2 U* f3 B6 i R$ {1 p( ?- {
; i, N7 \+ f9 l' p1 m. s- @
$ vim /usr/local/nginx/conf/nginx.conf8 M5 F7 h' @+ `1 y
6 ~' A" z1 H" @* W/ d
& o/ I# F" E) L6 c: Lserver {, d0 | J R/ N/ E
listen 80;
) |( q& D. H5 @. ~! \ server_name localhost mike.hi-linux.com;9 }8 }7 O: k- g4 k
access_log /var/log/nginx/yourdomain.log;
1 x) y8 x( P5 @3 j( R; E7 t! Y' e( v! L. F( w1 w
' y7 v% R* ]( O9 i6 J6 o
location / {; S/ R. [5 w3 Z0 Z/ Y z
% Z) S0 T8 x6 P1 t' b
/ Y7 X0 e$ E- A* N
modsecurity on;" U9 Y" _) o" T' w! o% G* Z
modsecurity_rules_file modsec_includes.conf;. y" y* D9 b; L: h( ^8 J7 y1 F. }
root html;
{4 p! X# j" d index index.html index.htm;# v0 d9 s7 Z) D3 }+ I: n, k w% J: }+ m
}) s- N. e. t. C8 A) _3 j
}( I2 ]- e$ f/ U/ D1 ~
验证Nginx配置文件/ z* Y. D/ ?2 Q4 y! E1 v' x0 v$ D
$ /usr/local/nginx/sbin/nginx -t! R* H0 }1 Z ^8 Q! T
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok5 F9 l/ b4 J$ e1 p% d/ Y2 @) \
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
7 G6 ~1 i l- {2 X {1 X启动Nginx$ @8 n8 H/ Y, z4 i# ?
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
8 J' f8 P& u2 \: \3 ?/ ?" V7 ?2 }* N. S. K0 {
测试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能够战胜更多复杂/未知/混淆的攻击模式。
) X( u! }' F+ \9 h8 I" i6 T, j: l |