|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。$ V5 g% s% Z% i
# ~$ ?" |9 ~3 |' ^6 p' E一.准备工作
& B; V# x1 w! ? G- z) }* m5 U
/ L, b4 V0 K9 f7 N系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
S) o' h, C0 A0 D0 y' i* N/ n0 o% g/ L$ w' h
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz. k7 J; |2 T3 i$ l3 D$ h: ^
! B1 O+ n2 U6 G: B" r, {
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz5 [( F7 O) \) T8 ?3 G) W
; K7 T3 W$ W7 O" L
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
; d, @$ S4 E+ w1 j+ L+ d
7 p- y: M5 [! S( Q' c9 r依赖关系:
3 y1 D1 k( e3 b O% R0 Q* Ptengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:: I! z3 N* S: k( K. t
% c8 n! e3 ~4 m
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
& k+ n' Q. }) s& d2 g6 U4 x( [modsecurty依赖的包:pcre httpd-devel libxml2 apr
9 H5 ~1 J+ H% S1 t
3 l/ m# J+ c8 @9 x- n& }( L% fyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel, T1 k# w# g \( Y& q9 L
二.启用standalone模块并编译
$ |8 ^- n/ F: `1 l+ e' f, m+ u/ Q O( v7 q Q
下载modsecurity for nginx 解压,进入解压后目录执行:
9 b2 m4 X' {& Y8 ^) O- s: s0 c' B* h5 [, d2 _9 y' B5 `& D! z
./autogen.sh
$ A L1 `) ?. k+ ?./configure --enable-standalone-module --disable-mlogc3 G% ~4 J5 n) J/ h6 U& ]: h
make 0 X& a0 W7 r5 V' S6 u
三.nginx添加modsecurity模块
' M5 |& W8 c) E$ v5 e+ z
, j: Y' B/ I6 I, W在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
& H2 a# `: L3 j6 l8 Y [) A
l# @+ n7 ^$ i1 D- k' a./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine1 G P$ [) \, M) ^" Y: j& A. O
make && make install; P" G; z; l/ w7 f
四.添加规则
* s9 u1 i& S$ r" A0 n& |& J1 R @
7 b6 M" y4 Z5 v9 ?modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。1 j: Y2 u9 u/ K! r& x
* w3 H( n% T+ t# a8 ?! g: h1.下载OWASP规则:
: |1 C9 b+ t/ k7 g" s* X, K# d, D
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs+ B0 u$ d( y# f7 b
! L" M/ z! o- z% B/ ?mv owasp-modsecurity-crs /opt/tengine/conf/7 F) B8 o2 K- M- c3 Y" o
7 E. h0 e% z9 W+ h" w$ Scd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
" ~0 ?4 S& V5 k) t1 n; j2.启用OWASP规则:. G% Z0 m0 R) E5 W2 k( p2 C1 P' R
# u3 T& ]5 l# U% H
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
) g$ L2 J# }8 ]4 S: S: R" p: U8 m' X3 n4 c% C0 Z
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on- Q- K8 a( ?& }
. E$ n$ l) u' n3 q* h( J
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
$ p5 o/ f2 ] G4 M0 l1 M1 V+ ^5 x( G& h5 S0 e a$ c2 p+ |
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf+ K' x3 w$ Y* [3 P6 L
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf. @4 M+ J( l+ s; ~2 I4 o: s
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
% s d0 i/ ]! rInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf# K: q5 u- U2 j, v% `1 o" W/ d( E
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
2 T9 F) m, t8 N! S5 e( D: @$ `Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
# Q8 U7 B0 O1 W$ g& H2 `0 q1 TInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
' S& m* ?4 U! [. [6 C# R五.配置nginx
3 l- F) T1 b0 J9 S# `8 m$ h- _4 q+ n* j" @) S
在需要启用modsecurity的主机的location下面加入下面两行即可:% V0 E! R( f0 F; G+ y$ i- ?8 V
8 [9 q/ L* b% p3 m7 M* T0 N6 R/ FModSecurityEnabled on; 1 n4 O: S5 j; ?5 S- P
ModSecurityConfig modsecurity.conf;6 {/ {9 b; ]' z* \" D
下面是两个示例配置,php虚拟主机:
3 L$ l+ ^+ `( ]9 ^- y2 g
% m1 K4 Z5 \" y' [1 L! [8 rserver {
# @: f4 F3 e8 J, X3 G9 o. q listen 80;6 q( T6 g. ^! K& X0 m4 p' M, n* e
server_name 52os.net www.52os.net;
3 J* s2 d2 V2 M1 E' o& ]# h
- x" z4 u8 p8 H3 _! ?& A location ~ \.php$ {
4 \2 H7 v( \; M, R+ e; k ModSecurityEnabled on; / g8 r* J8 D7 n" \9 Y
ModSecurityConfig modsecurity.conf;
0 y5 w, t: M y% k, i. H% Z$ S: ^2 p, Z' l k; D* c, i
root /web/wordpress;
1 k+ J9 a& O( O+ n# _2 b& ]) ^ index index.php index.html index.htm;
2 ], z- {/ \7 T I5 Z
5 @, C9 l! v2 g/ x4 f$ n, N fastcgi_pass 127.0.0.1:9000;
: H( P# ?/ |* }/ g fastcgi_index index.php;
8 a( U/ Y$ m4 x' V! i fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
: H8 C. I) r7 J2 a& ?) g7 h( w include fastcgi_params;1 s) q0 j q; V6 C( F- x1 |- C
}
1 e8 o# q# F9 G. u% e! U. o% P4 A }
7 U# {- f7 \ h+ c: B/ Gupstream负载均衡:
! L$ D* y$ g: I. ` X4 }$ E1 a
7 v1 G: E$ d/ m4 y$ p- @upstream 52os.net {
( N: y( A, r2 z1 m; V4 R server 192.168.1.100:8080;
" Z- O" ?$ o3 X) S0 a, j server 192.168.1.101:8080 backup;% b# m1 Z3 k6 ?, g9 K
}) C! m. A2 Y4 x$ V9 \
. X7 T, N {! Q9 M' n' ~server {
5 Q! K2 H% j5 \8 U' nlisten 80;: R/ e. S: m0 W. N3 |' ?' K
server_name 52os.net www.52os.net;
! g' G6 w! c7 g* P# B1 @. c. ]
' @8 Y% w: V2 q$ A0 Llocation / {! d# r) ?9 p9 R. w- r7 P
ModSecurityEnabled on;
) @, `/ i: O3 X ModSecurityConfig modsecurity.conf; " ?8 y/ e/ Y2 E8 k0 w8 g, ^! J) Q
6 S$ J0 {% Z3 h* z) c& O proxy_pass http://online;' i; }! ]. N' @1 f) c1 W
proxy_redirect off;
" N# u$ W- ~9 n+ W/ s proxy_set_header Host $host;
* K8 ^- B9 n) ~* b- E2 S5 r+ y proxy_set_header X-Real-IP $remote_addr;
* N \ @# G6 _, Y5 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;1 S4 ^0 c2 G% Q: E) ?% Y* V
}+ Y( q, D! F/ W' }9 ^
}
& w% ?8 y. N- A8 S8 D* d六.测试- f/ ]8 s9 g! r/ Q
, A4 I( ?( j- `' r1 [" B
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:% v! k) R/ M9 n% o
3 F2 a+ q$ S& { R- P0 c$ G
<?php( C5 u+ F- H2 R- P0 K( h* o
phpinfo();
2 t: P% L3 j W+ L" r+ b# |0 l?>
9 `/ z& X8 J) `1 W& K在浏览器中访问:% ~8 r: u5 r) J$ [
* D5 y7 h% ~7 M$ x( ~5 [( c
http://www.52os.net/phpinfo.php?id=1 正常显示。7 F: c. Z& H/ }& N, n
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。- Y2 K8 [$ T) M7 P
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。+ l5 f; o# D& a' e W7 M5 B
说明sql注入和xss已经被过滤了
" Q: o6 [1 Q# c6 e# P$ D
. I; d, l7 @$ b2 f# Y七、安装过程中排错, A* q% u( s% Y# x; G' O
$ R6 b: G6 S& C5 ~* f
1.缺少APXS会报错" I) K; T+ B- ~9 h7 ]6 {4 m$ D
. q) u9 o3 M4 oconfigure: looking for Apache module support via DSO through APXS
) T$ r1 w/ G0 J9 L+ ^configure: error: couldn't find APXS5 D8 d% Y' o$ z
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
1 X4 A0 E) X9 q解决方法:6 n# C0 V9 p* m- U( e% Y$ B
5 ~- O7 I' m0 I' ?
yum install httpd-devel; [8 f y1 c+ N* X+ I$ ~
2.没有pcre; q& L; z3 I! c& ~2 S, K) r
" J; F8 `; o- s
configure: *** pcre library not found.
6 U- q) {0 F1 o, `) mconfigure: error: pcre library is required
2 B& I) ^, a2 q' D解决方法:
8 U+ I5 I/ v: T2 p. u+ l) u' E$ K% ^+ D7 [% j) @
yum install pcre pcre-devel6 c, f7 j$ t9 U1 S2 `
3.没有libxml2' u! X& G9 G8 `0 I. z1 Z+ n
" [* K3 K S) {, Q, W- N
/ g! ~3 X7 ~+ k6 h9 H
configure: *** xml library not found.
# H, A: w; \) {, Qconfigure: error: libxml2 is required. h# _6 e! O( j( k7 k
解决方法:
# p, e ~: N. [/ r6 R& ~: v
1 h% R( ? f5 n. h8 m/ ?3 Lyum install libxml2 libxml2-devel' k2 m1 W. Y7 E
4.执行 /opt/tengine/sbin/nginx -m 时有警告6 ^' }( y4 E5 y, @- U a6 ?8 c7 T
3 y; G" k V- A4 C! M+ ], p, tTengine version: Tengine/2.1.0 (nginx/1.6.2)
8 Y7 P" n# X2 F; e5 xnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
# H: n; N* O! _ {9 [原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log0 f) {2 d2 U4 e7 I/ ?6 X& p) c" K
y! o" {- Z3 _, ~- H5 o" _' y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.8 t. L7 u! d' D( z6 q; {8 I3 `
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9") Q: o3 p, y& f W4 Q% B% i" c
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
2 K6 U* `) z: ^, \' P4 w2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
" ?& ]7 D" z) F# h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
4 G8 C; P& t% q# U4 K4 U2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
- @9 V7 H4 r: F3 W6 w& i/ L解决方法,移除低版本的APR (1.3.9)6 ~+ [6 C# x1 ?7 }) J6 Q X
3 `/ k$ ]. z' y4 y4 h0 U
yum remove apr& X- {' C1 o- L3 \. k
5.Error.log中有: Audit log: Failed to lock global mutex! E" x, C5 a8 M8 m* i' p% @
+ i. ]2 J2 N6 A8 ?# n$ ^6 l, U# q
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
+ S' O! J4 [6 h! Qglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
' ^, M4 j4 g/ i: m" J0 m解决方法:2 _8 ^2 P( c$ R) R c9 M# d/ t
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:0 a) O& K* z1 i; r1 t5 g5 Y& |
4 V5 A# o" D& {, J& u; @
SecAuditLogDirMode 0777
4 `( q; h+ H ASecAuditLogFileMode 0550
: z' e- R' H. k- lSecAuditLogStorageDir /var/log/modsecurity
( \) V) N) K, X! ?3 H8 BSecAuditLogType Concurrent. P& \7 A+ Z0 P p% C/ C& V
参考文章:- W0 k! O9 x" K! a& A4 t
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX4 B: B! H/ m3 y, v6 O
http://drops.wooyun.org/tips/2614 |
|