|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
: w+ O" ^; D/ i. g0 f
& r* [! j5 M: ~一.准备工作
3 }/ f' L0 i: D1 B/ o; p5 g
8 q) ]! M6 w3 }" U5 D- L系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
4 j, g9 i6 B5 G% e. s
3 v, j3 c( X* U8 w: q" |) ?tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz1 t Q" s. S+ Q+ F# Y
* U" w: o) {5 x! U( Q, d
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz% `* c. Y" l# C+ Q) | i
; ?; g' H$ W, x7 wOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
+ ~+ |- { L: q+ E. \& {0 E }& M; ?+ l
依赖关系:
D5 J1 t1 F' y& Otengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:: ~% |- ^6 e9 R/ W7 J, W0 ?. c, ^
$ v0 d( F8 x# \# L9 |, U9 ]/ f! {
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel, _* _% a9 G) g! o' O4 m* b9 B
modsecurty依赖的包:pcre httpd-devel libxml2 apr
( o5 A9 g! I' C) y% C
* t) z) i+ U5 \# Nyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
4 X6 M" \% _+ H( R: D; F二.启用standalone模块并编译% j9 V& p6 U% g6 y% p2 a. o8 u( C
' Y3 U% Y5 ?5 |. H下载modsecurity for nginx 解压,进入解压后目录执行:
- v0 B# m ~! N0 I! S4 N- e: ]
?/ x+ p9 m# j: \6 D. W./autogen.sh
/ h& k l( k2 K9 {% u. s./configure --enable-standalone-module --disable-mlogc
) A1 j+ l7 |' j5 i3 j0 C5 s$ emake . n# v+ H) J( n/ D4 N& e; d
三.nginx添加modsecurity模块! w- r$ ?" j$ H, {% F3 ]5 y
7 T2 Y1 C$ q9 ~3 ?/ Q+ Y, F在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:8 C- C8 s6 J E$ D* e" B# _
2 u9 Q. ]5 `, M W2 m2 U./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine9 N% b7 Y f- Q( z1 D
make && make install
: L! D+ [! o4 b3 w/ d U四.添加规则8 h$ Q2 `& i* Z) }
4 k5 `. f P3 {+ Hmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。. P2 d/ e: b9 m# j6 |
3 E5 Q: P/ {* C
1.下载OWASP规则:
. k' R8 w+ Y- E4 `2 E% X5 U' n2 m% O) K+ f& n
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs( s1 W% w( w: X
" D0 s' ^" K* F! P3 C
mv owasp-modsecurity-crs /opt/tengine/conf/
( W& K# e7 ?' Q$ X/ Q- g6 @
5 P& W: [. J/ x1 w# O! ccd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
" B- g1 y% l( N% p. z7 K) Q2.启用OWASP规则:
+ |# J) H6 {' g5 h2 p& _2 b* Z+ t
1 h; A9 e, @% j% P! ~/ M复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。7 G% d6 ?. c c B- j& E4 P
0 J! ?5 |4 ?: z# f; i编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
0 _$ C7 L8 `# z, o7 G3 q0 c( \! c. o) g. _; J
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。/ M$ w! R7 Q! m4 G }5 L& U( _8 T
' Q+ G; E/ B6 p6 j3 K% a% }) t. PInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf$ ~9 _6 g' G6 c6 J
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf* w: Y+ c2 t5 E! G5 y2 E* A$ X& Q! u
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf: ?, h2 I! h9 F# O- K
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf p% V" f+ P( T$ F3 m
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf. C& R1 I# r! y8 p- ?
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
D9 D" F5 p- o9 q' h3 WInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
1 d& o1 B2 T; n4 J. d五.配置nginx
5 @' P3 o. p# z8 z% N$ ^) O4 @& }2 B" y" m& H7 c4 b S1 ^; k
在需要启用modsecurity的主机的location下面加入下面两行即可:
% U! T. [/ t4 ?, M
3 ~. I1 V% j' A$ g. q& L9 G. F) QModSecurityEnabled on;
0 u. C7 n6 P& H% w* E1 t% W% EModSecurityConfig modsecurity.conf;
9 |0 g( ~$ I4 y下面是两个示例配置,php虚拟主机:
3 o1 t2 r/ D8 q# L8 m* L/ M" l2 n
server {
7 y6 |9 t+ }3 A, \# C listen 80;
6 R; R- s4 {$ O+ c# L; e, r8 G server_name 52os.net www.52os.net;' F: {+ W, s0 E; B
4 Z' W) b/ T* ?; v- U
location ~ \.php$ {' H% _$ Z* q) U2 A. ?6 O! w" E
ModSecurityEnabled on; ! _/ b4 a T5 m6 L. J/ x$ R, Z
ModSecurityConfig modsecurity.conf;
: K: R+ v; H m) P" d; s0 V$ ^/ [2 a# s; C! L$ E
root /web/wordpress;
L, S* b% b: Z* E R: e7 \ index index.php index.html index.htm;
, R& Y! r! z' h& j3 E% M) j
1 {3 ?' f2 @) b( V' j+ L fastcgi_pass 127.0.0.1:9000;% y3 v( e% T1 S' k- c* U$ T3 M
fastcgi_index index.php;
, a3 m: y3 m; F D/ q' L* w fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
/ V1 E' j1 |0 r1 c4 T" L2 K include fastcgi_params;
; |0 N0 d3 H% Y+ O( x }8 z! M" v& ~5 j& E
}5 I- s; A/ q2 w+ C( t) w
upstream负载均衡:, N' `! ]6 B+ P
* I3 I; Z( C$ ]+ T% x, m5 [
upstream 52os.net {
; H2 a" j% W3 i: S server 192.168.1.100:8080;
; f* x& s: J! F2 |3 s8 k3 n( [ server 192.168.1.101:8080 backup;
& z Z( o: Z& ^7 s}4 r2 U, C1 {5 F+ u$ Y8 Z( z
5 A! f) x# B/ @6 iserver {
+ o+ ?! c1 A `" P; _listen 80;2 A0 ?8 A$ r4 j
server_name 52os.net www.52os.net;9 v4 K* v4 c( q3 U8 U0 K: Z
( g7 X; L4 ?7 X$ f( U7 |location / {
9 v; [5 h) D% u! C+ c- y. U ModSecurityEnabled on;
: n; _! G8 \ s5 _ ModSecurityConfig modsecurity.conf;
& L9 J0 Z9 o; w% g/ t- F% f* s/ }/ ^# ^4 F) O7 f# c
proxy_pass http://online;
& T4 w+ q y w- d proxy_redirect off;
9 z# q9 b1 ^6 m proxy_set_header Host $host;/ Z3 _) }9 X( R6 O9 |/ g4 R
proxy_set_header X-Real-IP $remote_addr;
; w; u$ Y. x* Y1 T' O" z proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
) R" y/ t1 V* B' j0 O7 n$ h }& B, b. c3 [' f+ _! i0 ?3 w/ J
}
1 n4 [/ V- o& W六.测试7 r5 u' S* @- f
, y: W1 U7 X" G' Y* d
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:4 ?5 G$ F* r @4 ] N
+ O. m: P; v. w' s' X
<?php* {, O- Z P/ @5 h# i# g2 J3 j: ~
phpinfo(); & V9 X! F' g, ?) b( ?
?>2 u0 n* w x0 p& F. r" h7 {
在浏览器中访问:! H: I3 ?$ ~+ R
" y$ x) \7 p1 O, O3 C) A! ~1 V1 P
http://www.52os.net/phpinfo.php?id=1 正常显示。" |. J) ]- W1 \6 O' ^
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
6 I% A/ a8 d7 Y" l4 k( g) Ahttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
& k3 J- }. B1 \9 V说明sql注入和xss已经被过滤了
( I7 y: I( E& ^+ ?1 _! g; d
4 {3 z* y4 j5 z七、安装过程中排错
7 F. h& }* W6 o) B5 c! _7 m1 c* C; C* G) D S: y
1.缺少APXS会报错
" F; M: e8 r: Q* @+ d) L
* \: a1 q+ _4 A; oconfigure: looking for Apache module support via DSO through APXS" R. {1 T$ I0 T3 L- f* b, L
configure: error: couldn't find APXS) ^0 i/ C2 k0 u; J' E
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。. ^: R6 i- p- W% x5 K+ o. U
解决方法:& v# c# d# q) `' f I- l# E" v
" r, ?8 s U9 ], S- o; ^" Wyum install httpd-devel/ e9 D+ H6 F' `. o' X. D6 t
2.没有pcre
. ?4 B3 K# ?4 Z. `- d- e
4 c2 I; X9 K$ {2 xconfigure: *** pcre library not found.
# k$ u1 Z9 f$ u9 k: Y' U" n$ f( o; F! _configure: error: pcre library is required
% e6 w2 K& E9 @( T | M/ A$ B解决方法:
' n3 f. N' u+ a5 t+ e2 A1 G& {
0 \' @4 a+ V3 I$ Gyum install pcre pcre-devel/ M7 z4 n( ~6 e
3.没有libxml23 @3 J/ ~( u; p0 \
" }$ p" S! T6 {3 k3 F9 W
) [7 n) F8 G. | c7 a
configure: *** xml library not found.
$ x1 m t6 z5 I( X2 M$ u4 x6 F" \configure: error: libxml2 is required. r& w2 F& g9 y w( V6 r
解决方法:
3 y8 ]6 [# E, F; t$ D2 u# _) Y. w; Y4 _: ]7 l! |0 V/ C* b! z
yum install libxml2 libxml2-devel
7 U: z: n& n! j. O4 g4.执行 /opt/tengine/sbin/nginx -m 时有警告; `- [5 q/ S) u3 g
# U1 y( F2 T; F) \! L7 i+ lTengine version: Tengine/2.1.0 (nginx/1.6.2)/ j% U4 f4 ?8 w2 U- B
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
9 ]8 q! }* B1 _; [, Q8 O% c原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log, T" h7 v3 A6 U2 s- K u
$ I1 Y( O+ j+ W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.' r+ N: E' \( ?3 X! W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"# W) T7 ?) P$ T" b5 y, {
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
/ k: _( J! R2 X1 h; [$ L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
+ s, d+ S- \2 K/ Z5 J4 D2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
+ ~- ?: L7 T0 ?) ^, J4 |2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
[ M6 j' r( |2 B1 M解决方法,移除低版本的APR (1.3.9), E$ y* y3 _6 L& d1 i( Y
3 c# t5 g: T; D
yum remove apr
% \5 P, n0 z* V# S5.Error.log中有: Audit log: Failed to lock global mutex
. u& T M. \4 z4 s+ z- i' J R) w6 D: j9 x/ ]- M _) }" t( Z
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 1 f9 p0 }. B# \, D. u6 F% ?
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
$ w; j; G4 ^7 }* h A! I; U解决方法:; i4 v6 ?8 f( v- Q' k+ t+ n
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:4 f) M1 ^9 N; Z) S
0 b( u. @+ R9 {
SecAuditLogDirMode 0777
3 f0 ], J+ Y% g1 o+ d5 oSecAuditLogFileMode 0550
; K' [9 U1 R" x6 `9 ^SecAuditLogStorageDir /var/log/modsecurity
5 y) Z' i5 _1 lSecAuditLogType Concurrent, E8 V0 T3 q! i2 @' H
参考文章:
7 n' H5 E+ d/ {5 |2 [& p; ]https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
( u" U: X( b% g b+ zhttp://drops.wooyun.org/tips/2614 |
|