quick train commit, added comments and a bit of refactoring and exception handling
This commit is contained in:
parent
63c98b8ff4
commit
5736cbe1ab
53
certAlert.py
53
certAlert.py
@ -1,18 +1,24 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import datetime, sys, re, urllib2, logging
|
import datetime, sys, re, urllib2, logging
|
||||||
import pushnotify
|
import pushnotify
|
||||||
|
#TODO: replace pushnotify with a simple HTTP_POST
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
ERRSTR = '!!!!!!!!!!!!! '
|
ERRSTR = '!!!!!!!!!!!!! '
|
||||||
|
|
||||||
PUSHOVER_DEVICE = 'chromehome'
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 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
|
||||||
USER_KEY_PATH = 'C:\Users\Panki\Desktop\Privat\Dev\pushover_user'
|
USER_KEY_PATH = 'C:\Users\Panki\Desktop\Privat\Dev\pushover_user'
|
||||||
API_KEY_PATH = 'C:\Users\Panki\Desktop\Privat\Dev\pushover_key'
|
API_KEY_PATH = 'C:\Users\Panki\Desktop\Privat\Dev\pushover_key'
|
||||||
|
PUSHOVER_DEVICE = 'chromehome'
|
||||||
|
|
||||||
|
# To monitor more programs, simply add a string here
|
||||||
PROGRAMS = [u'Chrome', u'OpenSSH', u'Java', u'Linux', u'Apache', u'Windows']
|
PROGRAMS = [u'Chrome', u'OpenSSH', u'Java', u'Linux', u'Apache', u'Windows']
|
||||||
|
|
||||||
with open(USER_KEY_PATH, 'r') as userKeyFile:
|
with open(USER_KEY_PATH, 'r') as userKeyFile:
|
||||||
@ -22,6 +28,7 @@ with open(API_KEY_PATH, 'r') as apiKeyFile:
|
|||||||
API_KEY = apiKeyFile.read()
|
API_KEY = apiKeyFile.read()
|
||||||
userKeyFile.close()
|
userKeyFile.close()
|
||||||
|
|
||||||
|
# object to store a single cert alert
|
||||||
class Advisory:
|
class Advisory:
|
||||||
def __init__(self, html):
|
def __init__(self, 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()
|
||||||
@ -37,6 +44,7 @@ class Advisory:
|
|||||||
print('link: ' + self.link)
|
print('link: ' + self.link)
|
||||||
|
|
||||||
def startLogger():
|
def startLogger():
|
||||||
|
# thanks to whoever i stole this from
|
||||||
logger = logging.getLogger('pushnotify')
|
logger = logging.getLogger('pushnotify')
|
||||||
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')
|
||||||
@ -44,31 +52,54 @@ def startLogger():
|
|||||||
handler.setFormatter(formatter)
|
handler.setFormatter(formatter)
|
||||||
logger.addHandler(handler)
|
logger.addHandler(handler)
|
||||||
|
|
||||||
|
def getHTML(url):
|
||||||
|
try:
|
||||||
|
response = urllib2.open(url)
|
||||||
|
except urllib2.URLError:
|
||||||
|
print(ERRSTR + 'Failed getting webpage!')
|
||||||
|
print(ERRSTR + 'Check your internet connection or TARGET_URL.')
|
||||||
|
sys.exit(ERRSTR + 'Stopping execution!')
|
||||||
|
except:
|
||||||
|
e = sys.exc_info()[0]
|
||||||
|
print(ERRSTR + 'Error getting Webpage!')
|
||||||
|
print(e)
|
||||||
|
sys.exit(ERRSTR + 'Stopping execution!')
|
||||||
|
result = response.read();
|
||||||
|
return result
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
import pushnotify
|
import pushnotify
|
||||||
startLogger()
|
startLogger()
|
||||||
#readKeys()
|
|
||||||
client = pushnotify.get_client('pushover', API_KEY, 'certAlert')
|
client = pushnotify.get_client('pushover', API_KEY, 'certAlert')
|
||||||
client.add_key(USER_KEY, PUSHOVER_DEVICE)
|
client.add_key(USER_KEY, PUSHOVER_DEVICE)
|
||||||
try:
|
#try:
|
||||||
response = urllib2.urlopen(TARGET_URL)
|
# response = urllib2.urlopen(TARGET_URL)
|
||||||
except:
|
#except urllib2.URLError:
|
||||||
e = sys.exc_info()[0]
|
# print(ERRSTR + 'Failed getting webpage!')
|
||||||
print(ERRSTR + 'Error getting Webpage!\r\n' + e)
|
# print(ERRSTR + 'Check your internet connection or TARGET_URL.')
|
||||||
sys.exit(ERRSTR + 'Stopping execution!')
|
# sys.exit(ERRSTR + 'Stopping execution!')
|
||||||
html = response.read()
|
#except:
|
||||||
|
# e = sys.exc_info()[0]
|
||||||
|
# print(ERRSTR + 'Error getting Webpage!')
|
||||||
|
# print(e)
|
||||||
|
# sys.exit(ERRSTR + 'Stopping execution!')
|
||||||
|
#html = response.read()
|
||||||
|
html = getHTML(TARGET_URL)
|
||||||
soup = BeautifulSoup(html, 'html.parser')
|
soup = BeautifulSoup(html, 'html.parser')
|
||||||
|
# create a list of results and add objects created with the data of each table row
|
||||||
results = []
|
results = []
|
||||||
for adv in soup.find_all('tr', {'class' : re.compile('search-result-*')}):
|
for adv in soup.find_all('tr', {'class' : re.compile('search-result-*')}):
|
||||||
x = Advisory(adv)
|
x = Advisory(adv)
|
||||||
results.append(x)
|
results.append(x)
|
||||||
|
# here we're checking which advisory IDs we've already seen,
|
||||||
|
# so we don't send multiple notifications for the same advisory
|
||||||
|
# TODO: refactor into functions writeMemory(checkeIDs), readMemory()
|
||||||
try:
|
try:
|
||||||
with open(MEMORY_PATH, 'r') as memFile:
|
with open(MEMORY_PATH, 'r') as memFile:
|
||||||
checkedIDs = memFile.read()
|
checkedIDs = memFile.read()
|
||||||
memFile.close()
|
memFile.close()
|
||||||
except IOError:
|
except IOError:
|
||||||
|
# this most likely means file not found. this can happen during the first run
|
||||||
print(ERRSTR + 'Error reading memory file!')
|
print(ERRSTR + 'Error reading memory file!')
|
||||||
print(ERRSTR + 'Continuing without list of checked IDs...')
|
print(ERRSTR + 'Continuing without list of checked IDs...')
|
||||||
checkedIDs = ''
|
checkedIDs = ''
|
||||||
@ -78,6 +109,7 @@ def main():
|
|||||||
print(e)
|
print(e)
|
||||||
for result in results:
|
for result in results:
|
||||||
if result.risk > 3:
|
if result.risk > 3:
|
||||||
|
# here we're checking if the is related to our programs
|
||||||
for prog in PROGRAMS:
|
for prog in PROGRAMS:
|
||||||
if re.match(prog, result.description, re.IGNORECASE):
|
if re.match(prog, result.description, re.IGNORECASE):
|
||||||
if result.identifier not in checkedIDs:
|
if result.identifier not in checkedIDs:
|
||||||
@ -87,6 +119,7 @@ def main():
|
|||||||
print('========================================================================')
|
print('========================================================================')
|
||||||
else:
|
else:
|
||||||
print('Already sent an alert for ' + result.identifier +', skipping...')
|
print('Already sent an alert for ' + result.identifier +', skipping...')
|
||||||
|
# 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\n')
|
||||||
|
Loading…
Reference in New Issue
Block a user