找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9845|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
: `) R5 g9 K- I/ K" t9 G
/ x, x8 R7 I( X6 w" o" C一.准备工作4 \! k3 q3 O& J' a* M

: ^6 K- N$ a9 ?2 j/ A/ f系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
, L0 ^' a; _4 C' `0 K' k3 S1 [0 r, v5 I) ]" ?& C
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
! |, I7 i+ i7 U9 Z' f/ O
/ |  B( }% z# s5 [2 @2 @modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz9 F5 A0 ?' f  d2 p) u$ l3 B
) R0 A6 S  y9 }* h: Y( D
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs  g/ R" h- j" G6 ~% ]0 I/ n
' B1 q2 @/ N/ v9 b
依赖关系:
7 I" T# r+ \3 V7 s3 g) Etengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
4 G$ y4 F5 Q6 S. D: D5 a8 O) n, y
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
- U% G: ?% I7 H4 X' ?& Y1 \. `7 Y- \modsecurty依赖的包:pcre httpd-devel libxml2 apr7 c3 }3 m! W1 Q- u

# h6 ?/ O, V6 S2 zyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel1 d6 |# d" B/ g- _9 r( k3 ^
二.启用standalone模块并编译  g/ f6 p2 j# w

/ U- k  Z/ L' f+ Y( ]0 C; S, D下载modsecurity for nginx 解压,进入解压后目录执行:+ D9 i1 r8 `0 ^3 _' E2 t

7 a) S. g0 |5 d8 l& ?3 G./autogen.sh
7 w* n% j$ Q5 |& z./configure --enable-standalone-module --disable-mlogc
& C$ \  f- J/ E+ Jmake
3 k( R( f) P% H  i5 \, p" R& n5 R三.nginx添加modsecurity模块8 X: G7 D. N4 \& Y$ b
' Q4 ~8 T8 Q, X5 A& {3 t
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
/ S5 Z! V: F: z7 Y% @2 ?* k- p
8 h, B% e! v& j, R9 E- w4 |./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine" {$ n3 r6 t- K& O5 s- L: O+ k% x
make && make install0 ?- ?. c9 G5 A5 ?
四.添加规则3 G  V+ V# S# W+ Q$ T8 [

8 _; `% e3 C' J% N' C- ~$ c  Q' jmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。) A0 m, Y# r% n3 y

1 W- j7 \! d% i! s, _/ o1.下载OWASP规则:  }+ C' O( Y1 n% E

* b# H5 k2 w) ]4 n# J- t; ygit clone https://github.com/SpiderLabs/owasp-modsecurity-crs/ b/ ^; `. E8 C2 d; X

" m' g( _9 i( s; m. J3 cmv owasp-modsecurity-crs /opt/tengine/conf/
. }$ c8 k! M. g5 l4 \+ s* T
: ^4 J0 [9 j' s1 S/ t/ @cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf8 {; z4 c; j1 y0 k, t: Q
2.启用OWASP规则:" Y+ k- a( t" `& ^7 m* t& I+ J
( ]- L% q. F2 V4 F
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。+ N* D  \0 C1 K6 i! h/ w

! o0 I2 m  E3 g编辑modsecurity.conf 文件,将SecRuleEngine设置为 on# X1 o" H( h5 n0 }

+ u/ Q, K: B/ @owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。; d9 N" n" s- r! i

& v' A! _2 |1 O. k$ ^Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf: L: K7 G% o8 E
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf3 V4 ~/ t/ W0 `0 W# O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf( n+ n! C6 V% O3 ^+ {( U
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf7 Z" X8 W. x/ s; c; e5 C
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf8 E  u" {& g; ?' D0 K* A( Q
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf! _+ o9 x# J# d9 n- m+ G2 f  i0 q
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf# R  a# L$ y- Y" f
五.配置nginx
) v+ D: O3 W0 f* A# l% a" ^& A1 v+ r
在需要启用modsecurity的主机的location下面加入下面两行即可:; I, Q7 F$ G' u) _$ g5 Y/ @

