找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12912|回复: 0

nginx配合modsecurity实现WAF功能

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

6 F3 D2 i, o& `7 Q一.准备工作3 |) A2 d1 k) I: _. P4 i
! C, M  i; r2 [# d
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0- _4 m7 _& U3 w3 _

& {' P2 t" T: K: q# ]' F, G+ ctengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz! I) ?) H9 E$ b9 f8 W  n

3 J+ T5 @: E" mmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz1 j5 J+ w7 d1 W2 j. D8 g

- z! b$ n$ n6 A7 I" O2 cOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
2 k/ F) K4 |* }- z' d" {
' |- x7 ?; X5 `' W0 [8 L% J$ Z. t依赖关系:
# K& @+ J' c' {1 p, x2 `+ Ctengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:/ _' k5 ?* o/ R# S/ M5 L8 I* h. O

9 E. f1 I1 b0 P8 @/ Wyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel; G7 t/ c) L  `6 |% O8 m/ a
modsecurty依赖的包:pcre httpd-devel libxml2 apr5 H4 K& p5 P. ^$ P7 g
5 ~& Z! Z8 x% o7 h2 J5 a9 ]/ t8 B) h
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
$ {- _: V/ g+ {- p: E& A" f二.启用standalone模块并编译
5 u; ~& V: d; A1 K& t( X. y: M
0 d  x( [; x  L# I$ M% y下载modsecurity for nginx 解压,进入解压后目录执行:
" _+ Q2 M0 k: Y$ k! C* e
: F2 k# O  J  T( l' }./autogen.sh
/ Y4 Z9 h9 c3 p./configure --enable-standalone-module --disable-mlogc
7 Z% g  d- o; f) A. j) W( Omake * d; N: E* U$ J- g
三.nginx添加modsecurity模块6 m3 H1 W# H1 K" j' b( A
/ o* E$ E! _7 @. ?5 V
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
% Q3 Y8 B  h8 |" L# Y( K) g+ h. e# p7 J, Y
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine1 N* i# L0 u0 X6 m
make && make install  s) V5 H4 D3 ]5 c  {% C4 x/ h* Q9 M
四.添加规则* c  l' {" h% S5 d% x

  R4 k2 N' ~% Y5 G9 Z9 lmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。) s4 [/ B$ T( B* C0 p

7 t( ?5 `9 u6 ]* O% ~% S1.下载OWASP规则:1 B$ p0 E4 `8 ~5 {( F
0 K# S2 G- Q" h1 ~' Z' Z$ e
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
, K% G- [6 g+ v+ M8 g! S8 E1 e8 h# x
! s* c6 \  b$ B; emv owasp-modsecurity-crs /opt/tengine/conf/" U' Z& k, x1 L, }
( y5 p, g' Z' y! G; `( z
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
% b8 ]+ ?2 K2 w0 V/ L2.启用OWASP规则:
/ r( B3 w# m- i3 e- f. K. w6 b) g% p+ y* R
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
6 i! z% H  O) f9 }
) v: [& h& @$ Y编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
/ D. ~: ~# ?/ c+ k+ N# {: H- M* Q8 x4 b; N- U- @
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
$ d6 \& p6 `3 y4 d+ b1 R1 X- L# s' }4 v9 N
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf7 Z, [( K) |% {; W3 |, L3 j
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
* t+ J" H4 q- _9 a9 X2 W$ L  g, iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
7 L- z) f. L  h. O; y7 N8 b' EInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf$ ^1 Z. O  m) G( D9 v
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
/ e5 i, T* R  x9 f1 r5 l" v/ J9 qInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
9 r( y9 ?  {8 L  e6 ~8 H5 ?; cInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf( P- V. I( E) K/ Y4 ]9 Y' X  F! o
五.配置nginx; s4 Z4 r# |+ I0 W5 h; K

( c6 u- b; [5 D$ i& Y在需要启用modsecurity的主机的location下面加入下面两行即可:9 ]& ]5 o$ C7 ]. _1 A
5 e- ?# ~! W% z; `3 p  u
ModSecurityEnabled on;  # {, q: q* s( b+ G# P/ l
ModSecurityConfig modsecurity.conf;( m3 d% C4 W* n9 ~  o3 j
下面是两个示例配置,php虚拟主机:
' {7 K  l, |5 }% Z& @3 [& Q7 B
; K5 U! ~* c/ e5 A" b3 O& bserver {- o- B* ]- `7 [$ f2 e
      listen      80;" l. y; A6 I5 b+ n+ s4 b# n
      server_name 52os.net www.52os.net;
4 d. n, T7 X) x     
# m; W- r4 H7 C, h" }, ~/ {& d      location ~ \.php$ {; ^# r& y+ J! g7 X* h& h$ ~6 L% G
      ModSecurityEnabled on;  - S& c3 E# _0 x: N: {; I
      ModSecurityConfig modsecurity.conf;
0 K. h  o" a* p) w$ H8 o6 H
  Y/ X9 ?+ u+ m4 h$ A      root /web/wordpress;( k- Q$ g, y2 ~: t6 T5 b
      index index.php index.html index.htm;6 i, q1 I# ^- C, L
  * m- L" J! `/ @, C* N1 p
      fastcgi_pass   127.0.0.1:9000;* {  @/ o1 L% Y* j5 }/ ~% X. j9 `
      fastcgi_index  index.php;0 a$ ]( X4 S3 Z' |, u! s
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;) i+ r; z3 [4 W1 J4 c/ l7 s! E: R
      include        fastcgi_params;) U( p4 m7 ]' v& H  H
      }3 I( H% C) F: {* O; M2 P9 e# \
  }: F* r' |# A7 w
upstream负载均衡:. ~. y6 W+ ~$ J  X4 r" u9 X

, L$ {9 ~# `. Iupstream 52os.net {
; q# |& x' H) Y* W+ g* I    server 192.168.1.100:8080;
. s- X& N$ o: M) [3 N+ @( x; u( A    server 192.168.1.101:8080 backup;
/ \! L; t* ^3 V  o0 C' N! J6 `}: m% u" @( P& P" r3 e
" V* @+ w4 n; }) e7 W
server {
! ]3 [3 |$ @, b: v* F2 elisten 80;
' s  b, E+ Q, z- y* G4 eserver_name 52os.net www.52os.net;0 J: S  \* F# E3 W2 g! R9 S. ~* P: e1 P
8 ?5 V" S# _6 B
location / {
0 Q* r# d: P7 Q    ModSecurityEnabled on;  ' T* p* r. ^3 M- L# Z
    ModSecurityConfig modsecurity.conf;  1 K: m& T. h7 ^$ Q' Z' h! a# b

/ W. B# k9 P* `! F        proxy_pass http://online;
! w, o' {" b- [6 }& B& |        proxy_redirect         off;
% H! r# v0 i3 w! b, Q5 E8 z) I        proxy_set_header Host $host;4 S- A3 k0 E* v7 z# n( L& m& l( x
        proxy_set_header X-Real-IP $remote_addr;
) V  C) ^3 \" z5 Y        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;' H2 o# u8 I! \' R. j
    }( z5 t; P6 U) U/ c; e
}" p4 I6 A: Z5 s. v3 x9 t) H7 j
六.测试
! d4 n6 Y5 U( p1 \7 U& d' k! t
7 V& e. N! @# x8 ~我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:. R/ P* w/ K& W! A2 [- `6 I# q1 h
2 L- O6 K9 N: f* y. Y0 t' H& f1 y
<?php1 j! c! R' r- m3 a) b
    phpinfo();    # S( R; ^3 u% H. ^$ Y: B
?>; o+ q+ H7 i: c. Y0 f
在浏览器中访问:6 U' o. z/ Y1 y1 x4 z) L& @

6 b+ d/ n4 u' g5 W4 B: ]http://www.52os.net/phpinfo.php?id=1 正常显示。1 Y* k/ G0 l2 v8 @! d
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
* H: P. r3 y' A0 Shttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
/ O- J; k6 Y7 D, g- E说明sql注入和xss已经被过滤了
& @& ^6 r% y' _+ }7 b" j% z; S
$ M! s' M- Q: [; C/ w- A七、安装过程中排错
) I% d% q6 \0 B0 r7 S# p# ~( F
) U2 b! Q" {: V8 g; X1.缺少APXS会报错
9 Q. U: N& }2 C7 }+ u
- ^+ ?9 Y$ {4 ]configure: looking for Apache module support via DSO through APXS
/ J/ w* P; T2 A" I  V- cconfigure: error: couldn't find APXS. o' D# o' ]9 I/ m
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
8 }7 g+ i( F9 @: h, L& }, Z解决方法:
  F2 {7 O" B4 P/ t( `1 G5 \1 f5 ~1 j( U
yum install httpd-devel- c, l4 l5 g7 L4 {
2.没有pcre# A: w4 y! m* ^* ?

$ o! U7 [/ Y' d1 ^5 O4 U  O) Bconfigure: *** pcre library not found.
  {  Y' q' v8 Dconfigure: error: pcre library is required
" M$ `% t  x& h" k3 \% [+ p解决方法:+ |$ x/ P7 Z) j2 T& t, @
& {1 p5 L# j; f: s8 y6 p3 A! B
yum install pcre pcre-devel4 f# ~4 o7 h9 z$ e1 h
3.没有libxml2
3 S. e- Y% ]) v' l% r8 R9 y; H
: |8 W/ ~$ h6 s" @6 ~4 V, r/ s
' N5 ?1 F7 o& h: c- h9 N$ m7 `$ Q5 [configure: *** xml library not found.2 Y) e  w: B3 t! m+ [
configure: error: libxml2 is required
2 c8 H: T' m- N1 n  b- l解决方法:" w6 G4 D* e% y

, `9 s7 `' e; a; [0 ~yum install  libxml2 libxml2-devel
/ m9 H3 N  O9 k4.执行 /opt/tengine/sbin/nginx -m 时有警告
: I/ {4 i1 g, e+ [! r/ I
) O+ Z2 x# c3 yTengine version: Tengine/2.1.0 (nginx/1.6.2)9 Z( ]" i- K6 @' V2 c
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
' q) O1 ]" T" p" B+ [1 T原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
" i2 ^$ ~8 G& P; D
/ k" G) d. a" U3 L) s2 ?5 \0 Z8 k2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured./ b; ~% @* l" h6 A; w
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"4 `! O+ }" b9 `2 y4 [& E
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
. A9 L8 b. \9 c. e8 \6 H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"( S' F; C! b& i9 Q% w" V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"% T+ p) Z# k$ S" ~' m
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.& w3 D3 K& O# d$ f
解决方法,移除低版本的APR (1.3.9)
/ c/ F4 `2 V+ y# s" T
1 W! Z$ `. _1 t- a) a4 jyum remove apr
7 `# Z0 A! ]9 u* ^/ f# h- e* F5.Error.log中有: Audit log: Failed to lock global mutex
) ]4 Z1 n: ^. I% {5 R8 v% f/ m5 b; p, ~
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
. {: y; e9 S" h! mglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]$ K- ~; ?. a3 O# T7 c5 N3 c) T
解决方法:$ }( D  E* N5 `6 z2 S
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:# L( m( i- H. T: x: P

5 E! f# p( v3 w) n4 g4 ySecAuditLogDirMode 0777
, E' X3 E1 {; f% t6 |SecAuditLogFileMode 0550
/ D/ b% |4 X7 ^SecAuditLogStorageDir /var/log/modsecurity+ \  d) J9 g, y0 ?
SecAuditLogType Concurrent
! B4 s  F% M( g+ P; a. |参考文章:8 J2 I" d* Y. j5 K) r
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
, b7 S+ c: E0 N; }" ahttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-25 17:12 , Processed in 0.043017 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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