找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11695|回复: 0

nginx配合modsecurity实现WAF功能

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

0 H2 K7 O8 r. o% U0 Y一.准备工作
2 h4 b& A. L% J8 K% T7 a8 D: {0 m4 X- ?* l$ {& J/ {# M- b, u& T
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0# ]* n! C; m' a7 w+ E/ b7 Q
2 E9 T, e4 C: m, z! M
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz- D6 J' p* q! J* y4 H
  v8 ~2 _9 i1 D0 D$ E
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
4 G* \" R( m$ x9 h. }" T7 Y8 ]1 n. D& V. a, w, u8 n, E, [
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
' Z( n. {! q2 t8 l# a
. E5 F+ m# Z9 S6 I. O/ Q  G依赖关系:
) ]  M% V9 i: r/ O) x- Ctengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:, y9 C" T* {% |* w. M
, V8 [+ `+ O) I8 ]
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
! j) N6 x6 C1 C: R9 G! L- imodsecurty依赖的包:pcre httpd-devel libxml2 apr9 w6 ~7 y) o8 j) j1 A% S( s

  `& ~- A$ ?/ t0 h7 a1 L4 qyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
1 N/ J% g& m% r4 m# L二.启用standalone模块并编译' c# c. P  z/ y/ V
) ~) m/ u* Y: n7 U
下载modsecurity for nginx 解压,进入解压后目录执行:) l9 z$ P9 C) E7 `) S, `* ]

