找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13265|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。# q+ b- q9 u" n& V# M4 T) t

! t$ _0 R% f9 @8 g, c9 q: _3 t一.准备工作% o* Q- I" N1 S7 X4 V; l

* r  y8 p6 R! c2 Q3 P系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
, f- s+ {7 ^: c3 i* n& T" X
$ ~7 }  Z* @: Y& d) P" ktengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
; Z$ `+ N) M9 k- m1 A$ b9 f7 d1 p5 S$ O$ z
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz% i* L- m- U# {& b5 T! q
' s& K" Z4 B6 b! z. u  C
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
! M+ f9 b$ m& f3 t6 ?1 R
8 |, l# ]9 U( y; y' X依赖关系:4 B: ]0 l0 ^- [
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
3 J: x! p" Q6 Y+ T3 i* n2 k  G
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
- n2 _) a6 K5 lmodsecurty依赖的包:pcre httpd-devel libxml2 apr8 T, b( i9 {8 [3 S
" K; r% g0 ?4 n: o& q. Y' L
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
8 G3 Q$ Z6 x6 W1 N' m& S' ]( R二.启用standalone模块并编译
7 K/ K' ?" Y3 u; e5 R2 W" T, L- V5 Z* T
下载modsecurity for nginx 解压,进入解压后目录执行:
% c' l5 \% w; i* C- b* e+ U; q9 h, q
6 z! t- ~7 _& R7 r' S: ]- O./autogen.sh
1 S! {2 O  k9 c7 b8 Q  d./configure --enable-standalone-module --disable-mlogc
0 F) P1 ~) Z0 C9 @; j5 Kmake 1 Y9 Y/ E' T; v% C
三.nginx添加modsecurity模块
: p. f* Q4 O2 f, _# ]/ T7 l2 N' U# u# ?( ^
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
6 S5 Q# ]- Y; `$ _8 g
+ g" t: J6 e- O8 S# u3 }# K7 k( {1 V7 g./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
- ]5 `) _5 ~5 }# pmake && make install
' y/ v0 ~4 w  P# y. p. Z4 V1 X. A四.添加规则( ^$ a- i4 l! }$ e5 j0 i

4 |1 r) x# y& y: @- T/ D* Gmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。5 B; E8 c, Z& G! o
: N& a" r) v. V! N7 u! {/ v
1.下载OWASP规则:9 k- ?6 v$ f3 d* P4 }

2 S1 z+ {8 p! b8 ygit clone https://github.com/SpiderLabs/owasp-modsecurity-crs! a! r& ^# {  r, Q8 j" o( W

4 y6 U" v: T4 A5 qmv owasp-modsecurity-crs /opt/tengine/conf/
2 t! F, A0 C, k/ i+ S1 C; [$ [- Q/ s# N+ C3 Y
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
2 O7 n6 s3 l, |2.启用OWASP规则:1 z  J, R3 i- B6 a. G0 `

5 r$ w6 X) [$ O复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。' ^6 S" ?# N& j7 f, x! @

+ h9 ?7 }8 _- L  u编辑modsecurity.conf 文件,将SecRuleEngine设置为 on& y2 V, |6 c* [) i
- k4 w6 T$ G4 i4 Z
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
: V6 T+ O, X* ]7 v2 W
, I, a8 p# n5 `Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf4 l: [9 G" X4 T
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
# n. Q8 B3 P. o+ P, WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf9 ?( J' Q; i: }3 B
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
5 ?9 I$ U" V, [9 H- H; Q, ZInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf3 C2 r& K, c' Z% l( A
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf9 E# V- O& I: h3 `1 |
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf6 y0 T1 p- J8 X. m- L
五.配置nginx- S' d/ F0 }$ M& n+ W$ i4 b

