找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12658|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。4 Z- z6 f, B0 t7 Z! C! l3 j& t* y
. H" y7 W! j7 D1 B4 h  d
一.准备工作0 D+ _. \- d0 e2 e. e( [6 E
. k: Y5 V# B7 r/ H" b, [( a
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.07 J, Z3 a# t) B# h
7 Z# K: S8 L8 S* }8 }1 _% I
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
' S1 L5 F% m( ?) J' c$ j& o3 A$ v0 s& G
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz) |9 S; w9 l: q4 U& a1 v
2 C  x7 _: J2 y3 G. U
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
8 _4 W5 m* h+ {" O+ t6 a/ {# R6 C- T, s7 E$ v# `% B
依赖关系:
  e6 f0 @! F6 b, ]$ c, Utengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:4 _' S1 [  m. V; W6 {. b( l5 ?
% d2 P2 z3 S/ [! G% W
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel) p7 A+ Y0 Y8 P$ s/ Q* J$ M
modsecurty依赖的包:pcre httpd-devel libxml2 apr/ R2 _: U# C9 C: j. j: Z; _' o
/ v- |6 R! m) X$ @) m) i' o
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel) r- _1 Q  {& t
二.启用standalone模块并编译$ X% y  x- c$ e+ x
8 Q# b4 p6 s; Y8 c3 N, }4 o
下载modsecurity for nginx 解压,进入解压后目录执行:3 T: b7 L( @# |; S
* Y  j4 D8 T! ]! {
./autogen.sh% D( `! v; T) {
./configure --enable-standalone-module --disable-mlogc1 G" K& @0 t  x. Y$ ^
make
& e9 \/ z- k' s5 e三.nginx添加modsecurity模块% }% t, L- }- u% l
# n0 \/ `5 j8 J
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:# y5 ^# K  X& R, Y4 h2 j7 }3 S0 ?* G1 u
: j; u. a; m/ k
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
8 B% v6 x* Q8 y' K8 h" _make && make install
' Z+ a& b# y% x0 x四.添加规则
7 U. o% a; [8 F# @: E' M  e. x; p6 J# Y# d; @# C6 @' G2 n
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。  T5 l; V# x; ]/ I1 V0 Q

0 m- k+ @9 X3 w8 j/ ]- b1.下载OWASP规则:9 Z. h/ M4 L/ U
7 u6 X3 o; D6 ?6 L) s
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs9 W- i5 I2 {1 Z/ I2 H" N4 M
2 E5 Y( W2 U1 I5 W( L5 a! e
mv owasp-modsecurity-crs /opt/tengine/conf/
" O( s) m/ @' |6 `2 v2 f
7 g; ^7 A1 a' M0 L: x& v# o& R7 Kcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf# P3 o' a! W# ^
2.启用OWASP规则:
; M' V4 N" G, i9 G$ v
- k7 O8 ^9 D2 E复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。. _0 }9 W4 x& F% |- G8 f% X

& }, D# @8 j  D编辑modsecurity.conf 文件,将SecRuleEngine设置为 on+ X% Z* y' I$ \

1 I$ C6 h% n0 u8 U8 v4 |# Xowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。& O; w( H0 ?: K* ]6 `
- X5 {% I! [5 _- h
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
6 s) i: K; b( a* k, M' {+ l2 GInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
& |( j% ^$ o) b' l& ZInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf( ~  m# G! ~# s8 n$ W& O1 C8 k3 W
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf* @( b: `0 R% @5 ~" S; [, b
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf/ l  G' u8 V3 t* f
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
0 B3 z- k2 G% p) HInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf1 X& V- S! w# H6 ]. E- |/ [3 {
五.配置nginx2 y6 A* I3 s) j3 \' G9 o
$ X: o& X2 D7 @
在需要启用modsecurity的主机的location下面加入下面两行即可:3 O$ |8 ]+ B$ I

9 K+ h' x4 D4 S$ GModSecurityEnabled on;  
# L0 L( A3 @1 C0 e) ]+ p$ jModSecurityConfig modsecurity.conf;
; Q8 B$ u  x9 |; M$ @# n* [: {下面是两个示例配置,php虚拟主机:
8 F9 V- A: D% r% j) E( O% j4 z; g2 P3 M; o- r
server {
! u( H, b! Y& q! `  P0 e- _( U      listen      80;
) p  r' t4 g3 d; s$ M      server_name 52os.net www.52os.net;
6 u4 S+ ^9 _; {8 u4 B) ?4 o. c) }     
* ]2 |3 l2 w! |/ h; g      location ~ \.php$ {3 D% j$ P7 c/ b) V1 i% Q" ^
      ModSecurityEnabled on;  ! G4 h4 l, h& C; a
      ModSecurityConfig modsecurity.conf;+ b0 h6 \5 `4 B3 l3 k
3 N! M, L. J- ]
      root /web/wordpress;
4 Q2 M9 x5 U5 s0 v; Y9 ^      index index.php index.html index.htm;4 K1 x' ^8 w; c' d2 |) _  p. t8 D
  
- r5 B+ p& n: e  P      fastcgi_pass   127.0.0.1:9000;' |% N8 [0 U7 G# s7 n+ M6 H! C: H
      fastcgi_index  index.php;
! x' O8 d8 i% b+ D" |8 Y      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
8 q9 W$ ~3 x7 Q# q1 V& o! k      include        fastcgi_params;
. Z3 c- c2 D8 [3 ?# U      }1 g, R4 |: }% N  ^: B
  }
& U1 J- o4 v; c$ s; C/ mupstream负载均衡:7 d% E3 R: u5 j  p- l" O
! ~7 g8 }) a6 F% x* m
upstream 52os.net {) w- o) u. z. a* q/ R9 j
    server 192.168.1.100:8080;
; D7 t# g& X5 N$ I    server 192.168.1.101:8080 backup;2 q, X$ j* F' o' ^5 H. D# G( K
}
, ]9 N3 `& G. C
) n# T3 O+ e3 @) ~$ O. ]% gserver {
3 s) H1 ?. H9 z9 y+ Ylisten 80;
% l; ^' m  Z& J+ C* Bserver_name 52os.net www.52os.net;' h9 G7 \$ X3 g  z

/ Y, l/ S7 M& |7 b# H9 E$ Mlocation / {& O4 H4 T( T0 {/ C$ E
    ModSecurityEnabled on;  
. m# O; V" x6 C! E' S    ModSecurityConfig modsecurity.conf;  % e; S' v( }+ S4 k9 P
  t: o1 w4 z; O) ]/ T% {4 ]
        proxy_pass http://online;
