找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10099|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。' t- h' [5 D" M/ _8 A
3 c* P6 {0 A7 X# I
一.准备工作
( c7 Q) A4 C- T2 @* o7 r2 A
$ z. T$ l2 q3 [, s系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
4 g* w" ^* j& s/ C, s! e7 v0 v
& g  `1 f" d7 p# O1 E& Ttengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz& ?* O! o- `" ?; l

) H3 _  }4 n  {modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
; l+ j" q4 }# d  V( |& w% Q) T' C$ A4 k4 X" F5 y' g6 ?2 ]
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs) h0 W2 O" m6 s7 ~2 s) B& q

9 h4 E1 n. l8 H: t' q9 F5 S1 C& j+ x依赖关系:
# A" k$ N1 g9 D  @1 p" \tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:. Z; X1 a/ X6 G  B# ~$ d; @$ M% U
' i% x' D( P, k* ]1 q* d
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel& t" [3 n/ S7 D/ [
modsecurty依赖的包:pcre httpd-devel libxml2 apr+ ~& o# n. [+ ?- V- F: A0 ^3 J

' w8 G8 Q) ?, ~( ]3 Ryum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
) g; B# u, G; }" I3 X) E9 n* F二.启用standalone模块并编译
/ o7 r- O1 [+ a  }. |) T" ]) `# V, L$ M8 I4 |4 f1 s# Y
下载modsecurity for nginx 解压,进入解压后目录执行:, X: M% w9 z/ A
# V. M; Y1 J" \
./autogen.sh
7 X( `' N6 O; _5 H% _& V- u./configure --enable-standalone-module --disable-mlogc
  e% E2 F/ W& C. Gmake * e9 \( J3 V* F) k+ B8 V
三.nginx添加modsecurity模块
0 t: D0 Z& M, R, W2 F- i; e
' g& L1 a: s, p; _4 l. q" [在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
7 `$ c5 u- j& F4 w) s8 u/ g# c, Z& i, B- g% I+ }0 [+ Q
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
+ v3 s4 c, k. ]; @* ?/ ?make && make install
  Q; @& \2 [$ X+ m* I  e- g! w& C四.添加规则
: Q( H# b- C7 H2 U% j+ b9 r$ t; q$ s% v# r6 U8 f+ x" c7 r
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。6 g4 D1 X( q9 w" E6 @* P
1 ^/ Z  t' x, ]' x/ B
1.下载OWASP规则:' S5 n4 n* @( x
; X6 ^& {( _9 R0 N2 ?
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
$ ?3 A% u! s9 @
5 ~3 q, l# ~) c# N2 W7 F% g0 @: Umv owasp-modsecurity-crs /opt/tengine/conf/1 o% S/ M% U# m
& j, [6 W8 [1 b+ K: i" R
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf8 d# g7 ^$ d/ U7 s$ _3 V
2.启用OWASP规则:3 a  W. L9 T: ?$ N5 f# l
" u) _# I* H: O8 D! G
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。1 D  x5 S6 L) O2 S. G' a/ F  p
: `1 R# m" f4 T/ ]; H
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
- ^& `) j8 Q. ?- I* ?) s/ m" D; t/ G/ h$ y, P2 h
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
7 B' P  J: S4 H1 s5 o) D/ @) H
+ I2 N. z- |5 J( D% o7 X) W, q2 HInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
' y$ }- g, m% @: k7 C+ AInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
6 u- @  L1 I5 A- ^/ GInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
" g4 e+ j5 g6 F4 u! M0 T2 {3 XInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
( S6 e0 R' m3 F* s* M* T% vInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf9 \, f6 _; q8 H7 Q# A
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf; _. [* Y$ V' Z" l- }- T" P# l
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf4 c6 i' o& I4 @& K( e
五.配置nginx
8 i3 E& N) p: K( |- @
" @" o% I( n" h在需要启用modsecurity的主机的location下面加入下面两行即可:
' T. L9 U$ ]. A) b, t, g1 }0 |* e2 R
ModSecurityEnabled on;  
8 O$ y( e2 Q% V4 q/ q& X, [) J! cModSecurityConfig modsecurity.conf;
! T) t2 n4 w0 I7 T) a' u" q, \& R下面是两个示例配置,php虚拟主机:5 K5 a- E* L$ q0 R

" ^" x; D* d9 x- S+ userver {3 L/ M7 x  x1 s9 V* A* n, `$ J
      listen      80;
$ c' m, @  q& m. k      server_name 52os.net www.52os.net;
. h& [! q6 i9 R* ~+ d! I     
, j1 C" X* Y4 p& ~  s% z" z+ \      location ~ \.php$ {
) g$ `& G# B, m3 Z      ModSecurityEnabled on;  
' D6 g4 g' \0 Z! L      ModSecurityConfig modsecurity.conf;* _! {( H, p0 L  n% v6 d# g; }
9 a% c2 S6 I$ e; P& k2 R8 {% o9 G
      root /web/wordpress;
  ^# d: k" ^& \3 A; r7 n' \$ K      index index.php index.html index.htm;
: y7 L; u: X& F& L/ h$ z7 s  
) z/ _' r# a! _  g      fastcgi_pass   127.0.0.1:9000;3 k& @0 Z8 @: z5 E/ m3 U# }/ ^+ D. Q
      fastcgi_index  index.php;
5 d9 L6 n" x* m; |0 _      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;. P4 g1 w) `2 H) e
      include        fastcgi_params;, @; y/ }! g' @$ K5 T
      }
! O, u+ H6 t2 l1 A' G* l  }
2 Y1 e1 H2 z4 q8 t' l( V. E3 f- Yupstream负载均衡:+ j- u3 o8 }4 ]# Q$ I

, V9 |; H. W6 {) `upstream 52os.net {
, g+ M& N6 ~2 D; U% W! @    server 192.168.1.100:8080;, C7 B5 |) i! Y- }- ~" B
    server 192.168.1.101:8080 backup;) Z0 L/ t$ `4 K  y- _/ a$ |/ f
}
0 c! Q+ d; R) A$ s+ g5 M$ z# J% a
2 {0 G- A- y! Y+ Q0 C( Aserver {
6 Q3 o* v, @; z3 K5 p& llisten 80;
5 p* Y/ y" q5 O" A# Bserver_name 52os.net www.52os.net;2 m" J* j' Y1 ]3 Z  D* A
" c6 d. W6 Z" S3 S/ I
location / {* G. x# b9 _/ r' H2 `$ r2 c
    ModSecurityEnabled on;  0 E+ f* m) i7 R5 T$ w
    ModSecurityConfig modsecurity.conf;  0 B) S+ m; R6 y' C/ ~1 \

' W5 d. s. L6 ?4 t        proxy_pass http://online;
  U" Z% S& h: d        proxy_redirect         off;; B$ }" P7 V/ c: x5 V
        proxy_set_header Host $host;* ]" P% n# ]$ y3 a7 `
        proxy_set_header X-Real-IP $remote_addr;
. y$ O5 p$ f1 E/ |# \        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
( L5 X1 N' i! A    }
( t. _( |9 d' g+ Z}; t3 u: D* p4 d+ f
六.测试
5 ?9 s9 [% O' J
6 {3 T* z# f/ y# }1 i8 I$ p* e! m我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:* |; H+ U, H. ~; e3 F0 h

& ^2 \$ C5 e+ {% a0 o. ?<?php
8 B, e" |& @# V) i( \    phpinfo();   
3 o$ b/ k7 M( ~?>
& A% |0 {4 m+ |. c在浏览器中访问:% ?* c0 g5 r) X9 j* Z% d6 j
$ F: D3 e' s, A
http://www.52os.net/phpinfo.php?id=1 正常显示。$ H# Q& r; c  U4 I  j6 k5 {$ t
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
5 `1 C/ p# j" F1 ?! b) G* B+ R3 qhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。5 i/ D, U" f4 G5 x+ K8 }
说明sql注入和xss已经被过滤了
5 T' J  D' _# r) M6 a
( m- A) j) h9 ?7 a0 T, l七、安装过程中排错. H3 E( j2 P) ^/ q+ ^) ^* H4 ~

& [; \8 B! I$ k* q1.缺少APXS会报错
3 m( d6 X& l- u/ x
6 e- A) G: O$ d; j4 y6 X, pconfigure: looking for Apache module support via DSO through APXS
5 u9 z9 n" H9 l0 f/ e% econfigure: error: couldn't find APXS
+ W" G" w/ q* u4 gapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。# H: I6 |' e6 c9 y/ n
解决方法:
% }1 `6 S# u9 F/ t# h! P0 G
! Q# w: l5 Z0 J9 h. Cyum install httpd-devel1 @7 u( Q7 q+ i6 e9 f8 M+ Q
2.没有pcre. V' b! h1 z$ l9 c

% Y( u: n5 R3 H4 v: L7 ~4 ]- vconfigure: *** pcre library not found./ y; a, R; P3 U( ]. V& D% O- i7 w% L
configure: error: pcre library is required7 ?. }2 d: v1 D8 Y% o4 k
解决方法:8 c" ~$ t- s- k

! r; A8 V% ]% O# Z: A$ T& Dyum install pcre pcre-devel+ L+ C* q" M. K* X
3.没有libxml25 G) S; M/ F, g! u1 s3 B

' `, g# Y& u* h& w: T
5 T/ K. @5 `0 R* Dconfigure: *** xml library not found.! Q% z, Z+ d1 {# g9 n) ^- {; v
configure: error: libxml2 is required! w/ U1 v# |5 ~) _8 c5 a0 u1 c
解决方法:; _0 R1 \8 X5 d% |

' S' v7 i1 |! N( e1 q1 }5 hyum install  libxml2 libxml2-devel
( y' W0 D/ z. D9 Z8 k, D7 j( r5 c" P4.执行 /opt/tengine/sbin/nginx -m 时有警告  t; v7 P7 K3 B7 Y6 a- w* `0 q

4 J! I3 A1 V# g" jTengine version: Tengine/2.1.0 (nginx/1.6.2)
( ^' @: {  w( _4 \2 f1 w! ~nginx: [warn] ModSecurity: Loaded APR do not match with compiled!  V9 E) x$ f/ `9 R7 o
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
0 t3 P$ j  I, \  L& H9 J, g, o/ R' H, k9 ]5 \1 ^
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.1 @% P" c' e3 A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
0 K  `0 e% Z5 ?9 i7 L- B' Z1 A2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
( o9 }' R: i4 l# C! o0 {2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
1 q' Y& g( p) T8 T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"4 J* V) `7 M3 {
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.! t: Y: n4 c' J& G$ s  i6 p  ]- N7 l- Y! {) O
解决方法,移除低版本的APR (1.3.9)
8 e9 {6 E8 s- q5 p) m0 m4 ]
. w' N7 u  o4 x9 byum remove apr: T' J! v5 z+ ?8 v+ c
5.Error.log中有: Audit log: Failed to lock global mutex
; G! I  A! s9 w, c2 u
; P8 s# g  N2 ?# j" `( \2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     $ s! P( R2 O* M% ?9 [; f
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]% Y0 Q6 }6 I# V: h- @' Y9 O
解决方法:8 H1 j8 }% {. J2 \; [
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
0 L5 ~' R1 V2 ~4 ~3 o% ^$ X, y6 R5 ?& x6 D/ R) Y) B
SecAuditLogDirMode 0777
! k/ b( T3 H- \SecAuditLogFileMode 0550
- m& i# Y4 r/ c: {& l" [) D) n+ ASecAuditLogStorageDir /var/log/modsecurity
7 ~1 P6 T- }: VSecAuditLogType Concurrent
# S% r1 t- K1 V+ q' C0 T参考文章:
2 K1 e8 C$ G' Y) P) g) _8 F! [https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX* c- W5 ~1 S. ]" R. _
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-3-1 00:25 , Processed in 0.080796 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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