Ayuto

Offset dumper

Dec 7th, 2013
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.67 KB | None | 0 0
  1. # =============================================================================
  2. # >> IMPORTS
  3. # =============================================================================
  4. # Python
  5. from __future__ import with_statement
  6.  
  7. import os
  8. import pprint
  9. import time
  10.  
  11. from path import path
  12.  
  13. # binutils
  14. import binutils
  15.  
  16.  
  17. # =============================================================================
  18. # >> CONSTANTS
  19. # =============================================================================
  20. # On Linux it's 4 bytes bigger
  21. NEXT_DESC_OFFSET = 52 if os.name == 'nt' else 56
  22.  
  23.  
  24. # =============================================================================
  25. # >> FUNCTIONS
  26. # =============================================================================
  27. def symbol_is_datamap(symbol):
  28.     return not symbol.startswith('_GLOBAL') and '9m_DataMapE' in symbol
  29.    
  30. def dump_datamap(datamap):
  31.     dump = {}
  32.    
  33.     # Get the data map's name
  34.     classname = datamap.get_string(8)
  35.    
  36.     def dump_dmap(proppath, dmap, offset=0):
  37.         # Get the base description
  38.         basedesc = dmap.get_ptr()
  39.        
  40.         # Loop through all properties in this data map
  41.         for x in xrange(dmap.get_int(4)):
  42.             # Get the current description
  43.             desc = basedesc + (NEXT_DESC_OFFSET * x)
  44.            
  45.             # Get the property's name
  46.             name = desc.get_string(4)
  47.            
  48.             # Does the property have no name or is it a function?
  49.             if not name or desc.get_ptr(28):
  50.                 continue
  51.  
  52.             temppath = proppath + '.' + name
  53.            
  54.             # Calculate the current offset
  55.             tempoffset = offset + desc.get_int(8)
  56.            
  57.             # Is there no embedded datamap?
  58.             td = desc.get_ptr(32)
  59.             if not td:
  60.                 dump[temppath] = (
  61.                     tempoffset,
  62.                     desc.get_string(20),
  63.                     desc.get_int()
  64.                 )
  65.             else:
  66.                 # Dump the embedded data map, too!
  67.                 dump_dmap(temppath, td, tempoffset)
  68.  
  69.     # Dump the data map and its subclasses
  70.     while datamap:
  71.         dump_dmap(datamap.get_string(8), datamap)
  72.         datamap = datamap.get_ptr(12)
  73.        
  74.     return classname, dump
  75.  
  76. def dump_file(dump_file, output_file):
  77.     binary = binutils.find_binary(dump_file, False)
  78.    
  79.     with open(output_file, 'w') as f:
  80.         f.write('# Created at: %s\n'% time.ctime())
  81.         f.write('# File: %s\n'% dump_file)
  82.         f.write('# <property path>: (<Linux offset>, <external name>, <field type>)\n')
  83.         f.write('# Estimated Windows offset: <Linux offset> - 20\n\n')
  84.    
  85.         datamap_count = 0
  86.        
  87.         # Loop through all symbols of the file
  88.         for symbol_info in os.popen('nm %s'% dump_file).read().split('\n'):
  89.             if not symbol_info:
  90.                 continue
  91.                
  92.             symbol = symbol_info.rsplit(' ', 1)[1]
  93.            
  94.             # Is the the symbol no data map?
  95.             if not symbol_is_datamap(symbol):
  96.                 continue
  97.                
  98.             datamap_count += 1
  99.             classname, dump = dump_datamap(binary[symbol])
  100.            
  101.             f.write('# Class: %s\n'% classname)
  102.             f.write(pprint.pformat(dump, width=200) + '\n\n')
  103.            
  104.         f.write('# Dumped a total of %s data maps!'% datamap_count)
  105.    
  106.    
  107. # =============================================================================
  108. # >> START DUMPING
  109. # =============================================================================
  110. dump_file('cstrike/bin/server_srv.so', path(__file__).parent.joinpath('output.txt'))
Add Comment
Please, Sign In to add comment