找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12933|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。4 g& n. D/ U8 T( ]9 j7 B# y
) \- {, C4 |7 D0 ^+ N
一.准备工作
7 r* U& f- c7 j0 p1 W7 g
4 t% t. n  \1 ?; L系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.03 j# ^9 ]* z4 C8 N. |

% l1 r" m8 q0 W" I+ y+ Ctengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz5 w1 n  V& A$ w0 p, j; L# u, C4 y
2 s2 [( y1 e% G0 Q! z
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz9 |) q; {8 \  b
. Z3 Q, f7 I+ B; c) V. l
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
$ @9 v9 ]" m6 X% S! }% I* `2 f- ]' r: f* P. l1 P8 e
依赖关系:4 |- Q% I) T4 }8 r
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
) s2 Z2 s% r; I$ _
9 m) a7 C6 ]/ ]1 byum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel* b6 i8 B+ D' `9 V  M' \
modsecurty依赖的包:pcre httpd-devel libxml2 apr
* Z6 n: k6 L# f: Y$ W* R: F6 s" j" _* `0 d( @
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
( u- P) s  a" A0 |二.启用standalone模块并编译. |$ R" |. a8 e
% q) U- `) {( H- E7 w
下载modsecurity for nginx 解压,进入解压后目录执行:! g" V% g" z1 b3 Q1 R2 p9 Q& f) J7 L8 e
/ j* J2 u( K0 h9 h" |& T5 g- W' I
./autogen.sh  T3 W! v2 M8 @/ `
./configure --enable-standalone-module --disable-mlogc7 w' g& X8 K' H& w
make / F$ m1 `. m2 M& W1 p4 X
三.nginx添加modsecurity模块/ f8 y* }$ O6 W4 @, V5 {
; L* ^3 N& H1 G# v2 |
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:# u) @% U' w# {. d
# u: n/ r4 S+ o; J: L6 {
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine4 B6 b( q0 `9 T( n' O: c6 Z
make && make install
+ O( s2 d1 d# \四.添加规则
1 }5 c4 ]/ ?8 B( @/ B& J1 S8 o
- |2 Y8 X9 o( ^& A0 u3 imodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
" c2 }# M8 \  |& C; D" `9 G9 f4 [  _' t4 I
1.下载OWASP规则:# d( f4 |9 j7 p7 H6 ]
8 p( {" y+ ]0 D* d7 {5 H7 t
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs3 ^( ^* p; d; d. u6 K

+ n! ^6 w5 e- `5 b  \0 t3 {mv owasp-modsecurity-crs /opt/tengine/conf/
0 j2 B( d( E4 N9 k
7 X3 ?0 ?( j5 ~4 Ecd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf. f* \) E, h6 `5 T* Z1 u
2.启用OWASP规则:  V) p" _, O' ?9 Z8 I9 w1 j

, l- T& B( X& z# I; i- X复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
% d  w! P" l# s0 H7 H# b2 v, K* I* k4 I& P1 q
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on# a0 i* }  \( {" O: y% b& G& P4 x

; S& G9 N" `) ?6 towasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。- x& t9 _0 e. w. X

8 _/ a0 G5 B3 |  Q1 {+ {9 o' tInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf9 E# x- K& D1 _4 |1 Q9 c, }/ O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf; }* \+ o% j2 |2 O& i' O* j: B
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf! E9 ^, Z& l+ x" W2 i7 b
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
  I( S# _5 g8 s- [2 D0 O; ?- HInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
) E7 I+ G% }: C" U5 Y. l& ?Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
6 X! f3 R+ P& Z& ^Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
, X1 i/ j8 z0 Z五.配置nginx
& u6 z/ L. \! C7 ]4 G! q7 m& T  e/ i9 k4 ]" t! T
在需要启用modsecurity的主机的location下面加入下面两行即可:
3 a# B- z' ^! c: i7 i) g) ^! B' M6 l4 t* C# @
ModSecurityEnabled on;  0 V( c! U9 j* Q. t
ModSecurityConfig modsecurity.conf;6 H5 C. @. g, P$ Z; l
下面是两个示例配置,php虚拟主机:' A- ^9 {7 B  N. L. u
7 F) k# a$ w# T, r  \3 M
server {
0 N+ `/ z" B) j      listen      80;
2 }+ P- m' T0 s1 l& M& ^9 v      server_name 52os.net www.52os.net;# s% T# K# I* t1 {( l0 r" z9 n- q
     
8 u" U) P$ G, \* ~3 e* h; w      location ~ \.php$ {1 F2 S+ h- Z. y: |
      ModSecurityEnabled on;  
) t- B" ]' M. ~8 U; L      ModSecurityConfig modsecurity.conf;4 W7 Y, |* l: W: I
! D% L& O/ `  E5 l1 F; r* {
      root /web/wordpress;
  H$ d6 r% y6 l* J; p) E      index index.php index.html index.htm;
0 H2 g6 y9 k0 M5 O$ U$ J  
! l  ?9 ?, Z. _. S* H9 j" |# o* r      fastcgi_pass   127.0.0.1:9000;; r0 j, ^8 I4 b. }/ c
      fastcgi_index  index.php;% k& j; i% D- H$ _9 ?0 q4 x4 J
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;# i6 n0 V, g' k" x3 A
      include        fastcgi_params;/ J+ X3 x8 G  y0 P
      }
3 H* g1 e" V7 F- U' j  }8 J# ~% Q0 }4 O
upstream负载均衡:: T' w' U  b1 A3 b) T
4 z# x" U* [2 t  G# t
upstream 52os.net {
! n3 d. z# i/ }) G; C    server 192.168.1.100:8080;7 I, z7 u6 n' B2 u) ?
    server 192.168.1.101:8080 backup;3 S% _( U7 G  B( D, D  o' z& B5 T( }
}6 I. \' d  }& U+ _  I3 e
* R: \" P8 P1 l! X
server {* @& @% m  G6 W! C4 G# O7 O6 B* m
listen 80;
' w0 |7 R6 R6 K; I. Pserver_name 52os.net www.52os.net;, y; ~" N5 L6 A

3 R, Z8 }% p2 u* [, `location / {
! _; K2 T5 }3 I: l  Y$ S; l    ModSecurityEnabled on;  ; \/ Z" @7 {8 i3 q5 i& h
    ModSecurityConfig modsecurity.conf;  7 ~3 |/ I% p4 \& T" g( G

/ c7 k) j3 w' H! u- o7 I1 F        proxy_pass http://online;3 ?; c4 j7 \, ?0 a2 g
        proxy_redirect         off;$ X1 p' k* M: ^) |% \
        proxy_set_header Host $host;
9 @& M& Z( e: I: }        proxy_set_header X-Real-IP $remote_addr;
- e' F2 M% [" V( e/ q        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;. o1 F  d6 y% \; h
    }
) H/ k( j4 H7 e3 q}  o: F9 E& i' V# U
六.测试
( p! p. E- H3 z5 |9 b1 I! e+ W% z/ P9 t! s; F9 A& d3 P
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
5 Z% t" H. _* d  ^, r4 p5 N2 O! c* [
<?php
9 m1 P. S( u/ m' J2 T1 L  a3 ~& ]    phpinfo();    ) f" {" v6 ~6 k- ^7 r( d9 ~: {
?>5 P1 a$ Y6 {9 P' Y
在浏览器中访问:
, h% ^$ s7 J4 S4 f- [4 s* e( w
http://www.52os.net/phpinfo.php?id=1 正常显示。
" a) g7 r$ Q. \' D. ?http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。: L" ?  N" H' H: Y" ^
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
4 u8 S; F+ |  H' ]9 {说明sql注入和xss已经被过滤了7 ~+ K4 _3 S2 C+ a) [

% i' X) z0 r% v2 Y  U5 u5 Z  V七、安装过程中排错
! K) c) T; `) j) {7 p
7 W& j9 F0 I) c2 G! ?0 C( Q1.缺少APXS会报错
! C  M7 L* Z% T2 {2 N+ |0 B
6 ]% v; M( t* j* z+ q4 R" Aconfigure: looking for Apache module support via DSO through APXS
6 |) N! l8 g9 _6 ~) @2 Q$ o  Mconfigure: error: couldn't find APXS& r/ u% L" A7 }! a" }% k
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
5 E: f( u4 x7 T1 K( ~1 s解决方法:9 s6 D4 d) ?) r4 e- n

! u# z/ c6 d) P( X' B2 eyum install httpd-devel% @- ]% }; j' w# g* J, w
2.没有pcre
' R; e; k; a- m1 `6 m( l$ B  U$ J1 G( _8 `: k
configure: *** pcre library not found.
) L+ i) y; t9 Zconfigure: error: pcre library is required) R% s9 q8 M) H; X# l# j
解决方法:7 i, w/ a2 M3 ]# h1 Y$ u' j

