def test_array2bytes(self): """array to bytes""" ctypes = types.reload_ctypes(4, 4, 8) a = (ctypes.c_long * 12)(4, 1, 1, 1, 2) x = utils.array2bytes(a) self.assertEquals(b'\x04' + 3 * b'\x00' + b'\x01' + 3 * b'\x00' + b'\x01' + 3 * b'\x00' + b'\x01' + 3 * b'\x00' + b'\x02' + 3 * b'\x00' + 7 * 4 * '\x00', x) ctypes = types.reload_ctypes(8, 8, 16) a = (ctypes.c_long * 12)(4, 1, 1, 1, 2) x = utils.array2bytes(a) self.assertEquals(b'\x04' + 7 * b'\x00' + b'\x01' + 7 * b'\x00' + b'\x01' + 7 * b'\x00' + b'\x01' + 7 * b'\x00' + b'\x02' + 7 * b'\x00' + 7 * 8 * '\x00', x) a = (ctypes.c_char * 12).from_buffer_copy('1234567890AB') x = utils.array2bytes(a) self.assertEquals(b'1234567890AB', x) # mimics what ctypes gives us on memory loading. a = b'1234567890AB' x = utils.array2bytes(a) self.assertEquals(b'1234567890AB', x) pass
def __str__(self): """Print the direct members values. Never tries to recurse.""" import ctypes if hasattr(self, '_orig_address_'): s = "# <%s at @%x>\n" % ( self.__class__.__name__, self._orig_address_) else: s = "# <%s at @???>\n" % (self.__class__.__name__) # we need to ensure _mappings_ is defined in all children. for field, attrtype in self.getFields(): attr = getattr(self, field) if ctypes.is_basic_type(attrtype): # basic type, ctypes or python s += '%s : %s, \n' % (field, repr(attr)) elif (ctypes.is_struct_type(attrtype) or ctypes.is_union_type(attrtype)): # you can print a inner struct content s += '%s (@0x%lx) : {\t%s}\n' % (field, ctypes.addressof(attr), attr) elif ctypes.is_function_type(attrtype): # only print address in target space s += '%s (@0x%lx) : 0x%lx (FIELD NOT LOADED: function type)\n' % ( field, ctypes.addressof(attr), utils.get_pointee_address(attr)) elif ctypes.is_array_of_basic_type(attrtype): try: s += '%s (@0x%lx) : %s\n' % (field, ctypes.addressof(attr), repr(utils.array2bytes(attr))) except IndexError as e: log.error('error while reading %s %s' % (repr(attr), type(attr))) # FIXME elif ctypes.is_array_type(attrtype): # array of something else than int s += '%s (@0x%lx) :[' % (field, ctypes.addressof(attr)) s += ','.join(["%s" % (val) for val in attr]) s += '],\n' elif ctypes.is_cstring_type(attrtype): # only print address/null s += '%s (@0x%lx) : 0x%lx\n' % (field, ctypes.addressof(attr), utils.get_pointee_address(attr.ptr)) elif ctypes.is_pointer_type(attrtype): # and # not ctypes.is_pointer_to_void_type(attrtype)): # do not recurse. if attr is None: attr = 0 s += '%s (@????) : 0x0\n' % (field) else: s += '%s (@0x%lx) : 0x%lx\n' % (field, ctypes.addressof(attr), utils.get_pointee_address(attr)) elif (isinstance(attr, long)) or (isinstance(attr, int)): s += '%s : %s\n' % (field, hex(attr)) else: s += '%s : %s\n' % (field, repr(attr)) return s
def getIV(self): return array2bytes(self.r_iv)
def getDk(self): return array2bytes(self.dk)
def getEk(self): return array2bytes(self.ek)
def getCounter(self): return array2bytes(self.aes_counter)
def getKey(self): #return pointer2bytes(self.key,self.key_len) return utils.array2bytes( mappings.getRef( utils.get_subtype(self.key), get_pointee_address(self.key)) )
def BF_KEY_getP(self): return array2bytes(self.P)
def CAST_KEY_getData(self): return array2bytes(self.data)
def RC4_KEY_getData(self): return array2bytes(self.data)
def EVP_CIPHER_CTX_getIV(self): return array2bytes(self.iv)
def AES_KEY_getKey(self): #FIXME return array2bytes(self.rd_key)
def BF_KEY_getS(self): return array2bytes(self.S)