找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11665|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。* K2 n% U9 T+ s" r& P6 {$ v

0 D% Q: E3 S! r% B一.准备工作% J3 N1 B7 Q5 W' `
5 z- D6 h; W6 @7 {6 @, o
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
6 p& M  v. n6 k, h8 m$ H' G* u1 z3 N+ ~
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
0 k9 z! e) k% j9 ^  U" x
+ e2 e4 |, B/ I6 {modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz1 U/ o( V4 i; F: r0 V

; |% i; K% _! O( }( k+ |OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
7 T. y% m6 {: {6 h9 E: I
1 l8 [2 F5 u' ~: z8 [* ?" G依赖关系:" x: q4 S* _2 Y
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:, V' V" m' q0 n8 g: `

; S9 \# Q9 A7 Oyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
4 G' G7 ?  Q# t) s+ Gmodsecurty依赖的包:pcre httpd-devel libxml2 apr* |+ X3 l3 O- |% R

( Q- E& `4 E: `; C0 \yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
# q+ V% w4 |' Z6 j' s二.启用standalone模块并编译
0 m. M4 w: `! k  H+ Q6 A
/ i: i) W& [+ T2 M5 M. S0 u下载modsecurity for nginx 解压,进入解压后目录执行:
$ h( C/ V* S( D" e! M
* D" k" o" [. e5 e./autogen.sh
7 ~; C( T* I4 k+ B8 Q./configure --enable-standalone-module --disable-mlogc' [% @9 O  g) }: H7 E8 Q
make
! ]- j! u- N$ x) Q, a1 @$ M: a3 r三.nginx添加modsecurity模块
- U5 A3 D# L8 x# p8 R3 u( J6 h8 J  L7 m" v( n# k
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:' x& S7 S( s$ }( l" x8 U2 w, G
7 I  W& V! v. g7 @; `
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine& V. [) I+ B( B7 k2 z
make && make install* U% {# A, z0 c1 V2 H" @" h9 Y+ W
四.添加规则
. I; h5 e  V4 e( v8 F1 H# Q
5 Y1 f* k2 G+ F  ?9 y$ Jmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。8 {- n6 k) {% B$ m* K

% d- ]" X$ }+ r. r$ {8 ^0 G5 u1.下载OWASP规则:3 {, Q$ ?! |, j% \+ ^+ x% @" P! Q

2 f0 A% P2 U: X' J2 hgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ e) ?) D  X/ N
. p1 o6 y. U9 X* }5 i- `mv owasp-modsecurity-crs /opt/tengine/conf/' a7 L6 b9 H2 q3 Q
, H+ s* [9 E0 d
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf; H$ V3 q! y% \" I/ e( q
2.启用OWASP规则:
, g$ @2 Y$ H0 j: w* e! Y/ U
' t0 w: q; E9 B2 r. A复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
9 G7 P) y  c3 p# K8 D! c# B* y
; K" R) ^1 ~7 ?编辑modsecurity.conf 文件,将SecRuleEngine设置为 on$ [# L8 A6 r$ m2 f
& u3 |+ a2 l  x+ w! y5 Q  r
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
6 p5 \4 m5 n& g8 O0 a/ G5 C3 D2 g' I0 ]
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf+ v# z5 W& v% {% V3 ~* S
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf& l- ^% {% K2 V2 z2 K
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
  _/ q  a- N  ~$ m: kInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf; u; ~/ u2 s: F9 d6 y- e( D
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf: v1 M. Q3 I3 O
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
5 A& }' o; U4 p+ N' C9 c% x( t# A) zInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
" T# y. e* ~* Y$ `3 U" x' R8 t5 q五.配置nginx
" ?, r6 q/ E7 Y( I& W6 c7 s
. {! x/ A$ @% p& r8 H5 o在需要启用modsecurity的主机的location下面加入下面两行即可:! w* |3 a' J: c7 @0 n. ^( a

. D2 y5 q6 E; a& p; u& h9 jModSecurityEnabled on;  
- e& I" h3 J, E% i8 ]ModSecurityConfig modsecurity.conf;
& |" h. Z9 X; W% H1 U) V) R( K! U下面是两个示例配置,php虚拟主机:9 e8 s# n5 |) s5 _' e

$ v' N- H, F! L8 T+ x$ {server {; V+ m: ?7 |8 A7 ]7 g
      listen      80;1 [# h7 X9 j/ ]- s- X& ]
      server_name 52os.net www.52os.net;
% s7 y/ m# ?/ W2 C3 u9 y& W5 ]     ; b2 M* ~( Q& B9 G# h
      location ~ \.php$ {5 u5 V* {4 K9 i  M; ~/ _  i$ z
      ModSecurityEnabled on;  " k( J8 q$ h  R# A
      ModSecurityConfig modsecurity.conf;6 `) t9 K0 X5 ]3 _6 X7 a1 v
) Y& d# d( `3 ?- w/ Y. U/ A
      root /web/wordpress;% `! K* l0 O  _+ E* K- k
      index index.php index.html index.htm;
7 }  H6 v( y$ {& C7 B0 |, r8 r: `  
# F* E  q7 p( m0 n, D      fastcgi_pass   127.0.0.1:9000;6 v. a5 V" {3 T. X, f3 [# V
      fastcgi_index  index.php;
6 a, Y( ~8 t: |, F5 d      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
/ e. O+ x: y$ b: w      include        fastcgi_params;
  m$ w4 r& N. O( j8 i      }
# v: Q1 G$ o! ?8 y, Y0 i  }- O& v9 ?0 s1 X
upstream负载均衡:# C# v3 {9 V* G! d- ?3 j5 e

: R& i, k4 j) _* V& ?3 b. wupstream 52os.net {3 ~' M2 t5 b6 J6 Y) E. F
    server 192.168.1.100:8080;8 N5 g6 G8 r/ \" e
    server 192.168.1.101:8080 backup;
+ W6 b9 K7 e4 M: O" F+ A- t$ F* x% F$ o}) K" p$ U3 z) n* G
9 t* \' Q2 N2 _) H' m3 j' C0 |, x
server {( M% l1 M3 r9 C/ z8 @& a
listen 80;' M( {- G! O  S1 {( A
server_name 52os.net www.52os.net;
; Y3 m; X( U% m* _* p  f' p; B! e# t  o8 L
location / {
4 i  o+ N4 ~& i# q) r6 l& n, O1 Y9 r    ModSecurityEnabled on;  % v; w3 o+ K4 P( j: @
    ModSecurityConfig modsecurity.conf;  
4 v. p+ [. X( p, J/ f" L" m: y5 K
        proxy_pass http://online;
: v( M1 U4 l3 Z$ b& f        proxy_redirect         off;
* l+ @4 N9 f. f! b9 g" B0 I/ B        proxy_set_header Host $host;
' `' o9 A) G. ~! a2 c        proxy_set_header X-Real-IP $remote_addr;
9 P2 Y! F3 o# T, ~0 X7 j        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
# [. z- m, D& v/ n, q4 \' f    }9 R2 G2 B6 B4 W
}
! ?" w% x; n+ H# |& g- V六.测试4 j- _( r) s+ u( S4 i* W
/ v8 I/ f$ K: Y) @& I, T
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
+ C1 J1 b2 {" C8 B, R7 F; D5 U
% v! M* Y+ l# H+ j. i4 l% E' {; F<?php% ?! d& S1 T5 D% p+ Y% g
    phpinfo();   
9 L: ]8 y' Q7 J?>+ F- z8 W: F" U0 s* b. Y
在浏览器中访问:5 V6 e, Y, w, U/ t& i/ N" w
+ a( e% u8 O/ D4 m* [
http://www.52os.net/phpinfo.php?id=1 正常显示。
3 |* M. p6 L* V% C; K- Q1 Phttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
# l! J3 @: ?3 e2 Zhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。" [  D  A# L5 f) M# b8 I1 U
说明sql注入和xss已经被过滤了
1 ^; N  C0 Q9 g0 q8 x  X% V( I+ U0 G4 ?1 }
七、安装过程中排错
8 M9 d0 v0 c) c2 Z8 B; c
' B8 c6 M& Z) F1 h% C' q1 t' G' d1.缺少APXS会报错) m0 _8 k- @1 _( p
* _: o. z7 Y5 l2 Z# K
configure: looking for Apache module support via DSO through APXS  \) O( a  U/ r( o
configure: error: couldn't find APXS
- x* n% w1 j; W  Oapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。3 K, Q# w' l$ _* d/ P: }6 ^- ~
解决方法:
6 S. C1 x9 x# r/ |( X& E4 ~1 F5 {' G8 q, w, \0 s
yum install httpd-devel
, b9 C2 ~% T5 I) |+ a0 Q) V3 D2.没有pcre. _. J8 r2 B5 i1 B2 @# Q- N
1 B3 Q0 u9 M/ j
configure: *** pcre library not found.
- Z& k; \3 Q7 F9 G% [configure: error: pcre library is required9 L( L0 j5 U. f6 m
解决方法:
$ _5 A) F7 a4 |% F& B& q
3 T1 X# o8 n- Qyum install pcre pcre-devel% E6 M! V' S9 o9 H! N
3.没有libxml2
4 i, i+ n3 O6 g$ M! D3 W) p1 A: r2 t
$ D6 k# N+ w. D9 f* v; g! J& f. [
configure: *** xml library not found.
. l2 |/ p, V! R8 O1 w/ \* i: \8 Mconfigure: error: libxml2 is required' k" ]+ l* }( b8 N
解决方法:
" a7 s; f) M1 W* \1 b. C; @
% ?2 l( Y( R) ?8 x& G, Kyum install  libxml2 libxml2-devel/ R8 `  `3 ^3 ?+ p
4.执行 /opt/tengine/sbin/nginx -m 时有警告
( G2 O' g, G+ n7 t
3 N  |. B6 Z/ g; U* YTengine version: Tengine/2.1.0 (nginx/1.6.2)0 @& C% o( {/ O5 t% A
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!( @- |5 H- D# i0 Z6 h2 A# M3 F1 b
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
' W6 X5 A7 I- j- f) E4 q; H4 R9 G  m9 ]' l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.6 x, @+ r/ y6 l( m% h
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
/ O, w! @, o2 Z1 i1 |2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
, x- o( c( O) a& Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05". H0 W8 ]% X+ D5 W! f, g/ h/ F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
* J( G& d' a' @3 P0 |: g2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
, b6 h6 q4 l2 i4 C' M9 V- w解决方法,移除低版本的APR (1.3.9)
6 o9 |* e; p9 T& m1 ^% @
3 b0 d1 a# K: x& R0 ?$ Ayum remove apr- w" B# X6 Y8 B$ C5 a; Q& j* }
5.Error.log中有: Audit log: Failed to lock global mutex6 F$ V9 Z# d  |6 |2 b$ N: ]

! u. c0 o. P) T5 l4 l( B/ @0 V2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     7 u2 j- ^% T, B+ {" X
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]# g% d" n' O0 B  e/ n+ E
解决方法:
: |4 J  r7 s+ O" ~, Y9 J) p编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:1 b8 ?0 e% H# j, \# h) ?

1 L; O& F$ I, V4 l9 C- mSecAuditLogDirMode 0777
3 O+ ~/ O1 [! q/ nSecAuditLogFileMode 0550
! x3 @. w; E- f0 r8 r, k3 @9 W" oSecAuditLogStorageDir /var/log/modsecurity. p  B3 Y6 L* w" a6 f
SecAuditLogType Concurrent$ \" h, b* W2 F* Y# h' J
参考文章:9 d" Z6 G" Q# S+ u2 H# Y8 s' A
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX* o( Z4 ~) x: x% z8 x- T
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-4 12:22 , Processed in 0.074576 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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