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 ↩︎

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