summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMalfurious <m@lfurio.us>2022-03-17 03:51:25 -0400
committerMalfurious <m@lfurio.us>2022-03-17 03:51:25 -0400
commitc557d154b49eb76ddc955e9fa023bf4d7f3bb5b5 (patch)
tree5dfc653feaada601adb15f72da6548b295300403
parent380bc782b53bdafc2b1d5d37afb16d6d7b91a0e5 (diff)
parenteca4614ed7bf14117f45da023d23eb2d67432bab (diff)
downloadlib-des-gnux-c557d154b49eb76ddc955e9fa023bf4d7f3bb5b5.tar.gz
lib-des-gnux-c557d154b49eb76ddc955e9fa023bf4d7f3bb5b5.zip
Merge branch 'sploit/symtbl-retcon'
Fixes some problems found in the Sploit Symtbl module, and includes some additional cleanup suggested by dusoleil. * sploit/symtbl-retcon: sploit: Clean up use of __getattribute__ sploit: Fix bugs and simplify Symtbl
-rw-r--r--tools/sploit/sploit/mem.py127
-rw-r--r--tools/sploit/sploit/rev/elf.py5
-rw-r--r--tools/sploit/sploit/util.py8
3 files changed, 46 insertions, 94 deletions
diff --git a/tools/sploit/sploit/mem.py b/tools/sploit/sploit/mem.py
index ac2bbb1..3fee92f 100644
--- a/tools/sploit/sploit/mem.py
+++ b/tools/sploit/sploit/mem.py
@@ -1,88 +1,51 @@
-from sploit.util import __attr_filter__
+import types
class Symtbl:
- __subs__ = {}
def __init__(self, **kwargs):
- self.__dict__ = {**kwargs}
-
- def subtable(self, sym, off, table):
- setattr(self, sym, off)
- self.__subs__[sym] = table
-
- class __InnerTable__:
- def __init__(self,off,tbl):
- self.off = off
- self.tbl = tbl
- def __getattribute__(self,sym):
- if(sym in (['off','tbl'] + __attr_filter__)):
- return object.__getattribute__(self,sym)
- addr = getattr(self.tbl,sym)
- if(type(addr)==int):
- return addr + self.off
- if(type(addr)==self.__class__):
- addr.off += self.off
- return addr
- return addr
- def __setattr__(self,sym,off):
- if(sym in ['off','tbl']):
- return object.__setattr__(self,sym,off)
- return setattr(self.tbl,sym,off-self.off)
- def __str__(self):
- return str(self.tbl)
-
- def __getattribute__(self, sym):
- addr = object.__getattribute__(self,sym)
- if(sym in (['__subs__'] + __attr_filter__)):
- return addr
- if(sym == 'base'):return 0
- if(sym in self.__subs__):
- return self.__InnerTable__(addr,self.__subs__[sym])
- return addr
+ object.__setattr__(self, '_namesp', types.SimpleNamespace(base=0,sym={},sub={}))
+ for k, v in {**kwargs}.items():
+ setattr(self, k, v)
+
+ def __getattr__(self, ident):
+ self = self._namesp
+ if ident == 'base': return self.base
+ off = self.base + self.sym[ident]
+ if ident in self.sub: return self.sub[ident].map(off)
+ return off
+
+ def __setattr__(self, ident, value):
+ if ident in dir(self): raise Exception(f'Symtbl: assignment would shadow non-symbol "{ident}"')
+ if ident == 'base': raise Exception('Symtbl: may not redefine symbol "base"')
+ self = self._namesp
+ if type(value) is tuple: self.sub[ident], off = value
+ else: off = value
+ self.sym[ident] = off - self.base
+
+ def map(self, addr, off=0):
+ self = self._namesp
+ mm = Symtbl()
+ mm._namesp.sym, mm._namesp.sub = self.sym, self.sub
+ mm._namesp.base = addr - off
+ return mm
def adjust(self, off):
- self.__dict__ = {k:v+off for k,v in self.__dict__.items()}
-
- def rebase(self, sym):
- self.adjust(-sym)
-
- def __str__(self):
- return __str__(self,self.__dict__)
-
-class Memmap:
- def __init__(self, tbl, sym, addr):
- self.__tbl__ = tbl
- self.base = addr - sym
-
- def __getattribute__(self, sym):
- if(sym in (['__tbl__','base'] + __attr_filter__)):
- return object.__getattribute__(self, sym)
- addr = getattr(self.__tbl__, sym)
- if(type(addr)==Symtbl.__InnerTable__):
- addr.off += self.base
- return addr
- return self.base + addr
-
- def __setattr__(self, sym, addr):
- if(sym in ['__tbl__','base']):
- return object.__setattr__(self,sym,addr)
- return setattr(self.__tbl__,sym,addr-self.base)
-
- def __str__(self):
- s = __str__(self,self.__tbl__.__dict__)
- pos = -1
- for i in range(2):
- pos = s.find('\n',pos+1)
- s = s[:pos] + __tbl_format__.format(hex(self.base),'base') + s[pos:]
+ self = self._namesp
+ for k, v in self.sym.items():
+ self.sym[k] = v + off
+
+ def rebase(self, off):
+ self.adjust(-off)
+
+ def __str__(_self):
+ FMT = '\n{:<20} {:<20}'
+ self = _self._namesp
+
+ s = f'{len(self.sym)} symbols @ {hex(_self.base)}'
+ s += FMT.format('ADDRESS', 'SYMBOL')
+ for sym, _ in sorted(self.sym.items(), key=lambda x:x[1]):
+ addr = getattr(_self, sym)
+ if type(addr) is Symtbl:
+ s += FMT.format(hex(addr.base), f'[{sym}]')
+ else:
+ s += FMT.format(hex(addr), sym)
return s
-
-__tbl_format__ = '\n{:<20} {:<20}'
-def __str__(self,tbl):
- s = 'symbols: ' + str(len(tbl))
- s += __tbl_format__.format('ADDRESS', 'SYMBOL')
- for sym,off in sorted(tbl.items(),key=lambda x:x[1]):
- addr = getattr(self,sym)
- if(type(addr)==Symtbl.__InnerTable__):
- s += __tbl_format__.format(hex(addr.off),f'[{sym}]')
- else:
- s += __tbl_format__.format(hex(addr),sym)
- return s
diff --git a/tools/sploit/sploit/rev/elf.py b/tools/sploit/sploit/rev/elf.py
index 7bfd31f..e099819 100644
--- a/tools/sploit/sploit/rev/elf.py
+++ b/tools/sploit/sploit/rev/elf.py
@@ -1,5 +1,4 @@
from sploit.rev import ldd, r2
-from sploit.util import __attr_filter__
class ELF:
def __init__(self, path):
@@ -38,9 +37,7 @@ class ELF:
class __LOCALS__:
def __init__(self, elf):
self.elf = elf
- def __getattribute__(self, sym):
- if(sym in (['elf'] + __attr_filter__)):
- return object.__getattribute__(self,sym)
+ def __getattr__(self, sym):
return r2.get_locals(self.elf.path, getattr(self.elf.sym, sym))
def retaddr(self, caller, callee):
diff --git a/tools/sploit/sploit/util.py b/tools/sploit/sploit/util.py
index 8a259c4..c44ab78 100644
--- a/tools/sploit/sploit/util.py
+++ b/tools/sploit/sploit/util.py
@@ -12,11 +12,3 @@ def run_cmd_cached(cmd):
result = run_cmd(cmd)
__RUN_CACHE__[key] = result
return result
-
-__attr_filter__ = ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__',
- '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__',
- '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__',
- '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
- '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
- '__weakref__']
-