|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Notebook Title\n", |
| 8 | + " <details>\n", |
| 9 | + " <summary> <u>Details...</u></summary>\n", |
| 10 | + "\n", |
| 11 | + " **Notebook Version:** 1.0<br>\n", |
| 12 | + " **Python Version:** Python 3.6 (including Python 3.6 - AzureML)<br>\n", |
| 13 | + " **Required Packages**: kqlmagic, msticpy, pandas, pandas_bokeh, numpy, matplotlib, networkx, seaborn, datetime, ipywidgets, ipython, dnspython, ipwhois, folium, maxminddb_geolite2<br>\n", |
| 14 | + " **Platforms Supported**:\n", |
| 15 | + " - Azure Notebooks Free Compute\n", |
| 16 | + " - Azure Notebooks DSVM\n", |
| 17 | + " - OS Independent\n", |
| 18 | + "\n", |
| 19 | + " **Data Sources Required**:\n", |
| 20 | + " - Log Analytics/Azure Sentinel - Syslog, Secuirty Alerts, Auditd, Azure Network Analytics.\n", |
| 21 | + " - (Optional) - AlienVault OTX (requires account and API key)\n", |
| 22 | + " </details>\n", |
| 23 | + "\n", |
| 24 | + "Notebook description...." |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "markdown", |
| 29 | + "metadata": { |
| 30 | + "toc": true |
| 31 | + }, |
| 32 | + "source": [ |
| 33 | + "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n", |
| 34 | + "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Notebook-Title\" data-toc-modified-id=\"Notebook-Title-1\">Notebook Title</a></span><ul class=\"toc-item\"><li><span><a href=\"#Notebook-Initialization\" data-toc-modified-id=\"Notebook-Initialization-1.1\">Notebook Initialization</a></span><ul class=\"toc-item\"><li><span><a href=\"#Get-WorkspaceId-and-Authenticate-to-Log-Analytics\" data-toc-modified-id=\"Get-WorkspaceId-and-Authenticate-to-Log-Analytics-1.1.1\">Get WorkspaceId and Authenticate to Log Analytics</a></span></li></ul></li><li><span><a href=\"#Example-query\" data-toc-modified-id=\"Example-query-1.2\">Example query</a></span></li></ul></li></ul></div>" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "markdown", |
| 39 | + "metadata": {}, |
| 40 | + "source": [ |
| 41 | + "---\n", |
| 42 | + "### Notebook initialization\n", |
| 43 | + "The next cell:\n", |
| 44 | + "- Checks for the correct Python version\n", |
| 45 | + "- Checks versions and optionally installs required packages\n", |
| 46 | + "- Imports the required packages into the notebook\n", |
| 47 | + "- Sets a number of configuration options.\n", |
| 48 | + "\n", |
| 49 | + "This should complete without errors. If you encounter errors or warnings look at the following two notebooks:\n", |
| 50 | + "- [TroubleShootingNotebooks](https://github.com/Azure/Azure-Sentinel-Notebooks/blob/master/TroubleShootingNotebooks.ipynb)\n", |
| 51 | + "- [ConfiguringNotebookEnvironment](https://github.com/Azure/Azure-Sentinel-Notebooks/blob/master/ConfiguringNotebookEnvironment.ipynb)\n", |
| 52 | + "\n", |
| 53 | + "If you are running in the Azure Sentinel Notebooks environment (Azure Notebooks or Azure ML) you can run live versions of these notebooks:\n", |
| 54 | + "- [Run TroubleShootingNotebooks](./TroubleShootingNotebooks.ipynb)\n", |
| 55 | + "- [Run ConfiguringNotebookEnvironment](./ConfiguringNotebookEnvironment.ipynb)\n", |
| 56 | + "\n", |
| 57 | + "You may also need to do some additional configuration to successfully use functions such as Threat Intelligence service lookup and Geo IP lookup. \n", |
| 58 | + "There are more details about this in the `ConfiguringNotebookEnvironment` notebook and in these documents:\n", |
| 59 | + "- [msticpy configuration](https://msticpy.readthedocs.io/en/latest/getting_started/msticpyconfig.html)\n", |
| 60 | + "- [Threat intelligence provider configuration](https://msticpy.readthedocs.io/en/latest/data_acquisition/TIProviders.html#configuration-file)\n" |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "code", |
| 65 | + "execution_count": null, |
| 66 | + "metadata": { |
| 67 | + "ExecuteTime": { |
| 68 | + "end_time": "2020-05-16T02:00:38.505687Z", |
| 69 | + "start_time": "2020-05-16T02:00:31.727307Z" |
| 70 | + } |
| 71 | + }, |
| 72 | + "outputs": [], |
| 73 | + "source": [ |
| 74 | + "from pathlib import Path\n", |
| 75 | + "import os\n", |
| 76 | + "import sys\n", |
| 77 | + "import warnings\n", |
| 78 | + "from IPython.display import display, HTML, Markdown\n", |
| 79 | + "\n", |
| 80 | + "REQ_PYTHON_VER=(3, 6)\n", |
| 81 | + "REQ_MSTICPY_VER=(0, 5, 0)\n", |
| 82 | + "\n", |
| 83 | + "display(HTML(\"<h3>Starting Notebook setup...</h3>\"))\n", |
| 84 | + "if Path(\"./utils/nb_check.py\").is_file():\n", |
| 85 | + " from utils.nb_check import check_python_ver, check_mp_ver\n", |
| 86 | + "\n", |
| 87 | + " check_python_ver(min_py_ver=REQ_PYTHON_VER)\n", |
| 88 | + " try:\n", |
| 89 | + " check_mp_ver(min_msticpy_ver=REQ_MSTICPY_VER)\n", |
| 90 | + " except ImportError:\n", |
| 91 | + " !pip install --user --upgrade msticpy\n", |
| 92 | + " if \"msticpy\" in sys.modules:\n", |
| 93 | + " importlib.reload(msticpy)\n", |
| 94 | + " else:\n", |
| 95 | + " import msticpy\n", |
| 96 | + " check_mp_ver(MSTICPY_REQ_VERSION)\n", |
| 97 | + " \n", |
| 98 | + "from msticpy.nbtools import nbinit\n", |
| 99 | + "nbinit.init_notebook(\n", |
| 100 | + " namespace=globals(),\n", |
| 101 | + " extra_imports=[\"ipwhois, IPWhois\"]\n", |
| 102 | + ");\n" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "markdown", |
| 107 | + "metadata": {}, |
| 108 | + "source": [ |
| 109 | + "<a></a>[Contents](#toc)\n", |
| 110 | + "### Get WorkspaceId and Authenticate to Log Analytics \n", |
| 111 | + "<details>\n", |
| 112 | + " <summary> <u>Details...</u></summary>\n", |
| 113 | + "If you are using user/device authentication, run the following cell. \n", |
| 114 | + "- Click the 'Copy code to clipboard and authenticate' button.\n", |
| 115 | + "- This will pop up an Azure Active Directory authentication dialog (in a new tab or browser window). The device code will have been copied to the clipboard. \n", |
| 116 | + "- Select the text box and paste (Ctrl-V/Cmd-V) the copied value. \n", |
| 117 | + "- You should then be redirected to a user authentication page where you should authenticate with a user account that has permission to query your Log Analytics workspace.\n", |
| 118 | + "\n", |
| 119 | + "Use the following syntax if you are authenticating using an Azure Active Directory AppId and Secret:\n", |
| 120 | + "```\n", |
| 121 | + "%kql loganalytics://tenant(aad_tenant).workspace(WORKSPACE_ID).clientid(client_id).clientsecret(client_secret)\n", |
| 122 | + "```\n", |
| 123 | + "instead of\n", |
| 124 | + "```\n", |
| 125 | + "%kql loganalytics://code().workspace(WORKSPACE_ID)\n", |
| 126 | + "```\n", |
| 127 | + "\n", |
| 128 | + "Note: you may occasionally see a JavaScript error displayed at the end of the authentication - you can safely ignore this.<br>\n", |
| 129 | + "On successful authentication you should see a ```popup schema``` button.\n", |
| 130 | + "To find your Workspace Id go to [Log Analytics](https://ms.portal.azure.com/#blade/HubsExtension/Resources/resourceType/Microsoft.OperationalInsights%2Fworkspaces). Look at the workspace properties to find the ID.\n", |
| 131 | + "</details>" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "code", |
| 136 | + "execution_count": null, |
| 137 | + "metadata": {}, |
| 138 | + "outputs": [], |
| 139 | + "source": [ |
| 140 | + "# List Workspaces available\n", |
| 141 | + "# WorkspaceConfig().list_workspaces()\n", |
| 142 | + "\n", |
| 143 | + "# To use a specific workspace create a WorkspaceConfig using the\n", |
| 144 | + "# workspace parameter\n", |
| 145 | + "# ws_config = WorkspaceConfig(workspace='MyWorkspace')" |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "cell_type": "code", |
| 150 | + "execution_count": null, |
| 151 | + "metadata": { |
| 152 | + "ExecuteTime": { |
| 153 | + "end_time": "2020-05-16T02:01:11.022700Z", |
| 154 | + "start_time": "2020-05-16T02:00:49.394760Z" |
| 155 | + } |
| 156 | + }, |
| 157 | + "outputs": [], |
| 158 | + "source": [ |
| 159 | + "# Authentication\n", |
| 160 | + "ws_config = WorkspaceConfig()\n", |
| 161 | + "qry_prov = QueryProvider(data_environment=\"LogAnalytics\")\n", |
| 162 | + "qry_prov.connect(connection_str=ws_config.code_connect_str)\n", |
| 163 | + "table_index = qry_prov.schema_tables" |
| 164 | + ] |
| 165 | + }, |
| 166 | + { |
| 167 | + "cell_type": "code", |
| 168 | + "execution_count": null, |
| 169 | + "metadata": { |
| 170 | + "ExecuteTime": { |
| 171 | + "end_time": "2020-05-16T02:03:12.112983Z", |
| 172 | + "start_time": "2020-05-16T02:03:12.055984Z" |
| 173 | + } |
| 174 | + }, |
| 175 | + "outputs": [], |
| 176 | + "source": [ |
| 177 | + "query_scope = nbwidgets.QueryTime(auto_display=True)" |
| 178 | + ] |
| 179 | + }, |
| 180 | + { |
| 181 | + "cell_type": "markdown", |
| 182 | + "metadata": {}, |
| 183 | + "source": [ |
| 184 | + "## Example query" |
| 185 | + ] |
| 186 | + }, |
| 187 | + { |
| 188 | + "cell_type": "code", |
| 189 | + "execution_count": null, |
| 190 | + "metadata": { |
| 191 | + "ExecuteTime": { |
| 192 | + "end_time": "2020-05-16T02:03:25.227614Z", |
| 193 | + "start_time": "2020-05-16T02:03:21.291120Z" |
| 194 | + } |
| 195 | + }, |
| 196 | + "outputs": [], |
| 197 | + "source": [ |
| 198 | + "qry_prov.SecurityAlert.list_alerts(query_scope)" |
| 199 | + ] |
| 200 | + } |
| 201 | + ], |
| 202 | + "metadata": { |
| 203 | + "hide_input": false, |
| 204 | + "kernelspec": { |
| 205 | + "display_name": "Python 3.6", |
| 206 | + "language": "python", |
| 207 | + "name": "python36" |
| 208 | + }, |
| 209 | + "language_info": { |
| 210 | + "codemirror_mode": { |
| 211 | + "name": "ipython", |
| 212 | + "version": 3 |
| 213 | + }, |
| 214 | + "file_extension": ".py", |
| 215 | + "mimetype": "text/x-python", |
| 216 | + "name": "python", |
| 217 | + "nbconvert_exporter": "python", |
| 218 | + "pygments_lexer": "ipython3", |
| 219 | + "version": "3.7.6" |
| 220 | + }, |
| 221 | + "latex_envs": { |
| 222 | + "LaTeX_envs_menu_present": true, |
| 223 | + "autoclose": false, |
| 224 | + "autocomplete": true, |
| 225 | + "bibliofile": "biblio.bib", |
| 226 | + "cite_by": "apalike", |
| 227 | + "current_citInitial": 1, |
| 228 | + "eqLabelWithNumbers": true, |
| 229 | + "eqNumInitial": 1, |
| 230 | + "hotkeys": { |
| 231 | + "equation": "Ctrl-E", |
| 232 | + "itemize": "Ctrl-I" |
| 233 | + }, |
| 234 | + "labels_anchors": false, |
| 235 | + "latex_user_defs": false, |
| 236 | + "report_style_numbering": false, |
| 237 | + "user_envs_cfg": false |
| 238 | + }, |
| 239 | + "toc": { |
| 240 | + "base_numbering": 1, |
| 241 | + "nav_menu": {}, |
| 242 | + "number_sections": false, |
| 243 | + "sideBar": true, |
| 244 | + "skip_h1_title": false, |
| 245 | + "title_cell": "Table of Contents", |
| 246 | + "title_sidebar": "Contents", |
| 247 | + "toc_cell": true, |
| 248 | + "toc_position": {}, |
| 249 | + "toc_section_display": true, |
| 250 | + "toc_window_display": true |
| 251 | + }, |
| 252 | + "varInspector": { |
| 253 | + "cols": { |
| 254 | + "lenName": 16, |
| 255 | + "lenType": 16, |
| 256 | + "lenVar": 40 |
| 257 | + }, |
| 258 | + "kernels_config": { |
| 259 | + "python": { |
| 260 | + "delete_cmd_postfix": "", |
| 261 | + "delete_cmd_prefix": "del ", |
| 262 | + "library": "var_list.py", |
| 263 | + "varRefreshCmd": "print(var_dic_list())" |
| 264 | + }, |
| 265 | + "r": { |
| 266 | + "delete_cmd_postfix": ") ", |
| 267 | + "delete_cmd_prefix": "rm(", |
| 268 | + "library": "var_list.r", |
| 269 | + "varRefreshCmd": "cat(var_dic_list()) " |
| 270 | + } |
| 271 | + }, |
| 272 | + "types_to_exclude": [ |
| 273 | + "module", |
| 274 | + "function", |
| 275 | + "builtin_function_or_method", |
| 276 | + "instance", |
| 277 | + "_Feature" |
| 278 | + ], |
| 279 | + "window_display": false |
| 280 | + }, |
| 281 | + "widgets": { |
| 282 | + "application/vnd.jupyter.widget-state+json": { |
| 283 | + "state": {}, |
| 284 | + "version_major": 2, |
| 285 | + "version_minor": 0 |
| 286 | + } |
| 287 | + } |
| 288 | + }, |
| 289 | + "nbformat": 4, |
| 290 | + "nbformat_minor": 4 |
| 291 | +} |
0 commit comments