找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9874|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
) w4 T/ E* e6 v) S2 a( s, z9 n" U' W( V" ^& Z* v9 f4 o
一.准备工作
! p2 X' M1 u5 G% g3 z$ J$ v* M/ n* j
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
" u8 F) z3 G0 y8 a( A7 p
0 u- H; ?7 S, i/ w( m8 h/ Mtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz! [5 s  a2 }' X% m, V
1 W; Z& a) ~) ^6 T  g/ o3 p% V8 q$ b( w
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz0 m& s; N9 _7 B2 d
* D7 m! y. `- v0 R* A( ]* N9 J
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
" `" N3 u" N4 G/ j$ q8 `
/ F  P2 m! M4 \) R( [依赖关系:
/ E; {: P# L1 L+ g/ ], l/ W8 z. S% mtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
" C# `- Z. g8 f7 R" @
9 v$ ~' W" I7 t& _. S' ~1 k2 Tyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
3 V0 r7 O* @9 [% ?+ cmodsecurty依赖的包:pcre httpd-devel libxml2 apr; o4 G7 o# q8 [/ Z
9 n2 u% E" G3 V) s
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel& o" {) `6 U/ v9 g
二.启用standalone模块并编译
5 [0 c: A4 ?& s$ q4 Q( O. \
: j- D5 Q7 v. U% y2 b( W下载modsecurity for nginx 解压,进入解压后目录执行:
3 T& S' m7 I+ h# n& j, t: h; n: G1 k8 d! c" p6 ^" j
./autogen.sh+ K# b8 g* s" {
./configure --enable-standalone-module --disable-mlogc
- v1 g  E, C. Ymake , _4 ^8 q1 r: P0 q
三.nginx添加modsecurity模块
  n/ ?( _) }4 `: P' Z
5 S4 o: F& q4 Q在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:5 s/ W0 b; q: g) c& C: h
! t+ N! `5 L/ e$ v$ F6 K4 J* k
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine; N6 Z5 d- g0 ?& a; s5 F
make && make install
6 j; x( y6 @& ]& c5 Y四.添加规则9 y3 |) v$ P; Q! o' L
) [' K8 z/ D! x$ b; R8 ^
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。4 _% s1 b8 Y$ @) _0 o

# a% V2 e, k# m; M  h  p: }1.下载OWASP规则:+ T+ M6 E. p% G* }0 a- F
0 N" |: |4 {& Q3 R. m. R+ T
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
: c* R$ K8 m) V! p: d- k' d. U* t$ a( Z* S; P6 v3 F
mv owasp-modsecurity-crs /opt/tengine/conf/
9 A. c0 W1 D: v( o" s% h( W8 t/ C
4 t% j* a" O" f" @cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
4 m/ c, u1 E5 J* u2.启用OWASP规则:
5 N% Z" J( d2 X( i1 G4 @8 H) G% [4 Q, Y
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。1 ?$ n6 f% o: ]" v% M! P6 z  ?+ v
! B: x( g7 N; d/ M0 G; J6 C
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
2 o( D% V, S* C, b. q, [
$ T' z8 v, ~0 p- k, aowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
; }  I3 p3 v9 G6 q, R  o
+ U6 j) M, `: I1 y/ j7 {Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf* H: o- J7 j5 y; y, w
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
8 G% G3 q5 U9 [  Q  BInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
: C# }3 \4 z& |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
  k2 C7 h% d2 E' M4 bInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf% k& P  o3 F; Q* k$ [
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
4 `( ^$ {% l4 c4 X; C# H0 FInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
# i% o  u0 C5 F: h五.配置nginx
2 c+ H4 ^) {( C5 M7 `9 ?9 F1 I1 p( v# Y- X3 `' |. D, u
在需要启用modsecurity的主机的location下面加入下面两行即可:
# i' Q; a& |4 J( ^6 I% n" O* J7 w: \
2 b" R* \# j9 N3 bModSecurityEnabled on;  
, z# D9 E- L- }" r: I4 @3 m7 fModSecurityConfig modsecurity.conf;3 Q! g5 O4 _. V$ H# w7 O5 D
下面是两个示例配置,php虚拟主机:' H  E# T! b$ b( `

5 t1 O. b4 c/ J' b+ [$ x5 g( h" zserver {
- W" a, `) k. H2 b$ P      listen      80;
3 `- W, B. r$ f  Q5 X+ r* S/ W      server_name 52os.net www.52os.net;
+ l+ Q+ r9 u0 L& k( r; B4 j     : \" C( b- t5 N
      location ~ \.php$ {
. T/ O! z) m7 h+ Y7 |      ModSecurityEnabled on;  ) m8 R  Z- B( ?
      ModSecurityConfig modsecurity.conf;
9 s* o9 Q% N+ l  E- n) q$ ?4 k, U4 A( ~
      root /web/wordpress;3 E* c( N  \* K- [" Q
      index index.php index.html index.htm;
5 A6 ]4 W  q, R  
; ]% z2 c9 ]% ~: D; D( J& H" ^      fastcgi_pass   127.0.0.1:9000;
  U% {) R1 m0 }. Q# \+ O: B      fastcgi_index  index.php;
6 F8 m/ C0 l/ x3 ?      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
/ {( A9 c1 I& W# O7 C      include        fastcgi_params;
3 k+ A) n8 ?4 [2 a( D) O  }      }
  R6 Y& H4 O4 [8 I  E9 G' V  }
- q" S7 h& N" ]4 I. U( k+ Qupstream负载均衡:2 ^" w# i# s' t( \; k

7 ]; ~0 B6 ]; P: D: `) M4 U$ {! ^upstream 52os.net {, p0 m: T- m1 L8 ~
    server 192.168.1.100:8080;
" Y6 @) I; f5 r) P( x& d3 e! D    server 192.168.1.101:8080 backup;
  i2 y* D8 ]' @; d}
5 p; p! _4 p6 _5 }6 ]4 w7 J  r1 N% k/ `+ C9 u5 \
server {
/ m+ H1 _* ]/ d) |6 Y$ j% k1 \listen 80;
) Q% K& ~1 u$ o# T3 H1 f) S- bserver_name 52os.net www.52os.net;! k4 B7 J* T% A4 v
; t' W' Y6 ~) j* c- @" i4 `
location / {- y0 n! ^6 r; g5 U! m# U" i) i
    ModSecurityEnabled on;  
: q3 Q" L( _+ X& D+ I" x    ModSecurityConfig modsecurity.conf;  . P, C9 a* \& E

1 i* N1 M- Q# f        proxy_pass http://online;1 n  u5 D8 W$ d( G
        proxy_redirect         off;* ~- b5 |* G2 a3 U, D* `8 o5 K
        proxy_set_header Host $host;
3 u- A3 |6 @9 ?3 o( Z$ `  F        proxy_set_header X-Real-IP $remote_addr;
5 g8 J8 v5 e, F; l        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
6 c/ r+ P! S- O# u  b) f0 {    }
1 o2 @7 }0 S5 _, X( R, p# z. t4 w! r}
+ L- F8 {& L- }1 o1 }7 {六.测试
: o7 u% n, o$ O/ ^
( V. E! y# b( d我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:# \# c3 S" ?, q" N4 o! h# ~
. `, H& o; E/ ^1 V( i0 a8 F' v
<?php
% w) x4 t! A- D- Y+ W& z+ X& i* b    phpinfo();   
9 s5 w8 d% q2 A! A?>
$ C9 {  P5 W2 v4 {& u2 T' z在浏览器中访问:
. v/ I' B4 {, R. Q3 |6 [  F6 E& Z. o7 h- U. E
http://www.52os.net/phpinfo.php?id=1 正常显示。
* s7 I% Z* s4 N2 P- t" I2 ~" ^http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。+ v& N4 `, \  I( c. f& D( T9 P
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。4 e. d+ r* x) S) w; r
说明sql注入和xss已经被过滤了: b; [$ P) B! r$ `

( P/ m0 `, V  b0 X( U七、安装过程中排错3 h2 x4 s% q: s; K# S

0 Y4 n1 K6 V& G) W4 C$ J1.缺少APXS会报错
' h/ R6 X% g0 T4 r- P" F- @5 Z5 J' e" R# K- y5 p( t  P7 A* W
configure: looking for Apache module support via DSO through APXS
& i0 N9 w: S& _+ @6 C8 x' econfigure: error: couldn't find APXS4 A, [' R. {8 L7 W" g; a! r% f
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
- i7 z, G* p7 v( E7 N/ _; @解决方法:! ?5 ?$ M- S* [( H+ P
5 o/ e- N9 A; e  C# r% ~
yum install httpd-devel4 \" P$ G) _8 S
2.没有pcre
9 B- C: P" @- ]$ B+ Z8 U) ~) C+ K/ P; d! o' p6 f$ L+ e
configure: *** pcre library not found.' r/ B4 m; |+ F# B1 g+ h( z
configure: error: pcre library is required1 e/ A0 t* a( {# S' [
解决方法:* e! k8 m1 u, d/ |/ o. E

3 {, D! S* I3 U& v1 Byum install pcre pcre-devel
$ K% O4 L+ Q. u: i8 x& O3.没有libxml2- [$ j) H/ ?5 q& h6 o$ n( \
$ Q8 Z; Y8 ]% ]6 U0 e
- P! x* J% l. O1 L
configure: *** xml library not found.1 H3 U) q0 a  z3 R: \
configure: error: libxml2 is required8 n4 C6 H' _- Y0 z8 g, ^  @+ l
解决方法:
/ L5 n' B3 `6 C: Q3 C3 O5 ?2 `5 `' v8 U& l* ]: D1 n
yum install  libxml2 libxml2-devel/ H  e+ o* C' i) V# h( q3 W1 ~1 H
4.执行 /opt/tengine/sbin/nginx -m 时有警告
! \5 a( k1 t) z! D) A: g4 k( u& N6 a% u8 P. S3 O
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
0 S) X5 V/ M1 }& P, Qnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
8 l0 b, H  g  `+ O' J原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log6 o9 Z$ x9 p, v/ V8 J; H

' F/ B; J9 Z5 L* L9 \2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.0 f; a0 s& _5 E' w7 C  M) |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
$ i) G( v5 P" b1 O2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!, Q& j& t* x/ I  G" g( y! F  i; }
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
7 O1 N7 ]! T/ J0 ?7 m/ ]2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
( o& z- q- O' o7 u. P3 K9 Q7 S7 u2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.8 B1 o' k$ Z0 P, Y, t& ^3 h
解决方法,移除低版本的APR (1.3.9)  T$ `: J  O7 D7 ~9 {& @7 a) K

. L- h. e: S4 s, x9 [) `yum remove apr
! r# b3 m* a) c8 H5 u+ g5.Error.log中有: Audit log: Failed to lock global mutex" f; a* g2 F6 J! y8 @
) y) @5 Q/ e! o$ R9 h, d
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
% `% A- a: F, U( W- {: [: v4 Xglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
; ^0 I/ F$ a" h) V" z- ~5 W解决方法:/ A8 M2 W# V) T4 w
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:: b7 ~' ]4 _: Q- p1 ^3 E2 e
, ^- W, f% c) Q' W3 k" q* ]1 C
SecAuditLogDirMode 0777% L0 M# i0 C- b
SecAuditLogFileMode 05508 C) f+ I2 c* F! z- g8 O- [
SecAuditLogStorageDir /var/log/modsecurity+ d$ }2 o) r* O/ O( H: _
SecAuditLogType Concurrent
- M4 @: k1 p; ~, g; {8 L! `参考文章:
* x$ i% r5 C6 f, _. S1 _" q, U. `https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
! m4 Y  K0 X% Thttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-2-9 10:07 , Processed in 0.085268 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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