使用 的 GuardDuty 範例 AWS CLI - AWS SDK 程式碼範例

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 的 GuardDuty 範例 AWS CLI

下列程式碼範例示範如何使用 AWS Command Line Interface 搭配 GuardDuty 來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例示範如何使用 accept-invitation

AWS CLI

若要接受成為目前區域中 GuardDuty 成員帳戶的邀請

下列accept-invitation範例顯示如何接受邀請,以成為目前區域中的 GuardDuty 成員帳戶。

aws guardduty accept-invitation \ --detector-id 12abc34d567e8fa901bc2d34eexample \ --master-id 123456789111 \ --invitation-id d6b94fb03a66ff665f7db8764example

此命令不會產生輸出。

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的透過邀請管理 GuardDuty 帳戶。

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 AcceptInvitation

下列程式碼範例示範如何使用 archive-findings

AWS CLI

在目前區域中封存問題清單

archive-findings範例示範如何在目前區域中封存問題清單。

aws guardduty archive-findings \ --detector-id 12abc34d567e8fa901bc2d34eexample \ --finding-ids d6b94fb03a66ff665f7db8764example 3eb970e0de00c16ec14e6910fexample

此命令不會產生輸出。

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的建立禁止規則

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ArchiveFindings

下列程式碼範例示範如何使用 create-detector

AWS CLI

在目前區域中啟用 GuardDuty

此範例示範如何在目前區域中建立新的偵測器,以啟用 GuardDuty。:

aws guardduty create-detector \ --enable

輸出:

{ "DetectorId": "b6b992d6d2f48e64bc59180bfexample" }

如需詳細資訊,請參閱《Amazon GuardDuty》中的啟用 Amazon GuardDuty

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 CreateDetector

下列程式碼範例示範如何使用 create-filter

AWS CLI

範例 1:在目前區域中建立新的篩選條件

下列create-filter範例會建立符合從特定映像建立之執行個體的所有 Portscan 調查結果的篩選條件。這不會隱藏這些調查結果。

aws guardduty create-filter \ --detector-id b6b992d6d2f48e64bc59180bfexample \ --name myFilterExample \ --finding-criteria '{"Criterion": {"type": {"Eq": ["Recon:EC2/Portscan"]},"resource.instanceDetails.imageId": {"Eq": ["ami-0a7a207083example"]}}}'

輸出:

{ "Name": "myFilterExample" }

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的篩選 GuardDuty 調查結果GuardDuty

範例 2:在目前區域中建立新的篩選條件和隱藏調查結果

下列create-filter範例會建立符合從特定映像建立之執行個體的所有 Portscan 調查結果的篩選條件。此篩選條件會封存這些調查結果,使其不會出現在您目前的調查結果中。

aws guardduty create-filter \ --detector-id b6b992d6d2f48e64bc59180bfexample \ --action ARCHIVE \ --name myFilterSecondExample \ --finding-criteria '{"Criterion": {"type": {"Eq": ["Recon:EC2/Portscan"]},"resource.instanceDetails.imageId": {"Eq": ["ami-0a7a207083example"]}}}'

輸出:

{ "Name": "myFilterSecondExample" }

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的篩選 GuardDuty 調查結果GuardDuty

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 CreateFilter

下列程式碼範例示範如何使用 create-ip-set

AWS CLI

建立和啟用信任的 IP 組

下列create-ip-set範例會在目前區域中建立並啟用信任的 IP 組。

aws guardduty create-ip-set \ --detector-id 12abc34d567e8fa901bc2d34eexample \ --name new-ip-set-example \ --format TXT \ --location s3://amzn-s3-demo-bucket/customtrustlist.csv \ --activate

輸出:

{ "IpSetId": "d4b94fc952d6912b8f3060768example" }

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的使用信任 IP 清單和威脅清單

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 CreateIpSet

下列程式碼範例示範如何使用 create-members

AWS CLI

在目前區域中將新成員與您的 GuardDuty 主帳戶建立關聯。

此範例示範如何將要由目前帳戶管理的成員帳戶建立關聯,做為 GuardDuty 主帳戶。

aws guardduty create-members --detector-id b6b992d6d2f48e64bc59180bfexample \ --account-details AccountId=111122223333,Email=first+member@example.com AccountId=111111111111 ,Email=another+member@example.com

輸出:

{ "UnprocessedAccounts": [] }

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的管理多個帳戶

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 CreateMembers

下列程式碼範例示範如何使用 create-publishing-destination

AWS CLI

建立發佈目的地,以將目前區域中的 GuardDuty 問題清單匯出至其中。

下列create-publishing-destination範例示範如何設定發佈目的地以匯出目前 (未封存) GuardDuty 調查結果,以追蹤歷史調查結果資料。

