建立自訂動作

支援的國家/地區:

第一個自訂整合中,您定義了參數,並建立了 Ping 動作來測試連線。本文將逐步說明如何為自訂整合服務建立兩項新動作:

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

建立自訂動作

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

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

設定動作參數

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

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

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

如要編輯「取得網域詳細資料」動作,請按照下列步驟操作:

  1. 複製「Get Domain Details」(取得網域詳細資料) 的程式碼,然後貼到 IDE。請檢查程式碼。物件必須使用類別的 end 方法傳回輸出訊息和結果值,例如:
    siemplify.end(msg, None)
  2. 擷取整合和動作參數:使用 siemplify.extract_configuration_param 函式擷取整合參數,例如 API 金鑰。同樣地,您設定的動作參數 (包括 Domain NameCheck 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. 建立要求並處理結果:
    1. 擷取整合和動作參數後,即可建構要求網址。系統會根據 availability_check 參數的布林值建構網址。
    2. 網址準備就緒後,請向 WHOIS 網站提出要求。
    3. 剖析網站的回應,並將相關資料新增至動作的結果。
    4. 定義要向使用者顯示的輸出訊息,並加入 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」動作中,按一下「Get Domain Details」即可新增 JSON 範例。在「建立第一個自動化程序」程序中,使用劇本設計工具的 JSON 範例,擷取 JSON 中的特定欄位。

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

測試動作

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

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

建立測試案例

如果環境中沒有任何測試案例,請依序前往「案件」>「將快訊擷取為測試案例」建立測試案例。這項動作會建立測試案例,並在案例佇列中顯示「測試」標籤。建立測試案例後,請返回 IDE 並從清單中選擇測試案例。

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

  1. 前往「案件」頁面,然後選取案件。
  2. 將警告擷取為測試案例。系統會在案件佇列中建立標示「測試」的新案件。

建立測試案例後,請返回 IDE 並從清單中選取。

建立充實動作

這部分程序著重於建立擴充動作,以便將新資料新增至實體。接著,您可以在實體探索器中查看經過擴充的資料。如要建立擴充動作,請按照下列步驟操作:

  1. 在 IDE 中建立新動作,並命名為 Enrich Entities
  2. 複製下列程式碼並貼到動作中:
    
      from SiemplifyAction import SiemplifyAction 
      from SiemplifyUtils import output_handler 
      from SiemplifyDataModel import EntityTypes
      
      import requests
      
      # Example Consts: 
      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 -----------------")
          output_message = "output message :" # human readable message, showed in UI
          as the action result successful_entities = [] # In case this actions
          contains entity based logic, collect successful entity.identifiers 
          
        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]))
            siemplify.update_entities(successful_entities) # This is the actual
            enrichment (this function sends the data back to the server) 
        else:        
            output_message += "\n No entities where processed."      
      result_value = len(successful_entities) 
    
          siemplify.LOGGER.info("----------------- Main - Finished -----------------")    
          siemplify.end(output_message, result_value) 
    
      if __name__ == "__main__":    
          main()
      
  3. 擷取參數。指令碼會從整合的設定中擷取 API 金鑰。您必須使用這個金鑰,才能驗證對 WHOIS XML API 的要求。
  4. 找出目標實體。指令碼會識別要處理的實體。這個函式會逐一檢查所有實體,並只著重於兩種型別:
    • 非內部主機名稱
    • 網址
    • 
        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]))
              siemplify.update_entities(successful_entities) # This is the actual
              enrichment (this function sends the data back to the server)
          else:
              output_message += "\n No entities where processed."
    
    
          result_value = len(successful_entities) 
      
  6. 啟用動作,然後按一下「儲存」。您現在已建立自訂整合項目,其中包含三項自訂動作:
    • Ping 動作,用於測試與 WHOIS XML API 產品的連線。
    • 取得網域詳細資料動作,可擷取網域相關資料,並以 JSON 結果呈現。
    • 充實實體動作:為目標實體新增額外資料,您可以在「實體探索工具」模組中查看。

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

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