MISP MispQL

Query MISP in SQL

mispql is a command-line client for MISP. Write a question as an SQL-like statement: it becomes one MISP REST API search, sent with your API key, and the answer comes back as JSON, a text table or CSV.

SELECT id, event_id, type, value, Tag FROM attributes
WHERE type IN ('domain', 'ip-dst') AND timestamp >= '7d' AND tags != 'tlp:red'
LIMIT 5 FORMAT table
+--------+----------+--------+--------------------------+------------------------------------------------+
| id     | event_id | type   | value                    | Tag                                            |
+--------+----------+--------+--------------------------+------------------------------------------------+
| 913204 | 4711     | domain | update-check.example.net | tlp:amber,kill-chain:Command and Control       |
| 913205 | 4711     | ip-dst | 198.51.100.23            | tlp:amber                                      |
| 913388 | 4718     | domain | cdn-sync.example.org     |                                                |
| 913390 | 4718     | ip-dst | 203.0.113.7              | tlp:green                                      |
| 913412 | 4720     | domain | login-portal.example.com | tlp:clear,phishing:techniques="spear-phishing" |
+--------+----------+--------+--------------------------+------------------------------------------------+
5 rows
POST /attributes/restSearch
{
  "returnFormat": "json",
  "limit": 5,
  "page": 1,
  "query": {
    "AND": [
      {
        "type": {
          "IN": [
            "domain",
            "ip-dst"
          ]
        }
      },
      {
        "timestamp": {
          ">=": "7d"
        }
      },
      {
        "tags": {
          "!=": "tlp:red"
        }
      }
    ]
  }
}

One statement, one request

Every statement compiles to a single documented request to one of MISP's restSearch endpoints, before anything is sent. The whole statement is checked first, and a mistake is shown at the place in the text where it was made. --dry-run prints the request instead of sending it, so a statement can be turned into an API call for a script, a playbook or another tool.

SELECT * FROM events
WHERE date >= '30d' AND (type = 'btc' OR object_name = 'bank-account') AND tags != 'tlp:red'
ORDER BY event_id DESC LIMIT 20
POST /events/restSearch
{
  "returnFormat": "json",
  "limit": 20,
  "page": 1,
  "order": [
    "id desc"
  ],
  "query": {
    "AND": [
      {
        "date": {
          ">=": "30d"
        }
      },
      {
        "OR": [
          {
            "type": "btc"
          },
          {
            "object_name": "bank-account"
          }
        ]
      },
      {
        "tags": {
          "!=": "tlp:red"
        }
      }
    ]
  }
}

What it does

Eleven tables

Attributes, events, objects, galaxy clusters, event reports, notes, opinions, relationships, collections, tags and organisations.

Real conditions

AND, OR, NOT and parentheses over =, !=, comparisons, IN and LIKE, with relative times such as '7d'.

Your choice of output

JSON rows, a text table, CSV, or any of MISP's own export formats exactly as MISP sends them.

Counts

COUNT(*) answers with one number, counted by the server.

Discovery

SHOW TABLES and DESCRIBE ask the server which tables and fields you can use; SHOW DATABASES and USE switch between your MISPs and accounts.

Interactive or scripted

A prompt with completion and history, or statements from the command line, a file or a pipe.

Sees what you may see, and nothing else

mispql only searches. Each request runs as the user whose API key it carries, and MISP applies that user's access rules as on any other API call: a condition can narrow what the user is allowed to see, never widen it. Nothing is written to MISP.

More examples

How many domain-ip objects arrived in events changed this week:

SELECT COUNT(*) FROM objects WHERE object_name = 'domain-ip' AND event_timestamp >= '7d'
POST /objects/restSearch
{
  "returnFormat": "count",
  "query": {
    "AND": [
      {
        "object_name": "domain-ip"
      },
      {
        "event_timestamp": {
          ">=": "7d"
        }
      }
    ]
  }
}

Threat actors with a synonym containing "bear", from the galaxies MISP ships with:

SELECT id, value, Galaxy.name FROM galaxy_clusters
WHERE galaxy_name = 'Threat Actor' AND org = 'MISP' AND meta.synonyms LIKE '%bear%'
ORDER BY value LIMIT 50 FORMAT table
POST /galaxy_clusters/restSearch
{
  "returnFormat": "json",
  "limit": 50,
  "page": 1,
  "order": [
    "value asc"
  ],
  "query": {
    "AND": [
      {
        "galaxy_name": "Threat Actor"
      },
      {
        "org": "MISP"
      },
      {
        "meta.synonyms": {
          "LIKE": "%bear%"
        }
      }
    ]
  }
}

External organisations of the financial sector, as CSV for a spreadsheet or a script:

SELECT id, name, nationality, sector FROM organisations
WHERE local = FALSE AND sector IN ('Financial', 'Banking')
ORDER BY name LIMIT 100 FORMAT flat
POST /organisations/restSearch
{
  "returnFormat": "json",
  "limit": 100,
  "page": 1,
  "order": [
    "name asc"
  ],
  "query": {
    "AND": [
      {
        "local": false
      },
      {
        "sector": {
          "IN": [
            "Financial",
            "Banking"
          ]
        }
      }
    ]
  }
}

Get started

mispql needs a MISP 3.0 server and Python 3.11 or later. Your role on the server needs the Complex query permission, or you need to be a site administrator; it is off for every role until an administrator turns it on.

Install it from a checkout of the mispql repository:

python3 -m venv .venv
.venv/bin/pip install .

Tell it where your MISP is in ~/.config/mispql/config.toml, readable by you alone (chmod 600):

[default]
url = "https://misp.example.org"
key = "your API key"

Then ask a first question, or start the prompt with mispql alone:

.venv/bin/mispql -e "SHOW TABLES"

The guide covers the language, every table, the output formats and the options in detail.