ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。4 t1 w, p* ] Q/ f; n1 U
+ W" u x- h& P6 s& G
; Z, R' q) L6 M在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
1 y9 ?6 y, S" C1 O; `. c5 y6 Y% }9 p6 e6 p h) k0 n R
/ }3 W q( f, M" F4 o什么是ModSecurity
/ y2 V6 q% [$ AModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
4 s$ ]( a$ i" ]. x3 S% z% e
( F4 G6 _" {: A3 Z1 a9 f( p1 U9 d; ?- n' o" b" V0 v' w
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
+ Q+ c( P1 {6 [/ y+ h! @+ X+ }7 C; r# D* r# ]$ j! x" M8 c
9 N# P* S: S2 U* J6 JModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
! y: S; R! f5 a7 m N2 [; P
/ o. B; u+ p4 r9 G! G+ h" n* P8 r' m5 b3 _' m0 O! @& p
官网: https://www.modsecurity.org/ H1 f* j( ~9 }& C/ W' P" H
% }# s& {. H& T3 ^: a4 M) ?7 F a
& f O, R5 T4 Y5 r0 \1 w0 Z2 \
什么是OWASP CRS! N/ I0 _; ^6 @! f
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。! c7 W! e; l: _
6 b9 f$ H3 X$ ~( c8 R/ o' p
% P0 u4 S0 g2 E1 [! F1 iModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
- }0 O* y1 r, \; J9 }; |4 A! l, j. }' k1 k+ g$ y, g# X5 f7 w1 s
0 R" `1 s- t' b5 N% Q1 B3 Z
HTTP Protection(HTTP防御)
i" F9 L; x4 }# dHTTP协议和本地定义使用的detectsviolations策略。1 a( I2 X: h" t. j2 Z$ ^9 w4 |& ]
9 a% c3 p h5 \. M7 k: C9 d1 i
# a g9 |% R. k7 c! i" VReal-time Blacklist Lookups(实时黑名单查询)
* [ h5 ?; F# r4 P1 X利用第三方IP名单。
9 w- t w8 w4 A$ M& b1 k4 J% v/ m& M7 ^4 o6 ~5 r. s0 ?1 k; ~
: ] x5 @& P. P& bHTTP Denial of Service Protections(HTTP的拒绝服务保护)
5 K6 q& b$ g' c( X4 ]% v# h: O防御HTTP的洪水攻击和HTTP Dos攻击。
9 Z" R4 [. F5 M7 E9 B8 X4 ]
* N; ]4 U9 E1 p1 w/ J" R; \
5 s" d. e# T1 w% z n' s+ a6 H& R3 R0 YCommon Web Attacks Protection(常见的Web攻击防护)4 s- _5 g/ a: D" X5 E7 ~8 b) `# @
检测常见的Web应用程序的安全攻击。 \- {1 h) z, Z' G' U
! E* X7 X, \* B# T7 I" g9 ], j' O5 x/ S5 v! i+ u; R
Automation Detection(自动化检测)
3 W. F* H, {4 `+ Z1 n' f. l检测机器人,爬虫,扫描仪和其他表面恶意活动。
' {, f; z$ C8 x6 z
, e+ o7 F+ d/ S3 s* I( n& y
7 D1 Y$ b( f" |+ B: [5 rIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)- i( X- w7 R0 r6 N( _
检测通过Web应用程序上传的恶意文件。
8 Q! j7 s- N" U( |9 e% Y2 h( Q& |! b( m2 p! a7 L
6 e" g# B x1 X0 r" S/ d/ [
Tracking Sensitive Data(跟踪敏感数据)
; A5 Y8 G7 G: U) H/ Y: G, X信用卡通道的使用,并阻止泄漏。! ~) U2 N* U$ N' S# ~5 z: G4 Q# M
. B5 L1 I- N/ X$ G3 Z
1 `, q4 i# X* Z1 \; F2 |# `' o
Trojan Protection(木马防护)
1 W. m$ S6 B5 D) ~ v检测访问木马。
l1 [4 Z |5 W: ~( {% T
4 z$ |+ w( Q% p- y! F9 u R# L$ H/ Z( X+ b6 ^* Z' |
Identification of Application Defects(应用程序缺陷的鉴定)
+ a0 ]+ j0 L; X$ k( f检测应用程序的错误配置警报。
/ ?% M, U$ y" y9 a3 ~& S* f6 b, s7 O7 ~ o
" M" y) R# u Y9 M) a- wError Detection and Hiding(错误检测和隐藏)+ @, [! Y- [# j# x, @ m E
检测伪装服务器发送错误消息。
9 W( l* U& }# I0 ~$ c8 G4 Y
3 |$ s7 ~" p5 a& [1 N9 Q4 h: q1 x: F; l
安装ModSecurity
- o) O! P3 w! a$ Y/ ]( }8 Z( t软件基础环境准备
* d9 Z' c" \/ D# j" K下载对应软件包
' c2 ]+ f5 s$ L: R; T1 v$ cd /root
1 |" {& }* y+ ^+ q5 ^4 H+ x$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
+ ~5 X9 O8 \3 c5 ?' P: B' [$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
J* S' x; k [2 x% \安装Nginx和ModSecurity依赖包
7 }+ u9 A7 |9 ~. V# J r) o7 jCentos/RHEL
0 ]6 Q; [5 ]% m8 t- ~. K: ], ]1 w9 w9 a& H: n* O
+ Z4 e* g" F5 [, R. h( A1 Y$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
- c B$ {7 x! J9 q$ ?Ubuntu/Debian8 b4 h+ m) B" u1 ] S0 |
2 p8 N+ x2 A5 l- q, _$ a/ k, a
& q1 A; p. Z# h4 z' ]
$ 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' S* K% V# O4 F
编译安装ModSecurity [2 C, O8 o. V
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
3 T- ~& l% d7 K# _
! X% } Y* p! `
l" Z' `. X( }; w方法一:编译为Nginx静态模块
% M! Q N5 n' Y& U9 ]+ M n6 z7 h$ Y. k2 R! b' a
2 Q' \- T- L7 O" r7 b: U编译为独立模块(modsecurity-2.9.1)
4 w B/ ~2 R6 Z. M* C4 ^) v$ tar xzvf modsecurity-2.9.1.tar.gz
' k6 d% L* ^/ J) k0 E$ cd modsecurity-2.9.1/' }: \2 g; V, d2 w
$ ./autogen.sh( m8 I4 T( }7 ?& Z2 P# o
$ ./configure --enable-standalone-module --disable-mlogc# V6 V8 U6 p! o! g4 Y& `6 u$ s
$ make& j" `# H7 N- x F3 w5 c N
编译安装Nginx并添加ModSecurity模块
9 X) t' t/ x% E8 G5 V3 }, ^$ tar xzvf nginx-1.9.2.tar.gz$ l. a) r0 H% o
$ cd nginx-1.9.2+ S7 |8 m: [0 i) [
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
/ }$ @9 {% B5 C* z+ }3 L% f9 F$ make && make install: b. \# b6 R @) q1 l) }
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
! ]2 r6 Q5 ~* B G2 A, r, J' E5 N) F. v! U% K% _
4 l; f, G9 T" k2 c1 S
编译LibModSecurity(modsecurity-3.0)
! N3 h) k- d* ]/ [- \$ cd /root2 ^, s3 {) z) R2 H$ P
$ git clone https://github.com/SpiderLabs/ModSecurity& f9 j ~, Q8 J0 b4 U9 w+ X
$ cd ModSecurity# f8 ^6 ?. p3 I% c, q( O' r" I+ g( R
$ git checkout -b v3/master origin/v3/master
2 z* @' i1 t2 a& N9 M- p$ sh build.sh
& @; h9 p. |, D+ o" l' R$ git submodule init
- J+ j- M" G& X+ t- h$ git submodule update7 l6 c& T" [9 g t) s! p
$ ./configure5 x. t/ R) i$ w8 v9 W
$ make
# e- W" I3 W- J$ make install
: T4 H6 [* o Y1 F! e6 G& r) T# NLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
7 C+ Z- ]8 ?6 k/ Y5 z% d' W' ~
3 F; I: ]# H: \ _$ ls /usr/local/modsecurity/lib
, |* E* V0 F9 J2 U; q+ ]) X- w: i) alibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
4 A) w8 h0 X) Z+ m0 S- O j6 g编译安装Nginx并添加ModSecurity-Nginx Connector模块+ ^" i9 A( l0 J" S: o' O
使用ModSecurity-Nginx模块来连接LibModSecurity
6 w8 h4 f/ B3 T9 J f
H+ `& V w) w9 E% p
+ p' E: m; M1 [' b$ cd /root
$ t! O1 j, R: z8 x& }( u4 e$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx$ D+ U: s$ a+ ?5 c
$ tar xzvf nginx-1.9.2.tar.gz
8 f) y1 l( ^; y8 w+ i$ cd nginx-1.9.2
6 U- w" A$ g" m$ ./configure --add-module=/root/modsecurity-nginx
9 Z5 x. G9 ~3 A* @0 ?4 q6 R$ make/ w5 C1 v7 ?, q3 K9 c
$ make && make install
. v8 K7 U8 w H- A添加OWASP规则3 T4 n3 @$ D: Y; B0 \1 Z
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
6 }' E* x2 `% e# T Z* y) @6 K: P3 t/ ]3 z! Q8 N) Y& m! T' b
0 ^$ Y" ]/ ?: n' q! y
下载OWASP规则并生成配置文件
1 x+ I( x. [4 H! S8 e! {8 C* x$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
# A' T% n0 ]7 r: D! H$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
& a: b# L1 ?" o3 K$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
e R3 V. R1 B4 f2 G$ ]$ cp crs-setup.conf.example crs-setup.conf6 X4 A' F! r9 k3 n6 z7 e+ d0 F
配置OWASP规则
9 z E O$ R( k7 R编辑crs-setup.conf文件
; R5 X6 {# _- L5 I' [4 E/ L# F4 A# _/ N7 v0 x- _/ m
, {9 n5 [% u% r) x" @( q$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf2 d4 A7 i" B7 ?' Z
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf- x% ^: c" J' @4 U6 j# W" i
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
8 e2 ~8 t. k( N0 h# n6 {5 m6 S, O" l$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf0 D: t5 ~0 A$ ^" |7 Y3 [) M! E1 O; Q
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。$ n) H1 u3 I% y2 H8 u5 T
3 m$ d0 v g2 _; b* h' U
7 s9 k! j- e" |6 N- O) ~启用ModSecurity模块和CRS规则
9 c0 j8 k# ^0 o9 l& j复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
' F3 |( ^6 C6 U0 q4 q2 l% \" X5 B/ G9 ?1 I5 H0 @& m: ^
2 F* b$ C* v8 g9 Kmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
5 V6 r' \, x% Q5 Q$ ^4 P [! C6 Z8 Y7 X1 O( _. q3 k: m9 U: e
# K3 ]' ?9 |- Y. u$ cd /root/modsecurity-2.9.1/
' ~) K# o* f4 j- n$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
6 w6 G: n9 m' m }$ cp unicode.mapping /usr/local/nginx/conf/
5 v) ~% y/ O% `) P- t. U! R将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
6 o' { T" s2 Y9 Y0 {/ ?" b9 c6 Z) n
: J! s3 Q8 `7 }8 Y8 u
$ vim /usr/local/nginx/conf/modsecurity.conf
$ G7 T C- ^( {% r* M: HSecRuleEngine On& V3 m e3 F* u; M, f
ModSecurity中几个常用配置说明:: }: j; E7 U/ m! A1 t; S
( T) _. Q: A/ t# q0 e
* [' l8 K. x3 W0 P+ ~( w1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。$ t2 V- H6 h3 a9 q
[% A& Q4 I: E5 [$ U$ G2 Z
# T. ^- j t% u- `5 X4 s# j
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
6 Q7 ?/ n* Q8 J; q0 ?( D
+ U- Z/ t; ^ c3 [3 p. [$ m/ _7 F% p. e% ]! ?$ a' e8 S' {3 S2 b
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
# w' B) D) I* D5 c8 h) |; u' N' p- `, n$ \2 y) B- B
' ~4 u# B/ k. N9 ^3 E5 T# |. e4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。2 j4 ~9 M/ ~, h: X0 q6 X
5 B3 A; T+ t1 J$ |: [7 U
% |3 y. P- {$ l7 e* d
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
7 A3 @! W4 @3 z3 ^4 O, f9 r2 h" u0 u- i8 k2 m$ o2 v, i) Y
9 n3 m- V4 w. | K5 j5 x3.x版本CRS
+ k$ @4 Z' |* b- d$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
3 v4 O5 M0 S( r# 生成例外排除请求的配置文件1 Z4 X& ^! r8 L
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
- h5 f; K P; l! `$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
$ e* Y' m% H2 t8 s3 I2 R$ cp rules/*.data /usr/local/nginx/conf1 t7 d5 [8 {9 A% |
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
/ b1 {. Z1 h0 k) U
4 r+ y; |" N" h1 `' I( Y( m" _7 \ @7 H) T
( l, H: ?% G0 J( G% M9 U: w5 g$ vim /usr/local/nginx/conf/modsec_includes.conf6 j, Z8 {- R! X" g
@( O! h; B, D% @) G( y[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 c: t8 {' j1 Y- Q
$ M* f' l5 ] c- ?7 K& R8 {: x" c
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。 Z5 e+ u( p: m1 e. I* [ G# `( P
" y. e" G# k6 A% j* e5 \) v
& {5 w0 L: h+ ]4 F
配置Nginx支持Modsecurity
' ]1 t& d% a8 I$ F6 }启用Modsecurity' }* A7 _9 v# ^( ~
使用静态模块加载的配置方法
/ a, w- x) z8 x8 S) K在需要启用Modsecurity的主机的location下面加入下面两行即可:8 p$ ~) `2 A( p9 f; r/ X% G
& A9 l9 b P F- y! G" u6 D, a& m5 L4 V5 m& H
ModSecurityEnabled on;" ^4 X( R! L5 g- D/ A5 K
ModSecurityConfig modsec_includes.conf;
3 P- S. W! @$ B修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。! ]* {% V# I ]7 J0 P* _
) n+ a5 ~. ?6 {4 h3 [5 z$ |/ P# n: q2 {
$ vim /usr/local/nginx/conf/nginx.conf
5 Z& Y; y$ o- S6 G2 C9 o( t* l
! K9 e1 {* U$ R' j+ v/ v) W8 r9 ~- j3 H
server {
( q+ F# D$ p* J1 x) s listen 80;
7 G8 E f8 N8 ~ c' x server_name example.com;
- a& ^+ F( t! W/ N+ u- A
, {6 x5 Q* D+ ] P9 J/ c' e% o- O3 D$ ]& {2 c' ]$ |
location / {
' g; j' n" S v0 ? ModSecurityEnabled on;
" S3 [( E! q7 r3 ^9 h ModSecurityConfig modsec_includes.conf;' g- i' g) S* x4 _5 G
root html;4 \+ j2 p N9 n4 I" c$ A8 v
index index.html index.htm;
/ U D- x7 [( Y }* }0 G9 g2 ?) V; h8 Q! ^) y% ]
}
+ O: D8 F( F2 N) Z+ M使用动态模块加载的配置方法
! ]3 z& l( P4 y& d在需要启用Modsecurity的主机的location下面加入下面两行即可:
5 D) }, f" Z" O" A! O% ^& E$ X" \( E/ f9 |
2 E" ?5 i; S$ h1 L, l% xmodsecurity on;
7 n1 T8 i; U9 lmodsecurity_rules_file modsec_includes.conf;
6 m1 a0 _1 U: }$ {! M' ^4 l修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。5 ?% r x6 h$ d2 A: `. j% c
/ ~! H$ m: `% I, T9 e9 m' m# I9 a
! e: r5 k# d- F1 ` d! ^$ vim /usr/local/nginx/conf/nginx.conf: h; ?7 g, z# O1 [) i$ q$ _
" h2 @8 S+ }( ^$ a/ W& z* ]6 I
5 P% @" f, N# Q( z
server {
: W. g" d9 y/ r. G1 f listen 80;, x2 g" x' {5 H2 ^5 G5 l- o
server_name localhost mike.hi-linux.com;6 P) N5 X: J+ l/ _6 }: I
access_log /var/log/nginx/yourdomain.log;3 S5 D* y% k1 }% R( T
* |- ~" P+ Q2 k, w; G
" y& \& ^2 |1 E( _; B location / {
1 T( {: y% j" q1 c' h
j9 K( M$ D3 E. o6 g f8 ~: w! A6 ?0 B
modsecurity on;
( @5 N# X! Q6 M* e modsecurity_rules_file modsec_includes.conf;; w/ f1 u! {, c
root html;" u% m3 m3 E- V0 M U
index index.html index.htm;% Y* V1 p9 l6 I! H
}9 Z9 J% z- n9 K
}4 V3 v* r- w6 v7 S+ S: i/ r" m+ R
验证Nginx配置文件
: \! w$ K. u% ], P5 f$ /usr/local/nginx/sbin/nginx -t @ I) ~( k7 V2 C' j3 w
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
) ^; L& z. A, y" r( K( Nnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful Z* k* i# T7 n( S1 \' s; i" G. x
启动Nginx
+ N( l g# E! o, ~. S$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
, Z' M& v: f O/ C7 Z' A/ t Z$ p, S3 t& K6 k' G+ s5 V
测试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能够战胜更多复杂/未知/混淆的攻击模式。 - k' Y" n' a. x( P% u! u) ]
|