7 }! u- K0 L. MModSecurityEnabled on;  : G% }  E1 c$ l4 U( \4 Z! I
ModSecurityConfig modsecurity.conf;) y- D; Q: x3 y9 U
下面是两个示例配置,php虚拟主机:: z( B# Q" c/ X( z4 K+ `. H

6 H! f7 ?% k% M, Rserver {4 Z3 R9 r0 N9 d  v; T& u/ M( a
      listen      80;
8 {9 P" ^' x9 w& B* g' ]; B      server_name 52os.net www.52os.net;8 u1 x+ L/ a' h% F* R: _
     
1 s6 o1 ^& T0 E      location ~ \.php$ {. v' g( [2 L" W: S- M
      ModSecurityEnabled on;  : K1 c( ~4 [) |# Y0 e9 F" r5 H* ~$ K
      ModSecurityConfig modsecurity.conf;9 y) Q4 G( {$ I( u
8 o. c9 }: \5 f% O5 {/ `5 o
      root /web/wordpress;
7 E$ y' p- P- f4 ?6 Z/ f9 P4 c      index index.php index.html index.htm;& ~8 Q1 m4 H2 D) j% C! W+ {
  
$ b8 ?- q, z( ^* M9 r      fastcgi_pass   127.0.0.1:9000;
3 ]2 |( p* l4 R% {4 i/ E9 e1 j3 \      fastcgi_index  index.php;' d# w* E2 _) m$ _
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;7 \  V! m" C( J  o: W0 i
      include        fastcgi_params;
! s6 {0 |8 J9 d* R7 u      }
3 e3 ~2 A5 i1 S  }
, O# z4 |' h, h4 M# eupstream负载均衡:
2 ^, N1 ]' C4 [# M& i1 m3 Y2 A( [( X2 S& E: ^6 x  {
upstream 52os.net {
9 T3 w. F; |" n' B7 r% P+ c    server 192.168.1.100:8080;# B, w8 @0 M+ T. d/ S4 M" t9 N) B7 B
    server 192.168.1.101:8080 backup;9 y0 x/ {  O+ G
}0 ~! _( u3 f2 F% G  V1 c0 y
8 Z) `3 {: I: _9 ]6 M' @5 t" N7 {9 N
server {
9 e2 @) h- _$ olisten 80;* c  d9 k% Z/ a; H+ @" b
server_name 52os.net www.52os.net;3 R  [- V  y# H$ m1 g6 d. ~1 z
9 ~+ w& i; ~; o. I5 L' Z
location / {* J1 q$ U5 k4 c8 }; W( I
    ModSecurityEnabled on;  % {! x1 G3 a5 n4 |# T* |: \: F
    ModSecurityConfig modsecurity.conf;  / c2 D. r- p" y+ |* W* k& M' {# n

% ?; \. @6 f9 |        proxy_pass http://online;
$ [. ]2 ^3 G  B0 \6 K+ t8 K        proxy_redirect         off;  H% n) X' L6 ~
        proxy_set_header Host $host;- G8 Q& g7 C( O, o: _, Q+ n
        proxy_set_header X-Real-IP $remote_addr;
) i- E. O9 a# n: T6 h        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
; J1 K0 f9 P' s8 p4 I# O* U2 L    }2 S% D3 n  ^6 q& u) ^7 I$ A4 i
}# i6 j+ `0 Z- N4 i3 T' w( Y
六.测试: _( @7 [5 J+ ?1 Q9 J4 ~" }

  F7 I, V5 P3 n& _" p& }+ k我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:2 U- w) W6 B8 o# V) H
7 R/ T5 @- H1 I2 r* {; z8 V
<?php. B5 x2 P/ W! @/ y4 K# ^6 P
    phpinfo();   
9 Q6 ]2 A1 z& E" @?>8 t$ i# }3 b: I; @
在浏览器中访问:$ e/ t# x8 I* p) k" q

; m1 R# e/ l0 ~http://www.52os.net/phpinfo.php?id=1 正常显示。
0 k0 w0 W% U& J& F. whttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。: `; b0 @, F- K1 M$ G2 I1 r6 E, P/ h4 S, |
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
  j+ o. R( v& \* ]0 u* R; u: ^说明sql注入和xss已经被过滤了
5 @/ C2 [5 m3 g2 ^6 I$ u$ v" _  V% J2 R7 K8 K
七、安装过程中排错
. a' u9 `- }. p1 X' u" I
% t9 z4 j; \' ]$ _0 A' E1.缺少APXS会报错$ e; B- F, G9 I: I; G! N7 F
# D# o4 N4 F$ y. z4 l% [
configure: looking for Apache module support via DSO through APXS
& W% [; ^; }6 G7 ]# X9 w7 }configure: error: couldn't find APXS
6 V+ Z) w" p2 uapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
( g1 f+ X# m* e" t7 g0 E解决方法:
; C$ d# J# o) B( j' \( {# I) I; ~  k8 |  G0 f' O4 W2 c$ L: i. Z
yum install httpd-devel' E" {7 N) y7 P4 ~
2.没有pcre+ M  K+ t3 Z$ y# N

& P3 q7 R& Q2 K& N/ K5 qconfigure: *** pcre library not found.% G) _% H! v! f& j
configure: error: pcre library is required
3 c" x, T+ n& z8 N& ^' d解决方法:5 R' E+ F- `- T1 k/ i
$ w" `1 l# m  R  A
yum install pcre pcre-devel
. ?: s" H- [% \6 G- a  q; K9 ?8 q3.没有libxml29 E$ d" O& ^5 ]! O

: h% c5 o/ [" T* N( J7 m  \0 C' c( @# F+ T- _% N+ e
configure: *** xml library not found.3 i$ @+ Y& l* g" q1 h1 T+ k# t# c
configure: error: libxml2 is required& g! c  X5 r5 O8 ^; p
解决方法:! p1 U7 e7 Q  ^! G# @- C- `& q

$ W/ [! |# g$ W% p* N( D. dyum install  libxml2 libxml2-devel
7 ]! L% k. N7 x' ?4.执行 /opt/tengine/sbin/nginx -m 时有警告& I7 w; n; f* R4 W* s7 ?# M
$ R6 D( S. s8 t' ]
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
$ e2 J' n( Z: vnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
" A4 v8 i% C- n  W3 _) K8 q; k原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
6 Y  e8 M8 w+ L7 \. s9 y! v" J+ K' f
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
/ q2 h. C4 v2 A, V3 y- u. ]2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"/ L! M( D) U0 N' k% V# W
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!, g- e: l  g" T* ^+ x0 P
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
0 i3 o+ W0 |7 c8 f, a! Y5 ~2 E2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
# C! d! @9 w9 x5 R+ e# Q3 ]2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
8 S% R1 `% |+ q: Q- t- }解决方法,移除低版本的APR (1.3.9)
4 y0 j9 Z; G$ G6 e; _3 y9 C9 @5 @: z, A* W
yum remove apr
/ p7 T) F. W' f4 e5.Error.log中有: Audit log: Failed to lock global mutex
0 E: K& `* X1 I' l) [
& }' I( @- _7 G# W4 w3 x: S2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     6 z! R; h# M) Q# m, ~/ Y
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
- }% o# Y% v" @' V解决方法:
+ i- g8 _9 m3 s( m- |# k) a" x" n4 w编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
6 _6 g0 K( F- _9 L# D2 a8 H( n' J) {
SecAuditLogDirMode 0777
: F7 J/ W; B7 }- S1 o& xSecAuditLogFileMode 0550
! A/ l- q; z, i7 \3 b  SSecAuditLogStorageDir /var/log/modsecurity' ~# s9 `/ @; Q% `( Y
SecAuditLogType Concurrent
, @% q; {' |- R% F% \5 R参考文章:6 N- ]5 s: A- T& a2 E
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
, t4 h: H# Z4 ]http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-2-6 16:12 , Processed in 0.063183 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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