找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12144|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
3 {: N9 |$ ?* p2 B* Q  P' [
9 w, Q( }! U2 g" u一.准备工作
+ `9 ^) d& s# L, v  ^" a
# |7 X4 ]  ^4 B9 t/ k" _系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.03 ~! d) v1 f/ i: J
' ?/ R4 p" I* N8 c# B
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
2 B# @6 |6 x5 J
  n7 E* w: @5 Q' N) _8 \% Zmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz3 P) g0 N3 ]0 H0 l; h
" P) [" c+ }) p& ?& Z# N& J# C
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
- H8 v' ?- H. V( o7 b* x1 ^/ T3 Z- K
依赖关系:" i2 {% b% ]3 `/ X
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
8 b2 r% Y3 u) g' B3 Z! I
: ]5 c, V' ^- z+ [% }4 e# h* lyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
) n( k: d- }& f" G0 J7 s, p" V  dmodsecurty依赖的包:pcre httpd-devel libxml2 apr& x, d/ c: l6 @* _' @& S( v

: U9 v7 z; M) Y+ R  myum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
; v3 ^* W' `$ |( N3 O2 v二.启用standalone模块并编译$ i8 d. u) y- O) |9 b  B
% s4 ]3 v  c; Y5 g' [2 G# Q
下载modsecurity for nginx 解压,进入解压后目录执行:7 B. j' }. i, W
  y7 v. r/ i& ~  J, m& n% J0 @
