Security

How do you capture login and logout times from the log?

dinakar407
New Member

Hi Splunk Experts,

We have the below log file

40312   [6]   DEBUG   2018-09-03 08:28:42.987   TM1.Login   Login attempt by client: user1
40312   [6]   DEBUG   2018-09-03 08:28:43.007   TM1.Login   Login Success: User user1
40312   [6]   DEBUG   2018-09-03 08:28:59.392   TM1.Login   Logout User user1

Could you please help me with the search command to extract login and log out time of any user.

I am looking for a table with Username LoginTime LogoutTime Duration.

Regards
Dinakar

Tags (2)
0 Karma
1 Solution

DEAD_BEEF
Builder

Hi @dinakar407, is this what you're looking for?

alt text

| makeresults 
| eval data="40312 [6] DEBUG 2018-09-03 08:28:42.987 TM1.Login Login attempt by client: user1;
 40312 [6] DEBUG 2018-09-03 08:28:43.007 TM1.Login Login Success: User user1;
 40312 [6] DEBUG 2018-09-03 18:13:40.136 TM1.Login Logout User user1" 
| makemv data delim=";" 
| mvexpand data 
| rename data as _raw 
| rex field=_raw "(?<loginTime>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}).*?Login Success: User (?<Username>.*)" 
| rex field=_raw "(?<logoutTime>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}).*?Logout User (?<Username>.*)" 
| reverse 
| transaction Username startswith="Login Success" endswith="Logout User" 
| eval login_e = strptime(loginTime, "%Y-%m-%d %T.%3Q") 
| eval logout_e = strptime(logoutTime, "%Y-%m-%d %T.%3Q") 
| eval Duration = strftime((logout_e - login_e), "%T.%3Q") 
| table Username loginTime logoutTime Duration

If you're looking to test it against your own data, just start with your initial search and then paste everything that starts with the rex row

| index=x sourcetype=x
| rex field=_raw "(?<loginTime>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}).*?Login Success: User (?<Username>.*)" 
| rex field=_raw "(?<logoutTime>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}).*?Logout User (?<Username>.*)" 
| reverse 
| transaction Username startswith="Login Success" endswith="Logout" 
| eval login_e = strptime(loginTime, "%Y-%m-%d %T.%3Q") 
| eval logout_e = strptime(logoutTime, "%Y-%m-%d %T.%3Q") 
| eval Duration = strftime((logout_e - login_e), "%T.%3Q") 
| table Username loginTime logoutTime Duration

View solution in original post

0 Karma

DEAD_BEEF
Builder

Hi @dinakar407, is this what you're looking for?

alt text

| makeresults 
| eval data="40312 [6] DEBUG 2018-09-03 08:28:42.987 TM1.Login Login attempt by client: user1;
 40312 [6] DEBUG 2018-09-03 08:28:43.007 TM1.Login Login Success: User user1;
 40312 [6] DEBUG 2018-09-03 18:13:40.136 TM1.Login Logout User user1" 
| makemv data delim=";" 
| mvexpand data 
| rename data as _raw 
| rex field=_raw "(?<loginTime>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}).*?Login Success: User (?<Username>.*)" 
| rex field=_raw "(?<logoutTime>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}).*?Logout User (?<Username>.*)" 
| reverse 
| transaction Username startswith="Login Success" endswith="Logout User" 
| eval login_e = strptime(loginTime, "%Y-%m-%d %T.%3Q") 
| eval logout_e = strptime(logoutTime, "%Y-%m-%d %T.%3Q") 
| eval Duration = strftime((logout_e - login_e), "%T.%3Q") 
| table Username loginTime logoutTime Duration

If you're looking to test it against your own data, just start with your initial search and then paste everything that starts with the rex row

| index=x sourcetype=x
| rex field=_raw "(?<loginTime>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}).*?Login Success: User (?<Username>.*)" 
| rex field=_raw "(?<logoutTime>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}).*?Logout User (?<Username>.*)" 
| reverse 
| transaction Username startswith="Login Success" endswith="Logout" 
| eval login_e = strptime(loginTime, "%Y-%m-%d %T.%3Q") 
| eval logout_e = strptime(logoutTime, "%Y-%m-%d %T.%3Q") 
| eval Duration = strftime((logout_e - login_e), "%T.%3Q") 
| table Username loginTime logoutTime Duration
0 Karma

niketn
Legend

@dinakar407, you can try transaction command

Following is a run anywhere search example based on your sample data which extracts user action as Login Success: and Logout, and then creates a transaction based on user field which contains user id. Commands from | makeresults to | reverse generate sample data. You can replace with your current search instead.

| makeresults
| eval data="40312 [6] DEBUG 2018-09-03 08:28:42.987 TM1.Login Login attempt by client: user1;
40312 [6] DEBUG 2018-09-03 08:28:43.007 TM1.Login Login Success: User user1;
40312 [6] DEBUG 2018-09-03 08:28:59.392 TM1.Login Logout User user1"
| makemv data delim=";"
| mvexpand data
| rename data as _raw
| rex "DEBUG (?<_time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3})"
| eval _time=strptime(_time,"%Y-%m-%d %H:%M:%S.%3N")
| reverse
| rex "Login (?<action>(Login Success:|Logout))\sUser (?<user>[^\s]+)"
| transaction user startswith="action=Login"  endswith="action=Logout" maxevents=2 keepevicted=t

PS: Depending on your use case stats/streamstats might work better than transaction. For example whether you want to pull the latest login/logout per use or all.

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma
Get Updates on the Splunk Community!

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...

Splunk APM: New Product Features + Community Office Hours Recap!

Howdy Splunk Community! Over the past few months, we’ve had a lot going on in the world of Splunk Application ...

Index This | Forward, I’m heavy; backward, I’m not. What am I?

April 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...