找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9970|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。  S' W5 V9 c' p0 D* V1 r) A+ K' z+ D
6 ?5 t* r$ ]4 z" o1 P" I# C
一.准备工作0 E: \8 x6 G; N. |, q

3 x3 h. \* E8 v) q5 u6 c' T系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.08 n( W7 }( |  F/ `. j

2 p; O6 {& k) W9 J. A& h, q- p+ C+ Dtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
$ D, P. `+ Y) S
- |( {3 p5 a9 bmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz/ a! d& [5 E6 ~$ w9 v

3 e: z, Q# ?4 OOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
5 n0 L- S" j& S! j: s' M1 G& p& f  w7 L: J2 j8 G" D8 @* _
依赖关系:+ I9 g3 W  Y: G- g( U* s/ k# J
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:$ b. @  Z4 o7 z" y

% F' s1 E$ u+ L+ n0 u' S& D) w& fyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
9 \! H  Z4 z* z3 B' m' Cmodsecurty依赖的包:pcre httpd-devel libxml2 apr
9 U( x( U5 s+ G7 v4 B; c$ }. E4 G% X, \' r  w  ~6 |" B8 X4 _# d
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel3 z) B; Y3 t# u
二.启用standalone模块并编译7 n) }  d, V- g, D& O- r
( E" ]$ O% X6 b- a9 \
下载modsecurity for nginx 解压,进入解压后目录执行:
2 y3 C- T* e7 v" Q1 J- y5 x$ `+ Q* ~% M7 s
./autogen.sh; t) h7 Y( j( g/ w0 e/ @
./configure --enable-standalone-module --disable-mlogc. ^4 e5 n+ e0 n
make
: i  R3 n' |8 W6 Z. a9 j% R' \# Z6 v三.nginx添加modsecurity模块
1 U9 w7 N8 w1 [: v" ?5 i) i5 \) m0 r/ I5 M0 c: U
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
- j! A7 F: n% \9 D. S
* s4 A- q0 D. F9 @5 ]2 }  a0 Z9 ]./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine$ }" \1 p! O: t! [  r
make && make install
1 j" P- c# M) \四.添加规则$ W' q7 H0 `' M; h- O
  H" p6 S' e, k8 t( U  N
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
# X. }* @: n7 ~) t" O
8 ]* [& f! K& J' R  v* r1.下载OWASP规则:; R% v6 E# y0 G+ ]! G8 Y
" |, O. G8 X$ j) f) {) m" W
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
# E) i! c0 r3 M' [1 S. W$ K2 K7 s1 ^! v. i  S  `! A& y  D5 e8 `
mv owasp-modsecurity-crs /opt/tengine/conf// u& K. F5 t) w1 ], E- c
  i" q) S. V5 ~( o; e
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf" T/ X9 ~3 q( m1 x
2.启用OWASP规则:) s9 X0 `5 L+ B

( k3 h7 b$ \' i  p9 S/ G' Z/ N复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
4 P' d7 J( Q- k# N4 N: J' A0 K: x6 b- A9 q1 o- t
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on# \0 V# `  N8 `2 Z0 f: q3 h7 V$ B$ j

; w( P# W" }* U+ Q8 q0 H5 B' powasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
! U3 K6 |0 Z: L* B  p" e6 G6 ~" S. @/ j, U9 Q/ x# A4 K% I
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf/ e- |' v6 \6 O# @+ P
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
  J' F2 a7 S' V) V( TInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf' v( f/ ^7 f* ^4 \
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf  O. Q/ \. O; W6 P% z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf( r0 u( i8 X7 d! ^5 ?
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf, _: x* [& x) |; L/ ~5 _* R7 t. C/ n
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
0 G6 Y% K7 v  `7 q# {! D五.配置nginx
8 l3 d/ A6 w, h: E
' `+ i# i; I. l0 y8 V在需要启用modsecurity的主机的location下面加入下面两行即可:( d8 g- A2 A* k9 ?! x
) u; d9 ~& w: y* m  W+ }8 F
ModSecurityEnabled on;  
" x# o, g8 t* ^" z% \: qModSecurityConfig modsecurity.conf;" v7 V$ h! m9 g' Y+ G
下面是两个示例配置,php虚拟主机:# B: S0 g9 S5 q8 v$ f# j
* `" u5 g- O% B$ J% g* _
server {* Z& l: O! l6 q  ?* C0 [
      listen      80;
! b  L8 M, Z0 Y+ i: y; ]      server_name 52os.net www.52os.net;
2 Z5 t- i' B2 _9 n     ! _9 O2 J: t0 N  V' G8 p* X, q0 v
      location ~ \.php$ {
/ G- x  m" W; }+ ^' `; E/ g5 F4 w      ModSecurityEnabled on;  3 `: _( Z2 Y  J2 c- J$ t1 ?
      ModSecurityConfig modsecurity.conf;
& b2 j/ m- h' j4 P; K3 R- L
) N% j- L5 P" q5 P+ o9 G( H. }      root /web/wordpress;6 V: V6 z3 x) P) X, e( g$ [
      index index.php index.html index.htm;" `: }/ n4 V1 c4 W( V4 e% j! s
  / X$ ~- E) @1 q' B
      fastcgi_pass   127.0.0.1:9000;/ ^# B5 N$ W% ?; T9 L2 ^
      fastcgi_index  index.php;
