2022-01-19 16:20:18 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
"""
|
|
|
|
Telegram Module
|
|
|
|
============================
|
|
|
|
|
|
|
|
Search telegram username,channel and invite code
|
|
|
|
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from modules.abstract_module import AbstractModule
|
2022-10-25 16:25:19 +02:00
|
|
|
from lib.objects.Items import Item
|
2022-11-22 10:49:38 +01:00
|
|
|
from lib.objects.Usernames import Username
|
2022-01-19 16:20:18 +01:00
|
|
|
from lib import regex_helper
|
|
|
|
from lib import telegram
|
|
|
|
|
|
|
|
class Telegram(AbstractModule):
|
|
|
|
"""Telegram module for AIL framework"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super(Telegram, self).__init__()
|
|
|
|
|
|
|
|
# https://github.com/LonamiWebs/Telethon/wiki/Special-links
|
2023-05-15 10:12:58 +02:00
|
|
|
self.re_telegram_link = r'(telegram\.me|t\.me|telegram\.dog|telesco\.pe)/(\w+)'
|
2022-01-19 16:20:18 +01:00
|
|
|
self.re_tg_link = r'tg://.+'
|
|
|
|
|
|
|
|
re.compile(self.re_telegram_link)
|
|
|
|
re.compile(self.re_tg_link)
|
|
|
|
|
|
|
|
self.redis_cache_key = regex_helper.generate_redis_cache_key(self.module_name)
|
|
|
|
self.max_execution_time = 60
|
|
|
|
|
|
|
|
# Send module state to logs
|
2023-05-12 15:29:53 +02:00
|
|
|
self.logger.info(f"Module {self.module_name} initialized")
|
2022-01-19 16:20:18 +01:00
|
|
|
|
|
|
|
def compute(self, message, r_result=False):
|
|
|
|
item = Item(message)
|
|
|
|
item_content = item.get_content()
|
|
|
|
item_date = item.get_date()
|
|
|
|
|
|
|
|
invite_code_found = False
|
|
|
|
|
|
|
|
# extract telegram links
|
|
|
|
telegram_links = self.regex_findall(self.re_telegram_link, item.get_id(), item_content)
|
2023-05-15 10:12:58 +02:00
|
|
|
telegram_links = set(telegram_links)
|
2022-01-19 16:20:18 +01:00
|
|
|
for telegram_link_tuple in telegram_links:
|
2023-05-15 10:12:58 +02:00
|
|
|
# print(telegram_link_tuple)
|
|
|
|
# print(telegram_link_tuple[2:-2].split("', '", 1))
|
2022-01-19 16:20:18 +01:00
|
|
|
base_url, url_path = telegram_link_tuple[2:-2].split("', '", 1)
|
|
|
|
dict_url = telegram.get_data_from_telegram_url(base_url, url_path)
|
2022-11-22 10:49:38 +01:00
|
|
|
user_id = dict_url.get('username')
|
|
|
|
if user_id:
|
|
|
|
username = Username(user_id, 'telegram')
|
|
|
|
username.add(item_date, item.id)
|
|
|
|
print(f'username: {user_id}')
|
|
|
|
invite_hash = dict_url.get('invite_hash')
|
|
|
|
if invite_hash:
|
|
|
|
telegram.save_telegram_invite_hash(invite_hash, item.id)
|
|
|
|
print(f'invite code: {invite_hash}')
|
2022-01-19 16:20:18 +01:00
|
|
|
invite_code_found = True
|
|
|
|
|
|
|
|
# extract tg links
|
|
|
|
tg_links = self.regex_findall(self.re_tg_link, item.get_id(), item_content)
|
|
|
|
for tg_link in tg_links:
|
|
|
|
dict_url = telegram.get_data_from_tg_url(tg_link)
|
2022-11-22 10:49:38 +01:00
|
|
|
user_id = dict_url.get('username')
|
|
|
|
if user_id:
|
|
|
|
username = Username(user_id, 'telegram')
|
|
|
|
username.add(item_date, item.id)
|
|
|
|
print(f'username: {user_id}')
|
|
|
|
invite_hash = dict_url.get('invite_hash')
|
|
|
|
if invite_hash:
|
|
|
|
telegram.save_telegram_invite_hash(invite_hash, item.get_id())
|
|
|
|
print(f'invite code: {invite_hash}')
|
2022-01-19 16:20:18 +01:00
|
|
|
invite_code_found = True
|
|
|
|
if dict_url.get('login_code'):
|
|
|
|
print(f'login code: {dict_url["login_code"]}')
|
|
|
|
|
|
|
|
# CREATE TAG
|
|
|
|
if invite_code_found:
|
2022-10-25 16:25:19 +02:00
|
|
|
# tags
|
2022-11-22 10:49:38 +01:00
|
|
|
msg = f'infoleak:automatic-detection="telegram-invite-hash";{item.id}'
|
2023-04-13 14:25:02 +02:00
|
|
|
self.add_message_to_queue(msg, 'Tags')
|
2022-01-19 16:20:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
module = Telegram()
|
|
|
|
module.run()
|