ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。& |1 T" \. d5 M% a* l! I7 m# w- c
, O+ w+ @& Q6 D0 _$ ~# @: G/ T
/ C5 r9 H6 V5 ]8 K
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
) r% ~7 e2 ^- a% J2 `2 V3 m. V2 M, |
0 G! k7 \4 _# n' U* D. O- M$ M0 \
) i* L: N: y# D3 [4 Y9 c! j什么是ModSecurity) {% D; X+ W7 j, }
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。3 G1 \0 h* [' F" h
2 T8 X1 G l; Y! n6 `. ~9 N, u$ R' r; u7 ^$ b/ ?% {
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。% b& b5 J8 |+ J! @
+ ]4 ]4 D* q! S9 p1 Q# ]% M
) w! N( l& }7 n# f% y; BModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
2 I% V; ?1 L7 e- j& n) G V' }8 J6 {. S
; c/ s5 X1 {# y, h9 x3 a9 Z官网: https://www.modsecurity.org/$ Z1 M& v; m8 J; N3 D9 ~( B
% n$ |/ L, a% \( ]4 a( C# u
0 w# P4 @. @/ {- l) I( k- i什么是OWASP CRS
; t+ Y" B; |5 |# hOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
& y7 }; u% E* K; Y: ~9 z6 f
4 U1 j2 p. f/ n) C. {
8 {# N" Y: A J0 t8 VModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。8 Y8 k0 C$ a' Y% e! t0 x4 r
; n# F1 `0 _. Q; j, k+ \+ v4 \$ D! d/ O% T
HTTP Protection(HTTP防御)
6 f$ q3 }; E; ?1 f" E2 g' \HTTP协议和本地定义使用的detectsviolations策略。
" ] ~/ x' B2 b% t" ], e# E! R& z. R- e0 {* ]0 E
1 E: ?0 e y- b0 m2 @/ l6 [Real-time Blacklist Lookups(实时黑名单查询)
1 d: S$ a; _2 v- W! C: N) \利用第三方IP名单。
; k& r0 T D6 I4 e( H
6 n) t& v# k* u: I8 K. r5 E
B, s P( A: B/ ?/ L8 JHTTP Denial of Service Protections(HTTP的拒绝服务保护)
1 L2 p* N$ Z9 a) r: i$ n$ A2 y! E3 v- m; m防御HTTP的洪水攻击和HTTP Dos攻击。
8 `" G- Z* K2 O* W' z
* Z. X6 H% D& @! S& r6 g6 N! N( X1 ]$ E* q5 O- V. h
Common Web Attacks Protection(常见的Web攻击防护)- @) Q! ]% Y k% ?0 U) O( `
检测常见的Web应用程序的安全攻击。
- E p, ~- P& |+ b; l- W
; J* L6 d/ r% H. E- R8 s2 f* A9 t
8 t4 @+ u, X9 BAutomation Detection(自动化检测)7 N3 ~+ h, X# ?
检测机器人,爬虫,扫描仪和其他表面恶意活动。
# I4 t& e: ~8 @$ m9 k4 T4 z8 u3 G2 e F6 ~6 h
6 l# k+ x. A4 _5 WIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
1 l& @. T/ ~- t y: ]% T; n检测通过Web应用程序上传的恶意文件。
7 W8 l4 Z) M3 h3 Q! Z# D2 _: O4 l, s
0 G! M+ S2 l8 S+ Q; j5 O6 QTracking Sensitive Data(跟踪敏感数据)
! j/ Q8 L0 R& A& l* h信用卡通道的使用,并阻止泄漏。8 o- |1 G* G% G8 H% i
6 N& s9 {8 I- K& A3 A# J$ ?/ {: t. ?6 E1 C
Trojan Protection(木马防护)# c5 |% L; L6 w8 g, [/ ] D' _
检测访问木马。2 o& e4 f4 O/ F+ O! Z
+ h2 T4 I( m f9 v
. X# S% I' g& X$ e; B1 cIdentification of Application Defects(应用程序缺陷的鉴定): @) o. d. n$ z* G( v/ d* |
检测应用程序的错误配置警报。
" u7 [, K7 J5 r+ D# s2 w; K% r9 i2 Y! P8 u% B6 r- j( B# {5 x
! q+ t3 E4 m9 g
Error Detection and Hiding(错误检测和隐藏)
! M0 P! b6 j6 i; P% `检测伪装服务器发送错误消息。
d/ e) K3 ?$ D# `# D% a& W
; n3 Z0 E; @ z- N7 i& `" q2 L* H
安装ModSecurity3 {% S$ ^( l5 d0 v. F
软件基础环境准备% j* k5 T. Q7 M* w4 ^0 c
下载对应软件包
1 V$ D; \% g; T$ cd /root
/ w! S6 C- i. C$ x3 w8 b2 Y$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'/ O- I, n" y _" S* Y! u$ m+ x
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
9 B) P9 @9 M, A1 d: g# ?安装Nginx和ModSecurity依赖包
( d) L1 I" ^2 H3 G4 ZCentos/RHEL
- e; J" q; X7 ^- ?, v: A* b: B, u% H0 e
D9 w, E' ]/ A4 O! A
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel% F' O4 G' y, y
Ubuntu/Debian$ J3 L& l: W8 c0 G! \9 x$ Q
4 I+ Q' W+ t6 b, Y+ r- S# d7 t+ ]
$ _4 K# e& n8 `: o$ 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
: h- {1 l/ |% H编译安装ModSecurity
! E$ m/ D$ V1 }! w$ SNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。4 a# @. t8 e& g. Q' Z" Q# K; b
5 Z: [* V, Q0 ?- U
8 L- u0 T8 k7 a6 F( x% ]
方法一:编译为Nginx静态模块3 a# u. E1 R+ R% X6 D J
% `1 ^5 }+ O+ y" V! C0 I! t3 ~0 o2 \% X- [6 m9 W0 Z
编译为独立模块(modsecurity-2.9.1)) `4 m5 @# o. l
$ tar xzvf modsecurity-2.9.1.tar.gz" q# V. u+ _/ S
$ cd modsecurity-2.9.1/
! n5 j4 N% M1 }$ ./autogen.sh
5 H, s1 `9 Q0 M9 [, U9 H+ y$ ./configure --enable-standalone-module --disable-mlogc/ p4 O1 I8 v+ W( S! b, a
$ make
9 g+ z. q: W6 S' y) d: a D* q编译安装Nginx并添加ModSecurity模块
/ ^* A7 t$ Q7 `% [0 h! Z$ S$ tar xzvf nginx-1.9.2.tar.gz
M! r) d- ^! n0 @2 `6 R$ cd nginx-1.9.26 m# _4 |" L/ v* x1 f
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/5 {5 a* I6 ]; X: S& U# M
$ make && make install7 w9 K' U: r9 v$ R( B" K1 B
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块2 J! s3 f$ q. f# Z0 l u& ]
: |, P0 _3 q9 A @( `
0 V* i. w6 n! H2 I3 H) {2 x编译LibModSecurity(modsecurity-3.0)2 E5 r2 S3 |7 V7 t( U5 O# \- g
$ cd /root
% g& i2 y) q; k' j$ git clone https://github.com/SpiderLabs/ModSecurity
8 t) T, ~' K; b$ cd ModSecurity
1 b% W/ p$ J9 l Z; s/ }$ git checkout -b v3/master origin/v3/master
/ L! ]9 g% D; g5 t6 W E$ sh build.sh
( o9 C! ]7 K* `5 G$ git submodule init0 J6 w. v+ j1 `9 [( P
$ git submodule update6 L& z* d5 i4 ?
$ ./configure
7 C, u! G* E2 o; G$ make5 d# ^$ B$ {$ j/ G5 B8 p
$ make install
) U; M2 I! A, n% G' j; KLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
& B6 s6 K, J; M& Y, e7 m- d
7 a) [' d" ]4 o ]3 f. ?4 q+ F5 Q8 i+ ~$ w
$ ls /usr/local/modsecurity/lib$ f9 b5 Z" G" w
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
h; @2 M8 E+ F. j4 Z1 Y. K编译安装Nginx并添加ModSecurity-Nginx Connector模块
' C/ \' B" [/ p7 I" V3 R- B使用ModSecurity-Nginx模块来连接LibModSecurity
8 `" i4 k. p! M" w* t: P- m4 z$ w+ Y V( O& c9 q
% J' }, i. b5 | N8 O$ k1 b$ cd /root. A5 X' h$ f2 ]# C2 Z4 L7 T+ `
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx6 U; a2 T" A0 g! | n' b
$ tar xzvf nginx-1.9.2.tar.gz
' y. H# r% Y8 P f+ u$ cd nginx-1.9.2
# P6 H4 |3 W- j; P# J. J: d$ ./configure --add-module=/root/modsecurity-nginx
2 t- `! f X: ^( L$ m( F$ make* c# {0 { \3 G- ]5 m9 R. L
$ make && make install( ~/ M" h+ J d8 Q4 D% w6 t+ B
添加OWASP规则
2 O0 F& ?& w8 C% z2 yModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。6 M1 d" t2 {. e* F
, j& ?0 N2 `! N' G$ `. R$ u( E0 \$ w- c9 C) P
下载OWASP规则并生成配置文件4 e* h: J6 f" H5 e5 Y- U
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git: a6 n3 Z/ i8 K8 f& B" V, K2 }) h ~5 r
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
8 X& f+ E" ^" Q1 Y8 m6 J& A$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
3 D6 _: b7 V$ y; H p( _- {$ cp crs-setup.conf.example crs-setup.conf
0 E8 E! N: p- a- v/ h配置OWASP规则
1 }/ V2 P/ g3 Z! C) S5 X1 c7 |3 ^编辑crs-setup.conf文件: `) m3 @0 w/ d# Q$ Q
% t! K& a9 F6 d1 z
2 f8 O+ ^5 B4 o# G$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
( W8 w X5 M/ |* B. E0 I$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf4 L9 s3 ~( c3 B
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf' i5 |/ Y, i2 G; M0 a! N) k- O
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf! U2 K$ J9 w' f0 c# \9 Z* k1 L& r
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。" E4 Q8 A* {$ k: \1 Z$ q9 B$ X7 a2 y
( m9 C7 a9 @8 t: z5 {9 k$ D
" ]/ O4 p9 y6 ~" i8 i& r* [. l$ M
启用ModSecurity模块和CRS规则) r W$ l0 N9 x* t# U4 g. C8 [
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
5 s& }& `! M4 |3 y6 c7 G& I$ a. l8 c, x& u0 F
) R* f I$ G( W; m7 ?modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。7 B5 ]/ j) d% `4 A( X
" ^7 v* l" h* m
& x$ @0 j1 I$ o7 `6 L8 F$ cd /root/modsecurity-2.9.1/! M1 @! K& u* s3 j. A3 [
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf 8 U' i- ?( ?4 [. T
$ cp unicode.mapping /usr/local/nginx/conf/
$ t6 _) C" o7 N: t4 n将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。0 O: R- |% q; x+ A
: E6 L+ F& |% {# z
3 z8 c# V1 d7 Y6 L, M" s& _$ vim /usr/local/nginx/conf/modsecurity.conf
* n. m! {) R$ w. d7 kSecRuleEngine On4 P. q0 n! Y( V( U
ModSecurity中几个常用配置说明:& @ \9 f' l" b# j) h
- L9 Q. d/ e$ H; G" I% ^0 U* e6 N0 v8 o8 X3 P; ]( W
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
- Y( X8 V9 X* Y+ [: E
9 _: [3 b9 |, L& I5 w! K7 u
/ a" O0 C# y" o2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。. M/ Z' {; g5 z! |3 D' b
# J! P8 `, u. I) C9 |0 N0 Z
% k& X! Z1 A' d! G( Q( r1 n) m' c3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
. G8 H1 i0 v2 T% u& A' Z V+ j* Y/ Z& U8 z
4 {, X; X+ f, k9 G- w* Y4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
" V1 l9 D5 _6 J D4 n' q% ]
/ C( _8 t5 z+ R; J" H9 r# X7 M4 v7 d1 I0 H9 N
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。7 I0 ^ m5 T5 K) h
9 U$ t4 V9 Z5 l" F \. U' H' Z0 N' i+ W, h1 J
3.x版本CRS
" V8 P1 j3 b/ j. p, w$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
+ W- i: v! A( U# 生成例外排除请求的配置文件
% N- K- l; `9 j$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf! m5 c. G3 X% |; B2 C
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
; y& S' l h+ M/ c8 f: x$ cp rules/*.data /usr/local/nginx/conf6 l, |# {+ }$ J
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
! C c! j& q; i. p6 ~1 ~' I3 t; m
! h5 v# O4 {( E9 A+ W2 T1 ~2 A$ vim /usr/local/nginx/conf/modsec_includes.conf
D. H3 U6 u/ d6 j8 O
4 s/ k# K* g0 {6 P8 p[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 R* h3 l* P% D, J( V
* {- j0 ]# I. a0 G% ^ ~+ ?/ N注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
4 w; C! x6 [. [; s- U% S/ _& t/ g% C5 ]0 @( ^, i
9 J1 y3 |2 ]; ~# g' p配置Nginx支持Modsecurity% L3 l# T9 D. x; Q
启用Modsecurity
+ N( D% Q, L# J' W8 R! e4 a使用静态模块加载的配置方法) K- `& o6 Y6 M+ M
在需要启用Modsecurity的主机的location下面加入下面两行即可:
9 V; w! e# v1 o8 P8 o3 Q
$ y5 g. v1 |9 y' m3 u
4 J! }& [( j& |- c" t3 p8 bModSecurityEnabled on;
5 Z X1 c T x' GModSecurityConfig modsec_includes.conf;
8 D( k; u1 E2 n% I' V5 Y, ]7 @: ~3 o修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。+ |) G: {+ V2 }) ]! F
5 n1 u, {3 f6 a3 `
& u3 l* s3 u4 o
$ vim /usr/local/nginx/conf/nginx.conf
$ G/ \% d) v7 \, g8 @( z
4 A+ h' ]/ ?& Q+ ^7 p4 C/ P u6 S4 g3 p4 y8 E8 }
server {1 u a$ D) u0 `5 X$ o
listen 80;, a. j ~+ J- w o6 P
server_name example.com;
+ O* X1 t; Q8 S9 W6 T7 [
+ N# ], u, K3 z) ~) F3 n0 s1 \
`5 u0 [4 k, p1 a0 s* q9 Z location / {
( D6 R: k. x8 Z2 ?% d ModSecurityEnabled on;* I# `" y7 \' a0 u
ModSecurityConfig modsec_includes.conf;# \; m; ^ `& D) `1 `* |* `8 J5 u5 Q; x
root html;
! t, {" c6 D; Z9 u# t7 ^ index index.html index.htm;- Y& F& G6 O) z u. A6 d/ o
}1 j; [# J- p. o
}
# _1 M2 D' E0 E4 {3 v( _* S& v使用动态模块加载的配置方法
& V4 b9 J. Z; \ b% Z在需要启用Modsecurity的主机的location下面加入下面两行即可:- { {( c n. [, k
7 q+ w2 E! o7 ~7 \9 }3 d$ P* w0 ?8 @8 @4 F; I$ _
modsecurity on;
5 K$ r% g' L, X" E1 W Tmodsecurity_rules_file modsec_includes.conf;( M, Q/ D' U: H8 x1 p! P; {" Y; W
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。3 N) M8 _' C% y+ M, m
& C; O0 w9 ~/ V4 G- v* e3 D: Q) O* p. o& I; {% Q
$ vim /usr/local/nginx/conf/nginx.conf" f3 |1 K( P. d; Q* v
+ j" t0 G+ @- P( a. L
4 t, ~- t- I3 r) E
server {4 c: O. s8 t5 o! S! m. ~& j
listen 80;
! Q. k) w9 C/ V _% l1 O server_name localhost mike.hi-linux.com;% s+ d9 T* ?' G$ ~
access_log /var/log/nginx/yourdomain.log;8 B- ?( h5 ^4 y3 _3 S- q
$ O) T3 c# m% s7 j$ A7 G- E
: K" I( F/ E1 N6 U location / {, s& A" Z0 J1 f* ^% v& }# w
! K4 t, [. o) M8 a1 v5 T8 _+ i A% X* N- Q0 O8 M
modsecurity on;
- M/ f7 m# P- @% z: q2 G/ G0 [ modsecurity_rules_file modsec_includes.conf;
" l5 B6 m& R1 J, w" ]% N8 H root html;
& l9 N6 [6 e! l* z index index.html index.htm;
( T1 I" L) o2 a; X/ J5 k}0 V1 V! m6 w8 \
}
% ]! U4 Q. u* b0 F) f# o& @验证Nginx配置文件
5 Y2 W V5 ]. c$ /usr/local/nginx/sbin/nginx -t
' c) B8 M# | `6 d+ M) z. H- s4 ^nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok4 h1 P/ N7 q- t9 v d* k& w
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
y& ]+ ?: X; A- q: Q启动Nginx
0 [1 n5 L1 p' r, O, u! E- L$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
5 Q' u, V0 o1 v8 e) _7 q
0 D7 |0 Q8 _) @7 c% U测试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能够战胜更多复杂/未知/混淆的攻击模式。
' c+ T3 n" t: s |