LoginSignup
8
5

More than 5 years have passed since last update.

dm-crypt/luksの起動前パスフレーズ入力(暗号化解除)をSSH経由で行う

Last updated at Posted at 2016-07-28

今の時代、スマートフォン(Android/iPhone)ですらデバイス全体のデータに暗号化が掛かっているのに、PCはセットアップしないと暗号化がなされない。
せっかくCPUにAES-NI命令も乗っててほとんどオーバーヘッドがないので暗号化を掛けよう。

Linuxにはdm-crypt/luksでシステムの起動ディスク全体に暗号化を掛ける機能がある。これは、Linux起動前にパスワードの入力を必要とする。
PCはともかく、サーバなどに暗号化を掛けるときは、遠隔でパスワードを入力できないと不便なので、その部分をSSHで入力できるようにする。


サーバー側の準備

debian を想定。

1.1 インストール

必要なパッケージをインストール。 dropbear がキモ。組み込み用のSSHサーバー。

apt-get install openssh-server dropbear busybox

1.2 鍵のコピー

勝手に秘密鍵・公開鍵を作ってくれるので、秘密鍵 /etc/initramfs-tools/root/.ssh/id_rsa をSSH接続したい接続元マシンにrsync/sftpなどでコピーする。

GRUBをいじる

2.1 GRUB設定の書き換え

/etc/default/grub をvimなどで開いて、 GRUB_CMDLINE_LINUX= の部分を以下のように変更。

GRUB_CMDLINE_LINUX="ip=IPアドレス:空:ゲートウェイIP:ネットマスク:ホスト名:インターフェイス:none"

GRUB_CMDLINE_LINUX="ip=192.168.122.192::192.168.122.1:255.255.255.0::eth0:none" 

2.2 GRUB/initramを更新

root権限またはsudoした権限で

update-grub
update-initramfs -u # 一応(なくていいかも)

をしてgrub/initramfs等をアップデート。


サーバーに接続 (ディスク暗号化解除)

サーバーの秘密鍵 /etc/initramfs-tools/root/.ssh/id_rsa を手元に持ってきて -i で指定して接続をする。

SSHをすでにマシンに入れていた場合、initramfsで起動するSSHのホスト鍵と異なるので、 StrictHostKeyChecking=no にしないと接続されないことがある。

ちょい危険な方法

パスワードが接続元 history に残るので、接続元マシンが絶対に安全なとき以外は微妙。

$ ssh -i ~/id_rsa.initramfs \ 
 -o StrictHostKeyChecking=no \
 -o UserKnownHostsFile=/dev/null \
 host.example.com \
 "echo -ne \"PASSWORD\" > /lib/cryptsetup/passfifo"

Warning: Permanently added '130.158.xxx.xxx' (RSA) to the list of known hosts.

ちょっと安全な方法 (接続後に入力)

$ ssh -i ~/id_rsa.initramfs \ 
 -o StrictHostKeyChecking=no \
 -o UserKnownHostsFile=/dev/null \
 host.example.com

Warning: Permanently added '130.158.xxx.xxx' (RSA) to the list of known hosts.


BusyBox v1.22.1 (Debian 1:1.22.0-9+deb8u1) built-in shell (ash)
Enter 'help' for a list of built-in commands.

~ # echo -ne "PASSWORD" > /lib/cryptsetup/passfifo

以上のどちらかを実行すると、勝手に暗号化ロックが解除されて普通のrootドライブのLinuxが立ち上がってくる。

~/.ssh/config

Host host.example.com
        Hostname host.example.com
        User root
        UserKnownHostsFile ~/.ssh/know_hosts.initramfs
        IdentityFile ~/.ssh/id_rsa.initramfs

参考

8
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5