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 & Reportingand enterindex=main host=<Private DNS>in the search input box. SelectLast 4 hoursfrom the time chooser pull-down and hit search.Private DNSis your intance's private DNS name. -
Narrow your search by adding the
sourcetypeoflinux_secureor thesourceof/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 Fieldsfrom the bottom left pane. -
Select any event from the table on the bottom pane and click
Nexton 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. ClickNexton the upper pane next toExtract Fields, clickNextagain. Before clickingFinish, copy theExtraction/Transformstring, it should look something like this:^(?:[^ \n]* ){7}(?P<invalid_username>[^ ]+) from (?P<remote_host>.+). ClickFinish. -
If you clicked
Finishbefore 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 & Reportingby selecting it underApps. Enterindex=main host=<Private DNS> sourcetype="linux_secure". Pipe the results to therexcommand, passing torexthe regular expression we just built. Note thatrextakes 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
rextosearchand 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
rexcommand to also extract valid SSH logins into thessh_messagefield.
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
statscommand extract a new field which keeps count ofvalidSSH 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
transposeto turn the resulting rows into columns. -
Select
Visualizationfrom the tabs below the search box. In the charts pull-down selectPie Chart. -
Select
Save Asand create a new dashboard with aDashboard Titleof your student ID. ClickSave>View Dashboard