예제 #1
0
    def __init__(self, toolmark: str):

        if not isinstance(toolmark, str) or toolmark == "":
            raise Exception("Zgrab2 scanner toolmark is invalid")

        self._logger: MsLogger = MsLogManager.get_logger(type(self).__name__)

        self._toolmark: str = toolmark

        self._tmpdir: str = os.path.abspath(tmpdir)
        if not isinstance(self._tmpdir, str):
            self._tmpdir = os.path.abspath("./_clienttmpdir")
        self._tmpdir = os.path.abspath(
            os.path.join(self._tmpdir, self._toolmark))
        if os.path.isdir(self._tmpdir):
            helper_dir.remove_dirs(self._tmpdir)

        os.makedirs(self._tmpdir)
예제 #2
0
    def _convert(self, data: InputData) -> iter:
        """将中心下发的任务转换为自有的通用任务结构Task体枚举(一个文件可能有多个任务段)"""
        try:
            if data.stream is None or not data.stream.readable():
                self._logger.error(
                    "Data stream is None when trying to convert to standard Task: %s"
                    % data._source)
                return

            todir = os.path.join(self._tmpdir,
                                 data.name.strip(data.extension).strip('.'))
            while os.path.exists(todir):
                todir = helper_file.rename_file_by_number_tail(todir)

            with data._load_stream(mode='rb', enc=None) as fs:
                # 解压
                with zipfile.ZipFile(fs, 'r') as f:
                    for zfile in f.namelist():
                        f.extract(zfile, todir)

                # 处理task
                for pp in os.listdir(todir):
                    bfname, bfext = os.path.splitext(pp)
                    bfpath = os.path.join(todir, pp)
                    # 不是bcp的删掉
                    if not bfext.endswith("bcp"):
                        os.remove(bfpath)
                        continue
                    bffi = os.path.join(todir, pp)
                    # 一个bcp里有多行,每行都是一个task
                    for task in self._bcp_deal(bffi, data):
                        if task is None:
                            continue
                        yield task

            # 删除临时文件
            helper_dir.remove_dirs(todir)
            #

        except Exception:
            self._logger.error("Convert data to Task error:\ndata:%s\nex:%s" %
                               (data._source, traceback.format_exc()))
            if not data is None:
                data.on_complete(False)
예제 #3
0
    def __init__(self, toolmark: str):
        ScanPlugBase.__init__(self)
        if not isinstance(toolmark, str) or toolmark == "":
            raise Exception("invalid toolmark for scantool initialize")
        self._toolmark = toolmark

        if not ScanToolBase._dir_lockers.__contains__(toolmark):
            ScanToolBase._dir_lockers[toolmark] = threading.RLock()

        self._tmpdir: str = os.path.abspath(tmpdir)

        with ScanToolBase._dir_lockers[toolmark]:
            if not isinstance(self._tmpdir, str):
                self._tmpdir = os.path.abspath("./_clienttmpdir")
            self._tmpdir = os.path.abspath(
                os.path.join(self._tmpdir, self._toolmark))
            if os.path.isdir(self._tmpdir):
                helper_dir.remove_dirs(self._tmpdir)

            os.makedirs(self._tmpdir)
예제 #4
0
 def _check_exists_tmp_folder(self):
     """检查临时文件夹,如果有已存在的东西,需要处理掉"""
     # 先暂定为删除
     helper_dir.remove_dirs(self._tmpdir)