找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10284|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
& v* I5 t0 G5 `) k  q
7 ]. t8 _# B- s  M( n一.准备工作9 s3 Q6 R: Q* A, ]" L; j

8 Q1 ^( u6 r9 P系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.08 R5 @# Q' {9 J5 E
: G9 p: B. w* p3 |6 v# J* D, E# X
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz% h! N4 d3 o; J- t: R: S1 v9 N9 a

9 x, @% b% B# J% D. nmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
3 H: \6 R( m. `2 x% O. e* j8 G1 O* z' _4 x/ _' Y
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs" R* N) b8 c2 V3 s. q

- v0 z- C( w! M2 t9 k# a9 F依赖关系:
6 a# q# n( y2 A# y: \0 Ntengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
$ ^0 j4 C/ {: e# ?- }
4 x& I, ?# [, w9 V. z" pyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel! ?7 V) L* X* i4 ?: X; m: X
modsecurty依赖的包:pcre httpd-devel libxml2 apr
0 K& z0 }8 _0 P( Y; k
" v) s% n' {5 Z6 Z4 yyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel( u' b/ E" F* X; Z" d
二.启用standalone模块并编译
! U4 g. T5 R7 R% |) g4 E2 o2 b- q- g, z  ^! m: t8 J- B
下载modsecurity for nginx 解压,进入解压后目录执行:+ l6 c4 z' K/ K5 k- P6 I

9 L; T' c5 o  {2 D, j- N./autogen.sh
2 B9 ~. z. k* c./configure --enable-standalone-module --disable-mlogc
8 b& E% t- O9 B% Smake
/ n' M2 [) x& W8 i& J* k3 [, e5 r三.nginx添加modsecurity模块! `  [- z' Y" d2 I8 E

3 ?1 p8 @; `# c2 }$ \在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:2 ?+ l( q, H( O) g& Q
' o& d5 f3 T: I! G7 c( F
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine% X/ `" O; c/ A& E5 N
make && make install
5 s  {1 m  H& {* E/ i四.添加规则
" p) H7 r4 c/ U8 S$ v8 @4 E. }9 ?7 Z1 r7 }; W- @$ C; j
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
( t1 Y4 n5 P6 r4 `. M3 L3 j7 D3 w* G) _6 J  I
1.下载OWASP规则:
: `6 r9 j" `1 X% `" @
% V9 x# W/ H; I8 g2 \  E: ugit clone https://github.com/SpiderLabs/owasp-modsecurity-crs: D* N& N! y! \: w( [$ u( ~

+ L' [% t  O1 w4 s; U( K9 Y7 Vmv owasp-modsecurity-crs /opt/tengine/conf/- s+ ^2 x6 o) f' h' a, g
+ [/ e8 T. E- \( H
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf7 a/ p' C* V4 A" Y5 z
2.启用OWASP规则:
- z/ }, w' E4 ?  l5 W/ R
' I) P2 o7 c3 v0 L& A! v复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。! y& {, _+ V# i9 l: U

6 n* @7 p% }% P  I+ h- C% R  I' z编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
6 b3 T( U# `6 r2 U6 c1 H
/ {0 u; w! P4 }3 bowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。3 y" i5 k* z. W; H) t' B) V
) p- o2 W) e3 w% r
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf8 |0 i  H5 H6 i' _( s
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf7 H4 m( `% z" \  I  g
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf5 a  p% p1 k, J
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf" a' ]# s9 N. q4 E0 Z5 u
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
5 Y+ {) g$ k' P" E6 _Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
  T) b, q7 \  G% y8 CInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf6 x4 K- z4 Y8 h3 h! f& `3 Y% c
五.配置nginx
( H% l- l0 s/ l6 R1 `& x7 W
+ @: E- n* h5 a# d" q在需要启用modsecurity的主机的location下面加入下面两行即可:+ g4 v  K/ E4 m% e
2 l+ d( b$ W5 c/ i" p) C) |
ModSecurityEnabled on;  & T" w( h8 |5 J. t$ d  o5 s
ModSecurityConfig modsecurity.conf;2 h+ K" ~0 f/ A( v$ i! w
下面是两个示例配置,php虚拟主机:6 F- A' h1 V- V2 O
0 ^: x3 E% ^$ M* Y/ T, \- ?4 q* v
server {
' T+ n9 F6 k: P+ P/ U- A0 [      listen      80;$ b5 u& l0 f+ c5 I
      server_name 52os.net www.52os.net;2 b5 C/ E! U. }
     ( N5 ]5 ]. z! D+ b+ e1 T5 y  T6 Y. e( ^
      location ~ \.php$ {4 h$ [! v7 `$ n0 D+ n$ o
      ModSecurityEnabled on;  1 v/ ?3 r) y7 O, u( L
      ModSecurityConfig modsecurity.conf;9 h0 y/ g9 B  c" {
7 A' E, }% k9 e  @8 U
      root /web/wordpress;: l( v0 f7 ]6 g7 T5 K- u
      index index.php index.html index.htm;& M5 a: O# h. Q  Q
  + G0 L7 S. V. f* i
      fastcgi_pass   127.0.0.1:9000;
8 b" W8 G# H8 X, y' l      fastcgi_index  index.php;
! O( ]. W# u9 s2 C) i      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
* c$ O; r6 {% v) e7 _7 o# N% x- X      include        fastcgi_params;. P* T! \6 P4 m4 z* }# N
      }. i" A8 I  X' o$ ~
  }
/ f, ~. n9 f: N$ T7 `7 p  D7 Y+ tupstream负载均衡:; S. `- n5 @9 W" y% ~* y  Q
0 w) r* i, [% ?- m  L$ t- c
upstream 52os.net {; n; F2 Q$ p: ~3 T9 d) S# C) I. `9 ?
    server 192.168.1.100:8080;- a/ e' Z  F7 p+ r) I8 U* C/ Y. ~
    server 192.168.1.101:8080 backup;8 _# X) f5 M/ a
}
! N) a, B! a- M  ~) k" d6 w. [; O/ r( k% F; c3 t: B+ ^
server {$ d! G' I/ g; p- Y* u
listen 80;' _; b$ b6 ~+ i
server_name 52os.net www.52os.net;
7 f# s. O1 v8 ^+ p" W- \: Q- {. Y9 G" j0 ]8 k% [, X  |
location / {: P( M, V" c3 a7 M( ?
    ModSecurityEnabled on;  
( R' T% ?+ F9 L8 g: J5 z& x    ModSecurityConfig modsecurity.conf;  
% o0 |2 c. n6 S. c) D& T# x: v3 ]& u" s( s. M% h! N2 w! ~: t3 \
        proxy_pass http://online;
0 S8 A2 P8 V2 N4 x" n: `' g        proxy_redirect         off;+ M' z0 k4 ?5 c% V) n# u( s
        proxy_set_header Host $host;
" X% U* E* F' f5 j# |$ I        proxy_set_header X-Real-IP $remote_addr;5 G9 P2 O% \; Z3 A7 O
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;; p% U* [" R$ |- m  T, M
    }
$ B1 ]0 V- r+ }: w}9 Y* R7 V0 n1 ^# U
六.测试, x7 L' B* u9 Y

