I'm currently working with osquery and need some help in converting several queries that use regexp_like() to the equivalent regex_match() syntax. I have over 500 queries, and it's not practical to manually update each one. I am specifically working with ATC tables and need to use these queries in packs.
As an example, I have a query like this:
SELECT DISTINCT registry_key, act_timestamp FROM testregistry WHERE regexp_like (registry_key, '(?i)\\REGISTRY\\.*\\Services\\SharedAccess\\Parameters\\FirewallPolicy\\StandardProfile') AND lower(reg_val) = lower('EnableFirewall') AND lower(reg_val_data) = lower('0') AND DATE_STR_FILTER
I attempted to convert it by replacing regexp_like() with regex_match() and adding 0 as the index for the full match:
SELECT DISTINCT registry_key, act_timestamp FROM testregistry WHERE regex_match (registry_key, '(?i)\\REGISTRY\\.*\\Services\\SharedAccess\\Parameters\\FirewallPolicy\\StandardProfile', 0) AND lower(reg_val) = lower('EnableFirewall') AND lower(reg_val_data) = lower('0') AND DATE_STR_FILTER
However, this results in an "Invalid syntax" error.
Could someone provide guidance on the correct way to convert these queries? I appreciate any assistance or insights you can offer.
I never heard from osquery before, so I tried it out. It's a nice discovery! Thanks!
The function
regex_match()is built to return a value. So if you want to use it as a condition, you have to compare it with something. Example:This will return all services starting with the character
Aora.Another example to show that you can extract some data from a field and use it in your output:
This will list all services starting with
AorB(case insensitive) and extract the number in the service name and display it in a new field callednumber_in_name:Regex pattern to convert your SQL queries
The regex pattern, with the
x(extended) andi(case-insensitive) modifiers:Replacement:
Test it live here: https://regex101.com/r/3hMryB/3
I'm using PHP's PCRE engine, as it has the
xmodifier that helps you write regex patterns on multiple lines, with spaces, indentation and optional comments.I used named groups to capture the pieces from your SQL query as they are more readable than numbers in the replacement string.
It can get tricky to capture strings correctly because they can be single or double quoted (ex:
'hello'or"world"). And we also have to handle quotes inside of them. I see that in SQL you have to double them and not escape them with a backslash (ex:"Say ""Hello!""").Be also aware that the
(?i)modifier to make the pattern case-insensitive doesn't seem to work withREGEX_MATCH(). So this means that we have to rewrite some of the patterns. This could be done with some JS or PHP, by using a callback function during the replacement step. Detecting if(?i)is in your pattern and in this case convert all letters to a range with the lowercase and uppercase declinations (z->[zZ],é->[éÉ], etc).I wrote all that in JavaScript and you can run it directly here: