|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
; @% ]* F) l" Z) }0 O9 }' {% ~$ c$ }& D$ o8 m
一.准备工作4 X% P: `6 N. g# C! y" \
' C' N- g, \" v# w @+ K! T
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0( h3 i. d8 |% J& m
* L- u! Q! }; ^6 ]+ Atengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz+ d3 v; X3 l4 V6 C# ^) ?
) G9 O; P8 P1 `* i4 g
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
: S" {1 ?( V4 N! i8 e7 [; x# c! Y" x U* ^
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
9 H' J$ T' t4 o0 e) T# p7 B' E- A, R3 k7 O* a
依赖关系:' ~1 a+ P7 ~9 V5 |* G
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
/ { _! z9 n* u; i' h; R# V
: o- e0 [* \% {yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel5 |% X6 Q8 U7 b" k( q% R
modsecurty依赖的包:pcre httpd-devel libxml2 apr
' ~# H/ e: E1 u& M3 _; i' p. K& D$ b0 p- K) Y5 t- z
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel0 I9 j7 H6 z. z& T+ C
二.启用standalone模块并编译
/ b" v* v# O" {
! _5 H7 e4 ~( ~下载modsecurity for nginx 解压,进入解压后目录执行:. b' O( G0 B9 w; x/ ]/ a
; s7 [# Y3 `( U+ y9 ?./autogen.sh
8 ~0 p+ [1 w. T2 M, E- O; s./configure --enable-standalone-module --disable-mlogc
/ M5 `& M: N7 C( e4 r$ a7 omake 7 L4 y9 G6 J5 N
三.nginx添加modsecurity模块
. ?/ o; J* p4 ^" U2 F5 s/ j/ k
) E% d& D K/ p4 {/ P/ k+ H* Z, ?在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
2 t5 V! l0 R* a* p; r j4 v9 F# ?9 E% k$ @
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
9 K& `$ \& \: zmake && make install S. r. L) L3 J) ?, [
四.添加规则0 @3 E8 C5 l" t$ [( J
7 ^4 O0 q2 h& [( `) Y3 G
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
3 p8 T: h* P; b$ \ w3 T" w/ t5 ?3 V) H
1.下载OWASP规则:* |) j6 z% h& _" b# ?* m N
. Y% A+ N3 n: X- ?+ e: @3 Y$ bgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
0 j R: k5 `. L6 [
U7 ~8 w$ p# g% @ E" pmv owasp-modsecurity-crs /opt/tengine/conf/
% l8 Q6 u7 p; l0 A& |* c( H2 i( P$ C8 Y9 P% k* k
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
; l6 _7 p: s1 Q+ Y& T. p4 N7 L2 j2.启用OWASP规则:$ O/ @0 m' ?9 I" l, D. X
0 w5 v8 q" |' _复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
" P2 f) D% R! F- J/ {- Q% {# i: t! n
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on7 { r+ u: d# p" B# w: k2 C
- N3 y2 K! g% K5 Q/ }8 O
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。& b# l- {3 G8 n0 s0 X* t' R
6 G6 N2 y+ k* u* y" v; b
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf8 M" z- C$ A3 f" n, z/ {
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf. ~# v; H! L/ i E! T* k5 B
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf- |5 M6 o+ N% f
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf. P$ @/ G1 w0 ]" ~9 d
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf- H7 x$ E: ~! N8 c& S
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
9 ^8 ]* D' l0 K; A$ D( a* FInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
! D. a4 ?0 j0 f/ C9 P/ J! h五.配置nginx
5 T6 | E9 r' T5 s2 n% ~7 b" K4 \+ f
在需要启用modsecurity的主机的location下面加入下面两行即可:. z! U" p6 t4 c8 E) y
9 u2 g5 N8 I. v: D! x1 rModSecurityEnabled on;
?* A5 k) o! t0 @, V- L OModSecurityConfig modsecurity.conf;' M5 C8 E7 R3 _/ X, _+ D
下面是两个示例配置,php虚拟主机:3 s2 J( y* q0 k; M
2 t: N% Y# S2 `+ y4 x( R% Iserver {
; b$ S6 @2 L* F m# M listen 80;) j# ^; X4 F% _& C" S+ N5 {
server_name 52os.net www.52os.net;" B E, d9 s/ J1 f F/ d# ~
) Q* Q9 U" y" |! v$ Y" t
location ~ \.php$ { o$ R) B- `: b3 O+ m! d4 g
ModSecurityEnabled on;
" y& `3 t/ o' y ModSecurityConfig modsecurity.conf;2 i3 p8 h0 X' d( w8 \' M
4 h `' i; e, E3 b$ g root /web/wordpress;
* \, \+ E9 Z' q! p& k j/ P# C index index.php index.html index.htm;
5 O9 F& x/ N+ s7 N. s
0 O9 F/ ~7 N) ^$ H fastcgi_pass 127.0.0.1:9000;
' [6 n; P( i6 z- P9 Y fastcgi_index index.php;) r4 B/ y2 k1 ]
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;7 P( B. l. k! V: Z) D
include fastcgi_params;. d& z$ I3 W( z ?/ z4 K
}! U8 q. c) ~7 G, h8 ~
}
x$ j% G9 z( ?) `3 mupstream负载均衡:
% P9 q7 l' y+ Z% M; N& p4 v( {( G& t' _9 O/ z# J0 {. |
upstream 52os.net {- l; R& t6 @$ d1 R/ F
server 192.168.1.100:8080;
# A& d8 W+ f }; ?! V" E; a server 192.168.1.101:8080 backup;5 u' r' h- h( N, l- a& e
}
( M- Y+ ^8 X9 K: X9 d# i, m4 F9 _
- H( f* G9 x B4 E8 w$ n# G2 y% [: J" ?server {( K/ Q0 u; a9 D
listen 80;$ C& U; A3 Y+ P, z- H
server_name 52os.net www.52os.net;
& b. t0 W; A; {, S M7 C0 h( o3 o
location / {
& j7 I* Y1 g4 L$ i6 C! y8 l o ModSecurityEnabled on; + I9 Z- ~- p2 h4 t$ K/ m
ModSecurityConfig modsecurity.conf;
0 B) R6 H4 j% @: S1 e9 W p; o {+ G& g N
proxy_pass http://online;
2 n2 f! r6 [3 I; v0 }+ N7 x% e1 R proxy_redirect off;
2 W0 Z, J" g& y: O proxy_set_header Host $host;
9 v- i9 y/ H# K+ M- S9 ] proxy_set_header X-Real-IP $remote_addr;
% l! i! a3 a# S& j m8 p proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;* ^( `# }% C. |% q g8 P( O
}
5 D; u. L8 k6 g+ P9 v Q}
9 G" i4 `8 @: @% {3 j2 l4 N六.测试" A$ `0 m0 V) N5 a4 ]5 Q# U
1 v( n3 {& u0 b5 i! C# @; o2 F' G5 ~
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
! l) [+ H, {3 R4 u/ f8 F3 g7 k# E( P2 y, J! V _, H. X
<?php2 y' \+ \. m. s3 P
phpinfo();
8 ?. s ~' Z3 \8 d, }$ Q?>
8 W) C! w8 `& f( V9 Y在浏览器中访问:
% n( A+ d& E, B7 S# d4 `" v% I: g; d7 c3 \" H' L: P
http://www.52os.net/phpinfo.php?id=1 正常显示。
! S$ d9 i G5 ?6 U( fhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。* d! C5 O; ^) p3 X- F
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
) i' b) v* B6 D) ]7 G- F说明sql注入和xss已经被过滤了8 @2 h* r9 P) D# J
6 v0 i' I9 ?( G. a7 ^" G2 h% k
七、安装过程中排错7 e. \0 u0 x* D. C. Z
B8 X' C: T- ~1.缺少APXS会报错5 m& `1 c" C. z+ s( i% _+ Q
0 r- a. ^" T, C8 s8 g' W: rconfigure: looking for Apache module support via DSO through APXS9 v* O6 b" c2 p) k- ]3 E }' F: y
configure: error: couldn't find APXS
w( N+ ^$ u0 }$ Q$ wapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
+ T. [& {$ O2 [解决方法:8 q& J5 e- v# f2 ~+ h: |: Y9 l
$ G. O4 j1 V/ \5 u9 S# l! O
yum install httpd-devel, k2 A+ T; j: i/ H7 A' A( g
2.没有pcre3 e9 {5 z8 J6 V+ L8 w8 t
. M0 R5 v1 z* H' Kconfigure: *** pcre library not found.
$ P+ g% y' n. N4 A) ~+ q$ gconfigure: error: pcre library is required& b% |. i! n9 o0 R% N
解决方法:
5 e, `# O- j$ Z$ P! T4 ?6 E$ t6 q- l }( ~% T5 T |5 ^& b+ C; e
yum install pcre pcre-devel- W/ J3 N" I* q
3.没有libxml2/ Z# y: B! f F% u1 M; p& [ `1 F
3 R2 ~6 S9 R. j' k+ _; a
( K. O0 Q. w+ V" sconfigure: *** xml library not found.
( s$ T- E3 @0 `, _' zconfigure: error: libxml2 is required4 h. I& b, L7 R7 Z% R" H. b, t' Q& I; I
解决方法:0 g3 c4 J* q6 K$ M- |; d- ^. r
. {& s9 a: z/ }yum install libxml2 libxml2-devel
, C8 A: t7 i/ {, L: p0 H4.执行 /opt/tengine/sbin/nginx -m 时有警告
4 t5 o5 S2 A' v5 }2 T; t8 H6 j
" k+ _6 ]% J% RTengine version: Tengine/2.1.0 (nginx/1.6.2)
$ @ h9 |* l) z! w3 K! y0 d) Dnginx: [warn] ModSecurity: Loaded APR do not match with compiled!2 e7 [! n5 @5 s5 @
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
3 S4 U, G# c; X U! f( l* P2 A4 D, o$ n1 y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
* G' t' L8 P( S& O' R2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
$ C8 D/ d: h% v# f2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
. I1 D5 N9 \4 \$ B2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05") k* ]2 V* R( y: h: i/ T# {
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
0 i2 Z8 U; u/ B% w$ I( X2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
5 g* I8 x" a! J# x+ U; i) }; f解决方法,移除低版本的APR (1.3.9)9 g1 U% B5 j9 C6 f* @- b2 T: d
+ L' ^: d# z6 c& G( n
yum remove apr
/ ?) A! }4 C& P8 C1 Z6 m1 v5.Error.log中有: Audit log: Failed to lock global mutex
+ D8 `0 v$ ?7 D' d7 O
+ b! j! W3 C) @2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
& c& C0 q; P, F3 kglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]' Q) H8 }! I" ]1 P: o2 R
解决方法:
3 c: j* s" h1 ]- y' m3 Y: Y( M9 W8 W编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:/ \/ L; \2 Z% |: I. c: R% i8 B- A
4 [: t1 B* @; X, M; P/ z+ Y7 bSecAuditLogDirMode 07775 ^+ Z- V/ c7 R/ w: p0 d% `
SecAuditLogFileMode 0550! _9 l$ R& v2 _. c/ Z; @6 G
SecAuditLogStorageDir /var/log/modsecurity0 d# {$ p8 @4 d/ _/ c7 s/ J
SecAuditLogType Concurrent
% M7 Z$ R8 L! j5 L* y参考文章:
5 w# U, U' k6 \3 u( o/ L& V% I, }https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX4 w: U+ H5 d! f6 Y. c; w
http://drops.wooyun.org/tips/2614 |
|