polyswarm_api.api¶
Module Contents¶
-
class
polyswarm_api.api.PolyswarmAPI(key, uri=None, community=None, timeout=None, verify=True, **kwargs)[source]¶ Bases:
objectA synchronous interface to the public and private PolySwarm APIs.
-
wait_for(self, scan, timeout=settings.DEFAULT_SCAN_TIMEOUT)[source]¶ Wait for a Scan to scan successfully
- Parameters
scan – Scan id to wait for
timeout – Maximum time in seconds to wait before raising a TimeoutException
- Returns
The ArtifactInstance resource waited on
-
exists(self, hash_, hash_type=None)[source]¶ Search for the latest scans matching the given hash and hash_type.
- Parameters
hash – A Hashable object (Artifact, local.LocalArtifact, Hash) or hex-encoded SHA256/SHA1/MD5
hash_type – Hash type of the provided hash_. Will attempt to auto-detect if not explicitly provided.
- Returns
A boolean if the instance exists for search.
-
search(self, hash_, hash_type=None)[source]¶ Search for the latest scans matching the given hash and hash_type.
- Parameters
hash – A Hashable object (Artifact, local.LocalArtifact, Hash) or hex-encoded SHA256/SHA1/MD5
hash_type – Hash type of the provided hash_. Will attempt to auto-detect if not explicitly provided.
- Returns
Generator of ArtifactInstance resources
-
search_url(self, url)[source]¶ Search for the latest scan matching the given url.
- Parameters
url – A url to be searched by exact match
- Returns
Generator of ArtifactInstance resources
-
search_scans(self, hash_)[source]¶ Search for all scans ever made matching the given sha256.
- Parameters
hash – A Hashable object (Artifact, local.LocalArtifact, Hash) or hex-encoded SHA256
- Returns
Generator of ArtifactInstance resources
-
search_by_metadata(self, query, include=None, exclude=None, ips=None, urls=None, domains=None)[source]¶ Search artifacts by metadata
- Parameters
query – A query string
include – A list of fields to be included in the result (.* wildcards are accepted)
exclude – A list of fields to be excluded from the result (.* wildcards are accepted)
- Returns
Generator of ArtifactInstance resources
-
iocs_by_hash(self, hash_type, hash_value, hide_known_good=False)[source]¶ Retrieve IOCs by artifact hash
- Parameters
hash_type – Hash type of the provided hash_
hash_value – A list of fields to be included in the result (.* wildcards are accepted)
- Returns
Generator of IOC resources
-
search_by_ioc(self, ip=None, domain=None, ttp=None, imphash=None)[source]¶ Search artifacts by IOC (ip, domain, ttp, or imphash)
- Parameters
ip – ip address to search by
domain – domain address to search by
ttp – ttp to search by
imphash – ImpHash to search by
- Returns
Generator of ArtifactInstance resources
-
check_known_hosts(self, ips=[], domains=[])[source]¶ Check if ip addresses or domains are known.
:param ips :param domains :return: Generator of IOC resources
-
add_known_good_host(self, type, source, host)[source]¶ Add a known good ip or domain.
:param type :param source :param host :return: IOC resource
-
update_known_good_host(self, id, type, source, host, good)[source]¶ Update a known ip or domain.
:param type :param source :param host :return: IOC resource
-
submit(self, artifact, artifact_type=resources.ArtifactType.FILE, artifact_name=None, scan_config=None)[source]¶ Submit artifacts to polyswarm and return UUIDs
- Parameters
artifact – A file-like, path to file, url or LocalArtifact instance
artifact_type – The ArtifactType or strings containing “file” or “url”
artifact_name – An appropriate filename for the Artifact
scan_config – The scan configuration to be used, e.g.: “default”, “more-time”, “most-time”
- Returns
An ArtifactInstance resource
-
lookup(self, scan)[source]¶ Lookup a scan by Scan id.
- Parameters
scan – The Scan UUID to lookup
- Returns
An ArtifactInstance resource
-
rescan(self, hash_, hash_type=None, scan_config=None)[source]¶ Rescan a file based on and existing hash in the Polyswarm platform
- Parameters
hash – Hashable object (Artifact, local.LocalArtifact, or Hash) or hex-encoded SHA256/SHA1/MD5
hash_type – Hash type of the provided hash_. Will attempt to auto-detect if not explicitly provided.
scan_config – The scan configuration to be used, e.g.: “default”, “more-time”, “most-time”
- Returns
A ArtifactInstance resources
-
rescan_id(self, scan, scan_config=None)[source]¶ Re-execute a new scan based on an existing scan.
- Parameters
scan – Id of the existing scan
scan_config – The scan configuration to be used, e.g.: “default”, “more-time”, “most-time”
- Returns
A ArtifactInstance resource
-
live_start(self, rule_id)[source]¶ Create a new live hunt_id, and replace the currently running YARA rules.
- Parameters
rule_id – Yara ruleset id
- Returns
The ruleset with the associated live hunt
-
live_stop(self, rule_id)[source]¶ Stop a live hunt.
- Parameters
rule_id – Yara ruleset id
- Returns
The ruleset without an associate live hunt
-
live_feed(self, since=None, rule_name=None, family=None, polyscore_lower=None, polyscore_upper=None, community=None)[source]¶ Get live hunts feed
- Parameters
since – Fetch results from the last “since” minutes
rule_name – Filter hunt results on the provided rule name (exact match).
family – Filter hunt results based on the family name (exact match).
polyscore_lower – Polyscore lower bound for the hunt results.
polyscore_upper – Polyscore upper bound for the hunt results.
community – Community to retrieve live results from, or public/private.
- Returns
Generator of HuntResult resources
-
live_feed_delete(self, result_ids)[source]¶ Delete live feed results
- Parameters
result_ids – Live Feed Result IDs
- Returns
The deleted LiveHuntResult resources
-
live_result(self, result_id)[source]¶ Get yara ruleset for the live hunt result
- Parameters
result_id – Live result id
- Returns
Generator of HuntResult resources
-
historical_create(self, rule=None, ruleset_name=None)[source]¶ Run a new historical hunt.
- Parameters
rule – YaraRuleset object or string containing YARA rules to install
ruleset_name – Name of the ruleset.
- Returns
The created Hunt resource
-
historical_get(self, hunt=None)[source]¶ Get a historical hunt.
- Parameters
hunt – Hunt ID
- Returns
The Hunt resource
-
historical_update(self, hunt)[source]¶ Cancel a historical hunt :param hunt: The historical hunt id :return: The deleted HistoricalHunt resource
-
historical_delete(self, hunt)[source]¶ Delete a historical hunts.
- Parameters
hunt – Hunt ID
- Returns
The deleted Hunt resource
-
historical_list(self, since=None)[source]¶ List all historical hunts
- Returns
Generator of Hunt resources
-
historical_result(self, result_id)[source]¶ Get historical hunt result
- Parameters
result_id – Historical result id
- Returns
HistoricalHuntResult resource
-
historical_results(self, hunt=None, rule_name=None, family=None, polyscore_lower=None, polyscore_upper=None, community=None)[source]¶ Get results from a historical hunt
- Parameters
hunt – ID of the hunt (None if latest hunt results are desired)
rule_name – Filter hunt results on the provided rule name (exact match).
family – Filter hunt results based on the family name (exact match).
polyscore_lower – Polyscore lower bound for the hunt results.
polyscore_upper – Polyscore upper bound for the hunt results.
community – Community to retrieve live results from, or public/private.
- Returns
Generator of HuntResult resources
-
historical_results_delete(self, result_ids)[source]¶ Delete historical scan results
- Parameters
result_ids – Historical Hunt Result IDs
- Returns
The deleted HuntResult resources
-
historical_delete_list(self, historical_ids)[source]¶ Delete a historical hunts.
- Parameters
historical_ids – Historical Hunt IDs
- Returns
The deleted Hunt resource
-
ruleset_create(self, name, rules, description=None)[source]¶ Create a Yara Ruleset from the provided rules with the given name in the polyswarm platform. :param name: Name of the ruleset :param rules: Yara rules as a string :param description: Description of the ruleset :return: A YaraRuleset resource
-
ruleset_get(self, ruleset_id=None)[source]¶ Retrieve a YaraRuleset from the polyswarm platform by its Id. :param ruleset_id: Id of the ruleset :return: A YaraRuleset resource
-
ruleset_update(self, ruleset_id, name=None, rules=None, description=None)[source]¶ Update an existing YaraRuleset in the polyswarm platform by its Id. :param ruleset_id: Id of the ruleset :param name: New name of the ruleset :param rules: New yara rules as a string :param description: New description of the ruleset :return: The updated YaraRuleset resource
-
ruleset_delete(self, ruleset_id)[source]¶ Delete a YaraRuleset from the polyswarm platform by its Id. :param ruleset_id: Id of the ruleset :return: A YaraRuleset resource
-
ruleset_list(self)[source]¶ List all YaraRulesets for the current account. :return: A generator of YaraRuleset resources
-
tag_link_get(self, sha256)[source]¶ Fetch the Tags and Families associated with the given sha256.
- Parameters
sha256 – The sha256 of the artifact.
- Returns
A TagLink resource
-
tag_link_update(self, sha256, tags=None, families=None, emerging=None, remove=False)[source]¶ Update a TagLink with the given type or value by its id. :param sha256: The sha256 of the artifact. :param tags: A list of tags to be added or removed. :param families: A list of families to be added or removed. :param remove: A flag indicating if we should remove the provided tags/families. :return: A TagLink resource
-
tag_link_list(self, tags=None, families=None, or_tags=None, or_families=None)[source]¶ Fetch all existing TagLinks for the provided tags. :param tags: A list of tags that must be associated with the TagLinks listed. :param families: A list of families that must be associated with the TagLinks listed. :param or_tags: A list of tags where the TagLinks must be associated with at least one. :param or_families: A list of families where the TagLinks must be associated with at least one. :return: A TagLink resource
-
tag_create(self, name)[source]¶ Create a Tag. :param name: The tag we want to create. :return: A Tag resource
-
tag_get(self, name)[source]¶ Fetch a Tag. :param name: The tag we want to fetch. :return: A Tag resource
-
tag_delete(self, name)[source]¶ Delete a Tag. :param name: The tag we want to delete. :return: A Tag resource
-
family_create(self, name)[source]¶ Create a Family. :param name: The family name. :return: A MalwareFamily resource
-
family_get(self, name)[source]¶ Fetch a Family. :param name: The family name. :return: A MalwareFamily resource
-
family_delete(self, name)[source]¶ Delete a Family. :param name: The family name. :return: A MalwareFamily resource
-
family_update(self, family_name, emerging=True)[source]¶ Update the Family emerging status. :param family_name: The family name. :param emerging: A flag indicating if the family should be marked as emerging at this point in time. :return: A MalwareFamily resource
-
family_list(self)[source]¶ Fetch all existing Families :return: A generator of MalwareFamily resources
-
download(self, out_dir, hash_, hash_type=None)[source]¶ Grab the data of artifact identified by hash, and write the data to a file in the provided directory under a file named after the hash_. :param out_dir: Destination directory to download the file. :param hash_: The hash we should use to lookup the artifact to download. :param hash_type: Hash type of the provided hash_. Will attempt to auto-detect if not explicitly provided. :return: A LocalArtifact resource
-
download_id(self, out_dir, instance_id)[source]¶ Grab the data of artifact identified by hash, and write the data to a file in the provided directory under a file named after the hash_. :param out_dir: Destination directory to download the file. :param instance_id: The instance id we should use to lookup the artifact to download. :return: A LocalArtifact resource
-
sandbox_file(self, artifact, provider_slug, vm_slug, artifact_type=resources.ArtifactType.FILE, artifact_name=None)[source]¶
-
download_archive(self, out_dir, s3_path)[source]¶ Grab the data in the s3 path provided in the stream() method, and write the contents in the provided directory. :param out_dir: Destination directory to download the file. :param s3_path: Target S3 object to download. :return: A LocalArtifact resource
-
download_to_handle(self, hash_, fh, hash_type=None)[source]¶ Grab the data of artifact identified by hash, and write the data to a file handle :param hash_: The hash we should use to lookup the artifact to download. :param fh: A file-like object which we are going to write the contents of the artifact to. :param hash_type: Hash type of the provided hash_. Will attempt to auto-detect if not explicitly provided. :return: A LocalHandle resource
-
stream(self, since=settings.MAX_SINCE_TIME_STREAM)[source]¶ Access the stream of artifacts (ask info@polyswarm.io about access)
- Parameters
since – Fetch results from the last “since” minutes (up to 2 days)
- Returns
Generator of ArtifactArchive resources
-