找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13086|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。5 D* C- E+ x9 s+ ?- Q- A/ [* R4 ^( I
; N( ?4 D6 ?2 M  U% Q& L- G3 X
一.准备工作1 V! @* J# X$ c3 Z" `  d
4 A2 ?0 U3 {' U, ^) R- k
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.08 j9 X% k3 p6 w: M
7 J2 Z9 Q  I  B9 M) _" A5 j
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
( k) \3 s  p, `; M( M, q2 q: O
, @% r0 O7 N9 P6 b" `6 }# Tmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz, f9 r4 m" o. E; K+ w
5 b1 }9 u1 V# Y; r, b
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs" x4 ~) k# G) p
+ c( T7 |3 m4 n5 ?
依赖关系:
7 [+ E2 q3 L! ~tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
. W3 d* y' O' I2 ~% n9 o. x$ _9 `  I. G, ^) n0 ?+ M/ U) [' u
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel$ ]1 P" K/ j( z# m( B
modsecurty依赖的包:pcre httpd-devel libxml2 apr" H  `; S! b! f0 y8 Z

6 q7 q$ u; ^) ?  {5 C4 I2 Yyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
$ ?. ?" x8 }- [二.启用standalone模块并编译
* Q% z* w. F* \! [! y% X/ A- {& K( C, g+ E% J5 ~
下载modsecurity for nginx 解压,进入解压后目录执行:
6 R- Z4 W8 K. A% b* w. m0 _. P# `
2 L/ ?7 `3 K* L' j, r* h1 n' G./autogen.sh# U7 b4 s5 K9 H/ Q0 V; x; v5 A. G
./configure --enable-standalone-module --disable-mlogc
/ ~/ }6 j, j2 j. Vmake
" K0 [& L( r" q* m* p3 J4 |0 V8 d三.nginx添加modsecurity模块
. T5 O( Z$ _) ]$ e+ h
) X9 o, d8 D8 d6 J1 g) a) R. o9 S5 G在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
0 {$ m7 k3 q- ]
7 S- J8 `# O3 t  l! r. ~; }$ U./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine( i  q7 x/ j/ n( W* B+ M
make && make install3 X0 ?4 O; P8 ^1 a
四.添加规则
8 A/ S" L( A: v6 G/ l0 m2 P0 O9 ~: {9 X0 }2 _9 ~
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
# T8 T( a* e! K2 K5 a1 {! t0 _& G  t2 y; T" D6 `6 k$ {. i6 T( T
1.下载OWASP规则:
% O' C' l; f5 W2 w+ n, D0 [2 J
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs$ i: T; c& d" T

4 X; D0 t! d2 h& B5 M3 f9 H/ g" ~mv owasp-modsecurity-crs /opt/tengine/conf/
5 A+ V: W7 f2 N7 c5 X3 z! ^7 l
: z0 V8 O7 d9 l, x# U0 xcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf4 U. m0 H: f& n- k; C& E
2.启用OWASP规则:
+ ~4 {, m5 B. B2 I7 b( |7 z9 g) R6 y2 w7 ?4 F- Z& |& S
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
$ X/ x( B& H  q) i5 \2 K" s( B9 M- [  |& K
8 ^% X/ P" L8 w# e9 p2 r编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
/ l5 u( c/ {* T, o0 p, }" R+ ?6 S
$ ~" W) [$ j. g7 ?5 h; v# q1 Oowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
3 T4 P2 ?! ?# [2 C5 R
9 {  C+ W- B3 o$ {, U/ `Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf  ]3 I1 k$ I) c# L
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf5 \$ |. J. w- |" I$ j7 A
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
6 s0 T) `4 n( n- p& qInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf* {  i! g( V# Y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf* a8 L$ K) ^* D0 K9 w
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf+ A" F3 y: u- }; i
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
$ U( L  W( w: m2 ?2 ?3 I% k5 `五.配置nginx5 b! @  J4 K( U/ W3 _, R9 _9 s

& ?# n# ]5 d6 p) x. \7 l在需要启用modsecurity的主机的location下面加入下面两行即可:
1 X; S3 z+ j( w5 O- m/ z# q: `6 X. g( A9 n4 ~$ G( e3 x
ModSecurityEnabled on;  
  y3 m2 g* Z+ J0 DModSecurityConfig modsecurity.conf;
; r2 s1 q1 w, L5 h下面是两个示例配置,php虚拟主机:; X+ ^5 C' d7 g, v: \

  M, t% j$ u0 jserver {
' X( J: P. [0 G6 g      listen      80;
& i8 v# k( A1 X% C7 w2 C      server_name 52os.net www.52os.net;* x" q7 K; v" G
     
5 {% P# p- n4 Z      location ~ \.php$ {
* X: y) J4 E- Q  F( e' S9 S" l      ModSecurityEnabled on;    q, c# N# B" r8 q
      ModSecurityConfig modsecurity.conf;- m6 @& I+ ?, [6 g

0 d3 ?$ q! P0 }      root /web/wordpress;
, C0 H3 T5 y0 r) A      index index.php index.html index.htm;
$ O3 Q7 J* ]/ i# J/ V  
) l6 _) u0 M7 d  f' q& `      fastcgi_pass   127.0.0.1:9000;
; ?+ N) E0 c% W5 p3 X) a      fastcgi_index  index.php;
% D! @6 P; D) x9 k7 D8 {& r      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;8 {7 r$ A! I; k, b
      include        fastcgi_params;+ G0 `0 A! Q, i# x8 D
      }
7 ]  n2 g  {/ ~, A$ O# G  }1 I! z/ w8 X: y, q) ?
upstream负载均衡:
& s! t1 T$ d2 R) a0 e, i9 t$ W$ Y: E$ k
upstream 52os.net {; m' \8 l/ l& `1 t# ]4 T8 q
    server 192.168.1.100:8080;, C  [! D' p0 f# I' ^
    server 192.168.1.101:8080 backup;: f6 u) L# T  c7 i# b! x$ j7 H* H
}
2 H# E( N! g; k$ `4 o# O( o' N
) s( ^+ P8 {& Lserver {- O& [' ~9 I( d- u; J) o
listen 80;$ i0 c$ f: R% z+ R5 _; ~) ~7 @* I6 w
server_name 52os.net www.52os.net;/ H. K. J4 n6 c* H

, O: H8 T8 }  `, y5 C4 Wlocation / {
* j9 Z) t- I( {9 b) [8 h$ T    ModSecurityEnabled on;  / B% s* {4 G/ ~2 o- ?, j) c
    ModSecurityConfig modsecurity.conf;  * h3 ^; U) q6 B; l% \
: {6 `6 }9 D5 ~; V
        proxy_pass http://online;
" T9 M/ R& r& ^4 T        proxy_redirect         off;' o, i; `0 u4 Y8 x5 n0 f8 R
        proxy_set_header Host $host;& S! o: g' H! [8 N
        proxy_set_header X-Real-IP $remote_addr;) v: F! K) F+ w; }6 p
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;' E- U3 d& E; v+ j8 }
    }. i/ N& D2 |9 U
}
/ k6 W# H8 ?6 f  Y, O- d" `# Z& u4 A六.测试
: C! Q" a3 U3 t% ?8 o) ~' Z
8 f  o  ], W# F6 G3 c2 L我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
5 b+ P4 q' P1 k+ \  x% J4 i" V1 a. I' Y4 [) m# I
<?php
, t1 C, z2 ^8 m* h; f2 `0 l    phpinfo();    0 f) F& l1 V4 C+ ~# d
?>4 g5 ]% N' [6 r1 A( B* m
在浏览器中访问:" T8 P  f+ F) |! I2 y; y* ^. ?

7 z) H& d7 q3 {http://www.52os.net/phpinfo.php?id=1 正常显示。' k  m/ O: Y9 b1 Z
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
" z3 a4 g6 P1 D- d. Yhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。1 W: |3 r# R6 [1 I7 y  ~9 }  d
说明sql注入和xss已经被过滤了* g# d4 G0 z0 R( i( P
3 p. `5 C* C  W! f1 u" u. C' u1 K
七、安装过程中排错
; N0 ^/ }' y  Z% q3 [  D- }; T+ j& o& J, z1 d" W
1.缺少APXS会报错
5 L  k$ o" y/ n# z& j3 A- \( K8 F
& e3 C3 ^8 [8 J, f0 Q' Cconfigure: looking for Apache module support via DSO through APXS' C0 y. T" C5 X: c( F
configure: error: couldn't find APXS8 `6 K0 j! R+ l& t4 ?3 i; Z
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。5 f* R. B# ?* x6 |- a" V) g; e
解决方法:
) Z" H: j4 R" m  v$ m- Q  I# A# z, h" V2 R; u0 y
yum install httpd-devel* _1 X0 H! f3 d5 m2 ?
2.没有pcre& _' m" ]" B8 I  F; Z
2 w/ D% T6 S. K7 W5 Y
configure: *** pcre library not found.
6 c! i: G4 _  E' o) Jconfigure: error: pcre library is required: h% g4 t% j' @8 f+ J' @1 i
解决方法:
' n% j- O/ ^, A5 a- k
0 ^& n8 E3 [% r. `+ E/ Zyum install pcre pcre-devel% C$ u2 `. O, z6 h
3.没有libxml2
* B, _+ u6 `' P  C: F& Z* }( z3 m" G8 _6 j; V! H
8 Y' Y$ g9 V8 d. F2 f/ |
configure: *** xml library not found.
0 a& r) X; @* w' n& R/ Aconfigure: error: libxml2 is required
8 @7 z* x- [* |2 k( z$ V解决方法:% E3 D# F# l# |4 q5 i4 C: P$ U0 H
4 ^; h5 o, R+ U1 M( Y% v1 [
yum install  libxml2 libxml2-devel
1 \' f9 V: }9 K" k' k6 X$ H3 ?4 }5 e4.执行 /opt/tengine/sbin/nginx -m 时有警告
" ?' G8 n2 v+ ~: j, Z) |8 A
% h% j+ X9 z* W" hTengine version: Tengine/2.1.0 (nginx/1.6.2)1 t; i& w7 n$ a) F* J
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!7 M7 e$ i) T* }( L4 |: m
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
/ ^' X) y* K- t5 G4 o  U  |+ c5 _* F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
0 {7 W5 j5 p0 @8 y1 L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"4 E+ [/ ~8 O+ y) ?/ J
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!+ a% s5 [" J" y! s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
  [6 W& D1 ?& m2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
0 ?0 O5 N% g2 n# n; k2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.: R) P; R" ~" L% R4 i3 r9 R1 K' a9 k
解决方法,移除低版本的APR (1.3.9)
0 V; B& Q3 A1 E( N' t- Y, Z2 H- w7 c) v9 K" q  d  U
yum remove apr
3 o& u- l$ Y8 Q- g6 G3 |8 V* c" J5.Error.log中有: Audit log: Failed to lock global mutex. _6 \7 R- E; l# U) o9 ]6 q

) o( N! f. a; H5 \1 ^" M5 O# Q2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
, y, i, ~- N* i! ^, C  {global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]8 N% A* D8 h( w
解决方法:
# n; [- j, i  ?7 N! i编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
0 f* {' E0 ?9 l- A: O* l. Z8 }- Z# f
SecAuditLogDirMode 0777/ \3 A8 P2 `0 @3 ^- R7 v) g
SecAuditLogFileMode 0550
' Q9 X# W' W) I: ], RSecAuditLogStorageDir /var/log/modsecurity( q- x: X, ]9 G& u2 ]
SecAuditLogType Concurrent
+ b6 G- W2 y% m3 F$ U+ H参考文章:
+ E3 |. X3 t, h4 ?4 p  ~4 H5 z9 Khttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
! S& W) ^/ I/ [7 a9 O+ O4 chttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-12 19:05 , Processed in 0.072053 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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