找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12920|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。$ b0 N9 g! |3 r% d
+ j# X, U' S9 d5 a
一.准备工作
# ?* [3 n: z( e. y
, A9 [( ]2 C1 L  h& m9 _: |# {系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
- [1 d* l/ f( E5 Y6 y' A
* I2 Q# W# X: e9 t* r6 wtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
, {( @" g+ v& E- @1 [: D
3 @; }; ]# B3 P/ J3 y9 h. u, jmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz: a& m( V; {: `; T

0 p  m, w) s; I6 q! U+ p2 b3 {OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
4 K7 J( B# Q. |0 _9 f4 J' U+ z) x. T/ ]
依赖关系:( ~: U' }) h+ p
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:" D7 Z& }  K) k( ^+ C

) F8 j7 c$ K8 |3 c  G* B, Byum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel' e' j5 `& Q: C3 i8 p
modsecurty依赖的包:pcre httpd-devel libxml2 apr
8 Q7 I/ W6 d: M, ?. S9 u
1 {* X: r& g- ~$ Tyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel3 ]* B3 ?* H( \9 G; r. Q
二.启用standalone模块并编译* q% E8 _) p, {' H' O
2 e0 o0 c$ E; n, Q( D, X8 f9 q
下载modsecurity for nginx 解压,进入解压后目录执行:
- i) v/ d2 B$ h8 d5 [0 \. u" k
; r" k/ U/ c5 q7 `./autogen.sh
, o: w1 o2 O" S" [3 }" j6 P6 x./configure --enable-standalone-module --disable-mlogc
+ W5 }) l3 [5 L+ Z  `5 `make
2 G) ?' x, g: ?! F9 L6 S三.nginx添加modsecurity模块
! T  }& ^6 o: d$ C# R: n" \) p3 O; z' I" i3 p" R3 P* E
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
" a" ~$ x* O. t) H4 T6 @+ a. G
9 `# s& @6 U, G3 ]* T( T0 C7 z./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
, p/ _$ V4 M/ K8 g' S# Wmake && make install
. N# X) t7 ~$ v" D# m四.添加规则
/ R5 c  f. R6 z" Z
, _5 B4 O3 E3 ?modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
7 \, s: K8 t7 t, T% R! h
- {9 n- x0 I5 R5 d6 N: L1.下载OWASP规则:
. k+ S# ~* G) {+ ^8 d8 d% `2 U% t" A; ^! g, r
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs% ^. o: R. d. @2 R

5 z8 l) V# K" c4 \, ~2 f' Umv owasp-modsecurity-crs /opt/tengine/conf/
6 o# ~, u* _. c. _+ X  t( O, s; |/ u2 R' R7 q5 O
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
8 c: I3 l. J, p7 t2 J$ l( s2.启用OWASP规则:
3 E# Z0 Z- a7 Q
# a* W6 v' F( S复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。: {* q+ z1 Z4 o; \% j

* r( w$ v: C8 e4 ~: o( M编辑modsecurity.conf 文件,将SecRuleEngine设置为 on# q' N; R) g- R' J

7 ]9 j0 m8 S2 q! z& _8 Oowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。- J0 C8 H1 n: w/ Y& F$ I; R

! N8 R' S% f5 s8 n2 [Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf2 K3 @; X4 a* S4 y# ~
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf  S4 e6 i. h0 j1 b! u
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf/ r& ~$ r0 b2 f6 K# j
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf/ F' ~, S0 U1 n' Q! l* m8 S
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
1 Z) y/ x+ u" |" w% ]4 }5 ?+ U  ~7 pInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf' C+ `- q: I! V6 P1 s2 O) x
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
0 a5 J# b2 d8 Z; \/ q8 b" v五.配置nginx  s# I; h" I9 q- e
9 \# @. D7 n& k, {; ~6 S* L
在需要启用modsecurity的主机的location下面加入下面两行即可:
" ?2 ~  v  i" E* G; I& C' q1 h  |
ModSecurityEnabled on;  
2 y5 q, o  A$ J0 ?! Y" z: pModSecurityConfig modsecurity.conf;( B7 j1 g6 {" m. w$ O. ^
下面是两个示例配置,php虚拟主机:$ ~, p1 D. @' C5 f6 G