./autogen.sh$ s$ x; N0 B1 m# F" w3 ^
./configure --enable-standalone-module --disable-mlogc
: |6 z( k' j% M4 }: {$ J7 s2 kmake . A" P! i+ W5 @0 n3 S) q
三.nginx添加modsecurity模块
# c* _/ p1 ~7 C3 s$ `
) N+ g& K* r. G: X. }在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
; J- |8 {& {! j" X: f- G
3 z# C: l- r5 g% f( |! M./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine  @9 t' T9 e# R% W  @+ m& O
make && make install1 ~3 a: |2 ?. e/ w" u, u* u. e
四.添加规则3 j  J" e2 D% X  T) Y3 L

9 ^4 Z4 i: l) ?7 J( Umodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。5 {" M; ~9 T2 p) d& Y
3 n2 R/ {# S* d! V; n' y  x
1.下载OWASP规则:
$ ]& `* ?8 W8 J! _# ~! M9 J8 m# G1 S% X: ~
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs( e# Y2 S8 c& w+ E' U8 O6 m
  v$ G5 ~+ T4 f! U
mv owasp-modsecurity-crs /opt/tengine/conf/: G* T3 \/ v# G% H. G# D

3 p* Y* P/ ?2 B' {% I9 M5 e* Jcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf: s( O" E5 ~& `" i& k
2.启用OWASP规则:2 ^( {& F" T. y( ~- S

0 z& ?+ `) J1 F! g0 h6 Z" b& v复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。( M( l- c& w# n+ u

" Q5 l- l$ k: d5 b编辑modsecurity.conf 文件,将SecRuleEngine设置为 on2 V0 J9 O1 t& ^0 T& M
( J! z5 l; j3 R
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。  A" _9 _/ s1 V3 I/ p
7 ~9 A2 A+ N, n/ E% m& [& @
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
) w& Y) R3 v+ f8 X7 ^Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf  f$ \" t) [" ^" [+ p
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
: t& `- r* G  T2 h7 S) r/ i! gInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf2 K# I, `% Y" v* I, u% \# ]1 I
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf# C/ ], O) f- [! c+ ~! K
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
% \9 \3 t0 i1 B: eInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf/ n% E% ]. _) N  }- ^2 Q
五.配置nginx7 w& {$ }3 w' U9 _0 g

% f# _  U" ?# {; j5 ^# w在需要启用modsecurity的主机的location下面加入下面两行即可:. F% i; N* J- W0 b, B

# U( f4 K- s6 f0 ?6 H  o. Y4 DModSecurityEnabled on;  . K# @$ F- O# l  Q& t, j
ModSecurityConfig modsecurity.conf;
# n, |5 c2 v2 E( o9 u下面是两个示例配置,php虚拟主机:
4 Y# ^4 N8 L% I! i$ p2 x2 R5 C: J/ x. L2 x5 r$ ^4 `% a. h. B
server {
. R1 G0 u2 o- R3 U( W9 L- K  i      listen      80;
8 t2 J) |, ?* S6 M+ {# w0 v) ~      server_name 52os.net www.52os.net;
* u- z  u7 B2 m1 ?& f- D     , J  |% I: s$ D# f. G7 a' p
      location ~ \.php$ {9 U# ]2 @' ~9 S" n0 D, j3 u
      ModSecurityEnabled on;  . {# M+ s% D" l, }' n+ S
      ModSecurityConfig modsecurity.conf;) J5 L& A' N) h' I$ b3 G& E9 G

: s& M( d& G$ f. C      root /web/wordpress;
& q+ {( V; @9 \( C; M      index index.php index.html index.htm;$ ?( y1 q# L! }7 e; P
  
0 ]$ [4 Z' e; [; Y- i5 z& C. Y      fastcgi_pass   127.0.0.1:9000;
" a4 N2 w% o$ M; {9 w0 A; X      fastcgi_index  index.php;
3 I; a2 j* R7 [1 Z- M/ p% X5 x      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
& A& a, O4 b# g( _% h4 a      include        fastcgi_params;7 t. d5 C. r4 W/ F% s4 Z3 e* K
      }
$ x  \+ X+ t$ J7 p0 _9 ?  }. y2 }/ ^1 W7 x/ z- B
upstream负载均衡:
9 _' R  p8 n$ R% ^
  C% A3 W5 A/ N/ I0 T! wupstream 52os.net {
2 F1 o' n6 H! X2 |2 C; w. q! A    server 192.168.1.100:8080;
+ V1 _/ s. ]' m" K    server 192.168.1.101:8080 backup;. [6 n' p6 O" u6 B/ l( R. }8 u
}
1 S% e) @+ f& p1 I  S; \8 z& T+ _2 |
server {, A8 x; \3 O% X
listen 80;/ v2 ]" |! b9 ?1 C7 ?0 _. C
server_name 52os.net www.52os.net;' v/ g# E) g9 v0 I
$ T6 F' M: y! i8 {! k- m
location / {
8 ^- i9 Q) i; }& s    ModSecurityEnabled on;  
; }: |  M, G% H2 ?0 L    ModSecurityConfig modsecurity.conf;  
) Q9 i% A% `. V& Z7 P& x
2 D% S  K& Q, t( L! C        proxy_pass http://online;
( \6 m: f- q+ R  f$ L, V        proxy_redirect         off;5 O  I; [% N8 U; k4 I
        proxy_set_header Host $host;: G' D$ _+ u3 L6 F
        proxy_set_header X-Real-IP $remote_addr;8 I6 e$ P. T" G! l6 Q: M2 R
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;7 B' F8 @! h' @  @
    }$ f4 C% N1 Z4 A6 o: G
}; N8 O7 w, w/ W
六.测试* H1 @, w7 ?' N1 k) ~2 M* Q0 I
5 v& `$ Q$ T% w1 B1 L# r9 ~% P
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
3 W7 ]$ _0 T4 A. k! P- c5 x) ]9 H$ f6 P: {8 s& L
<?php
- O9 w: p% b  @3 j9 ]    phpinfo();    % F% \! a( c- N! B
?>7 A2 Q& E: t3 X5 j4 B# d/ ]
在浏览器中访问:
$ h) J! r, G+ b) L# j$ y, l# ]# {( \" |$ X  i& h. X- D+ |, b
http://www.52os.net/phpinfo.php?id=1 正常显示。* @3 ]. i5 V. @- m/ ]
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
2 p( ~8 @$ t  l; {3 jhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
* V& h' u2 {  c* Y- k说明sql注入和xss已经被过滤了
: S) `# J6 k0 W0 o  z2 y  Q0 `, \) l/ Y8 W3 H/ L1 f; |
七、安装过程中排错; @0 x3 v* r  g
# K8 ]3 k# R+ V. s9 k0 ]
1.缺少APXS会报错
5 N8 Y: }6 h1 g! f( O( B0 ~( Z. R: \3 @
configure: looking for Apache module support via DSO through APXS
7 J5 q' M% }$ l$ _, b* X2 ~5 Mconfigure: error: couldn't find APXS
: d9 a( X) z1 o& Eapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。2 j9 I: C' R4 f
解决方法:5 R1 j0 l" ~3 I; e5 o

, V8 f8 c& J9 G; Eyum install httpd-devel
4 A: x8 b: J4 h& `: o0 b2.没有pcre. G4 o1 ]: q3 @( A# H
# M3 M% S* F- s4 m, o$ o
configure: *** pcre library not found.9 p) v) T! V, X+ g0 b; d
configure: error: pcre library is required
4 w3 ]3 n% _. l解决方法:
( _3 w2 Z1 V: C6 n& x/ d  a
1 ~" ^$ E9 x- d5 a" u4 @4 v5 Nyum install pcre pcre-devel
9 I! Q1 b4 L  m3.没有libxml28 K1 G5 r2 Q$ j8 h5 q: [
' R, F- q; T* `5 u

. [5 h' f4 x4 l8 o& Lconfigure: *** xml library not found.
- G" U' k+ m! lconfigure: error: libxml2 is required
/ a9 S& t! j* y+ [' c解决方法:/ k* D9 ?0 D& F; \/ W" O

" w8 L( d5 v6 s  z* J5 Gyum install  libxml2 libxml2-devel
& z& |. m/ B) J! U' g4.执行 /opt/tengine/sbin/nginx -m 时有警告; f9 L* M. q2 b% L4 A

' k. `& `5 _- k) [# RTengine version: Tengine/2.1.0 (nginx/1.6.2); A1 C( T" E! f+ N; L- K3 t
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
' i9 e2 Z4 `. B) B- ]! }原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
# i3 I. t% J" H- U+ h/ f3 Y  G7 S( s1 C3 D2 T6 m. |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.4 v/ }5 [' ~( O* k' }/ a. T3 Y5 @: [
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
/ ]9 k/ J  K* \" ?4 {2 D' Q2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
* T# Q; W" [' c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
0 v8 C9 |* x! N' J( s4 n& G2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
% t7 P! y$ `2 R: ?& l; g) i2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
. |, P% W7 o! i9 ~$ ?! \解决方法,移除低版本的APR (1.3.9)
3 A5 c2 j$ x3 v5 T: `$ M7 m9 j4 R* y3 H# L& X
yum remove apr
# U, N0 h/ a3 x0 V1 U( a5.Error.log中有: Audit log: Failed to lock global mutex
' n! t+ q3 {. E- s
* W: z3 y) M" V. e+ k2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
) u* H0 F. D& M9 P6 N% n, zglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
. {+ h% g4 ~. z$ R解决方法:
1 v8 S. ?0 m, J/ B- t编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
/ f$ s) |5 K  G6 [8 `: M9 U: M. ~
' {) U5 }6 n9 q: p7 y  [7 ?) PSecAuditLogDirMode 0777
! Z* p: e8 T' tSecAuditLogFileMode 0550( A% h2 M; r! y  X' _; Q
SecAuditLogStorageDir /var/log/modsecurity/ G' [2 S# X7 ]% ^+ P
SecAuditLogType Concurrent
- h9 o6 y# |9 Y1 b参考文章:# Q& V$ i( l0 Z( R' |. y4 B
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
, d& A' @7 q' F- A, C- P( f8 Ihttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-13 07:58 , Processed in 0.082287 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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