溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何構(gòu)建基于WAF的s3cmd安全體系

發(fā)布時(shí)間:2021-12-30 16:26:24 來源:億速云 閱讀:119 作者:iii 欄目:云計(jì)算

這篇文章主要講解了“如何構(gòu)建基于WAF的s3cmd安全體系”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“如何構(gòu)建基于WAF的s3cmd安全體系”吧!

需求描述

有線上項(xiàng)目需要對RGW的bucket的訪問進(jìn)行白名單控制,只允許白名單內(nèi)的IP去訪問指定的bucket,簡單寫了個demo,基本思路是通過openresty寫一個WAF模塊,去實(shí)現(xiàn)設(shè)置bucket、IP白名單設(shè)置。

基本原理

OpenResty 處理一個請求,它的處理流程請參考下圖(從 Request start 開始):

如何構(gòu)建基于WAF的s3cmd安全體系

幾個關(guān)鍵階段的簡介如下

set_by_lua*: 流程分支處理判斷變量初始化

rewrite_by_lua*: 轉(zhuǎn)發(fā)、重定向、緩存等功能(例如特定請求代理到外網(wǎng))

access_by_lua*: IP 準(zhǔn)入、接口權(quán)限等情況集中處理(例如配合 iptable 完成簡單防火墻)

content_by_lua*: 內(nèi)容生成

header_filter_by_lua*: 響應(yīng)頭部過濾處理(例如添加頭部信息)

body_filter_by_lua*: 響應(yīng)體過濾處理(例如完成應(yīng)答內(nèi)容統(tǒng)一成大寫)

log_by_lua*: 會話完成后本地異步完成日志記錄(日志可以記錄在本地,還可以同步到其他機(jī)器)

本文的原理非常簡單:通過設(shè)置bucket和IP的白名單,在access_by_lua階段對request里面的host和uri等字段進(jìn)行規(guī)則匹配再決定是否放行。

nginx配置

配置文件路徑 /etc/nginx/conf.d/default.conf

upstream zone_write {
    server 10.63.48.18:7480 weight=13;#對應(yīng)后端RGW civetweb節(jié)點(diǎn)
    keepalive 30;
}

server {
    listen       80;
    server_name s3.ceph.work *.s3.ceph.work; #endpoint對應(yīng)的域名

    location  / {
    proxy_ignore_client_abort on ;
    proxy_http_version 1.1; #指定http版本,減少低版本帶來的安全隱患
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
    access_by_lua_file /etc/nginx/conf.d/access.lua; #WAF腳本
    proxy_pass http://zone_write;
    }
}

WAF腳本

腳本路徑 /etc/nginx/conf.d/access.lua

local uri = ngx.var.uri
local client_ip = ngx.var.remote_addr
local host = ngx.var.host
local endpoint = 's3.ceph.work' #endpoint地址
local white_ip_list = {["127.0.0.1"]=true} #IP白名單
local bucket_list = {["bucket1"]=true,["bucket2"]=true} #bucket白名單

function get_bucketname(host,uri,endpoint)
    local bucket_name = string.match(host, '^[%w-]+.' .. tostring(endpoint) .. '$')
    if (string.match(host, '^' .. tostring(endpoint) .. '$')) then
        if (string.match(uri,'^/$')) then
            return
        end
        local bucket_name = string.match(uri, '^/[%w-]+/')
        return string.sub(bucket_name,2,string.len(bucket_name)-1)
    elseif bucket_name then
        return string.sub(bucket_name,1,string.len(bucket_name)-string.len(endpoint)-1)
    else
        return
    end
end

if true == bucket_list[get_bucketname(host,uri,endpoint)] then
    if true ~= white_ip_list[client_ip] then
        ngx.log(ngx.ERR,"Forbidden:",client_ip)
        ngx.exit(ngx.HTTP_FORBIDDEN)
    end
end

功能驗(yàn)證

在一個IP白名單以外的機(jī)器訪問

curl bucket1.s3.ceph.work/asdasd #virtual hosted style 方式
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h2>403 Forbidden</h2></center>
<hr><center>nginx</center>
</body>
</html>


curl s3.ceph.work/bucket2/1233 #path-style 方式
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h2>403 Forbidden</h2></center>
<hr><center>nginx</center>
</body>
</html>

對應(yīng)的nginx日志

2017/09/21 14:05:42 [error] 30725#0: *28 [lua] access.lua:29: forbidden:10.xx.xx.xx, client: 10.xx.xx.xx, server: s3.ceph.work, request: "GET /asdasd HTTP/1.1", host: "bucket1.s3.ceph.work"

2017/09/21 14:02:47 [error] 30725#0: *22 [lua] access.lua:29: forbidden:10.xx.xx.xx, client: 10.xx.xx.xx, server: s3.ceph.work, request: "GET /bucket2/1233 HTTP/1.1", host: "s3.ceph.work"

感謝各位的閱讀,以上就是“如何構(gòu)建基于WAF的s3cmd安全體系”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對如何構(gòu)建基于WAF的s3cmd安全體系這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI