Beispiel #1
0
def load_file(ctx):
    if not os.path.exists(ctx.filename):
        error("file {ctx.filename} doesn't exist".format(ctx=ctx))
        if ctx.interactive:
            return False
        die()

    if not os.path.isfile(ctx.filename):
        error("this is not a file".format(ctx=ctx))
        if ctx.interactive:
            return False
        die()

    dirname = os.path.dirname(ctx.filename)
    db_path = dirname + "/" if dirname != "" else ""
    db_path += "." + os.path.basename(ctx.filename) + ".db"
    db_exists = os.path.exists(db_path)
    ctx.db_path = db_path

    try:
        dis = Disassembler(ctx.filename,
                           ctx.raw_type,
                           ctx.raw_base,
                           ctx.raw_big_endian,
                           load_symbols=not db_exists)
    except ExcArch as e:
        error("arch %s is not supported" % e.arch)
        if ctx.interactive:
            return False
        die()
    except ExcFileFormat:
        error("the file is not PE or ELF binary")
        if ctx.interactive:
            return False
        die()
    except ExcPEFail as e:
        error(str(e.e))
        error("It seems that pefile.parse_data_directories is bugged.")
        error("Maybe you should Retry")
        if ctx.interactive:
            return False
        die()

    # Load symbols in the database
    if db_exists:
        info("open database %s" % db_path)
        fd = open(db_path, "r")
        db = json.loads(fd.read())
        ctx.db = db
        sym = dis.binary.symbols
        rev_sym = dis.binary.reverse_symbols
        for name, addr in db["symbols"].items():
            sym[name] = addr
            rev_sym[addr] = name
        fd.close()

    ctx.dis = dis
    ctx.libarch = dis.load_arch_module()

    return True
Beispiel #2
0
def load_file(ctx):
    if not os.path.exists(ctx.filename):
        error("file {ctx.filename} doesn't exist".format(ctx=ctx))
        if ctx.interactive:
           return False
        die()

    if not os.path.isfile(ctx.filename):
        error("this is not a file".format(ctx=ctx))
        if ctx.interactive:
           return False
        die()

    dirname = os.path.dirname(ctx.filename)
    db_path = dirname + "/" if dirname != "" else ""
    db_path +=  "." + os.path.basename(ctx.filename) + ".db"
    db_exists = os.path.exists(db_path)
    ctx.db_path = db_path

    try:
        dis = Disassembler(ctx.filename, ctx.raw_type,
                           ctx.raw_base, ctx.raw_big_endian,
                           load_symbols=not db_exists)
    except ExcArch as e:
        error("arch %s is not supported" % e.arch)
        if ctx.interactive:
            return False
        die()
    except ExcFileFormat:
        error("the file is not PE or ELF binary")
        if ctx.interactive:
            return False
        die()
    except ExcPEFail as e:
        error(str(e.e))
        error("It seems that pefile.parse_data_directories is bugged.")
        error("Maybe you should Retry")
        if ctx.interactive:
            return False
        die()

    # Load symbols in the database
    if db_exists:
        info("open database %s" % db_path)
        fd = open(db_path, "r")
        db = json.loads(fd.read())
        ctx.db = db
        sym = dis.binary.symbols
        rev_sym = dis.binary.reverse_symbols
        for name, addr in db["symbols"].items():
            sym[name] = addr
            rev_sym[addr] = name
        fd.close()

    ctx.dis = dis
    ctx.libarch = dis.load_arch_module()

    return True
