跳至內容

如何找到出現在我的日誌中,但不在 Amazon VPC 中的 IP 位址?

6 分的閱讀內容
0

我想找到一個出現在我的日誌中,但不在 Amazon Virtual Private Cloud (Amazon VPC) 中的 IP 位址。

解決方法

**注意:**如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤訊息,請參閱對 AWS CLI 錯誤進行疑難排解。此外,請確定您使用的是最新的 AWS CLI 版本

檢視 CloudTrail 事件

若要使用 AWS CloudTrail 事件來尋找您的 IP 位址,請完成以下步驟:

若要使用 AllocateAddress 事件來檢查您是否已將彈性 IP 位址分配給您的 AWS 帳戶,請執行以下 lookup-events 命令:

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

**注意:**將 example-eip-address 替換為您的彈性 IP 位址,將 example-yyyy-mm-dd 替換為開始日期,並將 example-region 替換為您的 AWS 區域。上述 CloudTrail 命令不會列出 Amazon Elastic Compute Cloud (Amazon EC2) 公有 IPv4 位址。

若要使用 AssociateAddress 事件及 AllocationID 篩選條件來查看使用該彈性 IP 位址的服務,請執行以下 lookup-events 命令:

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

**注意:**將 example-allocation-id 替換為您的彈性 IP 位址 ID,將 example-yyyy-mm-dd 替換為開始日期,並將 example-region 替換為您的區域。上述命令會顯示彈性 IP 位址分配的完整歷史記錄。它會依您選取的日期,從最新到最早的分配順序顯示。

若要使用 CreateNetworkInterface 事件來檢查您是否將私有 IP 位址指派給彈性網路介面,請執行以下 lookup-events 命令:

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\")"

**注意:**將 example-private-ip-address 替換為您的私有 IP 位址,將 example-yyyy-mm-dd 替換為開始日期,並將 example-region 替換為您的區域。如果上述命令沒有傳回符合的事件,則不會有輸出結果。

若要使用 AttachNetworkInterface 事件來檢查資源是否曾使用過網路介面,請執行以下 lookup-events 命令:

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\")"

**注意:**將 example-eni-id 替換為您的網路介面 ID,將 example-yyyy-mm-dd 替換為開始日期,並將 example-region 替換為您的區域。

若要檢查 CloudTrail 是否能找到事件,請在不使用 jq 篩選器的情況下執行以下 lookup-events 命令:

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

**注意:**將 example-yyyy-mm-dd 替換為開始日期,並將 example-region 替換為您的區域。

若要使用 RunInstances 事件來檢查您是否在執行個體啟動時,將私有 IP 位址與該執行個體建立關聯,請執行以下 lookup-events 命令:

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[]] "

**注意:**將 example-private-ip-address 替換為您的私有 IP 位址,將 example-yyyy-mm-dd 替換為開始日期,並將 example-region 替換為您的區域。

若要使用 AssignPrivateIpAddresses 事件來檢查您是否將私有 IP 位址與網路介面建立關聯,請執行以下 lookup-events 命令:

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}"

**注意:**將 example-private-ip-address 替換為您的私有 IP 位址,將 example-yyyy-mm-dd 替換為開始日期,並將 example-region 替換為您的區域。

若要使用 AssignIpv6Addresses 事件來檢查您是否將 IPv6 位址指派給指定的網路介面,請執行以下 lookup-events 命令:

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] "

**注意:**將 example-IPv6-address 替換為您的 IPv6 位址,將 example-yyyy-mm-dd 替換為開始日期,並將 example-region 替換為您的區域。

使用 CloudWatch Logs 分析您的日誌資料

**注意:**若要將日誌事件傳送至 Amazon CloudWatch Logs,請設定您的追蹤。如需更多資訊,請參閱使用 Amazon CloudWatch Logs 監控 CloudTrail 日誌檔案

檢查指派至您帳戶的 IPv4 位址

若要查看過去彈性 IP 位址的分配情況以及使用公有 IP 位址的服務,請使用以下事件:

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

**注意:**將 example-public-ip-addresses 替換為您的公有 IP 位址,並將 example-allocation-id 替換為您的分配 ID。

若要尋找先前的私有 IP 位址指派,請使用以下事件:

CreateNetworkInterface 事件:

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

**注意:**將 example-private-ip-addresses 替換為您的 IP 位址。

RunInstances 事件:

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

**注意:**將 example-private-ip-addresses 替換為您的 IP 位址。

