找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13019|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。5 X- ~  V* S$ B' e. [) F
6 A4 K) {9 F3 w# m5 M7 q
一.准备工作7 [- B- Q3 P) B
9 ]1 u8 D3 m0 E% ~6 P+ g. `
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
+ J% U; C; J- t8 ?3 L; \: a. x( {
, H3 e4 v+ Q0 D# f8 v( ?tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz& z8 v0 @- \  l2 A% M
0 X2 X% B0 _6 x* h% T, r
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz  `8 Z  `+ H. c$ \  {- ~, A

2 F, F; |8 L* b3 I8 aOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs( t) e3 t% I, I
8 D. z, k. h; `4 S8 i/ a
依赖关系:
4 S, l: T3 |. C9 t# \& v- {9 Ltengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:! @, i$ \- @7 G1 j" D! k
% C7 c, ^5 y. u& j, ^6 R8 }
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel% {$ R& A* V0 o) o
modsecurty依赖的包:pcre httpd-devel libxml2 apr3 h" k" F+ r1 T; W2 Y! [

: A' r9 R% _4 }3 A3 D+ ]yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
' q& B7 j- q( Y, w3 g' K二.启用standalone模块并编译0 m% V6 T4 j# K" M( u- S- t+ B

; E5 O7 [6 E4 f& k下载modsecurity for nginx 解压,进入解压后目录执行:
8 Q# J. e5 B' ?1 r$ m
9 I: }7 s  X$ Z. o./autogen.sh$ b# o4 `! A7 D5 t% k( n, _
./configure --enable-standalone-module --disable-mlogc+ J; y. j* g0 p
make
9 |3 s; i, j# |0 _3 _% c( E三.nginx添加modsecurity模块7 D1 ^/ l2 ~3 ]. ?  c

4 i% `' e# f6 Z7 m在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
* Z9 I7 L2 Q, ]
  p* |, V( _( h2 y/ R' P./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine( A4 J: t) ]( T0 p8 K
make && make install
- ~. f4 Q: @6 B$ N/ j6 `四.添加规则+ i, O) Y: g9 w4 m! _

