我试图在python中创建一个防病毒软件,但是当我得到它的哈希值时我不断收到权限被拒绝错误并检查即使我让它忽略错误并尝试继续下一个文件来哈希并检查。我对这个错误很好,但是我试着让它移动到下一个文件,我尝试的任何工作都没有。我正在使用Windows。
#!/usr/bin/python3.5
import os
import sys
import time
import glob
import simple_hasher
from colorama import *
init()
#Begin code
#variable_set
virlfile = "ListFile.hash"
vircount = 0
virusdel = []
hashtype = "MD5"
#variable_end
#fileloadhash
if not os.path.isfile(virlfile):
try:
with open(virlfile, "w") as f:
f.close
except IOError as err:
print("Error, Something went WRONG!")
print("Error as reported:")
print("{}".format(err))
with open(virlfile) as f:
data = f.read()
#scanner
filecount = 0
try:
for file in glob.glob("./**/*", recursive = True):
if os.path.isfile(file):
file_hash = simple_hasher.get_hash(file, hashtype)
if file_hash in data:
print("{} | {}".format(file_hash, Fore.CYAN + file + " (!)ALERT" + Style.RESET_ALL))
vircount += 1
virusdel.append(file)
filecount += 1
else:
print("{} | {}".format(file_hash, file))
filecount += 1
except OSError:
pass
if vircount > 0:
print("\n{} Viruses Detected. Delete? Y/N".format(vircount))
try:
choice = input(">> ").lower().split(" ")
except KeyboardInterrupt:
print("SCAN ABORTED!")
try:
if choice[0] == "y":
for file in virusdel:
try:
os.remove(file)
print("File Deleted - {}".format(os.path.abspath(file)))
except Exception as err:
print ("Unable to remove file.\n{}".format(err))
print("\n(+) All Viruses Removed.\n")
else:
sys.exit()
except Exception as err:
print("Error: {}".format(err))
else:
print("\n(+)No viruses\n")
sys.exit()
我还有一个导入的自定义模块:
import hashlib
import os
import sys
def get_hash(file, ver):
if ver.lower() == "md5":
h = hashlib.md5()
elif ver.lower() == "sha1":
h = hashlib.sha1()
elif ver.lower() == "sha256":
h = hashlib.sha256()
else:
h = hashlib.sha1()
while not False:
try:
with open(file, "rb") as f:
while True:
data = f.read(2 ** 20)
if not data: break
h.update(data)
return h.hexdigest()
except Exception as err:
print("[Debug] [{}] - Message: {}".format(os.path.split(__file__)[1], err))
pass
错误:
[Debug] [simple_hasher.py] - Message: [Errno 13] Permission denied: '.\\hiberfil.sys'