dm-crypt 加密

块设备加密1

Device Mapper(DM)是Linux 2.6全面引入的块设备新构架,通过DM可以灵活地管理系统中所有的真实或虚拟的块设备。DM以块设备的形式注册到Linux内核中,凡是挂载(或者说“映射”)于DM结构下的块设备,不管他们是如何组织,如何通讯,在Linux看来都是一个完整的DM块设备。因此DM让不同组织形式的块设备或者块设备集群在Linux内核面前有一个完整统一的DM表示。

  • LUKS, the Linux Unified Key Setup, is a standard for disk encryption. It adds a standardized header at the start of the device, a key-slot area directly behind the header and the bulk data area behind that. The whole set is called a ‘LUKS container’. The device that a LUKS container resides on is called a ‘LUKS device’. For most purposes both terms can be used interchangeably. But note that when the LUKS header is at a nonzero offset in a device, then the device is not a LUKS device anymore, but has a LUKS container stored in it at an offset.

  • LUKS can manage multiple passphrases that can be individually revoked or changed and that can be securely scrubbed from persistent media due to the use of anti-forensic stripes. Passphrases are protected against brute-force and dictionary attacks by PBKDF2, which implements hash iteration and salting in one function.

  • Each passphrase, also called a key in this document, is associated with one of 8 key-slots. Key operations that do not specify a slot affect the first slot that matches the supplied passphrase or the first empty slot if a new passphrase is added.

几个概念2

dm-crypt is a transparent disk encryption subsystem. In this guide you will learn how to encrypt disks, partition, swap and even use files as encrypted, and portable containers for your sensitive data.

dm-crypt maps a physical block device to a virtual block device. When you write to the virtual device, every block of data is encrypted and stored on the physical device. When you read from the virtual device, every block is decrypted at runtime. Consequently, The blocks of data are encrypted on the storage device and the virtual device looks like a normal, unencrypted block device as far as the system is concerned. Because of this you should be aware of two important aspects:

  • Closing the virtual device when not in use will maximize the safety of your data.
  • When the virtual device is open, if an attacker manages to break into your server and gains access to the Linux kernel, he may be able to read from that device. While this is hard to do on a well-configured, secure, Linode, you can increase security by looking into ways to harden the Linux kernel (e.g. grsecurity) and/or Mandatory Access Control systems like AppArmor or SELinux.

准备工作

  • 安装 dmsetupryptsetup
    sudo apt-get install dmsetup cryptsetup

  • 检查是否已经建立设备映像程式
    ls -l /dev/mapper/control

  • 查看aes模块是否加载
    cat /proc/crypto

  • 如果没有的话,加载一个(可以是aes的任意一种)
    sudo modprobe aes

  • 加载dm-crypt模块
    sudo modprobe dm-crypt

  • sudo dmsetup targets
    如果一切顺利应该有如下输出:
    crypt v…
    striped v…
    linear v…
    error v…

加密及挂载设备

  • 使用随机数写入目标磁盘3次,完全擦除数据。<这一步不是必须的>
    shred --verbose --random-source=/dev/urandom --iterations=3 /dev/sda5

  • 首先卸载需要加密的设备
    sudo umount /dev/sda5

  • 创建加密设备,格式化询问的时候注意输入大写 YES
    sudo cryptsetup -y -v -c aes-xts-plain64 -s 512 -h sha512 -i 10000 luksFormat /dev/sda5

  • 打开加密设备,设备会被映射到 /dev/mapper/xxx 下,device mapper name
    sudo cryptsetup luksOpen /dev/sda5 xxx

  • 查看映射状态,用名字去查询
    sudo cryptsetup -v status xxx 或者使用 sudo crypsetup -v status /dev/mapper/xxx

  • 在加密设备上建立文件系统,用名字去操作
    sudo mkfs.ext4 /dev/mapper/xxx

  • 创建挂载点,用于挂载加密的文件系统
    sudo mkdir /mnt/dm-crypt

  • 挂载设备到目标目录下
    mount -t ext4 /dev/mapper/xxx /mnt/dm-crypt

  • 查看挂载是否成功
    df -Th

关闭加密设备

加密成功后如果直接挂载 /dev/sda5 会报错,因为加密设备无法直接挂载

  • mount /dev/sda5 /mnt
    mount: unknown filesystem type ‘crypto_LUKS’

  • 若不再使用加密设备 /dev/mapper/xxx 时,先卸载文件系统然后关闭该加密设备。
    umount /mnt/dm-crypt
    sudo cryptsetup luksClose xxx

  • Last but not least, clear the copy and cache buffers:
    sysctl --write vm.drop_caches=3

下次使用 luksOpen 打开该加密设备的时候就需要输入之前设置的密码才能进行下一步的操作。

开机挂载

  • luks 最多可以添加8个密钥,创建一个 keyfile 用于自动解锁,如 /etc/luks-keys/disk_secret_key
    dd if=/dev/urandom of=/etc/luks-keys/disk_secret_key bs=512 count=8

  • /etc/crypttab 增加加密块设备的描述,这样系统开机的时候会自动打开这个加密设备

