haiv

Which Process Use This DLL?

Apr 13th, 2018
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.31 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from __future__ import print_function, unicode_literals
  3. from pprint import pprint
  4. import itertools
  5. import json
  6. import os
  7. import re
  8. import sqlite3
  9. import subprocess
  10. import time
  11.  
  12.  
  13. """
  14. Sample listdlls output:
  15.  
  16.  
  17. ListDLLs v3.1 - List loaded DLLs
  18. Copyright (C) 1997-2011 Mark Russinovich
  19. Sysinternals - www.sysinternals.com
  20.  
  21. ------------------------------------------------------------------------------
  22. lsass.exe pid: 1396
  23. Command line: C:\WINDOWS\system32\lsass.exe
  24.  
  25. Base                Size      Path
  26. 0x00000000158f0000  0x11000   C:\WINDOWS\system32\lsass.exe
  27. 0x00000000bc450000  0x1d2000  C:\WINDOWS\SYSTEM32\ntdll.dll
  28. 0x00000000ba620000  0xac000   C:\WINDOWS\System32\KERNEL32.DLL
  29. 0x00000000b9480000  0x21d000  C:\WINDOWS\System32\KERNELBASE.dll
  30. ...
  31. ------------------------------------------------------------------------------
  32. svchost.exe pid: 1564
  33. Command line: C:\WINDOWS\system32\svchost.exe -k DcomLaunch
  34.  
  35. Base                Size      Path
  36. 0x00000000e1b80000  0xe000    C:\WINDOWS\system32\svchost.exe
  37. 0x00000000bc450000  0x1d2000  C:\WINDOWS\SYSTEM32\ntdll.dll
  38. 0x00000000ba620000  0xac000   C:\WINDOWS\System32\KERNEL32.DLL
  39. 0x00000000b9480000  0x21d000  C:\WINDOWS\System32\KERNELBASE.dll
  40. 0x00000000ba790000  0x59000   C:\WINDOWS\System32\sechost.dll
  41. ...
  42.  
  43. """
  44.  
  45. def get_all_dlls():
  46.     pattern = re.compile(r'^\w+\s+\w+\s+(.+)$')
  47.     output = subprocess.check_output( ['listdlls'])
  48.     output = output + "---\n"
  49.     lines = output.splitlines()
  50.  
  51.     with open('dlls.txt', 'w') as f:
  52.         f.write(output)
  53.  
  54.     # Skip the header
  55.     lines = itertools.dropwhile(lambda line: not line.startswith('---'), lines)
  56.     next(lines)  # Skip the dashes line
  57.  
  58.     for line in lines:
  59.         if 'pid:' in line:
  60.             # lsass.exe pid: 1396
  61.             process = line.split()[0]
  62.         elif line.startswith('0x'):
  63.             # 0x00000000e1b80000  0xe000    C:\WINDOWS\system32\svchost.exe
  64.             # ==> module=svchost.exe, path=C:\WINDOWS\system32\svchost.exe
  65.             matched = pattern.match(line)
  66.             if matched:
  67.                 path = matched.group(1)
  68.                 filename = os.path.basename(path)
  69.                 yield process, filename, path
  70.  
  71.  
  72. if __name__ == '__main__':
  73.     target = 'tabdoc.dll'
  74.     for module, dll, dll_path in get_all_dlls():
  75.         if dll == target:
  76.             print('{}, {}'.format(module, dll))
Advertisement
Add Comment
Please, Sign In to add comment