海运的博客

ubuntu通过dropbear ssh远程解锁luks rootfs全盘加密

发布时间:January 1, 2021 // 分类: LUKS // No Comments

之前使用preseed安装ubuntu luks全盘加密,这样每次重启系统都要通过vnc输入密码,可以将dropbear添加到initramfs,通过ssh解锁要方便很多。
安装dropbear-initramfs:

apt install dropbear-initramfs

安装时会提示:

dropbear: WARNING: Invalid authorized_keys file, remote unlocking of cryptroot via SSH won't work!

因为生成initramfs时要包含/etc/dropbear-initramfs/authorized_keys,复制ssh验证密钥:

#本地执行
ssh-copy-id  root@www.haiyun.me
#远程执行
cp -p ~/.ssh/authorized_keys /etc/dropbear-initramfs/

修改dropbear ssh端口:

echo 'DROPBEAR_OPTIONS="-p 2222"' >> /etc/dropbear-initramfs/config

配置initramfs ip:

#IP="${ip_address}::${gateway_ip}:${netmask}:${optional_fqdn}:${interface_name}:${auto_config}:${name_server}
echo 'IP=192.168.1.2::192.168.1.1:255.255.255.0::eth0:none:1.1.1.1' >> /etc/initramfs-tools/initramfs.conf

ip配置也可添加到grub启动参数:

GRUB_CMDLINE_LINUX="ip=192.168.1.2::192.168.1.1:255.255.255.0::eth0:none:1.1.1.1"

可选修改cryptroot-unlock解锁程序通过参数输入加密密码:

sed -i  '/^set/i if [ ! -n "\$1" ] ; then echo "use cryptroot-unlock password";exit;fi' /usr/share/cryptsetup/initramfs/bin/cryptroot-unlock
sed -i 's/read -rs/#read -rs/' /usr/share/cryptsetup/initramfs/bin/cryptroot-unlock
sed -i 's/\$REPLY/\$1/' /usr/share/cryptsetup/initramfs/bin/cryptroot-unlock

重新生成initramfs:

update-initramfs -u -k all

重启后通过ssh连接执行解锁luks:

cryptroot-unlock password

虽然/usr/share/initramfs-tools/scripts/init-bottom/dropbear有包含解锁luks后删除ip信息,但是启动后还是包含在initramfs内配置的ip信息,使用ubuntu在配置网络前清除ip信息:

sed -i '/iface eth0/a pre-up ip addr flush dev eth0' /etc/network/interfaces

使用php调用expect ssh远程自动解锁luks:

<?php
ini_set("expect.timeout", 5);
ini_set("expect.loguser", "off");
$host = "www.haiyun.me";
$port = 22;
$pass = "xxxxxx";
$stream = expect_popen("ssh -o StrictHostKeyChecking=no -p {$port} root@{$host}");
$cases = array(
  array("password:", "pass"),
  array("Enter 'help'", "shell"),
  array("Please unlock disk", "unlock"),
  array("set up successfully", "sus"),
  array("Permission denied", "den"),
  array("cryptsetup failed", "fai")
);

while (true) {
  switch (expect_expectl($stream, $cases)) {
  case "den":
    echo 'Permission denied'.PHP_EOL;
    break 2;
  case "pass":
    fwrite($stream, "password\n");
    break;
  case "shell":
    fwrite($stream, "/usr/bin/cryptroot-unlock\n");
    //fwrite($stream, "/usr/bin/cryptroot-unlock {$pass}\n");
    break;
  case "unlock":
    fwrite($stream, "{$pass}\n");
    break;
  case "fai":
    echo 'unlock failed, bad password or options?'.PHP_EOL;
    break 2;
  case "sus":
    echo 'unlock sus'.PHP_EOL;
    break 2;
  case EXP_TIMEOUT:
    echo 'timeout'.PHP_EOL;
    break 2;
  case EXP_EOF:
    echo 'eof'.PHP_EOL;
    break 2; 
  default:
    die("Error has occurred!");
  }
}
fclose ($stream);

php调用ssh2扩展远程解锁luks加密:

<?php
$host = 'www.haiyun.me';
$port = 2222;
$pass = 'xxxx';
if (!($conn = ssh2_connect($host, $port, array('hostkey'=>'ssh-rsa')))) {
  die("conn fail\n");
}
//注意路径不要使用~/.ssh/id_rsa.pub,会遇到段错误和其它莫名其妙的问题
if (ssh2_auth_pubkey_file($conn, 'root', '/root/.ssh/id_rsa.pub', '/root/.ssh/id_rsa')) {
  echo "auth sus\n";
} else {
  die("auth fail\n");
}
function expect($stream, $match) {
  $time = time();
  $res = '';
  while(!feof($stream)){
    //if (($buffer = fgets($stream, 4096)) !== false) {
    if (($buffer = fread($stream, 4096)) !== false) {
      $res .= $buffer;
    }
    if (stristr($res, $match)) {
      return 'sus';
    }
    $now = time();
    if (($now - $time) >= 10) {
      return 'timeout';
    }
    usleep(100);
  }
  return 'disconnect';
}
 
