找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13010|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
& Q9 b8 d6 L2 j3 F% L# i
7 d9 B0 [" [" ?, S2 e一.准备工作
" v7 y( c4 d4 Y  f$ T1 o1 m4 J. a( `3 y- U; m
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.00 e* x% E$ y/ h0 B( f
$ f8 p" q: f1 C$ z6 A6 p3 Z5 j
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
& |/ g2 R, c8 {1 ?3 S& I/ N. ]# R8 g+ F
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
6 [3 X2 ?" x' E" r% @! {$ B6 K9 X7 r9 ~1 m6 M/ @2 i2 w
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
; N& w9 k6 h) r. b- C9 `# J( ]& q/ v# h$ g* ^
依赖关系:' G, B# r. y6 G4 n5 ^( C9 ]# v
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:8 Y  c& [/ o0 t: {& M
' H( ]( n9 I' p# s$ L
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel5 j' Z- _) y* U3 _) _
modsecurty依赖的包:pcre httpd-devel libxml2 apr. U% E8 R9 D- g! H/ F) `# u$ n! n' F
2 l" }; f3 _* m& w( I% w
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
- P' \) r# q, b& \6 c+ [二.启用standalone模块并编译
% I- h0 u6 |0 G8 j; V$ F4 R+ ?" N1 i3 s! @- `( H( q
下载modsecurity for nginx 解压,进入解压后目录执行:. s) p* x6 D4 S+ s& L

6 T3 X1 F* b' I/ B./autogen.sh$ M: H/ C1 X$ s2 q
./configure --enable-standalone-module --disable-mlogc( ?) o2 o5 L3 w& [$ w& a
make ! J* N5 w% B/ z6 g& u
三.nginx添加modsecurity模块
) L  Z) M4 @2 e4 y) p" t$ O6 o
, w# o4 y3 v$ m在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
: ~5 K4 C5 M/ X& r7 }
1 t9 w0 h' ?8 k% {./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine2 W( B' t$ d6 D0 ^" }$ a4 k
make && make install5 C0 |: u) n, ^. O4 S/ K6 V. ?
四.添加规则6 O1 Q( @4 ?( g

- g( j; _* ?3 G9 Z8 m) vmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。! v& C- Y- i- B) c, m) @4 s6 C. w/ m
4 u. Z& A: Z. u% h9 o2 D
1.下载OWASP规则:
0 B$ _4 y" A6 F; ]
  d7 N7 ]5 _" w7 l2 f- ogit clone https://github.com/SpiderLabs/owasp-modsecurity-crs3 q/ e9 p# g5 }+ @- u
  J4 b( n2 [& y. A. \4 x
mv owasp-modsecurity-crs /opt/tengine/conf/' g; S" m7 p! S( q

0 N' r$ Y) t8 l! L4 Hcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
3 _4 O5 [" {0 m( c/ W2.启用OWASP规则:
3 g4 g5 x( Y( \$ j+ Q0 K( |; F$ a$ d. n/ W
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。8 L( w, ^% @; P+ ?7 w

9 z2 ]$ d2 I. J5 d3 n  u1 l编辑modsecurity.conf 文件,将SecRuleEngine设置为 on& K) e# G& X" e1 ?- w! k3 B
9 D) P5 M$ S9 q: ^9 @8 k
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
, Y5 |5 y: p( m: H# M: b! r' ^& t. R
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
- E* N: m. ^: L( \Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf* e! {/ Z: ]0 n) z5 T- \6 J: @
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf. d; i- p4 l/ k5 t/ Q3 R
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
1 u" m' k$ P( g$ _9 eInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
0 f4 Y' P7 W7 ^+ C+ E0 A7 J, zInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf( ^* J4 |" d/ a
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
( k5 `2 S8 c* K' i$ E, P4 w五.配置nginx% ?+ t& a- w" g7 T
& x) q$ l  p8 d
在需要启用modsecurity的主机的location下面加入下面两行即可:* m2 c/ {8 @( J! `2 S  c

, V4 x3 T& U$ X! d0 NModSecurityEnabled on;  
; k* a6 H/ {. _. B' T) O# o& nModSecurityConfig modsecurity.conf;. @' a/ g! b3 s6 i
下面是两个示例配置,php虚拟主机:9 D: T9 p6 V/ w' I- u/ H# t  k

9 d6 O2 {9 L+ V+ sserver {$ G" i3 Z9 `" _' e% E1 \, r
      listen      80;7 R, ^' j7 z6 B# L  ]+ J7 f
      server_name 52os.net www.52os.net;0 C, }, M4 A$ q2 m$ ^7 d1 f
     " e/ w5 A8 R3 b" u5 I
      location ~ \.php$ {
  g8 w' a* w1 R/ t' d* y      ModSecurityEnabled on;    C3 e& x' S4 {# g1 R, [
      ModSecurityConfig modsecurity.conf;
) M4 l. a' `8 ]% Z" c8 t
8 C+ R+ U* N. W      root /web/wordpress;' d  _# l+ E9 r/ G
      index index.php index.html index.htm;8 \1 ~0 v+ M$ E9 K0 t) W% C
  5 I. m# L. F9 f. b- L# l/ _. \
      fastcgi_pass   127.0.0.1:9000;6 B7 Z6 A/ P) ]
      fastcgi_index  index.php;: l) Y/ T  A' K8 K, _2 R0 k8 t
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;5 i, ^. \' N: v) \' |8 n
      include        fastcgi_params;4 [4 t' s- R+ [3 x! H
      }4 e/ u) f* V9 M$ u! d' ?
  }
+ d& }" V) B+ t; A! ?( eupstream负载均衡:4 q1 M1 V9 c+ }% ?& |0 H9 W2 `
/ A5 T0 u" K' A4 m+ i
upstream 52os.net {
8 R; ^* a" j1 e' n* N    server 192.168.1.100:8080;
  M0 p; n' I8 g% e4 G    server 192.168.1.101:8080 backup;% Q/ y  D& K  n" a* d9 t' b2 }
}8 Y% V' Z# z; E. e4 }