: d0 G* @% a/ w3 B/ m; f; n- v6 C./autogen.sh: [, k! J6 [+ L! c- @
./configure --enable-standalone-module --disable-mlogc
* p) u0 A. Z, x! R- x9 Xmake & N$ D! m+ D7 z) e+ k; N0 |9 I$ t
三.nginx添加modsecurity模块
6 z/ L/ n- h/ x' Y
" m* h+ |% W, ?( `7 L& E7 G在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:2 ~( O  w0 P4 n' Z! J1 B
0 w. Y  O, q& G3 x' @! w
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine( M& \7 f; V8 x5 ?, z/ r8 s1 i6 n+ d
make && make install
" ~* H' |( g1 A! b0 i$ u4 J四.添加规则
# V, ]1 n7 V4 A/ `4 n
# U7 b* e# t! ^" |modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
8 ]5 D8 D+ I6 ?0 u; @1 i, j
8 d, ^, q3 u* ?: x3 X" Z1.下载OWASP规则:
* X5 X! a+ U& f3 H+ p; M. H$ a  U. g5 F0 N( ?0 @
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
$ p- F$ W; @: H3 G5 o
$ Z6 v6 `* B$ V- d$ _mv owasp-modsecurity-crs /opt/tengine/conf/
& x8 w6 v6 c6 \; r1 y! n+ N$ h$ G: y3 P1 L7 G0 P$ V
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
+ ?/ W) l) F% f9 E# s5 n7 m2.启用OWASP规则:5 ^/ ^+ n" e: h. J/ a
% h6 u, |; U- s. B5 u
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
& ~. J9 k; F  S$ }/ u# M6 }
- |+ J. S$ B4 p: U1 M1 p3 n- d* o编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
; g* f: |' F0 X* r) K3 J2 v2 f2 D& Z1 Q) }; r5 G$ |/ i
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。& n. o# j% E, H+ P. G( g  D

) F: k3 c$ E% D' J, K3 sInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf3 H$ q" K! w2 O7 X' G
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
: @; Y- Z. I) T6 wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf: p  @& t+ {4 l' S
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf1 G# I, U& w& g
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
3 s( N5 x" j0 q; r3 kInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
1 w" C! d/ t& vInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
& t) R0 \) x5 }. n五.配置nginx
# \; O* f# Y; k% |" K/ X) r$ z' B7 g* X3 W; W0 v
在需要启用modsecurity的主机的location下面加入下面两行即可:3 h/ L: _- l4 i8 y4 t% C* p- H

4 Q/ H% o1 D7 x; mModSecurityEnabled on;  
( _+ d0 G/ {/ j+ D& sModSecurityConfig modsecurity.conf;/ c- w, T3 W  j
下面是两个示例配置,php虚拟主机:
3 z6 z" I: p1 U
% H$ o8 [$ k6 V1 Cserver {
* n( F% E; X8 P5 H3 ~/ X  h      listen      80;& A( G) s6 \" A$ g; V& ]( m
      server_name 52os.net www.52os.net;
2 l) m& d" K" q+ {% W/ ~     
  B' e  `# Y" R' Z      location ~ \.php$ {/ Y! ^$ O; c" r, j. `
      ModSecurityEnabled on;  
7 r) f4 M" r" t, f( j      ModSecurityConfig modsecurity.conf;* U* i6 h) [3 Q, f8 O

' B5 q4 D, X- L$ @9 r4 ?! Z      root /web/wordpress;2 q( \4 ~; y3 }: U( b$ L
      index index.php index.html index.htm;
  d* j3 L1 H$ P* F2 {; ~  / M8 C$ ^) X+ p5 \4 ^
      fastcgi_pass   127.0.0.1:9000;
# i% q0 w' G! T  I4 h  w  \      fastcgi_index  index.php;4 `( c; C/ f( {9 y+ T/ V5 L1 m
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;9 L( T4 L) X$ r3 q3 J; ?# l
      include        fastcgi_params;
* k- S4 P( a( `      }
8 K, B- o6 }$ P! F+ _$ L, n- G  }
9 |, s. v! d5 ~upstream负载均衡:
+ M- s; Z4 }# [
/ `' O* E" \' kupstream 52os.net {! I0 ^* u8 F6 z5 m# d
    server 192.168.1.100:8080;
& O, `& b6 F. B( z9 @" s! R; L    server 192.168.1.101:8080 backup;
+ Y, J* ?4 ?3 q}4 F- j# X! G# H, t( p( y

% i  K& c) Z8 g4 n/ }, Y& a. Kserver {/ c/ K- m- H3 {+ s* b
listen 80;
" v. H6 @6 N( R3 b& q* Z. `1 o8 ^/ Q1 Mserver_name 52os.net www.52os.net;
  m% M3 W; w* v
0 {4 J& d* R2 ~1 `8 D0 }location / {) D2 n$ r# U+ [9 |2 O- O% A- J) n/ V% l
    ModSecurityEnabled on;  ; _2 z6 _( ^; B$ _* y2 l0 |: u. Q& Z- H
    ModSecurityConfig modsecurity.conf;  * T7 g7 f' F7 V6 G' Q! {3 [1 R

$ ^4 W4 ?/ B/ ]1 Q: N5 r: m        proxy_pass http://online;+ K* T* t; U# y; S# j) i/ b! S
        proxy_redirect         off;
7 V& g1 O6 F5 r: G3 F5 y        proxy_set_header Host $host;
1 ?1 u3 Q% `2 ^* Y        proxy_set_header X-Real-IP $remote_addr;
* A! h6 l1 d" s, D. K; f        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;; R' U0 P  E! h$ e6 l! \% {
    }: y9 ?! L' O: C* ~- V7 A; ~% F- |
}
4 {' k* B4 ?3 Z1 l; a六.测试" F; {" _1 ^+ h3 K

# l+ H- ^* d0 S6 b4 S) F我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
) x! o/ b1 [% ]5 }5 K
# i: o1 J+ W# K% L9 p  l<?php  D, X* H2 |; g' R$ a* D, X0 z
    phpinfo();    , w8 Z2 }' b/ w+ P: {3 E
?>
) M1 M, x7 o- i2 |在浏览器中访问:6 r  ^. ~9 f, d  x8 h' a

