Detection rules › Kusto

AWSCloudTrail - Suspicious command sent to EC2

Status
available
Severity
high
Time window
1d
Source
github.com/Azure/Azure-Sentinel

An attacker with the necessary AWS permissions could be executing code remotely on an EC2 instance via SSM and saving the output to their own S3 bucket. Verify this action with the user identity and confirm it was authorized.

MITRE ATT&CK coverage

TacticTechniques
ExecutionT1651 Cloud Administration Command

Rules detecting the same action

Other rules on this platform that filter on the same API call or operation.

Rule body kusto

id: 21702832-aff3-4bd6-a8e1-663b6818503d
name: AWSCloudTrail - Suspicious command sent to EC2
description: |
  An attacker with the necessary AWS permissions could be executing code remotely on an EC2 instance via SSM and saving the output to their own S3 bucket. Verify this action with the user identity and confirm it was authorized.
severity: High
status: Available
requiredDataConnectors:
  - connectorId: AWS
    dataTypes:
      - AWSCloudTrail
queryFrequency: 1d
queryPeriod: 1d
triggerOperator: gt
triggerThreshold: 0
tactics:
  - Execution
relevantTechniques:
  - T1651
query: |
    let command_executed = AWSCloudTrail
    | where EventName in ("SendCommand","CreateAssociation") and isempty(ErrorCode) and isempty(ErrorMessage)
    | extend params = tostring(parse_json(RequestParameters).parameters)
    | extend s3bucketCommand = tostring(parse_json(RequestParameters).outputS3BucketName)
    | extend s3bucketAssociation = tostring(parse_json(RequestParameters).outputLocation.s3Location.outputS3BucketName)
    | where isnotempty(params)
    | extend commandId = tostring(parse_json(ResponseElements).command.commandId)
    | extend associationId = tostring(parse_json(ResponseElements).associationDescription.associationId)
    | extend executionId = iff(isnotempty(commandId), commandId, associationId)
    | extend s3bucket = iff(isnotempty(s3bucketCommand), s3bucketCommand, s3bucketAssociation)
    | extend UserIdentityUserName = iff(isnotempty(UserIdentityUserName), UserIdentityUserName, tostring(split(UserIdentityArn,'/')[-1]));
    AWSCloudTrail
    | where EventName == "PutObject" and isempty(ErrorCode) and isempty(ErrorMessage)
    | extend s3bucket = tostring(parse_json(RequestParameters).bucketName)
    | mv-expand todynamic(Resources)
    | extend accountId=tostring(todynamic(Resources.['accountId']))
    | where Resources contains "accountId" and accountId <> RecipientAccountId
    | join command_executed on s3bucket
    | extend UserIdentityArn = iif(isempty(UserIdentityArn), tostring(parse_json(Resources)[0].ARN), UserIdentityArn)
    | extend UserName = tostring(split(UserIdentityArn, '/')[-1])
    | extend AccountName = case( UserIdentityPrincipalid == "Anonymous", "Anonymous", isempty(UserIdentityUserName), UserName, UserIdentityUserName)
    | extend AccountName = iif(AccountName contains "@", tostring(split(AccountName, '@', 0)[0]), AccountName),
      AccountUPNSuffix = iif(AccountName contains "@", tostring(split(AccountName, '@', 1)[0]), "")
entityMappings:
  - entityType: Account
    fieldMappings:
      - identifier: Name
        columnName: AccountName
      - identifier: UPNSuffix
        columnName: AccountUPNSuffix
      - identifier: CloudAppAccountId
        columnName: RecipientAccountId
  - entityType: IP
    fieldMappings:
      - identifier: Address
        columnName: SourceIpAddress
customDetails:
  S3Bucket: s3bucket
  ExecutionId: executionId
  AWSRegion: AWSRegion
alertDetailsOverride:
  alertDisplayNameFormat: Suspicious EC2 command execution by {{AccountName}} with output to S3 {{s3bucket}}
  alertDescriptionFormat: User {{AccountName}} sent a remote command to EC2 (execution ID {{executionId}}) with output directed to external S3 bucket {{s3bucket}}.
version: 1.0.2
kind: Scheduled

Stages and Predicates

Let binding: command_executed

let command_executed = AWSCloudTrail
| where EventName in ("SendCommand","CreateAssociation") and isempty(ErrorCode) and isempty(ErrorMessage)
| extend params = tostring(parse_json(RequestParameters).parameters)
| extend s3bucketCommand = tostring(parse_json(RequestParameters).outputS3BucketName)
| extend s3bucketAssociation = tostring(parse_json(RequestParameters).outputLocation.s3Location.outputS3BucketName)
| where isnotempty(params)
| extend commandId = tostring(parse_json(ResponseElements).command.commandId)
| extend associationId = tostring(parse_json(ResponseElements).associationDescription.associationId)
| extend executionId = iff(isnotempty(commandId), commandId, associationId)
| extend s3bucket = iff(isnotempty(s3bucketCommand), s3bucketCommand, s3bucketAssociation)
| extend UserIdentityUserName = iff(isnotempty(UserIdentityUserName), UserIdentityUserName, tostring(split(UserIdentityArn,'/')[-1]));

Stage 1: source

AWSCloudTrail

Stage 2: where

| where EventName == "PutObject" and isempty(ErrorCode) and isempty(ErrorMessage)

Stage 3: extend

| extend s3bucket = tostring(parse_json(RequestParameters).bucketName)

Stage 4: mv-expand

| mv-expand todynamic(Resources)

Stage 5: extend

| extend accountId=tostring(todynamic(Resources.['accountId']))

Stage 6: where

| where Resources contains "accountId" and accountId <> RecipientAccountId

Stage 7: join

| join command_executed on s3bucket

Stage 8: extend (4 consecutive steps)

| extend UserIdentityArn = iif(isempty(UserIdentityArn), tostring(parse_json(Resources)[0].ARN), UserIdentityArn)
| extend UserName = tostring(split(UserIdentityArn, '/')[-1])
| extend AccountName = case( UserIdentityPrincipalid == "Anonymous", "Anonymous", isempty(UserIdentityUserName), UserName, UserIdentityUserName)
| extend AccountName = iif(AccountName contains "@", tostring(split(AccountName, '@', 0)[0]), AccountName),
  AccountUPNSuffix = iif(AccountName contains "@", tostring(split(AccountName, '@', 1)[0]), "")

Indicators

Each row is a field, operator, and value that the rule matches. The corpus column counts how many other rules in the catalog look for the same combination: high numbers point to widely-used, community-vetted indicators. Blank or 1 shows that the indicator is specific to this rule.

FieldKindValues
ErrorCodeis_null
  • (no value, null check)
ErrorMessageis_null
  • (no value, null check)
EventNameeq
  • PutObject transforms: cased
EventNamein
  • CreateAssociation transforms: cased
  • SendCommand transforms: cased
Resourcescontains
  • accountId
accountIdne
  • RecipientAccountId transforms: cased
paramsis_not_null
  • (no value, null check)

Output fields

Fields the rule emits when it matches. Chronicle authors list these in the outcome block; they appear on the detection and $risk_score drives alerting. Sentinel / Defender XDR rules build them up through project / summarize / extend stages. Sentinel maps these into alert fields via entityMappings and customDetails; Defender XDR custom detections surface them as alert fields directly.

FieldSource
s3bucketextend
accountIdextend
UserIdentityArnextend
UserNameextend
AccountNameextend
AccountUPNSuffixextend