找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13354|回复: 0

nginx配合modsecurity实现WAF功能

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

" x, b# K+ k2 V) X$ m) W# g: ~* X一.准备工作
0 _* C" g  H2 o$ i) ~: ]3 |$ {9 ~: A2 A( C9 u9 T% N
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
% V- Q) ~5 _) A1 o: v: I  i" m: E
6 n1 \' V0 e$ z8 o/ v" D9 Vtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz1 t# Z: I% I2 W. w, i

$ X. Z5 k) O1 P2 O, ^5 b# Amodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz9 y' Y) c4 f6 P* ~+ Y( @) ]

1 y5 u" U3 k! M" Q5 OOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
" @5 y0 x, m' B" S
/ X" Z6 s3 ~" |2 W+ U, w依赖关系:% G. Q% K5 Z6 l* T, x* }( J- G
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:. R" S  i) [' n+ ]
$ c; r: C2 m$ ]5 @% {9 B% g
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel3 s. X3 K. U5 d) T/ _. H9 s& L
modsecurty依赖的包:pcre httpd-devel libxml2 apr; V( Z. v- J1 i( }+ W/ l
2 X0 \2 A( _2 {$ b$ D* O! j
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
" A0 `7 u: ]( M* f二.启用standalone模块并编译
# p1 r1 ^: C  p9 U& p2 C. f
: X8 J8 M" n0 A% K. ^8 Z) @7 M下载modsecurity for nginx 解压,进入解压后目录执行:5 Z- G; j1 {7 C! A! n7 o+ d6 w/ G
5 [4 ?( w7 P0 r* ]
./autogen.sh
5 E- p  k! n, U% J& x6 l./configure --enable-standalone-module --disable-mlogc$ R. P6 c0 U, Q( {/ ]* h
make 4 H4 l3 N3 \' H9 b  V5 k& O+ D" Y
三.nginx添加modsecurity模块
4 `5 v; Y. H2 }+ u1 G/ s9 s5 Y8 {& f; v+ O! S8 I  A
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
# w! u' c+ K- x2 `+ S5 f
$ y! ^2 a1 m! \! {7 A  I$ O./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine7 F8 E2 Y  @) P! y# {
make && make install) T8 C1 Z9 d9 T2 i+ @/ _% U, |3 A/ P; u, T
四.添加规则
! E3 s: X8 j/ Q0 a) i* O" W' C( p  b8 w- z5 w
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
/ ]* p, d6 s6 z: s& f% R" H; w8 _
# s6 K8 }1 {# I1 i2 s1.下载OWASP规则:
: w: `/ j6 f( `, X) X1 G7 X
  O2 M2 b: |' R7 I' wgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
% N5 x' J2 g- ]8 O! Y2 @$ x% @
! a/ y) s/ Q! H/ s3 b, ^6 R2 rmv owasp-modsecurity-crs /opt/tengine/conf/  E: w9 a; w3 I
: a$ l2 ?! x. i0 Q: k* z; e
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
& C: Z8 S* K4 S1 }: n7 a7 d8 h2.启用OWASP规则:) r# k6 J( D( w0 A! c7 @" R+ f
! o: G  [$ H& D& o, Z4 N* @, [
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。* E2 b) @0 H: s. d

' P9 x% C3 ~( P/ x编辑modsecurity.conf 文件,将SecRuleEngine设置为 on; g: q: s( J# S) g4 }+ u1 g

0 C7 y0 Y0 l4 A7 S+ sowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
0 _, B* S7 o' e( w( {% Y5 ]
# O: v6 v" j3 M: tInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
$ s) O# W! d% b2 pInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
7 |9 x$ M5 L% l5 k' F1 WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf6 j# B% `4 f6 j1 v/ T0 ^" Y3 F
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf- X% q( T& o5 f* ?
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
' w3 |/ J3 H* \% ]9 [: Y: xInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
8 g+ K0 n9 L' Z- t, }Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf; a- H1 n1 N8 G) a
五.配置nginx
% `$ D6 Z' J9 ?2 L% E( U6 |4 q& S2 z2 A; M- K
在需要启用modsecurity的主机的location下面加入下面两行即可:, \0 j5 }& u, Y% o

$ f9 Q/ b, ?" N; ^ModSecurityEnabled on;  
/ M' j: b! {$ ]ModSecurityConfig modsecurity.conf;
. H0 _8 P/ D3 e" `. s1 b下面是两个示例配置,php虚拟主机:/ B* e2 r& {, l5 k! o1 }  J
! c3 Q- e7 Q3 V
server {
2 d* {+ |/ D1 X1 @3 i      listen      80;5 L$ Z# s) T8 a9 c
      server_name 52os.net www.52os.net;
1 R0 O- h1 c7 }! f( L6 l  P( f. D     5 P1 S# j8 r9 u7 O! k8 o( ]- i
      location ~ \.php$ {+ P! T3 m6 P- W6 n1 X: S
      ModSecurityEnabled on;    |% k* _1 S+ G6 _# _3 O
      ModSecurityConfig modsecurity.conf;
% L0 W  H3 ?' J$ k( ]; P1 B5 l( {! J4 u+ ~) H
      root /web/wordpress;
& |$ t, M: p# e8 V6 C      index index.php index.html index.htm;& F. O, R; s1 v' [
  
9 w  c3 Z: T, h" |      fastcgi_pass   127.0.0.1:9000;% M  [9 Q' M+ Y$ l! i
      fastcgi_index  index.php;
' E- f% o8 s9 o, V: b. D# c: w      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;4 K- B4 B- t2 h' T7 B
      include        fastcgi_params;
' o; {7 O8 }3 I; h6 ^      }7 t1 S) r4 N* a5 f, N: B8 e5 Z
  }
2 J0 n2 i# u* i& ]8 ~upstream负载均衡:
" M% q2 H4 }5 e( f7 x0 c8 Q3 W2 ?
6 S8 W2 B# H4 c  Supstream 52os.net {
: H- W* }+ y' p+ C    server 192.168.1.100:8080;
, s7 F3 K1 c  R& q/ b! C4 L    server 192.168.1.101:8080 backup;' X7 O% N$ k) s2 Z, s( g- o7 x% F
}
3 i' t' _# G9 g4 C. I4 Q$ t% {$ F' }
server {/ x6 U' o4 N4 r0 n7 b
listen 80;& i+ b" E# h/ K( N
server_name 52os.net www.52os.net;# M! ~' D, V* O) ~) [

- ~% M+ O+ J" |8 ulocation / {& ]8 o8 [8 G( y8 j7 i
    ModSecurityEnabled on;  
' a/ O0 g# }6 W9 L; ?" Y    ModSecurityConfig modsecurity.conf;  + b; L0 P: N0 l7 ~5 j
  E4 Z! C, p$ N9 b1 I# O1 R2 M! f
        proxy_pass http://online;7 ]. k/ L; B' T/ O( @- [. {, p
        proxy_redirect         off;" K+ ?: G9 u7 S+ r4 A# w" P6 m" y! V
        proxy_set_header Host $host;
0 Y# i$ t3 r7 \, `& D  ]) Y! F        proxy_set_header X-Real-IP $remote_addr;
* n- z3 I$ ?# P  Y! g        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
6 s9 d' {8 E6 [# U6 X    }, y, w5 x$ ]/ f, A  j  R
}
/ J: B3 p- V. T2 X: k! y2 ?六.测试9 x- n- B8 p, k+ M: f
* Q' b* f4 T1 c# P! |$ ?3 \' M
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
# w( y* D  Z( ^' n$ S
. X; l7 t3 c4 k1 W: b" ^$ O<?php! y, ^5 o. p9 o! n1 l5 ^
    phpinfo();   
) p6 J9 j4 T3 A. B# r?>
3 v' X# R3 S8 G$ D+ [4 M3 g( J" ^在浏览器中访问:% D# P9 B* g6 w  l, P0 d6 F
4 ]3 P# d+ j& `1 |4 S  [
http://www.52os.net/phpinfo.php?id=1 正常显示。
' [9 m1 O8 ?) x7 ]* b5 C' b" w( ~8 ?http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
+ q1 f+ C" v7 E6 E) fhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。0 Y! M5 X5 |! [, s: L9 S  E
说明sql注入和xss已经被过滤了9 w4 t# s' H2 e2 i( G- E5 w

