找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12904|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
4 n0 I' G3 t+ Q0 a) c5 e9 k- i
% I# r7 _  M, R, F/ n! c一.准备工作
6 q4 Q+ R$ L9 f; H
7 a3 G9 D" t! B/ S& K; f/ ~3 t系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0, g: M  W" {) M& j* g4 d; y# U$ U

6 ?* S8 G" [0 M* k3 \. ^tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz7 g% y/ o# j8 `

0 V, s7 V  s$ X9 Q1 {/ p: Hmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz9 {3 {3 c1 \+ R+ e( I, ?
- \, ?, S6 g/ s7 l- x
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
2 k4 o4 E( p7 P# x  p3 Y) H2 b1 l& O; |3 G# Z8 Y
依赖关系:8 j  I& L, _3 C: {" [+ W
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
* `6 U# O; p6 {# k" d/ E% Z1 L4 n; G" v0 i
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel; _+ |: I6 R+ S  i( Q( I9 F
modsecurty依赖的包:pcre httpd-devel libxml2 apr$ V; e1 `9 j  j3 u$ {

) C2 [0 m# ^: p5 j) Myum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel( k! Z6 t: q5 `3 O0 p
二.启用standalone模块并编译! T' C6 Q! p( H5 U8 I

# o# |, G2 z8 m& ~# u7 N( W# T8 L" _" p/ U下载modsecurity for nginx 解压,进入解压后目录执行:6 N7 H; j8 D' a1 A0 d/ Q

2 v1 j$ `0 e. C2 G/ t9 K' k8 V./autogen.sh4 `! L3 E% Q! V, t; d. d4 p. f; l
./configure --enable-standalone-module --disable-mlogc0 d6 @* N) }8 S$ D" W
make : S4 p  w- G/ B
三.nginx添加modsecurity模块
9 ^4 J3 @3 z5 R  `$ D
8 Q! o  X9 ~* e0 H在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
  l' h5 ?; j" u0 D5 f6 R9 ^: R, k9 Z* D* n  e( n5 n# s
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
% l/ p) w3 x5 X% i, nmake && make install
9 v+ L0 |, O1 |) }四.添加规则
  }; e' T; z4 n7 u2 W) K4 c1 |5 a$ a; U4 z& f# f% W$ p# k5 K1 u
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
1 H5 i, h3 S" r# V! ?4 d- Q
4 `4 w) x! V" \7 `7 D% L0 K1.下载OWASP规则:
, L8 h6 O2 L1 ]1 `9 X* [" V- s. x7 h
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs4 C) D6 T2 j3 o3 E* L7 b
/ G& ^. z# s2 j
mv owasp-modsecurity-crs /opt/tengine/conf/6 b- X* ~2 i$ K; L0 L2 q
* p1 J+ \- x  Y5 m* R0 K1 u
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
3 e( C/ R$ b+ a3 |, ~# C2.启用OWASP规则:
+ a8 {! a7 ^- Q6 E  x( S7 Z) V3 v! y- @' p) P  I
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。3 g) G/ b$ q) w5 u
: G' B7 F2 v" S9 m! k2 `
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on, G/ F# Q2 }' ?" Z/ x$ ]- [+ }

5 V8 M( u& n; M! {- Dowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。$ P8 b% s: b0 u5 ]1 W
. Y. o! c  R& L) a7 x2 q$ Q3 t8 \; R
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
% I' S5 n. v0 v  k3 ^Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
3 D! Z, E. A  z* L6 `Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf& [' S, x  B+ z4 M& A
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf& F. Y5 p+ i3 J! I5 `
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
3 b( i/ X( f) `& s0 W& |" HInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
' }* H9 A$ m& ]+ ^, t) mInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf0 ^. o) L) r8 H. R
五.配置nginx
4 `5 w- v2 Z1 x/ S2 o5 J5 T% ]0 ?& Q/ j
在需要启用modsecurity的主机的location下面加入下面两行即可:
; h$ h& ?, V- |/ y- A5 S/ o6 {& m8 I
ModSecurityEnabled on;  
" l9 R6 [; V$ e2 z9 m9 N6 ^ModSecurityConfig modsecurity.conf;
3 ~' z, ]7 m4 l- U9 f4 ~6 y2 r下面是两个示例配置,php虚拟主机:
8 D! t+ Z4 n4 I' P8 t
. i$ x' f" [; ~- o( @server {3 G" F- _& e1 |% x( u* @! {
      listen      80;& h- Q* a- m- ?+ ]3 C0 H' S
      server_name 52os.net www.52os.net;( \+ G3 y6 s* ~  `( ]7 Q
     # _0 l, z+ n% u7 ]* w/ b/ k
      location ~ \.php$ {* |5 B( W# I7 @& U
      ModSecurityEnabled on;  
- F. I1 c$ ~+ S/ t0 p      ModSecurityConfig modsecurity.conf;
) ~/ G0 h' R, o, c, J# U) ^+ y" s+ g' e. `9 c$ ]* B9 M0 p! I0 Q$ b
      root /web/wordpress;( ^5 p4 `: @& H5 P8 a$ ~/ G+ h
      index index.php index.html index.htm;
6 U0 E7 l0 |  _5 ?  
5 s) [# P" C% e; K      fastcgi_pass   127.0.0.1:9000;
1 S% k+ F( b- k6 W' {" y5 @( i4 X      fastcgi_index  index.php;; j# N2 C/ m( I9 S9 b4 _3 ?, h  [5 F
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;3 J, h& O4 l5 ^
      include        fastcgi_params;
; e! h7 J1 }  @      }- M" J' p' Q5 [& i
  }+ Y" v7 e1 u9 m* n7 W
upstream负载均衡:6 C( |( R* m/ @% F

7 N% v* d& y0 W6 ?9 J: p  eupstream 52os.net {2 K& a) H' U. @( s
    server 192.168.1.100:8080;
$ E/ R/ g9 I+ M( ^    server 192.168.1.101:8080 backup;
" s9 S' ^. [1 h, w}* T9 b# i) N2 s6 K$ ?7 P& e  s- S

+ o# m4 _5 G( l: N3 ^+ Bserver {
( p3 f5 e: o: ~# `" M9 f  o0 |listen 80;" Y+ ]. ^" ^5 N
server_name 52os.net www.52os.net;/ g4 T6 e/ ?5 I* X; X

# H0 E3 T1 p& ^' ?5 R% nlocation / {
% f8 f* _5 L' J9 Z8 s! r7 s: N) E7 r    ModSecurityEnabled on;  
* f, G8 e+ j, I- c, U2 F+ }& G0 O    ModSecurityConfig modsecurity.conf;  
3 X* y# x7 \9 X" ?/ A+ C$ K: m8 C! R5 W  X+ p, P: N* w1 _
        proxy_pass http://online;( T$ U2 r. J7 K1 P; e& m' _
        proxy_redirect         off;
0 O5 I$ z, Q6 g) B. P. R        proxy_set_header Host $host;
$ t- v; x- H1 F7 F) d  T+ w  p$ F        proxy_set_header X-Real-IP $remote_addr;0 D1 ]! w* H0 l( e0 Y
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;# U  U) K8 i3 d- }! R- I
    }* T/ n2 F& n& F; a
}
# `4 M; c+ p8 K. Y六.测试/ a& [- w7 q0 g. C# X( A& l/ o

) Z2 O9 k- u# T8 z我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
- b7 F$ Y* X- N7 K- c( X8 f6 Q; e- `% Y+ n
<?php
# N8 O9 F  @) y" V    phpinfo();   
7 b6 L3 Z2 z0 h  J2 }?>, {4 |5 x. j6 ~, z
在浏览器中访问:0 ]4 j* _2 S" d" B

5 a+ d/ o* r- rhttp://www.52os.net/phpinfo.php?id=1 正常显示。
$ k$ V' Q. s: T; ?* l8 i3 e: qhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
+ h4 ?1 ]$ W% N( Nhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
4 P, [1 k% o- o( S' z说明sql注入和xss已经被过滤了5 r5 e  n* S3 w2 `% o" j( a
% w. u; i) R4 l
七、安装过程中排错
% P1 `3 [+ _4 W+ c4 A( g1 X
' u. s% ]# m3 D  {/ t1.缺少APXS会报错
% r8 l) q+ t0 F4 s% x6 \( ~' v# G% @" X0 N$ ^8 ?
configure: looking for Apache module support via DSO through APXS$ n% t/ z7 F7 I2 p. |1 p2 J" z  w
configure: error: couldn't find APXS( J( X! `% t/ a
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。1 e+ J+ x1 C; `* C
解决方法:. q9 Z* }" w& r
. {" s* V8 U8 ]0 v
yum install httpd-devel
4 T: \% Y9 z2 r2 Z2.没有pcre
/ B3 d4 |$ l2 E) f
. G% r, \( D) C2 \) d  gconfigure: *** pcre library not found.
3 w* `. m$ W6 K1 nconfigure: error: pcre library is required% q4 P& u) U" |1 v. s: X% |3 G
解决方法:
3 `7 e% X9 k5 E4 B  l' y
/ q' G5 F& H' l6 l# o; Myum install pcre pcre-devel
8 c! J8 m) y! p# T4 R  ]! c3.没有libxml2
/ K7 K/ n- k  D8 c
8 p! C. Q* T- r0 i) Q
. K( Q% o3 a1 ^2 uconfigure: *** xml library not found.
9 D) ^. T3 g, a) |configure: error: libxml2 is required
7 a0 g& T! ~3 S0 s6 C; b解决方法:5 }0 Y! A# O( Y9 n
3 B0 {3 }. \  n5 @- Y9 Q
yum install  libxml2 libxml2-devel8 P# j$ o* t4 w. X+ J
4.执行 /opt/tengine/sbin/nginx -m 时有警告
$ t8 U4 `, [# z* V6 B, ]3 ^& z& v& z. n) {
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
' ^1 |% R" P. tnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
; F3 k; A! f6 w- L; C& r) g原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
" ~7 m' s9 O2 ?; a9 F  ^7 Y1 a' b& R6 L8 p$ S1 U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
6 K& _; C0 B( [# @$ q7 ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"! Y5 H1 A$ ]% x9 c- N! b' B" }
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!% D( K6 R) r; Z1 T" R  M" I) W% K+ o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"4 e3 x  w: i7 l4 H
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
+ }- U4 x  Y" t: b$ v/ b, A4 f+ c1 s2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
' S1 P& d3 Q* U1 B& n解决方法,移除低版本的APR (1.3.9)9 F0 w4 h2 [- E) ^" W
, v- C6 @( z0 c; y9 L' U
yum remove apr+ U% f1 L6 s* n7 Q0 }5 V# n" P$ Y
5.Error.log中有: Audit log: Failed to lock global mutex0 V, U) w4 y" E0 m( d' v2 u

5 P; k; I6 S( G! G) x7 W2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     ! U6 Q% t0 @4 ~" ]" E
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
1 r8 V  |* w+ O0 b4 a) P解决方法:
) _4 |7 {7 T0 ?  @& R编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
8 O7 v1 b- V9 Z& t4 r" p: W% z6 r" X0 n, x3 h1 h% N
SecAuditLogDirMode 0777: H5 V% n9 A2 I
SecAuditLogFileMode 0550
$ J- a" Q. a" T2 D+ }SecAuditLogStorageDir /var/log/modsecurity
& h+ G3 v& J" p+ O! j, XSecAuditLogType Concurrent
7 s7 J+ u" I5 V0 w1 z参考文章:# s2 ?2 ^' x1 ?3 n% s! M  q4 B
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX: k, L/ e9 q7 ?- N9 i8 ~
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-24 16:56 , Processed in 0.037663 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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