summaryrefslogtreecommitdiffstats
path: root/sploit/mem.py
blob: cf138b360e50fa438d34e0b7211340ad2e0d79a8 (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
44
45
46
47
48
49
50
51
52
53
54
55
class Symtbl:
    def __init__(self, **kwargs):
        self.__dict__ = {**kwargs}

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

    def rebase(self, sym):
        self.adjust(-sym)

    def __len__(self):
        vals = self.__dict__.values()
        if len(vals)<1:
            return 0
        hi = max(max(vals),0)
        lo = min(min(vals),0)
        return hi-lo

    def __str__(self):
        return __str__(self,self.__dict__)

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 __len__(self):
        return len(self.__tbl__)

    def __str__(self):
        s = __str__(self,self.__tbl__.__dict__)
        pos = -1
        for i in range(3):
            pos = s.find('\n',pos+1)
        s = s[:pos] + __tbl_format__.format(hex(self.base),'base') + s[pos:]
        return s

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