$ @+ I; o$ {; r七、安装过程中排错
. _% r' q) @/ l: a  b% F4 V5 c* p$ Z
1.缺少APXS会报错6 u: C7 z  `' t( z

8 }3 Y" q5 x9 T* t5 t4 n) \. xconfigure: looking for Apache module support via DSO through APXS8 t" m$ `/ v* r/ @( U% ?- g  {. t
configure: error: couldn't find APXS
+ S' d6 N  J# m/ r! V+ capxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
4 F/ d) c9 @" x0 u. e% }$ q/ T解决方法:* H! E1 I) {% n- Q: r1 f1 K, ?
3 k& H. w. w* J0 y( w
yum install httpd-devel
# }' K8 n9 y, d6 S2.没有pcre3 S6 ^5 U  h2 y/ w6 j# I

) @5 h" u# @( p) f" q  x! econfigure: *** pcre library not found.
# O) S# ^/ f, [$ Kconfigure: error: pcre library is required' `5 I' j% c! ?2 @8 m* u
解决方法:; v" }( W) T# L9 R1 q" c# O
1 @$ V! I4 w6 T
yum install pcre pcre-devel
6 Q% v6 Y" `6 T4 t8 D% @2 i3.没有libxml2; D. v" d+ \. E

# x7 L/ `0 }# `% o, g
" A2 N! q7 @6 o( x: @% |1 sconfigure: *** xml library not found.
) z& k( C% J) Xconfigure: error: libxml2 is required
9 P, }- H0 D3 L解决方法:
' ?) P( u: M' R6 r! X7 \' p8 j+ @9 O2 K
yum install  libxml2 libxml2-devel+ k) C9 O7 g  T3 U$ H% O. h* g
4.执行 /opt/tengine/sbin/nginx -m 时有警告
! g3 `& F* ^5 s) R8 C9 W
& }) e+ Q# d4 WTengine version: Tengine/2.1.0 (nginx/1.6.2)
8 m9 N/ \. a7 B% y1 q( n. Q/ y2 \nginx: [warn] ModSecurity: Loaded APR do not match with compiled!+ E0 H" S! ^8 Q* s
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log5 P, t5 `& {/ X3 W7 c; ~/ R
9 |+ U7 O' R$ o8 V' c
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.8 B; g% w. n8 [6 h2 `7 {, T% {1 o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"& K  Z6 P- h; O9 d" w1 a' |; o: u1 ~
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!. k% p" ^8 T0 e' @9 h( B
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
: G3 V* \2 g' w+ p4 {: e! o  Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"2 y) ?1 g; Z0 m( ^
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
% h& |7 L0 {/ P- P解决方法,移除低版本的APR (1.3.9). a+ C, e- o9 b; o: }" P1 W# B

8 \' v1 w2 N  ?, ^0 K' ?1 X2 i# N! Myum remove apr
3 E( v+ F5 K9 K2 ]! ^8 p) ]5.Error.log中有: Audit log: Failed to lock global mutex
$ Q4 F/ R" \# N4 c. P
) a1 ]' c9 T5 @# X. S2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     & k5 V1 l& @' U
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
- K: V5 b/ w/ N9 |( J" d  ~解决方法:" L# X5 Q" ^- K7 f
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:" K, [% y- q7 u+ X! }
6 r/ m9 Z* _8 O; L
SecAuditLogDirMode 0777" a; r/ |- Y! b/ ?  m
SecAuditLogFileMode 0550; J5 t1 V# }2 ~6 r
SecAuditLogStorageDir /var/log/modsecurity
- h5 c( [- ]% y) j' ~SecAuditLogType Concurrent
/ g3 P; U! l+ e0 d2 ^; L5 C' f参考文章:* t+ b4 W) U0 @" v* Y  o+ ^
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX  r9 k  y% Y$ w9 I; h4 }
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-12-6 05:31 , Processed in 0.067937 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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