$shell=ssh2_shell($conn, 'xterm');
fwrite($shell, "/usr/bin/cryptroot-unlock\n");
$res = expect($shell, 'Please unlock disk');
if ($res == 'sus') {
  fwrite($shell, "{$pass}\n");
  $res = expect($shell, 'set up successfully');
  if ($res == 'sus') {
  }
  var_dump($res);
}

发现的问题:此方法在ubuntu20.04使用编译的4.14内核bbrplus下导致系统启动很慢,5.4及5.10内核测试正常。
另外一种通过dracut生成initramfs调用openssh解锁luks的方法:
https://github.com/gsauthof/dracut-sshd
参考:
https://hamy.io/post/0009/how-to-install-luks-encrypted-ubuntu-18.04.x-server-and-enable-remote-unlocking/

小内存vps使用luks/cryptsetup全盘加密遇到的问题

发布时间:December 2, 2020 // 分类: LUKS // No Comments

有一512M内存vps,在使用pressed安装ubuntu luks加密时一直卡在分区那里,查看日志由于占用内存较高cryptsetup被kill,然后在本机制作一个ubuntu系统img模板dd到目标vps,启动后输入加密密码时提示密码错误,查看日志同样cryptsetup被kill,查找资料原来luks默认为版本2,PBKDF为argon2i,而argon2i比较占用内存。
解决方法:在preseed分区之前修改分区脚本添加参数使用pbkdf2或luks1:

d-i partman/early_command string sed -i 's/luksFormat/--pbkdf pbkdf2 luksFormat/' /lib/partman/lib/crypto-base.sh;
#d-i partman/early_command string sed -i 's/luksFormat/--type luks1 luksFormat/' /lib/partman/lib/crypto-base.sh;

也可指定argon2i最大使用内存,单位KB:

--pbkdf-memory 25600

对已存在的加密分区可转换:

cryptsetup -v luksDump /dev/sda5
cryptsetup -v --pbkdf pbkdf2 luksConvertKey /dev/sda5
cryptsetup -v convert /dev/sda5 --type luks1

20210208更新:
当前ubuntu20.04.2已添加以下参数可直接选择使用pbkdf2或luks1:

d-i partman-crypto/luksformat_options string --type luks1
d-i partman-crypto/luksformat_options string --pbkdf-memory 65535
d-i partman-crypto/luksformat_options string --pbkdf pbkdf2

也可在安装前自动判断内存小于小于多少设置使用pbkdf2:

d-i partman/early_command string if [ `free|grep Mem|tr -s ' ' ' '|cut -d ' ' -f 2` -lt 819200 ];\
    then debconf-set partman-crypto/luksformat_options "--pbkdf pbkdf2";fi

其实小内存主要是安装时luks默认占用内存过高导致安装失败,而dd的系统原虚拟机内存高,luks在设置密码时根据系统内存自动设置--pbkdf-memory,导致pbkdf-memory参数比vps内存都相差不大,所以dd的系统也不能正常启动,vps在安装前通过--pbkdf-memory设置小内存,后续再添加密码使用自动设置的内存参数就可以了。
参考:
https://lists.debian.org/debian-boot/2020/10/msg00000.html
https://gitlab.com/cryptsetup/cryptsetup/-/issues/372

preseed和kickstart自动安装centos/ubuntu使用luks加密硬盘

发布时间:July 14, 2019 // 分类: LUKS // No Comments

preseed:

d-i partman-auto/choose_recipe select boot-encrypt
d-i partman-auto/method string crypto
d-i partman-crypto/weak_passphrase boolean true
d-i partman-crypto/passphrase string haiyun.me
d-i partman-crypto/passphrase-again string haiyun.me
d-i partman-auto-lvm/guided_size string 100%
d-i partman-auto-lvm/new_vg_name string vg00
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true

