|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
4 l6 \7 D# c( X: r# `* o! R1 h
5 K+ `! U1 a$ X2 ~一.准备工作
1 ^1 h# e# ] J) e
, v/ G4 @' F& `. K( [系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
2 s) R: `' U9 f7 n
% V/ L# p$ ~" \tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
8 L' M& D0 p! D9 ?# a9 z& L- f3 _% B+ w+ {% M4 V2 {
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz2 \) a; \" e& j' B5 S9 D: E
/ T" n2 l- e' Y. cOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
. B6 L: o: c) T; h' k5 ]! K
1 f7 T2 m+ ]" t e# J依赖关系:! P% N, _2 B/ ?; K, {
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
" r0 ~. O( ~5 e- X
( i N3 G V# D0 ]( x% n( iyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel( A, ?: X6 K2 G# X* ^- u& A7 J/ f
modsecurty依赖的包:pcre httpd-devel libxml2 apr: M! k- t$ ^8 R" e& L/ h
8 X6 h- z* W9 F5 ~& Vyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel/ C2 a- F5 n# w, `: A0 F
二.启用standalone模块并编译
4 {1 n, d/ B% `: Y
) ]4 T; u. D+ H# j% `" \下载modsecurity for nginx 解压,进入解压后目录执行:
6 X8 F! Y# H7 l7 o6 h F; ?' k# z
; i* H. s5 c2 C# V) g$ K./autogen.sh, c1 `% s' @5 c9 B1 p
./configure --enable-standalone-module --disable-mlogc
. P7 B- {/ G8 s5 l/ p. dmake . m4 s' Q% _: O9 G
三.nginx添加modsecurity模块$ B( r4 b% p" L; g' C
' y, k1 w/ ^: j" ~$ t在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:& \# ^, d8 `8 P: E" Z8 |8 q
3 @. `5 n. A7 N$ N3 s/ r5 |: ^
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine- N( C. E$ G( G
make && make install
# k4 D- R1 h# V; ?四.添加规则4 }% _ X0 A1 ^% N" {' O" M
5 z3 C- [: Q0 I4 G* W+ W
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。% \" F, T2 k5 M( ^% s4 Y5 K
. ~8 w+ Q1 k1 b- O1.下载OWASP规则:
3 T! n, \# H9 O) b; {* Y
5 O( }% p: `; X9 A9 U; bgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
( @) u7 d& X+ l' J* D1 k; W" A6 c( l7 H0 x" `* {+ w
mv owasp-modsecurity-crs /opt/tengine/conf/" V. I: Q8 `/ X
7 P/ n _ m3 `9 q) g* ~: ~# Pcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
( [9 H( A) z1 V, S/ ~2.启用OWASP规则:8 H& J1 o5 F& S6 }# A
; `* o. p( u& I+ o0 N+ }
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。3 o0 O+ h+ E# p
5 v' k, Q6 a1 t% ]/ C9 X6 R编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
% M7 c) e h1 ]( m- Q" ?. \
9 b8 K8 l* J" jowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
. X% t1 n! ]! e$ \ i1 @. g+ D$ i) |/ A: b' a; t0 v
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
5 s" V' g3 l. t0 y3 X) i5 v' ?7 \Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf( K9 F+ v2 b! W; A5 ?+ ~7 x8 r! x
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf" ?* M: F1 p) G5 l
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf+ O; c+ r4 W" S/ {
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf3 b3 }: p# F: p- y4 z. |3 q0 |, H* _: l
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
+ V) o! O- G* B" c; a2 C5 TInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
8 } W2 X7 `" ^3 [0 E! M' M五.配置nginx: U; u5 w& q, O2 ?$ ?" D
: z: q# c8 X1 t" P$ n; _$ v& h
在需要启用modsecurity的主机的location下面加入下面两行即可:
; r/ N2 t# O- z: ?* D( U( W: v7 A5 E4 O) A+ }: k [
ModSecurityEnabled on;
5 E7 h/ u6 X8 K ZModSecurityConfig modsecurity.conf;. |7 @8 |2 P3 k! a% }2 }! l. X/ K
下面是两个示例配置,php虚拟主机:. B" `* m# r0 v% l' i
6 x" T" Y O% @. Oserver {) d: F- g: W" t
listen 80;& B8 h M3 d9 U/ i0 b; A! V9 ~
server_name 52os.net www.52os.net;
8 r5 i) t! |+ p0 d2 ~& L- N 2 q. M! @. _: p- `
location ~ \.php$ {
4 q& N$ Q/ |$ K/ T N ModSecurityEnabled on; 1 W& }% V9 ~- S P
ModSecurityConfig modsecurity.conf;
0 |+ C& i% }) G+ C
! X& G; S9 l7 `. o, ? root /web/wordpress;5 X! |0 T# _9 l
index index.php index.html index.htm;, l: \: g0 L, E9 b
S1 Z% }2 W$ @" N% k- |
fastcgi_pass 127.0.0.1:9000;
; t( a& u9 K" d6 i% D2 b d) \8 J% n fastcgi_index index.php;
/ n1 N8 M( d- u0 _ fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;+ O$ I0 q9 H" V4 p
include fastcgi_params;
! l" L0 R/ q% n8 q: h4 j n }0 y$ l4 y( v* [( ~
}: N6 V) ~4 G. _5 A5 q- r
upstream负载均衡:
: _% H8 V: _9 y: W: d1 Q* @/ e7 S/ P' l) _0 e/ g
upstream 52os.net {
* A2 W, u6 Y+ g/ N server 192.168.1.100:8080;
& }6 l, R! @2 B6 Y: r+ m3 M server 192.168.1.101:8080 backup;
7 E- a) @ E' F7 M) u" Z8 y) J} v3 x) y' A$ R
! W+ _9 h3 N4 `) V2 J
server {. L7 O6 w( K8 B# W9 I
listen 80;
& x4 c+ ^. F7 C5 T5 Z& B* S- W* y7 H4 Hserver_name 52os.net www.52os.net;: x/ ]! P7 l8 E! ~5 C- r& o6 ~
$ q/ d+ \8 z, z1 r! K+ M9 H
location / { [- A; L' _* l3 V
ModSecurityEnabled on;
$ s$ C4 C. k f- v7 E b ModSecurityConfig modsecurity.conf;
" N( F" Z, G0 N* r( {
, O% T7 Y y0 j$ K9 ^& I0 b# e! s proxy_pass http://online; U, q$ w! R1 F
proxy_redirect off;
# q2 V9 n1 p) u8 L" F/ l# Y0 w! [) c proxy_set_header Host $host;
/ \; C! V7 O) f/ G& x& j proxy_set_header X-Real-IP $remote_addr;6 u7 l6 e" s6 w
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;, H2 f( Y7 Q$ q4 Q6 D
}3 [( J, r" y# N r; Y
}) R# r3 z4 j* R" ~' T- L) [9 Y
六.测试' x \2 b9 m1 B8 D9 s$ ?
2 R" H# Y& ~2 m8 S+ O我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:; T$ I5 i1 m$ C
$ v7 `6 { |6 N: d7 U( x) \
<?php& y9 [4 r4 g/ z4 i. ?4 |
phpinfo(); ~1 i8 @# x9 e. {& p! p% X
?>
2 N7 k G8 \8 t8 B3 m" m在浏览器中访问:
5 g! u( T# M! s7 J! [% S' W" a+ w4 @5 o8 }. m/ S' y/ U7 d2 q
http://www.52os.net/phpinfo.php?id=1 正常显示。/ M; Z, L1 G, Y+ S: `
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
3 s4 V5 K9 B: K8 V: H5 ?http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。! [; O# G p2 q
说明sql注入和xss已经被过滤了
; _( I6 C: ]1 o1 d8 m/ |. [6 m3 R0 G$ q1 ~6 ]
七、安装过程中排错
' [( c+ Z; k! U" D+ j T+ j! K% b: o: |- n3 w7 o$ S
1.缺少APXS会报错
& J2 O5 p3 Q0 x! |) P/ q
7 O1 g# K. _; J7 jconfigure: looking for Apache module support via DSO through APXS' W% Y/ F9 L# N% l4 }" r& u& v
configure: error: couldn't find APXS
: D& A& Y9 z9 ?% B* w) p. G4 yapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
! e+ m! ]0 g8 c! _4 R+ R0 w' u解决方法:8 u$ Q% W( ]' C# T( A
; U3 N+ {9 D. I: J% x3 N( J* f
yum install httpd-devel! @2 d. C& R; J
2.没有pcre
" m) C4 O: B( d4 x8 l* @
- U' h+ E0 |$ u+ N& {7 W& e3 kconfigure: *** pcre library not found.7 _7 _% y7 E" l$ T4 U
configure: error: pcre library is required
U9 ~: L/ p2 [7 i8 ]( w) _' ], p解决方法:
/ E5 w& C! Q+ `' F+ K6 d5 }+ ]5 t+ ]3 H. W
yum install pcre pcre-devel2 n& `' N2 u/ p. p
3.没有libxml2: d8 ]) D6 A( u t0 O2 y1 [
) l% B/ L, N; A3 r: Y$ ^5 m7 C( |$ J4 I: _
configure: *** xml library not found.
s" v5 g% L! m0 w8 Uconfigure: error: libxml2 is required
; m9 H7 ^- f# }+ [, a% f4 g解决方法:
1 t& a( n2 D* M* B) }9 R( n2 s! s. J8 ]+ l' X! `8 M [2 I2 J$ ^% i1 B4 l( c* z. F
yum install libxml2 libxml2-devel" Q1 B6 M0 m: V! D3 |! T0 a$ L
4.执行 /opt/tengine/sbin/nginx -m 时有警告
* h: b4 B; y* L- y; L) g
8 h! X; j" A* ~7 z* x% ATengine version: Tengine/2.1.0 (nginx/1.6.2)
/ t+ V K( e- B0 Y9 hnginx: [warn] ModSecurity: Loaded APR do not match with compiled!8 ?/ R( N/ }9 p6 |
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log' y* X* d# T2 N3 w# }# k5 D
[4 L% C4 Q: Z! A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
* I& t4 `% u0 K( m7 Y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
: v* `. h! p- m( A* X9 x' ]0 I2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
5 y& V4 O6 ?# O5 [. Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05": E1 }8 n6 ]) h' N. I7 G* v! O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"+ u8 ~3 N1 V6 u" n, f' a' ]
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
6 n2 e: T* j( K }- k _0 s解决方法,移除低版本的APR (1.3.9)
: g: \9 b/ |0 R i* ^3 Z$ q# _
" K" `$ k9 y/ l/ tyum remove apr
+ U# L8 `6 E: A0 a. A$ C5.Error.log中有: Audit log: Failed to lock global mutex
7 f* L; a- L4 `( K
! @9 t1 m" M) ^ \1 `8 u2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
9 M$ |$ ?5 B, B8 q) Iglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]5 N$ D; \# l$ W( W0 G, d9 j @
解决方法:
; p+ K. w+ Q* H) T7 d; W编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
$ K4 m. W/ G$ R+ f6 g+ K/ e% f8 s" _( ^9 t
SecAuditLogDirMode 0777 y }) ]' {. H6 {/ ~: o
SecAuditLogFileMode 0550
# K) L, t# j% J& r- iSecAuditLogStorageDir /var/log/modsecurity
, X0 \+ T' c. HSecAuditLogType Concurrent6 y9 |; u1 m; |( c1 T
参考文章:
/ \7 R6 A! A* r( D0 ahttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
( {6 i* j1 M% Phttp://drops.wooyun.org/tips/2614 |
|