def __relocate__(self, data, symbol, section, namespace=None): '''Apply relocations for the specified ``symbol`` to the ``data``.''' namespace = namespace or {} # figure out the relocation information relocationva, relocationtype = self['VirtualAddress'].int(), self['Type'].int() # figure out the symbol type storageclass = symbol['StorageClass'].int() # get the symbol name so that it can be looked up against the namespace name = symbol['Name'].str() # lookup the symbol's value in the namespace first...otherwise, use what was actually assigned in the symbol table value = namespace.get(name, symbol['Value'].int()) # extract the value that's already encoded within the section's data result = ptypes.bitmap.zero generator = ( bitmap.new(ch, 8) for ch in data[relocationva : relocationva + 4] ) for x in generator: result = bitmap.insert(result, x) result = bitmap.int(result) currentva = relocationva + 4 # FIXME: figure out the machine type in order to determine the relocation types and how to apply them # XXX: this is only for x86 # figure out where to get the relocation's value from based on the relocation type if section is None: # externally defined result = value elif relocationtype == 0: pass # XXX: will these relocations work? elif relocationtype == 6: # 32-bit VA result = (value+result) # print '>',name,hex(result),targetsectionname,hex(namespace[targetsectionname]) elif relocationtype == 0x14: # 32-bit relative displacement result = (value+result+4) - (currentva) #raise NotImplementedError(relocationtype) elif relocationtype == 7: # use real virtual address (???) result = value elif relocationtype in [0xA, 0xB]: # [section index, offset from section] raise NotImplementedError(relocationtype) else: raise NotImplementedError(relocationtype) # calculate relocation and assign it into an array result, serialized = bitmap.new(result, 32), array.array('B','') while result[1] > 0: result, value = bitmap.consume(result, 8) serialized.append(value) # update segment data with new serialized relocation if len(serialized) != 4: raise AssertionError("Expected size of relocation was expected to be {:d} bytes : {:d} != {:d}".format(4, len(serialized), 4)) data[relocationva : relocationva + len(serialized)] = serialized # we're done. so return it back to the user return data
def set(self, string): if string in self._values_.viewvalues(): res = dict((v, k) for k, v in self._values_.viewitems()) return self.set(res[string]) res = map(int, string.split('.')) val = [res.pop(0)*40 + res.pop(0)] for n in res: if n <= 127: val.append(n) continue # convert integer to a bitmap x = bitmap.new(0,0) while n > 0: x = bitmap.insert(x, (n&0xf,4)) n //= 0x10 # shuffle bitmap into oid components y = [] while bitmap.size(x) > 0: x,v = bitmap.consume(x, 7) y.insert(0, v) val.extend([x|0x80 for x in y[:-1]] + [y[-1]]) return super(OBJECT_IDENTIFIER, self).set(str().join(map(six.int2byte, val)))
def set_bitmap_unsigned(): x = bitmap.new(0xf000000000000000, 64) #x = bitmap.set(x, 60, count=4) print(bitmap.string(x)) y, res = bitmap.shift(x, 4) print(res, bitmap.string(y)) x = bitmap.new(0, 0) x = bitmap.push(x, (0x1, 4)) x = bitmap.push(x, (0x2, 4)) x = bitmap.push(x, (0x3, 4)) x = bitmap.push(x, (0x4, 4)) print(x, bitmap.string(x)) x = bitmap.new(0, 0) x = bitmap.insert(x, (0x1, 4)) x = bitmap.insert(x, (0x2, 4)) x = bitmap.insert(x, (0x3, 4)) x = bitmap.insert(x, (0x4, 4)) print(x, bitmap.string(x)) x = bitmap.consumer(b'\x12\x34') print(x.consume(4)) print(x.consume(4)) print(x.consume(4)) print(x.consume(4)) x = bitmap.new(0, 4) for i in range(6): print(x) x = bitmap.add(x, 3) for i in range(6): print(x) x = bitmap.sub(x, 6) x = bitmap.new(4, 4) print(bitmap.string(bitmap.ror(bitmap.ror(bitmap.ror(x)))))
def set_bitmap_unsigned(): x = bitmap.new(0xf000000000000000,64) #x = bitmap.set(x, 60, count=4) print bitmap.string(x) y,res = bitmap.shift(x, 4) print res,bitmap.string(y) x = bitmap.new(0,0) x = bitmap.push(x, (0x1,4) ) x = bitmap.push(x, (0x2,4) ) x = bitmap.push(x, (0x3,4) ) x = bitmap.push(x, (0x4,4) ) print x,bitmap.string(x) x = bitmap.new(0,0) x = bitmap.insert(x, (0x1,4) ) x = bitmap.insert(x, (0x2,4) ) x = bitmap.insert(x, (0x3,4) ) x = bitmap.insert(x, (0x4,4) ) print x,bitmap.string(x) x = bitmap.consumer('\x12\x34') print x.consume(4) print x.consume(4) print x.consume(4) print x.consume(4) x = bitmap.new(0, 4) for i in six.moves.range(6): print x x = bitmap.add(x, 3) for i in six.moves.range(6): print x x = bitmap.sub(x, 6) x = bitmap.new(4,4) print bitmap.string(bitmap.ror(bitmap.ror(bitmap.ror(x))))
def decodeInteger(string, signed=False): '''given a string encoded in the native byte-order, will produce an integer''' res = bitmap.new(0,0) for ch in string: res = bitmap.insert(res, (ord(ch),8)) res,_ = res if not signed: return res bitflag = (0x100**len(string)) / 2 signbit,value = res & bitflag, res & (bitflag - 1) if res & signbit: return value - bitflag return value
def decodeInteger(string, signed=False): '''given a string encoded in the native byte-order, will produce an integer''' res = bitmap.new(0, 0) for ch in string: res = bitmap.insert(res, (ord(ch), 8)) res, _ = res if not signed: return res bitflag = (0x100**len(string)) // 2 signbit, value = res & bitflag, res & (bitflag - 1) if res & signbit: return value - bitflag return value
def __relocate(self, data, symbol, sectionarray, currentsectionname, targetsectionname, namespace): relocationva, relocationtype = self["VirtualAddress"].int(), self["Type"].num() name = symbol["Name"].str() storageclass, value = symbol["StorageClass"].num(), namespace[name] if value is None: raise ValueError("Attempted relocation for an undefined symbol `%s'." % name) result = bitmap.new(0, 0) generator = (bitmap.new(ord(x), 8) for x in data[relocationva : relocationva + 4]) for x in generator: result = bitmap.insert(result, x) result = result[0] currentva = relocationva + 4 if targetsectionname is None: # externally defined result = value elif relocationtype == 0: pass # XXX: will these relocations work? elif relocationtype == 6: # 32-bit VA result = value + result # print '>',name,hex(result),targetsectionname,hex(namespace[targetsectionname]) elif relocationtype == 0x14: # 32-bit relative displacement result = (value + result + 4) - (currentva) raise NotImplementedError(relocationtype) elif relocationtype == 7: # use real virtual address (???) result = value elif relocationtype in [0xA, 0xB]: # [section index, offset from section] raise NotImplementedError(relocationtype) else: raise NotImplementedError(relocationtype) result, serialized = bitmap.new(result, 32), array.array("c", "") while result[1] > 0: result, value = bitmap.consume(result, 8) serialized.append(chr(value)) assert len(serialized) == 4 data[relocationva : relocationva + len(serialized)] = serialized return data
def set(self, string): res = map(int, string.split('.')) val = [res.pop(0) * 40 + res.pop(0)] for n in res: if n <= 127: val.append(n) continue # convert integer to a bitmap x = bitmap.new(0, 0) while n > 0: x = bitmap.insert(x, (n & 0xf, 4)) n /= 0x10 # shuffle bitmap into oid components y = [] while bitmap.size(x) > 0: x, v = bitmap.consume(x, 7) y.insert(0, v) val.extend([x | 0x80 for x in y[:-1]] + [y[-1]]) return super(OBJECT_IDENTIFIER).set(''.join(map(chr, val)))
def bitmap(self): res = bitmap.zero for index, msize, busy in self['metadata'].busyfree(): val = bitmap.new((2**msize - 1) if busy else 0, msize) res = bitmap.insert(res, val) return res
def __relocate__(self, data, symbol, section, namespace=None): '''Apply relocations for the specified ``symbol`` to the ``data``.''' raise NotImplementedError( 'This internal method has been deprecated due to a refactor') namespace = namespace or {} # figure out the relocation information relocationva, relocationtype = self['VirtualAddress'].int( ), self['Type'].int() # figure out the symbol type storageclass = symbol['StorageClass'].int() # get the symbol name so that it can be looked up against the namespace name = symbol['Name'].str() # lookup the symbol's value in the namespace first...otherwise, use what was actually assigned in the symbol table value = namespace.get(name, symbol['Value'].int()) # extract the value that's already encoded within the section's data result = ptypes.bitmap.zero generator = (bitmap.new(ch, 8) for ch in data[relocationva:relocationva + 4]) for x in generator: result = bitmap.insert(result, x) result = bitmap.int(result) currentva = relocationva + 4 # FIXME: figure out the machine type in order to determine the relocation types and how to apply them # XXX: this is only for x86 # figure out where to get the relocation's value from based on the relocation type if section is None: # externally defined result = value elif relocationtype == 0: pass # XXX: will these relocations work? elif relocationtype == 6: # 32-bit VA result = (value + result) # print('>',name,hex(result),targetsectionname,hex(namespace[targetsectionname])) elif relocationtype == 0x14: # 32-bit relative displacement result = (value + result + 4) - (currentva) #raise NotImplementedError(relocationtype) elif relocationtype == 7: # use real virtual address (???) result = value elif relocationtype in [0xA, 0xB]: # [section index, offset from section] raise NotImplementedError(relocationtype) else: raise NotImplementedError(relocationtype) # calculate relocation and assign it into an array result, serialized = bitmap.new(result, 32), array.array('B', '') while result[1] > 0: result, value = bitmap.consume(result, 8) serialized.append(value) # update segment data with new serialized relocation if len(serialized) != 4: raise AssertionError( "Expected size of relocation was expected to be {:d} bytes : {:d} != {:d}" .format(4, len(serialized), 4)) data[relocationva:relocationva + len(serialized)] = serialized # we're done. so return it back to the user return data