找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11590|回复: 0

nginx配合modsecurity实现WAF功能

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

5 h: O) q5 T0 V- `. D一.准备工作2 Z# e) V' r- }1 o- v

1 z: l$ P; v/ f+ I# o3 f& a系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
+ \  @8 J. M: j3 o. Q7 K8 a$ o2 Y% r% S- O9 F; c! Y# [$ c
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz) A! K2 M0 i# c( o) i; M9 H4 H6 q
2 p9 B" x8 x0 `) w. A. o& o
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz; r; ~% \! M( C* [8 l# [# b
( r! t4 V9 N0 A9 ^: v( A% l3 k7 h
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
) d* ~; M& `: n$ {$ p1 T. r0 x4 U9 b, q- P
依赖关系:
! t- j* y1 l; j2 Etengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
! W# N7 X/ W5 g- _, W4 w5 i6 `- o1 ~  c
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel& c9 M+ z' @1 l
modsecurty依赖的包:pcre httpd-devel libxml2 apr
1 i; a) [/ j7 _1 K
, K4 [- m! P1 R: \0 h7 jyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel2 ~% a9 h4 Y2 K3 ?( c
二.启用standalone模块并编译
% p+ d% q& k+ }5 o
% ]* ]$ x/ k. e% ~9 Q. {! u* D1 N下载modsecurity for nginx 解压,进入解压后目录执行:
+ d2 l$ H2 {9 p9 Q% O1 N6 U/ u& N; `; H. E" t9 o
./autogen.sh
& R+ B5 V  n% X./configure --enable-standalone-module --disable-mlogc5 U- X) |5 ?  a, o, Q  _
make
3 a; ?! s" A7 Y% j( [* w& v1 V三.nginx添加modsecurity模块
1 S/ V+ y/ V7 j. c, d
, R% l$ [; q' C0 r' ~$ o在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
; W5 ]: v/ a) H* {. K! w+ B  V
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
1 c" D3 b5 d- F8 P5 F# Mmake && make install
* Y  _9 C7 N$ G  j, l四.添加规则; Z! f- p  {9 w$ N/ W

/ A' x/ j: _+ |- |modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。+ R( C5 H7 {% X. }2 r. J
% t: u( R5 W' n3 Z- h# L; U7 n
1.下载OWASP规则:4 v* N; i1 V% m6 }  _5 P5 D# b0 M. b

) ~$ l/ R0 @" kgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
6 P8 ^: D  R7 K" J$ n
" b" @7 ?. u! H' b' S4 |* a# tmv owasp-modsecurity-crs /opt/tengine/conf/
! t; ?5 _; R) K* f4 H; i' c+ A$ f, j& i1 g6 [2 j: Y
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
+ ~' o1 v1 x% a& q- Y( t8 J* k" v2.启用OWASP规则:! ~, H" n% v  N# Z5 s
" ^1 s+ N, p$ H. @
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。4 I# a9 z1 W6 D* k$ R3 @8 l2 W/ G+ g
; z+ ~3 z$ {! i$ |! ^
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on- ~9 l! e! r& B( o

3 f3 A, X* ?' q5 z2 Y3 [! a5 Uowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。- N7 Q" p! d& O  C  }
3 V% T3 U+ g, O4 M
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
3 |: i* k, r2 j  i0 {Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf& G; |* p' ?6 ^% X, n
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf5 `1 o; @+ b, v, \6 m, d
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf4 T" w* U, I. Z& [! \5 i) Z3 q
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
; D$ p, c: J, B) r7 }Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
3 Y5 {0 N5 M! v+ W* L! u9 k6 P& c+ vInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
1 r, B7 _# M" a+ Y五.配置nginx
+ ~! b+ d/ k# q5 m
' D9 @: ]1 I  k7 I8 o% t, {在需要启用modsecurity的主机的location下面加入下面两行即可:
0 ?& Z, P5 _) M: B; ]1 i+ d0 K( i+ v# ~
ModSecurityEnabled on;  * v  `9 L  a3 Q5 }$ s8 D
ModSecurityConfig modsecurity.conf;
6 w: G; z- g! y% V2 n下面是两个示例配置,php虚拟主机:+ F9 a( [! l: K- M  H
1 T3 Z7 k& j" G- K1 r( ?7 _
server {
: n( f" D6 o' Z3 i) o5 M; }      listen      80;
6 K, j0 I" H! t9 K! B& @7 X  }6 t, Y      server_name 52os.net www.52os.net;
. \! X* z1 k4 t8 i8 z+ _# z     " s1 F/ `' k5 o+ i+ s; S
      location ~ \.php$ {$ D! t1 _2 t8 N% T
      ModSecurityEnabled on;  
8 N3 U, S" z: W. `, a      ModSecurityConfig modsecurity.conf;) p. f( |; E  k- a% C8 I

3 n' F: z  }) M! B3 u- @      root /web/wordpress;
5 ^( F! ^  A% g" D( y      index index.php index.html index.htm;
8 m$ R, n" @) }5 s2 r& Y  
* |3 ~; v" A9 e% }" [' p. U      fastcgi_pass   127.0.0.1:9000;
7 J4 N: q; \; f) O# A      fastcgi_index  index.php;
: T2 h% O4 k# G7 t( a: Z      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;# f/ m- |3 t- `+ r  X5 ~" {
      include        fastcgi_params;
/ U. p! C- C7 `' J  Y! d      }3 g2 I1 y! ]$ c4 R9 f* ]
  }
2 d+ g8 T- P$ b& G' ~3 `' V- xupstream负载均衡:7 l2 x" V  f% A; p# Z! O; A( S5 F

4 b6 q6 i1 p3 a  u# A, A+ X" y  b. Q6 lupstream 52os.net {) @6 N3 l& D# @4 }; E! z5 J
    server 192.168.1.100:8080;  d7 T* y2 J, o" ?
    server 192.168.1.101:8080 backup;. N: K+ w, J) x7 F4 O
}% r, c2 Z- @& L  G+ ?

, R. T/ _* q/ T$ m1 F" {server {  \  Z, m2 d( y3 V) [% Q
listen 80;
( t8 v* w, t& c$ {- S5 k# E: O: ^server_name 52os.net www.52os.net;
$ x6 p0 [0 k) U& R+ H# \8 A6 W  b- ?) P# @9 O
location / {6 u* D* ^! {8 \
    ModSecurityEnabled on;  
1 Z& X/ i' `% z' r$ i  M$ k; K7 |% T    ModSecurityConfig modsecurity.conf;  
  x: ~* r! v3 M6 [+ I& I  n# U: f8 `" u
        proxy_pass http://online;
3 e2 o6 ^- j- D& `1 f* H        proxy_redirect         off;- S2 d( S* a/ C0 \% {: d
        proxy_set_header Host $host;! X! E+ `  H  i, p
        proxy_set_header X-Real-IP $remote_addr;
1 Y% M* D4 h9 t, `5 I        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
9 X+ S- O, y5 T$ p  j* B    }( e9 _# Y' V9 k2 u3 {! Z
}
7 [" S; W, l( H; i# E9 b六.测试
+ Z' Y- f2 f& M# y' D" Z1 T( {
" X3 J+ v2 R  l7 N' m! F( @% j我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
) l+ w2 K3 u- O
, z' u3 j2 Q- S# a+ N7 X<?php
8 o+ I  k4 e/ T* V5 _    phpinfo();    + M8 `# \( i; P3 \) W3 o: [
?>  c4 c; ~. x% c6 B, N
在浏览器中访问:8 R9 \. O) @) _, ?6 G% a# C
4 x3 P! ]- }! J3 o- J  R
http://www.52os.net/phpinfo.php?id=1 正常显示。. K! X8 [; c5 Z! ~5 h) H& {% O
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
" B+ y, s( x$ e1 X' }$ \7 rhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
2 A# W5 u9 g5 ]! i0 f/ H5 E: x说明sql注入和xss已经被过滤了
' E$ L' [6 O% @- a) g: Y, k
' b: @3 }3 a6 R4 P; V) z* [7 _8 |7 L七、安装过程中排错
. F. N# z) B- G8 i6 M" Q) y- m0 d* ~
1.缺少APXS会报错
& M$ g4 `: `# O/ g- q( p7 a  m  g5 M! z, w, [# P) E
configure: looking for Apache module support via DSO through APXS
6 M* t& |; Z# |& T3 pconfigure: error: couldn't find APXS/ F: |2 B# i1 p
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。; {0 q$ g0 c! y/ ?* _' P
解决方法:
9 S% u$ s' `1 J% \* x2 v
, H' s" Q6 A* m5 s& Ryum install httpd-devel0 L% G" G7 F! e! P
2.没有pcre
7 f  J4 B5 C; h3 h& g7 g$ a% J% B+ Y' I3 p. ]  t! d' l
configure: *** pcre library not found.) v, i) @% ?7 O4 o2 n
configure: error: pcre library is required- i0 W7 y# c  ]% |, [, k/ B- W
解决方法:$ q1 e% O" V" e* [* l4 [: |

! i1 J& Y9 F  T- Q# d; @yum install pcre pcre-devel
: F# z% r+ ~5 P( y- G/ b  d3.没有libxml24 h$ q, z/ {3 ?" A- E8 |7 I

! D: w+ p% ]. s  V
5 z  K8 y+ g1 ]. Dconfigure: *** xml library not found.
- Y# ^) f9 t2 \) {$ cconfigure: error: libxml2 is required
" |7 K7 K/ d2 v: a0 ^- b1 r& V解决方法:
: z2 B- v7 u9 E8 u7 R: W
$ I  E' O8 U8 F6 u- @yum install  libxml2 libxml2-devel5 W$ q* }1 W4 V3 N, f
4.执行 /opt/tengine/sbin/nginx -m 时有警告9 F1 ^: }) ]  R6 d: k. E' X- R
4 Y5 y9 c+ E* L6 b6 B3 F
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
3 X0 Z1 z8 {$ d. h* ]nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
# c$ k& ]* }4 V! P原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log3 }/ e: _9 e( E9 }/ y( X5 ?

' V% g! q4 t/ q' T1 E2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
+ d( m" W8 F# D. L! x2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"& f) W) l+ L. L( N% B4 A/ `& |
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!- d5 @8 C9 A" z" x
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"4 D& `, E. R! q) j! x  A/ S, ?
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
; i  H4 n0 p9 S- x9 e* n6 i2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
8 M4 @  S/ ?7 V' d1 V解决方法,移除低版本的APR (1.3.9)
# o6 R( X7 N! n: J3 x6 W
; }7 ]0 I$ C$ ~1 Byum remove apr
( i) }' \/ y9 r2 F5.Error.log中有: Audit log: Failed to lock global mutex
  [# d; x- e0 b3 g' l( }2 c4 K) T0 o# T# ~. ~3 ?. t$ o4 J
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     & |. \" g, m: o/ h
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
- E! V+ _; i8 ^/ Q- @  r4 \, v7 S解决方法:, h. T- P# m: Y
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:4 _7 ~. f; y9 @2 G+ D, D$ F6 a  X' B

! m: d' f+ W4 j, OSecAuditLogDirMode 0777
7 c, ]7 J9 V* GSecAuditLogFileMode 0550
( P  ~: T' w0 F$ M6 X4 z% MSecAuditLogStorageDir /var/log/modsecurity5 P, m, e9 w" q3 w7 \* R6 a
SecAuditLogType Concurrent8 ?  W" J3 }6 |$ O
参考文章:  c- R) n6 x2 L7 S
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX: C7 x' Q/ f' q$ V" ]) Q* _5 O
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-6-29 04:57 , Processed in 0.070180 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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