JQ: Another Tool We Thought We Knew

Published: 2023-07-24
Last Updated: 2023-07-24 14:15:25 UTC
by Rob VandenBrink (Version: 1)
2 comment(s)

So often you'll see folks (me included) use "jq" to take an unformatted JSON mess and turn it into a readable output.  For instance, last thursday we used the Shodan API to dump about 650k of host info like this:
curl -s -k "https://api.shodan.io/shodan/host/%1?key=%shodan-api-key%" | jq

In other words, up to today, I've just used jq as a JSON "prettifier".

At some point (OK, TIL), I finally clued into the fact that that the "q" in "jq" stood for "query"

First, let's simplify things first by making a file that we can play with (see Thursday's diary - https://isc.sans.edu/diary/Shodans+API+For+The+Recon+Win/30050/ - for details on the API call below):

curl -s -k "https://api.shodan.io/shodan/host/" > isc.txt

Let's use jq to query / extract the "ports" array in the file:

type isc.txt | jq ".ports"
.. and so on (123 open ports)

printing these without the carriage returns gets it all on one page, sometimes that's important:

type isc.txt | jq ".ports" --compact-output

Let's extract both the subdomains and hostnames? (often these are the same):
type isc.txt | jq ".domains,.hostnames"

At some point, you'll find that the IP addresses returned by shodan are typically in decimal.  No problem, convert decimal value to hex, then convert each octet back to digital and stuff the dots in!  Or you can just ask for both the ip and ip_str values:

type isc.txt | jq ".ip,.ip_str"

How about just dumping out the keys that you can mess with?

type isc.txt |  jq "keys"

There's way more to jq - you can execute scripts, add and delete keys, sort output or do math on the various values.  From the "query" perspective, you can treat your JSON input very much like a SQL database - you can use statements like select, index and join, which should all look very familiar.
You can also write scripts for jq to execute.  The scripts have all the scripty things you'd expect: if/then/else, try/catch boolean operators, regex support, text manipulation operators and so on. 
If you have jq installed, typing "man jq" will give you several pages of possibilties, even "jq --help" will get you started.  Googling "man jq" will give you the same if you don't have it installed yet.

For me, basic queries do the job most days (which is what was discussed above) - if I need more I tend to use other scripting solutions, most days bash, python or powershell.  But (just like most of us do with AWK), I'm just scratching the surface of what jq can do.

If you've done something cool with jq, please share in our comment form!  

Rob VandenBrink

Keywords: jq linux tools
2 comment(s)


jq is one of my favorite CLI utilities. We use it extensively in SANS SEC510: Public Cloud Security: AWS, Azure, and GCP. This query is probably the most advanced one in the course and the gnarliest one I have ever written. It counts the number of Google Cloud VPC Flow Logs for outbound traffic that was sent over the same protocol to the same IP and port, ordering the results based on the number of instances descending:

jq -r 'group_by(.jsonPayload.connection.dest_ip, .jsonPayload.connection.dest_port, .jsonPayload.connection.protocol)[] | {"dest_ip": .[0].jsonPayload.connection.dest_ip, "dest_port": .[0].jsonPayload.connection.dest_port, "protocol": .[0].jsonPayload.connection.protocol, "count": length}' | jq -s '. | sort_by(-.count)'
I see that jq has been ported to Cygwin. That can be useful!

Diary Archives