ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。3 n0 w; K" T' P: M
6 X( a- H& ~9 D, x8 l& ~- J# F2 F \
& D4 \$ D. a$ K* L在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
, I, k% x7 S+ X0 m
2 b2 s. g% d6 n2 o. ?8 m
) o4 L6 s- R% }( T. U6 j' P什么是ModSecurity4 D! D$ d" N2 N- k) d
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
' t% T4 S* o" U( H& f& c+ ]
8 \: x2 c6 V- i9 s7 g7 g! v+ P' j$ d) Z3 Q
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。# v* G6 |, d8 M1 a; ~' N
! d+ e$ a+ a6 B0 l& Q+ Z1 r
" Q$ ]1 ^4 m( rModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
% b. i# w4 k) A- T2 ?7 E$ I( ?4 q: [6 B+ ~2 r! }4 b
# f) e: v) _9 Y$ e官网: https://www.modsecurity.org/% r; r; g! @- J
* v3 A% C/ l. h$ m% o$ T" r
4 \2 S' w' N% ~" Q1 Y) G) e" |
什么是OWASP CRS! k+ a: a- e) L1 L7 ?6 p
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
4 p$ g$ f6 W" s9 W3 ?& q, M* C* j
$ `# ~/ a1 E1 |5 X5 i6 e* }
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。, x/ E! S7 G; c
( |+ C2 Y5 O6 w2 `( a; n
; v3 r& ?6 q- F8 O
HTTP Protection(HTTP防御)
4 l- S/ P/ O6 T3 ]# d pHTTP协议和本地定义使用的detectsviolations策略。
" n* P( a% l" h
( T6 h6 ] \# B5 K. y. g# q$ ~. A+ P) a* J6 ]! `3 o
Real-time Blacklist Lookups(实时黑名单查询)
( F9 {- e2 {6 _8 y& [7 y, W利用第三方IP名单。- @6 p0 @* ~7 |$ J( d- _4 R
' G! ?, R* m/ I; m' P, I' o6 k: h. E4 [9 Z3 V$ J
HTTP Denial of Service Protections(HTTP的拒绝服务保护)% |2 k# n; V6 b8 n5 f+ u/ s& {, m
防御HTTP的洪水攻击和HTTP Dos攻击。1 B9 B0 Y8 R% A
" Q# M4 A) ~4 Z' g; H
3 |& m# z% `$ aCommon Web Attacks Protection(常见的Web攻击防护)# p9 Q; r8 j, Y
检测常见的Web应用程序的安全攻击。) Y3 u1 J N- f; w
, b7 u) G$ y) z4 o( Z
` W; L, i9 B. z! Y& F5 u7 a' ?Automation Detection(自动化检测)
S+ K4 f) v2 w; z2 ]检测机器人,爬虫,扫描仪和其他表面恶意活动。7 a1 j H2 y9 B4 J; R/ V
% z C/ V8 j4 }. l% Q. o8 B
@+ e* c1 C1 [& O! ^1 _1 o
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)9 ]5 `) Z; K! U, Z" {
检测通过Web应用程序上传的恶意文件。# V- [) w4 [1 c" T
! p1 P( |1 p7 m
7 J- _ e2 V' qTracking Sensitive Data(跟踪敏感数据), i* e4 u0 d* ~9 E0 D3 |
信用卡通道的使用,并阻止泄漏。; [" x6 T" W4 S* e5 h$ J
3 a+ a' M8 C+ j7 H( H7 m/ t/ P8 U! a4 t7 w; f' r0 Y
Trojan Protection(木马防护)
# ]4 _% G' ?2 @; H) | o检测访问木马。' h6 m+ H7 c: l& S: a$ s
9 w) @" G$ f V( ^
( A* @/ B6 ?# B/ i( fIdentification of Application Defects(应用程序缺陷的鉴定)! \0 S: L1 D$ w' v. P
检测应用程序的错误配置警报。
) E. E: j# o5 V a7 E
4 C. t0 }9 T# X3 T2 H% v. Y% b& y& t' n% F( m
Error Detection and Hiding(错误检测和隐藏)( ]% r3 c3 A) [
检测伪装服务器发送错误消息。
' q8 `1 \ n8 {$ b
/ p$ I5 P" O& E* B( U# {8 a- d, u8 X, _, k! ^( ^
安装ModSecurity
5 _% r' b& F, O$ h: s' t) V6 ]# a软件基础环境准备' g: `9 O! H: P B- s0 I* v+ c* q
下载对应软件包
& z+ k0 ?$ {: M; \, _/ g3 S/ W$ cd /root
9 X3 U0 o) ^2 L6 v/ ?" D0 n# [* S$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'2 g* v+ ]# ]2 E e+ H$ k7 R l
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz- h7 `! I" F9 H8 S
安装Nginx和ModSecurity依赖包0 b* B% _' |6 r4 l( U: C' [
Centos/RHEL
% F& Y/ V# z {6 s' P# x5 O& c. ?
. k- {; v3 E; C
, _- g1 W+ l$ N; b7 K$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel! Q9 ]4 _4 ~- Z( _: F/ I; C/ A/ _/ `
Ubuntu/Debian
. q! A5 N. _ v% T% @0 U% j' X9 K9 ]- b: M
; c' W+ a% w- g- P$ 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
- A5 L1 h c2 D1 o* c编译安装ModSecurity
6 e5 K& f z( ~Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。* g% Q5 p7 W i5 k+ a. g
$ J+ R% x0 u- M7 d
( O8 d: u( Z/ Y; D; E" H7 O方法一:编译为Nginx静态模块
3 ^# \9 x6 a. N2 _
2 i8 o q4 D/ M; u! l
) [8 V9 e& @7 s0 Y O' G, u编译为独立模块(modsecurity-2.9.1)# t N( G0 v9 x0 o9 Q6 ~2 M
$ tar xzvf modsecurity-2.9.1.tar.gz
4 n$ [& f8 s. s$ cd modsecurity-2.9.1/, y0 C- `8 }5 |1 d& c% m% T* m
$ ./autogen.sh
5 O; q5 f! Z, _$ ./configure --enable-standalone-module --disable-mlogc7 W K" C8 Q, N
$ make X8 l/ i* C, {% @% x: p
编译安装Nginx并添加ModSecurity模块
5 _& e P: E1 D$ tar xzvf nginx-1.9.2.tar.gz' N' P) W9 [9 K0 w7 H" \
$ cd nginx-1.9.2
$ Y: P R, J X: O$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
, j1 f* z& {* M: ^2 g$ make && make install8 T+ Q! }( j: B# B3 H' y
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
' ^ Q: u) x3 t$ Q# y# A8 s' \8 L' O( h7 I& L& q& `3 `
5 n( {9 e8 z1 p$ l. [2 l( A
编译LibModSecurity(modsecurity-3.0)
* G7 O$ P0 j( \( `% X/ }$ cd /root
/ N2 B6 V) l$ I# w4 D$ git clone https://github.com/SpiderLabs/ModSecurity% `* ]- f n9 {( [
$ cd ModSecurity m+ s5 S6 p& M
$ git checkout -b v3/master origin/v3/master
9 n) \% o9 t9 m# e4 `$ sh build.sh- y+ D$ E' I, w+ r: t# x* V
$ git submodule init: v; i4 S/ M# C" H
$ git submodule update
, @! u0 A0 I3 Y$ ./configure
+ ]4 }! P3 @+ [0 x R$ make
6 J0 K& }9 Y! |* e* l$ make install; w# H; _/ s& O) Z6 w
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
: p' f; c7 s+ X6 ?0 t* n" r- }3 s# a2 I9 w8 Y \/ {
5 D4 Y$ X ~* g+ A5 u' M$ ls /usr/local/modsecurity/lib# C# ^: b- F9 |0 a5 r) Y
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0& g4 F4 Z# W; a: L/ V/ z( Q
编译安装Nginx并添加ModSecurity-Nginx Connector模块
0 r" i6 o5 @, N% D: a% l使用ModSecurity-Nginx模块来连接LibModSecurity! @8 E' b. F2 n9 w7 l' v+ D$ L* e8 J
: j* V( a1 \$ Z. G: O2 N
, k0 c: s6 U: N# h5 p$ cd /root6 R0 a/ P ^6 \# J$ \: @. Q* J
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx" g7 S3 V B$ q) x8 p& J" e
$ tar xzvf nginx-1.9.2.tar.gz
' y7 H" A0 c W* E/ U/ v8 M$ cd nginx-1.9.2- ]3 d) |/ H0 e! z$ H
$ ./configure --add-module=/root/modsecurity-nginx# [; Y! Z5 S+ m$ O4 ?( w8 f; @/ U
$ make/ U. f' B# {6 L w6 T3 c4 B) z* P* z$ S
$ make && make install
* H4 _' T' G' q7 _' D# R: Q! w添加OWASP规则# z) V# z: g8 R& K/ ` I) a2 [
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
4 u. o2 X: W: r8 a
+ @" O; B- }# i: F* S; C+ P' t* |! }
下载OWASP规则并生成配置文件
! t0 ^) N4 i1 N2 v$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
" S7 p# M* v8 G- z$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/) ^: s$ d( p# }+ f) _! \
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs" d. f3 T. v3 }# S, l- p
$ cp crs-setup.conf.example crs-setup.conf
^) G/ z' K1 B c# a配置OWASP规则6 Q6 ]* F1 j- M, }/ g3 i; l
编辑crs-setup.conf文件 h {/ s7 U- N, ]% R1 z: T! ^
$ x! X, U. _" j' Z+ [. c# c9 P& a' x8 P3 i1 O$ E! g
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf1 s* c1 [& H( X. x
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
# K) p6 ^9 j- l9 `0 {$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf# C* _. E' X8 z- ]/ G1 \4 t
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf' `! [" e N) k8 U
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。! Z/ W; Y: Z. n) @9 P" \3 ]2 D
7 c( K: U5 |3 j
' ~ L8 c' c; W4 \* L) C$ F# Y启用ModSecurity模块和CRS规则) v( u" ]( N! S5 h8 G9 ?
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。7 [) L% e( q3 _- Z) d) P4 {
) ~/ t3 M; p# F) @' k% ^/ P2 L8 J6 u/ c- h
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。; C, Y* \3 r2 F2 ]) n8 r. _! m4 F
7 a. L$ \' Y' Y% e
2 f" A* U2 x, X$ {# @$ cd /root/modsecurity-2.9.1/2 P6 U( a' u. y X# D
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
6 k7 c3 q6 X3 [7 s$ cp unicode.mapping /usr/local/nginx/conf/9 {9 U$ v3 {, R$ w+ {
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
6 }/ g7 S( b4 s' U7 R z
+ I8 ~4 a: c( ^/ W0 i# v8 ]4 M
# M$ ^9 A) n% f$ vim /usr/local/nginx/conf/modsecurity.conf
% u7 n& x% `$ H6 g# s- x& WSecRuleEngine On
" m1 q" o; e* `" wModSecurity中几个常用配置说明:
7 [5 t% e7 S5 v7 s. g2 D, q' H
+ ], q% C5 e) ^8 D- U0 Z2 W: O0 c$ r U& v" P5 m* Y/ c n
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
' E2 g4 I* o# U! g* |
5 R- @/ j* L% l* b4 c
7 L' E V+ g7 j/ |$ B& |! h: m2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
8 {* i) H% K8 W) l2 q2 q4 D9 L! G, i3 [8 y7 O8 n
+ Y2 x9 e; G' R4 d+ s, h8 V
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
% @! a$ n0 N' q% u! G; ~# c |+ W# ?. l8 C
7 | y- w" d; A- w3 j
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。/ h. @2 b! b+ z! M
# }) {8 t: r- E) h, t3 |6 |# D
1 n0 A% e' \/ P3 m, X S% O
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。7 A7 b2 G" T. c" k; _
9 x& q6 r2 U F4 ]$ q- T
# _% x7 r# }8 T$ [3 ~/ |
3.x版本CRS/ K3 b+ d* d. b! O, u2 ~
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs( ]" E# e; C: Y# j/ t! o" ?; D
# 生成例外排除请求的配置文件4 r2 D: ?8 B6 ^* I
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
" T( j2 K( q) |( ]4 p" ?1 o+ B* m6 l$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
0 I, R8 o+ f" k$ cp rules/*.data /usr/local/nginx/conf( t( N' s! y( H0 o F1 S
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
0 V+ [8 J. {! m2 I8 f- U0 O: t6 O7 S' R) W; L: k) k" D* U
8 C7 Z) C# Y5 h9 l2 f
$ vim /usr/local/nginx/conf/modsec_includes.conf
5 W' s7 L( ?9 b- O% S- W4 f G; c: y( |- [! K5 p- w' A' g4 U 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
& {% O7 Y% p1 V. F# @1 A* q0 w R2 h" K1 y& t
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
+ a/ `6 }0 Y$ e! ^9 ~1 a. C0 B
, `7 g; \8 }- D3 E* G( e% o( t1 B' |5 S3 p( A7 ^5 Z) F# q
配置Nginx支持Modsecurity/ x" v6 h* o) h' C }# y9 E' X5 o
启用Modsecurity
8 Y5 p# L# G- ?; {, u使用静态模块加载的配置方法
$ E9 y0 q9 @' _0 C7 y在需要启用Modsecurity的主机的location下面加入下面两行即可:' _/ t/ o. i: j1 M- S# |
" M; s* t2 ?$ ^( J8 W
1 m% F6 ^/ J& m) w( ]
ModSecurityEnabled on;
$ E6 f% ?1 ^/ U {; lModSecurityConfig modsec_includes.conf;
2 ?& v1 M" O5 y* `修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
/ e! e3 K; Y' M7 H: d; ^) H1 v' \* N6 T# I% E- E1 y& C! j
x- r. O: u% L8 G7 x
$ vim /usr/local/nginx/conf/nginx.conf
$ f* g- Y' O9 Z* n _8 U3 r6 |8 J. J! N) x. E$ J/ w
- V) Z' Y$ I/ Q& G& X' iserver {% v9 o, m; V! s: u0 _+ i8 J
listen 80;
. ]: U1 F6 p3 G5 s0 t3 r server_name example.com;
1 E: {" f4 v! }# Y# u. f2 T; O
: y3 ]) a, N- @# @# ~8 d6 k& u$ _+ U0 l: |4 s: J
location / {) k! k1 V; o) Z$ V: ^, X/ U3 |
ModSecurityEnabled on;) }5 O( O9 N- h3 V5 d0 |& e
ModSecurityConfig modsec_includes.conf;4 z' n5 T7 L; L& h2 k
root html;
/ `# T7 u$ H# @7 X" H index index.html index.htm;
2 W' u- n* K7 ` I }
5 A/ n8 x+ @" g! @" L3 \% S' i}" |9 G9 R' f5 p% e+ u: O
使用动态模块加载的配置方法
: L0 Z1 a. P8 t. B3 F; g1 ]在需要启用Modsecurity的主机的location下面加入下面两行即可:1 f4 p- e, K4 t$ X: J# u8 W
2 J, }# ?, l! T& G" ~: Q& A4 i% X- I8 w9 Z
modsecurity on;
p8 C, }, K! r2 u$ l& Bmodsecurity_rules_file modsec_includes.conf;
- o( O8 x% K( b! g修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。5 `. z f! s6 l1 C6 O+ [$ Y. I" n3 W
( u4 S% @! a8 H, E/ Y0 {7 R s2 ^. x/ n7 M+ o' e/ S) g E
$ vim /usr/local/nginx/conf/nginx.conf
) d. e6 P9 V& |6 G% n; \" G' @# y& d" T/ u6 }5 V
2 h2 K$ N: p' H! d% t$ G( L7 Fserver {
+ x2 _1 w: }7 \3 m# h listen 80;! v" V7 ?& }; U4 ^6 a
server_name localhost mike.hi-linux.com;# d% x+ Q$ Y/ G8 r5 u5 m' S
access_log /var/log/nginx/yourdomain.log;% c) u0 K, [; D0 h& A4 A$ M9 z
! l. A% @( `' j) ~: T5 P: Y# b; {/ H- C) u) l2 d
location / {
3 Q: s5 x0 v9 A! \: ^% L3 ~" M% }# V H3 F. n% {
+ D: k6 V2 Y$ k6 L {# a modsecurity on;
- |+ j0 F6 n7 W/ _: b% ]7 i modsecurity_rules_file modsec_includes.conf;
, p( H7 L3 Q8 m" O9 B: ~+ u& Z, B$ H root html;
" ~. v" {# }# P; O8 d5 d, V" y index index.html index.htm;
& `! e! t4 f8 s: J' i}. h$ N) p U1 i$ r9 K+ U* `$ `
}
9 ?- H( B) Z! }) f9 y验证Nginx配置文件- D- U) w! t$ B2 `- _$ S/ C
$ /usr/local/nginx/sbin/nginx -t
7 \( D' P, S+ y* s. X- o5 p, E! Fnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok& r2 N2 ?8 }. }6 Q
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
q) i+ Z3 e8 o7 g* g8 N启动Nginx
; S4 o) n. c$ e$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf v& V6 s. j0 M2 `' V, |
# i' B+ I. I+ `8 ~( i) T
测试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能够战胜更多复杂/未知/混淆的攻击模式。 + X; a$ L4 J! l$ Q% z
|