找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11734|回复: 0

nginx配合modsecurity实现WAF功能

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

2 ^$ T- j( r7 J2 c8 O一.准备工作8 p/ [7 e8 m  b$ a2 k, W
/ q  j9 F/ S- `( W: P
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0, t7 v  b/ p3 s6 x
# o5 Y" ]! Q' Q/ s' A- a
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz$ R- Z4 z0 Y, b1 a# b

2 x' V( c2 @- K  Bmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
( d  j" h6 C7 d1 M8 y# b) _. |8 A) M7 m8 @
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
# c' ]) x% V6 K# _/ L6 t0 L5 w2 N+ e$ z4 f2 G( F9 i! r
依赖关系:& s) Z( J' A3 R& u/ L; k: e! ~
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:' _7 ?9 C6 S; O( s6 [
* E- {& C, r+ Y1 x. f) m, L5 |0 h
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
& O- U) e8 T) H3 e$ cmodsecurty依赖的包:pcre httpd-devel libxml2 apr
. o8 l$ H# `7 ]  t1 ~3 `1 V* Q% C6 t" A- t- C
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
  M0 @  H3 X9 x9 ?( Q二.启用standalone模块并编译! C# D( w8 L" {. F) z

9 X- O" ^% M1 o& x5 U1 _3 [4 I2 \下载modsecurity for nginx 解压,进入解压后目录执行:# p3 Z; M6 {6 I
6 ?& k3 ?! M# _, g9 \5 R
./autogen.sh9 ^5 S. B) s' D0 E$ P
./configure --enable-standalone-module --disable-mlogc) \" p8 y: I1 ^0 r3 g
make 9 G% x9 J1 O1 P) g' a
三.nginx添加modsecurity模块
/ f' x5 ^; a% k. N& q; U$ Z- R  s  }# s8 W
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:# ]  _$ L2 a( a* E' C) e
8 S6 t1 ]+ V+ k' y. W
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
! R$ R- x* }0 xmake && make install
% M: B5 r  m8 r( q+ }7 V四.添加规则
) B) j8 P7 F* G7 w- r
( F2 B% ?' g* xmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。1 m% b( a) n, a

6 w; u9 S: o; f1 `1.下载OWASP规则:
# N5 g& Z3 E0 g2 {8 `  Y' `9 ?$ W8 ^3 r6 B1 f! U
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs" p5 e# Y. n, t8 N$ |1 H0 y
9 z/ }$ F: \: g/ N0 P
mv owasp-modsecurity-crs /opt/tengine/conf/5 a, x; q# }+ s4 i: o- M

) _1 D9 w  ^7 _cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
3 E+ w0 M" m6 ?2 U( I2.启用OWASP规则:/ j6 |& L4 j7 u! j2 V# g, z
6 p( U2 C1 W  G1 Z# h
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
# o2 V; a' @: B9 E, B7 R0 n
  |& |( l' i3 S! Q9 a编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
. K9 w2 u7 ^# D* X7 L
0 [- V6 J: q+ A6 z" Gowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。1 p8 D9 Y: u* q; i- X

5 @# y0 X/ |  w+ M3 kInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
/ j! d& ?# n, |' ^; s1 {Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
8 [2 m6 ]5 \; ~4 G1 L: L: j/ L8 p; T2 X- }Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf8 E& _/ m$ ^. _2 G# H8 x4 ~4 U
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
% ^/ s8 C4 C8 @: _+ SInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
6 ~, M8 w8 w; l, z% y5 a. ZInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
3 S* d1 e; P% E' c* ]) {Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf+ I8 \, Z  e0 _) Q
五.配置nginx& S2 C1 V, O  y8 C' d' |$ ^

: p! f! P* J' Q+ L在需要启用modsecurity的主机的location下面加入下面两行即可:
' U' ]" [6 r' T  R) t/ I7 e% K* v% }
# k9 Z5 V; j! c3 [  T- Q6 uModSecurityEnabled on;  
" l7 Q4 X" j% h& _; YModSecurityConfig modsecurity.conf;: U- ?3 T( w2 Y5 l7 n# f
下面是两个示例配置,php虚拟主机:
, a9 ~% s: b6 w4 ]2 G+ }) X. x2 w& I; `: H# J: a; }
server {7 M6 O% {' k" ~8 Y3 A
      listen      80;
+ V$ @3 T, t- P/ A: t* z      server_name 52os.net www.52os.net;3 L  L% Y2 |8 Z7 Y+ p/ B; _3 I
     
3 ]& e; E0 c8 @- ?/ L# G. D: V* H      location ~ \.php$ {
* U; W& |0 u1 p3 j" K) ^      ModSecurityEnabled on;  
" q/ b* T# X: {+ L. g8 r      ModSecurityConfig modsecurity.conf;% V4 |7 T1 o9 L5 {, o
/ B, p; f( b$ t% m4 w8 [, I
      root /web/wordpress;
5 U' I! C: C) v1 r  E      index index.php index.html index.htm;$ S5 m2 S8 S! k, h- u' y0 D
  
$ ?: ?% ~9 A1 d! O8 c  a      fastcgi_pass   127.0.0.1:9000;. j2 ?/ u$ C5 e8 F
      fastcgi_index  index.php;
. C6 f  q* b4 x, m      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
) e+ `4 A& U* o) r      include        fastcgi_params;
0 n- _+ A$ z7 Q      }
1 f6 s- Y  B% Y8 a  }# A: R- `; E2 n* N6 k+ }) o
upstream负载均衡:) `" @) d/ S) {( j# S& V: D+ F' [+ B
9 P. h8 w+ W& y9 g/ m
upstream 52os.net {
( [) y: I! X& v& ~$ _, E    server 192.168.1.100:8080;
+ o* u2 }3 b  h! ?2 C! q/ k    server 192.168.1.101:8080 backup;
1 A& t7 @2 a( m5 T6 U+ ]! h}5 U6 Q7 N) J% D7 N9 b* h; ]- K

$ J) H) t$ F7 O& tserver {3 F6 Y$ Q9 a( |* q% ^
listen 80;) ^0 _  j. v8 Q& x+ Y* I# x/ K
server_name 52os.net www.52os.net;9 C; Q  M6 `1 p3 e% q% C, Y2 n9 f

