找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12691|回复: 0

nginx配合modsecurity实现WAF功能

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

1 r" @9 l, m6 N: R8 x( N9 r一.准备工作
' C- w2 }' n1 d
! A; {" A+ E- r' Q; V系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0* T, m2 n% _; g* V2 h% X+ t

8 ~0 b& c" b0 |1 Vtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz" v5 g7 e! v% B; B- X6 A7 k

: f9 V6 y. `3 `2 Lmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz8 H+ u2 s6 U$ Q0 m

* d! U* r) J; V# h: q4 w: ~$ zOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs. _9 Z5 D7 ~+ x# T; J/ }0 p% o) r- A
9 d1 O' }. e6 A- X; z( r
依赖关系:; S4 }3 v) ?4 x4 h- `% e4 C2 d1 _- c
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:; u& [; |: r' x$ E

; V* G% n! h" A4 X* tyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
5 J2 F; s1 L! ~+ J6 tmodsecurty依赖的包:pcre httpd-devel libxml2 apr
* Z; I* L; n0 ~, f+ Z# e7 O$ V/ k1 ?9 p9 R" L
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel( G# C3 k* f# y- @$ K
二.启用standalone模块并编译0 E9 @% H* s/ C, ~$ V  u

- X5 O. b2 H5 P/ M6 V" M0 b下载modsecurity for nginx 解压,进入解压后目录执行:
  _& \% C2 E. x
6 {, u3 E6 O* T./autogen.sh" n% V' H9 g# s3 b# u
./configure --enable-standalone-module --disable-mlogc) }5 q4 C  A4 h3 E( u* E
make
. T* a, t9 Q5 N5 y# U三.nginx添加modsecurity模块  w9 U) Z1 K+ t6 R) Q( j

# z8 D8 n9 ^6 Q: M, T在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
, S1 g  L" ^3 R1 Z
# A- p6 i6 z1 D, a./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
4 z% }. t9 x) ^; @3 _- gmake && make install3 R+ P+ M8 \& m2 d6 ~4 V2 }
四.添加规则
- @! H# q9 g; o' t8 D
6 ?4 ~# O% E2 |: @4 i- smodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
' A2 _" p, {( Q: s; ]" ]
4 Z6 q8 [1 j  \  a0 [% g1.下载OWASP规则:3 X6 W: u" ~& ^6 d& l9 C3 \4 C: [1 Y
& o- o& Q4 R/ e! {( Y
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs3 l/ Y9 K  K% c
: R% z  [; m! A. U
mv owasp-modsecurity-crs /opt/tengine/conf/3 L7 P; F$ |5 ~  q& }

  u+ }/ h  V, t8 M: O4 F) ycd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf9 x1 m: l/ @4 B1 E6 S, C
2.启用OWASP规则:9 S( M4 r& x% ?' t  T' m' r

) w- v- Z) e; [* w7 c# P3 O复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
6 K, H% o5 v  n0 Q6 {7 U) q  n6 A0 Q( e: k1 ~; k) f
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on2 G" `* W1 ^1 K5 G! q
) s9 B$ G2 q" ]! H
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。+ g/ V) V; P/ |+ _4 ^) C& c

# }4 h0 @8 b; ^# G) g0 C# L3 CInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
9 W* ^; u5 N$ }! @Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf- {% y% v5 C- t- T6 i: I+ K8 r
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
  O5 U. b/ Z, Z" y  z- u3 r, O+ CInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
& _$ ]" {  P. i, b! [5 _' xInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf3 l5 p! e/ q% Y4 ~% r
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf2 G; V' d# O. M, t0 u
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
& ?/ f3 u- r' x1 B五.配置nginx
7 o5 R" u5 R7 ^4 p; w$ U8 E( y" y; h
在需要启用modsecurity的主机的location下面加入下面两行即可:/ a, I& v( S" \, B  e' c; X
4 s, A  p$ b9 v6 S- r2 Y
ModSecurityEnabled on;  
+ \1 a9 S" R' X' r2 zModSecurityConfig modsecurity.conf;
. [) R  a0 v) o# m6 h8 N下面是两个示例配置,php虚拟主机:
$ H! j, r3 q7 p; h4 k) E
7 p3 q4 F6 O# cserver {; L0 h' I7 H8 [
      listen      80;5 b2 b/ a, H( P3 _1 x
      server_name 52os.net www.52os.net;. N; @+ }& B4 ?4 r# H  A# Z7 f2 ?
     * E0 n8 s" Z# ?6 w- a  w& \, E
      location ~ \.php$ {0 W  C6 {4 U2 {
      ModSecurityEnabled on;  
5 L9 Y  f% U/ P0 h      ModSecurityConfig modsecurity.conf;( d4 m- P! I: c0 a6 p$ d
$ l$ s* ~. |2 O( ?* M" u  C. A5 M# y! `
      root /web/wordpress;' A, d! v1 s0 O
      index index.php index.html index.htm;
- t$ z* l, R0 {& R  / f2 R0 F* K! R; }5 w
      fastcgi_pass   127.0.0.1:9000;
4 b9 t" g4 l0 s" f" l2 P: v+ s      fastcgi_index  index.php;
+ x1 z1 {: u4 O" h" F0 B) _1 D      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
5 u% F+ @7 I# s, ~9 Q2 w      include        fastcgi_params;, T8 _+ k) x% i+ M' f
      }
5 q7 O( E3 X2 w7 E, S- h1 x  }' e0 i, E+ U0 a8 ?
upstream负载均衡:' H- S! M- q; R4 D
' Z) G+ w5 M4 c
upstream 52os.net {
  t+ Q2 Z; `9 D. a$ P2 J; ~$ k    server 192.168.1.100:8080;" a+ d6 x5 l9 K; z7 n' U7 B7 |  S
    server 192.168.1.101:8080 backup;. k7 c/ t4 R/ Z; d% L, d
}
8 s, ^5 K0 S, o! A7 E( M! o) k7 k  T  s5 O  ]$ Y
server {: P6 B7 X+ J8 v6 n# }
listen 80;3 U* j3 ^: @2 N( Y) X7 V  @. o; q( }- ~
server_name 52os.net www.52os.net;
8 A! K, h% U4 Q: `3 }4 A9 Y) G5 O8 x, a% H/ v9 v  J
location / {6 |) q! Q; X$ N
    ModSecurityEnabled on;  
; O9 v( O& A# B- W) j3 Z    ModSecurityConfig modsecurity.conf;  + Q1 J2 l% N( C% f7 F. O: N
$ a  K( `, v/ D' o: \  t- Z
        proxy_pass http://online;
; P; Q3 F8 w5 `, @( a% P. g4 D. X2 O/ Z        proxy_redirect         off;
. m5 M& l" z1 ~        proxy_set_header Host $host;7 }, Z0 J2 x5 i8 Y
        proxy_set_header X-Real-IP $remote_addr;: M6 e9 W0 @: |3 u3 [4 E/ ]5 q
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;5 Y- p% Z# K. B+ Y8 J! I( |
    }* m2 W; Y5 |* s% u( B# Z. R. q2 f
}
+ b" a# O! w) P. F2 Y/ k( {六.测试( _* D6 ~8 I+ I* h. x* a0 g. _
8 \2 s. ?: t$ h8 a5 G2 P
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:- z; g4 h9 ]' C  o
7 O, |- j' }. K
<?php2 _! X8 a3 M5 t' {' W8 n
    phpinfo();    5 R  _# n4 F" O; P7 @
?>
  Q; D* o/ ^4 j1 d1 M. H在浏览器中访问:# Y, ?) S& M. F. K5 @) L' H0 F' k

2 ]4 B  i  P8 U; shttp://www.52os.net/phpinfo.php?id=1 正常显示。
- C" H# J# O" _+ w! P, \9 ?http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
' c" M$ t# U3 ?% Phttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。$ J1 t1 {( S1 k5 o- G& D" H1 v$ l. V
说明sql注入和xss已经被过滤了
1 C* W1 d' l: H# E
+ X/ f% x- i# t七、安装过程中排错
+ _. k0 U9 N+ G) x  w' D
% v9 f5 n# L6 X9 F* f1.缺少APXS会报错
  K" L/ U/ E7 Q9 F. n6 l$ N4 \  i1 n1 i5 @5 D
configure: looking for Apache module support via DSO through APXS
3 X$ F6 }6 B* T8 g6 K: mconfigure: error: couldn't find APXS
- \9 ~. ^0 q7 d! {apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。5 B4 z5 y+ ]3 c. ?3 }8 _. J
解决方法:- c- l/ Z- h2 b/ f
* ^# B8 }4 X5 B, x7 {- r* ~4 H
yum install httpd-devel
6 r% h+ ]" Y; F8 v  f) j+ `  \2.没有pcre
! A" s5 W) F: u% a
! f2 Y8 [- Y/ Q: Bconfigure: *** pcre library not found./ w( V2 _" J! B# N
configure: error: pcre library is required' T4 h( k2 F9 ~; o2 R, F& e
解决方法:) ~2 _0 }. d8 _" o4 F6 ^2 ]
1 h, B3 w1 y! k( i6 T, p/ d+ ?
yum install pcre pcre-devel
0 r' o* k2 Z, ~8 T" |9 f3.没有libxml2
8 Q& T7 s6 E1 P; X
7 v% V; B# V2 A' @* Q! }% M) K% d$ l. @+ Y# R3 p6 Y/ X
configure: *** xml library not found.
0 y- r8 x9 ~4 ]" x3 y' Y. }$ Gconfigure: error: libxml2 is required
; z% a/ |& V7 v" i" Z2 p$ K4 L% U解决方法:
4 q. V. w/ p$ l+ Z; o- ?. I- F6 @$ B/ J$ V' v! j% B
yum install  libxml2 libxml2-devel
/ Q) F7 A- G# W3 {, a4.执行 /opt/tengine/sbin/nginx -m 时有警告! [1 v/ ~1 L3 P) i
4 y/ d& N% s, Y5 J" `
Tengine version: Tengine/2.1.0 (nginx/1.6.2)- O1 d# y3 p3 G1 ~
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
( g; `# Q: K, {# y2 |原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log- x5 O* r! C. B8 }
+ t2 `9 W8 @8 |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.% Z- ~2 Y7 E1 L) K& v7 i% b6 O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
( t7 C" _( f2 \$ F2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
5 @2 B1 d, N" C: z) s, N6 H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"/ F0 r  F" `7 L5 ]
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
8 c' M' k. M: ?1 ?& h0 J; S2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.) L% @8 T- I- H
解决方法,移除低版本的APR (1.3.9)
, C  U5 X* V; i' ^- T9 [7 u0 h$ v# k2 h
yum remove apr# u( P- `" M8 V
5.Error.log中有: Audit log: Failed to lock global mutex
8 ?. n% g# K& l0 u! i' o' ]7 @9 `# p; O# I+ }; O  Y
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
5 U& D/ _" P8 Q6 yglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
% F* V* M. ^* O8 c3 X解决方法:/ M! h; a: @/ [  D* z4 \
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
1 p$ a  ]. D; ?5 f6 U5 q3 c& r- G) a3 F
SecAuditLogDirMode 0777$ l# Y  K6 R* L* D9 I
SecAuditLogFileMode 0550) ]. n0 p% `) ]9 q/ Y# ~
SecAuditLogStorageDir /var/log/modsecurity5 v/ h: D( Z1 ~( U: }
SecAuditLogType Concurrent+ T: L1 q& D/ b' q6 Z2 A6 J
参考文章:  I  I* e. O' l3 p$ Z3 h( Y2 k" K
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
$ N7 ]  u& D) k2 `' E  Fhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-30 02:38 , Processed in 0.064422 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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