找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11827|回复: 0

nginx配合modsecurity实现WAF功能

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

) Z2 F: A8 W$ u  ~$ N一.准备工作5 U7 ^( p5 r) G% Q: R7 K
0 E, \! B: h$ f* r7 b+ a( Y
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
& I& f& c: G) u0 |9 k
, H$ x% h9 S& o+ D9 vtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
7 f5 N3 `. ]8 Z9 H% j
# s5 w  W9 ?5 \( |3 j* G- h2 cmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz) N8 A1 |. S- Z) n
6 h7 T5 s% U% O
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs5 R0 k. N4 `% S) m/ O! g( J1 u

8 y; \! m3 [" ?1 O0 Y  S依赖关系:* ]& S3 }# b# t# y) K
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:6 i  m4 M) S2 e/ U/ |
& [$ Z" l, ~7 L& y
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel7 {) Y3 p  M0 R5 E
modsecurty依赖的包:pcre httpd-devel libxml2 apr
, D; l3 `/ s0 U7 a
$ s1 Y  b, _9 |7 w  @6 y5 Zyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel, \- }8 v' x+ V! o6 x1 `
二.启用standalone模块并编译1 [5 d  e0 V7 z  e% J# z+ A0 }

( q: n5 R& m9 r& X, O下载modsecurity for nginx 解压,进入解压后目录执行:
' W7 V# `2 |+ Z2 v4 J* g6 |6 a% t# u+ e
./autogen.sh
) Y  \( W, o9 Y3 l5 O  S2 V. _# z./configure --enable-standalone-module --disable-mlogc
! `9 e- v; X) g: L2 F( Zmake
7 z* H% O  i1 m. O" o三.nginx添加modsecurity模块: {1 z6 A2 G9 d& Y0 i% Y5 t
6 @3 l0 ?+ |5 p1 n6 I6 M( M
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
' a. A" P9 |: I$ _, q: q4 k2 N. l  Q" s) H
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine8 ]2 ?/ z6 P5 |* b
make && make install
2 t% A+ w) I/ Q3 y四.添加规则9 S4 o. g, V# E$ ?" k' G

' O2 k" A- X4 f* jmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。: @3 B1 ?2 H% x! q7 Z& g6 ?/ b
5 A3 }: A. e+ q+ q
1.下载OWASP规则:
0 t: @$ [" j4 U! t0 X- {8 s( `+ K2 r4 w& ?$ G1 H) V/ S3 O
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs6 x. a* V  O" r1 o/ V/ s

3 P+ Q5 P  E: T" r. f" ]7 Rmv owasp-modsecurity-crs /opt/tengine/conf/
& \4 A7 }7 o0 I: _6 ]
# ]# X. D0 [, G# b1 t+ y$ Ncd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
: W8 Y9 R7 R" }( y& P2.启用OWASP规则:) A8 w+ v  T5 c
  K$ A( h- O* S0 i5 e& c/ R
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
& ?, N2 U' S* B. o( g4 E0 M# ?& _! z6 o) W( v; ]( D
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on7 _5 j: H- B' a7 x

+ b3 g- m* i0 Z9 Iowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。& c4 Z  d. \# @5 j+ p! C' Y; ~+ }

- N( i- D8 L1 b9 G' QInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf% m% A& z7 [4 g# l/ j- b
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf8 E( N) M4 f' A  c
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf% O6 z2 p+ ?1 W1 r" p* |
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf- F" e0 Y- B5 Q, _
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
- d  E9 M0 G" a1 hInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
) R. Q" ^3 R9 k$ fInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
# r+ g& r& R! A7 D1 ]" ^* r  ~五.配置nginx
0 p) P4 K8 ~# u! f/ f' @8 r1 Z7 g8 j# Y" \0 _% e( {* _0 j! r6 p
在需要启用modsecurity的主机的location下面加入下面两行即可:$ q2 C; L* g4 G8 O/ \( D
9 D  m' ]8 k( J% {
ModSecurityEnabled on;  6 a5 \/ b9 @; P
ModSecurityConfig modsecurity.conf;  ~- K; [2 `; w/ r1 L  j; F' A
下面是两个示例配置,php虚拟主机:
: G* b$ ~) O5 Z  P2 ?2 S3 Z
+ g. Q; p: x& Z3 m  m1 \server {
, T4 [) n1 q& x4 M( _      listen      80;
2 K: K& |" h% U0 t      server_name 52os.net www.52os.net;
$ z& ~0 G% ^6 B) L6 {     # f% e7 j- a3 G4 A( Z1 s
      location ~ \.php$ {- q$ l$ n, z, m0 X$ U! o% k
      ModSecurityEnabled on;  * u5 s3 @* I1 Q) X& Y8 T
      ModSecurityConfig modsecurity.conf;
! K3 H  t( `, l
5 S1 Y$ S) L3 `0 }- Y      root /web/wordpress;
: B2 U) ?; Q3 X  H      index index.php index.html index.htm;3 i5 }7 |" }+ W  ]) p; X
  " o" v5 g4 S% A4 E: ^1 x
      fastcgi_pass   127.0.0.1:9000;! P, U& g9 P7 E2 V* u
      fastcgi_index  index.php;% T4 V. |4 E4 c5 L, {2 n8 [. M- L
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
9 I. _9 k- r2 P5 I      include        fastcgi_params;
  I  {; ?1 b6 [7 \9 I1 R      }& t+ T) X) [7 K) k  m/ W- s
  }$ c& E# _& j; C
upstream负载均衡:' x- m+ M5 O$ c  V3 Z
; A& @- V* [9 J' G
upstream 52os.net {% ]! Z4 J$ ]7 ^  o* X. E) Z
    server 192.168.1.100:8080;  X/ W  a5 k( Y* N" n% h
    server 192.168.1.101:8080 backup;
/ B9 e, g; [9 b! }; d}
2 y" o% s1 v, P$ |' J1 h+ z: Q
5 M3 [/ ^( J2 A4 X" q" H9 Jserver {% p9 a! ?; W: B7 E# g: g, z; Z; }
listen 80;( R3 f; X! e7 H: U( u8 c/ p1 x0 G
server_name 52os.net www.52os.net;
* C8 f. l% a; J) }! S$ ?
6 s: Z' y  F* e6 Mlocation / {
% P- C. l0 y# y7 C" Y4 A( W    ModSecurityEnabled on;  
/ V+ s0 @0 d. D% p6 N5 S( t% p    ModSecurityConfig modsecurity.conf;  & T$ o. e, s3 R6 I% `  v

  D# Z5 _* T4 M4 g        proxy_pass http://online;
0 p. N+ T& T/ N9 [( o        proxy_redirect         off;
  P+ s4 L6 B+ Q4 L        proxy_set_header Host $host;- d0 f5 j( m6 I
        proxy_set_header X-Real-IP $remote_addr;
+ m8 U# F5 X0 A        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;" v% k4 d$ w* e. L) O2 J0 N: q; n
    }! |0 \" X! d! ?) F/ `3 D
}
6 V9 w$ h( h( s5 o六.测试
9 D7 \  t& F8 h0 H% a# ~% o' A: |  A: B( \& v* A  u
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:! x8 X3 D9 B; |' l% i1 o
/ c$ C; X$ Z: G' q
<?php
( l9 p$ p+ ~8 H5 c* X    phpinfo();   
4 ]" x, }- s$ Y# X?>2 a. C7 ~9 i- e, i5 H+ y/ i
在浏览器中访问:
! |  n6 T9 X$ ~6 e. d# J9 v- S% B& |6 s/ J: o5 b& O! n
http://www.52os.net/phpinfo.php?id=1 正常显示。
% u$ l% Z/ t# H' e4 ?http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。$ _8 n# }, L4 q' N
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。# \/ e$ v" g6 s  m; j
说明sql注入和xss已经被过滤了
  ~% x1 y. W6 Z" H! c, [2 N
5 I# e1 N3 [  H6 I% j" U; K0 |* w七、安装过程中排错/ `' b" E) p, i5 O7 S" |

; T5 F- `' W4 H2 n9 v: w1.缺少APXS会报错5 i* f( I, O. _$ W, J7 Z; F: i+ @* z
4 M5 M: i1 J. J! m
configure: looking for Apache module support via DSO through APXS
) T! e& F* q0 b$ O5 {4 xconfigure: error: couldn't find APXS
( \! r. f3 P2 d9 y, p1 e% c2 tapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。& ^$ N) J: Z( I4 B/ c9 {
解决方法:' r4 v5 a7 ]) ?. {+ M9 n$ C

0 G( i  q% N4 Z/ B5 }! z9 {0 m6 [/ Eyum install httpd-devel
9 t$ R' t/ s7 Y% v5 \3 z2.没有pcre
% x7 L3 N7 A4 `+ _
  ^4 _5 ?6 @) Gconfigure: *** pcre library not found.
3 l  j! r( C5 e5 H  y( n7 J" v+ ?configure: error: pcre library is required
1 k  f+ ~2 [: {1 y7 `6 P+ Z# u, g解决方法:
$ l1 c- T0 L* z0 d% a% G! t" U9 g- O2 _# y0 q9 s* i) G  m
yum install pcre pcre-devel# |. c8 l4 D$ G3 z
3.没有libxml2  l5 e  [' C, F) U% Q

8 L, f% @- S1 C
# M  x* r! R4 qconfigure: *** xml library not found.  f3 B0 h+ Z+ Z# O
configure: error: libxml2 is required! ?. h+ K# j2 O& z. k
解决方法:# c2 X0 _$ |9 z& a
! v2 _/ Q1 D% H% d8 D
yum install  libxml2 libxml2-devel7 n! A+ H; G: R5 ~& L; B
4.执行 /opt/tengine/sbin/nginx -m 时有警告
2 I2 _1 `! }, I! Q& e, l3 H3 v1 n+ Q9 d; R" p5 ?
Tengine version: Tengine/2.1.0 (nginx/1.6.2)7 I$ F  z* w: ]1 D# p; b
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
% W, {! r# N3 d' T& ~( L# S/ B/ ]原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
9 }  s$ R; @6 L! e, x5 N) I& D( G: f: J
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.. C% Y( z5 H( J1 N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
# t5 I* {4 `* G8 g4 q3 e) E2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!7 ]* |- @6 ^/ U* {% |6 I
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
7 j' W( G; b5 B4 V+ \0 K8 N$ W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"' s4 @2 l0 m% J" [  M" j
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
; P8 S6 N4 L) ~" k; D" P解决方法,移除低版本的APR (1.3.9)
; }* `/ F' _( ~( P  J: M6 [: r* q1 h) \
yum remove apr% k( m( `! l( U" T# M; \9 _( Y: E
5.Error.log中有: Audit log: Failed to lock global mutex
4 t! p: I$ j% d& F3 s7 a, T# l! G7 \" F; z' ]
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     ! G4 K% m1 J; R* n- X0 e( a
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
( M$ |: c+ H( P解决方法:% U" `$ a4 x: ?  y. }3 w( @+ r7 V
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
+ x# x) `6 G, l; C/ e4 |) _4 @% o0 k& H) I: R; S/ \& q& y% c) z5 i. K
SecAuditLogDirMode 0777
4 h! \" M! y4 j- WSecAuditLogFileMode 0550
( C* s: w2 a& V' J5 FSecAuditLogStorageDir /var/log/modsecurity
8 g- v1 k; x* X: v! t% _SecAuditLogType Concurrent
" a) j' m7 y) D$ M8 s参考文章:: ]- p9 A& H% a. b. u; y
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
; e! j, q! ?; [/ vhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-17 07:36 , Processed in 0.063958 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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