Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. def analyze_files(dir):
  2. def check_dir(dir):
  3. try:
  4. for filename in os.listdir(dir):
  5. path_to_file = os.path.join(dir, filename)
  6. if os.path.isfile(path_to_file):
  7. check_file(path_to_file)
  8. elif os.path.isdir(path_to_file):
  9. check_dir(path_to_file)
  10. except PermissionError:
  11. print("{}: Permission denied".format(dir))
  12.  
  13. def check_file(path_to_file):
  14. try:
  15. file = pefile.PE(path_to_file, fast_load=True)
  16. file.parse_data_directories(
  17. directories=[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_IMPORT']])
  18. except Exception:
  19. return
  20.  
  21. first_found = True
  22. if hasattr(file, "DIRECTORY_ENTRY_IMPORT"):
  23. for library in file.DIRECTORY_ENTRY_IMPORT:
  24. try:
  25. library_name = library.dll.decode().lower()
  26. except AttributeError:
  27. continue
  28. if library_name in restricted_libraries:
  29. suspicious_functions = []
  30. for function in library.imports:
  31. try:
  32. function_name = function.name.decode()
  33. except AttributeError:
  34. continue
  35. if function_name in restricted_libraries[library_name]:
  36. suspicious_functions.append(function_name)
  37. if suspicious_functions:
  38. print("Suspicious file {}\nFile using network library: {}\n"
  39. "Found following suspicious functions: {}".format(path_to_file, library_name,
  40. ', '.join(
  41. suspicious_functions)))
  42. first_found = False
  43. break
  44. if not first_found:
  45. print()
  46.  
  47. restricted_libraries = get_libraries_list()
  48.  
  49. import os
  50. import pefile
  51. from timeit import default_timer
  52. start = default_timer()
  53. check_dir(dir)
  54. print("Total time: {}".format(default_timer() - start))
  55.  
  56.  
  57. def get_libraries_list():
  58. with open("list_of_network_libraries.json", 'r') as file:
  59. import json
  60. return json.load(file)
  61.  
  62.  
  63. if __name__ == "__main__":
  64. # analyze_files(input("Input directory to analyze: "))
  65. # analyze_files('Test_dir')
  66. analyze_files('C:\Windows\System32')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement