Lesson 5.6

SQL when you need it

Query Arcsentry evidence with read-only SQL: the scope table, what is allowed, and when SQL is the better choice than AQL.

10 minExperienced analysts
At a glance for experienced analysts
  • Switch the query bar to SQL. Read from the table scope, which already holds the host, event types and time range.
  • Only one SELECT or WITH statement runs; anything that changes data is refused.
  • Use SQL for percentiles, set operations such as EXCEPT, expanding lists and correlated subqueries. Use AQL for everything else.
Command line
arcsentry query --case <uuid> --evidence <id> --event-type logs_nginx --sql 'SELECT http_status_code, count(*) AS n FROM scope GROUP BY http_status_code ORDER BY n DESC'

AQL answers almost every investigative question. SQL is there for the few it cannot express, and for analysts who think in SQL already.

The scope table

Pick the host and event types as usual, switch the query bar to SQL, and read from scope:

SELECT http_status_code, count(*) AS n
FROM scope
GROUP BY http_status_code
ORDER BY n DESC

scope holds exactly the records Hunt would query in AQL: the evidence, event types and time window you picked. The columns are the same fields you see in the Table.

The rules

  • One statement, SELECT or WITH. Nothing that writes.
  • Read from scope, and only from scope.
  • The answer appears in the same results area. Rows with a time get a histogram, totals do not.

When SQL is the better tool

Need Why SQL
Percentile ranks and quartiles Window functions over the whole scope
What is in A but not in B EXCEPT and INTERSECT
One row per value in a list field SQL can expand a list into rows
Compare each row with the whole set Correlated subqueries, such as “above the average”

If you can write it in AQL, prefer AQL: it is shorter, it is what rules (arrows) use, and it is what plain-English questions produce.

Check yourself

1. In SQL mode, how do you limit a query to web01's nginx logs?
2. What happens to DELETE FROM scope?

Try it

On web01, with only the nginx event type picked, count requests per HTTP status code in SQL. Which statuses are there, and how many 403s?

Show the answer

SELECT http_status_code, count(*) AS n FROM scope GROUP BY http_status_code ORDER BY n DESC returns 200: 9,864, 404: 734, 301: 164, 304: 132, 302: 115, 403: 21, and 3 with no status. 594 of the 734 404s come from the scanner, 203.0.113.45, probing for paths that do not exist.

Progress is saved in this browser only.