- m# w6 @) t9 e# v! v  q! e: Ayum install pcre pcre-devel6 p7 c) t8 C% C" y& F% m$ d
3.没有libxml2
( _( V* s1 ?0 s0 s0 r* h* F
$ T# z- Q' v7 G# d6 E$ M  I0 b' V  c% ^: R5 U" a
configure: *** xml library not found.& L  Y, p; e2 g. H- Q4 x, Q1 l& D
configure: error: libxml2 is required8 A6 v. z. w7 a1 Z$ T/ O6 u
解决方法:
' _* T6 v/ V* J8 Q, ^2 C. ?1 j1 X, a( P8 [7 S5 G
yum install  libxml2 libxml2-devel! G" s) {* W+ I1 V/ m3 S4 f
4.执行 /opt/tengine/sbin/nginx -m 时有警告4 q  R- T0 b- v
+ i4 I8 ?5 W$ z0 [  E
Tengine version: Tengine/2.1.0 (nginx/1.6.2), w' z) \4 ^& ~( A: y* z, D
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!9 {1 H" M1 G5 X8 l  ?5 _
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
' |' a- w0 k- w+ G) x
7 u  i+ l: H4 Q: e- R3 y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.7 w- e* W* I: R8 o2 M" O8 R* Y0 E
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"9 c3 g1 m; [8 }+ W& p( {& ^. Q
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!3 a8 A9 z* Q9 e9 n
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"9 t6 V* f* m) a6 @7 z; F7 n3 i' @
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
2 M$ q! z9 L) b2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
* S% p6 E8 w* |$ n& `解决方法,移除低版本的APR (1.3.9)) k8 E# m1 k1 y3 r5 k

- Q, g' [) l- I/ |: u/ X& n* q. Ryum remove apr
# Q% y5 P6 N% Y6 c! T' i9 P5.Error.log中有: Audit log: Failed to lock global mutex
4 Z3 I8 R  t  j# w2 C
9 n: J% i. V( ~2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
# }, h# p6 G! I- ^global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
6 s# t$ d$ Y9 Q; P' [解决方法:
, `! D" Q! N0 }8 d$ C编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:3 d+ I1 W5 f+ q5 M

% {: U9 X0 t& u* ~. VSecAuditLogDirMode 0777% M, i7 G9 \9 i" `- e8 F: r+ s4 _5 I
SecAuditLogFileMode 0550
# f7 A6 c- b3 N9 s! w' D% }SecAuditLogStorageDir /var/log/modsecurity
3 l; F3 Y9 |6 F; nSecAuditLogType Concurrent5 C! ?$ I! A# B4 M
参考文章:$ T% V% O7 E' v" `* c
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
/ m6 G* d1 @8 S: rhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-27 09:34 , Processed in 0.098712 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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