|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
+ ^8 O% g+ |% Y/ F6 A9 Z$ a) a" ?. J- B8 S6 O1 M
一.准备工作. D# _5 [* z7 m3 X* E+ t0 @4 }
; {1 [; r7 q, v+ N) [
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0: C3 G( i/ v: `/ S7 V/ G3 j7 z
, \9 m+ ^1 ]* ?' ] x$ G2 K8 jtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
- E% ?5 W( g, u! f
6 |9 m$ J J8 H/ V: |9 r5 Emodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz9 o( J0 m% W' J* x# T3 p' `" q
+ {3 k) b6 u- W! y; zOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs7 U; Y% e# c8 G6 x5 {+ X. Z; a9 Y
" g8 R) ^- j( |- ^7 k4 V# o
依赖关系:5 \; @% @9 P4 ?7 l7 @' z& q; X$ M- {0 m
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
8 X+ F8 n) c8 G+ ]& a- U3 [# _$ x( e0 j
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
1 _! m8 } W, x+ `) ?1 imodsecurty依赖的包:pcre httpd-devel libxml2 apr
m! k' r$ l# v3 e
) Z S9 n$ [1 {, F* ^2 Iyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
% H, R' U6 p" F& T) w+ @& h二.启用standalone模块并编译 B; d! i( e# d- j
9 N0 K7 z# K& |0 B% r% D5 e
下载modsecurity for nginx 解压,进入解压后目录执行:8 U" i. z% p; K
X+ B6 Y t2 A
./autogen.sh
3 E0 A" s5 s5 O. c$ M2 r* B# q( | V./configure --enable-standalone-module --disable-mlogc
* v0 G- `' O. x& @6 Z7 s* emake
3 F5 a" d6 R7 H" v8 f1 C三.nginx添加modsecurity模块: ~) S; z) X2 w$ p3 |* b% T
2 l4 j+ e4 u2 m1 s: z# P G
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:! Z7 d: g8 n4 C6 m
0 b3 W7 a0 e9 R ]$ e# u
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine/ o" y& r# z" [7 w
make && make install
# ^% R$ S1 {. W% T四.添加规则" z4 B; S& J* x
- B. a; `8 d3 B& U6 b1 h9 d1 S
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。0 U4 V$ c9 h) {# s7 `
) D6 i/ F; F3 v+ J& l& c i: n0 x1.下载OWASP规则:
& I6 f% Z9 i' t5 F; v& f5 m" A$ U3 m* c& U* U; a3 a
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs/ Q5 ^) K0 [4 t' D- V
" L* T! S* {2 x0 @7 o2 Rmv owasp-modsecurity-crs /opt/tengine/conf/6 j0 W/ Y# u' c: e" Q- e2 F- l
. m4 }7 q3 k. q; F# l. @, a% L! K" ]- |
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf9 _. P+ C6 d- S' @# `# L H6 d, i, p
2.启用OWASP规则:+ _. c# O3 X6 _2 f4 w, C- I
0 o$ W4 K$ w- h0 D7 ]6 i+ c: p
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
2 E1 A0 C; \0 P4 Q1 R+ _1 N/ R' {- H8 Q" M1 w
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
/ |; ^0 Y/ Q. m! A# E& ]
' }0 U5 L) P" a# h! }owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
7 O/ m/ \- E& b6 Q' @
- `# {. c; ^5 ^, M1 b: l! a: v1 BInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf, L, o/ ^0 M' u U3 X
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
) O: Y3 M$ ~+ `/ ZInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf, u( x1 A# g6 E& i
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf% g j9 v( I: y* J T
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
; m. x1 I/ p3 u2 BInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf* J7 \* Y1 M {1 e
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf' r1 [* Z9 b2 L: ^: U/ s- M0 E
五.配置nginx7 u. s2 F; [4 x8 }9 g
& m6 h3 k( x6 v3 t
在需要启用modsecurity的主机的location下面加入下面两行即可:
6 I [* k5 h8 @0 I; m/ f+ ?/ L* k7 _/ ~* b, v. _2 c' T, r
ModSecurityEnabled on;
0 J( v V9 _. D: L& M+ {" e" lModSecurityConfig modsecurity.conf;/ |7 f" \9 P( `. o3 Y
下面是两个示例配置,php虚拟主机:7 }. W3 B0 g5 x9 X1 a( X# N
0 i" K/ a8 G* M. s# i# y. J$ eserver {
3 f( `- T+ I: h3 N" a& L listen 80;/ q9 K: ]& [! x
server_name 52os.net www.52os.net;
7 ^ m9 U3 d5 S2 q g8 h- ` - i- W g6 ~2 o, o" Z; Y! `) ]
location ~ \.php$ {
; F `* ^' g+ |1 O! d: r ModSecurityEnabled on;
/ Z* p5 I; i: h ModSecurityConfig modsecurity.conf;
& ?: t! ~8 X2 S" @" C4 h5 T* g1 Z4 |1 y
root /web/wordpress;! G; r1 s- a. M ~4 z* k
index index.php index.html index.htm;( t& g1 q6 E% _/ M4 @
% m% ?$ M# c" M; M4 A fastcgi_pass 127.0.0.1:9000;
3 N/ g2 c1 I+ \& R' d6 G fastcgi_index index.php;
@+ F" m9 d- A8 x4 F fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;# H# G. S& [* |
include fastcgi_params;; i% k# N- d# e: ]
}
0 |0 f6 V* x# m' e }
% }3 ]' O, F* \3 K& @, F9 wupstream负载均衡:0 a/ d6 H8 m5 ]$ m# T, v5 Z0 k3 H
5 r1 k* q' Z. F3 pupstream 52os.net {
4 ~! D* M2 |: Z( J ` server 192.168.1.100:8080;) U/ [3 {! {4 |) q6 A
server 192.168.1.101:8080 backup;
" g5 A' o1 y% T}
( q7 o: F% ]4 O* F, M' ?) b5 @- X* }& f+ s; a
server {
8 P" n8 x* z; Q; W8 t" Slisten 80;) D5 \8 k* r2 g# O: K9 Z% g
server_name 52os.net www.52os.net;
- p8 u$ g' S' N# l. z2 |" w( Y7 }: h* }* |$ _* O' c6 \
location / {* l' I" n7 |5 g) v1 Q9 t8 j, M+ Z
ModSecurityEnabled on; # S/ U7 \. w" r4 k8 N
ModSecurityConfig modsecurity.conf;
E- n& H1 Q! H: p; z# _3 E) y- ^: \+ l+ r; m3 ^& D9 F* z. D
proxy_pass http://online;1 {% w$ T& r( [, U
proxy_redirect off;$ Y- ?6 W. I. W! g, j! V6 K
proxy_set_header Host $host; D( u4 l8 v2 m4 |5 d* l" u" H
proxy_set_header X-Real-IP $remote_addr;4 r {$ Q7 ~- L* o5 U4 U
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
. z) ]+ M2 s; r6 O }% q# R; v9 a" o' o8 L
}# E8 |# `7 j; L
六.测试/ i: T0 u2 O3 Y# v) m& B& I, t# `
) w9 C5 \. P b
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:. y# I1 ?4 e4 o
" m6 J8 h, o" m# m
<?php8 |+ X: a$ j2 }# Q2 A. ^
phpinfo();
- k- c2 C( V. t1 e1 ~3 ~?>" j- V8 Y1 w$ j7 \* u; q+ t5 y% _( P5 M
在浏览器中访问:/ ^2 K) {; B# N- T1 p8 `5 K
; I4 p1 |7 K3 x! f9 \( Ehttp://www.52os.net/phpinfo.php?id=1 正常显示。
6 y& U1 a5 H( W% N5 N0 Z7 b* Qhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
0 Q2 y. D# j! Khttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
3 _# _2 [ X$ J5 F2 ?说明sql注入和xss已经被过滤了
' m' a1 \+ m" K7 K8 e
* a& ]! V0 ]7 j% X7 p# J七、安装过程中排错4 ^+ l8 W: U2 h0 M5 a! N) [7 f- ?
" v$ e+ s7 U1 h3 M& n: c, Z# F1.缺少APXS会报错/ \' Z9 ?" E3 G) W$ P7 B& b* e W
7 e% m+ ^) D- |0 K6 |8 r& m
configure: looking for Apache module support via DSO through APXS9 u" s( i! V, f' P( t9 M# u) b
configure: error: couldn't find APXS
! ~$ c0 L. Z* r, B: w$ n) Y2 papxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
1 p1 P! O0 r9 J ]8 O6 o解决方法:( C4 N' q$ ^3 U- p& P `
0 V& `: I, I9 [
yum install httpd-devel% k3 V( l, W6 {3 |' G+ S
2.没有pcre
% }0 m+ @9 @) F e% O& s0 ~, G! \. d* B: f1 K; D! O8 K4 m; S
configure: *** pcre library not found.
& z5 A& S2 t" k' M6 q0 n4 Oconfigure: error: pcre library is required/ p6 q. L( i' a5 u
解决方法:
/ ]+ u+ }: c! M \ V
. [% i. W. |& Q; k. n1 i# jyum install pcre pcre-devel1 A" @- v# @0 F
3.没有libxml24 V& H; h7 R- C6 S2 N7 c& ?9 T
2 p M% T3 g) C! r( R/ n4 B* m' ^) R# I
configure: *** xml library not found.2 r2 p. B5 C; I
configure: error: libxml2 is required
0 Z- i4 |# R9 ^解决方法:: W9 V. g" p ~: o3 b# ^7 `
& O6 }) ?/ d, P5 s# s0 ~9 M/ [4 \yum install libxml2 libxml2-devel/ l% m9 ?- {/ J' g+ @0 {, \
4.执行 /opt/tengine/sbin/nginx -m 时有警告2 |+ P9 q2 T2 ^! p2 h' T! e+ {
+ A" b" B Z* _4 A1 h, M. R/ ATengine version: Tengine/2.1.0 (nginx/1.6.2)
9 l" B, \* I! E) Y% E- Ynginx: [warn] ModSecurity: Loaded APR do not match with compiled!$ ^* a8 `1 n. D% M5 ^
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
2 F4 [- M, H7 |, c
& g1 A1 K; i8 E: @/ \: z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured." v% J- p# h8 l3 V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9". k; K- l% m8 h* m# @. X `" K
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!% a7 y9 G* B7 l+ n; B) S* ~. W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
; A& F0 i( i1 I% C' m( y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
; h$ t) B4 w$ W; h2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.6 `9 q, X4 l9 D/ R; M& w
解决方法,移除低版本的APR (1.3.9); @7 V2 }0 U7 \& D, b# _6 y! O
@& z: [& E6 F
yum remove apr' X" I: V+ r6 w* Y, ~1 L& O+ ]& ?* B
5.Error.log中有: Audit log: Failed to lock global mutex
5 L3 X" |' T8 x, q8 S- i3 F/ l" S6 \# l# C, Y
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 9 O4 W$ m0 y4 n/ s( K
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]3 z0 h( E6 \: A7 c
解决方法:
9 _6 {. A8 W9 _6 I- n编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
' D% |' X* W d) |9 Y" W8 F, z/ X- }" J9 Q; ]* ?5 w
SecAuditLogDirMode 0777) C. M) f4 S* V a3 i! e1 t, f
SecAuditLogFileMode 0550
; s* W# `/ |1 R) lSecAuditLogStorageDir /var/log/modsecurity6 Q: d N% U$ x# F; ]! t
SecAuditLogType Concurrent
$ `; I/ D; y p' D( U" l参考文章:* a* Y$ B. z4 l4 L2 Y) w" m9 ~7 C
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
& x& ^( p: D' w; W A- J( c, bhttp://drops.wooyun.org/tips/2614 |
|