1 O6 ]3 E( W1 z! G2 h- S" K) \modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。/ W" j( J- |' V5 j, z. L
4 {! m! S& I* O! d
1.下载OWASP规则:( l1 G- F1 g* W* W  R2 O

- f5 Z: N* j; \- L  Q, D- V1 a0 l# Ugit clone https://github.com/SpiderLabs/owasp-modsecurity-crs5 k0 [- O5 v: X0 e! {0 e

2 l& h. M7 l0 W3 x: Vmv owasp-modsecurity-crs /opt/tengine/conf/
- W6 C' \+ _& x1 f. h3 b! t- _4 x+ C' ?% [* u3 K5 f3 e+ r
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf3 x6 X5 q4 f( t( j3 x  O7 F' P
2.启用OWASP规则:) T& [1 \1 k7 n3 R* A4 e& I4 m! D
$ r' K% O: f2 }. m: x
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。) o4 ?/ |" {# c* {8 ~- B! v
4 U; y( `$ T9 F8 |3 q
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
/ E% \6 L  f* u$ j/ V# e  F" u
! ~9 `' b2 V% E6 d1 d1 @# I! m5 yowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。+ Z) h- ~7 K1 Q- P3 Y2 m  v
5 p3 a- }/ n, a" ^) ^
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf6 O- ?3 X4 m; `7 z! X0 f% ^
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf/ t" R* Y; B. p" J/ z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
$ r$ W7 N% E& t; H$ O1 n9 z' i6 vInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
6 s, H) J- t0 H+ I- O) P* U3 F# gInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf9 w5 \2 C7 v/ W4 J
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf( f5 O: `+ B: ?! B7 h, y3 X( e
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
1 D$ l! j  z* G* U0 l& ~五.配置nginx0 _& U1 N7 X' f) E* {

4 E6 y" p: ?, v在需要启用modsecurity的主机的location下面加入下面两行即可:
, y0 D$ M. J* }  X& `9 E
% ~! ]& o2 _3 ]' I5 cModSecurityEnabled on;  8 t6 L5 P$ b7 I' x: F0 A* l
ModSecurityConfig modsecurity.conf;
0 v  z! x- S0 B. f2 g下面是两个示例配置,php虚拟主机:
5 v" a' L1 B3 ?9 ^2 }1 N, c+ i/ g  E! n5 x; O( f5 e
server {
  I; g1 S. ]' {: L      listen      80;
$ y, D7 _& T3 q5 x  @' C      server_name 52os.net www.52os.net;
4 G! A% A0 A1 f8 b. p( ~8 {* ^     
" e* o7 Y! \2 l- {      location ~ \.php$ {
$ t; k5 J7 q8 ?: R8 Y  P8 x      ModSecurityEnabled on;  
3 m. Z  h/ O! w2 u: B; J" Z) z      ModSecurityConfig modsecurity.conf;8 l  C5 y2 J4 t: ^/ g3 Z
) E9 ?: o; f0 H0 B+ d. I
      root /web/wordpress;
) a( f, q9 C. |9 U  `- d      index index.php index.html index.htm;
# a: i  o8 x( L# w7 M1 f7 X  2 t7 R, |$ {7 D! u  Y( J7 {
      fastcgi_pass   127.0.0.1:9000;
# S' n$ `. o. G7 R: a* X      fastcgi_index  index.php;
; H  n5 }5 A/ K* k      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
: k: g3 A/ \9 l/ t& x1 |( l: t8 E      include        fastcgi_params;' w) \" C2 c) w1 e% a/ L
      }
% |+ B: Y" C+ p  |4 ~% u. [  }2 ?4 {, y# X6 W' P+ `
upstream负载均衡:
/ k" b5 ]5 M# i; _7 a" g. O4 _( ^4 \7 t0 d" n  h( ^& h
upstream 52os.net {
& @3 I" f$ y8 [$ ~9 m    server 192.168.1.100:8080;
7 v: _2 Y7 Z' T# ^  W- ?    server 192.168.1.101:8080 backup;2 c" P% T9 w$ g. f, q% c1 E! d6 M
}6 A. A/ j, m9 `- U
. ]" r: U* q# w
server {; N( z1 Z+ \4 t' C/ H9 t1 g
listen 80;0 f9 w) ?, j# Z1 A4 o6 P: A
server_name 52os.net www.52os.net;
( L) g1 o: e' g. t* X* J/ h4 M% B* n/ y
location / {6 r# r/ T+ \( E
    ModSecurityEnabled on;  
) S' a8 U. x3 V- N  z# ~) Z3 v    ModSecurityConfig modsecurity.conf;  
8 {, M4 d; q1 I! @& O8 W" `+ F: j' G- O3 @1 u5 g
        proxy_pass http://online;
$ H' F2 i& V2 Q$ ^% d. c' [        proxy_redirect         off;' d+ t( i. u! p8 n2 z
        proxy_set_header Host $host;
2 d, }+ k5 N- d4 C        proxy_set_header X-Real-IP $remote_addr;
+ o- t3 S8 O- B$ P2 ]) H        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;9 r2 r+ F" I! N/ h% J5 A
    }
! x* b, E- B. c9 T/ }+ Z}  ~5 P4 P2 o: U1 A
六.测试% u9 |# V* \7 ^0 J- ]8 `8 F

! X; k1 O: O7 t( [! ?; A% E; k我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
' g8 [: D$ p" n% A3 u/ K! r1 ]" r1 p# ?6 X
<?php( N* F$ w3 F0 A- c# z3 M7 H8 f
    phpinfo();   
9 u# f* T. e: B: x) d  V?># d; i) U7 Y3 O: X/ M
在浏览器中访问:
. ^9 H7 L- e* r* R8 h' V, C6 y% n9 f+ r
http://www.52os.net/phpinfo.php?id=1 正常显示。
+ W  T9 q" l5 P, r, L7 zhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。5 @) z% ?( U9 F2 b3 [7 J' A9 q
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
/ A: ^$ i2 d' ]说明sql注入和xss已经被过滤了
, @' I7 ^* h: C9 v
# S8 D' `% f" h七、安装过程中排错$ ~1 D( ?+ v  [/ u  E* {4 H

* _5 o" f; k2 f. L1.缺少APXS会报错
0 ~; @0 K2 u! A( r3 x& j, c; C0 V' m+ C' C
configure: looking for Apache module support via DSO through APXS& K& A  B! V; f6 q- O- n
configure: error: couldn't find APXS
. a: x' }6 n0 L( N4 fapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
% m3 j6 k+ X( i+ ?% M4 O% n7 _解决方法:
/ X/ U) c0 s: D- v' m/ x6 ~
% {8 c- F& [4 E2 i; r5 ?yum install httpd-devel& \  R. q2 v/ A/ V" p8 x4 @$ g* _
2.没有pcre8 x) A9 g: K) t' P/ ^
6 N% I; [( F7 l( q7 |
configure: *** pcre library not found./ ?2 v0 y' O5 e: @
configure: error: pcre library is required
4 V* I0 ]; H* \" r9 d0 U解决方法:
( L2 E, [. j& w* y4 ~, d
2 Z2 h0 m5 R+ X+ {# }5 Q' }yum install pcre pcre-devel
4 a( m1 V4 x% {$ U; t$ o" Z3.没有libxml2
/ \+ ?8 v# _- Z$ N: R) T/ ~6 Q/ A+ m' e, S! ]. B/ N

1 k. X$ w$ Y) J  A" Gconfigure: *** xml library not found.9 p/ ?( |. S: W0 ]: z1 }% W
configure: error: libxml2 is required
/ N' l% {% C$ X- S' e+ W$ N解决方法:% p4 ^! _& N# ?/ _, B
6 q) a9 S0 ^7 y% q" x8 a2 J% W
yum install  libxml2 libxml2-devel
# g! y- L' g% X3 }9 b4.执行 /opt/tengine/sbin/nginx -m 时有警告, g6 ~' ~2 s: c
# N# s' L3 Q9 u  m
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
$ A( u6 R5 P" r( U0 vnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
6 O  t7 v# k2 r+ Z. j8 z原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
" L: ?8 C" K" F
* j* @2 n: n* {, i, x+ _2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.1 c7 E% O& Z, p1 I  [( O8 r
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
  C& F5 C) W6 ^$ i# ~2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
9 S* j% m0 _/ t5 o2 v2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
$ l3 a  ?$ _; M& ^1 \8 s2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
' Q1 w! z+ k, y  V1 }8 b) ~2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
, A! `) B8 c2 U! f, q解决方法,移除低版本的APR (1.3.9)2 O% s2 A: Z; i) q: Y4 m
4 u' b% a6 x( w6 K/ _
yum remove apr
' X" E2 B2 v. g$ C! X7 e1 k. C5.Error.log中有: Audit log: Failed to lock global mutex2 W) Q. d: r/ p7 v9 q' w
; H; m: ~9 j1 _6 J' d4 H
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     & K7 q, Q7 A. l$ \6 |
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]( g# S: s, `& o  i3 p
解决方法:: K7 w& C1 K4 ~
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
0 f; p7 ^) O& E# [2 [0 L8 ^$ L! y) E- _& z+ Q- T3 i
SecAuditLogDirMode 0777; Q. C4 j8 }3 [' U" a" z
SecAuditLogFileMode 0550" Z5 W1 Q5 c6 `0 T4 a/ \5 y; ]
SecAuditLogStorageDir /var/log/modsecurity1 M& P- v9 o/ {
SecAuditLogType Concurrent/ N- _5 p7 F4 [9 C7 _' q
参考文章:8 `+ p0 d  V( G& a
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX7 _2 y% b/ I% I$ C) ^* A+ O; }
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-4 10:37 , Processed in 0.064361 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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