# g( A# b; K& P) c, T' y我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:& A/ a% }$ g0 E2 k; \
# Y: ]/ _$ s4 A0 {2 x( x- N
<?php0 V, q% `! z' N" [; ]) s
    phpinfo();    8 r$ D" s4 O# z: {! K7 @! v
?>
9 O, C+ p. S' n; [. A在浏览器中访问:$ X7 m/ `% Y* w6 O+ {, k

: ]" ]6 B; h/ k4 dhttp://www.52os.net/phpinfo.php?id=1 正常显示。$ ]* I# @4 l. r1 ~% i! z& o
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。) B/ v" x4 v5 ?* F: }
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。  S8 y$ q1 B2 g0 f
说明sql注入和xss已经被过滤了
0 d' f: |9 S. a2 k) `# `2 r" i9 z
, W2 E- c' c, V0 x七、安装过程中排错
% L2 r9 s$ H- z4 Z& V' ]5 c) I' n: M/ T# D" Q4 O( n/ @
1.缺少APXS会报错
' Q8 F+ p& S$ a" t3 i1 _' s* ?  L' v4 h
configure: looking for Apache module support via DSO through APXS4 p: ^( O& ?3 L. [: D
configure: error: couldn't find APXS6 E) w) }% a9 x5 p9 s3 g
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
) F- w+ F* G; L# r解决方法:
  H& p8 S8 `  `% c8 s; h# W+ Z$ ?& Y$ }
yum install httpd-devel1 K! a- z2 Y5 t% W
2.没有pcre
/ M! ]. w% |, \3 J2 c/ }- Z$ G. U9 y+ F$ b; n$ O
configure: *** pcre library not found.
) _+ H2 P# p; \1 P, ~# [/ j" N1 Iconfigure: error: pcre library is required$ n9 ?' U, U- G3 H% H
解决方法:1 N$ W+ K& I; r" ]9 Z8 B+ b

