找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12086|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
) n0 x3 o- Z. l& T. W3 r9 @* _) T# ?' n& F) d0 z$ n
一.准备工作
9 a/ Z' D. L% \$ ~$ N
& T8 D! S+ o& L5 H  v' f$ t  d4 T系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
' Y) M6 M( @8 E, H2 l" n# q* S' J( P* H  g8 u% }7 t0 W
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
/ A. e9 q% P5 O* @
7 C6 q( T; T( @9 Hmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz) e3 e4 m, u5 C" D' k& b

) ]+ h& [7 k9 S4 aOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
' P% o7 s& Z1 ^2 I! ~' C$ L
0 j, H% G% N1 t4 v: `5 m: x5 C依赖关系:* L6 ?& {) z3 ?2 h. ~5 F
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:3 x5 E9 k) F  U. m6 U; Q* e* k
: P# I1 B, O8 Z% g
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
; a" M2 J; o9 k# o% W. i  c9 Zmodsecurty依赖的包:pcre httpd-devel libxml2 apr
6 @# Y0 w( ?/ g- ?3 J( d) a8 j5 ]$ U  A
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel6 l" T" z: v, ]; H, w" n
二.启用standalone模块并编译7 y5 v% e6 k# ^/ J( w
# k: f1 I8 Y) u' U
下载modsecurity for nginx 解压,进入解压后目录执行:. w& |# c! c( F8 a

# d  v3 D7 r/ m  U/ ]) O9 Z./autogen.sh( O8 {. n1 S) ^, |- v1 N
./configure --enable-standalone-module --disable-mlogc
, c/ w5 Z+ d: Y% Hmake
+ ~! L. T( K! z# {- T三.nginx添加modsecurity模块
, C) i7 R7 L9 Q& b+ S/ n: h6 D, S* i
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:, N. d% I# s: \8 \8 r5 }4 Q
- z7 b% l1 x# O
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine& n/ Q. q% u. ]( A( G
make && make install* m  |) C/ h- k* q& S
四.添加规则
* E: z% F% V% N* j4 P& t0 K6 _* |; d8 q9 f
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
$ k8 q! q$ u: y) D7 ^' q$ y$ e% y9 ^, x* P" s" P" A1 J$ T
1.下载OWASP规则:! T& t  X5 ]1 x7 r- @
: `  @4 z- o- B% g  K" Y
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs7 f! W3 s! ~) D5 V$ R" I

5 ]9 B. B; w: P3 ]5 jmv owasp-modsecurity-crs /opt/tengine/conf/3 O! j; Y( L1 _# N3 u
+ S8 C/ M$ k( \$ }$ Y6 f$ N  ?$ e. D
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf9 l3 [5 N9 d' C& l2 F6 e  X. Z
2.启用OWASP规则:. }, k  g; l, A
; _* F% {, u: g( m' Q
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
% l0 @+ X* P" p9 e6 F( Z8 u, V5 S1 u4 s+ y/ q  B3 C
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
4 x7 L/ L3 c2 A1 N) I
! @( y5 \( E6 z( ^6 i5 kowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。5 t- D4 z# `3 [+ L% E- j7 B

. o" C6 c/ Z5 FInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
* v8 R7 }1 E% e  kInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
( ?. `8 `) z  j9 f/ F# FInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
3 d! X6 Z& }  NInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf5 Y8 K6 F7 ^3 U' ~9 {4 I+ w
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf) P! E1 E. \9 S( @; {9 X
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
- k( f0 s9 P6 v3 FInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf9 n9 r9 Z$ w: G* W  z4 S! |. w
五.配置nginx) s$ H9 K& H' I' B
" N2 s# Q, |7 S
在需要启用modsecurity的主机的location下面加入下面两行即可:5 t! `& k0 ?. k4 T" Q% w" [; t

3 R2 ^: W- [9 W; f3 d2 TModSecurityEnabled on;  
/ Z& c- B' ]( {3 \ModSecurityConfig modsecurity.conf;
3 A: E; i4 l# D# ^2 s8 b下面是两个示例配置,php虚拟主机:
! [6 r  ^. m) q  [4 D$ `. w6 J$ D, r, s; L* Y1 O
server {
+ B7 ~0 k. {1 v9 U! s6 j$ B+ a      listen      80;
1 {* d8 k- c3 @: d. O+ d: |# Z( z      server_name 52os.net www.52os.net;
9 k  }  P) @) z* B3 {- L' _, P6 \( j     
+ g) m6 F! {1 S  U      location ~ \.php$ {
/ h+ S7 S. q$ t      ModSecurityEnabled on;  . N$ @( ?3 j0 r/ v
      ModSecurityConfig modsecurity.conf;
  P! @4 O% y7 I# \/ w6 O& g, J, t7 ?4 p5 d
      root /web/wordpress;# J& m8 J4 Z7 d; ]7 t+ a* r
      index index.php index.html index.htm;
9 H$ ~. B3 Z2 _) V% s( P  ) v0 r9 c9 ~  }+ O, [2 n4 j
      fastcgi_pass   127.0.0.1:9000;- Z) _+ B" g0 g# ^, H) b) x
      fastcgi_index  index.php;
0 Y& S# B' o( R8 B% X8 Y      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
0 B8 e4 ^% v9 q' r9 \% J: _2 P      include        fastcgi_params;
  e. H2 ^+ y/ P; M      }' R8 k: y. I3 }8 I2 W7 _3 r  n
  }5 n7 b# k8 H3 @. ^2 h& @* i
upstream负载均衡:; u' `  T+ E, s$ \" Z0 I# E* ~
) m' ~! B# H* N9 S
upstream 52os.net {- p! P7 n- X; c* s/ a- g
    server 192.168.1.100:8080;1 v1 {) ~5 _% l6 S6 ?, @
    server 192.168.1.101:8080 backup;( D' X; w( W/ w8 H0 {' @3 [
}3 w& m/ l# ^7 Y# N# d" A) O
7 U# c2 h1 n, R& ]
server {
0 E2 v8 B* c, j4 E) E& u+ zlisten 80;
0 c8 v6 W8 F* Q* \/ n$ ?% p' sserver_name 52os.net www.52os.net;1 A" Z) m4 w5 c  l1 U7 _! B2 Y

# D& w3 v5 w$ `( K" }# E- Llocation / {7 a* R3 Y7 Z2 n5 _. T% Q8 _
    ModSecurityEnabled on;  
. S8 I/ y# Q7 n    ModSecurityConfig modsecurity.conf;  
1 r  K7 e2 Z0 f/ k# J0 a) f! W: `# G  n3 ?7 }8 u
        proxy_pass http://online;4 Y0 N; v& m+ H/ h3 x
        proxy_redirect         off;
/ A3 s* y7 N- E) ]        proxy_set_header Host $host;3 V% w( e6 e7 x- F4 X
        proxy_set_header X-Real-IP $remote_addr;7 I3 W; H1 u4 H( E5 _- Q
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
5 t- p1 E& t* c- K. Z+ e2 K    }2 _* E3 N% a) O* y0 b" B4 y5 U6 e
}8 |$ D1 k1 O. E# l& W4 X" ]8 ?
六.测试
0 M. L% x+ c7 S% J7 d; n! ^/ J
7 ?" D4 D0 _7 y: L4 l我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:6 U; b6 n3 t6 `6 B% Z' G1 O

) V/ B% C/ M8 {* A6 }% C<?php
! v, V1 O& d3 O" [  e8 ^2 e    phpinfo();   
' g/ j; `: q/ U! [9 y* j?>
. p2 l% h. V$ \5 I! T, v7 m在浏览器中访问:
( I' \/ {( k: l$ }4 v9 }8 k' I( b! O! f& |" p/ [+ e# Q7 C
http://www.52os.net/phpinfo.php?id=1 正常显示。0 I8 A3 D( B/ o, B2 {5 u' v) Z5 C
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。* Z: G2 w( G2 P9 G) c+ x
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。7 {* Z9 Q! ~7 Y4 B( \" s) a( l
说明sql注入和xss已经被过滤了1 e: x6 y8 W% o4 Q) c* v
$ H2 V) ]" `7 `, a4 ]( u! e+ ^
七、安装过程中排错
& m, @) L: J0 I  H6 J' x( F- A- J, N/ p
1.缺少APXS会报错
0 h9 [# r: y0 D8 o
- r) j$ M* J; J, C+ o' iconfigure: looking for Apache module support via DSO through APXS" G6 i2 R( [% m2 T+ i# [
configure: error: couldn't find APXS
. I5 L3 K$ c" ~/ x  |apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
. x7 w+ R! Y* i  S" D6 Z解决方法:
; ]- Z5 h* g( E( Z' J( j7 m8 ~6 G, ?$ V/ y- U
yum install httpd-devel4 y6 ~3 I- d2 X7 F7 d) v
2.没有pcre7 r* w* `, ?4 o' v" }1 b, ?

% r; U$ ~& x  I( S; i% j6 d1 K/ z5 bconfigure: *** pcre library not found.. [2 D1 }$ E9 [0 K9 Z
configure: error: pcre library is required! x6 c" L+ N' _  T2 J: p7 R3 Y
解决方法:
5 n, K1 C2 N5 r1 Y
" P7 ~$ B! O2 lyum install pcre pcre-devel
6 H2 P8 h" d) J3 }3 z5 X3.没有libxml27 n& u& P' n% _) f- i! r3 Z7 ?
$ R+ v( o1 u, g
& b  H- \0 @5 ^8 R# h; b+ g0 ]% l( C
configure: *** xml library not found.0 L! S: f% D0 b
configure: error: libxml2 is required8 T& A' b: L4 P- {$ j
解决方法:+ R- f( e! v+ Q0 R; {
; b% U8 e! F6 J4 l
yum install  libxml2 libxml2-devel0 t" E, V, E4 x0 e4 w0 W
4.执行 /opt/tengine/sbin/nginx -m 时有警告/ l+ j' J' X. p9 X& M: E7 l
( c; z2 `, y, f* S" \2 z
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
+ e$ a# l# N! ?& Y7 r$ |nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
5 V$ D& V6 D1 r+ O原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
, B9 g% S9 L- p! z) E/ l6 ?7 r% v) G+ Y( F" g7 Y# p
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.3 _/ Q* z3 Q  Z. P, L1 P& Z  A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"6 d+ H% V. p8 g2 ]8 X
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!8 h8 k/ Q* Q; z' k+ s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"0 `/ r% @0 o- {+ w
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"8 _# |% y/ h9 S4 \& }8 x* e
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.4 Z3 g) I; _" V5 _
解决方法,移除低版本的APR (1.3.9)
/ e9 E. S4 u5 R6 w* a, h+ m& \2 x1 \( T( P& k# P! U* v
yum remove apr% T. B% K& ]. l
5.Error.log中有: Audit log: Failed to lock global mutex4 H/ t+ F  A% f& d

' Z6 W: i+ k9 A2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     , s" o$ E# I1 k% Q
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
' E3 [9 Y/ _6 D解决方法:) |5 c! j" [- ~3 F
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
0 b" v0 Z) M3 r5 ^" w! R& b! i! E
SecAuditLogDirMode 0777
7 |% B- Q5 l4 Y0 K" R! xSecAuditLogFileMode 0550* K9 `3 R! H) u1 a
SecAuditLogStorageDir /var/log/modsecurity6 H0 w- ^! J) f* _
SecAuditLogType Concurrent
1 s4 m7 g" E: w/ S( H参考文章:
2 H! ~6 z! Z( j9 U" i5 Ahttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX# \2 e! ^7 R7 \# {
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-9 06:34 , Processed in 0.069555 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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