找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11336|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
1 I. A2 @- p- t: N) _( w
9 C: V: |$ x- ^0 Y一.准备工作
$ T* D+ T5 r' C1 E5 _0 E5 M' d; S. v
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0( r& g! {3 I( w5 c& O

% u: L- b% u( F$ q. g6 Y/ n9 ?7 G( ?tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
* k' Z3 C& ?6 h% Y2 d8 }6 X1 o  \# p; S$ q4 E
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz6 W& Z6 p' P: j3 k

$ o2 I9 W9 E) i. B& _- W- AOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs* }5 ~. }# @, r% I. k8 Y
2 w% c6 h6 c* C5 z% u
依赖关系:9 _! `$ u" z. o; z' P
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
. ]" Y+ }" i. h2 R7 Y  A( ~$ v  R! |  r- f& i
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
- y8 ?# H6 U  H+ W4 x6 A1 Q/ Fmodsecurty依赖的包:pcre httpd-devel libxml2 apr
2 \, \6 }6 a6 j' C& f
; m7 H4 L2 U% g, ?5 e% F8 kyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel+ d" R- w' }; D  O; I  x
二.启用standalone模块并编译& t2 T% O. ^+ S3 g) d( R# p

4 q1 F& K, q' O( X下载modsecurity for nginx 解压,进入解压后目录执行:
! e. |+ ~8 t( D. `( u
, v7 T" ~6 I* _./autogen.sh; W. D2 ]5 ?. H+ x
./configure --enable-standalone-module --disable-mlogc
5 Q% f' Q7 N6 |& S3 W- @( Lmake
4 l: T( _* h% ]. ]) t三.nginx添加modsecurity模块* D' @' R$ W1 \  A' l- J
, g/ Y: h% B7 w* G* c8 w; w2 q  K
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:4 {# z% ~7 `" Z8 f& V; w) H7 L

( O/ q, k: G# G  p* F- A./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
( q* I5 p" @# Vmake && make install- P/ _$ t' b- L: y4 y: L3 s; c
四.添加规则
3 L2 {* s: ?* _( [" T( t% ?
* F# v$ q! g+ Kmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
* X2 Q  I. r0 _& C9 x2 f/ U. z% Q9 J+ K7 i3 a+ b: p
1.下载OWASP规则:4 T+ W# d; f9 ~) |! _' x3 _

6 i& w4 e2 ^+ D% z5 P4 Tgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
8 ]# b- V/ l# z  e9 B. k% i$ u
mv owasp-modsecurity-crs /opt/tengine/conf/4 u  J$ l$ X3 x

$ @6 a' o, @9 w8 t+ V' w& q* V/ Vcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
# k5 _5 q5 A8 V3 b/ n" p% F$ T2.启用OWASP规则:' ]; d( @, o$ q3 Y' k$ D. r

* R( W( L" B2 `7 b复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
$ [$ u* ^3 }# K+ z8 v( @6 P% G# r" L7 u9 `, k: L1 {  f
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
! X8 ^# r5 S8 ?, L+ s6 ]0 Q
3 N0 y7 X0 y6 Nowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
* N  B" p$ L& t' J6 a# B: I; J0 y
, n; ]- |/ W# h3 |8 m% dInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf  i% A- ?  i- q' C: c+ C0 `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf& g4 f7 M, L$ O+ t* H" I
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf( S7 Y6 u" R0 \
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
. o6 O! H7 ^5 \# H6 hInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
2 K! f- a8 V# uInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf. }% Q( N3 i9 E( c9 Q
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
8 J) r1 r) O  d  {  Y% i五.配置nginx3 F9 ?; V4 d! ^1 C) A9 p
, z% b  T( v  N$ E6 X
在需要启用modsecurity的主机的location下面加入下面两行即可:6 ~9 H0 Q2 P* O, e7 b& ~8 y$ \

2 c# S, W# Q! ^: e+ `) aModSecurityEnabled on;  
) `; L5 A. l- m6 @. {' \ModSecurityConfig modsecurity.conf;' e9 z6 X1 C- H& ^
下面是两个示例配置,php虚拟主机:1 k# f! i, M6 m6 U# z

8 H3 s# i+ D7 s0 J7 h5 Iserver {6 I& L6 A. a! P5 E
      listen      80;7 a7 w: c/ i) t3 ?: B9 q
      server_name 52os.net www.52os.net;/ {) e5 {1 P0 H* l$ V5 a
     & o( y/ F3 ?1 s) V5 _
      location ~ \.php$ {2 ?' O5 y. M# H3 }9 X5 f6 K
      ModSecurityEnabled on;  0 y  r% `8 _+ o
      ModSecurityConfig modsecurity.conf;
0 a3 J& t1 S, ~' R$ l
$ T$ `! q) z9 o  |  N1 p      root /web/wordpress;2 Z2 O( Q/ j5 `4 z  S
      index index.php index.html index.htm;( V4 {0 j9 p1 w
  
; G) p8 O# D' D. E$ b. E" C      fastcgi_pass   127.0.0.1:9000;+ d7 Z- U9 Q0 X5 s% l+ `% x. A& q
      fastcgi_index  index.php;. k9 c. W% \6 E8 E
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
8 q9 I! h0 Y8 h/ u) m* q      include        fastcgi_params;
* b8 O5 m, k) H9 d' \, F" `: t      }" U4 D0 I2 l6 d3 L: l
  }
( R: c0 n4 f) Xupstream负载均衡:
( P! E7 z+ d' Q: b  m0 {7 a
3 e  ]. y7 ]- v3 S" Dupstream 52os.net {; b" z+ F; G+ W' r
    server 192.168.1.100:8080;3 N9 g0 V3 p+ h. \8 x1 K1 F. J4 O  r& w
    server 192.168.1.101:8080 backup;/ _- b& b& ~5 w8 y: N0 o, c: ?
}
; G1 f6 ^8 h3 }0 A9 h' s
! b9 \8 D. a/ Q+ v- P6 Yserver {
0 c# N% V7 w0 r1 Klisten 80;, D. g7 U# [3 x, a9 I
server_name 52os.net www.52os.net;: l+ P9 C! d6 O2 q; _3 U7 Z
5 G8 {" C! x' E) }+ X( _/ O- y
location / {
6 z( y, a, C  ?* ?    ModSecurityEnabled on;  , `- s, k1 o, _- F) L
    ModSecurityConfig modsecurity.conf;  * s  m% {' n& M/ H0 W
5 A& g* u  M, H$ w
        proxy_pass http://online;
, t6 b* n  I6 b. v5 o        proxy_redirect         off;
* i' d7 R9 Y1 _& Y4 O' U        proxy_set_header Host $host;# P* K6 z6 m2 q2 M1 W5 |8 v, g0 ^8 F
        proxy_set_header X-Real-IP $remote_addr;& }2 F- b- J2 o+ O+ G
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
0 \/ R" h' u0 \3 H2 {- g    }* P# ~% u6 ?, x# U
}1 {1 c# N% S2 ^1 S: |) i1 X
六.测试) [; V3 C5 V& W' a1 w

( F% ]0 j, v$ f我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
7 X  J( S1 M; s' U* b! K, ~" z8 V* E& Q  h3 x+ G
<?php% h2 j$ z7 `- e& J$ c# Y7 ~8 d" s. R) D
    phpinfo();   
; c7 G- ^3 z, k1 K1 _1 Y1 w" H?>
0 d' z, e. y/ Y/ _' B9 ?" f在浏览器中访问:/ F; W7 j. u/ D9 Z6 j: x( D
6 Q5 p) [) R. `
http://www.52os.net/phpinfo.php?id=1 正常显示。; h+ A3 h; X$ ^9 L9 O6 ?  U. X
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。& O0 Y3 j6 {  R1 Y7 J; I
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
1 K* l5 Y! ^8 V8 ^( Z说明sql注入和xss已经被过滤了
/ N7 S. Y0 ]" y  w: t- D* v7 O5 P. g4 u
七、安装过程中排错
9 u7 S0 B& ~0 j6 r
. e- P0 M1 K" I: d, X. z, F1.缺少APXS会报错& I( a, p' p6 n6 t( O
6 C: r% U  H/ y3 `- A5 a0 @
configure: looking for Apache module support via DSO through APXS
1 U8 P8 M% ]- h! F! \configure: error: couldn't find APXS
/ S" l& \0 Q; `$ Tapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
% T6 h7 L; f6 k: X' j& c解决方法:
6 W$ Y: g/ T+ q2 S. U4 Y6 V/ J% L1 d" C3 O, [6 O
yum install httpd-devel# I6 f2 j2 |2 T+ s' o* q4 o
2.没有pcre
; F6 r* x" L' F; `& ?) ]
5 L) o  r6 Z- w, Mconfigure: *** pcre library not found.2 ]2 [0 D' b% U
configure: error: pcre library is required
5 u* ?( X3 P/ {2 V9 o; L解决方法:# Y) @/ O+ J; A; t# R' \. u0 A, i
' Z2 P5 h# R1 k  K- G$ n9 ^
yum install pcre pcre-devel
& G4 L8 {2 d4 S, W5 d' a" ~  o3.没有libxml2
# o4 n& z) w7 n/ i. c6 ~+ @! X+ P& |

5 E# H! D2 T& P: q* A- vconfigure: *** xml library not found.+ |) y: Q. j$ ~( `% s
configure: error: libxml2 is required
& M& ~0 T/ E( x解决方法:
5 r4 w/ k5 i) m% o) }0 h( ^9 M' ]9 C( o: X
yum install  libxml2 libxml2-devel
" _- _& i  G5 R8 \2 r& u& H4.执行 /opt/tengine/sbin/nginx -m 时有警告
' d: l' e! G3 ~0 \2 V
! ~& P" E4 B! ~+ N, }Tengine version: Tengine/2.1.0 (nginx/1.6.2)3 `$ K: ?# p# W4 O5 y
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!9 [; t9 B: w& P, Z# g
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
, L5 ?( ]- B' X1 ]% c6 _, e" F5 R! j  F" C: Q: E1 T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured." @% w5 }! `) U* F6 c: l) G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
# S% j) |5 J/ `5 y6 n0 S2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
: ^$ F% B4 n( [1 B2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"( w' I, U9 z0 d0 I& j0 T! d& j2 s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"& p2 x: a. l- }. V4 n# o
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.9 ?1 u) o' N4 B+ u0 H' z) T8 m
解决方法,移除低版本的APR (1.3.9)
* c! z. P7 q% j/ @0 f! C2 L5 N! e6 {. b7 D, i0 ^' H3 m1 L
yum remove apr
. [) u$ j) p+ X2 n4 D5.Error.log中有: Audit log: Failed to lock global mutex* `7 O7 s7 `  K3 y

5 c% I, C9 l% @  i* Z8 V2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     6 m# n" j% t* e: J8 ^
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
# f8 m8 [. f, K) B- f- g, f9 ]7 ]解决方法:' i: a5 L- x- o/ H' E4 g
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
1 f. E% _7 F+ U) {1 Q( A2 o9 V/ r. J: u' g1 T- N: ]
SecAuditLogDirMode 0777/ ^8 F7 h( q6 [0 }
SecAuditLogFileMode 0550
* Z( C+ @/ {3 l7 o  k1 y' ?& NSecAuditLogStorageDir /var/log/modsecurity
0 l8 }& R8 d( K) m) p5 kSecAuditLogType Concurrent6 T, B: Z2 f/ C1 b$ s, w) l% K
参考文章:
: W$ {" ]8 v: {# X3 O8 k& mhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX+ a4 H0 e" p  e0 o6 `9 ?5 t2 @$ Z1 d4 E
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-6-10 18:45 , Processed in 0.087461 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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