コード例 #1
0
def main():
    if len(argv) == 1:
        io = process("./a.out")
    else:
        io = remote(argv[1], int(argv[2]))
    log.info("get output")
    out = get_output(io, 10)
    x0, y0 = z3.BitVecs('x0 y0', 64)
    x, y = x0, y0
    s = z3.SimpleSolver()

    for v in out:
        s.add((x + y) & bit64 == v)
        x, y = xo128(x, y, z3.LShR)

    ans = []

    for i in range(1, sys.maxsize):
        if s.check().r != 1: break  # quit if failed
        soln = s.model()
        x, y = (soln[i].as_long() for i in (x0, y0))
        ans += [
            "ACTF{" + long_to_bytes(x).decode("utf-8")[::-1] +
            long_to_bytes(y).decode("utf-8")[::-1] + "}"
        ]
        for j in range(10):
            x, y = xo128(x, y)
        s.add(z3.Or(x0 != soln[x0], y0 != soln[y0]))

    for a in ans:
        log.info("possible flag: " + a)
コード例 #2
0
ファイル: esilstate.py プロジェクト: Asll666/esilsolve
    def __init__(self, r2api: R2API, **kwargs):
        self.kwargs = kwargs
        self.r2api = r2api
        self.pure_symbolic = kwargs.get("sym", False)
        self.pcode = kwargs.get("pcode", False)
        self.check_perms = kwargs.get("check", False)

        if kwargs.get("optimize", False):
            self.solver = z3.Optimize()
        elif kwargs.get("simple", True):
            self.solver = z3.SimpleSolver()
        else:
            self.solver = z3.Solver()

        #self.solver.set("cache_all", True)

        timeout = kwargs.get("timeout", None)
        if timeout != None:
            self.solver.set("timeout", timeout)

        #self.solver.set("threads", 4)

        # without this push z3 does not use an "incremental" solver
        # which causes it to try tactics which are wildly slow
        self.solver.push()

        #self.constraints = []
        self.model = None
        self.current_instruction = None

        self.esil = {"cur": 0, "old": 0, "stack": [], "size": 64, "type": 1}

        self.stack = self.esil["stack"]
        self.info = self.r2api.get_info()
        self.debug = kwargs.get("debug", False)
        self.trace = kwargs.get("trace", False)

        self.memory: ESILMemory = None
        self.registers: ESILRegisters = None
        self.proc: ESILProcess = None

        self.aliases = {}
        self.condition = None

        # steps executed and distance from goal
        self.steps = 0
        self.distance = 0xffffffff
        self.target = None

        if "info" in self.info:
            self.bits = self.info["info"]["bits"]
            self.endian = self.info["info"]["endian"]
        else:
            self.bits = 64
            self.endian = "little"

        if kwargs.get("init", True):
            self.proc = ESILProcess(r2api, **kwargs)
            self.init_state()
コード例 #3
0
 def _fresh_init(self, config=None):
     super(CovManager, self)._fresh_init(config)
     if not self.__dict__ or "_cov_manager_status" not in self.__dict__ or not self._cov_manager_status:
         # a result cache when solving conditions for sym_nodes
         self.cov_cached = {}
         # explanation please refer to `_ast_manager_status` in manager.AstManager
         self._cov_manager_status = True
         # the rtb constraint read from rtb file.
         self.predicate_expr = True
         # the cache for z3 expression satisfiability checking
         self.z3_cache = {}
         z3.set_option("smt.arith.solver", 2)
         self.z3_solver = z3.SimpleSolver()
         self.z3_assumptions_cache = {}
         self.z3_assumptions_computed_cache = {}
         self._infer_wrapper = lambda x: x
         # caching of abutments cover calculation involving multiple inference results
         self.leaf_cells_dict = {}
         # list of function arguments that is z3 variables
         self._z3_func_args: List[nodes.Arg] = []
コード例 #4
0
func = sys.argv[1]  # '+' = plus scrambler
if func not in ('+', '**'):  # '**' = mul-rotate-mul
    print 'Unknown scrambler'
    sys.exit()

if sys.argv[2] == 'seed':  # usage xo256.py func seed x0 x1 x2 x3
    x = [int(sys.argv[i], 0) for i in range(3, 7)]
    for i in range(10):  # generate random numbers
        print '0x%x' % scramble(x, func)
        xoshiro256(x)
    sys.exit()

x0, x1, x2, x3 = z3.BitVecs('x0 x1 x2 x3', 64)
x = [x0, x1, x2, x3]  # mutable state, allow update
s = z3.SimpleSolver()

for v in sys.argv[2:]:  # usage xo256.py func seq1 seq2 seq3 ...
    s.add(scramble(x, func, z3.LShR) == int(v, 0))
    xoshiro256(x, z3.LShR)

for i in xrange(1, sys.maxint):
    print '\n#%d = %s' % (i, s.check())
    if s.check().r != 1: break  # quit if failed
    soln = s.model()
    x = [soln[i].as_long() for i in (x0, x1, x2, x3)]
    print 'seed = 0x%016x,%016x,%016x,%016x' % tuple(x)
    for j in range(10):  # show predictions
        print '0x%x' % scramble(x, func)
        xoshiro256(x)
    s.add(