44

如何在kali Linux上创建一个用于隐藏文件的加密容器?

 5 years ago
source link: http://www.freebuf.com/articles/database/184683.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

作为渗透测试人员,有时我们需要长期的存储一些较为敏感的客户数据。为了保证这些数据的安全性,我们会使用到各种加密手段及算法。虽然,这在很大程度上保证了我们数据的安全性,但有关加密学和 加密学的主题 非常庞大和复杂。因此,本文我将教大家在Kali Linux上创建一个简易但非常安全的,用于隐藏文件的加密容器(container )。当容器被锁定时,除你之外将没有任何人能够查看存储在其中的文件。

注:如果你的系统是Ubuntu或Debian,为了保证安全性请使用非root权限运行以下命令,并且只在需要时添加sudo。

创建一个解锁容器的密钥

首先,我们来创建一个文件作为解锁容器的主密钥。这里我不建议大家使用密码,如果使用密码的话攻击者很可能会实施爆破攻击。

dd if=/dev/urandom of=/path/to/master.keyfile bs=4096 count=1

1+0 records in
1+0 records out
4096 bytes (4.1 kB, 4.0 KiB) copied, 0.000300577 s, 13.6 MB/s

以上的密钥文件可以取任意名称。为了简单起见,我使用的名称为“master.keyfile”。 dd 命令将随机(/dev/urandom)生成一个4096字节的密钥文件,这比我们能想到的任何可能的32字符密码更为强壮。你可以将该密钥文件保存在你的 U盘 或远程服务器上。但切记千万不要将其共享或电子邮件发送甚至丢失。因为一旦丢失的话,容器将永远无法被解锁!

创建一个空容器

接着,我们使用dd和 zero 命令来创建一个空容器(根据需求指定创建容器大小)。

dd if=/dev/zero of=/path/to/CONTAINER bs=1 count=0 seek=1G

0+0 records in
0+0 records out
0 bytes copied, 0.000190114 s, 0.0 kB/s

以上我们可以将“CONTAINER”命名为“Vault”或“Encrypted”以方便我们的查找。或者如果你想要更加隐蔽,你也可以将其命名为像“TaylorSwift.mp3”这类的名称,并将其放在歌曲目录中以规避被动取证。

加密容器

使用 cryptsetup 命令加密容器。

sudo cryptsetup -v -y -c aes-xts-plain64 -s 512 -h sha512 -i 5000 --use-random luksFormat /path/to/CONTAINER /path/to/master.keyfile

WARNING!
========
This will overwrite data on /tmp/CONTAINER irrevocably.

Are you sure? (Type uppercase yes): YES
Key slot 0 created.
Command successful.

以上我将cipher( -c ),密钥大小(-s)和hash(-h)都设置为了高强度值。cryptsetup命令将格式化(luksFormat)CONTAINER,并对其进行加密。

系统会要求你输入“YES”以进行下一步操作。此过程可能需要等待几分钟的时间,具体取决于你容器的大小。100 GB大小的容器需要大概5分钟的时间才能完成。

解锁容器

完成后,我们需要解锁CONTAINER格式化卷(volume)。

sudo cryptsetup luksOpen /path/to/CONTAINER PRIVATE --key-file /path/to/master.keyfile

cryptsetup命令将使用之前我们创建的“master.keyfile”( –key-file )解锁(luksOpen)我们的容器。 mapper point (“PRIVATE”) 用于映射(以及稍后挂载)设备和卷。名称“PRIVATE”是任意的,你也可以使用其它的名称。

格式化未锁定卷

格式化未锁定的CONTAINER,将允许我们实际读取和写入卷的数据。有许多可用的格式类型,如 FAT32和NTFS 。这里我将使用 Ext4 ,一种广泛支持且灵活的Unix格式。任何Debian或Ubuntu机器,都能够解锁和读取存储在容器中的文件。

使用以下 mkfs 命令格式化未锁定容器。

sudo mkfs.ext4 /dev/mapper/PRIVATE

mke2fs 1.44.3 (10-July-2018)
Creating filesystem with 261632 4k blocks and 65408 inodes
Filesystem UUID: 948eae1b-dd6d-4910-8a3c-3474488bdf0c
Superblock backups stored on blocks:
  32768, 98304, 163840, 229376

Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

将未锁定卷挂载到本地目录

首先,我们使用 mkdir 命令来创建一个目录(名称任意)。当解锁卷和挂载后,将可在该目录下访问容器中的文件。 

mkdir ~/Private

