ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。! x; ^5 u/ d: ~5 W( o" x
* o5 G- C2 o; N- N
, Y! I0 }# G# g在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
8 H3 D/ k5 f' J9 C0 h+ O; l/ {) J5 F
3 Y+ \1 O6 k( Y9 M) e* q
什么是ModSecurity
4 }4 i. Y. q; wModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
! `" O/ h9 g2 A6 g
6 e# |; R0 m4 }) u# L' R& h1 s' h6 T7 C
6 s1 x A4 ~6 z. y! q, DModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。) A) T& [1 ]$ c; U9 K7 p1 |
0 B+ T; }/ b' X% c. t* ?
; `; t% e$ W* xModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。3 B! ]- P! A5 @- x4 E
N7 Y) y) I. k/ ~' I
! Q- S! ]8 f( U, H6 ^
官网: https://www.modsecurity.org/6 j! @& l1 s5 V. i
0 l, A: d% f H- |0 b5 }
D d) M( D1 L什么是OWASP CRS
! s U+ X3 ~3 `" Y7 Q. hOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
8 V: Q9 ?7 b2 r0 U3 [9 J% _$ F5 ]8 K
' O8 [( z- P3 L& Q/ \0 J
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。3 D9 P5 M. ]! W2 g& S& f, @
; k3 y3 J- E2 i4 B6 [
3 I: B l4 R0 V* EHTTP Protection(HTTP防御)
& I* P+ {! e3 Z* x1 S. }( sHTTP协议和本地定义使用的detectsviolations策略。( l k8 s( T w. I0 q2 Q& @+ S! L
7 Y/ @( U+ w5 t0 ? d+ Q
, C' N/ d, W4 A( Z+ b) u7 v
Real-time Blacklist Lookups(实时黑名单查询), L% Y. `0 U1 @6 {6 W! D `
利用第三方IP名单。, F1 F- H; y) E$ Z) ~$ Q( n
, F/ q5 B! |9 w8 a5 `' A/ e# v7 T6 @3 Y
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
1 L3 T7 R! i! A9 e$ S. L" W防御HTTP的洪水攻击和HTTP Dos攻击。 a' c' g1 o2 c# w1 u) b* \
- c# r T0 Z/ W; h# l0 V7 l' D: x4 A) z" y. o [+ u
Common Web Attacks Protection(常见的Web攻击防护)5 j7 @9 c4 j9 d+ v" R* f
检测常见的Web应用程序的安全攻击。2 I1 c" Z0 F5 `$ U7 Y( \
; `# U( W* V) `& s- }3 Q4 i/ p3 P" @* @: Z3 i/ ^6 z7 d' y
Automation Detection(自动化检测)! A a E8 D7 {; r; \& d1 `
检测机器人,爬虫,扫描仪和其他表面恶意活动。1 W5 J/ O7 G# c; T& W3 E
* u1 {# c$ \; l( t* M2 `8 E. ]
u+ `: ?7 x1 P+ T5 r
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)& I1 g* L4 x8 X W2 e5 H
检测通过Web应用程序上传的恶意文件。& O2 G6 H% h" g6 \7 V
& b$ [0 r6 p: P" `, r! H! f2 N$ q$ r8 _1 d" k
Tracking Sensitive Data(跟踪敏感数据)
6 ~# D" j% `0 V3 e* ?) |: K& P1 S# N信用卡通道的使用,并阻止泄漏。
9 P' `8 W, w4 B/ G
# C' u3 ~2 Y! E4 x) R+ ]+ X/ ~% U# f+ g" F. G
Trojan Protection(木马防护)/ n& e& i/ f8 T8 w
检测访问木马。# w/ z' {: ~1 P1 G( {
- x5 v% `! z5 M3 ~! P i# p
0 M) Z/ L6 C7 }. U5 _
Identification of Application Defects(应用程序缺陷的鉴定)
0 O) k! a2 S A4 j检测应用程序的错误配置警报。
1 g: o: n& T1 r* D% F x _* `6 ?) @% e+ H! w M8 J
1 v8 z5 _ q7 x1 I
Error Detection and Hiding(错误检测和隐藏)
' h: \. T/ w' Y9 o* j/ T( W @- C! S# E检测伪装服务器发送错误消息。
) Y% ~, L7 V( x9 r) [' _$ M
/ M* x% b; q% n2 c
) N2 L: d$ [6 H: Y& {3 Z( ^: T3 K6 c( q安装ModSecurity4 Q4 f6 f" {: [" K
软件基础环境准备
/ X' v) z2 Q% ~0 i" f下载对应软件包
) n/ X- D$ {: c5 m2 C1 U, l) I$ cd /root
0 j6 W2 t; w8 g/ W$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
+ a. ^+ ]" w( k. x5 @$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz$ _; s( V$ q) M! p6 B- B, u! A
安装Nginx和ModSecurity依赖包
# j$ v$ ]1 g* v' m2 I4 K! N1 lCentos/RHEL
; J/ m% W9 Q, Z1 _% c
$ `. q( I& v* y" M+ ^/ q
0 m9 i+ y. ~: Z% I$ v$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
- K* O9 c2 {& PUbuntu/Debian! M1 S! l5 j& H/ \
1 p% L, S. g- p1 U2 c+ O; C; F6 S" Q0 h/ p+ j
$ 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- I) a" |% Y" @$ J
编译安装ModSecurity
/ Q8 g" [* _; t% T2 qNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。, w$ }) e1 n( L' G/ F
$ b* Y2 n. a6 K* Z% e( m3 s
5 L8 f* L0 k0 L# {/ Z" H% V方法一:编译为Nginx静态模块/ _3 A( d( v. @
3 `8 u" S& k0 ?
. G! C6 a# k3 \. f3 _+ o! _2 @编译为独立模块(modsecurity-2.9.1)
' g1 e+ S3 g6 @; x# w' H y' T$ tar xzvf modsecurity-2.9.1.tar.gz
, k; T6 c. ^. [) a$ cd modsecurity-2.9.1/1 G9 Q0 f! V+ S3 R0 i0 W$ a9 r
$ ./autogen.sh4 Y( o4 C+ L- N4 P; x. y
$ ./configure --enable-standalone-module --disable-mlogc
; x( I" b* D3 M# S7 U, S$ make
/ q% y$ N: \# @: i9 g1 J; r C$ B编译安装Nginx并添加ModSecurity模块
5 g" s" Q. v" x* Q5 N. G4 G. s3 _1 [$ tar xzvf nginx-1.9.2.tar.gz6 c% k& e/ T7 j% G7 |6 q
$ cd nginx-1.9.22 k+ ], W$ d+ M. ]1 J' K
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
, ?1 ]& O6 w3 k- {$ make && make install
5 Q% i8 `3 H" L/ Y! E4 [9 {方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
9 n" F6 G* a3 H( R9 O: I5 v1 m" r0 q7 n& H# s3 L
; `7 `- m2 L- M$ C) u+ m+ N
编译LibModSecurity(modsecurity-3.0)$ P3 w' D+ ~( D$ U
$ cd /root4 |) ^7 B, `. h, L
$ git clone https://github.com/SpiderLabs/ModSecurity) [4 n1 d" n) l2 V2 T6 @
$ cd ModSecurity
- V3 D* j/ k2 E5 p$ git checkout -b v3/master origin/v3/master
1 i9 L) L* y: O: D8 r$ sh build.sh9 c1 v5 Q3 X* E! |1 B# o! ?
$ git submodule init
0 n; G! I0 M2 Z. r% T' n$ l$ git submodule update
* R- r w4 X: [2 `0 f0 Z7 X$ ./configure! c7 c4 [0 \; j# ?" j
$ make
4 e3 ~- x2 c4 G6 y+ r% e$ make install
) R4 Y+ V, F! n( v/ }8 j* z! w* @5 }LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
: H/ \% t2 L- p9 c7 z5 i; ], q3 B- h' E: H
! {) M3 ?5 I8 [- [4 ^# t$ ls /usr/local/modsecurity/lib
( j: }$ U# `: z0 G2 Z4 V3 ]libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0+ Q5 o7 M" `! f4 O4 f" E, t
编译安装Nginx并添加ModSecurity-Nginx Connector模块8 z2 h' S. j) G% r+ p
使用ModSecurity-Nginx模块来连接LibModSecurity
0 E4 `- ]* z' G' m
+ g( Q& {3 B* p
& _; ]- W o" H* [$ cd /root
& b8 k1 x/ [3 p% E% W) O. r& K1 I$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx0 R; w) {4 w: S' ~* L7 h' F
$ tar xzvf nginx-1.9.2.tar.gz7 j6 u" [: C* e! F/ B) @, q
$ cd nginx-1.9.2
! t t4 y, S9 u6 }$ ./configure --add-module=/root/modsecurity-nginx
; Z. @. O: l8 n" v$ x" ^& h5 I$ make- A) V- _0 F$ G% b, q a
$ make && make install, V; `) D9 u5 A" M
添加OWASP规则
1 }: V. u$ Z9 U2 }1 U) a) RModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。) `* N( J' P1 H5 z+ S, _8 C
/ q* }( Z* ?" U! d, G
: {4 H% B4 x- V5 Q* I下载OWASP规则并生成配置文件1 B% C' ?0 x6 n3 F& M, E
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
. r3 {8 x( d/ _! j) ~$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
8 u- q$ k: O% M( C$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
$ n# M2 F9 N' I( q. W. E$ cp crs-setup.conf.example crs-setup.conf
# U I/ J% D8 P2 C% _5 h配置OWASP规则
6 Y1 M. ~- J* l; ^( A5 Q编辑crs-setup.conf文件
9 d2 G" F$ r, q) g* l7 o: s# M' d: J) p8 f) N1 L& t2 Q
% d, Q6 {9 ^* A8 \1 G6 r# o$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
5 U1 _1 W' i( [. n2 r) p$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
u- s! M% @) f6 P1 M" P6 t7 U$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf6 K# ^- S1 o/ _' P; [, @- ?
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
$ W& @3 Z. J8 F3 `3 c默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
) c+ p: K4 v* O3 |7 I7 }: W U* b; c% [
- ~" h# s$ {# D [, x6 ~启用ModSecurity模块和CRS规则
' p, L, J) e* g* l复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。" x8 p; F, D; _$ S
; o0 s2 `; [; U' L; i
) H+ l, ~; @3 T" J; j. Gmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。' e) b) P8 m$ i* I- G# x
' a& e5 {5 v; y5 r6 w! T/ C c; V0 A. r' Q1 q
$ cd /root/modsecurity-2.9.1/3 ^4 Q) a2 }; u8 ^7 |
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf 3 s1 t% Q3 z+ x+ W% S
$ cp unicode.mapping /usr/local/nginx/conf/
& G1 O" }! `4 w8 p将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
; W* Q; K0 o& t1 D Z
9 J- ~$ O, b! ^" R' C0 l7 E I4 s% m$ p! \- h' m
$ vim /usr/local/nginx/conf/modsecurity.conf
: m2 L. p0 D4 S, H0 w4 tSecRuleEngine On
) Q8 k3 i5 }: mModSecurity中几个常用配置说明:
# O' g; `: ]4 C) c' m6 c; n
' c, `/ V4 ?3 k" c7 G1 O6 E' y* B, F% d& X4 }8 Z
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。1 k% X+ ` I! r$ `- Q
/ X5 b2 q( t$ f9 b( |) U4 H( M( Z, m8 @1 k8 ^4 R; E
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
) L% F8 p, m! a2 Z
3 |+ J$ e- l6 r) W/ ]5 w0 Y8 c# r2 [5 P
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。' g" h5 E! w% {9 |2 _3 \
7 D$ l/ i# r) g0 L0 B
0 G. }# ?, L, I+ m/ E0 o8 R
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
4 v. o5 M3 Z3 v
, |+ n! F: [ z
+ H0 @7 O7 R& N1 @7 m& Y在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。1 n" \, J! o" j! O- ~8 p- b
4 V! Y7 G; @5 P D
+ B% [- Z2 d5 }# x3.x版本CRS$ E( f9 ~' M! {& X4 r' w4 |
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs( l& p5 k# W" M+ h$ A. V* S' U/ d
# 生成例外排除请求的配置文件
4 T& g( s j, B; u2 b$ Q7 i$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf7 I5 x0 b8 G* R
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf: h4 h+ p0 l: i
$ cp rules/*.data /usr/local/nginx/conf
7 p4 P8 [" H( e' F. k为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。7 Q2 l0 E/ [2 c* m; Z4 X' ?
" V1 ]% n. k; m6 u
) _5 ^7 F, E9 x# t' O$ vim /usr/local/nginx/conf/modsec_includes.conf$ H% L" C# J" c5 S: t" \% a( d
0 l: _2 z$ Z3 i) [4 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
* R# h; S4 x) j7 q: X# `, S
$ `+ d" M/ U1 A3 }; W7 p/ n注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。4 O: c0 G# @; z( a; Z- p, d. @
, m Y) w7 R" |. {$ T" t( r9 ^
) {. b0 i5 Z' ^6 d: x配置Nginx支持Modsecurity
% `- Q9 {" o8 V8 v# y- o- p3 g启用Modsecurity
( {0 }) @' \2 P使用静态模块加载的配置方法
3 s& k$ p( p" S9 j在需要启用Modsecurity的主机的location下面加入下面两行即可:
( B; A/ d# I# S9 N! T, m1 |) ?" q# V- U' c
, p4 b0 X% Q4 c, R: T* Y# A2 R
ModSecurityEnabled on;
L) a+ }; T0 X$ {) U" \" ]ModSecurityConfig modsec_includes.conf;
, e7 b r+ U+ ^* I修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。1 Y6 A- K, ]; S. {. ]+ E" s" ~
" ?& x$ a. {% h/ B
3 ~ j$ z/ G# Q& @) o+ z$ vim /usr/local/nginx/conf/nginx.conf2 e& W; X" m$ I0 w( O
* J& j4 k! I9 q& {0 j+ |7 o
) p' ~* h7 ~, R& ~, R: Jserver {
: t$ s3 Y7 @6 M% N; o" Y listen 80;1 ]! [7 B# k4 _ K. Y* u; Z. S
server_name example.com;
2 b+ |% D% W9 l/ V
0 `, F1 b8 A! z+ v" N; K7 d/ i) X( ]2 L6 y! T# N# t
location / {
/ A Y* \& M; Y2 D8 @6 I- d ModSecurityEnabled on;* c- H5 X4 t# A
ModSecurityConfig modsec_includes.conf;. U5 U3 `* |7 I
root html;8 u F4 d- n1 e3 ^
index index.html index.htm;
. D& K i6 B8 @" q* { F4 _" Z }4 N! J0 Y" `5 X$ D
}- q2 c3 S9 q8 k0 K8 U0 H
使用动态模块加载的配置方法
+ }1 H9 d1 y+ u9 r R9 T在需要启用Modsecurity的主机的location下面加入下面两行即可:1 O+ p% V; M! |2 x5 a7 j* |
- |3 ^9 T$ i- @* A: N4 q2 i2 P# {# [1 w
4 o5 m0 w3 U+ f/ wmodsecurity on;
) `1 ~! N: O8 P- z3 n9 Smodsecurity_rules_file modsec_includes.conf;* X1 _2 k9 ?! a# A. M& m
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。' f$ |, J N+ i( q2 m
" {: z9 O1 n5 O; t- D
% e. O/ k1 K2 p1 n A$ vim /usr/local/nginx/conf/nginx.conf
" u" v+ ~9 u0 C0 G7 z, J1 W5 T2 |, {% m4 r: M
1 [" o5 G1 {9 a. @- Z1 Y
server {" v( ]$ { h5 C% |
listen 80;- p5 x( U) V' p& L
server_name localhost mike.hi-linux.com;
|* ?. o* W4 Q- A8 S access_log /var/log/nginx/yourdomain.log;0 V1 B) j6 u+ s
* `! A! W% F6 Z& G+ d: B& |9 g2 A6 k6 x7 @$ {* a
location / {
; [7 G+ B' b1 E! G# w; ]$ T& l( l3 c$ \3 m
+ u7 s( w6 q2 ^2 ~+ f3 _
modsecurity on;# P9 f) n' c# z& V- y7 N3 \
modsecurity_rules_file modsec_includes.conf;
6 d: f6 F5 Z Z3 `$ C8 | root html;
3 P4 E9 c5 E7 A2 j* | index index.html index.htm;+ B6 w+ l: m3 R8 L0 e
}
" N) J' n1 _2 H" X}# T9 K: l9 i$ Z' S! U8 ] B2 t
验证Nginx配置文件2 @4 s4 b* D" V( e3 z( \
$ /usr/local/nginx/sbin/nginx -t
9 ]9 i. C' V3 Y. P( W* Dnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
/ H" p! ?! O3 t5 g' x1 rnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
. w: h% J" y5 U k7 X启动Nginx
! T& ^) B2 X- D- v% B$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf! M. f3 y7 ^$ j% Q$ K& |
: |1 A7 t! d3 w) o测试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能够战胜更多复杂/未知/混淆的攻击模式。
9 B$ `; V# n2 v5 c |