找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12166|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
' E6 L* x6 ~& D: a
8 \' Y- S4 E( S- T一.准备工作" G. ?6 ^. ~4 Q6 ?, A& f

9 }1 g  U) b- l' D系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.05 l* n$ e9 x, _. g8 Y' m

: i' w' L8 Y( u+ ftengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
6 m9 W( x2 E2 b9 W5 p
# ~0 s7 p. S3 W' Vmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
, H/ [" u9 S* R% g
! t$ L5 s( Z' QOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
8 Z* W. ~! T3 N( t8 U" }& t( i* t# \5 m, p" e5 D- _: d
依赖关系:1 h' O( E" }% ^( ~. l
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
3 E- ~/ |+ q/ Q. ^5 z- c! O2 e7 j# {* @( t6 e; I
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel# m6 @6 J* w& T9 ?0 u
modsecurty依赖的包:pcre httpd-devel libxml2 apr
' X  Q  H( X- V% H1 Q7 T: X, v  l; @
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel: Z8 x( {- ~' ]+ }
二.启用standalone模块并编译( K% H9 S0 r  k; A: I) O
; |" L4 k2 `$ w+ b9 b; G
下载modsecurity for nginx 解压,进入解压后目录执行:
. F' ^9 X! n9 s
' y3 Y1 E, X& ^% p0 I./autogen.sh8 T' V) h, g* s  @/ A. R
./configure --enable-standalone-module --disable-mlogc3 y/ i: R3 V$ `
make
, D! X9 h  a3 `' _三.nginx添加modsecurity模块- E! s( }. f) k5 C2 k) Z# [5 E$ ?

2 K8 N- c4 e6 i2 _8 N在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
7 f% |" P/ N' V: @' a) X
+ S) s5 n- }( K( b' d./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
9 b! |1 l% U5 E9 k9 f' G5 Smake && make install# ~/ {  P( h) ^/ U
四.添加规则* Y1 I3 `( ]4 n9 R& _
3 D! {2 m3 A# v+ g$ j: G
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
5 G, n- ~2 k% X, @9 n& T
4 v' V; S  h4 [& j1.下载OWASP规则:0 B4 ~+ U& ]6 V( R7 C. Z5 d6 `
( O' T3 S3 E% [" j; C0 ?
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
- T0 A  e) L8 \$ J# C8 F( m$ z
1 z+ L# y8 G) U+ fmv owasp-modsecurity-crs /opt/tengine/conf/3 I# n: _" s; q; Q0 E# S9 |

0 m+ h; _' q' l2 hcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
+ I3 X# \; N" A1 Y2.启用OWASP规则:
9 |# `, e. O7 `) ^6 ?
5 r; A) u5 V1 E* R复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
7 M+ R6 X8 W9 x/ j2 p$ h  R, @/ U2 A( |
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on5 `5 {* k6 \8 j8 ^: D. g4 [& g9 m' B9 l
: W1 E3 E' w6 q0 b, Z, N
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。/ K2 |5 @. M: z4 V

% t/ d" p/ u0 V  z$ iInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
: b, F1 v9 V& _7 u$ G5 bInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf9 ^! u' f# X" V8 J3 X
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
. Y% j0 F, {  g! p% PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf" d7 E  T* P2 h, K6 t0 X
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf3 K& j. l, P; D8 X' P% B8 v2 H
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
  @& J- L2 g- E( r$ x/ U+ b" ^; N5 M0 PInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf& c4 @, f% K- |+ s
五.配置nginx
# W+ Z! j# G" Y! m" J- W0 ?9 u" O4 O3 \$ C2 E
在需要启用modsecurity的主机的location下面加入下面两行即可:. s" _7 F& f1 R: H7 a  O
) G: W" v5 C2 w$ w
ModSecurityEnabled on;  * p# ^( w3 W: ^7 O; u& y3 a  R" I* {
ModSecurityConfig modsecurity.conf;; }9 G+ Y, Y9 k# K( t
下面是两个示例配置,php虚拟主机:7 M9 e9 B* S5 w$ V# @% G& S. c7 h
) t, s1 ]9 g) F$ H
server {& e! s) s& e7 C$ x" D8 h, ]: ?
      listen      80;
+ I$ k2 G# ]7 r! k      server_name 52os.net www.52os.net;6 S9 {8 P; K4 U3 u# U$ r8 g8 j
     9 D) V% w' S4 W% j0 _6 S0 C% ]- v$ f
      location ~ \.php$ {9 z. I' b  w# S3 g, ?
      ModSecurityEnabled on;  ( Q. t' |( ^5 @2 Z( \* |" O" D
      ModSecurityConfig modsecurity.conf;
) ?2 {) s+ i% g& u0 |( ]' Z2 O% H+ q' q' n, |
      root /web/wordpress;2 Y5 _4 p2 G2 H* b- T0 U' T
      index index.php index.html index.htm;
) d: d7 E5 {- |( K. Z% I! X. Q5 N  
8 t' B/ Q; m8 }$ F2 i      fastcgi_pass   127.0.0.1:9000;
+ i$ B% x3 Q5 O" a& |: C      fastcgi_index  index.php;) V' ^9 q* w. I- v8 d
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;2 ~% X# L# y" L
      include        fastcgi_params;
; I) B: z% \- I7 T# Q2 S      }
& ~% L$ E* c' T& Z3 P- A6 z1 U  }, `1 y2 z# v" b+ A) Z5 v# R5 d$ l
upstream负载均衡:: f+ A2 D! j5 D: u8 D- }
$ ^: n5 Z9 a. D: U
upstream 52os.net {3 `7 X  Q/ r6 i+ m- Q
    server 192.168.1.100:8080;' x$ u, E/ M( {: E3 i7 c. t
    server 192.168.1.101:8080 backup;, i) m' l% ?" _8 o2 C7 R
}. w* l) u( I+ q6 ]7 @, A9 y
+ k/ @: F3 b( K2 K( `$ Z
server {- W6 |7 X* x$ @
listen 80;& Y0 W" B3 Y3 ]+ c7 @0 Y# v3 s0 S
server_name 52os.net www.52os.net;6 S2 t4 s9 Z7 V' x& h

: C0 @' \; G( n) Y" G' Z. [location / {
1 D: F# ^4 e7 e) [' X- Q, |  P, ^    ModSecurityEnabled on;  
! |' L3 y: L# w, T' v: }- E' E! s) C    ModSecurityConfig modsecurity.conf;  % q( I0 z: m$ A, l2 c; _1 N3 s: t

5 `' B, O. ]7 t; @8 I* w( x        proxy_pass http://online;
. G/ z) g9 _, Y) B% ^6 k( }5 Q        proxy_redirect         off;
3 l  X  _- R7 v4 q( C  }        proxy_set_header Host $host;
4 X/ {# b9 y. C* l( ?, y        proxy_set_header X-Real-IP $remote_addr;# Y: L; q  ?. N
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;, t2 y' O* {/ J' \+ G
    }" f( Y' E2 w" S1 [; F4 E
}
! ^# n/ [( A1 G& {六.测试
# ?* q$ V& a; ]$ v
! Y3 w- K: m4 \9 K7 T! {3 E我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
' s. i9 ^* n) ~, d/ l/ Z: C2 i$ A& q8 W5 L8 _
<?php
. l0 g. k# A9 H; j# ?/ Q    phpinfo();    $ x# r8 @1 b( j. Y# S! j! y, N
?>  [0 v2 Z) P& R5 |. E  r( O1 `0 M5 ~
在浏览器中访问:
/ n8 L( D+ K- {. j2 g
+ f. b. B5 P. Y3 J9 Bhttp://www.52os.net/phpinfo.php?id=1 正常显示。- F4 R1 H1 L  l
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
. g& F1 N$ F0 T5 Y/ bhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
5 E/ E4 \) q- K- |& s. W, [% O说明sql注入和xss已经被过滤了
" D% e0 \# M* V! s; m8 T, e4 s: r  j+ [. I6 j3 m
七、安装过程中排错' a; H" ~+ w: ]' `

0 }: G7 y/ d; G. {9 D8 X7 u1 k! N1.缺少APXS会报错; @8 w  R. L1 g4 a5 V# D9 p7 r

" Q+ W. T/ X) S# p4 Cconfigure: looking for Apache module support via DSO through APXS! w$ H/ _0 O7 f+ L: }
configure: error: couldn't find APXS2 w1 `1 e& _! P
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。" G0 v& ~2 i9 B7 f9 e
解决方法:
- ^. X' _0 B1 z5 c+ q" F8 t* H
) n" t8 L8 L8 G3 d) D  Syum install httpd-devel: D7 S$ O3 q1 Z/ B' b. f/ V3 g: I
2.没有pcre. _: |  u+ N8 p. f( t/ h0 d5 Y8 p* O

8 R' J, v( J5 z  T1 Zconfigure: *** pcre library not found.$ O' k* N% C* B  M
configure: error: pcre library is required4 p$ Q3 n: M6 U+ {* m" p
解决方法:
) R' a$ {% U7 Q  w, t: A. K) ^9 b% Y  O, _2 f* |3 G
yum install pcre pcre-devel8 E( W. S$ x; d
3.没有libxml24 }- m. Z. R' U. k, e$ h
0 S% B9 }6 {& P4 H
8 h7 X( g% p! l9 ^% b4 d8 i& j/ b- P
configure: *** xml library not found.$ {: v( q; @0 t+ S4 g% ]
configure: error: libxml2 is required
; J, T- [1 n# L" B% {# D, G% @解决方法:1 |- G$ o$ l- j

2 ?: `" X8 ~( Ryum install  libxml2 libxml2-devel+ ?0 q3 m5 e: e) {( i, Q" W7 K
4.执行 /opt/tengine/sbin/nginx -m 时有警告9 ^( D% w% G* i8 E: U% F
/ s" J/ P/ I4 U* V( W
Tengine version: Tengine/2.1.0 (nginx/1.6.2)' t0 F+ T0 ?# X/ z% C
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
( z4 w- C2 h6 M" e1 z7 z7 \, Y, ^原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
! o: S$ b# X6 [0 x; K- B( {; t/ W: q" S8 a! N& T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
/ g$ _; I; h/ ?, ?; F$ U+ L7 D* A2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
! w1 ]4 G8 q% ], ]2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
' O" |# f9 `. D# A! \2 f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"! q0 X  @! R. B9 l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
% \1 h9 N2 T/ [" K3 u2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
! n. f4 P7 _) E- z2 {* E. F解决方法,移除低版本的APR (1.3.9)6 ]" v" l$ S6 Y4 h) F# f+ n  e

/ r3 p5 [  P% z; @1 wyum remove apr
+ _( M" `8 |$ n- u! b5.Error.log中有: Audit log: Failed to lock global mutex; s& R$ e- N2 b# _
* r* _2 X: O2 ^& y  Y# c5 B
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     7 ]! G, `) Q4 y* V! O8 P5 G
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
# d  B4 t$ C0 ^: T" Z: S2 B" ]- O  e解决方法:" J* ?4 @  @/ }+ I7 ?
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:! @4 U6 L1 w/ s' M

& Z1 E/ `! Q. L: g8 K9 kSecAuditLogDirMode 0777  a% {. A+ G- z" j9 T
SecAuditLogFileMode 05502 l1 J4 K3 N) t/ X; u7 n7 Q
SecAuditLogStorageDir /var/log/modsecurity
! A1 P' A& g$ g$ D( `7 eSecAuditLogType Concurrent
& C, O( W0 g6 }) ?& O0 ^2 {5 V参考文章:
: P  x5 a2 A, Hhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX  |( w) V1 W& I9 G1 S7 I3 I
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-15 08:34 , Processed in 0.055682 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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