Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/auxiliary/client/telegram/send_message.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'faraday'67class MetasploitModule < Msf::Auxiliary8def initialize9super(10'Name' => 'Telegram Message Client',11'Description' => %q{12This module can be used to send a document and/or message to13multiple chats on telegram. Please refer to the module14documentation for info on how to retrieve the bot token and corresponding chat15ID values.16},17'Author' => [18'Ege Balcı <egebalci[at]pm.me>', # Aka @egeblc of https://pentest.blog19'Gaurav Purswani' # @pingport8020],21'License' => MSF_LICENSE,22)2324register_options(25[26OptString.new('BOT_TOKEN', [true, 'Telegram BOT token', '']),27OptString.new('MESSAGE', [false, 'The message to be sent']),28OptInt.new('CHAT_ID', [false, 'Chat ID for the BOT', '']),29OptPath.new('DOCUMENT', [false, 'The path to the document(binary, video etc)']),30OptPath.new('IDFILE', [false, 'File containing chat IDs, one per line']),31OptEnum.new('FORMATTING', [false, 'Message formatting option (Markdown|MarkdownV2|HTML)', 'Markdown', [ 'Markdown', 'MarkdownV2', 'HTML']])32], self.class33)34end3536def formatting37datastore['FORMATTING']38end3940def message41datastore['MESSAGE']42end4344def document45datastore['DOCUMENT']46end4748def bot_token49datastore['BOT_TOKEN']50end5152def id_file53datastore['IDFILE']54end5556def send_document(conn, chat_id)57unless ::File.file?(document) && ::File.readable?(document)58fail_with(Failure::BadConfig, 'The document to be sent does not exist or is not a readable file!')59end60raw_params = { 'chat_id' => chat_id, 'document' => Faraday::UploadIO.new(document, 'application/octet-stream') }61params = {}62raw_params.each_with_object({}) do |(key, value), _tmp_params|63params[key] = value64end65response = conn.post("/bot#{bot_token}/sendDocument", params)66if response.status == 20067print_good("Document sent successfully to #{chat_id}")68elsif response.status == 40369print_bad("Error while sending document! Make sure you have access to message chat_id : #{chat_id}")70else71print_bad("Error while sending the document to #{chat_id} API Status : #{response.status}")72end73end7475def send_message(conn, chat_id)76params = { 'chat_id' => chat_id, 'text' => message, 'parse_mode' => formatting }77response = conn.post("/bot#{bot_token}/sendMessage", params)78if response.status == 20079print_good("Message sent successfully to #{chat_id}")80elsif response.status == 40381print_bad("Error while sending document! Make sure you have access to message chat_id : #{chat_id}")82else83print_bad("Error while sending the message to chat_id #{chat_id} API Status : #{response.status}")84end85end8687def run88unless document || message89fail_with(Failure::BadConfig, 'You must supply a message and/or document')90end91url = 'https://api.telegram.org'92conn = Faraday.new(url: url) do |faraday|93faraday.request :multipart94faraday.request :url_encoded95faraday.adapter Faraday.default_adapter96end9798if id_file99print_warning("Opening `#{id_file}` to fetch chat IDs...")100unless ::File.file?(id_file) && ::File.readable?(id_file)101fail_with(Failure::BadConfig, 'The ID file is not an existing readable file!')102end103File.readlines(id_file).each do |chat_id|104send_document(conn, chat_id) if document105send_message(conn, chat_id) if message106end107return108end109send_document(conn, datastore['CHAT_ID']) if document110send_message(conn, datastore['CHAT_ID']) if message111end112113end114115116