Advertisement
pr4gasm

Untitled

Jul 9th, 2021
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. from triton import *
  2.  
  3. def initCtx(t):
  4.     t.setConcreteRegisterValue(t.registers.rsp,0x7fffffffde40)
  5.  
  6. def createSymVars(t):
  7.     t.symbolizeRegister(t.registers.rbx,"SymRbx")
  8.     rsp_val = t.getConcreteRegisterValue(t.registers.rsp)
  9.     t.symbolizeMemory(MemoryAccess(rsp_val,CPUSIZE.QWORD),"SymRspAccess")
  10.     t.symbolizeMemory(MemoryAccess(rsp_val,CPUSIZE.QWORD),"SymRspAdd2Access")
  11.  
  12. def init():
  13.     t = TritonContext()
  14.     t.setArchitecture(ARCH.X86_64)
  15.     t.enableSymbolicEngine(True)
  16.     t.enableTaintEngine(True)
  17.     initCtx(t)
  18.     createSymVars(t)
  19.     return t
  20.  
  21. def load(t,entry,insns):
  22.     pc = entry
  23.  
  24.     while True:
  25.         insn = Instruction(pc,insns[pc])
  26.         t.processing(insn)
  27.         print(insn)
  28.         if insn.isBranch():
  29.             break
  30.         pc = t.getRegisterAst(t.registers.rip).evaluate()
  31.     return insn
  32. def getConstraint(t,dstAddr):
  33.     if type(dstAddr) != int:
  34.         return None
  35.     astctx = t.getAstContext()
  36.     dstAddrBv = astctx.bv(dstAddr,64)
  37.     rbx = t.getRegisterAst(t.registers.rbx)
  38.     constraint = astctx.unroll(astctx.equal(rbx,dstAddrBv))
  39.     return constraint
  40.  
  41. def getModelVars(m):
  42.     MVars = dict()
  43.     for i in range(len(m)):
  44.         sm = m.get(i)
  45.         if sm == None:
  46.             continue
  47.         sv = sm.getVariable()
  48.         name = sv.getName()
  49.         v = sm.getValue()
  50.         MVars[name] = v
  51.     return MVars
  52.  
  53. def main():
  54.  
  55.     insns = {0x400078:b"\x48\x0F\xC1\x1C\x24",0x40007d:b"\x48\x2B\x5C\x24\x02",0x400082:b"\x48\x0B\x1C\x24",0x400086:b"\xFF\xE3"}
  56.     entry = 0x400078
  57.     takenAddress = 0x40007f
  58.  
  59.     t = init()
  60.     load(t,entry,insns)
  61.     constraint = getConstraint(t,takenAddress)
  62.     if not t.isSat(constraint):
  63.         print("UNSAT")
  64.         exit(-1)
  65.     model = t.getModel(constraint)
  66.     #modelVars = getModelVars(model)
  67.    
  68.     #ks = list(modelVars.keys())
  69.     print("SAT\nmodel:",model,sep="\n\t")
  70.     #for k in ks:
  71.     #    print("%s=0x%x" % (k,modelVars[k]),end="\n\t")
  72.     #print("\r")
  73.  
  74. if __name__ == "__main__":
  75.     main()
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement