|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
L: t/ O0 P" R7 G; Y/ o9 y i* L$ g# P4 s$ ^# r' a; f
一.准备工作
8 R, O3 ?& l4 t# }8 W L
( C$ a2 g/ ~# I" f1 @& v. {( c0 x系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
3 I: e' Q) ?6 D8 o) X; d
- i) U. w) Q% g7 L \( Xtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz! B' { C+ X/ o. |0 Q
* K0 }: ?1 A3 r& t- xmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz: y9 R* r. E% p7 I/ I6 o) f
6 v% `6 r% Y& r7 M* e
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
7 b* i5 Q# q5 g9 Z; ]6 z2 P$ x6 ^. v& Z, V* |, z' x: d
依赖关系:
# {' |, c& z/ O' Z0 _tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:/ x o8 [; |/ F/ W# V
0 Q9 E+ p: U+ q4 O& H5 Y% l
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
2 z+ }% B7 Q! d# c& p1 A8 omodsecurty依赖的包:pcre httpd-devel libxml2 apr: k( U" e1 o5 n& C- Y9 Y5 T; W0 k
3 t% ?$ {$ a$ [& J+ Lyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
9 p' l/ S. O- r二.启用standalone模块并编译: e* S G4 |# G3 l6 D8 B
/ }: P0 L6 B3 a6 U
下载modsecurity for nginx 解压,进入解压后目录执行:2 O" z# q0 `0 o" p1 n' e8 _
! C r4 f w0 j w0 m) V* h0 n./autogen.sh
& Z6 G* g+ V5 [& b./configure --enable-standalone-module --disable-mlogc0 o' X$ ~) q2 @! F
make
X8 G( t B1 R9 U$ k& _三.nginx添加modsecurity模块
* W6 n' k! G' U. j. i: U) E, F
" r. ^: E& Z% p在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:9 u. }( C! N, r/ t/ Q: j. }
6 X5 n' E4 r& Z+ |+ i8 L& W
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine( B( C k+ F0 X# ?2 a
make && make install! c; [2 m8 e, \' R
四.添加规则
2 d% D0 i* F. r" r4 H! `$ l1 Q5 Z* E6 t7 |' L/ Y
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
4 O1 S2 g, E5 I- z4 \' L7 Q" Y+ D6 A4 @9 L, _1 G
1.下载OWASP规则:
8 i2 O& |8 @1 u/ N8 J: ~0 E) s! P& R) A0 l
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
# o# A( T" h9 L+ T8 H I( Q8 k
1 i* y5 P( d) g0 }mv owasp-modsecurity-crs /opt/tengine/conf/" q7 g! e' Z" c- h* _; n( O6 R
6 d& ]% n1 j( q! B7 w
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf. j9 V, ?4 `# u9 h0 s) \ N
2.启用OWASP规则:
. l3 Z6 h" k/ \3 g) i% x
' w3 F2 D8 d% e7 l* {9 {/ ~7 R复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。. q5 u0 P* b4 J' A8 b4 [
! ^8 q. R& U* T& t" l E
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
/ B+ ]$ r' l0 t7 L) l9 D2 [) U) j& e9 m$ p t) P+ Z% o
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。, Z: X1 m# A% _# {
) W$ g- G. |1 ~+ ^$ SInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
+ |- r" d9 e0 C& V% j8 R1 ?Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf! a; U A2 p" o8 G
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf7 A( L0 t1 M f0 {
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf1 f: n9 ~* {1 K' ?1 s4 t
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
0 ~) Z2 |9 v" o. T( [/ p1 e. ]) e% XInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
2 q! s; D' Q: J! qInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf- e" G. S; L2 R% d0 X9 A& V
五.配置nginx1 ^/ H1 }/ C6 z+ {' N7 k. n
* X" {0 L' H; R7 R8 n) i0 @+ f在需要启用modsecurity的主机的location下面加入下面两行即可:$ }, O( b# j6 Q. R# g- o$ ]
" B1 s D L* I# u' QModSecurityEnabled on;
1 Q0 D5 h' e) E& [+ i9 W8 yModSecurityConfig modsecurity.conf;
9 B3 b7 B' u& O" @下面是两个示例配置,php虚拟主机:) d( F0 d# O6 O2 K' I9 ]
$ A* x1 c, C! w
server {
6 G4 v+ U W9 e listen 80;8 C$ j( m9 I$ O$ r+ ?/ c
server_name 52os.net www.52os.net;
4 ?$ d7 r5 X6 x. D; q' E' ?' M( h 7 ^0 G; ?. [( @" C7 r# b; f
location ~ \.php$ {6 q0 p: k( s( j4 R% \; B/ x8 `
ModSecurityEnabled on; 7 J: r9 G2 {, u" m+ \
ModSecurityConfig modsecurity.conf;
4 l7 x- l- V4 B9 } x, w
/ E F( }1 _; h! S; ~! o root /web/wordpress;
3 h; |! n& h( L9 l8 g9 O index index.php index.html index.htm;: H8 [# b. y# [! _' g8 T9 n% o
; g; j; O- n0 M$ S, L1 w+ N fastcgi_pass 127.0.0.1:9000;( Y3 c" J! g+ ]- e& ?; \( a
fastcgi_index index.php;
4 E5 ~ t5 O; t6 [% _* P& O: d fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;4 ^# a# u! J9 r3 m/ P2 T' b
include fastcgi_params;" F/ F8 i& D3 l
}6 q% ]3 Z5 ?- z! M
}
7 G( b/ Y2 ~4 ~3 N; {+ iupstream负载均衡:# e+ y8 k; f# i# \$ s7 E# y* G
* d1 W7 {$ T4 i: J) ~6 Eupstream 52os.net {9 i8 S2 d5 B2 w( T% F$ K3 @
server 192.168.1.100:8080;$ X4 [4 j2 V$ e9 L: A
server 192.168.1.101:8080 backup;
( W" e7 Q+ s3 F* L! z}
! x0 n4 }) ^9 o- l6 f8 a; y: m" D% J+ H+ F S5 `( V* K
server {, z1 ?+ ^+ i/ t0 T6 t. H! G8 S
listen 80;& [' [4 y$ S) @1 W6 G
server_name 52os.net www.52os.net;5 y5 R, S# O8 W9 { M- W* ^
d1 M9 v. r* {# M" N4 T: F) B
location / {& }* p$ S0 U0 G, J- w
ModSecurityEnabled on;
) Z! j, Y: I3 l: o4 Z ModSecurityConfig modsecurity.conf; - \! D s4 p) Z. R5 ~4 V P
. X. D5 }6 ^3 G" }( T6 p$ P+ Z proxy_pass http://online;
z3 J3 T2 N4 i8 {" } proxy_redirect off;
$ A6 [% x& b0 Q* M. M8 G proxy_set_header Host $host;3 f& }6 |8 k r" M- A
proxy_set_header X-Real-IP $remote_addr;
& s# H$ q) S/ C. J2 G& Y proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;+ c, Y. v1 ?. B) M; t0 G8 {
}
- i0 f# g. c9 x0 V4 c/ l$ W}
0 t% M' T1 ^7 i六.测试* G& r% k: W: t
2 Z- l O- L$ j# D% t! H" ^我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
: t' H$ I+ i( I! P0 L, }) I. m* L3 J, N2 Z
<?php/ ]8 F0 W( M; F- l) q
phpinfo(); ' K' z5 G& E( _. y
?>
' k" j. p; R7 P+ S9 U在浏览器中访问:) Z6 v0 _. s5 I- v6 |
1 ?+ y. _7 Y, r) `9 ^* I2 J- O! X8 nhttp://www.52os.net/phpinfo.php?id=1 正常显示。% r4 {, F" o) X5 }
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
- i2 m. F# V( Dhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。% @9 w5 n! V/ ? I! q R
说明sql注入和xss已经被过滤了1 a0 [" h* {- G7 _
1 U; {8 b5 X5 F* ?7 B七、安装过程中排错
9 R; E( p( F2 C. ]( C5 n' s8 `! s
1.缺少APXS会报错 @0 X3 V8 ~7 x X
6 h; H2 }0 d+ |2 p( P
configure: looking for Apache module support via DSO through APXS& R9 p+ [, N2 T1 Z
configure: error: couldn't find APXS
2 l) f) G9 f+ v: Lapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
/ C' w6 n: c" j! O3 b! ?, h解决方法:/ E0 I9 S0 `( Z; k6 _: h
5 s/ {" D) l8 s; ~& [2 E
yum install httpd-devel5 q& I4 Q7 m [; h9 [( p
2.没有pcre
/ v. I3 P {, T
4 ?' ?! e+ c; i2 Yconfigure: *** pcre library not found.
) n q8 @. O3 J% W! Kconfigure: error: pcre library is required |9 {* b% S9 w" z4 x% f
解决方法:
9 N- V1 f8 T2 K- z% E! o8 E# q
0 w: r- F! ]4 @8 z8 Hyum install pcre pcre-devel; j" y6 y) L) a- }3 ~" w
3.没有libxml2% ?: H% j) o9 Q7 f7 [" n
8 M' }, D6 b; c5 d9 d
5 w# p; x; i, `2 b
configure: *** xml library not found.
- J* U, c7 F6 q: [2 @; j' Gconfigure: error: libxml2 is required
+ C9 w, `" [4 @9 O5 U' g3 \# T, g解决方法:
, u+ @( s1 R& [
1 G$ i4 q8 {( c, y" Gyum install libxml2 libxml2-devel
; J D/ l3 C/ g: q0 \. u8 m* b0 `$ R4.执行 /opt/tengine/sbin/nginx -m 时有警告
* d) R: ~3 Z9 c3 t8 O4 {
) I% g; s$ |5 G; xTengine version: Tengine/2.1.0 (nginx/1.6.2)
6 j) o# c b+ p$ O5 _% q2 rnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
! Y- a9 h; ^ \( N% A- `原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
' ~- A. Y z o$ h+ ?4 V2 E8 ^
, l* ?6 Q1 R8 h4 P8 C4 y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
! a. k6 F: `" z! \+ ?2 q7 n4 E; G+ P5 k2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"6 r9 I1 r! h1 W0 X# C7 q
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!% w& ^+ j/ `+ d' C: X/ l0 E0 i& j
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
9 ?) v) o: Z' k: c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
0 A$ l# @! H Y/ m+ U9 R4 n$ c2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.3 z' B/ R O0 M( M
解决方法,移除低版本的APR (1.3.9)
5 W' G5 T5 I2 ?3 K* ~8 O" z" y+ k4 p8 J
yum remove apr: O' K: j$ L, U5 x6 M
5.Error.log中有: Audit log: Failed to lock global mutex% w+ Q' q; y/ F: z- p% g
! v& a( S" w9 R: U1 g, a- [6 p2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock / S# k, q. Y2 Y, x# z/ J) Y1 o# ~7 e
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
$ ?# {5 l- v8 [7 k5 I$ `* }' y; L解决方法:( Z5 q7 F) t9 @' ^8 u. f
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
$ B& l8 p% T* L" Q- S4 K/ ?" R$ Y. ?9 X
SecAuditLogDirMode 07775 m5 C/ o7 d; Y# b
SecAuditLogFileMode 0550
+ J) G5 Q1 H' a$ `* b h3 VSecAuditLogStorageDir /var/log/modsecurity
+ i- V8 r6 `! Z$ H: X- BSecAuditLogType Concurrent
+ |2 H- N" K4 Z- t参考文章:" g' @% r: a( A" B9 e6 h# e6 Z( U
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX4 @2 e# ~% |, q. k, A5 C
http://drops.wooyun.org/tips/2614 |
|