找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9339|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
" I$ C8 y. E0 p! q: A9 B
5 s5 E  a( {' {# [一.准备工作+ |" H, \2 ?% W: _. I! l
2 x; J- t4 J9 @! T
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
& e0 b9 T  J' k# b5 r0 z) J+ j4 ~& w& Y5 f6 h5 g* u9 D
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
. |7 \$ E8 h$ ~% U' s" o
! S* J  i0 {' Omodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ w( S) ~7 b& p4 H
3 o- _/ C# y/ E! k2 zOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
, @9 H* r& R0 a  b" f
% z& O1 F$ F/ S, I8 I4 B8 d8 }7 z' z依赖关系:
& Y* N) x0 O1 ^, r& R' ftengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:! ?: t+ y- O# {( e$ C* g# s
2 h7 D4 L- Q8 p: c5 t8 Q* V
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
$ P& p' e+ p/ W+ P3 P. s  Y9 jmodsecurty依赖的包:pcre httpd-devel libxml2 apr
; U8 a+ c% Y; b
) J9 \' }9 a5 T- K( pyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
. l, M+ P0 m1 q5 S4 v" q二.启用standalone模块并编译
" r; C- ]3 q2 C* l7 _/ t. l5 E; A& X( k; s
下载modsecurity for nginx 解压,进入解压后目录执行:3 U+ Z& p2 a5 s: p# l

