|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
1 U9 z: U7 M2 l( |; l% a, Y5 u: n. W, x( l5 _# [
一.准备工作% I7 p" d2 {" t( R+ m- {$ m. j" h
" x! J' l! w6 w) w M2 `% s系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.01 @9 R- e a/ X' w6 ]: G1 V2 }
0 {' X( l( U* ?/ `0 e- D
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
# x- d% l2 u+ V) s8 u$ R" c. P
; g( l5 j' r. Y2 k" i8 ]modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz0 _- M; m' m7 i; @/ {8 ]( M
3 _! B' S# n, w& eOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
r( b& O# E# W/ ?3 i+ `8 l) i& B
依赖关系:
" }: B4 H' J. Z# ^) R# ]' f- itengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
7 y4 w2 J6 r, v# y. {, c& L
' D! _' B: H. C. `- ^8 Ryum install zlib zlib-devel openssl openssl-devel pcre pcre-devel% Q- i5 |" D% ?, x4 M, T
modsecurty依赖的包:pcre httpd-devel libxml2 apr" M3 ~( c! Q2 X" C5 M: K
- D* C6 X1 G0 Q0 S4 K6 b z
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel1 p; [+ @# D1 d; z
二.启用standalone模块并编译* l7 m9 M9 n. e" |. |
, V9 @ O) e2 F7 S下载modsecurity for nginx 解压,进入解压后目录执行:; @6 h" X, w# C) [
7 b$ D$ {9 V; \# d./autogen.sh
/ b" Y/ r6 t2 o./configure --enable-standalone-module --disable-mlogc
3 Q9 C( T2 w1 t- w% f0 Umake
+ R6 u1 T( L: X# ?% u三.nginx添加modsecurity模块
" ?1 F9 ^( R3 R0 [2 C" F3 l4 }& }/ F9 }/ T3 y
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:/ u# d5 g& t% Z) D
, F2 T. [' x% a3 M./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine3 y7 B* E; `8 l( s- O6 Y
make && make install8 O% B! u v# E4 g0 n7 ^( |
四.添加规则
9 L) N1 o) V1 N( P# y6 j# @! ~6 L0 o4 D b, m2 G& ?
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
) I* z/ n$ z0 ^/ n. j; t: C* r) H$ b3 b, t b
1.下载OWASP规则:
4 ?, ~; f% i! R, h; F( v! p8 J: \) M6 ?1 H8 n8 y
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs% a2 Z0 m# p/ E8 U C) G
" p! J( `8 A( W- i, C4 j
mv owasp-modsecurity-crs /opt/tengine/conf/% h' h9 w4 R' `- [
3 V0 x7 w$ t$ J4 o% Scd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
. J I4 k& ~" r; X5 E2.启用OWASP规则:
5 O" w/ E; S, e$ x( [4 u/ \( }( [2 j; p$ s
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。- O" O( l# e6 P! x# B) @- z& ~
' l8 l: w; k9 L0 K1 D: z- F编辑modsecurity.conf 文件,将SecRuleEngine设置为 on6 |# i/ P/ Q1 L3 c0 A
/ C k2 ?% ~. k
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。2 f# b1 \( h4 w% y
, u$ W! F9 r3 ^/ R* {+ H* ]7 CInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
" a p% d2 {, E" U# XInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf; c0 {2 g4 E6 |! m. O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
" V. E1 O, g. gInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
7 i8 l8 U8 m8 M& Z! J; e8 DInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf# n7 K: h) r% c: v4 t
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf5 I4 P* y* b7 Z7 w7 Y0 C1 L, @
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
0 G* [$ s- V! r- @五.配置nginx
5 F( j& s; \2 R! |, s9 w$ L5 z" O5 x8 b5 }
在需要启用modsecurity的主机的location下面加入下面两行即可:
1 R1 e/ o! v# J, F
4 A8 J* h$ Z: j2 {" F; L2 R, C# q2 yModSecurityEnabled on;
7 }2 U/ y& w9 hModSecurityConfig modsecurity.conf;
0 [1 w+ f% s& `, _0 W5 D4 T1 C u下面是两个示例配置,php虚拟主机:1 C/ |8 A' n! c; y" H, n
( e6 I6 J1 }( u1 H, a7 g, k8 k+ a
server {, a" M% ^8 f! j+ f6 W/ W' l
listen 80;1 f& C* i" F6 E& P- `
server_name 52os.net www.52os.net;& T, A# a; K0 ^: L+ X+ E1 n
- H! C( x$ z6 F. ?1 i8 M' `
location ~ \.php$ {
7 k0 L" b2 v2 O( Z- y8 l ModSecurityEnabled on;
3 f8 F( W y V4 ]- p+ E! z ModSecurityConfig modsecurity.conf;
; V; A0 ^3 B! s
" a% e1 L. w2 S6 D- z root /web/wordpress;
9 s B; d3 A* t3 K; b index index.php index.html index.htm;
4 U ^$ o+ A) ?
9 n8 Y5 f3 `! g, O! q7 s fastcgi_pass 127.0.0.1:9000;0 p6 z+ U* f8 {+ _7 T: a
fastcgi_index index.php;* s& e1 ~# X+ W( N
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;+ p5 t' ^9 M, L) Q) x. P% }
include fastcgi_params;
W$ Q/ ^3 T4 o6 }9 r+ ^* | }
$ t: B2 U9 x! f3 C8 U, R g. r }) R$ U- @6 v {. {+ e1 [' [
upstream负载均衡:
/ \7 |$ K! ]: L, K5 e8 ?
* x0 i# Y) [! H1 q4 e7 C$ mupstream 52os.net {
! k, [. a0 i1 O% Q3 H server 192.168.1.100:8080;
0 U9 T2 @/ q4 C W% z' F0 k5 u) c( t( W server 192.168.1.101:8080 backup;! E9 v+ s9 `* P( W& b
}8 L: z3 C" [, G6 U3 X
8 O+ P& }' J" P z k0 H
server {
G5 p7 m" N& _8 V; zlisten 80;
' N/ X' R! t3 N' l4 m/ N0 d9 mserver_name 52os.net www.52os.net;6 L8 g! |6 Q' Z5 Z; W+ I! ~! M
1 ~. p; F; L# ^3 ?- v Tlocation / {
" Y! f9 E6 e" j" r% u ModSecurityEnabled on; - @* _. a% R! u7 M, G2 f* w; O6 G
ModSecurityConfig modsecurity.conf; 8 p8 j2 [4 u c
5 O. K6 E$ a; j% y- A
proxy_pass http://online;
+ |( \+ K3 |' L! X. l- ^2 x proxy_redirect off;/ b) E8 }3 P0 g* i. e. d
proxy_set_header Host $host; o6 M; j/ ?7 A4 @* x. d
proxy_set_header X-Real-IP $remote_addr;& C* V7 a. t [
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;7 u6 D9 {0 _) O! Y: p' F, C" K" V
}
+ c( V" W" n9 H' R}" T7 u( l( [0 a! a
六.测试
) H& f! R8 K2 v0 |! x3 Z" O
2 k$ X# r) X, `# v8 _我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:5 s' \$ z7 X" C g* L# m9 e( \; R
, e; X! Z, w0 f v& U/ m
<?php
& L6 d! r% y4 y+ i8 [+ m8 K+ t. L phpinfo();
. V. L4 D1 p+ ]' x?>
% l8 j$ n/ P, W! o7 Q& e在浏览器中访问:
5 X# l* C$ S0 X8 V5 d7 ~" e, E- V+ `+ R- ~0 k0 r9 {; @4 q( T4 D
http://www.52os.net/phpinfo.php?id=1 正常显示。; i6 J0 ?! H0 f7 A! |) V7 \
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。, \, s# t1 m0 j3 R7 p# w
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。$ g4 E, w9 { c+ F
说明sql注入和xss已经被过滤了
8 r0 B! v& ?( ^; w
) Q6 C2 c8 t" N七、安装过程中排错) X. d# D( ^& y2 ^5 p3 B7 p Q2 R
% S" {5 x3 m0 l/ l3 B
1.缺少APXS会报错9 x# J$ @1 e4 ^( R- a4 y
: u* ^% T$ s- ^
configure: looking for Apache module support via DSO through APXS& d2 W' F1 x/ u8 s
configure: error: couldn't find APXS# C0 ] T5 R1 P4 u4 c6 [' a, e
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。5 h, u$ _ \0 y9 H( S
解决方法:: b% f3 F/ j( d+ q+ Q
8 S6 H1 i( ~& O/ }2 d
yum install httpd-devel
! R+ @5 n. W( ?/ L& |2.没有pcre0 D0 M3 c7 Z. `1 N4 O' n. X
) {- u! ^& b* o: y, `configure: *** pcre library not found.% p$ c4 o% s" x1 ?) D
configure: error: pcre library is required
( S# j! \) |! h. I/ c' Z8 h解决方法:( o; o/ o* [$ V9 C% R) {
' A# c) y' \9 D2 I& G$ h+ s# D7 F9 pyum install pcre pcre-devel& @+ S- i6 k- u# z% Q% E8 \8 O
3.没有libxml25 _/ ?+ P1 a z, \$ \
$ H1 {1 D3 U5 n
, Y6 \9 n3 M/ Vconfigure: *** xml library not found.% j3 ]" m' c" m3 T( C8 \
configure: error: libxml2 is required
+ @$ v# q7 x$ q解决方法:' f% t3 C4 a$ E6 z
9 b2 k- W' v6 }0 h7 Syum install libxml2 libxml2-devel& H; O6 `: S) K2 R& G
4.执行 /opt/tengine/sbin/nginx -m 时有警告& Q2 m( N/ F$ T5 R/ j& j0 ` y" n
- U9 r4 H: W/ A. j9 h
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
7 _: t \/ H, p7 k4 rnginx: [warn] ModSecurity: Loaded APR do not match with compiled!# X/ [" T) Q! D* P4 ]% y( Y' S
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
% }( J( v$ b' \7 [1 l+ y9 ?' \/ {& D& g5 M% ^) J4 r$ W+ N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
8 k! C; N) |5 e+ Y, @0 f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"* p9 s" o! l' I6 n
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
: H7 r2 _; o' P2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"9 V& v5 ]; D" y' T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"5 I. I/ }$ l) Y9 q( {( f$ Z
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.; e @2 ^( K1 e8 l0 n5 [
解决方法,移除低版本的APR (1.3.9)! C0 {! S8 B" ?% F( r
" F, ]2 }3 g, T9 f: ?: ryum remove apr
, n: H' o. ~* G; z# s5.Error.log中有: Audit log: Failed to lock global mutex1 q4 _3 q( u& n0 Y3 C+ \
2 O3 I1 v$ a6 K H' N/ z6 U z
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock : d& w7 Y' N# U5 h5 q0 E
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
) w* M* S9 T0 d$ Z9 m8 A: Y解决方法:
1 j7 L5 |+ \3 A编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
- r; X1 }) x! z/ W
2 v) r, m4 j0 i) _( H: x3 T* D% KSecAuditLogDirMode 07774 k# c* `- s) ^1 W# w& ~
SecAuditLogFileMode 0550
: D3 k* c& D3 p6 x' D1 @SecAuditLogStorageDir /var/log/modsecurity
; H7 i& |& @9 p$ u- _SecAuditLogType Concurrent/ ?# y( l) s \& G/ I9 O, U1 I5 F
参考文章:
/ m+ n( D2 F5 R9 m$ f9 \# |/ qhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX2 H5 V- ]& Q, z) b
http://drops.wooyun.org/tips/2614 |
|