PISA Pakistan — Community CERT

← ⚙️ Cyber Threat Intelligence Foundations

34. Automating CTI with Python: Building Custom Threat Feed Ingestion and Enrichment Scripts

⏱ 8 min

Overview

Manual IOC ingestion does not scale when you need to correlate thousands of indicators daily. Python automation lets CTI analysts fetch, normalize, and enrich data from public feeds, commercial platforms, and internal sources, producing actionable intelligence ready for detection pipelines.

Python Libraries for CTI Automation

  • pymisp – Interact with MISP (add events, retrieve attributes).
  • stix2 – Create and parse STIX 2.1 objects.
  • otx – Query AlienVault OTX API for pulses.
  • requests – HTTP calls to any REST endpoint.
  • pandas – Manipulate CSV/Excel IOC lists.
  • shodan – Enrich IP indicators with host profiles.

Building a Feed Ingestion Script

  1. Authenticate to MISP using pymisp.PyMISP() with API key in environment variables.
  2. Fetch CSV from a public feed (e.g., AbuseIPDB) using requests.get.
  3. Parse with pandas, drop duplicates, filter on Pakistani prefixes (42.*, 111.*).
  4. Enrich each IP with Shodan for open ports and banners; tag with enriched:shodan.
  5. Convert enriched data to STIX 2.1 Cyber Observable Objects and attach to a MISP event.
  6. Schedule via cron or systemd timer to run every 15 minutes.

Enriching IOCs with Threat Intelligence Services

  • Query VirusTotal for file hashes; store verdict in a custom attribute.
  • Pull related pulses from OTX and map them to MITRE ATT&CK tactics.
  • Cross‑reference NADRA’s internal abuse list (provided by PTA) to flag compromised national IDs.

Deploying Automation in a Pakistani CTI Pipeline

  • Store scripts in GitLab with CI/CD linting (flake8).
  • Output enriched JSON to a folder watched by Filebeat; logs ship to ELK for dashboards.
  • Alert SOC via TheHive when a high‑confidence IOC matches production traffic.

Do / Don't

  • Do keep API keys in environment variables or a secrets manager (python-dotenv).
  • Do implement retry logic with exponential backoff for external API calls.
  • Don’t hardcode feed URLs; use a configuration file (config.yaml).
  • Don’t skip error handling—unhandled exceptions can stall the pipeline.
← PrevNext →