Skip to content

How do I find an IP address that's in my logs but not in my Amazon VPC?

9 minute read
0

I want to find an IP address that's in my logs but isn't in my Amazon Virtual Private Cloud (Amazon VPC).

Resolution

Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.

Review CloudTrail events

To use AWS CloudTrail events to find your IP address, complete the following steps:

To use the AllocateAddress event to check if you allocated the Elastic IP address to your AWS account, run the following lookup-events command:

aws cloudtrail lookup-events \
    --region example-region \
    --lookup-attributes AttributeKey=EventName,AttributeValue=AllocateAddress \
    --start-time example-yyyy-mm-dd \
    --query 'Events[].Resources[?ResourceName == 'example-eip-address'].{ResourceType:ResourceType,IP:ResourceName}[]' \
    --output table

Note: Replace example-eip-address with your Elastic IP address, example-yyyy-mm-dd with your start date, and example-region with your AWS Region. The preceding CloudTrail command doesn't list Amazon Elastic Compute Cloud (Amazon EC2) public IPv4 addresses.

To use the AssociateAddress event and the AllocationID filters to view the service that uses the Elastic IP address, run the following lookup-events command:

aws cloudtrail lookup-events \
    --region example-region \
    --lookup-attributes AttributeKey=ResourceName,AttributeValue=example-allocation-id \
    --start-time example-yyyy-mm-dd

Note: Replace example-allocation-id with your Elastic IP address ID, example-yyyy-mm-dd with your start date, and example-region with your Region. The preceding command shows the full history of the Elastic IP address allocation. It starts from latest to earliest allocation based on the date that you select.

To use the CreateNetworkInterface event to check if you assigned the private IP address to the elastic network interface, run the following lookup-events command:

aws cloudtrail lookup-events \
    --region example-region \
    --lookup-attributes AttributeKey=EventName,AttributeValue=CreateNetworkInterface \
    --start-time example-yyyy-mm-dd \
    --query 'Events[].CloudTrailEvent' --output text | jq -r "select(.responseElements.networkInterface.privateIpAddressesSet.item[].privateIpAddress == \"example-private-ip-address\")"

Note: Replace example-private-ip-address with your private IP address, example-yyyy-mm-dd with your start date, and example-region with your Region. If the preceding command doesn't return matching events, then you don't get an output.

To use the AttachNetworkInterface event to check if a resource used a network interface in the past, run the following lookup-events command:

aws cloudtrail lookup-events \
    --region example-region \
    --lookup-attributes AttributeKey=EventName,AttributeValue=AttachNetworkInterface \
    --start-time example-yyyy-mm-dd \
    --query 'Events[].CloudTrailEvent' --output text | jq -r "select(.requestParameters.networkInterfaceId == \"example-eni-id\")"

Note: Replace example-eni-id with your network interface ID, example-yyyy-mm-dd with your start date, and example-region with your Region.

To check if CloudTrail can find the events, run the following lookup-events command without the jq filter:

aws cloudtrail lookup-events \
    --region example-region \
    --lookup-attributes AttributeKey=EventName,AttributeValue=CreateNetworkInterface \
    --start-time example-yyyy-mm-dd

Note: Replace example-yyyy-mm-dd with your start date and example-region with your Region.

To use the RunInstances event to check if you associated the private IP address with the instance at instance launch, run the following lookup-events command:

aws cloudtrail lookup-events \
    --region example-region \
    --lookup-attributes AttributeKey=EventName,AttributeValue=RunInstances \
    --start-time example-yyyy-mm-dd \
    --query 'Events[].CloudTrailEvent' --output text | jq -r "select(.responseElements.instancesSet.items[].privateIpAddress == \"example-private-ip-address\") | [.responseElements.instancesSet.items[].networkInterfaceSet.items[]] "

Note: Replace example-private-ip-address with your private IP address, example-yyyy-mm-dd with your start date, and example-region with your Region.

To use the AssignPrivateIpAddresses event to check if you associated the private IP address with the network interface, run the following lookup-events command:

aws cloudtrail lookup-events \
    --region example-region \
    --lookup-attributes AttributeKey=EventName,AttributeValue=AssignPrivateIpAddresses \
    --start-time example-yyyy-mm-dd \
    --query 'Events[].CloudTrailEvent' --output text | jq -r "select(.responseElements.assignedPrivateIpAddressesSet.assignedPrivateIpAddressSetType[].privateIpAddress == \"example-private-ip-address\" ) | {requestParameters,responseElements}"

Note: Replace example-private-ip-address with your private IP address, example-yyyy-mm-dd with your start date, and example-region with your Region.

To use the AssignIpv6Addresses event to check if you assigned the IPv6 address to the specified network interface, run the following lookup-events command:

