找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12899|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。( a$ \4 L! C$ \, Q9 {* |
# R$ \# b2 \( H. o" ~0 m
一.准备工作+ P: i! x( a9 f
# b$ S1 |8 h; O3 C) {' C8 b" u
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.00 u1 r! G, c+ l: E/ r: f
2 `- s9 n& f* l! i
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz% s1 y  }6 q& a+ _; k8 {

8 c5 _+ E" k8 b" n* Ymodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz( s3 @) a7 v% G: K6 M9 I

. E! N, y/ f; W1 D& z- m2 s4 hOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs: y% J5 T! Y$ _" @# H8 b7 ~# h. Y
. y( X$ r  i. |- j, A
依赖关系:
. X$ c) P+ M2 M1 d0 vtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
2 V  n/ K7 U( k2 t* e  W5 t) d7 r6 k& E+ o
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
/ k! a3 c/ t" q- o; f$ Tmodsecurty依赖的包:pcre httpd-devel libxml2 apr
3 @! _; o( K% o( C3 D4 Q$ E3 _  S6 `0 L0 I8 |+ f0 Z& ~- h: P, T
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
# ]( C$ x! n2 H0 l) I$ S' t二.启用standalone模块并编译
. o" G% V% N* P. n; R
& j" z; C' X) r+ Q; L8 ^: j3 U下载modsecurity for nginx 解压,进入解压后目录执行:9 e0 |9 z, g8 R6 P6 A. H" \( I# t

* g" y4 Z. [% S! v& [/ K./autogen.sh
  Y3 R; K0 r* J. \" T./configure --enable-standalone-module --disable-mlogc+ h  M# g9 Z; w- n0 |
make
  o% F6 R, o7 r, p; O$ {3 z* R三.nginx添加modsecurity模块' R& i$ n. S" w
: ?" ^, B( v+ f$ Y0 F1 p
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
, ~, K+ f7 P- u& x% S$ K
- v8 Q, C( l+ z* Z: b! G* X) o' C# W./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine0 z9 W+ v4 O5 s: P: R
make && make install
: T' x  h6 Y: d- T3 k四.添加规则
7 r$ W6 U' |  P# K
- K) w9 K$ M' F' ^9 T# B' dmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
& O) H" c$ b9 j6 s. k$ ^5 q7 \( e- P( x% P: r8 S/ q7 V
1.下载OWASP规则:1 V9 N& c% q5 Z/ ?
: w# d2 Q% i3 }" j! d/ U
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
, j# A/ N  N" N5 Y% [+ ^. X8 t! n+ I
4 g, A4 l- m0 @6 Zmv owasp-modsecurity-crs /opt/tengine/conf/
4 H( s% h1 p- ?! _; |
/ n4 ~; E+ O6 e6 [cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf6 k7 }; n9 O2 \# B" n; C
2.启用OWASP规则:" O2 k7 D8 F  Q0 g

4 W9 Z$ X( P: w) ]3 W. \复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。. x1 }$ h. \  F5 Y/ e. O

3 o/ N/ |( b) T8 ?9 k- }: e编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
& Y- X7 D# ]0 g! j/ x, M  o( ^/ D1 ^9 `# g6 Y$ K5 C, \
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
) z( Z+ Y( Z4 y% a4 E) Z, s9 Q) \5 ~: `
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
0 ^4 @4 @5 d4 t& d# W5 |; wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf8 w# W) d; x5 ~2 g
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
% p& @( O! N8 [, J5 ~Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf+ {, Z+ ]/ c' X; \/ X
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
% k3 T" G# U5 W# GInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf) S( U$ {  |, H( x4 w1 [
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
* f; ^. x( I9 S3 f* F五.配置nginx
3 f% [7 i: m( C$ P  i/ [% m+ }% ]8 e  o( P$ `6 h- p
在需要启用modsecurity的主机的location下面加入下面两行即可:
& i( }& a8 ]/ W9 N6 n
+ o. _: e  u6 m- H6 H2 c: YModSecurityEnabled on;  
6 O6 ?$ s5 b/ T1 U# d6 R/ DModSecurityConfig modsecurity.conf;) i5 R9 v' a4 A
下面是两个示例配置,php虚拟主机:% Q5 v8 w1 r& y' A" [, f/ {
  s( T( N, x9 J% y. T; i+ g
server {
1 G% w# Y) }$ a. s8 ~      listen      80;
) F1 ~  d" H( ]1 X$ ?      server_name 52os.net www.52os.net;; V: t5 m: h' M3 C5 j- U
     
5 `+ n) {2 b, C; }1 _! V      location ~ \.php$ {/ w2 c9 G: E4 I
      ModSecurityEnabled on;  
: D1 X! N% o; @- d9 f- V      ModSecurityConfig modsecurity.conf;
3 O+ e+ i0 t& p* r, O
5 u' E1 E( b& \7 {      root /web/wordpress;" r- l- {+ u  E( ]2 K! V) u) ?+ O
      index index.php index.html index.htm;; f! ?. J9 \+ P  Y2 Y! g
  
9 ]: T  i7 A) {0 U2 y: m% E) N# {/ i      fastcgi_pass   127.0.0.1:9000;' H% d# p' }2 S
      fastcgi_index  index.php;5 @: h: o. B* D' M# N2 i2 r( s& H
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
& M* |5 C9 N& d7 e      include        fastcgi_params;% t- I( {8 ]5 n6 t+ W5 ]" q
      }
  A# y, B7 Z+ S" R1 \  }
# C8 T, y+ o; ^! X' g6 kupstream负载均衡:
4 v* }9 ]  J. J2 I% h
8 Z' n7 P# k6 Iupstream 52os.net {
) M& u# G0 g, ~" v    server 192.168.1.100:8080;
$ d% y, h. a. W* P6 J  g9 h* R    server 192.168.1.101:8080 backup;
# W) a  {' r$ [4 b" [5 ?}
8 C) {: _3 e2 D6 j6 G: s/ e- {4 I5 C  m  p  w
server {
* k( N* I2 b9 N& Jlisten 80;
- D9 l6 \) }$ @( Gserver_name 52os.net www.52os.net;7 r- p( {. Z' c) l

4 _$ h" i* {! T- {) [. g' alocation / {; }- Z$ ~' k% H. n9 n
    ModSecurityEnabled on;  8 f7 F! K0 t9 f2 g& c3 j/ F
    ModSecurityConfig modsecurity.conf;  
' y# @/ R0 y7 a" T8 z7 \# g5 q: w$ ~0 O  _
        proxy_pass http://online;! d" p5 ^- }- B1 g9 V& t/ R4 D9 I
        proxy_redirect         off;
# E+ N+ z/ m  o! B! z# s0 ]        proxy_set_header Host $host;& m+ x$ U: @3 d' z: n) w( t6 |
        proxy_set_header X-Real-IP $remote_addr;
' m) U) W0 ^9 O2 A9 b3 q3 k! f        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
8 b& b  u  W" V- [! [. F% }1 x    }4 @' Y" [* W( _6 ]& r
}1 y& ]& ]- G; g4 k
六.测试* V' T* L5 c% U" d* \8 {$ b

. ?# t6 u) k) B" j1 @2 B' }* @9 E我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:4 Z4 `0 x4 y1 G9 n& C8 U
) J; h9 i% h3 O; c
<?php
( S  Y; u$ d; v+ H" [) L+ M  ~( T; m    phpinfo();    6 Q' L4 C7 i0 Q& g9 e3 z
?>
1 H3 s, l5 F+ s% P! E" Y6 X在浏览器中访问:$ M4 ?/ x: |7 R  p( f# P# d

& n: X: q4 e2 G8 U& ^7 l% x, }; _http://www.52os.net/phpinfo.php?id=1 正常显示。
! D0 N$ Z* ^, s! E( ]http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
! ]$ r8 {3 |2 u) L4 Thttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
& o+ y2 T0 w$ i. B说明sql注入和xss已经被过滤了# [; R0 r- c2 V+ D, `9 b0 |; [

, Y  M/ N% a# B, M七、安装过程中排错
. j5 t& C) w* C* N7 a' T3 j4 G1 g" M. I, Y: X& m% K/ z2 t
1.缺少APXS会报错
) e$ b9 T1 E; C; L0 E
, T) \. J5 a1 N1 fconfigure: looking for Apache module support via DSO through APXS- J, a4 H: ?, d
configure: error: couldn't find APXS- z+ b# b, ]' L
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
, X4 q8 t( Y/ h' j解决方法:! H  w0 Z% M9 e3 s" k

( @( ]6 y' I1 b7 uyum install httpd-devel
5 f( z- {: m9 k& w- m+ o0 [2.没有pcre
" t# k4 W' U) z& }# A4 s% Q+ k) C
configure: *** pcre library not found.; {9 p% m' k4 ?+ z" X/ R' q! f+ B
configure: error: pcre library is required
- t. t# [- h: f" o: c解决方法:
$ D8 _4 t: V( I& h  @3 p
& Q8 F$ k. Y+ y! w) _yum install pcre pcre-devel  @& ]% A0 c* b6 e4 L! L/ y& ^
3.没有libxml29 ^+ B* [; {4 o2 L3 Z

% Q1 y: S: f  c4 I# r, b) u" C; O! T
configure: *** xml library not found.: B0 X! Y* x% @4 g
configure: error: libxml2 is required, j7 I2 ?/ P! \% r& U1 ?
解决方法:. p# h* W" `7 `
+ T( Z0 A( z  A7 V0 y  l( g5 K
yum install  libxml2 libxml2-devel# f7 \8 y& b3 n: o/ \
4.执行 /opt/tengine/sbin/nginx -m 时有警告, [+ o" j5 f& v% Q0 A5 M) ?
2 p6 y1 {1 h6 i& C4 R
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
% K9 |* C7 \3 z' ynginx: [warn] ModSecurity: Loaded APR do not match with compiled!
9 v) R2 S. W  Q& |' V4 P$ A原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log: {, z; Y* b1 A- n

6 w3 n& Q5 ^1 j' G( J# y, A" f4 @2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.* ^3 f9 j4 K* l4 r# C( V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9": v% P+ e4 A3 U+ M# z
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
: r0 L: {# m" ~" D8 Q! x/ Y8 `6 F5 e! ]2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
# n; i- F0 ]2 y: g# j2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
( o+ i1 H; |, [- h1 E2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.# l5 b8 s* k. u+ K
解决方法,移除低版本的APR (1.3.9)9 z8 n9 P0 S) M0 L( \9 r6 }. r
9 T) W) y3 p7 f. V$ I
yum remove apr8 L3 G8 }) l1 b3 {2 z
5.Error.log中有: Audit log: Failed to lock global mutex
2 k# ]6 F/ H9 a& z' J; q6 S6 _8 r5 V$ i  s& }8 r
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     9 A2 F4 B4 S9 ]! w
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
7 U+ d' F6 k; O7 L  W8 c3 J7 Y, x解决方法:
% I+ ]. }7 d+ D. B% j4 v9 @编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:8 F0 l9 ]5 P; I

& a8 m  g+ N2 i# Q/ NSecAuditLogDirMode 0777" q1 L3 Q' E9 k# \! M4 ~
SecAuditLogFileMode 0550
& I; \) U  H% g9 {5 rSecAuditLogStorageDir /var/log/modsecurity# U) n$ D0 x4 ?$ D: S2 U5 ~
SecAuditLogType Concurrent
9 \* d' N( h0 u/ V2 N/ t% }参考文章:
8 `1 {5 V2 r2 D0 w* F6 H. hhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX$ r: X9 ~: |' c' `3 z
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-24 08:14 , Processed in 0.056647 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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