* {# k% t, C1 O  E" ?% P$ x0 K# r5 Jyum install pcre pcre-devel0 k5 L& K; O& T7 [6 y5 s
3.没有libxml2* r( B5 U* Y  M* \& }+ r

  K5 h' V/ `$ F, R; D9 q* w  s- `6 y
  g& M8 o% A6 c  @7 t5 h# ~) Y0 `* Sconfigure: *** xml library not found.3 z# W# a6 ^% M# X
configure: error: libxml2 is required; }7 U" ]" l) @) I. {/ T, Y
解决方法:- O" d" b7 J$ \; [

) N  e5 f( W' Jyum install  libxml2 libxml2-devel; k4 a  K. P" `$ E
4.执行 /opt/tengine/sbin/nginx -m 时有警告
. _: ]+ K7 Y3 `4 E& ?0 t; C! _; K  s1 `2 f+ i( Y
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
1 Z9 |& K2 m: N# h4 c, Z1 gnginx: [warn] ModSecurity: Loaded APR do not match with compiled!: m4 c6 @6 S) l4 S5 l
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log3 Q2 n2 a5 w3 r) d& @: G  {0 b
2 ^9 n  `! r4 E* e/ @  F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
: B3 r; K1 o. q2 N* P1 w" ]& S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
1 x% _  K( B+ a4 e" ?) D0 B2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!' w; N  B8 A* G% A: n$ H
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
5 {+ Z; B) H* x4 ?2 ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
- d( P4 R; C) E. Q9 E( }2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.9 g& ]& Y, A7 I$ u* K
解决方法,移除低版本的APR (1.3.9)1 w5 M; K( ]0 U: M+ J" g

/ N0 y" F* G3 K' F6 Fyum remove apr
" c# i4 s3 M. z. x- N5 r% {5 g5.Error.log中有: Audit log: Failed to lock global mutex2 V* Z6 S3 ?; w) X
3 Q7 u3 a0 G5 [! @: E
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     $ \- H; a; f- N: ]  B
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]# c/ |* f  l* j% s7 X2 [; h: D
解决方法:
7 R- }; F' g# `) J5 [编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:$ J: U0 u, z2 m/ H; F2 \- |
4 ?" l3 F* }; r2 K! c: e
SecAuditLogDirMode 0777$ `0 S7 ]/ E: |
SecAuditLogFileMode 0550, @6 I3 `+ D% O4 t* F& M0 y* X
SecAuditLogStorageDir /var/log/modsecurity
+ n8 h9 s  H4 e% wSecAuditLogType Concurrent- Z9 @+ j  ?) Z: S% W
参考文章:* H( T' F, r" L! X! g
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX5 Y* y+ U, T! V% m
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-3-19 08:00 , Processed in 0.105889 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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