找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12701|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
- e/ s) v0 b5 w$ y
7 m6 H2 J' J( h  _1 O/ {6 i6 Q6 g3 T一.准备工作' S' G5 j7 e3 }0 V

' C  ]) d+ J/ i1 {: O6 d1 Y系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.01 V: B+ T0 Q* h8 x9 h7 ~! [9 [% J5 n# ]
  P' A( ?- r2 h( I. V# S
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
4 g& Y( M  y/ l: j. W7 o
5 |, x( u% g2 mmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz; B, a7 |, P' Q5 P

1 H* K" ]9 e9 \! h6 H$ rOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
+ `/ O( V) `! c4 h9 m& _$ o/ F7 G, B4 n3 ]6 @7 _: C- w
依赖关系:0 k2 r) c0 }. t. R* P
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
+ X# n5 _" ^+ b/ v" ?2 n4 ^: H$ k$ \1 w9 }1 @
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
) `5 |7 `. d' amodsecurty依赖的包:pcre httpd-devel libxml2 apr6 Y# F/ ~& G6 @2 C) K

* k2 B5 l2 F2 s  m5 gyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
! O# `, v5 Y3 q) a) D9 L: r二.启用standalone模块并编译
( O3 v+ _& u9 ]1 V4 `; @
" h9 O! ], T- N下载modsecurity for nginx 解压,进入解压后目录执行:2 B+ \+ ?  B1 c3 ?
7 Q' [% ]; g( X& P6 Z9 a- `% w5 V
./autogen.sh
! m- b3 Z! @  K& w9 ]- E./configure --enable-standalone-module --disable-mlogc5 W; ]" }" q4 n- J3 |2 y/ F
make
8 \. g( o4 Z, J( k( i三.nginx添加modsecurity模块
6 H# I6 d1 [* ?5 w# R
$ C( {2 T$ V) n9 L0 ^6 [" M在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
4 Q/ y4 E5 R) }7 }: v1 c$ Z3 A+ ^3 ]+ r
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
* B6 v  `! Q0 e/ J, Y, ?make && make install
8 H4 O! U: i: f0 R" A四.添加规则  ?: b0 O$ B- n
9 o# c, u0 ]' d4 V( A/ F- E
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
% ^& n/ h9 k: v+ X: c4 C! l" [- l4 L) [$ j
1.下载OWASP规则:
* K, L9 I6 D5 ^/ R5 g6 l. b0 P/ e2 [& u
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
9 F% f7 S4 h6 T( F; l# _- M% ?! }# f  A
mv owasp-modsecurity-crs /opt/tengine/conf/: H1 }) B  |$ i  u" R1 Q, M# q
0 a0 W3 Z, d. ?) a: M; {9 _
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf0 }, J( v/ D7 r& y$ P3 N6 ^
2.启用OWASP规则:
3 ~1 c9 {' j' h6 O5 z* h$ L. ]9 b; n4 S' D- ]7 u' Y
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。, t  i. S4 h  B, h* w1 c/ ?
4 C7 m& M' A2 n) Q3 ]* Y# `
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on5 ~5 s" V. e9 y9 p- F+ X' H/ K

& @/ w" j' k3 x3 Uowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
* i. }5 S3 J# P; l! G; G2 f! t2 _. a8 [2 O+ n! d# y; [
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
1 ~, f# j) K  M, T$ RInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
2 F) b+ a6 M  E3 G0 SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
1 A# Z: b0 z! vInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
* c4 k8 x' A" q1 y- AInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
; [: H5 J: {. u" m% ]: C$ \Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf! ?5 {# G( P$ w5 Q, V
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf2 q* b6 h% \4 A! r6 w
五.配置nginx% H0 X& {/ T5 H1 B5 V$ S3 T' n
) [3 Z8 `6 }7 C
在需要启用modsecurity的主机的location下面加入下面两行即可:
4 Y6 @6 L- G$ S  R: H8 S; k- n4 U) p1 g
ModSecurityEnabled on;  5 T- r5 m) C( T% X* F
ModSecurityConfig modsecurity.conf;6 s. t. M( M  D+ {3 _
下面是两个示例配置,php虚拟主机:
7 q+ c$ X/ @  u4 K( k+ W& j5 O0 G: F1 V
server {, p5 ]/ _5 s# a5 f
      listen      80;8 W4 \3 [5 M; `9 b, g9 N
      server_name 52os.net www.52os.net;
( T0 V) |- N9 P7 [1 h' b     
: }  l. e3 ~& E2 I  ]- e      location ~ \.php$ {8 s6 k3 }; D, b. O) B- c
      ModSecurityEnabled on;  
/ `& V& V/ l3 H2 s" F      ModSecurityConfig modsecurity.conf;3 k2 F% U, D2 O7 I6 q* h9 C$ N

. P6 S1 s4 F2 @9 M- D      root /web/wordpress;6 O3 E2 [! d" i2 E; k. Y
      index index.php index.html index.htm;
# R" _! p5 ?6 q2 B! Z  0 c2 w) {' F5 ?6 X
      fastcgi_pass   127.0.0.1:9000;5 K. |# O- q( z2 `
      fastcgi_index  index.php;
  A) ]9 l" P! b+ W! q      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
