Skip to content
KitploitKITPLOIT
工具博客
提交
工具博客
提交

黑客、渗透测试和网络安全工具,武装您的安全武器库!

Kitploit 是一个黑客、网络安全和渗透测试工具的目录。发现最新的项目更新,查找漏洞、分析系统、自动化测试并加强你的安全。

··订阅源·联系·隐私·© 2026 Kitploit

工具目录

分类

查看所有分类
Loading categories
mofos — 虚拟机操作框架 | Kitploit
工具/GitHubGitHub/synacktiv/mofos
脚本与自动化安全虚拟化网络安全渗透测试实用工具与框架红队
GitHubsynacktiv/mofos

mofos

虚拟机操作框架

查看仓库
10865个月前Kitploit 审核通过

最受欢迎

查看全部 →

发现我们社区最常用的工具。

探索所有工具

浏览我们的工具集合

查看所有工具 →
分享
  • mofos
    • 概念
    • 安装
    • 配置宿主机
      • QEMU/KVM 系统会话
      • 安全注意事项
    • Mofos 配置
    • 安装模板
      • 配置防火墙
      • 安装
      • 导入
    • 创建空的上层磁盘
    • 创建虚拟机
    • 自定义
      • 模板 - 安装钩子
      • 虚拟机 - 新建钩子
      • 虚拟机 - 启动钩子
    • 功能特性
      • 设置启动或停止虚拟机时的通知
      • 剪贴板
      • 设置机器间的路由
      • 设置通过渗透测试主机的隧道
      • USB 管理
      • PCI 设备管理
      • 共享文件夹
    • Windows 虚拟机
    • 自动补全
    • IP 地址重叠

mofos

