找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11288|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
0 m6 {9 q/ o$ G$ M. c, N0 Z# x7 w0 K- M2 M
一.准备工作3 _/ e- `/ @1 p

3 T; `5 M& Z% J3 H系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
% X' a% V( g0 z4 A1 u1 [& |
* Y# ]  g) \& {tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
/ i6 r6 c# V  ~4 |, y- O9 o4 \2 o2 J+ R
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
6 W9 Y; l, Q! B& o6 M7 g- y
$ b. F5 A6 C* `% L& o& D6 R( w3 JOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
& Q9 K' O0 T+ ^% \6 p) v+ g9 V/ C4 ?% q  @
依赖关系:
6 j/ S$ ?" [9 R7 f  Ktengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
% m9 Y9 ?9 M: E+ N, n
  A4 x/ S0 e4 V5 \" x+ iyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel" _8 A& u: j4 d' o
modsecurty依赖的包:pcre httpd-devel libxml2 apr
; p: i0 |5 i- S' U  e  n, h" i$ I- o, b- K2 ^. `" G% b/ ?
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
9 Q- x7 o! K: I二.启用standalone模块并编译
9 Q: R; Z' b& B3 i' ]4 M' H9 c9 G, @' _7 N5 V$ x# ^
下载modsecurity for nginx 解压,进入解压后目录执行:
2 _6 [7 E1 y  i+ \6 A; W, p6 D6 t2 Q  J$ d9 V7 B
./autogen.sh
$ z0 d! p+ Y! A% j8 J( O./configure --enable-standalone-module --disable-mlogc* j7 q3 B" F- Y. k  R& Y4 X
make , s: Q. s  X" m/ A9 `- i" m# [
三.nginx添加modsecurity模块
" M+ F0 \3 G# _; Z2 K+ H6 J2 C8 @" N% u
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:- k5 L* Z( `% w! @8 ?4 t

/ f& |+ g1 A: s/ A4 c./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
2 E! A% B% N1 |$ `# O4 Rmake && make install
; L6 p% A1 [, [/ g8 M四.添加规则4 s& l, C- Z: }6 u( I- A4 i
& Y' Y7 q. h3 i1 K
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。8 x8 p; {' q2 w' h: D% b6 E0 [* ?$ B$ }
) _; @* ~# U; T. o& T
1.下载OWASP规则:
; Y- e/ q, S$ m5 M8 s4 T# `
, `5 K! r: `; [- m6 p6 V0 [" [git clone https://github.com/SpiderLabs/owasp-modsecurity-crs8 ^# \% ~" T9 z

- i+ S- i1 ^, p6 d% I& H" Qmv owasp-modsecurity-crs /opt/tengine/conf/
! G9 u2 `. V8 m# C
3 H" T/ q! ?: g' `) p; w  J8 rcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf; D! s3 u/ p  o4 ^
2.启用OWASP规则:5 C" f* K( u: W5 j3 F: b+ v

6 a: k- ^& y" s+ I" Z复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
, I6 C8 y: W" a& J# X7 w9 g7 P" y, L4 j8 r1 n& o  @
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on3 ?% d( X& v6 Z. l. v) O- O

5 }! m' y0 @) g  B/ towasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。9 s3 H7 |, c8 z! f) {: w6 e

; {$ D% t4 U: IInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf; q* S4 G5 T: Y9 `0 d0 {4 {% z! T
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf6 t/ M4 y$ Z4 L; B( I# [$ }9 u
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf) {/ c  v; m2 t7 s: k2 ^$ }
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
; A3 I/ `) k% W% G1 \Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
/ K) o' b! i4 o2 }/ Q: H5 PInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
& }# ~' J) N& l- W* {' D( T6 BInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf; q6 E; f; g7 \# H
五.配置nginx" ~6 q, R, m9 `2 O3 ^. D, n6 Z6 E( q
1 S9 |0 y: j- y$ a2 x7 p* L
在需要启用modsecurity的主机的location下面加入下面两行即可:# H/ m0 E$ Q+ J5 J6 L6 c
0 A* q  c+ W+ v& n% d( H. r0 P
ModSecurityEnabled on;  
( r6 e- N  j- X% N* g, c0 zModSecurityConfig modsecurity.conf;3 s/ u9 x. R9 Q# Z' W
下面是两个示例配置,php虚拟主机:
" J9 A" I; s4 g/ C* I" }9 t
3 p" E: v9 z/ T7 i; K' ?server {
: Z# M: Z& I7 D7 q; O      listen      80;
* S) o+ k* c* r: [* J0 J$ K3 i  C- U) y      server_name 52os.net www.52os.net;# [5 |' z8 c: I0 ~! }+ g* G
     # ]2 [7 |8 b' ~0 R- X' T' r
      location ~ \.php$ {
1 Q! q9 }8 g9 e  I      ModSecurityEnabled on;  
$ V( r6 W( f) d7 K1 ?, n      ModSecurityConfig modsecurity.conf;0 ~* R" S1 k' _. f- l. t/ a- m7 H$ B
+ q9 E2 p0 O5 J$ W  Y# T: n
      root /web/wordpress;2 Y) u, x6 R; z8 X3 _+ [
      index index.php index.html index.htm;, X7 ^# a1 M9 I8 r0 n9 v7 C6 r  c8 B1 }
  2 W) }0 u( `: }/ O1 S0 @6 o! G
      fastcgi_pass   127.0.0.1:9000;( ]" H1 w1 ]( o2 b9 t, @
      fastcgi_index  index.php;
3 T6 g) z/ S! F/ y      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
. N2 R+ U: m/ ]) c5 j2 |3 o      include        fastcgi_params;- s9 k0 v) J# N$ j7 p( Y
      }& s' w' O/ B' @; R# h8 B2 ^
  }- p) a' K7 C; i  l  o# n* d; n
upstream负载均衡:
+ w: }* m" K/ I. M; W( y2 V
3 ~' G  Y% y7 Q) T( L/ d9 Jupstream 52os.net {
" c: ?  _: t1 I    server 192.168.1.100:8080;+ w/ M: p4 ~+ @. O: a- S
    server 192.168.1.101:8080 backup;
7 X" i3 ~- Y9 G5 T; a}6 M) X! J' o0 N
# B0 z3 ?; M5 _9 V4 G; m+ v
server {
. M2 C; A+ q! Z2 ^$ j' K7 t; ]6 [listen 80;* b3 d8 Y* o8 @- Q/ q3 |9 n$ z) `
server_name 52os.net www.52os.net;
4 ?+ p0 _. g7 B+ ^4 W' Q
% N' G$ w' D) l3 c. llocation / {
( d8 K8 k2 z. E& I1 Y    ModSecurityEnabled on;  & M" E$ [" r2 N% f
    ModSecurityConfig modsecurity.conf;  
' N! C& t' N4 T; s! H: i2 H; s% A" K4 `( x! B
        proxy_pass http://online;9 j/ l+ ^6 z/ m( |( V1 i( x3 u
        proxy_redirect         off;0 l' S/ E: N- M6 }2 [
        proxy_set_header Host $host;2 u" ~! C* @& c2 {
        proxy_set_header X-Real-IP $remote_addr;8 \; V* Z7 j1 `. k( b- Z# d
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
) s( }8 H: r( A. v    }
# K/ o7 A! Q' h% H6 v}. u6 Z( ~! ]/ {6 C$ s* @
六.测试
: Q; \3 e7 `7 ~5 u/ j
2 t1 t  E* A* N我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:3 j4 }* B8 y3 Y# [. _6 n) u
8 X* Z( O; U! s. b7 w4 H# D* L
<?php
( o" d' w% d: _    phpinfo();    + S# Q( V4 P! j4 z% v
?>1 E! \- g. {9 ]( S8 Q% ^/ ?
在浏览器中访问:
6 N3 b( K7 b. r5 `. y6 m. e1 b
7 b# H0 f8 _; ^2 k+ mhttp://www.52os.net/phpinfo.php?id=1 正常显示。
+ i& r& {* r7 c: h: A7 Y) L. rhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。9 j( m/ c- E' d6 R0 u
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。9 p  K5 I) n' I6 D* w
说明sql注入和xss已经被过滤了, _! P: j7 a. r& k
, y) {" E+ W8 s
七、安装过程中排错! {9 T1 i4 S+ y. E( i5 k

* `0 R( o6 G/ F7 I# Q: a1.缺少APXS会报错
- _$ `" c1 w; X. R7 j. b$ D! t* u/ M
configure: looking for Apache module support via DSO through APXS  H& U! O$ D* D! h6 {) I% z
configure: error: couldn't find APXS
" T. `6 R- n; d) bapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。& y( f, z  x% D* f6 q. f* k
解决方法:; w; q3 z% w. Y1 W; r
1 a8 U+ e( x+ C
yum install httpd-devel% q4 E+ U7 H' q' x
2.没有pcre) c1 Z( O: I1 W

, @6 I# M" r( @- T/ Mconfigure: *** pcre library not found.
1 d# T2 p+ q2 aconfigure: error: pcre library is required/ m: m0 ]6 g- t; _2 g4 k
解决方法:
* |* ]2 Y0 P4 E3 H, `3 w
( s0 o8 b+ a: ?/ ?' @yum install pcre pcre-devel4 H- |5 y. M& l- b, P
3.没有libxml2
) M, L9 g+ W2 f7 i# g! ^! O: c6 o* G8 f$ |. w" P* a/ m/ S

/ }7 F0 }7 y7 C- dconfigure: *** xml library not found.
* J( @5 h0 d+ v* lconfigure: error: libxml2 is required* B/ E+ p5 e) b0 S; T) S, v
解决方法:! ^: H" M6 @9 {/ F' g

  @+ o7 O! [9 t, A1 m  v) ]yum install  libxml2 libxml2-devel
4 a& \& o. B& P, L, k: a. X* k: u4.执行 /opt/tengine/sbin/nginx -m 时有警告
6 w  y. I4 K5 {
' g8 C; l- t! g4 Q0 i( r2 z; TTengine version: Tengine/2.1.0 (nginx/1.6.2)& ~4 `' ]7 ?/ {4 C; E3 b
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
+ @1 b. K& W8 U7 C, z) p原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
/ z( z, z( f$ W2 J& G" y/ D6 Q3 v
/ p9 [( ^3 `6 u8 L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
7 w- U5 s1 K. J2 D' N8 S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"6 [4 [7 F' `% Y+ G
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
$ h2 X. N$ f, T4 l2 U8 q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
# P1 b6 t, Y' `* H9 h$ n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"2 w- T# i  T% T' s
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
/ s( L4 _  Y/ @/ p解决方法,移除低版本的APR (1.3.9)' q9 z: W/ q8 f6 ^( G3 S( ^
  O. s( _6 C  k/ ^* j4 K7 K, n
yum remove apr  R+ _* t. x9 ~% e2 H( l
5.Error.log中有: Audit log: Failed to lock global mutex
8 w$ `# J; J( d& A" z6 ?9 ^. ]2 G' n$ N
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     ( J) B3 }& d6 |* B7 A0 g
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
. s% X0 j/ j8 C- Q) J0 e) |解决方法:
* I" }: H% f& i$ {/ J8 q1 Y$ L编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
% s7 x$ e( |1 }6 h' b3 u1 ~3 @) @
SecAuditLogDirMode 0777$ n, b$ q" o( w9 o# c0 w( u
SecAuditLogFileMode 0550
' }6 |" G" L' Q2 W% N1 K5 B  FSecAuditLogStorageDir /var/log/modsecurity
' {+ i% q; i0 kSecAuditLogType Concurrent
& T3 }( d7 R( {5 l/ `参考文章:. p. X" w$ t# i7 ~
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
, K/ T! \% @' z7 n- h6 J$ thttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-6-6 19:13 , Processed in 0.079159 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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