|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
# W$ U0 |: S" q8 k, I1 B0 B2 `; | O. y) G$ s6 _
一.准备工作
% v: w. [# ]) h6 {# U2 k2 [- C$ ^. \, P
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
9 U7 _+ Y1 t9 N: U+ I1 ~, z0 S( F4 {
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
1 i1 q" U h5 Q3 D) ]6 e2 l
2 [9 D& P5 T* I# F1 Smodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ g8 a, O" l! `$ v5 D1 b* e, \: n
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
2 X& }1 o# F4 f$ R }' B, v5 f4 _) E, |9 P+ w0 t
依赖关系:0 S2 F! m8 K3 \9 w1 H& W
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:0 E" V' c3 B {) x
- ~/ f& n( X, G7 A+ V- w9 k' fyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel7 g& h/ W2 W5 i
modsecurty依赖的包:pcre httpd-devel libxml2 apr
" z0 ~) `$ H. p5 [# T
" F# N3 r+ {# `" ?yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel6 c5 p: I" H+ R/ M" {
二.启用standalone模块并编译
. a1 l/ L0 q, v2 `
9 Q# I" W, @7 R4 [; V9 E! R下载modsecurity for nginx 解压,进入解压后目录执行:) g) ~( u B r. l" Z( i
# c6 d6 { e* I/ l7 m./autogen.sh- x) c' t& N! ~4 F, P7 ]3 V- B
./configure --enable-standalone-module --disable-mlogc
; u) ~1 {3 J3 Y) f0 T+ Umake 7 y2 }( ]0 f% u4 D' a6 C
三.nginx添加modsecurity模块: k1 a; j8 w; Z
8 B% u! u1 e' S6 T6 z" R& d, U在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:: a+ F7 y d$ [+ S; `+ n0 i3 R
# Q8 ?: _# A% G% M
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine) N1 d1 r4 L$ u% O: C, S8 k$ X, O
make && make install+ u. r/ S* t. }4 C7 R$ V
四.添加规则/ S, ~9 @ z. m! Y" k# n
3 |! p3 X! R4 I8 Dmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。5 L! {$ U) c" P1 @' z5 {
. s* i* w0 G& y9 @' h# B/ I! _
1.下载OWASP规则:9 @1 E) |' Q% ~6 V
# |/ i& j$ A6 X: e
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ Q: r9 o5 ?! X% Y j0 i! D* _+ a! J
mv owasp-modsecurity-crs /opt/tengine/conf/
" x4 y) g* t. Z7 s
5 A0 J% y: V' P8 |% d1 t: n8 }cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
. G" c. l* L2 B5 f; u2.启用OWASP规则:
' R' r/ u0 s5 s) T
' G5 z7 O$ d( F% b7 c2 \复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
9 B. {7 K( i, m$ v4 n9 H4 x+ i( r; B, K% S( {$ _
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
' X0 n1 i" M! w4 B7 \) W' f2 n W J, m$ J
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
, N8 O2 s+ h: X% v* a% ?5 O
; p2 G# M" _8 @! ] TInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf4 l f" `; I+ H. U3 [' f, _) [
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf1 n( ^# K; Q: S- M
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
9 w$ f3 y/ q q. Q. \4 `* BInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf) z/ D& `( ^& G! {
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
- a" V2 x; U( j# AInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf" L0 { e) k. f! L- s; l
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
2 R+ m: S+ e3 R$ \% ^( \9 q五.配置nginx
w9 ]' B2 N* G6 x- Z( r+ n+ W1 L: h" R/ m; z0 Z Z& g
在需要启用modsecurity的主机的location下面加入下面两行即可:! ^! m+ N% C0 m9 j& p3 W1 |$ E
6 p* I- A H8 f# y2 p
ModSecurityEnabled on;
- u; n/ p0 y, M8 L$ p7 RModSecurityConfig modsecurity.conf;" W9 g$ u7 w* c! V6 n# x
下面是两个示例配置,php虚拟主机:
6 A, H2 a2 e9 ]$ w
: V( D# l3 X9 o. @) Hserver {# x4 l* ]+ G4 e' {2 [; M ]
listen 80;+ W. n: v& ~& C
server_name 52os.net www.52os.net;
2 T. T* R6 @) T0 O0 `0 {' A- O
4 ?9 I. W' `' u4 E3 X8 l; Y location ~ \.php$ {
$ O' q0 s5 d1 T7 y! f, j7 C' f6 J ModSecurityEnabled on; " }( v, f2 _8 l. L8 g' z6 w
ModSecurityConfig modsecurity.conf;. _$ a: e) j" H
- }3 ~% D/ f! ?' B, [* R) Z root /web/wordpress;
7 G1 N7 u" K3 r8 J5 P index index.php index.html index.htm;
% h; _3 f1 p S2 e9 y5 L" G$ C ' G- b1 h. `( e4 Y
fastcgi_pass 127.0.0.1:9000;5 y8 Z1 D6 Y% k1 \0 w! y: _
fastcgi_index index.php;; z# ?3 I9 i2 T7 {; m7 u% M S
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;- H: l8 \# S% U8 j* L
include fastcgi_params;- M, o g% V* w" e" q
}
7 D4 b6 C' v$ x2 {- h8 a3 N }; `1 l6 d7 G- C5 s7 v& E
upstream负载均衡:
0 m7 ?9 w$ K! v! @
; z! E3 ?7 d! i. O. Eupstream 52os.net {
+ D" [, c& X2 _4 w1 l0 H8 I server 192.168.1.100:8080;4 ]) Z6 c1 C# |6 l. ?, c- x0 D3 ?
server 192.168.1.101:8080 backup;' u: p5 X' c1 }: K' h6 w
}
7 r' A& I6 ^2 x4 q% q; c& e1 f+ M& @' H
server {) z! l7 z' s+ z# N5 K
listen 80;# M/ | W& o4 B+ B9 ^. ~# U" I$ @
server_name 52os.net www.52os.net;% I0 o! @' J" w4 ^8 }
$ j9 K1 e3 j6 u6 ~8 elocation / {
r' ^4 N& V o ModSecurityEnabled on; 7 m0 s# t. k' ^5 B3 ?1 d2 e/ n
ModSecurityConfig modsecurity.conf;
/ I$ e' A6 C) B" f
% t- K/ s7 a5 g9 L- t proxy_pass http://online;+ b( h3 G8 T6 W7 h: W2 \9 F2 l- a
proxy_redirect off;" U F- O2 x( ` r1 ~- K; @7 ^& J' `
proxy_set_header Host $host;2 Z0 e6 V- _$ }- E% v
proxy_set_header X-Real-IP $remote_addr;
. f8 B8 e4 {* ` proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;- o* \" a4 e9 j0 q
}9 b0 w8 Y; }9 q) Q( Z" H; R
}: H3 N8 W6 `0 @/ `6 d
六.测试! K1 Z) G) s% M+ S5 e
& o1 s9 @0 i k# a/ t; n我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:& W! a( d7 X' ?9 T+ {( G W
% o8 r, _/ s) O1 H" ^% W<?php" q, e0 y, ^* P( k9 z
phpinfo();
1 c) x" J, K: \0 L& j?>
+ w& J. z# T6 t, x! C5 Z5 W) G3 d在浏览器中访问:* n0 e% `! X' v9 o' S" b, b% k
( t2 ]0 s/ O" t% B9 b5 zhttp://www.52os.net/phpinfo.php?id=1 正常显示。
6 h0 B+ q0 z. X; q5 Phttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
$ ~) E+ @" T) V1 Ohttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。& ^9 ]5 O+ e. Z6 r# ^7 D
说明sql注入和xss已经被过滤了
9 v5 O7 r( z- ^2 N/ p- n; L) d; A' H6 V: U1 n
七、安装过程中排错0 X- K8 s: ~: O4 x `' T
! }) M+ k& n) s0 h
1.缺少APXS会报错7 j1 O9 @, o! y) P( D
8 \6 \9 S$ f# ^* L" cconfigure: looking for Apache module support via DSO through APXS& ]8 F: `$ r9 k, Q, J! J
configure: error: couldn't find APXS
; B9 |1 }5 d' V5 ^' Tapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
, v9 k. J3 U+ L3 i1 v I解决方法:- X/ M$ \- r2 u6 M+ m
5 x U8 p# C& z! B8 k! E# E- u" W7 tyum install httpd-devel
8 U; W2 m7 f0 s$ w D% w. d2.没有pcre
d7 @! b, y! C9 q+ w5 T X& {0 R
configure: *** pcre library not found.
. j+ m% Q- W1 A9 c/ Tconfigure: error: pcre library is required
' P/ @2 {8 t% H( D4 G# @* @% H解决方法:& z8 z# `2 B. V, T0 l
2 m8 k3 Y7 ^* e+ c h, ^
yum install pcre pcre-devel' b& z( C8 B9 N
3.没有libxml2$ K' g6 H: `! Y+ g
3 D R+ ?. r( A8 M9 J) ]# l- c+ D
6 l! E: t0 ]; V# z2 ]5 b% n7 v( U
configure: *** xml library not found.
5 x) E$ a/ t+ }5 F+ ]configure: error: libxml2 is required
% j! c! R7 T1 ?. m, ]解决方法:( ^( v. z8 { O+ G
1 Q& P% t6 l# P$ } d t3 L4 t% E- {6 Pyum install libxml2 libxml2-devel
) g: Z- D* f E0 ?4.执行 /opt/tengine/sbin/nginx -m 时有警告
4 H2 p2 R F& s) b a+ b5 }3 r6 W' {, ` j- X0 Z7 r" p
Tengine version: Tengine/2.1.0 (nginx/1.6.2)& K$ o0 V+ i: J8 Z- H6 R# @. r
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!* R7 m1 ^, f$ V' {- _1 n% u( F
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
. u9 M5 N& X( Y! e# }; c5 X$ |# c8 b8 r. `; ~/ E& n
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.& G0 ^ g" J, X" Y8 u @
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
6 R& D- ` d. J3 W& \9 d3 _* u5 ^2 d2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
+ Q$ X6 x& W! O6 Q- M/ x" N3 C" S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
, k2 `; \, G+ l( I& {* ^! Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"4 r; C$ J' n# V$ L: h/ G
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
- g8 k2 X! I+ ?1 u9 R解决方法,移除低版本的APR (1.3.9)! o6 p9 ^" J5 J( i: A7 V3 K- d/ a
7 D7 f; r' h4 X4 p4 w; j
yum remove apr& r* b3 g* q. r1 N* w
5.Error.log中有: Audit log: Failed to lock global mutex
8 D* Y Q. s* k- l* y7 ?: z5 Q' Y% l0 I' p% |& s$ w3 ]% }+ u
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock : X1 U' R3 F5 B$ u! Y
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]' q$ q& `% m, X% g4 d. K
解决方法:6 E5 T7 T9 U8 P2 \% Y) b Z
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
4 u* }- q3 P4 @! a! f
+ c( N$ g! W KSecAuditLogDirMode 0777
& N. I$ x: }: C4 pSecAuditLogFileMode 0550; r) M. L/ q& ?* l* @; j. p) {( [
SecAuditLogStorageDir /var/log/modsecurity
& K9 D! f) G3 v( j/ OSecAuditLogType Concurrent
0 N" B u$ @( u: P参考文章:
2 O& T' d/ Y" dhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
- t8 Q* n3 b7 R/ Z) Ehttp://drops.wooyun.org/tips/2614 |
|