Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """
  4. OVERVIEW
  5.  
  6. Extract USB mass storage device events from Cb Enterprise Response (CbER).
  7. """
  8.  
  9. import argparse
  10. import csv
  11. import json
  12. import os
  13. import sys
  14.  
  15. from cbapi.response import CbEnterpriseResponseAPI
  16. from cbapi.response.models import Process
  17.  
  18.  
  19. match_guid = '{53f56307-b6bf-11d0-94f2-00a0c91efb8b}'
  20. search_terms = ["registry\\machine\\system\\currentcontrolset\\control\\deviceclasses\\{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}\\*",
  21. "registry\\machine\\currentcontrolset\\control\\deviceclasses\\{53f56307-b6bf-11d0-94f2-00a0c91efb8b}\\*"]
  22.  
  23.  
  24. class USBEvent:
  25. def __init__(self, path):
  26. self.path = path
  27.  
  28. self.vendor = ''
  29. self.product = ''
  30. self.version = ''
  31. self.serial = ''
  32. #self.drive_letter = ''
  33. #self.volume_name = ''
  34.  
  35. self.parse()
  36.  
  37. def __repr__(self):
  38. for k,v in self.__dict__.iteritems():
  39. print '%s,%s' % (k, v)
  40.  
  41. def parse(self):
  42. path = self.path.split('usbstor#disk&')[1]
  43. fields = path.split('&')
  44. self.vendor = fields[0].split('ven_')[1]
  45. self.product = fields[1].split('prod_')[1]
  46.  
  47. if self.vendor == 'drobo':
  48. # Drobo doesn't provide a version
  49. drobo_fields = self.product.split('#')
  50. self.product = drobo_fields[0]
  51. self.serial = drobo_fields[1]
  52. else:
  53. self.version = fields[2].split('#')[0].split('rev_')[1]
  54. self.serial = fields[2].split('#')[1]
  55.  
  56.  
  57. def usbstor_search(cb_conn, query, query_base=None, timestamps=False):
  58.  
  59. if query_base is not None:
  60. query += query_base
  61.  
  62. query_result = cb_conn.select(Process).where(query)
  63. query_result_len = len(query_result)
  64.  
  65. results = set()
  66.  
  67. for proc in query_result:
  68. for regmod in proc.regmods:
  69. if match_guid in regmod.path and 'usbstor#disk&' in regmod.path:
  70. usb_result = USBEvent(regmod.path)
  71.  
  72. output_fields = [proc.hostname,
  73. usb_result.vendor,
  74. usb_result.product,
  75. usb_result.version,
  76. usb_result.serial]
  77. if timestamps == True:
  78. output_fields.insert(0, proc.timestamp)
  79.  
  80. results.add(tuple(output_fields))
  81.  
  82. return results
  83.  
  84. def main():
  85. parser = argparse.ArgumentParser()
  86. parser.add_argument("--prefix", type=str, action="store",
  87. help="Output filename prefix.")
  88. parser.add_argument("--days", type=int, action="store",
  89. help="Number of days to search.")
  90. parser.add_argument("--minutes", type=int, action="store",
  91. help="Number of days to search.")
  92. parser.add_argument("--timestamps", action="store_true",
  93. help="Include timestamps in results.")
  94. parser.add_argument("--profile", type=str, action="store",
  95. help="The credentials.response profile to use.")
  96.  
  97. args = parser.parse_args()
  98.  
  99. if args.prefix:
  100. output_filename = '%s-usb-storage-events.csv' % args.prefix
  101. else:
  102. output_filename = 'usb-storage-events.csv'
  103.  
  104. if args.profile:
  105. cb = CbEnterpriseResponseAPI(profile=args.profile)
  106. else:
  107. cb = CbEnterpriseResponseAPI()
  108.  
  109. output_file = file(output_filename, 'w')
  110. writer = csv.writer(output_file, quoting=csv.QUOTE_ALL)
  111.  
  112. header_row = ['endpoint', 'vendor', 'product', 'version', 'serial']
  113. if args.timestamps == True:
  114. header_row.insert(0, 'timestamp')
  115. writer.writerow(header_row)
  116.  
  117. for term in search_terms:
  118. query = 'regmod:%s' % term
  119.  
  120. if args.days:
  121. query += ' start:-%dm' % (args.days*1440)
  122. elif args.minutes:
  123. query += ' start:-%dm' % args.minutes
  124.  
  125. results = usbstor_search(cb, query, query_base=None, timestamps=args.timestamps)
  126.  
  127. for row in results:
  128. row = list(row)
  129. row = [col.encode('utf8') if isinstance(col, unicode) else col for col in row]
  130. writer.writerow(row)
  131.  
  132. output_file.close()
  133.  
  134.  
  135. if __name__ == '__main__':
  136.  
  137. sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement