找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12297|回复: 0

nginx配合modsecurity实现WAF功能

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

5 ~- l* w* x) f, z, i' C一.准备工作
) x& }2 S& Z! V9 D( ^) s- j
: l8 U  X' N* z8 e系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.04 x2 N5 n: w/ W" J, L

2 V4 ]4 X/ m5 t! G5 Qtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz3 I* r" k$ {% ^; c& Y' \5 L  y

% S. F6 O: @% b  u5 omodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
; G$ E' t3 V6 u8 ~; ]2 r
+ T; T* \5 ]5 n$ R4 K+ S) b7 fOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
6 e% j: q( b- `$ f6 w; Y+ \( z5 n. J& o: i- d! _+ A9 d6 V  n9 A
依赖关系:
. m7 M; y& u# \7 |" M" ptengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:- G3 v% }% |  U& Y' G# q

5 @$ y( b: L! C" a; p; a% @yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
9 K: a" R3 J4 |) e; H% |" Tmodsecurty依赖的包:pcre httpd-devel libxml2 apr6 L$ j7 G4 ]- X- Z

6 O. l, P$ v5 ^3 {2 yyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel/ S8 H5 e  P# S' [
二.启用standalone模块并编译
- X# [* |1 g' w) a8 M! Q5 y- L4 H$ L3 d8 M
下载modsecurity for nginx 解压,进入解压后目录执行:7 Y. B0 }1 r. w' ~4 \% G
. c) p. Y' V- D, S
./autogen.sh
2 o6 a) b5 V0 O7 l9 W: N9 l./configure --enable-standalone-module --disable-mlogc+ Q2 }3 j. o! `$ D  J; ^( d/ ]
make 5 A7 w3 b; y2 F) S" U' J
三.nginx添加modsecurity模块
0 e8 S! Y+ O, g; H( _
; n3 Y$ i! n, z" L) D; y在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:# Z$ v/ f" u8 [; ~3 F! M6 L( a
' a' D( U) M- q& i  @' u# t! d
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine  }* m" f3 k+ e* n& O
make && make install$ i+ K, N: {6 C9 F8 ?) L- o
四.添加规则4 N) o$ T6 z. J9 Q

0 x$ s( w+ I' Ymodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。1 s9 l- s! I- r# v5 _
( B; c6 ?: J% ~$ }! a- S1 k
1.下载OWASP规则:2 W4 ^. o- N. N3 Q8 ^

/ {* d4 h. J0 t! X3 S* Mgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs9 K! n& X2 ~8 N- i" U

; d% r/ L+ c' ^! X/ q) U: s" Rmv owasp-modsecurity-crs /opt/tengine/conf/
. i- u, W  G7 q. Q* L
% O3 z8 h; c3 s2 ?! h7 [. E- hcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf' x. _# t* |9 d& G( J3 a
2.启用OWASP规则:
& \0 g8 `$ t  g% }( d
% s& z; w6 C" o8 `% y- f) U1 C复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。) n( M! Z: F5 A) ]
  Q/ k' r; q6 m7 b) w0 N% S3 L
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
2 h1 @  T! @/ A
* v1 \2 _( n' A! Oowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
! ~# A7 N0 p' E0 p+ n5 I. ]8 W. _, U# T& @" l
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf& a9 j2 N* r; Y: y4 l" J) v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
; X0 b0 i( Y9 ZInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf" }1 C- w- P4 @) f- j* S
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf2 I5 H' `* p: t/ ~
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf% q& e; m, h/ v; x( V
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf  a* d) z) N' n5 [: a- Z
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
3 j7 h! F& @- r3 O, X$ l7 L五.配置nginx
1 Z4 U. Y+ X7 d+ O# I" `/ X; y0 o9 J* h- {6 g0 E7 d& J9 n
在需要启用modsecurity的主机的location下面加入下面两行即可:
' h% _2 l0 D; A- M6 S/ g* s! y4 \$ n+ ], I! J7 D' t
ModSecurityEnabled on;  
% l4 P0 z+ H% w7 k& W1 q" gModSecurityConfig modsecurity.conf;
7 K& F  Y+ \1 x$ p& W9 q下面是两个示例配置,php虚拟主机:
' f1 _8 w& r9 i" C1 ~5 M
; V' c5 G8 j& z. j7 Eserver {
) N" G' I( O, o0 P2 e- C3 c      listen      80;
6 s# Y; U0 j7 G. P% I3 I$ C: m! c      server_name 52os.net www.52os.net;
  W; q2 g  n; y4 e! D! `/ ?+ L! B) Q     
" P1 }4 Z' _5 Z+ @7 Y; L      location ~ \.php$ {
. e! i8 ]! g" w7 F) s9 Q      ModSecurityEnabled on;  7 K7 Q+ P# e3 P/ O  J# _+ q9 x
      ModSecurityConfig modsecurity.conf;
/ d; x7 a/ R8 r- E4 h
' \5 p2 E" r. F. R5 z' L. A      root /web/wordpress;
/ f/ K3 h$ Q, `) p4 b      index index.php index.html index.htm;
( B! ^) N( z( g2 U' z6 b5 L0 |( ]  
* d4 E$ C0 D3 T4 w9 D! t      fastcgi_pass   127.0.0.1:9000;
$ q4 T; [5 s' ]$ {      fastcgi_index  index.php;
- E! d: _% b. `/ ~1 H      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;" w* F! O+ V5 a, W' Y: S' ?
      include        fastcgi_params;
/ t) @$ s1 X4 o. J. }1 k( I      }
7 c! N; X' L: k" y" m1 D+ F  \  }2 m2 r' u- ]) O+ T9 m
upstream负载均衡:
, n8 \$ l7 i: j
+ x# H# \7 K" y) \: Rupstream 52os.net {2 E. h( r6 f4 |' Y7 ^
    server 192.168.1.100:8080;
9 |" J1 r- `4 x6 b) n    server 192.168.1.101:8080 backup;$ `4 A2 A+ C% c7 j% A; h
}
; U$ I7 {7 C  X/ `3 l
2 g: y7 i/ \/ h6 N( J3 _server {
7 T8 J) @+ _- }listen 80;
# h. {6 u- o' m9 E0 W; C: Tserver_name 52os.net www.52os.net;0 H( g% P  K3 s6 Y
, W' `- H% D& C9 k# m/ a  d
location / {5 R: t. ^0 H; G3 d  e6 ^
    ModSecurityEnabled on;  
: ]5 R7 i" o( F% P    ModSecurityConfig modsecurity.conf;  . q) ?% J+ n! O: x! [7 g: l3 D3 q
6 Y( K: E( |* d# _+ @5 B
        proxy_pass http://online;& B. z! d4 K/ D$ o! v9 r3 ?6 p) F
        proxy_redirect         off;( _' K; F) \/ a: a) n0 U% Q7 ~
        proxy_set_header Host $host;  I6 z7 z: c) q$ T  P
        proxy_set_header X-Real-IP $remote_addr;6 ^- P( `7 U* r0 L+ v3 x
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
4 q5 N* D( v3 ]" i; ~9 o    }' W; H5 r' p/ d4 x6 ^
}* l- T" X) H* R1 b! [! ]# ?4 v
六.测试
( \# c1 d& ^! C1 q0 o
# D% y. n9 u% T7 ?% `/ x我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
9 J- u5 l" A3 c4 V6 S; H6 \2 q9 V  |* C
<?php. d: V/ C" ^3 }) s7 G
    phpinfo();   
  e' y& [: P" {" j! Y$ Q# B?>
& d; b/ e9 T' F3 M- f1 B4 {$ P在浏览器中访问:5 k+ J" s/ W: s/ C0 X) q
3 F; k( {' Z* K' ^: J  [# W
http://www.52os.net/phpinfo.php?id=1 正常显示。
; c  U  ?* s& y: phttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。/ b! o/ j, l, H* D) A% I6 M3 N
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
$ `: v* F* V1 K说明sql注入和xss已经被过滤了/ l' A' Y7 o$ W: ]+ @5 `* T: t
3 C  S9 h) {4 f2 V
七、安装过程中排错% z/ b, o# V7 r, x4 Q+ Y
- p& y- {, p$ j; w% v9 W8 b
1.缺少APXS会报错
6 Z) m7 y  Q' u+ C5 p( S, C) m& O0 R; T2 a6 @2 C6 I( |* ]% X5 w
configure: looking for Apache module support via DSO through APXS
3 O' h' }. |0 U5 n% S' p& L+ T+ vconfigure: error: couldn't find APXS& ~) T) }2 _- h5 u9 `$ |+ e
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。- }  d( ?! @! C  n! n
解决方法:
% ], M' ~0 P4 d/ v! N! u0 A
% I4 c: j! o# C0 A. S  D  Wyum install httpd-devel
. ~9 R% `) M  H# v7 q# C2.没有pcre
9 ^7 ~: w- p/ M3 L2 P: X0 S  ?( o/ A- \# q2 U, y/ \
configure: *** pcre library not found.
1 V. M8 C& F0 H+ w! [! i- D' r9 o2 ^% Wconfigure: error: pcre library is required3 U2 l+ [9 f2 f8 c/ w1 v6 ?
解决方法:/ c& a9 T3 I! F# j" u7 T
" D7 E& i* X, H# I; @5 d* v
yum install pcre pcre-devel) |( h3 b% [7 Q5 m( g
3.没有libxml2, |2 i8 W) t7 _+ R2 H8 M6 ~1 Z

$ h- x' D, v( M* M8 i6 S
( n7 h+ U) K" z8 ?configure: *** xml library not found.
" B& J0 O% t; z7 K( rconfigure: error: libxml2 is required
8 t; Z% ^5 g5 Z# ^- b9 ^: P解决方法:
$ ]# M- w6 Q0 T8 M: r
* k2 `, Q/ O  s7 {yum install  libxml2 libxml2-devel
4 q- o7 z5 R4 Z; U4.执行 /opt/tengine/sbin/nginx -m 时有警告/ t: O1 X8 @7 K2 ^1 @- V" b* Y8 I

% U% H0 n( K! O8 G2 U0 `: z- MTengine version: Tengine/2.1.0 (nginx/1.6.2)
2 [1 \* e! i8 f9 a. H. Qnginx: [warn] ModSecurity: Loaded APR do not match with compiled!# T1 [5 ~0 k% G
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
  @' _- d5 |* H0 j) o/ ~6 ?  E, s
1 s$ f/ @8 y- J  x  C! W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
1 |! _# y, B) _8 ^- N" E1 n4 S  J' @2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
  v# o: T- ], u0 ?2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
7 T" h8 a  u5 Z2 T' i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
* G- [8 v2 X' F7 H* e8 k2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
0 a& P5 O' m- \$ ~. L+ Q3 M2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.+ G- `4 s- l; E9 n6 e6 s
解决方法,移除低版本的APR (1.3.9), K# P) r1 E3 I' ~, B9 ?# m6 G

0 _2 s& `; d1 K2 E: myum remove apr
* M/ i/ n3 i3 m2 u+ m* u5.Error.log中有: Audit log: Failed to lock global mutex
7 D1 K0 u/ t# _/ E3 x+ E3 b/ Q) |1 A# K1 u, U
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     6 T+ V2 a' I$ W  T# h4 ^/ L( g
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
% u. D1 @9 ]7 B, L6 J解决方法:% Z( T0 m& W2 @3 X$ d5 @9 I0 s
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:! o# H" g) t# |' C2 ]# V7 {" P
) T9 x: Y+ i, z1 }, Y
SecAuditLogDirMode 07778 ?: ^2 R5 G0 e! @6 Y
SecAuditLogFileMode 05502 d3 b8 ^) E  A# V  \
SecAuditLogStorageDir /var/log/modsecurity
7 W7 _4 b7 j: ~1 O8 H; wSecAuditLogType Concurrent
' t" t" w3 w8 `1 S' E7 B1 a/ a参考文章:0 x8 I" @4 }. @8 k$ f
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
$ F: I0 G( ^4 [" P% u) g/ Q- v8 ]http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-24 11:58 , Processed in 0.041027 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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