Use with the sqlite CLI, the Mihari.ipnb
Jupyter Notebook, Datasette or when developing Grafana dashboards.
Appended after the FROM
or INNER JOIN
statements, can be used to constrain results to a specific time period.
WHERE date(artifacts.created_at) = date('now')
SELECT
alerts.title AS "Alert Type",
COUNT(*) AS "Total"
FROM artifacts
INNER JOIN alerts ON alerts.id = artifacts.alert_id
WHERE date(artifacts.created_at) >= "2021-01-01"
AND date(artifacts.created_at) <= "2021-04-01"
GROUP BY
"Alert Type"
ORDER BY
"Total" DESC,
lower("Alert Type") ASC
WHERE date(artifacts.created_at) = date('now', '-7 day')
WHERE date(artifacts.created_at) = date('now', '-1 month')
WHERE date(artifacts.created_at) = date('now', '-1 year')
SELECT
strftime("%Y-%m-%d", alerts.created_at) AS "Date",
alerts.title AS "Alert Type",
artifacts.data AS "IP"
FROM artifacts
INNER JOIN alerts ON alerts.id = artifacts.alert_id
ORDER BY
"Date" DESC
SELECT
strftime("%Y-%m-%d", alerts.created_at) AS "Date",
alerts.title AS "Alert Type",
artifacts.data AS "IP",
artifacts.pDNS AS "pDNS, Last 10 Days"
FROM artifacts
INNER JOIN alerts ON alerts.id = artifacts.alert_id
WHERE pDNS IS NOT ""
AND pDNS IS NOT NULL
ORDER BY
"Date" DESC
SELECT
strftime("%Y-%m-%d", alerts.created_at) AS "Date",
alerts.title AS "Alert Type",
data AS "Website"
FROM artifacts
INNER JOIN alerts ON alerts.id = artifacts.alert_id
WHERE data_type = "url"
ORDER BY
"Date" DESC
SELECT
strftime("%Y-%m-%d", created_at) AS "Date",
count(strftime("%Y-%m-%d", created_at)) AS "Total"
FROM artifacts
GROUP BY
"Date"
ORDER BY
"Date" ASC
SELECT
strftime("%Y-%m", created_at) AS "Month",
count(strftime("%Y-%m", created_at)) AS "Count"
FROM artifacts
GROUP BY
"Month"
ORDER BY
"Month" ASC
SELECT
strftime("%Y", created_at) AS "Year",
count(strftime("%m", created_at)) AS "Count"
FROM artifacts
GROUP BY
"Year"
ORDER BY
"Year" ASC
SELECT
day AS "Day of Week",
count(*) AS "Total"
FROM artifacts
INNER JOIN date ON date.day_id = strftime("%w", created_at)
GROUP BY
"Day of Week"
ORDER BY
date.sorder ASC
SELECT
name AS "Tag",
count(*) AS "Total"
FROM artifacts
INNER JOIN alerts ON alerts.id = artifacts.alert_id
INNER JOIN taggings ON taggings.alert_id = alerts.id
INNER JOIN tags ON tags.id = taggings.tag_id
GROUP BY
"Tag"
ORDER BY
"Total" DESC
SELECT
alerts.title AS "Alert Type",
count(*) AS "Total"
FROM artifacts
INNER JOIN alerts ON alerts.id = artifacts.alert_id
GROUP BY
"Type"
ORDER BY
"Total" DESC
SELECT
title AS "Alert Type",
count(description) AS "Total Rules"
FROM (
SELECT DISTINCT
title,
description
FROM alerts)
GROUP BY
"Alert Type"
ORDER BY
lower("Alert Type") ASC
SELECT
countries.name AS "Country",
count(*) AS "Total"
FROM artifacts
INNER JOIN countries ON countries."alpha-2" = artifacts.country_code
GROUP BY
country_code
ORDER BY
"Total" DESC,
"Country" ASC
SELECT
countries.region AS "Region",
count(*) AS "Total"
FROM artifacts
INNER JOIN countries ON countries."alpha-2" = artifacts.country_code
GROUP BY
"Region"
ORDER BY
"Total" DESC,
"Region" ASC
SELECT
"sub-region" AS "Sub Region",
count(*) AS "Total"
FROM artifacts
INNER JOIN countries ON countries."alpha-2" = artifacts.country_code
GROUP BY
"Sub Region"
ORDER BY
"Total" DESC,
"Sub Region" ASC
SELECT
asn AS "ASN",
asn_name AS "ASN Name",
count(*) AS "Total"
FROM artifacts
GROUP BY
"ASN"
ORDER BY
"Total" DESC,
"ASN" ASC
SELECT
round(avg(count)) AS "Average"
FROM (
SELECT
strftime("%Y-%m-%d", created_at) AS "Day",
count(strftime("%Y-%m-%d", created_at)) AS "Count"
FROM artifacts
GROUP BY
"Day")
SELECT
round(avg(count)) AS "Average"
FROM (
SELECT
strftime("%Y-%m-%d", created_at) AS "Day",
count(strftime("%Y-%m-%d", created_at)) AS "Count"
FROM artifacts
WHERE date(artifacts.created_at) = date('now', '-7 day')
GROUP BY
"Day")
SELECT
tld AS "TLD",
count(*) AS "Total"
FROM domains
GROUP BY
"TLD"
ORDER BY
"Total" DESC,
"TLD" ASC
SELECT
strftime("%Y-%m-%d", created_at) AS "Date",
count(strftime("%Y-%m-%d", created_at)) AS "Total",
count(*) - LAG(count(strftime("%Y-%m-%d", created_at)), 1, 0)
OVER (
PARTITION BY "Date"
ORDER BY
"Total"
) AS "Δ"
FROM artifacts
GROUP BY
"Date"
ORDER BY
"Date" DESC
SELECT
title AS "Alert Type",
count(description) AS "Rules"
FROM (
SELECT DISTINCT
title,
description
FROM alerts)
GROUP BY
"Alert"
ORDER BY
"Alert" ASC
Helpful in understanding reliance on specific data sources.
SELECT
source AS "Provider",
count(*) AS "Total"
FROM alerts
INNER JOIN artifacts ON artifacts.alert_id = alerts.id
GROUP BY
"Provider"
ORDER BY
"Provider" ASC
Helpful in detecting API health concerns — including API quota exhaustion, and staleness in provider data.
SELECT
"Provider",
"Last Observed",
round(julianday(date("now")) - julianday("Last Observed")) AS "Days Since Last Alert"
FROM (
SELECT
source AS "Provider",
strftime("%Y-%m-%d", artifacts.created_at) AS "Last Observed"
FROM alerts
INNER JOIN artifacts ON artifacts.alert_id = alerts.id
ORDER BY
"Last Observed" DESC)
GROUP BY
"Provider"
ORDER BY
"Provider" ASC