diff --git a/bgservice/bgservice.py b/bgservice/bgservice.py index db0f15e..437cdda 100644 --- a/bgservice/bgservice.py +++ b/bgservice/bgservice.py @@ -2,6 +2,7 @@ import time import sys import os import pyinotify +import checker class EventHandler(pyinotify.ProcessEvent): @@ -77,18 +78,24 @@ class EventHandler(pyinotify.ProcessEvent): NOTIFIER = None STDOUT = sys.stdout +DIR = None +START_TIME = None def watch_dir(watched_dir="/tmp", logdir="/tmp/skooloslogs"): + global DIR + global START_TIME + global NOTIFIER + DIR = watched_dir if not os.path.exists(logdir): os.makedirs(logdir) logfile = open( logdir + "/skoolos_" + time.strftime("%m%d%Y-%H%M%S", time.localtime()), 'w') sys.stdout = logfile + START_TIME = time.time() print("Start time: " + time.strftime("%A, %B %d, %Y %H:%M:%S", time.localtime()) + "\n\n") - global NOTIFIER wm = pyinotify.WatchManager() mask = pyinotify.IN_CREATE | pyinotify.IN_CLOSE_WRITE | pyinotify.IN_DELETE | \ pyinotify.IN_MOVED_TO | pyinotify.IN_MOVED_FROM | pyinotify.IN_OPEN @@ -99,7 +106,17 @@ def watch_dir(watched_dir="/tmp", logdir="/tmp/skooloslogs"): def stop_watching(): NOTIFIER.stop() + now = time.time() print("End time: " + time.strftime("%A, %B %d, %Y %H:%M:%S", time.localtime())) + print("\nTotal work time: " + + time.strftime("%H:%M:%S", time.gmtime(now - START_TIME))) + suspicious_files = checker.file_check(DIR) + if suspicious_files != []: + print( + "\n\n--------------------------------------------------\n\n\n" + + "WARNING: One or more file did not have file extensions that are acceptable.\n" + + "The paths to these files are listed below:\n") + print(*suspicious_files, sep='\n') sys.stdout = STDOUT print("Done watching.\n") diff --git a/bgservice/checker.py b/bgservice/checker.py index 35d48fc..78afdae 100644 --- a/bgservice/checker.py +++ b/bgservice/checker.py @@ -1,9 +1,80 @@ import os +from glob import glob + +file_whitelist = [ + # text and document files + ".doc", + ".docx", + ".odt", + ".pdf", + ".rtf", + ".tex", + ".txt", + ".wpd", + # video files + ".3g2", + ".3gp", + ".avi", + ".flv", + ".h264", + ".m4v", + ".mkv", + ".mov", + ".mp4", + ".mpg", + ".mpeg", + ".rm", + ".swf", + ".vob", + ".wmv", + # spreadsheet files + ".ods", + ".xls", + ".xlsm", + ".xlsx", + ".csv", + # programming files + ".c", + ".class", + ".cpp", + ".cs", + ".go", + ".h", + ".java", + ".pl", + ".sh", + ".swift", + ".vb", + # presentation files + ".key", + ".odp", + ".pps", + ".ppt", + ".pptx", + # image files + ".ai", + ".bmp", + ".gif", + ".ico", + ".jpeg", + ".jpg", + ".png", + ".ps", + ".psd", + ".svg", + ".tif", + ".tiff", +] def shell_check(): - bash_history = [line.strip() for line in open(os.path.expanduser("~/.bash_history"), 'r')] - zsh_history = [line.strip() for line in open(os.path.expanduser("~/.histfile"), 'r')] + bash_history = [ + line.strip() + for line in open(os.path.expanduser("~/.bash_history"), 'r') + ] + zsh_history = [ + line.strip() for line in open(os.path.expanduser("~/.histfile"), 'r') + ] report = "Suspicios commands found:\n" for i in bash_history + zsh_history: if "git" in i: @@ -13,5 +84,17 @@ def shell_check(): return "Nothing suspicious found in bash or zsh history." -def file_check(dir_): +def verify_file(file_): + for ext in file_whitelist: + if file_[len(file_) - len(ext):] == ext: + return True + return False + +def file_check(dir_): + files = glob(dir_ + "/**/*", recursive=True) + suspicious_files = [] + for file_ in files: + if not verify_file(file_): + suspicious_files.append(file_) + return suspicious_files diff --git a/requirements.txt b/requirements.txt index ceb2027..b4e22a6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -23,6 +23,7 @@ lazy-object-proxy==1.4.3 mccabe==0.6.1 meson==0.53.2 msgpack==0.6.2 +numpy==1.18.5 oauthlib==3.1.0 ordered-set==3.1.1 packaging==20.1 @@ -59,3 +60,4 @@ webencodings==0.5.1 Werkzeug==1.0.1 wpgtk==6.1.3 wrapt==1.12.1 +yapf==0.30.0