找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11479|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。6 v( t9 i$ w; L. ~$ L

6 r! p- L+ f6 U+ G. W一.准备工作
) i* R! l# X/ y1 j7 d' V8 v9 u8 E! a' X6 \8 G
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.05 P$ a  I! ~- y( A' T3 Y
& ^. a% c# h2 X7 E$ D
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
" k, t5 p, X) a
# m& x3 ~  _0 \. E; Kmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz- t9 F. h; S) r( {5 B" |1 l7 N

! k( s! b- @7 E2 Y) u) aOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs5 j1 I% h/ W: R% v: V) {* B
: b! `: a% V8 v: s9 o* N9 d
依赖关系:
4 f: f/ ?% e$ ltengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:3 k  I8 C4 _# Q

9 U+ ]: M7 ~& s5 d0 f- v* [! M; Lyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel8 d0 J8 q6 v9 u3 Q  V8 x
modsecurty依赖的包:pcre httpd-devel libxml2 apr
: y1 X5 Y6 A2 t* n4 f# Z8 K
: ^% w, |# e+ l0 }( ryum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel( b* U5 f8 f1 |2 Z3 ~& R
二.启用standalone模块并编译( @! U4 x2 _5 n! d) G
. y) L0 |6 N1 [6 o5 ]/ E+ S
下载modsecurity for nginx 解压,进入解压后目录执行:6 ?8 F1 C% `; j- w0 H+ M

6 @: P& Z( D% B4 w% |( R& G./autogen.sh( x2 W# A2 O; t) C5 e
./configure --enable-standalone-module --disable-mlogc
9 O( ?) \! @4 ^. ?7 t) Ymake ( c3 d# b5 o) u. m) K9 l
三.nginx添加modsecurity模块
; }/ ]) n9 y9 v$ k) o3 Q' ]6 Y4 ^5 `9 a
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:" X& p! J! w5 I* r

