找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13026|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
" t' w; g# N1 q, o" W0 o6 N2 E/ P$ V# x: U& p6 C  I# _( y* C0 j
一.准备工作
$ @$ k8 O6 I; _: S% n2 P2 ?* `6 ~/ u6 K) [& B; W7 ~& Y7 S
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
4 e# d! M1 C0 u* }$ d; N7 b8 {" c- D3 B/ v0 V+ z) p6 B) [1 j6 L: ]
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
7 {! s7 |7 o7 |( p8 ?4 p0 @
% o6 h3 d4 E( S, Xmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ E1 S% f0 u6 L5 m0 y7 Z( r5 u/ x5 D
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
0 C) a* C9 Z. \0 p- ~( r" l+ r9 I% U# \8 U7 T. l% M. i5 D
依赖关系:9 o6 `) _& C% t1 u7 |
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
, e% H* x: h0 E9 b' O1 J) D3 W% J/ x' R& s
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel! ~# ~9 O9 ]' Z" y8 q3 I
modsecurty依赖的包:pcre httpd-devel libxml2 apr
/ |8 N6 t& o. j( r5 x2 `' E" W+ H8 L! `6 T, \1 C/ g' O
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel) K& j) w4 I% m- s' R+ P
二.启用standalone模块并编译8 T: w$ k, }3 C# L2 c; V6 v. l
, Y  E* J  C1 S: b0 s
下载modsecurity for nginx 解压,进入解压后目录执行:; @, o3 @5 b- e# M! y) J) e
- e! {* i, a* H4 _8 s+ Z, ^
./autogen.sh  w# J- @, Y. [# ~  ~0 B2 ?
./configure --enable-standalone-module --disable-mlogc
8 I; `0 A( j8 A6 m/ {make 3 G# {5 b! r! M  a
三.nginx添加modsecurity模块
" E8 I, \. L* `+ }8 j7 s9 ]; m
2 j# n2 f% Y* R+ ]- ?- J在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
# v" M) `* L. u/ G$ Z. E. D
3 y; L# j2 T. V0 n' z./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
2 K2 [$ v- j' a! Z+ qmake && make install
. M+ V7 S; C$ w1 c4 U9 r, z四.添加规则1 Y+ y; A0 {5 C' Y

, k" g+ n' j4 hmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
1 v; k3 W& u( M9 r# Z2 n
" n5 [# ^( p/ z) j4 i0 A( D1.下载OWASP规则:' v" j3 i+ @" n0 ]

: h* ]6 U6 f# S/ n9 W3 egit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
; s+ l7 q  Q& Y1 d. W
9 {$ Q" U# ?. l$ H! B& qmv owasp-modsecurity-crs /opt/tengine/conf/  {% g& O* H; u/ j) f

; x  x* c! N7 ]1 T) R. g2 [- Y0 gcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf9 {  R, V4 K' h  h
2.启用OWASP规则:
  P% |: A; K+ Q: Z+ @; n' b7 B' G1 Y: e! F6 m
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。9 V0 W: Q/ n; H$ {" w
$ J. B4 j) ~; E* E) o/ w# X3 e- x, l
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on7 @* o& t# v1 z3 O% x5 w
4 V# a2 l* M* W' d  B+ l# n
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
! ^& b" }- _- b" A
! U; o+ ?9 D7 Y# `2 j7 A+ IInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf5 a) _2 m( I6 c
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
7 Q' Z- D( \0 v  C, y5 t4 |* wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf  w! a$ D, x6 e, y- m  c9 S2 T
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf8 D9 X6 U: |$ f# T7 ~
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf7 e) s  f$ D7 ~4 F
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
- W  L+ X" S+ jInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
0 y6 [: L* h. v* ^& R8 i2 }五.配置nginx
% c% M. F8 _0 M$ g6 ^! o: M4 ?! o! ], {# I% E# L, u2 U
在需要启用modsecurity的主机的location下面加入下面两行即可:
* [( Z1 S# I9 E, G4 z9 J
% f, d9 p3 [* s! P4 q" U* ]* yModSecurityEnabled on;  
& }" u1 j7 q) M: GModSecurityConfig modsecurity.conf;( K. N% ]' \( S2 H
下面是两个示例配置,php虚拟主机:
& A; c1 j$ Q# I" \, O  O% C/ e3 O% q1 [* d( C& G- L
server {% U) B2 ?! [, Z/ |# V; E3 S8 U) ?% ~
      listen      80;/ |" C1 s, b8 b3 A
      server_name 52os.net www.52os.net;
" j# }7 L4 T6 f* s; E  B$ ~     
- B, Y: G9 W0 y' L3 h7 C4 J" P5 a      location ~ \.php$ {/ c, r7 n# \: ]6 h% i
      ModSecurityEnabled on;  2 d. h+ ~9 X$ W
      ModSecurityConfig modsecurity.conf;; d" e- ~. ~5 ^2 z! c

1 t; D( V5 _! E( a0 @& \) y% M      root /web/wordpress;
7 e* B6 _6 [0 G# \- m      index index.php index.html index.htm;
3 V2 H3 u$ w5 q; W, R0 I- a# P  2 Q8 n1 r& X% d3 j6 k: C
      fastcgi_pass   127.0.0.1:9000;
5 l0 H9 ^2 U* m* R9 \      fastcgi_index  index.php;5 U2 O$ T9 @! n% R- c7 u( p
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;8 U; Z' a' C+ p- H3 ~( D% z2 X3 l" q
      include        fastcgi_params;$ J7 T3 P8 ?7 @1 E5 K4 ^
      }8 Y" ~1 Z7 o- E3 w! T( ~
  }
) E4 _5 F$ l- b' R4 t4 e1 ]! aupstream负载均衡:  a' P) Y" m6 B6 a  j, ?+ N

: z6 b/ ~) A3 pupstream 52os.net {! i4 s/ z* z( O
    server 192.168.1.100:8080;
" L$ R$ }5 G0 S. M! a: h' {    server 192.168.1.101:8080 backup;
6 `* h0 p' {5 h/ ]8 K$ L0 G}- v  i. K7 u) v* _; B- z8 _
1 ?& P+ Q. F& g
server {5 H' C* F1 X- U4 \
listen 80;( h. Y7 \& ~6 l% s
server_name 52os.net www.52os.net;9 }& T( a$ Y" T: V. b
' k( e) g, t/ J* ]" F
location / {
6 W( \( O; F) b# H" c: F    ModSecurityEnabled on;  
( C# o# {3 G# ]4 m5 b  H    ModSecurityConfig modsecurity.conf;  6 C9 ]0 o9 K( G3 ?' _; U7 @2 J
" @) C' M& B" }8 E9 l, T4 o
        proxy_pass http://online;
3 h2 c  W7 q7 \% M        proxy_redirect         off;; C8 v) r9 T& h/ A; l* ]! Q
        proxy_set_header Host $host;5 q% b& e# f4 h8 ~* x' g4 W
        proxy_set_header X-Real-IP $remote_addr;# d- T$ t: v9 a: n* j, |
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;) ~# k7 \  V" U" M
    }
" x* `0 N2 P( ?/ R/ W) J}/ S+ o7 l) E5 `4 b1 u/ f% _8 s
六.测试
& A( e( ^4 q* b. c& ]) o
) h9 n0 J- K" @$ \8 k我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:' t! @: y+ J1 F& R+ W2 z  e! U

+ K) G& f: V9 C5 w# j& m<?php
: `7 o: ?6 E$ t1 V5 ~9 E( p2 Z& W. S    phpinfo();    4 _) t0 b" _2 }3 ~3 ^) N; ^# }
?>3 m- ?; [3 R; O1 l6 D! K/ \
在浏览器中访问:. t+ T2 P) `$ c% U
* l: L: {: @2 `7 L
http://www.52os.net/phpinfo.php?id=1 正常显示。
9 a$ l# v: d8 U# p! `- z9 D$ l& `http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
$ Y* L: U! M- ~" Rhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。, `# v4 ~' p. |6 ?; f
说明sql注入和xss已经被过滤了
! |( h) o+ ^1 v8 h6 ~7 a, d! E- E7 E: F( a! I+ R, F" M
七、安装过程中排错7 y' G  S: J0 _* D
  ~0 C: c) s9 {7 n; h
1.缺少APXS会报错# G) U' O, z5 h/ V& I1 V

9 Q' B) [) j! a/ J! {6 gconfigure: looking for Apache module support via DSO through APXS% T/ y/ _/ H1 a0 J
configure: error: couldn't find APXS( s  V+ V; ^5 c& k/ S/ k6 R4 H
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。' ]' f+ b' j9 `& H) ^: m& W
解决方法:/ N5 l# q2 J4 t. |

0 F; G. T5 H) C7 G: g6 Nyum install httpd-devel
! }$ S9 B( I# o9 ]# H2.没有pcre3 ?3 q3 o8 ?% f6 a# o
$ ?' I9 J! r4 |/ P" N$ F
configure: *** pcre library not found.3 ~% L9 B7 t: _' T
configure: error: pcre library is required9 n4 \9 O/ \- \" K; s3 o
解决方法:! R; M, ~* L7 o( i! d: S( s/ s$ C
7 @- L6 p0 a% y' w
yum install pcre pcre-devel4 ?& n) Y* s, Y" u/ j
3.没有libxml2; x6 U0 u, T* a1 j0 r8 K

) q* }. x2 g" D/ q/ \  ?1 J( t6 m% F5 n+ T) D
configure: *** xml library not found.
8 K' `. J4 @2 {: i, @+ uconfigure: error: libxml2 is required
9 h! H4 P5 c% `" n4 P% `解决方法:
7 X. K! K! {* q/ [% N/ M
/ [" @3 Z' C& G7 T3 z0 H5 {  u5 S1 Jyum install  libxml2 libxml2-devel
7 V) `$ _" Y7 t3 p5 F4.执行 /opt/tengine/sbin/nginx -m 时有警告5 a5 Q) [3 L. u. c% r

# j( V; ?/ B* L% o. h* GTengine version: Tengine/2.1.0 (nginx/1.6.2)- O0 \6 [3 `9 Y. m3 r, ]% m. }4 A
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!. @! y! t1 P6 t; e
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log1 x9 X2 X- E6 F# Z
1 |9 w( j9 ?* O7 Y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured." X% B0 m& N/ T2 g1 Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"( N; k. c4 d& g0 m2 ^2 ^
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!* `* _% f3 w7 w) D  h2 ]4 H
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
3 u1 ?9 b+ T! t& S% X8 ]6 q- N2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6". Q+ Z8 B. Y' U
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
0 y* B3 Y8 I: O1 [3 F6 Q" j3 M% P解决方法,移除低版本的APR (1.3.9)% j0 f9 N( N% M2 M
% `2 y! U' M  H% a6 t
yum remove apr0 Q& e8 f2 j* f' O+ X# D# ~
5.Error.log中有: Audit log: Failed to lock global mutex
: [1 z. Q! ^3 S+ n% _7 F2 q* i. Q0 S& G4 u; p
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     8 ]9 N; Z- O) p0 n
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
9 M6 n8 J; R6 p) U7 N7 Z2 j4 J1 [* e% x解决方法:* f% m3 a* Q; |1 Y& y
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
) {: k& k- o( o, A8 \; z
: m' V6 X. b0 z; Q/ w& e0 ?SecAuditLogDirMode 0777
3 W; {, M4 {( T7 r+ b: PSecAuditLogFileMode 05502 a  Q8 `! a7 M: B+ z) L
SecAuditLogStorageDir /var/log/modsecurity
  ^. e6 r' N7 LSecAuditLogType Concurrent! `4 k2 w( n  V* j7 m0 u: C
参考文章:4 a7 V7 X* Q, x. u" \
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
* t$ @; g; [) P6 Whttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-5 00:56 , Processed in 0.066328 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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