|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。4 o: J, u! j3 N3 y* E
% Y# M4 x6 O- b
一.准备工作0 g$ g$ d' E( o& E$ Z h7 f9 v
3 T) _! i Q8 A: @
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.08 Q, f7 o2 {& i$ l( o
" o1 E( t3 l9 Z/ D3 A8 itengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz k/ B+ w3 p2 u
]% R! k8 [& z: Y6 c* k
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
: U5 Y5 ~2 l7 Z4 E3 f, x- z. u4 [$ v4 ^. V1 U
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs3 A" n6 d1 B# H. F% s
" @% \' `+ v* m2 t; W
依赖关系:' s1 q# E8 C1 k- v( L9 _ ]
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有: B8 Q8 |( h; r* l3 @: x, X
9 M5 e, Z5 s n1 O) m1 \+ O
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel6 s( B, @2 j! c3 Q1 W7 w- K$ E( u+ m
modsecurty依赖的包:pcre httpd-devel libxml2 apr+ ?: c/ s, L5 @- v: ? J
1 F6 P, i% o; F b. \yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel% H, m S$ c6 a6 Z' \
二.启用standalone模块并编译4 c L' }- o9 Y" M, v# J3 [
6 `3 ? P- b; n5 Y9 R( S) G5 F5 E
下载modsecurity for nginx 解压,进入解压后目录执行:
8 j7 f! f, l; W$ \& `, @
$ ]( k9 A1 g: K. B./autogen.sh
8 t# X# e! x) p./configure --enable-standalone-module --disable-mlogc
. X: z5 Y' Y& S* _! l7 z1 vmake 7 A4 P1 O) d- V$ j
三.nginx添加modsecurity模块1 Y' N" m" y) H' J9 n f9 p
% P% c' _1 n: W6 c( s t
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
. m0 v# x, J8 H+ s) _% E! A2 `1 [) R3 m7 w' f% L3 W% y
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine& o" ~$ z7 Q4 B+ V
make && make install* R" a- n! L4 ?/ O1 A( v0 p3 T% K: }
四.添加规则( o4 q7 u$ ]8 u( N3 r" Y8 ~ W4 z
: r3 N6 E5 S9 k9 D
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。 ]$ W6 x. J- y) a2 }
7 }: M, Z2 F) \4 x5 e
1.下载OWASP规则:, y, A: ?- k2 U
. Q: l7 E" v, R/ R. E# l
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs4 h' B* t- {' O9 K( V- m
+ C, S9 W( Y& |3 G
mv owasp-modsecurity-crs /opt/tengine/conf/; X% ^8 V% Q7 i( _! }
0 e+ R5 \# Z' t. }3 W. b5 q n6 m
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf+ D% U) f0 ?) ?% g/ G
2.启用OWASP规则:
4 R& l" _4 g. w8 x9 ~+ M' o, F1 w, K$ W1 d3 b8 R2 `; W8 B2 O! p
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
b, N( S; J" m
( ^4 x7 p9 j9 P# K8 x1 N编辑modsecurity.conf 文件,将SecRuleEngine设置为 on7 p" e- Z* }3 I6 s
: h9 V. {/ q7 f3 n" Q
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。" a2 W& h% M* R) ~
) T+ ?) i/ R$ P# JInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf9 `3 j. X! h5 ]0 E% \! y' t+ p
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
/ ^0 b* ?$ j- {Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
( Y: {* M- P% [- `2 `: n, Q7 I9 CInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf3 e% z/ Y* N4 A2 ^ D+ r
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
V8 s! `: C- v$ m- [9 t3 yInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
# y5 K# F8 T/ }! y' |Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
) v; A- ~% z2 k5 D! w% ?$ S2 H五.配置nginx
) \$ A8 N* _% Q- k; R) W0 I4 u* B( o$ @% V# U) ]. F) J
在需要启用modsecurity的主机的location下面加入下面两行即可:* s' V8 D. V6 n# V/ R$ L) M
: n/ ]2 Y* F' F, u
ModSecurityEnabled on; 8 d$ W' ?5 b5 M) z: M: k" V
ModSecurityConfig modsecurity.conf;& w6 U* a6 ^" F% @2 S. H
下面是两个示例配置,php虚拟主机:: P* ]3 W# X& @2 i- `9 }
8 D" A, v, L9 z) P5 S$ e C
server {
r3 Y. f' F* E; v+ s listen 80;0 Q: y# X9 ]% h( p
server_name 52os.net www.52os.net;1 u; f' F1 ?) z2 Z& @: f
+ b$ l) ?/ E) w; x+ a: I location ~ \.php$ {4 {& P, ?, a8 `+ c2 V1 r8 i
ModSecurityEnabled on;
0 {- H6 A/ Q* X# j# b$ e/ Q. I3 } ModSecurityConfig modsecurity.conf;
6 w& x& G4 z# Z: h* a7 z6 F& ^7 m, X
root /web/wordpress;
9 G; M/ C6 e& `" j6 H index index.php index.html index.htm;/ T P9 C6 c5 b+ d. s; j
1 A) g0 y* S# |& Z! m8 B. K3 a" A fastcgi_pass 127.0.0.1:9000;
7 a v. R7 a% j* Y y2 w7 p fastcgi_index index.php;, U! l( }1 E1 h& m6 K+ z9 B
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
9 G1 G4 s7 V% L# z2 E7 b" G include fastcgi_params;3 X4 q; M& P! T
}! K" K0 K' R9 g) X) R
}8 z& t+ ?6 }1 K! D7 T
upstream负载均衡:
: F2 k$ H! I f$ P% Y5 h: o
4 _+ f7 Y4 I+ E, Y0 ^: bupstream 52os.net {
0 H) ~. D! n, K5 E server 192.168.1.100:8080;" H2 O9 Z# [6 |7 g
server 192.168.1.101:8080 backup;
; T: B4 A$ B9 u. s7 ?& N s' |}/ g; w# x' h) U2 k4 m7 A- g
3 R) t: K9 \/ L1 L7 k8 D$ V
server {
$ k, ?! c( q( S# p$ @' elisten 80;
5 I5 g# [; ~! n' ^server_name 52os.net www.52os.net;: ?9 k# e8 i" A6 @" z# L) ]$ J
. p3 z# u: k% E' Glocation / {
% {, s9 K% F+ @; T ModSecurityEnabled on; 5 s0 m! q/ A/ a e3 x1 ?2 [7 g
ModSecurityConfig modsecurity.conf; " _$ H* |" v) p6 K& \+ V1 j
* i5 E" h1 a% i proxy_pass http://online;
# r, n1 j) c& e! J, e& V proxy_redirect off; X7 e1 M/ Y$ P# s B& |, M
proxy_set_header Host $host;
1 x: A5 p& x. ~# G. D* | proxy_set_header X-Real-IP $remote_addr;
1 L, ~5 w2 i+ @& _' ^* Y! } proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' v5 _# m5 e" \( i* g
}
. t) A9 l* i7 |4 H0 r$ f}/ g& j& A2 ^( ?0 O. i. h1 l
六.测试$ D0 p, t+ P3 h# q2 }
. X) o9 I6 `! s/ G5 ? b$ C0 G我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
L* G: c% S: e% W, V/ m" y% R, W0 S5 t
<?php
0 w& i; L+ V6 k phpinfo();
2 U8 |' C. U+ j- a4 @?>. d0 h$ }: E, E. [
在浏览器中访问:! b0 m& b9 s |% x
* r, ?$ r- c$ ]& r' ~; t6 T$ W( ?5 vhttp://www.52os.net/phpinfo.php?id=1 正常显示。. I/ W: w0 U4 M! J. _, \% @$ `; u; h
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。5 l- U$ B6 J4 k3 X$ a
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
7 L8 Y8 S9 ~; j3 `: o说明sql注入和xss已经被过滤了
* a G! h; u7 N0 V
4 V. X9 E' P+ e七、安装过程中排错
, v" J* Q$ P W$ z9 _9 ~2 |2 P- `& G) f4 X
1.缺少APXS会报错
) m" R$ z; J* X9 d4 k ^7 m
4 E4 J" z8 @1 L. Jconfigure: looking for Apache module support via DSO through APXS* u1 i! f8 i( Y* i' V! q% F# D
configure: error: couldn't find APXS
2 Z1 w7 p' S- Yapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
# P: O, U* e& G7 Z9 I解决方法:
4 \. L0 \& }& g; X& u9 [
- |3 d8 m9 F# [yum install httpd-devel+ N% d/ t7 j) u. z M6 t5 x7 f
2.没有pcre/ N; n% h& B3 j9 V! F
4 s1 T6 L) v; l A' b. dconfigure: *** pcre library not found.0 T6 l5 q5 }! H) s ?
configure: error: pcre library is required" c6 r% j5 y% B+ ^
解决方法:
9 b) E8 Y2 D* X7 F0 B T- \- e- _* I+ K8 j! T
yum install pcre pcre-devel3 P* r. e( o( X F+ q8 y
3.没有libxml2
. w- {6 N" e; c9 o1 Z& m& g$ z0 |8 I) c- q0 s2 E
4 _3 L' }% c/ U" X) W( ?: w6 Y& Zconfigure: *** xml library not found.) L! P1 f9 P3 g) S; F% G( g
configure: error: libxml2 is required Y4 j, X# |; o& L
解决方法:
{- ?3 L5 L4 X, a: ? `4 E7 C4 @+ j9 J
yum install libxml2 libxml2-devel
' \ |2 q6 N2 y9 l7 y; I" H4.执行 /opt/tengine/sbin/nginx -m 时有警告3 O8 B3 D0 A1 R- t+ p
: [0 M8 M# z$ c- z* C+ W- HTengine version: Tengine/2.1.0 (nginx/1.6.2)
9 y- a. M; w) o9 f& O: C7 snginx: [warn] ModSecurity: Loaded APR do not match with compiled!! N; a* u# B9 c- z1 f
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log5 K& H8 y' K% H8 Z7 @" I
0 @4 Y, @- m% q1 x, R; z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
8 R+ ^1 E9 u+ a( Y0 l) R2 t2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
: K* \+ z6 T& T' C C8 ~2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
0 g U! _3 S4 W: M1 z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
& n& @ }$ w2 W& q- w5 R2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
. Y' p# l, L! Z' O- M2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
' a2 P" v. ~* y8 q- h9 b解决方法,移除低版本的APR (1.3.9)
) L4 p7 b. n+ F q& P$ U; q
8 \/ y3 t8 f. ^% K j L- Cyum remove apr) X2 Y0 Z- A3 S- o* Q3 P
5.Error.log中有: Audit log: Failed to lock global mutex# @5 l7 ?( H1 ^
/ Q4 C5 S" }* [6 B+ B2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
7 f+ j8 M0 `3 O* kglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
$ f1 |9 K& J; q% E/ b& m4 w6 J; c解决方法:
$ h" `( s# E1 z+ ~编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
1 r+ F' H. s$ t3 S8 o H6 o! Z% T0 q) V, d) n
SecAuditLogDirMode 0777# S& n5 N2 D/ O2 K
SecAuditLogFileMode 0550
1 g$ B/ m. G) E0 W: ^& b: _6 aSecAuditLogStorageDir /var/log/modsecurity6 b( B- n: j) T1 A, j2 Z3 Y% R
SecAuditLogType Concurrent8 Q! A7 z4 V8 \0 e: U
参考文章:+ A( H/ @' {- X
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX1 U* f, A+ n ]; M0 T
http://drops.wooyun.org/tips/2614 |
|