& v9 J, H9 {0 U. ?+ [  ^在需要启用modsecurity的主机的location下面加入下面两行即可:) u8 E# \, x' ^. E' f, Q( @- b3 m9 r
& I7 v- [! z0 `
ModSecurityEnabled on;  
) o6 m0 i" \7 ]9 o+ f& i6 WModSecurityConfig modsecurity.conf;
6 X0 D" S8 U1 T$ o2 {  Y2 F下面是两个示例配置,php虚拟主机:4 p: R6 `  b5 N5 c1 H' V: g
9 R0 K/ r% I: K  X
server {
1 z& ]+ I: I6 w$ w# b/ l" I      listen      80;
0 F7 o8 m& s' X      server_name 52os.net www.52os.net;
+ |/ P6 p' n2 `* w( ~! N+ |& ?' c     
, ?" a9 Z6 W! v* b- W$ j7 B      location ~ \.php$ {
) E; a9 S* c( }) v0 y  Y      ModSecurityEnabled on;  
" q9 a" m3 z: v; ^      ModSecurityConfig modsecurity.conf;
4 Y4 {( P9 y# a" w; R- t" h- h& n: {# b3 ^7 o) W
      root /web/wordpress;) I0 t" l4 v  G; Z% J1 @
      index index.php index.html index.htm;$ S: y8 \5 c6 V
  
' [! Y- `$ a9 V+ `" L: \      fastcgi_pass   127.0.0.1:9000;& V& t+ h& \9 C2 y8 H
      fastcgi_index  index.php;0 q' V5 W6 i1 m$ e
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;/ h* ^- K2 c9 X( b$ x0 x
      include        fastcgi_params;
7 ^8 v/ [; S6 t1 [1 W$ z% S      }0 c( J: K# J& s8 l5 m- {$ k; x( }4 M
  }
% z$ }( K& b) Qupstream负载均衡:8 V: v2 S' {) g3 B6 D# A% n

* N. B% @, K# e7 R) zupstream 52os.net {
- J7 c" L5 t" m  o/ L1 \) \2 g    server 192.168.1.100:8080;  B# l& M8 N3 ]( J" B
    server 192.168.1.101:8080 backup;9 t/ o, D6 ?3 F8 k! g1 {- c) b
}
: K9 T- v: Y1 N
0 W4 p- ^) W2 m' xserver {( A+ C$ `( O8 y2 s7 O
listen 80;
+ R+ D9 b. ]' d# t' l% f6 `server_name 52os.net www.52os.net;
& {# \3 I' X6 Q5 q9 `+ A
/ |; t# K8 K7 Clocation / {2 ^' Q+ t  X6 t! B' z% n$ |7 f: g
    ModSecurityEnabled on;  # p* E# V0 L. X
    ModSecurityConfig modsecurity.conf;  
