找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12858|回复: 0

nginx配合modsecurity实现WAF功能

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

2 p) u; V( L1 I2 B/ [$ O+ D一.准备工作
- S7 t' T  N9 h4 e/ ?
- K* O0 B& V5 A7 t5 l系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.04 b# f# L7 x" e0 ]( A# K4 `
) d( q: E& ~; A$ ]; H7 h
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
  a) Z# _) K5 v6 v4 J  @" ~) d; @
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
) n& @1 S2 w+ a! _5 m5 d0 C6 ~
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
3 u6 ]/ l7 g1 u& F
5 t- E' n+ x9 A: }# O依赖关系:
1 g9 C9 m/ d$ T& a& Ctengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
  ?( n- A& ^, K! f$ L# |; z" z6 U2 D: |+ q0 j9 d8 R& c' l. a
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel+ w/ P3 _9 k* ]. H4 D7 Y' B
modsecurty依赖的包:pcre httpd-devel libxml2 apr9 Z% o# d$ o: |3 z
3 z  I+ S, P$ D/ H% q- Z* A
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel+ x# x/ G$ ~/ K1 ]( O5 h, j
二.启用standalone模块并编译
# ^- W1 [7 \" R
$ U' D' B1 E8 J8 L下载modsecurity for nginx 解压,进入解压后目录执行:
7 Z3 o5 r$ ~/ }) s
# P& k4 H" Z- Z./autogen.sh
3 A6 [. W( D8 y6 E8 A6 q' \" U./configure --enable-standalone-module --disable-mlogc+ N) d3 \9 l+ p1 ~+ ^7 U* j/ P2 d5 b
make 7 f! i7 m% @; I( Z: @
三.nginx添加modsecurity模块% A5 z" z& |' P" L4 w. W! {

% P$ l# R  y0 R5 i  `: ]在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:9 W6 c7 F* q, L% F0 `* E

" a7 `" i6 p, d5 h* w2 e1 P3 u./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine8 B! C2 `1 t: a& N3 g
make && make install/ k. j: f4 E9 q7 o9 u5 x
四.添加规则* ?( h9 r6 |  _

# W0 h) {6 F9 H4 ~modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
$ w1 p  T( S. b& E
# t3 v' f% q9 B% z1.下载OWASP规则:
2 J  m' H8 N& d3 q% a! n. N9 `6 Q1 U) |: R
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs$ G7 m0 g. |/ b

4 K: v7 q6 x" K  P! ^mv owasp-modsecurity-crs /opt/tengine/conf/( @! u& I1 _$ _. U

0 m' M) k$ ?( ?3 R7 p6 wcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
% H6 \" e" Y+ b% b0 f2.启用OWASP规则:9 V. j8 E: G* X+ l# a% l

2 O3 L3 r0 Y- b; V' N复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
- Q$ ?: `$ k+ C& l* l
/ `- M- E+ P8 e) w( G编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
% y0 q' ]# `, Q3 n
  M! w9 b, C! m  \7 g" rowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
" v, s. e! J0 `& a# o
  q) q, ]* B3 d3 e6 p; JInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf' l$ X3 r* ?. a/ u3 L4 J( Y" v, R
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
0 f5 z" l! j3 y7 I! pInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
: F0 G, y3 _% R/ Q3 {. fInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
! ~" m2 ^! |& [) U6 J* V2 HInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
% W4 t7 u* Y) r+ C8 [Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf5 \  W& _' P8 X
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
  A' b. R3 b0 F; ?' A- A9 A  l  i五.配置nginx9 q1 p0 Y4 u. ^: |  J  {$ I0 i. P4 D
9 x- A% b+ Q9 J: C+ [: u! y
在需要启用modsecurity的主机的location下面加入下面两行即可:8 u+ }, l0 Z# k, k1 _
6 |( P& p$ \+ L7 j* r; }9 v
ModSecurityEnabled on;  % O7 v: Q  r8 p" f: t
ModSecurityConfig modsecurity.conf;. V9 ^% j/ q. v% W& A0 W2 |
下面是两个示例配置,php虚拟主机:9 ]! u8 `1 d3 D7 D( z

+ `" x, {" I, o- `9 {/ |server {
, X- g8 \% @, L1 r      listen      80;1 ]/ p6 y# [' f; t8 x1 l
      server_name 52os.net www.52os.net;1 H- F, a& D: ~8 ~+ |; z
     
$ B% @& L; j9 i" E: v3 g9 s8 f$ h      location ~ \.php$ {9 N7 B/ ?2 {( Q4 f2 L$ u
      ModSecurityEnabled on;  9 q! j" {, |9 P; U; r0 r) X
      ModSecurityConfig modsecurity.conf;
2 @& L6 D5 J0 U8 g- ^
( J9 p* ?' x) a& S      root /web/wordpress;
! [# @6 @* `  N! P6 A! c, i      index index.php index.html index.htm;
: x4 f3 |+ M8 G  % |8 ]2 O) R) z1 o( n
      fastcgi_pass   127.0.0.1:9000;
$ q! b$ H5 T- }& v2 _      fastcgi_index  index.php;$ F9 U( n! E2 K+ N
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;  y* D2 P- j  u0 R! ?; o- u) G
      include        fastcgi_params;
% F' a5 Z+ L/ j2 Z% m      }
' k% s" t+ S+ L$ Y" k& |  }
9 ~8 |0 d! ?6 X3 b5 |' J& Xupstream负载均衡:4 u3 Z' S, {0 \( R
- W+ z$ \0 o3 m' ?- [
upstream 52os.net {/ S" Q& x9 ?( p, p& C6 E
    server 192.168.1.100:8080;% [4 [" t5 [, U
    server 192.168.1.101:8080 backup;, y7 z6 F( Z+ x2 O2 M: H( P) f5 y+ w
}9 S/ Q, L, y* R$ {1 }( x# J
! i1 @7 J  X8 S4 u" N
server {# H+ y' s0 I9 {5 ?3 F6 {; {& P
listen 80;1 V: R3 c  h8 a: H' p( V& G
server_name 52os.net www.52os.net;# H* `' u0 ~$ q* U, S$ [

4 F* t6 G  {  J; x4 G1 Vlocation / {
9 |! O' C+ @3 h3 _8 l    ModSecurityEnabled on;  % d4 |- f1 U( a: I2 c, X0 I) B1 U* T, F
    ModSecurityConfig modsecurity.conf;  0 f; u1 B% X4 ^7 x0 q+ t

/ K0 r8 D) Q3 {% a' n" N        proxy_pass http://online;2 G0 ^: z  z; V: w" q2 u
        proxy_redirect         off;0 I0 j0 @7 w/ z2 A) a. m1 v
        proxy_set_header Host $host;
% Z' I/ K9 l5 {8 K; ]        proxy_set_header X-Real-IP $remote_addr;
7 N! M( b7 d. T& p/ Z2 E) Y        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
% n) N, A' u8 I* }    }# s& }3 W1 @5 i7 |: j9 j2 ?
}  N2 |# e: r7 d& B$ i( D1 }
六.测试- G1 ?* b0 M" ]& H' s0 j: ?* b
) J" ~/ ^4 [# G/ c" W9 ~
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:$ k% R5 X: q; e8 A# N# I/ W8 x3 \/ w( Y
4 F. C7 o+ c- U# f
<?php
1 K4 H5 h8 ~# Y% P    phpinfo();    $ ]% k# y1 t1 W3 |8 [: g/ z2 M
?>
+ ]6 g. J& D, q( J在浏览器中访问:5 g$ @1 x- X( c% Z( c

) F* D8 @7 V  p3 s' S4 o; Mhttp://www.52os.net/phpinfo.php?id=1 正常显示。
% V$ C$ m+ m4 @# b( T; chttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
3 B) L- u3 w' ~& G9 O3 nhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。, ]2 G6 [6 d! o: }5 \  Z6 q
说明sql注入和xss已经被过滤了1 J6 a6 }1 b- W1 u9 B
! B# ]* t% f; d, B1 w" y
七、安装过程中排错
9 P; ]# s) D2 v# N" Y# h
! P2 n- x- r2 P1.缺少APXS会报错
8 @+ d& ~; H1 p- s9 Q; F- }9 ]7 o' m8 d
configure: looking for Apache module support via DSO through APXS7 o7 M' }- k0 @* n8 `- ?) Q! M
configure: error: couldn't find APXS
" {' i4 B; F# A, Z$ N5 b) L; hapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
3 H& n" Q' I0 W) {% E, n0 n* {5 {解决方法:1 |- H+ I' e1 F8 s$ y

  n: r9 B5 d7 l  f3 R) qyum install httpd-devel% j) R8 h4 [5 Q3 }. N
