Jasper Alblas
Jasper Alblas
Welcome to this walkthrough of the OSquery Room on TryHackMe. Let’s cover the basics of Osquery, a Osquery is an open-source agent created by Facebook in 2014. It converts the operating system into a relational database, which allows us to query data with SQL.
Room URL:
https://tryhackme.com/room/osqueryf8
I am making these walkthroughs to keep myself motivated to learn cyber security, and ensure that I remember the knowledge gained by these challenges on HTB and THM. Join me on learning cyber security. I will try and explain concepts as I go, to differentiate myself from other walkthroughs.
Osquery is an open-source tool developed by Facebook in 2014 that transforms an operating system into a relational database, allowing users to query system data using SQL. It is useful for security roles like analysts, incident responders, and threat hunters, and works across Windows, Linux, macOS, and FreeBSD.
Learning Objectives in this introduction:
Note: SQL knowledge is helpful for using Osquery effectively.
Answer: No answer needed
The virtual machine attached to this room already has Osquery installed and configured for you on Windows and Linux. Before proceeding, start the attached VM and use the following credentials to connect. The VM will be accessible in the split screen on the right side. In case the VM is not visible, use the blue Show Split View button at the top-right of the page.
Click on the powershell terminal
pinned at the taskbar and enter osqueryi
to enter the interactive mode of osquery.
Machine IP: MACHINE_IP
Username: James
Password: thm_4n6
Answer: No answer needed
Getting Started with Osquery Interactive Mode
osqueryi
in the terminal..help
to list available meta-commands for navigating and interacting with the tool.Useful Meta-Commands
.tables
– Lists all available tables..tables user
lists all tables with “user” in their name..schema table_name
– Displays the schema (columns and data types) of a table..schema users
shows columns like uid
, username
, directory
, etc.Querying Data
SELECT column1, column2 FROM table;
SELECT gid, uid, description, username, directory FROM users;
Display Modes
.mode
, such as:csv
, column
, line
, list
, pretty
(default)Tip: For full reference, check Osquery’s online schema API for a complete list of tables, columns, and descriptions.
Open up a terminal and run osqueryi to start Osquery. Afterwards run:
.table process
There are three results.
Answer: 3
To show the schema of the processes table, we run:
.schema processes
Here we can see a column called pid, which must mean process ID.
Answer: pid
Simply run the help command:
.help
There are 5 output display modes for the .mode command.
Answer: 5
We can look up the schema documentation at https://osquery.io/schema/5.5.1. Here we can see the different tables we can query, and the columns each includes.
Here are the main points:
.table
command in interactive mode.With this layout, you can confidently explore Osquery schemas and find the information you need.
Simply open the documentation page:
https://osquery.io/schema/5.5.1
The version should be set to 5.5.1 so all we have to do is select both Linux and Windows on the table compatibility dropdown, as shown below.
There are 56 common tables.
Answer: 56
Simply select MAC OS instead of Windows and Mac, and you should see 180 tables.
Answer: 180
Select the Windows Operating system. Now have a scroll through the list of tables on the left side of the screen. You should see a table called programs.
Answer: programs
Once more, look at the table list. Luckily, the names are pretty descriptive, so the table is called registry.
The tables includes a data column, which contains the data contents of registry values.
Answer: data
Osquery SQL Overview:
SELECT
statement since Osquery is read-only by default.UPDATE
, DELETE
) are only usable with run-time tables or supported extensions.Basic SQL Structure in Osquery:
SELECT
statementFROM
clause;
) at the endprograms
table:SELECT * FROM programs LIMIT 1;
Selecting Specific Data:
SELECT name, version, install_location, install_date FROM programs LIMIT 1;
Counting Entries:
count(*)
to get total entries:SELECT count(*) FROM programs;
Filtering with WHERE:
WHERE
:SELECT * FROM users WHERE username='James';
=
, <>
, >
, BETWEEN
, LIKE
, %
, and _
.Wildcards in Paths:
%
: one level match%%
: recursive match/Users/%/Library/%%
→ recursively monitors all user Library foldersRequired WHERE Clause:
file
) require a WHERE
clause. Otherwise, an error is returned.Joining Tables:
SELECT p.pid, p.name, p.path, u.username FROM processes p JOIN users u ON u.uid = p.uid LIMIT 10;
Easy one if you know SQL. It is actually one of the examples from the theory. Simply use the following command:
SELECT count(*) from programs;
Answer: 19
There are quite some ways to do this, but I simply selected the username & description columns from the users table:
SELECT username, description FROM users;
--Alternatively filter on name
SELECT username, description FROM users WHERE username = 'James';
Answer: Creative Artist
Simply run the query as mentioned in the question.
Look for the user for which the path ends with 1009, and find the corresponding name.
Answer: S-1-5-21-1966530601-3185510712-10604624-1009
Simply run the query again.
The answer expects the path of this Url Search Hook extension.
Answer: C:\Windows\System32\ieframe.dll |
Once more, run the query. The answer is the name of the program returned: Wireshark 3.6.8 64-bit.
Answer: Wireshark 3.6.8 64-bit
Now that we have explored various tables, learned how to create search queries, and ask questions from the operating system, it’s time for a challenge. Use OSquery to examine the host and answer the following questions.
You can use the .tables command for this, but I recommend look at the documentation again for Windows:
https://osquery.io/schema/5.5.1
I will be honest, this took me a long time to find. I first thought it was processes, but nope. After some searching and guessing I found out it was userassist.
UserAssist Registry Key tracks when a user executes an application from Windows Explorer.Improve this Description on Github
COLUMN | TYPE | DESCRIPTION |
path | text | Application file path. |
last_execution_time | bigint | Most recent time application was executed. |
count | integer | Number of times the application has been executed. |
sid | text | User SID. |
Answer: userassist
For this we can simply select all rows for the earlier found tables.
SELECT * FROM userassist;
It took me some patience, but in the above screenshot, on the second to last line you will see the DiskWipe.exe process.
Answer: DiskWipe.exe
Take a look at the documentation again, and you should find the table called programs.
https://osquery.io/schema/5.5.1/#programs
Time for some SQL ninja skills using the LIKE operator to find all rows which include VPN in its name.
SELECT * FROM programs WHERE name LIKE '%VPN%';
Here we find ProtonVPN.
Answer: ProtonVPN
For this we can use the COUNT(*) operator to count all rows in the services table.
SELECT COUNT(*) FROM services;
Answer: 214
Time for another WHERE clause:
SELECT * FROM autoexec WHERE name LIKE '%.bat';
Answer: batstartup.bat
See above.
Answer: C:\Users\James\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\batstartup.bat
Congratulations on completing Osquery: The Basics. I really enjoyed this quick room, as I have a background as data engineer. Therefore, writing these SQL queries felt very natural to me. Quite a cool technology this Osquery!
Come back soon for more walkthroughs of rooms on TryHackMe and HackTheBox, and other Cybersecurity discussions.
Find more of my walkthroughs here.
You are welcome to comment on this post, or share my post with friends. I would be even more grateful if you support me by buying me a cup of coffee:
I learned a lot through HackTheBox’s Academy. If you want to sign up, you can get extra cubes, and support me in the process, if you use the following link: