找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12592|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。. C$ f$ M' I; E. N5 ~! k4 y

& T1 F. Q6 o* v" Y" B一.准备工作. e" ]# Q3 V% y* E1 y7 l0 ?( O% [& @
4 ?- o$ s, }& H! U1 t
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.05 R1 x. I+ S2 y' Q8 m. j+ X

. y" X4 k: v) B9 Jtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
+ V; K7 @0 O" y8 T  s4 g) M0 A- g- z% A& s' j) v
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz4 P- j/ @+ Y% A! f- p7 O9 C

3 c: r. Q- t7 r6 \OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
) v4 q; B# P; E# T4 x* h: o7 j
' `& }4 }4 g! ~7 U* I) q* g依赖关系:* L0 f; O) U8 N4 H6 M  }
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:" t, ~: M7 v# ?9 g

' E, [, ^7 d' k( q4 Myum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
! n* n/ l* K+ n* v5 |: Cmodsecurty依赖的包:pcre httpd-devel libxml2 apr' Q* L* F- p2 Q5 p. R2 j
4 c! w7 s7 G  w5 ~" k
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
" k. o) P5 M/ \8 E二.启用standalone模块并编译9 F( Q" G8 K3 j: q
' |! a/ c6 s0 Z( b
下载modsecurity for nginx 解压,进入解压后目录执行:( Y4 w/ W2 c& C8 ^, L# p: _% _

' C0 _( v; p! e# L./autogen.sh
$ q' X* y3 Q; Q) ~./configure --enable-standalone-module --disable-mlogc4 A1 i$ Z8 M( P" |
make + R& }1 s: |% A/ t/ Z
三.nginx添加modsecurity模块) K% N- B4 u$ u6 k0 Z7 [

: i+ {2 |! B" u# I% l+ C3 Q* s6 H% d, E在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
- c3 H! |4 y, q4 [4 k
# ?4 t7 O9 ]* P% A$ f% V./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine: y. z6 o, ^; v( l) u: X
make && make install0 \& u9 k8 T" U8 Y! V1 {% A
四.添加规则8 I$ v) o8 p. }/ ?/ X: I7 P* [, p

5 y5 G& a9 K/ R6 fmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
9 c4 {5 \! M$ g+ Q! c
/ E+ O$ \) n5 ~2 ]1.下载OWASP规则:6 O. o* p. H8 S

& |& r8 E3 x- m. q; {1 d( @git clone https://github.com/SpiderLabs/owasp-modsecurity-crs  V! w, u& z, U$ L2 s
+ H9 E9 S4 s9 k* g/ d, |
mv owasp-modsecurity-crs /opt/tengine/conf/
5 X) B1 B* x; L2 m% |8 y5 m3 i5 r$ M/ n/ V' Y
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf3 S" B# W$ {% S+ f6 [5 r9 o  ?
2.启用OWASP规则:
9 |2 I( {3 S6 K; t8 |" h
" ], [3 W4 a8 K! }复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
5 ?& Y! b! x) w+ M8 R8 E3 S
& M0 \9 A; D$ m- z: i; ^编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
9 I- g* R* x; z. r* ?  F1 z
2 t6 t3 @/ M& J, c3 v6 Xowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。8 i% g4 g; T& ?7 b3 J' _- u+ u3 W

, d* K( j1 e. vInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
% t4 ?1 f4 B6 J  e2 s! fInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
9 p8 a) k' v0 n/ t6 nInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf9 m: J  D8 M5 }! x, ?; y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
& M8 Y8 z8 l0 E3 E! P% _$ k2 K4 u/ TInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
$ U. e4 Y" u, V( ?4 ], kInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf" C; p  f6 s+ u) _
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
3 A' ?& i5 S* h" G( r% g五.配置nginx
) x# K9 d) {2 F$ U+ L
  Q8 W. z) e+ p9 J. Y# @在需要启用modsecurity的主机的location下面加入下面两行即可:
  x5 H: {2 y0 @* i" i
9 o2 a/ u+ r3 |3 F, [- z1 UModSecurityEnabled on;  
- X3 B3 H% r0 g) ?8 [% ^- @9 CModSecurityConfig modsecurity.conf;
& @; M1 m0 k, ^+ J# a* y下面是两个示例配置,php虚拟主机:& b" [+ {1 r9 X: P7 u7 @- a+ s. t
% r9 q$ e7 F. T7 v' B
server {
9 w+ {/ ^- t5 P3 h8 w      listen      80;
) m% Q) h3 H/ p* G, d! C% s1 h* f/ c      server_name 52os.net www.52os.net;
$ |- i6 X* v. V* Q     + B% F  ?# d( f- f$ }6 H' N
      location ~ \.php$ {
1 Z8 o$ [( V8 Z; B2 L. O/ ]8 N+ |      ModSecurityEnabled on;  
2 E- ?6 \$ U) t      ModSecurityConfig modsecurity.conf;
% r5 O, D) U, i  z/ f  u6 o/ y, j8 B
& @5 q8 J. |* \: L: g6 m3 U      root /web/wordpress;* A& d9 o4 t4 A
      index index.php index.html index.htm;
' k6 S  g7 n; R3 u- c0 Y6 z  
0 D' ]& l& R. Q) D      fastcgi_pass   127.0.0.1:9000;
: l2 Q5 {+ X; c7 C/ [9 |) F* K$ u      fastcgi_index  index.php;# Q) H7 m0 B2 n6 P2 o4 t
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
* [/ o0 ?. K1 Z- L5 R' q; A      include        fastcgi_params;& ]) }8 R; [1 O
      }1 v& y/ g# {- Q! |2 u% X5 S6 C
  }
; o& F+ x% k( r( B8 L+ k: T" I2 Qupstream负载均衡:
" i+ y4 V2 P: B7 A8 q3 F# `% C: F8 q1 i  Y9 p, c
upstream 52os.net {
/ Z0 K4 H2 U6 G- n, `6 m7 g    server 192.168.1.100:8080;
* z' F8 y& _) ]& J. c& N# |    server 192.168.1.101:8080 backup;
7 x3 y# i: b% @# F: a  X- N8 w}( R* e  p! I6 E$ u1 d
9 d2 R! |8 e; \6 \
server {
' J8 E& U4 ]) w' _+ alisten 80;
+ @  l/ p$ k7 @+ H$ O. userver_name 52os.net www.52os.net;
! ]9 M& |9 q0 e4 M8 R7 |( b% q8 C: r& x$ t
location / {* c4 L) B8 W3 U; G
    ModSecurityEnabled on;  : k# x! ]+ d' U% g: U
    ModSecurityConfig modsecurity.conf;  8 B) L. Z* E5 V

4 D: q4 e/ f% i3 \& {! f        proxy_pass http://online;1 n) p- n1 X4 u4 U2 f) w: R
        proxy_redirect         off;- f  u2 q% M- F& L3 G
        proxy_set_header Host $host;
8 t& ?/ @* m; E" R3 D, C9 L, n        proxy_set_header X-Real-IP $remote_addr;
' r/ }7 E, t( a1 G        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
/ ~6 Q% B! [! O& d+ M/ _  H    }! `: }6 z2 H) x% `  U3 R' B- z& H1 w
}
2 `% ~# E: P1 C0 \- F0 q- f六.测试
6 d4 _% F0 f; U0 Y
$ |6 W8 U  p, w7 v' K我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:; z8 F0 X9 X  m: P

" B5 a+ j$ \# E<?php; b. l8 z( O# g  B! ^) c
    phpinfo();   
4 p( f  `* g: p( p, H?>
- A- n4 C+ V: [: n$ b在浏览器中访问:* B9 ^: F7 Q- h6 b+ P  T' d

" F* z6 y; Y/ h) }http://www.52os.net/phpinfo.php?id=1 正常显示。) Q  K0 Y# N* t5 i$ F9 `% u7 o# f
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
( n3 @3 c- ~2 Yhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
8 b4 j# m- T. A' M# g说明sql注入和xss已经被过滤了
) p9 H& L8 J" m3 y4 w1 C3 w, l* E; I; b) s& C% Q5 A0 B
七、安装过程中排错4 w$ p3 ?1 _7 s  ^0 C1 }- q6 ^# i

) `' s% G+ @% K/ \9 Z' G+ _) c) Q) I- N1.缺少APXS会报错
" r* V3 p; \0 s5 }
8 |2 f, ?3 G5 pconfigure: looking for Apache module support via DSO through APXS9 l# V5 x' ~4 }, C# Y1 x
configure: error: couldn't find APXS, s% I5 l$ c- K# r5 R; K
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。, B; o! i: F: L; @9 \* o7 `
解决方法:
0 k5 B& q1 I9 T3 r2 T. d) ~& j9 T3 e& q* n2 c! i4 I
yum install httpd-devel
, X& a. O3 r" F. Q' B. v/ N& h) `2.没有pcre
. J  ~' v. w* Y# m8 @  L* t& e$ h+ s
configure: *** pcre library not found.
. A! C$ P) n( g- e3 Oconfigure: error: pcre library is required/ m2 u/ z! \& c1 [+ X
解决方法:( x$ @3 T$ }, q- k1 @

$ k* s& n8 e/ q3 Syum install pcre pcre-devel
0 \5 ^8 o" g4 a: u- A3.没有libxml2
. y6 k( c9 y& S3 z) ?# @# U( ~( e/ u  v7 [7 g9 c( r
& K* M1 c3 `* ]. |+ `) I9 f
configure: *** xml library not found.
& Z+ X7 k! T' f, h+ Xconfigure: error: libxml2 is required
3 l4 R6 L. J+ z3 k" J. q解决方法:& N  F6 n! u% R7 W( s8 R

$ |. |1 L- h7 R; Xyum install  libxml2 libxml2-devel0 y9 }# O. d' ?: I# [, A' o
4.执行 /opt/tengine/sbin/nginx -m 时有警告. o  _4 v# ?% ~. h7 ~

