def perform(self, paste, analyzer_name=None, matches=None): """ Sends an email to the specified receiver with the paste's content :param paste: The paste passed by the ActionHandler :param analyzer_name: The name of the analyzer which matched the paste :param matches: A list of matches, on which the analyzer matched on :return: None """ text = TemplatingEngine.fill_template(paste, analyzer_name, template_string=self.template, matches=matches) email = MIMEMultipart() email['From'] = self.username email['To'] = self.receiver email[ 'Subject'] = 'Paste matched by pastepwn via analyzer "{}"'.format( analyzer_name) email.attach(MIMEText(text, 'plain')) # TODO there should be a way to use starttls - check https://realpython.com/python-send-email/ with smtplib.SMTP_SSL(self.hostname, self.port) as smtp: smtp.login(self.username, self.password) text = email.as_string() smtp.sendmail(self.username, self.receiver, text)
def perform(self, paste, analyzer_name=None, matches=None): """ Stores the paste as a file :param paste: The paste passed by the ActionHandler :param analyzer_name: The name of the analyzer which matched the paste :param matches: List of matches returned by the analyzer :return: None """ if not os.path.exists(self.path): os.makedirs(self.path) if self.file_ending.startswith("."): file_name = "{0}{1}".format(paste.key, self.file_ending) elif self.file_ending == "": file_name = str(paste.key) else: file_name = "{0}.{1}".format(paste.key, self.file_ending) content = TemplatingEngine.fill_template(paste, analyzer_name, template_string=self.template, matches=matches) with open(os.path.join(self.path, file_name), "w", encoding="utf-8") as file: file.write(content)
def perform(self, paste, analyzer_name=None, matches=None): """Send a message via Discord to a specified channel, without checking for errors""" r = Request() text = TemplatingEngine.fill_template(paste, analyzer_name, template_string=self.template, matches=matches) if self.webhook_url is not None: # Send to a webhook (no authentication) url = self.webhook_url else: # Send through Discord bot API (header-based authentication) url = 'https://discordapp.com/api/channels/{0}/messages'.format( self.channel_id) r.headers = {'Authorization': 'Bot {}'.format(self.token)} res = r.post(url, {"content": text}) if res == "": # If the response is empty, skip further execution return res = json.loads(res) if res.get( 'code' ) == 40001 and self.bot_available and self.webhook_url is None and not self.identified: # Unauthorized access, bot token hasn't been identified to Discord Gateway self.logger.info('Accessing Discord Gateway to initialize token') self.initialize_gateway() # Retry action self.perform(paste, analyzer_name=analyzer_name)
def perform(self, paste, analyzer_name=None, matches=None): """Tweet a message""" text = TemplatingEngine.fill_template(paste, analyzer_name, template_string=self.template, matches=matches) self.twitter_api.PostUpdate(text)
def perform(self, paste, analyzer_name=None): """Send a message via a Telegram bot to a specified user, without checking for errors""" r = Request() text = TemplatingEngine.fill_template(paste, analyzer_name, template_string=self.template) api_url = "https://api.telegram.org/bot{0}/sendMessage?chat_id={1}&text={2}".format( self.token, self.receiver, text) r.get(api_url)
def perform(self, paste, analyzer_name=None, matches=None): """ Logs a paste to the syslog :param paste: The paste passed by the ActionHandler :param analyzer_name: The name of the analyzer which matched the paste :param matches: List of matches returned by the analyzer :return: None """ text = TemplatingEngine.fill_template(paste, analyzer_name, template_string=self.template, matches=matches) self.logger.debug(text)
def perform(self, paste, analyzer_name=None, matches=None): """Perform the action on the passed paste""" if self._exception_event.is_set(): self.logger.error( "The exception event is set. The IRC action might not perform as it should! Messages will be buffered for the case of a " "reconnect.") text = TemplatingEngine.fill_template(paste, analyzer_name, template_string=self.template, matches=matches) self._send_message(text)
def perform(self, paste, analyzer_name=None): """Perform the action on the passed paste""" if self.template is None: text = "New paste matched by analyzer '{0}' - Link: {1}".format( analyzer_name, paste.full_url) else: text = TemplatingEngine.fill_template( paste, analyzer_name, template_string=self.template) self.ircsock.connect((self.server, self.port)) self.ircsock.send( bytes( "USER " + self.nick + " " + self.nick + " " + self.nick + "n", "UTF-8")) self.ircsock.send(bytes("NICK " + self.nick + "n", "UTF-8")) self.ircsock.send(bytes("JOIN " + self.channel + "n", "UTF-8")) self.ircsock.send( bytes("PRIVMSG " + self.channel + " " + text + "n", "UTF-8")) self.ircsock.send(bytes("QUIT n", "UTF-8"))
def get_file_content(self, paste, analyzer_name, matches): """Returns the content to be written to the file""" return TemplatingEngine.fill_template(paste, analyzer_name, template_string=self.template, matches=matches)