Regular Expression (Regex) Basics and Best Practices

In this post, we will show some basic examples of using Regular Expression Patterns (regex) in Tanium. Just learning a little bit of regex can give a lot of power to the Tanium operator.

What is regex? It is used in searches as a shorthand, or a “pattern”, to tell a computer what you are looking for. So for example, using regex you can tell a computer you want to search for any instance of Bat or Cat. There is more than one valid expression for that: (B|C)at or [BC]at.

Why use regex? Here are some Tanium use cases when you might use regex:

  • Grouping computers together dynamically. Select computers based on the third character in a computer name.

  • Efficiency. Search multiple text items with a single query (i.e. search for filenames with the words password, secret, or finance; you could also flag a file only if it contained any two of those words in any order.).

  • Being very specific with the data you want. Find instances of a product, such as Adobe, but not Adobe Acrobat or Adobe Reader.

Below are some basic things to know about regex functionality, and examples of how you would apply regex to your searches.

Text and Anchors

In regular expression matching we are examining whether a “search text” matches our “expression” (aka regex). The simplest example is a literal string match, so if you are searching for Cat, the regex could be the literal string Cat.

Note:

Regex can be specified as case sensitive or insensitive. In many places in Tanium case is treated as insensitive: Cat will also match cat.

Anchors are used to specify where the text should match.

  • ^ When used preceding the text, it indicates the text should be at the start of the line

  • $ When used after the text, this indicates the text should be at the end of the line

So if you wanted to match an instance of Cat where there was no additional text you could use the regex ^Cat$. This would match Cat but not Catastrophic.

Quantifiers

Quantifiers are used to specify how many times something must occur to match.

. Is a placeholder that will match any single character

  • ? Means to match zero or one time

  • * Means to match zero or more times

  • + Means to match 1 or more (at least 1)

  • Means to match exactly this many times

{n,m} Means to match between n and m times

\ Means a literal '.' ; you can escape any special character in regex (e.g. . ? * +) by preceding it with a '\ to remove the special meaning (E.g. \ would mean a literal '*' and not zero or more)

Examples:

  • ^Ex.$ will match Exp and Exl . Any character in the last position will match.

  • ^Ex?$ will match E and Ex but not Exp.

  • ^Ex*$ will match E and Ex and Exxxxxxxx. (E followed by 0 or more x’s)

  • ^Ex{5}$ will match only Exxxxx, not Exxx and not Exxxxx. (E followed by 5 x’s)

  • ^Ex{2,4}$ will match Exx and Exxxx but not Ex (only 1 x). (E followed by 2,3, or 4 x’s)

  • ^E\+$ will match E followed by 1 or more periods ('.')

Examples with Tanium Questions

Regular expressions in Tanium Questions need to search the whole line or response. Consider running processes: you want to match any processes with explor in the name (e.g. explorer.exe, iexplore.exe, etc.).

  • As a regular Tanium question, searching for explor will not work well because it is not the whole line:

    • Get running processes matching “explor” from all machines

  • Using a regex with a “.” placeholder will partly work: explorer.exe will match, but iexplore.exe will not, as we haven’t considered characters appearing before our match term:

    • Get running processes matching “explor.*” from all machines

  • It is best practice to use start and end anchors (^ and $). This requires that you also consider what could come before and after the text you are searching for:

    • Get running processes matching “^.*explor.*$” from all machines

    • This regex matches what we are looking for (all terms with explor in the name) and reads as: “start of the line (^) followed by zero or more repetitions of any character (.*) followed by the literal text explor followed by zero or more repetitions of any character (.*) followed by the end of the line ($)

Alternatives

Alternatives allow the user to set the scope of flexibility in the returned results.

  • | is shorthand for an “or” (aka “Alternation”)

  • [abc] matches a character in the brackets

  • [A-Z] matches a range of characters

  • [^n] means do not match n

Examples:

  • B|C would match the letter B or the letter C

  • (B|C)at would match Bat or Cat (the “()” limits the scope of the alternation, aka “grouping”)

  • [ABC]at would match Aat, Bat, or Cat

  • [C-M]at would match Cat, Dat, Eat...Mat; it would not match Nat

  • [^C]at would match Bat and Dat, but not Cat

In Tanium it is often useful to be able to search using number ranges. Regex can support this use case. For example, [0-9] will match any single digit 0 through 9. [1-9][0-9] will match between 10 and 99.

Best practices

  1. Test. Test in a lab or development environment before trying something in Production. You can use lab data to test the pattern of data and do not need to match exactly. You can also use test data on an online regex tester to test against many strings quickly.

  2. Use the keyword “match”, and use the question builder. In Tanium, questions use the keyword “match” instead of “contains” when using regex.

  3. Use Anchors. Using the regex syntax .*book.* is similar to using Tanium’s “contains”; it will find the term book in the middle of any string. If you are using the “match” keyword in Tanium, it is best practice to use anchors: ^book$ will match only the word book.

Final Thoughts

  • Remember the context of where you are searching. When using regex in a question, remember the regex needs to match the entire string.

  • Do not fear regex. Regex is a very valuable tool that can save you a lot of time and effort down the road.

  • Additional reading: This post is not a comprehensive resource for using regular expressions. A good resource for testing regular expressions is https://regex101.com/. A resource that is more instructive and allows students to test expressions as they read along is https://regexone.com/.

Fun Regex Questions to Try

For fun and an added challenge, try to figure out what these questions are looking for.

  • Get Computer Name matches "[\]+[0-9]{2,12}(\.*)?$" from all machines

  • Get Computer Name from all machines with Computer Name matches "^(win|sol-).*$"

  • Get Operating System from all machines with Operating System matches "^(redhat|centos|scientific).*$"

  • Get Computer Name from all machines with Computer Name matches "[\]+\?$"

  • Get Computer Name and Tanium Client IP Address from all machines with Tanium Client IP Address matches “^192\168\0\1[0-2]?[0-9]$”

  • Get Tanium Client Subnet from all machines with Tanium Client Subnet Matching “^.*/(2[5-9]|3[012])$”