Пример #1
0
    def activate(self, ctx):
        ida_auto.set_ida_state(ida_auto.st_Work)
        if self.with_labels:
            print('FakePDB/generate pdb (with function labels):')
        else:
            print('FakePDB/generate pdb:')

        dumper = DumpInfo()
        native = Native()

        #calculate locations
        idb_dir = os.path.dirname(ida_loader.get_path(
            ida_loader.PATH_TYPE_IDB))
        pe_filename_ext = ida_nalt.get_root_filename()
        pe_filename, _ = os.path.splitext(ida_nalt.get_root_filename())

        filepath_exe = ida_nalt.get_input_file_path()
        filepath_json = os.path.join(idb_dir, pe_filename_ext + ".json")
        filepath_pdb = os.path.join(idb_dir, pe_filename + ".pdb")

        #generate json
        print('    * generating JSON: %s' % filepath_json)
        dumper.dump_info(filepath_json)

        print('    * generating PDB: %s' % filepath_pdb)
        native.pdb_generate(filepath_exe, filepath_json, filepath_pdb,
                            self.with_labels)

        print('    * symserv EXE id: %s' % native.pe_timestamp(filepath_exe))
        print('    * symserv PDB id: %s' % native.pe_guidage(filepath_exe))
        print('    * done')

        ida_auto.set_ida_state(ida_auto.st_Ready)
        return 1
Пример #2
0
    def _dialog_accepted(self, dialog):
        project, database = dialog.get_result()
        self._plugin.core.project = project.name
        self._plugin.core.database = database.name

        # Save the current database
        self._plugin.core.save_netnode()
        input_path = ida_loader.get_path(ida_loader.PATH_TYPE_IDB)
        ida_loader.save_database(input_path, 0)

        # Create the packet that will hold the file
        packet = UpdateFile.Query(project.name, database.name)
        with open(input_path, "rb") as input_file:
            packet.content = input_file.read()

        # Create the upload progress dialog
        text = "Uploading database to server, please wait..."
        progress = QProgressDialog(text, "Cancel", 0, 1)
        progress.setCancelButton(None)  # Remove cancel button
        progress.setModal(True)  # Set as a modal dialog
        window_flags = progress.windowFlags()  # Disable close button
        progress.setWindowFlags(window_flags & ~Qt.WindowCloseButtonHint)
        progress.setWindowTitle("Save to server")
        icon_path = self._plugin.plugin_resource("upload.png")
        progress.setWindowIcon(QIcon(icon_path))

        # Send the packet to upload the file
        packet.upback = partial(self._on_progress, progress)
        d = self._plugin.network.send_packet(packet)
        d.add_callback(partial(self._file_uploaded, progress))
        d.add_errback(self._plugin.logger.exception)
        progress.show()
Пример #3
0
    def activate(self, ctx):
        # get active filename
        pe_filename_ext = ida_nalt.get_root_filename()
        if not pe_filename_ext:
            print('FakePDB/generate lib: file not loaded')
            return 1

        ida_auto.set_ida_state(ida_auto.st_Work)
        print('FakePDB/generate lib:')

        dumper = DumpInfo()
        native = Native()

        #calculate locations
        idb_dir = os.path.dirname(ida_loader.get_path(
            ida_loader.PATH_TYPE_IDB))

        pe_filename, _ = os.path.splitext(ida_nalt.get_root_filename())

        filepath_exe = ida_nalt.get_input_file_path()
        filepath_json = os.path.join(idb_dir, pe_filename_ext + ".json")
        filepath_lib = os.path.join(idb_dir, pe_filename + ".lib")

        #generate json
        print('    * generating JSON: %s' % filepath_json)
        dumper.dump_info(filepath_json)

        print('    * generating LIB: %s' % filepath_lib)
        native.coff_createlib(filepath_json, filepath_lib)

        print('    * done')

        ida_auto.set_ida_state(ida_auto.st_Ready)
        return 1
    def _dialog_accepted(self, dialog):
        repo, branch = dialog.get_result()
        self._plugin.core.repo = repo.name
        self._plugin.core.branch = branch.name

        # Save the current database
        self._plugin.core.save_netnode()
        inputPath = ida_loader.get_path(ida_loader.PATH_TYPE_IDB)
        ida_loader.save_database(inputPath, 0)

        # Create the packet that will hold the database
        packet = UploadDatabase.Query(repo.name, branch.name)
        with open(inputPath, 'rb') as inputFile:
            packet.content = inputFile.read()

        # Create the progress dialog
        text = "Uploading database to server, please wait..."
        progress = QProgressDialog(text, "Cancel", 0, len(packet.content))
        progress.setCancelButton(None)  # Remove cancel button
        progress.setModal(True)  # Set as a modal dialog
        windowFlags = progress.windowFlags()  # Disable close button
        progress.setWindowFlags(windowFlags & ~Qt.WindowCloseButtonHint)
        progress.setWindowTitle("Save to server")
        iconPath = self._plugin.resource('upload.png')
        progress.setWindowIcon(QIcon(iconPath))
        progress.show()

        # Send the packet to upload the file
        packet.upback = partial(self._on_progress, progress)
        d = self._plugin.network.send_packet(packet)
        d.add_callback(partial(self._database_uploaded, repo, branch))
        d.add_errback(logger.exception)
Пример #5
0
def GetIdbDir():
    """
    Get IDB directory

    This function returns directory path of the current IDB database
    """
    return os.path.dirname(ida_loader.get_path(ida_loader.PATH_TYPE_IDB)) + os.sep
Пример #6
0
    def upload_file(plugin, packet):
        # Save the current database
        plugin.core.save_netnode()
        input_path = ida_loader.get_path(ida_loader.PATH_TYPE_IDB)
        ida_loader.save_database(input_path, 0)

        with open(input_path, "rb") as input_file:
            packet.content = input_file.read()

        # Create the upload progress dialog
        text = "Uploading database to server, please wait..."
        progress = QProgressDialog(text, "Cancel", 0, 1)
        progress.setCancelButton(None)  # Remove cancel button
        progress.setModal(True)  # Set as a modal dialog
        window_flags = progress.windowFlags()  # Disable close button
        progress.setWindowFlags(window_flags & ~Qt.WindowCloseButtonHint)
        progress.setWindowTitle("Save to server")
        icon_path = plugin.plugin_resource("upload.png")
        progress.setWindowIcon(QIcon(icon_path))

        # Send the packet to upload the file
        packet.upback = partial(SaveActionHandler._on_progress, progress)
        d = plugin.network.send_packet(packet)
        if d:
            d.add_callback(
                partial(SaveActionHandler.file_uploaded, plugin, progress)
            )
            d.add_errback(plugin.logger.exception)
        progress.show()
    def _database_downloaded(self, branch, progress, reply):
        """
        Called when the file has been downloaded.

        :param branch: the branch
        :param progress: the progress dialog
        :param reply: the reply from the server
        """
        # Close the progress dialog
        self._on_progress(progress, 1, 1)

        # Get the absolute path of the file
        appPath = QCoreApplication.applicationFilePath()
        appName = QFileInfo(appPath).fileName()
        fileExt = 'i64' if '64' in appName else 'idb'
        fileName = '%s_%s.%s' % (branch.repo, branch.name, fileExt)
        filePath = local_resource('files', fileName)

        # Write the packet content to disk
        with open(filePath, 'wb') as outputFile:
            outputFile.write(reply.content)
        logger.info("Saved file %s" % fileName)

        # Save the old database
        database = ida_loader.get_path(ida_loader.PATH_TYPE_IDB)
        if database:
            ida_loader.save_database(database, ida_loader.DBFL_KILL)
        # Save the current state
        self._plugin.core.save_state(True, database)
        # Open the new database
        QProcess.startDetached(qApp.applicationFilePath(), [filePath])
        qApp.quit()  # https://forum.hex-rays.com/viewtopic.php?f=8&t=4294
Пример #8
0
    def activate(self, ctx):
        ida_auto.set_ida_state(ida_auto.st_Work)
        if self.with_labels:
            print('FakePDB/generate pdb (with function labels):')
        else:
            print('FakePDB/generate pdb:')

        dumper = InformationDumper()
        generator = PdbGenerator()

        #get exe location
        filepath_ida = ida_loader.get_path(ida_loader.PATH_TYPE_IDB)
        pre, _ = os.path.splitext(filepath_ida)
        pre, _ = os.path.splitext(pre)
        filepath_exe = pre + ".exe"
        filepath_json = pre + ".exe.json"
        filepath_pdb = pre + ".pdb"

        #generate json
        print('    * generating JSON: %s' % filepath_json)
        dumper.dump_info(filepath_json)

        print('    * generating PDB: %s' % filepath_pdb)
        generator.generate(filepath_exe, filepath_json, filepath_pdb,
                           self.with_labels)

        print('    * symserv EXE id: %s' %
              generator.get_symserv_exe(filepath_exe))
        print('    * symserv PDB id: %s' %
              generator.get_symserv_pdb(filepath_exe))
        print('    * done')

        ida_auto.set_ida_state(ida_auto.st_Ready)
        return 1
Пример #9
0
def main():
    # ida_hexrays.init_hexrays_plugin()
    ret = []
    for func in idautils.Functions():
        usr = get_user_define(func)
        if usr:
            ret.append((func, usr))
    with open(
            os.path.splitext(ida_loader.get_path(
                ida_loader.PATH_TYPE_ID0))[0].decode("utf-8") + "_.dmp",
            "wb") as f:
        pickle.dump(ret, f, pickle.HIGHEST_PROTOCOL)
Пример #10
0
    def activate(self, ctx):

        filepath = ida_loader.get_path(ida_loader.PATH_TYPE_IDB)
        pre, _ = os.path.splitext(filepath)
        filepath = pre + ".json"

        dumper = InformationDumper()
        print('FakePDB/dumpinfo:')
        ida_auto.set_ida_state(ida_auto.st_Work)
        dumper.dump_info(filepath)
        ida_auto.set_ida_state(ida_auto.st_Ready)
        print('   * done')
        return 1
Пример #11
0
def main():
    filepath = ida_loader.get_path(ida_loader.PATH_TYPE_IDB)
    pre, ext = os.path.splitext(filepath)
    filepath = pre + ".exe.json"

    output = {
        'segments': processSegments(),
        'functions': processFunctions(),
        'names': processNames()
    }

    with open(filepath, "w") as f:
        json.dump(output, f, indent=4)
Пример #12
0
    def activate(self, ctx):
        # get active filename
        pe_filename_ext = ida_nalt.get_root_filename()
        if not pe_filename_ext:
            print('FakePDB/dumpinfo: file not loaded')
            return 1

        #calculate locations
        idb_dir = os.path.dirname(ida_loader.get_path(
            ida_loader.PATH_TYPE_IDB))

        filepath_json = os.path.join(idb_dir, pe_filename_ext + ".json")

        dumper = DumpInfo()
        print('FakePDB/dumpinfo:')
        ida_auto.set_ida_state(ida_auto.st_Work)
        dumper.dump_info(filepath_json)
        ida_auto.set_ida_state(ida_auto.st_Ready)
        print('   * done')
        return 1
Пример #13
0
 def update(self, ctx):
     if not ida_loader.get_path(ida_loader.PATH_TYPE_IDB):
         return ida_kernwin.AST_DISABLE
     if not ida_auto.auto_is_ok():
         return ida_kernwin.AST_DISABLE
     return super(SaveActionHandler, self).update(ctx)
Пример #14
0
    def activate(self, context):
        """
            Called when the action has been clicked by the user
        """
        idb_path = ida_loader.get_path(ida_loader.PATH_TYPE_IDB)
        ida_loader.save_database(idb_path, 0)

        with open(idb_path, 'rb') as input_file:
            idb_data = input_file.read()

        # Create the progress bar
        progress_bar = QProgressDialog(
            'Uploading your idb to the server, please wait...',
            'Cancel',
            0,
            1
        )
        progress_bar.setCancelButton(None)  # Remove the cancel button so the user won't be able to cancel
        progress_bar.setModal(True)  # Set this as a modal dialog
        window_flags = progress_bar.windowFlags()  # Disable close button
        progress_bar.setWindowFlags(window_flags & ~Qt.WindowCloseButtonHint)
        progress_bar.setWindowTitle('Upload to server')

        idb_data_len = len(idb_data)
        idb_data_stream = io.BytesIO(idb_data)
        idb_hash = hashlib.sha1(idb_data).digest()
        total_packets = (idb_data_len / self.CHUNK_SIZE)
        total_packets = total_packets + 1 if idb_data_len % self.CHUNK_SIZE != 0 else total_packets  # correction
        # Build the initiation packet here
        self._plugin.network_manager.send_request(
            RequestType.UPLOAD_IDB_START,
            idb_name=Unicoder.decode(os.path.split(idb_path)[-1]),
            idb_hash=idb_hash,
            idb_size=idb_data_len,
        )

        self._plugin.logger.debug('starting to send packets')
        self._plugin.logger.debug('The amount of packets needed to be sent: {}'.format(total_packets))

        def _on_error(progress_bar):
            progress_bar.close()
            success = QMessageBox()
            success.setIcon(QMessageBox.Critical)
            success.setStandardButtons(QMessageBox.Ok)
            success.setText("Could not upload IDB")
            success.setWindowTitle("Upload to server FAILED")
            success.exec_()

        for i in range(total_packets):
            current_pkt_data = idb_data_stream.read(self.CHUNK_SIZE)

            self._plugin.network_manager.send_request(
                RequestType.IDB_CHUNK,
                callback=partial(self._update_progress, progress_bar, i, total_packets),
                err_callback=partial(_on_error, progress_bar),
                data=current_pkt_data,
            )

        self._plugin.logger.debug('finished sending packets')
        self._plugin.logger.debug('sending upload_end')

        def _close_window(progress_bar):
            SaveMenuActionHandler._update_progress(progress_bar, 100, 100)
            progress_bar.close()
            success = QMessageBox()
            success.setIcon(QMessageBox.Information)
            success.setStandardButtons(QMessageBox.Ok)
            success.setText("IDB successfully uploaded!")
            success.setWindowTitle("Upload to server")
            success.exec_()

        self._plugin.network_manager.send_request(
            RequestType.IDB_END,
            callback=partial(_close_window, progress_bar)
        )

        progress_bar.show()
