天天看點

資料庫記錄安全解決方案

中國廣東省深圳市龍華新區民治街道溪山美地

518131

+86 13113668890

+86 755 29812080

版權 © 2014 http://netkiller.github.io

版權聲明

轉載請與作者聯系,轉載時請務必标明文章原始出處和作者資訊及本聲明。

資料庫記錄安全解決方案

文檔出處:

<a target="_top" href="http://netkiller.github.io/">http://netkiller.github.io</a>

<a target="_top" href="http://netkiller.sourceforge.net/">http://netkiller.sourceforge.net</a>

2014-08-19

摘要

2014-08-28 第二版

我的系列文檔

<a target="_top" href="http://netkiller.github.io/architect/index.html">netkiller architect 手劄</a>

<a target="_top" href="http://netkiller.github.io/developer/index.html">netkiller developer 手劄</a>

<a target="_top" href="http://netkiller.github.io/php/index.html">netkiller php 手劄</a>

<a target="_top" href="http://netkiller.github.io/python/index.html">netkiller python 手劄</a>

<a target="_top" href="http://netkiller.github.io/testing/index.html">netkiller testing 手劄</a>

<a target="_top" href="http://netkiller.github.io/cryptography/index.html">netkiller cryptography 手劄</a>

<a target="_top" href="http://netkiller.github.io/linux/index.html">netkiller linux 手劄</a>

<a target="_top" href="http://netkiller.github.io/debian/index.html">netkiller debian 手劄</a>

<a target="_top" href="http://netkiller.github.io/centos/index.html">netkiller centos 手劄</a>

<a target="_top" href="http://netkiller.github.io/freebsd/index.html">netkiller freebsd 手劄</a>

<a target="_top" href="http://netkiller.github.io/shell/index.html">netkiller shell 手劄</a>

<a target="_top" href="http://netkiller.github.io/security/index.html">netkiller security 手劄</a>

<a target="_top" href="http://netkiller.github.io/www/index.html">netkiller web 手劄</a>

<a target="_top" href="http://netkiller.github.io/monitoring/index.html">netkiller monitoring 手劄</a>

<a target="_top" href="http://netkiller.github.io/storage/index.html">netkiller storage 手劄</a>

<a target="_top" href="http://netkiller.github.io/mail/index.html">netkiller mail 手劄</a>

<a target="_top" href="http://netkiller.github.io/docbook/index.html">netkiller docbook 手劄</a>

<a target="_top" href="http://netkiller.github.io/version/index.html">netkiller version 手劄</a>

<a target="_top" href="http://netkiller.github.io/database/index.html">netkiller database 手劄</a>

<a target="_top" href="http://netkiller.github.io/postgresql/index.html">netkiller postgresql 手劄</a>

<a target="_top" href="http://netkiller.github.io/mysql/index.html">netkiller mysql 手劄</a>

<a target="_top" href="http://netkiller.github.io/nosql/index.html">netkiller nosql 手劄</a>

<a target="_top" href="http://netkiller.github.io/ldap/index.html">netkiller ldap 手劄</a>

<a target="_top" href="http://netkiller.github.io/network/index.html">netkiller network 手劄</a>

<a target="_top" href="http://netkiller.github.io/cisco/index.html">netkiller cisco ios 手劄</a>

<a target="_top" href="http://netkiller.github.io/h3c/index.html">netkiller h3c 手劄</a>

<a target="_top" href="http://netkiller.github.io/multimedia/index.html">netkiller multimedia 手劄</a>

<a target="_top" href="http://netkiller.github.io/perl/index.html">netkiller perl 手劄</a>

<a target="_top" href="http://netkiller.github.io/radio/index.html">netkiller amateur radio 手劄</a>

<a target="_top" href="http://netkiller.github.io/devops/index.html">netkiller devops 手劄</a>

目錄

<a href="http://netkiller.github.io/journal/mysql.security.html#what">1. 什麼是防删除,防撰改</a>

<a href="http://netkiller.github.io/journal/mysql.security.html#why">2. 為什麼要做防删除,防撰改限制</a>

<a href="http://netkiller.github.io/journal/mysql.security.html#when">3. 何時做防删除,防撰改限制</a>

<a href="http://netkiller.github.io/journal/mysql.security.html#where">4. 在哪裡做防删除,防撰改限制</a>

