找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12396|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
, ~" \9 |5 J- ^- N; y9 E$ p( X8 H$ v8 P6 e8 X( u
一.准备工作" Q% G8 U, q# X+ G7 w! \. \. c( V
# s1 h2 D, N' O
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0$ F3 ~. T! b. n* M3 R9 m4 \
4 Y$ o, }# [9 ]4 F  d0 Q% a
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
/ h3 n& [8 N$ G- r! K- y- Z4 s7 J1 E+ L: d$ h) |/ i( p
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
7 A. ~* E- ^! [' q( Y
8 w( X8 d" M+ O, G1 p8 nOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
3 R1 P/ }, w% E  H
' h% o- \' }# a# l( N. A依赖关系:. p$ ?* G& G8 P% S: u, ^
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:! p# G  A6 B4 B  n

' B- t  r5 T3 G; iyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel% f" l0 y0 F; d8 _1 p4 S
modsecurty依赖的包:pcre httpd-devel libxml2 apr; @0 X5 b' T5 D8 t0 D

# ^8 v: \4 X. V5 g) \% P7 n8 s9 hyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel5 u, B; f; }2 K, G
二.启用standalone模块并编译
1 Q+ G0 j! ]: B' l$ g& o- r0 f, `! N& R
下载modsecurity for nginx 解压,进入解压后目录执行:
0 p# |' v4 _/ I3 L2 {0 @, A
$ ^, q" \( a# }& ^3 C3 N./autogen.sh+ H! }! l6 T! I' _& J* k+ J
./configure --enable-standalone-module --disable-mlogc- s  W; d9 R" _6 Z# L2 s0 r1 z
make
- v: k/ J8 T4 p" [+ }三.nginx添加modsecurity模块
5 L7 r( q* e5 a  ^. ^; @
) B% m+ u; k, \2 k' p' X# U, }在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:1 s7 e5 ~$ U3 c/ F4 H

7 `+ y1 L, C0 |& j) G./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine1 v+ L7 s9 R  Z  K, y. M# \: S
make && make install
" y3 i' \( r, t- h四.添加规则
' ^! n3 Z6 O! R8 k7 {' r
1 c$ v. F- @% I/ q6 Fmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
3 H3 b3 v+ f1 b9 J( \$ @; X- r$ e6 h7 \: {
6 F& P5 g4 ?; |; C3 r6 [4 ]1.下载OWASP规则:/ k% b  n  Z1 W4 U

; [. H; E- ?# J% Dgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
* p$ \& @' f, W: H0 d& m$ U" U7 N+ x4 S
mv owasp-modsecurity-crs /opt/tengine/conf/* \7 G- {) T/ S7 j: A% ]
& c0 F! l$ d' v
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf$ t1 n8 ~& v& ^1 I  \3 y+ B
2.启用OWASP规则:
* ], C: M- x7 O2 d) `! J
. {  n) j8 Q5 c/ e. A" ]9 J: v. S复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。: i5 e+ a+ V7 v; ?) j) O

( R7 ?% r, Z1 y: |编辑modsecurity.conf 文件,将SecRuleEngine设置为 on) }+ p3 z# q% E* u% ?

& \1 g* l& ^2 y: A+ ~5 |1 Y2 y, \owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。1 A# S0 A, \, Z
! Y) m0 w4 [4 G8 ^& U. g- T
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf1 V3 c) D" i! h1 k0 ?# P( M# ?' R5 a
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
( x# |, @% g/ N& r( ?( ZInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf$ H: p  b" F) c% O6 J
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
0 O1 W" L# ^/ F2 e$ M/ a+ BInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
1 ~6 u3 f) z6 VInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
* ^3 U5 @; V% v4 K4 y- c& kInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf& g4 G" m# g8 L4 A' s& n' y$ {. a
五.配置nginx
5 I; m3 o9 C( q1 r9 i7 }4 o: j$ ~2 w; a1 _6 u7 d
在需要启用modsecurity的主机的location下面加入下面两行即可:3 w! B/ ?3 M) u8 l# @& g
/ |" p) n* j5 N5 L8 y
ModSecurityEnabled on;  
0 N7 K, b- t4 V% s& f* R0 I1 nModSecurityConfig modsecurity.conf;
, K0 f6 Z: J: i* Z4 t# E( O$ @0 |下面是两个示例配置,php虚拟主机:8 j) i; i: D( q3 Q
# u; Q$ `4 i. G
server {% w4 z' j. V9 y% `
      listen      80;
) w  ~# S$ t+ w: r# T! t2 S7 O0 s      server_name 52os.net www.52os.net;
( v/ H% u' e6 l" {$ P+ E' `  j. y     
' O3 [% |  [. y6 [      location ~ \.php$ {4 [3 \" C  _2 ?7 Z- ^8 {3 F
      ModSecurityEnabled on;  
2 k, ~; ]( Z# G5 E0 X, C( [      ModSecurityConfig modsecurity.conf;, S- n$ }2 ~4 @
  u6 `! X9 _' m0 _1 _
      root /web/wordpress;' s. p- l  P" _, \$ s6 e
      index index.php index.html index.htm;
5 @0 V7 K. }; p  ( m: r% P' ^; |  F
      fastcgi_pass   127.0.0.1:9000;$ a2 h% H( h5 V
      fastcgi_index  index.php;
" E* D, U1 A( P" K- }      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
: F5 }6 b' ~! M% J- F, q; L0 O# f      include        fastcgi_params;
) c) a3 p  i2 J- Y5 x; A/ y5 _3 ^% q" X      }% @* ?& \% W2 c/ m* V- z# D
  }" c5 @8 l# a) G8 ^. o8 Q
upstream负载均衡:8 N/ i' U/ K3 B1 q! y
1 l8 ~" s/ j& m- }! V) E! \
upstream 52os.net {4 P8 ?& P% l7 O) h2 c; v
    server 192.168.1.100:8080;
# {6 h3 x; M4 [    server 192.168.1.101:8080 backup;
6 Q) ]! y+ Z. N. I2 j}
* J3 Q& j9 d$ {3 M5 H. }% v
* j) G3 B8 k' G6 k1 w) M9 {server {2 e6 _3 S6 [' |6 V
listen 80;
' q9 u' [5 X+ b9 x) zserver_name 52os.net www.52os.net;( J! X& Y5 v  a; p( u$ H5 l
, n! S" q' r5 q' ^+ Z
location / {. J  p3 e2 F4 _
    ModSecurityEnabled on;  ' _4 V# ^& w" A2 n
    ModSecurityConfig modsecurity.conf;  4 j& ]0 S" `3 Q, T  ~, l) M
" Y" ^1 e. K/ g' g
        proxy_pass http://online;
7 ^3 [" H, w6 c4 }6 a        proxy_redirect         off;
  W! o; x4 V7 ]) L5 r: l4 s* f        proxy_set_header Host $host;# l" X& N2 o5 F
        proxy_set_header X-Real-IP $remote_addr;
" e$ }0 G# V' y$ ?3 u        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
: w/ m4 C, h. l; s2 y! _. G1 O2 j/ D    }
7 M% d& m/ S% g/ z8 Z. g+ f5 Q; x}6 O3 S: o3 V! D! ]3 [" }4 T% i  y
六.测试
: X; d4 [6 y1 m! n
5 A* b( \8 ~' _我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:& z. y) M$ U& p3 M; [& _3 G0 \
7 w# A2 u* o# ]: i, p4 _
<?php
& ?1 j/ ^  J6 c3 J# v    phpinfo();    , n' H1 k/ o, S3 z6 C+ Z
?>9 w; U  C: v$ q7 x. P1 T5 j8 V$ }
在浏览器中访问:1 q; A7 V/ A8 t, ]) G* x7 G; B

+ l9 U  A5 b* Z2 m- ohttp://www.52os.net/phpinfo.php?id=1 正常显示。4 N$ p; @, _/ p! _0 f6 V  R0 r
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。& \% @  V/ Z) v) d- K3 ^8 T  M! }
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
0 N" E1 r$ F7 I8 ^+ H说明sql注入和xss已经被过滤了
; u$ A" ?! X6 k: t( O; D
+ `* Q2 d7 ~* B/ f9 B* R- A# y七、安装过程中排错: V& Y! Q! @( Z) I

3 \! G4 t" D* I8 E! Y) D1.缺少APXS会报错
: n& i/ A4 x8 D% J; }9 `! e3 d$ a. d0 r8 E3 [/ _
configure: looking for Apache module support via DSO through APXS
% o9 J' e  e9 p: d% |configure: error: couldn't find APXS2 j  [/ j& a" t
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。4 D' y6 \* g9 c# {
解决方法:
3 Q5 s/ x3 v% x1 l! ^8 ]
0 C& L) F$ s% c' Nyum install httpd-devel
& _; ^5 u. N  d) d( g2.没有pcre
( o- y8 I1 n; f/ Y; t7 U- v$ V9 }4 F. f
configure: *** pcre library not found.
6 Y. p. {( k6 [) Bconfigure: error: pcre library is required, g- I7 G0 G5 b: S$ P, v& i
解决方法:
+ y  i% \! n5 w  y& R5 r# m3 y( o( d4 g+ L6 _
yum install pcre pcre-devel
- P2 o2 P' y. Z3.没有libxml2
2 H' {3 P# l: n+ J; t1 Z8 O
/ Z7 C/ ^) n; r/ Z
; i" n9 ]- F' G9 k/ Mconfigure: *** xml library not found.
  T0 w* ], }+ i9 d. z/ h* V/ oconfigure: error: libxml2 is required
, `: `: k' `$ ?解决方法:
* C# b/ L1 s  m+ y4 z0 h+ ^, _
) I5 u" w  C2 Lyum install  libxml2 libxml2-devel2 k. }( N/ b4 n/ M
4.执行 /opt/tengine/sbin/nginx -m 时有警告$ y! ?. _$ J: x6 p& Q5 ]! X" J

4 \( I& x# y8 i0 l) TTengine version: Tengine/2.1.0 (nginx/1.6.2)
4 G) l$ [2 M' B: o: o. ?4 i( s5 C, Bnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
% b( J1 y7 k, D" `  O原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
. G0 A+ q. V0 t% U9 T9 s; p2 H# _
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured." [9 B" X+ N0 \3 c
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"1 \& ]1 Q* p; m
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
9 H. Y# d: M9 o0 L3 e' F% N1 ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"8 Q, I7 q) J% x; l$ c  V) A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"7 B, t! _' J% `1 |; D& d5 q4 C9 y$ J% T
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.8 ?1 C; ~$ _- g, k. \# A
解决方法,移除低版本的APR (1.3.9)
. G' d9 H+ u$ S- d3 n# s( Z# J; }
( l9 a. b9 i) H$ V8 O" s. \  y( @yum remove apr
' k8 n  z' I5 \* n) z9 c5 E5.Error.log中有: Audit log: Failed to lock global mutex
& v: R& E9 I2 m! \2 b
' E  x! i% W' _/ W3 [2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
* a: Q& L- n0 W( \global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
$ j' O( B& |: i4 b解决方法:
& }1 x- z5 l! W, d, e$ o/ F1 [编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:8 c, T0 n9 Y# f# I; I  W4 v" c$ y7 E& F
: F# D" ?/ z( }; {  T
SecAuditLogDirMode 0777
" _) ]4 o! d' x% o7 g# h' J9 ?SecAuditLogFileMode 0550/ p+ a, G5 [3 }' g2 ~- J3 B& s9 ]( T
SecAuditLogStorageDir /var/log/modsecurity
4 Y1 q( f  J) A8 `4 Q7 ZSecAuditLogType Concurrent
# ~: {: ~5 n5 O% y9 ~$ X参考文章:
4 i5 @( H' m7 s; o5 c7 Bhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
: C. i! Q) v3 O) Whttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-2 08:33 , Processed in 0.068510 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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