AWS EC2 Instance Connect SSH Public Key Uploaded

Identifies when a new SSH public key is uploaded to an AWS EC2 instance using the EC2 Instance Connect service. This action could indicate an adversary attempting to maintain access to the instance. The rule also detects the SendSerialConsoleSSHPublicKey API action, which could be used for privilege escalation if the serial console is enabled. Monitoring these activities helps ensure unauthorized access attempts are detected and mitigated promptly.

Elastic rule (View on GitHub)

  1[metadata]
  2creation_date = "2024/04/30"
  3integration = ["aws"]
  4maturity = "production"
  5min_stack_comments = "AWS integration breaking changes, bumping version to ^2.0.0"
  6min_stack_version = "8.9.0"
  7updated_date = "2024/06/03"
  8
  9[rule]
 10author = ["Elastic"]
 11description = """
 12Identifies when a new SSH public key is uploaded to an AWS EC2 instance using the EC2 Instance Connect service. This
 13action could indicate an adversary attempting to maintain access to the instance. The rule also detects the
 14`SendSerialConsoleSSHPublicKey` API action, which could be used for privilege escalation if the serial console is
 15enabled. Monitoring these activities helps ensure unauthorized access attempts are detected and mitigated promptly.
 16"""
 17false_positives = ["Administrators may upload SSH public keys to EC2 instances for legitimate purposes."]
 18from = "now-9m"
 19index = ["filebeat-*", "logs-aws.cloudtrail-*"]
 20language = "kuery"
 21license = "Elastic License v2"
 22name = "AWS EC2 Instance Connect SSH Public Key Uploaded"
 23note = """
 24## Triage and Analysis
 25
 26### Investigating AWS EC2 Instance Connect SSH Public Key Uploaded
 27
 28This rule detects when a new SSH public key is uploaded to an AWS EC2 instance using the EC2 Instance Connect service. Adversaries may upload SSH public keys to EC2 instances to maintain access to the instance. The rule also covers cases where the `SendSerialConsoleSSHPublicKey` API action is used to upload an SSH public key to a serial connection, which can be exploited for privilege escalation.
 29
 30#### Possible Investigation Steps:
 31
 32- **Identify the Actor**: Review the `aws.cloudtrail.user_identity.arn` and `aws.cloudtrail.user_identity.access_key_id` fields to identify who performed the action. Verify if this actor typically performs such actions and if they have the necessary permissions.
 33- **Review the Request Details**: Examine the `aws.cloudtrail.request_parameters` to understand the specific details of the SSH public key upload. Look for any unusual parameters that could suggest unauthorized or malicious modifications.
 34- **Analyze the Source of the Request**: Investigate the `source.ip` and `source.geo` fields to determine the geographical origin of the request. An external or unexpected location might indicate compromised credentials or unauthorized access.
 35- **Contextualize with Timestamp**: Use the `@timestamp` field to check when the SSH public key was uploaded. Changes during non-business hours or outside regular maintenance windows might require further scrutiny.
 36- **Correlate with Other Activities**: Search for related CloudTrail events before and after this action to see if the same actor or IP address engaged in other potentially suspicious activities.
 37- **Check for Serial Console Access**: If the `SendSerialConsoleSSHPublicKey` action was used, verify if the `ec2:EnableSerialConsoleAccess` permission was also used, which might indicate an attempt to enable and exploit the serial console.
 38
 39### False Positive Analysis:
 40
 41- **Legitimate Administrative Actions**: Confirm if the SSH public key upload aligns with scheduled updates, development activities, or legitimate administrative tasks documented in change management systems.
 42- **Consistency Check**: Compare the action against historical data of similar actions performed by the user or within the organization. If the action is consistent with past legitimate activities, it might indicate a false alarm.
 43- **Verify through Outcomes**: Check the `aws.cloudtrail.response_elements` and the `event.outcome` to confirm if the upload was successful and intended according to policy.
 44
 45### Response and Remediation:
 46
 47- **Immediate Review and Reversal if Necessary**: If the upload was unauthorized, remove the uploaded SSH public key from the EC2 instance and review the instance's access logs for any suspicious activity.
 48- **Enhance Monitoring and Alerts**: Adjust monitoring systems to alert on similar actions, especially those involving sensitive instances or unusual file extensions.
 49- **Educate and Train**: Provide additional training to users with administrative rights on the importance of security best practices concerning SSH key management and the risks of unauthorized key uploads.
 50- **Audit EC2 Instance Policies and Permissions**: Conduct a comprehensive audit of all EC2 instance policies and associated permissions to ensure they adhere to the principle of least privilege.
 51- **Incident Response**: If there's an indication of malicious intent or a security breach, initiate the incident response protocol to mitigate any damage and prevent future occurrences.
 52
 53### Additional Information:
 54
 55For further guidance on managing EC2 instances and securing AWS environments, refer to the [AWS EC2 Instance Connect documentation](https://docs.aws.amazon.com/ec2-instance-connect/latest/APIReference/API_SendSSHPublicKey.html) and AWS best practices for security. Additionally, consult the following resources for specific details on SSH key management and privilege escalation techniques:
 56- [Stratus Red Team - AWS EC2 Instance Connect](https://stratus-red-team.cloud/attack-techniques/AWS/aws.lateral-movement.ec2-instance-connect/)
 57- [HackTricks - AWS EC2 Privilege Escalation](https://cloud.hacktricks.xyz/pentesting-cloud/aws-security/aws-privilege-escalation/aws-ec2-privesc)
 58- [AWS EC2 Instance Connect API Reference](https://docs.aws.amazon.com/ec2-instance-connect/latest/APIReference/API_SendSSHPublicKey.html)
 59"""
 60references = [
 61    "https://stratus-red-team.cloud/attack-techniques/AWS/aws.lateral-movement.ec2-instance-connect/",
 62    "https://cloud.hacktricks.xyz/pentesting-cloud/aws-security/aws-privilege-escalation/aws-ec2-privesc",
 63    "https://medium.parttimepolymath.net/aws-ec2-instance-connect-a-very-neat-trick-4d2fc0c28010",
 64    "https://docs.aws.amazon.com/ec2-instance-connect/latest/APIReference/API_SendSSHPublicKey.html",
 65    "https://docs.aws.amazon.com/ec2-instance-connect/latest/APIReference/API_SendSerialConsoleSSHPublicKey.html",
 66]
 67risk_score = 47
 68rule_id = "873b5452-074e-11ef-852e-f661ea17fbcc"
 69severity = "medium"
 70tags = [
 71    "Domain: Cloud",
 72    "Data Source: AWS",
 73    "Data Source: Amazon Web Services",
 74    "Data Source: AWS EC2",
 75    "Use Case: Identity and Access Audit",
 76    "Tactic: Privilege Escalation",
 77]
 78timestamp_override = "event.ingested"
 79type = "query"
 80
 81query = '''
 82event.dataset: aws.cloudtrail
 83    and event.provider: ec2-instance-connect.amazonaws.com
 84    and event.action: (SendSSHPublicKey or SendSerialConsoleSSHPublicKey)
 85    and event.outcome: success
 86'''
 87
 88
 89[[rule.threat]]
 90framework = "MITRE ATT&CK"
 91[[rule.threat.technique]]
 92id = "T1098"
 93name = "Account Manipulation"
 94reference = "https://attack.mitre.org/techniques/T1098/"
 95[[rule.threat.technique.subtechnique]]
 96id = "T1098.004"
 97name = "SSH Authorized Keys"
 98reference = "https://attack.mitre.org/techniques/T1098/004/"
 99
