API-Keys are now loaded from a single file. See the template!

This commit is contained in:
panki27 2017-12-04 10:36:51 +01:00
parent de0fca7419
commit e20a97d88b
2 changed files with 27 additions and 23 deletions

View File

@ -1,39 +1,42 @@
#!/usr/bin/env python #!/usr/bin/env python
# before running this you might want to export PYTHONIOENCODING=UTF-8, or add it permanently to /etc/environment
import datetime, sys, re, urllib2, logging import datetime, sys, re, urllib2, logging
#before running this you might want to export PYTHONIOENCODING=UTF-8
#TODO: replace pushnotify with a simple HTTP_POST
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from pyfcm import FCMNotification from pyfcm import FCMNotification
ERRSTR = '!!!!!!!!!!!!! ' ERRSTR = '!!!!!!!!!!!!! '
# REMEMBER TO CHANGE THESE!!! # REMEMBER TO CHANGE THESE!!!
TARGET_URL = 'https://www.cert-bund.de/overview/AdvisoryShort' TARGET_URL = 'https://www.cert-bund.de/overview/AdvisoryShort'
MEMORY_PATH = 'C:\Users\Panki\Desktop\Privat\Dev\certAlert\out.txt' MEMORY_PATH = 'C:\Users\Panki\Desktop\Privat\Dev\certAlert\out.txt'
#TODO: Put all this in a single file KEY_FILE_PATH = 'C:\Users\Panki\Desktop\Privat\Dev\keyfile'
USER_KEY_PATH = 'C:\Users\Panki\Desktop\Privat\Dev\pushover_user'
API_KEY_PATH = 'C:\Users\Panki\Desktop\Privat\Dev\pushover_key'
REG_KEYS_PATH = 'C:\Users\Panki\Desktop\Privat\Dev\device_list'
# To monitor more programs, simply add a string here # To monitor more programs, simply add a string here
PROGRAMS = [u'Git', u'Chrome', u'OpenSSH', u'Java', u'Linux', u'Apache', u'Windows'] PROGRAMS = [u'Git', u'Chrome', u'OpenSSH', u'Java', u'Linux', u'Apache', u'Windows']
with open(USER_KEY_PATH, 'r') as userKeyFile: # this function loads our API Keys into memory from an external file specified above
USER_KEY = userKeyFile.read()
userKeyFile.close() try:
with open(API_KEY_PATH, 'r') as apiKeyFile: with open(KEY_FILE_PATH, 'r') as keyFile:
API_KEY = apiKeyFile.read() contents = keyFile.readlines()
apiKeyFile.close() keyFile.close()
with open(REG_KEYS_PATH, 'r') as regKeyFile: except:
REG_KEYS = regKeyFile.readlines() e = sys.exc_info()[0]
regKeyFile.close() print(e)
print("I couldn't load your credentials. Did you specify your keyfile?")
# extract just the text behind the equals sign
API_KEY = contents[0].split("=",1)[1]
USER_KEYS = contents[1].split("=",1)[1]
# get rid of that pesky newline
API_KEY = API_KEY.strip()
# now we split along semicolons to get single keys in a list
USER_KEYS = USER_KEYS.split(";")
# object to store a single cert alert # object to store a single cert alert
class Advisory: class Advisory:
def __init__(self, html): def __init__(self, html):
# this is the fun parts. here we 'fish' the information out of the HTML
self.date = datetime.datetime.strptime(html.td.text, '%d.%m.%y').date() self.date = datetime.datetime.strptime(html.td.text, '%d.%m.%y').date()
self.risk = int(html.find('span', {'class': re.compile('search-result-crit-*')}).text) self.risk = int(html.find('span', {'class': re.compile('search-result-crit-*')}).text)
self.identifier = html.find('a', {'class': 'search-result-link'}).text self.identifier = html.find('a', {'class': 'search-result-link'}).text
@ -48,7 +51,7 @@ class Advisory:
def startLogger(): def startLogger():
# thanks to whoever i stole this from # thanks to whoever i stole this from
logger = logging.getLogger('pushnotify') logger = logging.getLogger('certAlert')
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(name)s-%(levelname)s: %(message)s') formatter = logging.Formatter('%(name)s-%(levelname)s: %(message)s')
handler = logging.StreamHandler() handler = logging.StreamHandler()
@ -72,8 +75,8 @@ def getHTML(url):
return result return result
def main(): def main():
#import pushnotify
#startLogger() #startLogger()
print('Getting client using ' + API_KEY)
client = FCMNotification(api_key=API_KEY) client = FCMNotification(api_key=API_KEY)
html = getHTML(TARGET_URL) html = getHTML(TARGET_URL)
soup = BeautifulSoup(html, 'html.parser') soup = BeautifulSoup(html, 'html.parser')
@ -105,10 +108,9 @@ def main():
if re.search(prog, result.description, re.IGNORECASE): if re.search(prog, result.description, re.IGNORECASE):
if result.identifier not in checkedIDs: if result.identifier not in checkedIDs:
#this means we have found an alert that we have not seen before! lets alert the user... #this means we have found an alert that we have not seen before! lets alert the user...
for key in REG_KEYS: for key in USER_KEYS:
print(type(key))
try: try:
key = key.rstrip()
response = client.notify_single_device(registration_id=key, message_body=result.description, message_title=result.identifier) response = client.notify_single_device(registration_id=key, message_body=result.description, message_title=result.identifier)
except: except:
e = sys.exc_info()[0] e = sys.exc_info()[0]
@ -121,7 +123,7 @@ def main():
# now we overwrite our memory file with the IDs we just checked # now we overwrite our memory file with the IDs we just checked
with open(MEMORY_PATH, 'w') as memFile: with open(MEMORY_PATH, 'w') as memFile:
for result in results: for result in results:
memFile.write(result.identifier + '\r\n') memFile.write(result.identifier + '\r')
memFile.close() memFile.close()

2
keyfile_template Normal file
View File

@ -0,0 +1,2 @@
API_KEY=XXXXXXXXXX
USER_KEYS=YYYYYYY;ZZZZZZZZZ