# h& p5 A6 D) R% `9 g/ {        proxy_redirect         off;+ h) ]( f( t9 F, e: X
        proxy_set_header Host $host;
  a% M8 u* O: z, J5 j, L        proxy_set_header X-Real-IP $remote_addr;! Y( `  [1 \4 W- ]4 t# _: ^
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;" k, H% O+ Q* g- S; B
    }
% @7 {. y' K; S5 ]( p}! v. I' M. M% \, J
六.测试
" G( V/ G: d1 D3 S( \. a# y
  L# O* v0 ?  n3 c0 y; l我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
$ \& y2 \" m* r: I4 O* j( Y  X3 ]# X, |+ h. w3 X/ u
<?php
( j7 ]4 J' H( \+ H) W0 K* T6 Z    phpinfo();   
0 f" e# ?- P5 X* q6 Z?>
. i9 Y# {" `5 k3 y- k% n在浏览器中访问:
( q5 A( u1 e# {* [$ {
9 f4 k& t& N  V, g8 F5 y: rhttp://www.52os.net/phpinfo.php?id=1 正常显示。; i6 U' {: c$ p4 }& R
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
' J; h4 A1 f% }5 U. v, G$ l6 h% Chttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。+ P# r( Z' v: ]' P$ b% r) W+ Z0 _
说明sql注入和xss已经被过滤了
- m# c8 K3 U- \( k8 V, j1 K8 M- F" P7 t- u# N, d
七、安装过程中排错
& M9 d; ]% ?# O& W  z% T4 m: _% d( X3 s; x
1.缺少APXS会报错% n/ h: U/ |# p: B

7 n9 {; }1 P9 J" ?configure: looking for Apache module support via DSO through APXS! ~$ W5 g8 b7 I5 X
configure: error: couldn't find APXS) w/ ~7 V% K2 h
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。1 C1 f( S0 c6 v$ ]
解决方法:; j/ Z6 l$ a1 K- T/ M( l
  ?6 C% I1 v/ v$ J9 `
yum install httpd-devel
( t; ?/ k$ Q5 h2 A' t' }8 X2.没有pcre
' A( S& {% E4 I1 Z8 O3 n) U
4 ?6 ?7 s6 j! t/ Z, O& `3 Aconfigure: *** pcre library not found.
5 `! f8 D5 t5 [/ F* j2 dconfigure: error: pcre library is required
  v2 V, H9 R5 a7 ~解决方法:9 n5 o; c  ?7 S5 l  o2 ~* ^, c" s  Z+ e

2 O2 Q$ S; A  \7 Yyum install pcre pcre-devel4 P2 p" ^+ i4 h4 k
3.没有libxml2
5 |" x  H6 Z4 h8 w6 m( r8 D$ H) w; R. M% G2 |6 I
: U& Y8 N# G9 g
configure: *** xml library not found.
9 M5 s: D" \" m4 }& f& ?* Lconfigure: error: libxml2 is required- C& r3 A* N1 o: U! G7 a, j
解决方法:+ z/ q* s+ L3 ~" {) W6 K  V

$ R7 E# J6 S% d: q/ {yum install  libxml2 libxml2-devel. v) i* f+ C5 h8 v% D2 V
4.执行 /opt/tengine/sbin/nginx -m 时有警告9 w# \2 o, ^9 e; r* f# Z
* h! _/ e  r7 i# e
Tengine version: Tengine/2.1.0 (nginx/1.6.2)  ^8 X5 I* B; }* d
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!" K, A2 d+ L5 {2 d2 T
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log# ^  P5 z% f% U2 D. g
- T" j! M+ V5 l4 U$ v4 r
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
0 i  [2 B1 q/ L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"0 d+ n) d& T* T2 i, J" o- w- W
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!4 `. v/ t! N' r  r3 S0 V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
$ b( E1 [2 ]) Q/ n  p0 O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"; _9 u: K* ?& J$ p& E
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.3 e- [) A6 l* l" M
解决方法,移除低版本的APR (1.3.9)3 C% V; y4 l8 U7 ^- t

7 t4 U. B; i% d. @! I+ Hyum remove apr6 J" i" B. d! Z
5.Error.log中有: Audit log: Failed to lock global mutex, \; B7 v8 M% m& |* R
# V, j% a# \. g: A+ T
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     , k% Q/ L- R8 I  V$ ^9 U' K) U
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]) b, A5 H. O2 g2 m9 ]3 p. P
解决方法:
( u: m( p& Q' h1 D1 ?2 v. X+ J编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
* m9 R* [# j  @: j2 r8 K9 X2 {  d$ f5 `
SecAuditLogDirMode 0777" m3 `  m- _2 Z/ W/ u1 K: F% H
SecAuditLogFileMode 0550. |3 z6 p% W: r0 a: x) ?  Q% h
SecAuditLogStorageDir /var/log/modsecurity
' |; e1 \/ }+ a. \5 qSecAuditLogType Concurrent
1 F$ Y0 I# u1 |! O/ @参考文章:1 X( M' ?" E' }) Q
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
9 a) A% o2 ?, E7 _+ @6 a4 fhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-27 06:29 , Processed in 0.070724 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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