非交互式 ecryptfs 目录加密/解密

非交互式 ecryptfs 目录加密/解密

我可以从脚本解密 ecryptfs Private 目录吗?

我对此类活动的基本用例是进行远程备份。想象一下,您有一台机器(称为 privatebox),带有加密的私人目录,用于存储您的照片(或其他一些敏感信息)。它仅在登录时解密。想象一下,您希望能够在远程计算机上编写一个脚本,该脚本将登录到 privatebox,解密目录以添加照片,然后重新加密并注销。所有这些都不需要用户交互步骤(也许它从 cron 运行)。请注意,私人盒子的密码不会以纯文本或任何形式存储在私人盒子上。而且由于它会被加密(更新期间除外),如果有人获得了 SD 卡等,它将受到保护。

这样的脚本将像这样工作(在我看来):

  • 在 privatebox 上设置使用密码加密的私有目录
  • 设置从本地计算机到 privatebox 的 ssh 密钥,以便您可以非交互方式使用 ssh(cron 可以登录)
  • 然后呢?如果您知道密码,如何以非交互方式解密私人文件夹?

看来 ecryptfs 是专门设计来不允许这样做的(即使使用 SSH 密钥欺骗,你仍然必须手动挂载你的私有目录)。

基本上,我正在寻找的是“ecryptfs-mount-private”的非交互式版本或类似的版本(如果有人知道解决方案)。就像是:

% ecryptfs-mount-private -p $PASSPHRASE

我可以在其中传递密码而不必键入它。

如果 ecryptfs 不能做到这一点,有人知道替代方案吗?谢谢!

答案1

好吧,我明白了。感谢您对 Xen2050 的帮助,我在这里没有足够的声誉来给您投票(还)。

这是适合我的 bash 脚本:

#Set this variable to your mount passphrase. Ideally you'd get this from $1 input so that the actual value isn't stored in bash script. That would defeat the purpose.
mountphrase='YOURMOUNTPASSPHRASE' 

#Add tokens into user session keyring
printf "%s" "${mountphrase}" | ecryptfs-add-passphrase > tmp.txt

#Now get the signature from the output of the above command
sig=`tail -1 tmp.txt | awk '{print $6}' | sed 's/\[//g' | sed 's/\]//g'`
rm -f tmp.txt #Remove temp file

#Now perform the mount
sudo mount -t ecryptfs -o key=passphrase:passphrase_passwd=${mountphrase},no_sig_cache=yes,verbose=no,ecryptfs_sig=${sig},ecryptfs_cipher=aes,ecryptfs_key_bytes=16,ecryptfs_passthrough=no,ecryptfs_enable_filename_crypto=no /home/user/.Private /home/user/Private

请注意,我必须禁用文件名加密才能正常工作。当我尝试使用文件名加密时,我在安装过程中遇到了库错误。为了不进行文件名加密,您在创建 Private/ 目录时必须使用以下内容:

ecryptfs-setup-private -n

这现在对我有用。

为了回答一些人会说“为什么要这样做?”,好吧,我并不总是希望在每次登录时安装我的私人数据。我希望有一种快速安装数据的方法,不需要我使用实际的用户登录密码。想象一下我想与其他人共享数据吗?我必须给他们我的密码。使用挂载密码本质上允许我拥有一个比我的登录安全性更低的挂载密码。如果您想自动挂载数据并将该短语存储在某处(可能在 USB 记忆棒上作为解锁数据的密钥),这也很有用。我永远不想将我的登录密码以纯文本形式存储在任何地方。但是,如果您知道数据的内容,并且知道数据本身的私密性不如您自己的帐户,那么这是一个很好的解决方案。

答案2

您可以首先使用ecryptfs-add-passphrase将密码短语放入内核密钥环中,就像通过管道将密码短语传递给它一样(保持密码短语的安全,而不将其保留在明文文件中是一个问题):

printf "%s" "passphrase" | ecryptfs-add-passphrase [--fnek] -

然后使用mount.ecryptfs_private

mount.ecryptfs_private 是一个挂载帮助实用程序,供非 root 用户以加密方式挂载私有目录,默认情况下为 ~/Private。

该程序可以选择采用一个参数 ALIAS。如果省略 ALIAS,程序将默认使用“Private”,使用: - $HOME/.Private 作为源 - $HOME/Private 作为目标 - $HOME/.ecryptfs/Private.sig 作为密钥签名。

如果指定了 ALIAS,则程序将在以下位置查找 fstab(5) 样式配置: - $HOME/.ecryptfs/ALIAS.conf 并在以下位置查找密钥签名: - $HOME/.ecryptfs/ALIAS.sig

当且仅当满足以下条件时,安装才会继续: - 所需的密码位于其内核密钥环中,并且 - 当前用户同时拥有 SOURCE 和 DESTINATION 安装点 - DESTINATION 尚未安装

该计划将:

  • 将 SOURCE 安装到 DESTINATION
  • 作为 ecryptfs 文件系统
  • 使用 AES 密码
  • 密钥长度为 16 字节
  • 使用签名位于 ~/.ecryptfs/Private.sig 中的密码

或者通读man ecryptfs并设置您自己的行,使用密钥文件或文件描述符或变量以您认为合适的mount方式获取命令的密码。mount就像mount -t ecryptfs [SRC DIR] [DST DIR] -o [OPTIONS]注意以下选项一样:

   passphrase_passwd=(passphrase)
          The actual password is passphrase. Since the password is  visible
          to  utilities  (like ps under Unix) this form should only be used
          where security is not important.

   passphrase_passwd_file=(filename)
          The   password   should   be   specified   in   a    file    with
          passwd=(passphrase).  It  is  highly recommended that the file be
          stored on a secure medium such as a personal usb key.

   passphrase_passwd_fd=(file descriptor)
          The password is specified through the specified file descriptor.

   openssl_keyfile=(filename)
          The filename should be the filename of a file containing  an  RSA
          SSL key.

etc...

但是,如果每当用户登录时加密目录就会被解密/挂载,那么您所要做的就是登录,不是吗?

答案3

这是一种不需要 sudo 或临时文件并且适用于文件名加密的方法。我基于 ecryptfs-mount-private 脚本(这是一个 shell 脚本,因此您也可以复制并直接编辑它)。

WRAPPED_PASSPHRASE_FILE="$HOME/.ecryptfs/wrapped-passphrase"
LOGINPASS="YOUR PASSWORD"

# If not using filename encryption
# printf "%s\0" "$LOGINPASS" | ecryptfs-unwrap-passphrase "$WRAPPED_PASSPHRASE_FILE" - | ecryptfs-add-passphrase -

# If using filename encryption
printf "%s\0" "$LOGINPASS" | ecryptfs-insert-wrapped-passphrase-into-keyring "$WRAPPED_PASSPHRASE_FILE" -

# Try mounting
/sbin/mount.ecryptfs_private >/dev/null 2>&1

相关内容