找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12583|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。  L% E, c& ]) L; X( w" c& G

4 ~& j4 H) X! `* [1 o7 E一.准备工作! @" E  R3 P5 h  Y! y
6 V% N/ Y) ^6 I6 D# M
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0# f0 n9 ]/ _- F' B7 t, s
4 P7 m. I, B! v& L% j0 n1 ~
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz5 f8 x4 Y% R, L# x( V  ]

  L( B+ s: m! ~0 D$ p* wmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz' a$ g- T! S1 I4 X$ R" I' ?
9 h3 Q* v& D# z4 Q% b$ ^" [
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
! {% a* z; w. S1 F
/ u6 v: N4 V0 D# r8 u7 ?8 i5 x依赖关系:
5 i  X# g1 C& o' t$ g3 d+ l4 e( Ftengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
+ @% F- C; L$ B  W6 O9 w
, |2 c0 R2 n( M8 ]' }/ ~yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
3 t# l+ Z4 i& s1 N9 W" j, p5 x/ H& Q; @modsecurty依赖的包:pcre httpd-devel libxml2 apr
( _2 O6 o2 p1 E. d) c0 F9 m( M% ]3 z5 X' C: P4 @
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel! y/ C6 f+ Y. J% P& B5 F  H
二.启用standalone模块并编译) e( n. [! D' X5 Z6 x
4 n9 ?3 N. O4 t, y/ O7 b
下载modsecurity for nginx 解压,进入解压后目录执行:9 }0 q& K/ N) l
; A5 n' b# d# j; W( j" O
./autogen.sh6 t! _) K9 c) U! ^! ^( V* i
./configure --enable-standalone-module --disable-mlogc( o) ]) c! Q3 S, C
make " A% J0 s) `" S% S% H) i" ]
三.nginx添加modsecurity模块  x- B: ]5 b# v7 C! Z

! ~3 E* A( T( Z% ~- ]$ ]; i( `3 h在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:  c* l; ^# o/ \: h0 k
8 ^7 r+ y% U# J4 s% C3 t2 @2 f7 h' W: A
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
& B0 N! m0 \3 \' {make && make install- F2 j1 _) P' t/ g; J1 s$ l. W  ]% V
四.添加规则' K7 M& S% h) X2 w0 P8 _
; t: G7 ]7 k% k# n0 h
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
; b6 F! S5 q6 @' A2 \5 G* q) s4 ^3 s4 D$ B
1.下载OWASP规则:
# a& V# v8 e1 @$ f! c
, T4 ]! r, G/ a& t; Dgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
! V4 u6 b& p5 ~3 r* P5 H0 s1 r  }1 P( U, g6 C
mv owasp-modsecurity-crs /opt/tengine/conf/
4 O4 @2 t9 ]( w1 c8 Q
& q* H! [2 X& a( P  x* g* gcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf8 O5 W2 T1 h8 [
2.启用OWASP规则:, X+ i; p6 I1 ~# K- p9 Y

1 j6 R; e+ e: i$ U; z2 S* s; T复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
6 i# w+ }2 W; J+ L: l! t$ |2 ~- x+ |7 P
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
( _) W2 V, h* v& K- J" h6 W0 w5 u7 V* t9 I. S5 x
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
( ?% p, V0 ^; q6 i7 J
( p0 [4 i3 j5 }: t6 vInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
9 H* M' }- K, S, {: h# JInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
+ p& i8 }  M( o$ pInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
$ \: r' x- e6 k' D! T* S; L# xInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
; A0 J# \# w0 K3 B5 P6 QInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf5 u( T/ I$ @  O1 i8 T6 @* q
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf( d. ?7 a3 l5 R( i4 T
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
6 j! v2 [; l) r0 j* }3 t五.配置nginx, h8 E/ a9 n  p# ^9 J2 z* B' ^
( r$ }7 J/ z, l' k  E9 K8 v6 H
在需要启用modsecurity的主机的location下面加入下面两行即可:
9 a5 A( S! c  A* K1 S& e& x0 I5 ?  \( b* _- a) u
ModSecurityEnabled on;  
/ h+ F& ~6 U" q, c/ OModSecurityConfig modsecurity.conf;
& z8 ?, y0 Z' }6 w" P下面是两个示例配置,php虚拟主机:" U; b5 |/ X" g; d$ ?( e* A

$ W9 K( w- w* ^2 E% Fserver {
% n( B. @5 {- m0 U3 U, l3 z% i      listen      80;
( ]( C" c5 G9 ]1 p) k9 o; D      server_name 52os.net www.52os.net;) W! p5 P" Y1 y6 }8 T0 X0 W
     
/ D' g5 l  p: @8 i% _) v      location ~ \.php$ {
' S$ B5 P' X% p( b) W      ModSecurityEnabled on;  6 ~/ \  f, X& \: f" D0 f
      ModSecurityConfig modsecurity.conf;
# q. ^, x, L2 F1 P1 l+ G( O* e, q
+ b: u2 @( C$ ~      root /web/wordpress;
% S8 H, u2 L/ j& m      index index.php index.html index.htm;& T- o8 u2 O# P$ O
  ) U5 u$ A) U8 }9 ?* O
      fastcgi_pass   127.0.0.1:9000;1 P) ~2 X, Q  w+ @: p
      fastcgi_index  index.php;
$ ~# v! \2 l! S& p      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;5 l2 X1 i: V3 `0 n5 U
      include        fastcgi_params;& r$ m$ f' K1 Q9 h* |7 o
      }