9 z' a* H! r" _! O2 O8 y7 Oserver {$ f. p2 `% W# ^- ?; d: T8 j
listen 80;
4 {6 j3 N9 Z/ ?. I: D7 Qserver_name 52os.net www.52os.net;9 `) t' O/ J) V- H; m+ u2 x! {7 v
$ x! y6 U. y+ S' o: t! n) i
location / {
( ?. A# f# N: v: R) a% s    ModSecurityEnabled on;  " D( B: J  t. @! X& |
    ModSecurityConfig modsecurity.conf;  
6 @# b( D9 {. L0 C) d/ ~7 g/ B# h1 Y
        proxy_pass http://online;
! q! h; u( n! T; g, r  \. s        proxy_redirect         off;/ |* G, l. v3 m) X. \, V3 Q6 M' X1 V
        proxy_set_header Host $host;
; x) k: t/ F0 K2 {! }1 {8 ]        proxy_set_header X-Real-IP $remote_addr;
' e" Q  C! }9 Z! Z        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
1 |0 L# z5 Z7 O! a0 F    }. n  h) \1 m7 v7 t) v; x; e
}
+ _, H& x9 `. a( T六.测试
4 o- s0 V$ j" z* N+ v1 X( e+ r5 q* N
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:: U, E* t8 Q/ i, e) m

9 w2 |/ t& P( D/ g, t% b<?php, i( Z% l( o; c+ l. k9 w3 k
    phpinfo();   
: T' n/ y$ F  l1 f0 d$ }$ |' u6 E?>) B7 {4 [5 o2 W
在浏览器中访问:, J1 m: f; X8 e% F/ [8 t+ o$ y

% ^: T$ u" L* S. N' nhttp://www.52os.net/phpinfo.php?id=1 正常显示。5 j1 m, B+ z8 U/ g. }. P8 S+ G
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。" ~+ R1 t2 G9 e9 Z
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
# u2 i9 S" T7 F& Q7 g说明sql注入和xss已经被过滤了
/ t! E. F6 U* I4 c2 q( {4 U, ?& L+ \& G+ y% }$ A: B
七、安装过程中排错9 X, u, [# X; u3 K

4 R+ ?% L7 z( I1.缺少APXS会报错- _5 v2 [# V. u: R7 y  A

9 v$ C* b8 ^+ N$ ~) p" Gconfigure: looking for Apache module support via DSO through APXS
9 J& ]9 Y. O* t5 H: g) sconfigure: error: couldn't find APXS8 f  W) |+ m9 N- W1 `+ D" d
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
1 U: a9 T) r: g, z5 m, ^解决方法:
& y5 a3 @5 l8 C. q: {( B2 v, `5 L
yum install httpd-devel
5 D' s! K" d4 d7 n% w2.没有pcre
+ b( [" X, d4 g9 r: ~! u  Z: R8 y" N- v% \% _
configure: *** pcre library not found.
+ U) E& n# F, Xconfigure: error: pcre library is required
+ W, s+ q  W8 V  h# F8 D4 S解决方法:
# I( t- c) J$ P1 C) ^6 ?( {. O
( A! l% @9 [. zyum install pcre pcre-devel+ h& m1 b! ?) u% F; I9 C/ A- |, s
3.没有libxml2/ b3 U: I; Y: I, |
0 ^3 y0 h+ N! p* r, K4 c& R

- }! y$ I7 ]) n0 z- Qconfigure: *** xml library not found.8 A; v7 p! s8 Y8 D( M6 b
configure: error: libxml2 is required
0 R4 L) P7 V* H- u解决方法:9 z2 L" {2 F9 Q& |! K

% }' R6 u$ l- ]  gyum install  libxml2 libxml2-devel
/ i! K$ |3 x7 y( U# G) D- f1 O1 x4.执行 /opt/tengine/sbin/nginx -m 时有警告
, S3 I2 R2 A* ^6 f. x2 z# C1 k# Q( n" _, O
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
$ |) S, p" S/ ]8 i" c7 cnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
2 P# K# ], M4 H5 f' C, z1 I原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
  s; q0 P  u5 e& S& c7 S2 r5 y# p/ K5 y) O' k7 Y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
" u) j1 _/ q* D- B, y! i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9", ~9 J8 H+ T  k+ }( x5 F6 y) M8 y
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
6 |; H5 m5 X9 d: y; H, N7 Y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"% S$ F8 S& J% u& M2 q$ i; h
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"  z* X! M4 V3 u
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
+ B6 Q. d$ q2 r7 I解决方法,移除低版本的APR (1.3.9)
  Z' S, G2 c5 U" Z7 R% u4 H) F1 [3 ]9 B/ d2 c' c
yum remove apr) l3 A9 T0 m8 @
5.Error.log中有: Audit log: Failed to lock global mutex; ]7 V+ e  m4 k1 l/ F( o
( @" g$ e% W* t' j
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
9 {1 Z2 X1 Q5 e" ^1 yglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
# r/ n! O' |# F9 l解决方法:
* G1 O9 @, V1 `) \编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
; I. [4 }; k* c6 y3 T' A6 ?6 N( u; f2 o& j$ o; _
SecAuditLogDirMode 07771 S* z- T* O  m& J7 i2 V* X
SecAuditLogFileMode 0550
* B# ]' [- [9 \0 ?  q4 e' a" gSecAuditLogStorageDir /var/log/modsecurity* F1 \7 g9 U/ o7 B) b/ b
SecAuditLogType Concurrent+ _1 j! b' E) A9 F7 t* r- t" K% U( M
参考文章:/ A) `- n1 ]) o8 R2 a
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX8 i, D4 P' \; u4 f4 {3 t
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-3 13:46 , Processed in 0.064356 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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