找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11985|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。0 _0 T0 D& U$ f* N8 r
& {6 T6 P* e5 Y, t5 R5 n7 a- f
一.准备工作
& {0 y' @, T" ]! c# ?' D# @6 x8 o  B$ L5 y1 S7 A  b# ~
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.06 r  u2 T$ X2 }4 X/ Y

. y* `. X7 u& Mtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
4 V# y: Q2 t% j) R* e  J  k
! F1 A  l( J9 j. T6 {' I' e1 }! tmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz  |' S. N# r0 S5 N
' H3 y' A) E5 c- Q
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
; J6 L, ]' G; Y* T% e5 R2 s- y; y( W1 _+ z
依赖关系:4 F7 H2 h7 o. r
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:8 N2 r) f/ Q  {' T: ]! [

. v' D3 }) I5 V2 a  Fyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
* b$ m5 j" }8 r2 mmodsecurty依赖的包:pcre httpd-devel libxml2 apr
+ }1 N( m4 m$ a" a( s+ `5 U4 |, P0 m! ^. ]+ I
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
) D$ [5 c& F& v: B; c: Q& a二.启用standalone模块并编译
: G% f% G, j' Y1 O% s2 W/ |4 }/ D( `: U# ?, U) ~( N8 e7 R9 R
下载modsecurity for nginx 解压,进入解压后目录执行:
' @% x( m2 O( Z# A! t3 G/ u" N: F* Y" ^6 Z; h
./autogen.sh- z$ W/ u. A' B' v( o, V5 |6 K
./configure --enable-standalone-module --disable-mlogc* [; r+ e2 h- ^1 n- N- Y0 E
make
* h; J' i( S! S% X1 K) k, V% {三.nginx添加modsecurity模块& L+ Y  K. E$ ^/ i
( O: W$ h  `5 S: W8 v9 X8 L
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:  a% d) o  f$ j
! A7 A5 d; ]( f' V: X( e- g* m
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
. f8 r0 x, C1 n+ ?3 e% ?make && make install
' R1 U8 p  O0 }+ r四.添加规则
( Z: c  r( f1 `9 F8 _
1 Z$ m( e+ E! X, z- w+ R( T4 K+ L4 umodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。$ i  `. R7 m0 n, p; \$ J3 e" l
3 y( G) b3 {( O! D! M1 [* K6 A  q
1.下载OWASP规则:
4 V* ^; ]( ~1 S! R/ U! G' q; q+ j7 {" @- c
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs5 `& |9 ^( j" R1 p. U- G( Q& o

7 m0 Y8 Q* j1 i! ^3 ^4 Cmv owasp-modsecurity-crs /opt/tengine/conf/
4 h- K7 a9 A3 O* c. W$ @+ a7 s& n. Z1 z% i2 c
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf: A5 J: t$ x! I8 W
2.启用OWASP规则:
3 n0 I" k4 J( N" _, x6 ^  q# E. C3 t4 U6 t
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。6 B3 A" V# y* ?& j/ N

0 T# D$ {# f3 F9 Y编辑modsecurity.conf 文件,将SecRuleEngine设置为 on( `6 u& v$ K( }/ N1 j5 G& s9 p8 n( j0 y
& @5 v9 O6 m6 \3 m3 o! B( E$ D+ K
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。" C( b6 h% g7 S8 Q

) G, {$ c, n. u  H- l+ y1 UInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf3 _, O* I$ u* D( t4 T' c
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf( l  C; ^0 ]( C8 n) P, ~' y: H
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
7 w( C9 x+ f" u0 p4 H  Q& r8 pInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf: Z) v( j: w' J* j
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf. v1 ~! c2 ^4 T, X& M6 Z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf7 F/ x5 L1 S* T% B( t3 G& m
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf- f* g$ j! \# }
五.配置nginx
( H* X8 D% v8 m5 S( I! `/ u. z) E- ~- m( t9 K9 R% v% q
在需要启用modsecurity的主机的location下面加入下面两行即可:0 f& T" m& S. [' h# o& k( \8 x
9 f. Y( i% L0 d9 \5 m; B) E& A
ModSecurityEnabled on;  7 u, Z% ?; N) M& i2 M% Q( w) Z, \
ModSecurityConfig modsecurity.conf;# [% R" U  _% Q/ O$ u' K
下面是两个示例配置,php虚拟主机:
) i, y, j* {+ M4 F& F, j! g, t2 ?5 C8 Z/ {
server {4 l6 K! g6 y  K8 b' Z. B
      listen      80;
  M( G" z8 k% [9 J- g      server_name 52os.net www.52os.net;
' N2 V+ }2 d7 d7 s3 O: z# |8 l* [     ; u0 o2 j3 _* a3 a# w. d7 b+ \
      location ~ \.php$ {
+ H) |* U6 T0 W/ I7 d# Z2 D      ModSecurityEnabled on;  " f/ _' v* g3 n' X. J) _) S
      ModSecurityConfig modsecurity.conf;
9 A3 }" v! x" U  C% K; L
0 t0 J6 {2 k- X. B3 T6 q% `      root /web/wordpress;
" R2 L! u0 ?0 c2 w% x# [3 ]1 e      index index.php index.html index.htm;
/ X: l1 Q$ d" ]0 V7 ?  , n2 F7 ^- h* c- M# N  T
      fastcgi_pass   127.0.0.1:9000;5 I/ o" f# E8 N* E7 w# O4 J6 t
      fastcgi_index  index.php;
4 a# \' `: u6 L- c: o      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;8 F8 {5 s' H* R- `8 v$ }) [5 F6 V1 t% j
      include        fastcgi_params;
: h3 h$ h/ c$ J9 ~+ N: B/ j      }
0 ]& ]* k; p# c" {1 S) H  }
5 z3 O& C3 ~# z+ N' vupstream负载均衡:
. x$ |. U/ }! ~% _8 N+ H$ w) f! k( x! L& U/ v8 |+ Y4 w: m
upstream 52os.net {
) l3 B+ L% R3 ]( N    server 192.168.1.100:8080;. u, O" |( V6 o6 M4 Q
    server 192.168.1.101:8080 backup;
: z# c" l8 c! Z# E5 q}
/ ~: d6 s* H6 L
' {7 q- g/ R# p: n- Gserver {* u) ~5 X- T1 l- z0 @2 j
listen 80;
3 R! Q) t* Z4 z* @+ X. t2 Lserver_name 52os.net www.52os.net;
8 s* u+ u1 h6 W0 W. q% b/ e( i: o; v. N
location / {% H3 A$ M; A4 K  T
    ModSecurityEnabled on;  
2 |8 z+ _. |# n/ ^; y# K7 _! U    ModSecurityConfig modsecurity.conf;  
: o- F4 A8 d. [& D' f2 E4 c0 w7 g0 E  y+ \1 Q
        proxy_pass http://online;$ p5 q& x  H3 L" Z
        proxy_redirect         off;
) F/ G" h2 c: V/ r5 L7 ?        proxy_set_header Host $host;
0 K% A' k% _0 s& O        proxy_set_header X-Real-IP $remote_addr;
! ]% F4 ^. _0 s+ X, [: v        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
: N+ \6 Q: R. V    }  P7 x6 P/ ^  F; t7 @( |* ~
}
# V; q: `9 r  s) e六.测试* l5 e5 h6 b' m5 f

& `! e& h! _; }# s/ N: M我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:1 d/ T* J6 T7 a9 Y( f6 N' b" k
% m8 G- F) \' y1 q* D8 ^
<?php
. `8 V. x) F  |0 y. }# d: A    phpinfo();   
) }0 q& z( u' w: p: D: n$ b/ Y?>
8 e; U. Y3 ~. `1 `* B在浏览器中访问:
/ P& u5 `8 N: W
$ G6 X4 m, r: Lhttp://www.52os.net/phpinfo.php?id=1 正常显示。
7 e9 q/ A$ A+ W4 W6 ~" fhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
/ e: n+ C5 V1 s$ Ahttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。# T" @0 p, P2 C2 h  f; A9 m
说明sql注入和xss已经被过滤了( x8 T1 a8 V2 b7 a2 z

7 W2 u( A. s5 ^4 z, A$ J七、安装过程中排错
) V2 _3 `0 d* ^) U: k. Y
* e2 t% |5 b8 Y7 D; L4 ^1.缺少APXS会报错
. h( ~- k" U0 B5 N5 C' d5 s) h8 _: H+ b5 O: u0 A! ?7 m
configure: looking for Apache module support via DSO through APXS
: l, }) t  v7 r: qconfigure: error: couldn't find APXS
  X/ d/ I0 q, V& Q7 M9 G. napxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。6 k$ e/ u/ q9 @1 F9 T9 o% C
解决方法:5 U3 {0 D8 d% b+ K

/ S/ z% r) J; U8 s  Z; o" Fyum install httpd-devel/ |4 x% W" K4 _8 R
2.没有pcre
- V0 j# Q8 g0 `4 y8 O
: H) i* [: p) a8 [configure: *** pcre library not found.
* ?7 f; K3 u, d' `2 @configure: error: pcre library is required
# n8 c/ k, f8 _/ G7 Q  O1 s! b解决方法:
1 _( B/ T2 j7 [* J" _7 s/ W! S7 X  u0 N5 p
yum install pcre pcre-devel5 P5 F, b* B& U
3.没有libxml2  Q5 v( r$ j. J# H, S3 F

! o: A, G$ X; K" k0 C0 t5 ^& S6 S- L& W$ ~. b# N0 [
configure: *** xml library not found.
8 C0 M2 F6 ~: `, S' `' ]configure: error: libxml2 is required5 r6 X+ E* k, H( Z( [
解决方法:5 t. h1 Z3 ?/ }+ J! M2 }1 d
1 a1 v- F8 f% F: f/ C2 e, ]
yum install  libxml2 libxml2-devel5 m! l2 X& O1 X' Q
4.执行 /opt/tengine/sbin/nginx -m 时有警告
; k- g6 {; |4 {4 ?; o  {$ h# @( ^# z, I0 J. |
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
, a! T: E, I9 ^* g# Q0 @0 J! r, d8 Wnginx: [warn] ModSecurity: Loaded APR do not match with compiled!' q9 |/ G* Q% A, I$ H( Q4 e
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
# i, M* @2 F* B1 F) E
/ K% j2 o8 `6 ~6 ?" B. e+ S2 ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.( [9 K% l3 l/ H# W6 A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
3 Y) j' ~, Z  D. D* N" c2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!7 o' V6 w+ y7 @& V; B* i0 I- q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"* C  o7 f8 h7 c; k; f  z  h
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"7 @6 c# x; d$ |, C( e
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
9 p  Q1 C9 h/ H- J解决方法,移除低版本的APR (1.3.9)
4 I. t; _$ L2 E+ u) p1 w1 u% @4 R3 M1 Z6 f! e, ]
yum remove apr: }+ j& a& x7 \4 P1 j* ~
5.Error.log中有: Audit log: Failed to lock global mutex
( N, t" C# z9 s5 }, H
/ j2 K3 t! t3 k, M2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     - S) e" k- c5 v. V  @/ t
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]+ n' w5 m& @* h1 C
解决方法:  @+ ?1 M# t, X
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:6 A! Y3 ?2 ?& N' `  _
; R! C. _' s0 P  b" m
SecAuditLogDirMode 0777! W7 v% l! l- s; ~/ G
SecAuditLogFileMode 0550* q- J. c( x. p# _  ]7 {# \
SecAuditLogStorageDir /var/log/modsecurity$ l" x8 P5 m5 U( ^8 u
SecAuditLogType Concurrent
1 N/ g. U" Y+ P% A1 W) b+ X参考文章:
- \1 u1 @% o0 f: `; ~- mhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX3 o: k/ z3 L6 R- t
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-1 04:41 , Processed in 0.072141 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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