|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
4 J9 V( _% `" x4 L0 ^- p
$ f, Y* R; Y! D. n5 N% Q一.准备工作 J8 u$ o6 m1 }/ H7 h3 N' y( P
: I3 O% I1 e* ~( T% w/ x! y系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0 n5 |) g) s* K$ h& X
4 H( |* N- C0 N$ G) f
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz" }! b5 n' y) j0 x1 @4 K; l! w
9 R5 P( b/ O v: Q
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz6 t/ j6 ^8 n' L" e0 F
7 z" u8 \3 a' g7 H
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
' R0 N# o4 x9 z( ]$ F. J/ c* e3 o. K8 M7 |* D
依赖关系:: X" |% i' o9 k3 n6 @5 M' |( N
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
) l+ ?; N9 p% Y0 \% K& w
& L* E- u q4 i2 Jyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel; `7 R6 o! x. q W
modsecurty依赖的包:pcre httpd-devel libxml2 apr! S+ F. X. {) H3 \
# ?4 L% Z9 I) _4 @: u
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
7 F5 m; c& n. Q二.启用standalone模块并编译
: m e4 H$ n/ B+ b$ U* g! H2 y- k) h
下载modsecurity for nginx 解压,进入解压后目录执行:
$ E$ Y- `2 F( N
* a4 r* O! l' v, m1 E( I0 W./autogen.sh
+ L0 G( T5 V- d. y& c7 f/ m./configure --enable-standalone-module --disable-mlogc
. Z( Y5 M# D" E0 w9 c4 I* k" q) O' lmake " s, ?& u) ?3 P8 h8 ?& _# Z- N
三.nginx添加modsecurity模块
0 Z, r. d8 v; t$ @9 f5 G# T; a% K: E
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
5 C# v6 P' f0 X$ d/ O9 X9 Y2 x: A/ K1 E
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine1 `: C8 T; x3 ]; G4 ^: K) z; ^& ]7 w
make && make install
5 g, c5 ~4 U( u" K/ A+ f& C四.添加规则
' |( d0 [' l3 Y1 _& ?7 l, O! \" R T: R* z3 X
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。' K- u) k: p# Z- l% s1 v& F
; A1 a) h% [0 f1 K1.下载OWASP规则:
/ F+ t0 Q. ~% o, r3 p0 c2 m3 h9 h) j2 l1 _3 a$ d
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs" F1 _* E% B" M5 I/ p
% s/ Y& n$ m& G8 R# fmv owasp-modsecurity-crs /opt/tengine/conf/
& Y# |# L7 P: A1 g- E, i! b1 {
2 y% q' s# p( G; g, @cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf1 }; o3 z9 g* S% W6 f! c1 R
2.启用OWASP规则:5 @& @% @8 F; s
`+ m) X( h- i- {8 G1 K
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: G; K% h9 h: u* P. h& _% L8 M8 H' g% P7 I! K1 Z6 J
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
$ j* }/ q: K3 o; g2 [+ G
* `7 c% k- \, g- _; N: D7 Y# nowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
/ B; x8 B `& _/ k/ f# D+ t% G7 c7 C7 j2 T L: g( [
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
) s7 D; _1 }. R/ y5 {Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf: G/ K8 Z+ b* }6 Z0 M: a
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
" Y" a' J0 D6 V- T+ QInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
/ Z% f9 p0 n' {% V& uInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf9 \/ v" B& j( S
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf7 d4 ?' A) ?4 N; V
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf! S$ ]2 S* \' B( w7 p( ^
五.配置nginx
1 ]1 N+ ]4 V" o9 p# |$ Z2 H2 t8 v9 Y
- q1 c* F4 E$ \$ o在需要启用modsecurity的主机的location下面加入下面两行即可:- p* a' b/ P8 j
( p8 p$ a0 ]4 B! J( F- ^ModSecurityEnabled on; ! n* p3 Q* S- U& p$ Y2 Y* y
ModSecurityConfig modsecurity.conf;
' [7 X8 ?: D8 B2 l* d下面是两个示例配置,php虚拟主机:
; R1 D, b% {/ I7 Y# j$ y3 k4 @
3 A7 D6 e" R2 d0 userver {5 J: g7 m: S8 e% v+ L
listen 80;
1 O" U7 L. e. @. @) d( F+ p- [ server_name 52os.net www.52os.net;
& n* K3 D: a: ^6 \ _+ m3 \% k; r0 ~" j
location ~ \.php$ {
; n( N7 p! Q8 f ModSecurityEnabled on; & M' W2 `* b0 d; ^
ModSecurityConfig modsecurity.conf;
5 K/ u1 e2 x9 y& r$ ~" G, `6 g; [- A' Z7 r4 n& X$ U5 I, s
root /web/wordpress;: Y; k+ d0 t( @- V0 y. n
index index.php index.html index.htm;% G. D3 `+ q7 ~4 C& b
8 d/ l3 A! ~+ }1 h" Z; Y fastcgi_pass 127.0.0.1:9000;6 ?* i* v$ Z- d
fastcgi_index index.php;
2 b. e, }/ g' X1 R fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
3 D+ ?3 m0 l& `, b1 U" N$ ^% E include fastcgi_params;4 z7 n$ @+ J' y0 U
}( h+ p7 t3 P' O
}
6 q1 Z: \1 c% y! O4 r8 Nupstream负载均衡:
+ t+ X! k2 `5 W
% d" r0 Y* T8 C( D0 f- F1 c$ ]upstream 52os.net {+ Y: M, L0 D1 k/ w7 v
server 192.168.1.100:8080;0 G S* S% e6 E: s1 o
server 192.168.1.101:8080 backup;3 K2 n# [: `$ [: U
}! g" a4 ~3 A' k/ N
$ u6 p, N9 k2 k% G; Jserver {% f6 _0 d& r2 O. {' E/ D& G0 g
listen 80;
6 o2 r2 o: e7 Rserver_name 52os.net www.52os.net;
2 O2 T, ]' L* r: E9 U: v5 ~% [5 L
5 B* w% F# @ b+ b1 J9 mlocation / {% g/ G& X, P: U2 j$ ?
ModSecurityEnabled on;
& P2 g/ b' S- [( w6 N6 Z! Y; D ModSecurityConfig modsecurity.conf; 0 y5 ^. [2 }( X3 S _3 R D* L
. H6 m, ]! e/ D2 v$ E* f
proxy_pass http://online;
0 Y5 D% V* N' f3 d+ r& [7 T B: i proxy_redirect off;
# L$ j! v2 P/ t8 [; o% @! { proxy_set_header Host $host;/ L- a1 d) P& D$ u# d2 F7 Q
proxy_set_header X-Real-IP $remote_addr;; {. p7 U8 ` z9 `: E
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
, j. r8 T5 S+ Z }* j/ l! }# g- l! s
}) j5 T$ C+ x0 R9 s7 l8 u" @( A
六.测试
5 k. d' Y) Y0 ?; _7 R# \1 T
5 V: s3 d8 U3 v" g我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:6 X5 O ]4 s* F, I4 \; j
! g2 ?6 b! _4 F6 ], N* l3 W
<?php5 _% C7 @+ T. k1 N. \3 H* n
phpinfo(); 6 O; G( r2 s2 e. [) [$ }7 Z% G
?>
6 [; b7 H+ V5 k! `在浏览器中访问:+ n1 q+ _! X. {/ F/ W" r
; s9 i/ w$ M( L& M" ]& a
http://www.52os.net/phpinfo.php?id=1 正常显示。: j* y! u3 T* q' n
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
. \0 {7 K2 Q! g6 r9 w; Nhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。% L( j) }) V6 J* b
说明sql注入和xss已经被过滤了, P8 X# T' A- f; r% D
7 e9 h' K, V1 ^0 C5 x) t% R七、安装过程中排错
* g; s% i" u" K4 T, u6 @: d' w3 o9 h0 ]( a. ^' ~+ c4 }; x
1.缺少APXS会报错
) O$ N# e9 \( O& t# ` D
4 h4 K5 k3 f. ]; Cconfigure: looking for Apache module support via DSO through APXS F _$ T' q) @4 S- h
configure: error: couldn't find APXS5 g4 s# D2 [3 N- s9 P; m: @
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。7 r9 W) P. Z) C) T8 L) x
解决方法:' \! R% Q& W7 T( {6 j% x2 M
- l0 i7 P) K0 Kyum install httpd-devel' d" P" a n0 F, K( d6 z5 f
2.没有pcre" M. F0 b8 G7 L/ l- B Y/ }
, E$ V6 `8 |. c7 E7 M
configure: *** pcre library not found.1 x3 s. G) z6 f" r/ a {
configure: error: pcre library is required' d. l. G' ?# s! d
解决方法:
! J' q: o0 c3 O$ Z; ?& v( u
" C L5 P# |. yyum install pcre pcre-devel
* H, v9 w& l; q% O3 r1 J3.没有libxml2
( j7 n& W- x+ b( T+ U
/ J) f3 O& p/ v4 [
* ?- s' G& S5 ?6 u$ W: A+ _configure: *** xml library not found. y( j' w, N$ k2 l& R1 D. R
configure: error: libxml2 is required2 T2 o% q) N( w9 Z& g3 X8 P
解决方法: P+ \$ p, @5 P9 }7 g( G8 F9 n
[% q! F6 s) i5 ]. u% U) n
yum install libxml2 libxml2-devel- |9 H0 m0 S( G# Y) q9 v- d6 i; R+ Z
4.执行 /opt/tengine/sbin/nginx -m 时有警告' S0 D {, K* ^/ {
5 ?: n) k/ I$ E7 t- W
Tengine version: Tengine/2.1.0 (nginx/1.6.2)+ o4 F4 t8 O, f3 N
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!+ l2 E, z- k; w3 m$ g5 G2 G6 q
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
# q+ R3 x8 P: W4 |; h" q6 p# I1 \# }+ O- C
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
" _1 E& d; _+ P9 L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"! @" t+ T/ T! X9 P" s% E
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!8 Y$ _- b& `& E- H# L
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"- [2 D0 P+ `) q0 |! I5 l% r- l! k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
0 ], [4 ?: t; d* c! l3 g) q3 U2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.6 D I; b1 N' ]% b' D7 n
解决方法,移除低版本的APR (1.3.9)9 P3 o V& ^% J+ q
1 y# J h0 k; }3 K% n% i" ?2 J
yum remove apr
8 m+ J9 V7 V# q9 v* j$ e5.Error.log中有: Audit log: Failed to lock global mutex
& q" w6 }$ v: I/ o6 \0 R, O3 w1 d7 ~1 I$ B
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 5 Y1 L* l0 W5 K x. E
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]7 y) E b5 j7 k% X/ ?
解决方法:
! P1 y7 p. w: f5 R8 c$ U编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:; g: P- ?! J) F
6 p4 U! j: E; i
SecAuditLogDirMode 0777, p/ V0 o1 T2 I+ s# }" n
SecAuditLogFileMode 0550
. n$ E7 R6 L3 s+ b' M% ~$ |$ ESecAuditLogStorageDir /var/log/modsecurity
( \% s9 G. C: @& RSecAuditLogType Concurrent
- _. B+ ]* C% r7 k. |, A参考文章:
6 `7 y: \/ r3 C: r* Q" chttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX4 \8 b# A9 r @; ~% B- Y
http://drops.wooyun.org/tips/2614 |
|