aws cloudtrail lookup-events \
    --region example-region \
    --lookup-attributes AttributeKey=EventName,AttributeValue=AssignIpv6Addresses \
    --start-time example-yyyy-mm-dd \
    --query 'Events[].CloudTrailEvent' --output text | jq -r "select(.responseElements.AssignIpv6AddressesResponse.assignedIpv6Addresses.item == \"example-IPv6-address\") | [.responseElements.AssignIpv6AddressesResponse] "

Note: Replace example-IPv6-address with your IPv6 address, example-yyyy-mm-dd with your start date, and example-region with your Region.

Use CloudWatch Logs to analyze your log data

Note: To send log events to Amazon CloudWatch Logs, configure your trail. For more information, see Monitoring CloudTrail log files with Amazon CloudWatch Logs.

Check for IPv4 addresses assigned to your account

To view the past Elastic IP address allocation and the service that used the public IP address, use the following events:

fields      eventTime as Time,
     userIdentity.accountId as AccountID,
     userIdentity.principalId as Principal,
     awsRegion as Region,
     eventName as Action,
     requestParameters.allocationId as AssociateAddress_AllocationID,
     requestParameters.instanceId as InstanceID,
     requestParameters.privateIpAddress as PrivateIP,
     responseElements.allocationId as AllocateAddress_AllocationID,
     responseElements.publicIp as EIP
| filter
    
     eventName = "AllocateAddress" or
     eventName = "AssociateAddress" and
     # AssociateAddress does not indicate resources outside EC2 instances.
     (
          EIP = "example-public-ip-addresses" or
          AssociateAddress_AllocationID = "example-allocation-id"
     )
| sort @timestamp desc

Note: Replace example-public-ip-addresses with your public IP addresses and example-allocation-id with your allocation ID.

To find the previous private IP address assignments, use the following events:

CreateNetworkInterface event:

parse @message '"privateIpAddressesSet":{"item":[{"privateIpAddress":"*"' as PrivateIP| fields
eventTime as Time,
userIdentity.accountId as AccountID,
userIdentity.principalId as Principal,
awsRegion as Region,
eventName as Action,
responseElements.networkInterface.networkInterfaceId as ENI
| filter
eventName = "CreateNetworkInterface" and
(
PrivateIP = "example-private-ip-addresses" 
)
| sort @timestamp desc

Note: Replace example-private-ip-addresses with your IP addresses.

RunInstances event:

parse @message '{"privateIpAddress":"*"' as PrivateIP|fields 
     eventTime as Time,
     userIdentity.accountId as AccountID,
     userIdentity.principalId as Principal,
     awsRegion as Region,
     eventName as Action
| filter 
     eventName = "RunInstances" and
     (
          PrivateIP = "example-private-ip-addresses" 
     )
| sort @timestamp desc

Note: Replace example-private-ip-addresses with your IP addresses.

AssignPrivateIpAddress event:

parse @message '"assignedPrivateIpAddressSetType":[{*}]' as PrivateIpAddress| fields 
     eventTime as Time,
     userIdentity.accountId as AccountID,
     userIdentity.principalId as Principal,
     awsRegion as Region,
     eventName as Action
| filter 
     eventName = "AssignPrivateIpAddresses" and
     (
          PrivateIpAddress like "example-private-ip-addresses" 
     )
| sort @timestamp desc

Note: Replace example-private-ip-addresses with your IP addresses.

View the resources that used the network interface:

fields      eventTime as Time,
     userIdentity.accountId as AccountID,
     userIdentity.principalId as Principal,
     awsRegion as Region,
     eventName as Action,
     requestParameters.instanceId as InstanceID,
     requestParameters.networkInterfaceId as ENI
| filter 
     eventName = "AttachNetworkInterface" and
     (
          ENI = "example-eni-id" 
     )
| sort @timestamp desc

Note: Replace example-eni-id with your network interface ID.

Check for IPv6 addresses assigned to your account

To find the previous private IP address assignments, use the following events:

AssignIpv6Addresses event:

fields    eventTime as Time,
    userIdentity.accountId as AccountID,
    userIdentity.principalId as Principal,
    awsRegion as Region,
    eventName as Action,
    responseElements.AssignIpv6AddressesResponse.assignedIpv6Addresses.item as IPv6
| filter
    eventName = "AssignIpv6Addresses" and
    (
        IPv6 = "example-ipv6-addresses"
    )
| sort @timestamp desc

Note: Replace example-ipv6-addresses with your IP addresses.

RunInstances event:

fields    eventTime as Time,
    userIdentity.accountId as AccountID,
    userIdentity.principalId as Principal,
    awsRegion as Region,
    eventName as Action,
    responseElements.AssignIpv6AddressesResponse.assignedIpv6Addresses.item as IPv6
