Table of Contents
- Search for Invalid SSH Login Attempts
- Splunk Field Extraction
- Splunk Statistics
- Create a Splunk Alert
In this lab we will be searching for invalid SSH login attempts, extracting fields of interest and creating a dashboard.
Resources:
- http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Search
- http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Rex
- http://regexr.com/
- http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Stats
- http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Eval
Search for invalid SSH login attempts made against your instance.
-
Login to Splunk by opening the Splunk Console with your browser. Splunk Console: https://54.186.140.211/en-US/account/login?return_to=%2Fen-US%2F
-
Search for your instance's logs. Click
Search & Reporting
and enterindex=main host=<Private DNS>
in the search input box. SelectLast 4 hours
from the time chooser pull-down and hit search.Private DNS
is your intance's private DNS name. -
Narrow your search by adding the
sourcetype
oflinux_secure
or thesource
of/var/log/secure
. What is the difference between these? -
Add
"Invalid user " AND " from "
to the search string, hitEnter
. If you do not get any results, attempt to SSH into your instance with an invalid username/password.
Extract interesting fields (invalid_username
& remote_host
) from our search results (from previous section).
-
Select
+ Extract New Fields
from the bottom left pane. -
Select any event from the table on the bottom pane and click
Next
on the upper pane next toExtract Fields
, this should take you toSelect method
. -
Select
Regular Expression
, clickNext
. Select the username of the invalid user and name itinvalid_username
, clickAdd Extraction
. -
Select the IP address and name it remote_host, click
Add Extraction
. ClickNext
on the upper pane next toExtract Fields
, clickNext
again. Before clickingFinish
, copy theExtraction/Transform
string, it should look something like this:^(?:[^ \n]* ){7}(?P<invalid_username>[^ ]+) from (?P<remote_host>.+)
. ClickFinish
. -
If you clicked
Finish
before copying the extraction regular or you simply want to lookup what you just did, selectSettings
>Fields
>Field extractions
. Put your username in the search box in the upper right and hitEnter
. Copy the regular expression underExtraction/Transform
. -
Go to
Search & Reporting
by selecting it underApps
. Enterindex=main host=<Private DNS> sourcetype="linux_secure"
. Pipe the results to therex
command, passing torex
the regular expression we just built. Note thatrex
takes a regular expression surrounded by double-quotes. See resources above. -
List events containing only invalid users. You can do this by piping the results from
rex
tosearch
and passinginvalid_username=*
tosearch
.
Run statistics on valid vs invalid SSH login attempts.
- Modify the search query to also extract characters preceding invalid usernames into a field.
Your search query should look something like: index=main host=ip-10-0-0-0.us-west-2.compute.internal sourcetype="linux_secure" | rex "(?<ssh_message>Invalid user )(?P<invalid_username>[^ ]+) from (?P<remote_host>.+)" | search ssh_message=*
. Also select All time
from the time range picket.
- Count the invalid SSH login attempts.
Use stats
and eval
functions to count the number of times ssh_message
is equal to "Invalid user "
, name the resulting column invalid
.
Your new query should looks something like: index=main host=ip-10-0-0-0.us-west-2.compute.internal sourcetype="linux_secure"| rex "(?<ssh_message>Invalid user )(?P<username>[^ ]+) from (?P<remote_host>.+)" | stats count(eval(ssh_message="Invalid user ")) as invalid
- In the same search query modify the regular expression passed to the
rex
command to also extract valid SSH logins into thessh_message
field.
Extract Accepted publickey for
into the ssh_message
field by appending to the regular expression used above. This should look something like "(?<ssh_message>Accepted publickey for |Invalid user )(?P<username>[^ ]+) from (?P<remote_host>.+)"
.
- Now using the
stats
command extract a new field which keeps count ofvalid
SSH logins.
Add another count
section to the stats
command. This second count
should count the number of times ssh_message
is equal to Accepted publickey for
. The first count command can serve as an template for your second command.
Create a dashboard displaying valid vs invalid SSH login attempts.
-
Pipe the results to
transpose
to turn the resulting rows into columns. -
Select
Visualization
from the tabs below the search box. In the charts pull-down selectPie Chart
. -
Select
Save As
and create a new dashboard with aDashboard Title
of your student ID. ClickSave
>View Dashboard