然后,使用以下命令将未锁定卷 挂载 到新创建的〜/Private目录。

sudo mount /dev/mapper/PRIVATE ~/Private

Kali用户可以使用文件管理器导航到/root/Private目录访问内容。

baIRnya.jpg!web

Ubuntu和Debian用户可以导航到/home/<username>/Private目录访问内容。

设置文件权限

设置文件权限对Ubuntu和Debian用户尤为重要。设置卷权限,可以方便非root用户在容器未锁定时访问其中的数据。否则,将只有root权限的用户才能更改容器中的内容。

sudo chown -R "$USER":"$USER" ~/Private

chown命令将递归(-R)将未锁定容器及其内容的权限,更改为当前用户 ($USER) 权限设置。

完成后锁定容器

在容器中添加或删除文件后,使用下面的umount命令 卸载 卷并锁定(luksClose)容器。

sudo umount /root/Private && sudo cryptsetup luksClose PRIVATE

需要时解锁容器

之后,当你需要解锁容器时可以使用以下命令(luksOpen)容器。

sudo cryptsetup luksOpen /path/to/CONTAINER PRIVATE --key-file /path/to/master.keyfile

然后,使用mount命令将卷挂载到本地目录。

sudo mount /dev/mapper/PRIVATE ~/Private

自动创建安全容器(可选)

这里我编写了一个交互式可用于自动化创建安全容器的Bash脚本。一旦运行,你只需输入容器名称,密钥文件名,挂载点及容器大小即可。其余操作都将自动为你完成,且整个过程不到一分钟的时间。

#!/bin/bash

    G="\033[1;32m";
    N="\033[0;39m";
    function notification ()
    {
        echo -e "$G" "\n[+] $1" "$N"
    };
    function nameVol ()
    {
        read -p "Name of encrypted container (e.g., "Vault", "grocerylist.txt"): " vol_name;
        if [[ ! -n "$vol_name" ]]; then
            vol_name='EncryptedContainer';
        fi
    };
    function nameKey ()
    {
        read -p "Name of Key file (e.g., "master.keyfile", "image.jpg"): " key_file;
        if [[ ! -n "$key_file" ]]; then
            key_file='master.keyfile';
        fi
    };
    function nameMount ()
    {
        read -p "Where to mount the container when it's unlocked (e.g., "luksPrivate"): " mount_dir;
        if [[ ! -n "$mount_dir" ]]; then
            mount_dir='luksPrivate';
        fi
    };
    function nameSize ()
    {
        read -p "Choose volume size (e.g., 10G, 200M): " vol_size;
        if [[ ! -n "$vol_size" ]]; then
            vol_size='1G';
        fi
    };
    function ddZero ()
    {
        dd if=/dev/zero of="$vol_name" bs=1 count=0 seek="$vol_size" && notification "Empty volume created."
    };
    function ddRandom ()
    {
        dd if=/dev/urandom of="$key_file" bs=4096 count=1 && notification "Key file successfully created."
    };
    function encryptCon ()
    {
        sudo cryptsetup -v -y -c aes-xts-plain64 -s 512 -h sha512 -i 5000 --use-random luksFormat "$vol_name" "$key_file" && notification "Encrypted container created."
    };
    function encryptOpen ()
    {
        sudo cryptsetup luksOpen "$vol_name" "$mount_dir" --key-file "$key_file" && notification "Volume unlocked."
    };
    function mkfsFormat ()
    {
        sudo mkfs.ext4 /dev/mapper/"$mount_dir" && notification "Volume formatted."
    };
    function mountDir ()
    {
        if [[ ! -d $HOME/"$mount_dir"/ ]]; then
            mkdir -p $HOME/"$mount_dir"/;
        fi;
        sudo mount /dev/mapper/"$mount_dir" "$HOME"/"$mount_dir"/ && notification "Volume mounted."
    };
    function volPerm ()
    {
        sudo chown -R "$USER":"$USER" "$HOME"/"$mount_dir" && notification "Volume permissions set. Don't lose the Key file!"
    };
    nameVol;
    nameKey;
    nameMount;
    nameSize;
    ddZero;
    ddRandom;
    encryptCon;
    encryptOpen;
    mkfsFormat;
    mountDir;
    volPerm

将上述脚本保存到名为“createContainer.sh”的文件中,并赋予文件可执行(chmod)权限,然后运行文件。

chmod +x createContainer.sh
./createContainer.sh

截图:

I3yIJbq.jpg!web

  *参考来源: wonderhowto FB小编 secist 编译,转载请注明来自FreeBuf.COM


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK