Skip to content

Commit

Permalink
fixed a bug where the offset isn't displayed for query yielding more …
Browse files Browse the repository at this point in the history
…results than the first page; also added support for the global and desc flag for search-by-term (#21)
  • Loading branch information
peter-kwan authored Jan 13, 2025
1 parent e827d6f commit 0daa47b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 15 deletions.
9 changes: 6 additions & 3 deletions praetorian_cli/handlers/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
@click.option('-t', '--term', help='Enter a search term', required=True)
@click.option('-c', '--count', is_flag=True, default=False, help='Return statistics on search')
@click.option('-d', '--details', is_flag=True, default=False, help='Show detailed information')
def search(chariot, term, count, details, offset, page):
@click.option('-desc', '--desc', is_flag=True, default=False, help='Return data in descending order')
@click.option('-g', '--global', 'global_', is_flag=True, default=False, help='Use the global data set')
def search(chariot, term, count, details, offset, page, desc, global_):
""" Query Chariot for matches or counts using the search syntax
\b
Expand Down Expand Up @@ -53,9 +55,10 @@ def search(chariot, term, count, details, offset, page):
- praetorian chariot search --term "status:OH"
- praetorian chariot search --term "status:OH" --details --page all
- praetorian chariot search --term "#asset#www.example.com"
- praetorian chariot search --term "dns:https://github.com/praetorian-inc/"
- praetorian chariot search --term "dns:https://github.com/praetorian-inc/" --desc
"""
if count:
print_json(chariot.search.count(term))
else:
render_list_results(chariot.search.by_term(term, offset, pagination_size(page)), details)
render_list_results(chariot.search.by_term(term, offset, pagination_size(page), desc=desc, global_=global_),
details)
2 changes: 1 addition & 1 deletion praetorian_cli/handlers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def render_list_results(list_results, details):
def render_offset(offset):
if offset:
click.echo('There are more results. Add the following argument to the command to view them:')
click.echo(f'--offset "{json.dumps(offset)}"')
click.echo(f'--offset {json.dumps(offset)}')


def pagination_size(page):
Expand Down
5 changes: 3 additions & 2 deletions praetorian_cli/sdk/chariot.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ def my(self, params: dict, pages=1) -> {}:
process_failure(resp)
resp = resp.json()
extend(final_resp, resp)

if 'offset' not in resp:
break

params['offset'] = json.dumps(resp['offset'])

if 'offset' in resp:
final_resp['offset'] = json.dumps(resp['offset'])

return final_resp

def post(self, type: str, params):
Expand Down
6 changes: 5 additions & 1 deletion praetorian_cli/sdk/entities/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ def by_ip(self, ip_prefix, offset=None, pages=10000) -> tuple:
def by_dns(self, dns_prefix, offset=None, pages=10000) -> tuple:
return self.by_term(f'dns:{dns_prefix}', offset, pages)

def by_term(self, search_term, offset=None, pages=1000, exact=False) -> tuple:
def by_term(self, search_term, offset=None, pages=1000, exact=False, desc=False, global_=False) -> tuple:
params = dict(key=search_term)
if offset:
params = params | dict(offset=offset)
if exact:
params = params | dict(exact='true')
if desc:
params = params | dict(desc='true')
if global_:
params = params | {'global': 'true'}

# extract all the different types of entities in the search results into a
# flattened list of `hits`
Expand Down
17 changes: 9 additions & 8 deletions praetorian_cli/sdk/model/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,29 +83,32 @@ class AddRisk(Enum):


CAPABILITIES = (
'reverse-whois',
'csp-mine',
'tls-mine',
'azuread-discovery',
'edgar',
'cidr',
'favicon',
'reverse-csp',
'builtwith',
'nuclei',
'whois',
'subdomain',
'csp-mine',
'tls-mine',
'portscan',
'github',
'github-repository',
'secrets',
'amazon',
'bitbucket',
'azure',
'gcp',
'ns1',
'cloudflare',
'gato',
'crowdstrike',
'crawler',
'gitlab',
'ssh',
'azuread-discovery',
'edgar',
'nessus',
'nessus-import',
'insightvm',
Expand All @@ -114,11 +117,9 @@ class AddRisk(Enum):
'qualys-import',
'burp-enterprise',
'ip',
'cidr',
'website',
'reverse-whois',
'digitalocean',
'burp-internal',
'seed-import',
'builtwith'
'tenablevm',
)
3 changes: 3 additions & 0 deletions praetorian_cli/sdk/test/test_z_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ def test_search_cli(self):
self.verify(f'add asset -n {o.asset_name} -d {o.asset_dns}')

self.verify(f'search -t "#asset#{o.asset_dns}" -p all', [o.asset_key])
self.verify(f'search -t "#asset#{o.asset_dns}" -p all --desc', [o.asset_key])
self.verify(f'search -t "#asset#{o.asset_dns}" -p all -g')

self.verify(f'search -t "#asset#{o.asset_dns}" -d -p all', [o.asset_key, '"key"', '"data"'])
self.verify(f'search -t "#asset#{o.asset_dns}" -c -p all', ['"A": 1'])

Expand Down

0 comments on commit 0daa47b

Please sign in to comment.