' }* n( V8 R  Y1 h0 o  T' K      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;0 Q7 E6 H' Y, b/ e
      include        fastcgi_params;
  i4 U, W& p( p$ y  ]& b      }$ I2 m% W3 _; V6 I- W$ R3 {
  }
) ^0 s& \; b4 z: Fupstream负载均衡:- Y# o, a, x+ n- w1 p- C
" i' F8 \0 V% t# O. M' D4 Y
upstream 52os.net {" b8 I+ R6 ]- F% W
    server 192.168.1.100:8080;% i& E1 ]7 [6 ?; J: b
    server 192.168.1.101:8080 backup;
! G6 q. C1 d  w8 z; T}
& p7 E5 E0 q% \' v* c; ~* }( n8 |0 x7 r
server {- y; Q$ @, C7 Q2 N% S6 R8 I
listen 80;
+ @9 y4 E. l/ Vserver_name 52os.net www.52os.net;
& P& A: X0 e6 F* H7 l) t' z) C( A+ @# G$ B( e$ T
location / {
  @1 p, |( p6 Q1 s. s, L    ModSecurityEnabled on;  8 a7 O: P. J! ~4 \2 K* _. R
    ModSecurityConfig modsecurity.conf;  4 k( Y7 D# M7 B( J/ t9 l! I

% g, C& u0 X$ x. s        proxy_pass http://online;
4 E1 g, P+ o! u3 f8 y        proxy_redirect         off;
* x$ a; o6 [9 R9 \        proxy_set_header Host $host;
- @4 C+ G- [& u* {( H0 \( L% }        proxy_set_header X-Real-IP $remote_addr;/ A6 C/ ]3 O" C$ J1 D
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
* d6 i) X$ M9 w2 P    }1 Z5 F* |4 s/ |% s2 F6 _
}+ j9 {/ f7 J& j& s# E( g4 X
六.测试( C3 ]0 [1 V: S# ]& o+ I/ ~
; s8 Z( B: u& o6 _, G2 r
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:# X# E% F2 _1 K' O+ m
- U6 J% o- U! b% F; }7 Z. Y8 R
<?php$ y; u3 _; K+ B6 Z5 N
    phpinfo();   
- K/ O6 m$ }- l2 K. T% y  F?>
7 W1 i# l- z: L7 @& L. `; N' S' I在浏览器中访问:
5 E6 o* J5 m4 V  m+ M  t0 b7 ]9 T9 z: ^7 Z3 G) w6 `1 e4 Z# a1 T
http://www.52os.net/phpinfo.php?id=1 正常显示。5 d# |1 P) A6 o6 B
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。* r8 F( f2 r0 ~/ n
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
6 s/ V* q7 K2 ?4 _# p) j说明sql注入和xss已经被过滤了1 g2 X( F: A% Q  Z* K! {, Q

2 d# R# ~9 V4 G! O* R七、安装过程中排错! j" \  F  S: t6 B4 P* I1 e- D
; s& r. |. _4 C, c# U
1.缺少APXS会报错: a9 O8 v! N; _- O
6 e" p9 u. t/ r' Y
configure: looking for Apache module support via DSO through APXS
7 Y; B6 J+ M. p! x* d3 k5 Iconfigure: error: couldn't find APXS7 Y3 V9 ]' R4 J3 |& i
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
1 Q8 @6 j% M. ], ~. K3 N5 k3 Z/ v2 P解决方法:
" W7 j0 N. I  Y" j5 d
4 S; y& F$ p1 c! v# pyum install httpd-devel5 q, c: G) J+ b8 Q
2.没有pcre
) _& T$ C7 p  j+ [5 U+ z
& I5 z. D, k4 s5 K! zconfigure: *** pcre library not found.1 _+ \  t- }+ Y. o+ l
configure: error: pcre library is required8 N1 @: J" E/ X- A. ?
解决方法:
" P% `- q- s' |: D+ t
4 {: {6 W$ d/ J# P; C0 Ayum install pcre pcre-devel, d4 k- j$ U$ v+ C* C9 T/ e
3.没有libxml2. ^5 N  V. t* D8 q- M% O

4 `6 E) u+ |: [! K: w
: j0 I8 i! z9 U- Qconfigure: *** xml library not found.* S; _( U5 u& N8 z8 V% M0 y
configure: error: libxml2 is required" o/ a$ S  t# x) Q2 s8 p5 J
解决方法:' r+ s% G4 M# P! v$ n
, D& o2 R7 y6 d2 u
yum install  libxml2 libxml2-devel: c; `9 V2 Q8 a6 q. C
4.执行 /opt/tengine/sbin/nginx -m 时有警告
7 N( N5 |/ w$ I  S) Y! b8 \& T
( n* n# ?  Y  c# h$ nTengine version: Tengine/2.1.0 (nginx/1.6.2)6 P4 e! }9 p% A; O$ r" m& W2 `- ~
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
, k: y4 X. [9 B/ l% g2 {" F* X原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
3 F  |' Y: }" ]% @
4 c9 c9 _- s+ p# j+ ]2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
5 K4 Z6 z% O  B! y* g5 c$ t2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"" M% N' q' ?( Y- A, \4 I
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
1 ]; m; Q6 s0 m# C/ @2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
/ d& R2 D1 ~. R2 P0 |  J' [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
# |* c; W% P6 x0 P5 Q: Z( q* D2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
. R3 X& W/ ~. F) ]解决方法,移除低版本的APR (1.3.9)8 E4 T' F8 p- g5 Z- A" r- s9 M

! a% U! X" c4 S4 }) p8 ]7 Jyum remove apr+ ~9 A% L* b* N+ P
5.Error.log中有: Audit log: Failed to lock global mutex; W3 w+ m9 ^7 {; ~

( w1 N! J+ N2 i. T' c2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
* Z8 D) o% T6 U2 v" Hglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]( V2 c* ?. y6 W+ Y6 c0 }
解决方法:
, q6 m+ T% ~9 z% P/ I编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:! M) e* D- I! Z8 `5 U# n; |8 f

$ q) D' I- T- V; d8 f8 S% zSecAuditLogDirMode 0777
0 l) s* y7 c: Q, o* ^SecAuditLogFileMode 0550
8 }$ X, S9 J$ U$ d% BSecAuditLogStorageDir /var/log/modsecurity
$ b8 v6 D  {3 s- L" ^' \# u4 _SecAuditLogType Concurrent! t1 |) [' `9 V, X
参考文章:
! F) a9 w; \' ]2 F0 u# [& M0 `https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
$ I, O' K) F$ G# z+ ^# \http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-2-17 09:23 , Processed in 0.065771 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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