aws guardduty create-publishing-destination \ --detector-id b6b992d6d2f48e64bc59180bfexample \ --destination-type S3 \ --destination-properties 'DestinationArn=arn:aws:s3:::amzn-s3-demo-bucket,KmsKeyArn=arn:aws:kms:us-west-1:111122223333:key/84cee9c5-dea1-401a-ab6d-e1de7example'

輸出:

{ "DestinationId": "46b99823849e1bbc242dfbe3cexample" }

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的將產生的 GuardDuty 調查結果匯出至 Amazon S3 儲存貯體。 GuardDuty

下列程式碼範例示範如何使用 create-sample-findings

AWS CLI

在目前區域中建立範例 GuardDuty 調查結果。

此範例示範如何建立所提供類型的範例調查結果。

aws guardduty create-sample-findings \ --detector-id b6b992d6d2f48e64bc59180bfexample \ --finding-types UnauthorizedAccess:EC2/TorClient UnauthorizedAccess:EC2/TorRelay

此命令不會產生輸出。

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的範例調查結果

下列程式碼範例示範如何使用 create-threat-intel-set

AWS CLI

建立和啟用新的威脅情報集

下列create-threat-intel-set範例會建立和啟用目前區域中的威脅情報集。

aws guardduty create-threat-intel-set \ --detector-id b6b992d6d2f48e64bc59180bfexample \ --name myThreatSet-example \ --format TXT \ --location s3://amzn-s3-demo-bucket/threatlist.csv \ --activate

輸出:

{ "ThreatIntelSetId": "20b9a4691aeb33506b808878cexample" }

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的使用信任 IP 清單和威脅清單

下列程式碼範例示範如何使用 decline-invitations

AWS CLI

拒絕邀請讓目前區域中的另一個帳戶管理 Guardduty。

此範例示範如何拒絕成員資格邀請。

aws guardduty decline-invitations \ --account-ids 111122223333

輸出:

{ "UnprocessedAccounts": [] }

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的透過邀請管理 GuardDuty 帳戶。

下列程式碼範例示範如何使用 delete-detector

AWS CLI

在目前區域中刪除偵測器並停用 GuardDuty。

此範例示範如何刪除偵測器,如果成功,則會在該偵測器相關聯的區域中停用 GuardDuty。

aws guardduty delete-detector \ --detector-id b6b992d6d2f48e64bc59180bfexample

此命令不會產生輸出。

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的暫停或停用 GuardDuty

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DeleteDetector

下列程式碼範例示範如何使用 delete-filter

AWS CLI

刪除目前區域中的現有篩選條件

此範例說明如何建立刪除篩選條件。

aws guardduty delete-filter \ --detector-id b6b992d6d2f48e64bc59180bfexample \ --filter-name byebyeFilter

此命令不會產生輸出。

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的篩選問題清單。

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DeleteFilter

下列程式碼範例示範如何使用 disable-organization-admin-account

AWS CLI

移除 帳戶做為組織中 GuardDuty 的委派管理員

此範例示範如何移除 帳戶做為 GuardDuty 的委派管理員。

aws guardduty disable-organization-admin-account \ --admin-account-id 111122223333

此命令不會產生輸出。

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的管理 AWS 組織帳戶

下列程式碼範例示範如何使用 disassociate-from-master-account

AWS CLI

取消與目前區域中目前管理員帳戶的關聯

下列disassociate-from-master-account範例會將您的帳戶與目前 AWS 區域中的目前 GuardDuty 管理員帳戶取消關聯。

aws guardduty disassociate-from-master-account \ --detector-id d4b040365221be2b54a6264dcexample

此命令不會產生輸出。

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的了解 GuardDuty 管理員帳戶與成員帳戶之間的關係GuardDuty

下列程式碼範例示範如何使用 get-detector

AWS CLI

擷取特定偵測器的詳細資訊

下列get-detector範例顯示指定偵測器的組態詳細資訊。

aws guardduty get-detector \ --detector-id 12abc34d567e8fa901bc2d34eexample

輸出:

{ "Status": "ENABLED", "ServiceRole": "arn:aws:iam::111122223333:role/aws-service-role/guardduty.amazonaws.com/AWSServiceRoleForAmazonGuardDuty", "Tags": {}, "FindingPublishingFrequency": "SIX_HOURS", "UpdatedAt": "2018-11-07T03:24:22.938Z", "CreatedAt": "2017-12-22T22:51:31.940Z" }

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的概念和術語

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 GetDetector

下列程式碼範例示範如何使用 get-findings

AWS CLI

範例 1:擷取特定調查結果的詳細資訊

下列get-findings範例會擷取指定問題清單的完整 JSON 問題清單詳細資訊。