Пример #15
0
    def _file_downloaded(self, database, progress, reply):
        """Called when the file has been downloaded."""
        progress.close()

        # Get the absolute path of the file
        app_path = QCoreApplication.applicationFilePath()
        app_name = QFileInfo(app_path).fileName()
        file_ext = "i64" if "64" in app_name else "idb"
        file_name = "%s_%s.%s" % (database.project, database.name, file_ext)
        file_path = self._plugin.user_resource("files", file_name)

        # Write the file to disk
        with open(file_path, "wb") as output_file:
            output_file.write(reply.content)
        self._plugin.logger.info("Saved file %s" % file_name)

        # Save the old database
        database = ida_loader.get_path(ida_loader.PATH_TYPE_IDB)
        if database:
            ida_loader.save_database(database, ida_loader.DBFL_TEMP)

        # This is a very ugly hack used to open a database into IDA. We don't
        # have any function for this in the SDK, so I sorta hijacked the
        # snapshot functionality in this effect.

        # Get the library to call functions not present in the bindings
        idaname = "ida64" if "64" in app_name else "ida"
        if sys.platform == "win32":
            dllname, dlltype = idaname + ".dll", ctypes.windll
        elif sys.platform == "linux2":
            dllname, dlltype = "lib" + idaname + ".so", ctypes.cdll
        elif sys.platform == "darwin":
            dllname, dlltype = "lib" + idaname + ".dylib", ctypes.cdll
        dllpath = ida_diskio.idadir(None)
        if not os.path.exists(os.path.join(dllpath, dllname)):
            dllpath = dllpath.replace("ida64", "ida")
        dll = dlltype[os.path.join(dllpath, dllname)]

        # Close the old database using the term_database library function
        old_path = ida_loader.get_path(ida_loader.PATH_TYPE_IDB)
        if old_path:
            dll.term_database()

        # Open the new database using the init_database library function
        # This call only won't be enough because the user interface won't
        # be initialized, this is why the snapshot functionality is used for
        args = [app_name, file_path]
        argc = len(args)
        argv = (ctypes.POINTER(ctypes.c_char) * (argc + 1))()
        for i, arg in enumerate(args):
            arg = arg.encode("utf-8")
            argv[i] = ctypes.create_string_buffer(arg)

        v = ctypes.c_int(0)
        av = ctypes.addressof(v)
        pv = ctypes.cast(av, ctypes.POINTER(ctypes.c_int))
        dll.init_database(argc, argv, pv)

        # Create a temporary copy of the new database because we cannot use
        # the snapshot functionality to restore the currently opened database
        file_ext = ".i64" if "64" in app_name else ".idb"
        tmp_file, tmp_path = tempfile.mkstemp(suffix=file_ext)
        shutil.copyfile(file_path, tmp_path)

        # This hook is used to delete the temporary database when all done
        class UIHooks(ida_kernwin.UI_Hooks):
            def database_inited(self, is_new_database, idc_script):
                self.unhook()

                os.close(tmp_file)
                if os.path.exists(tmp_path):
                    os.remove(tmp_path)

        hooks = UIHooks()
        hooks.hook()

        # Call the restore_database_snapshot library function
        # This will initialize the user interface, completing the process
        s = ida_loader.snapshot_t()
        s.filename = tmp_path  # Use the temporary database
        ida_kernwin.restore_database_snapshot(s, None, None)
