Advertisement
Guest User

check for new

a guest
Jan 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. # Imports
  2. import glob
  3. import os
  4. import time
  5. import Tkinter
  6. import tkMessageBox
  7. import subprocess
  8. import tempfile
  9. import shutil
  10. import magic
  11.  
  12. # Constants
  13. DIR = r'C:\users\{}\downloads'.format(os.getenv('username'))
  14. TEMP_NAME = "Temp_Power.ps1"
  15. TEMP_ICON = "SomeIcon.bmp"
  16. POWER_CODE = """Add-Type -AssemblyName System.Drawing
  17. $Icon = [System.Drawing.Icon]::ExtractAssociatedIcon("{}")
  18. $Icon.ToBitmap().Save("{}")
  19. """
  20.  
  21. def msg(head, msg2, style):
  22. box = [tkMessageBox.showinfo, tkMessageBox.askyesno]
  23. return box[style](title=head, message=msg2)
  24.  
  25. def main():
  26.  
  27. # Gets the current last modifieded file in the downloads folder
  28. current = max([r'{}'.format(os.path.join(DIR,d)) for d in os.listdir(DIR)], key=os.path.getmtime)
  29. already_checked = []
  30.  
  31. # Every 3 seconds checks if a new file was downloaded
  32. while True:
  33. time.sleep(3)
  34.  
  35. # If a new file was downloaded print its name
  36. if (current != max([r'{}'.format(os.path.join(DIR,d)) for d in os.listdir(DIR)], key=os.path.getmtime)) and (current not in already_checked):
  37.  
  38. # Sets the current last modifided file to the new file
  39. already_checked.append(current)
  40. current = max([r'{}'.format(os.path.join(DIR,d)) for d in os.listdir(DIR)], key=os.path.getmtime)
  41.  
  42. # Checks if the icon of the file is in known icons
  43. # Creates a temp dir
  44. temp_dir = tempfile.mkdtemp()
  45.  
  46. # Opens a powershell script and writes to it
  47. with open(os.path.join(temp_dir, TEMP_NAME), 'w') as temp:
  48. temp.write(POWER_CODE.format(current, os.path.join(temp_dir, 'new.bmp')))
  49.  
  50. # Runs the powershell script
  51. print os.path.join(temp_dir, TEMP_NAME)
  52. ps = subprocess.Popen(["powershell", os.path.join(temp_dir, TEMP_NAME)],
  53. stderr = subprocess.PIPE,
  54. stdout = subprocess.PIPE)
  55. output, error = ps.communicate()
  56. print error
  57.  
  58. # Reads the icon file
  59. with open(os.path.join(temp_dir, 'new.bmp'), 'rb') as newiconfile:
  60. newicon = newiconfile.read()
  61.  
  62. # Gets the data from the known icon files
  63. with open(r'C:\Users\Ofir Tal\Desktop\dustless_sky\word.bmp', 'rb') as wordfile:
  64. wordicon = wordfile.read()
  65.  
  66. # Deletes the temp folder and files
  67. shutil.rmtree(temp_dir)
  68.  
  69. if wordicon == newicon:
  70. print 'ALERT!'
  71.  
  72. # Checks if the file is an exe, and if it does, checks if is icon is matching to any known icon.
  73. if 'executable' in magic.from_file(current):
  74.  
  75. # Opens a powershell script and writes to it
  76. with open(os.path.join(temp_dir, TEMP_NAME), 'w') as temp:
  77. temp.write(POWER_CODE.format(current, os.path.join(temp_dir, 'new.bmp')))
  78.  
  79. # Runs the powershell script
  80. print os.path.join(temp_dir, TEMP_NAME)
  81. ps = subprocess.Popen(["powershell", os.path.join(temp_dir, TEMP_NAME)],
  82. stderr = subprocess.PIPE,
  83. stdout = subprocess.PIPE)
  84. output, error = ps.communicate()
  85. print error
  86.  
  87. # Reads the icon file
  88. with open(os.path.join(temp_dir, 'new.bmp'), 'rb') as newiconfile:
  89. newicon = newiconfile.read()
  90.  
  91. # Gets the data from the known icon files
  92. with open(r'C:\Users\Ofir Tal\Desktop\dustless_sky\word.bmp', 'rb') as wordfile:
  93. wordicon = wordfile.read()
  94.  
  95. # Deletes the temp folder and files
  96. shutil.rmtree(temp_dir)
  97.  
  98. msg("ALERT!", "Hi you just downloaded an exe file named {}\n while his icon is not maching to an exacutable file".format(current.split('\\')[-1]), 0)
  99.  
  100. option = msg("Need help?", "Would you like me to delete it for you?", 1)
  101. if option:
  102. os.system('del /f "{}"'.format(current))
  103. msg("DONE!", "You are safe now!", 0)
  104. else:
  105. msg("Be Careful", "Just remember I warned you", 0)
  106.  
  107. if __name__ == "__main__":
  108. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement