|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
8 |1 _# u0 @) K3 O3 F) P( }+ i1 a3 X/ {) N
一.准备工作6 D7 y0 x T* U' m6 l! K
3 {3 Q7 c- K9 Z: r系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.08 ~0 q4 o- x6 I) P, r. ?8 S; @
& X' g5 ^' h) J% [
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz) w2 r5 |% w, |* i) K+ ?
; o* @; w/ c { omodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz1 K4 ~6 I6 i& d: N- Z
$ f- q& ]9 R/ q
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
+ e: M0 N" M0 r! l0 C% Q6 A
6 E4 N* \9 b0 g4 m) {9 _* c( C" m2 R: d依赖关系:
/ g5 P% E1 h- N) f$ {tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:; j( q7 ]" S3 Y" ^" V
) J/ @( V9 r4 q; E1 J% `% ^* Q/ y
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel# m4 y, y% H5 I
modsecurty依赖的包:pcre httpd-devel libxml2 apr2 R0 d8 s: |1 h3 x) I# ]
7 F# K( a2 i( p0 ~+ tyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel) m9 Y z2 M; [! L: m
二.启用standalone模块并编译
; Y' P0 e2 W5 O1 q1 l; B( k7 F( B. t- O& H
下载modsecurity for nginx 解压,进入解压后目录执行:( Z: `4 |- B7 v' |" G
2 A) e7 w3 ]6 i l- m) L./autogen.sh
5 B# T9 f. R* n7 V" L( X& l./configure --enable-standalone-module --disable-mlogc3 \' V3 @6 x% L
make , c# Q9 S& L( R$ e- z- U5 S
三.nginx添加modsecurity模块
' z4 W5 n3 {7 B+ q* F: X5 h5 R- c$ Y1 b! h- O8 f( M$ Q* C8 _
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
$ y- Z) h8 ?) d1 _$ A y
" l) b, U2 u2 {./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
! Z5 ?0 \& a0 A4 i! Y1 n# hmake && make install7 }% c( i* e: V7 M0 k7 D) Z4 d) A
四.添加规则4 B/ f9 d' P( N
; t0 p: H2 c& [' C: Z7 d& K
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
" ]; c) d8 T7 P2 P/ M, r4 t6 L, r
- J- v4 ^# x' P! b' U: G2 [! T1.下载OWASP规则:9 d3 s U0 \! ^ c( i1 p/ R
% ?; |7 _9 X1 s& m0 C- }git clone https://github.com/SpiderLabs/owasp-modsecurity-crs# \" W& M. |/ Q3 N2 ^; q
6 q& w6 {$ ?# Z% L
mv owasp-modsecurity-crs /opt/tengine/conf/
# a; {2 A6 Z1 c( B. K3 B5 y- r: J4 V" n9 x: a& Z7 v+ Q9 b
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
8 i" a# a) z& ^& ]2 E$ _( ]1 I& d2.启用OWASP规则:
* U3 H4 r6 F+ @' \; U" y- W) k+ F% Q0 h- E8 D9 m9 O e5 C
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
! E5 h5 w$ b* g/ V/ ` E
+ C7 B$ i7 s3 S7 a; v- i编辑modsecurity.conf 文件,将SecRuleEngine设置为 on1 _7 F" m/ }1 m4 i' P2 O
/ m6 g0 r( L* I; A; a6 w
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。, G; r! g. Z! M! F5 ~& h$ R
7 A9 S. L5 l: i" O# v: T
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
3 Z" ?% d0 O/ e/ ?/ W# j$ s; F$ jInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf/ |; M& k7 P" n
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf/ A& T* W+ ^1 O7 S1 s1 a1 d
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
- }8 h( X' E8 XInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
; q1 W9 X( ^# {' U- H0 t: bInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf/ F1 r- h. z0 d$ ~
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
/ P0 o+ d, h6 s- ~' ^" a, G五.配置nginx5 o% Y1 s2 p! x1 x' s8 g
9 F! g3 @" Q% b, G在需要启用modsecurity的主机的location下面加入下面两行即可:
0 l# Q* s3 T* o% b* g8 X) p
7 j C3 j6 a, Z" \# O- jModSecurityEnabled on;
% I8 N/ j$ ~# d1 A8 PModSecurityConfig modsecurity.conf;" R) n. j: W% p* D
下面是两个示例配置,php虚拟主机:( d) L' F, X2 G$ _. }5 ~
; ^3 Q" `2 M' ?2 ?) v! ^server {
. o4 { T8 b# m h! d: C' g0 g listen 80;( N+ P- S" B7 W
server_name 52os.net www.52os.net;- z: W) ~0 M% d7 r% a
. k9 _" t* i$ g( x- ]: M/ @ v
location ~ \.php$ {
" Y; O$ Z/ u0 a. x ModSecurityEnabled on;
3 Q$ R; C: ]: [8 j ModSecurityConfig modsecurity.conf;
9 h( z7 e8 U( j9 T2 ? _8 N
. ^* j) h1 S. K1 n$ b& s `8 R% z root /web/wordpress;
7 Z; p1 h; F4 K% B/ }$ w3 B index index.php index.html index.htm;0 }% ]( e3 x( c$ S: U, b! A
. `; B) i. k" Q3 Z X1 o
fastcgi_pass 127.0.0.1:9000;
" k% w$ G; V4 e2 y1 M fastcgi_index index.php;5 n3 h8 W% Z4 D
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
9 o0 m5 c; g" C) ]" P: m include fastcgi_params;
0 t. e1 |3 I' W/ i" e- |9 w }
/ {% f' U( x9 j- Q6 ^ }- o; f6 t: s9 A, Y t: I6 I( C
upstream负载均衡:
& R t, g) p) K# V/ c; V y
) V4 w; o; B" b- gupstream 52os.net {
$ V2 X6 y( _4 V. L. | server 192.168.1.100:8080;8 m' f5 ?" S Q& q+ N ~: `
server 192.168.1.101:8080 backup;# O5 j! h, c8 {/ @5 D2 o+ e
}
% v5 G+ T2 g: }4 v0 S; c3 ~+ Q+ j& t& Z% ? b
server {; O6 D9 H7 E% g9 `1 p$ G3 t
listen 80;: h* h! z- V7 [% a# V& H& b2 F
server_name 52os.net www.52os.net;
) p" b- _6 [) m. C3 l* w
* V( t* a: i. I' O3 s2 a" H3 flocation / {
& Z y+ A9 h' T ModSecurityEnabled on; 1 Q$ `8 h2 {/ y W, D) p$ P( t! l
ModSecurityConfig modsecurity.conf; 2 l: Q# m9 D' Q* f
# s j& ~* J9 S5 J; Y) \5 q proxy_pass http://online;4 M' j& v2 V S2 K- q4 L
proxy_redirect off;2 g' U9 T- S, ?+ z
proxy_set_header Host $host; w! B' ]% s6 f9 C; W
proxy_set_header X-Real-IP $remote_addr;. z# y( j2 {& E! Y( O7 S
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;" c) r$ E) ]! N, ?7 \
}
; w9 {4 i1 I* B( D! O}
& b E2 m& f$ a1 O; T& _4 t* x0 t六.测试3 R) n' E. s/ Q& @$ ~5 S
9 m" d; Q5 H& u+ h2 u, H) w& T我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:: X& D8 \/ d: `8 G f$ W
% D4 h; f7 u3 q0 K<?php& _ M7 k3 _9 J7 J! Z& V
phpinfo();
( s1 h+ x' G2 q% s6 j9 v2 o) r$ `?>
! {& n3 Y) C1 \" C在浏览器中访问:
' }" `) N$ n- R: T0 J7 i8 e0 V! k) o3 d& s7 s
http://www.52os.net/phpinfo.php?id=1 正常显示。" Y' m3 X& j8 {- e, p
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
, ?& b) F `; H0 B2 x; M7 B* Fhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
% j6 f1 t% ?! y! a- R, c说明sql注入和xss已经被过滤了0 ~9 s8 z1 A& V
+ i7 |* x: b5 b7 E七、安装过程中排错
3 n8 N' z) G* H9 P7 Y/ C8 H4 u
9 A/ h r: k+ s1.缺少APXS会报错
, D ]; B* l( ~3 \9 A t" P+ `1 J. ]6 j& _6 k+ R! ?
configure: looking for Apache module support via DSO through APXS
( J' [3 ~2 p1 u2 B7 tconfigure: error: couldn't find APXS
. z3 P8 c$ @0 Q+ _apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
) n8 K" R3 g& X |解决方法:
/ X0 S$ ]" v$ [- e) }8 f/ z' D* E2 {. t7 P( Y3 Y. E- W7 ]% d
yum install httpd-devel) y4 w) m& K4 S; z$ e8 K
2.没有pcre; l' w% Y8 U4 @5 z% ^+ N
0 d! D, T" T; v F6 e- f; {1 y- Tconfigure: *** pcre library not found.
+ t+ l5 j- y' f1 t/ ^, S. `configure: error: pcre library is required
& ?0 Y2 c7 U( y1 ]* _ v解决方法:
7 M7 e* {; y4 i# T F9 |" K: g; i- Q$ ^$ F
yum install pcre pcre-devel
! y$ d: r7 [( ?4 K3 x/ s3.没有libxml2
M2 j; J8 p& I1 ~1 h! q3 b
, h# w4 B& [3 \! e& P1 Q
2 i0 u8 v* u8 Iconfigure: *** xml library not found.
# t2 o6 O1 D. @configure: error: libxml2 is required) S" A# y/ k( i( u# H9 q
解决方法:
/ p+ z8 ^. ~3 L2 a1 N5 X( G* U
: z9 K) R! Y; s( F5 u1 z; `- }yum install libxml2 libxml2-devel
0 H3 p$ }- T0 b' l( U4.执行 /opt/tengine/sbin/nginx -m 时有警告2 o) d1 [; H( J5 U9 T* N9 \
% |; `: \& O* I: A/ t
Tengine version: Tengine/2.1.0 (nginx/1.6.2)) h" B- C! d- c5 n2 W. _" u
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
$ f: ]3 H/ `' K0 z2 O7 J: X原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
) M) T9 @% q7 a7 z" y3 I; t. s. S( J L* e& G0 o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
& G, q p9 W6 ?- i# j2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9". t4 k: j3 F. q
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
) Q5 y6 G( Z+ ?1 h }7 M; o! D2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
8 D7 J: w; t5 f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
. r' e2 U- g/ S1 @. a T. k5 s$ T2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
1 ?! `' d$ Y7 W' M. P" \# ~2 _* v解决方法,移除低版本的APR (1.3.9)5 ~. U- K' a$ | ^6 Y. U2 @% {, s
% I, @7 W2 _* m: i# M, q2 oyum remove apr3 g+ X8 R- c Q" r
5.Error.log中有: Audit log: Failed to lock global mutex3 p; _9 C1 a/ ?# o7 `4 P
# D# u+ @$ K1 a) _( a4 @! x/ a: t
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
0 @1 I7 B- e5 L9 Kglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
/ E, l1 `8 C# Y6 I解决方法:" i8 g6 \9 D. ]7 f$ w
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:3 b. k+ p& y4 q' ?+ |
E+ O& w+ A1 xSecAuditLogDirMode 0777
' E( \5 `2 f* L& m+ x; pSecAuditLogFileMode 0550
m; X( c& X% E6 I, `* tSecAuditLogStorageDir /var/log/modsecurity
+ r: f% b1 ?6 b' F. bSecAuditLogType Concurrent9 o+ K. q) d3 g2 `
参考文章:
! l$ H3 a }; B: U3 zhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX- {1 _/ y2 @) A* S& N9 W* r
http://drops.wooyun.org/tips/2614 |
|