1 K3 V. o. L1 S1 S; A
+ O; l2 _$ J2 O7 n        proxy_pass http://online;' y6 F" `* g+ y. h6 e+ A" x
        proxy_redirect         off;2 u6 f5 h( w" l4 Y5 f! S  h0 _5 p
        proxy_set_header Host $host;
0 m3 O7 T& v( a; C8 a$ E+ w8 A" H9 ~5 w        proxy_set_header X-Real-IP $remote_addr;
) `) C7 `" o  f1 g. ~* @) w+ H* r        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
6 P6 U1 p8 h6 Q0 Y$ _) B+ D+ Q    }
! w8 M7 u* r- j/ V0 Q  p}0 T- e3 h  R% u2 `0 m$ t, e2 u* [
六.测试
  Z+ y/ B7 _& a2 \1 v3 w1 L; m4 |* a  a; t) F4 h! F' P& Y
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
3 _. c" ?5 p' _2 E' O! [( @  b) h% O! _' T
<?php7 i, g; _$ o! z* U! r
    phpinfo();    4 l% K; U1 O0 _7 N" I
?>
$ d4 x! B# p5 A8 n; H在浏览器中访问:$ H2 d; ]5 d0 s* O4 Q1 D- z
1 y/ m% K" q8 [* F- ^/ x( U
http://www.52os.net/phpinfo.php?id=1 正常显示。
; U, S/ o0 F% X: p+ e( G& ehttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
- }; o! M; v2 Lhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
( g# O1 {* E, _* t9 @4 j- p# P说明sql注入和xss已经被过滤了
8 K, k+ A$ d$ H- u# [1 O& ~! j7 P( j3 J
七、安装过程中排错
+ x; c$ K" B# R( q/ P! A$ Y* M: P2 a& V5 O
1.缺少APXS会报错/ `# a6 r1 ?: B% J% l, C
& G1 d- g+ ]& K2 v  P  ?/ `
configure: looking for Apache module support via DSO through APXS
: h/ S6 n: K" D, Q" M- b- Aconfigure: error: couldn't find APXS. G, Q4 p/ u2 x% }8 L2 |
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
- @* }9 B" Z6 q, {解决方法:
0 s/ ^+ i/ R3 t6 p: v
  Y8 g2 }+ d- h3 Hyum install httpd-devel: W# y! o0 ~$ E. g( _
2.没有pcre
1 @' W7 Q/ w& t
7 h! v9 S9 P9 h/ U* H, v$ m, F1 gconfigure: *** pcre library not found.
& V' |+ A5 o. Z* F0 Kconfigure: error: pcre library is required, \" G1 j$ |, u5 i" G4 e
解决方法:
" C$ l/ M0 a8 m$ R; S
% A$ R! c0 F* m8 vyum install pcre pcre-devel1 Z1 f& c& U* }7 h7 y
3.没有libxml21 p) r* ]; L9 c$ q1 d" e

, [2 W9 V/ i1 E! U; }! m4 O
' j4 t* }& G$ X' a% @( Wconfigure: *** xml library not found.
* O' I, ^" X$ Dconfigure: error: libxml2 is required) }' k* q0 J! D) A
解决方法:" M' X# P" @5 ^7 P
) f( h/ f6 p* b/ ~
yum install  libxml2 libxml2-devel* \" p: U+ z9 n! \5 P
4.执行 /opt/tengine/sbin/nginx -m 时有警告) _8 E% P7 A) r' A9 v! F+ `; z
# w) f/ z! j4 _% J1 x
Tengine version: Tengine/2.1.0 (nginx/1.6.2)( `. |  S5 T" E2 v0 w
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
( Q  A) h* O5 t原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log$ a. B) H( A! x' k8 Q
6 e% Q5 Y# j2 |6 c: Q. M
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
7 K# w8 e$ s5 i% f% W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
5 b) e0 S7 {1 G2 l6 t" L0 |7 u2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!2 ^0 A9 U) n4 O- H. F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05", u; ?. }# P7 a2 B
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
9 Q4 R) b! F9 h2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
' Z: W( c0 \( i, j- k解决方法,移除低版本的APR (1.3.9)
1 |/ T, |$ J% r; ^
" ~. S1 s) l3 s% W: f' @yum remove apr
3 `5 p# O- U& ~2 X5.Error.log中有: Audit log: Failed to lock global mutex
4 m+ Q4 ]( @% P2 E. r# o8 [  ]8 y7 v8 q: e1 c) k# s0 }% X0 \# |* T+ J
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
8 n/ E. M" m) r/ |& s' bglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
9 @; m" J5 [, x9 v( h解决方法:
* d( S8 l8 p% s5 ]8 p9 D$ U编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
9 @$ y( s: v. O. L- O& h# J2 b/ i2 H+ ~
SecAuditLogDirMode 0777
  I6 t) O& z- C$ QSecAuditLogFileMode 0550
$ U% n1 n$ M9 f8 }! r( C# mSecAuditLogStorageDir /var/log/modsecurity! \+ a- f# Q- L+ K
SecAuditLogType Concurrent, Z! y/ C) L, x8 S% j8 w$ o
参考文章:
: D- l" t* o4 g- k) p% k+ phttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
7 y& T7 l- {, p1 Ahttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-30 15:59 , Processed in 0.113798 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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