CryptDB安装与使用

实验目的

通过对CryptDB的安装和使用,了解学习加密数据库查询技术的原理和实现方式.

实验环境

  • 操作系统:ubuntu 12.04
  • 工具:CryptDB、git、ruby

CryptDB简介

  • CryptDB 是来自MIT的一个开源项目,它不是某种数据库,而是加密数据库查询技术的一种,可以在加密的数据库(目前支持MySQL)上进行简单的操作。正常说来, 一个应用是直接连接数据库的,配置了CryptDB后,CryptDB作为应用和数据库的中间代理,以明文的方式与应用交互,以密文的方式与数据库交互。

  • CryptDB首次解决了实用性的问题,它将数据嵌套进多个加密层,每层使用不同的密钥,这些加密密钥与用户的密码有关,即便是数据库管理员也不能访问这些加密的数据,这也防止了因数据库泄露导致用户信息泄露的问题。虽然目前支持的SQL语句有限,还没有到真正投入使用的程度,但其性却非常出众。Google也根据CryptDB的设计开发了Encrypted BigQuery client。

  • CryptDB官网的介绍如下

    Online applications are vulnerable to theft of sensitive information because adversaries can exploit software bugs to gain access to private data, and because curious or malicious administrators may capture and leak data. CryptDB is a system that provides practical and provable confidentiality in the face of these attacks for applications backed by SQL databases. It works by executing SQL queries over encrypted data using a collection of efficient SQL-aware encryption schemes. CryptDB can also chain encryption keys to user passwords, so that a data item can be decrypted only by using the password of one of the users with access to that data. As a result, a database administrator never gets access to decrypted data, and even if all servers are compromised, an adversary cannot decrypt the data of any user who is not logged in. An analysis of a trace of 126 million SQL queries from a production MySQL server shows that CryptDB can support operations over encrypted data for 99.5% of the 128,840 columns seen in the trace. Our evaluation shows that CryptDB has low overhead, reducing throughput by 14.5% for phpBB, a web forum application, and by 26% for queries from TPC-C, compared to unmodified MySQL. Chaining encryption keys to user passwords requires 11-13 unique schema annotations to secure more than 20 sensitive fields and 2-7 lines of source code changes for three multi-user web applications.

实验步骤

CryptDB安装

  1. 安装Ubuntu系统

    CryptDB依附于ubuntu系统,本次实验使用的是12.04版本的系统,已经安装完毕;

  2. 安装git和ruby

    安装git是为了获取官网的源码;

    安装ruby是因为CryptDB的安装脚本使用ruby语言编写;

    1
    $ sudo apt-get install git ruby

  3. 克隆CryptDB代码

    把CryptDB项目克隆到了主目录下,后续步骤中也会安装到这个目录;

    1
    $ git clone -b public git://g.csail.mit.edu/cryptdb

  4. 安装CryptDB

    进入到cryptdb文件夹,执行安装脚本,按照提示,等待完成。

    在此过程中会要求设置mysql密码,因后续过程需要mysql和CryptDB密码相同,所以在此设置了mysql密码为CryptDB默认密码letmein;

    1
    2
    $ cd cryptdb
    $ ./scripts/install.rb .

  5. 添加环境变量EDBDIR.bashrc

    编辑cryptdb/目录下的.bashrc,把下面的代码添加到最后;

    1
    export EDBDIR=/home/levi/cryptdb/

    至此,安装完成,重启设备即可使用CryptDB;

CryptDB使用

本实验中准备了3个终端:

  • 终端1:用于运行CryptDB,在上面显示密文;
  • 终端2:用于从代理端口3306访问数据库,显示用户实际操作状态;
  • 终端3:用于从正常端口访问数据库,显示明文;

启用proxy

MySQL使用本地3306端口,CryptDB使用本地3307端口,CryptDB把3307端口的数据处理后通过3306端口与MySQL交互;

终端1中输入如下内容,系统返回started即为执行成功;

1
2
3
4
5
6
$ /home/levi/cryptdb/bins/proxy-bin/bin/mysql-proxy  \
--plugins=proxy --event-threads=4 \
--max-open-files=1024 \
--proxy-lua-script=$EDBDIR/mysqlproxy/wrapper.lua \
--proxy-address=127.0.0.1:3307 \
--proxy-backend-addresses=localhost:3306

启用CryptDB和mysql

  1. 终端2中输入如下命令,连接到本机3306端口的mysql;

    1
    $ mysql -u root -p -h 127.0.0.1 -P 3306

    终端3中输入如下命令,连接到本机3307端口的CryptDB;

    1
    $ mysql -u root -p -h 127.0.0.1 -P 3307

CryptDB使用演示

  1. 终端3中查询数据库show databases;

    此时在终端1中显示CryptDB查询数据库的结果;

  2. 终端3中创建数据库名称为leeyuxun create database leeyuxun;

    此时在终端1中显示CryptDB创建数据库leeyuxun的结果;

  3. 终端3中打开数据库leeyuxun use leeyuxun;

    此时在终端1中显示CryptDB打开数据库的结果;

  4. 终端3中新建表users

    1
    create table users(id int(2) not null primary key auto_increment,username varchar(40),password varchar(40));

    此时在终端1中显示CryptDB新建表users的结果;

  5. 终端3中的users表中增加数据;

    1
    insert into users(username,password) values("lizhilin","123456");

    此时在终端1中显示CryptDB的users表增加数据的结果;

  6. 终端3中查询表users中的记录select * from users;

    此时在终端1中显示CryptDB的users表查询记录的结果;

  7. 终端1中发现新创建的users表在Mysql中储存的名字为table_CIGLJIQNCC,在终端2中查询以此命名的表,发现储存数据为密文,表明数据在Mysql端是加密的;

修改密码

修改CryptDB密码

输入如下语句即可更改CryptDB的密码;

1
$ sudo xdg-open /home/levi/cryptdb/mysqlproxy/wrapper.lua

更换letmein为其他密码即可;

修改mysql密码

输入如下语句并按提示输入当前密码,要更改的密码,并确认密码;

1
$ mysqladmin -u root -p password