開發第一個自訂動作

支援的服務:

在第一個自訂整合中,您定義了參數,並建立 Ping 動作來測試連線。本文件將引導您為自訂整合建立兩項新動作:

  • Get Domain Details:擷取網域資訊,並以 JSON 格式呈現結果。
  • 擴充實體:使用網域詳細資料擴充實體。

建立自訂動作

如要建立自訂動作,請按照下列步驟操作:

  1. 前往整合式開發環境 (IDE),然後按一下「 新增」,新增 IDE 項目。
  2. 選取「動作」。
  3. 將動作命名為 Get Domain Details,然後選取整合項目。
  4. 點選「建立」。IDE 會產生新範本,並內建程式碼註解和說明。

設定動作參數

根據 WHOIS XML API 說明文件,「Get Domain Details」動作需要兩個參數:Check Availability 和 Domain Name。如要設定這些參數,請按照下列步驟操作:

  1. 在 IDE 模組中,按一下「 新增」。
  2. 填寫「查看供應情形」欄位,然後按一下「儲存」。這個參數會指出網域是否可用,其結果會用於您稍後建立的自動化動作。
  3. 填寫「網域名稱」欄位,然後按一下「儲存」。在這個欄位輸入要檢查的網域。

編輯「取得網域詳細資料」動作

如要編輯「Get Domain Details」動作,請按照下列步驟操作:

  1. 複製「Get Domain Details」提供的程式碼,貼到 IDE 中,然後查看程式碼。物件必須使用類別的 end 方法,傳回輸出訊息和結果值:

    siemplify.end(msg, None)
    
  2. 擷取整合和動作參數。整合參數 (例如 API Key) 是使用 siemplify.extract_configuration_param 函式擷取。動作參數 (例如 Domain Name 和 Check availability) 是使用 siemplify.extract_action_param 函式擷取:

    api_key = siemplify.extract_configuration_param(
        provider_name=INTEGRATION_NAME,
        param_name="API Key",
    )
    url = f"https://www.whoisxmlapi.com/whoisserver/WhoisService?apiKey={api_key}&outputFormat=json"
    domain = siemplify.extract_action_param(
        param_name="Domain Name",
        print_value=True,
    )
    availability_check = siemplify.extract_action_param(
        param_name="Check availability",
        is_mandatory=False,
        print_value=True,
    )
    
  3. 建構要求並處理結果:

    • 根據 availability_check 參數的布林值建構要求網址。
    • 將要求傳送至 WHOIS API。
    • 剖析回應,並將相關資料新增至動作結果。
    • 定義要向使用者顯示的輸出訊息,並加入 JSON 結果:
    # Add domain to scan
    url = f"{url}&domainName={domain}"
    
    # Determine availability check
    if availability_check.lower() == "true":
        availability_check_qs = 1
    else:
        availability_check_qs = 0
    
    url = f"{url}&da={availability_check_qs}"
    response = requests.get(url)
    response.raise_for_status()
    
    # Add a JSON result that can be used in the next steps of the playbook
    siemplify.result.add_result_json(response.json())
    
    # Add the JSON to the action result presented in the context details
    siemplify.result.add_json("WhoisDetails", response.json())
    
    msg = f"Fetched data for {domain}"
    siemplify.end(msg, None)
    
    if __name__ == "__main__":
        main()
    

為動作新增 JSON 結果

在「Get Domain Details」動作中,新增 JSON 範例,以便在「Create your first automation」程序中,於應對手冊設計工具擷取特定欄位:

  1. 複製 WHOIS 網站 JSON 範例中的 JSON。
  2. 在 IDE 的「Details」(詳細資料) 分頁中,啟用「Include JSON Result」(包含 JSON 結果) 切換鈕,IDE 頂端就會顯示 JSON 圖示。
  3. 依序點選「file_json」「管理 JSON 範例」>「login」「匯入 JSON 範例」。

測試動作

如要測試建立的動作,請按照下列步驟操作:

  1. 前往「測試」分頁。
  2. 在「範圍」欄位中,選取「測試案例」和「整合執行個體」。
  3. 在 IDE 中點按「投影播放」圖示 。
  4. 在「測試」分頁中查看動作結果。測試完成後,您也可以在「Debug Output」分頁中查看記錄和輸出內容。

建立測試案例

如果環境中沒有任何測試案例,請按照下列步驟建立:

  1. 前往「案件」頁面,然後選取案件。
  2. 選取「擷取警告做為測試案例」。這項操作會在案件佇列中建立新案件,並加上「測試」標籤。
  3. 返回 IDE,然後從清單中選取測試案例。

