summaryrefslogtreecommitdiffstats
path: root/sploit/mem.py
blob: e257c034ea47ba841e1a4e7354c718519aff3266 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Symtbl:
    def __init__(self, **kwargs):
        self.__dict__ = {**kwargs}

    def __str__(self):
        tbl_format = '\n{:<20} {:<20}'
        s = 'len: ' + str(len(self.__dict__))
        s += tbl_format.format('ADDRESS', 'SYMBOL')
        for sym,addr in sorted(self.__dict__.items(),key=lambda x:x[1]):
            s += tbl_format.format(hex(addr),sym)
        return s

class Memmap:
    def __init__(self, tbl, sym, addr):
        object.__setattr__(self,'__tbl__', tbl)
        base = addr if sym == 'base' else addr - getattr(self.__tbl__, sym)
        object.__setattr__(self,'base', base)

    def __getattribute__(self, sym):
        if sym == '__tbl__' or sym == 'base':
            return object.__getattribute__(self, sym)
        a = getattr(self.__tbl__, sym)
        return self.base + a

    def __setattr__(self, k, v):
        raise TypeError('Memmaps are Read-Only! Modify offsets with Symtbl instead!')

    def __str__(self):
        tbl_format = '\n{:<20} {:<20}'
        s = 'len: ' + str(len(self.__tbl__.__dict__)+1)
        s += tbl_format.format('ADDRESS', 'SYMBOL')
        s += tbl_format.format(hex(self.base),'base')
        for sym,addr in sorted(self.__tbl__.__dict__.items(),key=lambda x:x[1]):
            s += tbl_format.format(hex(addr+self.base),sym)
        return s

def adjust(tbl, off):
    tbl.__dict__ = {k:v+off for k,v in tbl.__dict__.items()}

def rebase(tbl, sym):
    off = -getattr(tbl, sym)
    adjust(tbl, off)