Пример #16
0
  once that is done, produce a .c file containing the
  decompilation of all the functions in that file.

  Run like so:

        ida -A "-S...path/to/produce_c_file.py" <binary-file>

  where:

    * -A instructs IDA to run in non-interactive mode
    * -S holds a path to the script to run (note this is a single token;
         there is no space between '-S' and its path.)
"""

import ida_pro
import ida_auto
import ida_loader
import ida_hexrays

# derive output file name
idb_path = ida_loader.get_path(ida_loader.PATH_TYPE_IDB)
c_path = "%s.c" % idb_path

ida_auto.auto_wait()  # wait for end of auto-analysis
ida_hexrays.decompile_many(  # generate .c file
    c_path, None, ida_hexrays.VDRUN_NEWFILE
    | ida_hexrays.VDRUN_SILENT
    | ida_hexrays.VDRUN_MAYSTOP)

ida_pro.qexit(0)
Пример #17
0
    def _database_downloaded(self, branch, progress, reply):
        """
        Called when the file has been downloaded.

        :param branch: the branch
        :param progress: the progress dialog
        :param reply: the reply from the server
        """
        # Close the progress dialog
        progress.close()

        # Get the absolute path of the file
        appPath = QCoreApplication.applicationFilePath()
        appName = QFileInfo(appPath).fileName()
        fileExt = 'i64' if '64' in appName else 'idb'
        fileName = '%s_%s.%s' % (branch.repo, branch.name, fileExt)
        filePath = local_resource('files', fileName)

        # Write the packet content to disk
        with open(filePath, 'wb') as outputFile:
            outputFile.write(reply.content)
        logger.info("Saved file %s" % fileName)

        # Save the old database
        database = ida_loader.get_path(ida_loader.PATH_TYPE_IDB)
        if database:
            ida_loader.save_database(database, ida_loader.DBFL_TEMP)

        # Get the dynamic library
        idaname = 'ida64' if '64' in appName else 'ida'
        if sys.platform == 'win32':
            dllname, dlltype = idaname + '.dll', ctypes.windll
        elif sys.platform == 'linux2':
            dllname, dlltype = 'lib' + idaname + '.so', ctypes.cdll
        elif sys.platform == 'darwin':
            dllname, dlltype = 'lib' + idaname + '.dylib', ctypes.cdll
        dllpath = ida_diskio.idadir(None)
        if not os.path.exists(os.path.join(dllpath, dllname)):
            dllpath = dllpath.replace('ida64', 'ida')
        dll = dlltype[os.path.join(dllpath, dllname)]

        # Close the old database
        oldPath = ida_loader.get_path(ida_loader.PATH_TYPE_IDB)
        if oldPath:
            dll.term_database()

        # Open the new database
        LP_c_char = ctypes.POINTER(ctypes.c_char)

        args = [appName, filePath]
        argc = len(args)
        argv = (LP_c_char * (argc + 1))()
        for i, arg in enumerate(args):
            arg = arg.encode('utf-8')
            argv[i] = ctypes.create_string_buffer(arg)

        LP_c_int = ctypes.POINTER(ctypes.c_int)
        v = ctypes.c_int(0)
        av = ctypes.addressof(v)
        pv = ctypes.cast(av, LP_c_int)
        dll.init_database(argc, argv, pv)

        # Create a copy of the new database
        fileExt = '.i64' if '64' in appName else '.idb'
        tmpFile, tmpPath = tempfile.mkstemp(suffix=fileExt)
        shutil.copyfile(filePath, tmpPath)

        class UIHooks(ida_kernwin.UI_Hooks):
            def database_inited(self, is_new_database, idc_script):
                self.unhook()

                # Remove the tmp database
                os.close(tmpFile)
                if os.path.exists(tmpPath):
                    os.remove(tmpPath)

        hooks = UIHooks()
        hooks.hook()

        # Open the tmp database
        s = ida_loader.snapshot_t()
        s.filename = tmpPath
        ida_kernwin.restore_database_snapshot(s, None, None)