收集 CSV 自定义 IOC 文件

支持的平台:

本文档介绍了如何使用 Google Cloud Storage 将 CSV 自定义 IOC 文件提取到 Google Security Operations,然后将这些字段映射到 UDM,处理各种数据类型(例如 IP、网域和哈希),并使用威胁详情、实体信息和严重程度级别来丰富输出。

准备工作

确保您满足以下前提条件:

  • Google SecOps 实例
  • 已启用 Cloud Storage API 的 GCP 项目
  • 创建和管理 GCS 存储分区的权限
  • 管理 GCS 存储分区的 IAM 政策的权限
  • 创建 Cloud Run 服务、Pub/Sub 主题和 Cloud Scheduler 作业的权限
  • 对一个或多个 CSV IOC Feed 网址 (HTTPS) 或提供 CSV 的内部端点的访问权限

创建 Google Cloud Storage 存储分区

  1. 前往 Google Cloud 控制台
  2. 选择您的项目或创建新项目。
  3. 在导航菜单中,依次前往 Cloud Storage > 存储分区
  4. 点击创建存储分区
  5. 提供以下配置详细信息:

    设置
    为存储分区命名 输入一个全局唯一的名称(例如 csv-ioc-logs
    位置类型 根据您的需求进行选择(区域级、双区域级、多区域级)
    位置 选择相应位置(例如 us-central1
    存储类别 标准(建议用于经常访问的日志)
    访问权限控制 统一(推荐)
    保护工具 可选:启用对象版本控制或保留政策
  6. 点击创建

为 Cloud Run 函数创建服务账号

Cloud Run 函数需要一个服务账号,该账号具有向 GCS 存储分区写入内容以及被 Pub/Sub 调用的权限。

创建服务账号

  1. GCP 控制台中,依次前往 IAM 和管理 > 服务账号
  2. 点击创建服务账号
  3. 提供以下配置详细信息:
    • 服务账号名称:输入 csv-ioc-collector-sa
    • 服务账号说明:输入 Service account for Cloud Run function to collect CSV IOC files
  4. 点击创建并继续
  5. 向此服务账号授予对项目的访问权限部分中,添加以下角色:
    1. 点击选择角色
    2. 搜索并选择 Storage Object Admin
    3. 点击 + 添加其他角色
    4. 搜索并选择 Cloud Run Invoker
    5. 点击 + 添加其他角色
    6. 搜索并选择 Cloud Functions Invoker
  6. 点击继续
  7. 点击完成

必须拥有这些角色,才能:

  • Storage Object Admin:将日志写入 GCS 存储分区
  • Cloud Run Invoker:允许 Pub/Sub 调用函数
  • Cloud Functions Invoker:允许调用函数

授予对 GCS 存储分区的 IAM 权限

向服务账号授予对 GCS 存储分区的写入权限:

  1. 前往 Cloud Storage > 存储分区
  2. 点击您的存储分区名称。
  3. 前往权限标签页。
  4. 点击授予访问权限
  5. 提供以下配置详细信息:
    • 添加主账号:输入服务账号电子邮件地址(例如 csv-ioc-collector-sa@PROJECT_ID.iam.gserviceaccount.com)。
    • 分配角色:选择 Storage Object Admin
  6. 点击保存

创建发布/订阅主题

创建一个 Pub/Sub 主题,供 Cloud Scheduler 发布消息,并供 Cloud Run 函数订阅。

  1. GCP 控制台中,前往 Pub/Sub > 主题
  2. 点击创建主题
  3. 提供以下配置详细信息:
    • 主题 ID:输入 csv-ioc-trigger
    • 将其他设置保留为默认值。
  4. 点击创建

创建 Cloud Run 函数以收集 CSV IOC 文件

Cloud Run 函数由来自 Cloud Scheduler 的 Pub/Sub 消息触发,用于从 HTTPS 端点提取 CSV IOC 文件并将其写入 GCS。

  1. GCP 控制台中,前往 Cloud Run
  2. 点击创建服务
  3. 选择函数(使用内嵌编辑器创建函数)。
  4. 配置部分中,提供以下配置详细信息:

    设置
    Service 名称 csv-ioc-collector
    区域 选择与您的 GCS 存储分区匹配的区域(例如 us-central1
    运行时 选择 Python 3.12 或更高版本
  5. 触发器(可选)部分中:

    1. 点击 + 添加触发器
    2. 选择 Cloud Pub/Sub
    3. 选择 Cloud Pub/Sub 主题中,选择 Pub/Sub 主题 (csv-ioc-trigger)。
    4. 点击保存
  6. 身份验证部分中:

    1. 选择需要进行身份验证
    2. 检查 Identity and Access Management (IAM)
  7. 向下滚动并展开容器、网络、安全性

  8. 前往安全标签页:

    • 服务账号:选择服务账号 (csv-ioc-collector-sa)。
  9. 前往容器标签页:

    1. 点击变量和密钥
    2. 为每个环境变量点击 + 添加变量
    变量名称 示例值 说明
    GCS_BUCKET csv-ioc-logs GCS 存储分区名称
    GCS_PREFIX csv-ioc 日志文件的前缀
    IOC_URLS https://ioc.example.com/feed.csv,https://another.example.org/iocs.csv 以英文逗号分隔的 HTTPS 网址
    AUTH_HEADER Authorization: Bearer <token> 可选的身份验证标头
    TIMEOUT 60 请求超时时间(以秒为单位)
  10. 变量和 Secret 部分中,向下滚动到请求

    • 请求超时:输入 600 秒(10 分钟)。
  11. 前往设置标签页:

    • 资源部分中:
      • 内存:选择 512 MiB 或更高值。
      • CPU:选择 1
    • 点击完成
  12. 修订版本扩缩部分中:

    • 实例数下限:输入 0
    • 实例数上限:输入 100(或根据预期负载进行调整)。
  13. 点击创建

  14. 等待服务创建完成(1-2 分钟)。

  15. 创建服务后,系统会自动打开内嵌代码编辑器

添加函数代码

  1. 函数入口点中输入 main
  2. 在内嵌代码编辑器中,创建两个文件:

    • 第一个文件:main.py:
    import functions_framework
    from google.cloud import storage
    import json
    import os
    import urllib3
    from datetime import datetime, timezone
    import time
    
    # Initialize HTTP client with timeouts
    http = urllib3.PoolManager(
        timeout=urllib3.Timeout(connect=5.0, read=30.0),
        retries=False,
    )
    
    # Initialize Storage client
    storage_client = storage.Client()
    
    @functions_framework.cloud_event
    def main(cloud_event):
        """
        Cloud Run function triggered by Pub/Sub to fetch CSV IOC feeds over HTTPS and write to GCS.
    
        Args:
            cloud_event: CloudEvent object containing Pub/Sub message
        """
    
        # Get environment variables
        bucket_name = os.environ.get('GCS_BUCKET')
        prefix = os.environ.get('GCS_PREFIX', 'csv-ioc').strip('/')
        ioc_urls_str = os.environ.get('IOC_URLS', '')
        auth_header = os.environ.get('AUTH_HEADER', '')
        timeout = int(os.environ.get('TIMEOUT', '60'))
    
        ioc_urls = [u.strip() for u in ioc_urls_str.split(',') if u.strip()]
    
        if not bucket_name:
            print('Error: GCS_BUCKET environment variable is required')
            return
    
        if not ioc_urls:
            print('Error: IOC_URLS must contain at least one HTTPS URL')
            return
    
        try:
            # Get GCS bucket
            bucket = storage_client.bucket(bucket_name)
    
            run_ts = int(time.time())
            written = []
    
            for i, url in enumerate(ioc_urls):
                print(f'Processing URL {i+1}/{len(ioc_urls)}: {url}')
    
                # Build request
                req_headers = {'Accept': 'text/csv, */*'}
    
                # Add authentication header if provided
                if auth_header:
                    if ':' in auth_header:
                        k, v = auth_header.split(':', 1)
                        req_headers[k.strip()] = v.strip()
                    else:
                        req_headers['Authorization'] = auth_header.strip()
    
                # Fetch data with retries
                data = fetch_with_retries(url, req_headers, timeout)
    
                if data:
                    # Write to GCS
                    key = generate_blob_name(prefix, url, run_ts, i)
                    blob = bucket.blob(key)
                    blob.upload_from_string(data, content_type='text/csv')
    
                    written.append({
                        'url': url,
                        'gcs_key': key,
                        'bytes': len(data)
                    })
    
                    print(f'Wrote {len(data)} bytes to gs://{bucket_name}/{key}')
                else:
                    print(f'Warning: No data retrieved from {url}')
    
            print(f'Successfully processed {len(written)} URLs')
            print(json.dumps({'ok': True, 'written': written}, indent=2))
    
        except Exception as e:
            print(f'Error processing CSV IOC feeds: {str(e)}')
            raise
    
    def fetch_with_retries(url, headers, timeout, max_retries=5):
        """Fetch data from URL with retry logic for 429/5xx errors."""
        if not url.lower().startswith('https://'):
            raise ValueError('Only HTTPS URLs are allowed in IOC_URLS')
    
        attempt = 0
        backoff = 1.0
    
        while attempt < max_retries:
            try:
                response = http.request('GET', url, headers=headers, timeout=timeout)
    
                if response.status == 200:
                    return response.data.decode('utf-8')
                elif response.status == 429 or (500 <= response.status < 600):
                    print(f'Received status {response.status}, retrying in {backoff}s (attempt {attempt+1}/{max_retries})')
                    time.sleep(backoff)
                    attempt += 1
                    backoff *= 2
                else:
                    print(f'Error: Received unexpected status {response.status} from {url}')
                    return None
    
            except Exception as e:
                if attempt < max_retries - 1:
                    print(f'Request failed: {str(e)}, retrying in {backoff}s (attempt {attempt+1}/{max_retries})')
                    time.sleep(backoff)
                    attempt += 1
                    backoff *= 2
                else:
                    raise
    
        print(f'Max retries exceeded for {url}')
        return None
    
    def generate_blob_name(prefix, url, run_ts, idx):
        """Generate a unique blob name for the CSV file."""
        # Create a short, filesystem-safe token for the URL
        safe_url = url.replace('://', '_').replace('/', '_').replace('?', '_').replace('&', '_')[:100]
    
        # Generate timestamp-based path
        timestamp_path = time.strftime('%Y/%m/%d/%H%M%S', time.gmtime(run_ts))
    
        return f"{prefix}/{timestamp_path}-url{idx:03d}-{safe_url}.csv"
    
    • 第二个文件:requirements.txt:
    functions-framework==3.*
    google-cloud-storage==2.*
    urllib3>=2.0.0
    
  3. 点击部署以保存并部署该函数。

  4. 等待部署完成(2-3 分钟)。

创建 Cloud Scheduler 作业

Cloud Scheduler 会定期向 Pub/Sub 主题发布消息,从而触发 Cloud Run 函数。

  1. GCP Console 中,前往 Cloud Scheduler
  2. 点击创建作业
  3. 提供以下配置详细信息:

    设置
    名称 csv-ioc-collector-hourly
    区域 选择与 Cloud Run 函数相同的区域
    频率 0 * * * *(每小时一次,在整点时)
    时区 选择时区(建议选择世界协调时间 [UTC])
    目标类型 Pub/Sub
    主题 选择 Pub/Sub 主题 (csv-ioc-trigger)
    消息正文 {}(空 JSON 对象)
  4. 点击创建

时间表频率选项

  • 根据日志量和延迟时间要求选择频次:

    频率 Cron 表达式 使用场景
    每隔 5 分钟 */5 * * * * 高容量、低延迟
    每隔 15 分钟 */15 * * * * 搜索量中等
    每小时 0 * * * * 标准(推荐)
    每 6 小时 0 */6 * * * 量小、批处理
    每天 0 0 * * * 历史数据收集

测试集成

  1. Cloud Scheduler 控制台中,找到您的作业 (csv-ioc-collector-hourly)。
  2. 点击强制运行以手动触发作业。
  3. 等待几秒钟。
  4. 前往 Cloud Run > 服务
  5. 点击函数名称 (csv-ioc-collector)。
  6. 点击日志标签页。
  7. 验证函数是否已成功执行。查找以下项:

    Processing URL 1/X: https://...
    Wrote X bytes to gs://csv-ioc-logs/csv-ioc/YYYY/MM/DD/HHMMSS-url000-...csv
    Successfully processed X URLs
    
  8. 前往 Cloud Storage > 存储分区

  9. 点击您的存储分区名称 (csv-ioc-logs)。

  10. 前往前缀文件夹 (csv-ioc/)。

  11. 验证是否已创建具有当前时间戳的新 .csv 文件。

如果您在日志中看到错误,请执行以下操作:

  • HTTP 401/403:检查 AUTH_HEADER 环境变量
  • HTTP 429:速率限制 - 函数将自动重试并进行退避
  • 缺少环境变量:检查是否已设置所有必需的变量
  • 仅允许使用 HTTPS 网址:验证 IOC_网址S 是否仅包含 HTTPS 网址

检索 Google SecOps 服务账号

Google SecOps 使用唯一的服务账号从您的 GCS 存储分区中读取数据。您必须授予此服务账号对您的存储分区的访问权限。

获取服务账号电子邮件地址

  1. 依次前往 SIEM 设置 > Feed
  2. 点击添加新 Feed
  3. 点击配置单个 Feed
  4. Feed 名称字段中,输入 Feed 的名称(例如 CSV Custom IOC)。
  5. 选择 Google Cloud Storage V2 作为来源类型
  6. 选择 CSV 自定义 IOC 作为日志类型
  7. 点击获取服务账号。系统会显示一个唯一的服务账号电子邮件地址,例如:

    chronicle-12345678@chronicle-gcp-prod.iam.gserviceaccount.com
    
  8. 复制此电子邮件地址,以便在下一步中使用。

向 Google SecOps 服务账号授予 IAM 权限

Google SecOps 服务账号需要对您的 GCS 存储分区具有 Storage Object Viewer 角色。

  1. 前往 Cloud Storage > 存储分区
  2. 点击您的存储分区名称 (csv-ioc-logs)。
  3. 前往权限标签页。
  4. 点击授予访问权限
  5. 提供以下配置详细信息:
    • 添加主账号:粘贴 Google SecOps 服务账号电子邮件地址。
    • 分配角色:选择 Storage Object Viewer
  6. 点击保存

在 Google SecOps 中配置 Feed 以注入 CSV 自定义 IOC 文件

  1. 依次前往 SIEM 设置 > Feed
  2. 点击添加新 Feed
  3. 点击配置单个 Feed
  4. Feed 名称字段中,输入 Feed 的名称(例如 CSV Custom IOC)。
  5. 选择 Google Cloud Storage V2 作为来源类型
  6. 选择 CSV 自定义 IOC 作为日志类型
  7. 点击下一步
  8. 为以下输入参数指定值:

    • 存储分区网址:输入带有前缀路径的 GCS 存储分区 URI:

      gs://csv-ioc-logs/csv-ioc/
      
        • csv-ioc-logs:您的 GCS 存储分区名称。
        • csv-ioc:存储日志的可选前缀/文件夹路径。
      • 示例

        • 根存储分区:gs://csv-ioc-logs/
        • 带前缀:gs://csv-ioc-logs/csv-ioc/
        • 使用子文件夹:gs://csv-ioc-logs/ioc-feeds/
    • 来源删除选项:根据您的偏好选择删除选项:

      • 永不:永不删除转移后的任何文件(建议用于测试)。
      • 删除已转移的文件:在成功转移后删除文件。
      • 删除已转移的文件和空目录:在成功转移后删除文件和空目录。

    • 文件存在时间上限:包含在过去指定天数内修改的文件。默认值为 180 天。

    • 资产命名空间资产命名空间

    • 注入标签:要应用于此 Feed 中事件的标签。

  9. 点击下一步

  10. 最终确定界面中查看新的 Feed 配置,然后点击提交

UDM 映射表

日志字段 UDM 映射 逻辑
asn entity.metadata.threat.detection_fields.asn_label.value 直接从“asn”字段映射。
类别 entity.metadata.threat.category_details 直接从“category”字段映射。
分类信息 entity.metadata.threat.category_details 附加到“classification - ”,并映射到“entity.metadata.threat.category_details”字段。
第 2 列 entity.entity.hostname 如果 [category] 与“. ?ip”或“. ?proxy”匹配,且 [not_ip] 为 true,则映射到“entity.entity.hostname”。
第 2 列 entity.entity.ip 如果 [category] 与“. ?ip”或“. ?proxy”匹配,且 [not_ip] 为 false,则合并到“entity.entity.ip”中。
置信度 entity.metadata.threat.confidence_score 转换为浮点数并映射到“entity.metadata.threat.confidence_score”字段。
country entity.entity.location.country_or_region 直接从“country”字段映射。
date_first entity.metadata.threat.first_discovered_time 解析为 ISO8601 并映射到“entity.metadata.threat.first_discovered_time”字段。
date_last entity.metadata.threat.last_updated_time 解析为 ISO8601 并映射到“entity.metadata.threat.last_updated_time”字段。
详细信息 entity.metadata.threat.summary 直接从“detail”字段映射。
detail2 entity.metadata.threat.description 直接从“detail2”字段映射。
域名 entity.entity.hostname 直接从“domain”字段映射。
电子邮件 entity.entity.user.email_addresses 已合并到“entity.entity.user.email_addresses”字段中。
id entity.metadata.product_entity_id 附加到“id - ”并映射到“entity.metadata.product_entity_id”字段。
import_session_id entity.metadata.threat.detection_fields.import_session_id_label.value 直接从“import_session_id”字段映射。
itype entity.metadata.threat.detection_fields.itype_label.value 直接从“itype”字段映射。
纬度 entity.entity.location.region_latitude 转换为浮点数并映射到“entity.entity.location.region_latitude”字段。
经度 entity.entity.location.region_longitude 转换为浮点数并映射到“entity.entity.location.region_longitude”字段。
maltype entity.metadata.threat.detection_fields.maltype_label.value 直接从“maltype”字段映射。
md5 entity.entity.file.md5 直接从“md5”字段映射。
媒体 entity.metadata.threat.detection_fields.media_label.value 直接从“media”字段映射。
media_type entity.metadata.threat.detection_fields.media_type_label.value 直接从“media_type”字段映射。
org entity.metadata.threat.detection_fields.org_label.value 直接从“org”字段映射。
resource_uri entity.entity.url 如果 [itype] 与“(ip
resource_uri entity.metadata.threat.url_back_to_product 如果 [itype] 与“(ip
得分 entity.metadata.threat.confidence_details 直接从“score”字段映射。
和程度上减少 entity.metadata.threat.severity 如果与“LOW”“MEDIUM”“HIGH”或“CRITICAL”匹配,则转换为大写并映射到“entity.metadata.threat.severity”字段。
来源 entity.metadata.threat.detection_fields.source_label.value 直接从“来源”字段映射。
source_feed_id entity.metadata.threat.detection_fields.source_feed_id_label.value 直接从“source_feed_id”字段映射。
srcip entity.entity.ip 如果 [srcip] 不为空且不等于 [value],则合并到“entity.entity.ip”中。
entity.metadata.threat.detection_fields.state_label.value 直接从“state”字段映射。
trusted_circle_ids entity.metadata.threat.detection_fields.trusted_circle_ids_label.value 直接从“trusted_circle_ids”字段映射。
update_id entity.metadata.threat.detection_fields.update_id_label.value 直接从“update_id”字段映射。
entity.entity.file.full_path 如果 [category] 与“.*?file”匹配,则映射到“entity.entity.file.full_path”。
entity.entity.file.md5 如果 [category] 与“.*?md5”匹配,且 [value] 是一个 32 字符的十六进制字符串,则映射到“entity.entity.file.md5”。
entity.entity.file.sha1 如果 ([category] 与 ". ?md5" 匹配且 [value] 是一个 40 字符的十六进制字符串) 或 ([category] 与 ". ?sha1" 匹配且 [value] 是一个 40 字符的十六进制字符串),则映射到 "entity.entity.file.sha1"。
entity.entity.file.sha256 如果 ([category] 与“. ?md5”匹配且 [value] 是十六进制字符串且 [file_type] 不是“md5”) 或 ([category] 与“. ?sha256”匹配且 [value] 是十六进制字符串),则映射到“entity.entity.file.sha256”。
entity.entity.hostname 如果 [category] 与“. ?domain”匹配,或者 [category] 与“. ?ip”或“.*?proxy”匹配且 [not_ip] 为 true,则映射到“entity.entity.hostname”。
entity.entity.url 如果 [category] 与“.*?url”匹配,或者 [category] 与“url”匹配且 [resource_uri] 不为空,则映射到“entity.entity.url”。
不适用 entity.metadata.collected_timestamp 用事件时间戳填充。
不适用 entity.metadata.interval.end_time 设置为 253402300799 秒的常量值。
不适用 entity.metadata.interval.start_time 用事件时间戳填充。
不适用 entity.metadata.vendor_name 设置为常量值“Custom IOC”。

需要更多帮助?获得社区成员和 Google SecOps 专业人士的解答。