2 K+ A' ?: i$ Ulocation / {
# S+ D& j, H5 \( {+ G; S    ModSecurityEnabled on;  - T: W6 k, ]; Z0 q
    ModSecurityConfig modsecurity.conf;  . @6 B' c& a. ?3 `1 b6 {$ K

: C& S  F- o; T        proxy_pass http://online;5 P( _" [) o/ G
        proxy_redirect         off;% [5 e& u& C5 j$ O/ J! o% l8 n
        proxy_set_header Host $host;$ Y* {) }1 X+ E3 i- P( _+ [
        proxy_set_header X-Real-IP $remote_addr;
5 }# O6 c, w, v. P8 f        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
# J6 b0 k9 h; |5 M& t    }+ @0 C" P- w5 _
}+ @+ _/ ^% p7 j; v2 b7 W7 [
六.测试
2 o6 i6 [' m2 D" \4 g' m+ J7 S  o& |2 g' `) t( i
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:: U+ Y8 P7 |( r- r/ F% p2 M- O
: D; s& E: V8 T) y
<?php% a8 c) ?+ V8 e: j0 Z2 e/ f5 ~, V
    phpinfo();   
8 `0 O4 e8 x& N# _4 f; |?>
! Z" {0 O% Z- O" a% i& [# x在浏览器中访问:
) }8 d/ C4 }( z# b8 X, w0 {* o& h5 T$ }( K( Z2 {+ B
http://www.52os.net/phpinfo.php?id=1 正常显示。0 O) R# T7 e  _- H, j! _
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
% f4 u% ^. n3 q4 S- b$ s2 \1 whttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
) X3 W3 y' Y& t* e" N3 j5 [6 K; p( @说明sql注入和xss已经被过滤了
2 X; {4 Q* ?2 ~! f3 E
# ~4 O. p9 {+ k0 A0 a1 R七、安装过程中排错
. G0 e$ ~, e& E/ `- Z6 e. O2 }  a# W4 p, p
1.缺少APXS会报错
% P9 V" S3 d$ @/ L, I% u' \6 V) p/ ~" p0 h5 ]
configure: looking for Apache module support via DSO through APXS
4 {3 F% O: x6 I$ h$ L1 k. qconfigure: error: couldn't find APXS
" N) a. W# F! Z* P2 I0 E* Xapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。$ Y( G, {( g0 W7 g1 Y( ]
解决方法:
' p/ W" P; {; x& H' r! X" `
& v& b* g$ @$ Ayum install httpd-devel1 C' z+ r- k) H! D
2.没有pcre$ C# Z' f$ o( ]* Y0 a6 S. E
( V  O$ w* Q2 C/ I6 o3 u
configure: *** pcre library not found., L7 J+ a6 S8 l. s0 ?$ j
configure: error: pcre library is required1 a  E7 B/ n7 w
解决方法:
& Y3 b  N6 H5 T. L
- Z  n4 z& }, t$ Q% h/ w- Pyum install pcre pcre-devel. w7 A' O7 y9 P, A; V1 q
3.没有libxml2
$ }: I& }4 b& A- H0 z2 h7 F
6 Y- a" o: c+ ?. I5 I: r  [  Y& x% D( t( d
configure: *** xml library not found.* K* Z, W( U9 R6 {! o( J$ }% B
configure: error: libxml2 is required
6 o3 V& a9 s- x8 @解决方法:
- A, R& E& w0 L8 p" l5 l7 j+ T# ?; x4 \% @' X1 i% |
yum install  libxml2 libxml2-devel
8 \1 g5 t% r) R& h4.执行 /opt/tengine/sbin/nginx -m 时有警告
* V+ r$ o& o4 ^8 \6 `0 ]3 Q+ S
  T' t6 i9 j/ Y6 u" yTengine version: Tengine/2.1.0 (nginx/1.6.2)
2 Z) |8 |2 w6 e4 q3 Wnginx: [warn] ModSecurity: Loaded APR do not match with compiled!8 e+ D  q, D+ B  e5 M
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
: T$ j- ?7 a) G: p" F& s6 D# b& \
3 d4 W. e* |, p' `# a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.  @1 c8 C8 |! P' H
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
; w, t1 N' v8 x) F- _3 P7 J  U2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!3 |* g2 U0 i5 J6 n/ l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
5 V% ~3 _( Q+ f. M# x9 s3 l2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6") z, y, i* s; W9 g
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.9 N. j3 o5 @/ L# W  J* s
解决方法,移除低版本的APR (1.3.9), f. f1 o. q( x8 J
( _: n! z* s/ ?/ Z; l+ s: \
yum remove apr
* D3 r+ N; S4 E3 k5.Error.log中有: Audit log: Failed to lock global mutex, r' D& O4 L3 L" x

7 a& M& U6 Q  l7 k3 F" b2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
6 f; A3 N8 M9 E: b* \1 rglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
# j9 T+ r* g- @0 r" u! |  W解决方法:/ k- ^2 ~7 ?4 t  v# [
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
, T6 d* n! f4 ^( B$ g. u' H8 M) f4 m3 J! `1 I+ J1 J  F
SecAuditLogDirMode 0777
6 j: S0 G; |! R; }3 f9 D8 R: SSecAuditLogFileMode 0550
7 C! t# _( B0 H% d$ SSecAuditLogStorageDir /var/log/modsecurity  [- c  K2 v" a  ]+ A' U2 i$ o
SecAuditLogType Concurrent
. L/ \3 C. M& d: w7 {参考文章:
3 F/ X; _, o" y) n7 Ihttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
2 o0 I# }+ q* ^/ i0 y8 \5 N% ^* @http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-9 17:50 , Processed in 0.072889 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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