天天看点

数据库记录安全解决方案

中国广东省深圳市龙华新区民治街道溪山美地

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意外的字段修改

限制删除的触发器