Splunk Search

Create a field from a string

evang_26
Communicator

Hi users,

I have a big string in one field from which I want to extract specific values such as user and IP address and count based by that. As a reference of my logs take a look below.

Message: The user julie connected from 127.0.0.1 but failed an authentication attempt due to the following reason: The remote connection was denied because the user name and password combination you provided is not recognized, or the selected authentication protocol is not permitted on the remote access server.

The query that I created by without success is the following:

sourcetype="WinEventLog" *remote access* *failed* | rex field=Message "The user : (?<user>[a-z].*) connected from (?<ip>[1-9].*) but.*$"  | table user, ip

Do you have any suggestions??
Thanks in advance!

Tags (4)
0 Karma
1 Solution

tgow
Splunk Employee
Splunk Employee

The regex needs to look something like this:

^\w+\s+user\s+(?<user>\S+)\s+connected\s+from\s+(?<ip>\S+)\s+

So the search will be:

sourcetype="WinEventLog" remote access failed | rex field=Message "^\w+\s+user\s+(?<user>\S+)\s+connected\s+from\s+(?<ip>\S+)\s+"  | table user, ip

The capital "\S+" tells regex to grab everything that is not a space. You user names might have numbers in them for instance and you ip address has periods.

Hope that helps.

View solution in original post

evang_26
Communicator

You guys are GREAT! Fantastic! I was quite close then. However, give me a little more insight here.

Breaking down the expression "(?\S+)" to smaller pieces, why we use the "?" ? I know that this is used so as to define an optional previous character. However this is not the case here.

As for the "" obviously that's the way to create the field, so that's fine!

I appreciate your answear, again!

0 Karma

tgow
Splunk Employee
Splunk Employee

The regex needs to look something like this:

^\w+\s+user\s+(?<user>\S+)\s+connected\s+from\s+(?<ip>\S+)\s+

So the search will be:

sourcetype="WinEventLog" remote access failed | rex field=Message "^\w+\s+user\s+(?<user>\S+)\s+connected\s+from\s+(?<ip>\S+)\s+"  | table user, ip

The capital "\S+" tells regex to grab everything that is not a space. You user names might have numbers in them for instance and you ip address has periods.

Hope that helps.

evang_26
Communicator

You guys are GREAT! Fantastic! I was quite close then. However, give me a little more insight here.

Breaking down the expression "(?\S+)" to smaller pieces, why we use the "?" ? I know that this is used so as to define an optional previous character. However this is not the case here.

As for the "" obviously that's the way to create the field, so that's fine!

I appreciate your answear, again!

0 Karma

kristian_kolb
Ultra Champion

Yep, the colon after Message is not a word character (\w).

0 Karma

somesoni2
Revered Legend

The rex didn't work for me with sample data from evang_26. Updated rex which worked : "The user\s+(?\S+)\s+connected\s+from\s+(?\S+)\s+"

0 Karma
Get Updates on the Splunk Community!

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...

New Release | Splunk Cloud Platform 10.1.2507

Hello Splunk Community!We are thrilled to announce the General Availability of Splunk Cloud Platform 10.1.2507 ...

🌟 From Audit Chaos to Clarity: Welcoming Audit Trail v2

&#x1f5e3; You Spoke, We Listened  Audit Trail v2 wasn’t written in isolation—it was shaped by your voices.  In ...