AssignPrivateIpAddress 事件:

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

**注意:**將 example-private-ip-addresses 替換為您的 IP 位址。

檢視使用該網路介面的資源:

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

**注意:**將 example-eni-id 替換為您的網路介面 ID。

檢查指派至您帳戶的 IPv6 位址

若要尋找先前的私有 IP 位址指派,請使用以下事件:

AssignIpv6Addresses 事件:

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

**注意:**將 example-ipv6-addresses 替換為您的 IP 位址。

RunInstances 事件:

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

**注意:**將 example-private-ip-addresses 替換為您的 IP 位址。

CreateNetworkInterface 事件:

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

**注意:**將 example-ipv6-addresses 替換為您的 IP 位址。

檢視使用該網路介面的資源:

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" or
     )
| sort @timestamp desc

**注意:**將 example-eni-id 替換為您的網路介面 ID。

(選用) 若要檢查公有 IP 位址的 BGP 首碼ASN 是否為 AWS IP 位址,請執行以下 whois 命令:

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

**注意:**將 example-public-ip-address 替換為您的公有 IP 位址。

檢視 IP 位址歷史記錄

您可以使用 ip-ranges.json 檔案、Amazon VPC IP 位址管理器 (IPAM) 或 AWS CLI,來尋找 IP 位址歷史記錄中的 IP 位址。

ip-ranges.json

請完成下列步驟:

  1. 若要查看屬於 AWS 服務的所有公有 IP 位址清單,請檢查 ip-ranges.json 檔案。
  2. 若要篩選清單,請使用以下變數:
    "ip_prefix"
    "region"
    "service"

IPAM

若要使用 IPAM 追蹤一段時間內使用特定 IP 位址的 VPC 或資源,請完成以下步驟:

  1. 開啟 IPAM 主控台
  2. 在導覽窗格中,選擇 Search IP history (搜尋 IP 歷史記錄)。
  3. 輸入您的 IP 位址。
  4. 選擇 Search (搜尋)。

搜尋結果會顯示以下項目:

  • 與 IP 位址關聯的 VPC 或網路介面
  • 指派 IP 位址的時間範圍
  • 相關資源

AWS CLI

若要使用 AWS CLI 取得 IP 位址資訊,請執行以下 get-ipam-address-history 命令:

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

**注意:**如果您使用 AWS Organizations,則可以在組織層級擴展 IP 位址搜尋,以集中追蹤多個會員帳戶。

指定 IPAM 管理員帳戶

請完成下列步驟:

  1. 在您的組織中,選擇一個 AWS 帳戶作為 管理帳戶
  2. 若要將您的帳戶 ID 設定為組織的 IPAM 管理員,請在管理帳戶中執行以下 enable-aws-service-access 命令:
    aws organizations enable-aws-service-access \
     --service-principal ipam.amazonaws.comaws ec2 enable-ipam-organization-admin-account \
     --delegated-admin-account-id YOUR_ACCOUNT_ID

**注意:**將 YOUR_ACCOUNT_ID 替換為您的帳戶 ID。

在管理員帳戶中建立 IPAM

在您的 IPAM 管理員帳戶中完成以下步驟:

  1. 若要建立 IPAM,請執行以下 create-ipam 命令:

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

    **注意:**將 us-east-1us-west-2 替換為您的區域。

    此 IPAM 現在可以收集並管理跨多個帳戶的 CIDR 使用情況。

  2. 若要建立資源探索,請執行以下 create-ipam-resource-discovery 命令:

    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}]

    **注意:**將 us-east-1 替換為您的區域。

  3. 若要將資源探索與 IPAM 建立關聯,請執行以下 associate-ipam-resource-discovery 命令:

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

您可以與其他帳戶共用資源探索

**注意:**您組織中的每個會員帳戶都必須與 IPAM 共用資源資料。如果您使用相同的組織並設定了集中式探索,則資源資料會自動共用。使用 IPAM 儀表板監控已探索的資源,例如 VPC 和 IP 位址。

重要:

  • 組織中任何帳戶或 VPC 分配的所有 IP 位址都會出現在 IPAM 的 IP 位址歷史記錄中。
  • 您可以使用 IPAM 主控台或 AWS CLI 查詢 IP 位址的過去使用情況。
  • 如果 IPAM 歷史記錄中的 IP 位址來自 AWS 且存在惡意活動,請回報 AWS 資源濫用行為
AWS 官方已更新 3 個月前