Advertisement
Guest User

BaseProcess.py

a guest
Mar 13th, 2017
2,597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.54 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: UTF8 -*-
  3.  
  4. """ Base class for process not linked to any platform """
  5.  
  6. import re
  7. import struct
  8.  
  9. def re_to_unicode(s):
  10.     newstring = ''
  11.     for c in s:
  12.         newstring += re.escape(c) + '\\x00'
  13.  
  14.     return newstring
  15.  
  16.  
  17. def type_unpack(type):
  18.     """ return the struct and the len of a particular type """
  19.     type = type.lower()
  20.     s = None
  21.     l = None
  22.     if type == 'short':
  23.         s = 'h'
  24.         l = 2
  25.     elif type == 'bool':
  26.         s = 'c'
  27.         l = 1
  28.     elif type == 'ushort':
  29.         s = 'H'
  30.         l = 2
  31.     elif type == 'int':
  32.         s = 'i'
  33.         l = 4
  34.     elif type == 'uint':
  35.         s = 'I'
  36.         l = 4
  37.     elif type == 'long':
  38.         s = 'l'
  39.         l = 4
  40.     elif type == 'ulong':
  41.         s = 'L'
  42.         l = 4
  43.     elif type == 'float':
  44.         s = 'f'
  45.         l = 4
  46.     elif type == 'double':
  47.         s = 'd'
  48.         l = 8
  49.     else:
  50.         raise TypeError('Unknown type %s' % type)
  51.     return ('<' + s, l)
  52.  
  53.  
  54. def hex_dump(data, addr = 0, prefix = '', ftype = 'bytes'):
  55.     """
  56.    function originally from pydbg, modified to display other types
  57.    """
  58.     dump = prefix
  59.     slice = ''
  60.     if ftype != 'bytes':
  61.         structtype, structlen = type_unpack(ftype)
  62.         for i in range(0, len(data), structlen):
  63.             if addr % 16 == 0:
  64.                 dump += ' '
  65.                 for char in slice:
  66.                     if ord(char) >= 32 and ord(char) <= 126:
  67.                         dump += char
  68.                     else:
  69.                         dump += '.'
  70.  
  71.                 dump += '\n%s%08X: ' % (prefix, addr)
  72.                 slice = ''
  73.             tmpval = 'NaN'
  74.             try:
  75.                 packedval = data[i:i + structlen]
  76.                 tmpval = struct.unpack(structtype, packedval)[0]
  77.             except Exception as e:
  78.                 print e
  79.  
  80.             if tmpval == 'NaN':
  81.                 dump += '{:<15} '.format(tmpval)
  82.             elif ftype == 'float':
  83.                 dump += '{:<15.4f} '.format(tmpval)
  84.             else:
  85.                 dump += '{:<15} '.format(tmpval)
  86.             addr += structlen
  87.  
  88.     else:
  89.         for byte in data:
  90.             if addr % 16 == 0:
  91.                 dump += ' '
  92.                 for char in slice:
  93.                     if ord(char) >= 32 and ord(char) <= 126:
  94.                         dump += char
  95.                     else:
  96.                         dump += '.'
  97.  
  98.                 dump += '\n%s%08X: ' % (prefix, addr)
  99.                 slice = ''
  100.             dump += '%02X ' % ord(byte)
  101.             slice += byte
  102.             addr += 1
  103.  
  104.     remainder = addr % 16
  105.     if remainder != 0:
  106.         dump += '   ' * (16 - remainder) + ' '
  107.     for char in slice:
  108.         if ord(char) >= 32 and ord(char) <= 126:
  109.             dump += char
  110.         else:
  111.             dump += '.'
  112.  
  113.     return dump + '\n'
  114.  
  115.  
  116. class ProcessException(Exception):
  117.     pass
  118.  
  119. class BaseProcess(object):
  120.  
  121.     def __init__(self, *args, **kwargs):
  122.         """ Create and Open a process object from its pid or from its name """
  123.         self.h_process = None
  124.         self.pid = None
  125.         self.isProcessOpen = False
  126.         self.buffer = None
  127.         self.bufferlen = 0
  128.  
  129.     def __del__(self):
  130.         self.close()
  131.  
  132.     def close(self):
  133.         pass
  134.     def iter_region(self, *args, **kwargs):
  135.         raise NotImplementedError
  136.     def write_bytes(self, address, data):
  137.         raise NotImplementedError
  138.  
  139.     def read_bytes(self, address, bytes = 4):
  140.         raise NotImplementedError
  141.  
  142.     def get_symbolic_name(self, address):
  143.         return '0x%08X' % int(address)
  144.  
  145.     def read(self, address, type = 'uint', maxlen = 50, errors='raise'):
  146.         if type == 's' or type == 'string':
  147.             s = self.read_bytes(int(address), bytes=maxlen)
  148.             news = ''
  149.             for c in s:
  150.                 if c == '\x00':
  151.                     return news
  152.                 news += c
  153.             if errors=='ignore':
  154.                 return news
  155.             raise ProcessException('string > maxlen')
  156.         else:
  157.             if type == 'bytes' or type == 'b':
  158.                 return self.read_bytes(int(address), bytes=1)
  159.             s, l = type_unpack(type)
  160.             return struct.unpack(s, self.read_bytes(int(address), bytes=l))[0]
  161.  
  162.     def write(self, address, data, type = 'uint'):
  163.         if type != 'bytes':
  164.             s, l = type_unpack(type)
  165.             return self.write_bytes(int(address), struct.pack(s, data))
  166.         else:
  167.             return self.write_bytes(int(address), data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement