|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
- Z! p6 M* n1 L) K3 T
% R0 p2 N/ \& [4 i+ r2 X' q一.准备工作; G- ~+ ?% I& k K5 D$ s
( j+ s: l- @( j" l5 {5 U/ u7 `3 k
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0# ?0 G* S3 D5 t
% f5 R$ D: D2 V f" U5 P q4 Htengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
% r* A4 g4 V" A. v
4 n: E) \8 u- x) ]( f: N/ lmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
! `. \# O7 Q0 s3 z# C; L
% L& W6 T( V; y( E# j5 G: `& I6 R( n: qOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs( P, ]6 l0 l0 [9 R
+ u0 K* m' l3 J' T# I X1 X4 I9 S
依赖关系:3 Q, x* t* @/ u& e B4 z# z$ s! w
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:% t2 E: [+ w- |: U* Z; x& y% o, V& d
; R j' f( Q( i9 M* H" }
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel) } s8 W1 J8 \4 O
modsecurty依赖的包:pcre httpd-devel libxml2 apr
& ^4 Y) V2 V1 S" N4 v( a, m e
7 [4 Z1 F9 o* U7 J$ s% Lyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel3 O$ Y5 ]/ h- M1 R0 w; c& u
二.启用standalone模块并编译9 h" y% d+ @9 ~* W( m p
" t! b& v N, K) g3 Y
下载modsecurity for nginx 解压,进入解压后目录执行:
3 q7 a, S3 I& z5 u% [4 t" y( F; w$ A) {! j" I. k' ?. T1 c, Q
./autogen.sh
8 M J+ _9 X7 e' R. e! g' b./configure --enable-standalone-module --disable-mlogc# Q6 h' x0 ^; Z8 _8 N- T& V
make
: P& J; x9 x. B5 ~) H8 t! |三.nginx添加modsecurity模块
& w0 @/ D- W* d7 @ X5 E6 a5 K( \! Y0 c
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
; w4 ]" S! e! k4 a, p+ b; v: A# F5 O7 e4 k
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
, v: M! X9 O8 T& ~6 s4 b8 Cmake && make install- O' I4 h% W$ K% ?
四.添加规则+ P( {3 \0 Y% D% J( O* s
5 o. H6 T: A T! M9 ^
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
1 [/ e* r! m! d
6 Y) k; A- r% o) p1.下载OWASP规则:
: K6 _ {+ f/ z/ r; U' v
- O) Q& n" u3 E# E/ }; ?git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
_5 l1 ?3 J, u( d) {& U2 P4 q& J
/ j% J! s: u ?' zmv owasp-modsecurity-crs /opt/tengine/conf/2 U3 ]( C" I4 y
. f1 Y% \( u- J5 `* z6 |cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf1 C* G- |6 Y0 }
2.启用OWASP规则:
, n/ g! E0 s A" Y; h8 K8 y. Y1 m7 P: Q7 O. S( @7 `
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
6 X, Q P! E$ P& w! X. ?6 o) }* H& g0 x3 T
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on" V9 [& R+ r+ h8 n* q
* S% O/ v( M, h" d% q8 S. ]. eowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
# Y" W( H4 D, v) N* g" g7 L( m: l$ ?& C$ q3 [
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf/ K: p8 r- U' i
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
( K9 @) E2 \$ JInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf5 M- g) X3 S4 X
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
8 P, |) f$ ]4 [9 b5 yInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf1 ], Z; y; e; B" u$ `1 t$ M K
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf# w1 ^5 f- A2 p5 Z4 B
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
; o4 |( r: K5 M五.配置nginx
J# R; y/ ]5 i @5 A
( Q9 H: k- ~: ?2 l在需要启用modsecurity的主机的location下面加入下面两行即可:/ |+ U- k* a9 u/ F. p* d
) s" d& |& F) B
ModSecurityEnabled on;
+ `( C( D0 C; X% zModSecurityConfig modsecurity.conf;! j0 E+ Q2 d7 i/ y2 ~: J ]1 X) E5 X
下面是两个示例配置,php虚拟主机:4 V, e8 n9 l# a) Q( B: H
$ l2 @5 I, \/ ^6 H
server {
' v0 t5 y5 S+ m2 y listen 80;, ^" ~" N1 H& [/ g: H' }
server_name 52os.net www.52os.net;% h) O$ L* S$ _+ g& {
( n, f! S; z* M
location ~ \.php$ {
( M! a+ G$ H/ f( U ModSecurityEnabled on; 3 S4 Y( J/ S- Z
ModSecurityConfig modsecurity.conf;( ?5 W9 J6 W/ g& ~/ F0 u$ ]. p
7 E8 B5 O; s1 q" h1 b root /web/wordpress;5 u9 i+ A) R* ? D' G! Q& Y5 J
index index.php index.html index.htm;+ o6 Y$ H# r* ^; ]. m. c
! H* V2 q. N; _5 T/ t0 U fastcgi_pass 127.0.0.1:9000;
* B. }6 l- P0 C. q fastcgi_index index.php;/ k' e6 I d& ~
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name; X3 q* T# G/ n( \3 @8 F4 ]
include fastcgi_params;
8 L6 i/ o2 g# }# h1 o" c7 Q }2 A4 M, A E5 ]# P$ k% o& h
}6 [$ V q! W: x% V2 X9 L
upstream负载均衡:/ y2 ^ g4 U4 T1 ~ i: \" O& c( T
- L$ J. r& f/ A6 h3 Oupstream 52os.net {
5 b s! B' M) h1 u2 z6 ^ server 192.168.1.100:8080;
8 R1 P# P7 g4 o; Q R; s9 Z1 V server 192.168.1.101:8080 backup;! R( I3 k3 I7 F! y5 [+ z& D8 T
}
1 }% E( U! A r/ A `" g/ E4 u" e: r1 _& s0 d1 D& ]7 V, Y
server {1 I1 y( G4 ~) q
listen 80;/ T8 I; I4 e. E* _# J
server_name 52os.net www.52os.net;; t4 {) i3 r! `: h) X+ k; R y
6 D# W. U% R. S* J5 C& Mlocation / {
3 i3 p& g4 Q+ W6 A3 r# E ModSecurityEnabled on;
) r5 T& a* ?: F# U ModSecurityConfig modsecurity.conf; / K3 k' g2 s2 c7 y( {
# L9 o# G @) x5 L
proxy_pass http://online;
; c y+ ^2 R9 x; X proxy_redirect off;
7 a2 F6 L# v* z) n7 _6 L; m proxy_set_header Host $host;, a, w8 q7 f; h6 {" y
proxy_set_header X-Real-IP $remote_addr;: y' C1 P8 q7 Y5 ?9 U
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
7 b" d$ z) D8 _) c }
5 w" ^( w! O; C}4 H3 s( v0 w6 b' G6 I. f
六.测试8 ~5 [# ^- L7 P
- ?; y1 r2 m+ H
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
1 `% A$ x5 Y+ |" z% v/ r" X) f; e) V* n; w- W0 u5 \$ t3 ]3 d$ R
<?php( P2 `2 a6 s- W* P' s2 |2 c
phpinfo();
. l) ?5 {1 r2 H( ~?>! `/ o N! k8 H. _/ Y
在浏览器中访问:/ I+ u9 F, E7 E5 }9 o
* N7 A, N' t% I( S6 {9 f8 y7 i
http://www.52os.net/phpinfo.php?id=1 正常显示。2 o6 o' l0 `8 C( K2 L& N: y
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
; j' g! L) m; K4 V# d5 `http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
' J9 ^9 l: u5 Z( i6 U# b说明sql注入和xss已经被过滤了
$ z8 J1 f0 B+ _2 R) e8 `
7 ~$ L2 n' q+ x) n# ~' ~; X七、安装过程中排错( \6 b6 @+ I8 q; ]' V
, Y- P, [- D. y
1.缺少APXS会报错
% u" j! J6 V+ H& Q2 U7 i% U6 h" o: Z4 e% j+ F- o
configure: looking for Apache module support via DSO through APXS- W9 S2 ]& A4 Q
configure: error: couldn't find APXS
/ Y& {& ?7 Q8 t! n9 b6 z$ @apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
* \5 A/ r( ~! ^. W7 [* W5 n解决方法:
& F/ K' M. Q+ q3 A: ]: }0 m3 T0 H
: Q, O" e V5 i, K$ k9 jyum install httpd-devel/ l- Y! V% c( I! R. V/ q; W) m6 E4 v
2.没有pcre
; z0 Y( ]! G! v! B7 }; s0 I. H$ A
. f" t3 C" _ Y/ _( Jconfigure: *** pcre library not found.
! V, a# }2 i4 u& T9 Zconfigure: error: pcre library is required
9 i5 f9 J8 k, e$ p% Z$ J解决方法:
/ E2 s: ?( F8 P5 i$ L( S, ^
$ d* ~; S$ h0 s; O6 V! ayum install pcre pcre-devel
/ l' f8 p7 A% c) F" I1 [$ e( O$ A3.没有libxml25 W4 Q9 c8 Z: p& s) ^ J! {
0 N% B1 Z! m, h8 @" N+ r7 d
% U; H/ K' c. D7 [# G, Hconfigure: *** xml library not found.
& ^( _, e; L' M5 b, B2 {# Oconfigure: error: libxml2 is required4 M* t ^) L5 ~0 @# P* j/ T$ g
解决方法:" {$ Y! N2 ~' o( m ^
+ }& B4 N- j& ~# y3 n
yum install libxml2 libxml2-devel
6 Z7 a! n d$ K4.执行 /opt/tengine/sbin/nginx -m 时有警告
, w8 B/ {6 V8 Y x3 H$ I5 A) p. ~: V6 `) M/ T
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
' m5 ^/ D( f2 `5 L, q; f: Unginx: [warn] ModSecurity: Loaded APR do not match with compiled!
, y( O5 \4 m. D( v0 W# r+ n8 R2 @) L原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log. H% {* @! u- a$ g
% ?3 g, b" _% Y: t
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
7 P+ C5 Y, V) f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"* m: l( y0 p3 Y1 {/ s C; l
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!! c2 u# A: F& `2 `) H( A; Q; i
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"% D: f+ }# P# _7 t5 S/ j
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6") e5 j* ]$ i) P9 w
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.9 d- C- Z; h$ a; r3 M: P
解决方法,移除低版本的APR (1.3.9)
$ `( k/ n3 I% G# ]7 T6 W5 g% b# z+ U# W$ B, u4 _- }* U8 F8 s4 `7 {
yum remove apr
; z/ `" O: u0 C" g; D0 \5.Error.log中有: Audit log: Failed to lock global mutex
0 y) e2 E# j. [/ ?' P
& d/ l; q& A' O2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
' P& L# `: M& }( nglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"], r$ O# x0 y( N. k- r o0 ]
解决方法:
0 w0 ?% r5 Y: O: l! t) u编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:* {: Y* ]$ S; |! h. }# y% u
5 N7 e; G+ F" {$ Y/ `( z; }
SecAuditLogDirMode 0777
& g6 W. U+ k9 q0 w7 jSecAuditLogFileMode 05502 F- `" {5 Y2 Z7 ~( c. D
SecAuditLogStorageDir /var/log/modsecurity
1 N4 Y9 Q5 X9 c4 |! ^SecAuditLogType Concurrent0 I% k* H% U# {+ I) R
参考文章:
3 g9 e' K h% V/ ~1 H. _/ \% ?( |https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX+ B4 O9 N# i4 p8 O8 p
http://drops.wooyun.org/tips/2614 |
|