找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12900|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。1 y6 k8 W/ p  P! U

+ h9 r, H) \3 U9 Q  g- O一.准备工作1 P, T0 V- _  d, Q
6 Q+ J/ ]( g+ s" y1 }: z
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.07 @7 |4 k* |- a: [) B; A* C- J
* O  f9 z. |  D1 b' ~/ ~
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
$ C9 P4 q: }8 p% O1 C
# @+ _9 F, ?/ |; p: O, x" h# q* emodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz% i+ A& z* r5 s& }' t
  @6 ^0 L6 s/ W6 h
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs- P2 l% Y6 e$ p1 s( z# \  `/ S

) S; t$ |8 U1 K依赖关系:
1 l0 s% H6 B7 @1 {" h+ \9 M2 B3 vtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:  B' I2 u6 h9 c8 b$ p8 ?
% f0 w9 f2 r6 q
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
' W0 _2 t0 Q) Z, g( ]8 Mmodsecurty依赖的包:pcre httpd-devel libxml2 apr
6 w) d$ T+ B" F$ w1 _6 p
& `- e/ N% h: q# ]& ]- N5 syum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel1 ~" x  d  l: j% d
二.启用standalone模块并编译. L$ X" V; k3 ?; d
+ i& M  |# G& J0 c: H2 K
下载modsecurity for nginx 解压,进入解压后目录执行:
0 _* Y3 K4 F1 N  i# r
/ v# z2 T" B, O  E./autogen.sh7 i* X% Z, [3 e$ \, Q
./configure --enable-standalone-module --disable-mlogc
; Y: ^1 u; J8 n. S, k) c1 }make
% T* i7 K7 |: J三.nginx添加modsecurity模块
2 v4 M9 m/ R$ ?" o* v' w) y) \4 S' L0 U; U. F6 k3 q: K2 y( t
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:$ e# W, N& l% k0 O8 [2 G
8 g; i% h* S- }' [$ N5 j5 @
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine* y) H3 o$ [7 ~1 E: Z- [* |
make && make install
& ~+ e& W! B4 F% G四.添加规则
5 @7 d/ z$ y/ r' H& h8 G  t; O  W+ `/ s% p6 ^' C- N* i
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。" k/ X- o% n! k' k
8 M- M9 Z8 V  o' Y  s4 h% c. b
1.下载OWASP规则:# e, L) M3 A' f+ O% E1 O" y

* |' d5 ?1 w* d' ]git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
9 ^* g# H$ ^! `( M/ Q; M  J) x# a5 T  j  R& Q4 t' `; Z$ K4 V. y
mv owasp-modsecurity-crs /opt/tengine/conf// K) l6 q, H0 W, [/ P

# L' u- _& a$ a  p5 ^cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
5 X+ U  u* r. g+ g- ?1 Z. }' P: M2.启用OWASP规则:- z$ `7 L7 b- w/ [% _4 n8 r, r

) l) h9 v- q2 v& v$ x- k  `4 ?8 z: G复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。8 j+ Q) Q5 U0 f

% [4 v6 D8 \0 D: U' }- \: ?编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
5 A% N& s' U! S: D& n2 T" g
+ V( T* W0 ^5 \0 i, I* i2 Z) qowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。, p$ h( m, v' k+ y" D4 d

. j+ S4 N% P0 EInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf0 r: V% V6 k. `% g# l/ ~1 X/ K
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf. ], _9 {: g0 D9 q4 A  @8 E
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
' s; e0 j8 r) U$ m4 K& _4 dInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
0 L) [) Q$ s6 ^! VInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf1 [( ]+ _5 H! m$ C- i! u, a
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
3 e) J  T( i/ fInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf7 a  d- k1 \7 D. ^
五.配置nginx
( U+ r8 O) `. K# t
# ?: ?" \2 O  J2 {# u在需要启用modsecurity的主机的location下面加入下面两行即可:" i0 Z0 v8 y1 R: V* x# T: H* w

) S. X% o( ?6 R' [- Q0 A7 V+ RModSecurityEnabled on;  9 ?! E, B5 z8 v$ P# b
ModSecurityConfig modsecurity.conf;8 G, b: q. V9 l8 s% |5 x: V$ R( l; Q
下面是两个示例配置,php虚拟主机:) o" n, N) `+ v, ^7 p2 p
5 [4 t8 d5 b3 L3 o! C( g6 @: h
server {
, @0 p& R- |! {      listen      80;
% a! u7 N) i( r2 E  N: s. B# t      server_name 52os.net www.52os.net;+ S/ ]9 Y( I9 Q' u+ E/ ~9 G
     ) V* P) Z5 R1 `
      location ~ \.php$ {. Z, P2 Y# |4 z. O: e4 q. }
      ModSecurityEnabled on;  . z; f" L  ]) [; r
      ModSecurityConfig modsecurity.conf;
* v. k  C: g; `  f$ b) l! |' `. j; W9 K% l3 X5 Z
      root /web/wordpress;& Z5 `3 E( @7 b7 }. t# I4 i
      index index.php index.html index.htm;, F! H# G: j- \( `0 K
  0 V3 g  h7 }% S- r+ U
      fastcgi_pass   127.0.0.1:9000;
! Y2 Y3 U- l0 S) q' {2 w; m) |  z6 V" A      fastcgi_index  index.php;) P  {+ R* G9 S9 o4 q  A
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;9 X3 N6 i2 P6 k6 J1 t+ N
      include        fastcgi_params;+ _3 R" b8 w+ P4 b4 M' j+ d
      }
0 V, y) Z8 Z5 G0 c2 ^  }8 T  `: r+ L, ]3 h; ^( U
upstream负载均衡:
+ a6 ?7 q$ c9 N5 f) ?2 I) G1 k' ^' A
upstream 52os.net {2 @8 G1 `8 S. U+ B7 K! a
    server 192.168.1.100:8080;; T2 Q3 Y5 @: H6 G' t
    server 192.168.1.101:8080 backup;
* b- ]( [6 E4 R0 A0 Q4 I}
  \) b9 w* H7 E% a* |0 O. F# Q$ {/ ~, k) D. H6 L7 b
server {+ R: F: i# w2 G1 R! C( c
listen 80;! S4 n; Z5 q. X. {
server_name 52os.net www.52os.net;
; g1 W! e/ p. _" S6 U
1 w0 V3 b% ^1 z5 y/ P! j  W0 `3 Klocation / {
% ~' I* o" B' Z! p( u9 P1 z% k    ModSecurityEnabled on;  8 L! u  V( `: N) S& J) @: L
    ModSecurityConfig modsecurity.conf;  
' R0 x3 V' M) N* q" C7 _* H- V4 J6 y( h/ K: W6 q9 f
        proxy_pass http://online;
