Skip to content

Commit 828647f

Browse files
Add the option to fetch all results in aggregate methods
1 parent b6cc68c commit 828647f

File tree

1 file changed

+62
-56
lines changed

1 file changed

+62
-56
lines changed

ReversingLabs/SDK/a1000.py

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -848,18 +848,18 @@ def list_extracted_files_v2(self, sample_hash, page_size=None, page=None):
848848

849849
return response
850850

851-
def list_extracted_files_v2_aggregated(self, sample_hash, max_results=5000):
851+
def list_extracted_files_v2_aggregated(self, sample_hash, max_results=None):
852852
"""Get a list of all files TitaniumCore engine extracted from the requested sample during static analysis.
853853
Paging is done automatically and results from individual responses aggregated into one list and returned.
854854
The max_results parameter defines the maximum number of results to be returned to the list.
855855
:param sample_hash: hash string
856856
:type sample_hash: str
857-
:param max_results: maximum number of results to be returned
858-
:type max_results: int
857+
:param max_results: number of results to be returned in the list;
858+
set as integer to receive a defined number of results or leave as None to receive all available results
859+
:type max_results: int or None
859860
:return: list of results
860861
:rtype: list
861862
"""
862-
pass
863863
result_list = []
864864
next_page = 1
865865

@@ -875,14 +875,16 @@ def list_extracted_files_v2_aggregated(self, sample_hash, max_results=5000):
875875
results = response_json.get("results", [])
876876
result_list.extend(results)
877877

878-
if len(result_list) > max_results:
879-
results = result_list[:max_results]
880-
return results
881-
882878
next_page_url = response_json.get("next", None)
883879
next_page = int(next_page_url.split("?")[1].split("&")[0].split("=")[1]) if next_page_url else None
884880

885-
return result_list
881+
if not max_results:
882+
if not next_page:
883+
return result_list
884+
885+
else:
886+
if not next_page or len(result_list) >= max_results:
887+
return result_list[:max_results]
886888

