找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9337|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
; @  ?" O; P, k3 y6 W
1 Q" m. ]5 O- G3 s1 f" G; I- c$ @一.准备工作
; \0 R7 K: \  z8 \6 k- a$ L" t: X4 E9 I' Z
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0  o- y( A0 |6 z& s, I0 G6 G

  g" D% I8 p/ A. L* S& v6 A3 ?7 N! Qtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
& @  Z/ `  w9 s; V, i! p! D
& R+ W0 G/ w4 imodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz7 U+ h5 I; N0 \. Z
5 @: m8 z) @2 o: t# y
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
) |( }; `# D! h5 i- ]3 I0 ?
) Z/ U# ]' v% P, J) f( _依赖关系:
9 m: H+ S% d, {! e' c4 |5 Ytengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
; Z% Q$ n/ o5 {. z& S, R. a2 [4 D3 b
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
7 S6 m' {% i% `9 U+ amodsecurty依赖的包:pcre httpd-devel libxml2 apr
% q7 {. ~4 i0 N, \  l' t7 o* a7 V: ?; r% Y4 N
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
# \/ n/ q' C' \9 d' {: D4 `! _5 O二.启用standalone模块并编译
0 v& n/ N& Z; L/ M4 ^; D+ Q  d' I$ Y( W4 M( ^! ~
下载modsecurity for nginx 解压,进入解压后目录执行:- d1 {! F. M: v6 p# x) j/ ]
. c# I% q# t. r( z0 |
./autogen.sh
- G( z2 g" ]% p./configure --enable-standalone-module --disable-mlogc
4 i  g& H) [$ j( X" lmake - p$ b9 ]% |( V" V% ]
三.nginx添加modsecurity模块& f0 L: U5 R9 p

4 T7 l' E' g) _& U在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
) {* n: t1 @: `( p& h% c" |  P4 d; D1 ~* ^
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
# n6 Z& b8 ?$ E% ?7 i5 ~make && make install
+ N" a4 P+ O; m) K9 C四.添加规则
9 Q8 z% E/ Q. o9 E
0 I- U+ f+ c( M% V- Zmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
4 Z6 |# Z6 S- q% F' ?% T6 W4 t2 U) w6 n) |/ c
1.下载OWASP规则:1 T, G" u/ Z7 L# \+ k

+ a1 Q5 m( B. T+ V4 |9 mgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
3 b& y, P2 p. V$ b8 J2 y' n9 F
0 p, j" K* y. O/ H. ?mv owasp-modsecurity-crs /opt/tengine/conf/
7 H7 `2 |3 }. m- e
* l+ e3 S+ n. ~$ u4 a1 I1 E1 U0 Ocd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
" b7 o. P  Q5 t( G2.启用OWASP规则:9 l* }) x- n+ A$ W: m9 P
7 F6 T% k0 P3 w* l1 y
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。- x9 k2 k9 ]6 c% T0 P
: r$ c. ?5 L! h) `, C
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
5 x9 h! l$ b* N& R8 E0 o- ?$ R, S% R! S* [0 t
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
* O" x. g& `0 d  ^* W- f: h8 d/ Z* p: r7 s% S
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
" n) G6 k7 O$ X9 }( D# v- ?Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
* d6 ^- Q3 c9 w9 X  QInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf8 X* ]' {; q( i
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
' x- Z3 J" V. q- v! P2 T; OInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf: d$ ^6 [2 O1 I8 @! G
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf( v* G% ]5 m/ z& q2 |' |# H4 V
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf8 _- F4 ~0 ]7 N5 W: T
五.配置nginx
6 y9 B) P$ N! g, }8 j$ w+ C! t
在需要启用modsecurity的主机的location下面加入下面两行即可:
4 |: J- ~+ v$ f$ K5 r# Q$ z0 H: C1 S7 X. M) b: r
ModSecurityEnabled on;  $ k' @: F( [6 T9 G) U0 @
ModSecurityConfig modsecurity.conf;0 ?$ T" R5 O8 u5 s7 k( u- U+ L
下面是两个示例配置,php虚拟主机:
7 s. m4 M, V; J8 u2 T
( `& C  o: I5 D$ Aserver {
3 I; L1 x+ u  i) i      listen      80;7 b/ E+ F& C/ M4 x& T+ F) U
      server_name 52os.net www.52os.net;1 a: S/ k' d# I+ |7 S1 K" Y3 P
     + F( I% w! E7 G2 m
      location ~ \.php$ {' ]9 j& y2 Q5 U9 v8 F0 h. u# t
      ModSecurityEnabled on;  % K/ L1 c4 H! c) R' a7 L2 m# a
      ModSecurityConfig modsecurity.conf;. b/ C* k8 J1 k
* e" Z# [, i( J% G" M2 ]0 h4 ~4 Y
      root /web/wordpress;* x' [( i0 X5 o+ d1 |
      index index.php index.html index.htm;" b5 W: g5 K- J5 e1 M( _
  
7 s  v" D9 G" D0 J' B2 i      fastcgi_pass   127.0.0.1:9000;
. w. t# M- H) \* l& ~      fastcgi_index  index.php;  E# N5 Z( W" A- L) T$ s% j
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
! L, }. K8 C2 }      include        fastcgi_params;* O7 ]3 c# U) P$ v+ V
      }) z6 h  Z+ L" x$ u
  }2 ^- T( ?2 F* Z/ _
upstream负载均衡:) t7 v* I( b# U9 Y5 d+ `0 W/ U

& e& _( N- p7 C- v$ wupstream 52os.net {2 [, e) O5 T! K) ^+ z% L
    server 192.168.1.100:8080;
/ g. a- P8 B8 @- T9 @" a) S    server 192.168.1.101:8080 backup;" P$ X) m, X, j# }- }" W# w: q8 Q
}& }% M" F$ h; j( F0 c  D

