找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13039|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
- H( n' t" Y, \* O
0 i; j0 r( g7 g  T一.准备工作
9 u0 Y' ~( f  K3 A& M
6 ^1 j$ J" g# [; v% K- U系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
9 [6 Q1 y* B5 I5 d  d
0 L& O3 @& n4 a6 [0 Etengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
  q2 D7 ^! Y( A, r# X; a- T
8 ?  G) D+ B) p; dmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
2 |5 Z+ u2 ]2 m* ^$ j& t( x4 `; o$ [
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
: d3 a' w! B; l6 y* a: \  ~4 |: k
  r+ O# e. N8 Q2 e依赖关系:
$ e8 X: X) t+ y" O: M7 ~tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
& i5 s' ?: \; t$ q* {: x, [; v: P
- h" r. L3 v1 `6 I# [yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
5 ]- P( f, l' {. umodsecurty依赖的包:pcre httpd-devel libxml2 apr6 m% v6 l5 I# u9 I, d

9 A; J* u9 G+ Nyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel" {* X. D1 v+ X4 d  ~, ^
二.启用standalone模块并编译
) s: F. U# G0 {4 b; H" q0 f; _" ?8 _/ e
下载modsecurity for nginx 解压,进入解压后目录执行:
5 [0 V& x1 W$ c. S3 m0 b
9 n! A, H  ^% U$ d7 R./autogen.sh7 B5 p! ~. A% s: C* n  D
./configure --enable-standalone-module --disable-mlogc: Q% B& e, m4 b8 C
make
, @9 V, e3 X. s) B, m  @8 `三.nginx添加modsecurity模块2 m6 T+ q3 E, M1 E1 K% r! d
& g3 i/ M8 H7 J2 v9 C9 l# f7 i
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:% j- e6 O; ~  v8 w
* H( J" u+ D; \  @& h, K0 A/ ^
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
% m# f8 N% @9 _  ?, e- q4 G$ F) V1 [make && make install% ]. L6 u; r* G* @5 J' t
四.添加规则! @0 v0 a# C6 Y2 [% j
6 a0 Q: f2 K, h
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。' \% C* z; f3 B4 m2 e9 M

9 |- H6 B( o3 d, X' d$ z1.下载OWASP规则:, Y* Y3 m* }' O- \' V& n' k
8 S/ U6 q0 H0 l
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ |& h/ X. Q! P+ j$ K( ~6 c$ i! J! ~! L! i0 Z
mv owasp-modsecurity-crs /opt/tengine/conf/
0 q/ j2 A: t$ C( t8 o  b% d9 i( a; \4 s6 N- L
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf3 E( P8 C- k8 p, o6 p
2.启用OWASP规则:
+ ^. ~1 E/ V# M4 X1 h# b% {: N% ]" v- y, D- |$ _+ F4 O
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。3 f! R3 t- y# O$ H9 t
( T( W6 T& L4 t* X9 s% N0 {$ V9 B
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
2 [2 q4 w) J" N! v, m+ W
; |+ ~/ @4 h; I3 U2 I( towasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
/ @/ V$ G7 Y0 T
; W" S% e' Q& p' K- x1 ~* IInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf' [. l+ O1 P+ J7 K
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
2 g+ b# c0 V) T8 O1 fInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf9 a) R* |) x) `1 D1 T/ u6 `  n) {
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
5 d- c  V+ I/ G  bInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
% ]  p% Z; @' q; Q* v7 o$ iInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
7 Z/ p1 l) X  {8 V& sInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
1 _& Z. x" x9 |& G五.配置nginx
3 U: R, v8 T- d- H- c/ C" i- q2 G% V* V- {5 M
在需要启用modsecurity的主机的location下面加入下面两行即可:
8 q9 M& `6 v$ Z
/ e. _6 F7 ?& F+ V, M$ n6 v% R2 m8 aModSecurityEnabled on;  
8 W- o# g7 Y- ?! i  BModSecurityConfig modsecurity.conf;* j' K1 B5 X) r6 a
下面是两个示例配置,php虚拟主机:5 G5 U3 q9 t9 l9 j

0 g9 @9 M4 [5 |$ z9 g- t7 l' {server {/ E3 I1 T' c& i0 K/ ^! S0 @9 u: O! @
      listen      80;* N; q- n3 d  P& h6 Q
      server_name 52os.net www.52os.net;( o6 {; N# N! ]
     
/ M; J5 [, o2 c6 J) b      location ~ \.php$ {
2 W" ^  e% T, E      ModSecurityEnabled on;  ' t2 E3 a) n* e" d) ~
      ModSecurityConfig modsecurity.conf;
# I5 E- z2 a+ [5 c1 W' }
% N, f7 W; [% a7 a+ H      root /web/wordpress;- |7 M/ Z5 E5 ]' Q" Q3 E1 |
      index index.php index.html index.htm;
) K$ B5 {( K2 ^$ m  
# Z6 b4 p) a" v      fastcgi_pass   127.0.0.1:9000;
6 Z3 W  p8 u$ O1 S$ D' ^! t% S      fastcgi_index  index.php;, R2 l9 T" o2 t  J# |
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;2 G& m8 H9 u& M  m9 d
      include        fastcgi_params;2 t% |3 D. }8 R* R. w
      }
; Q* @$ c) d8 x5 c' K* r% I$ C  }
/ u& N8 s' a; M$ r4 mupstream负载均衡:
9 V8 `+ x/ H4 a- i4 O2 Q; u3 \# Z9 z7 `
upstream 52os.net {+ x& d8 O8 A. w/ x
    server 192.168.1.100:8080;" `& r( F8 U3 [$ g# N" _, N
    server 192.168.1.101:8080 backup;
7 J( S/ I3 N# R( S2 [6 y7 E}
( \/ B- V" Y& j" Y: O
) E" |! c. e3 V: n0 H" rserver {
9 y4 L7 O  p) G6 o  \listen 80;. B9 p1 t/ Z( [; r9 V; c
server_name 52os.net www.52os.net;. t4 j# u) @% D, b$ P2 x$ ^
. z) X) Y. L" i' [$ L  n# t2 g5 f
location / {2 j7 _  I* T+ L
    ModSecurityEnabled on;  
: o3 \7 e+ Z# P0 Q! X2 F3 E& h    ModSecurityConfig modsecurity.conf;  
# s  V2 e, r$ \3 K, x. G  m4 E. V& c4 ]: a5 E
        proxy_pass http://online;" }# [1 j  M; b% R0 d, f
        proxy_redirect         off;
$ ]( `' P- N# E        proxy_set_header Host $host;6 c6 M* M. f3 T$ ^- q8 E# I, z: G
        proxy_set_header X-Real-IP $remote_addr;
1 P; m3 n  h$ D3 ^' ~        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;" x" c' x1 P5 P3 y+ f! m# U
    }
2 g) g6 [0 }  W" k' g}
7 m1 G& A( ~  p" \0 n& d六.测试; [) c. p2 D/ m9 F% z# E4 t

. n- J& ~+ q) o我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:5 {/ n! r; T* g* j* @/ M: U" |
. h4 V' E! y/ S' C1 n! c1 c
<?php
& j* i+ V5 S# n, y( j* y+ K    phpinfo();   
* u6 z0 y; z$ m' Y% r* z?>
- M# j& K6 I4 f# `在浏览器中访问:9 A) P, S# m; y& f1 Y% I! D
# b7 f9 Z1 \% y# o, P
http://www.52os.net/phpinfo.php?id=1 正常显示。$ V7 `& H0 f; u9 [6 P: r2 R( E7 S
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
; U' _8 u) a- L% b! g" Yhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
+ T/ ~6 c% T- W+ Z0 O3 p8 u说明sql注入和xss已经被过滤了
! M6 f  A: c0 L( v/ L
/ j6 ^4 z) C* X$ M( W7 ~$ R! N七、安装过程中排错
' V* z6 s7 N) j: j$ j
, I6 V5 E& F) `3 R; m- L3 R1.缺少APXS会报错
' c* D+ W- [$ V1 q. W! F6 M7 D5 c! H$ n' W) ~- N
configure: looking for Apache module support via DSO through APXS- z" [% W" [7 d6 L% _
configure: error: couldn't find APXS
3 w% h* c( J& R" }- t, xapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
1 b8 ]! i% N, e7 q. w, Q9 n解决方法:' q3 N. ^) @* e( f7 B
! U0 _+ ?8 w% l* l* ]9 k
yum install httpd-devel
7 e- A' I7 b% ^1 k2.没有pcre: T! F4 w0 x6 _# O+ _
( m0 l4 l& q7 }. K
configure: *** pcre library not found.
* i% g' n: d1 W5 p" ?$ |% b) t& zconfigure: error: pcre library is required7 Z5 E) x7 O' x' {
解决方法:* t1 [; @& Z4 t! b

% a  ]  u  T* r/ r1 R& ]' Byum install pcre pcre-devel
8 e0 n) v3 z& E2 T3.没有libxml2
' \0 `* f6 Q4 A& Z  Y2 s3 p: H8 b: ^0 ?& C7 F5 L0 x

/ I) r( e# o$ U) _/ Aconfigure: *** xml library not found.# T3 `8 l3 U2 D
configure: error: libxml2 is required' V  Q- N9 G# q
解决方法:) r7 {- l  S8 d( E
! g0 G& A4 M! E8 E6 t3 A) ~4 {
yum install  libxml2 libxml2-devel5 f+ q7 z4 I6 P  U3 x" f9 g' d
4.执行 /opt/tengine/sbin/nginx -m 时有警告# _# K/ j) |' Q& W; {

7 y$ R. d1 H- G, b0 DTengine version: Tengine/2.1.0 (nginx/1.6.2)2 t* h1 @2 _. C7 f
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
2 H- X6 _+ x! Z+ s, a# X' W  j原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log1 k3 S/ F  ~8 o  l. y% M  ~- b
! D# t  B/ C( i6 N: F0 l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
/ [( ?: S# Q0 B& O# z- _2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"7 r- L  x4 ?- Y% w2 i2 c, Z
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
9 _% M+ B2 {/ S, f" R/ X2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"$ f- z! p; U4 |0 R, W9 c/ n  W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
0 K3 d1 v! j9 n2 s' C( m* p2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
9 N, @9 X& j7 K& L$ l/ c1 `& r1 b解决方法,移除低版本的APR (1.3.9)/ y) W% G( c& y- K* z( e! O1 |8 n9 u
. S2 `4 s; a) Q8 C) q
yum remove apr- U; d' C; |0 r1 E  _6 t
5.Error.log中有: Audit log: Failed to lock global mutex
! Z8 T( M) p: {7 ]9 g8 J
& M+ I) R! W' L2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     / H3 ^, c7 F$ N9 W
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]9 @/ E2 L# a9 X7 h3 S  ?7 E% ~
解决方法:
8 {' }2 c1 z3 G: P1 r) V% e* J1 l编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
5 W0 [9 t# @  z5 Z: Q
! Y% r' W" G, Z# @) }9 SSecAuditLogDirMode 0777/ R* p9 H2 k. }( }$ a4 |) a1 F$ Q/ Z
SecAuditLogFileMode 0550+ `5 t4 `- w2 Y, E
SecAuditLogStorageDir /var/log/modsecurity
' w3 W% `8 h1 T/ b  U: t9 eSecAuditLogType Concurrent
+ [+ w, t4 U$ ^) W# q$ Y2 ^参考文章:- _; m0 T, ?. x, v( x* \; c) F: A
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX7 m' u' l' p( X6 i4 k; x' i$ d1 W6 k
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-6 18:45 , Processed in 0.087084 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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