Pico Org
854 字
4 分钟
qcow2取证分析环境构建

QCOW2取证分析环境构建#

在日常工作中,难免遇到一些镜像分析工作,当我们采用人工分析时,需要的常见命令在本文中进行总结。

一、QCOW2简介#

KVM虚拟化中使用的镜像格式通常为RAWQCOW2两种格式,QCOW2镜像格式是目前虚拟化中主流的镜像格式。

原文QEMU copy-on-write format with a range of special features, including the ability to take multiple snapshots, smaller images on filesystems that don’t support sparse files, optional AES encryption, and optional zlib compression.

QCOW(QEMU copy-on-write)镜像文件具有一系列特性, 包括多重快照(能够创建基于之前镜像的新镜像,速度更快),体积更小(不支持稀疏特性,不会预先分配指定Size的存储空间),可选AES加密方式,可选zlib压缩方式等功能。

二、qemu-img的基本使用#

  1. 创建 QCOW2 镜像文件

    $ qemu-img create -f qcow2 img.qcow2 4G
    Formatting 'img.qcow2', fmt=qcow2 size=4294967296 encryption=off cluster_size=65536 lazy_refcounts=off
  2. 查看 QCOW2 镜像文件信息

    $ qemu-img info img.qcow2
    image: img.qcow2
    file format: qcow2
    virtual size: 4.0G (4294967296 bytes)
    disk size: 196K
    cluster_size: 65536
    Format specific information:
        compat: 1.1
        lazy refcounts: false
  3. 创建 QCOW2 的快照

    $ qemu-img snapshot -c snap img.qcow2
    # 再次查看 QCOW2 镜像文件信息, 可以看见快照列表
  4. 删除 QCOW2 镜像文件的快照

    $ qemu-img snapshot -d snap img.qcow2
    # 再次查看 QCOW2 镜像文件信息, 可以看见快照被删除
  5. 快照恢复

    $ qemu-img snapshot -a snap img.qcow2
    # 以快照位置启动
  6. QCOW2 镜像文件转换成 RAW 格式

    $ qemu-img convert img.qcow2 -O raw raw.img
    # 转化为RAW格式

三、分析环境构建#

大部分工具包含在libguestfs库中,相应工具下载release版本guestfs-tools即可。

3.1 文件系统挂载#

  1. 安装

    $ sudo apt-get install guestfs-tools
    guestfs-tools is already the newest version (1.52.0-3).
  2. 挂载

    # 创建挂载点
    $ mkdir -p /mnt/qcow2
    # 只读模式
    $ sudo guestmount -a /path/to/img.qcow2 -i --ro /mnt/qcow2
    # 读写模式
    $ sudo guestmount -a /path/to/img.qcow2 -i /mnt/qcow2

3.2 Linux镜像密码修改#

当处于特殊情况无法获得Linux用户密码,启动镜像又无法登陆进系统时,就需要考虑通过修改镜像的方式强制更新密码。方法有很多,这里列举工具和手工两种方式

3.2.1 virt-customize工具#

  1. 安装

    $ sudo apt-get install guestfs-tools
    guestfs-tools is already the newest version (1.52.0-3).
  2. 修改root密码

    $ sudo virt-customize -a /path/to/img.qcow2 --root-password password:1234569
    [   0.0] Examining the guest ...
    [  15.0] Setting a random seed
    [  15.3] Setting passwords
    [  21.7] SELinux relabelling
    [  21.9] Finishing off

3.1.2 guestfish手动修改#

  1. 安装

    $ sudo apt-get install libguestfs-tools
    libguestfs-tools is already the newest version (1:1.52.0-6.1).
  2. 生成随机密码

    $ openssl passwd -1 123456
    $1$YA53Va4U$F1w1stz83NO8ee4kCgtzQ1
  3. 进入文件系统

    $ sudo guestfish --rw -a /path/to/img.qcow2
    
    Welcome to guestfish, the guest filesystem shell for
    editing virtual machine filesystems and disk images.
    
    Type: ‘help’ for help on commands
        ‘man’ to read the manual
        ‘quit’ to quit the shell
    
    ><fs> 
  4. 修改密码

    ><fs> run
    100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ --:--
    ><fs> list-filesystems 
    /dev/sda1: unknown
    /dev/sda2: ext4
    ><fs> mount /dev/sda2 / # 选择文件系统挂载
    ><fs> vi /etc/shadow # 修改root密码为openssl生成的随机密码
                         # 形如root:!!:19017:0:99999:7:::
                         # 改为root:$1$YA53Va4U$F1w1stz83NO8ee4kCgtzQ1:19017:0:99999:7:::
    ><fs> quit

3.3 qemu启动镜像#

  1. qemu挂载文件系统

    # -hda: 镜像路径
    # -m: 内存
    # -net: 转发ssh端口到主机的40022
    $ qemu-system-x86_64 -hda /path/to/img.qcow2 -m 8G -net nic -net user,hostfwd=tcp::40022-:22
  2. qemu serial console登陆

    直接输入账号root和修改后的密码123456即可。

    localhost login: root
    password:
  3. ssh登陆

    # 根据提示输入密码
    $ ssh root@localhost -p40022
    root@localhost's password:
qcow2取证分析环境构建
https://picoorg.github.io/posts/qcow2取证分析环境构建/
作者
Pico Org
发布于
2024-08-30
许可协议
CC BY-NC-SA 4.0