2.没有pcre
0 n( p% s2 |8 F$ Q) N- Y3 ?
+ I# C" K$ z  y3 b+ n, Oconfigure: *** pcre library not found.3 E: h- n* @' g- A
configure: error: pcre library is required: a% J* i6 v3 v8 ~" v! _8 A4 ^
解决方法:
0 r1 r% S( k  {3 J; v' w9 {. X  I# c% i8 X2 k1 s) [+ B) |
yum install pcre pcre-devel
; n9 c; d9 R* D3.没有libxml2
, K& |4 T! g6 ?/ i( b' G% W
; ^  g) V" h5 D/ D* i2 {
. V2 {- Z* s* G8 E9 E, Hconfigure: *** xml library not found.+ X: L4 U$ i4 [
configure: error: libxml2 is required+ p8 F- O7 M; Y& J/ e8 ~
解决方法:" y9 S3 c# D4 u9 I9 y
5 k$ _& p- p+ q
yum install  libxml2 libxml2-devel
! s" c2 h$ N* B8 g# f& m/ L4.执行 /opt/tengine/sbin/nginx -m 时有警告
6 h& n5 [  t* G' I' u- a9 j0 C/ H/ {3 W+ w8 f$ R
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
0 g8 c% Q: v0 D, r9 B  Gnginx: [warn] ModSecurity: Loaded APR do not match with compiled!6 @+ T) _) w5 G# g% v; x
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
; ]) `8 j' N# ^8 F3 l
+ A: ?' o7 D( N5 m3 G$ X: o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
0 _5 e5 _# R: s$ W3 H/ Z, e9 c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9". ?' P% C, w- c. B
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!4 u' m# y$ V+ H
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05". {! R% M7 B. f3 x( i) t/ r  v
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
& d( `" |3 g) l) C# b1 N& `2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
. N6 ]" x# r7 y: W3 |4 ~2 G解决方法,移除低版本的APR (1.3.9)
* M) q3 [2 j/ Q6 h$ r2 ]3 p# }/ s1 H8 G4 D. Q* ~0 b3 T
yum remove apr
/ t9 [1 Z  i& Y+ b4 p7 \. \, b  X& F5.Error.log中有: Audit log: Failed to lock global mutex
, Y" u4 ^% x1 }! [( I
7 f4 J: E, W- I; w) F+ x9 c3 t/ f8 A2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
7 I# E; X0 o- e: l% l4 G1 H8 w7 Qglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]- i, h7 e; v5 p& Y, L- L. N
解决方法:6 ^7 z; Y. h+ d2 r7 V
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:; Y" F. W- D) c: j8 o3 z( V4 ]
7 h8 J% q* O; l% ^) _& [: V4 T8 ~
SecAuditLogDirMode 0777
5 ?2 D! x8 D: ?/ l) M9 NSecAuditLogFileMode 0550
/ b, i* L% s- {SecAuditLogStorageDir /var/log/modsecurity
$ Z$ o5 i! n5 @1 {/ x( mSecAuditLogType Concurrent
' h* Z. z  D' q, R( H, l; K# E% q. N参考文章:
  V/ y$ o0 w3 h5 y" L( Y) O+ ]https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX* y( a$ g+ a# _! I: q
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-19 09:41 , Processed in 0.052994 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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