Esempio n. 1
0
    def _pre_analysis(self):

        # Call _initialize_cfg() before self.functions is used.
        self._initialize_cfg()

        # Initialize variables used during analysis
        self._pending_jobs = PendingJobs(self.functions, self._deregister_analysis_job)
        self._traced_addresses = set()
        self._changed_functions = set()
        self._updated_nonreturning_functions = set()

        self._nodes = {}
        self._nodes_by_addr = defaultdict(list)

        self._function_returns = defaultdict(set)

        entry = self.project.entry  # type:SootAddressDescriptor
        entry_func = entry.method

        obj = self.project.loader.main_object

        if entry_func is not None:
            method_inst = obj.get_soot_method(
                entry_func.name, class_name=entry_func.class_name, params=entry_func.params)
        else:
            l.warning('The entry method is unknown. Try to find a main method.')
            method_inst = next(obj.main_methods, None)
            if method_inst is not None:
                entry_func = SootMethodDescriptor(method_inst.class_name, method_inst.name, method_inst.params)
            else:
                l.warning('Cannot find any main methods. Start from the first method of the first class.')
                for cls in obj.classes.values():
                    method_inst = next(iter(cls.methods), None)
                    if method_inst is not None:
                        break
                if method_inst is not None:
                    entry_func = SootMethodDescriptor(method_inst.class_name, method_inst.name,
                                                      method_inst.params)
                else:
                    raise AngrCFGError('There is no method in the Jar file.')

        # project.entry is a method
        # we should get the first block
        if method_inst.blocks:
            block_idx = method_inst.blocks[0].idx
            self._insert_job(CFGJob(SootAddressDescriptor(entry_func, block_idx, 0), entry_func, 'Ijk_Boring'))

        total_methods = 0

        # add all other methods as well
        for cls in self.project.loader.main_object.classes.values():
            for method in cls.methods:
                total_methods += 1
                if method.blocks:
                    method_des = SootMethodDescriptor(cls.name, method.name, method.params)
                    # TODO shouldn't this be idx?
                    block_idx = method.blocks[0].label
                    self._insert_job(CFGJob(SootAddressDescriptor(method_des, block_idx, 0), method_des, 'Ijk_Boring'))

        self._total_methods = total_methods
Esempio n. 2
0
    def _soot_create_invoke_successors(self, stmt, addr, invoke_expr):

        method_class = invoke_expr.class_name
        method_name = invoke_expr.method_name
        method_params = invoke_expr.method_params
        method_desc = SootMethodDescriptor(method_class, method_name,
                                           method_params)

        callee_soot_method = self.project.loader.main_object.get_soot_method(
            method_desc, none_if_missing=True)
        caller_soot_method = self.project.loader.main_object.get_soot_method(
            addr.method)

        if callee_soot_method is None:
            # this means the called method is external
            return [(stmt.label, addr,
                     SootAddressDescriptor(method_desc, 0, 0), 'Ijk_Call')]

        targets = self._soot_class_hierarchy.resolve_invoke(
            invoke_expr, callee_soot_method, caller_soot_method)

        successors = []
        for target in targets:
            target_desc = SootMethodDescriptor(target.class_name, target.name,
                                               target.params)
            successors.append(
                (stmt.label, addr, SootAddressDescriptor(target_desc, 0,
                                                         0), 'Ijk_Call'))

        return successors
Esempio n. 3
0
    def _resolve_invoke_target(self, expr, state):
        # get the type of the base object
        base = translate_expr(self.expr.base, self.state).expr
        if base is not None:
            base_type = base.type
        else:
            # the base is not set, for example if we process an invocation of an
            # unloaded library function
            # => fallback: use the statically retrieved type
            base_type = self.expr.class_name

        # based on the class of the base object, we resolve the invoke target
        try:
            return resolve_method(state=self.state,
                                  method_name=self.expr.method_name,
                                  class_name=base_type,
                                  params=self.expr.method_params,
                                  ret_type=self.expr.type,
                                  raise_exception_if_not_found=True)
        except SootMethodNotLoadedException:
            # in case that the method is not loaded, continue with the infos
            # available from the invoke expression
            return SootMethodDescriptor(self.expr.class_name,
                                        self.expr.method_name,
                                        self.expr.method_params,
                                        ret_type=self.expr.type)
Esempio n. 4
0
def test_androidnative1():
    sdk_path = os.path.join(os.path.expanduser("~"), "Android/Sdk/platforms/")
    if not os.path.exists(sdk_path):
        print(
            "cannot run test_apk_loading since there is no Android SDK folder")
        return

    apk_location = os.path.join(file_dir, "androidnative1.apk")
    loading_opts = {
        'android_sdk': sdk_path,
        'entry_point': 'com.angr.nativetest1.MainActivity.onCreate',
        'entry_point_params': ('android.os.Bundle', ),
        'supported_jni_archs': ['x86']
    }
    project = angr.Project(apk_location, main_opts=loading_opts)
    project.hook(
        SootMethodDescriptor(class_name="java.lang.String",
                             name="valueOf",
                             params=('int', )).address(),
        Dummy_String_valueOf())

    blank_state = project.factory.blank_state()
    a1 = SimSootValue_ThisRef.new_object(
        blank_state, 'com.angr.androidnative1.MainActivity')
    a2 = SimSootValue_ThisRef.new_object(blank_state,
                                         'android.os.Bundle',
                                         symbolic=True)
    args = [SootArgument(arg, arg.type) for arg in [a1, a2]]
    entry = project.factory.entry_state(args=args)
    simgr = project.factory.simgr(entry)

    simgr.run()

    int_result = simgr.deadended[0].solver.eval(result)
    assert int_result == 221
