Advertisement
Zakkq

Python Experimental Anti-Virus

Jun 18th, 2014
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.66 KB | None | 0 0
  1. __author__ = 'Zakkq'
  2. # Firestorm - Fight fire with fire
  3.  
  4. import binascii
  5. import sys
  6. import os
  7. import time
  8. if sys.version_info < (3,0):
  9.     import Tkinter as tkinter
  10.     import tkMessageBox as mbox
  11. else:
  12.     import tkinter
  13.     import tkinter.messagebox as mbox
  14. mfs = raw_input('Max file size you want to scan? in Mega Bytes eg "5": ')
  15. sd = raw_input('Starting Directory, Standard is "C:\\": ')
  16. c = 0
  17. string = []
  18. # Getting the HEX Signatues
  19. with open("signatures.txt", 'rb') as s:
  20.     content = s.read()
  21. # Formatting the HEX Signatues
  22. contentbuffer = content.rstrip().split('\n')
  23. i = 0
  24. while i < len(contentbuffer):
  25.     string.append(contentbuffer[i].split('=', 1)[1])
  26.     i = i + 1
  27. # Setting starting Dir
  28. if sd == "":
  29.     directories = ["C:\\"]
  30. else:
  31.     directories =[sd]
  32. # Choosing files not to scan
  33. safeFiles = ["signatures.txt"]
  34. while True:
  35.     if len(directories) == 0:
  36.         window = tkinter.Tk()
  37.         window.wm_withdraw()
  38.         mbox.showinfo('Virus Detector','DONE ' + str(c) + " Scanned Objects -" + " " + str(filename))
  39.         break
  40.     folder = directories[0]
  41.     del directories[0]
  42.     print folder
  43.     try:
  44.         filename = os.listdir(folder)
  45.     except Exception as e:
  46.         print "Error reading dir:", folder
  47.         continue
  48.     i = 0
  49.     filename2 = []
  50.     if len(directories) > 1000 or len(filename) > 10000:
  51.         print "Breaking because of too much stuff"
  52.         break
  53.     for cur_file in filename:
  54.         abs_path = os.path.join(folder, cur_file)
  55.         if os.path.isfile(abs_path):
  56.             #print "FILE:", cur_file
  57.             filename2.append(abs_path)
  58.         elif os.path.isdir(abs_path):
  59.             #print "DIR:", cur_file
  60.             directories.append(abs_path)
  61.         else:
  62.             print "IGNORING:", cur_file
  63.     filename = filename2
  64.     i = 0
  65.     while i < len(string):
  66.         string[i] = " ".join(string[i][g:g+2] for g in range(0, len(string[i]), 2))
  67.         string[i]=string[i].replace(" ",",")
  68.         i = i + 1
  69.     i = 0
  70.     print str(filename) + "\n"
  71.     try:
  72.         for cur_file in filename:
  73.             t0 = time.clock()
  74.             base_name = os.path.basename(cur_file)
  75.             #print base_name
  76.             if os.path.getsize(cur_file) > (mfs * 1024 * 1024):
  77.                 print "Large file ignored", cur_file
  78.                 continue
  79.             try:
  80.                 if cur_file in safeFiles:
  81.                     print "file in SafeFiles ignored:"
  82.                     continue
  83.                 else:
  84.                     with open(cur_file, 'rb') as f:
  85.                         content = f.read()
  86.                         c = c + 1
  87.             except IOError as err:
  88.                 print "Unable to read:", cur_file
  89.                 continue
  90.             hex = str(binascii.hexlify(content))
  91.             formatted_hex = ','.join(hex[i:i+2] for i in range(0, len(hex), 2))
  92.             i = 0
  93.             while i < len(string):
  94.                 if string[i] in formatted_hex:
  95.                     window = tkinter.Tk()
  96.                     window.wm_withdraw()
  97.                     mbox.showinfo('Virus Detector','VIRUS AT ' +  cur_file + " The virus signature number is " + string[i])
  98.                 else:
  99.                     i = i + 1
  100.                     pass
  101.             a = time.clock() - t0
  102.             astring = str(a)
  103.             print base_name + " Has been Scanned it took: " + astring[:3] + " :seconds to scan the element"
  104.     except Exception as e:
  105.         exc_type, exc_obj, exc_tb = sys.exc_info()
  106.         fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
  107.         print(exc_type, fname, exc_tb.tb_lineno)
  108.         print time.clock() - t0
  109.         raw_input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement