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.

Continuing:

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類再使用幾個有助於測試的參數:

  • 測試:當非零時,此參數指定代表在退出前將生成的最大懸賞數。
  • 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通過從磁盤上工件來構建eicar.py,並可選擇構建遠程 URI。 在真實代表中,這些工件將來自消費者向代表進行的提交。

測試您的參與者

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 →