<a href="http://netkiller.github.io/journal/mysql.security.html#who">5. 誰去做防删除,防撰改限制</a>

<a href="http://netkiller.github.io/journal/mysql.security.html#how">6. 怎樣實作防删除,防撰改限制</a>

<a href="http://netkiller.github.io/journal/mysql.security.html#idp59833664">6.1. 限制删除</a>

<a href="http://netkiller.github.io/journal/mysql.security.html#idp59836096">6.2. 限制修改</a>

<a href="http://netkiller.github.io/journal/mysql.security.html#idp59840720">6.3. 為資料安全而分庫</a>

<a href="http://netkiller.github.io/journal/mysql.security.html#traces">7. 怎樣實作資料修改留痕</a>

<a href="http://netkiller.github.io/journal/mysql.security.html#idp59853168">7.1. 版本控制</a>

<a href="http://netkiller.github.io/journal/mysql.security.html#idp59859504">7.2. 一張表實作曆史日志記錄</a>

禁止資料删除,資料一旦增加不允許資料被任何人删除

禁止資料修改,資料一旦建立不允許對資料做修改操作

很多時候我們的資料是隻增加,不會删除資料。有些敏感子段一旦資料家裡是不允許再修改的,例如銀行賬戶表中的資金子段。

另一個原因是我們防止誤操作

我認為在資料庫設計時就應該考慮倒這些問題,如果發現資料被删除或者被撰改,亡羊補牢也不晚,我們不能允許再次發生。

你可以取消使用者的 delete 權限,使之隻能做查詢操作,但修改(update)呢?你就無能為力!如果取消update程式将不能正常運作。

程式設計之初你就應該想到這些問題,如果沒有考慮倒,你隻能修改現有邏輯。通常的做法是所有表增加一個删除狀态子段,删除操作即是更新狀态。這種方式也有弊端就是垃圾資料會不停地膨脹。

我認為可以分為兩種人,一種是dba,一種是開發者。這裡主要将資料庫部分。

對account表中的記錄做删除操作,資料庫抛出異常 permission denied

禁止所有修改操作

限制部分子段修改,其他子段扔允許修改

在資料庫修改前我們覆寫掉修改的資料,使之更新後資料保持不變。

我們通常使用一個資料庫開發,該資料庫包含了前背景所有的功能,我建議将前背景等等功能進行分庫然後對應各種平台配置設定使用者權限,例如

我們建立三個資料庫cms,frontend,backend 同時對應建立三個使用者 cms,frontend,backend 三個使用者隻能分别通路自己的資料庫,注意在系統的設計之初你要考慮好這樣的劃分随之系統需要做相應的調整。

backend 負責背景,權限最高

frontend 是前台權限,主要是使用者使用者中心,使用者注冊,登入,使用者資訊資料編輯,檢視新聞等等

cms 使用者是網站内容管理,主要負責内容更新,但登陸cms背景需要`backend`.`employees`表使用者認證,是以他需要讀取權限,但不允許修改其中的資料。

cms與backend 通常我們會限制ip位址來源,安全相對好控制。

frontend 主要對外提供服務,我們假設一旦被駭客入侵,所波及的範圍被限制在frontend權限下,至少`backend`.`employees`不會被撰改,cms内容也得到了保護。

想100%解決資料的安全是非常空難的,但我們至少保護了一部份資料的安全。使其安全不會進一步擴散影響。

資料記錄每一次修改我們都需要保留之前的資料,這樣可以随時調出曆史資料,使用者審計等等。

主表

本版控制表,用于記錄每次變動

任何資料的變化都會複制一份到曆史表,我們可以随時比較兩個版本資料的變化,我還為此開發了一個類似diff的工具,可以逐行比較,通過色彩變化現實資料的不同。

我有一個表,裡面隻有固定行數的行記錄,這些資料就是配置參數,我們将配置檔案儲存在資料庫中,因為需要做負載均衡而不能使用檔案配置檔案。

有這樣一個需求,這個記錄每次修改都要儲存曆史記錄,用于審計等等。我是這樣設計該表的

資料記錄的形态

如上圖所示,狀态 current 是目前記錄,而trash是廢棄的曆史記錄。

每次修改資料,首先将current改為trash,然後插入一條新資料狀态為current,我們隻會使用最後一條狀态為current的資料。

我們使用更新觸發器控制除了status,mtime意外的字段修改

限制删除的觸發器