找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13240|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
: _. f- ]0 i* q8 Z3 m& w& i" S4 [7 ?8 k, g
一.准备工作; L! `! }6 e5 X8 N+ s3 h" V) M7 C( n

" m! O" J4 d7 C系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0+ d3 s% A" U4 q5 M* ~- D; G
; k0 A, b3 C# f3 Q
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz' ]* s3 t7 Z& [
. z' r" L% f/ z5 H) i. s8 Q
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz2 Z, ~4 _9 d: i7 G: Q

" ]$ n/ g9 ^: G! t  W* ?. p# DOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs  s* r4 P/ H  ]7 g7 `' }- l( }" q

# f3 X: l. L+ Z& X% w依赖关系:2 z$ @+ m+ d5 k; L& c, t
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:+ f3 |4 @, o' X. \+ _
/ }# i  E+ A; L9 U
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel7 w' W. w& s2 ^' V6 v2 v% z; A$ N
modsecurty依赖的包:pcre httpd-devel libxml2 apr
  Z9 ~5 x9 O2 B1 f: r9 A# H" d: S5 T4 u
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel7 Q$ g9 _6 ^8 c5 F# E
二.启用standalone模块并编译, B3 ^" ]. }# O: F9 [
, _; E" |: N6 {: ]6 \: \* \
下载modsecurity for nginx 解压,进入解压后目录执行:
5 q9 J5 _, k; J- h( g8 g0 E- r8 _: n/ j$ I) M! A4 e
./autogen.sh' B0 S4 N; B, o& y0 w
./configure --enable-standalone-module --disable-mlogc
( F- ~' M/ \# U) `make   c$ {" m- j. A6 }  g
三.nginx添加modsecurity模块
# U3 K  `  j4 N' k
. m- H9 t; P. a$ z9 Q7 d$ O  H在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
7 X. J5 a4 T! r. j- G6 |
0 F" X4 N' N0 m./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
  t4 H+ [# O3 imake && make install
7 z1 O2 [( w  @9 n9 Q. u四.添加规则* X  `. J' N1 l+ B2 v
* C# \0 Y* z: p
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
. x) U6 U/ s# {, j. |
' ~5 Y1 ]* D: R+ B( L! w1.下载OWASP规则:
' H- n  ^. u0 @5 _, Q: _- f9 u1 \  m! ~+ z3 F
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
! {# \3 y% y9 t1 g6 u& I: F4 E. P" H7 Y0 N5 y. a2 E$ J
mv owasp-modsecurity-crs /opt/tengine/conf/
+ a5 m( g- h! y9 ]" \
; O* I# [1 d% H, k# Bcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
' a2 i1 k6 |9 v  ~/ c0 }2.启用OWASP规则:
" F4 g  }  h; X6 `2 N4 l# I0 [$ r! j+ Q$ a! R6 g' T0 G. `
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
& i* J$ P  N( S" c5 y# ^) p& [
8 r1 F9 x/ n# q3 n" W7 X) D! ?# f4 _编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
2 s# N/ g! X% b% Y9 j
2 k+ Y1 c, t, U3 i5 E: |# X; jowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
  U) v: G0 w$ d& k2 e
- y) q$ w: R+ X$ k( N' `) l# oInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
. V+ m6 @& U) g- p) A, \5 wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
$ J# C* d! i% A. qInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf- _& d' ]& A4 t- {$ b- P! O% ~
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf7 P2 G$ _# w# J- m. `8 j4 S6 ?
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf! T: p/ r; f& W. N9 x/ c
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf+ [5 G) M3 o- x% x5 A! J) d& L6 ]
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
0 e9 }2 b  T2 O( z  j, I五.配置nginx
1 l% j0 S  Q" x
( H( _, u3 t6 U! `/ p& _在需要启用modsecurity的主机的location下面加入下面两行即可:
# y0 Q& p: k2 O4 @" N
4 k- a. k  S/ C2 i, y' X, Y1 u3 QModSecurityEnabled on;  5 Z9 x2 o0 @2 y/ W3 b; z7 x
ModSecurityConfig modsecurity.conf;
. h( h1 |: q- W1 Q5 O; Z; M下面是两个示例配置,php虚拟主机:
9 R& S9 n0 g) {+ ~7 E6 D' C% e8 |/ d2 h/ ~: x, @1 y
server {
; _! r; \$ X- M7 |) R      listen      80;
( T4 ?3 [: L3 I5 P$ T$ c      server_name 52os.net www.52os.net;
( K' e# N+ r' c% n5 C7 _% q$ R: g     * H- D1 t) N9 p7 F" A
      location ~ \.php$ {5 b  c+ J2 |9 F* I
      ModSecurityEnabled on;  ) f( X4 O8 J( ]) [) E
      ModSecurityConfig modsecurity.conf;
. p6 v* z: D. ]! q( q) u: ?. P  g' F; @( S: i: X8 V
      root /web/wordpress;
& _# V' e" k7 I5 I6 V. }      index index.php index.html index.htm;* |3 ?6 I  K1 [2 W& v4 s
  
$ O6 u. C9 G- d      fastcgi_pass   127.0.0.1:9000;6 A& Y$ T$ K1 T: {/ ~; d
      fastcgi_index  index.php;
2 ]! y+ A, _5 H- Q8 t      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
" R2 J9 i7 {9 q* h2 c. H7 x, N      include        fastcgi_params;4 c3 N' e8 [3 u9 k
      }. S6 h& ~+ z% c! ?8 \6 E
  }
( \/ O6 t' H3 z. o1 q* oupstream负载均衡:6 e1 S# {& u2 ]6 ?, f

% I- _. j1 q4 U  D4 kupstream 52os.net {
4 n* }5 X  K9 G4 D0 H7 Q; T7 P9 [1 e    server 192.168.1.100:8080;, o- x' A$ }! \" r' p5 Y- Q( f7 ?/ y
    server 192.168.1.101:8080 backup;% U' F; @' H/ W( P2 B# d8 H2 x
}% s- @5 I) E0 |2 h3 `9 X

  u3 v* B- r, _7 J8 A) B4 xserver {) X% g5 @- G+ I2 I' i; f
listen 80;
# f& n: N1 n- n3 N- B6 \. W3 i  Iserver_name 52os.net www.52os.net;& A+ m2 v( L  B' n) t7 C

; G( ]$ U# z) S8 Plocation / {) J" E1 h* s) N' A7 z" k
    ModSecurityEnabled on;  
4 B* r: P2 m; O1 A    ModSecurityConfig modsecurity.conf;  & L8 X4 L6 o& J/ s% I9 k" `$ z

5 p) U- J' m& K, t/ N4 @; g        proxy_pass http://online;
5 j5 o( T" W2 n# M+ [        proxy_redirect         off;3 _( X0 v1 g2 c# p9 t6 g
        proxy_set_header Host $host;6 _1 {2 Q% w& Z" v: b" c5 K
        proxy_set_header X-Real-IP $remote_addr;1 I+ ~! x2 I6 @* P+ d( A
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;/ @3 S5 T* x# V' }- W  I
    }
9 {6 M! h+ A4 h; O2 \+ G}+ J" q% R" t' M7 ]. O9 V
六.测试
6 c- D- n3 e$ A: v5 D- B8 l) j9 _  W+ H
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
( X# G: L% u# B3 {7 l: p1 E. {2 H, [- q
<?php- _8 Y5 C3 v6 I5 N
    phpinfo();   
" u+ J# c# x3 l  Q+ a7 K; {" T?>+ t3 i8 A: w" q2 t- S3 m* \; P
在浏览器中访问:
" {6 s& k. S0 v1 |
* h& ?& f6 B% i( V2 W& zhttp://www.52os.net/phpinfo.php?id=1 正常显示。
1 B1 T8 _2 G& j7 @http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。- x% o5 n6 r7 W6 O# x. ~5 Y% e
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。. X: X6 N) n% Q" H0 v8 U" A8 T" p' ?
说明sql注入和xss已经被过滤了
. F' l9 S7 n$ g4 L* L, H  E' ?( H. m* w) s: E$ [
七、安装过程中排错' p3 O/ X* z9 Y! z; m1 Q
. ]0 r1 K% L! m
1.缺少APXS会报错% L4 f$ [9 k) D9 i+ z

: s+ P- m; \7 E0 S0 n/ |configure: looking for Apache module support via DSO through APXS" U, Y1 }( F5 H
configure: error: couldn't find APXS
7 {+ n0 `. h+ ]  Y- i; n8 l) Yapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
+ n( u- [+ ^9 q解决方法:
' D& ^8 h% y$ T2 {- f/ T  O. t
( z- \* `0 N# \/ ~  {3 Myum install httpd-devel
( G' S5 j  `+ c7 k. V9 m2.没有pcre
; n) A$ p8 a) R
9 N* ?5 Y. r. ^$ e8 |- n& w$ M- v1 Hconfigure: *** pcre library not found.
& w* I8 f+ P* u" @! Rconfigure: error: pcre library is required
; [, X- q( I. K4 M2 W; D) Q$ u1 t& {解决方法:4 P3 D2 N, Q# u5 s  F8 ^

* j; L: N# q  H8 |: e$ [6 C9 Myum install pcre pcre-devel
+ u3 f4 y2 B$ l0 k3.没有libxml25 e& L1 ?. m) C7 A* d  \/ ~- t

; Q6 K+ {6 `+ n! }! R: }4 N! K) j' {/ D% a) e
configure: *** xml library not found.
+ @$ c0 _" `) P  `configure: error: libxml2 is required9 @% c; f9 \6 D0 I9 Q% F: Q
解决方法:) E, y, n  _# C* b/ o) g& J! v

3 I8 z& q* h% t, ~9 p8 dyum install  libxml2 libxml2-devel
! @; i: x/ o! m$ \4.执行 /opt/tengine/sbin/nginx -m 时有警告
+ |. D4 d" L7 x% k0 M" V( K2 }1 U7 ]$ x
Tengine version: Tengine/2.1.0 (nginx/1.6.2)" ]+ T& `, w- `+ F" @% v
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!4 C) W5 F- e% j2 z9 v. M9 a
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log: g. q: }* J; J% D2 k
, Y4 k  q5 K+ w
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.6 i' |9 P7 a) o0 B( k& H1 F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"% U6 [1 Y9 r/ t7 s0 b: H( m, I
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!% D, t: z- Z5 g' D' A. ^
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"1 q/ B: D8 A7 ~' }
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"- a5 e; K7 P4 U5 p% X* e3 _4 y; J$ U
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
1 h6 H$ f9 L7 L, S解决方法,移除低版本的APR (1.3.9)7 D. j2 H! n, {( v
: B5 y& v2 r7 [8 x
yum remove apr
1 e' z/ W/ w* O0 m5.Error.log中有: Audit log: Failed to lock global mutex
. H5 p1 T- K& I: t" g) R  S5 q- Q/ B5 X- f3 o: ~
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
! w& v8 |( w0 c) v# D, A$ mglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]; {: X! J, P1 |% u' r* s- M4 O
解决方法:% t3 j2 t. m3 r, \4 Y1 x
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:5 j. u+ s  o2 ?. y% G

9 q" y1 V0 J, k  K# ~SecAuditLogDirMode 0777
. \2 k9 E* t! d, A- H, B* ?SecAuditLogFileMode 0550) K1 W. a8 I. ^2 E- O
SecAuditLogStorageDir /var/log/modsecurity1 W% p$ L% Y7 D( b
SecAuditLogType Concurrent
$ K+ Y6 D0 g6 u/ F& q参考文章:% p- W; s% _$ j% y* O+ n# o
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX! c& L- f. U8 {; @9 y: ^
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-28 02:22 , Processed in 0.050532 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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