Path: blob/master/machine-learning-notebooks/Guided Investigation - Anomalous users generated by Isolation Forest Model for SOC Analysts.ipynb
3250 views
Guided Investigation - Anomalous users generated by Isolation Forest Model for SOC Analysts
Details...
Notebook Version: 1.0
Python Version: Python 3.8 - AzureML
Required Packages: Msticpy, Msticnb, matplotlib, ipywidgets
Platforms Supported: Azure Machine Learning Notebooks
Data Source Required: Yes
Data Source: SecurityEvents
Description
In this sample guided scenario notebook, we will demonstrate how to hunt for anamalous user logon activity using Isolation forest model.
We will start with reading historical windows logon data from Microsoft Sentinel workspace, then we will prepocess the dataset using series of data preparation steps such as aggregation, summarization, data type conversion, deriving new fields etc. Then we will perform Feature Engineering and select subset of features from the data prepared from previous steps to create isolation forest model. Finally, we will run the model to score the results and identify anomalies with higher score.
The isolation forest algorithm will split the data into two parts based on random threshold value. It will recursively continue the splitting until each data point is isolated. Then we will detect anomalies using isolation (how far a data point is to the rest of the data). To detect an anomaly the isolation forest takes the average path length (number of splits to isolate a sample) of all the trees for a given instance and uses this to determine if it is an anomaly (average shorter path lengths indicate anomalies)

Image Credits: Detecting and preventing abuse on LinkedIn using isolation forests
Please run the cells sequentially to avoid errors.
Please do not use "run all cells".
Notebook initialization
The next cell:
Checks for the correct Python version
Checks versions and optionally installs required packages
Imports the required packages into the notebook
Sets a number of configuration options.
This should complete without errors. If you encounter errors or warnings look at the following two notebooks:
If you are running in the Microsoft Sentinel Notebooks environment (Azure Notebooks or Azure ML) you can run live versions of these notebooks:
You may also need to do some additional configuration to successfully use functions such as Threat Intelligence service lookup and Geo IP lookup. There are more details about this in the ConfiguringNotebookEnvironment notebook and in these documents:
Authentication to LA Workspace
Use the following syntax if you are authenticating using an Azure Active Directory AppId and Secret:
instead of
Note: you may occasionally see a JavaScript error displayed at the end of the authentication - you can safely ignore this.
On successful authentication you should see a popup schema button. To find your Workspace Id go to Log Analytics. Look at the workspace properties to find the ID.
Data Preparation
In this step, we will prepare the Windows logon events and do some preprocessing before we do data modelling. For this case, we are primarily considering logon event ids 4624, 4625 with specific logon type.
4624 and 4625 events are related to Successful sign in and Failed Sign-in. You can check more about the event Ids in below links.
Historical Data Processing
For this model, we can consider upto 21 days of historical data. If you want to generate this anomalies on recurrent basis then depending on scale and volume of the data, you can set up intermediate pipeline to save historical data into custom table and load results from it. Check out the blog for ways to export historical data at scale using notebook Export Historical Log Data from Microsoft Sentinel For this demo, we are retrieving data from the original table. We also have provided demo dataset if you want to test the notebook without connecting to your workspace.
Feature Engineering
In this step, we are creating additional features/columns.
We have selected 4 columns(features) with numeric data points
FailedLogons
SuccessfulLogons
ComputersSuccessfulAccess
SrcIpSuccessfulAccess
and also deriving additional columns by calculating mean, standard deviation and zscores on each of them. Converting to zscores is not necessary for numerical features as Isolation forest are scale invariant but this pre-processing is done so as to use these features later in the visualizations such as PCA. We have also done log scaling as part of data pre-processing steps which is not required but based on various data studies in production environment we have seen it gives finer results. You can skip or add this step based on data study and analyzing results.
Data Modelling
In this step we will specify features to be modelled and run isolation forest algorithm against the data.
Isolation Forest Anomaly detection
In this step, we will select subset of features generated from previous step and use it for data modelling. We will also use Isolation Forest model on the data with selected features and calculate the anomalies.
Investigating Anamalous User logon sessions
Based on the output of local outlier feature importance
Gather the respective values from the historical dataset
Analyze/plot the results
Investigate for any malicious activity
You can also further use msticpy nblets to gather account information from various datasets. Below is an example for account nblet
Enter account name and query time window
Type the account name that you want to search for and the time bounds over which you want to search.
You can specify the account as:
a simple user name (e.g.
alice)a user principal name (
[email protected])a qualified windows user name
mydomain\alice
In the second two cases the domain qualifier will be stripped off before the search. The search is not case sensitive and will match full substrings. E.g. bob will match domain\bob and [email protected] but not bobg or bo.
Windows Logon Summary for the Account
Windows Failed Logon Summary
Conclusion
In this notebook, we started with windows event logs login data with the goal of finding users with anomalous login patterns. This notebook is targetted towards SOC Analysts who can use it to take anomalous user logon session generated from Isolation Forest model and investigate further. We can use the AccountSummary notebooklet to gather various information associated with the account such as associated alerts, bookmarks , entries from ActiveDirectory, Windows and Linux Host logs.