Splunk Search

How to use rex to perform field extraction from the end of the field as opposed to the beginning

thomas_porter
Explorer

I want to extract the top level domain from the CN field of a certificate in Splunk. The CN field may have multiple levels. I only want the top level domain (e.g.: mysite.com), as opposed to the full CN (e.g.: server.content.mysite.com). The CN can have multiple levels. I only want to extract the last something dot something. Any suggestions?

0 Karma
1 Solution

acfecondo75
Path Finder

You can anchor your regex to the end of the line using $. Find a period followed by anything that isn't a period followed by a period followed by anything that isn't a period anchored to the end of the line. See below:

.(?[^.]+.[^.]+)$

While that answers your question, the better solution that I would suggest is leveraging the URLToolbox app (https://splunkbase.splunk.com/app/2734/). Determining TLD and other subcomponents or stats around URLs can be pretty complicated and this app provides a lot of tools to assist with this so you don't have to reinvent the wheel. When you download the app, there's a README that will explain how the app works and what parts would be most relevant to you.

View solution in original post

thomas_porter
Explorer

Thank you for the quick response.

0 Karma

acfecondo75
Path Finder

You can anchor your regex to the end of the line using $. Find a period followed by anything that isn't a period followed by a period followed by anything that isn't a period anchored to the end of the line. See below:

.(?[^.]+.[^.]+)$

While that answers your question, the better solution that I would suggest is leveraging the URLToolbox app (https://splunkbase.splunk.com/app/2734/). Determining TLD and other subcomponents or stats around URLs can be pretty complicated and this app provides a lot of tools to assist with this so you don't have to reinvent the wheel. When you download the app, there's a README that will explain how the app works and what parts would be most relevant to you.

jpolvino
Builder

This will match the last 2 elements of a CN:

| rex "^.*?(?<theField>[^.]+\.\w+)$"

See regex101: https://regex101.com/r/ev3p1K/2

0 Karma
Get Updates on the Splunk Community!

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...

They're back! Join the SplunkTrust and MVP at .conf24

With our highly anticipated annual conference, .conf, comes the fez-wearers you can trust! The SplunkTrust, as ...

Enterprise Security Content Update (ESCU) | New Releases

Last month, the Splunk Threat Research Team had two releases of new security content via the Enterprise ...