" E5 Z, a- w' Y' I& |- ]Tengine version: Tengine/2.1.0 (nginx/1.6.2)
! N9 u$ v8 q) f+ K: ynginx: [warn] ModSecurity: Loaded APR do not match with compiled!
, \9 T2 k7 G0 L8 ]原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log  y' T: E7 t8 w2 F0 E
. O. a& S) V( p$ B( w8 j
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
1 y- J$ \3 y( C0 r2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
) L. }. U# n! O- _8 A2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
" v) ~. K5 B' W3 n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05": ~* b* o! N; j  v1 w3 E
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"$ {8 h3 c3 `: a- U4 U8 u( _: J+ l
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
) D" q9 `" V4 ?0 n解决方法,移除低版本的APR (1.3.9)0 c9 i9 N4 i; W5 a- b

8 D7 Z# n+ P" U* l  j2 }# G; D3 fyum remove apr
9 b. T6 Y' y7 A5.Error.log中有: Audit log: Failed to lock global mutex( n& G, m$ N( h" B

# L! ?  F! u) Z0 g5 |7 ^  |0 c2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     % k# B( R/ |& A9 r5 k! k
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
. M5 z8 d% d: Y7 p% s( H4 y' ]解决方法:# z; D" {" ~$ b, ~. w& [% V
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:1 T! S' e! H! {5 g) R( S: _- b

* O! G0 _, H! }/ |* ]! u2 d& xSecAuditLogDirMode 0777" y! _$ l2 Y  w5 z7 @
SecAuditLogFileMode 0550
1 B. c; x; y3 |% F+ W# [SecAuditLogStorageDir /var/log/modsecurity
! X- T. G* [- h8 A/ @+ |* S9 b% C, zSecAuditLogType Concurrent
- p5 `$ R+ W# w& D. e# {7 g参考文章:
* U, v5 d- s0 Nhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
2 |3 K$ N" r2 i2 c1 A+ W4 Uhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-20 08:56 , Processed in 0.041616 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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