2 b. O- s1 j) v* B' o, x4 Y5 aserver {
' i' ~9 x" r* Z# t5 B) jlisten 80;+ N! G* T, `6 _! o8 F- g+ J5 {
server_name 52os.net www.52os.net;
9 d2 @) w5 e1 w, Y3 x, Z
3 o- `4 ]* `2 }4 x* a- V& o0 n: clocation / {' s/ {( `7 Z. Y, y/ i
    ModSecurityEnabled on;  . |7 j: `$ K9 t
    ModSecurityConfig modsecurity.conf;  , r: Z) z* t! ?8 p  o, B, m

% g; I: ]/ \/ R) h        proxy_pass http://online;
, B6 U$ ?5 I% G3 t! _        proxy_redirect         off;. q7 G& E! N7 p  L  Z& g; {
        proxy_set_header Host $host;9 ~8 C/ i* b! n$ K$ T7 P3 i
        proxy_set_header X-Real-IP $remote_addr;
$ n+ O) J# c  b3 q3 b! Z0 G        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;8 b& V* q) E# S3 v3 J
    }
# m* J. Q" ~9 |- q* p}. c: D9 G: [) g5 a
六.测试
0 H0 ~, b4 Z5 h
' b% o3 y+ Q! p# W% z, ^& O4 a我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
& j/ ]  ~- {) m5 N/ }, y% r6 J5 l4 \) A4 R4 b- c+ q3 n# F  Y
<?php
5 G. r  V7 b* k2 y2 g4 ]    phpinfo();    6 Y6 k/ G3 t- P7 K: h8 l% v( ^. n+ s
?>
! v7 j7 m5 ^: s; u在浏览器中访问:
& I3 S# i4 I; Z/ `1 B) x4 k2 ^1 r8 k. X' F+ w7 ~, j: C" w
http://www.52os.net/phpinfo.php?id=1 正常显示。7 `* y# g/ A4 z- \7 Q
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。' J' F; W4 R1 ?' Y9 C' L
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。7 w3 B& ^1 \5 q8 T* c
说明sql注入和xss已经被过滤了$ [# l' c8 S5 {8 O; b7 d) j

$ a. X7 I1 v1 v* k0 N7 d, o七、安装过程中排错
6 b* G1 b& R% W" ?7 ^3 w# W
; z/ G9 v4 Y$ Z  @6 m4 l  r0 L1.缺少APXS会报错$ Z  t; }8 w3 [% o2 j( r+ z6 Y

7 b: @; q, b, s! hconfigure: looking for Apache module support via DSO through APXS6 ]2 h' K1 |3 T2 d- Y; I
configure: error: couldn't find APXS4 g' q3 `4 H# b! f
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。* ^, Y& v% c- C; F7 [: ]
解决方法:& |# v% \4 ]7 p) w; K

  T' f/ E/ [  fyum install httpd-devel
9 U- e) S- C: }1 X9 H, G- ?5 z; J2.没有pcre
9 X3 l% `. U* x
4 ~5 n" l8 |- p2 qconfigure: *** pcre library not found.
2 t# K. \5 l1 v# a$ R/ jconfigure: error: pcre library is required
* i, m3 }  k3 u" d( E" C( z) q; \! h4 [2 r解决方法:) o4 U0 J$ Z6 k$ _* c/ v

