finalized checker.py

This commit is contained in:
Nathaniel Kenschaft 2020-06-15 23:31:47 -04:00
parent 76db4476d2
commit 04ce324566
2 changed files with 104 additions and 4 deletions

View File

@ -2,6 +2,7 @@ import time
import sys import sys
import os import os
import pyinotify import pyinotify
import checker
class EventHandler(pyinotify.ProcessEvent): class EventHandler(pyinotify.ProcessEvent):
@ -77,18 +78,24 @@ class EventHandler(pyinotify.ProcessEvent):
NOTIFIER = None NOTIFIER = None
STDOUT = sys.stdout STDOUT = sys.stdout
DIR = None
START_TIME = None
def watch_dir(watched_dir="/tmp", logdir="/tmp/skooloslogs"): 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): if not os.path.exists(logdir):
os.makedirs(logdir) os.makedirs(logdir)
logfile = open( logfile = open(
logdir + "/skoolos_" + logdir + "/skoolos_" +
time.strftime("%m%d%Y-%H%M%S", time.localtime()), 'w') time.strftime("%m%d%Y-%H%M%S", time.localtime()), 'w')
sys.stdout = logfile sys.stdout = logfile
START_TIME = time.time()
print("Start time: " + print("Start time: " +
time.strftime("%A, %B %d, %Y %H:%M:%S", time.localtime()) + "\n\n") time.strftime("%A, %B %d, %Y %H:%M:%S", time.localtime()) + "\n\n")
global NOTIFIER
wm = pyinotify.WatchManager() wm = pyinotify.WatchManager()
mask = pyinotify.IN_CREATE | pyinotify.IN_CLOSE_WRITE | pyinotify.IN_DELETE | \ mask = pyinotify.IN_CREATE | pyinotify.IN_CLOSE_WRITE | pyinotify.IN_DELETE | \
pyinotify.IN_MOVED_TO | pyinotify.IN_MOVED_FROM | pyinotify.IN_OPEN 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(): def stop_watching():
NOTIFIER.stop() NOTIFIER.stop()
now = time.time()
print("End time: " + print("End time: " +
time.strftime("%A, %B %d, %Y %H:%M:%S", time.localtime())) 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 sys.stdout = STDOUT
print("Done watching.\n") print("Done watching.\n")

View File

@ -1,9 +1,80 @@
import os 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(): def shell_check():
bash_history = [line.strip() for line in open(os.path.expanduser("~/.bash_history"), 'r')] bash_history = [
zsh_history = [line.strip() for line in open(os.path.expanduser("~/.histfile"), 'r')] 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" report = "Suspicios commands found:\n"
for i in bash_history + zsh_history: for i in bash_history + zsh_history:
if "git" in i: if "git" in i:
@ -13,5 +84,17 @@ def shell_check():
return "Nothing suspicious found in bash or zsh history." 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