Mofos 是一款用于创建、运行和管理虚拟机的工具。它基于 Libvirt/QEMU/KVM 和 Python,因此兼容任何 Linux 发行版。Mofos 深受 Qubes OS (https://www.qubes-os.org/) 的启发,旨在复现其许多功能。

该工具已在 Debian 及基于 Debian 的虚拟机上进行了广泛测试。虽然其他 Linux 发行版预计也能正常运行,但可能需要进行一些额外配置。更多细节有待补充。

Mofos 提供了一系列专注于安全地管理虚拟机的功能,包括:

  • 使用 Xpra 实现无缝窗口集成。
  • 剪贴板系统,支持在宿主机与虚拟机之间发送和接收剪贴板内容。
  • 通过 SSH 与虚拟机通信。
  • 默认网络限制,并提供灵活选项,可将流量从一台虚拟机路由或隧道传输到其他虚拟机或远程服务器。

概念

一台 mofos 机器由两个通过 overlayfs 组合在一起的磁盘组成。第一个磁盘称为下层,是一个只读的模板磁盘;第二个磁盘则存储虚拟机所做的所有更改。这个模板磁盘在多台虚拟机之间共享。因此,创建新虚拟机只需克隆一个已经分好区、用于保存修改数据的空磁盘。这种方法确保可以快速创建新虚拟机,同时允许模板独立更新。模板的任何更新都将在依赖它的虚拟机下次重启时生效。

安装

根据 Linux 发行版的不同,可以利用 Makefile 来生成 deb 包或直接安装这些文件。``` make deb apt install ./mofos-VERSION.deb

root@kitploit:~
在 `apt` 安装过程中,会提示各种设置。通常可以接受默认选项。唯一需要注意的设置是 mofos libvirt 网络使用的子网地址(默认:`192.168.90.0/24`)。

或者 

安装以下依赖:
- guestfs-tools
- libnotify-bin
- libvirt
- libvirt-clients
- libvirt-daemon
- make
- python3-click
- python3-click-completion
- python3-colorama
- python3-cryptography
- python3-dbus
- python3-jinja2
- python3-lxml
- python3-prettytable
- python3-pyroute2
- python3-tqdm
- qemu-system-common 
- qemu-system-modules-spice
- socat
- spice-client-gtk
- sudo
- virtinst
- virt-install
- virtiofsd
- virt-manager
- virt-viewer```
make install_files

Depending on the distribution, the Python files copied to /usr/lib/python3/dist-packages may not be detected by the Python interpreter and should be placed elsewhere. For example, on Fedora, the Python files must be copied to /usr/lib/python3.11/site-packages.

[!WARNING] 注意,自 Debian trixie 起,xpra 不再打包发布,您必须从其自定义软件仓库手动安装。有关详细说明,请参阅 https://github.com/Xpra-org/xpra/wiki/Download#-for-debian-based-distributions。

配置宿主机

QEMU/KVM 系统会话

Mofos 使用 QEMU/KVM 系统会话,因此要允许 virsh 命令访问虚拟机及相关资源,请将环境变量 LIBVIRT_DEFAULT_URI 设置为 qemu:///system:```console export LIBVIRT_DEFAULT_URI=qemu:///system

root@kitploit:~
### 安全考量

使用 QEMU/KVM 系统会话可提高宿主机与客户机虚拟机之间的隔离性,其做法是在专用用户(`libvirt-qemu`)下运行 qemu 实例,并为每个实例应用特定的安全配置文件。

然而,默认情况下,普通用户无法与 `libvirtd` 系统套接字交互,也就无法管理机器、网络及其他资源。要获得访问权限,用户要么必须是 libvirt Unix 组的成员,要么使用 sudo。从历史上看,本地权限提升漏洞正是利用 libvirt 组成员身份来获取 root 权限的。

为降低这些风险,本仓库在基于 AppArmor 的系统上为 `libvirtd` 进程提供了强化的 AppArmor 配置文件。该配置文件显著限制了 `libvirtd` 可写入文件的位置以及可执行的程序。

此外,还包含了 `polkit` 规则,以进一步控制允许 `libvirt` 组成员执行的操作。

请注意,AppArmor 配置文件随 `deb` 包一起发布,但不会由 Makefile 的 `install_files` 目标安装,因此必须单独安装。

## Mofos 配置

Mofos 需要一个位于 `$HOME/.config/mofos/config.toml` 的配置文件,并包含最少量的设置才能正常运行。最小示例配置可在 `/usr/share/mofos/config.minimal.toml` 中找到,而更全面的配置则记录在 `/usr/share/mofos/config.sample.toml` 中。

以下错误表示未找到配置文件:```
[-] Copy the sample configuration file from /usr/share/mofos/config.minimal.toml to ~/.config/mofos/config.toml

以下错误表明当前用户不是 libvirt 组的成员:``` [-] libvirtError("authentication unavailable: no polkit agent available to authenticate action 'org.libvirt.unix.manage'")

root@kitploit:~
需要在配置文件中自定义的关键配置设置如下:
- key (路径):用于访问虚拟机的 SSH 私钥文件。建议为此目的创建专用密钥。
- user (字符串):用于 SSH 访问虚拟机的用户名。
- root_password (哈希值):安装新模板期间要设置的 root 密码哈希值。
- root_ssh_pubkey (字符串):模板安装期间要安装到 root 用户目录中的 SSH 公钥。

此外,还必须为模板安装配置以下参数:
- ntp
- dns (如果提供了代理,则可选)
- proxy

## 安装模板

### 配置防火墙

由于安装过程依赖于 PXE 网络启动,因此需要有效的互联网连接。应配置以下防火墙规则:```
sysctl net.ipv4.ip_forward=1
iptables -t nat -I POSTROUTING -s 192.168.90.0/24 -j MASQUERADE
iptables -t nat -I POSTROUTING -s 192.168.91.0/24 -j MASQUERADE

或者使用 nftables:``` sysctl net.ipv4.ip_forward=1 nft insert inet nat postrouting iifname "install-*" masquerade nft insert inet nat postrouting iifname "mof0" masquerade

root@kitploit:~
当 `ip_forward` 参数设置为 1 时,应配置 FORWARD 链,以防止网络上的其他设备将主机用作路由器。

总的来说,建议采用以下规则:```
iptables -I INPUT -i mof0 -p udp --sport 68 --dport 67 -j ACCEPT -m comment --comment "mofos dhcp"
iptables -I INPUT -i mof0 -p udp --dport 69 -j ACCEPT -m comment --comment "mofos tftp"
iptables -I OUTPUT -o mof0 -j ACCEPT -m comment --comment "host -> mofos"
iptables -I FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -I FORWARD -i mof0 -j ACCEPT -m comment --comment "mofos ->"
iptables -t nat -I POSTROUTING -s 192.168.90.0/24 -j MASQUERADE
iptables -t nat -I POSTROUTING -s 192.168.91.0/24 -j MASQUERADE

或者使用 nftables:``` table inet filter {

root@kitploit:~
chain input {
  type filter hook input priority 0; policy drop
  
  iifname "install-*" ip daddr 255.255.255.255 udp sport 68 udp dport 67 accept comment "mofos dhcp"
  iifname "mof0" ip daddr 255.255.255.255 udp sport 68 udp dport 67 accept comment "mofos dhcp"
  iifname "install-*" udp dport 69 accept comment "mofos tftp"
}

chain forward {
  type filter hook forward priority 0; policy drop
  
  ct state established,related accept;
  ct state invalid drop;
  
  iifname "mof0" counter accept
  iifname "install-*" counter accept
}

chain output {
  type filter hook output priority 0; policy drop
  
  oifname "mof0" counter accept
}

} table inet nat { chain postrouting { type nat hook postrouting priority 100 iifname "mof0" masquerade iifname "install-*" masquerade } }

root@kitploit:~
It is recommended to restrict masquerade and forwarding rules based on your specific needs, to prevent mofos virtual machines from accessing the entire host network.

建议根据您的具体需求限制 masquerade 和转发规则,以防止 mofos 虚拟机访问整个主机网络。

Isolation between virtual machines is automatically enforced by a libvirt hook, which requires nft to function properly.

虚拟机之间的隔离由 libvirt hook 自动强制执行,这需要 nft 才能正常运行。

### Install

### 安装

The first step is to build the initial layer: the template. By default, Mofos can install a Debian 12–based template.

第一步是构建初始层:模板。默认情况下,Mofos 可以安装基于 Debian 12 的模板。```
mofos template create debian-template

注意:对于每个命令,都可以使用 --debug 选项来获取详细的技术信息,以防出现意外行为。具体到本命令,--debug 标志还会强制显示一个图形窗口以展示安装进度。或者,也可以使用 virt-manager 来监控安装过程。

目前,仅支持 Debian 12 模板进行安装。下面的 Python 字典指定了要使用的安装映像:```python NETBOOT = { "debian-stable-amd64": { "variant": "debian11", "url": "https://deb.debian.org/debian/dists/stable/main/installer-amd64/current/images/netboot/netboot.tar.gz", } }

root@kitploit:~
变体设置为 `debian11`,因为在 Debian 12 中,其 `osinfo` 变体尚无法通过 libvirt 安装。

要安装其他发行版,请修改位于 `/usr/lib/python3/dist-packages/mofos/settings.py` 中的字典。

运行上述命令时,Mofos 会下载 netboot 文件并将 `tar.gz` 压缩包缓存到 `$HOME/.cache/template-installer`。目前,如果 netboot 压缩包已存在,Mofos 不会再次下载。如果缓存的压缩包已过时,这可能会导致错误。如果安装过程中出现此类错误,删除缓存的压缩包将强制 Mofos 下载更新版本,从而解决该问题。

接下来,压缩包被解压到 `/tmp`,并配置 libvirt 通过 TFTP 提供其内容。

然后创建模板虚拟机,并将其设置为通过 PXE 引导,使用提供的 preseed 文件(默认为 `/usr/share/mofos/templates/debian/preseed.cfg.j2`)安装指定发行版。该文件是一个 `jinja2` 模板;在将其复制到 TFTP 根目录之前,会注入配置文件(`ntp`、`proxy`、`dns`、`root_password`)中的变量。

安装结束时,`postinstall` 脚本会被放置到 TFTP 目录中并在模板上执行。默认使用位于 `/usr/share/mofos/templates/postinstall.sh.j2` 的脚本。此 `jinja2` 模板会注入要在模板上配置的 SSH 公钥。

除了配置 root 的 SSH 公钥外,还会执行以下操作:
- 禁用标准 SSH 服务,并启用基于虚拟套接字(vsock)的 SSHD。
- 清空 `/etc/resolv.conf` 文件。
- 将 GRUB 超时设置为 0 秒。
- 安装一个 initramfs 钩子,当检测到标记为 `overlay` 的分区时挂载 overlayfs。

安装完成后,libvirt 虚拟机被获取并压缩为本地 `qcow2` 文件,保存到当前目录。```
$ mofos template create debian-template
[*] Installing debian-template
[*] Configure the SSH host key of the template
[*] Waiting for the installation to be complete
[*] Installation is complete
[*] Downloading the resulting qcow2 disk
[*] Compressing the disk
[*] Save template's public ssh host key
[+] Template installation finished
[+] Template disk is debian-template-disk.qcow2

除了 qcow2 文件之外,此过程还会在 $HOME/.local/share/mofos/ssh.json 文件中创建一个条目,其中包含机器的名称及其公共 SSH 密钥:```json { "disk": { "debian-template-disk.qcow2": "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINrbOdOPENEj2KeHrYLdorQe9Ez1b9Bu5agZmgNDMayy" } }

root@kitploit:~
此文件在导入过程中会自动使用。

### 导入

下一步是将创建的 `qcow2` 模板文件导入 Mofos。```
$ mofos template import debian-template debian-template-disk.qcow2 http://debian.org/debian/11 
[*] Uploading debian-template-disk.qcow2 to mofos pool
[*] Creating the virtual machine debian-template
[*] Configuring the template metadata
[*] Configuring the SSH key
[+] debian-template successfully imported

目前,osinfo 变体是必需的。它们可以在文件中找到: /usr/lib/python3/dist-packages/mofos/settings.py。

导入后,可以在 mofos ls 命令中看到模板:``` $ mofos ls +----+-----------------+---------+-------------+-------+-----+--------------+ | Id | Name | State | Description | Alias | Cid | IPv4 address | +----+-----------------+---------+-------------+-------+-----+--------------+ | | debian-template | shutoff | | | | | +----+-----------------+---------+-------------+-------+-----+--------------+

root@kitploit:~
从此时起,即可启动该模板,并可根据需要访问以进行修改。```
$ mofos start debian-template
$ mofos ls
+----+-----------------+---------+-------------+-------+-----+----------------+
| Id | Name            | State   | Description | Alias | Cid | IPv4 address   |
+----+-----------------+---------+-------------+-------+-----+----------------+
| 3  | debian-template | running |             |       | 3   | 192.168.90.202 |
+----+-----------------+---------+-------------+-------+-----+----------------+

The -rpc flag enables the SQLite RPC API. This is the only way to obtain columns from HIVE tables using insert statements. It is also required to create tables using the create statement with a custom schema. Otherwise, tables are created with an ad-hoc schema based on the inserted rows.

After starting the listener and executing commands via BeEF or other means, this server will log connections. The -sqlite <db> flag enables logging to SQLite, and the -pcap <file> flag enables logging to a PCAP file.

The following example will start the MySQL server, log to a SQLite database, and write to a PCAP file:

root@kitploit:~
./mysqlsv -i 0.0.0.0 -p 3306 -rpc -sqlite /root/log.sqlite -pcap /root/mysql.pcap

When connecting with a client like mysql or sqlite3, you can use any username and password you want:

root@kitploit:~
mysql -h 127.0.0.1 -u root -p

Performing queries will return the table and column names of the actual mock data, as shown below:

root@kitploit:~
mysql> use db1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+----------------+
| Tables_in_db1  |
+----------------+
| employees      |
| departments    |
+----------------+
2 rows in set (0.00 sec)

mysql> select * from employees;
+-------+-----------+---------+
| name  |   city    | salary  |
+-------+-----------+---------+
| Alice | New York  |  80000  |
| Bob   | Chicago   |  75000  |
...

Note: The -python flag enables a Python 2 compatible SQLite RPC API.``` $ mofos ssh debian-template --user root root@linux:~#

root@kitploit:~
## 创建上层的空磁盘

以下命令在磁盘的单个分区上创建一个带有指定标签的空上层:```
$ mofos template create-overlay-disk

默认情况下,此磁盘设置为 50 GB,但最初仅占用约 100 MB。此大小可在配置文件中自定义。

创建虚拟机

注意:建议在操作 Mofos 虚拟机之前始终关闭模板。虽然支持同时运行模板及其虚拟机,但这可能会导致不稳定。``` $ mofos new test [] New virtual machine name is test [+] Virtual machine test successfully created [] Triggering post install actions [] Create SSH known_hosts entries for test [] Waiting for test to be up [+] test is ready $ mofos ssh test -u root Last login: Mon Jun 2 11:09:51 2025 from UNKNOWN root@linux:~#

root@kitploit:~
请记住,SSH 连接是通过虚拟套接字建立的,因此您无法直接 SSH 进入新创建的机器。相反,您必须使用 `mofos ssh` 命令。或者,您可以创建一条 SSH 配置,指定 `ProxyCommand ` 来访问该机器的 SSH 端口:```
$ mofos inventory --format ssh
Host debian-template
	User user
	PasswordAuthentication no
	IdentityFile /home/user/.ssh/id_ed25519
	ProxyCommand /usr/bin/mofos proxy-cmd %h
	CanonicalizeHostname=no

Host test
	User user
	PasswordAuthentication no
	IdentityFile /home/user/.ssh/id_ed25519
	ProxyCommand /usr/bin/mofos proxy-cmd %h
	CanonicalizeHostname=no

根据所选服务,脚本动态生成并执行查询以收集以下信息:

  1. 可用的命名空间/数据库:检索并显示所有可用的命名空间或数据库。

可用的命名空间/数据库图片

  1. 每个命名空间/数据库中的集合/表:列出每个命名空间或数据库中存在的所有集合或表。

每个命名空间/数据库中的集合/表 GIF

  1. 文档/行计数:显示每个集合的文档计数或每个表的行计数。

文档/行计数 GIF

  1. 前5个文档/行:从每个集合中检索并显示前5个文档,或从每个表中显示前5行。

前5个文档/行 GIF

[!WARNING] Pinkerton 仅用于教育和研究目的。作者不对任何误用或由此工具造成的损害负责。用户有责任确保在使用 Pinkerton 时遵守所有适用的法律法规。

🔨 用法

要使用 Pinkerton,请运行以下命令:``` $ mofos inventory --format ssh > ~/.ssh/mofos $ echo "Include ~/.ssh/mofos" >> ~/.ssh/config $ ssh root@test Linux linux 6.1.0-37-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.140-1 (2025-05-22) x86_64

The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Mon Jun 2 11:09:51 2025 from UNKNOWN root@linux:~#

root@kitploit:~
在 `mofos ssh` 会话期间,会预先建立一个 `ControlMaster` 套接字以加速后续连接。这就是为什么不会显示 MOTD。

## 自定义

为了简化模板和虚拟机的自定义,Mofos 引入了钩子(hooks)和标签(tags)的概念。对于每个标签,可以执行相应的钩子(一个 Bash 脚本)来对目标虚拟机或模板执行自动化操作,并据此对其进行配置。

最小钩子位于 `/usr/share/mofos/hooks`。这些是简单的 Bash 脚本,接收以下输入:```
TAG=$1
NAME=$2
OS=$3
DISTRIB=$4
HOSTNAME=$5

模板 - 钩子安装

该机制可借助 Ansible 在模板安装过程中执行 playbook:```bash #!/bin/bash

During template installation, the machine is not yet managed by mofos,

the inventory would not working.

A custom inventory is created on the fly using the given tag as main ansible

group and the distribution as secondary.

TAG=$1 NAME=$2 OS=$3 DISTRIB=$4 HOSTNAME=$5

if [ -z "${TAG}" ] || [ -z "${NAME}" ] || [ -z "${DISTRIB}" ] ; then exit 1 fi

Create inventory

ANSIBLE_DIRECTORY="/home/user/Documents/ansible" RANDOM_SUFFIX=$(printf "%x" $RANDOM) INVENTORY_FILE="${ANSIBLE_DIRECTORY}/inventory-${NAME}-${RANDOM_SUFFIX}.ini"

trap 'rm -f "${INVENTORY_FILE}"; exit' EXIT

cat > $INVENTORY_FILE <<EOF [all:vars] ansible_ssh_common_args="-o ProxyCommand='mofos proxy-cmd %h' -o CanonicalizeHostname=no"

[${TAG}] ${NAME}

[${DISTRIB}] ${NAME} EOF

export ANSIBLE_CONFIG="/home/user/Documents/ansible/ansible.cfg" export ANSIBLE_VERBOSITY=1

/usr/bin/ansible-playbook
-i "${INVENTORY_FILE}"
-l "${NAME}"
/home/user/Documents/ansible/playbooks/pentest/install.yml

root@kitploit:~
该脚本动态创建清单(inventory)并针对其运行 playbook。

### 虚拟机 - 新挂接点(hook new)

在虚拟机创建过程中,可以使用另一个挂接点(hook),例如用于随机化主机名。

默认情况下,Mofos 会基于经典的 Windows 命名约定生成别名(例如 `DESKTOP-2BF9753`)。该名称以及其他信息会传递给挂接点脚本:```
#!/bin/bash

TAG=$1
NAME=$2
OS=$3
DISTRIB=$4
HOSTNAME=$5

if [ -z "${TAG}" ] || [ -z "${NAME}" ] || [ -z "${OS}" ] ; then
  exit 1
fi

if [ -z "${HOSTNAME}" ] ; then
  HOSTNAME="${NAME}"
fi

mofos run -u root "${NAME}" "echo ${HOSTNAME} > /etc/hostname && hostname ${HOSTNAME}"

接下来,应编辑配置以启用此钩子:``` [hooks.test] new = "/home/user/.config/mofos/hooks/new.sh"

root@kitploit:~
然后,在创建虚拟机期间,将执行此脚本:```
$ mofos new test2 --tags test 
[*] New virtual machine name is test2
[+] Virtual machine test2 successfully created
[*] Triggering post install actions
[*] Create SSH known_hosts entries for test2
[*] Waiting for test2 to be up
[*] Running new hook: test
[+] test2 is ready

I received no translatable content in this chunk. Please provide the actual text for chunk 53.``` $ mofos ssh test2 -u root Last login: Mon Jun 2 11:09:51 2025 from UNKNOWN root@DESKTOP-2BF9753:~#

root@kitploit:~
与安装阶段类似,在此阶段也可以执行 Ansible 剧本。在这种情况下,可以使用 `mofos inventory` 命令生成 Ansible 清单,从而简化选择和访问虚拟机的过程:```
$ mofos inventory 
{
    "_meta": {
        "hostvars": {
            "debian-template": {
                "ansible_host": "debian-template",
                "ansible_ssh_common_args": "-o ProxyCommand='/usr/bin/mofos proxy-cmd %h'  -o CanonicalizeHostname=no"
            },
            "test": {
                "ansible_host": "test",
                "ansible_ssh_common_args": "-o ProxyCommand='/usr/bin/mofos proxy-cmd %h'  -o CanonicalizeHostname=no"
            },
            "test2": {
                "ansible_host": "test2",
                "ansible_ssh_common_args": "-o ProxyCommand='/usr/bin/mofos proxy-cmd %h'  -o CanonicalizeHostname=no"
            }
        }
    },
    "debian": [
        "debian-template",
        "test",
        "test2"
    ],
    "test": [
        "test2"
    ]

请注意,这些组是根据发行版变体以及标签创建的。这些组可用于加载不同的变量。

例如,下面的脚本运行任意一个 playbook:```bash #!/bin/bash

TAG=$1 NAME=$2 OS=$3 DISTRIB=$4 HOSTNAME=$5

if [ -z "${TAG}" ] || [ -z "${NAME}" ] || [ -z "${OS}" ] ; then exit 1 fi

if [ -z "$HOSTNAME" ] ; then HOSTNAME=$NAME fi

if [ $OS == "windows" ] ; then TAGS="hostname,desktop" else TAGS="hostname,hosts,desktop" fi

export ANSIBLE_CONFIG="/home/user/Documents/ansible/ansible.cfg" export ANSIBLE_VERBOSITY=0

/usr/bin/ansible-playbook
-i /home/user/Documents/ansible/inventory.py
-l "${NAME}"
-t "${TAGS}"
-e "hostname=${HOSTNAME}"
/home/user/Documents/ansible/playbooks/pentest/update.yml

root@kitploit:~
### Virtual machine - hook start

类似地,启动钩子(start hook)在虚拟机启动时执行。它通常用于启动虚拟机所需的服务。

例如,要在虚拟机内启用无缝的 Windows 集成,您可以安装 `Xpra`。

首先安装 `ansible`:```
# apt install ansible

接下来,运行位于 ansible 目录中的用户 playbook。在此之前,编辑 playbooks/user.yml 文件,更新要安装到用户主目录中的密码哈希和公共 SSH 密钥。``` ~/mofos/ansible$ ls ansible.cfg ansible.log inventory.sh playbooks

root@kitploit:~
The input chunk is empty — there is no content to translate. Please provide the actual source text for chunk 63 of 104.```
$ ansible-playbook playbooks/user.yml -l test2
Using /home/user/mofos/ansible/ansible.cfg as config file
[WARNING]: Found both group and host with same name: test

PLAY [Create and configure a user]

[...]

PLAY RECAP
test2 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0   

之后,只要配置将此用户指定为默认用户,您就可以 SSH 登录到该用户帐户:``` $ mofos ssh test2 user@DESKTOP-2BF9753:~$

root@kitploit:~
接下来,运行 `Xpra` playbook。该 playbook 会从 Debian 镜像安装软件包。请注意,Mofos 网络默认不会配置 DNS、代理或默认网关。必须在运行 playbook 之前设置好这些项。

默认情况下,无需对 playbook 进行任何修改。```
ansible-playbook playbooks/xpra.yml -l test2

此 playbook 在虚拟机上安装并配置 Xpra,并启动 xpra 服务。

远程服务可以使用以下方式手动启动:``` user@DESKTOP-2BF9753:~$ systemctl --user start xpra

root@kitploit:~
否则,服务将在下次重启时自动启动。

然后,本地客户端服务也可以手动启动:```
$ mofos xpra start test2

回到虚拟机中,显示应设置为 :10(默认值),之后可以启动图形应用程序,它将出现在宿主的桌面环境中。``` user@DESKTOP-2BF9753:$ export DISPLAY=:10 user@DESKTOP-2BF9753:$ xterm

root@kitploit:~
为了确保此机制顺利且自动地运行,可以从启动钩子中启动 `mofos xpra` 命令。```bash
#!/bin/bash

TAG=$1
NAME=$2
OS=$3
DISTRIB=$4
HOSTNAME=$5

if [ -z "${TAG}" ] || [ -z "${NAME}" ] ; then
exit 1
fi

mofos xpra start "${NAME}"

功能特性

在启动或停止虚拟机时设置通知

为了获知 libvirt/qemu 虚拟机的启动和停止情况,可以修改 libvirt 的 qemu hook,以指定主机用户的名称(默认为 user):```

sed 's/USER = "user"/USER = "foobar"/' /etc/libvirt/hooks/qemu.d/90-mofos

root@kitploit:~
### 剪贴板

Mofos 还可以管理虚拟机与宿主机之间的剪贴板。其思路是配置一个特定的快捷键,以触发与当前聚焦虚拟机的 SSH 连接,从而拉取 X11 剪贴板内容或向其中推送内容。

要向虚拟机 X11 剪贴板推送内容:```
mofos clipboard in pentest-0

从虚拟机 X11 剪贴板中提取内容:``` mofos clipboard out pentest-0

root@kitploit:~
对于每个操作,mofos 将建立到虚拟机的 SSH 连接并使用 xclip,同时在主机上根据窗口系统技术(Wayland 与 X11)使用 `wl-copy` 或 `xclip`。

上述跟踪信息中的机器名称可以省略,在这种情况下,脚本将识别当前聚焦的虚拟机并将其作为目标。

如果你使用的是 Gnome Wayland,你可能需要通过 `sudo make install_gnome_extension` 安装 [email protected] Gnome Shell 扩展。
你需要重新登录以确保扩展已安装。

### 设置机器之间的路由

mofos 中有两种路由虚拟机(VM)的选项:
- `mofos route` 接受一个网关,并在主机上配置 `ip rule`,以通过该网关路由虚拟机。
- `mofos pivot` 接受另一台 mofos 机器,获取其 IP 地址,并将其配置为当前虚拟机的默认网关。

两个选项都可以接受一个 DNS 服务器来进行配置。

### 通过渗透测试机器设置隧道

`mofos tunnel` 命令设置一个 SSH VPN(`ssh -w`),以将 mofos 机器的流量通过服务器路由。此功能依赖少量配置:

具体来说,`mofos tunnel` 命令执行以下操作(这里没有黑魔法):
1. 创建一个本地 tun 接口(`sudo /usr/libexec/mofos/mofosnet.py tun add pentest_box`),并为此接口分配一个 IP 地址。
2. 在远程服务器上运行 `tunneling.command`,该命令应创建一个 tun 接口,然后与本地接口链接(`/usr/libexec/mofos/mofosnet.py sshvpn start pentest_box`)。
3. 通过远程机器的网关路由 mofos 机器的流量(`/usr/libexec/mofos/mofosnet.py route mofos_vm_ip gateway_ip`),mofos 虚拟机的默认网关也会被更改。

`tunneling.command` 必须在 `/etc/mofos/mofosnet.toml` 配置文件中定义。例如:```toml
[tunneling.command]
start = "sudo ssh-vpn start"  # the peer remote address will be supplied as argv[1]
stop = "sudo ssh-vpn stop"

至于 route 和 pivot 命令,也可以提供 DNS 来对其进行配置。

USB 管理

mofos usb 命令允许管理 USB 设备。

设备通过其 ID(vendor_id:product_id)进行标识,attach 和 detach 命令需要该 ID:```console $ mofos ls usb +-----------+------------------------------------------------+-------------+ | ID | Device | Attached to | +-----------+------------------------------------------------+-------------+ | 0bda:8153 | Realtek, RTL8153 Gigabit Ethernet Adapter | | | 046d:c077 | Logitech, Mouse | | | 0a5c:5842 | Broadcom Corp, 58200 | | | 1bcf:28d2 | CN0Y9V728LG003AGBCJZA01, Integrated_Webcam_FHD | | +-----------+------------------------------------------------+-------------+ $ mofos usb attach pentest-1 0bda:8153 $ mofos usb +-----------+------------------------------------------------+-------------+ | ID | Device | Attached to | +-----------+------------------------------------------------+-------------+ | 0bda:8153 | Realtek, RTL8153 Gigabit Ethernet Adapter | pentest-1 | | 046d:c077 | Logitech, Mouse | | | 0a5c:5842 | Broadcom Corp, 58200 | | | 1bcf:28d2 | CN0Y9V728LG003AGBCJZA01, Integrated_Webcam_FHD | | +-----------+------------------------------------------------+-------------+ $ mofos usb detach pentest-4 0bda:8153

root@kitploit:~
`force` 选项会分离并重新附加设备。它通常用于
重新附加一个在未先分离的情况下就被拔出的设备。

注意:附加 USB 设备仅对虚拟机的当前生命周期有效。当虚拟机停止时,设备会自动分离。

### PCI 设备管理

与 USB 设备类似,mofos 允许将 PCI 设备附加到正在运行的虚拟机上,命令相同。 

另一个限制涉及属于同一组的一些 PCI 设备。例如,将以太网卡附加到正在运行的虚拟机可能需要将整个 PCI 组移入虚拟机。由于无法按顺序完成,因此尚不支持。但对于独立的 PCI 设备(如 Wi-Fi 网卡),则可以正常工作。

### 共享文件夹

`mofos mount` 命令会创建共享文件夹,并可将新添加的文件系统挂载到虚拟机内的目录中。`mofos umount` 将卸载该目录,并从虚拟机配置中移除共享文件夹。

该机制依赖于 `virtiofsd` 技术,该技术需要启用共享内存,现在创建虚拟机时默认会完成此操作。否则,以下命令将启用它:```console
virt-xml -c qemu:///system --edit --memorybacking source.type=memfd,access.mode=shared DOMAIN

此外,提供的 AppArmor 策略限制了主机可共享的目录。这是为了防止你的用户在 /etc 目录或文件系统根目录上配置共享,并利用虚拟机内的 root 权限进行修改。因此,需要修改 usr.lib.qemu.virtiofsd 策略以允许共享任意目录。

需要调整两行:``` @{SHARE_DIRS}=/data/libvirt/shares// /home/user/Public/**/ [...] pivot_root /data/libvirt/shares//, pivot_root /home/user/Public/**/,

root@kitploit:~
`SHARE_DIRS` 变量在策略中被复用,然而,由于 apparmor 的限制,无法将其复用于 `pivot_root` 指令。因此,需要针对每个共享目录手动调整 `pivot_root` 指令。

要挂载本地目录:```
mofos mount test ./Public/share -d /home/user/share

为了不干扰要挂载共享的虚拟机,如果远程目录非空,则会发出警告,并让您手动执行挂载操作。

最后,挂载操作不是持久的,每次重启后都需要重新执行。

Windows 机器

要创建 Windows 虚拟机,必须首先手动创建一台模板机器。然后使用该模板的磁盘作为后备文件来创建子机器。默认情况下,使用 Mofos 创建的 Windows 虚拟机未进行预配置。但是,可以通过使用钩子来配置 Mofos 以执行后期配置任务,例如更改主机名并在 known_hosts 文件中添加专用条目。

要启用创建后设置,必须满足以下先决条件:

  • OpenSSH 服务器应处于监听状态
  • 应配置 SSH 密钥,以便以管理员用户身份顺畅进行身份验证。必须在 C:\Program Data\ssh\administrators_authorized_keys 中创建 authorized_keys 并设置特定的 ACL```powershell $admin_group = "Administrators" $system = "SYSTEM"

$acl = Get-Acl C:\ProgramData\ssh\administrators_authorized_keys $acl.SetAccessRuleProtection($true, $false) $administratorsRule = New-Object system.security.accesscontrol.filesystemaccessrule($admin_group,"FullControl","Allow") $systemRule = New-Object system.security.accesscontrol.filesystemaccessrule($system,"FullControl","Allow") $acl.SetAccessRule($administratorsRule) $acl.SetAccessRule($systemRule) $acl | Set-Acl

root@kitploit:~
OpenSSH 应配置为使用 `PowerShell` shell,而不是 `cmd.exe`。```powershell
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" `
                 -Name DefaultShell `
                 -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" `
                 -PropertyType String `
                 -Force
  • Windows 模板的 SSH 主机密钥应定义在 $HOME/.local/share/mofos/ssh.json 中。这将确保每台新机器都会在 known_hosts 中创建一条使用模板主机密钥的对应条目。
  • 应修改模板机器的元数据,将其标识为模板:``` mofos metadata update --set_template windows-server-template
root@kitploit:~
## 自动补全

自动补全功能应针对 `bash` 和 `fish` 开箱即用。对于 `zsh`,可能需要运行以下命令:```
autoload -Uz compinit
compinit
source /usr/share/zsh/vendor-completions/_mofos

IP 地址重叠

基于同一模板磁盘的虚拟机,即使其网卡的 mac 地址因 DHCP 标识符而改变,也可能获得相同的 IP 地址。可以通过编辑模板的网络配置来改变此行为:

使用 systemd-networkd 时:``` [Match] Name=eth0

[Network] DHCP=yes MulticastDNS=no IPv6AcceptRA=no

[DHCP] ClientIdentifier=mac

root@kitploit:~
对于传统的网络接口 `/etc/network/interfaces`:```
iface eth0 inet dhcp
  client no
下载工具