& Y: X7 v9 p5 ?0 N$ g7 k$ V. e5 @; J5 F      include        fastcgi_params;/ g6 \& A% J3 ~: m" n
      }
1 H7 W* g! O. `  }
. c. \- ?4 }0 t. R0 O. |* ^upstream负载均衡:
& x' Y5 U+ ~  v
2 [7 `1 @' U: p) G) x" Lupstream 52os.net {, U. f  j$ L7 ?) Y! Q5 [
    server 192.168.1.100:8080;
; b  o1 G8 O: U4 d& p6 A% p! [/ @    server 192.168.1.101:8080 backup;) t, U8 a) t% W4 G
}5 P4 a% Y$ Q. m- V
. y( a9 |( h4 x) @: n- k# {
server {
; {5 _0 M: b. v2 T) H+ flisten 80;6 J6 O& ?& A1 i& I; _
server_name 52os.net www.52os.net;, K/ `$ h6 U* |! D/ h  Z3 v
. N. R2 K& z( O) W* \# V
location / {7 `& p% g& n9 {* Z
    ModSecurityEnabled on;  
% i  S  \7 D* Z0 T- q    ModSecurityConfig modsecurity.conf;  
/ U: u9 Q5 T  l/ P4 X( G2 |) m4 X$ ]( s
        proxy_pass http://online;
2 |( X- W* E! J+ r% q8 x4 v/ H        proxy_redirect         off;
1 V* [* B; ^5 _8 ]$ X        proxy_set_header Host $host;' `/ n: H8 C) r7 m- c* N3 k
        proxy_set_header X-Real-IP $remote_addr;. N& w. }0 T4 n4 s/ [( _9 E
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
" q$ D& F1 x/ _% D* b- L4 P* Q& Y" ~    }0 M: d  `, s1 h1 {( _5 F7 A  U
}$ T/ a' C9 d6 J. z! m( ^
六.测试3 ~7 P* g# W5 E8 i6 G! N* ^
6 o7 r% x5 N& `# c  c! T0 v
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:# x# Y: O0 L4 f; L5 a. ^3 E& {$ V

+ o- X( R- q6 B! \' @8 P, G% p<?php, w2 u* j9 D0 ?" q8 R
    phpinfo();   
  M8 L8 b' y5 ]3 |# I7 ??>
5 S+ V+ b) }: \% K& k1 a在浏览器中访问:7 e9 s- m7 r' i

6 p4 \! `" ~3 Ihttp://www.52os.net/phpinfo.php?id=1 正常显示。
* ]1 O$ E5 z8 ]6 J1 ohttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。6 {; ~4 O0 n& C# y
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
- X- X# ?5 ?) ]2 n: z说明sql注入和xss已经被过滤了
9 n2 m+ R! E+ L( |4 w4 q( c! U9 _: e
七、安装过程中排错
; i7 P1 ]$ b; R" W  c' a3 \/ o; Y+ Y2 ~9 W5 Q
1.缺少APXS会报错
& P. Z% n, o' \2 @6 f* n  i% h! y5 _$ p6 G! w
configure: looking for Apache module support via DSO through APXS. k/ f; Z- V$ }! K. B4 w# [
configure: error: couldn't find APXS
! F% E% i& o) z  Q; `+ W5 Q+ Eapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。! C7 j! R9 g6 Q4 S2 Y. A
解决方法:
, W8 ~1 v2 z  h# P& G
) c5 d7 B$ S/ v3 O( k- W7 yyum install httpd-devel& U+ ?6 {( ~1 ?: @& w
2.没有pcre
% z$ ?3 [3 E  K4 c, Z# `- w
# i# f6 o( F: ^# A5 P' P+ aconfigure: *** pcre library not found.8 F) E" a% T' H$ ^2 |/ C$ s/ n" k3 y
configure: error: pcre library is required
' U! @, l2 [. v% f1 O) t; K& K解决方法:) ^( h: V5 c. x9 W  L

8 Y# W. [1 A$ y5 X- |5 t& zyum install pcre pcre-devel
# T% r+ m' c) X; c3 J1 W3.没有libxml2
3 N  b/ Y5 p8 J' G3 r/ j
( x' r' S# X! Q; }3 s( a8 ?1 ?3 `+ f
configure: *** xml library not found.
# e9 i' U; y& Q2 l; |configure: error: libxml2 is required( m* o, o, l5 j7 T* p
解决方法:6 B. q( s  n/ h  S3 \4 Z( b. ~

+ f/ w) e0 j2 d4 z1 R. Xyum install  libxml2 libxml2-devel' Q8 k( a. w6 G) n/ Y
4.执行 /opt/tengine/sbin/nginx -m 时有警告
2 j9 }: B4 ~7 b+ ?+ P( ^; |- \5 G0 {0 J6 h7 X
Tengine version: Tengine/2.1.0 (nginx/1.6.2)- B) k: Q6 A2 X/ D4 u! @& I0 g+ z. r7 B
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!1 O: ?, v; Q7 t9 ]
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log4 i0 z( L5 u2 S3 j  H1 I2 G

) T7 J- k8 m  u! X. A$ ^2 }( \2 T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.) W1 a' O  i% m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
$ ^& b/ W% \, s2 u% A5 b+ R8 A2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
7 X* ]0 T+ `7 C  P2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
6 }9 g% l1 b" {2 I! E7 i( W, Y3 z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
' F) _: E; @+ W( M; t2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
1 F5 P. I1 q  ?2 z) B解决方法,移除低版本的APR (1.3.9): u$ h9 B7 D, G

6 ~9 I7 y9 V+ Y( ?+ Iyum remove apr: R/ B/ `- K$ J8 u' e
5.Error.log中有: Audit log: Failed to lock global mutex
6 T% D% D, n8 y6 b$ u. K+ T" Q9 W/ o3 w% z
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
2 l* `' L( R5 d( b" r, `global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]2 i! F* g9 v; D; E
解决方法:
. T/ u. ~/ c& |编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
. d; z; b$ o3 _7 h9 T) j; L$ \) O) _
2 `* V5 q- s+ K: M% TSecAuditLogDirMode 0777
& \6 ~- I# G& S  y! l- @SecAuditLogFileMode 0550$ B" X3 {8 u# V- I( T
SecAuditLogStorageDir /var/log/modsecurity
+ z* n( r1 r* {8 ASecAuditLogType Concurrent$ w- K1 k4 t) u0 {$ X
参考文章:
/ f5 h' u4 c+ e4 W9 `https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX& U* A/ l& ]) z5 m
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-1 12:16 , Processed in 0.034110 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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