|
9 | 9 | import requests
|
10 | 10 | import time
|
11 | 11 | from urllib import parse
|
| 12 | +from warnings import warn |
12 | 13 |
|
13 | 14 | from ReversingLabs.SDK.helper import ADVANCED_SEARCH_SORTING_CRITERIA, DEFAULT_USER_AGENT, RESPONSE_CODE_ERROR_MAP, \
|
14 | 15 | MD5, SHA1, SHA256, SHA512, \
|
@@ -1595,6 +1596,131 @@ def get_yara_cloud_retro_scan_status(self, ruleset_name):
|
1595 | 1596 |
|
1596 | 1597 | return response
|
1597 | 1598 |
|
| 1599 | + def advanced_search_v2(self, query_string, ticloud=False, page_number=1, records_per_page=20, sorting_criteria=None, |
| 1600 | + sorting_order="desc"): |
| 1601 | + """THIS METHOD IS DEPRECATED. Use advanced_search_v3 instead. |
| 1602 | +
|
| 1603 | + Sends a query string to the A1000 Advanced Search API v2. |
| 1604 | + The query string must be composed of key-value pairs separated by space. |
| 1605 | + A key is separated from its value by a colon symbol and no spaces. |
| 1606 | + For directions on how to write advanced search queries, consult the A1000 documentation. |
| 1607 | + If a page number is not provided, the first page of results will be returned. |
| 1608 | + Query string example: |
| 1609 | + 'av-count:5 available:TRUE' |
| 1610 | +
|
| 1611 | + :param query_string: query string |
| 1612 | + :type query_string: str |
| 1613 | + :param ticloud: show only cloud results |
| 1614 | + :type ticloud: bool |
| 1615 | + :param page_number: page number |
| 1616 | + :type page_number: int |
| 1617 | + :param records_per_page: number of records returned per page; maximum value is 100 |
| 1618 | + :type records_per_page: int |
| 1619 | + :param sorting_criteria: define the criteria used in sorting; possible values are 'sha1', 'firstseen', |
| 1620 | + 'threatname', 'sampletype', 'filecount', 'size' |
| 1621 | + :type sorting_criteria: str |
| 1622 | + :param sorting_order: sorting order; possible values are 'desc', 'asc' |
| 1623 | + :type sorting_order: str |
| 1624 | + :return: response |
| 1625 | + :rtype: requests.Response |
| 1626 | + """ |
| 1627 | + warn("This method is deprecated. Use advanced_search_v3 instead.", DeprecationWarning) |
| 1628 | + |
| 1629 | + if not isinstance(query_string, str): |
| 1630 | + raise WrongInputError("The search query must be a string.") |
| 1631 | + |
| 1632 | + if not isinstance(ticloud, bool): |
| 1633 | + raise WrongInputError("ticloud parameter must be boolean.") |
| 1634 | + |
| 1635 | + if not isinstance(records_per_page, int) or not 1 <= records_per_page <= 100: |
| 1636 | + raise WrongInputError("records_per_page parameter must be an integer with a value " |
| 1637 | + "between 1 and 100 (included).") |
| 1638 | + |
| 1639 | + url = self._url.format(endpoint=self.__ADVANCED_SEARCH_ENDPOINT_V2) |
| 1640 | + |
| 1641 | + post_json = {"query": query_string, "ticloud": ticloud, "page": page_number, |
| 1642 | + "records_per_page": records_per_page} |
| 1643 | + |
| 1644 | + if sorting_criteria: |
| 1645 | + if sorting_criteria not in ADVANCED_SEARCH_SORTING_CRITERIA or sorting_order not in ("desc", "asc"): |
| 1646 | + raise WrongInputError("Sorting criteria must be one of the following options: {criteria}. " |
| 1647 | + "Sorting order needs to be 'desc' or 'asc'.".format( |
| 1648 | + criteria=ADVANCED_SEARCH_SORTING_CRITERIA |
| 1649 | + )) |
| 1650 | + sorting_expression = "{criteria} {order}".format( |
| 1651 | + criteria=sorting_criteria, |
| 1652 | + order=sorting_order |
| 1653 | + ) |
| 1654 | + |
| 1655 | + post_json["sort"] = sorting_expression |
| 1656 | + |
| 1657 | + response = self.__post_request(url=url, post_json=post_json) |
| 1658 | + |
| 1659 | + self.__raise_on_error(response) |
| 1660 | + |
| 1661 | + return response |
| 1662 | + |
| 1663 | + def advanced_search_v2_aggregated(self, query_string, ticloud=False, max_results=5000, sorting_criteria=None, |
| 1664 | + sorting_order="desc"): |
| 1665 | + """THIS METHOD IS DEPRECATED. Use advanced_search_v3_aggregated instead. |
| 1666 | +
|
| 1667 | + Sends a query string to the A1000 Advanced Search API v2. |
| 1668 | + The query string must be composed of key-value pairs separated by space. |
| 1669 | + A key is separated from its value by a colon symbol and no spaces. |
| 1670 | + For directions on how to write advanced search queries, consult the A1000 documentation. |
| 1671 | + Paging is done automatically and results from individual |
| 1672 | + responses aggregated into one list and returned`. |
| 1673 | + The 'max_results' parameter defines the maximum desired number of results to be returned. |
| 1674 | + Query string example: |
| 1675 | + 'av-count:5 available:TRUE' |
| 1676 | +
|
| 1677 | + :param query_string: search query - see API documentation for details on writing search queries |
| 1678 | + :type query_string: str |
| 1679 | + :param ticloud: show only cloud results |
| 1680 | + :type ticloud: bool |
| 1681 | + :param max_results: maximum results to be returned in a list; default value is 5000 |
| 1682 | + :type max_results: int |
| 1683 | + :param sorting_criteria: define the criteria used in sorting; possible values are 'sha1', 'firstseen', |
| 1684 | + 'threatname', 'sampletype', 'filecount', 'size' |
| 1685 | + :type sorting_criteria: str |
| 1686 | + :param sorting_order: sorting order; possible values are 'desc', 'asc' |
| 1687 | + :type sorting_order: str |
| 1688 | + :return: list of results |
| 1689 | + :rtype: list |
| 1690 | + """ |
| 1691 | + warn("This method is deprecated. Use advanced_search_v3_aggregated instead.", DeprecationWarning) |
| 1692 | + |
| 1693 | + if not isinstance(max_results, int): |
| 1694 | + raise WrongInputError("max_results parameter must be integer.") |
| 1695 | + |
| 1696 | + results = [] |
| 1697 | + next_page = 1 |
| 1698 | + more_pages = True |
| 1699 | + |
| 1700 | + while more_pages: |
| 1701 | + response = self.advanced_search_v2( |
| 1702 | + query_string=query_string, |
| 1703 | + ticloud=ticloud, |
| 1704 | + page_number=next_page, |
| 1705 | + records_per_page=100, |
| 1706 | + sorting_criteria=sorting_criteria, |
| 1707 | + sorting_order=sorting_order |
| 1708 | + ) |
| 1709 | + |
| 1710 | + response_json = response.json() |
| 1711 | + |
| 1712 | + entries = response_json.get("rl").get("web_search_api").get("entries", []) |
| 1713 | + results.extend(entries) |
| 1714 | + |
| 1715 | + if len(results) > max_results: |
| 1716 | + results = results[:max_results] |
| 1717 | + return results |
| 1718 | + |
| 1719 | + next_page = response_json.get("rl").get("web_search_api").get("next_page", None) |
| 1720 | + more_pages = response_json.get("rl").get("web_search_api").get("more_pages", False) |
| 1721 | + |
| 1722 | + return results |
| 1723 | + |
1598 | 1724 | def advanced_search_v3(self, query_string, ticloud=False, start_search_date=None, end_search_date=None,
|
1599 | 1725 | page_number=1, records_per_page=20, sorting_criteria=None, sorting_order="desc"):
|
1600 | 1726 | """Sends a query string to the A1000 Advanced Search API v3.
|
|
0 commit comments