-
Notifications
You must be signed in to change notification settings - Fork 612
[New Rule] AWS EC2 LOLBin Execution via SSM #5354
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
Open
terrancedejesus
wants to merge
12
commits into
main
Choose a base branch
from
terrancedejesus/issue5353
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d31032e
[New Rule] AWS EC2 LOLBin Execution via SSM
terrancedejesus a3f56d4
updated from command
terrancedejesus afa26de
removed high order tag
terrancedejesus d93597e
adjusted query logic
terrancedejesus c594c43
updated reference
terrancedejesus 9aa9adf
add ESQL_priv. to keep
terrancedejesus 8b47c8f
Update rules/cross-platform/execution_aws_ec2_lolbin_via_ssm.toml
terrancedejesus 072e31d
Update rules/cross-platform/execution_aws_ec2_lolbin_via_ssm.toml
terrancedejesus 8c1cd8a
Merge branch 'main' into terrancedejesus/issue5353
terrancedejesus 8d539fc
cleaned up comments
terrancedejesus 37fbac5
updating query logic to use coalesce
terrancedejesus 80df7bc
Merge branch 'main' into terrancedejesus/issue5353
terrancedejesus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
245 changes: 245 additions & 0 deletions
245
rules/cross-platform/execution_aws_ec2_lolbin_via_ssm.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,245 @@ | ||
| [metadata] | ||
| creation_date = "2025/11/23" | ||
| integration = ["aws", "endpoint"] | ||
| maturity = "production" | ||
| updated_date = "2025/11/23" | ||
|
|
||
| [rule] | ||
| author = ["Elastic"] | ||
| description = """ | ||
| Identifies the execution of Living Off the Land Binaries (LOLBins) or GTFOBins on EC2 instances via AWS Systems Manager | ||
| (SSM) `SendCommand` API. This detection correlates AWS CloudTrail `SendCommand` events with endpoint process execution | ||
| by matching SSM command IDs. While AWS redacts command parameters in CloudTrail logs, this correlation technique reveals | ||
| the actual commands executed on EC2 instances. Adversaries may abuse SSM to execute malicious commands remotely without | ||
| requiring SSH or RDP access, using legitimate system utilities for data exfiltration, establishing reverse shells, or | ||
| lateral movement. | ||
| """ | ||
| false_positives = [ | ||
| """ | ||
| Legitimate administrative tasks using SSM to run system utilities may trigger this rule. Review the command context, | ||
| user identity, and timing to determine if the activity is authorized. | ||
| """, | ||
| """ | ||
| Automated configuration management or monitoring scripts that use LOLBins via SSM for legitimate purposes. Consider | ||
| excluding known automation accounts or specific command patterns. | ||
| """, | ||
| ] | ||
| from = "now-9m" | ||
| interval = "8m" | ||
| language = "esql" | ||
| license = "Elastic License v2" | ||
| name = "AWS EC2 LOLBin Execution via SSM SendCommand" | ||
| note = """## Triage and analysis | ||
|
|
||
| ### Investigating AWS EC2 LOLBin Execution via SSM SendCommand | ||
|
|
||
| AWS Systems Manager (SSM) enables remote command execution on EC2 instances without SSH/RDP access. While legitimate for administration, adversaries exploit this by running LOLBins—system utilities abused for malicious purposes like data theft or backdoors. This detection correlates CloudTrail API logs with endpoint telemetry using SSM command IDs, bypassing AWS's parameter redaction to reveal actual executed commands and identify suspicious activity. | ||
|
|
||
| This is an ESQL aggregation-based rule, thus all original event fields and detail may not be present in the alert. It is recommended to pivot into the raw events from both data sources for full context during investigation. | ||
|
|
||
| ### Possible investigation steps | ||
|
|
||
| - Review the SSM command ID in the alert to track the full lifecycle of the command from initiation to execution across both CloudTrail and endpoint data | ||
| - Examine the CloudTrail user identity, including the ARN and access key ID, to determine who initiated the SSM command and verify if the activity is authorized | ||
| - Analyze the command lines of the executed LOLBins to understand what commands were run and assess their intent, looking for indicators of data exfiltration, reverse shells, or reconnaissance | ||
| - Check the source IP address and user agent from the CloudTrail event to identify if the request came from an expected location or tool | ||
| - Investigate the affected EC2 instances for other suspicious activities or signs of compromise during the same timeframe, including network connections and file modifications | ||
| - Review the SSM shell process details to see the full context of what the SSM agent executed and identify the parent-child process relationships | ||
| - Correlate the timing between the CloudTrail event and endpoint execution to ensure they occurred within the detection window and represent the same activity | ||
| - Check if the same user identity or source IP has executed similar SSM commands on other EC2 instances in your environment | ||
|
|
||
| ### False positive analysis | ||
|
|
||
| - Routine administrative scripts that use utilities like curl, wget, or python for legitimate configuration management should be documented and excluded by user identity or source IP | ||
| - Automated monitoring tools that execute commands via SSM for health checks or data collection can be filtered by identifying their consistent patterns and access key IDs | ||
| - DevOps CI/CD pipelines that deploy or test applications using SSM may trigger alerts; create exceptions based on known automation roles or specific command patterns | ||
| - Security scanning tools that legitimately use SSM for vulnerability assessments should be allowlisted by their known IAM roles or source IPs | ||
| - Scheduled maintenance tasks using LOLBins for backup, log rotation, or data synchronization can be excluded by command pattern matching or execution timing | ||
|
|
||
| ### Response and remediation | ||
|
|
||
| - Immediately isolate the affected EC2 instance from the network to prevent further unauthorized command execution or lateral movement | ||
| - Review AWS CloudTrail logs to identify the IAM user, role, or access key associated with the suspicious SSM command and revoke or rotate compromised credentials | ||
| - Terminate any unauthorized processes identified on the endpoint that match the LOLBin execution patterns detected in the alert | ||
| - Conduct a forensic analysis of the affected EC2 instance to identify any persistence mechanisms, backdoors, or data exfiltration indicators | ||
| - Implement stricter IAM policies to limit SSM `SendCommand` permissions to only trusted users and roles, following the principle of least privilege | ||
| - Enable multi-factor authentication (MFA) for IAM users with SSM execution privileges to reduce the risk of credential compromise | ||
| - Review and update VPC security groups and network ACLs to restrict outbound traffic from EC2 instances to only necessary destinations, preventing data exfiltration | ||
| - Escalate the incident to the security operations center (SOC) for further investigation and to determine if additional AWS resources or accounts have been compromised | ||
| """ | ||
| references = [ | ||
| "https://www.mitiga.io/blog/abusing-the-amazon-web-services-ssm-agent-as-a-remote-access-trojan", | ||
| "https://www.kali.org/tools/pacu/", | ||
| "https://www.100daysofredteam.com/p/ghost-in-the-cloud-abusing-aws-ssm", | ||
| "https://hackingthe.cloud/aws/post_exploitation/run_shell_commands_on_ec2/", | ||
| "https://gtfobins.github.io/", | ||
| ] | ||
| risk_score = 47 | ||
| rule_id = "a8b3e2f0-8c7d-11ef-b4c6-f661ea17fbcd" | ||
| severity = "medium" | ||
| tags = [ | ||
| "Domain: Cloud", | ||
| "Domain: Endpoint", | ||
| "OS: Linux", | ||
| "Use Case: Threat Detection", | ||
| "Tactic: Execution", | ||
| "Tactic: Command and Control", | ||
| "Data Source: AWS", | ||
| "Data Source: Amazon Web Services", | ||
| "Data Source: AWS CloudTrail", | ||
| "Data Source: Elastic Defend", | ||
| "Resources: Investigation Guide", | ||
| ] | ||
| timestamp_override = "event.ingested" | ||
| type = "esql" | ||
|
|
||
| query = ''' | ||
| FROM logs-aws.cloudtrail*, logs-endpoint.* METADATA _id, _version, _index | ||
| | WHERE | ||
| // CloudTrail SSM SendCommand with AWS-RunShellScript | ||
| ( | ||
| event.dataset == "aws.cloudtrail" | ||
| AND event.action == "SendCommand" | ||
| AND aws.cloudtrail.request_parameters LIKE "*documentName=AWS-RunShellScript*" | ||
| ) | ||
| // Linux endpoint process events, prefiltered to SSM shell runner OR LOLBins/GTFOBins | ||
| OR | ||
| ( | ||
| event.dataset == "endpoint.events.process" | ||
| AND host.os.type == "linux" | ||
| AND ( | ||
| // SSM shell (_script.sh) runner | ||
| process.command_line LIKE "%/document/orchestration/%/awsrunShellScript/%/_script.sh" | ||
| // LOLBins / GTFOBins | ||
| OR process.name IN ( | ||
| "base64", | ||
| "curl", | ||
| "wget", | ||
| "openssl", | ||
| "nc", "ncat", "netcat", | ||
| "socat", | ||
| "python", "python3", | ||
| "perl", | ||
| "php", | ||
| "ruby", | ||
| "ssh", | ||
| "scp", | ||
| "sftp", | ||
| "rsync" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. optional - add |
||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| // Endpoint leg: extract SSM command ID from parent command line | ||
| | DISSECT process.parent.command_line | ||
| "%{}/document/orchestration/%{Esql.process_parent_command_line_ssm_command_id}/%{}" | ||
|
|
||
| // CloudTrail leg: extract SSM command ID from response_elements | ||
| | DISSECT aws.cloudtrail.response_elements | ||
| "%{}commandId=%{Esql.aws_cloudtrail_response_elements_ssm_command_id},%{}" | ||
|
|
||
| // Coalesce SSM command ID from both data sources | ||
| | EVAL Esql.aws_ssm_command_id = COALESCE( | ||
| Esql.aws_cloudtrail_response_elements_ssm_command_id, | ||
| Esql.process_parent_command_line_ssm_command_id | ||
| ) | ||
| | WHERE Esql.aws_ssm_command_id IS NOT NULL | ||
|
|
||
| // Role flags | ||
| | EVAL Esql.is_cloud_event = event.dataset == "aws.cloudtrail" | ||
| | EVAL Esql.is_endpoint_event = event.dataset == "endpoint.events.process" | ||
|
|
||
| // Identify the SSM shell processes (the _script.sh runners) | ||
| | EVAL Esql.is_ssm_shell_process = | ||
| Esql.is_endpoint_event | ||
| AND process.command_line LIKE "%/document/orchestration/%/awsrunShellScript/%/_script.sh" | ||
|
|
||
| // LOLBins / GTFOBins on Linux | ||
| | EVAL Esql.is_lolbin_process = | ||
| Esql.is_endpoint_event AND NOT Esql.is_ssm_shell_process | ||
|
|
||
| // Aggregate per SSM command ID | ||
| | STATS | ||
| // Core correlation counts & timing | ||
| Esql.aws_cloudtrail_event_count = SUM(CASE(Esql.is_cloud_event, 1, 0)), | ||
| Esql.endpoint_events_process_lolbin_count = SUM(CASE(Esql.is_lolbin_process, 1, 0)), | ||
| Esql.endpoint_events_process_ssm_shell_count = SUM(CASE(Esql.is_ssm_shell_process, 1, 0)), | ||
| Esql.aws_cloudtrail_first_event_ts = MIN(CASE(Esql.is_cloud_event, @timestamp, null)), | ||
| Esql.endpoint_events_process_first_lolbin_ts = MIN(CASE(Esql.is_lolbin_process, @timestamp, null)), | ||
|
|
||
| // AWS / CloudTrail identity & request context | ||
| Esql_priv.aws_cloudtrail_user_identity_arn_values = | ||
| VALUES(CASE(Esql.is_cloud_event, aws.cloudtrail.user_identity.arn, null)), | ||
| Esql_priv.aws_cloudtrail_user_identity_access_key_id_values = | ||
| VALUES(CASE(Esql.is_cloud_event, aws.cloudtrail.user_identity.access_key_id, null)), | ||
| Esql_priv.user_name_values = | ||
| VALUES(CASE(Esql.is_cloud_event, user.name, null)), | ||
|
|
||
| // AWS environment / request metadata | ||
| Esql.cloud_region_values = VALUES(CASE(Esql.is_cloud_event, cloud.region, null)), | ||
| Esql.source_ip_values = VALUES(CASE(Esql.is_cloud_event, source.ip, null)), | ||
| Esql.user_agent_original_values = | ||
| VALUES(CASE(Esql.is_cloud_event, user_agent.original, null)), | ||
|
|
||
| // Endpoint host & user context | ||
| Esql.host_name_values = VALUES(CASE(Esql.is_endpoint_event, host.name, null)), | ||
| Esql_priv.endpoint_user_name_values = | ||
| VALUES(CASE(Esql.is_endpoint_event, user.name, null)), | ||
|
|
||
| // SSM shell processes on endpoint | ||
| Esql.process_command_line_ssm_shell_values = | ||
| VALUES(CASE(Esql.is_ssm_shell_process, process.command_line, null)), | ||
| Esql.process_pid_ssm_shell_values = | ||
| VALUES(CASE(Esql.is_ssm_shell_process, process.pid, null)), | ||
|
|
||
| // LOLBin processes on endpoint | ||
| Esql.process_name_lolbin_values = | ||
| VALUES(CASE(Esql.is_lolbin_process, process.name, null)), | ||
| Esql.process_executable_lolbin_values = | ||
| VALUES(CASE(Esql.is_lolbin_process, process.executable, null)), | ||
| Esql.process_command_line_lolbin_values = | ||
| VALUES(CASE(Esql.is_lolbin_process, process.command_line, null)), | ||
| Esql.process_pid_lolbin_values = | ||
| VALUES(CASE(Esql.is_lolbin_process, process.pid, null)), | ||
| Esql.process_parent_command_line_lolbin_values = | ||
| VALUES(CASE(Esql.is_lolbin_process, process.parent.command_line, null)) | ||
| BY Esql.aws_ssm_command_id | ||
|
|
||
| // Detection condition: SSM SendCommand + AWS-RunShellScript + LOLBin on endpoint | ||
| | WHERE Esql.aws_cloudtrail_event_count > 0 | ||
| AND Esql.endpoint_events_process_lolbin_count > 0 | ||
| AND DATE_DIFF( | ||
| "minutes", | ||
| Esql.endpoint_events_process_first_lolbin_ts, | ||
| Esql.aws_cloudtrail_first_event_ts | ||
| ) <= 5 | ||
| | SORT Esql.aws_cloudtrail_first_event_ts ASC | ||
| | KEEP Esql.*, Esql_priv.* | ||
| ''' | ||
|
|
||
|
|
||
| [[rule.threat]] | ||
| framework = "MITRE ATT&CK" | ||
| [[rule.threat.technique]] | ||
| id = "T1651" | ||
| name = "Cloud Administration Command" | ||
| reference = "https://attack.mitre.org/techniques/T1651/" | ||
|
|
||
|
|
||
| [rule.threat.tactic] | ||
| id = "TA0002" | ||
| name = "Execution" | ||
| reference = "https://attack.mitre.org/tactics/TA0002/" | ||
| [[rule.threat]] | ||
| framework = "MITRE ATT&CK" | ||
| [[rule.threat.technique]] | ||
| id = "T1105" | ||
| name = "Ingress Tool Transfer" | ||
| reference = "https://attack.mitre.org/techniques/T1105/" | ||
|
|
||
|
|
||
| [rule.threat.tactic] | ||
| id = "TA0011" | ||
| name = "Command and Control" | ||
| reference = "https://attack.mitre.org/tactics/TA0011/" | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.