/ [# }3 N* G8 |* i3 Y+ k9 \0 ohttp://www.52os.net/phpinfo.php?id=1 正常显示。
- p  q4 N) A& _- l; Ahttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。. h4 `3 L3 ?" A8 U( m
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
2 U) b+ b2 G4 V说明sql注入和xss已经被过滤了: O, I/ G- i7 T, m

+ v) U& M: }4 [! y0 x, e( a七、安装过程中排错
4 i9 F: R6 V' E9 w2 B3 W
2 I" E0 v9 e" L  S$ N" D1.缺少APXS会报错, B; T6 B' k# W) P, \
( E+ c  W* c  z9 m1 X" h! Z
configure: looking for Apache module support via DSO through APXS
" N1 `* f8 y7 Rconfigure: error: couldn't find APXS# x9 A6 z7 _( L, R. W4 R8 v
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。$ a3 o) {6 }1 v
解决方法:9 q1 {9 @, M: t: @, p2 G

& F/ O4 f: C) `& F# \yum install httpd-devel9 I1 t  G6 m( M7 o9 \/ _
2.没有pcre5 x/ E/ E7 g0 @3 x' F2 f: c

( E3 {! W" J! Jconfigure: *** pcre library not found.4 j: T8 r+ f- ~5 u* C
configure: error: pcre library is required, }# z) }- k+ V7 q! L1 }# d
解决方法:4 ~4 c2 o* i9 ?

8 M9 n8 W: n9 I3 Iyum install pcre pcre-devel) T6 ]) e5 P' o7 t7 w# c! b( G
3.没有libxml24 H; F, Q0 X5 h3 n7 q$ e

) n: C7 V* T& ?6 N: J& ~
6 X/ A5 m* D. r' A  f$ L4 Wconfigure: *** xml library not found.1 z/ c2 d5 G  ~+ |; ^# _) c
configure: error: libxml2 is required
5 i) g  P# I" I3 A) ~解决方法:
5 a6 N) ]/ m$ |9 n7 t0 G3 I5 v$ D6 F) a
yum install  libxml2 libxml2-devel4 v, F4 B& W! U
4.执行 /opt/tengine/sbin/nginx -m 时有警告
) t8 D+ o, ?: V5 C
" J- ~7 q& V7 a# b6 u* TTengine version: Tengine/2.1.0 (nginx/1.6.2)9 P* X# r  p0 J# S8 z" R( K  N
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!0 K% K2 I( F' i2 N# ^& ], n
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
& X6 v% a  a* W. {3 d
5 C* t2 x3 \/ V. @2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.# w$ o# a# \* S9 c8 _1 k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
  k5 e0 @- L2 w, g# t5 t) ~2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
6 m! r  Z$ Y9 h2 `  W8 ^+ y" \, u2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
4 Q9 Q4 S+ {; C- D5 f/ o5 b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
) g  v0 T7 P, ~& n1 B2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.9 z# X1 D* \! o! L/ M# S* [
解决方法,移除低版本的APR (1.3.9)
& g9 }; j/ w- K$ \/ V/ {' X0 ~/ o& k) j
yum remove apr
. a$ C+ V- `) c4 U* J8 ]2 h5.Error.log中有: Audit log: Failed to lock global mutex" ]5 X. N+ ]( j- [9 J4 |, _2 q9 w
9 ~5 R$ |# r% y6 M# H
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     - h1 O9 T- M& b+ \2 C
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]; r: X) Q0 t1 f
解决方法:! M( N* M% v# v
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
' e7 K3 F& Y+ }. J) R0 z$ O7 i% C1 R2 r! G) J* R# a1 K1 V
SecAuditLogDirMode 0777
  T& G, D7 N$ u  \" A# ASecAuditLogFileMode 0550. q) k5 f9 Z$ j4 E& c, ~
SecAuditLogStorageDir /var/log/modsecurity
0 Z# @4 J$ ^7 m( p) m* t5 P/ [SecAuditLogType Concurrent
: `+ M. |/ i7 a5 d参考文章:" v1 c1 \$ J8 p0 b. G
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX: v5 T$ P9 u( s; S' P/ s
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-6 23:05 , Processed in 0.069625 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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