aws guardduty get-findings \ --detector-id 12abc34d567e8fa901bc2d34eexample \ --finding-id 1ab92989eaf0e742df4a014d5example

輸出:

{ "Findings": [ { "Resource": { "ResourceType": "AccessKey", "AccessKeyDetails": { "UserName": "testuser", "UserType": "IAMUser", "PrincipalId": "AIDACKCEVSQ6C2EXAMPLE", "AccessKeyId": "ASIASZ4SI7REEEXAMPLE" } }, "Description": "APIs commonly used to discover the users, groups, policies and permissions in an account, was invoked by IAM principal testuser under unusual circumstances. Such activity is not typically seen from this principal.", "Service": { "Count": 5, "Archived": false, "ServiceName": "guardduty", "EventFirstSeen": "2020-05-26T22:02:24Z", "ResourceRole": "TARGET", "EventLastSeen": "2020-05-26T22:33:55Z", "DetectorId": "d4b040365221be2b54a6264dcexample", "Action": { "ActionType": "AWS_API_CALL", "AwsApiCallAction": { "RemoteIpDetails": { "GeoLocation": { "Lat": 51.5164, "Lon": -0.093 }, "City": { "CityName": "London" }, "IpAddressV4": "52.94.36.7", "Organization": { "Org": "Amazon.com", "Isp": "Amazon.com", "Asn": "16509", "AsnOrg": "AMAZON-02" }, "Country": { "CountryName": "United Kingdom" } }, "Api": "ListPolicyVersions", "ServiceName": "iam.amazonaws.com", "CallerType": "Remote IP" } } }, "Title": "Unusual user permission reconnaissance activity by testuser.", "Type": "Recon:IAMUser/UserPermissions", "Region": "us-east-1", "Partition": "aws", "Arn": "arn:aws:guardduty:us-east-1:111122223333:detector/d4b040365221be2b54a6264dcexample/finding/1ab92989eaf0e742df4a014d5example", "UpdatedAt": "2020-05-26T22:55:21.703Z", "SchemaVersion": "2.0", "Severity": 5, "Id": "1ab92989eaf0e742df4a014d5example", "CreatedAt": "2020-05-26T22:21:48.385Z", "AccountId": "111122223333" } ] }

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的調查結果

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 GetFindings

下列程式碼範例示範如何使用 get-ip-set

AWS CLI

列出取得指定信任 IP 集的詳細資訊

下列get-ip-set範例顯示指定信任 IP 集的狀態和詳細資訊。

aws guardduty get-ip-set \ --detector-id 12abc34d567e8fa901bc2d34eexample \ --ip-set-id d4b94fc952d6912b8f3060768example

輸出:

{ "Status": "ACTIVE", "Location": "s3://amzn-s3-demo-bucket.s3-us-west-2.amazonaws.com/customlist.csv", "Tags": {}, "Format": "TXT", "Name": "test-ip-set-example" }

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的使用信任 IP 清單和威脅清單

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 GetIpSet

下列程式碼範例示範如何使用 get-master-account

AWS CLI

擷取目前區域中主帳戶的詳細資訊

下列get-master-account範例顯示目前區域中與偵測器相關聯的主帳戶狀態和詳細資訊。

aws guardduty get-master-account \ --detector-id 12abc34d567e8fa901bc2d34eexample

輸出:

{ "Master": { "InvitationId": "04b94d9704854a73f94e061e8example", "InvitedAt": "2020-06-09T22:23:04.970Z", "RelationshipStatus": "Enabled", "AccountId": "111122223333" } }

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的了解 GuardDuty 管理員帳戶與成員帳戶之間的關係GuardDuty

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 GetMasterAccount

下列程式碼範例示範如何使用 list-detectors

AWS CLI

列出目前區域中可用的偵測器

下列list-detectors範例列出您目前 AWS 區域中可用的偵測器。

aws guardduty list-detectors

輸出:

{ "DetectorIds": [ "12abc34d567e8fa901bc2d34eexample" ] }

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的概念和術語

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ListDetectors

下列程式碼範例示範如何使用 list-findings

AWS CLI

範例 1:列出目前區域的所有調查結果

下列list-findings範例顯示目前區域的所有 findingIds清單,依嚴重性從最高到最低排序。

aws guardduty list-findings \ --detector-id 12abc34d567e8fa901bc2d34eexample \ --sort-criteria '{"AttributeName": "severity","OrderBy":"DESC"}'

輸出:

{ "FindingIds": [ "04b8ab50fd29c64fc771b232dexample", "5ab8ab50fd21373735c826d3aexample", "90b93de7aba69107f05bbe60bexample", ... ] }

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的調查結果

範例 2:列出符合特定調查結果條件之目前區域的調查結果

下列list-findings範例顯示符合指定findingIds。

