PolySwarmPolySwarmPolySwarmPolySwarm
Help

Building a File-Submitting Ambassador

polyswarm-client's filesystem.py ambassador expands on the eicar.py ambassador, submitting artifacts from a local filesystem.

它以类似的方式开始:

import logging
import random
import os

from concurrent.futures import CancelledError

from polyswarmartifact import ArtifactType

from polyswarmclient.abstractambassador import AbstractAmbassador
from polyswarmclient.corpus import DownloadToFileSystemCorpus

logger = logging.getLogger(__name__)

ARTIFACT_DIRECTORY = os.getenv('ARTIFACT_DIRECTORY', 'docker/artifacts')
ARTIFACT_BLACKLIST = os.getenv('ARTIFACT_BLACKLIST', 'truth.db').split(',')
BOUNTY_TEST_DURATION_BLOCKS = int(os.getenv('BOUNTY_TEST_DURATION_BLOCKS', 5))

同样,将处理导入,对悬赏时间进行硬编码,并配置日志记录。 filesystem.py makes use of polyswarmclient.corpus, a helper class that will download, decrypt and extract an artifact collection. Swarm Technologies uses this class internally during continuous integration to ensure that malicious artifacts are detected as such by microengines.

继续:

class Ambassador(AbstractAmbassador):
    """Ambassador which submits artifacts from a directory"""

    def __init__(self, client, testing=0, chains=None, watchdog=0, submission_rate=30):
        """Initialize a filesystem ambassador
        Args:
            client (`Client`): Client to use
            testing (int): How many test bounties to respond to
            chains (set[str]): Chain(s) to operate on
        """
        super().__init__(client, testing, chains, watchdog, submission_rate)

filesystem.pyAbstractAmbassador 类再使用几个有助于测试的参数:

  • testing:当非零时,此参数指定代表在退出前将生成的最大悬赏数。
  • watchdog:块间隔。 将对照每个新区块对该代表放置的悬赏进行检查,以确保悬赏已成功地放在链上。
  • submission_rate:如为非零,则会在主事件循环中生成睡眠,以防止代表在测试期间使 polyswarmd 超负荷。

继续:

        self.artifacts = []
        u = os.getenv("MALICIOUS_BOOTSTRAP_URL")
        if u:
            logger.info("Unpacking malware corpus at {0}".format(u))
            d = DownloadToFileSystemCorpus()
            d.download_and_unpack()
            bfl = d.get_benign_file_list()
            mfl = d.get_malicious_file_list()
            logger.info("Unpacking complete, {0} malicious and {1} benign files".format(len(mfl), len(bfl)))
            self.artifacts = bfl + mfl
        else:
            for root, dirs, files in os.walk(ARTIFACT_DIRECTORY):
                for f in files:
                    self.artifacts.append(os.path.join(root, f))

如果设置了环境变量 MALICIOUS_BOOTSTRAP_URL,代表将从测试存储库中下载工件。 如果未设置,将相对于代表当前的工作目录对 ARTIFACT_DIRECTORY 目录进行遍历。 收集文件,准备悬赏生成。

filesystem.py 将覆盖 generate_bounties 方法:

    async def generate_bounties(self, chain):
        """Submit bounty from the filesystem
        Args:
            chain (str): Chain sample is being requested from
        """
        amount = await self.client.bounties.parameters[chain].get('bounty_amount_minimum')

        while True:
            try:
                filename = random.choice(self.artifacts)

                logger.info('Submitting file %s', filename)
                ipfs_uri = await self.client.post_artifacts([(filename, None)])
                if not ipfs_uri:
                    logger.error('Error uploading artifact to IPFS, continuing')
                    continue

                await self.push_bounty(ArtifactType.FILE, amount, ipfs_uri, BOUNTY_TEST_DURATION_BLOCKS, chain)
            except CancelledError:
                logger.warning('Cancel requested')
                break
            except Exception:
                logger.exception('Exception in bounty generation task, continuing')
                continue

This is identical to the logic contained withing eicar.py.

filesystem.py 通过从磁盘上且可以是远程的 URI 来构建工件,从而在 eicar.py 上构建。 在真实代表中,这些工件将来自消费者向代表进行的提交。

测试您的参与者

Once everything is in place, let's test our participant:

Unit Testing →

Next Steps

Now that you're familiar with developing several proof of concept ambassadors, it's time to consider what it would take to build an production ambassadors that connects to the PolySwarm marketplace.

Production Ambassadors →