ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
2 A/ ~: X4 ?2 v. R T- ` {( {9 Y% }* B
' ?1 W4 E' E- b l在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
6 h. _4 Y E( h! @- {7 i! S0 \* t6 i# Z v) v; X3 @ X6 I, B8 j
) f, _( \' D( L1 e什么是ModSecurity# f3 @! V! C: [* W& Y0 }' F
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。& }2 q$ i& ~7 ]* ?* X
/ n" K8 i5 L% o& c: S( e1 C7 d& S W
, a9 M' Q1 ?3 ^ i6 n4 n, U& k4 T
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。+ W; T( L9 X9 U) K3 ^) K
1 L5 }7 f# K/ J3 H( [ J3 f5 ~5 ]: ]# y3 q8 R2 g( J4 M
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。2 z# `' N" ~8 P: {" W1 I& o
, N- q" _( A' @* C* G) O
: o9 q( z) T0 k; v9 `7 S. E, k1 f x
官网: https://www.modsecurity.org/
1 w) v- q1 a K9 F2 d$ n; G/ k7 y9 e& y- f4 |5 W. }" x
8 z4 B- j/ e. M( ~( M4 Z% s; O
什么是OWASP CRS
. Y, h S! z% UOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。1 J9 S& n, K7 P4 X( c1 r
2 Z5 \0 P m" {; u2 q, h4 r& R8 h, j& ~: F
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
/ P/ E, U* ^/ U* a( y5 M; @, A8 n$ P- \3 J$ c! y
; n. r6 m/ ^. m6 ]( E% k: _" v7 YHTTP Protection(HTTP防御)
1 {0 D: I! n3 q+ R8 P7 ~HTTP协议和本地定义使用的detectsviolations策略。
4 V3 `- A! ]0 } B+ o
0 u0 n1 `3 V- L) ~% z5 x$ z, x* u+ Y8 E
Real-time Blacklist Lookups(实时黑名单查询)
, ?) ^5 z8 I- K9 K7 v( z利用第三方IP名单。 z% g" w( ]* Y+ ~0 c4 W r
: N! s1 B& P; S: j4 c4 G) u+ W7 I) d
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
) c$ h% P1 N6 F1 x& c防御HTTP的洪水攻击和HTTP Dos攻击。
/ m( r) I o' H/ z4 O
5 Q! a! s6 i+ s- Y9 ^ q) \, o# m9 ^6 c# B, l
Common Web Attacks Protection(常见的Web攻击防护)
) Q# s4 K8 M1 Z9 G$ V9 f- K3 |4 r检测常见的Web应用程序的安全攻击。
9 H" \! w6 V- S0 `0 W7 ~/ n
% @3 _/ i- a- a& q4 T' k0 q7 X
) P/ k/ T9 P4 Z3 e, H1 F' d I: nAutomation Detection(自动化检测)" x+ L7 C% M* H8 f: g
检测机器人,爬虫,扫描仪和其他表面恶意活动。5 Z: v- Y2 q' ^; ~5 c% M
& J; S. D7 q" Q8 x5 U9 l2 g1 ]/ s9 s+ F
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)1 A- e9 ~5 t8 W# C1 g
检测通过Web应用程序上传的恶意文件。
; s d) r1 a* H1 q+ }5 g _0 O
, {; s5 Y2 s8 y' ]4 W6 f2 k6 R& f, D8 O, [$ h! d
Tracking Sensitive Data(跟踪敏感数据)2 \' M9 N9 A$ l" _! A
信用卡通道的使用,并阻止泄漏。# C2 |2 s/ o$ p2 j
+ @$ k P3 L: s6 z e
, @$ {4 A9 z- ~Trojan Protection(木马防护)0 L5 _" L, N9 q1 `
检测访问木马。" k& H5 a* O& y9 D. J4 U2 C
m' C% _7 S6 @- I( R( M: j. j1 d( V7 T" {! l4 ~
Identification of Application Defects(应用程序缺陷的鉴定)) h7 p! v. Q7 w* E% e' S- [$ t! }4 W
检测应用程序的错误配置警报。
$ n, D7 I7 ^: S) T. Q& B$ w( ?: c( ]) d5 `4 f
* ^+ B$ X `. i0 {4 aError Detection and Hiding(错误检测和隐藏)' f9 Q( ~: S: r3 i- a
检测伪装服务器发送错误消息。
# O+ w2 v* |! X* A" v7 C. W( N2 i# T/ U8 \
- G. ^* S& V7 [. @& [( w+ n! q1 M安装ModSecurity
3 a; N. r" I: @9 b2 k软件基础环境准备: n) X, O8 D4 a b# }/ a
下载对应软件包% a# q& J9 Q& J; M9 k! U
$ cd /root* _0 Z" n. O; [; k
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
6 ?- i3 T2 r A1 x8 g$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz, N5 C" v# ? j# y; t
安装Nginx和ModSecurity依赖包; H: d* f0 ~; y
Centos/RHEL9 M2 p' B4 F" |8 {2 V% z
+ l3 G! G' ~6 w! I/ B2 r) r1 N
4 A) @" h) m# {0 o
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel- Q' ~, }# }5 B
Ubuntu/Debian
* b1 h! P% T( q* v9 V3 i8 o$ ~
0 K; P/ p: F! T* J- j/ f
3 g) G% Q1 x. w7 {& |0 j9 ?$ 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
+ `% M% \6 L/ ^% n( b$ e1 U2 d编译安装ModSecurity8 E% A Y5 L- ? k( `! [
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
/ M' q: r! s9 ^! a( E$ l" z: c
7 M" d$ y" K3 t( n. r l
3 K( j$ t2 } H( R& N( l方法一:编译为Nginx静态模块9 `4 T( o" L: x, u: X/ ?6 c
6 A$ C. I* T' S/ H. ~1 w2 \
0 I/ w8 a* l# ]* V7 I0 X* `" T编译为独立模块(modsecurity-2.9.1)# i5 [. _3 j7 W
$ tar xzvf modsecurity-2.9.1.tar.gz
' j6 @7 C8 L/ J- k$ cd modsecurity-2.9.1/
; l9 L( b/ h9 _0 l4 m. p$ ./autogen.sh
- s& k( ^) E0 k! B6 | h$ ./configure --enable-standalone-module --disable-mlogc& [" z; K% ~- H$ A& A% T
$ make
4 H+ w( p$ N8 `. ]9 t3 Y编译安装Nginx并添加ModSecurity模块
# e' g, g& }% _* ?$ tar xzvf nginx-1.9.2.tar.gz3 u& r* E Q# E% @1 \5 l- b
$ cd nginx-1.9.2, }( _: O6 e0 H$ v" `( \) I
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
. v9 G" I3 i3 f- O& {7 j$ make && make install; c* e+ T* I/ f: H! X0 ^
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
, p" _4 d- T. A
# | w8 Q: [0 h8 Q' {" ^
3 A3 m' R o7 D; |编译LibModSecurity(modsecurity-3.0)
: J- J* y" c7 u8 f$ cd /root
( j" | l5 M! U6 \6 f4 |$ git clone https://github.com/SpiderLabs/ModSecurity
* z5 U) @/ |$ v9 t, I1 B1 v$ cd ModSecurity; ^4 G+ d& C. | Q9 _3 J# c
$ git checkout -b v3/master origin/v3/master/ u# ~% y, c% b! t% W; w
$ sh build.sh/ t1 w3 i; k/ z, U* i
$ git submodule init3 g* N) T8 E; w1 }7 H; K% K7 t
$ git submodule update
. ]9 A/ g. K7 N$ u2 h9 W$ ./configure# g' @/ }3 @" [8 ^8 |4 Z
$ make
) B& h" [# t' ^1 I7 u9 C$ make install$ @7 O. d, W, @& c: u) b N/ g
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。2 T% T8 r- M8 x4 ~7 l: W
4 h r( x, V: Z
% \2 n9 B1 n1 L5 Q7 r! a$ ls /usr/local/modsecurity/lib; t+ V2 J( \8 W
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.08 R: n& V2 X" H( ~: O
编译安装Nginx并添加ModSecurity-Nginx Connector模块
% [2 }" ]- c* n使用ModSecurity-Nginx模块来连接LibModSecurity
) f$ n7 C6 } E3 d+ j9 O" M! M1 ?! o, n$ q7 F9 |: J3 `
/ i/ v4 V0 {! K4 z$ cd /root6 O) V4 U/ c$ j
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx4 W- @* d" ]/ d% W
$ tar xzvf nginx-1.9.2.tar.gz
* }% }: d% E8 u1 s4 w$ cd nginx-1.9.2
3 } _3 |* S$ h Y0 |3 d% u$ ./configure --add-module=/root/modsecurity-nginx
! q' a+ R# O4 L! {& }$ make" i0 l# A6 I- B( I
$ make && make install4 I4 Y9 q( s: b5 p
添加OWASP规则
9 l' l! E7 U( |, w1 f9 mModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
8 [% y, u% {8 s) ]1 D h; N% j* r& n7 W
7 @% ?! H1 {' p, s' ]1 e下载OWASP规则并生成配置文件: a1 h" ]4 s j0 N# `2 }+ t
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
2 b' ~) o: `/ z; j) S$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/
6 e7 t( K) }) |$ cd /usr/local/nginx/conf/owasp-modsecurity-crs2 W, [4 r. q- i2 ~
$ cp crs-setup.conf.example crs-setup.conf
! |; d9 y% G( D5 r! ` O }配置OWASP规则
' v6 A# \2 n, x, G/ b编辑crs-setup.conf文件
$ r& A7 H) e/ u; `# }6 e
# u- ^, ~4 A X1 F( z7 w4 K$ H- ^1 b: W/ D( D3 s0 Y# n" r
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf/ r' u, I* E# p4 ?# E
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf* \: F: q, l# e% L! {! a: {2 h5 Z6 F
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
% V' l$ @0 g& j- Q0 N( I# t" x1 B$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
* l$ F+ |1 x" X5 C' D- E! Q默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
8 L O6 o0 z" z7 c$ B1 g$ o5 E0 p: [
. t& y+ W+ n4 |; o
启用ModSecurity模块和CRS规则
# ~( f [) z. D2 K' f7 H8 D3 d/ r1 B复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。# x8 Y k' r) @7 [/ J! U
~* k e: A4 Q, z" z% M* Q' S; s5 f" E" B- l: D
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
4 o0 h) Z( x4 R- m
0 d* }) e2 ?9 f, Q' z
) x9 W# D7 n2 W. t7 h- s" W$ cd /root/modsecurity-2.9.1/: _' r8 M4 u& T9 V
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
2 h" A7 X: b7 C6 {6 }$ cp unicode.mapping /usr/local/nginx/conf/! G5 | h( w" F, ]1 n4 W
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。6 t: o1 p1 ]4 Z' I% |/ l8 m' | p
0 o0 i! T; a7 u! w+ B c) F
E6 ~7 W* v4 b0 {. y& d- i$ vim /usr/local/nginx/conf/modsecurity.conf, l3 A! `% X& `4 a
SecRuleEngine On
* I P2 F G: z" l7 ]. J/ Q7 h" fModSecurity中几个常用配置说明:
4 ^1 Q9 a" e: p, a; a% D* w* A2 D% `: W3 U: n& \
( k$ q+ U# q3 u: A4 O
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。+ P' E% `7 h$ k* C& K$ J
( j/ n" C& a7 H7 T* f
. I+ [, J* d. `* R. g0 W
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
) E6 J$ H! }0 F$ @
* ]; L! t/ e9 ^( G& g3 P. ~
; t* X$ K* w9 Q" b3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
+ T. _* e5 @ n+ }3 }5 ?( }
( n L4 N$ r2 u8 c
/ x8 G4 Z! Z4 x ?0 t( C4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
$ Q$ A4 U+ i; V& |, Z
* w" V8 S7 n. Z% ?
4 u: {6 q+ x2 ~% g, U在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。- ^" H4 n+ L$ [0 c
' ?* D0 V9 Z( }
+ i# C" x! ^" e) C1 @2 j! _
3.x版本CRS2 E, T7 ^1 Z7 \: h$ w4 c h
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
: h9 ^; q9 U; [- o+ B# 生成例外排除请求的配置文件" P6 F/ ]5 J" H. x
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf |4 E! D8 u. E
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf$ F* B% Q$ W- v& ?$ o: L
$ cp rules/*.data /usr/local/nginx/conf
6 \ s6 C5 f m, `为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
1 w3 v2 h, ?. j* C1 ? R
; N, o; V+ d/ m0 b3 o: }/ ]$ K- z& r- m7 o6 R
$ vim /usr/local/nginx/conf/modsec_includes.conf
+ o: a6 L/ u% l# q
0 }% a `: a1 G: V5 E) P v3 _& `[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
# p( I! U+ n4 c* X4 u* X0 x" \" A) L, R) L3 C; S
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。1 _/ t5 ?+ p3 k+ u! |0 `+ \
$ B& r- u! v5 A! I) R y$ [9 N# P' Z5 E* g0 g2 ]1 T
配置Nginx支持Modsecurity6 Y2 B4 \1 c& H$ q: s- p5 }0 p
启用Modsecurity
# h9 A0 R* a- X% n! g使用静态模块加载的配置方法
. l! ~. t8 R- H3 N- C# U, k在需要启用Modsecurity的主机的location下面加入下面两行即可:' W: i/ C' B' w t
x7 @ m6 `8 ~' Z6 P
+ v6 T i- L) gModSecurityEnabled on;; c4 Q; S1 y, t/ k# m
ModSecurityConfig modsec_includes.conf;2 y4 G. W5 F9 O% v3 {0 f
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。, d% r6 i/ S4 r" j M
6 X. f [# k. o
7 I6 U' }% C3 |, @0 f& X$ vim /usr/local/nginx/conf/nginx.conf
5 {4 M F- \* M( `0 g( e5 C
7 Y& g3 K+ ?6 I" X; Y b% ~$ n$ e( [% b
server {
) G. k: }0 O! g z$ c, ~6 U listen 80;* W/ P+ ~! {9 n, V
server_name example.com;% s9 J- Q& g% J; D1 l3 z
9 A8 Y0 z8 @' c( h( g6 H+ n& O
7 q9 B. n* Z# u9 T location / {
( O$ d) {: @1 O- p: T0 T( p ModSecurityEnabled on;! D( o' G2 m+ s) M1 V3 S6 b5 x) {
ModSecurityConfig modsec_includes.conf;0 p. e9 { n! B4 p* O
root html;
: l) V; E8 @9 R" T4 Q# [9 O index index.html index.htm;
e! ?! P; `+ t) t% s$ _% p }
& X5 Q; r% A# U, g1 Q, H}
3 H+ m5 I( q* w9 ?2 V! l使用动态模块加载的配置方法6 f8 d7 R' Q# w3 N0 r5 L
在需要启用Modsecurity的主机的location下面加入下面两行即可:
( c3 E- K7 {1 E- d& A W9 |7 R' I, Q5 q4 D- L
9 q" G* r$ b! w, J1 B* K& @modsecurity on;" `: m, @' i8 }* h0 R2 U& C* z
modsecurity_rules_file modsec_includes.conf;
9 Q! A$ L! U" R9 j. D修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。0 `# ^+ ?/ @8 u
8 X) c" T7 q$ Z. G r
& E2 Y4 F* B- p) B, H8 q$ vim /usr/local/nginx/conf/nginx.conf
/ e- V7 {' B3 X6 e$ {) S1 y
; m" i0 J7 R; j2 ]2 I! |
6 U$ D% n0 [4 ~0 t, `server {; [. g, A' X0 _8 s4 S
listen 80;
6 n' A) l( u6 E/ S, U7 } server_name localhost mike.hi-linux.com;4 E. U& m$ t- E0 m
access_log /var/log/nginx/yourdomain.log;
7 y. s! H; ^3 d, c4 d; N/ F. i1 N |7 }' [5 [9 i0 E- X' P
# d N: g9 P" G1 O
location / {: y$ T0 o; j; B
: u6 I( b. N z( @3 l
7 N/ R7 }: j; P% K modsecurity on;! x+ u4 ^* ?% u" p% R
modsecurity_rules_file modsec_includes.conf;# I% L0 E; n" V' K( n* @
root html;8 O: A- g" j- A
index index.html index.htm;+ I* A; |3 ?, O
}
T1 C! w- n2 R# V. u; Y0 x& S}
* E( F) `6 t, w m$ v* T- u# J, Q6 E验证Nginx配置文件/ W9 T5 r5 n; {8 }! ]4 ]; x
$ /usr/local/nginx/sbin/nginx -t* ]. N4 d0 d# R, ~) W
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok" g' D$ Y- j4 l6 Q
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
/ H3 |& U+ d0 Z/ d" p! o6 u3 A: V0 }7 }启动Nginx- B* ~! H" |2 J/ A0 G# b8 y E# K& ?7 g
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf' G0 z9 m6 }+ a, s0 e( X) G" e
- y1 z; N$ d% r/ E1 B+ U2 y
测试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能够战胜更多复杂/未知/混淆的攻击模式。
. n" ^2 R( B2 w5 f |