ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。& D. V& h% s& n9 x
8 V, ~! p# Z% a
3 O" l6 b9 z/ }在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
6 x3 N* m' _2 D# v- y, n
' G; p5 } o5 H. Z$ ~1 }
% n( \2 s. X4 M" Q* \1 T) c什么是ModSecurity8 T& k) w) c B5 q& ?
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
3 R; [) s% Y3 |7 @( U) p! Y/ L- | ]: W( u: M) O
) n3 x; J- w+ D" ~8 W) s& TModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。; k) ]" r; n1 T/ N- y# V
I& o7 e' H' ~! k
4 ~' T/ }1 x" T/ U: V, ZModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。% z) {# ?( b0 ^7 j; j
, _. H& z6 ~- k) w
1 s; z( X2 a0 x3 F8 F/ y$ ~% I官网: https://www.modsecurity.org// T o/ m, S9 \) D( @9 b% u* ~
V3 d2 y7 K$ e- r! w/ \
3 Q! \" M/ a, |6 T$ ?/ \
什么是OWASP CRS* s2 T# o6 v0 M" `! Y# ]( u" g
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
* R* `5 r9 s3 ^# Z3 Z5 J- u7 s% K( K+ o1 c
+ l$ |6 b; k. `# T+ a1 {ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
, A6 q5 B. [; S* J0 V7 t2 N
/ C' ^+ S1 H6 ]! o* k* x# G$ o9 w2 }
HTTP Protection(HTTP防御)
2 b- d% C, X4 SHTTP协议和本地定义使用的detectsviolations策略。% p# |% O# N9 Q, ^' ?
( t, H( o' _- w
# t8 p: X1 d4 N0 E
Real-time Blacklist Lookups(实时黑名单查询)
4 ?. q; a) N6 r4 G4 N1 s利用第三方IP名单。: U% W; ?9 j& l5 C5 Z
2 E2 i3 s A ^& s1 S+ f2 e, r
$ U0 e5 b! g# FHTTP Denial of Service Protections(HTTP的拒绝服务保护)
3 X- k1 N- G: w0 r% q防御HTTP的洪水攻击和HTTP Dos攻击。3 {4 G0 G8 v" J7 m2 ?1 r4 O
9 O/ D( r# j) b0 i7 t
5 F+ e% P1 Z p. Y- |Common Web Attacks Protection(常见的Web攻击防护)
9 i' y/ l5 t9 Z$ y7 w检测常见的Web应用程序的安全攻击。0 s/ V0 d" i+ s: B
. v5 d; `3 i9 W1 F: b- _* ?% O1 o
( K* W: @$ P; l! k, B# o
Automation Detection(自动化检测)
0 ` F: @) r- d4 i4 K2 U检测机器人,爬虫,扫描仪和其他表面恶意活动。/ L& ?) t+ e! ^: T& e' z
5 _6 j8 _( j c/ ?( r3 D2 ^- @8 m
$ R9 D! [) x! N- j# O7 o: H
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
: a% T9 z% H* `& B! o' ?+ E. L; o- E; N检测通过Web应用程序上传的恶意文件。
1 _! R9 a4 c7 [; V5 S' q7 D: L; ]+ f k" L3 y3 R: e
8 B* M- I8 I2 h( ?4 z
Tracking Sensitive Data(跟踪敏感数据)7 F8 V: F! h& Z
信用卡通道的使用,并阻止泄漏。
8 u3 v2 \2 [0 s9 J' r/ D% q
% V$ R. E. ~ k$ H: w* z
, P g0 d- q6 x% Q# m0 y/ _ dTrojan Protection(木马防护)
9 d9 }, u6 ?( c: {检测访问木马。. f, v1 T8 ~( g6 g1 G/ ~- i
& c2 r* \7 _7 j+ ?; H. V
! w% f" f% L# r: y
Identification of Application Defects(应用程序缺陷的鉴定); x4 m; O, @/ i' k- Q3 o' F
检测应用程序的错误配置警报。# h% R6 b+ u, y
& _5 f% Z3 d' g( D, x" d7 J A$ R) L% p
Error Detection and Hiding(错误检测和隐藏)4 w9 e% ?: N+ ?0 F2 l
检测伪装服务器发送错误消息。
x& A* g {2 v8 l
" Z' A7 L9 G. D# ? K' o' T" y* ` k8 J9 M' Y$ V- `& e9 ~
安装ModSecurity' U) ], c' o5 S2 r) Y
软件基础环境准备2 ?: w) C% Z- M
下载对应软件包$ ^7 R: g) Q3 R6 H, u2 J
$ cd /root1 d, v$ g4 P2 t0 I; H- Q' H) ?
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
, |, L3 ?( d2 w% F7 t$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz: K7 U1 p4 `3 S5 l" V" t( r
安装Nginx和ModSecurity依赖包
, Q& g7 t7 i7 e$ ECentos/RHEL
2 ]# I) k6 w3 I3 K1 g; A& s' ]; b, M
; S' A2 f- c; J8 x
$ yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel& z( I2 ]& S- u/ L
Ubuntu/Debian
& t% q8 W3 c8 T4 w
# `# o) ]5 B8 G) a) N* L- `" [8 v: z X: x' z* m
$ 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' |4 n3 R% L, h; @' D8 I# d
编译安装ModSecurity
% z8 n- V# Z$ H8 v3 ~* }Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
* R$ v$ g2 d* n$ y ?( V: C, T' L, g$ \* G' U, z$ \
7 t2 _* f |/ o) w) O+ @0 t
方法一:编译为Nginx静态模块
6 C, b' w& U! e2 ?( Z- m3 m
& z/ [ k O7 G* a1 R: Y) n& ^- G8 a0 R/ R- ~" X5 o6 m
编译为独立模块(modsecurity-2.9.1)
9 B' m/ V2 ?4 x/ U0 c' ^ H2 O Z$ tar xzvf modsecurity-2.9.1.tar.gz! @, l, m0 y9 h/ e7 A& ^
$ cd modsecurity-2.9.1/- ^. p* ^0 m6 P2 g
$ ./autogen.sh5 w( T. ]. J# b4 l/ u1 w
$ ./configure --enable-standalone-module --disable-mlogc; S: ~( z6 J- ^5 i
$ make9 T; \# p+ A- g) ^ ]
编译安装Nginx并添加ModSecurity模块 H9 d" `4 J3 t n9 T% F5 c2 r
$ tar xzvf nginx-1.9.2.tar.gz
$ W9 r( \( ^7 `! c7 p2 f$ cd nginx-1.9.2
( T& X) d! h3 ^7 q" U2 C$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
" H2 h0 b+ Y3 O8 J0 F0 b/ k$ make && make install+ m! x3 @8 D9 u2 T3 L7 |: P$ t T
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
2 Y8 G. g5 O+ ]- M
# d2 `1 a2 \' d$ z! u: X% a' {. A( Y, W7 |1 |* l
编译LibModSecurity(modsecurity-3.0)
+ @: d+ }: c$ @3 G1 D* B$ cd /root
; l' m. ^4 M* D/ C/ s$ git clone https://github.com/SpiderLabs/ModSecurity
( B/ d1 A( t5 I: x$ cd ModSecurity
1 R& v* F5 L7 o5 Q$ git checkout -b v3/master origin/v3/master1 J8 j- q. x- r% Q- O
$ sh build.sh
( w: N; S+ E0 V) b; l. ?$ git submodule init
3 Z0 X& m( |' O: f4 P$ git submodule update$ a1 r: z8 |! {- h. M
$ ./configure- Y. F2 e% H" Q& v7 S: _; W' P3 e
$ make8 g+ T# u4 ~$ Q9 a5 ~
$ make install# I. Z2 b+ p7 x6 F6 |, a
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
! S+ R* _" Y1 F" I/ d
2 \$ B" d8 C2 N
$ d$ K: G, Q0 U8 q% C' z$ ls /usr/local/modsecurity/lib, E, J8 U' N6 Q. W# ]! e. T/ \
libmodsecurity.a libmodsecurity.la libmodsecurity.so libmodsecurity.so.3 libmodsecurity.so.3.0.0
5 E" V) U) f/ P( Y编译安装Nginx并添加ModSecurity-Nginx Connector模块
; {5 j/ e7 |# q9 D8 r; M使用ModSecurity-Nginx模块来连接LibModSecurity/ c+ |# p r9 ?4 p& q' g! q
5 q) [5 F2 j2 d- d$ M! V6 K) r3 O+ c" K# B
$ cd /root5 M( `& c7 V; Q4 J8 O3 d& W8 X3 C, o$ ^
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
, W+ M: P& w7 ~. P7 M6 R, n$ tar xzvf nginx-1.9.2.tar.gz# r% N l: Y1 h; |) a% i5 w2 k& w
$ cd nginx-1.9.2+ x$ Q* k4 v# W, |2 Q! u, @
$ ./configure --add-module=/root/modsecurity-nginx2 }9 a3 H- L/ y- ?1 l- V$ [$ i
$ make
* C6 C4 H) A2 w7 a8 v$ M& U) q$ make && make install$ Y5 {( t* P; _, s$ T
添加OWASP规则
. c; }2 i( I# A' d k9 Y6 K8 k/ PModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。$ V7 i, l8 [) g% ?# V5 d* y! [
' x+ L7 D( }- O# [) f! U1 v" h3 A' m' g- H
下载OWASP规则并生成配置文件
& l8 f9 |, d+ F, |$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git% X \+ \/ W# X4 z7 f6 X" s0 V# c
$ cp -rf owasp-modsecurity-crs /usr/local/nginx/conf/ H) O G+ s. o" b- f$ x
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
: R, O, Q1 x' I' M) d' m$ cp crs-setup.conf.example crs-setup.conf
5 b7 C" z" s& m配置OWASP规则% H$ @! a/ Y& Z6 r
编辑crs-setup.conf文件8 F1 Y# e! ?9 T: q+ ^! }4 K
5 S2 K( L X2 ?! a( X" Z
0 X# I* S+ ] V$ n1 M. L$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
2 e& [" H: N8 y$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf3 F+ k: f7 S& e( b& X/ @5 R
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf& s5 t% U; A* B3 A- z
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
) z- f7 x/ S( ^& k5 d6 V4 ~. S默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
0 b- [" q+ N4 n
4 Z3 e8 f4 M5 `: j9 m( f/ X0 [* F
, Y" J; T2 G8 x' l6 I: Y% u) Z- B0 [; `启用ModSecurity模块和CRS规则& s+ w8 Q3 ]& k. r$ V. Z
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
) `& R! {' I5 J" a
* ^$ M6 W8 I$ k6 U. d) E7 a) H. q( G7 u
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。! ~( E! W) j. n# @6 L9 c! M
7 T: b/ ]: S( k
$ W5 `9 w8 y: O9 n0 A
$ cd /root/modsecurity-2.9.1/
2 q! b1 \) a8 w0 m% r6 V$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf
' G3 Z1 Z$ v& i, _% P+ p' k$ cp unicode.mapping /usr/local/nginx/conf/
+ x- K- T, g# S: x$ A# A将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
" Q0 f. u7 D- \* @; d9 H+ i; b
( K& _ B9 c( e6 ]; M
. L4 N1 m* s( C/ u$ vim /usr/local/nginx/conf/modsecurity.conf. O% @0 x( C* w' ]7 [! o, B: y
SecRuleEngine On
0 J& l$ N7 K7 L% U8 y0 kModSecurity中几个常用配置说明:2 f7 H8 |+ `" \( r. v
2 v- e; f! E8 s
1 f/ D Q" y( L1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
% J+ U0 `9 [# R
2 s6 ~& F1 `; X% ^+ `1 \4 f0 E" m1 j7 H1 F1 t' C- J7 v
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
p3 l1 k* I" x0 |; q9 C( F/ p3 |: l
6 T9 r! c3 \% n" P* R9 Z P& x
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。( l. U# [: J' y3 I, R2 ?/ r
; G2 j7 k+ F3 p1 X9 U9 F! m% ~2 z, m& m) {0 |: F7 v
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。' m6 Z+ Z- N5 F3 }* f$ a
* B' `5 I7 `+ f, ~6 g) q1 D% [4 d5 [: \
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
/ v0 y1 D3 D6 X# _* Q6 ]/ }
1 O# x' K& O3 V% j ~3 W7 s2 q2 Y( P: b' b y
3.x版本CRS d$ m* C; K/ K- q0 |) W2 i
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
3 r! \ u) ^; i6 ~# 生成例外排除请求的配置文件% v; l& t/ i2 [7 c$ t) R) s0 |8 E
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf0 m2 R( n4 {9 }! J# I0 J! _. H
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf0 j% a6 l: Z( J, ?3 G8 P
$ cp rules/*.data /usr/local/nginx/conf4 e1 R! C: i9 D8 N
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。4 V% v& y& i; P& w8 _! W6 f" C0 S
- @& p4 s: k" ?3 O
0 G. v8 M( P. s9 Z8 b7 B$ vim /usr/local/nginx/conf/modsec_includes.conf
0 @, Z: V6 J9 ~9 M- A- h& q$ s+ | w0 k0 ^% x
[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 1 `. r- q; d4 J$ W- w
, l" W0 v( Q8 K" c- i6 W
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。* D$ Y& e8 l5 a
8 _6 o" W% Y. d+ I5 T& s' W
( B5 H& \) G1 \) J配置Nginx支持Modsecurity
% d/ V* c' n$ A. D4 q4 J启用Modsecurity
$ c$ [5 \3 T/ r3 f使用静态模块加载的配置方法, f7 ^, Q' I+ S r- {# o
在需要启用Modsecurity的主机的location下面加入下面两行即可:
1 {7 K, r; w: o( t! l5 [' d
. M5 Z# R9 l" m6 u$ y
$ q: ]5 Q5 W) b; jModSecurityEnabled on;
2 c4 s0 t' N0 w+ U5 j% w" _ModSecurityConfig modsec_includes.conf;! Y* u- G9 \( S/ O1 q& l8 @4 a
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
- ?* \) J( X) H/ ~5 _
5 ~8 X1 c: u$ ?" T
2 P U7 J/ _# g6 ~9 P, G$ vim /usr/local/nginx/conf/nginx.conf5 B- \2 S' p. u+ ^$ j
& L" A. g$ @: c" V
0 }& z7 M% w* S T3 Wserver {4 ^3 R% N; D ]2 ^! e. m5 V) o
listen 80;# [: e3 G7 r, T2 I" ?3 B
server_name example.com;
- g0 d' f4 o7 z( o5 c4 j. p3 l% e1 j P* H
$ K1 i' X: o7 v( x location / {
+ I5 i2 x; ?% g4 e, a$ ] ModSecurityEnabled on;) ^; F2 i6 W( S, I
ModSecurityConfig modsec_includes.conf;/ [, Z+ N: d3 k/ O4 E
root html;
) k$ Q K! h4 I! n$ l$ q index index.html index.htm;2 z/ O, [7 ~* x+ a) J) P; ]0 u
}
; v% c9 ]$ \# m5 Q# t, h7 {}" N* q; R* ?8 A y$ H, O3 J Y
使用动态模块加载的配置方法
- j: M) o1 A1 A/ W9 S. N1 \3 d4 w/ r在需要启用Modsecurity的主机的location下面加入下面两行即可:4 }! J0 d/ v# ?( P7 a- _" J
, J9 G+ }1 i( m1 n" p
. j# p7 Z+ @2 N7 h" V: C$ Nmodsecurity on;
: r* T9 z! |9 w X) E3 q' T( E4 r+ cmodsecurity_rules_file modsec_includes.conf;
" N f, w& Q7 ^; m修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。6 m+ \6 e# _+ H; o5 v
- K. d& O. F3 v1 v+ e* N; S
3 @. J7 Z; A7 v1 J: E' P2 x
$ vim /usr/local/nginx/conf/nginx.conf
9 G% b* G. G7 f0 s) {
0 q9 ^' T& W( q4 l7 m' Z, n: y( \0 q
server {$ b- k, u' ]8 a' C: k* u' u
listen 80;
2 {- A' d6 p) m" P' o server_name localhost mike.hi-linux.com;
7 u% [; G& u' `. Z access_log /var/log/nginx/yourdomain.log;
* A u: {7 g8 {! [+ A
: j( w; l% y0 Z3 G1 x2 `; \3 `7 J% B; P6 N S3 T5 V
location / {
0 R$ i9 {: U) E
3 v# q3 `# H. i- m- E7 [
! e* r( h, C6 ]( }; v modsecurity on;
6 x) D2 ]1 Z. X- l" u# u) _ modsecurity_rules_file modsec_includes.conf;
& G$ ]+ K! z2 a- U1 |+ j root html;- b4 y+ [4 v" @; E
index index.html index.htm;" j1 i4 S+ \5 i) ?" B
}$ m8 ]$ ?4 z5 t" C6 b" {$ |
}- s8 d' x) g! s9 Y
验证Nginx配置文件, ]: {% q/ b7 Y# I6 e6 B
$ /usr/local/nginx/sbin/nginx -t
2 m4 Y" W4 ^7 K. r5 t |6 C/ X6 H# j. ~nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
" E1 }3 d/ S3 [+ b ]! Ynginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
7 w, H( J( Y' c$ {% o2 ]7 N; n2 W5 n启动Nginx
" h3 Q. o- g8 C) R$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf- {6 [3 Y$ c6 W, S! E
% i4 Y; B7 ?+ `1 D. N; ^( B2 w: d测试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能够战胜更多复杂/未知/混淆的攻击模式。 , ]$ s0 m4 g* C# G, j5 W
|