Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debugging functionality to the list_discovered_hosts.py sample #1192

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 37 additions & 31 deletions samples/discover/list_discovered_hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,36 @@
Creation date: 02.08.2022 - jshcodes@CrowdStrike
"""
import os
import logging
from argparse import ArgumentParser, RawTextHelpFormatter
from tabulate import tabulate
try:
from falconpy import Discover
from falconpy import Discover, Hosts
except ImportError as no_falconpy:
raise SystemExit("The crowdstrike-falconpy package must be installed "
"in order to run this progrem.\n\nInstall with the command: "
"in order to run this program.\n\nInstall with the command: "
"python3 -m pip install crowdstrike-falconpy") from no_falconpy


def parse_command_line() -> object:
"""Parse any received inbound command line parameters."""
parser = ArgumentParser(
description=__doc__,
formatter_class=RawTextHelpFormatter
)
)
parser.add_argument(
'-k',
'--client_id',
help='CrowdStrike Falcon API key ID.\n'
'You can also use the `FALCON_CLIENT_ID` environment variable to specify this value.',
required=False
)
)
parser.add_argument(
'-s',
'--client_secret',
help='CrowdStrike Falcon API key secret.\n'
'You can also use the `FALCON_CLIENT_SECRET` environment variable to specify this value.',
required=False
)
)
parser.add_argument(
'-b',
'--base_url',
Expand All @@ -68,7 +68,15 @@ def parse_command_line() -> object:
help='Reverse sort (defaults to ASC)',
required=False,
action="store_true"
)
)
parser.add_argument(
'-d',
'--debug',
help='Enable API debugging',
required=False,
default=False,
action="store_true"
)
parser.add_argument(
'-f',
'--format',
Expand All @@ -77,19 +85,13 @@ def parse_command_line() -> object:
'pretty, psql, rst, mediawiki, moinmoin, youtrack, html, unsafehtml, \n'
'latext, latex_raw, latex_booktabs, latex_longtable, textile, tsv)',
required=False
)

)
return parser.parse_args()


def get_sort_key(sorting) -> list:
"""Return the sort colum value for sorting operations."""
"""Return the sort column value for sorting operations."""
return sorting["hostname"]


# Retrieve all inbound command line parameters
# Retrieve all inbound command line parameters if args debug is present
args = parse_command_line()

# Set constants based upon received inputs
BASE_URL = "auto"
if args.base_url:
Expand All @@ -106,6 +108,10 @@ def get_sort_key(sorting) -> list:
SORT = False
else:
SORT = bool(args.reverse)
# add debug with logging put after parser
if args.debug:
logging.basicConfig(level=logging.DEBUG)

TABLE_FORMATS = [
"plain", "simple", "github", "grid", "fancy_grid", "pipe", "orgtbl", "jira", "presto",
"pretty", "psql", "rst", "mediawiki", "moinmoin", "youtrack", "html", "unsafehtml",
Expand All @@ -116,7 +122,6 @@ def get_sort_key(sorting) -> list:
table_format = args.format.strip().lower()
if table_format in TABLE_FORMATS:
TABLE_FORMAT = table_format

# Headers used in our result display table
HEADERS = {
"hostname": "Hostname",
Expand All @@ -125,33 +130,34 @@ def get_sort_key(sorting) -> list:
"plat": "Platform",
"osver": "Version"
}

hosts = Hosts(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
debug=args.debug
)
# Connect to the Discover API
discover = Discover(client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
base_url=BASE_URL
)

discover = Discover(auth_object=hosts)
# Empty list to hold our results
identified = []
# Query for a complete list of discovered hosts. Maxes out at 100.
host_lookup = discover.query_hosts()
if host_lookup["status_code"] == 200:
if host_lookup.get("status_code") == 200:
identified_hosts = host_lookup["body"]["resources"]
if not identified_hosts:
# No hosts returned for this search
print("No hosts identified")
else:
# Retrieve all details for all discovered hosts
host_detail = discover.get_hosts(ids=identified_hosts)["body"]["resources"]
# Add each hosts relevant detail to our `identified` list so we can display it
# Add each host's relevant detail to our `identified` list so we can display it
for host in host_detail:
found = {}
found["hostname"] = host.get("hostname", "Not identified")
found["current_local"] = host.get("current_local_ip", "Unknown")
found["current_external"] = host.get("external_ip", "Unknown")
found["plat"] = host.get("platform_name", "Unknown")
found["osver"] = host.get("os_version", "Unknown")
found = {
"hostname": host.get("hostname", "Not identified"),
"current_local": host.get("current_local_ip", "Unknown"),
"current_external": host.get("external_ip", "Unknown"),
"plat": host.get("platform_name", "Unknown"),
"osver": host.get("os_version", "Unknown")
}
# Append this result to our display list
identified.append(found)
# All findings have been tabulated, show the results
Expand Down
Loading