找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12380|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
2 s$ I& p% k2 f2 q6 g- F  W1 ?& u0 y- m& z
一.准备工作7 J  B# Q, x2 s0 }% B
8 n. d5 w) }& ?+ e* U
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
' i3 P$ y% V+ y- d& y3 e: g; Y6 o& E. t6 q) N6 M; y
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz0 _" g4 P/ y# }" J; _% i* ^
8 Q: W4 e3 d! y7 q5 K5 K- f
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
1 R/ K) V  M$ H4 E6 d% n- L# F- z. n6 K2 B
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs& ~1 M4 P( u* j# K
) ?* [( H! X  p1 v* h
依赖关系:
+ d* |$ v) K  qtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:, G4 h& W1 h! C3 v' H( l% }2 b
% V. f# d2 L. ~* b
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
. ]4 w# W# O3 n! q; T4 lmodsecurty依赖的包:pcre httpd-devel libxml2 apr5 z8 K; z+ X& T  Y+ u

& p7 M4 X8 s$ a! Kyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
. [4 G5 B) |: y, \二.启用standalone模块并编译
/ g. L, a! ]* m8 C% g7 K% n- f3 n$ p' C1 I6 H! A
下载modsecurity for nginx 解压,进入解压后目录执行:
; L/ f! j) g3 U% R$ [8 ^( _) \, X& M1 Q
1 g/ c; e5 q1 {! o; _./autogen.sh
& h7 C% j6 W9 Q+ x: S, S./configure --enable-standalone-module --disable-mlogc
" s" k4 @9 E" Y& N# J$ tmake , U# T4 k" m8 J5 l9 m
三.nginx添加modsecurity模块( [. u8 x- G5 f& g3 P

3 [$ D! z1 t3 w' }在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
! K1 W0 B5 d1 x& R& R, W8 C9 s' G+ @+ e$ A( U) b$ c. R% t( n
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
% J( }; L7 J  _% J. f3 ^make && make install
; n& w; M3 A! V/ N四.添加规则* i% S/ U2 j! u2 O3 V

$ h/ x; q! A$ p4 e+ D* Qmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。' m7 J  H, `+ b. U2 K
& i7 p" C) k1 }9 d3 s) U
1.下载OWASP规则:9 u6 S  s, K5 b6 d* ?  h0 x: B+ k- P
/ f2 ?& r: `/ L2 ^, D+ E
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
$ `1 M0 a# {6 q4 `( y( d% k- Q3 ~3 x
mv owasp-modsecurity-crs /opt/tengine/conf/6 M4 a% o7 g3 e: X. ]

. o  H3 \% ?) W( y/ G4 S0 D$ \9 }cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
. O( u) o0 |. R/ a+ `2.启用OWASP规则:
, S! v% @( @; o, Z
7 _* }9 u, U7 K0 N1 k复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
2 u% t- S9 l) o2 A5 e. N
  m1 E: a- n9 S( L% c) e2 N编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
, u* H3 _  V+ [  R) s$ q! G1 u; P) i1 i9 ^  \3 _
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
3 h% h. n- h3 J7 O6 K  L5 S" r( D& n) [$ L
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf' K& j6 ?; A& x
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
; b2 h1 C5 @1 `- ^$ Y$ v5 \8 ]Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf: a" b1 ?. B5 o5 E" \, B- a8 E
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf8 n, d" A5 P: D3 W% J  z/ P
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
8 X7 ]6 `+ j4 m3 M  a0 VInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
2 h* E# L( m+ C7 m; j/ I% fInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
& R$ Q; c1 V8 y$ L五.配置nginx( f+ J& M# j( m0 r) S! s

' H! p' f* N5 `在需要启用modsecurity的主机的location下面加入下面两行即可:2 ^6 N, T. Y3 ]* q) d. I2 R" }
/ }2 W2 |6 `; A2 g( }
ModSecurityEnabled on;  
7 q; r  e' e; `! A1 iModSecurityConfig modsecurity.conf;) w. F2 t" J4 u, S8 O
下面是两个示例配置,php虚拟主机:
- o$ w: T7 N0 A1 d5 N  c+ w) U3 W7 E6 b( K) H% w0 y
server {
2 F$ x4 q5 i5 Y/ P( `  ?" e0 L/ Z8 G      listen      80;7 F+ r( U$ V7 o/ q- w" m
      server_name 52os.net www.52os.net;
0 Q; }& R+ N! J     
! H0 Y: `! o0 u      location ~ \.php$ {7 T0 X, G6 M& Y9 `: E7 t. N  s
      ModSecurityEnabled on;  , U5 q- e4 z( d; v
      ModSecurityConfig modsecurity.conf;& @8 y- B; ~+ b( V
9 o) l. w9 p" @0 c, Z$ h
      root /web/wordpress;
! }$ z8 o4 p" }4 V+ f9 Q2 y; _      index index.php index.html index.htm;
% t- z1 D" \  w3 c) ^1 H0 d0 I% k  8 @9 p- E, `6 r7 L* B# x
      fastcgi_pass   127.0.0.1:9000;
$ z$ F8 @2 n* N. ~, T      fastcgi_index  index.php;; C& V+ v3 [- ]$ v' x) {) ?/ X
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;7 Q% R! d: x* k. \- K+ l" n
      include        fastcgi_params;1 b9 I& d8 E/ O, n+ B
      }: l$ v8 Z( j4 e4 ?, Z5 W) O+ L3 Y
  }( [  S; ?/ o& o+ E
upstream负载均衡:3 z' E5 K0 v6 S+ k" I$ o" ]
% |1 G/ d/ |' `3 K
upstream 52os.net {; B) e5 ?% E' m" `! S+ t  m5 r
    server 192.168.1.100:8080;
5 G% h. S0 y  @9 c0 O( @0 W: z# {    server 192.168.1.101:8080 backup;
: e8 R3 F( w1 g( L}$ _, G1 z- ^: W+ P( i: b

# Q& X( q! y2 d7 \9 |1 Xserver {  y7 r( K0 ^9 e4 S
listen 80;
4 ^1 N" j8 F2 ]/ cserver_name 52os.net www.52os.net;4 |, ^- t; ^, W* W! a
0 i. o: l8 W7 F* Y  r5 k9 i% R1 K# c
location / {
: L. Y3 ?% Q4 a( r# ^4 S0 R    ModSecurityEnabled on;  
7 E" [& B5 Y, k( W; Q0 |8 r    ModSecurityConfig modsecurity.conf;  
7 ?. K, F8 m  m
/ Y9 f4 E% |# k! ^  V# B! a' g  w        proxy_pass http://online;% b  X# T5 H( ~; j) G, k, C
        proxy_redirect         off;
' x" a& ~, f- y) x3 M" _! b        proxy_set_header Host $host;" X6 E8 s9 W$ ?1 Z' {) }4 X+ v  F
        proxy_set_header X-Real-IP $remote_addr;+ i- W. e; Q) t  e$ H
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;8 k' @% Z, J, G3 Z5 n$ Y
    }- V1 l" ?* S1 D  y, X: S. S
}4 l0 V. `% Z: U2 _
六.测试
% Z! `( a. X% G! `0 r" l/ Y" o% G, X
9 b6 a& r% i' a$ o) q- ~/ D我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:! W$ ]8 W* }9 _; G* W
5 g+ ^0 ]; \. C: s" X0 G4 T& Y
<?php; @2 t4 g+ V% S% x) F' b6 h0 A
    phpinfo();   
1 T: g# |) O, r9 o3 g?>
) ^' H7 M+ l8 C% ?在浏览器中访问:! C, j1 ~( }% ]  Y0 j+ n8 a, r( H
* K) @0 v5 k1 q. D5 K& A. y
http://www.52os.net/phpinfo.php?id=1 正常显示。
, q2 B4 C+ S: {2 M# F% ^) |http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。' e, a: V8 C* V
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。; |) q7 F7 R0 g( K, ]1 |! |5 m
说明sql注入和xss已经被过滤了2 J8 x! O+ k0 n' s' L% f# i
8 M) T  B  U. |) E
七、安装过程中排错& O" X) [' X0 p% `# R
5 g! F  p6 |* Z
1.缺少APXS会报错
, `  i* i- b; A  V$ x% P$ n& e
9 S3 C- |6 f4 s7 `. bconfigure: looking for Apache module support via DSO through APXS$ C, D  _* z1 D, Y
configure: error: couldn't find APXS
  q6 O$ x$ C4 qapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。) n3 P/ E$ r# N4 F
解决方法:2 e0 d" a2 g# Q* D

* `& d8 w- g& f, E: tyum install httpd-devel. D1 T3 x( y$ F" p
2.没有pcre  h6 s! \7 V( Q3 ^. F. A2 B7 \4 ^; `

8 @* }( U6 g" U! u; U  `3 x% Bconfigure: *** pcre library not found.
+ \' y8 I) p3 w% Oconfigure: error: pcre library is required7 G% Z2 N6 ^5 ^+ _; r% n
解决方法:
2 i6 y  H3 P/ r5 F
0 S: N0 p+ l+ m! _: Tyum install pcre pcre-devel8 ?0 f4 `' L/ z/ U, C/ O6 g
3.没有libxml2
! p( @" C( t3 Q1 Q, I  W; y# r# t; G4 u" h( t3 k% Q8 E+ X

: B8 a6 P& o+ x5 t6 k. Pconfigure: *** xml library not found.
' b, W) w" \/ U7 k9 |7 Q( Mconfigure: error: libxml2 is required
- q8 L8 x+ @% r3 n6 @, B- B解决方法:
/ D+ r. R3 m1 `. T2 a; v, e9 Q, [2 I% M# n
yum install  libxml2 libxml2-devel
, ?2 {* g! g# k$ O+ \4.执行 /opt/tengine/sbin/nginx -m 时有警告
; o( {. L, C% ~9 k  g- U5 A; o/ {: u
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
+ ?! N& J' b( O9 y2 B; {nginx: [warn] ModSecurity: Loaded APR do not match with compiled!. Y/ a* v% d* q# [- X
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
/ P  G2 c6 q1 u+ |" _$ }2 T* W
' W6 N8 ?# {, G6 I# T% k2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
( N; w- `/ f3 [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
- w( D# I# g$ m2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
$ ]+ V, L6 F* `4 s2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"! c5 \! S! Y0 U% C) Z( b5 g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
$ {/ ]# ~- Q0 Z' B, ~4 J" H! y2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.; l  P- _# r# o6 }& M
解决方法,移除低版本的APR (1.3.9)2 D9 P7 F, K% W" O
2 l1 y+ h+ E3 f" L
yum remove apr
# ?/ C! l3 e/ d; O5.Error.log中有: Audit log: Failed to lock global mutex
6 q  v" G; a) w" e% Q" p; J0 ^/ w: B% I& r' G0 a3 V* e
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
8 g, _0 P) E( V) jglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
) K( d6 K3 I. @+ O解决方法:
5 f7 l( x# l; B6 o% Y! X编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:# |" E6 U" Q# d& m  {
$ f, Z4 s6 G* n3 x1 r, D. _- b
SecAuditLogDirMode 0777
: c+ M. o' n6 ]$ vSecAuditLogFileMode 05504 J' }" O4 W$ a
SecAuditLogStorageDir /var/log/modsecurity
* N: N( Y8 C. u' wSecAuditLogType Concurrent
- j  k1 Q$ s, B参考文章:' c9 T2 m9 l7 _0 u8 _% k$ v
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX9 N. d# A) m) i
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-1 01:33 , Processed in 0.078968 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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