ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
1 V" L% N2 I# Q; C
9 r; ?* y% E: z/ D
# v8 R; y6 c0 i4 k在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。, R. @1 y v0 d. l+ P7 k9 \2 e
5 |" c$ b& u* A: @ u5 y+ \
7 r2 B5 ]" S. ]; ?! u( z: M8 T
什么是ModSecurity
G0 {+ e: z" {$ `8 {( F) `# q% }( |' }ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。3 k$ Q6 Y4 r- u# v h& i& M
, M4 D4 z2 r. Z7 ^" D
/ Q% C8 y5 x; M% _- r8 C( n3 D4 n
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
6 d. j0 Z* k2 Y0 D" m- ]
# w3 \5 f4 X1 M& D
: `( [7 m0 x& R3 y4 x! l1 QModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
# J7 W9 t, Y; u F
. i* Y4 ]" Y& ^& [& W3 H8 Y# t; x8 E$ k X4 k
官网: https://www.modsecurity.org/
+ l6 F ?3 N; s& O: ^' x
+ L- ~: z/ H F+ z" ~" q; f6 B, l2 v; Z" h- U% @
什么是OWASP CRS9 C7 ?7 Q$ x2 Q" \
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
' s7 j) q9 S; P6 j7 P P! |
& G y C% L; _# D. G
; H b+ W7 F) w5 FModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。$ e6 [% C- x0 C$ K1 X+ q7 [
5 m$ F; |% [6 i# t! O& S" e9 z- ^7 [* o, n+ L
HTTP Protection(HTTP防御)
; P- r% W' ?% }9 e; {, W: U) u+ fHTTP协议和本地定义使用的detectsviolations策略。
) |/ E0 u ]$ ]9 k$ ?! z7 i
( E. M: i" @9 D9 L8 K' V% _% ?
1 W4 f8 n! U G$ q- H% i1 a$ tReal-time Blacklist Lookups(实时黑名单查询)# J5 X; w/ {& q/ h- A0 E
利用第三方IP名单。. j: ]/ B* T7 `. S9 a: t' F, V6 b
- b5 ]% j% ]* a0 X; R
! ?& H) v5 E) F9 N, Y
HTTP Denial of Service Protections(HTTP的拒绝服务保护), _# Q* ?# O* n% u2 S" e
防御HTTP的洪水攻击和HTTP Dos攻击。
" e" ~: q7 m; I2 l# H0 f+ Q+ E, e/ |& |6 g
* k2 ~6 r& ?- q2 j- S/ z
Common Web Attacks Protection(常见的Web攻击防护)$ G9 g4 ~3 \& n5 T
检测常见的Web应用程序的安全攻击。9 [8 r% O! N$ y* i$ W" j
, B0 @) ~4 p& ]4 C( C6 G8 {4 f
8 m# n; u; a& |( P SAutomation Detection(自动化检测) m, l4 }# s }* C# S# u1 {
检测机器人,爬虫,扫描仪和其他表面恶意活动。
' k# P+ |8 H( E+ F+ r
% t% P; f& L2 s- `5 m0 ^; v8 ?& i, P7 Q
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)7 @: C" f4 A4 L% i6 w# ]
检测通过Web应用程序上传的恶意文件。 O) l( @9 D# T. `2 j
1 Q/ X% b r% W* b/ f
! y% P2 m0 U) ?1 k# V+ d7 h
Tracking Sensitive Data(跟踪敏感数据)
# S& E# t/ }! B4 V) j信用卡通道的使用,并阻止泄漏。' K' L2 ~, p5 f! \* u
0 ~) B! l5 j" [: P) {, l4 z Z- a' [- v( P* c' R- s" C: f5 U( r# S7 r
Trojan Protection(木马防护)
/ {. E! r3 {$ b$ ?8 w# j, L- n检测访问木马。# |* w' y, R0 }2 \
$ n3 C9 R+ |4 t; w
1 ?2 u* s1 l8 u" O: b0 G! jIdentification of Application Defects(应用程序缺陷的鉴定)4 ]+ b& S# F; s9 z4 r
检测应用程序的错误配置警报。
7 C6 u u. P3 T: j8 t# ^7 r4 l {. l" w
0 l/ K" C# r1 d, A% rError Detection and Hiding(错误检测和隐藏); l! l! ? v# B( d+ h) W8 i4 ~* ^
检测伪装服务器发送错误消息。5 q5 P4 ?# D* ~ r: n
! d2 w: W a6 v
2 B0 u! ]7 H. J0 a" d安装ModSecurity
/ ?( q( k* [# i) ~# Q, z. T- c软件基础环境准备
* k( e! Z. R& J. h2 K$ k% D& {下载对应软件包
5 j8 ?4 U, F. c# u* g9 a3 r8 G$ cd /root0 n8 l; U% m( v Q2 |5 B
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
/ r7 \$ a; U6 m- p1 p: |4 C8 v$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
: Z/ g, E: g, h9 x q, i+ E安装Nginx和ModSecurity依赖包
5 e5 P1 g( I+ g$ R0 u( V) ~ P. LCentos/RHEL
a3 E1 ?1 P% T* y9 _- ]# j5 V! J& j% t5 m/ E' I/ n0 }5 L0 l
2 e" W1 h: H: O7 ~. r$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel% i' q3 p+ ~4 ^$ B& K2 b
Ubuntu/Debian0 z d6 q! x" H* s# L* e ^/ @
- ]7 J+ ^ l( E+ L5 y8 A; U' o
2 Q& E6 Z1 C H/ E$ v$ 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/ h5 _$ a" a. E5 L2 z
编译安装ModSecurity
7 c0 A( g; N2 ~& j7 |0 [/ O! [Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。+ z |3 l! u2 u
; O2 g: a! I. d0 E0 w
9 ~; {7 c( V U+ S" B2 Q
方法一:编译为Nginx静态模块. V) P% f) {. W/ f" F
f7 P& c' _% f5 r
4 Y( _. X. S8 V n! a/ {' }编译为独立模块(modsecurity-2.9.1)
5 D5 ~( N6 p3 u4 h$ tar xzvf modsecurity-2.9.1.tar.gz
# D* i( v* C6 d0 o( [" N) n$ cd modsecurity-2.9.1// ? r3 ^' {9 o; Y
$ ./autogen.sh ^+ e; F* U" v3 B8 o
$ ./configure --enable-standalone-module --disable-mlogc
& u& n m0 c% [* M. V- q$ W2 b. a$ make* }0 {! _( y! C
编译安装Nginx并添加ModSecurity模块, h3 k- \5 M: y" R9 n" D: `& _9 L
$ tar xzvf nginx-1.9.2.tar.gz
8 p1 ]$ s; g- Q% A$ cd nginx-1.9.2
1 |! J0 T4 f0 G- y; `$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
; r/ t7 X/ c% F3 L" c, L$ make && make install) l4 w' R% E) c. ~
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块7 Y# s) m3 i; A5 h1 i' r; w
2 V: V1 W" n [+ c
; _) S+ L/ N4 M$ Z3 j- [: Q# r编译LibModSecurity(modsecurity-3.0)- y" x" _3 g1 x! {8 O6 I8 p" f$ q2 D
$ cd /root
: z* e& q: ` T4 e6 s8 R7 L; n2 V$ git clone https://github.com/SpiderLabs/ModSecurity1 C, |$ H; F- e4 ^7 L/ ^
$ cd ModSecurity, }! u3 j1 I3 v" N' g6 p! q
$ git checkout -b v3/master origin/v3/master( N i5 B# }8 v+ W0 _3 e2 Z& |, I
$ sh build.sh
( x& }' R" z+ H& @) _# X& S$ git submodule init
- \6 b0 z) x; Q. p9 N$ git submodule update
" K0 a1 r9 E0 l5 z$ A1 J- l$ ./configure+ a8 v9 P0 i* c
$ make
6 l2 \ C5 y* D9 o, |; G- i o$ make install$ K. J b4 ?9 a. z3 @ o
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。+ s2 n9 G2 `; A+ v( B& E! ~# u$ h3 B
?% |! N# b4 u7 m3 C, k# |
: x' J, n8 R }2 N6 W2 N
$ ls /usr/local/modsecurity/lib; G7 R" k6 Q; ^1 V' x
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
0 B3 k$ |. H+ C8 m编译安装Nginx并添加ModSecurity-Nginx Connector模块
4 U( [5 C4 M7 q. U6 ~使用ModSecurity-Nginx模块来连接LibModSecurity$ `; q m8 K) w* T0 k- R0 G
" v7 N; W9 _5 C+ Q; I
5 F4 v5 H5 z9 M/ L% v$ cd /root
$ e' T& e2 Z4 V) Q6 r6 l$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx9 ?& U5 S; x6 b, G
$ tar xzvf nginx-1.9.2.tar.gz
& m" E0 ^5 o, o ?* {$ cd nginx-1.9.2' s) w3 H8 t5 u2 u6 e- ]. s
$ ./configure --add-module=/root/modsecurity-nginx2 K; w. o% L- K' H9 l
$ make
+ d* u, T0 m5 I$ make && make install
9 ^8 p! U% A8 l, R% Y* h添加OWASP规则. U& g1 a' X# Z/ d$ Q
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。+ y: F2 I# ~; V0 u
- w" G$ H h i7 b7 }* _5 _: l
' X$ s. O% h% O' p/ |8 A8 |8 n下载OWASP规则并生成配置文件- M! h# H: D+ x! L2 J& ?
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git4 }* X$ U0 n; `( K+ M
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
2 `7 d& d/ s9 W% _, ]$ cd /usr/local/nginx/conf/owasp-modsecurity-crs$ {2 G- z' \7 V5 d
$ cp crs-setup.conf.example crs-setup.conf9 ^3 V. w: B2 p _ L
配置OWASP规则
! z. M7 b2 Z; |/ }编辑crs-setup.conf文件
' f0 j+ L: m" u5 V1 m }( e k- N" c, _& d
. ^) E/ d, a/ c. n6 _0 U
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf j1 p i z+ @7 `0 j& |* G: P
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
6 t( D( A4 Z0 _& c$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
7 }2 i) t6 o* ?2 d8 a, ]$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf+ {9 v# a' ]! p3 E, s! p% i7 u
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。, w# A, ~0 j9 P5 y) [
2 w$ Y4 n8 S J" n; F
) \8 X2 D! {8 T D启用ModSecurity模块和CRS规则
" b. P& ~% x1 e9 N3 o; _复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。. m- t1 _7 [0 y/ t2 u% M
1 v$ Z* z% S/ b' e3 K$ z. X: a2 d- p; i
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
4 A/ @- A4 w: P+ g4 b% Y& T# E# {6 r) d! ~8 _
7 O# D" V, ~+ H& l' K/ c9 @6 \5 e! }7 F" r
$ cd /root/modsecurity-2.9.1/" ?# H& H3 y% b* M3 u3 S
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf : V* M+ J6 {. w! q
$ cp unicode.mapping /usr/local/nginx/conf/
+ |1 O) b8 n; U- V3 {4 u将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。2 q8 s1 v- ?0 O
5 U2 U/ O4 I7 Y# W" d1 g0 k" H
. w- M7 z% B6 G$ a$ X$ vim /usr/local/nginx/conf/modsecurity.conf9 ?. }" V- r( S
SecRuleEngine On
1 f+ f! X/ k4 a8 B; b/ ]* gModSecurity中几个常用配置说明:
, I/ F1 x! R4 Z# s6 C
# e j) F1 s8 B3 |+ {4 R/ o* o8 S h. m, ~5 O
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。1 q3 q7 b/ w" h* k+ @/ \- K
. V- Z, M, V" G" n! x5 K1 z
. B/ r4 M, j5 y7 @3 P
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。3 Y/ f8 I- e0 S
# I3 y& a2 e6 `# [9 R
- H9 v/ L# J5 C+ o% M% l; X3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。1 \ Z9 \. e* u1 |2 i
- x0 m5 m9 z' T
4 ]7 h6 G5 S' z+ b3 r; p4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。 V* I; F9 g3 l+ \* P
8 ?' y' B4 Z7 j5 r( p: i/ ~
! I. P+ t/ j+ H# ^6 f0 ~# B% N2 G在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。, V7 X3 n! i/ i9 a
4 u# _0 w: G) z1 C9 N2 \+ l* ~9 R- l" u. `
3.x版本CRS
$ l- S+ ?; S- y% \8 u4 C$ t# }$ cd /usr/local/nginx/conf/owasp-modsecurity-crs$ ~2 N2 S* l5 a7 r
# 生成例外排除请求的配置文件$ V) o2 X6 G1 @" n
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
9 d2 L9 e. L3 |+ d! e; x- r" t+ U" _$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
9 L" e2 j7 ?4 R, ?+ W$ cp rules/*.data /usr/local/nginx/conf6 p6 v3 I/ D% y; J$ x, Y3 R' C) H
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。2 I& O! i+ F g
) s! ~9 L& T9 G) V2 u* B$ c
0 F6 d$ c" k& x* m( i+ u& _$ vim /usr/local/nginx/conf/modsec_includes.conf
3 A4 N5 ^/ F& Z- d" z+ y* c( z& y
' H" Q: T) S1 T0 o[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 . C2 S* H$ p# O* S
' Z0 P( g8 v' z: K, h; w/ j. s注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
# i! k0 T$ q( P% f+ F
: E3 Y& N! e! Y5 `: G( {
8 d" I+ j! E$ W配置Nginx支持Modsecurity
/ |( p& z' i7 }启用Modsecurity
+ r( u- A5 {2 C% H使用静态模块加载的配置方法
$ ?. g$ S/ U; r, Z5 M; e7 k在需要启用Modsecurity的主机的location下面加入下面两行即可:$ S' l5 Q: @6 T# |
# D1 J: o7 F( n/ {# w: u" ?1 b0 h% Y: ^" j. |9 W& F
ModSecurityEnabled on;
+ n# }9 c! p1 }2 M6 P, v# \. @ModSecurityConfig modsec_includes.conf;
4 p: A/ q2 P9 H5 _ R7 z1 ~2 O, E9 I修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。( o: u* y# q7 ^0 P1 M
& O: ^- h Y/ J8 z) E: Z1 U/ A
9 r% L, o3 e. L# W4 p& p$ vim /usr/local/nginx/conf/nginx.conf: u0 l4 v: i i3 J) Z/ }& X; }
4 B' o7 e* m3 ^, O
0 V" x" v4 R. {/ x! ^
server {) O m4 d$ ^8 {% m
listen 80;( d7 a! J* @6 r% F$ z
server_name example.com;$ b' S, T* q% l4 I% O$ D
' x2 l/ G& F) [1 H b: S4 q
. s$ M" p! @0 S9 |) o# j location / {
! V, V- C2 ]/ h: z" l ModSecurityEnabled on;' V- u5 _2 Z$ o/ u# F9 t
ModSecurityConfig modsec_includes.conf;4 H4 L& E w) |, T& g; F1 C
root html;* L! N3 E8 O1 k8 I) p
index index.html index.htm;" j2 `0 g1 M3 g. l* g9 F0 k( ~
}
- G3 e6 X! `4 [}
2 c( m0 i# s2 u7 p/ S8 @0 J8 ^使用动态模块加载的配置方法
! e. j4 N/ X7 e5 J P9 [在需要启用Modsecurity的主机的location下面加入下面两行即可:
) @9 Z- y2 Y+ W" m6 x- T; M# m& k; t8 h! z2 R3 s; I
* ~/ p2 P5 b! t6 M$ {, @6 A
modsecurity on;
! C: X8 r4 y, k7 T1 [- b$ emodsecurity_rules_file modsec_includes.conf;
7 P! C* K' i' {5 C- x7 g修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。, |" M& @# C, ~9 Y6 w
7 C7 r, Z3 F: j8 o. `0 f/ F& O0 V' p$ A/ n7 ~1 R9 p+ U
$ vim /usr/local/nginx/conf/nginx.conf
: {3 r8 b; M9 V0 b" q
( x# E7 `: O( Y, O9 x" ?. \, @) _* v. Y3 A e1 I- L, q8 T+ _
server {
1 H1 T6 C2 q) e& l listen 80;
( S- @" l/ C& n Z* x1 Q- | server_name localhost mike.hi-linux.com;3 w: T* M5 ?2 T9 p% w9 _5 I
access_log /var/log/nginx/yourdomain.log;
a8 Q( ^! [; t3 \# D. }2 ~+ N9 o/ J2 i }* |
6 \2 C" A4 R; k7 T* K; ~# [
location / {
/ q3 T1 p+ a0 x( i: w; {" p5 g/ N, A
; u4 Z- I( X; s6 e- u/ j) U. l
modsecurity on;
3 _: A7 y; t/ g! _: E7 B modsecurity_rules_file modsec_includes.conf;0 f3 ~, v: j+ V( @$ R( W; j; z
root html;+ X8 s6 x0 O* r) `; W
index index.html index.htm;! Y, \+ _ W4 d# ^
}6 d) b1 n; g( l7 Y/ q* H( b! e
}
' `/ d! l& [: t$ p验证Nginx配置文件
; f( H6 d. p1 z1 K$ /usr/local/nginx/sbin/nginx -t* {' q5 S5 _, W1 H9 I7 {- L- y/ P' X
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok) N) g; H' }- S( {
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful) y$ V t1 W5 w3 w2 ~8 l. V) v
启动Nginx, t9 U. M8 \" ~! R' `" Q
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf8 X8 U; H- t0 Y5 R- i
m& j& l2 c. u" U/ F7 f6 \ I测试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能够战胜更多复杂/未知/混淆的攻击模式。
& S% y3 X0 z- `3 @( y, S2 M |