|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。3 }- O$ o2 u0 W
9 T! @% ?7 I- e' Z4 X
一.准备工作
# V; y" [* w1 e; v: H h' c
( w+ k- w& [0 a6 X9 f z系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
T, Q1 @& ?& _- k% i: Y3 W1 `7 U9 a' }3 H8 G) `/ t' N
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
- \1 V! ~$ m ?; C7 A$ N
3 ?2 ^* O; w, {/ \! ^modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
8 Q* f2 b# B3 L) @" t' b. Y+ o, C
: B6 [2 y; M" O2 w* F6 b8 Z7 F# `OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
: @: G7 h' U0 s( p$ \- S. y& O. L2 d# z8 M8 F
依赖关系:. I; x9 _6 G" |: g8 w/ D' D
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:2 S3 Y/ _3 U+ M
/ q8 z9 R% n) C; T) d0 t* ~yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel: `0 ]2 o& N7 `$ E. |
modsecurty依赖的包:pcre httpd-devel libxml2 apr; a. _/ k" u) c! }. e" x
' M% H, a1 i# R( j! I* f0 u
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
5 |) G! z3 h( t5 e$ H; c二.启用standalone模块并编译, r9 m- C- F; x9 ?; s
! U0 {6 |( I" F9 M下载modsecurity for nginx 解压,进入解压后目录执行:
; v" D9 P. u9 c& N( v& S( ~" L" R4 T4 k7 t
./autogen.sh
% u2 X/ m) ]4 v$ F" l./configure --enable-standalone-module --disable-mlogc6 s* m! B( b9 q
make
{# c4 H5 e n" o三.nginx添加modsecurity模块
3 g( j1 }# p/ k0 K
4 Y a$ X7 }( \' a+ o2 s1 l在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
, d3 M' I; a4 W8 w' q/ D* n, M7 s* Z/ O: J! I
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine: `& f0 J* @; z; b0 U4 j" u) p5 \; M9 `
make && make install
7 N/ W2 g$ @* {* S8 j, c四.添加规则9 j2 l% B/ _7 U( i, r. p5 ~
! D. p1 l! S- Z( i+ omodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。& S0 s8 w$ B/ i6 D0 ~
; l3 `) F" k2 a% C1.下载OWASP规则:
1 L7 X( a. k1 V- ^5 ^
+ j( ~, A) u% f4 ~/ X. Vgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
( H5 O( N) Q& j- _6 A, F4 S1 j6 E | O" ^0 A% J0 t5 _
mv owasp-modsecurity-crs /opt/tengine/conf/
6 M$ b; U5 G* {9 d: i" L
8 U% z' j1 Q6 o$ d# k Rcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
4 f# S8 d6 H2 Z. k5 R7 r8 r# W- G7 Q2.启用OWASP规则:
3 z! Z. M& }' g8 l9 m5 D
4 @( Y+ |" D( P复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
4 c K7 p9 j0 }3 L) a1 Y
~' i5 P! v& I6 Q编辑modsecurity.conf 文件,将SecRuleEngine设置为 on m3 r: v& ~! [6 C" I
2 s6 v8 c9 }; A, O5 T% lowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。# R3 W5 E% J" B
L2 k! Y. v8 e& z2 }Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf# X5 c) K3 u) \+ ~- Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
& {( f( I# H6 t9 I0 k# S/ pInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf+ }3 B$ ~5 w4 N9 X" `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
& y' ]5 l% r1 y) fInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
0 v' g1 t3 T) h& ^Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf% K, U$ H) h1 l& }. a; i/ S
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
, D6 J8 k0 M$ P, Q) U五.配置nginx7 Q. n& d- K- X7 z. K& i
! c) q; U) u( Q& S- J/ ?在需要启用modsecurity的主机的location下面加入下面两行即可:
( u+ Q' I! A! ~: S8 i% a* G0 ^# Q& g& E' |# y
ModSecurityEnabled on;
# J: Y. l7 K/ A0 Y2 c. sModSecurityConfig modsecurity.conf;, h3 }( T+ b$ B9 {+ v" [( M @
下面是两个示例配置,php虚拟主机:" n7 w8 Q3 L8 A) `
8 ]; |7 }# B- M( W8 u! a8 p; |server {
. ]1 x+ X5 y$ i. h6 l listen 80;
0 R+ ~) l" ~7 Z( p/ a server_name 52os.net www.52os.net;/ c9 U% q; E4 R( g, c h& H2 I
* `" x, v. }- ~/ Y- `7 }
location ~ \.php$ {
+ U+ W4 g( }3 U( C ModSecurityEnabled on;
$ O1 {6 \$ l$ V ModSecurityConfig modsecurity.conf;
. Q6 O6 u" z; P' s3 ~. u- m3 T
. s" ]3 d, z/ a" ?3 e1 j7 k root /web/wordpress;
0 e( G6 P* k4 Z index index.php index.html index.htm;5 [ J4 h; s7 C1 [& K' B7 w
9 R5 p7 n( G- H4 x/ }2 S6 Y fastcgi_pass 127.0.0.1:9000;0 B' `+ u) ]2 g+ ~" V
fastcgi_index index.php;
' u4 @) l& j' X$ b fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
. f5 k/ c: q& Q include fastcgi_params;
$ E+ c$ e4 J# i5 k7 l: I a }. {% |4 C2 `) Z: v2 a& W! e
}+ v+ q! L* k$ c0 _" v' h% N
upstream负载均衡:
# ^4 t, D, H2 q+ Z# V. T ]! m& w; U" C6 S Q; B
upstream 52os.net {
" w- _/ g6 x1 L# e: m2 e4 e2 d server 192.168.1.100:8080;
6 D$ y: u7 F% m# z. ^! x" [8 | L) ? server 192.168.1.101:8080 backup;
9 G6 ~+ M' U% n! W}% ]' L, }. U8 y# Z
- a) x7 g: w3 p) w# I I
server {- e2 x$ z" y% M2 w( d+ _
listen 80;
' u% N1 o' f; M, A/ d# ]2 Sserver_name 52os.net www.52os.net;! D$ w3 L3 m6 v
; `2 L# v! t% N, |+ U, r
location / {2 O0 k- s6 _) b! Z
ModSecurityEnabled on; " V, q$ b5 I0 U+ i' A7 C
ModSecurityConfig modsecurity.conf; + N' G# Y {- E8 k3 i
5 _7 W0 q. ^ U, @ proxy_pass http://online;
* W7 c- W; _8 t proxy_redirect off;, T: E5 Z+ [$ k4 F& q/ X
proxy_set_header Host $host;
8 P3 Y' H$ t' U `& W6 K1 j& H proxy_set_header X-Real-IP $remote_addr;0 p: `* I& g* G5 J
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- l! N, G6 o5 o& N }3 Q: D+ v! ~7 \) j6 `
}0 A9 N8 k. O$ F# q6 b
六.测试2 ]" X, Q" K& n0 P0 c
6 G4 V% q- {2 U" p5 y# R我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
' n1 `% z2 X9 S8 l4 p1 g* \) S8 F/ k* K. V; T) o0 L
<?php }4 j* J$ ~9 Y# |' T! y
phpinfo();
6 i/ q( ~4 N6 Q4 d?>
V+ M8 M, ] u6 P2 i在浏览器中访问:8 r- \ R' r; @( E- g
) ^( Y3 l: Y- Q+ G. ^http://www.52os.net/phpinfo.php?id=1 正常显示。4 Z! i9 ~) v9 ]- D5 [' U
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。 j' I" h/ _7 e2 d: O, D
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
; E. t( c6 O* E' ~! H# i说明sql注入和xss已经被过滤了* H' W+ C8 S7 F2 o' m& ` ]
7 `& ]1 @5 L+ K: @/ c; l/ J5 _8 P' z七、安装过程中排错7 f; D2 a; p, m0 q0 \- n
# e' Y4 Q. Q: u+ ^, g, E' |1.缺少APXS会报错
* e( G9 Q# T' ^) Q; m: Z1 R
! ~5 a" F' P g, x( E6 L2 G% w5 zconfigure: looking for Apache module support via DSO through APXS
) h& n% s: f) ~( I9 Y, ~% mconfigure: error: couldn't find APXS
& t5 ]+ s' m0 s: Q: G8 O' iapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。4 k7 d% N9 H! u
解决方法:; c7 ?: b0 l8 c: C3 c3 |# E
& M4 D& e: D# Y+ z+ M8 M# Iyum install httpd-devel
0 D6 r8 R3 k0 v2.没有pcre
3 c# U7 f% q5 [% l; k7 V) _5 S2 S, @5 S( T' s
configure: *** pcre library not found.
7 x2 y# t) v8 rconfigure: error: pcre library is required5 e y" b: ?* t. }- K% b
解决方法:
+ C2 a a+ S$ R5 T) Y
& f3 ^$ N9 Q' X: b+ v7 U6 myum install pcre pcre-devel
* \: b) j$ F' c9 H( i1 Y3.没有libxml2# b& T$ s! P' X* |' \6 J
0 S: b1 D$ @" \+ b& e
: c2 {, C3 n7 h0 E6 Z, @* C7 ^* ?( bconfigure: *** xml library not found.
7 Y# m9 O6 }' y( gconfigure: error: libxml2 is required, l# ]& e: Z8 q7 u4 o% U
解决方法:# y3 O4 ?+ J5 i: T& f) t8 _
; F+ f4 \; b2 \" \: F! p7 _" ?
yum install libxml2 libxml2-devel
, D* ~: n0 d! o5 y: q4.执行 /opt/tengine/sbin/nginx -m 时有警告! e, N5 k* {' ?3 p
% }$ T9 J0 |% m
Tengine version: Tengine/2.1.0 (nginx/1.6.2)9 P/ G1 n- l: @3 z( q
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
5 b: G& f1 m/ p7 b原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
* O3 R2 A7 E# J3 S4 R0 a
2 R6 W* O6 J. D3 t5 X! E8 v' s2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.: n; z- f: }+ _2 P
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9" v( S% L- ]9 @( ]
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!+ l/ y" c4 _0 |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"' I7 }- h9 b8 e% Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"7 S) {9 K$ K+ ^6 a5 v- f. {4 R
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.9 Z; i2 N7 v) D9 b2 ]2 W$ b% }
解决方法,移除低版本的APR (1.3.9)4 q1 P. U1 K! D4 l* k
. z5 A8 \9 m- \# s# `
yum remove apr7 W7 [0 Q% L1 I- M s" _6 L
5.Error.log中有: Audit log: Failed to lock global mutex
1 L# T: G) ^) t& ? S! V
- h) k( p K* C9 }3 Y4 J: u$ c2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
2 Q% d% Y/ l! u0 K* r7 W5 K! [global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
0 B# R, k! ]. A解决方法:; ^* x9 B: a" a& H0 t* y( M5 N& h
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:( E6 D% v6 ~2 H ]- b* I
4 F& ], _- N! w6 {SecAuditLogDirMode 0777
. ]; f; W4 f; P8 h) d4 P3 \% @SecAuditLogFileMode 0550
1 n( t5 i: ~. h {8 W' OSecAuditLogStorageDir /var/log/modsecurity
0 I( c8 s2 @& h% I5 b1 L! SSecAuditLogType Concurrent# y1 m1 g, {4 K
参考文章:
9 \" J# A) c$ _* ihttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX4 D8 J1 V) M% K# g4 q! T2 j
http://drops.wooyun.org/tips/2614 |
|