d-i partman-auto/expert_recipe string                         \
      boot-encrypt ::                                         \
              300 1 300 ext4                                  \
                      $primary{ } $bootable{ }                \
                      method{ format } format{ }              \
                      use_filesystem{ } filesystem{ ext4 }    \
                      mountpoint{ /boot }                     \
              .                                               \
              512 2 512 linux-swap                            \
                      $lvmok{ } lv_name{ lv_swap }            \
                      in_vg { vg00 }                          \
                      method{ swap } format{ }                \
              .                                               \
              1 3 -1 ext4                                     \
                      $lvmok{ } lv_name{ lv_root }            \
                      in_vg { vg00 }                          \
                      method{ format } format{ }              \
                      use_filesystem{ } filesystem{ ext4 }    \
                      mountpoint{ / }                         \
              .   

kickstart:

part /boot --fstype="ext4" --ondisk=sda --size=300
part pv.01 --fstype="lvmpv" --ondisk=sda --size=1 --grow --encrypted --passphrase=haiyun.me
volgroup rootvg --pesize=4096 pv.01
logvol swap  --fstype="swap" --size=512 --name=swaplv --vgname=rootvg
logvol /  --fstype="ext4" --grow --size=1 --name=rootlv --vgname=rootvg

更多见:通过网络preseed/kickstart安装ubuntu/centos系统

linux使用luks/cryptsetup加密硬盘或u盘

发布时间:June 19, 2018 // 分类: LUKS // No Comments

yum install cryptsetup
apt install cryptsetup-bin

加密整个硬盘,加密单个分区先fdisk新建分区再加密指定分区,按提示输入大写YES和密码:

cryptsetup luksFormat /dev/sdb 
#加密单个分区
#cryptsetup luksFormat /dev/sdb1

可设置8个密码,添加新密码:

cryptsetup luksAddKey /dev/sdb

使用stdin重定向输入添加新密码:

#!/bin/bash
oldPassword=haiyun.me
newPassword=www.haiyun.me
rootdevice=`blkid |grep crypto_LUKS|awk -F':' '{print $1}'`
echo -e "$oldPassword\n$newPassword\n$newPassword" | cryptsetup luksAddKey $rootdevice

也可使用文件作为密码:

dd if=/dev/urandom of=/root/enc.key bs=1 count=4096
cryptsetup luksAddKey /dev/sdb enc.key

删除密码:

#输入要删除的密码
cryptsetup luksRemoveKey /dev/sdb
#删除文件密码
cryptsetup luksRemoveKey /dev/sdb -d enc.key

使用stdin删除密码:

echo -e "password\n" | cryptsetup luksRemoveKey `blkid |grep crypto_LUKS|awk -F':' '{print $1}'`

解开加密的磁盘并映射为可挂载的分区,在此分区上可创建pv使用lvm:

#解密的分区位于/dev/mapper/test
cryptsetup luksOpen /dev/sdb test
#cryptsetup luksOpen /dev/sdb test -d enc.key 

格式化分区并挂载目录:

mkfs.ext4 /dev/mapper/sdb
mkdir test
mount /dev/mapper/sdb /mnt/

查看状态:

cryptsetup status /dev/mapper/sdb

查看加密的分区信息:

cryptsetup luksDump /dev/sdb

开机自动挂载:

#/etc/crypttab文件添加以下:
#开机时手工输入密码
test /dev/sdb  luks
#使用密码文件自动输入
#test /dev/sdb /root/enc.key luks
#/etc/fstab文件添加以下:
/dev/mapper/sdb /mnt ext4 defaults        0 0

关闭解密的分区:

umount /mnt
cryptsetup luksClose sdb

加密raw文件可挂载磁盘:

dd if=/dev/zero of=loop.img bs=1M count=100  
#挂载  
losetup /dev/loop10 loop.img 
cryptsetup luksFormat /dev/loop10
#卸载
losetup -d /dev/loop10
分类
最新文章
最近回复
  • Jimmy: 方法一 nghtp3步骤需要改成如下才能编译成功: git clone https://git...
  • 海运: 地址格式和udpxy一样,udpxy和msd_lite能用这个就能用。
  • 1: 怎么用 编译后的程序在家里路由器内任意一台设备上运行就可以吗?比如笔记本电脑 m参数是笔记本的...
  • 孤狼: ups_status_set: seems that UPS [BK650M2-CH] is ...
  • 孤狼: 擦。。。。apcupsd会失联 nut在冲到到100的时候会ONBATT进入关机状态,我想想办...
  • 海运: 网络,找到相应的url编辑重发请求,firefox有此功能,其它未知。
  • knetxp: 用浏览器F12网络拦截或监听后编辑重发请求,修改url中的set为set_super,将POS...
  • Albert: 啊啊啊啊啊啊啊啊啊 我太激动了,终于好了英文区搜索了半天,翻遍了 pve 论坛没找到好方法,博...
  • jiangker: good, very helpful to me
  • fengfeng: N1 armbian 能有编译下内核吗。。我要开启can 不懂怎么操作