; _/ i2 ]! H8 }* v; O  }+ f/ M: F( U4 C2 k
upstream负载均衡:
: M* w: B' ^' Z% k6 b
' y: C, w' X: G0 ~upstream 52os.net {
! \1 i. {- ?7 F% @9 N    server 192.168.1.100:8080;
. a9 k6 n% m& S1 r* S* y    server 192.168.1.101:8080 backup;
, m- Q. N. H& h}) k% p" @0 b& I9 L5 y4 d
4 a, S( |( `) u. \+ \; e. B) Z
server {
7 @; i+ {; a; j- ]* A; ?listen 80;
% w+ l3 V" [. ~- O; Jserver_name 52os.net www.52os.net;( H. @) b9 P# R; ~; T2 S+ Y( X# h
( n% i6 H( n9 `" ]: a
location / {$ t  i  ]- u8 C- M
    ModSecurityEnabled on;  
3 O8 S/ S# n9 H0 Z, [; e    ModSecurityConfig modsecurity.conf;  ) t- Q/ G- B6 e* w0 q
7 N" p$ s- L& r4 i. x
        proxy_pass http://online;
. D( i1 J+ ^* }4 g' b4 e0 W  T        proxy_redirect         off;
3 y4 G" r/ |' W7 |: S2 P6 b        proxy_set_header Host $host;/ V- l' B1 `$ L  Y3 o& w
        proxy_set_header X-Real-IP $remote_addr;
, {( T& D9 o5 H- H& o' g        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;' r$ z+ Z; m, }" ^) e7 ]! |
    }
% X" z6 U( i* D$ D7 \; T7 r}
& ^8 r& e6 r0 p# E六.测试6 f* k$ p3 D7 d9 f- Z* ~

# ?8 q7 z/ Y' d; x3 {我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
  h3 j  N( T) o+ z4 y% y! \' p; @: x) k/ f
<?php8 U2 L$ m# c' M& e; b8 W
    phpinfo();   
( j  K; ]" e5 B?>
# Z! x4 n) e9 L& _. G在浏览器中访问:
; ~6 O2 n* g% p2 R6 `  {  k/ I9 ]3 _& m
http://www.52os.net/phpinfo.php?id=1 正常显示。! A+ H! }  O/ b. q2 c$ q6 |- t* f, B
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。( k# q8 {) M3 _5 k5 |
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
. s/ V& w9 \. }3 R" W* V$ Z9 K说明sql注入和xss已经被过滤了
/ C. Y: p1 |2 {; e1 w6 \( x8 R1 q+ b+ e8 O
七、安装过程中排错1 d7 b- L# t( D' z3 q& G1 x* C

4 N9 ?$ o; Z* V1.缺少APXS会报错
% A5 l2 T  [$ x/ T) G  w
6 m6 p. J4 \& N2 I9 A9 {! @4 B- mconfigure: looking for Apache module support via DSO through APXS
  s3 R$ {' Z% x( [; R0 B, fconfigure: error: couldn't find APXS1 L& [" ]7 X4 {. e2 M$ k4 ]* b
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。( R+ P$ s& g  f1 A" W5 v' i1 P
解决方法:
4 J7 k, c9 ^4 z! n2 `  C5 S; q* x! H+ ]0 L- X  m- R& g3 X; J" u+ J* n/ [
yum install httpd-devel
2 r; ]+ @0 H% F: W8 Z) T$ m/ z6 l7 j/ I2.没有pcre
, r" Q# x& g9 o
( {  |  r6 Z8 C4 [' \+ _  d/ d" m9 cconfigure: *** pcre library not found.6 Q) a* j2 }$ y5 J
configure: error: pcre library is required
% ?% L: \+ ?( v7 G: `/ e( E解决方法:
: a1 |7 W- ^% f, F% _
) Y4 A7 h: M- _yum install pcre pcre-devel1 G3 m* D) Y9 z  ?6 e$ P" s2 @
3.没有libxml2
( ~3 T. X4 y. R' T  c4 W+ o; o1 r/ O- S: f
" j* N# g, q3 v1 x
configure: *** xml library not found.
& D) w/ b* e+ [4 Zconfigure: error: libxml2 is required' }! [; _. H1 B! x
解决方法:3 T) I" m' P7 D
& F$ G- R0 ^' M) z( w) \
yum install  libxml2 libxml2-devel
& w7 X. C6 w* g# l  X+ Q, v4.执行 /opt/tengine/sbin/nginx -m 时有警告. E$ @' O& X  q- y
% T/ U) c& G, ^2 e( u0 C
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
+ R/ f: q) ~: x9 H; gnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
+ g. p: @3 {9 V' K% D8 \原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log$ o5 C. u/ {' o8 E+ d! c+ N
4 J6 v; h$ h- ~& k" ~* E
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured./ Q: _2 r: Y* m. f+ M- Y! i& y! q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"/ n, {& U# F9 m: ?' \/ w
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!2 O2 {9 O+ ~, n$ ]' d
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
6 G. L3 \: X3 D% s  P2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
/ z2 G: x8 [. r( _* H5 J# q1 w2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
: K7 @) }( u: s6 r1 |/ k; p7 J  ^解决方法,移除低版本的APR (1.3.9). q  [4 k: c& f. D7 B2 W3 d, v$ v3 L) @" x

& ^# q6 O- P* Kyum remove apr# y% z: d1 Z4 z6 Y7 J$ u
5.Error.log中有: Audit log: Failed to lock global mutex
1 [5 F5 j; G0 x! o7 j$ T+ Z  C. T2 k9 ^( K* h
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock       |7 x6 N& B% c4 ], F$ A$ x
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]( R! g" s' C) d, @+ y4 x
解决方法:' B$ S( k$ d( M2 E& x
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:5 |6 ^/ z+ k$ j$ }

9 s: M: i% G3 s$ S( E# _' f7 dSecAuditLogDirMode 07776 i4 y7 s' u2 ]4 i! j
SecAuditLogFileMode 0550) O5 h% X' S# ]! @, E, i! |1 n
SecAuditLogStorageDir /var/log/modsecurity
0 |- [' h& Y- |0 R$ I- E: _7 @SecAuditLogType Concurrent' U* i7 a- ]0 Y3 j+ x
参考文章:
, M1 i/ h5 g6 S6 a  `0 G" ^; Ehttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX0 E7 R# U! Q3 L, @5 w9 M
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-19 19:03 , Processed in 0.054478 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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