Home
%3CLINGO-SUB%20id%3D%22lingo-sub-294434%22%20slang%3D%22en-US%22%3EAutomating%20Security%20Operations%20Using%20Windows%20Defender%20ATP%20APIs%20with%20Python%20and%20Jupyter%20Notebooks%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-294434%22%20slang%3D%22en-US%22%3E%3CH1%20id%3D%22toc-hId-1849529449%22%20id%3D%22toc-hId-1901367716%22%3EAutomating%20Security%20Operations%20Using%20Windows%20Defender%20ATP%20APIs%20with%20Python%20and%20Jupyter%20Notebooks%3C%2FH1%3E%0A%3CH2%20id%3D%22toc-hId--899141017%22%20id%3D%22toc-hId--847302750%22%3EIntroduction%3C%2FH2%3E%0A%3CP%3EWindows%20Defender%20ATP%20provides%20a%20great%20hunting%20experience%20out%20of%20the%20box.%20Analysts%20can%20search%20over%20process%20events%2C%20network%20events%2C%20logons%2C%20registry%20activity%2C%20and%20more.%26nbsp%3B%20In%20this%20post%2C%20I%20will%20show%20how%20analysts%20can%20go%20even%20further%20by%20using%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fwindows%2Fsecurity%2Fthreat-protection%2Fwindows-defender-atp%2Fapis-intro%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ethe%20WDATP%20APIs%3C%2FA%3E%3C%2FSPAN%3E.%20If%20any%20of%20the%20following%20scenarios%20are%20interesting%20to%20your%20security%20operations%20team%2C%20then%20this%20post%20is%20for%20you%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EAutomate%20common%20investigative%20steps%20such%20as%20gathering%20additional%20host%20and%20network%20details%2C%20triggering%20host%20actions%20like%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fwindows%2Fsecurity%2Fthreat-protection%2Fwindows-defender-atp%2Fcollect-investigation-package-windows-defender-advanced-threat-protection-new%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ecollecting%20an%20investigative%20package%3C%2FA%3E%3C%2FSPAN%3E%2C%20or%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fwindows%2Fsecurity%2Fthreat-protection%2Fwindows-defender-atp%2Frun-av-scan-windows-defender-advanced-threat-protection-new%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Erunning%20anti-virus%20scans%3C%2FA%3E%3C%2FSPAN%3E.%3C%2FLI%3E%0A%3CLI%3EQuery%20WDATP%20collected%20data%20about%20machines%2C%20users%2C%20domains%2C%20IPs%2C%20and%20files%2C%20enrich%20it%20with%203rd%20party%20services%20such%20as%20VirusTotal%2C%20and%20visualize%20the%20results.%3C%2FLI%3E%0A%3CLI%3EPerform%20hunting%20tasks%20by%20searching%20across%20the%20rich%20data%20collected%20by%20WDATP%20to%20find%20new%20malicious%20activity.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3ENormally%20when%20one%20hears%20the%20word%20%E2%80%9CAPIs%E2%80%9D%2C%20one%20thinks%20of%20a%20developer%20feature%20that%20requires%20compilers%20and%20SDKs.%20This%20post%20will%20show%20two%20features%20that%20make%20these%20APIs%20approachable%20to%20SOC%20analysts%E2%80%94Python%20support%20and%20Jupyter%20Notebooks.%26nbsp%3B%20Even%20though%20this%20post%20focuses%20on%20Python%2C%20you%20can%20call%20the%20APIs%20from%20PowerShell%2C%20C%23%2C%20or%20any%20language%20that%20can%20call%20REST%20endpoints.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-843669318%22%20id%3D%22toc-hId-895507585%22%3EGetting%20Started%3C%2FH2%3E%0A%3CP%3EThe%20first%20step%20is%20deciding%20on%20how%20you%20want%20to%20access%20the%20APIs.%20WDATP%20uses%20standard%20OAuth2.0%20authentication.%26nbsp%3B%20To%20get%20started%2C%20perform%20the%20following%20steps%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ERegister%20an%20application%20with%20Azure%20Active%20Directory.%20This%20allows%20you%20to%20assign%20the%20desired%20permissions%20to%20the%20app.%20You%20can%20create%20apps%20that%20only%20read%20alerts%2C%20run%20advanced%20queries%2C%20collect%20forensics%2C%20or%20many%20other%20combinations%20of%20permissions.%3C%2FLI%3E%0A%3CLI%3EDecide%20on%20your%20access%20model%3A%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fwindows%2Fsecurity%2Fthreat-protection%2Fwindows-defender-atp%2Fexposed-apis-create-app-webapp%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ewithout%20a%20user%3C%2FA%3E%3C%2FSPAN%3E%20(like%20a%20background%20service)%20or%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fwindows%2Fsecurity%2Fthreat-protection%2Fwindows-defender-atp%2Fexposed-apis-create-app-nativeapp%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eon%20behalf%20of%20a%20user%3C%2FA%3E%3C%2FSPAN%3E.%20Following%20the%20steps%20in%20those%20links%20will%20provide%20you%20with%20an%20access%20token%20to%20call%20the%20APIs.%3C%2FLI%3E%0A%3CLI%3EUse%20the%20token%20to%20call%20Windows%20Defender%20ATP%20APIs.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH2%20id%3D%22toc-hId--1708487643%22%20id%3D%22toc-hId--1656649376%22%3EAPI%20Functionality%3C%2FH2%3E%0A%3CP%3EA%20list%20of%20the%20exposed%20APIs%20can%20be%20found%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fwindows%2Fsecurity%2Fthreat-protection%2Fwindows-defender-atp%2Fexposed-apis-list%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E%3C%2FSPAN%3E.%20In%20this%20post%2C%20we%E2%80%99ll%20focus%20on%20running%20some%20advanced%20hunting%20queries%2C%20and%20then%20calling%20the%20API%20to%20get%20further%20information%20on%20machines%20and%20users.%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3EAdvanced%20Hunting%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3ERun%20queries%20from%20API.%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3EAlerts%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3ERun%20API%20calls%20such%20as%20get%20alerts%2C%20alert%20information%20by%20ID%2C%20alert%20related%20actor%20information%2C%20alert%20related%20IP%20information%2C%20and%20alert%20related%20machine%20information.%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3EDomain%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3ERun%20API%20calls%20such%20as%20get%20domain%20related%20machines%2C%20domain%20related%20machines%2C%20statistics%2C%20and%20check%20if%20a%20domain%20is%20seen%20in%20your%20organization.%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3EFile%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3ERun%20API%20calls%20such%20as%20get%20file%20information%2C%20file%20related%20alerts%2C%20file%20related%20machines%2C%20and%20file%20statistics.%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3EIP%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3ERun%20API%20calls%20such%20as%20get%20IP%20related%20alerts%2C%20IP%20related%20machines%2C%20IP%20statistics%2C%20and%20check%20if%20and%20IP%20is%20seen%20in%20your%20organization.%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3EMachines%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3ERun%20API%20calls%20such%20as%20find%20machine%20information%20by%20IP%2C%20get%20machines%2C%20get%20machines%20by%20ID%2C%20information%20about%20logged%20on%20users%2C%20and%20alerts%20related%20to%20a%20given%20machine%20ID.%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3EUser%3C%2FP%3E%0A%3C%2FTD%3E%0A%3CTD%3E%3CP%3ERun%20API%20calls%20such%20as%20get%20alert%20related%20user%20information%2C%20user%20information%2C%20user%20related%20alerts%2C%20and%20user%20related%20machines.%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CH3%20id%3D%22toc-hId--162190813%22%20id%3D%22toc-hId--110352546%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CH3%20id%3D%22toc-hId-1580619522%22%20id%3D%22toc-hId-1632457789%22%3EAuthenticate%20to%20WDATP%3C%2FH3%3E%0A%3CP%3EAfter%20registering%20your%20app%2C%20you%20have%20all%20you%20need%20to%20call%20the%20APIs%3A%20your%20WDATP%20Tenant%20ID%2C%20an%20App%20Id%20and%20App%20Secret%20created%20during%20registration.%26nbsp%3B%20Here%20is%20the%20code%20to%20authenticate%20to%20the%20WDATP%20endpoint%3A%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3Eimport%20json%3C%2FP%3E%0A%3CP%3Eimport%20urllib.request%3C%2FP%3E%0A%3CP%3Eimport%20urllib.parse%3C%2FP%3E%0A%3CP%3Edef%20wdatp_get_AAD_token(tenantId%2C%20appId%2C%20appSecret)%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20url%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Flogin.windows.net%2F%25s%2Foauth2%2Ftoken%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Flogin.windows.net%2F%25s%2Foauth2%2Ftoken%3C%2FA%3E%22%20%25%20(tenantId)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20resourceAppIdUri%20%3D%20'%3CA%20href%3D%22https%3A%2F%2Fapi.securitycenter.windows.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fapi.securitycenter.windows.com%3C%2FA%3E'%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20body%20%3D%20%7B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20'resource'%20%3A%20resourceAppIdUri%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20'client_id'%20%3A%20appId%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20'client_secret'%20%3A%20appSecret%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20'grant_type'%20%3A%20'client_credentials'%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20data%20%3D%20urllib.parse.urlencode(body).encode(%22utf-8%22)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20req%20%3D%20urllib.request.Request(url%2C%20data)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20response%20%3D%20urllib.request.urlopen(req)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20jsonResponse%20%3D%20json.loads(response.read())%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20aadToken%20%3D%20jsonResponse%5B%22access_token%22%5D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20return%20aadToken%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CH3%20id%3D%22toc-hId--971537439%22%20id%3D%22toc-hId--919699172%22%3EGet%20Alerts%3C%2FH3%3E%0A%3CP%3EThis%20code%20shows%20calling%20the%20API%20to%20get%20recent%20alerts.%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3Edef%20wdatp_get_alerts(aadToken)%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20url%20%3D%20%22%3CA%20href%3D%22https%3A%2F%2Fapi.securitycenter.windows.com%2Fapi%2Falerts%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fapi.securitycenter.windows.com%2Fapi%2Falerts%3C%2FA%3E%22%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20headers%20%3D%20%7B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20'Content-Type'%20%3A%20'application%2Fjson'%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20'Accept'%20%3A%20'application%2Fjson'%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20'Authorization'%20%3A%20%22Bearer%20%22%20%2B%20aadToken%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20req%20%3D%20urllib.request.Request(url%2C%20headers%3Dheaders)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20response%20%3D%20urllib.request.urlopen(req)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20jsonResponse%20%3D%20json.loads(response.read())%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20return%20jsonResponse%5B%22value%22%5D%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3ENow%20that%20you%20know%20the%20basics%20of%20getting%20up%20and%20going%2C%20let%E2%80%99s%20walk%20through%20a%20few%20different%20scenarios.%20To%20make%20it%20easy%20to%20repeat%20these%20scenarios%2C%20we%20will%20use%20a%20Jupyter%20notebook%20to%20encapsulate%20them.%20There%20is%20nothing%20about%20the%20APIs%20that%20requires%20Jupyter%2C%20but%20you%20may%20find%20it%20to%20be%20a%20handy%20tool%20when%20working%20with%20Python%20and%20the%20APIs.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-967786401%22%20id%3D%22toc-hId-1019624668%22%3EIntroducing%20Jupyter%20Notebooks%3C%2FH2%3E%0A%3CP%3EIf%20you%E2%80%99re%20familiar%20with%20Jupyter%20Notebooks%2C%20you%20can%20skip%20this%20paragraph.%26nbsp%3B%20If%20not%2C%20%3CSPAN%3E%3CA%20href%3D%22http%3A%2F%2Fjupyter.org%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EJupyter%20Notebooks%3C%2FA%3E%3C%2FSPAN%3E%20are%20an%20open%20source%20project%20designed%20to%20make%20interactive%20computing%20and%20sharing%20re-usable%20analysis%20easier.%20There%20are%20over%203%20million%20notebooks%20shared%20on%20GitHub.%20There%20is%20an%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fconferences.oreilly.com%2Fjupyter%2Fjup-ny%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eannual%20conference%3C%2FA%3E%3C%2FSPAN%3E%20and%20support%20for%20both%20%3CSPAN%3E%3CA%20href%3D%22http%3A%2F%2Fjupyter.org%2Finstall%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Elocal%3C%2FA%3E%3C%2FSPAN%3E%20and%20cloud%20versions%20by%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fnotebooks.azure.com%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eevery%3C%2FA%3E%3C%2FSPAN%3E%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fcloud.google.com%2Fdatalab%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Emajor%3C%2FA%3E%3C%2FSPAN%3E%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.aws.amazon.com%2Fdlami%2Flatest%2Fdevguide%2Fsetup-jupyter.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ecloud%3C%2FA%3E%3C%2FSPAN%3E%20provider.%20While%20many%20use%20cases%20for%20notebooks%20surround%20data%20science%20and%20machine%20learning%2C%20they%20are%20an%20excellent%20infosec%20investigative%20tool%20for%20exploration%2C%20visualization%2C%20and%20analysis.%20Let%E2%80%99s%20jump%20right%20in!%26nbsp%3B%20If%20you%20want%20to%20follow%20along%2C%20download%20the%20notebook%20from%20the%20WDATP%20GitHub%3A%20%3CA%20title%3D%22WDATP%20API%20Jupyter%20Notebook%20%5BGitHub%5D%22%20href%3D%22https%3A%2F%2Fgithub.com%2FMicrosoft%2FWindowsDefenderATP-Hunting-Queries%2Fblob%2Fmaster%2FNotebooks%2FWDATP%2520APIs%2520Demo%2520Notebook.ipynb%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EWDATP%20API%20Jupyter%20Notebook%3C%2FA%3E.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1584370560%22%20id%3D%22toc-hId--1532532293%22%3EJupyter%20Basics%3C%2FH2%3E%0A%3CP%3EPlace%20the%20notebook%20you%20downloaded%20from%20GitHub%20in%20the%20notebook%20directory.%20You%20can%20find%20the%20default%20notebook%20location%20by%20looking%20at%20the%20output%20from%20launching%20Jupyter%3A%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%26gt%3B%20C%3A%5CAnaconda3%5CScripts%5Cjupyter.exe%20notebook%20--notebook-dir%20c%3A%5Chome%5Cjupyter%3C%2FP%3E%0A%3CP%3E%5BI%2009%3A29%3A34.126%20NotebookApp%5D%20Serving%20notebooks%20from%20local%20directory%3A%20c%3A%5Chome%5Cjupyter%3C%2FP%3E%0A%3CP%3E%5BI%2009%3A29%3A34.126%20NotebookApp%5D%200%20active%20kernels%3C%2FP%3E%0A%3CP%3E%5BC%2009%3A29%3A34.126%20NotebookApp%5D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Copy%2Fpaste%20this%20URL%20into%20your%20browser%20when%20you%20connect%20for%20the%20first%20time%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20to%20login%20with%20a%20token%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3CSPAN%3E%3CA%20href%3D%22http%3A%2F%2Flocalhost%3A8888%2F%3Ftoken%3D%253caccesss%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Flocalhost%3A8888%2F%3Ftoken%3D%3CACCESS%3E%3C%2FACCESS%3E%3C%2FA%3E%3C%2FSPAN%3E%20token%26gt%3B%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3ETo%20orient%20first%20time%20users%2C%20here%20are%20the%20basics.%26nbsp%3B%20A%20notebook%20is%20composed%20of%20input%20and%20output%20cells.%26nbsp%3B%20In%20the%20example%20below%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EInput%20line%20%232%20shows%20printing%20%E2%80%9Chello%20world%E2%80%9D.%20To%20execute%20a%20cell%2C%20put%20the%20cursor%20in%20the%20cell%20and%20hit%20Shift%2BEnter.%20This%20will%20run%20the%20cell%20and%20move%20the%20input%20focus%20to%20the%20next%20input%20cell.%3C%2FLI%3E%0A%3CLI%3ELine%20%233%20shows%20performing%20a%20calculation.%20This%20is%20a%20Python%203%20notebook%2C%20so%20it%20is%20the%20%E2%80%9CPython%20kernel%E2%80%9D%20that%20is%20doing%20the%20work.%3C%2FLI%3E%0A%3CLI%3ELine%20%234%20shows%20a%20feature%20of%20interactive%20Python.%20Python%20stores%20the%20output%20of%20the%20previous%20command%20in%20an%20automatic%20variable%20named%20underscore%20(%E2%80%98_%E2%80%99)%3C%2FLI%3E%0A%3CLI%3EYou%20can%20execute%20shell%20commands%20by%20prefixing%20them%20with%20a%20bang%20(%E2%80%98!%E2%80%99)%3C%2FLI%3E%0A%3CLI%3EJupyter%20has%20a%20way%20to%20provide%20convenience%20commands%20through%20a%20feature%20called%20%E2%80%9Cmagics%E2%80%9D.%20You%20can%20invoke%20a%20magic%20by%20prefixing%20it%20with%20%25%20or%20%25%25.%20The%20timeit%20magic%20runs%20the%20command%20in%20the%20cell%20several%20times%20and%20provides%20an%20average%20time.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH3%20id%3D%22toc-hId--38073730%22%20id%3D%22toc-hId-13764537%22%3E%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F61248iE2D7E3DDBBCBBA3A%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22about_jupyter.png%22%20title%3D%22about_jupyter.png%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EJupyter%20basics%3C%2FSPAN%3E%3C%2FSPAN%3EInstalling%20the%20sample%20from%20GitHub%3C%2FH3%3E%0A%3CP%3ETo%20install%20the%20sample%20code%2C%20run%20the%20following%20cell%20by%20hitting%20Shift%2BEnter.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F61259i7C8C48BAB90574E0%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22jupyter_wrapper_code.png%22%20title%3D%22jupyter_wrapper_code.png%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3ERunning%20the%20WDATP%20wrapper%20sample%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1508440621%22%20id%3D%22toc-hId--1456602354%22%3ECalling%20the%20APIs%3C%2FH2%3E%0A%3CP%3EInitialize%20the%20WDATP%20endpoint%20by%20passing%20in%20your%20tenant%20Id%2C%20and%20app%20information.%20Then%20query%20for%20the%20top%205%20alerts.%20The%20notebook%20will%20automatically%20format%20the%20results%20in%20a%20HTML%20table.%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3Ewdatp_api%20%3D%20WDATP(tenantId%2C%20appId%2C%20appSecret%2C%20vt_api_key)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Ewdatp_api.alerts(filterstr%20%3D%20%22%24top%3D5%22)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20926px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F61257i79FF4F350A6B0F3E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22jupyter_top_alerts.png%22%20title%3D%22jupyter_top_alerts.png%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EInstantiating%20the%20WDATP%20API%20endpoint%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CH2%20id%3D%22toc-hId-234369714%22%20id%3D%22toc-hId-286207981%22%3EGetting%20Alerts%3C%2FH2%3E%0A%3CP%3ELet%E2%80%99s%20start%20by%20getting%20some%20alerts.%26nbsp%3B%26nbsp%3B%20You%20can%20use%20the%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fwindows%2Fsecurity%2Fthreat-protection%2Fwindows-defender-atp%2Fexposed-apis-odata-samples%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Estandard%20OData%20syntax%3C%2FA%3E%3C%2FSPAN%3E%20when%20querying%20some%20of%20the%20WDATP%20entities.%3C%2FP%3E%0A%3CP%3EBy%20default%2C%20this%20sample%20saves%20the%20results%20from%20API%20calls%20in%20a%20convenient%20tabular%20data%20structure%20called%20a%20Pandas%20DataFrame.%20Pandas%20is%20an%20open%20source%20package%20tailored%20for%20working%20with%20structured%20data.%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%23%20Let's%20select%20just%20the%20columns%20we%20want%20to%20view%20using%20OData%3C%2FP%3E%0A%3CP%3Ewdatp_api.alerts(filterstr%20%3D%20%22%24select%3DalertCreationTime%2Ccategory%2Ctitle%2Cdescription%2Cid%2Cseverity%26amp%3B%24filter%3DSeverity%20eq%20'High'%26amp%3B%24top%3D5%22)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23%20let's%20assign%20the%20API%20results%20to%20a%20variable%20so%20we%20can%20process%20them%20without%20making%20an%20API%20call%20each%20time%3C%2FP%3E%0A%3CP%3Edf%20%3D%20wdatp_api.alerts(filterstr%20%3D%20%22%24top%3D100%22)%3C%2FP%3E%0A%3CP%3Eprint(%22number%20of%20rows%20returned%20%3D%20%25d%22%20%25%20len(df))%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23%20show%20all%20the%20columns%20in%20the%20alert%3C%2FP%3E%0A%3CP%3Edf.columns%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23%20Let's%20select%20just%20the%20columns%20we%20want%20to%20view.%20This%20uses%20the%20Pandas%20syntax%20for%20selecting%20columns%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Edf%5B%5B'alertCreationTime'%2C'category'%2C'description'%2C'id'%2C'severity'%5D%5D.head(5)%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F61251iA1B16F489A917A6C%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22jupyter_alert_DF.png%22%20title%3D%22jupyter_alert_DF.png%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EAlerts%20to%20a%20data%20frame%3C%2FSPAN%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F61249i46F69A6944B72EA5%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22jupyter_alert_DF_2.png%22%20title%3D%22jupyter_alert_DF_2.png%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EInteracting%20with%20a%20data%20frame%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3ETo%20show%20the%20power%20of%20using%20Python%20and%20open%20source%20packages%20like%20Pandas%2C%20we%E2%80%99ll%20show%20how%20to%20do%20a%20simple%20count%20of%20alert%20frequency%20and%20render%20the%20results%20in%20a%20pie%20chart.%20This%20also%20shows%20that%20the%20notebook%20can%20include%20pictures%20and%20not%20just%20text%20output.%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%23%20let's%20group%20them%20by%20title%20and%20count%20their%20frequency%3C%2FP%3E%0A%3CP%3Edf%5B'title'%5D%20%3D%20df%5B'title'%5D.apply(lambda%20t%3A%20'Windows%20Defender%20AV%20detection'%20if%20t.startswith('Windows%20Defender%20AV%20detected%20')%20else%20t)%3C%2FP%3E%0A%3CP%3Edf1%20%3D%20df%5B%5B'id'%2C'title'%5D%5D.groupby('title').count().rename(index%3Dstr%2C%20columns%3D%7B%22id%22%3A%22Alert%20Count%22%7D)%3C%2FP%3E%0A%3CP%3Edf1%20%3D%20df1.sort_values(%5B'Alert%20Count'%5D%2C%20ascending%3DFalse)%3C%2FP%3E%0A%3CP%3Edf1%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%23%20show%20a%20quick%20pie%20chart%20using%20matplotlib%3C%2FP%3E%0A%3CP%3E%25matplotlib%20inline%3C%2FP%3E%0A%3CP%3Eplt%20%3D%20df1.plot.pie(y%3D0%2C%20figsize%3D(5%2C%205)%2C%20legend%3DFalse%2C%20autopct%3D'%251.0f%25%25')%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F61256iFFBCA54C40938A57%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22jupyter_pie.png%22%20title%3D%22jupyter_pie.png%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EDataframe%20manipulation%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1977180049%22%20id%3D%22toc-hId-2029018316%22%3EThe%20Role%20of%20Magics%3C%2FH2%3E%0A%3CP%3EThere%20is%20a%20time%20and%20place%20for%20code%2C%20but%20sometimes%20you%20want%20a%20simpler%20way%20to%20access%20data.%20The%20API%20wrapper%20in%20this%20sample%20has%20several%20Jupyter%20magics%20created%20for%20just%20this%20purpose.%20Magics%20are%20just%20shortcuts%20to%20invoking%20code%20on%20your%20data.%26nbsp%3B%20Use%20the%20%25wdatp_alert%20magic%20with%20a%20valid%20Alert%20ID%20from%20your%20WDATP%20data.%20The%20below%20command%20also%20shows%20assigning%20the%20alert%20data%20to%20a%20variable%20(alert_df)%20and%20then%20rendering%20it%20in%20a%20vertical%20view%20by%20invoking%20the%20Pandas%20transpose%20function%20on%20it%20(alert_df.T).%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%25wdatp_alert%20636740134254933385_-1113968221%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%23%20output%20from%20the%20previous%20command%20is%20stored%20in%20an%20automatic%20variable%2C%20the%20understore%20_%3C%2FP%3E%0A%3CP%3Ealert_df%20%3D%20_%3C%2FP%3E%0A%3CP%3Ealert_df.T%26nbsp%3B%20%23%20Transpose%20rows%20and%20columns%20for%20an%20easy%20way%20to%20view%20the%20alert%20details%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F61254i11B6597EFFDC664F%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22jupyter_magic.png%22%20title%3D%22jupyter_magic.png%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EJupyter%20WDATP%20Magics%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3EThe%20below%20example%20shows%20invoking%20the%20wdapt_ip%20magic.%20There%20are%20two%20kinds%20of%20Jupyter%20magics.%20Line%20magics%20start%20with%20a%20single%20%25%20and%20operate%20on%20the%20input%20right%20after%20the%20magic%20name.%20The%20%25wdapt_alert%20command%20we%20just%20invoked%20is%20an%20example%20of%20that.%26nbsp%3B%20By%20prefixing%20a%20magic%20with%20two%20%25%20signs%2C%20it%20is%20invoked%20as%20a%20cell%20magic%20and%20operates%20on%20the%20entire%20contents%20of%20the%20cell.%26nbsp%3B%20One%20feature%20of%20%25%25wdatp_ip%20is%20that%20it%20will%20scan%20through%20the%20cell%20text%20and%20use%20a%20regular%20expression%20to%20find%20any%20data%20that%20resembles%20an%20IPV4%20address.%20This%20is%20handy%20where%20you%20might%20have%20an%20email%20or%20webpage%20with%20the%20IPs%20you%20need%20to%20query.%20While%20you%20can%20copy%20and%20paste%20them%20individually%2C%20it%20is%20sometimes%20faster%20to%20just%20grab%20the%20entire%20paragraph%20containing%20them%2C%20paste%20them%20into%20a%20cell%2C%20and%20the%20magic%20will%20do%20the%20work%20to%20extract%20them%20properly.%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%25%25wdatp_ip%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELet's%20check%20for%20any%20communication%20to%20some%20IPs.%20I%20can%20include%20any%20text%20here%20I%20want%20because%20the%20Jupyter%20magic%20for%20ip%20will%20regex%20extract%20IPv4%20addresses%20and%20look%20them%20up%3C%2FP%3E%0A%3CP%3E52.239.151.138%3C%2FP%3E%0A%3CP%3E65.52.108.90%3C%2FP%3E%0A%3CP%3Eend%20of%20list%20of%20IPs%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F61255i29B4C325747782ED%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22jupyter_magic2.png%22%20title%3D%22jupyter_magic2.png%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EWDATP%20IP%20Magic%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CH2%20id%3D%22toc-hId--574976912%22%20id%3D%22toc-hId--523138645%22%3EUsing%20the%20Advanced%20Hunting%20API%3C%2FH2%3E%0A%3CP%3EWDATP%20collects%20a%20rich%20set%20of%20data%20from%20endpoints%20and%20makes%20it%20available%20for%20hunting%20in%20the%20portal.%20You%20can%20also%20perform%20hunting%20queries%20with%20the%20API.%20This%20is%20a%20powerful%20feature%20that%20allows%20you%20to%20build%20automation%20around%20hunting%20scenarios.%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20example%20below%2C%20we%20get%20the%20most%20recent%20alert%20and%20then%20construct%20a%20query%20to%20get%20relevant%20records%20around%20the%20time%20of%20the%20alert%20from%20process%20creation%20events%2C%20network%20activity%2C%20and%20file%20creation%20activity.%20One%20could%20build%20on%20this%20to%20create%20triage%20scripts%20that%20automate%20common%20steps%20for%20responding%20to%20alerts.%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%23get%20the%20most%20recent%20alert%3C%2FP%3E%0A%3CP%3Ealert_df%20%3D%20wdatp_api.alerts(filterstr%3D%20'%24top%3D1')%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Eprint(%22title%3A%20%25s%5Cncategory%3A%20%25s%5Cnalert%20id%3A%20%25s%22%20%25%20(alert_df.title.values%5B0%5D%2C%20alert_df.category.values%5B0%5D%2C%20alert_df.id.values%5B0%5D))%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23%20build%20a%20hunting%20query%20that%20gets%20data%20around%20the%20time%20of%20the%20alert%3C%2FP%3E%0A%3CP%3Eget_records_qry%20%3D%20'''%3C%2FP%3E%0A%3CP%3Elet%20alertId%20%3D%20%22%25s%22%3B%3C%2FP%3E%0A%3CP%3Elet%20alert%20%3D%20AlertEvents%20%7C%20where%20AlertId%20%3D%3D%20alertId%20%7C%20summarize%20AlertFirstEventTime%3Dmin(EventTime)%20by%20MachineId%3B%3C%2FP%3E%0A%3CP%3Elet%20machineId%20%3D%20toscalar(alert%20%7C%20project%20MachineId)%3B%3C%2FP%3E%0A%3CP%3Elet%20timestamp%20%3D%20toscalar(alert%20%7C%20project%20AlertFirstEventTime)%3B%3C%2FP%3E%0A%3CP%3Elet%20lookupPeriod%20%3D%2010m%3B%3C%2FP%3E%0A%3CP%3Efind%20in%20(ProcessCreationEvents%2C%20NetworkCommunicationEvents%2C%20FileCreationEvents)%3C%2FP%3E%0A%3CP%3Ewhere%20EventTime%20between%20((timestamp%20-%20lookupPeriod)%20..%20lookupPeriod)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20and%20MachineId%20%3D%3D%20machineId%3C%2FP%3E%0A%3CP%3E%7C%20take%201000%3C%2FP%3E%0A%3CP%3E'''%20%25%20(alert_df.id.values%5B0%5D)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Erelated_data_df%20%3D%20wdatp_api.query(query%3Dget_records_qry)%3C%2FP%3E%0A%3CP%3Eprint%20(%22Number%20of%20records%20found%20%25d%22%20%25%20len(related_data_df))%3C%2FP%3E%0A%3CP%3Erelated_data_df%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F61253i617ED935BDFF72EE%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22jupyter_hunting_alerts.png%22%20title%3D%22jupyter_hunting_alerts.png%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EAdvanced%20Hunting%20API%3C%2FSPAN%3E%3C%2FSPAN%3E%26nbsp%3B%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1167833423%22%20id%3D%22toc-hId-1219671690%22%3EVisualizing%20Data%3C%2FH2%3E%0A%3CP%3EIn%20the%20example%20below%20we%20have%20a%20query%20that%20searches%20for%20machines%20and%20their%20associated%20outbound%20communication%20patterns.%20It%20then%20renders%20the%20machines%20and%20the%20IP%20ranges%20they%20communicate%20with%20in%20a%20%3CSPAN%3E%3CA%20href%3D%22http%3A%2F%2Fholoviews.org%2Freference%2Felements%2Fbokeh%2FChord.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EChord%20diagram%3C%2FA%3E%3C%2FSPAN%3E.%26nbsp%3B%20This%20kind%20of%20analysis%20might%20be%20handy%20to%20take%20a%20population%20of%20machines%20that%20should%20be%20communicating%20similarly%20because%20they%20are%20all%20the%20same%20role%20(for%20example%20Domain%20Controllers)%20and%20spot%20outliers.%3C%2FP%3E%0A%3CP%3EThe%20code%20uses%20the%20%3CSPAN%3E%3CA%20href%3D%22http%3A%2F%2Fholoviews.org%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eholoviews%20library%3C%2FA%3E%3C%2FSPAN%3E%20to%20do%20the%20visualization.%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%23%20this%20query%20gets%20a%20handful%20of%20machines%20named%20desktop%3C%2FP%3E%0A%3CP%3Equery%20%3D%20'''%3C%2FP%3E%0A%3CP%3Elet%20machine_list%20%3D%20MachineInfo%20%7C%20where%20EventTime%20%26gt%3B%20ago(1d)%20%7C%20where%20ComputerName%20startswith%20'desktop-'%20%7C%20summarize%20by%20ComputerName%2C%20MachineId%20%7C%20take%205%3B%3C%2FP%3E%0A%3CP%3ENetworkCommunicationEvents%3C%2FP%3E%0A%3CP%3E%7C%20where%20EventTime%20%26gt%3B%20ago(5d)%20%7C%20where%20isnotempty(MachineId)%3C%2FP%3E%0A%3CP%3E%7C%20where%20RemoteIPType%20%3D%3D%20'Public'%20and%20RemoteIP%20contains%20('.')%20and%20RemotePort%20in%20('80')%3C%2FP%3E%0A%3CP%3E%7C%20extend%20MaskedIP%20%3D%20strcat(split(RemoteIP%2C'.')%5B0%5D%2C'.'%2C%20split(RemoteIP%2C'.')%5B1%5D%2C%20'.*.*')%3C%2FP%3E%0A%3CP%3E%7C%20summarize%20by%20MachineId%2C%20MaskedIP%3C%2FP%3E%0A%3CP%3E%7C%20join%20kind%3Dinner%20(machine_list)%20on%20MachineId%3C%2FP%3E%0A%3CP%3E%7C%20summarize%20by%20ComputerName%2C%20MaskedIP%3C%2FP%3E%0A%3CP%3E'''%3C%2FP%3E%0A%3CP%3Edf%20%3D%20wdatp_api.query(query)%3C%2FP%3E%0A%3CP%3Eprint%20(%22rows%20returned%20%3D%20%25d%22%20%25%20len(df))%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%23%23%20this%20does%20some%20footwork%20for%20holoviews%20to%20allow%20us%20to%20format%20the%20colors%2C%20nodes%2C%20and%20edges.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Eimport%20holoviews%20as%20hv%3C%2FP%3E%0A%3CP%3Ehv.extension('bokeh')%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Edef%20holoview_charting_objects(df)%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20g1%20%3D%20list(df%5Bdf.columns%5B0%5D%5D.astype(str).unique())%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20g2%20%3D%20list(df%5Bdf.columns%5B1%5D%5D.astype(str).unique())%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20g1d%20%3D%20list(map(lambda%20x%3A%20%7B'name'%3A%20x%2C%20'group'%3A1%7D%2C%20g1))%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20g2d%20%3D%20list(map(lambda%20x%3A%20%7B'name'%3A%20x%2C%20'group'%3A2%7D%2C%20g2))%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20nodes%20%3D%20hv.Dataset(pd.DataFrame(g1d%20%2B%20g2d)%2C%20'index')%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20src%3Dlist(map(lambda%20x%3A%20nodes.data%5Bnodes.data%5B'name'%5D%20%3D%3D%20x%5D%5B'index'%5D.values%5B0%5D%2C%20list(df%5Bdf.columns%5B0%5D%5D.values)))%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20dst%20%3D%20list(map(lambda%20x%3A%20nodes.data%5Bnodes.data%5B'name'%5D%20%3D%3D%20x%5D%5B'index'%5D.values%5B0%5D%2C%20list(df%5Bdf.columns%5B1%5D%5D.values)))%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20df2%20%3D%20pd.DataFrame(list(zip(src%2Cdst))%2C%20columns%3D%5B'source'%2C'dest'%5D)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20return%20(df2%2C%20nodes)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%25opts%20Chord%20%5Bwidth%3D800%20height%3D800%5D%3C%2FP%3E%0A%3CP%3E%25opts%20Chord%20%5Blabel_index%3D'name'%20color_index%3D'index'%20edge_color_index%3D'source'%5D%3C%2FP%3E%0A%3CP%3E%25opts%20Chord%20(cmap%3D'Category20'%20edge_cmap%3D'Category20')%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Eimport%20holoviews%20as%20hv%3C%2FP%3E%0A%3CP%3Ehv.Chord(holoview_charting_objects(df))%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F61252iE55B8997616BE59C%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22jupyter_chord.png%22%20title%3D%22jupyter_chord.png%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EChord%20diagram%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%26nbsp%3B%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHoloviews%20has%20a%20wide%20variety%20of%20visualization%20options.%20We%20can%20render%20the%20same%20data%20in%20a%20Scatter%20plot.%20This%20allows%20one%20to%20quickly%20identify%20netblocks%20that%20are%20commonly%20communicated%20to%20by%20many%20machines%20and%20also%20see%20IP%20blocks%20that%20have%20only%20one%20or%20two%20machines%20communicating%20with%20them.%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%23%20we%20can%20also%20render%20the%20same%20data%20in%20a%20Scatter%20plot%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%25opts%20Scatter%20%5Bwidth%3D800%20height%3D800%5D%20(size%3D10)%3C%2FP%3E%0A%3CP%3Eimport%20holoviews%20as%20hv%3C%2FP%3E%0A%3CP%3Ehv.extension('bokeh')%3C%2FP%3E%0A%3CP%3Ehv.Scatter(df).options(show_grid%3DTrue)%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F61250i92ED37AEF51658CA%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22jupyter_holoviews_scatter.png%22%20title%3D%22jupyter_holoviews_scatter.png%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EScatterplot%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1384323538%22%20id%3D%22toc-hId--1332485271%22%3EEnriching%20WDATP%20data%20by%20calling%20other%20APIs%20and%20services%3C%2FH2%3E%0A%3CP%3EIn%20this%20section%20we%20will%20show%20how%20you%20can%20call%20other%20APIs%20to%20enrich%20WDATP%20data--in%20this%20case%20VirusTotal%20(VT).%20If%20you%20don%E2%80%99t%20have%20a%20VT%20API%20key%2C%20you%20can%20sign%20up%20for%20one%20for%20free%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fsupport.virustotal.com%2Fhc%2Fen-us%2Farticles%2F115002088769-Please-give-me-an-API-key%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E%3C%2FSPAN%3E.%20You%20pass%20in%20your%20VT%20API%20key%20to%20the%20sample%20by%20creating%20the%20WDATP%20endpoint%20as%20follows%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3Ewdatp_api%20%3D%20WDATP(tenantId%2C%20appId%2C%20appSecret%2C%20vt_api_key%3D%20'%3CYOUR%20vt_api_key%3D%22%22%3E')%3C%2FYOUR%3E%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20example%2C%20we%20query%20WDATP%20for%20hashes%20of%20files%20to%20gain%20additional%20context.%26nbsp%3B%20The%20query%20collects%20SHA1%20file%20hashes%20from%20WDATP%20Alerts.%20By%20querying%20what%20VT%20knows%20about%20the%20hashes%2C%20we%20may%20gain%20additional%20insight.%3C%2FP%3E%0A%3CP%3ENext%2C%20we%E2%80%99ll%20take%20any%20file%20hashes%20where%20VT%20had%20results%20and%20query%20WDATP%20data%20to%20enrich%20it.%20The%20below%20shows%20querying%20the%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fwindows%2Fsecurity%2Fthreat-protection%2Fwindows-defender-atp%2Ffiles-windows-defender-advanced-threat-protection-new%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Efiles%20API%3C%2FA%3E%3C%2FSPAN%3E%20to%20get%20global%20prevalence%20information%20and%20combining%20the%20results%20with%20a%20link%20to%20the%20VirusTotal%20webpage%20for%20the%20file.%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%23%23%20query%20WDATP%20data%20for%20hashes%20of%20EXE%20files%20created%20in%20the%20user's%20AppData%20folder.%20Choose%2050%20of%20them%20at%20random%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Edf_results%20%3D%20wdatp_api.query(query%20%3D%20'''%3C%2FP%3E%0A%3CP%3EAlertEvents%3C%2FP%3E%0A%3CP%3E%7C%20where%20EventTime%20%26gt%3B%20ago(1d)%3C%2FP%3E%0A%3CP%3E%7C%20summarize%20by%20SHA1%3C%2FP%3E%0A%3CP%3E%7C%20extend%20randsortorder%3Drand()%20%7C%20sort%20by%20randsortorder%3C%2FP%3E%0A%3CP%3E%7C%20take%2050%3C%2FP%3E%0A%3CP%3E'''%3C%2FP%3E%0A%3CP%3E)%3C%2FP%3E%0A%3CP%3Eprint(%22Number%20of%20hashes%20%3D%20%25d%22%20%25%20len(df_results))%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%23%23%20query%20VirusTotal%20to%20see%20if%20anyone%20has%20submitted%20them%20for%20scanning%20before.%3C%2FP%3E%0A%3CP%3E%23%23%20List%20any%20results%20with%20at%20least%20one%20AV%20engine%20positive%20result%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Evtdf%20%3D%20wdatp_api.vtresults(list(df_results.SHA1)).fillna('')%3C%2FP%3E%0A%3CP%3Evt_pos%20%3D%20vtdf.loc%5Bvtdf%5B'response_code'%5D%20%3D%3D%201%5D.loc%5Bvtdf%5B'positives'%5D%20!%3D%200%5D%3C%2FP%3E%0A%3CP%3Evt_pos%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CP%3E%23%23%20look%20up%20each%20positive%20hit%20in%20WDATP%20data%20and%20enrich%20results%20with%20WDATP%20information%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Ewdatp_file_results%20%3D%20%5B%5D%3C%2FP%3E%0A%3CP%3Edf_all%20%3D%20pd.DataFrame()%3C%2FP%3E%0A%3CP%3Efor%20sha1%20in%20list(vt_pos%5B'sha1'%5D)%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20df_file%20%3D%20wdatp_api.files(filehash%20%3D%20sha1)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20if%20len(df_file)%20%26gt%3B%200%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20wdatp_file_results.append(df_file)%3C%2FP%3E%0A%3CP%3Eif%20len(wdatp_file_results)%20%26gt%3B%200%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20df_all%20%3D%20pd.concat(wdatp_file_results)%3C%2FP%3E%0A%3CP%3Edf_all%20%3D%20df_all.merge(vt_pos%2C%20right_on%20%3D%20'sha1'%2C%20left_on%20%3D%20'sha1')%3C%2FP%3E%0A%3CP%3Edf_all%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F61258iFCE480E2201A08C4%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22jupyter_vt.png%22%20title%3D%22jupyter_vt.png%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EVirusTotal%20API%20example%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-358486797%22%20id%3D%22toc-hId-410325064%22%3EIn%20closing%3C%2FH2%3E%0A%3CP%3EIn%20this%20post%20we%20walked%20through%20how%20to%20get%20started%20with%20the%20WDATP%20APIs.%20Because%20the%20APIs%20are%20exposed%20as%20REST%20endpoints%20they%20are%20simple%20to%20call%20using%20Python%20or%20other%20languages.%20The%20Jupyter%20Notebook%20is%20a%20useful%20open%20source%20package%20that%20makes%20it%20easy%20to%20interact%20with%20data%20and%20save%20the%20results%20in%20a%20reusable%20notebook%20that%20is%20easy%20to%20share%20with%20others.%20We%20look%20forward%20to%20what%20you%E2%80%99ll%20do%20with%20the%20APIs%20and%20the%20notebooks%20you%E2%80%99ll%20create!%3C%2FP%3E%0A%3CP%3EThe%20author%20would%20like%20to%20thank%20Eric%20Hutchins%20(%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Ftwitter.com%2Fkillchain%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%40killchain%3C%2FA%3E%3C%2FSPAN%3E)%20for%20introducing%20him%20to%20Jupyter%20notebooks.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-294434%22%20slang%3D%22en-US%22%3E%3CP%3EAdvanced%20hunting%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-294434%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAdvanced%20hunting%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Visitor

Automating Security Operations Using Windows Defender ATP APIs with Python and Jupyter Notebooks

Introduction

Windows Defender ATP provides a great hunting experience out of the box. Analysts can search over process events, network events, logons, registry activity, and more.  In this post, I will show how analysts can go even further by using the WDATP APIs. If any of the following scenarios are interesting to your security operations team, then this post is for you:

  • Automate common investigative steps such as gathering additional host and network details, triggering host actions like collecting an investigative package, or running anti-virus scans.
  • Query WDATP collected data about machines, users, domains, IPs, and files, enrich it with 3rd party services such as VirusTotal, and visualize the results.
  • Perform hunting tasks by searching across the rich data collected by WDATP to find new malicious activity.

Normally when one hears the word “APIs”, one thinks of a developer feature that requires compilers and SDKs. This post will show two features that make these APIs approachable to SOC analysts—Python support and Jupyter Notebooks.  Even though this post focuses on Python, you can call the APIs from PowerShell, C#, or any language that can call REST endpoints.

Getting Started

The first step is deciding on how you want to access the APIs. WDATP uses standard OAuth2.0 authentication.  To get started, perform the following steps:

  • Register an application with Azure Active Directory. This allows you to assign the desired permissions to the app. You can create apps that only read alerts, run advanced queries, collect forensics, or many other combinations of permissions.
  • Decide on your access model: without a user (like a background service) or on behalf of a user. Following the steps in those links will provide you with an access token to call the APIs.
  • Use the token to call Windows Defender ATP APIs.

API Functionality

A list of the exposed APIs can be found here. In this post, we’ll focus on running some advanced hunting queries, and then calling the API to get further information on machines and users.

Advanced Hunting

Run queries from API.

Alerts

Run API calls such as get alerts, alert information by ID, alert related actor information, alert related IP information, and alert related machine information.

Domain

Run API calls such as get domain related machines, domain related machines, statistics, and check if a domain is seen in your organization.

File

Run API calls such as get file information, file related alerts, file related machines, and file statistics.

IP

Run API calls such as get IP related alerts, IP related machines, IP statistics, and check if and IP is seen in your organization.

Machines

Run API calls such as find machine information by IP, get machines, get machines by ID, information about logged on users, and alerts related to a given machine ID.

User

Run API calls such as get alert related user information, user information, user related alerts, and user related machines.

 

Authenticate to WDATP

After registering your app, you have all you need to call the APIs: your WDATP Tenant ID, an App Id and App Secret created during registration.  Here is the code to authenticate to the WDATP endpoint:

import json

import urllib.request

import urllib.parse

def wdatp_get_AAD_token(tenantId, appId, appSecret):

    url = "https://login.windows.net/%s/oauth2/token" % (tenantId)

 

    resourceAppIdUri = 'https://api.securitycenter.windows.com'

 

    body = {

        'resource' : resourceAppIdUri,

        'client_id' : appId,

        'client_secret' : appSecret,

        'grant_type' : 'client_credentials'

    }

 

    data = urllib.parse.urlencode(body).encode("utf-8")

    req = urllib.request.Request(url, data)

    response = urllib.request.urlopen(req)

    jsonResponse = json.loads(response.read())

    aadToken = jsonResponse["access_token"]

    return aadToken

Get Alerts

This code shows calling the API to get recent alerts.

def wdatp_get_alerts(aadToken):

    url = "https://api.securitycenter.windows.com/api/alerts"

    headers = {

        'Content-Type' : 'application/json',

        'Accept' : 'application/json',

        'Authorization' : "Bearer " + aadToken

    }

 

    req = urllib.request.Request(url, headers=headers)

    response = urllib.request.urlopen(req)

    jsonResponse = json.loads(response.read())

    return jsonResponse["value"]

Now that you know the basics of getting up and going, let’s walk through a few different scenarios. To make it easy to repeat these scenarios, we will use a Jupyter notebook to encapsulate them. There is nothing about the APIs that requires Jupyter, but you may find it to be a handy tool when working with Python and the APIs.

Introducing Jupyter Notebooks

If you’re familiar with Jupyter Notebooks, you can skip this paragraph.  If not, Jupyter Notebooks are an open source project designed to make interactive computing and sharing re-usable analysis easier. There are over 3 million notebooks shared on GitHub. There is an annual conference and support for both local and cloud versions by every major cloud provider. While many use cases for notebooks surround data science and machine learning, they are an excellent infosec investigative tool for exploration, visualization, and analysis. Let’s jump right in!  If you want to follow along, download the notebook from the WDATP GitHub: WDATP API Jupyter Notebook.

Jupyter Basics

Place the notebook you downloaded from GitHub in the notebook directory. You can find the default notebook location by looking at the output from launching Jupyter:

> C:\Anaconda3\Scripts\jupyter.exe notebook --notebook-dir c:\home\jupyter

[I 09:29:34.126 NotebookApp] Serving notebooks from local directory: c:\home\jupyter

[I 09:29:34.126 NotebookApp] 0 active kernels

[C 09:29:34.126 NotebookApp]

 

    Copy/paste this URL into your browser when you connect for the first time,

    to login with a token:

        http://localhost:8888/?token=<access token>

To orient first time users, here are the basics.  A notebook is composed of input and output cells.  In the example below:

  • Input line #2 shows printing “hello world”. To execute a cell, put the cursor in the cell and hit Shift+Enter. This will run the cell and move the input focus to the next input cell.
  • Line #3 shows performing a calculation. This is a Python 3 notebook, so it is the “Python kernel” that is doing the work.
  • Line #4 shows a feature of interactive Python. Python stores the output of the previous command in an automatic variable named underscore (‘_’)
  • You can execute shell commands by prefixing them with a bang (‘!’)
  • Jupyter has a way to provide convenience commands through a feature called “magics”. You can invoke a magic by prefixing it with % or %%. The timeit magic runs the command in the cell several times and provides an average time.

 about_jupyter.pngJupyter basicsInstalling the sample from GitHub

To install the sample code, run the following cell by hitting Shift+Enter.

jupyter_wrapper_code.pngRunning the WDATP wrapper sample

Calling the APIs

Initialize the WDATP endpoint by passing in your tenant Id, and app information. Then query for the top 5 alerts. The notebook will automatically format the results in a HTML table.

wdatp_api = WDATP(tenantId, appId, appSecret, vt_api_key)

 

wdatp_api.alerts(filterstr = "$top=5")

 

jupyter_top_alerts.pngInstantiating the WDATP API endpoint

Getting Alerts

Let’s start by getting some alerts.   You can use the standard OData syntax when querying some of the WDATP entities.

By default, this sample saves the results from API calls in a convenient tabular data structure called a Pandas DataFrame. Pandas is an open source package tailored for working with structured data.

# Let's select just the columns we want to view using OData

wdatp_api.alerts(filterstr = "$select=alertCreationTime,category,title,description,id,severity&$filter=Severity eq 'High'&$top=5")

 

# let's assign the API results to a variable so we can process them without making an API call each time

df = wdatp_api.alerts(filterstr = "$top=100")

print("number of rows returned = %d" % len(df))

 

# show all the columns in the alert

df.columns

 

# Let's select just the columns we want to view. This uses the Pandas syntax for selecting columns

 

df[['alertCreationTime','category','description','id','severity']].head(5)

jupyter_alert_DF.pngAlerts to a data framejupyter_alert_DF_2.pngInteracting with a data frame

To show the power of using Python and open source packages like Pandas, we’ll show how to do a simple count of alert frequency and render the results in a pie chart. This also shows that the notebook can include pictures and not just text output.

# let's group them by title and count their frequency

df['title'] = df['title'].apply(lambda t: 'Windows Defender AV detection' if t.startswith('Windows Defender AV detected ') else t)

df1 = df[['id','title']].groupby('title').count().rename(index=str, columns={"id":"Alert Count"})

df1 = df1.sort_values(['Alert Count'], ascending=False)

df1

# show a quick pie chart using matplotlib

%matplotlib inline

plt = df1.plot.pie(y=0, figsize=(5, 5), legend=False, autopct='%1.0f%%')jupyter_pie.pngDataframe manipulation

 

The Role of Magics

There is a time and place for code, but sometimes you want a simpler way to access data. The API wrapper in this sample has several Jupyter magics created for just this purpose. Magics are just shortcuts to invoking code on your data.  Use the %wdatp_alert magic with a valid Alert ID from your WDATP data. The below command also shows assigning the alert data to a variable (alert_df) and then rendering it in a vertical view by invoking the Pandas transpose function on it (alert_df.T).

%wdatp_alert 636740134254933385_-1113968221

# output from the previous command is stored in an automatic variable, the understore _

alert_df = _

alert_df.T  # Transpose rows and columns for an easy way to view the alert details

jupyter_magic.pngJupyter WDATP Magics

The below example shows invoking the wdapt_ip magic. There are two kinds of Jupyter magics. Line magics start with a single % and operate on the input right after the magic name. The %wdapt_alert command we just invoked is an example of that.  By prefixing a magic with two % signs, it is invoked as a cell magic and operates on the entire contents of the cell.  One feature of %%wdatp_ip is that it will scan through the cell text and use a regular expression to find any data that resembles an IPV4 address. This is handy where you might have an email or webpage with the IPs you need to query. While you can copy and paste them individually, it is sometimes faster to just grab the entire paragraph containing them, paste them into a cell, and the magic will do the work to extract them properly.

%%wdatp_ip 

Let's check for any communication to some IPs. I can include any text here I want because the Jupyter magic for ip will regex extract IPv4 addresses and look them up

52.239.151.138

65.52.108.90

end of list of IPs

jupyter_magic2.pngWDATP IP Magic

Using the Advanced Hunting API

WDATP collects a rich set of data from endpoints and makes it available for hunting in the portal. You can also perform hunting queries with the API. This is a powerful feature that allows you to build automation around hunting scenarios. 

In the example below, we get the most recent alert and then construct a query to get relevant records around the time of the alert from process creation events, network activity, and file creation activity. One could build on this to create triage scripts that automate common steps for responding to alerts.

#get the most recent alert

alert_df = wdatp_api.alerts(filterstr= '$top=1')

 

print("title: %s\ncategory: %s\nalert id: %s" % (alert_df.title.values[0], alert_df.category.values[0], alert_df.id.values[0]))

 

# build a hunting query that gets data around the time of the alert

get_records_qry = '''

