ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
$ f6 d8 N# h8 }
3 T; l# r" V/ W+ @9 o3 ^
4 H' C$ n* R( w. ]$ |2 D在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。$ i* `; j2 }& ?+ w( q
% T' g$ O# R1 x
! ~4 G7 V% A$ u9 U6 y, f( C什么是ModSecurity8 i! [& E+ r' Z
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
2 [- e- G( b+ N/ H; r; o8 e8 J! \ n" P$ c% F; w9 K7 `
5 a8 o4 e. o$ L
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。4 H& t( o5 a/ E! [
" }& b! x1 |7 o
) y" l2 J; A6 X+ S* sModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。4 i3 F: X( A2 H* ^; Y
3 V& y8 w4 T: ?
7 @1 n" v' H8 M) b% v: ?
官网: https://www.modsecurity.org/
* K/ {& P t8 e% `5 d9 z) s) n: J' @% @$ o9 g1 Y
/ q1 l, j" @5 }4 ? ]: u9 M3 s什么是OWASP CRS
! h5 v2 z. R# |# F* P' ]OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。" z. a; K0 W" W+ c3 D! x1 n
) |. ], o3 z. x+ a" M N0 v+ I
8 P: S- J, D& Q1 oModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。: }, G; p& [4 o m, x7 |
6 f; A' m* R5 G* |, E! g/ v! e2 K
/ y3 \0 G6 v, v# f' }7 L0 E, aHTTP Protection(HTTP防御): {" R( c8 n& i+ p
HTTP协议和本地定义使用的detectsviolations策略。* H8 d" @- C u2 [+ T7 O5 Y! K
7 \) K1 ~1 a" q* D: A8 [+ l. x! m ^, n. N& r+ I' [
Real-time Blacklist Lookups(实时黑名单查询)2 c0 n) I3 G6 {0 J g" f1 p0 D
利用第三方IP名单。& O8 q5 G2 k' X1 C2 E S
' s+ Y2 j+ V0 f# u* q0 U5 K; s
1 K0 r' ?+ }! J1 a" AHTTP Denial of Service Protections(HTTP的拒绝服务保护)( e6 C! Q; S* W
防御HTTP的洪水攻击和HTTP Dos攻击。. M% J) k5 e3 ]- J9 Y
7 r& a" e$ Q/ G- S1 R
2 d5 V- f) H. I- V- hCommon Web Attacks Protection(常见的Web攻击防护). b: ?/ f3 a# }& r% l. ~
检测常见的Web应用程序的安全攻击。
0 W l2 `2 \9 C. X0 X) Z: D1 [5 v" x5 m
7 K6 E( C0 o, z- B) T
" R: e# o+ [/ nAutomation Detection(自动化检测)" j/ J- T# a& U
检测机器人,爬虫,扫描仪和其他表面恶意活动。8 @5 ? i- u/ a, X, G
0 h# z7 D- U) [, m
9 u. r) t+ o }0 T: E8 MIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)/ o+ [+ _- J6 Q3 I" L
检测通过Web应用程序上传的恶意文件。
/ ~' E' G& k: |% i+ z y# m* W/ E6 f. |, x: r" N
4 C5 f2 R7 v2 V; p
Tracking Sensitive Data(跟踪敏感数据)
$ l1 h$ v0 a) ]( w+ C信用卡通道的使用,并阻止泄漏。6 O% R( W1 @+ X
1 q$ ]7 b1 g+ l6 b& X9 i3 i2 m5 i7 S n! o# W- B" R1 m
Trojan Protection(木马防护)! F8 G3 |$ X% [
检测访问木马。
" X% _! i- x: l6 I' C1 U/ }; l9 c7 R- d3 ]! j0 \: p K5 J
, Z, s! j5 C: d9 w R4 ?2 L0 Z
Identification of Application Defects(应用程序缺陷的鉴定)
6 I: V' c/ A( f检测应用程序的错误配置警报。
# b* D- ^. {1 J( t0 J% Y$ u
+ Q6 B# `6 {9 F: a( |8 r& \5 s" w; I S4 u [) z, [2 E
Error Detection and Hiding(错误检测和隐藏)1 a5 e* ]& Y. [# o/ n- u, ~0 W
检测伪装服务器发送错误消息。( G2 ~5 {7 H* R; i
4 b, d' s) I$ ` c1 x
6 n6 a2 {6 v/ ?0 A1 k' A' e2 Z1 J安装ModSecurity
% {6 J5 @- s3 l" R- d4 r" ]软件基础环境准备( ]0 n4 i7 W0 F4 v
下载对应软件包7 R+ r9 R6 a* ?% _1 @7 N5 L: B
$ cd /root
2 M) e, [: K- t6 O% O k8 Z$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
9 \5 a+ T% G. t" D- G$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz3 S& |4 o ] |- W Z& {' K
安装Nginx和ModSecurity依赖包 S* S2 q! G% i* S: j+ L
Centos/RHEL
' o4 P& m9 I4 \* F4 X- }. g- L& _. H' U7 b4 q! {
F+ }) O0 H, S! C9 O# i* D& u$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel/ Z( |$ w/ p5 U# ~$ A: y/ ]
Ubuntu/Debian0 F. Y) [" ~+ l# h0 F$ Y$ m. n
0 K- a3 t1 ]7 L! s- \& N8 } m, J
5 [% h( [( Y1 R$ 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
+ O- j( p( N3 N* b& G/ F编译安装ModSecurity# L2 P# c5 P {) l2 Y8 ^: N8 j8 |
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
) ^. P2 e2 y, O$ D' _, p0 e' p- Q
' q% K8 n9 P* b. V6 `2 X T$ j3 X4 Q% ~
方法一:编译为Nginx静态模块
U9 \' g) Z% f# J6 I# Z
: A% Y0 E* s1 k- f8 j/ E" q
5 U1 o0 E* Q8 a- n4 M3 i: F编译为独立模块(modsecurity-2.9.1)" a' L* u" G% J5 V2 a8 m' x# J
$ tar xzvf modsecurity-2.9.1.tar.gz
0 W+ Q, G2 G, e. p2 D! a1 a/ k$ cd modsecurity-2.9.1/3 W- `7 v r# r+ |- y7 z1 M$ O
$ ./autogen.sh
2 z+ v6 c: \/ L& Q$ ./configure --enable-standalone-module --disable-mlogc4 B' T( I! e: z% W' _
$ make
# s9 ]' U% ~3 q) N! j1 W& z编译安装Nginx并添加ModSecurity模块
. {" j. I3 }$ N0 {0 _% F7 W8 ]6 C$ tar xzvf nginx-1.9.2.tar.gz
: r- O+ G4 _" ?( R$ cd nginx-1.9.2
) }; n5 [ ~ ~; X/ ^$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
( q4 D5 z' x! ?% d) W Z2 q" X$ W$ make && make install
2 ^2 }6 g$ P4 z- e* M5 S% V: t* R方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
" H8 f6 R d+ b: g3 @
6 H3 a# W% j& \2 ^) t% ?0 C2 u' u9 _
: ~% {0 C p. w V编译LibModSecurity(modsecurity-3.0), W. z& ~5 h" a
$ cd /root
& J' g% n1 G) a$ git clone https://github.com/SpiderLabs/ModSecurity; h# ^9 l: {) n# y
$ cd ModSecurity8 }. e: Z3 \$ B2 U/ z& h5 v; I- I3 i
$ git checkout -b v3/master origin/v3/master
/ F' W# M/ c- K1 c1 }0 G$ sh build.sh- ?: ~) P) A/ ]% n
$ git submodule init
: y+ d1 Q& }5 g# A5 x7 z* [$ ]$ git submodule update
x3 w- G' J) A$ ./configure4 c; p5 R/ O4 E o
$ make" l2 F5 I0 m. ]
$ make install
$ }, \1 [- @4 V) U b b; ]LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
* x8 N# m6 F9 F
9 F: `% _1 K$ P. A( E3 W0 s6 |. H" |. @" c
$ ls /usr/local/modsecurity/lib
( L- D9 v% J7 a: O1 Z$ o8 P/ Ilibmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
, b" b* b5 u' [) O x编译安装Nginx并添加ModSecurity-Nginx Connector模块6 U/ j3 B+ k9 }& F
使用ModSecurity-Nginx模块来连接LibModSecurity/ r& W) g$ j( m) U: ^ A+ u* M
' Y- t, \0 p3 G7 c
' y) t' l6 s; o) J( u$ cd /root
8 d; s9 `& B y1 w* V% Q$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx" \0 \6 C$ Z0 H% N
$ tar xzvf nginx-1.9.2.tar.gz
$ c' a: t/ N4 V% f# a" \5 V& E$ cd nginx-1.9.28 P, s1 W, I w' @7 X3 k- Q
$ ./configure --add-module=/root/modsecurity-nginx
3 ^7 _1 m1 Z4 m$ make
7 m9 b$ _& R! X$ make && make install
5 P6 D w. @. s( e8 r. L. k添加OWASP规则
- T3 h. m, S: L% y0 m$ w. V: |ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。+ q( t& k9 g" |- s( J
7 u+ B( e- A" d' T4 f+ v2 B% {/ {% f- s5 g( k
下载OWASP规则并生成配置文件
; |$ N0 Z. X/ v* ?1 y$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git* q: }! A( E% }! {/ p/ f
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
' x6 o0 p; ?: F4 T* Y6 j5 x$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
1 e+ v0 Y) H# h* v4 I$ cp crs-setup.conf.example crs-setup.conf7 q2 i" I0 R Z/ y! _
配置OWASP规则
) Q1 F, `& }6 s O) i6 D编辑crs-setup.conf文件
+ p2 U9 g) v5 A8 W u& @+ s' ~
x+ w1 h5 x; d1 _+ q1 [: [8 ?
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
% ]. k6 `# Q/ _* N1 A$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
3 \& I& B+ ?* E @$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
$ r- l! T- D1 H% H4 j. d/ ]$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf0 L6 u- L& E$ G3 Q) d+ t
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。+ d- @& f$ W H
2 y5 G; [8 k5 H( r* e
; r% z! D# Q5 U* b' A. ]启用ModSecurity模块和CRS规则
! t- b' m4 |1 a4 r$ F5 g复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。/ S5 N( ]- G3 _9 F, i+ N
# E" y1 B- U: H; u8 G: X1 ]) [
" d3 x$ L. n% r& D+ \modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
. z M$ }: z9 q0 J" ]( W) X
% s" I$ }; R( B" f$ n* P
- k6 Y: D0 J. B$ cd /root/modsecurity-2.9.1/8 o; ]: V" F. N! [6 v' L9 E: M# p* \: P
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
, o3 x: {# N" Q7 g, D# I5 f( I$ cp unicode.mapping /usr/local/nginx/conf/
# u9 U& \* p! o/ d将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。4 h3 g& k: U1 T* X0 f
/ g3 i: k) A0 E, _% l" b1 B
~8 ~2 a+ T. _% V9 a5 E+ W, Q! i! j, \$ vim /usr/local/nginx/conf/modsecurity.conf
" W& x) K4 j1 f" {; }4 X D/ uSecRuleEngine On
0 [7 f$ q# r9 N5 M2 EModSecurity中几个常用配置说明:
5 V# B5 W# q7 g6 f( @! B: ?5 ~
; [% p ?. _) B; j5 \, x- o$ R" m* @4 Z4 N
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
3 D, S# g& x; K! P/ U, p9 \7 c' E& R# T& P+ [
: `( Y6 d, R' }: m) o2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
& r* x* p6 X4 L2 b" l! I4 J$ N1 b; D4 r; S
! t: r. d* u. z9 b3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。) O) } K- C* n6 o W; w
" d z R9 c+ b
+ `8 k0 o) ]; V: \4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。" o. I9 S; A+ G" u5 d, b
S1 g3 W! e6 h7 D _2 m) ]4 N+ H4 p1 B+ O; V4 c, f( @
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。7 J6 t# l T& }4 O$ l5 r
4 C7 f/ B* a7 P( o' M
+ n: a0 r9 }6 b: g0 H
3.x版本CRS! n% J0 w: R+ u4 L8 f& x/ G
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
/ ]% @8 A2 [8 z) F8 t# 生成例外排除请求的配置文件2 ]1 _" b M5 n, f0 h$ D [' E
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf( x. S8 |3 ], i' z) X
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf' v: o6 |% |6 _. }
$ cp rules/*.data /usr/local/nginx/conf& B6 u2 v9 @. e5 Y( j c
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。* o: g, X6 [- ]7 m9 H/ n
' c" N4 y7 z: @2 i* j* m' Z
2 _$ q' Q- Z% j8 m+ I8 L
$ vim /usr/local/nginx/conf/modsec_includes.conf
q! d& B8 \; S" `' f
/ V! P0 Z, g2 E[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
" O T: B: }) O" {1 U
- n" k/ b, t: V) x* G% x4 _注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。) W e2 i. t( x7 |, q+ D
3 M2 _% s5 _" o0 l U& i% d
6 H0 I) n9 W h2 F7 a配置Nginx支持Modsecurity
6 c2 [+ A7 F4 A2 b# l( Y% O' y启用Modsecurity1 q' |$ n; o+ S
使用静态模块加载的配置方法
2 c9 U# M! P/ B/ d7 @' F$ [0 g在需要启用Modsecurity的主机的location下面加入下面两行即可:+ o8 T y% ~! V1 {. H
' d# y8 {( Y6 H: n
" B" _1 B* |% [. s
ModSecurityEnabled on;, O# p7 C0 K2 t& Z
ModSecurityConfig modsec_includes.conf;4 E2 N3 z" j" E5 t
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
$ ^6 I3 a9 m% |) D( G1 Y- f7 L" o0 d4 }
2 l3 L f- W @# R& B) T$ vim /usr/local/nginx/conf/nginx.conf+ X) E' a% a5 z3 g) E
1 s! R8 C: }. i; l2 ^# @/ @
9 {! L7 ~8 _! e% L2 u; M
server {
! h4 i- i: y2 S8 b" }6 H3 E listen 80;3 z6 H7 q) q/ {: N( `
server_name example.com;
2 l9 y1 W R/ h+ |9 a8 ]& s/ u9 b7 C/ [& \% t
, n2 M' S# a/ U5 f/ A, M: _ location / {5 ]# @$ @* {+ g% z0 v3 J2 O
ModSecurityEnabled on;
* p# |, R' c1 Q. G0 ]# i ModSecurityConfig modsec_includes.conf;
# k! h: h, _0 F4 {8 _! y root html;
8 ~; u0 _, E& g* }/ \4 b0 A* F index index.html index.htm;% I4 c. a; O& Z4 d4 s
}" e. @% v* y4 Q: @3 D. F
}0 [/ y R% r3 I5 ]* @: @
使用动态模块加载的配置方法! h E, x+ X" ~1 Z r# ]
在需要启用Modsecurity的主机的location下面加入下面两行即可:4 K4 c8 r" `+ A4 L, }: {5 u
0 b9 |! ~3 } K6 v: t5 e" U( ?* N* u- z- d F. k! o
modsecurity on;
1 \) }2 l' H. P" T) qmodsecurity_rules_file modsec_includes.conf;; I3 o/ ^7 h0 {$ \! Z- y* `
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
. Y/ D8 P9 F) B; @7 Q
( _! ]4 d3 }! n H$ h; F) _& A R+ p% d) }; s, W5 g5 F
$ vim /usr/local/nginx/conf/nginx.conf$ v& ?; }0 o1 R% s5 H2 l5 }. s7 ]
& s' i/ D' G' n
4 W& j' M1 n: Z
server {
4 c/ l7 R2 ?: Q# S8 X8 d* v% L3 [ listen 80;+ Q/ Y f+ E1 ?. o: D
server_name localhost mike.hi-linux.com;# o4 p* ?2 y! ?2 x \0 X" T: J
access_log /var/log/nginx/yourdomain.log;
5 j8 |5 b" E0 h: Z2 B; S: P% m" w- z6 S& I+ I0 Y, R( p! d, ?; G
) s; W* f; j/ n) g
location / {
" j( {# C: s! R4 V
5 E1 t( [( s6 ?; ]3 w7 ?7 h' G& C) a- E: q: N( U& a
modsecurity on; m9 C. }7 [0 l0 C) `2 N
modsecurity_rules_file modsec_includes.conf;
" t0 Z' f& H N$ i root html;
/ C/ B. V( f% r8 f) w. f. B index index.html index.htm;
( h [" F8 G5 s9 i' @. ?& P! q}8 n6 _) |. h' Y$ A* r* @" }
}, r- z1 P' \. b* I$ p
验证Nginx配置文件
" w h; n, {8 o; _$ X$ /usr/local/nginx/sbin/nginx -t/ N% g4 l) J; Q. E
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
7 M- ~6 p4 F! l. L. ^( `nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful0 `. T6 s1 Q% B
启动Nginx2 R& Z/ B4 I+ E* B% _( @6 {! c
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" G; s( E3 t2 G8 `
* j) r0 ? p0 G" R7 _* }3 J测试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能够战胜更多复杂/未知/混淆的攻击模式。 0 s' e7 a2 y* n: z
|