Esempio n. 5
0
    def run(self, ptr_env, class_, ptr_method_name, ptr_method_sig):

        method_class = self.state.jni_references.lookup(class_)
        method_name = self._load_string_from_native_memory(ptr_method_name)
        method_sig = self._load_string_from_native_memory(ptr_method_sig)

        # derive parameter type from signature
        params, _ = ArchSoot.decode_method_signature(method_sig)

        # create method ID and return reference to it
        method_id = SootMethodDescriptor(method_class.name, method_name, params)
        return self.state.jni_references.create_new_reference(method_id)
Esempio n. 6
0
def resolve_method(state,
                   method_name,
                   class_name,
                   params=(),
                   ret_type=None,
                   include_superclasses=True,
                   init_class=True,
                   raise_exception_if_not_found=False):
    """
    Resolves the method based on the given characteristics (name, class and
    params) The method may be defined in one of the superclasses of the given
    class (TODO: support interfaces).

    :rtype: archinfo.arch_soot.SootMethodDescriptor
    """
    base_class = state.javavm_classloader.get_class(class_name)
    if include_superclasses:
        class_hierarchy = state.javavm_classloader.get_class_hierarchy(
            base_class)
    else:
        class_hierarchy = [base_class]
    # walk up in class hierarchy, until method is found
    for class_descriptor in class_hierarchy:
        java_binary = state.project.loader.main_object
        soot_method = java_binary.get_soot_method(method_name,
                                                  class_descriptor.name,
                                                  params,
                                                  none_if_missing=True)
        if soot_method is not None:
            # init the class
            if init_class:
                state.javavm_classloader.init_class(class_descriptor)
            return SootMethodDescriptor.from_soot_method(soot_method)

    # method could not be found
    # => we are executing code that is not loaded (typically library code)
    # => fallback: continue with infos available from the invocation, so we
    #              still can use SimProcedures
    if raise_exception_if_not_found:
        raise SootMethodNotLoadedException()
    else:
        return SootMethodDescriptor(class_name,
                                    method_name,
                                    params,
                                    ret_type=ret_type)
Esempio n. 7
0
def solve_given_numbers_angr(numbers):
    global fake_input_fd, fake_output_fd

    binary_path = os.path.join(self_dir, "bin/service.jar")
    jni_options = {'jni_libs': ['libnotfun.so']}
    project = angr.Project(binary_path, main_opts=jni_options)

    # hooks
    project.hook(
        SootMethodDescriptor(class_name="java.util.Random",
                             name="nextInt",
                             params=('int', )).address(), Random_nextInt())
    project.hook(
        SootMethodDescriptor(class_name="java.lang.Integer",
                             name="valueOf",
                             params=('int', )).address(), Dummy_valueOf())
    project.hook(
        SootMethodDescriptor(class_name="NotFun",
                             name="print",
                             params=('java.lang.Object', )).address(),
        Custom_Print())
    project.hook(
        SootMethodDescriptor(class_name="NotFun", name="getInt",
                             params=()).address(), Custom_getInt())

    # set entry point to the 'game' method
    game_method = [
        m for m in project.loader.main_object.classes['NotFun'].methods
        if m.name == "game"
    ][0]
    game_entry = SootMethodDescriptor.from_soot_method(game_method).address()
    entry = project.factory.blank_state(addr=game_entry)
    simgr = project.factory.simgr(entry)

    # Create a fake file with what it is going to be printed to the user (concrete)
    fake_output_fd = entry.posix.open(b"/fake/output", Flags.O_RDWR)
    ff = entry.posix.fd[fake_output_fd]
    tstr = b"".join([bytes(str(n), 'utf-8') + b"\n" for n in numbers])
    ff.write_data(tstr, len(tstr))
    ff.seek(0)

    # Create a fake file with what the user as to insert (symbolic)
    fake_input_fd = entry.posix.open(b"/fake/input", Flags.O_RDWR)
    ff = entry.posix.fd[fake_input_fd]
    solutions = [claripy.BVS("solution%d" % (i), 32) for i in range(3)]
    for s in solutions:
        ff.write_data(s, 4)
    ff.seek(0)

    print("=" * 10 + " SYMBOLIC EXECUTION STARTED")
    while (len(simgr.active) > 0):
        simgr.step()
        print("===== " + str(simgr))
        print("===== " + ",".join([
            str(a.addr)
            for a in simgr.active if type(a.addr) == SootAddressDescriptor
        ]))

        # If we reach block_idx 30, it means that we solved 1 round of the game --> we stash the state
        # If we reach the gameFail() method, it means that we failed --> we prune the state
        simgr.move(
            'active', 'stashed',
            lambda a: type(a.addr) == SootAddressDescriptor and a.addr.method
            == SootMethodDescriptor("NotFun", "game",
                                    ()) and a.addr.block_idx == 30)
        simgr.move(
            'active', 'pruned', lambda a: type(a.addr) == SootAddressDescriptor
            and a.addr.method == SootMethodDescriptor("NotFun", "gameFail",
                                                      ()))

    print("=" * 10 + " SYMBOLIC EXECUTION ENDED")
    assert len(simgr.stashed) == 1
    win_state = simgr.stashed[0]
    numeric_solutions = []
    for s in solutions:
        es = win_state.solver.eval_atmost(s, 2)
        assert len(es) == 1
        numeric_solutions.append(es[0])
    return numeric_solutions