let alertId = "%s";

let alert = AlertEvents | where AlertId == alertId | summarize AlertFirstEventTime=min(EventTime) by MachineId;

let machineId = toscalar(alert | project MachineId);

let timestamp = toscalar(alert | project AlertFirstEventTime);

let lookupPeriod = 10m;

find in (ProcessCreationEvents, NetworkCommunicationEvents, FileCreationEvents)

where EventTime between ((timestamp - lookupPeriod) .. lookupPeriod)

        and MachineId == machineId

| take 1000

''' % (alert_df.id.values[0])

 

related_data_df = wdatp_api.query(query=get_records_qry)

print ("Number of records found %d" % len(related_data_df))

related_data_df

jupyter_hunting_alerts.pngAdvanced Hunting API 

 

Visualizing Data

In the example below we have a query that searches for machines and their associated outbound communication patterns. It then renders the machines and the IP ranges they communicate with in a Chord diagram.  This kind of analysis might be handy to take a population of machines that should be communicating similarly because they are all the same role (for example Domain Controllers) and spot outliers.

The code uses the holoviews library to do the visualization. 

# this query gets a handful of machines named desktop

query = '''

let machine_list = MachineInfo | where EventTime > ago(1d) | where ComputerName startswith 'desktop-' | summarize by ComputerName, MachineId | take 5;

NetworkCommunicationEvents

| where EventTime > ago(5d) | where isnotempty(MachineId)

| where RemoteIPType == 'Public' and RemoteIP contains ('.') and RemotePort in ('80')

| extend MaskedIP = strcat(split(RemoteIP,'.')[0],'.', split(RemoteIP,'.')[1], '.*.*')

| summarize by MachineId, MaskedIP

| join kind=inner (machine_list) on MachineId

| summarize by ComputerName, MaskedIP

'''

df = wdatp_api.query(query)

print ("rows returned = %d" % len(df))

 

## this does some footwork for holoviews to allow us to format the colors, nodes, and edges.

 

import holoviews as hv

hv.extension('bokeh')

 

def holoview_charting_objects(df):

    g1 = list(df[df.columns[0]].astype(str).unique())

    g2 = list(df[df.columns[1]].astype(str).unique())

    g1d = list(map(lambda x: {'name': x, 'group':1}, g1))

    g2d = list(map(lambda x: {'name': x, 'group':2}, g2))

    nodes = hv.Dataset(pd.DataFrame(g1d + g2d), 'index')

 

    src=list(map(lambda x: nodes.data[nodes.data['name'] == x]['index'].values[0], list(df[df.columns[0]].values)))

    dst = list(map(lambda x: nodes.data[nodes.data['name'] == x]['index'].values[0], list(df[df.columns[1]].values)))

    df2 = pd.DataFrame(list(zip(src,dst)), columns=['source','dest'])

    return (df2, nodes)

 

%opts Chord [width=800 height=800]

%opts Chord [label_index='name' color_index='index' edge_color_index='source']

%opts Chord (cmap='Category20' edge_cmap='Category20')

 

import holoviews as hv

hv.Chord(holoview_charting_objects(df))

jupyter_chord.pngChord diagram

 

 

Holoviews has a wide variety of visualization options. We can render the same data in a Scatter plot. This allows one to quickly identify netblocks that are commonly communicated to by many machines and also see IP blocks that have only one or two machines communicating with them.

# we can also render the same data in a Scatter plot

 

%opts Scatter [width=800 height=800] (size=10)

import holoviews as hv

hv.extension('bokeh')

hv.Scatter(df).options(show_grid=True)

 jupyter_holoviews_scatter.pngScatterplot

 

Enriching WDATP data by calling other APIs and services

In this section we will show how you can call other APIs to enrich WDATP data--in this case VirusTotal (VT). If you don’t have a VT API key, you can sign up for one for free here. You pass in your VT API key to the sample by creating the WDATP endpoint as follows

wdatp_api = WDATP(tenantId, appId, appSecret, vt_api_key= '<YOUR VT_API_KEY>')

 

In this example, we query WDATP for hashes of files to gain additional context.  The query collects SHA1 file hashes from WDATP Alerts. By querying what VT knows about the hashes, we may gain additional insight.

Next, we’ll take any file hashes where VT had results and query WDATP data to enrich it. The below shows querying the files API to get global prevalence information and combining the results with a link to the VirusTotal webpage for the file.

## query WDATP data for hashes of EXE files created in the user's AppData folder. Choose 50 of them at random

 

df_results = wdatp_api.query(query = '''

