|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。+ s8 `3 X# W* r' _
! f. V1 B4 T( k1 f/ i! l8 T
一.准备工作) G, f( H4 m" ]
# u* s) ~+ ? ]" c6 E6 \3 ^6 Z9 q( d
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
# F+ P1 X, n( S5 e2 i2 v' U }+ b: w& o/ A) P
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
) e* Y- F3 ~! }& C) \. U
1 J! {6 |2 x5 M) V4 _modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
1 {1 s3 _# L' L
; g7 H8 t, ?! e" ]7 OOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs, i+ S+ H" }( K+ g: p- [# @
1 Y* I; o, J2 q5 A% t) R
依赖关系:
5 \9 L! t& H1 [+ H6 e& r, Ytengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:: q8 O6 v1 n X4 S" P; o
& ~% E. ^" }! }/ q
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel" ]0 n- y) z. |2 ^0 W
modsecurty依赖的包:pcre httpd-devel libxml2 apr
4 n- P) @4 Y$ g7 @7 U3 @6 k* ~+ R& s0 n( u: R' c0 F
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel: J# Q- x" K% R
二.启用standalone模块并编译1 K5 F3 B. Q, Z3 g J
% o: L7 C0 _9 y
下载modsecurity for nginx 解压,进入解压后目录执行:" d" ~2 R/ F( u: G& b
, U0 j2 f8 ]- Q' D; `2 O* }./autogen.sh/ }$ B, Q1 I; @4 y* v+ h. f/ K* k
./configure --enable-standalone-module --disable-mlogc# b I+ c5 ]( ^% D3 n5 f3 m8 J: \
make ' |! |9 B. y' S. r& E' E
三.nginx添加modsecurity模块
2 e3 p$ p5 f# D; a6 e4 r' o' u
4 {3 W) F7 z% o3 c# q在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
5 E8 q1 \; v7 d0 f) C& K8 z6 h2 Q" @
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine+ @0 P/ F( P. Z& K7 t' L: k
make && make install4 _4 \8 o" X7 O2 Y
四.添加规则
% [3 U+ W$ z; ~" D2 O0 Z
9 e) o7 N7 h- x: ~+ `modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。$ A- v/ `- Z0 u9 c
5 H" ^' T" L! F) Z1.下载OWASP规则:
8 F. p' r+ V$ i: Z' r# h" f# ?$ o9 ^+ Z
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs }3 ^+ E0 \ F2 O
. d1 {& T. X0 f( E- n- l/ d- ^) Lmv owasp-modsecurity-crs /opt/tengine/conf/6 y8 @- w- K: n: A- ?5 e# {5 e
, \' z3 o0 j! W# |$ C7 Xcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf `0 U8 q8 u& l: K3 e H' q
2.启用OWASP规则:
/ a9 g: L/ C& T; g' X+ X- {4 E, M# z; u; W2 m) {
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
& r+ R; r& [' q/ T4 e
, j; B2 z3 D) S' |编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
$ F4 r0 E+ H4 B. T% j1 [4 u4 M4 S- p7 C0 R1 r/ e7 ^
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
0 g1 ^2 P T1 I
, B+ c1 {" H' c" ?0 ?Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf9 ~" f8 W2 E/ \
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf W8 `) C# ]3 R1 I8 M/ T: r/ e
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf$ ]: Y8 m& m1 Z' o( [$ l1 b% t
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf% ]& o5 X" p: m
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
! g& Y/ V4 v q4 x. `2 c6 a3 { VInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf5 {: w+ G& A1 L/ N
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
) V4 L) G; v& C% R五.配置nginx, K; _, Q: _3 x- @2 x
2 T u: _2 o+ i s5 ~* V' n1 g
在需要启用modsecurity的主机的location下面加入下面两行即可:1 j8 D$ l. e7 D' G) q- P( L% `* `
* ~. b& h& ?1 s( s$ E* O4 kModSecurityEnabled on;
& k1 t3 j5 _' G; R5 |ModSecurityConfig modsecurity.conf;
1 i* W4 s& R3 a* u/ l( D下面是两个示例配置,php虚拟主机:
6 E' v. s3 N! ~- Y, F+ c* z4 J. Z. P: Z0 m: Y2 k+ P% C2 p
server {& X0 E: n; c d3 G/ r
listen 80;
A c2 U6 q& R( B. p( i" E server_name 52os.net www.52os.net;
$ A" X! n9 C6 B9 y/ y& r; f' r' C & W! h0 @/ R8 K# t9 ?
location ~ \.php$ {
& o7 r& f; f% J% O" U ModSecurityEnabled on; , q( t7 g" d. n- q$ v. `
ModSecurityConfig modsecurity.conf;
6 n/ h4 o& H6 [0 h% P r% P
4 c8 X" [- [5 s5 E5 J root /web/wordpress;
" E7 Y Y; M7 b `9 } z index index.php index.html index.htm;
$ w; B, e. V) i4 o8 `# M2 Z5 z3 ^3 l 3 l5 q+ {5 V3 R
fastcgi_pass 127.0.0.1:9000;
' S' B& K/ g$ @$ V4 o4 M fastcgi_index index.php;+ Q- k3 D6 c3 n0 ^5 p) u/ w
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
5 @5 H; U8 w* E- F Y/ y include fastcgi_params;# M' j i. F/ U5 s( k* m
}2 ? p9 C8 [7 x$ U: g
}
2 S7 N# n- p- g$ c! ~! y& Xupstream负载均衡:
$ ]7 u- d# i6 S! A8 |/ w t; }4 q0 x3 h) d
upstream 52os.net {
+ b# H5 ~, M0 B | server 192.168.1.100:8080;
$ g( S' s1 _) V' v' l) p0 o! J0 D server 192.168.1.101:8080 backup;
2 V9 _# Q+ X' o' ]: k}
% V0 C# d1 V. x2 p0 E0 N9 O' ~# H9 N5 u0 Y
server {$ L, _7 H, W) P# x; R
listen 80;
- L: m2 z3 A; O fserver_name 52os.net www.52os.net;8 k3 |0 p1 Y5 V) P
) a( e1 }" T/ n( m) f2 ilocation / {/ s% I: Z% g$ r7 u; m( v$ G- ~
ModSecurityEnabled on; ' t8 ^! e5 K, Y e* }* U
ModSecurityConfig modsecurity.conf; 4 E0 I' g! C3 |
& C7 e$ A9 @, \1 d# Q3 \# ? proxy_pass http://online;) T9 d4 B5 f/ ?8 U( q' H, y
proxy_redirect off;% y. C6 Y: Y3 m5 o
proxy_set_header Host $host;# `5 P$ V! w+ j' c. t: V, N
proxy_set_header X-Real-IP $remote_addr;2 @: A5 ^# @7 |: G
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;$ u# N+ L- y2 R+ M. ?' | ]
}$ W# l2 C2 Y( r) _7 M" \- o
}
$ n M+ ?9 v* Z9 ^+ g六.测试
% k% W; b- }5 F6 s$ a! m2 _4 x8 k9 l
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:, m0 z F3 C' ~: y) a7 i( F
6 i* ^5 o$ o8 Q8 F6 J# S<?php
: @4 d4 k* k, a* O phpinfo(); ) u7 v9 l' Y6 M# x" s8 Z u& `
?>1 h% Y" G- H, r1 b5 D
在浏览器中访问:
K5 V/ @' o* X& y8 m
/ Y; K' ~3 t+ Vhttp://www.52os.net/phpinfo.php?id=1 正常显示。. m- s) J& K4 L+ u% y! o
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。/ t/ g+ v2 s* F1 z% T
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。9 o) n% e' O* S7 K
说明sql注入和xss已经被过滤了1 f, w& D# E. c T. z: W9 d1 s+ l" w
* o0 {8 A' a z) |6 O" r9 a/ g( v2 b
七、安装过程中排错8 W! S: b& f' I0 X& D. v
- J; p! Q( z# l( [! }6 p) q# G. J
1.缺少APXS会报错% b+ u% R& n6 S& m! K3 G! N
- w1 l/ \4 D+ P3 @1 Y: G" A
configure: looking for Apache module support via DSO through APXS, G" F9 j8 }7 p g
configure: error: couldn't find APXS% T: g' w5 i/ u
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。; ^7 ?, F7 R+ @( {$ z1 _+ _
解决方法:* X+ M" H. ^6 W% U+ d
. Q1 f X2 p( `. ` o# F6 n( k
yum install httpd-devel
8 `8 H3 }) ]" Y8 m2 B2.没有pcre; T* n1 ?( g1 x9 f, Z
$ [% {2 _9 B8 n# T/ o+ V F
configure: *** pcre library not found.
' M9 V- [) m4 E' W) kconfigure: error: pcre library is required) i+ Z! A& z& a( T2 M4 d
解决方法:
3 u8 T1 I2 A4 q: y0 U# t" U' Z: o/ z2 N% v8 W
yum install pcre pcre-devel
! `' {& e- j/ h3.没有libxml2/ w4 j. _' p `) A
5 p0 D8 E+ S7 `
* _7 H$ e. ]( B; z
configure: *** xml library not found.# H1 L& t6 s# d6 q. g
configure: error: libxml2 is required
# J8 c3 B& D2 a: g5 o4 r: @' u解决方法:' ]. A% W! X' \/ |0 O1 u2 k
0 P7 p# s [4 }) B( n1 iyum install libxml2 libxml2-devel/ ^; ^$ G4 h z
4.执行 /opt/tengine/sbin/nginx -m 时有警告
d& S2 ?4 h* U0 E: v* j `0 u
" Z- j& V' [5 P, S: ]: r, tTengine version: Tengine/2.1.0 (nginx/1.6.2)) ^+ w" d- h2 n& p* b8 i3 i7 z
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
W' _5 J1 j3 m原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log7 C4 [5 D3 V8 B" `) e
3 E# R& U( M/ l/ o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.7 e7 F6 l" g8 z5 | O6 x7 E
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"# P; K: y& b. \" e# D) M/ A( W& c
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!1 U. O. ^: t& D, F0 a
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
- c0 Q/ ]8 z! Q/ p: u6 u2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
* h7 B. g5 j( Q1 @ Y1 V2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.+ P' ]$ R& N2 b. j5 v' p
解决方法,移除低版本的APR (1.3.9) W4 s! U5 C1 `9 E. s
. f" r: P9 w; z# Z Zyum remove apr
4 ]8 B6 ~2 R/ x7 d5.Error.log中有: Audit log: Failed to lock global mutex2 l; T, i, d# v3 b# N2 [; l
) o4 L- \3 G5 ~" y. d( s- ^2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
' J6 d! c; Y K+ Xglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]+ s. i5 j4 y+ f3 c' f7 P3 i
解决方法:! l$ }' L5 C* m) @% k$ B
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:2 g9 N$ G- o$ [* U5 ~. T
& n a5 f' L& ?. x, Y$ rSecAuditLogDirMode 0777, C' ?) o* l& \# {5 \0 A; h
SecAuditLogFileMode 0550( o& I- T2 ^7 o7 j
SecAuditLogStorageDir /var/log/modsecurity+ [# W; \9 U! q! H) X
SecAuditLogType Concurrent
4 ~) E8 \8 M h参考文章:
6 Q! W6 Q4 A7 Dhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
6 |0 `, G5 l+ C6 \" khttp://drops.wooyun.org/tips/2614 |
|