github.com/Coalfire-Research/Slackor@v0.0.0-20191010164036-aa32a7f9250b/impacket/examples/esentutl.py (about)

     1  #!/usr/bin/env python
     2  # SECUREAUTH LABS. Copyright 2018 SecureAuth Corporation. All rights reserved.
     3  #
     4  # This software is provided under under a slightly modified version
     5  # of the Apache Software License. See the accompanying LICENSE file
     6  # for more information.
     7  #
     8  # Description:
     9  #             ESE utility. Allows dumping catalog, pages and tables.
    10  #
    11  # Author:
    12  #  Alberto Solino (@agsolino)
    13  #
    14  #
    15  # Reference for:
    16  #  Extensive Storage Engine (ese)
    17  # 
    18  from __future__ import division
    19  from __future__ import print_function
    20  import sys
    21  import logging
    22  import argparse
    23  
    24  from impacket.examples import logger
    25  from impacket import version
    26  from impacket.ese import ESENT_DB
    27  
    28  
    29  def dumpPage(ese, pageNum):
    30      data = ese.getPage(pageNum)
    31      data.dump()
    32  
    33  def exportTable(ese, tableName):
    34      cursor = ese.openTable(tableName)
    35      if cursor is None:
    36          logging.error('Can"t get a cursor for table: %s' % tableName)
    37          return
    38  
    39      i = 1
    40      print("Table: %s" % tableName)
    41      while True:
    42          try:
    43              record = ese.getNextRow(cursor)
    44          except Exception:
    45              logging.debug('Exception:', exc_info=True)
    46              logging.error('Error while calling getNextRow(), trying the next one')
    47              continue
    48  
    49          if record is None:
    50              break
    51          print("*** %d" % i)
    52          for j in list(record.keys()):
    53             if record[j] is not None:
    54                 print("%-30s: %r" % (j, record[j]))
    55          i += 1
    56  
    57  def main():
    58      print(version.BANNER)
    59      # Init the example's logger theme
    60      logger.init()
    61  
    62      parser = argparse.ArgumentParser(add_help = True, description = "Extensive Storage Engine utility. Allows dumping "
    63                                                                      "catalog, pages and tables.")
    64      parser.add_argument('databaseFile', action='store', help='ESE to open')
    65      parser.add_argument('-debug', action='store_true', help='Turn DEBUG output ON')
    66      parser.add_argument('-page', action='store', help='page to open')
    67  
    68      subparsers = parser.add_subparsers(help='actions', dest='action')
    69  
    70      # dump page
    71      dump_parser = subparsers.add_parser('dump', help='dumps an specific page')
    72      dump_parser.add_argument('-page', action='store', required=True, help='page to dump')
    73  
    74      # info page
    75      subparsers.add_parser('info', help='dumps the catalog info for the DB')
    76  
    77      # export page
    78      export_parser = subparsers.add_parser('export', help='dumps the catalog info for the DB')
    79      export_parser.add_argument('-table', action='store', required=True, help='table to dump')
    80  
    81      if len(sys.argv)==1:
    82          parser.print_help()
    83          sys.exit(1)
    84  
    85      options = parser.parse_args()
    86  
    87      if options.debug is True:
    88          logging.getLogger().setLevel(logging.DEBUG)
    89      else:
    90          logging.getLogger().setLevel(logging.INFO)
    91  
    92      ese = ESENT_DB(options.databaseFile)
    93  
    94      try:
    95          if options.action.upper() == 'INFO':
    96              ese.printCatalog()
    97          elif options.action.upper() == 'DUMP':
    98              dumpPage(ese, int(options.page))
    99          elif options.action.upper() == 'EXPORT':
   100              exportTable(ese, options.table)
   101          else:
   102              raise Exception('Unknown action %s ' % options.action)
   103      except Exception as e:
   104          if logging.getLogger().level == logging.DEBUG:
   105              import traceback
   106              traceback.print_exc()
   107          print(e)
   108      ese.close()
   109  
   110  
   111  if __name__ == '__main__':
   112      main()
   113      sys.exit(1)