当前位置: 首页>>代码示例>>Python>>正文


Python TimeoutServer.get_results方法代码示例

本文整理汇总了Python中lib.cuckoo.common.utils.TimeoutServer.get_results方法的典型用法代码示例。如果您正苦于以下问题:Python TimeoutServer.get_results方法的具体用法?Python TimeoutServer.get_results怎么用?Python TimeoutServer.get_results使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在lib.cuckoo.common.utils.TimeoutServer的用法示例。


在下文中一共展示了TimeoutServer.get_results方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from lib.cuckoo.common.utils import TimeoutServer [as 别名]
# 或者: from lib.cuckoo.common.utils.TimeoutServer import get_results [as 别名]

#.........这里部分代码省略.........
            self.upload_analyzer()
            # Give the analysis options to the guest, so it can generate the
            # analysis.conf inside the guest.
            self.server.add_config(options)

            # If the target of the analysis is a file, upload it to the guest.
            if options["category"] == "file":
                try:
                    file_data = open(options["target"], "rb").read()
                except (IOError, OSError) as e:
                    raise CuckooGuestError("Unable to read %s, error: %s"
                                           % (options["target"], e))
                
                data = xmlrpclib.Binary(file_data)
                self.server.add_malware(data, options["file_name"])

            # Launch the analyzer.
            pid = self.server.execute()
            log.debug("%s: analyzer started with PID %d" % (self.id, pid))
        # If something goes wrong when establishing the connection, raise an
        # exception and abort the analysis.
        except (socket.timeout, socket.error):
            raise CuckooGuestError("%s: guest communication timeout, check "
                                   "networking or try to increase timeout"
                                   % self.id)

    def wait_for_completion(self):
        """Wait for analysis completion.
        @return: operation status.
        """
        log.debug("%s: waiting for completion" % self.id)

        # Same procedure as in self.wait(). Just look at the comments there.
        abort = Event()
        abort.clear()
        def die():
            abort.set()

        timer = Timer(self.timeout, die)
        timer.start()
        self.server._set_timeout(self.timeout)

        while True:
            time.sleep(1)

            # If the analysis hits the critical timeout, just return straight
            # straight away and try to recover the analysis results from the
            # guest.
            if abort.is_set():
                raise CuckooGuestError("The analysis hit the critical timeout,"
                                       " terminating")

            try:
                status = self.server.get_status()
            except Exception as e:
                log.debug("%s: error retrieving status: %s" % (self.id, e))
                continue

            # React according to the returned status.
            if status == CUCKOO_GUEST_COMPLETED:
                log.info("%s: analysis completed successfully" % self.id)
                break
            elif status == CUCKOO_GUEST_FAILED:
                raise CuckooGuestError("Analysis failed: %s"
                                        % self.server.get_error())
            else:
                log.debug("%s: analysis not completed yet (status=%s)"
                          % (self.id, status))

        self.server._set_timeout(None)

    def save_results(self, folder):
        """Save analysis results.
        @param folder: analysis folder path.
        @return: operation status.
        """
        # Download results from the guest.
        try:
            data = self.server.get_results()
        except Exception as e:
            raise CuckooGuestError("Failed to retrieve analysis results: %s"
                                   % e)

        # Write the retrieved binary data to a in-memory zip archive.
        zip_data = StringIO()
        zip_data.write(data)

        archive = ZipFile(zip_data, "r")
        if not os.path.exists(folder):
            try:
                os.mkdir(folder)
            except (IOError, OSError) as e:
                raise CuckooGuestError("Failed to store analysis results: %s"
                                       % e)

        # Extract the generate zip archive to the specified folder, which is
        # going to be somewhere like storage/analysis/<task id>/.
        log.debug("Extracting results to %s" % folder)
        archive.extractall(folder)
        archive.close()
开发者ID:Missuniverse110,项目名称:cuckoo,代码行数:104,代码来源:guest.py


注:本文中的lib.cuckoo.common.utils.TimeoutServer.get_results方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。