scan_dll.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import pefile
  2. import sys
  3. import re
  4. import os
  5. import queue
  6. import shutil
  7. # constant definitions
  8. KNOWN_SYS_DLLS = ['WINMM.DLL', 'MSVCRT.DLL', 'VERSION.DLL', 'MPR.DLL',
  9. 'DWMAPI.DLL', 'UXTHEME.DLL', 'DNSAPI.DLL', 'IPHLPAPI.DLL']
  10. # below is for Ubuntu 18.04 with specified PPA enabled, if you are using
  11. # other distro or different repositories, change the following accordingly
  12. DLL_PATH = [
  13. '/usr/x86_64-w64-mingw32/bin/',
  14. '/usr/x86_64-w64-mingw32/lib/',
  15. '/usr/lib/gcc/x86_64-w64-mingw32/7.3-posix/'
  16. ]
  17. missing = []
  18. def parse_imports(file_name):
  19. results = []
  20. pe = pefile.PE(file_name, fast_load=True)
  21. pe.parse_data_directories()
  22. for entry in pe.DIRECTORY_ENTRY_IMPORT:
  23. current = entry.dll.decode()
  24. current_u = current.upper() # b/c Windows is often case insensitive
  25. # here we filter out system dlls
  26. # dll w/ names like *32.dll are likely to be system dlls
  27. if current_u.upper() not in KNOWN_SYS_DLLS and not re.match(string=current_u, pattern=r'.*32\.DLL'):
  28. results.append(current)
  29. return results
  30. def parse_imports_recursive(file_name, path_list=[]):
  31. q = queue.Queue() # create a FIFO queue
  32. # file_name can be a string or a list for the convience
  33. if isinstance(file_name, str):
  34. q.put(file_name)
  35. elif isinstance(file_name, list):
  36. for i in file_name:
  37. q.put(i)
  38. full_list = []
  39. while q.qsize():
  40. current = q.get_nowait()
  41. print('> %s' % current)
  42. deps = parse_imports(current)
  43. # if this dll does not have any import, ignore it
  44. if not deps:
  45. continue
  46. for dep in deps:
  47. # the dependency already included in the list, skip
  48. if dep in full_list:
  49. continue
  50. # find the requested dll in the provided paths
  51. full_path = find_dll(dep)
  52. if not full_path:
  53. missing.append(dep)
  54. continue
  55. full_list.append(dep)
  56. q.put(full_path)
  57. path_list.append(full_path)
  58. return full_list
  59. def find_dll(name):
  60. for path in DLL_PATH:
  61. for root, _, files in os.walk(path):
  62. for f in files:
  63. if name.lower() == f.lower():
  64. return os.path.join(root, f)
  65. def deploy(name, dst, dry_run=False):
  66. dlls_path = []
  67. parse_imports_recursive(name, dlls_path)
  68. for dll_entry in dlls_path:
  69. if not dry_run:
  70. shutil.copy(dll_entry, dst)
  71. else:
  72. print('[Dry-Run] Copy %s to %s' % (dll_entry, dst))
  73. print('Deploy completed.')
  74. return dlls_path
  75. def main():
  76. if len(sys.argv) < 3:
  77. print('Usage: %s [files to examine ...] [target deploy directory]')
  78. return 1
  79. to_deploy = sys.argv[1:-1]
  80. tgt_dir = sys.argv[-1]
  81. if not os.path.isdir(tgt_dir):
  82. print('%s is not a directory.' % tgt_dir)
  83. return 1
  84. print('Scanning dependencies...')
  85. deploy(to_deploy, tgt_dir)
  86. if missing:
  87. print('Following DLLs are not found: %s' % ('\n'.join(missing)))
  88. return 0
  89. if __name__ == '__main__':
  90. main()