|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2025 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Get multiple IoCs from Chronicle.""" |
| 18 | + |
| 19 | +from typing import List |
| 20 | + |
| 21 | +from common import regions |
| 22 | +from google.auth.transport import requests |
| 23 | + |
| 24 | +CHRONICLE_API_BASE_URL = "https://chronicle.googleapis.com" |
| 25 | + |
| 26 | + |
| 27 | +def batch_get_iocs( |
| 28 | + http_session: requests.AuthorizedSession, |
| 29 | + proj_id: str, |
| 30 | + proj_instance: str, |
| 31 | + proj_region: str, |
| 32 | + ioc_values: List[str], |
| 33 | + ioc_type: str, |
| 34 | +) -> dict: |
| 35 | + """Get multiple IoCs by their values from Chronicle. |
| 36 | +
|
| 37 | + Args: |
| 38 | + http_session: Authorized session for HTTP requests. |
| 39 | + proj_id: GCP project id or number to which the target instance belongs. |
| 40 | + proj_instance: Customer ID (uuid with dashes) for the instance. |
| 41 | + proj_region: region in which the target project is located. |
| 42 | + ioc_values: List of IoC values to retrieve. |
| 43 | + ioc_type: Type of IoCs being requested. One of: |
| 44 | + IOC_TYPE_UNSPECIFIED |
| 45 | + DOMAIN |
| 46 | + IP |
| 47 | + FILE_HASH |
| 48 | + URL |
| 49 | + USER_EMAIL |
| 50 | + MUTEX |
| 51 | + FILE_HASH_MD5 |
| 52 | + FILE_HASH_SHA1 |
| 53 | + FILE_HASH_SHA256 |
| 54 | + IOC_TYPE_RESOURCE |
| 55 | +
|
| 56 | + Returns: |
| 57 | + Dict containing the requested IoCs. |
| 58 | +
|
| 59 | + Raises: |
| 60 | + requests.exceptions.HTTPError: HTTP request resulted in an error |
| 61 | + (response.status_code >= 400). |
| 62 | + """ |
| 63 | + base_url_with_region = regions.url_always_prepend_region( |
| 64 | + CHRONICLE_API_BASE_URL, proj_region) |
| 65 | + instance = f"projects/{proj_id}/locations/{proj_region}/instances/{proj_instance}" |
| 66 | + url = f"{base_url_with_region}/v1alpha/{instance}/iocs:batchGet" |
| 67 | + |
| 68 | + body = { |
| 69 | + "ioc_values": ioc_values, |
| 70 | + "ioc_type": ioc_type, |
| 71 | + } |
| 72 | + |
| 73 | + response = http_session.request( |
| 74 | + "POST", |
| 75 | + url, |
| 76 | + json=body, |
| 77 | + ) |
| 78 | + if response.status_code >= 400: |
| 79 | + print(response.text) |
| 80 | + response.raise_for_status() |
| 81 | + return response.json() |
0 commit comments