AlertEvents

| where EventTime > ago(1d)

| summarize by SHA1

| extend randsortorder=rand() | sort by randsortorder

| take 50

'''

)

print("Number of hashes = %d" % len(df_results))

## query VirusTotal to see if anyone has submitted them for scanning before.

## List any results with at least one AV engine positive result

 

vtdf = wdatp_api.vtresults(list(df_results.SHA1)).fillna('')

vt_pos = vtdf.loc[vtdf['response_code'] == 1].loc[vtdf['positives'] != 0]

vt_pos

## look up each positive hit in WDATP data and enrich results with WDATP information

 

wdatp_file_results = []

df_all = pd.DataFrame()

for sha1 in list(vt_pos['sha1']):

    df_file = wdatp_api.files(filehash = sha1)

    if len(df_file) > 0:

        wdatp_file_results.append(df_file)

if len(wdatp_file_results) > 0:

    df_all = pd.concat(wdatp_file_results)

df_all = df_all.merge(vt_pos, right_on = 'sha1', left_on = 'sha1')

df_all

 jupyter_vt.pngVirusTotal API example

 

In closing

In this post we walked through how to get started with the WDATP APIs. Because the APIs are exposed as REST endpoints they are simple to call using Python or other languages. The Jupyter Notebook is a useful open source package that makes it easy to interact with data and save the results in a reusable notebook that is easy to share with others. We look forward to what you’ll do with the APIs and the notebooks you’ll create!

The author would like to thank Eric Hutchins (@killchain) for introducing him to Jupyter notebooks.