& K; K2 Y7 n1 }% R! b  @3 d& V- i/ [./autogen.sh
( D* u% B& @6 M./configure --enable-standalone-module --disable-mlogc4 V/ L7 b* [, m5 |1 D
make 0 w# ]# X8 x9 t9 I0 J7 I, f
三.nginx添加modsecurity模块
, L; {. F- |- A2 C) }' e
! ?! c. u/ N  X8 X" P在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:2 y( R: x$ l: w: ?) {$ _
. `& b& [1 C. S0 h: X- N
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
0 ~8 s. e4 S# j' o) T/ q: nmake && make install% Q4 L" p. D& f
四.添加规则" k! }7 y9 V! p2 i' h4 h- `

( f2 C8 J+ p2 I3 b3 Lmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
% L4 _8 o, n# L* H+ }. ^. {5 t* J9 M; t4 @( O8 h
1.下载OWASP规则:5 c+ ^6 x" D) D0 b& q
3 f, q' ], \+ _0 s9 F
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs" q2 Z- c# J+ p5 j# E" T3 h, v
% n4 u6 y( `4 j. g# {" I2 O# V
mv owasp-modsecurity-crs /opt/tengine/conf/7 r  k7 E) P- ~  p: c- T
' }9 u2 }  ]) z; |5 W3 ^6 U% w
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
; @. S; a5 W* A2.启用OWASP规则:1 D. ]6 r2 g4 K
3 q6 v) l6 Y9 B' o9 i
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。5 G! L5 b% V, b' z, v: x
- b+ H  Z/ d3 `. `; E1 w7 ]( a
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on( q3 ]' A- A4 s# R+ `5 F  h9 F* |
7 b+ E: z9 Z4 M  [0 u
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。: r! e5 b0 f4 X) e1 i: N! l) z# B
. n% q# O2 Y7 B. a8 D% k7 U0 Q9 C
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf) c. Q0 Z8 Q0 m( p! G! \
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
. Q; T, L% Z$ j! L. g4 hInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
: }1 z2 F" i! x9 `! S( @7 Z! T% c7 j5 |& uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
* l1 [1 n0 J1 d* R! OInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
- r, s9 e% M  m- sInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf0 k6 x% P( @+ \$ G' x  F
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf6 Y, ^% J  M& s6 q9 B( r
五.配置nginx
. x2 v/ Y* h, ?, [8 S, W4 M- b/ Q0 A' U; Y! v# S
在需要启用modsecurity的主机的location下面加入下面两行即可:# z& N8 v, a  k6 N
  r" a& M* B: o! {2 f0 R
ModSecurityEnabled on;  
3 C* I: v# b$ m' f" s7 nModSecurityConfig modsecurity.conf;
! n6 `6 _1 _! F9 M1 p$ w0 h5 V$ V下面是两个示例配置,php虚拟主机:, J- I! V' v6 a
2 O, D! W7 w. k7 d
server {
9 _& _3 T4 M" R+ a$ \      listen      80;
5 W$ a( E5 j+ D# {- R      server_name 52os.net www.52os.net;
. J6 s  P+ T4 Y     
) F7 _6 l( I0 R8 D      location ~ \.php$ {+ Q' h  S' O1 A8 f* f
      ModSecurityEnabled on;  
1 i5 N) e  e" \' ~      ModSecurityConfig modsecurity.conf;+ {: v1 D; Q6 y& F

& x1 N1 T' [; W8 [8 ?, h      root /web/wordpress;: C& V, \# \( w3 q  |. g5 T- ?9 C
      index index.php index.html index.htm;
- C; q/ T2 M, @6 ?- ^' n) {1 {8 n  , u& C/ U+ l! m3 f4 {/ @
      fastcgi_pass   127.0.0.1:9000;$ u% e& }1 L/ J# J: B( L+ I. H, L3 f
      fastcgi_index  index.php;
. e1 K- C' O  K8 t5 H      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
- f5 ?/ r9 x! Y+ B      include        fastcgi_params;+ S7 H3 E2 Y7 N
      }
  w& O3 M# D$ j" z! w4 X+ [  }
5 y- T+ [) S9 }& d( y5 Q2 kupstream负载均衡:# d( W) o0 m' W# k
1 `7 ?# x0 R  Z1 A# y: }
upstream 52os.net {- F5 l8 ~2 m- H/ H9 p; l3 S
    server 192.168.1.100:8080;' V7 _0 R, \$ ^
    server 192.168.1.101:8080 backup;
' t4 r3 X. i! ]$ q: ^" X}( C! E7 c) x) {6 O/ r) T2 b( q: w

8 }  h7 O" U# u" N* a' g; Wserver {3 _8 r$ a- m+ E- ?
listen 80;/ K( O! P% f+ V7 Z# Q5 Q6 r
server_name 52os.net www.52os.net;
9 _6 P0 ~% U9 v$ i0 |6 _' n% @/ |3 D( T- ~
location / {
1 {& p$ i. V. p' P7 s3 S    ModSecurityEnabled on;  
3 \$ Q# \4 Q2 T6 o* k% b6 c) f    ModSecurityConfig modsecurity.conf;  ' c# J: K0 S' m5 M: ~6 V0 [* j" Q) s
, j/ R. @/ x# g! Y1 F. D
        proxy_pass http://online;
7 |2 _7 w& [0 Y- l& z  [        proxy_redirect         off;
- _( a# j1 ]$ a/ Y3 q- O/ w- ^        proxy_set_header Host $host;# ^# s  J+ u/ h1 \" \
        proxy_set_header X-Real-IP $remote_addr;" g! |+ x" W& u8 s, X
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;2 j/ u! m/ _$ Q6 E
    }! o% @$ y% R% l. k4 F
}& m9 ?9 L( B3 Z0 j
六.测试
, i/ S7 y, S9 [/ I  r* u8 e8 e, g( o$ D& d. b" L8 Y% I% g
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:8 Y( M0 P7 W# R5 W9 c0 N
9 i  Q# x8 ?  h; Y8 b
<?php3 Q9 O: _4 [& j, @# A5 r
    phpinfo();   
6 k( P$ f! j! w( B$ W?>
6 M( m. f) {1 F) Y在浏览器中访问:
. p( ?" T. m4 m
# N( x' h4 w$ S/ _9 N4 m8 Nhttp://www.52os.net/phpinfo.php?id=1 正常显示。
  j' E7 x( ]% @% M" i1 b" I) ohttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。) ]1 t7 d1 ]7 e# d1 _8 n( T
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
" X8 \4 \3 z" N4 ?' X9 v+ e说明sql注入和xss已经被过滤了
8 [% H  T5 v5 `0 K1 u; O
% ?- w' i1 ~( a7 s9 C6 Y* h+ |$ `七、安装过程中排错) A) M  }; e4 r0 B1 K: {: |* Q
6 a- N' Z8 }$ `& x% a
1.缺少APXS会报错
; y( h" t. i5 U: h; E% S8 o
% Z1 P% F( P( B  mconfigure: looking for Apache module support via DSO through APXS
9 V6 G) \( `, G, E' oconfigure: error: couldn't find APXS5 a. D, z; ~! \9 d" l
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
* b# r' F2 z' m: b' S9 v% r解决方法:
# e" m) Y0 |; N4 T$ V
3 G+ C4 J/ E* [* `2 W0 Syum install httpd-devel
, N/ R* V( V0 A6 v6 D7 n2.没有pcre  h& Y3 _( K8 _8 e1 D, A0 K7 \

) i8 u" U3 Z' ~( C# K. z& Y5 J8 Mconfigure: *** pcre library not found.: f- x  P! c: t/ u- q6 j" ^
configure: error: pcre library is required, H! w. [  `, [" P
解决方法:
$ A4 M) U1 A" ~) _
7 Q( n/ d$ X) kyum install pcre pcre-devel
3 K+ Q$ ~* n* t" v3 M. J3.没有libxml2
9 ~% ]! |2 x. M+ h# J6 p1 {2 P" u
& K3 c: E3 Z" V( ?
1 Y0 T, C$ ~$ \8 ?6 ^configure: *** xml library not found.
7 x' |* V; s: O! R7 bconfigure: error: libxml2 is required
0 P$ @0 G0 s4 n. u' L& K: ~4 n9 ^解决方法:' \0 D4 s3 [, H: \
1 I4 s. [. ^1 A7 _
yum install  libxml2 libxml2-devel
9 v9 Y6 L3 J+ \) l4 l4.执行 /opt/tengine/sbin/nginx -m 时有警告2 K- m  U9 }" T
( |' \/ U- @5 o6 L3 t+ y/ G
Tengine version: Tengine/2.1.0 (nginx/1.6.2)3 ?2 ]6 G' Z& T: J. [
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
2 Z- \' y% L3 Q* v2 X  z原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log/ G  H1 |7 A0 B4 [2 ?- e6 V( h

) J& F5 Z& H. W, ^6 ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
' l% R6 Z, p7 `' c. W/ h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"( v, v, u" s4 x
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
; u. @! a+ M# m* ]- o5 o" H2 I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"2 U7 @$ T9 _% j$ m. [
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"5 ^5 d2 p/ {5 F& C
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
9 E4 ~3 T+ @6 h4 L& H. E. ]) n' v7 \解决方法,移除低版本的APR (1.3.9)# P; E! G' r! l2 Y9 j7 U
7 \9 N* U; `7 `# g5 C# x
yum remove apr1 [0 f- g' O* `$ s. B
5.Error.log中有: Audit log: Failed to lock global mutex" `: T* {* T; M9 o2 T2 V8 h+ E+ i

; Q5 Z* T7 V. d5 D7 o/ P0 K2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     3 z9 X! {) a$ C. Z
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
' r. U5 _& m, Z/ x/ Y' c6 P- Y解决方法:, `8 b# h" }. e& S
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:) d0 G* c# I& L: S: b5 J/ `( a
' ^( V' M2 E( C4 M
SecAuditLogDirMode 0777( J8 A# v& z+ c6 V. Z
SecAuditLogFileMode 0550. l) S* _; ?- y3 }9 \
SecAuditLogStorageDir /var/log/modsecurity
7 W+ l+ [( ?7 k9 |, h4 KSecAuditLogType Concurrent' W- {- _3 g7 m3 m
参考文章:% {; _* _; r6 r( T+ V; _6 b
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
9 t) s1 I7 E, N8 m7 b9 J6 xhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-12-8 08:14 , Processed in 0.057584 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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