|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。$ m4 C9 b& E% u( z* v5 ]
3 {4 a* l' f; K! @一.准备工作
* x8 ^+ T/ i; V3 g
' S3 M) y% C' M! I& ~$ o ~系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0* d$ G* \& E2 M' I" X
, o1 R2 S6 s: E ttengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
2 ^; e% Y- U4 k3 U0 k
3 k% l. ]. }) r3 B, Xmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
/ Y( c5 `5 T3 N1 F* G" G3 n8 _# {5 [ Z E
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs2 \) W- b9 N7 N7 e( V
* q2 O5 j6 k% H2 P
依赖关系:6 Y, ~2 L$ Y) x: k4 c) J
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
f& ]% t/ c4 G" K
8 L4 i' _8 x) \yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
. C4 w: I% D, k! J' E1 `6 _modsecurty依赖的包:pcre httpd-devel libxml2 apr2 O; B! J7 r+ W s
; U! j: L6 d4 B+ i4 |( P6 G, n
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
) V! M9 E6 e6 i, K+ v) c二.启用standalone模块并编译( n. \+ z6 C H% A) c
1 `2 T/ R% k, f8 S. V7 Z' u$ `
下载modsecurity for nginx 解压,进入解压后目录执行:4 G, L: A% A8 b' L' M
4 s6 Y+ t P5 Z. m
./autogen.sh
& P; v3 o9 n3 J0 A( T% d: v./configure --enable-standalone-module --disable-mlogc
& M% H% {6 N4 [: kmake
2 h; I7 S. n# o三.nginx添加modsecurity模块
2 I8 c5 b* \& f' W( W6 V/ K! l/ I. g: [( h
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
( N/ ?* X/ I$ W# `( `, u! v) o9 Z) H
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
# {$ ?. _1 u! g0 m* Fmake && make install
1 [9 h ^6 _: h$ p9 }3 }四.添加规则& B) u; n3 `0 {; R# C+ B6 r
5 \8 o. v; K% o2 X9 v- {) ?
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
. G# D: M- H: Z+ B* R( l P2 n" z1 U
1.下载OWASP规则:2 D; a/ @! b2 ]# |' f5 L
8 u ~3 H. Y, q- |git clone https://github.com/SpiderLabs/owasp-modsecurity-crs/ S5 s0 X6 E$ ?6 y" w
" W0 Z' _" u R8 q
mv owasp-modsecurity-crs /opt/tengine/conf/
3 _2 i& s, t" U# D
) d0 E% [7 Q' j0 C; Q! m1 [) vcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf( c$ i0 \# [0 s9 W' N, y) i; X
2.启用OWASP规则:
! ?6 n+ E& K/ F* r4 d- X2 ]9 M! x4 p6 D
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。" J' s) n9 x1 o- w
8 b6 W/ G& l# \4 Q' U
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on0 S0 ^8 P: \) s
4 {2 t7 \4 [% c9 [( Z2 ^( f
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
$ {2 ^2 C. ~ {! N0 H- N3 U. C0 i8 |9 ]& B" `
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
9 v5 k! ]* v* bInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf. d* `0 H2 r1 ?1 r+ x- S- M
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
! `. ?( r7 N7 i( ?- gInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf2 m h! Q, A! Y6 `6 j* U$ E J
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
4 U( j- ~+ c. T+ }Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf! e. F1 e3 R$ L! n4 E
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf i5 X; D8 X( S' }; `, Z+ M$ ?
五.配置nginx# r& X6 `8 |: ^! T, u! z% h( V1 G0 w
, C/ C& v0 ?* Y+ [0 |1 n在需要启用modsecurity的主机的location下面加入下面两行即可:
3 X8 N/ j! V' j9 ]7 d% m
! v! M( r: S. O# d5 @ModSecurityEnabled on;
8 p+ w, g0 u0 @! Q p9 kModSecurityConfig modsecurity.conf;4 \8 g1 }( H. z$ U
下面是两个示例配置,php虚拟主机:* h) V0 p6 u* S$ T
+ I9 Q" S: r, m: P9 S! ~% aserver {- O0 F8 I d) ~
listen 80;0 I' d: X( z/ d6 j& C
server_name 52os.net www.52os.net;' `: i7 c$ I: Q1 Y5 L; i$ ?, W' _6 O
) f3 G2 k) t6 N0 G. L location ~ \.php$ {
( q1 U0 P4 }$ V" L) M5 w ModSecurityEnabled on;
+ ~$ t; I( {% A" D( j# K ModSecurityConfig modsecurity.conf;
* F4 n9 _# r4 S
" \* r/ s' L9 W( ~ root /web/wordpress;
) C2 }. `( x; h; Q s" G( ^" R% U index index.php index.html index.htm;
! f2 z" b; F8 x$ z
- o3 t$ q. }; I0 P! \9 | fastcgi_pass 127.0.0.1:9000;
( c1 T( z9 m* [7 F, n fastcgi_index index.php;) M4 H4 i" U% t3 S2 }+ B0 f1 N; h, V
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;9 r6 |6 j& @% r: F% n
include fastcgi_params;
/ Y: b4 t. T& T1 E2 O: W; T3 @4 w }
5 H7 l- Y/ v9 G, ~$ K7 _ }
9 S$ f2 y% H( i1 ~upstream负载均衡:
5 j$ l' L! ~4 Y( U; J9 B5 D. f: L' M5 p6 D+ G% D
upstream 52os.net {
% g5 o8 i9 {0 l server 192.168.1.100:8080;: f" A4 `, O# T3 J4 h1 c
server 192.168.1.101:8080 backup;. `7 e2 i4 x/ T; |' G, ~' A! A
}" L* _2 b9 A5 p2 t6 t8 i
; Q j" b! T* B, Iserver {" t2 Y/ Q7 Z1 X( i- @8 X
listen 80;
2 o/ y9 }5 A5 gserver_name 52os.net www.52os.net;
1 G, \* z* ?4 p' Y" [6 `$ N& X M5 h& C" K
location / {
8 B6 b0 @8 {: @' \- \2 g( ? ModSecurityEnabled on; 0 G7 p w7 L3 y& n8 S3 z
ModSecurityConfig modsecurity.conf; 7 i2 U8 y- a6 m/ \9 D# d
( b$ a3 X0 X) C: v
proxy_pass http://online;+ y* D; P1 _: F) M9 T
proxy_redirect off;
# O0 g! i' ` A% y4 g proxy_set_header Host $host;
% a$ G* {# u& d( P& U proxy_set_header X-Real-IP $remote_addr;4 [9 l$ y. l& I1 M; I- m# q
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;0 ?0 e$ Z/ i* o' a$ H
}8 q3 y# z: F( `
}
& D+ Q$ ~6 ]( G* v- y9 y! e/ Z6 T六.测试& F; y# j6 k4 Y: U( I, E
$ g+ @( D& I; R: @0 `0 `, @我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:: m' j. q9 q+ @) U, G
# H, J& r Q% |1 H<?php' l: a; y) j+ Q- t
phpinfo(); . n% ]- l( G$ w T) G
?>
$ I8 Y1 _4 s1 e在浏览器中访问:5 ?- p, ?. M# V7 R; j% w* p: c
* p; C, z/ \5 e0 z9 f& Y4 c" Lhttp://www.52os.net/phpinfo.php?id=1 正常显示。
# p& ~# v& o1 C4 C# L, Rhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。; j) Q1 D* ?- V. X; m* L
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。5 B1 V2 n8 r# `
说明sql注入和xss已经被过滤了
0 S4 J+ {1 v+ Z4 d7 x
% u4 a2 m1 i' s( N七、安装过程中排错3 T# R" n k+ k
1 y0 q" |$ d+ i1 a- \0 \
1.缺少APXS会报错" P/ ~. m& A, `4 n
3 S6 p; h( d' W2 Q1 A& m) F! Econfigure: looking for Apache module support via DSO through APXS
0 U6 L7 g9 J& S) @) ^configure: error: couldn't find APXS3 P+ W2 W1 P1 j+ ]+ h; h
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。! @) R' J/ k3 I9 z8 _; ~
解决方法:
! c5 p6 r6 Q% v9 s. h; q/ Y* K
% |# o% e2 H; Byum install httpd-devel; R7 M2 a& ?" F, \, l) J# j8 U
2.没有pcre+ s1 J! q% ~& V e: i) P6 o
4 [8 L9 F/ T' x% `; |% O
configure: *** pcre library not found.
; N: C5 ~' Y% K1 L; w) s# a pconfigure: error: pcre library is required2 M6 S1 G9 |" ` B$ X6 E8 x
解决方法:7 z+ j7 w+ J' \+ @) |
1 v# a: w1 J' l7 N( lyum install pcre pcre-devel b0 p: c; ~+ {$ @1 f) C/ |
3.没有libxml2
8 V0 O6 E% x6 r, z. J5 }" i1 O3 g2 x }1 Z0 g1 T) x
/ H! `& {/ }. E( m B1 U: \6 q
configure: *** xml library not found.
& T- n" h3 V/ N! R4 J6 mconfigure: error: libxml2 is required4 } e0 w+ a# ]2 B4 j4 T
解决方法:
- n) e" ~ Q. I. V: m/ M) m' x9 W! I7 ^& G% P U5 q6 F* m
yum install libxml2 libxml2-devel" ^% k' C" n o/ j8 h- G
4.执行 /opt/tengine/sbin/nginx -m 时有警告3 L; _- Z& q- J" V6 E8 l
' o5 r* {, I, N7 Y9 ^1 |$ s- qTengine version: Tengine/2.1.0 (nginx/1.6.2)
; Z0 U, k) z% j" J' C2 ~, e- N. H* Jnginx: [warn] ModSecurity: Loaded APR do not match with compiled!0 @% Q- |. D1 K! L4 A& _3 n7 B- d
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
' h9 L% [6 g3 [! X0 c9 ~. X+ g5 I
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured. g2 K' b- ^4 K8 X- \
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
; r( s: d, D. F; f; a2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
9 J7 `7 n3 B1 k& M3 x2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"" ?; U; d: l7 h
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
3 N) k e. W/ v2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.+ b! a& o+ l$ n5 o: k
解决方法,移除低版本的APR (1.3.9). [1 I- Z8 v+ ]* T5 U
7 p7 G% B! q9 T) Qyum remove apr
" K, s6 M2 N1 \7 m4 f, X5.Error.log中有: Audit log: Failed to lock global mutex% L4 w- N/ L! n& e
' v4 e* R3 K* G N* q+ V' @2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 7 K- v F* s }; p9 Q
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
# L: ~& r) n3 Q2 ^3 _4 J解决方法:
3 s% V5 a8 Y( O编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
+ B6 S. b1 t+ p0 t m4 W+ b7 E# [/ L9 x$ N8 H
SecAuditLogDirMode 0777: X! T) a% h& q, F' X
SecAuditLogFileMode 0550
( N+ L7 D6 N4 v6 U/ VSecAuditLogStorageDir /var/log/modsecurity' u- t& k# f3 H: p& [& P2 h. B
SecAuditLogType Concurrent
* s0 m+ k9 I1 O6 e1 e) V( T$ d p4 {( h参考文章:
/ H# W5 O) d# c8 V& Rhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX& p# w# {( M" s0 z. d; z4 R2 V
http://drops.wooyun.org/tips/2614 |
|