找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12221|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
; R$ Q7 B  j! P1 ?
' Y# R; Q4 Q& b" V. C一.准备工作  a; o4 v3 k  z" {6 ]

" @8 ~  ]. f# }& l% E- ^$ ^系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0( a& ]1 W" n" b) H

) ]) C4 q, I: W4 J$ a( @tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz0 W6 a# E; q8 Z6 I

) g8 u0 v! q; O0 ]% Q) N/ N! e: J/ omodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
, n& A# k$ Y' P# j' ?, D# ~. Y0 X7 d) @. \9 n
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
0 t( i6 U- J# {: l* U0 f# ^5 O0 C7 t0 ^& T* ^- t/ @
依赖关系:
8 r5 o5 F8 \- C2 c# f" l: j3 ztengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:# ?- Y, z( }; h1 D9 ~2 j# I
) E+ m- g9 R/ v4 Z
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
5 l" H; e+ l, K* E8 imodsecurty依赖的包:pcre httpd-devel libxml2 apr( U% r- C# z; s4 Z# w
: y' C# L3 v# E. Y3 p9 r
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
# X. v8 U& ?8 D& F二.启用standalone模块并编译
* o8 z% K) T- `. ^% C+ d6 L  N( L# e. P3 b
下载modsecurity for nginx 解压,进入解压后目录执行:
2 [* Y- ~& a6 L
* {" A  ^5 W$ R* _% Q./autogen.sh
7 X) w* k$ f! G9 G. x: O- e./configure --enable-standalone-module --disable-mlogc8 z& S0 I2 a6 \& Y1 \( K9 Q8 K" Q  @
make 6 h$ h7 R1 M" S; N5 V
三.nginx添加modsecurity模块& \9 ^  n2 |3 _7 f! a
+ c2 c7 n% C: s( ^0 N! S0 R
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:. ~+ d1 k8 s0 m/ r1 \" L: O

# Z6 O7 _5 Z' d& p8 `$ _. V. C./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
) J  m: {6 w  N1 a' l7 Imake && make install
" p6 ?4 f/ k, P* A1 J四.添加规则
! t  `  d4 [' C1 X* n9 U) _# C6 @- S/ Q- H! @" j- Q! ^2 e
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
2 E% m7 j( D/ P# w4 ]! `. r( B/ A4 f: E# P
1.下载OWASP规则:# ]1 H: \- |. g% i1 n) }8 E! {

0 W1 ~" m; R3 c$ v( ogit clone https://github.com/SpiderLabs/owasp-modsecurity-crs  n+ G6 y6 a& ~

  h5 H& J' ^% t, {5 k1 c+ h% q4 Lmv owasp-modsecurity-crs /opt/tengine/conf/
4 m) X; m- C# H- v4 d1 s+ {+ w- D" V) s! M
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf6 f4 U0 p; W' _, u; {
2.启用OWASP规则:& @4 l6 Z- `$ ?& K

' V4 D/ `; Y( I复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
* B. C0 l7 K& I8 K0 t, v" E9 ~
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
9 _9 {, n7 _9 v4 A% d5 r0 B0 l. U, c1 g7 k: D# U& a: a
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
) r6 ^% u; C6 E4 d' R9 L: y; h: P& v9 r3 T2 m' v7 x' v
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf/ n1 \; J8 g0 |: u3 n! H8 T
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf" e2 g- V+ e* F9 l: f9 g7 v: ]. p
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
& z9 U. \  O# _- LInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
5 m6 z7 O$ X# s; b2 v9 FInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf. N3 G6 E4 p: g
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf8 x* y: A4 ?# f
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
; D  @) Y( `/ t% w$ Q五.配置nginx
8 k  S9 }" ^4 y9 d% K) E2 |/ F9 s0 z3 N+ ~) o' a
在需要启用modsecurity的主机的location下面加入下面两行即可:
5 Y0 J- b6 Y; U5 {1 v0 L, g& e5 q& ~5 ?# t2 `2 R5 r
ModSecurityEnabled on;  $ a4 ]# g0 x7 B$ M# t0 ?8 v  b
ModSecurityConfig modsecurity.conf;
. C2 x) n- [2 i- k* C下面是两个示例配置,php虚拟主机:8 X6 A: ?- z" k/ @: c
" ?9 ?& S4 u$ o: m1 n6 C2 B* V, Z8 a
server {
# G0 s+ @6 C) E0 U$ s& u3 D, F, u8 i3 q      listen      80;. o# I, k6 A  R& ^
      server_name 52os.net www.52os.net;
7 p6 o2 ]$ c* a2 f; B     - a9 c- J0 R1 r/ t: r0 {
      location ~ \.php$ {
6 B0 p4 X% {- @- [9 H( @4 g1 N      ModSecurityEnabled on;  
7 x6 _5 E8 t( m5 f* v      ModSecurityConfig modsecurity.conf;
- L- Z* Q' |/ X8 s, {
: W. b+ h9 Q' T0 [      root /web/wordpress;
2 c5 K9 `6 p9 K" h! N      index index.php index.html index.htm;
0 b8 S8 N8 m/ z) C* ~( T  
% ?$ z: R8 I1 Y2 U      fastcgi_pass   127.0.0.1:9000;# X1 l7 Q! y2 g& e9 y
      fastcgi_index  index.php;4 m/ g# }9 q1 H: y5 t) r1 ~
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
2 ]. G+ ]( T6 X. m& b6 g; q$ x: X      include        fastcgi_params;
9 `" j+ @! k. g      }7 A1 \( y" s, x
  }
1 M! `5 A" p+ dupstream负载均衡:8 Q' i- }( b, M: P
( {0 C1 F+ I9 z; z
upstream 52os.net {( S9 M) ?) p2 `& ?! j
    server 192.168.1.100:8080;3 i0 I& P( A0 e! p) h/ W$ I
    server 192.168.1.101:8080 backup;, [( R, I9 u* m" ~" P; k
}0 T, x/ a# [  z/ l: J5 x
: l, d, M! G7 m6 R; P
server {. g$ S4 n/ m3 m7 |5 x1 r7 X! p3 \
listen 80;# i. B9 W$ O5 d- |! o
server_name 52os.net www.52os.net;
2 c1 J8 Y% s+ l+ [" Q" s
2 r- p& Z3 T; xlocation / {( H% ~3 v% j, P
    ModSecurityEnabled on;  ! b# q/ o1 X+ k' \. l, a
    ModSecurityConfig modsecurity.conf;  & v- g2 |" U. {! U& d$ m+ a% h9 A

$ D4 ?. G8 G3 b  ]% @        proxy_pass http://online;0 o1 d$ o: A! h" B# e3 D% ^
        proxy_redirect         off;4 q! p8 E0 u/ q- D2 q/ r
        proxy_set_header Host $host;
" Q/ t6 \# Q, n, j8 n. \; X        proxy_set_header X-Real-IP $remote_addr;$ f' y, k$ O% Z5 ?! y% O( p, Y& \
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
: d( t5 f# @% q    }; r7 P: B. r$ p0 f( J
}6 e3 G' i+ _( K* }7 I0 c/ Q( w
六.测试
" k* Q$ Y5 V3 |; ^1 \+ x+ q2 a4 ^0 ?1 f- |9 t5 s
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:3 s% u8 W. \/ Y. G* A
5 K( }1 M1 g3 O3 R- H! d: Z
<?php* o. u" B, P  c- f- Y5 R& Y0 ^
    phpinfo();    # n' A- d  G2 @0 o; S6 ]
?>
% @9 e& b7 Z2 a! A在浏览器中访问:
$ z4 g9 S; g( f" `+ ^
, Z/ o5 ^6 p( e# E5 ~, Shttp://www.52os.net/phpinfo.php?id=1 正常显示。
& M+ O* @3 v4 l" [http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
" f# k+ _' x2 t0 ^+ v6 d4 Ghttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。( I4 Q3 D# h, `6 i  s$ |1 X
说明sql注入和xss已经被过滤了2 a& E4 d* N: L' i9 }# ]- f

9 `* b# \+ R& Z, V: r( `. I七、安装过程中排错8 L) D8 j, D  a$ S& T
& r" Y1 v* g6 k% n8 g0 O9 x
1.缺少APXS会报错
# w+ n' Z# g7 k* L7 f3 X4 x) a; x+ x8 k* S; x2 N2 V; k/ e
configure: looking for Apache module support via DSO through APXS
9 x( l, Y+ g( K: p: V) Zconfigure: error: couldn't find APXS
1 l/ \2 w) I  B0 z& Japxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。! j4 }. `% p3 n  k5 i- ^; f/ Q
解决方法:
! A2 \4 h( t1 G" l( E
* f; N4 B$ Q1 tyum install httpd-devel& w& r5 H" u' x6 U
2.没有pcre' ^1 d2 h1 [2 N2 g

1 ]+ F& h" k( c  n. [' Y3 Jconfigure: *** pcre library not found.3 Z5 h0 E+ m0 F) O2 L+ z, S+ z: z- g
configure: error: pcre library is required
3 @; s7 u( ^% P6 @# F- a5 u1 {解决方法:
: Q) F9 b" U' J5 f8 P# `3 T0 j9 ~$ J8 J
8 v+ o9 ]( I% d& xyum install pcre pcre-devel
" [# `* k6 M  B+ o/ h- V+ }3.没有libxml27 Y8 R# O1 b$ b+ \4 `
- t; ]& i  |/ w3 s$ l6 n( _

$ s! m% K8 T: a( z1 g! Pconfigure: *** xml library not found.% w: L, W2 p8 W0 ~" `" Q
configure: error: libxml2 is required
7 y7 i9 @. p2 o5 V解决方法:
7 _6 }2 A# [4 a! I" j7 O  R, B) p4 n
yum install  libxml2 libxml2-devel' C7 K7 O. D% S/ @/ o2 P
4.执行 /opt/tengine/sbin/nginx -m 时有警告
7 @9 i  S0 n8 }# Z; S
3 W' s% F: [! a+ i; k+ ETengine version: Tengine/2.1.0 (nginx/1.6.2)
0 [& B# Z4 ?8 p( f1 Nnginx: [warn] ModSecurity: Loaded APR do not match with compiled!% I' y% N8 @5 F  \
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log3 r/ Z) `1 `* z9 V

' a- S3 i# B' x2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
% ^- P+ _8 i* t% H8 K1 X9 E% H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"% W8 H6 g9 K0 Z2 T
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
9 U) i% w' |2 T5 N! A, q/ D2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"/ B& [$ v! u5 N% j) p8 g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
  }! x7 p% \9 W: k7 j+ m, |2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.' m/ X' z$ b7 [# G3 S
解决方法,移除低版本的APR (1.3.9)
& L! w" V- Q2 r7 y! z+ t
6 H& U3 h, a& P7 ?$ l0 @& @yum remove apr
/ p6 M5 ^+ r) L# w  g5.Error.log中有: Audit log: Failed to lock global mutex4 E0 ~7 Z$ z1 N( e/ I

) p5 [) I6 P' b+ R' p6 Q# \2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     9 y3 A. c+ }8 o8 \3 W; H+ Q
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
; U6 m) w; b6 u" `解决方法:
7 D7 A, m  {& l. _# g5 j编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
5 W* [% h  t0 n6 X* }0 d# `6 g' w9 r6 r1 s6 X. u
SecAuditLogDirMode 0777. s+ q! q% E: s' m. ]
SecAuditLogFileMode 0550
" V* T7 }6 _$ U' H' H0 {, l, }$ BSecAuditLogStorageDir /var/log/modsecurity
; e/ p1 Y  t; |* U6 m  VSecAuditLogType Concurrent
" [* V3 g' @# W& x6 ?& P参考文章:  S2 C( i2 |! P3 G7 N
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
( M! i* @# Z" L, X" @http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|第一站论坛 ( 蜀ICP备06004864号-6 )

GMT+8, 2026-8-17 05:15 , Processed in 0.072684 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表