MispQL reference
MispQL is an SQL-like language for searching MISP. It comes from the MISP project: MispQL is written by Andras Iklody at CIRCL and released under the MIT licence. Huntbase runs MispQL against a MISP connection in api mode, read-only, as the user the connection's auth key belongs to.
A statement names a table, the conditions rows must meet, and how many to return:
SELECT id, event_id, type, value, Tag FROM attributes
WHERE type IN ('domain', 'ip-dst') AND timestamp >= '7d' AND tags != 'tlp:red'
LIMIT 100
Huntbase checks the statement, turns it into one search request to MISP and shows the rows MISP answers with. MISP evaluates the conditions, so only matching rows come back, and a condition can only narrow what the user may see, never widen it.
This page describes the full language, which needs MISP 3.0 and a key whose role has the Complex query permission. On MISP 2.4 and 2.5, and on MISP 3.0 without the permission, a smaller subset runs. See Which MISP versions.
Statements
SELECT * | COUNT(*) | column, ... FROM table
[WHERE condition]
[ORDER BY field [ASC | DESC], ...]
[LIMIT n [OFFSET n]]
DESCRIBE table
SHOW TABLES
- Keywords can be written in any case. Table and field names are
case-insensitive. A name in backticks is taken as written, for a keyword
used as a name, such as
`count`. - Strings take single or double quotes. Numbers are whole numbers. Booleans
are
TRUEandFALSE. Types are strict:id = '50000'is an error. - A search needs a
WHEREor aLIMIT.--starts a comment. - Run one statement at a time. The MispQL command line's
FORMAT,USE,SHOW DATABASESandWHOAMIaren't used in Huntbase: results are always rows, and the connection chooses the MISP server and key.
Tables
Each table is one kind of MISP record.
| Table | Records | Common fields |
|---|---|---|
attributes | Attributes, with their event and tags | id, event_id, type, category, value, to_ids, comment, timestamp, tags, and on the attribute's event: eventinfo, org, published, event_timestamp, publish_timestamp, threat_level_id |
events | Events | id (or event_id), eventinfo, org, date, published, threat_level_id, analysis, timestamp, publish_timestamp, tags; type and value select events holding a matching attribute |
objects | Objects, with their attributes | id, event_id, object_name, object_template_uuid, event_timestamp, value |
galaxy_clusters | Galaxy clusters, with their galaxy | id, value, galaxy_name, galaxy_namespace, org, description, meta.<key> such as meta.synonyms |
event_reports | Event reports | id, event_id, name, content, published, date, tags |
notes, opinions, relationships | Analysts' notes, opinions and relationships | object_type, object_uuid, created, modified; opinion on opinions; relationship_type on relationships |
collections | Collections, with their elements | name, type, element_type, created, modified |
tags | Every tag on the server | name, numerical_value, is_galaxy, hide_tag |
organisations | Local and external organisations | name, type, nationality, sector, local |
tags matches a tag on the record or on its event, and its value is matched
against tag names as a pattern, as in MISP. DESCRIBE lists every field of a
table, with its type and the operators it takes.
Columns
SELECT *returns each table's usual columns. Onattributes, for example:id,event_id,category,type,value,to_ids,timestampandTag.- A column list returns those columns, in that order. Dots reach into nested
data:
Event.info,Event.Orgc.name,Galaxy.name. - The names conditions use work as columns too:
eventinforeturns the event's info,orgthe creator organisation's name. Tagis one cell: the tag names, separated by commas.
Operators
| Condition | Meaning |
|---|---|
field = value, field != value | Equals, does not equal. <> is the same as !=. |
<, >, <=, >= | Numbers, timestamps, dates and datetimes only. |
field IN (v, ...), NOT IN | One of the values, none of them. |
field LIKE 'pattern', NOT LIKE | Text fields only. % at the start, the end or both: '%.example.com'. |
AND, OR, NOT, ( ) | Combine conditions. NOT binds tighter than AND, and AND tighter than OR. |
=andINcompare literally. A value starting or ending with%is refused there; useLIKE.!=,NOT IN,NOT LIKEandNOTalso match rows where the field is empty:comment NOT LIKE '%test%'includes attributes without a comment.
SELECT * FROM attributes
WHERE event_id = 4711 AND NOT (to_ids = TRUE OR comment LIKE '%false positive%')
LIMIT 100
Times
- Timestamps (
timestamp,event_timestamp,publish_timestamp) take a Unix time or a relative time: a number and one ofd,h,mors, in quotes.timestamp >= '7d'is the last seven days,'12h'the last twelve hours. - Dates (
date, the event date) take'YYYY-MM-DD'or a number of days, such as'30d'. - Datetimes (
createdandmodifiedon analyst data and collections) take what timestamps take, or'YYYY-MM-DD'and'YYYY-MM-DD HH:MM:SS'.
On attributes, events, objects and event_reports, when a statement has
no condition on timestamp, Huntbase adds the time-range pill's window as one. A condition on timestamp in the statement wins: to
search all time, add timestamp >= 0.
Sorting, paging and counting
ORDER BYtakes one or more fields, eachASC(the default) orDESC. Each table sorts by a fixed set of fields, whichDESCRIBElists;objectscan't be sorted. An unknown sort field is an error, rather than being ignored.LIMIT nasks fornrows, andOFFSET nskips rows before them.COUNT(*)returns the number of matching rows, counted by MISP, without fetching them. It needs aWHEREand takes noORDER BY,LIMITorOFFSET.
SELECT id, eventinfo, org, attribute_count FROM events
WHERE value = '198.51.100.23' AND published = TRUE
ORDER BY event_id DESC LIMIT 20
SELECT COUNT(*) FROM attributes WHERE type = 'domain' AND timestamp >= '30d'
A query returns at most 10,000 rows; a larger LIMIT is lowered to that, and
the run says so. MISP can also cap the rows per request for the key's role.
When it caps a page below your LIMIT, the run shows the rows it got and a
warning.
Discovery
SHOW TABLESlists the tables the connection's key may query.DESCRIBE table(orDESC table) asks MISP for a table's fields, each with its type and operators, the fieldsORDER BYtakes, and the limits in effect.
MISP limits how large a condition may be. By default: five levels of nesting,
50 conditions and operators, and 100 values in one IN. An administrator can
change them, and DESCRIBE shows the ones in effect.
Starter templates
A MISP connection ships with starter templates in Content library › Queries:
| Template | What it returns | Runs on |
|---|---|---|
| Look up indicators in MISP | The attributes whose value is one of the IPs, domains, URLs, email addresses or file hashes you give, with their event, IDS flag and tags. Nothing, if your MISP has never seen the values. | All versions |
| Network indicators from the last few days | IDS-flagged IP addresses, domains, host names and URLs added or changed recently, with their tags. The values can seed a retro-hunt in your logs. | All versions |
| Events with a tag | Events carrying a tag, such as tlp:clear or a threat-actor galaxy tag, with their organisation, threat level and attribute count. | All versions |
| Attributes of a MISP event | Every attribute of one event, whatever the time range. | All versions |
| Threat actor in the MISP galaxies | Threat Actor galaxy clusters whose name contains the text you give (bear finds Fancy Bear and Venomous Bear), with the tag that attaches them to events. Use the tag with Events with a tag to find the events. On MISP 3.0, add OR meta.synonyms LIKE '%bear%' in brackets to search the synonyms too. | All versions |
| Count of IDS attributes since | How many IDS-flagged attributes arrived in the last few days, counted by MISP. A quick check that your feeds and sharing partners are still delivering. | All versions |
Look up indicators in MISP is the starter: it is the one to run first on a new connection.
Scout and MispQL
During a hunt, Scout checks the IPs, domains, URLs and hashes it is working
with against your MISP live, in MispQL, alongside your other connected tools.
It runs one read-only lookup on attributes for up to 100 values at a time,
over all time rather than the hunt's window, because intel is worth finding
whatever its age. The lookup runs on every MISP version. Scout quotes the MISP
events, tags and TLP it finds as what your intel says about the values. A match in MISP is not a sighting:
whether a value was seen in your estate comes from your telemetry. See
Scout and MISP.
You can also ask Scout in plain English, such as "Which MISP events mention this domain?", and it writes the MispQL for you.
Use MispQL in a hunt.md playbook
In a hunt playbook written in
hunt.md, a mispql step can pull a
fresh indicator list from your MISP every time the hunt runs, and hand it to
the next step as a parameter. The list stays current, and each value keeps a
note of the MISP event it came from.
Declare the list as a typed parameter that the MispQL step fills, and point
the misp target at your MISP connection:
parameters:
intel_window: { type: duration, default: "30d" }
lookback: { type: duration, default: "14d" }
brute_force_ips:
type: list[ip]
from: { kind: misp-event, step: pull-brute-force-ips }
targets:
misp: { category: intel, name: MISP, huntbase: { product: misp-feed } }
siem: { category: siem, name: SIEM, huntbase: { product: azure_log_analytics } }
Then the MispQL step writes its first column to the list with
out=$brute_force_ips, and provenance=event_id records the event behind
each value:
## pull-brute-force-ips
```mispql target=misp params=(window=intel_window) out=$brute_force_ips provenance=event_id
SELECT value, event_id
FROM attributes
WHERE type IN ('ip-src', 'ip-dst')
AND tags IN ('misp-galaxy:threat-actor="TeamTNT"', 'circl:incident-classification="brute-force"')
AND to_ids = TRUE
AND timestamp >= '{{window}}'
```
## sweep-ssh-auth
```kql target=siem params=(days=lookback, ips=$brute_force_ips)
Syslog
| where TimeGenerated > ago({{days}})
| where Facility in ("auth", "authpriv") and ProcessName == "sshd"
| extend SrcIP = extract(@"from ((?:\d{1,3}\.){3}\d{1,3})", 1, SyslogMessage)
| where SrcIP in (split("{{ips}}", ","))
| summarize attempts = count(), successes = countif(SyslogMessage startswith "Accepted")
by SrcIP, Computer
```
The MispQL step uses only AND, IN, a tag match and a relative time, so it
runs on every MISP version. The second step can be any language your
connections serve, such as KQL, SPL, APL or a
Huntbase Lake search.
Errors
Huntbase checks a statement before anything is sent, and an error points at the text it is about. The run shows the line and column, the message, and the statement with the part it is about underlined:
Line 1, column 39: expected a value ('string', number, TRUE or FALSE), found domain (strings need quotes)
SELECT * FROM attributes WHERE type = domain LIMIT 10
^^^^^^
What only MISP can check, such as field names and the operators each field takes, comes back from MISP as an error per condition, placed on the statement the same way.
Next steps
- MISP — connect, versions and query mode
- Query languages
- Query workspace
- MispQL on GitHub — the MISP project's guide to the language