<target name> <source device> <key file> <option>
映射设备名 加密设备 密码文件(为none或者空时表示开机需要手动输入密码) 配置选项

sudo cat > /etc/crypttab << "EOF"
> xxx /dev/sda5 /etc/luks-keys/disk_secret_key luks
> EOF
  • 给加密设备增加一个密钥,使用 设备名 关联。
    cryptsetup luksAddkey /dev/sda5 /root/passwd
    此时,你既可以用密码打开该加密盘,也可以用 keyfile 打开。

  • 验证使用 keyfile 打开加密盘
    cryptsetup --key-file 所用keyfile的路径 luksOpen 已加密的物理分区或虚拟盘 映射名

  • 设置加密设备的自动挂载
    blkid /dev/mapper/xxx 得到设备的UUID,sudo vi /etc/fstab 增加一条记录。
    UUID=XXX /mnt ext4 defaults 0 0

取消设备加密

先卸载设备,然后关闭设备的映射,之后进行强制格式化即可。
umount /mnt/dm-crypt
sudo cryptsetup luksClose xxx
mkfs -t ext4 -f /dev/sda5

全盘加密

从理论上讲:任何一种磁盘加密工具,(在不借助外部机制的情况下)都【不可能】实现真正的“自启动全盘加密”。 要想【自启动】,必须要有一个引导程序(在MBR中),至少这个引导程序不能加密(引导程序如果被加密了,就无法引导启动了)。所以,凡是能够实现“自启动全盘加密”的,其【引导程序】都是明文的(无加密的) 。比如 TrueCrypt 和 VeraCrypt 进行全盘加密,都会替换原有的主引导扇区(MBR)的内容,在里面放入一段代码。这段代码会在开机启动的时候,提示你输入密码,然后用你输入的密码进行解密。 对于 dm-crypt 的全盘加密,除了“主引导扇区”【没有】加密,还包括 /boot 分区也【没有】加密(因为/boot 分区要引导管理器和内核)。

cryptsetup 常用参数3

luksFormat:	设定加密盘
luksOpen:		开启映射加密设备
luksClose:		关闭映射加密设备
luksAddKey:	添加密钥
luksRemoveKey:	移除密钥
luksChangeKey:	更改密钥
luksDump:		查看密钥 slot 状态
luksUUID:		打印加密设备的UUID号
status:		查看映射设备信息

特别注意,数据备份4

硬盘损坏实际上并不是小概率事件,据说保养较好的机械硬盘每年有5%的概率会损坏。
另外 LUKS 设备的 header 或者密码保存区域出现问题也会导致硬盘无法解密,所以务必要做好数据备份工作。

备份及恢复 luksHeader

cryptsetup luksHeaderBackup --header-backup-file <file> <device>
cryptsetup luksHeaderRestore --header-backup-file <file> <device>

终极大法:全盘备份,或者关键资产备份,可以使用 tar 以及 gpg 加密备份后的文件。

虚拟磁盘加密

所谓的“虚拟加密盘”,就是说这个盘并【不是】对应物理分区,而是对应一个虚拟分区(逻辑卷)。这个虚拟分区,说白了就是一个大文件。虚拟分区有多大,这个文件就有多大。“虚拟加密盘”的一个主要好处在于——可以拷贝复制。比如你可以在不同的机器之间复制这个虚假分区对应的大文件。

A benefit of using files as encrypted containers is that they’re slightly easier to move around from system to system. But keep in mind that files are also more prone to being deleted accidentally.

dd if=/dev/zero of=/root/luks.vol bs=1M count=1024

对于特别大的容器文件,性能【高于】dd 命令。
fallocate -l 64G /root/luks.vol

truncate -s 64G /root/luks.vol
The advantage of this command is that the file starts out
by occupying 0 bytes on the filesystem and grows as required.

之后对这个虚拟分区 /root/luks.vol 进行加密即可。

交换分区加密

Swapped memory keeps data indefinitely on the physical device until it is overwritten, exposing you to the possibility of leaking private information. You can disable it entirely if you have a large amount of RAM on your machine or encrypt it with a random key each time your machine boots.

交换分区里的数据会一直保留,直到它被覆盖。如果机器本身内存够大的话可以禁用交换分区,或者每次机器启动的时候对该分区进行加密。
编辑 /etc/crypttab/dev/xxx 是交换分区名
swap-encrypted /dev/xxx /dev/urandom swap,noearly

因为交换分区已经被重新映射到 /dev/mapper/swap-encrypted,所以需要改 /etc/fstab的自动挂载信息。
/dev/mapper/swap-encrypted none swap sw 0 0


  1. https://program-think.blogspot.com/2015/10/dm-crypt-cryptsetup.html ↩︎

  2. https://www.linode.com/docs/security/encrypt-data-disk-with-dm-crypt/ ↩︎

  3. https://linux.die.net/man/8/cryptsetup ↩︎

  4. https://gitlab.com/cryptsetup/cryptsetup/wikis/FrequentlyAskedQuestions ↩︎

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章