! v" Z8 F7 ], F& x        proxy_redirect         off;2 k: }* p. S- {* L
        proxy_set_header Host $host;/ m5 O4 c4 g$ i$ d+ C7 G
        proxy_set_header X-Real-IP $remote_addr;0 l3 k. L# a) e: t3 V
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;. J( m# u7 v0 d3 K4 q( R
    }) x  b5 ~) ~, n2 @9 u, a; }
}- ~3 ~* V* v. y9 G8 Q
六.测试1 M) h/ |& `) {. R% S1 ^3 @

" q$ f. y2 g& X; }: w% c3 ^我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
8 `: u' h7 q  y4 F- T9 m/ R2 J: \) U$ w- d
<?php
" M0 Q7 K. u/ v  S( P+ f    phpinfo();    7 v+ r+ l* g) y* ~1 X8 g) Z
?>
2 _+ v1 `, i3 l) L9 S2 R3 ~# p在浏览器中访问:
9 ~; p! L7 _. b- ^1 R
& E+ [2 Q+ s, [2 W" J0 Ohttp://www.52os.net/phpinfo.php?id=1 正常显示。& U7 I% U) Y9 X' c) M, d. F
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
$ X' K9 i8 W2 z) vhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。4 N* C+ g$ L$ O- E; T& a5 g
说明sql注入和xss已经被过滤了
& W7 j9 k6 g7 b. \
. S# f' i3 W5 w8 ?" q+ i( t七、安装过程中排错
4 N* {3 O: ^  M  m8 Z, a+ f1 `+ i0 b2 [/ {
1.缺少APXS会报错
' N/ J$ L* Z; G
7 ]7 M% ?1 g9 _* Dconfigure: looking for Apache module support via DSO through APXS
: n, `/ K/ T; e$ pconfigure: error: couldn't find APXS0 l" I% E. v7 F% F
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
( d# p/ o4 g. K: D解决方法:, a# M$ L) @. v) C. F2 [+ o
) l) M/ N. M3 A' u2 e
yum install httpd-devel
9 k& C' |9 j0 _6 Y2.没有pcre: d) \: w4 C5 v% D* S% P

/ C3 d& A* p1 b1 Nconfigure: *** pcre library not found.. z  B4 W+ a" R8 Z/ R' ]; ]6 s/ l9 }' n
configure: error: pcre library is required
  c% J* @) {; G; _解决方法:1 l/ u2 a  m6 U: D) E
2 d  B0 J* `( A8 X' J: A1 ?
yum install pcre pcre-devel/ L% O' n7 |9 _% u+ {
3.没有libxml2
) m$ S  o* m1 ^5 t! f8 U" [
0 `5 J7 A, ~1 g/ f) r& i# F) w. x0 e) }+ f
configure: *** xml library not found.
& n8 r  g3 p1 u1 Yconfigure: error: libxml2 is required) f) z$ v0 x2 o2 U
解决方法:/ l; u2 S  `# h6 Z7 r
. L3 W6 G4 W3 j, ~" K6 m! F6 C
yum install  libxml2 libxml2-devel1 K+ `. Q  G4 x8 C2 I) N
4.执行 /opt/tengine/sbin/nginx -m 时有警告3 w4 z7 e7 l6 Y7 l

4 R* p: ]. @. x" W5 }0 h8 ?8 pTengine version: Tengine/2.1.0 (nginx/1.6.2)* I2 v- a$ U1 r* c, F3 ?& g: l
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
2 [. J- S5 s$ k3 W& B* p8 v# F原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
  I: J- F# d2 o- J8 H
6 l9 Y9 V& B0 ?7 `2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
  f0 Y' r4 E: {9 i0 E+ C2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
' a0 v! ?0 W( g) Z8 H2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!) r$ X$ w( [7 t, G  g5 B8 K
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
. D+ U* U% N; F0 w2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"$ j, U# [2 w: B
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
9 O2 k5 [, A3 e* f5 Q5 {5 h2 q解决方法,移除低版本的APR (1.3.9)
# N. p8 M/ A+ B
5 N+ T9 o0 |) S6 X2 R  Ryum remove apr
0 M. d* E9 r/ q, ]( k5.Error.log中有: Audit log: Failed to lock global mutex3 R% K- S! i; B1 Y# f
: u8 n$ U# e8 Y/ R
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     2 A. L: |3 \& J0 A/ ]
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
0 \8 Y: \- L" ]  N" K) T) p: ?解决方法:, B+ @4 l3 L/ L
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:% n% h+ ?8 j4 y, \+ s* f

; Y6 q' [8 {  e8 QSecAuditLogDirMode 0777
7 b/ j6 X5 L. O2 z3 SSecAuditLogFileMode 0550
. u" z: K; d3 m- ASecAuditLogStorageDir /var/log/modsecurity
) w8 B1 P( D% [* z0 h. ~) s8 c( d4 tSecAuditLogType Concurrent
% s3 o+ |3 `3 g7 Z参考文章:6 z; q& L. D* c4 P- F
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX5 _! U3 u3 l; Y4 U  V4 [9 M0 {3 C" d) h
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-24 12:18 , Processed in 0.091700 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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