找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12471|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
7 A1 ~% `# K- y6 X3 }8 A& y7 n
% b; k" q9 G3 G8 F一.准备工作* a. n7 Q* a  H  ?# Z$ t

) b, m8 ]3 T" ?% F1 _6 f, J2 v( _系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
5 q$ `! |; {( B& k- T& [1 s7 H  }- m0 @) q* O% ~  T
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz  H. h$ M, M" ~& W; Y
& v2 G: m- P  R5 m
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
- N4 b  _2 b9 k. R! J2 x4 y
$ Z) w( `: K0 z) k0 N: R8 dOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
4 o" \4 e( C9 s" T1 j" W6 a  R) {* L, j, w9 t) ~& y* n
依赖关系:4 m) U& {0 c. s/ K+ q
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
  h9 n4 j' \. t7 Y: C2 \. K5 z, g) g6 J
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
. s2 {" T- h2 e6 ?( i. _modsecurty依赖的包:pcre httpd-devel libxml2 apr
6 [* I4 I/ R0 S# a' V  E6 E
+ o" C3 ]- V) w+ o, b. ~3 t/ }yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
  I; B; J; s* \5 y) \二.启用standalone模块并编译& M2 s# e% y: |: D8 f; F

% h' n4 |! [6 r  s7 W下载modsecurity for nginx 解压,进入解压后目录执行:; V) k" z/ i0 x! s

  h1 r5 P7 U$ g: l./autogen.sh
: Q6 U) v+ @7 j: o./configure --enable-standalone-module --disable-mlogc
* j, ]9 D3 ^& \  O: Jmake 3 x4 P% O4 N. R8 H
三.nginx添加modsecurity模块
3 u, y( M6 g% i& a6 X
. H$ X, e6 K' M2 H在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:: [' d) l7 y$ |* c- b( B1 m
- V5 u7 p  n  J; y5 p3 q
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine* J3 G; l  i8 F* |2 [# g
make && make install( A- Y" P6 T' P, J
四.添加规则$ Y1 D8 X5 T/ @  \3 ~

9 {# S/ i1 d- t" T' D# P0 r/ \modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。& D. f$ \5 o" j- D) Z. r% Z2 k
  p# |& l5 h7 J* t' }7 ?! v
1.下载OWASP规则:! {) F3 G. p5 ~4 w- o% N

3 f3 o3 w/ q. f- Z3 a9 ggit clone https://github.com/SpiderLabs/owasp-modsecurity-crs+ J  g0 P8 Z2 N9 t: W* l4 k# ?

$ P$ j, r* D& Q9 Z( gmv owasp-modsecurity-crs /opt/tengine/conf/3 v" G" W- ^  J( E/ O1 {. U
, Q- g* T& m) j) I3 T. L8 W+ A
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
0 @1 p1 e, b9 i$ s2.启用OWASP规则:
7 a; W- k( {3 R# G* ]- b) [* i
# M, g1 a! a6 x, E- d复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
) z. u8 A6 m) R, j$ n/ H/ d& ~) e8 }) p- |
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on% N4 y6 [% x6 y& ?) T/ B8 M; E5 [
/ w4 t# z% W% k! N; e
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
' S' K+ n5 x- Y* V$ p- o6 ^4 M
1 {% N) g  S% G) o' T1 wInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf) M2 i: m( r8 _! A# W
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf/ Z. ]6 m& W0 _5 L: @7 f
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf- r! j& Y. r4 Q2 J8 B. d0 k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf/ Q# w' @1 o6 `: U2 T
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
' F1 S( M9 @9 ~1 K, X; G( L% hInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf1 Y2 {9 ?5 K% v2 U  H
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
7 O" P- b6 s8 i4 D五.配置nginx
1 K8 O% k6 \; y4 _9 J3 j9 O/ l' V1 g# U( C+ f& c4 i
在需要启用modsecurity的主机的location下面加入下面两行即可:
) e9 e5 R* q6 G: h3 Z( t5 _& u* |
% j1 z3 J0 n% K8 s2 {ModSecurityEnabled on;  , @5 o9 H" Y8 l4 V
ModSecurityConfig modsecurity.conf;
0 V" d: i, ~' B7 c& B下面是两个示例配置,php虚拟主机:# e0 G* I: g; T9 d+ g1 K9 E
, n1 H; D' a2 S
server {. U& q$ p$ B$ I1 a, @0 {
      listen      80;. U: r7 p( S; s/ h
      server_name 52os.net www.52os.net;  B- q- x. g! q
     ) S8 J0 s6 h' j% {0 D
      location ~ \.php$ {% U6 f! l% U6 }
      ModSecurityEnabled on;  8 w5 ?6 ]2 X' r7 O( R) R
      ModSecurityConfig modsecurity.conf;% m1 s2 Q0 o# T5 d5 @. u) X4 _) v

  {3 e! O4 q1 _      root /web/wordpress;7 ^( h0 F8 ]7 B% \5 u
      index index.php index.html index.htm;1 A; v, m# Y2 ]* K
  6 x/ y9 p' l0 r
      fastcgi_pass   127.0.0.1:9000;
# k3 J/ h; J. b      fastcgi_index  index.php;
7 T  @6 ~0 X' @5 Y6 f# t: a      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;# |4 m" Q! d" m: H1 l: J
      include        fastcgi_params;2 T4 @' e* \/ \* z6 M' Z
      }
/ ^* j& r6 q- B' S" v1 G  }
  O" R3 U: l* H) r( ]4 G, C2 Supstream负载均衡:9 _9 s+ a; X' v  l
" o" k- h# e8 p" W
upstream 52os.net {
; H3 h+ D1 K" J& O    server 192.168.1.100:8080;
4 _0 q9 m) k* S5 X7 [; v  j    server 192.168.1.101:8080 backup;
! d) d0 d+ w. ^1 h5 c}
. @: M$ G8 X( C% U
0 W: @% K" \3 D) s- J) _server {6 D0 U% D( m) z5 h
listen 80;
$ t' d: _' Z1 J; O8 H4 Qserver_name 52os.net www.52os.net;
' n6 y6 B4 P5 d$ Z6 M" d9 n" t) Q% Q4 d; Y
location / {
% N: ]9 j' b9 @    ModSecurityEnabled on;  
* ]: k, j2 u( y* i& G2 h    ModSecurityConfig modsecurity.conf;  1 V( E4 Q9 o) t5 m2 B3 B& U- W

+ E: ~* j9 F  k* Q        proxy_pass http://online;6 i# h" e4 j( E9 i5 C
        proxy_redirect         off;
; H1 X% ]4 g. u$ }        proxy_set_header Host $host;) e# s: x; {4 a$ p% k8 R: M
        proxy_set_header X-Real-IP $remote_addr;
5 s5 j7 a5 Q) `) @2 O2 x4 R        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
7 i' m3 p/ x9 V  R5 F/ k7 Z- C    }$ G# @  I6 S6 Y* W
}
" j. E, ^5 ^7 ^: X1 c/ @! R4 E六.测试: J4 a$ @6 T: Y* X: d2 o% [

+ {! V1 M% y( T7 [& Q* Z: U) b2 f) y我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:6 _4 Y- ]  D& R
; `# y& ?: L1 v/ h( a" g* a
<?php/ z1 i' ?+ C- J, q' m5 z" G6 p
    phpinfo();    8 F8 p8 J* ]% N* U# h# `% S0 O
?>0 x7 s# B% V4 C$ _, g7 [$ r
在浏览器中访问:
, B+ o& J% e# z2 |
- y8 s2 F2 u- ^8 ?) yhttp://www.52os.net/phpinfo.php?id=1 正常显示。
# ]# D! e" j; E  b' i, lhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。6 a! `' I5 U/ o+ p
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
6 Y1 N3 z- a# S2 B& O( s+ {0 U% A4 q说明sql注入和xss已经被过滤了
9 S. [8 A0 Q7 N2 `' ?" h. O, I) \( m) I& @& A+ I4 N
七、安装过程中排错
, N% K  t, B6 {7 u. ?6 S. x. M
) t" n9 h0 o$ L2 a8 A$ k( w( B5 }1.缺少APXS会报错# e" j* [# u; u3 y+ ~

/ H* I! z' m, u, k7 L# t: `& U& ?configure: looking for Apache module support via DSO through APXS! P6 W/ V" [' @4 V3 r+ O
configure: error: couldn't find APXS
8 y2 a6 t0 ~# h& a: `8 D$ iapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。5 t! O4 C0 _; @6 M; e
解决方法:
3 a% R( A& Q% u% P, y+ H
! w) ?" K+ g9 ]* `/ V, c) oyum install httpd-devel
( U% c, y* R  D/ Z( A2.没有pcre
; z5 U4 w; p' C0 I% R9 E6 K0 P& `0 l: ^* E$ Z% L
configure: *** pcre library not found.
0 z/ W( z5 ~' w5 p$ n7 B7 mconfigure: error: pcre library is required- g6 _2 a' D- l5 {9 O
解决方法:+ t( a( t! `  l

, x; v* e) R/ Y8 v* |- wyum install pcre pcre-devel/ s5 v! d$ ~4 o9 W& ~- d3 C
3.没有libxml2
& j3 @) S2 C; ]& i# \/ p( b# V9 w6 @/ I. g  S, Y1 O
7 d6 H- o, I0 l9 L% G
configure: *** xml library not found." r" z& l8 J* G% B1 j. F& v
configure: error: libxml2 is required# ]+ x; w6 `  `6 z! l
解决方法:
% R2 `' B1 ]; r4 `: A/ I$ Y0 K& r; o
% ~  k0 y/ q3 C( N& s  t2 cyum install  libxml2 libxml2-devel
& w" U5 ~# j. b1 M4.执行 /opt/tengine/sbin/nginx -m 时有警告
% [5 p0 s+ Y  U4 |" F" V1 S' z! p0 N# q  z9 g; O3 {
Tengine version: Tengine/2.1.0 (nginx/1.6.2); g0 d0 r/ C2 B2 v1 D
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
9 {3 A. S% w+ f. n原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
; ]. q4 A" `) e5 Z/ R/ d. j
) I" D- y. k+ u2 p+ t2 A2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
# G. ]& \3 F. x+ O' g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
3 N" b7 l$ L; ?6 V; X. ^7 O2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
. P& ^' d* v+ e! g5 S9 J. B) S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
3 q6 |6 z8 ~& [4 b! p1 t! b; D2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
2 ^$ B: |5 y7 U6 k( J: R! T: _, G" H( S2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
9 Z5 R' {3 k6 X, M+ ~4 g0 b' q解决方法,移除低版本的APR (1.3.9)
; P' v8 v# x- S/ m3 Y& N5 k
9 J; C0 S2 M) y) Tyum remove apr8 l; {: X7 [4 A
5.Error.log中有: Audit log: Failed to lock global mutex& Y/ z" X/ g( J* l/ ?% J% |

- q+ ~" ~" `5 q  ~$ f2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
9 b8 g! m0 L, B& [  F' ]global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]& _1 c5 O: H* p8 b# q" L4 R
解决方法:3 D2 f$ D' z% a' l  ^8 X; |
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:, N4 G# [+ ]/ L7 E) ?1 v% o# D2 G

  x( M5 l" _# k" G0 lSecAuditLogDirMode 0777, ]- {  i6 w0 z
SecAuditLogFileMode 0550
6 ]* h- [% y6 e* l* NSecAuditLogStorageDir /var/log/modsecurity5 j* X& o' k4 k! g( r1 U# \
SecAuditLogType Concurrent* v* k. Q! }5 v6 {9 R2 m
参考文章:
. _3 \) Y  f6 S+ B3 Nhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX) t5 k$ j- W, J" `* u
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-8 19:54 , Processed in 0.069393 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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