|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。! H2 i# E7 Z* Y+ d; d
7 f: K2 R ]6 `9 i/ ~; O一.准备工作. J$ t$ \0 L* J# t( C" b: r
; z( a) F s+ n9 N) X
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0* W, p+ k, M- T6 ] @
4 ~3 G6 o% @8 K2 v( v. N7 b( q( vtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz5 _2 ?- u3 J, `7 o9 L9 w# f& i
6 R3 v% h4 }" q$ i6 bmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz$ q/ p3 S; T# O- S' Z
; K$ X/ y6 z' @0 ~! u
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
) `# ~) }' \, B" m
7 G6 @: i* [- p# o2 i依赖关系:1 X. S3 ?* | s9 r
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
5 K$ b! r4 x. g! Q9 n' R' D
, N2 g( c% R! H+ Eyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel3 s G4 p5 T$ z; W5 S( m3 c
modsecurty依赖的包:pcre httpd-devel libxml2 apr. O# @) A. }! a8 k" w
/ O; t. Z" \4 n7 x3 D$ [yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
5 o) B$ v- s6 s: w; V二.启用standalone模块并编译
/ u: A4 H! K* E/ y! o3 f8 m
J2 P6 D! {" x下载modsecurity for nginx 解压,进入解压后目录执行:
& ]- b3 D; x4 c
% ?) Y+ i* q, N: b2 u./autogen.sh$ R3 q5 ?: `6 o% n" y# P
./configure --enable-standalone-module --disable-mlogc
+ b( H7 S3 X' n$ Z4 d$ S H3 kmake
' h& a; o% K7 V( z; ^ O三.nginx添加modsecurity模块3 L+ n" m* } G0 v* n& V
0 P/ u, L8 [' w7 w. C在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:( k1 C, z* M1 k: V5 d; b8 N
! l* |! a; q+ Z./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
/ a7 M+ w: e3 u9 A' r* j" _make && make install
. A Y# H! T& x四.添加规则/ N; O/ W3 Z1 q
4 G( a' o+ f8 M8 x7 y
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
0 h! m$ v, e2 ^( [9 @0 l* }0 V# Q" n& X$ X: z( H
1.下载OWASP规则:
! S% {3 [3 G7 c
, P5 e5 L- O; h5 X. \) M/ c1 fgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs+ g* M5 c2 E3 i( e9 f
( Q- n. `! X6 p- z! D! r
mv owasp-modsecurity-crs /opt/tengine/conf/! c8 Y, t5 N, r
% d' W6 L* f! L" |cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf8 E: E4 d3 Z/ Y! G$ f- H j; }) A% H8 f
2.启用OWASP规则:
( B+ v9 P: \1 C# g' a x7 o1 i6 i7 {3 W/ F4 o) X) G
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。8 y# R5 ~/ L6 v+ ~" a
% i3 I# O: M, n; H4 K; C7 l: {
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
9 v8 U' M7 C3 F
2 F6 U# x! l" Y, B0 x1 o3 i4 Nowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
. j. u7 v5 n0 s9 s4 Z5 t* p% K7 }- ^$ ^
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf" L4 j9 `( n7 ~# b' t7 Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf8 b% ~' H0 i# b7 ^ W6 l
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
# b k; w; u$ ^: N5 M# f* lInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
! Z7 T/ m$ ~" \; t- KInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf: r% w2 j8 n- N) E# u1 l, X/ L
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf; ~; ?+ O; D) A- {8 P/ U& K
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
+ D- o, u1 C: B& E9 O五.配置nginx
* V5 \( ~- N" M( G
5 H6 g8 y9 O( w; H) @- i& N在需要启用modsecurity的主机的location下面加入下面两行即可:* }6 ^* S; E. s3 g
/ ^4 i c- c: m8 J( v O& y VModSecurityEnabled on;
0 L6 X, `3 R @) w7 R% C3 V6 w* oModSecurityConfig modsecurity.conf;
( `8 I/ Q ]5 v% G下面是两个示例配置,php虚拟主机:
! [7 M3 c% N3 a9 Z% c6 D- O
8 j0 V7 n: A( Q8 e( [0 J) }server {9 A. g( m3 m3 ]+ X; W3 s
listen 80;/ y" P j, L8 F/ N$ s
server_name 52os.net www.52os.net;
; \5 d9 a# G# O3 ?2 t0 V + Z$ n/ ^: `; y6 z, H
location ~ \.php$ {
4 X( _4 e7 F/ ~% f ModSecurityEnabled on; . q: X/ F4 k9 }' K# [
ModSecurityConfig modsecurity.conf;) G1 Y8 V0 O% A D
* B) ^6 U( |# r: ~# Y
root /web/wordpress;! R+ Y& R9 E- R l9 V
index index.php index.html index.htm;2 l% G+ g5 u: R6 J$ D8 l8 Q
4 y! g5 x6 D- {% l, ^ fastcgi_pass 127.0.0.1:9000;4 @) O3 ~1 h$ [* O( _! H1 s& A
fastcgi_index index.php;
5 F' ^! c+ c& i- h fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;% |# ?1 z8 i$ F0 C! Z& B* ]$ k
include fastcgi_params;
2 o0 Q$ r' ~' {6 F) T }. O" ]. p% \8 X5 D6 m; ] |
}; U) `7 E x& Z. o* A& h# ~" s! ~- r4 b
upstream负载均衡:
3 f6 A# I* u& U* F {
j9 ^9 K6 i2 W7 o' U8 U5 R( Supstream 52os.net {
6 o) H; j& R/ E* Q/ N, c server 192.168.1.100:8080;
8 z% v7 X' v* x! l6 G server 192.168.1.101:8080 backup;7 X. Q& J- s8 w
}' M5 Z7 Y7 J- \' a" x5 E+ p) w
, @5 @ v2 C# t6 mserver {
$ M* g4 p: x, ^6 p8 Glisten 80;
' k& j- W9 ?% p# H* q2 [) @server_name 52os.net www.52os.net;% a+ d- J+ u' ^5 Z2 _3 `, B
' X. K) x$ O1 J% D2 ]8 tlocation / {/ l+ [- T( K1 b2 f! X) \
ModSecurityEnabled on;
% ?- a* i; G6 n ModSecurityConfig modsecurity.conf;
/ i1 }: i' `" V% R! U- U
7 ?/ R8 f- Q0 h7 ^- Q proxy_pass http://online;# E9 ?1 T9 X& f) d+ L
proxy_redirect off;
9 @# ]3 b/ ?2 Q3 V proxy_set_header Host $host;
3 A" i3 v4 n0 \3 z+ d. J* E proxy_set_header X-Real-IP $remote_addr;+ r8 O0 f2 w! t7 I& Z
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;1 u$ P/ H' a- H: B5 b- r: k
}
6 p" [& B2 P0 W# R/ v c}- f+ L- a# |$ w, L& F; K/ u ?
六.测试
) ?3 n5 Y# S$ X" p' n5 b% f6 A5 e7 y5 X
2 w ^3 i- q; M9 x% a# }, L* I我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:4 m* g; _- ~9 Z3 _
: J- x! {. [5 I3 ?
<?php
, H9 `. ]* k" w( w/ m" s+ E. \* Z phpinfo();
. h1 ^% u2 J% h$ t/ f0 ??>
) S5 V; a( l, \* W在浏览器中访问:
6 m- s) x+ f) H1 U( l5 ?# s- G- R
" f9 O% m1 e% G& N Whttp://www.52os.net/phpinfo.php?id=1 正常显示。$ y. h# \( g& e8 r
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
: [2 i' b* z$ E5 C1 g- |4 zhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。5 M/ w% b7 c/ {7 Q# z5 k8 u7 w! |
说明sql注入和xss已经被过滤了
, _6 i! E2 x- \6 S7 w! r
6 L: y" a( w; C* H七、安装过程中排错6 r2 x0 G7 i. i
9 Q7 s1 h9 m; E, o
1.缺少APXS会报错
0 G! z2 c( I% q3 }) }# T( V) v4 ~# p' H0 W
configure: looking for Apache module support via DSO through APXS
# K @2 U6 n, Kconfigure: error: couldn't find APXS" L& Y# k3 r. n+ |5 f' D0 t4 D
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。5 D1 y$ T/ C+ ^
解决方法: a5 k7 C5 B3 s, b- V- _' H3 _
& |% o; f1 }9 D4 i
yum install httpd-devel
. ^6 F+ @% W4 D7 T: i! b6 N2.没有pcre
6 p% C8 q* H+ j4 }# q& F9 n! q9 B% V
4 T- W0 G+ K; N$ F- f: ^% nconfigure: *** pcre library not found.9 A. x: c1 \6 o; g
configure: error: pcre library is required1 {. ]4 |. d- Z
解决方法:
* M# K- U1 f, G5 N% Q
4 m, w" ^/ y. K! y( z' nyum install pcre pcre-devel
( v: |( N. Z T" r/ L3.没有libxml2
! E* o2 O! d: X& ^# z. o0 o2 c/ g f+ s! T0 x9 ?6 W: y' B
1 n/ f. J3 J9 G' t
configure: *** xml library not found.9 H1 n! k5 v; ?3 }/ p( W- x& R2 L8 B
configure: error: libxml2 is required
- A0 G2 D" b2 e# g' Y解决方法:
# Q9 u) k l6 a; l4 x+ M' X( I; R% d# l( d% Z. }
yum install libxml2 libxml2-devel$ }5 n* d) N" x- ~2 a s+ ]: Z4 k% }. ~0 @
4.执行 /opt/tengine/sbin/nginx -m 时有警告2 F/ i& l+ D, M7 c6 F1 J7 V
5 e4 J, ?2 M9 ?- i% y" JTengine version: Tengine/2.1.0 (nginx/1.6.2): Z" k& `. p7 G/ ~$ A; q) h( Q
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
( P; N8 f- J. a7 I% R, O7 D原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log3 T7 ?8 F* y. ?1 C" D; s
# D$ L3 V; g8 ~- ?& @9 R a6 t
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
/ U3 S+ S6 S5 ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
4 Z; L) y K. m' f$ |2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!% \8 p9 {; @- d4 L# h
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"5 M3 ^( E7 y2 w
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6") W. r: X8 o# N% \9 w' i( a6 O
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On., K& K: T* r9 x$ u
解决方法,移除低版本的APR (1.3.9)* H7 a0 a1 F8 J$ k7 p2 Y0 K
( F3 [) ?5 a" y
yum remove apr
* w% t. O0 Y5 f. @5.Error.log中有: Audit log: Failed to lock global mutex. n% [+ D& F+ n9 q; y3 V, ^& B
9 X7 d! i/ r. b3 T6 n0 k+ C
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
! m7 p- j9 @( G$ g/ g8 gglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
8 X+ Y- Q) c- a; S0 n$ A4 B# T解决方法:( h: p3 V5 |1 g6 g
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:4 \! B6 L5 B# H& A/ G3 i F( e' C
% x6 J. t; J7 _% f5 E$ L
SecAuditLogDirMode 0777+ Q5 v, J& h* z' F% M
SecAuditLogFileMode 0550
/ }( g* y T* j6 w8 [SecAuditLogStorageDir /var/log/modsecurity, s( H) g9 f7 J, D
SecAuditLogType Concurrent
1 k" E. Y7 s P( T$ D$ e参考文章:+ W% ? f2 B' J4 B8 }& {
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX5 O& L6 M0 ]4 c# V* N: X8 `
http://drops.wooyun.org/tips/2614 |
|