Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Pushover Support #62

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions pystemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import time
import urllib
import urllib2
import httplib
try:
import yaml
except:
Expand Down Expand Up @@ -211,6 +212,7 @@ def verify_directory_exists(directory):
class Pastie():
def __init__(self, site, pastie_id):
self.site = site
self.notify_alert = False
self.id = pastie_id
self.pastie_content = None
self.matches = []
Expand Down Expand Up @@ -286,6 +288,9 @@ def search_content(self):
# ignore if exclude
if 'exclude' in regex and re.search(regex['exclude'], self.pastie_content, regex_flags):
continue
#Verify Notofication
if 'notify_alert' in regex:
self.notify_alert = True
# we have a match, add to match list
self.matches.append(regex)
if self.matches:
Expand All @@ -304,8 +309,11 @@ def action_on_match(self):
if yamlconfig['mongo']['save']:
self.save_mongo()
# Send email alert if configured
if yamlconfig['email']['alert']:
if yamlconfig['email']['alert'] and self.notify_alert:
self.send_email_alert()
# Send pushover alert if configured
if yamlconfig['pushover']['alert'] and self.notify_alert:
self.send_pushover_alert()

def matches_to_text(self):
descriptions = []
Expand All @@ -328,6 +336,17 @@ def matches_to_regex(self):
else:
return ''

#set FLAG for messages
def matches_to_regex_flag(self):
descriptions = []
for match in self.matches:
if 'flag' in match:
descriptions.append(match['flag'])
if descriptions :
return unicode(descriptions)
else:
return ''

def save_mongo(self):
content = self.pastie_content.encode('utf8')
hash = hashlib.md5()
Expand All @@ -350,6 +369,7 @@ def send_email_alert(self):
msg['To'] = ','.join(recipients) # here the list needs to be comma separated
# message body including full paste rather than attaching it
message = '''
{flag}
I found a hit for a regular expression on one of the pastebin sites.

The site where the paste came from : {site}
Expand All @@ -360,7 +380,7 @@ def send_email_alert(self):

{content}

'''.format(site=self.site.name, url=self.public_url, matches=self.matches_to_regex(), content=self.pastie_content.encode('utf8'))
'''.format(site=self.site.name, url=self.public_url, matches=self.matches_to_regex(), flag=self.matches_to_regex_flag(), content=self.pastie_content.encode('utf8'))
msg.attach(MIMEText(message))
# send out the mail
try:
Expand All @@ -378,6 +398,31 @@ def send_email_alert(self):
except Exception, e:
logger.error("ERROR: unable to send email. Are your email setting correct?: {e}".format(e=e))

def send_pushover_alert(self):
alert = "Found hit for {matches} in pastie {url}".format(matches=self.matches_to_text(), url=self.url)
# headers
tokenID = yamlconfig['pushover']['token']
userID = yamlconfig['pushover']['user']

message = '''
{flag}
I found a hit for a regular expression on one of the pastebin sites.

The site where the paste came from : {site}
The original paste was located here: {url}
And the regular expressions that matched: {matches}

Below (after newline) is the content of the pastie:

{content}'''.format(site=self.site.name, url=self.url, matches=self.matches_to_regex(), flag=self.matches_to_regex_flag(), content=self.pastie_content.encode('utf8'))
conn = httplib.HTTPSConnection("api.pushover.net:443")
conn.request("POST", "/1/messages.json",
urllib.urlencode({
"token": tokenID,
"user": userID,
"message": message,
}), { "Content-type": "application/x-www-form-urlencoded" })
conn.getresponse()

class PastiePasteSiteCom(Pastie):
'''
Expand Down Expand Up @@ -981,4 +1026,3 @@ def main_as_daemon():
main_as_daemon()
else:
main()

8 changes: 8 additions & 0 deletions pystemon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ email:
password: '' # (optional) Password for authentication. Leave blank for no authentication.
subject: '[pystemon] - {subject}'

pushover:
alert: no
token: "app_token"
user: "user_key"

#####
# Definition of regular expressions to search for in the pasties
#
Expand All @@ -51,6 +56,8 @@ search:
# # Warning: when setting this the default is overridden
# # example: 're.MULTILINE + re.DOTALL + re.IGNORECASE'
# to: '' # (optional) Additional recipients for email alert, comma separated list
# notify_alert:'' # ability to set notification on different regex
# flag: '' # add the ability to define flags for the message

- search: '[^a-zA-Z0-9]example\.com'
- search: '[^a-zA-Z0-9]foobar\.com'
Expand All @@ -59,6 +66,7 @@ search:
exclude: 'porn|sex|teen'
count: 4


#####
# Configuration section for the paste sites
#
Expand Down