# h( a: u! G. _" G/ S( r; ?4 Mserver {- T7 G. o# Z9 }1 g  l
      listen      80;
; b6 L# ^! w/ J$ O6 q2 e* t3 \: ?8 A      server_name 52os.net www.52os.net;/ H: H2 V( D$ B: m" d
     
1 B- R9 r" P5 T, B% {& F2 E1 S3 F      location ~ \.php$ {/ e( g6 R( |1 a; A
      ModSecurityEnabled on;  4 z8 k/ W1 Z; g& M* X" M% j
      ModSecurityConfig modsecurity.conf;1 g" G7 [( U) q* c% {

- d9 u: v8 R" Y( r) H      root /web/wordpress;
6 k( u7 p5 x! O# {3 ^8 v! Y( W      index index.php index.html index.htm;
& [7 C# U/ S: M7 w# ]  " h4 O( D8 L# E% }$ Y; R
      fastcgi_pass   127.0.0.1:9000;
9 L( J& J- v& D5 q! [1 z      fastcgi_index  index.php;
% A% {. k5 B2 W8 `+ t9 x      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
3 t- E- r7 D" q4 Q6 L6 h9 [      include        fastcgi_params;5 V! m! o& a' ?% I  h* ]5 [
      }
4 H7 O7 [$ ~- W0 J7 }  }8 `8 r' H: y* R! O$ Y+ P2 U/ s( V
upstream负载均衡:6 ]3 j; n8 `, s; V: o
. O2 x4 u' k1 U7 Y
upstream 52os.net {  Y1 N: J* o3 _4 H; \' d  F+ N
    server 192.168.1.100:8080;
& n# l, U! S: I( q' `. m0 d    server 192.168.1.101:8080 backup;
1 m1 K4 ~0 y- i4 K. W6 m}6 g* Z7 O2 X: j( @. M
0 z- A$ a: R0 Z2 i  Q
server {
1 A2 `, {) X% I+ s/ @) qlisten 80;
) w+ S* b! ~9 _7 n: T0 nserver_name 52os.net www.52os.net;
" Q* @) K# Q% J" ]; ?7 d5 O# O" x2 A- s) R  o
location / {
2 H) Y2 q0 G4 \; F* z9 r    ModSecurityEnabled on;  
: X* a1 M% `: g$ h: L    ModSecurityConfig modsecurity.conf;  3 u7 c# K4 j9 O4 z  W8 |
: G6 {" I( I: J7 D. W
        proxy_pass http://online;
1 Q+ x6 d5 o  \        proxy_redirect         off;
# u9 A& m6 u2 M) W4 B# A        proxy_set_header Host $host;
( |! g& c4 O' J' I) M        proxy_set_header X-Real-IP $remote_addr;
1 p8 {2 u6 v  q7 T8 o        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
7 I: M3 q8 p( s  Z% b0 e1 d; B1 T    }
% `0 ]$ {8 a8 ~7 P}" i8 X* m$ o1 U7 m8 h0 O
六.测试% a( ^0 D1 g6 G* c

& S% T- B! G3 O$ Q$ v* q我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:( q) `8 p8 n9 m' O- T, R& F

) I8 J5 @/ }5 t9 \. J1 T( w<?php
- b' s( H0 I$ H7 P# D  i2 o. O    phpinfo();    5 T9 S$ l/ _/ c4 _+ R
?>
1 ^9 s' N( u+ N2 S9 Q6 U) q在浏览器中访问:
& X$ a. v2 Y( U( ~1 v5 J7 W/ h1 _" ?. D' Q  k# V
http://www.52os.net/phpinfo.php?id=1 正常显示。
7 r$ t9 b7 k1 ~: k- Uhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。) i+ P5 j' N' F2 @7 x
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
/ ]7 [  N2 b9 R0 P  F, J5 R说明sql注入和xss已经被过滤了8 ]8 m5 ?$ K0 y( p9 J7 P6 P5 X( R8 [

. o6 m( G& E& T七、安装过程中排错
) J4 I2 x$ z) d0 q6 e4 N
! D, P& |5 z) T1.缺少APXS会报错
3 d2 D' h0 N. p+ e3 ?
# g4 _, o. ~! h, A5 ~; r6 Q; h! O" j8 Vconfigure: looking for Apache module support via DSO through APXS
& p2 u$ v6 \/ V0 s3 yconfigure: error: couldn't find APXS
* i+ z2 M5 l* _9 U) |apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
( r& v7 `/ |  Y解决方法:4 Z7 y" t: Q4 l: [4 \6 w+ J; t

, U- \; B' q2 h+ z( _; t' k9 ryum install httpd-devel
3 m- w) `- @9 E2.没有pcre
* M7 `% t6 [" L: _
$ P6 ~9 C4 a' C+ [; S% Sconfigure: *** pcre library not found.
( U( e/ A6 P3 X$ f( rconfigure: error: pcre library is required
. e2 _/ ], |" P4 L2 D( o4 D, Z6 W" e解决方法:
4 W7 t. l$ w  `0 B% q
" D* v) I6 _, R0 m, yyum install pcre pcre-devel; W7 e$ X( L- ^# _7 U
3.没有libxml2; M- V& a$ I- W3 ^2 o4 a

; n% Y. `# {( f* I- {+ `
: X; H( O8 d- [configure: *** xml library not found.. C3 p6 M( L/ ]+ @. W2 _
configure: error: libxml2 is required
0 ]' i& [* }; @/ t8 R# y解决方法:
3 l: Y4 c+ u+ W% [' E# \5 D* J  e% Q# S8 @! U8 ~
yum install  libxml2 libxml2-devel. V. P6 h$ P- g9 ]9 a8 ~( d* T
4.执行 /opt/tengine/sbin/nginx -m 时有警告$ C9 k( b1 d: D
6 ^: H$ q) p( u
Tengine version: Tengine/2.1.0 (nginx/1.6.2)2 w: W- k& z% Y) n# ?# g
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
. P; k  @- n; u* B# G# b原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log7 Y3 r- O' L5 P. ~+ K
6 J% ]/ ~0 ?* |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.+ B) I1 b7 \2 ~2 U* x
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"* C3 V8 W  ]1 o7 q7 B" k. a. u% |
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!. l- W6 T  O, v- m: X( f7 W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"/ h9 X! f3 e% Q' a8 n* G( `3 R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
9 m! v) h1 x: r+ ~- h+ u0 |2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
% g2 c* H% Y( g: k+ B! y, Z解决方法,移除低版本的APR (1.3.9)$ J- [% W& F% W( g, r" Z
, ?2 t) I) t9 b: m
yum remove apr4 g7 x3 x6 [% A4 j5 q9 M( ?( c
5.Error.log中有: Audit log: Failed to lock global mutex
6 }* T  k0 C8 @. j) s! q6 `7 n& b0 ~3 O. I3 v  m
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     9 X. w+ F: k; r. W, C& ?
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
4 h- f" e$ y) N  k解决方法:
/ P% a9 s5 r/ U编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:7 D9 s9 |) Y7 Y. z. m& y8 C
( t* |  K& y. g, n  i# W
SecAuditLogDirMode 0777
$ `7 x4 D. U) D+ r& Z" h& Z: B7 `SecAuditLogFileMode 0550% s. ]! E" _; ~, v7 s
SecAuditLogStorageDir /var/log/modsecurity
' P  ~; R) A; E( bSecAuditLogType Concurrent
/ H! h+ t& \- E# @& O  u参考文章:  P: x" S* K8 ]" s; _  S+ |
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX  a/ [8 I. c* o  ?# g0 E* W
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-26 07:08 , Processed in 0.067091 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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