887889
def download_extracted_files(self, sample_hash):
888890
"""Accepts a single hash string and returns a downloadable archive file
@@ -1726,7 +1728,7 @@ def advanced_search_v2(self, query_string, ticloud=False, page_number=1, records
17261728

17271729
return response
17281730

1729-
def advanced_search_v2_aggregated(self, query_string, ticloud=False, max_results=5000, sorting_criteria=None,
1731+
def advanced_search_v2_aggregated(self, query_string, ticloud=False, max_results=None, sorting_criteria=None,
17301732
sorting_order="desc"):
17311733
"""THIS METHOD IS DEPRECATED. Use advanced_search_v3_aggregated instead.
17321734
@@ -1744,8 +1746,9 @@ def advanced_search_v2_aggregated(self, query_string, ticloud=False, max_results
17441746
:type query_string: str
17451747
:param ticloud: show only cloud results
17461748
:type ticloud: bool
1747-
:param max_results: maximum results to be returned in a list; default value is 5000
1748-
:type max_results: int
1749+
:param max_results: number of results to be returned in the list;
1750+
set as integer to receive a defined number of results or leave as None to receive all available results
1751+
:type max_results: int or None
17491752
:param sorting_criteria: define the criteria used in sorting; possible values are 'sha1', 'firstseen',
17501753
'threatname', 'sampletype', 'filecount', 'size'
17511754
:type sorting_criteria: str
@@ -1756,9 +1759,6 @@ def advanced_search_v2_aggregated(self, query_string, ticloud=False, max_results
17561759
"""
17571760
warn("This method is deprecated. Use advanced_search_v3_aggregated instead.", DeprecationWarning)
17581761

1759-
if not isinstance(max_results, int):
1760-
raise WrongInputError("max_results parameter must be integer.")
1761-
17621762
results = []
17631763
next_page = 1
17641764
more_pages = True
@@ -1778,14 +1778,16 @@ def advanced_search_v2_aggregated(self, query_string, ticloud=False, max_results
17781778
entries = response_json.get("rl").get("web_search_api").get("entries", [])
17791779
results.extend(entries)
17801780

1781-
if len(results) > max_results:
1782-
results = results[:max_results]
1783-
return results
1784-
17851781
next_page = response_json.get("rl").get("web_search_api").get("next_page", None)
17861782
more_pages = response_json.get("rl").get("web_search_api").get("more_pages", False)
17871783

1788-
return results
1784+
if not max_results:
1785+
if not more_pages:
1786+
return results
1787+
1788+
else:
1789+
if not more_pages or len(results) >= max_results:
1790+
return results[:max_results]
17891791

17901792
def advanced_search_v3(self, query_string, ticloud=False, start_search_date=None, end_search_date=None,
17911793
page_number=1, records_per_page=20, sorting_criteria=None, sorting_order="desc"):
@@ -1862,7 +1864,7 @@ def advanced_search_v3(self, query_string, ticloud=False, start_search_date=None
18621864
return response
18631865

18641866
def advanced_search_v3_aggregated(self, query_string, ticloud=False, start_search_date=None, end_search_date=None,
1865-
records_per_page=20, max_results=5000, sorting_criteria=None,
1867+
records_per_page=20, max_results=None, sorting_criteria=None,
18661868
sorting_order="desc"):
18671869
"""This method handles the paging automatically.
18681870
Sends a query string to the A1000 Advanced Search API v3.
@@ -1885,8 +1887,9 @@ def advanced_search_v3_aggregated(self, query_string, ticloud=False, start_searc
18851887
:type end_search_date: str
18861888
:param records_per_page: number of records returned per page; maximum value is 100
18871889
:type records_per_page: int
1888-
:param max_results: maximum number of returned results
1889-
:type max_results: int
1890+
:param max_results: number of results to be returned in the list;
1891+
set as integer to receive a defined number of results or leave as None to receive all available results
1892+
:type max_results: int or None
18901893
:param sorting_criteria: define the criteria used in sorting; possible values are 'sha1', 'firstseen',
18911894
'threatname', 'sampletype', 'filecount', 'size'
18921895
:type sorting_criteria: str
@@ -1895,9 +1898,6 @@ def advanced_search_v3_aggregated(self, query_string, ticloud=False, start_searc
18951898
:return: list of results
18961899
:rtype: list
18971900
"""
1898-
if not isinstance(max_results, int):
1899-
raise WrongInputError("max_results parameter must be integer.")
1900-
19011901
results = []
19021902
next_page = 1
19031903
more_pages = True
@@ -1922,10 +1922,13 @@ def advanced_search_v3_aggregated(self, query_string, ticloud=False, start_searc
19221922
next_page = response_json.get("rl").get("web_search_api").get("next_page", None)
19231923
more_pages = response_json.get("rl").get("web_search_api").get("more_pages")
19241924

1925-
if len(results) >= max_results or not more_pages:
1926-
break
1925+
if not max_results:
1926+
if not more_pages:
1927+
return results
19271928

1928-
return results[:max_results]
1929+
else:
1930+
if not more_pages or len(results) >= max_results:
1931+
return results[:max_results]
19291932

19301933
def list_containers_for_hashes(self, sample_hashes):
19311934
"""Gets a list of all top-level containers from which the requested sample has been extracted during analysis.
@@ -2045,21 +2048,19 @@ def network_ip_to_domain(self, ip_addr, page=None, page_size=500):
20452048

20462049
return response
20472050

2048-
def network_ip_to_domain_aggregated(self, ip_addr, page_size=500, max_results=5000):
2051+
def network_ip_to_domain_aggregated(self, ip_addr, page_size=500, max_results=None):
20492052
"""Accepts an IP address string and returns a list of IP-to-domain mappings.
20502053
This method performs the paging automatically and returns a specified maximum number of records.
20512054
:param ip_addr: requested IP address
20522055
:type ip_addr: str
20532056
:param page_size: number of records per page
20542057
:type page_size: int
2055-
:param max_results: maximum number of returned records
2056-
:type max_results: int
2058+
:param max_results: number of results to be returned in the list;
2059+
set as integer to receive a defined number of results or leave as None to receive all available results
2060+
:type max_results: int or None
20572061
:return: list of results
20582062
:rtype: list
20592063
"""
2060-
if not isinstance(max_results, int):
2061-
raise WrongInputError("max_results parameter must be integer.")
2062-
20632064
results = []
20642065
next_page = None
20652066

@@ -2077,10 +2078,13 @@ def network_ip_to_domain_aggregated(self, ip_addr, page_size=500, max_results=50
20772078

20782079
next_page = response_json.get("next_page", None)
20792080

2080-
if len(results) >= max_results or not next_page:
2081-
break
2081+
if not max_results:
2082+
if not next_page:
2083+
return results
20822084

2083-
return results[:max_results]
2085+
else:
2086+
if not next_page or len(results) >= max_results:
2087+
return results[:max_results]
20842088

20852089
def network_urls_from_ip(self, ip_addr, page=None, page_size=500):
20862090
"""Accepts an IP address string and returns a list of URLs hosted on the requested IP address.
@@ -2112,21 +2116,19 @@ def network_urls_from_ip(self, ip_addr, page=None, page_size=500):
21122116

21132117
return response
21142118

2115-
def network_urls_from_ip_aggregated(self, ip_addr, page_size=500, max_results=5000):
2119+
def network_urls_from_ip_aggregated(self, ip_addr, page_size=500, max_results=None):
21162120
"""Accepts an IP address string and returns a list of URLs hosted on the requested IP address.
21172121
This method performs the paging automatically and returns a specified maximum number of records.
21182122
:param ip_addr: requested IP address
21192123
:type ip_addr: str
21202124
:param page_size: number of records per page
21212125
:type page_size: int
2122-
:param max_results: maximum number of returned records
2123-
:type max_results: int
2126+
:param max_results: number of results to be returned in the list;
2127+
set as integer to receive a defined number of results or leave as None to receive all available results
2128+
:type max_results: int or None
21242129
:return: list of results
21252130
:rtype: list
21262131
"""
2127-
if not isinstance(max_results, int):
2128-
raise WrongInputError("max_results parameter must be integer.")
2129-
21302132
results = []
21312133
next_page = None
21322134

@@ -2144,10 +2146,13 @@ def network_urls_from_ip_aggregated(self, ip_addr, page_size=500, max_results=50
21442146

21452147
next_page = response_json.get("next_page", None)
21462148

2147-
if len(results) >= max_results or not next_page:
2148-
break
2149+
if not max_results:
2150+
if not next_page:
2151+
return results
21492152

2150-
return results[:max_results]
2153+
else:
2154+
if not next_page or len(results) >= max_results:
2155+
return results[:max_results]
21512156

21522157
def network_files_from_ip(self, ip_addr, extended_results=True, classification=None, page=None, page_size=500):
21532158
"""Accepts an IP address string and returns a list of hashes and
@@ -2194,7 +2199,7 @@ def network_files_from_ip(self, ip_addr, extended_results=True, classification=N
21942199
return response
21952200

21962201
def network_files_from_ip_aggregated(self, ip_addr, extended_results=True, classification=None, page_size=500,
2197-
max_results=5000):
2202+
max_results=None):
21982203
"""Accepts an IP address string and returns a list of hashes and
21992204
classifications for files found on the requested IP address.
22002205
This method performs the paging automatically and returns a specified maximum number of records.
@@ -2206,14 +2211,12 @@ def network_files_from_ip_aggregated(self, ip_addr, extended_results=True, class
22062211
:type classification: str
22072212
:param page_size: number of records per page
22082213
:type page_size: int
2209-
:param max_results: maximum number of returned records
2210-
:type max_results: int
2214+
:param max_results: number of results to be returned in the list;
2215+
set as integer to receive a defined number of results or leave as None to receive all available results
2216+
:type max_results: int or None
22112217
:return: list of results
22122218
:rtype: list
22132219
"""
2214-
if not isinstance(max_results, int):
2215-
raise WrongInputError("max_results parameter must be integer.")
2216-
22172220
results = []
22182221
next_page = None
22192222

@@ -2233,10 +2236,13 @@ def network_files_from_ip_aggregated(self, ip_addr, extended_results=True, class
22332236

22342237
next_page = response_json.get("next_page", None)
22352238

2236-
if len(results) >= max_results or not next_page:
2237-
break
2239+
if not max_results:
2240+
if not next_page:
2241+
return results
22382242

2239-
return results[:max_results]
2243+
else:
2244+
if not next_page or len(results) >= max_results:
2245+
return results[:max_results]
22402246

22412247
def __ip_addr_endpoints(self, ip_addr, specific_endpoint, params=None):
22422248
"""Private method for all IP related endpoints from the Network Threat Intelligence API.

0 commit comments

Comments
 (0)