Beispiel #3
0
    def load_file(self, filename=None):
        if filename is None:
            filename = self.filename

        if not os.path.exists(filename):
            error("file {self.filename} doesn't exist".format(self=self))
            if self.interactive_mode:
               return False
            die()

        if not os.path.isfile(filename):
            error("this is not a file".format(self=self))
            if self.interactive_mode:
               return False
            die()

        self.db = Database()
        self.db.load(filename)

        try:
            dis = Disassembler(filename, self.raw_type,
                               self.raw_base, self.raw_big_endian,
                               self.db)
        except ExcArch as e:
            error("arch %s is not supported" % e.arch)
            if self.interactive_mode:
                return False
            die()
        except ExcFileFormat:
            error("the file is not PE or ELF binary")
            if self.interactive_mode:
                return False
            die()
        except ExcPEFail as e:
            error(str(e.e))
            error("it seems that there is a random bug in pefile, you shoul retry.")
            error("please report here https://github.com/joelpx/reverse/issues/16")
            if self.interactive_mode:
                return False
            die()

        self.dis = dis
        self.libarch = dis.load_arch_module()

        return True
Beispiel #4
0
def load_file(ctx):
    if not os.path.exists(ctx.filename):
        error("file {ctx.filename} doesn't exists".format(ctx=ctx))
        if ctx.interactive:
           return False
        die()

    if not os.path.isfile(ctx.filename):
        error("this is not a file".format(ctx=ctx))
        if ctx.interactive:
           return False
        die()

    try:
        dis = Disassembler(ctx.filename, ctx.raw_type,
                           ctx.raw_base, ctx.raw_big_endian)
    except ExcArch as e:
        error("arch %s is not supported" % e.arch)
        if ctx.interactive:
            return False
        die()
    except ExcFileFormat:
        error("the file is not PE or ELF binary")
        if ctx.interactive:
            return False
        die()
    except ExcPEFail as e:
        error(str(e.e))
        error("It seems that pefile.parse_data_directories is bugged.")
        error("Maybe you should Retry")
        if ctx.interactive:
            return False
        die()

    ctx.dis = dis
    ctx.libarch = dis.load_arch_module()

    if ctx.symfile:
        dis.load_user_sym_file(ctx.symfile)

    return True
Beispiel #5
0
def load_file(ctx):
    if not os.path.exists(ctx.filename):
        error("file {ctx.filename} doesn't exists".format(ctx=ctx))
        if ctx.interactive:
           return False
        die()

    if not os.path.isfile(ctx.filename):
        error("this is not a file".format(ctx=ctx))
        if ctx.interactive:
           return False
        die()

    try:
        dis = Disassembler(ctx.filename, ctx.raw_type,
                           ctx.raw_base, ctx.raw_big_endian)
    except ExcArch as e:
        error("arch %s is not supported" % e.arch)
        if ctx.interactive:
            return False
        die()
    except ExcFileFormat:
        error("the file is not PE or ELF binary")
        if ctx.interactive:
            return False
        die()
    except ExcPEFail as e:
        error(str(e.e))
        error("It seems that pefile.parse_data_directories is bugged.")
        error("Maybe you should Retry")
        if ctx.interactive:
            return False
        die()

    ctx.dis = dis
    ctx.libarch = dis.load_arch_module()

    if ctx.symfile:
        dis.load_user_sym_file(ctx.symfile)

    return True
Beispiel #6
0
def load_file(ctx):
    if not os.path.exists(ctx.filename):
        error("file {ctx.filename} doesn't exist".format(ctx=ctx))
        if ctx.interactive:
           return False
        die()

    if not os.path.isfile(ctx.filename):
        error("this is not a file".format(ctx=ctx))
        if ctx.interactive:
           return False
        die()

    dirname = os.path.dirname(ctx.filename)
    db_path = dirname + "/" if dirname != "" else ""
    db_path +=  "." + os.path.basename(ctx.filename) + ".db"
    db_exists = os.path.exists(db_path)
    ctx.db_path = db_path

    jmptables = {}
    inline_comments = {}
    previous_comments = {}
    sym = {}
    rev_sym = {}
    mips_gp = -1

    # Open the database
    if db_exists:
        info("open database %s" % db_path)
        fd = open(db_path, "r")
        db = json.loads(fd.read())
        ctx.db = db

        # Saved symbols
        sym = db["symbols"]
        for name, addr in db["symbols"].items():
            rev_sym[addr] = name

        try:
            # Saved comments
            for ad, comm in db["inline_comments"].items():
                inline_comments[int(ad)] = comm
            for ad, comm in db["previous_comments"].items():
                previous_comments[int(ad)] = comm

            # Saved jmptables
            for j in db["jmptables"]:
                jmptables[j["inst_addr"]] = \
                    Jmptable(j["inst_addr"], j["table_addr"], j["table"], j["name"])
        except:
            # Not available in previous versions, this try will be
            # removed in the future
            pass

        try:
            mips_gp = db["mips_gp"]
        except:
            # Not available in previous versions, this try will be
            # removed in the future
            pass

        fd.close()

    try:
        dis = Disassembler(ctx.filename, ctx.raw_type,
                           ctx.raw_base, ctx.raw_big_endian,
                           sym, rev_sym,
                           jmptables, inline_comments,
                           previous_comments,
                           load_symbols=not db_exists,
                           mips_gp=mips_gp)
    except ExcArch as e:
        error("arch %s is not supported" % e.arch)
        if ctx.interactive:
            return False
        die()
    except ExcFileFormat:
        error("the file is not PE or ELF binary")
        if ctx.interactive:
            return False
        die()
    except ExcPEFail as e:
        error(str(e.e))
        error("it seems that there is a random bug in pefile, you shoul retry.")
        error("please report here https://github.com/joelpx/reverse/issues/16")
        if ctx.interactive:
            return False
        die()

    ctx.dis = dis
    ctx.libarch = dis.load_arch_module()

    return True
Beispiel #7
0
def load_file(ctx):
    if not os.path.exists(ctx.filename):
        error("file {ctx.filename} doesn't exist".format(ctx=ctx))
        if ctx.interactive:
            return False
        die()

    if not os.path.isfile(ctx.filename):
        error("this is not a file".format(ctx=ctx))
        if ctx.interactive:
            return False
        die()

    dirname = os.path.dirname(ctx.filename)
    db_path = dirname + "/" if dirname != "" else ""
    db_path += "." + os.path.basename(ctx.filename) + ".db"
    db_exists = os.path.exists(db_path)
    ctx.db_path = db_path

    jmptables = {}
    inline_comments = {}
    previous_comments = {}
    sym = {}
    rev_sym = {}
    mips_gp = -1

    # Open the database
    if db_exists:
        info("open database %s" % db_path)
        fd = open(db_path, "r")
        db = json.loads(fd.read())
        ctx.db = db

        # Saved symbols
        sym = db["symbols"]
        for name, addr in db["symbols"].items():
            rev_sym[addr] = name

        try:
            # Saved comments
            for ad, comm in db["inline_comments"].items():
                inline_comments[int(ad)] = comm
            for ad, comm in db["previous_comments"].items():
                previous_comments[int(ad)] = comm

            # Saved jmptables
            for j in db["jmptables"]:
                jmptables[j["inst_addr"]] = \
                    Jmptable(j["inst_addr"], j["table_addr"], j["table"], j["name"])
        except:
            # Not available in previous versions, this try will be
            # removed in the future
            pass

        try:
            mips_gp = db["mips_gp"]
        except:
            # Not available in previous versions, this try will be
            # removed in the future
            pass

        fd.close()

    try:
        dis = Disassembler(ctx.filename,
                           ctx.raw_type,
                           ctx.raw_base,
                           ctx.raw_big_endian,
                           sym,
                           rev_sym,
                           jmptables,
                           inline_comments,
                           previous_comments,
                           load_symbols=not db_exists,
                           mips_gp=mips_gp)
    except ExcArch as e:
        error("arch %s is not supported" % e.arch)
        if ctx.interactive:
            return False
        die()
    except ExcFileFormat:
        error("the file is not PE or ELF binary")
        if ctx.interactive:
            return False
        die()
    except ExcPEFail as e:
        error(str(e.e))
        error(
            "it seems that there is a random bug in pefile, you shoul retry.")
        error("please report here https://github.com/joelpx/reverse/issues/16")
        if ctx.interactive:
            return False
        die()

    ctx.dis = dis
    ctx.libarch = dis.load_arch_module()

    return True