Test GuardDuty’s integration with Radiant
Test your AWS integration with a GuardDuty automation script.
Requirements
Bash script
#!/bin/bash
# Path to the credentials file
CREDENTIALS_FILE="credentials.txt"
# Path to the log file
LOG_FILE="results.log"
# Function to log messages
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}
# Function to log errors
log_error() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - ERROR: $1" >> "$LOG_FILE"
}
# Function to fetch enabled regions for the organization
fetch_enabled_regions() {
echo $(aws ec2 describe-regions --query "Regions[*].RegionName" --output text)
}
# Function to process each account's GuardDuty operations
function process_guardduty_for_account() {
local account_id="$1"
local key_id="$2"
local access_key="$3"
local session_token="$4"
local region="$5"
log_message "Processing for Account ID $account_id in region $region..."
# Fetch the first GuardDuty detector ID in the region
local output
output=$(AWS_ACCESS_KEY_ID="$key_id" AWS_SECRET_ACCESS_KEY="$access_key" AWS_SESSION_TOKEN="$session_token" AWS_DEFAULT_REGION="$region" aws guardduty list-detectors --query "DetectorIds[0]" --output text --region "$region" 2>&1)
local status=$?
if [ "$status" -ne 0 ]; then
log_error "Failed to list detectors for Account ID $account_id in region $region: $output"
return
elif [ "$output" == "None" ]; then
log_message "No GuardDuty detectors found for Account ID $account_id in region $region."
return
fi
local detector_id="$output"
log_message "Generating sample findings for Detector ID $detector_id..."
output=$(AWS_ACCESS_KEY_ID="$key_id" AWS_SECRET_ACCESS_KEY="$access_key" AWS_SESSION_TOKEN="$session_token" AWS_DEFAULT_REGION="$region" aws guardduty create-sample-findings --detector-id "$detector_id" --finding-types 'UnauthorizedAccess:EC2/SSHBruteForce' --region "$region" 2>&1)
status=$?
if [ "$status" -ne 0 ]; then
log_error "Failed to create sample findings for Account ID $account_id: $output"
else
log_message "Sample findings generated for Account ID: $account_id in region $region."
fi
}
# Main function to initiate processing
function main() {
log_message "Starting GuardDuty operations..."
# Assigns the first argument to credentials_path, or defaults to $CREDENTIALS_FILE if not provided.
local credentials_path="$1"
if [ -z "$credentials_path" ]; then
credentials_path="$CREDENTIALS_FILE"
fi
# Assigns the second argument to accounts_list, or defaults to all accounts if not provided.
local accounts_list=(${@:2})
local enabled_regions=($(fetch_enabled_regions))
local current_account_id
local aws_access_key_id
local aws_secret_access_key
local aws_session_token
while IFS= read -r line || [[ -n "$line" ]]; do
case "$line" in
\[*\]*)
# Process the previous account if all data is available
if [[ -n "$current_account_id" ]]; then
if [ ${#accounts_list[@]} -eq 0 ] || [[ " ${accounts_list[*]} " =~ " $current_account_id " ]]; then
for region in "${enabled_regions[@]}"; do
process_guardduty_for_account "$current_account_id" "$aws_access_key_id" "$aws_secret_access_key" "$aws_session_token" "$region"
done
fi
fi
# Start new account block
current_account_id=$(echo "$line" | sed -e 's/\[\(.*\)_.*\]/\1/')
aws_access_key_id=""
aws_secret_access_key=""
aws_session_token=""
;;
aws_access_key_id=*)
aws_access_key_id="${line#*=}"
;;
aws_secret_access_key=*)
aws_secret_access_key="${line#*=}"
;;
aws_session_token=*)
aws_session_token="${line#*=}"
;;
esac
done < "$credentials_path"
# Process the last account if all data is available
if [[ -n "$current_account_id" ]] && [ ${#accounts_list[@]} -eq 0 ] || [[ " ${accounts_list[*]} " =~ " $current_account_id " ]]; then
for region in "${enabled_regions[@]}"; do
process_guardduty_for_account "$current_account_id" "$aws_access_key_id" "$aws_secret_access_key" "$aws_session_token" "$region"
done
fi
log_message "GuardDuty operations completed."
}
# If no arguments are provided, the script will use the default credentials file and process all accounts.
main "$@"Script components
Script operation
Configuration
Usage
Example
Last updated