& q  E: z4 \* k5 q" }yum install pcre pcre-devel
6 i$ }9 }+ Q& u, ^8 H) L$ a( k3.没有libxml25 F# V5 o" ]/ m2 _8 z" Z

$ [" l- w( K; z
+ A, h; B* h, z3 Sconfigure: *** xml library not found.
. I# A& M- a- V+ Q, yconfigure: error: libxml2 is required
( ^, N( l4 [5 O解决方法:. R9 C7 {- z, s6 F3 T7 @# g5 C) W
6 o0 l! t8 \+ p0 a; o) W
yum install  libxml2 libxml2-devel
- @5 _3 e" W& _5 J- O: f2 }4.执行 /opt/tengine/sbin/nginx -m 时有警告: J: G( r) F9 A' o# C. [) V& S
; J- |) r4 F7 c3 F( Y, ?0 r0 Y
Tengine version: Tengine/2.1.0 (nginx/1.6.2)/ Q/ e1 _* L% Y0 j3 Z
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
0 K: G8 r% U& @0 |# n6 [原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log( j$ A6 ?" D# o/ Z: p
/ `; l1 y* \. N) w* \+ j
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
) O! G: ]# L" q) \2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9". m( j, R7 c* G
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
. z0 U% v+ Y0 h7 c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
+ O. _! A$ K& {2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
3 ^4 m2 @. C) q' j- n2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.2 A5 ^$ e: A4 A3 e
解决方法,移除低版本的APR (1.3.9)
6 d8 M6 `( a: V
( V# h- e6 q/ o# }0 F& wyum remove apr
8 r, W' h* x7 g$ {" b5.Error.log中有: Audit log: Failed to lock global mutex- W; g* S8 w/ I2 T: e

8 |5 b* m! ~# [6 Y% n  F3 R4 P2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
8 f4 l5 h4 o# M3 s# o! mglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
* R, _! a" G1 a1 t( t) L解决方法:* k: {/ j7 ]$ Q1 M  d/ w
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
8 D9 u4 C' X4 [4 L- b: c# R! l. ~9 {. R7 m' ]9 B' _
SecAuditLogDirMode 0777, E" s" o7 P0 G: X; f' [9 E
SecAuditLogFileMode 0550
8 L2 M0 n9 K. L  E) F+ LSecAuditLogStorageDir /var/log/modsecurity
' Z" N, Z) F: X0 [; iSecAuditLogType Concurrent
; _" l2 a6 D6 S; h, W9 F参考文章:: R: D' e% u/ w! h# F
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
( d, C8 T* z6 G% f  ehttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-12-7 21:02 , Processed in 0.066117 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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