100
101
102[rule.threat.tactic]
103id = "TA0004"
104name = "Privilege Escalation"
105reference = "https://attack.mitre.org/tactics/TA0004/"

Triage and Analysis

Investigating AWS EC2 Instance Connect SSH Public Key Uploaded

This rule detects when a new SSH public key is uploaded to an AWS EC2 instance using the EC2 Instance Connect service. Adversaries may upload SSH public keys to EC2 instances to maintain access to the instance. The rule also covers cases where the SendSerialConsoleSSHPublicKey API action is used to upload an SSH public key to a serial connection, which can be exploited for privilege escalation.

Possible Investigation Steps:

  • Identify the Actor: Review the aws.cloudtrail.user_identity.arn and aws.cloudtrail.user_identity.access_key_id fields to identify who performed the action. Verify if this actor typically performs such actions and if they have the necessary permissions.
  • Review the Request Details: Examine the aws.cloudtrail.request_parameters to understand the specific details of the SSH public key upload. Look for any unusual parameters that could suggest unauthorized or malicious modifications.
  • Analyze the Source of the Request: Investigate the source.ip and source.geo fields to determine the geographical origin of the request. An external or unexpected location might indicate compromised credentials or unauthorized access.
  • Contextualize with Timestamp: Use the @timestamp field to check when the SSH public key was uploaded. Changes during non-business hours or outside regular maintenance windows might require further scrutiny.
  • Correlate with Other Activities: Search for related CloudTrail events before and after this action to see if the same actor or IP address engaged in other potentially suspicious activities.
  • Check for Serial Console Access: If the SendSerialConsoleSSHPublicKey action was used, verify if the ec2:EnableSerialConsoleAccess permission was also used, which might indicate an attempt to enable and exploit the serial console.

False Positive Analysis:

  • Legitimate Administrative Actions: Confirm if the SSH public key upload aligns with scheduled updates, development activities, or legitimate administrative tasks documented in change management systems.
  • Consistency Check: Compare the action against historical data of similar actions performed by the user or within the organization. If the action is consistent with past legitimate activities, it might indicate a false alarm.
  • Verify through Outcomes: Check the aws.cloudtrail.response_elements and the event.outcome to confirm if the upload was successful and intended according to policy.

Response and Remediation:

  • Immediate Review and Reversal if Necessary: If the upload was unauthorized, remove the uploaded SSH public key from the EC2 instance and review the instance's access logs for any suspicious activity.
  • Enhance Monitoring and Alerts: Adjust monitoring systems to alert on similar actions, especially those involving sensitive instances or unusual file extensions.
  • Educate and Train: Provide additional training to users with administrative rights on the importance of security best practices concerning SSH key management and the risks of unauthorized key uploads.
  • Audit EC2 Instance Policies and Permissions: Conduct a comprehensive audit of all EC2 instance policies and associated permissions to ensure they adhere to the principle of least privilege.
  • Incident Response: If there's an indication of malicious intent or a security breach, initiate the incident response protocol to mitigate any damage and prevent future occurrences.

Additional Information:

For further guidance on managing EC2 instances and securing AWS environments, refer to the AWS EC2 Instance Connect documentation and AWS best practices for security. Additionally, consult the following resources for specific details on SSH key management and privilege escalation techniques:

References

Related rules

to-top