| filter
    eventName = "RunInstances" and
    (
        PrivateIP = "example-private-ip-addresses"
    )
| sort @timestamp desc

Note: Replace example-private-ip-addresses with your IP addresses.

CreateNetworkInterface event:

parse @message '"ipv6AddressesSet":{"items":[{"ipv6Address":"*"' as IPv6| fields 
     eventTime as Time,
     userIdentity.accountId as AccountID,
     userIdentity.principalId as Principal,
     awsRegion as Region,
     eventName as Action,
     responseElements.instancesSet.items.0.instanceId as InstanceID
| filter 
     eventName = "CreateNetworkInterface" and
     (
         IPv6 = "example-ipv6-addresses"
     )
| sort @timestamp desc

Note: Replace example-ipv6-addresses with your IP addresses.

View the resources that used the network interface:

fields      eventTime as Time,
     userIdentity.accountId as AccountID,
     userIdentity.principalId as Principal,
     awsRegion as Region,
     eventName as Action,
     requestParameters.instanceId as InstanceID,
     requestParameters.networkInterfaceId as ENI
| filter 
     eventName = "AttachNetworkInterface" and
     (
         ENI = "example-eni-id"
     )
| sort @timestamp desc

Note: Replace example-eni-id with your network interface ID.

(Optional) To check if the BGP prefix and ASN of the public IP address is an AWS IP address, run the following whois command:

whois -h whois.cymru.com " -v example-public-ip-address"

Note: Replace example-public-ip-address with your public IP address.

Review IP address history

You can use an ip-ranges.json file, Amazon VPC IP Address Manager (IPAM), or the AWS CLI to find an IP address in your IP address history.

ip-ranges.json

Complete the following steps:

  1. To view a list of all public IP addresses that belong to an AWS service, check the ip-ranges.json file.
  2. To filter the list, use the following variables:
    "ip_prefix"
    "region"
    "service"

IPAM

To use IPAM to track the VPCs or resources that used a specific IP address over time, complete the following steps:

  1. Open the IPAM console.
  2. In the navigation pane, choose Search IP history.
  3. Enter your IP address.
  4. Choose Search.

The search results show the following items:

  • VPC or network interface associated with the IP address
  • Time range when the IP address was assigned
  • Related resources

AWS CLI

To use the AWS CLI to get the IP address information, run the following get-ipam-address-history command:

aws ec2 get-ipam-address-history \
    --ipam-scope-id ipam-scope-###### \
    --cidr 10.0.1.5 \
    --region us-east-1

Note: If you use AWS Organizations, then you can extend your search for IP addresses at the organization level to centrally track multiple member accounts.

Designate an IPAM administrator account

Complete the following steps:

  1. In your organization, choose an AWS account to be the management account.
  2. To make your account ID the IPAM administrator for your organization, run the following enable-aws-service-access command in your management account:
    aws organizations enable-aws-service-access \
        --service-principal ipam.amazonaws.com
    Then, run the following enable-ipam-organization-admin-account command:
    aws ec2 enable-ipam-organization-admin-account \
        --delegated-admin-account-id YOUR_ACCOUNT_ID

Note: Replace YOUR_ACCOUNT_ID with your account ID.

Create an IPAM in the administrator account

Complete the following steps in your IPAM administrator account:

  1. To create an IPAM, run the following create-ipam command:

    aws ec2 create-ipam \
        --description "Org-wide IPAM" \
        --operating-regions RegionName=us-east-1 RegionName=us-west-2

    Note: Replace us-east-1 and us-west-2 with your Regions.

    This IPAM can now collect and manage CIDR usage across multiple accounts.

  2. To create a resource discovery, run the following create-ipam-resource-discovery command:

    aws ec2 create-ipam-resource-discovery \
        --description "Org-wide Discovery" \
        --operating-regions RegionName=us-east-1 \
        --tag-specifications ResourceType=ipam-resource-discovery,Tags=[{Key=Org,Value=Enabled}]

    Note: Replace us-east-1 with your Region.

  3. To associate resource discovery with IPAM, run the following associate-ipam-resource-discovery command:

    aws ec2 associate-ipam-resource-discovery \
        --ipam-id ipam-######## \
        --ipam-resource-discovery-id ipam-res-disco-####

You can share a resource discovery with another account.

Note: Each member account in your organization must share resource data with the IPAM. If you use the same organization and you set up centralized discovery, then resource data is automatically shared. Use the IPAM dashboard to monitor discovered resources, such as VPCs and IP addresses.

Important:

  • All IP addresses that you allocate in any account or VPC in the organization appear in the IPAM's IP address history.
  • You can use the IPAM console or AWS CLI to query for past usage of an IP address.
  • If the IP address in the IPAM history is from AWS and there's malicious activity, then report abuses of AWS resources.
AWS OFFICIALUpdated a year ago