建立資料充實動作

本節說明如何建立擴充動作,將新資料新增至實體。您可以在實體探索工具中查看經過擴充的資料。如要建立擴充動作,請按照下列步驟操作:

  1. 在 IDE 中建立新動作,並命名為 Enrich Entities。
  2. 複製下列程式碼並貼到動作中:

    from SiemplifyAction import SiemplifyAction
    from SiemplifyDataModel import EntityTypes
    from SiemplifyUtils import output_handler
    import requests
    
    # Example constants
    INTEGRATION_NAME = "My first Integration - Whois XML API"
    SCRIPT_NAME = "WHOIS XML API EnrichEntities"
    
    @output_handler
    def main():
        siemplify = SiemplifyAction()
        siemplify.script_name = SCRIPT_NAME
        siemplify.LOGGER.info("================= Main - Param Init =================")
    
        api_key = siemplify.extract_configuration_param(
            provider_name=INTEGRATION_NAME,
            param_name="API Key",
        )
        url = f"https://www.whoisxmlapi.com/whoisserver/WhoisService?apiKey={api_key}&outputFormat=json"
    
        siemplify.LOGGER.info("----------------- Main - Started -----------------")
    
        # Human-readable message shown in the UI as the action result
        output_message = "output message :"
        # Collect successful entity identifiers
        successful_entities = []
    
        for entity in siemplify.target_entities:
            siemplify.LOGGER.info(f"processing entity {entity.identifier}")
            if (
                entity.entity_type == EntityTypes.HOSTNAME and not entity.is_internal
            ) or entity.entity_type == EntityTypes.URL:
                entity_to_scan = entity.identifier
                scan_url = f"{url}&domainName={entity_to_scan}"
                response = requests.get(scan_url)
                response.raise_for_status()
                register_details = (
                    response.json().get("WhoisRecord", {}).get("registrant", {})
                )
                if register_details:
                    entity.additional_properties.update(register_details)
                    successful_entities.append(entity)
    
        if successful_entities:
            output_message += "\n Successfully processed entities:\n {}".format(
                "\n ".join([x.identifier for x in successful_entities])
            )
            # Send the enriched entity data back to the server
            siemplify.update_entities(successful_entities)
        else:
            output_message += "\n No entities were processed."
    
        result_value = len(successful_entities)
    
        siemplify.LOGGER.info("----------------- Main - Finished -----------------")
        siemplify.end(output_message, result_value)
    
    if __name__ == "__main__":
        main()
    
  3. 擷取設定參數。指令碼會從整合設定中擷取 API Key 參數,驗證對 WHOIS XML API 的要求。

  4. 找出要處理的目標實體。指令碼會疊代 siemplify.target_entities,且只處理下列實體類型:

    • 非內部主機名稱
    • 網址
    for entity in siemplify.target_entities:
        siemplify.LOGGER.info(f"processing entity {entity.identifier}")
        if (
            entity.entity_type == EntityTypes.HOSTNAME and not entity.is_internal
        ) or entity.entity_type == EntityTypes.URL:
            entity_to_scan = entity.identifier
    
  5. 掃描網域,並定義擴充步驟和輸出訊息。由於這項動作是在「實體」範圍內執行,因此您不需要設定個別動作參數:

    scan_url = f"{url}&domainName={entity_to_scan}"
    response = requests.get(scan_url)
    response.raise_for_status()
    register_details = (
        response.json().get("WhoisRecord", {}).get("registrant", {})
    )
    if register_details:
        entity.additional_properties.update(register_details)
        successful_entities.append(entity)
    
    if successful_entities:
        output_message += "\n Successfully processed entities:\n {}".format(
            "\n ".join([x.identifier for x in successful_entities])
        )
        # Send the enriched entity data back to the server
        siemplify.update_entities(successful_entities)
    else:
        output_message += "\n No entities were processed."
    
    result_value = len(successful_entities)
    
  6. 啟用動作,然後按一下「儲存」。您現在已完成自訂整合,並設定下列自訂動作:

    • Ping:測試與 WHOIS XML API 產品的連線。
    • Get Domain Details:擷取網域相關資料,並以 JSON 結果呈現。
    • Enrich Entities:將註冊者資料新增至目標實體,您可以在「Entity Explorer」中查看。

現在,你可以使用自訂動作建立第一個自動化動作。

還有其他問題嗎?向社群成員和 Google SecOps 專業人員尋求解答。