找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12357|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。" m: d0 B0 Y" P4 h
  H8 k1 ?9 X) h
一.准备工作* Z# x$ c! _6 p/ d3 k! y% }3 g* O; C

6 F% C$ K& `5 P+ [3 P9 i6 x7 q系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
5 l9 @  {* h* w7 q7 [% @
: g5 l' b1 q5 |+ e- E! F8 c1 }9 X; E; F6 {tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz3 d3 }. v. S0 {
  }; m6 w, h1 S1 A  x
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
" e& \) r' d$ z1 o+ A2 b7 j- Q4 `0 E$ }' \+ |* `2 N0 x5 s
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
8 _8 N) u2 Q1 Y5 v8 f* U, q' v+ e: _* ]. O5 o
依赖关系:: k+ p' G. e$ ~$ K* d
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:3 T, o3 ]3 O+ T7 c5 F7 S

9 p6 B$ J- f+ R! i7 C' Eyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel6 d- N5 M5 b4 Q( |% A5 b. U2 F. O
modsecurty依赖的包:pcre httpd-devel libxml2 apr
2 f" h9 D7 F+ J; E  i. i5 z. J/ J: _" j2 ]5 W& `: u5 y1 @2 y
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
- n* J5 R/ I7 B* G" f二.启用standalone模块并编译, `! O+ K3 u9 U- ^- s
4 \& j8 d4 K+ t$ Q0 E* ~
下载modsecurity for nginx 解压,进入解压后目录执行:6 ^1 k9 K9 Y  q' H
5 ]+ `- Z( W% S; c
./autogen.sh& K) u$ m' {/ n1 T% E2 H, |# x3 k
./configure --enable-standalone-module --disable-mlogc
& O4 z1 D2 ^6 s; \- f* a' M; ?make
/ M9 k+ ]0 O- W: a% n1 F三.nginx添加modsecurity模块: O; T5 M) Z% P/ T7 b* ~8 f  V
+ O& e+ T  ]. |* r0 _- l' P8 \( E
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
2 d0 ]0 p: u: _( ]# @2 }
% b0 \7 V: e4 h* v, U5 h./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
) @$ h* w" d: y& C9 i; Mmake && make install
! ]; `) \4 O0 {' O1 n1 x四.添加规则
7 g, }. ]' P! m  L9 N  }: J9 f; i+ c3 @0 u0 S8 ~' O% W: |
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
. d2 B/ J- J. A7 e$ }  @
3 o* u% s1 H, M9 Y( |1.下载OWASP规则:
' N3 |& W5 C/ ]% n1 m8 ~% N* b0 s7 V
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
5 @: e* z% _4 b# i$ b) i/ j% t( |3 w2 K4 E
mv owasp-modsecurity-crs /opt/tengine/conf/( @, W7 E( T- Z4 J! n
4 l' R( g0 V) o8 @0 n7 X/ Q
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
% |4 i! p" Y; u3 k$ \  `2.启用OWASP规则:
/ D$ a2 `) n2 A/ f& N5 j' |* A# x# ^; O4 O1 \/ n" K: n. \- z: y9 r
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
9 d4 U8 a0 D) f1 g; ?. _* o( b) g+ Z. U- V! `' c: O  a
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on: C( m8 q; I5 L& h7 W( L

" |) M! I( M& Rowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。! p* O2 ?! D* s( T/ q1 G* \! x" G+ _

: N1 u' {/ R* ^  o( pInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
) v6 K9 r* [( }6 c9 XInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
( l4 {2 @1 `8 d' K7 c1 TInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
0 M$ k; F) z9 C* j. s8 mInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf& h# X% f( C& J6 }+ K
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf* |& W- w% J6 d; K8 T
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf: e5 ?! d2 r& M  O8 s" ~* A4 G
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
$ K( V+ p# N2 O2 ~$ Q五.配置nginx& p, W& i5 U. ?
% t$ b; C& x9 h
在需要启用modsecurity的主机的location下面加入下面两行即可:
/ K  ^5 T5 @2 t0 G& W* B! @; N# n& l3 e, m5 w' s. i: t
ModSecurityEnabled on;  
0 ]/ M4 g# c8 P$ o' WModSecurityConfig modsecurity.conf;8 l3 x( [" W( r  Q9 H/ ?
下面是两个示例配置,php虚拟主机:/ ?5 Z5 F) O! P/ E3 R$ W

/ c1 ]7 c/ ?3 g' _& u3 ?' B3 h6 W5 _server {9 q4 |0 j3 a: P
      listen      80;+ e  i7 U: y7 Z0 r6 Q/ s
      server_name 52os.net www.52os.net;
* z$ m" u6 K3 x4 g5 G     - `$ W3 Z9 L: G# \
      location ~ \.php$ {
% V& {( U3 f4 L- G7 Q( R      ModSecurityEnabled on;  ' L: |" f2 ^/ S  z+ R9 [
      ModSecurityConfig modsecurity.conf;$ h0 T. B3 @6 K- K8 P% t

+ E+ I8 t: g, {6 n      root /web/wordpress;) O8 C/ s) A" i
      index index.php index.html index.htm;' ~1 q3 j( |" E3 I$ {( ^
  
6 Y6 H7 A* f' r) n* ^, n      fastcgi_pass   127.0.0.1:9000;
# ?) h! L% M, T# B5 j      fastcgi_index  index.php;& e" ~7 z7 @* l. Y; u
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;; o5 n# r, N/ V2 A
      include        fastcgi_params;5 d% g6 z: E  z7 u) y" T+ z
      }
, }7 X3 k3 n& Z# B  }
# c" [% u% R1 }% }, D# P" wupstream负载均衡:7 |' \8 f. ?7 U  }# G2 f+ u9 w

% ~6 T% u3 K* M( Iupstream 52os.net {
2 o0 Z7 n% i' t0 I    server 192.168.1.100:8080;/ I: C% F7 F; q
    server 192.168.1.101:8080 backup;# p0 v" j4 H/ w. N, g0 B
}, @$ }) ^$ o, F3 Y8 ?4 k, {6 _( u

4 L! [! u8 p+ j4 q$ S9 Oserver {
% q$ e/ T0 K: v+ M- \listen 80;
1 H8 h; J, n/ V0 y' `; cserver_name 52os.net www.52os.net;: {5 G! K3 C4 ]# |% n  ~3 x( p  o6 E

' h( P% a. T3 H/ xlocation / {* ~: g) Z' B! V* S6 V- C
    ModSecurityEnabled on;  ( `& K, {( u1 ]' H0 ]1 m% S
    ModSecurityConfig modsecurity.conf;  
) E2 ]/ I( b! A2 N7 m2 G7 J6 ]$ b2 Y0 n
        proxy_pass http://online;% s, A. B# u( C- _4 r
        proxy_redirect         off;
7 S7 g, }! K2 a& H" H) t0 v  R+ U: A        proxy_set_header Host $host;
2 _5 z' H% u/ h( ~+ @' F        proxy_set_header X-Real-IP $remote_addr;, B' [- d/ w( V/ `# C# }/ R* M2 R% J& h
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;; r" M7 ~: @! z9 w% I
    }
" {+ T$ k5 P' E) J. a  Z1 \  R* Q}9 _3 e/ `( o; A# o$ @* R
六.测试& N3 S2 Z' f8 j
3 j" k" W( M! o4 D
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:6 w- d2 Q, F$ N$ Y6 q
( z/ b  C4 h  L
<?php
  K; o: G/ {" x8 B; q1 |    phpinfo();    # ]% `5 s. J' W- q( R
?>0 Z# }7 c" q+ p  U
在浏览器中访问:
) _# T( m* D0 w5 Z; C3 v3 h) G/ C/ u1 O) m; i% H, i& v% G7 t
http://www.52os.net/phpinfo.php?id=1 正常显示。  e, _6 g0 m  `
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。+ L# w1 G' V: O- s1 J; b
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。5 j$ |! R& s) d1 r; `) y# T
说明sql注入和xss已经被过滤了
, B" h$ n# N' u/ f# z, ]' U9 a& \; H6 b5 g
七、安装过程中排错
' _% J. ^) K2 ~+ D
5 ~* ]5 H: l) {. c( S1.缺少APXS会报错% n2 t, Z, {# [  F; K
- p8 J5 z+ Z) I9 I. M! s8 Z
configure: looking for Apache module support via DSO through APXS
2 g1 z2 \8 h8 {* x" oconfigure: error: couldn't find APXS# W: `6 m: Z" e
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。& M, B  v: |, o8 c( S. p  ^
解决方法:
6 {  e: X5 y: t$ t  d- k" v& O: q" T) x2 r4 w
yum install httpd-devel& c' O. o8 D0 h2 b( Q- K# N0 S
2.没有pcre  Y. t, ^. ~# K7 ^4 T# ]) g9 C! Q

) `7 G. |  U3 A0 m. j+ L, g- `configure: *** pcre library not found.( `1 @2 j3 r5 g+ E  c. y' t. E
configure: error: pcre library is required, l. H( {( p1 p. I. {
解决方法:
7 E$ n5 I: V7 m8 M5 U
( q  Y: x  B+ y) P7 I  [+ g1 syum install pcre pcre-devel" E. P& G' v. J
3.没有libxml22 V; n; {  G7 X- t* {+ S

$ d1 N7 a2 Y' ?/ \7 `/ @) k4 ^" p0 D, t) Z4 x; d! x
configure: *** xml library not found.1 M9 |0 A5 x7 q9 Q9 M7 g
configure: error: libxml2 is required
0 r8 V+ [; w2 p7 n2 H6 {解决方法:
6 N  {3 o( W1 `# u5 i& g% D: j
- c3 f) I' \) j8 zyum install  libxml2 libxml2-devel
% t+ ]' {- @' o  K4.执行 /opt/tengine/sbin/nginx -m 时有警告
& U$ l6 K4 `" X# @  g7 N' I0 a
. {0 @2 `4 k1 d/ h5 A! w% u. }Tengine version: Tengine/2.1.0 (nginx/1.6.2)
. ^; H. z, T1 `' p1 }: F: anginx: [warn] ModSecurity: Loaded APR do not match with compiled!
$ _& \& i( J" g& L. G' f) F原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
5 Z+ @* O- y. m7 s2 P
: U2 g7 B* ?# i& D" R2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
1 n' a2 [5 t8 M8 u4 |4 Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
* S. p4 P4 W: ^) I2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
! D) ?( ]9 s1 h; N& S8 a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"5 P, x' s' E" H/ G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"4 g# |  Y, ?( J0 E1 V  r/ `
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.8 D' F& S" {- E3 U7 u& g
解决方法,移除低版本的APR (1.3.9)
* L1 O; c, o2 q& C4 d: ^. Z) y+ X0 [+ ~
yum remove apr
: E" K: d" ^" W$ q% a6 d5.Error.log中有: Audit log: Failed to lock global mutex
3 j  T. b! E& f8 z% n- h9 ?* V& a1 p1 j& c! R
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     + h6 l6 G  l/ P0 M* }/ p
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]& _& @$ R; x; z7 a0 z
解决方法:
$ D! l0 i# l& r1 v$ Q6 i6 u% M/ X  N编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:1 m$ R$ @; A+ P

4 u* s) }" m5 K( W3 _) A2 gSecAuditLogDirMode 0777
; d# s) l1 j2 Y6 k. H+ R5 CSecAuditLogFileMode 0550
9 q$ M. k9 A  z; nSecAuditLogStorageDir /var/log/modsecurity
+ t! V/ h: R3 USecAuditLogType Concurrent; u: ~+ V$ G' F- N% k3 I
参考文章:
3 K1 y4 `8 o, u1 Z$ z' w2 b# S6 Khttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
9 y5 ~' E- F/ Vhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-30 11:03 , Processed in 0.085250 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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