找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10070|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
) k3 Y3 D# g( Y9 ?! u( i; X: f, m: o9 G1 H  y8 F$ T) s/ o
一.准备工作( }' g8 c# Y8 d" w1 q( Q: B

  W" @1 I; Q3 M& a6 m, k系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
4 N6 y1 k+ l: y) R, S8 K. M: k
) R/ ^* P9 v! q# ~; g: \  S) ytengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
+ X$ w! j  E2 y/ O" ?4 n+ O9 F6 ~( G- w) z: L4 U  L
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
/ T- |& e( @& Y- ~3 a: N$ a; E  L# V( x0 u% `7 S3 c! \
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs3 [3 b) z! d: w/ B
6 j5 e& h$ m4 w4 v0 I* y, E
依赖关系:
' @, w3 X  E- u0 Y/ s/ }- Ttengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
- ?4 g" M, z' \. s8 p0 S' W/ G1 H, J3 U1 S3 F" k
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
( ?, l; j( e+ u" X: U: Vmodsecurty依赖的包:pcre httpd-devel libxml2 apr
9 p2 A7 c$ V' h$ m8 r* A3 B% k
1 @2 k5 ?% M2 qyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel3 m. }4 h9 v% T" {" m% n, m
二.启用standalone模块并编译
$ H4 Y7 i! A8 E2 M; b  z
9 Q: c# ~$ _2 K8 j/ C下载modsecurity for nginx 解压,进入解压后目录执行:2 Y8 s  n8 W* [. I# ^
# D/ Q3 @+ s  \/ I8 l
./autogen.sh
6 @6 S' L0 \: Z- w./configure --enable-standalone-module --disable-mlogc
$ q2 V  o, h, j( X6 L6 U4 zmake
; }8 c( J1 k8 w7 Z+ j三.nginx添加modsecurity模块
/ p: h; s. C+ `4 S& n% r5 Y  o, a5 A6 \9 U- i5 j5 w
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:% N7 j* {3 o9 `9 v/ N: {
) a- h5 U" y: Z0 C2 N! P
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
+ f! E# [4 B% I0 Amake && make install
# w  e, _$ b& w6 f: a4 c* W四.添加规则
2 J' Y9 A% ?+ t% L- y( ]! R3 W2 U( I4 p4 @3 `! N; j
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
9 K3 T/ M1 t0 t+ a* D0 D5 k9 S$ F8 j3 [5 w
1.下载OWASP规则:
3 {3 f8 J9 N7 u5 i- r$ x' y
& g  }$ l/ D- a1 l9 m4 l& ]& Ggit clone https://github.com/SpiderLabs/owasp-modsecurity-crs' R( G, p! _  q9 d3 _* V" }

6 t! s! @8 e+ b$ l% W6 {& M/ gmv owasp-modsecurity-crs /opt/tengine/conf/! f5 Z% B! l0 b8 ?0 c5 h' p
5 S% X: i  M- U- B$ S5 O
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf4 [6 u4 Q' Q% x% u+ K
2.启用OWASP规则:
% B7 F- e  p" M; G3 |8 n
  A, X, \- {" A: d# ~9 I$ x复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
# D8 M7 j5 v- D. X4 A
+ s2 M* M1 k0 i- J1 p0 V- H编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
! v% u2 Q- n) Y5 m: u0 L/ O. B9 x1 W* a6 s5 D% v
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。, i$ A) s  s) k* Z9 `& I. j
5 {" ^9 W& l9 Q; p3 G0 L4 C* e
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf* b2 c+ T& U' o7 E/ T: h
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf  U% B/ d6 C+ u  D, A- P4 q; }
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
& o) i' |2 P. X' ^) PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
' ?* X( r. Y- }: k; ^" I" w) o( BInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf& C+ ]' B+ ^9 T# Q8 U7 T$ s
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf  P$ ~& n4 K4 x. t7 H5 v% @
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
  H6 t* O7 @. S0 \) ]五.配置nginx& k' X$ g9 v: L9 s7 q5 k/ E" l

- i% W6 M( E: j7 r$ }; U7 J在需要启用modsecurity的主机的location下面加入下面两行即可:; X: H2 r8 \: Y2 a$ x/ o  q# m, a
2 a2 |" D. F* x* U3 e
ModSecurityEnabled on;  4 ~# ~) K3 M8 {
ModSecurityConfig modsecurity.conf;
! I: ~% K5 J" q0 l  b下面是两个示例配置,php虚拟主机:
& D6 Q# i5 v2 I3 K7 u+ [7 s$ C+ T
0 a1 y7 }9 |2 H2 m1 Nserver {5 A) z) S# I, b  _' ^$ G
      listen      80;! @1 `+ N& g0 v7 h* e0 R# R1 E
      server_name 52os.net www.52os.net;
, R7 H3 |5 B8 M- ?* y" j     
" ]+ M8 C# ]* u6 l9 \      location ~ \.php$ {, d& A' Z: j; P- O" s* V6 u( s, B
      ModSecurityEnabled on;  # v) m4 q0 Q, ~
      ModSecurityConfig modsecurity.conf;$ n6 M. X3 x7 g3 d0 X) v: S
( W) Z# [: E5 t3 r0 @8 Q
      root /web/wordpress;9 ~) H0 m& w5 }
      index index.php index.html index.htm;. m. ~) D* d3 A* D9 B5 s
  8 R& G$ T# _4 y! C+ f5 x
      fastcgi_pass   127.0.0.1:9000;( L( _- k7 F7 H  q% X
      fastcgi_index  index.php;/ l" S) t& v  [
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;* j: U# z& K+ a" ~; Q
      include        fastcgi_params;4 A9 `$ O2 k- z  P3 E
      }+ l6 O& E7 ]9 b8 D5 b/ m. H
  }
* M  B# t" S$ Q' R8 `# Mupstream负载均衡:2 }- k1 x3 M2 |7 y" T2 T
9 ]' Z, u0 d7 O  E5 Y  t/ v
upstream 52os.net {( X  N, W! O+ g& s
    server 192.168.1.100:8080;; k; M" ~1 H# c3 G+ A
    server 192.168.1.101:8080 backup;
7 |4 d! B$ V( v0 L! q+ [' R}
2 Q& Y2 B/ N, _
  p8 \8 b0 h; a) U$ k: v) x; D: kserver {
# J' N  F+ q5 |! `6 [% Clisten 80;
8 d& ]" R( Q5 r6 hserver_name 52os.net www.52os.net;
. j# D6 o) D7 t9 @4 ]) K8 R6 B, M* g5 Z# {2 @$ ~
location / {
2 X6 R: F; H, b    ModSecurityEnabled on;  
4 Z2 C' Z: \0 E0 q  C    ModSecurityConfig modsecurity.conf;  - G" X' @) {5 d3 Q' c$ G7 A4 _
- a2 D1 y: R* O" S  h
        proxy_pass http://online;! S1 W% p- ?' a1 b; ^. q
        proxy_redirect         off;
! j+ ^7 R; G( }' r0 R        proxy_set_header Host $host;
$ j! X) R( N3 D( ^1 y$ f        proxy_set_header X-Real-IP $remote_addr;
# I8 l( ^4 r2 j' k4 q, d        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;9 _4 S% |( U& N3 @# x
    }4 k5 Y. Q9 ?0 G7 r. A
}
1 p/ g/ O+ U/ y2 B* \  {5 ?六.测试
/ s% c, s, d, e4 Q" s
$ `- Y2 A: g- E6 d$ m我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:; K  q8 t9 E$ P* G. }# O

- K" \# C7 _  \0 Y4 l5 @5 Y<?php4 G2 ], Z* X7 C) u& d7 m
    phpinfo();    # G( E4 R& T/ u8 Y
?>& l* d! _0 z6 s
在浏览器中访问:$ L( E: M: w. q; b. M4 k/ J/ b+ L
0 a- T. P+ Q6 Y
http://www.52os.net/phpinfo.php?id=1 正常显示。
7 q8 T  e2 X5 P: x( Ahttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
! G5 s# |, B6 ^# `  s/ Uhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。8 j' w( R( {: }. h
说明sql注入和xss已经被过滤了7 b, b* X, r, p$ }# O
) s0 e6 N' y* c. |# ~
七、安装过程中排错
3 L; |  J& r8 m$ n7 o  z4 E* m! N$ g1 k# \( t
1.缺少APXS会报错0 A  p  P$ _: D2 f
2 l8 l; g7 O* A
configure: looking for Apache module support via DSO through APXS
* v/ w9 D( y! S1 _+ H8 n" z1 bconfigure: error: couldn't find APXS
& K( N, f: B. x: i) {9 Zapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
/ ?& u2 E) \5 S5 c' q解决方法:
" W) }' D% b6 |. p* H: n# J! m% [3 I8 K; {" X* z
yum install httpd-devel; a4 n+ J6 J( V8 |# y0 ~
2.没有pcre
# z0 }! B( s$ a" |6 w  E' d* j1 v7 B' z9 P$ v, O. I3 E
configure: *** pcre library not found.
; t6 L* M1 U0 {/ uconfigure: error: pcre library is required( _9 \/ r% g/ u( a. C7 M
解决方法:
0 [8 q- ]" t, {
7 m2 N7 b7 Q, w3 _+ vyum install pcre pcre-devel3 L5 T# k& Y, H4 R4 ?4 w
3.没有libxml2
! W  ]5 _4 ~" t1 Y
7 ?" I% x, `, Z8 e* F
& X& x. o' ~; A! }6 j  f& K! B* econfigure: *** xml library not found.
' F1 V3 T1 f1 s3 L. A! e; m* q  ~! Qconfigure: error: libxml2 is required
3 d- Q9 g3 J) O0 d3 i. h0 |解决方法:. A; J* ^3 m" q5 i6 W- U

3 r% @0 T5 N  x1 z" [( ]yum install  libxml2 libxml2-devel
9 A0 S3 ?$ A; e% _& k! A: w4.执行 /opt/tengine/sbin/nginx -m 时有警告
  Z- }( f1 F% Q5 o5 B8 _' ~: L' P8 g* E( A8 v" P! s) H3 I
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
- Y! M9 x* W" inginx: [warn] ModSecurity: Loaded APR do not match with compiled!
. Q' }# r0 l4 z* S% \; [0 a7 j原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
6 \$ B1 v7 O: ^- l( L! a4 w* u& k$ M# }8 v( p
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.# {& g, q/ g7 O& N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"8 W& g7 J* {! O2 z: g: `
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
0 G' W4 ?) a; m' C9 {7 {. z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"- }9 \' l. H3 n) c' T& |3 V( Q% v
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"2 s6 H  F+ u( A  `9 Y1 Q' I
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.# \) x9 S" k* p' W
解决方法,移除低版本的APR (1.3.9)& |, Q: H) A1 B# Q; v5 q
" L9 o) ^/ N1 |8 q
yum remove apr5 s# @8 o" v* H* U- Z
5.Error.log中有: Audit log: Failed to lock global mutex" d! C0 |1 o. [: b

# Q6 M" ~6 T) k, e2 J8 W2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     * d' K& @8 R4 r' J8 c- |# M$ a
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]7 H. f) `) A% c6 v
解决方法:4 G4 P; Z* G! {+ n
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
6 d$ o4 U' L/ \0 l. s% ?2 U# F
0 H1 b) J9 z" e* A. X6 B: fSecAuditLogDirMode 0777
( Y6 X6 t) j4 R# `! b) Q" zSecAuditLogFileMode 05503 V$ X$ g" _7 z$ n$ v" C9 i
SecAuditLogStorageDir /var/log/modsecurity8 P( ~5 j" t& ]3 d
SecAuditLogType Concurrent9 Z) ~5 y$ N# y
参考文章:/ b  l" o( e/ K$ p& x1 d; u/ A, E
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
) [, T% p- E, d* O, I) C, K, ahttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-2-25 22:52 , Processed in 0.073483 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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