|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
2 s" O2 X3 a4 B# W% `7 \& ]( q8 ~8 }& {5 f, o8 ~- b4 H
一.准备工作: m. K. i. V1 Q* m- p% b9 h
! b/ c$ d" r3 B% m5 [0 b1 U系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
5 Q3 _) `: f$ V9 y: H& z
2 c r3 J& t8 f& e# M/ \$ V4 t, [tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
% I) A$ `/ y& Y8 k' V4 Y6 j$ y/ e* _* K0 x# m6 T
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz3 p7 V& `/ t' r! h" I4 l+ v W
9 O9 F, d2 O' L# {OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
8 m, O/ g- s0 I/ o/ K3 f: ]% S( ?% @! V' f: ^# v
依赖关系:9 x! l J, O X7 {
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
: T3 l" B5 _" |9 p
) c& b# R; L+ l5 ]4 A9 l; s) oyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
/ Z) Q B2 C! Z+ `modsecurty依赖的包:pcre httpd-devel libxml2 apr1 B( y/ f/ ]% [8 C0 R' h& [
, M& r: U5 ^/ `) x8 N
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
; K& x8 {9 i/ W A二.启用standalone模块并编译8 A& S5 `" ? y' G; b9 R1 a
1 V9 ?, Z% M; V/ ?* W9 T5 u
下载modsecurity for nginx 解压,进入解压后目录执行:
1 D8 ^3 P7 G. h5 k4 J/ r; w, `) T: S. f8 |$ B
./autogen.sh% }0 Y% X0 w8 V& u5 [6 E
./configure --enable-standalone-module --disable-mlogc+ V$ s" V- _( a7 C9 w4 S
make
$ K9 f1 H/ s8 ]! O0 }' F4 m三.nginx添加modsecurity模块' l; W" f* q& F7 L. h; S
4 i& |& ?- a+ O& F! H3 f
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:+ H M8 ?" c3 t; D
* ^4 K8 h8 b) g! F3 t9 b; k7 p3 ^$ f./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
6 u% x1 @$ H+ Smake && make install4 E: M" Y W% f- H) |+ g0 p5 E
四.添加规则
3 h* W* R' a: U4 p$ H1 A7 a" \# @# I9 e; d
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
* w2 F$ G- D$ W2 G2 W/ }: V# k* k
1.下载OWASP规则:
5 { I: u# O) D) l& |/ Q2 ^9 Y
8 ^$ u' ?) V" i3 l1 t: q! |- Ggit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
. R7 q- q% l2 ^. x! u) ?; K0 o
# R9 e; w! [5 ]3 R! c* rmv owasp-modsecurity-crs /opt/tengine/conf/
6 {6 l1 }5 X5 B, @) d; a. O( V- [/ e) B4 C; H- F
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf5 k7 C5 ~3 j% p" `
2.启用OWASP规则:
( ^- @( @2 f0 z C/ o3 I2 ?) o2 _* Q- s3 p$ W: _
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
0 H: w- k/ Q5 z0 R6 G8 Q; d, X7 H) c7 h1 A
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on" n! f: e: y+ d; v8 h) b$ r
) |& w( f7 }0 G9 _& j: u
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
g0 x! K( j' k) g. a; ]9 E2 V+ z0 ?/ K; [
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf* x# a4 _/ ?' i' N
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf/ K! [ Y, [: x6 F; Y/ g* `) l
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf/ j' o9 Q+ P0 F; s$ q% {0 c
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf5 V# H: y$ j+ P( k, G N6 R R
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf3 B0 o4 E' g& C& W- P; f9 R; g
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf7 K9 Z' V E2 C
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf; Z5 k( N8 w7 E# d
五.配置nginx2 H8 L1 Q& [; f v/ D9 [
. c: g1 a2 ?) \5 g: G! |, ?在需要启用modsecurity的主机的location下面加入下面两行即可:
( A' \7 w* z* F/ b: }6 Q! ?+ c* t7 y' g8 h1 G' A% d2 `
ModSecurityEnabled on;
, N) o/ Q( u- r6 p& I2 s+ PModSecurityConfig modsecurity.conf;
* ~$ I8 O( Y7 j' s/ J( G' o下面是两个示例配置,php虚拟主机:
{( |) X9 A+ c( \0 x* m
2 n/ l( h9 f6 @$ u! {" Bserver {
& H, X6 |, g4 f7 V9 B' k listen 80;
3 L3 b* |( p6 Y" k D( A server_name 52os.net www.52os.net;
& u6 S0 W$ R* o: g8 T) R9 \
+ t! l0 A8 n% d( o. H1 | Z location ~ \.php$ {, t$ U4 k6 P8 z% G6 m$ \# s
ModSecurityEnabled on;
4 K4 f% {# ?" N# a& ? ModSecurityConfig modsecurity.conf;
7 }& C+ o2 ]1 p* p# E) t
3 d# ~# @- r* B6 c root /web/wordpress;
7 t' L; [4 c z, t6 k3 b0 S" Q' ` index index.php index.html index.htm;/ F8 ?7 H! K, m
% E6 w, E4 v6 t+ y fastcgi_pass 127.0.0.1:9000;# P: m4 ?- R) L( \
fastcgi_index index.php;2 X) s: u9 c' S3 Q' B
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
0 f2 ^( G+ U- \ include fastcgi_params;
- d' R* x+ T5 J- y: E }
0 @/ d& T& U5 D) t0 `+ _ }
* t0 _5 y0 ^. n6 |upstream负载均衡:
( F4 s6 m1 [3 y7 o8 v3 i* W% J( I9 \6 W
upstream 52os.net {
% z9 m x# U6 q7 g- D1 O; U server 192.168.1.100:8080;
2 |. X( n" u! V0 s server 192.168.1.101:8080 backup;
4 l7 H& |& k& f4 c% J/ C( P3 K: N}& G; a' \/ ?: `- g* }" N6 W5 h/ P
4 T& ^" m H( `: v8 Pserver {
( m1 ^" ]- j1 g8 @8 b: c2 j5 |listen 80;
2 Q; s) ?6 s( C1 }' Oserver_name 52os.net www.52os.net; r- ^+ d5 O6 O6 n
m' F5 m& c0 W
location / {
2 q% [- s5 c( [# l ModSecurityEnabled on;
" a; @- P" t3 |. ^ ModSecurityConfig modsecurity.conf;
+ l3 {2 w- e6 v3 I% \1 A) q( @3 G, B, z4 q/ h, B$ k5 B) v
proxy_pass http://online; N6 }& _2 K0 L: R. W- O5 G7 c2 ^8 d
proxy_redirect off;
/ q) Q3 ]8 I% `2 b3 s proxy_set_header Host $host;
/ R& j7 z) o' G/ \$ K1 ^ proxy_set_header X-Real-IP $remote_addr;0 h& E- W8 @ P7 h8 B
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
8 k) A/ z4 K) T/ q- S2 D4 ^ }' v: G4 o7 G6 o) h5 q
}
# v6 U4 z9 ]$ V2 K/ O2 w5 `六.测试
& W" D8 Q [) R% T2 ^' {9 v
0 i1 T* B7 R0 e) G1 Y我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:3 t0 g; y0 b- W% r7 o
9 {, K" z- \3 j
<?php1 a! z+ q( ^+ \- C I+ r; R8 k
phpinfo(); + @0 q4 i: W6 y( Z
?>& W/ ]8 p; Y' E ~
在浏览器中访问:
: C y; C7 Z0 D0 J7 m
. j* @: G8 p0 ~, @http://www.52os.net/phpinfo.php?id=1 正常显示。
) `% G9 o6 t9 ]# a# Y, C5 b6 d1 r+ Hhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
1 w! Q# Q m, f$ Phttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
$ b* c- N5 E, c说明sql注入和xss已经被过滤了
7 p1 ]- F- S8 r% o3 i, D5 \/ w! g- W
七、安装过程中排错; K2 W8 b, `$ N* K
; G+ V! z/ u. P+ z4 J& k1.缺少APXS会报错8 ]* ]# v* t" [, h8 \8 x
# g7 \& r/ q- r) H4 B; ?- O
configure: looking for Apache module support via DSO through APXS
! k2 ~& o6 [2 g3 p5 pconfigure: error: couldn't find APXS3 v2 Q/ i7 `$ f! K7 H$ L) J8 q/ C
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。7 U" V+ R5 _/ G% y
解决方法:3 D* J" ^1 L# c" Y5 O/ @
5 H9 z5 u: S! t( ]yum install httpd-devel
& Z7 d2 \) U- c1 o6 h2.没有pcre% }" R% j; L/ D
5 K$ f- s; h3 z/ g" G$ l7 n
configure: *** pcre library not found.
8 ?3 m4 q) q; }4 o8 }# F0 \) Dconfigure: error: pcre library is required
- B: T' A+ U/ ^* ?( u0 V解决方法:
# o* Z- |' p M0 N l { m% @ M6 _5 ?1 i* t
yum install pcre pcre-devel+ ` J7 h0 b8 |# u9 q* j
3.没有libxml2( ~9 _7 ~" W7 E7 |( x
5 B% Z) w* I6 W; ~7 T
- ]+ A8 F/ o4 q: w% d! S @configure: *** xml library not found.' ]! `1 E q/ L: I3 a1 M
configure: error: libxml2 is required2 M' T' k* B6 ]3 `: w3 s
解决方法:: j8 a( E& }9 m$ W$ v
1 Y$ ?% x# k2 D
yum install libxml2 libxml2-devel5 z" x% A% H4 C' N1 L
4.执行 /opt/tengine/sbin/nginx -m 时有警告4 ` h% z% Y0 d( N/ q7 E9 ^: d, O
6 [5 Q% Q: D* G% x
Tengine version: Tengine/2.1.0 (nginx/1.6.2)) ]' q1 b- O6 s, `1 o, }' P( o& u' S
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
9 D% w# l% r( h5 u原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
3 [- H# c' O7 ?" i/ P( {& E0 J, D6 O1 _5 Q% K! v
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
# n6 T+ s% r4 g. H' U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
* }5 R1 b/ M+ x4 Z2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!! H1 H; i% B. B8 k0 A S
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"; V/ i; r: m* r4 m* _! S
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"1 C, K9 Q. _4 X, S) _; l$ _
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.6 m* h5 Q: ^* g' ~
解决方法,移除低版本的APR (1.3.9)
7 X/ x# J; V5 d: I: V! c. L: i6 k/ k6 _
yum remove apr8 M# f6 |5 J: p0 L3 B+ B; R
5.Error.log中有: Audit log: Failed to lock global mutex
4 J& d, Q# e4 q: _, O3 I! [ w& n; L5 m% c
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
2 j8 N6 q, n' M: R" z( Oglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
: a, a% D. l1 a* Y解决方法:
" ]6 L+ g5 i& S1 v编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
3 o% a; l% y2 _ a' i4 Y/ j& r3 f, V" w7 P& d9 ?
SecAuditLogDirMode 0777
3 H. U2 J4 a( R- b6 D: ~SecAuditLogFileMode 0550
" q- Z7 U+ h! K R& W5 d: }8 wSecAuditLogStorageDir /var/log/modsecurity8 N* q- T+ d- `4 p
SecAuditLogType Concurrent: |+ h2 k7 y- |! Z8 T. T
参考文章:
3 ~8 y- Y+ `" z8 C; x0 lhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
" k/ Q: [+ A& r# Q& l' K' _2 ?http://drops.wooyun.org/tips/2614 |
|