6 ^9 J7 W$ K; u% `' E./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
. n* q$ B, C$ [make && make install
6 p+ i; @% y( Z四.添加规则
: D  p) p7 n5 _( c- h: _9 ~+ F
- l1 q, b% Z3 B. h3 l" fmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
1 x- Y4 S$ C/ a3 y+ ^0 `6 q* y/ Z, a7 z/ C$ }
1.下载OWASP规则:4 R9 c& p. E4 W" I/ @

) W4 x! O% C' \0 H& ]4 r; Lgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
$ S. J9 b  p- M2 `2 O  b8 E3 [( C4 T1 h" @
mv owasp-modsecurity-crs /opt/tengine/conf/3 q7 E! e1 z9 X9 E! a8 c4 T& |9 b
' k9 v3 t6 Y! L( m( s9 g8 [! P
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
+ V! H: z7 a/ a" l2.启用OWASP规则:8 `" g9 k2 P6 w# `- G8 b0 b
! n. |) {2 i6 }2 l1 {* p
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; z5 I: M. X' ~: h7 P( m
+ `7 T  H. Q, @6 R" X6 r5 w
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
& J* w# {' C4 l5 c
( ?/ L4 P4 Y! ]3 z5 P6 \owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。+ f( l! D( {% G- |$ `3 c

( d; G5 z& G6 wInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf( Q; P" b" e( W6 c$ a
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
9 L1 S$ F4 s' G! h$ ^/ n" [# q, [Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf# J2 d3 f. v$ N4 w  R+ v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
# c( }: N9 \5 P: L! zInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf" g' N! Z0 ^6 ]+ J9 w
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf. v0 y' R  A$ B
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf% [7 b5 c0 e' ]- Y
五.配置nginx. e% w% \2 C1 b0 K$ R% ?7 ]

+ s# ?8 r) s/ z, b! x- r* `' u- H在需要启用modsecurity的主机的location下面加入下面两行即可:
; q+ p  \+ E+ F" c% N8 e$ J- Y( R! b% o& b1 g( G' Y: j4 x0 n2 t
ModSecurityEnabled on;  
- ~0 @) e+ e" x( `& K+ a3 g, @9 j1 ]ModSecurityConfig modsecurity.conf;$ ]8 r/ W) T/ ~1 N, I% f$ p
下面是两个示例配置,php虚拟主机:
0 q& D& o& P8 V' p8 {: @9 w- m
1 p* L. D, J% d) f& U/ `+ Tserver {7 }2 Y6 s% G* _1 e1 u8 ]
      listen      80;
  P" y6 k+ t, ^, }9 v      server_name 52os.net www.52os.net;
/ o, a5 @2 r% c" s* s  q1 K) ?% {     
! s# Y" O7 ?2 e; ]1 t- W      location ~ \.php$ {
' N! B$ m- m: [2 U" l3 r+ _# }      ModSecurityEnabled on;  + C# _- q3 \* t0 V  A" ^8 ?) _
      ModSecurityConfig modsecurity.conf;
; W; H& U6 @- x- S# y7 e8 m4 l
6 f3 a" I! f0 \% Q9 P4 e: k  C      root /web/wordpress;
  m4 N0 t! [% r! \      index index.php index.html index.htm;& ^( o- w( ?% S& B9 n7 Q
  7 g6 x4 t, L9 T2 }, |4 i8 @
      fastcgi_pass   127.0.0.1:9000;
2 l' c* Q' n! p      fastcgi_index  index.php;% P. x: Y% N' G3 ^$ i* w! F7 s
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;" n9 X6 K( p, U4 i4 V/ Z) {( |
      include        fastcgi_params;, V0 r, @3 g# D
      }" {2 [/ |8 e1 c: X& @' d
  }5 Z$ ^  @2 ?$ P; }! U
upstream负载均衡:
9 c  ~) ~6 C4 H/ v8 a  V( p% z( L6 _7 B' J2 J" _- y
upstream 52os.net {
. j3 f# O" l. l4 Y    server 192.168.1.100:8080;# o. Q3 F. a) M7 y0 u) v- j2 S
    server 192.168.1.101:8080 backup;  j$ [& M2 H- r) U* V
}
$ P6 {9 s; s7 i. [7 X) \9 t5 h) ~, a
server {& ]; c( g" c% @0 Q- k: z
listen 80;3 o& b1 z4 v" [4 [% \$ H
server_name 52os.net www.52os.net;
0 U; ^7 a- v! Y- o( f1 {: m4 ~8 y( W* r- J7 Y
location / {
8 ~- `: c. R6 z; M    ModSecurityEnabled on;  
/ R$ n! k9 k5 H. F    ModSecurityConfig modsecurity.conf;  " U9 u) Q' U$ ]4 t
" n( m% V1 C) }. z, B) E
        proxy_pass http://online;
' {# U5 R2 k. ?  f# W2 W        proxy_redirect         off;4 ]3 h% ^& ^5 j+ k5 W" m
        proxy_set_header Host $host;
9 L0 }" f; h( t7 S3 J3 |) j; K        proxy_set_header X-Real-IP $remote_addr;
! Q% s3 I7 G, J        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
* r* {  E2 `; R% O9 o' k    }
* n1 Y5 ~$ f6 O. l$ U+ ~; Y9 |' n9 Q}
8 T( d# `& k* o7 b4 i7 F7 Z1 V/ u! G六.测试
# J3 X" [5 L/ M( H$ I4 e% V1 Y/ n' A( g9 |+ W
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:& b7 {6 z. T$ E0 _

9 a' A8 {7 h# C, ]# |! r# j<?php/ S+ c% ]' ~' u
    phpinfo();    , o% Z# K3 w8 c' R
?>
' m5 _9 D# }2 F* Q4 H& u' d在浏览器中访问:
; p* Y$ L" S: }/ a
; b8 p2 K3 M8 e& ?http://www.52os.net/phpinfo.php?id=1 正常显示。( }5 H3 m! }7 u/ R5 A# d) e8 h
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
9 Y# J6 f" A1 N4 ]http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
' [: F! K8 c% e3 A. @$ ?( ]说明sql注入和xss已经被过滤了3 f  Y' }! _# b/ ^

' k% m* j" ]9 K) b0 l七、安装过程中排错) ]9 {) W  Z( T# E& M: O+ L3 h

) J4 [) M" r6 G+ C" Y4 I3 p' @1.缺少APXS会报错+ k* K6 ^; D9 Y, \9 L; o* J/ r

4 V0 b6 ~9 d! X/ p3 ], Iconfigure: looking for Apache module support via DSO through APXS9 X2 T$ h2 R/ E9 N9 q  u
configure: error: couldn't find APXS
8 @8 @& q/ L# S% p, y% eapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
( B, b6 f! C6 L: n, v, ^解决方法:
+ p$ z/ V* Z4 H' ?4 N7 p0 [+ J. ^' E+ e
yum install httpd-devel, @4 @2 m0 m; K$ x! c3 V
2.没有pcre
* d$ ~5 G1 k/ F0 N
; a& w! P+ F! w( e  Oconfigure: *** pcre library not found.
' p  [& j8 v7 N  rconfigure: error: pcre library is required
0 H9 L% r/ ~9 f& m- F4 J. f8 e7 {解决方法:
# t+ T; u" L+ Q3 K
5 P2 ?2 [" a4 d/ Y* z% e9 _yum install pcre pcre-devel( z4 @! j& ?0 q5 q  {
3.没有libxml2
4 R2 s8 Y( V! X) d  R+ Y- j, L& `+ \3 \  A( Y

: d3 J0 n) I+ q5 aconfigure: *** xml library not found.
9 U' v' o; Q6 iconfigure: error: libxml2 is required6 O  w+ x1 B: J: w) G8 d
解决方法:: N3 z3 I" \1 U6 y: n* C- W* L( d
8 w( m* M& C! u: d& V- ^- y) r
yum install  libxml2 libxml2-devel
0 o# @+ O" u' T5 ]. a4.执行 /opt/tengine/sbin/nginx -m 时有警告
2 d- i" w& H1 r1 H( g2 x0 Z2 A% f8 f8 A6 J+ x
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
$ t+ S' x- _0 g( x5 Xnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
6 L  e0 @' O+ K原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log0 X0 g) P1 t- l5 }; y
# Y' a7 N$ Z, V- _1 h
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured." {8 S! n# Y& G' B1 _" n
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"$ J8 B% C+ u" k1 F$ a
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
6 f0 S$ D& {5 E1 |7 V; u2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05". {: i1 o, F+ y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
3 F( L- |) K. W' w  a1 M* l- P& j+ R" {2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.' ~7 S, Q6 j3 A/ |0 _
解决方法,移除低版本的APR (1.3.9)9 I. B+ r$ S! F  {) g2 u* s

0 e( u# @7 k0 [& M. p6 y7 Tyum remove apr& H% Q- V7 T9 B
5.Error.log中有: Audit log: Failed to lock global mutex# k4 C) x) `2 R7 y6 x
3 {! r7 W: S5 [: ^: c8 I3 M3 P$ |
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     4 g0 I4 F& ~' i
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
. C  n, J! p! F6 Q6 G; G解决方法:; M4 H/ D% B" @+ `
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
7 U) m2 Y; t! v* `7 q  _5 g( j8 X+ w. H
SecAuditLogDirMode 0777
! M$ l' u; Z! c! F2 e- RSecAuditLogFileMode 0550$ x& Z; O+ D; H
SecAuditLogStorageDir /var/log/modsecurity! [( M1 u2 n4 T( s: o
SecAuditLogType Concurrent
3 r0 N! |, t8 N- p! O2 l  L参考文章:
  M: g6 J4 C' K9 p) [7 @5 hhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
. F8 e6 b7 ~* n& F  Ghttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-6-22 04:03 , Processed in 0.061014 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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