How to Download Files Using RTR with FalconPy SDK? #1081
-
Hello FalconPy Community, I am currently working on a project where I need to use the FalconPy SDK to download files from a host using the RTR (Real Time Response) capabilities of CrowdStrike's Falcon platform. While I have some understanding of initiating RTR sessions and executing commands, I am specifically looking for guidance on how to correctly use the get command to retrieve files. Here are my specific questions:
I have gone through the FalconPy documentation but was unable to find a detailed example that fits this use case. Any guidance, examples, or pointers to relevant parts of the documentation would be greatly appreciated. Thank you for your time and assistance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 14 replies
-
Hi @joseraeiro - Here are two simple variations (one for a single host and one for multiple hosts using batch commands). Give them a try and let us know if you have questions. 😄 Download a file from a single host using RTR_ExecuteActiveResponderCommand"""This example demonstrates downloading a single file from a single host."""
from logging import basicConfig, DEBUG
from falconpy import Hosts, RealTimeResponse
# Set these constants before executing
SHOW_API_RESPONSES = True # Turn this off to disable debugging
hostname = "" # Host we are targeting, ex: "testing-host"
target_location = "" # File location, ex: /home/ec2-user/
target_file = "target_file.txt" # Target file name
if SHOW_API_RESPONSES:
basicConfig(level=DEBUG) # Set our log level and output configuration
# Construct instances of the Service Classes we are wanting to use.
hosts = Hosts(client_id=CLIENT_ID, client_secret=CLIENT_SECRET, debug=SHOW_API_RESPONSES)
rtr = RealTimeResponse(auth_object=hosts)
# Retrieve our target device's AID.
target_device = hosts.query_devices(filter=f"hostname:'{hostname}'")["body"]["resources"][0]
# Initialize the session
session = rtr.init_session(device_id=target_device)
session_id = session["body"]["resources"][0]["session_id"] # Session ID
# Get the file
cloud_request_id = rtr.execute_active_responder_command(base_command="get",
device_id=target_device,
session_id=session_id,
command_string=f"get {target_location}{target_file}"
)["body"]["resources"][0]["cloud_request_id"]
# Wait for the cloud upload to complete
waiting = True
while waiting:
download_result = rtr.check_active_responder_command_status(cloud_request_id=cloud_request_id)["body"]["resources"][0]
if download_result["complete"] and (download_result["stdout"] or download_result["stderr"]):
if download_result["stderr"]:
raise SystemExit(download_result["stderr"])
waiting = False
target_sha = None
# Retrieve a list of files uploaded during this session
file_list = rtr.list_files_v2(session_id)["body"]["resources"]
# Get the SHA256 for the file from the list files lookup
target_sha = [
get_file["sha256"] for get_file in file_list if get_file["cloud_request_id"] == cloud_request_id
][0]
if target_sha:
# Use the SHA to request this file's contents to be saved to a local 7zip archive.
with open(f"{target_file}.7z", "wb") as save_file:
save_file.write(rtr.get_extracted_file_contents(session_id=session_id,
sha256=target_sha,
filename=target_file)
)
# Delete the session
rtr.delete_session(session_id) Download a file from multiple hosts using BatchGetCmd"""This example demonstrates downloading a single file from multiple hosts."""
from logging import basicConfig, DEBUG
from falconpy import Result, Hosts,RealTimeResponse
# Set these constants before executing
SHOW_API_RESPONSES = True # Turn this off to disable debugging
target_filter = "" # Host filter, ex: hostname:*'*falconpy*'
target_location = "" # File location, ex: /home/ec2-user/
target_file = "target_file.txt" # Target file name
if SHOW_API_RESPONSES:
basicConfig(level=DEBUG) # Set our log level and output configuration
# Construct instances of the Service Classes we are wanting to use.
hosts = Hosts(client_id=CLIENT_ID, client_secret=CLIENT_SECRET, debug=SHOW_API_RESPONSES)
rtr = RealTimeResponse(auth_object=hosts)
# Retrieve our target device AIDs.
target_devices = hosts.query_devices_by_filter_scroll(filter=target_filter)["body"]["resources"]
# Initialize a session with the host batch.
session_init: Result = rtr.batch_init_sessions(host_ids=target_devices)
batch_id = session_init["body"]["batch_id"] # Grab the batch ID
# Issue a batch get command
result = rtr.batch_get_command(batch_id=batch_id, file_path=f"{target_location}{target_file}")
# Grab the batch ID
batch_req_id = result["body"]["batch_get_cmd_req_id"]
# Check the status of the batch command
status = rtr.batch_get_command_status(batch_req_id)
# Loop thru the results
for device_id, device_result in status["body"]["resources"].items():
session_id = device_result["session_id"] # Session ID
file_sha = device_result["sha256"] # File SHA256
with open(f"{device_id}.7z", "wb") as save_file: # Save to a file named after the device ID
save_file.write(rtr.get_extracted_file_contents(session_id=session_id,
sha256=file_sha,
filename=target_file,
))
rtr.delete_session(session_id) There are also a few different RTR examples located in the Sample Library. |
Beta Was this translation helpful? Give feedback.
-
Hi @jshcodes, In addition to this question. In this part for example:
I noticed that when a file is large or there is slow internet the status["body"]["resources"] value is empty, so the API thinks there is no file to collect, however in the UI is see that its at 1% for example |
Beta Was this translation helpful? Give feedback.
Hi @joseraeiro -
Here are two simple variations (one for a single host and one for multiple hosts using batch commands).
Give them a try and let us know if you have questions. 😄
Download a file from a single host using RTR_ExecuteActiveResponderCommand