aws guardduty list-findings \ --detector-id 12abc34d567e8fa901bc2d34eexample \ --finding-criteria '{"Criterion":{"type": {"Eq":["UnauthorizedAccess:EC2/SSHBruteForce"]}}}'

輸出:

{ "FindingIds": [ "90b93de7aba69107f05bbe60bexample", "6eb9430d7023d30774d6f05e3example", "2eb91a2d060ac9a21963a5848example", "44b8ab50fd2b0039a9e48f570example", "9eb8ab4cd2b7e5b66ba4f5e96example", "e0b8ab3a38e9b0312cc390ceeexample" ] }

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的調查結果

範例 3:列出符合 JSON 檔案中定義之一組特定問題清單條件的目前區域問題清單

以下list-findings範例顯示所有未封存的 findingIds清單,並涉及名為「testuser」的 IAM 使用者,如 JSON 檔案中所指定。

aws guardduty list-findings \ --detector-id 12abc34d567e8fa901bc2d34eexample \ --finding-criteria file://myfile.json

myfile.json 的內容:

{"Criterion": { "resource.accessKeyDetails.userName":{ "Eq":[ "testuser" ] }, "service.archived": { "Eq": [ "false" ] } } }

輸出:

{ "FindingIds": [ "1ab92989eaf0e742df4a014d5example" ] }

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的調查結果

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ListFindings

下列程式碼範例示範如何使用 list-invitations

AWS CLI

列出您邀請成為目前區域中成員帳戶的詳細資訊

下列list-invitations範例會列出您邀請成為目前區域中 GuardDuty 成員帳戶的詳細資訊和狀態。

aws guardduty list-invitations

輸出:

{ "Invitations": [ { "InvitationId": "d6b94fb03a66ff665f7db8764example", "InvitedAt": "2020-06-10T17:56:38.221Z", "RelationshipStatus": "Invited", "AccountId": "123456789111" } ] }

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的透過邀請管理 GuardDuty 帳戶。

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ListInvitations

下列程式碼範例示範如何使用 list-ip-sets

AWS CLI

列出目前區域中的信任 IP 集

下列list-ip-sets範例列出您目前 AWS 區域中的信任 IP 集。

aws guardduty list-ip-sets \ --detector-id 12abc34d567e8fa901bc2d34eexample

輸出:

{ "IpSetIds": [ "d4b94fc952d6912b8f3060768example" ] }

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的使用信任 IP 清單和威脅清單

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ListIpSets

下列程式碼範例示範如何使用 list-members

AWS CLI

範例 1:僅列出目前區域中的目前成員

下列list-members範例只會列出目前區域中與 GuardDuty 管理員帳戶相關聯的目前成員帳戶詳細資訊。

aws guardduty list-members \ --detector-id 12abc34d567e8fa901bc2d34eexample \ --only-associated="true"

輸出:

{ "Members": [ { "RelationshipStatus": "Enabled", "InvitedAt": "2020-06-09T22:49:00.910Z", "MasterId": "111122223333", "DetectorId": "7ab8b2f61b256c87f793f6a86example", "UpdatedAt": "2020-06-09T23:08:22.512Z", "Email": "your+member@example.com", "AccountId": "123456789012" } ] }

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的了解 GuardDuty 管理員帳戶與成員帳戶之間的關係GuardDuty

範例 2:列出目前區域中的所有成員

下列list-members範例列出並提供所有成員帳戶的詳細資訊,包括目前區域中已取消關聯或尚未接受來自 GuardDuty 管理員的邀請者。

aws guardduty list-members \ --detector-id 12abc34d567e8fa901bc2d34eexample \ --only-associated="false"

輸出:

{ "Members": [ { "RelationshipStatus": "Enabled", "InvitedAt": "2020-06-09T22:49:00.910Z", "MasterId": "111122223333", "DetectorId": "7ab8b2f61b256c87f793f6a86example", "UpdatedAt": "2020-06-09T23:08:22.512Z", "Email": "your+other+member@example.com", "AccountId": "555555555555" } ] }

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的了解 GuardDuty 管理員帳戶與成員帳戶之間的關係GuardDuty

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ListMembers

下列程式碼範例示範如何使用 update-ip-set

AWS CLI

更新信任的 IP 集

下列update-ip-set範例示範如何更新信任 IP 集的詳細資訊。

aws guardduty update-ip-set \ --detector-id 12abc34d567e8fa901bc2d34eexample \ --ip-set-id d4b94fc952d6912b8f3060768example \ --location https://amzn-s3-demo-bucket.s3-us-west-2.amazonaws.com/customtrustlist2.csv

此命令不會產生輸出。

如需詳細資訊,請參閱《GuardDuty 使用者指南》中的使用信任 IP 清單和威脅清單

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 UpdateIpSet