找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10315|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。" g; n+ H6 s4 O# F
- t6 t# T1 c/ M7 t/ c# x# @; T
一.准备工作  Y5 B5 t# o+ y5 f7 d+ S6 N" L7 `

# T0 i0 R  T+ }- C, T+ {" Q$ }系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0( G3 `3 i- w4 o" {
- Q+ i" I2 r8 l/ Q+ s
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz8 S+ A5 \; R: E$ F
$ N! n# K7 g' k' F* e  e' P4 W3 \. X
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
, O0 \! q5 V% s( |4 R  S. U  e+ A, g- {$ X
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
9 `8 {$ m: X- [/ E7 F9 p8 S
3 Z0 Q5 T- R: z* j依赖关系:
! w& z8 Z- g5 _& h" G" gtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
7 T$ D& ?4 x+ i; y9 m1 e1 J2 L4 X/ \. W3 J/ Y7 Y& P3 J
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel+ N  W! _+ b& w& z; Y2 h
modsecurty依赖的包:pcre httpd-devel libxml2 apr
% g$ `3 y/ t5 x8 [5 M1 Q( {3 [4 X6 g" G! }" @2 \# W
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel, Q5 P7 p4 a0 A, z+ R- g! L7 W; U
二.启用standalone模块并编译
) e6 Y1 E' U! P, J5 n- w( R  e; M8 P0 C9 |; t# n+ [% C; G: A
下载modsecurity for nginx 解压,进入解压后目录执行:7 p, [. n% Q+ }6 \* n  A" R+ \

% P- M3 C' Q$ [1 {& x+ K./autogen.sh
+ W' \9 O; A7 m' o./configure --enable-standalone-module --disable-mlogc
+ {1 H! _6 i& |make
( o) t+ P* O' K6 q7 l$ ~% N" d三.nginx添加modsecurity模块
2 f6 B5 Y. T5 g- b7 K% n" j- [/ k# V/ B8 z( k8 f
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
, X; L) [6 p( c% B+ D5 z9 `# C
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
% f3 o: M3 V; S( fmake && make install
0 Z% k* z, J0 j1 a7 q3 }6 u四.添加规则
8 h$ K3 t: w  ]. O1 H8 n1 U' v" Q5 v# z* I! W9 @
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
* q/ H) t& T' Z0 I# @0 O1 I1 C0 y/ j- Y
1.下载OWASP规则:; A. ^  ~5 Z% d3 j7 L& y

. s/ T6 Q- g' U$ ugit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
  ^& A4 ~# y' j4 v+ J5 V
" D  n# h2 b4 Y: Bmv owasp-modsecurity-crs /opt/tengine/conf/
* ~7 V' L" D& F1 w  j* r" T3 z" V0 j6 Q; g
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
! W( c; g1 t, ^' c) `' _/ u$ R2.启用OWASP规则:- B- }/ B. x2 a1 d( W% R

) V0 I" d* M0 K6 s* z  z" X复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
# B6 w0 w( p; H9 H, R' i5 h) D- g$ e& U( y; N' R
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
. `, X$ c& x3 ^/ H( `$ P6 R8 t" N# m6 ?. s4 s
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。8 E% i4 Z, {; l$ U! Q
! G) d6 X/ f7 Z' N2 A- S7 ?; F
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
( J4 y; G( v  [  z: H/ KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
9 y: H0 V) F* H2 M: q* l6 b! O& GInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf' d- D' f  d( T
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
; e, }0 Z' a# e- M# I4 N; tInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
4 e# w  C) ~& @' {, ?7 AInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
+ `: a* ^5 D% v8 W/ TInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
/ K4 q  ]- i2 E' z3 h. g! B  M! E五.配置nginx
' r$ `* C5 O) s( n
! I7 w. u7 n2 o2 r: a在需要启用modsecurity的主机的location下面加入下面两行即可:/ J% c$ G2 j% s. C

- T8 {; q$ {' l$ m# q* ~3 eModSecurityEnabled on;  9 q% S  d9 d# S1 p
ModSecurityConfig modsecurity.conf;
9 p, R* i; b  d3 ]4 r  A下面是两个示例配置,php虚拟主机:
8 n& I7 q, p" i$ v$ }, `5 T+ k' o. v. z, S$ q0 k# U. K
server {
+ |( X: d& S6 i  f, v+ V; _      listen      80;
# j1 Z9 m0 V8 r/ R" n( U      server_name 52os.net www.52os.net;
9 v8 t2 @% ^/ B* P6 _! |4 g. Z- m1 o     
2 u, d6 m1 }: \* M4 q      location ~ \.php$ {. M; |3 ?) z' X7 n! j# t
      ModSecurityEnabled on;  
* E  Z+ b/ t' l! }      ModSecurityConfig modsecurity.conf;6 {7 @" d0 L/ a: u& a% x

1 t5 ]" ~! `1 R      root /web/wordpress;/ J% I# x5 _& R$ G
      index index.php index.html index.htm;3 p" B: Q: y# b- ^9 Y1 S
  
& g! O) b$ ]! v; X! j      fastcgi_pass   127.0.0.1:9000;( I$ G* w' [1 x( L1 ]
      fastcgi_index  index.php;
0 ]& }5 i# [, S; L2 V2 k      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;; a6 {; J  Q8 J  m% X# S
      include        fastcgi_params;! w, @3 ^5 K5 Z9 ~* W$ H
      }
1 R) v& K) z# G5 s  }
6 q( E5 ~# {+ K% L& a3 x+ A/ zupstream负载均衡:5 D, a! V! W; C* d) ?
% c. X$ g+ m$ c4 ^* Z' t1 c
upstream 52os.net {
0 `4 A% s1 w0 X- n* K" l. D    server 192.168.1.100:8080;3 U% }2 I& h: B
    server 192.168.1.101:8080 backup;
- I, K# C) d% @# |}
0 {5 o  f! L# \& r0 w; f
6 s3 ]$ A% b$ \( T' [) Aserver {
% Q0 P$ x4 q1 g3 jlisten 80;
8 m6 ]& M- P+ A: o) Lserver_name 52os.net www.52os.net;* d# ^# o# Q8 S# Y3 t, i$ c: b* f
4 x- ^0 x: a8 ^! Q
location / {
% J4 o" u' K0 b2 u5 o2 v' _    ModSecurityEnabled on;  0 Y# X$ k. S/ a2 z9 z
    ModSecurityConfig modsecurity.conf;  2 h+ x% V# d0 x! O
* Y% c) G! K0 t9 n* U: I! m
        proxy_pass http://online;
3 E) p& m& Q& Z        proxy_redirect         off;7 W) \( Y- ?: N2 s, S- V% e
        proxy_set_header Host $host;4 L- G5 W. J' |. r
        proxy_set_header X-Real-IP $remote_addr;
% Z7 h' j7 ^2 B$ L9 K; o        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;( {) L! ~# W6 P" S
    }
; c: b- o; i+ @$ r( A! ?! `  l}% T$ ~$ N. D/ E7 ?7 l8 }0 S& O
六.测试
, u* e! L( t. s& Q6 }5 F" Y. z
- @0 z2 x6 e0 V- o# F+ m我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
1 f1 a& T5 W& E: `7 Z8 v6 p- [: }2 Y9 C# W
<?php
% g* m) V: d4 }8 E' L% S2 H    phpinfo();    ; k; a6 b& O) }- _3 w& T- Q$ M
?>
/ w4 C( l- h+ v  a& f在浏览器中访问:8 C, K) R  P* h- u7 R3 g+ e. i3 s4 z3 [

- U. I4 U8 @! [0 Ohttp://www.52os.net/phpinfo.php?id=1 正常显示。
. ?' P3 l* U2 P5 fhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。. }' N7 T/ h% ^) _# b
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
: C" i2 P( `+ e; L说明sql注入和xss已经被过滤了
* J7 d. S- [( ]+ c% `7 }. X2 I
2 b0 c4 p6 y7 L' l' d' |7 w+ v七、安装过程中排错
8 K7 {% j" p1 y# v' V2 {9 L7 O/ y* V' T# [0 t0 W& |' s. C1 e
1.缺少APXS会报错
: T9 m1 ]$ R+ P6 A8 [1 g
" j: l+ g* c/ W. w7 c; ?- m6 Yconfigure: looking for Apache module support via DSO through APXS9 x4 x+ _) c2 d. F5 c5 i
configure: error: couldn't find APXS
$ A7 b0 ?0 m+ \. ~3 J. t7 sapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
4 N3 P+ V6 M& x; w6 h解决方法:2 O, M" i5 ~1 |; f4 n( R
5 n+ @2 s2 Z# ^) a" j; y
yum install httpd-devel
: ?3 a& o0 c$ [2.没有pcre) E( v3 w9 B) X8 ?/ s% a  k

3 j: c9 `! a- a  y" H' Econfigure: *** pcre library not found.
& E( K: D: M- T. yconfigure: error: pcre library is required' h; [" @$ X5 w7 k  U
解决方法:
. ~3 S* x: _4 E3 l9 W1 `* D+ y7 j, B2 f& c7 E
yum install pcre pcre-devel
# q: d8 T. Y' [, k: y7 g3.没有libxml25 a, u( W' z/ q& D" f
6 n; A) Z  K6 r5 |2 q& W# p: w
( @) ~  i+ J2 ~
configure: *** xml library not found.
1 C2 o8 }4 P* a5 V3 vconfigure: error: libxml2 is required& Y3 J1 }% t  q7 U; H1 M  A3 t
解决方法:
: }1 ~( v) F/ k1 o0 Y3 o. r% d8 K6 u8 ^7 J
yum install  libxml2 libxml2-devel
* t! p8 X7 j* _) z( C( T4.执行 /opt/tengine/sbin/nginx -m 时有警告* o5 T! l. @' p
" s( F2 V3 U# i! E3 |$ K
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
6 l- ]3 p9 x1 U+ ?nginx: [warn] ModSecurity: Loaded APR do not match with compiled!. E% W6 Z7 a( I$ j  S
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log+ x5 Y) R: Q0 l4 q- E

) ^0 e  L; B% h8 Y+ |6 b5 k+ W+ r$ D6 i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
: c. D. k" y. _6 N7 c: U, I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"( |: C. v1 `; y5 r5 g7 y
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!5 X% w8 P6 z* K& S8 G& P4 Q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"0 `' O, r" y$ O' q! d& M
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6". w3 k) g3 H/ y9 S  L8 H8 \
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.  G2 g8 y3 L5 R
解决方法,移除低版本的APR (1.3.9)( S4 t5 D8 ~9 [: Y$ i% A/ o

4 R  A/ S& }; |7 ayum remove apr' p% m" }9 C) U- d
5.Error.log中有: Audit log: Failed to lock global mutex
& M4 r6 }5 N: ^+ C) |. |% h1 e3 f
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
& C. e! |3 ]# G! l& G0 K7 L5 kglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]: h; h* x: R" \0 o/ ^
解决方法:
4 l- U3 ?' O; D7 q) F/ n编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:$ C* _" d5 {( K3 v' Z' w" u( e$ I5 V
; Q$ }; |$ u4 Y4 W7 ^6 Z/ z8 M
SecAuditLogDirMode 0777
& r: \3 f7 v) f' T% k) ASecAuditLogFileMode 0550
+ d+ }+ q0 e( P8 m( gSecAuditLogStorageDir /var/log/modsecurity8 X8 g: c. j+ t( S
SecAuditLogType Concurrent: p! E' f4 f4 c; n2 L
参考文章:
$ |+ X" Q1 w* H1 Rhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
. q/ D4 W/ D+ G0 _; w0 X2 rhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-3-21 19:19 , Processed in 0.058142 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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