找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10019|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。' e& y0 {  q2 p& L$ G
; f! O2 G3 V! l2 ~9 J; m
一.准备工作0 s, J6 m, w4 d

: }! O: l1 n5 r# E- N! J系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0  U5 X$ [4 f2 N6 R# Z, S" K8 Y

5 L! W4 I. P& W% ?% K1 Jtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
2 n& u8 G' p* m) K  L& f5 a+ f* ]4 u4 s3 G' @; \/ I
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
4 `$ E( q7 G. d5 Y( c, R, w  Z9 `2 q+ G) {. K3 ~
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
5 B1 ^# s8 m. J1 y( \' A; }' s4 g) f4 G! H/ v& R
依赖关系:
# [4 j$ N4 B! E, P& T) y3 M7 [+ Vtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
! V1 [( W0 _7 J9 i! A! r2 S9 H1 u) |# y. ^; ?
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
: F9 A7 k; w3 Fmodsecurty依赖的包:pcre httpd-devel libxml2 apr
% ?7 E! s  m% y+ \& y
1 n, o: H" e; E- m  [yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
, L; t8 W, E2 X& T  S2 [6 M9 V二.启用standalone模块并编译  U* d' F$ i( r

& @0 I7 H( {6 a" C, Q下载modsecurity for nginx 解压,进入解压后目录执行:
4 `& v/ |, r4 ^
5 C2 }6 x4 ]% ]./autogen.sh
0 z4 l3 E1 W2 d1 \4 W./configure --enable-standalone-module --disable-mlogc/ e# w! Q4 w, ^; i8 c3 _6 ~8 h: {9 c/ y/ V
make 2 b9 j9 R( Y; o; D
三.nginx添加modsecurity模块
% x  Z- B  z: C* y& c( t( I/ n  |1 \
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:( U, M: _- _2 y) t. A3 T4 u5 e5 }

+ W& ^5 z. t, d$ D4 T./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
$ g* ?: |8 L% N5 y/ q* Tmake && make install6 d) B& K' k: Q3 i2 `
四.添加规则) `5 |. n; y7 d( |! i( k- s
! G2 ]4 Q8 u1 @* q$ j8 g
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
7 W" b0 i9 n7 b. E! [+ v* E; e% a6 q# d
1.下载OWASP规则:+ s, S) ?6 a( a

7 m4 z1 Q8 ^9 j7 pgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs9 c0 K) _  W4 m1 ^1 C3 b
1 U( K$ b% l( {, C* \+ Y
mv owasp-modsecurity-crs /opt/tengine/conf/. E- P: _, I7 Q
7 B& ?: S6 l: i7 E9 i+ [# b6 r* r
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
5 J. w$ M* K: J4 y! C2.启用OWASP规则:" l& u8 @4 @' U* n" M

5 l. r1 D% w6 p1 W. o复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。( A* R4 E& {6 {- K+ F" E8 a
( c) h( W  @' V5 j6 o8 @
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on! X3 _' `; v# n# x

4 R1 z/ [0 J! \0 howasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。7 O3 s; c  @7 z

8 m$ t; y  l6 `+ k7 x8 H) |Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
+ e  U* w( Q9 dInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
" \' A5 P4 c2 z8 y. K* P& R4 x1 {Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
& ]+ Z5 i2 }" l* w7 K9 lInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf) B* F: E/ \4 \# @# `, x' `
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf% o: C  G3 U/ R2 v
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf1 r3 d$ `& t7 [# r* x! S7 Q
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf% Z( ~# q! e3 {. |& p7 O* l: C
五.配置nginx
% ~5 p* N, `1 G% U' _  ]
/ h$ d& ]# |. A: \在需要启用modsecurity的主机的location下面加入下面两行即可:
$ Q" E" A  U) Q
* b( v$ f( p+ [1 |2 iModSecurityEnabled on;  
! e/ C4 O; V: G, `4 kModSecurityConfig modsecurity.conf;
/ O. u) F7 T# R% N$ `: {2 U8 w下面是两个示例配置,php虚拟主机:9 y& G$ C& \/ Z; C

1 h) S# t* S9 Y+ w' sserver {' H( X: L4 M2 n) A! T
      listen      80;, v1 V! ?" k# |
      server_name 52os.net www.52os.net;
$ M) d" b+ ?+ h/ v: h     " |5 Q6 q; ?0 i9 ?9 s$ {
      location ~ \.php$ {
  }4 I" d7 O7 j( c7 z# z      ModSecurityEnabled on;  $ Q, S9 T7 @4 U5 b5 j
      ModSecurityConfig modsecurity.conf;
/ O4 {. X- _: S5 P" s5 R
( W  V; y9 z* Z' D9 u* \      root /web/wordpress;9 A8 l- o' q& T: c- S, I5 O
      index index.php index.html index.htm;
* Q2 }  H9 x. s  _2 R1 O  % ^0 u0 ~7 `' n# M/ ]
      fastcgi_pass   127.0.0.1:9000;
4 T! D/ G: J4 p" k  Y1 L8 O+ X      fastcgi_index  index.php;
2 o7 ^2 Q9 t) @4 e      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;6 i, q6 r% K) m
      include        fastcgi_params;
9 L9 a6 k/ H- }1 L2 R0 T' ~      }8 v5 [7 |0 }. S( q6 v* d
  }9 Q  Y/ x5 c+ y0 O' p
upstream负载均衡:9 }5 H3 C& b9 p, \) D0 ?

4 G7 `$ n$ X* V! S0 K0 I4 m; Dupstream 52os.net {
2 m/ F& k! B) x" H, p4 |3 |9 O    server 192.168.1.100:8080;
: I7 L& y! h0 I. T. w( i$ \5 \( q; `1 Z    server 192.168.1.101:8080 backup;0 m* s, ]1 ]0 c& F5 h
}& ^  ?2 A- x! B3 A
7 ^1 V  E) p) Y9 v  r+ f
server {
# s% T8 K3 U% g6 }listen 80;7 A, Y: q& I  }( |! P6 M
server_name 52os.net www.52os.net;
7 d- c: N( _: _* H0 N- U0 M; ]6 W1 N* c9 A
location / {% {9 f4 R) X& i1 [3 \- B
    ModSecurityEnabled on;  
2 I+ p5 f; v) `: C5 a    ModSecurityConfig modsecurity.conf;  4 n7 @- m0 x, d3 X

! G1 B  P0 l: o! v# E; W& F/ D( C        proxy_pass http://online;
0 X! g/ B& ?: R! l& \        proxy_redirect         off;
' d; n4 f1 @$ H1 C" ^# L! E0 B8 o        proxy_set_header Host $host;
% Q) k5 H3 I( h0 \  M9 u        proxy_set_header X-Real-IP $remote_addr;
5 m( |: V$ w3 f8 h        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
3 e2 U( w8 S! I7 M$ V' w+ E    }
5 F- T7 z, n" C. K  Q}
% W5 C6 V; _% @8 R* T+ t: j7 l, H六.测试4 Z: y7 `/ u( }$ ]6 t

1 [! B. i! t* W我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:% q, ~4 `! M& I; T( P

- e9 p$ l, ]& _% Z/ V<?php( B+ l5 l& u/ o' b/ N; ?2 V3 b
    phpinfo();    5 u0 T, o9 ]- b( T9 ^( Y
?>
. }0 r" T1 H& v3 v# g- W# P4 U5 Y在浏览器中访问:" q2 t1 J$ q/ T8 n5 V

! ]3 H$ W# Z. Yhttp://www.52os.net/phpinfo.php?id=1 正常显示。
1 \1 P9 T2 L% P9 P' @http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。! @1 @! q: o/ B6 S7 u
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。+ t* Z- w/ F( \. e
说明sql注入和xss已经被过滤了% ?: A/ U! K* Q- v* G6 V5 y

; J! I3 J  Z# I& D2 A" e; y七、安装过程中排错
0 o4 j" o2 f8 R# v; W7 L' B. Z% B% q% e/ W* J" ]1 X
1.缺少APXS会报错9 s2 s+ k3 ~, s; w5 x! m

- x! t5 W4 s( U. C5 c0 yconfigure: looking for Apache module support via DSO through APXS5 m" j! c" \/ ~8 G* ]+ ~
configure: error: couldn't find APXS# E2 b& W+ v/ C
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。0 ^: U: m2 l4 m* M1 ~* d9 p
解决方法:! D+ `% A) z! ?* x7 z& I1 W( Z
0 Q/ y# h4 g+ s8 z* I
yum install httpd-devel
3 D* x" J% L; k% I; o# r+ V7 e2.没有pcre5 E6 K6 i7 X1 s9 a+ S
, n/ M. d  S' J. C
configure: *** pcre library not found.
0 J& ]& `3 o9 ~8 vconfigure: error: pcre library is required+ l/ r, A# `. q/ B3 R& ^
解决方法:) {  ]$ |% h8 ]2 A  G
* Y' m- X$ K! i) y
yum install pcre pcre-devel& `& P! A- B1 j( Z& L9 R. C+ I: p7 c
3.没有libxml22 X3 T  `5 U. N8 l& F
5 I% K7 d* y! T8 X! S5 \0 H1 L

% r; {8 r! P6 n: H0 y0 N% @configure: *** xml library not found.
' l9 k- r6 r, n: C1 Vconfigure: error: libxml2 is required. i) _0 j! z9 t" s
解决方法:! S# Y9 g; G. D+ y, S4 B

. `* ]: ]3 ]& E; lyum install  libxml2 libxml2-devel
$ ^# Z6 p5 s' F- W4.执行 /opt/tengine/sbin/nginx -m 时有警告
# {% U; V. m8 d3 m; n
3 F9 O8 }4 D) B* H! rTengine version: Tengine/2.1.0 (nginx/1.6.2)
5 B1 K8 G+ i2 ^3 Ynginx: [warn] ModSecurity: Loaded APR do not match with compiled!
4 }- g/ ?5 a( [- D: f3 N0 s7 B原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log0 q" N+ ^; r" \- x: f8 z: p

. [! O0 ]3 q7 y# h4 D3 d1 X2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
4 y0 v5 Y+ k4 z5 @3 o: }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
8 B( J) N1 K4 G  h( e) _2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
6 u; t# b% q* J  W6 k1 F# H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
' b9 y: Q6 J) P2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"4 u; z/ L+ V& G8 s0 N1 ?2 u+ |8 A
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
! l" m1 b( u: R$ _6 a9 ~解决方法,移除低版本的APR (1.3.9)' Z& s- s- G1 R

% m) b. F: n" O: b7 i  x# ]) u& L0 Gyum remove apr
' H% f# \* ~6 w8 \) a1 S5.Error.log中有: Audit log: Failed to lock global mutex- _$ _  a3 q6 ~( K

4 a$ S3 g% o3 u: C: K9 j" ]2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
/ p+ s+ ?+ r' E$ H0 ~1 ?global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
8 [6 Y" J3 L- }( ]! X解决方法:3 z  g8 F' F! H  o) H- I/ N+ ~
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:' Q! l6 y4 g3 m0 z

, g2 J6 O# }- [6 b& fSecAuditLogDirMode 0777( B8 S* T8 ]) K( l4 F
SecAuditLogFileMode 0550
) @; Y! Z% a6 C/ X# i. [7 Q, B/ i% ISecAuditLogStorageDir /var/log/modsecurity
2 l1 L3 {( {+ t2 `SecAuditLogType Concurrent
# v- J5 L. f2 d, Q3 m9 x参考文章:8 h- h# c! v! `7 U% T2 ?
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
) T( p0 \- h/ n* d2 v% p- Nhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-2-22 08:34 , Processed in 0.060294 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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