|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。$ u& \* o+ v) E5 X3 b4 ~! u
- \2 S l; P( J' |/ M一.准备工作 N% v4 x& A( H4 M/ s8 i4 M8 @6 x* M
/ a+ _, v; Z: Q( g系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
7 w# W; w3 l+ |' W
% m; U4 |; ^& Z" Itengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz, F1 J5 e, W n/ t# x# x0 j6 W
" n/ ]& A' m. s9 M- R# v- r) n' Cmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz# n* W i Q& o) c" G" k
- i+ f7 C# m" {& G7 Y$ o+ QOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs+ t; Z% I7 P" V' l& A* b% s
: }1 Z3 g8 q) v
依赖关系:& ]9 b$ p3 g: t# P
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:" O8 @9 E3 K* Q
: q) a k5 T3 f/ [/ e6 f# o( F- t
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
0 l+ N a% p: D& R! y' |4 Fmodsecurty依赖的包:pcre httpd-devel libxml2 apr
. A L( g% J6 _& [$ K0 i
! e. b/ {! E" H5 i' Pyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
- t7 @, P, E2 M& b: v; E9 A二.启用standalone模块并编译
# X5 _/ i0 U1 A; c& R4 B( d% l1 q2 {- w2 ]' v
下载modsecurity for nginx 解压,进入解压后目录执行:+ c. f4 }, l. c) G
! ^# R, I% g+ S# f) l! u! j; v: I
./autogen.sh2 q Q/ |6 ^9 {8 `2 Y
./configure --enable-standalone-module --disable-mlogc
+ I0 o' q& l6 q+ _' ~& `. Qmake
( H" _# d* ~/ y6 u. _8 m0 ~三.nginx添加modsecurity模块* u+ b9 j0 G* k$ v, ]1 g- D
3 u A' f* u" z4 U! @* O在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
, c( `5 F: t5 H. {1 D5 v9 b. B0 r3 c" h2 R5 H* |
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
# g' M2 \) e1 r- Gmake && make install
+ ?( ~1 L/ b8 a/ w四.添加规则
- F% ^$ O/ v4 e) I R( F2 k% q, V. p, Y8 q
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
; E' Y9 ]# R9 s% M
* K) d, p9 Y! G I& O; K: C4 s1.下载OWASP规则:
3 E3 d0 c( u8 ^0 k# F6 i
5 i" M& [$ P4 Lgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
8 j- P l, \, n$ j: L7 l! l% Y8 R7 r( q3 M/ E9 O
mv owasp-modsecurity-crs /opt/tengine/conf/7 f# r8 P( s4 [0 L# C* \
* v1 ^- i$ O+ s3 i$ p4 ~cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf. ^% x4 g/ L& N* g4 V
2.启用OWASP规则:
5 T3 H9 V V& @3 s3 D/ |$ {8 k) c7 s" B7 V4 u3 c
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。2 R5 I. M) m ]$ I
) q g7 ]: o% }; y: M/ |, Q
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
" @, ~4 x4 Q/ {& X( a( \2 u1 y1 g% R4 j# t
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。$ X( Z$ A- w, P% x
& X+ d; @3 o7 S; X1 J; RInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf" u/ G" ?7 R; Z% ]! I2 d' j6 N# a
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf# d+ `1 p3 r/ l1 S/ }# A
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
3 E+ C' i' ^; e6 m0 A# Q6 F; GInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
6 d& S) T- R* lInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf# U6 W2 w' O6 U6 K
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf; u: _1 M& z. @$ ~
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf* I3 Q4 W8 x8 e! k( H) E5 m
五.配置nginx
+ \) @9 v2 i3 N
/ E; k, }( j; u# }4 U: \在需要启用modsecurity的主机的location下面加入下面两行即可:* {: ]0 f% U, G7 R. x
1 I( P4 {5 K9 _% F* y8 |# jModSecurityEnabled on; ; S, z& a. [$ n2 j( _
ModSecurityConfig modsecurity.conf;
/ o7 ]9 N7 G# a下面是两个示例配置,php虚拟主机:
8 V& F. T; {" [6 u* B; b
' L) V# n: L( n5 c: W, Sserver {$ l4 R* N. Z8 B8 a; a" Z" h
listen 80;
! M( l5 H- s* ^ server_name 52os.net www.52os.net;# r4 b9 b" ^7 k; ~5 ^" S: _6 g3 O4 f
- D2 x: u! f3 @7 h location ~ \.php$ {
; R9 l+ e; T' ?! J7 L, p ModSecurityEnabled on;
; b0 L$ P% r% j- d& i5 Y8 F7 T ModSecurityConfig modsecurity.conf;
+ W) d% B5 F! l4 M) Z; V; A2 j9 m# E- K% c" M
root /web/wordpress;
4 `1 b& @$ U9 @) E: s index index.php index.html index.htm;
* R' l; M3 e3 p' q. i& c8 @ # v8 r% F) o% H' Z; q( u4 X% ~- ^
fastcgi_pass 127.0.0.1:9000;2 K* K4 N8 f- _& Y! x7 @. A8 Y
fastcgi_index index.php;
7 i q6 t) }4 A& E6 t fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
& g* M. i" F% T4 S8 @* ^ include fastcgi_params;
& _6 s# x9 [9 @( ~. ]+ ?. \ }
2 c( g* Q- t& I& Q9 O4 r8 w- \ }
( y& L# q9 `5 y/ J. G$ W5 p$ e' Bupstream负载均衡:- M2 y+ V7 } D: k5 u0 J3 @4 ]
$ N; ]5 @; D6 [; |( |- q) o7 z) D
upstream 52os.net {
/ d+ E. f! ]+ l( | server 192.168.1.100:8080;
1 ?- b/ v6 |( E0 I- p9 e- i server 192.168.1.101:8080 backup;1 v& y) R6 \* ^9 d6 }& ]
}
/ \9 {# X- G8 }2 ?4 Q$ l4 Z
1 r9 c! A5 O3 x& E' G' ?server {& Z; W. R' ]; Z# L
listen 80;
( v% ]2 n; ^# P* q9 R, _2 yserver_name 52os.net www.52os.net;. Q6 l# V5 C$ r9 [6 ?% r0 Y3 j
, m ?3 M/ |1 G/ elocation / {
) O7 V# r8 w, J1 W& ]7 B ModSecurityEnabled on; ( W* w0 e6 w, |* k2 r
ModSecurityConfig modsecurity.conf; & q5 n, t2 U' G7 x0 Q. ?. M
& i7 T2 s: A, Z, o proxy_pass http://online;. X0 V" Z5 G1 T4 I T( B3 z* F
proxy_redirect off;6 ?" U- i N. A+ f( D* G# g) N
proxy_set_header Host $host;! S& s8 }- x+ h& e
proxy_set_header X-Real-IP $remote_addr;8 m6 ~3 z; q) k V7 \
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;3 v. q( X B4 E ]* ]
}- Y: _ @ B4 w8 S1 B8 b) n
}
0 `" Q$ ~0 O4 B! v: w/ @' R7 [7 d六.测试
% D2 `) _8 I8 C. e
( T" M6 w% l* m, ], Y. R我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
5 p0 ~6 p9 }8 o+ }0 {2 m, _: l+ G: [" p- i0 j. y' |
<?php- D1 h: b/ A4 T% _! \# l: I
phpinfo(); * x! m) R1 o7 N/ w; u' m! Y
?>: g8 M2 v, q" K2 P1 x
在浏览器中访问:
! ~! }3 }$ |8 a8 f \- n# s, q
9 @0 q8 g6 I# i$ t* E6 @# }4 f6 shttp://www.52os.net/phpinfo.php?id=1 正常显示。
5 j, Z. l+ @* G) b) [# a A. G$ q* Mhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
3 h) y6 \9 R! X& y+ M( Xhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。4 x6 N& w1 }6 T# O& l" p
说明sql注入和xss已经被过滤了+ F, a8 `8 m% [
/ {5 v- U* \' d1 s# X七、安装过程中排错2 `) R' w1 b# M( V
, C4 S4 G, F- E4 p) F' t
1.缺少APXS会报错
; q5 \6 Q1 b% O c x4 I
% J: ^- N7 I* U9 X" r G8 k, Mconfigure: looking for Apache module support via DSO through APXS' f1 y/ H5 G! C; U: L& t
configure: error: couldn't find APXS
! J5 O, R3 F6 gapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
H4 {( w1 k" g* N7 ~解决方法:
, C+ f0 ~( ]/ \) p X$ `' ?% S
3 B$ @) G8 j' O2 T6 _* m; Dyum install httpd-devel
. i) x( E* b; B' J2.没有pcre
+ b; ?. f7 `3 G+ K$ p/ \4 j' C3 p$ J# E* e" H& {+ P% f8 j
configure: *** pcre library not found.9 ~, y9 Q/ F' u! N8 q! L& j
configure: error: pcre library is required* g: N& v7 ^- \1 M! i x |
解决方法:, P( N9 x- B$ B
3 f. ?" V+ Q+ Z5 Z' I* l
yum install pcre pcre-devel6 Z- m- Z4 l; E8 g5 ^
3.没有libxml2. N9 F- N3 Z) K, h3 F) E: R5 w$ I
$ w. S' _5 b) q+ G- o7 K# l8 U
* r1 i3 K2 @. X0 @1 pconfigure: *** xml library not found.2 Y! o% [' M4 b& i( w/ G
configure: error: libxml2 is required
% U4 a- f I; c9 R0 N5 T解决方法:
$ _2 z- n S5 V2 w& z% L) J; s8 B( _
yum install libxml2 libxml2-devel
/ L# e% w/ D5 v- [4.执行 /opt/tengine/sbin/nginx -m 时有警告
- x9 J/ f+ k% Y/ ^& ^- C9 N7 L ~# r& ]) K( ?) B6 ~, @
Tengine version: Tengine/2.1.0 (nginx/1.6.2)6 K$ E+ M% S g1 N
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!7 e: y/ B1 g( `) j3 E
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log: [; {* ]" r' Z' S$ v- A2 n
. s) y& D( f; v4 g* C
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured./ X# l+ o3 q' Y' E2 ^- V* m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"" J: M3 P& ?& m1 ]! C
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
' D0 ` F3 {* b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
w1 Q% ] U9 \0 K& K; _2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"4 G E, Z0 y( e$ T5 }& ~
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.+ G% b- k7 q$ x5 u
解决方法,移除低版本的APR (1.3.9)" ?& r6 }# d' U; _
/ S9 N+ `, W# r* k- W& U
yum remove apr( I0 d1 W) n" ]9 m# Q% T
5.Error.log中有: Audit log: Failed to lock global mutex
9 v q7 i% L2 c. j* A
2 K4 [- ^7 S& y( o2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
0 I/ s T5 v; t# K- x6 aglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
/ i( j! p5 \* E$ F; a解决方法:
- O2 v+ w5 R, q7 t7 q: A编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:. l1 }$ C- u1 h! p6 z5 J0 a E
$ F, D. Z: t9 c* W
SecAuditLogDirMode 0777$ }! O2 w: k A' w
SecAuditLogFileMode 0550 ~0 f/ s9 ?8 e! {
SecAuditLogStorageDir /var/log/modsecurity# a( I5 w1 V% k
SecAuditLogType Concurrent/ d2 G2 {6 m' E8 `5 L' J
参考文章:) q! h% Y8 S: B' N) t; d* p
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
. T) O# O. b0 f* Bhttp://drops.wooyun.org/tips/2614 |
|