{"id":89808,"date":"2019-09-03T09:00:03","date_gmt":"2019-09-03T16:00:03","guid":{"rendered":"https:\/\/www.microsoft.com\/en-us\/security\/blog\/\/?p=89808"},"modified":"2023-05-26T14:40:04","modified_gmt":"2023-05-26T21:40:04","slug":"deep-learning-rises-new-methods-for-detecting-malicious-powershell","status":"publish","type":"post","link":"https:\/\/www.microsoft.com\/en-us\/security\/blog\/2019\/09\/03\/deep-learning-rises-new-methods-for-detecting-malicious-powershell\/","title":{"rendered":"Deep learning rises: New methods for detecting malicious PowerShell"},"content":{"rendered":"

Scientific and technological advancements in deep learning, a category of algorithms within the larger framework of machine learning, provide new opportunities for development of state-of-the art protection technologies. Deep learning methods are impressively outperforming traditional methods on such tasks as image and text classification. With these developments, there\u2019s great potential for building novel threat detection methods using deep learning.<\/p>\n

Machine learning algorithms work with numbers, so objects like images, documents, or emails are converted into numerical form through a step called feature engineering<\/em>, which, in traditional machine learning methods, requires a significant amount of human effort. With deep learning, algorithms can operate on relatively raw data and extract features without human intervention.<\/p>\n

At Microsoft, we make significant investments in pioneering machine learning<\/a> that inform our security solutions with actionable knowledge through data, helping deliver intelligent, accurate, and real-time protection against a wide range of threats. In this blog, we present an example of a deep learning technique that was initially developed for natural language processing (NLP) and now adopted and applied to expand our coverage of detecting malicious PowerShell scripts, which continue to be a critical attack vector<\/a>. These deep learning-based detections add to the industry-leading endpoint detection and response capabilities<\/a> in Microsoft Defender Advanced Threat Protection (Microsoft Defender ATP<\/a>).<\/p>\n

Word embedding in natural language processing<\/h2>\n

Keeping in mind that our goal is to classify PowerShell scripts, we briefly look at how text classification is approached in the domain of natural language processing. An important step is to convert words to vectors (tuples of numbers) that can be consumed by machine learning algorithms. A basic approach, known as one-hot encoding<\/em>, first assigns a unique integer to each word in the vocabulary, then represents each word as a vector of 0s, with 1 at the integer index corresponding to that word. Although useful in many cases, the one-hot encoding has significant flaws. A major issue is that all words are equidistant from each other, and semantic relations between words are not reflected in geometric relations between the corresponding vectors.<\/p>\n

Contextual embedding<\/em> is a more recent approach that overcomes these limitations by learning compact representations of words from data under the assumption that words that frequently appear in similar context tend to bear similar meaning. The embedding is trained on large textual datasets like Wikipedia. The Word2vec algorithm<\/em><\/a>, an implementation of this technique, is famous not only for translating semantic similarity of words to geometric similarity of vectors, but also for preserving polarity relations between words. For example, in Word2vec representation:<\/p>\n

Madrid – Spain + Italy \u2248 Rome<\/em><\/p>\n

Embedding of PowerShell scripts<\/h2>\n

Since training a good embedding requires a significant amount of data, we used a large and diverse corpus of 386K distinct unlabeled PowerShell scripts. The Word2vec algorithm, which is typically used with human languages, provides similarly meaningful results when applied to PowerShell language. To accomplish this, we split the PowerShell scripts into tokens, which then allowed us to use the Word2vec algorithm to assign a vectorial representation to each token .<\/p>\n

Figure 1 shows a 2-dimensional visualization of the vector representations of 5,000 randomly selected tokens, with some tokens of interest highlighted. Note how semantically similar tokens are placed near each other. For example, the vectors representing -eq<\/em>, -ne<\/em> and -gt<\/em>, which in PowerShell are aliases for \u201cequal\u201d, \u201cnot-equal\u201d and \u201cgreater-than\u201d, respectively, are clustered together. Similarly, the vectors representing the allSigned<\/em>, remoteSigned<\/em>, bypass<\/em>, and unrestricted<\/em> tokens, all of which are valid values for the execution policy setting in PowerShell, are clustered together.<\/p>\n

\"\"<\/p>\n

Figure 1. 2D visualization of 5,000 tokens using Word2vec<\/em><\/p>\n

Examining the vector representations of the tokens, we found a few additional interesting relationships.<\/p>\n

Token similarity<\/em>: Using the Word2vec representation of tokens, we can identify commands in PowerShell that have an alias. In many cases, the token closest to a given command is its alias. For example, the representations of the token Invoke-Expression and its alias IEX are closest to each other. Two additional examples of this phenomenon are the Invoke-WebRequest<\/em> and its alias IWR<\/em>, and the Get-ChildItem<\/em> command and its alias GCI.<\/p>\n

We also measured distances within sets of several tokens. Consider, for example, the four tokens $i<\/em>, $j<\/em>, $k<\/em> and $true<\/em> (see the right side of Figure 2). The first three are usually used to represent a numeric variable and the last naturally represents a Boolean constant. As expected, the $true token mismatched the others \u2013 it was the farthest (using the Euclidean distance) from the center of mass of the group.<\/p>\n

More specific to the semantics of PowerShell in cybersecurity, we checked the representations of the tokens: bypass<\/em>, normal<\/em>, minimized,<\/em> maximized<\/em>, and hidden<\/em> (see the left side of Figure 2). While the first token is a legal value for the ExecutionPolicy flag in PowerShell, the rest are legal values for the WindowStyle flag. As expected, the vector representation of bypass was the farthest from the center of mass of the vectors representing all other four tokens.<\/p>\n

\"\" \"\"<\/p>\n

Figure 2. 3D visualization of selected tokens<\/em><\/p>\n

Linear Relationships<\/em>: Since Word2vec preserves linear relationships, computing linear combinations of the vectorial representations results in semantically meaningful results. Below are a few interesting relationships we found:<\/p>\n

high – $false + $true \u2248’ low <\/em>
\n\u2018-eq\u2019 – $false + $true ‘\u2248 \u2018-neq\u2019 <\/em>
\nDownloadFile – $destfile + $str \u2248’ DownloadString \u2018<\/em>
\nExport-CSV\u2019 – $csv + $html ‘\u2248 \u2018ConvertTo-html\u2019<\/em>
\n\u2018Get-Process\u2019-$processes+$services ‘\u2248 \u2018Get-Service\u2019<\/em><\/p>\n

In each of the above expressions, the sign \u2248 signifies that the vector on the right side is the closest (among all the vectors representing tokens in the vocabulary) to the vector that is the result of the computation on the left side.<\/p>\n

Detection of malicious PowerShell scripts with deep learning<\/h2>\n

We used the Word2vec embedding of the PowerShell language presented in the previous section to train deep learning models capable of detecting malicious PowerShell scripts. The classification model is trained and validated using a large dataset of PowerShell scripts that are labeled \u201cclean\u201d or \u201cmalicious,\u201d while the embeddings are trained on unlabeled data. The flow is presented in Figure 3.<\/p>\n

\"\"<\/p>\n

Figure 3 High-level overview of our model generation process<\/em><\/p>\n

Using GPU computing in Microsoft Azure<\/a>, we experimented with a variety of deep learning and traditional ML models. The best performing deep learning model increases the coverage (for a fixed low FP rate of 0.1%) by 22 percentage points compared to traditional ML models. This model, presented in Figure 4, combines several deep learning building blocks such as Convolutional Neural Networks (CNNs) and Long Short-Term Memory Recurrent Neural Networks (LSTM-RNN). Neural networks are ML algorithms inspired by biological neural systems like the human brain. In addition to the pretrained embedding described here, the model is provided with character-level embedding of the script<\/a>.<\/p>\n

\"\"<\/p>\n

Figure 4 Network architecture of the best performing model<\/em><\/p>\n

Real-world application of deep learning to detecting malicious PowerShell<\/h2>\n

The best performing deep learning model is applied at scale using Microsoft ML.Net<\/a> technology and ONNX<\/a> format for deep neural networks to the PowerShell scripts observed by Microsoft Defender ATP through the AMSI<\/a> interface. This model augments the suite of ML models and heuristics<\/a> used by Microsoft Defender ATP to protect against malicious usage of scripting languages.<\/p>\n

Since its first deployment, this deep learning model detected with high precision many cases of malicious and red team PowerShell activities, some undiscovered by other methods. The signal obtained through PowerShell is combined with a wide range of ML models and signals of Microsoft Defender ATP to detect cyberattacks.<\/p>\n

The following are examples of malicious PowerShell scripts that deep learning can confidently detect but can be challenging for other detection methods:<\/p>\n

\"\"<\/p>\n

Figure 5. Heavily obfuscated malicious script<\/em><\/p>\n

\"\"<\/p>\n

Figure 6. Obfuscated script that downloads and runs payload<\/em><\/p>\n

\"\"<\/p>\n

Figure 7. Script that decrypts and executes malicious code<\/em><\/p>\n

Enhancing Microsoft Defender ATP with deep learning<\/h2>\n

Deep learning methods significantly improve detection of threats. In this blog, we discussed a concrete application of deep learning to a particularly evasive class of threats: malicious PowerShell scripts. We have and will continue to develop deep learning-based protections across multiple capabilities in Microsoft Defender ATP<\/a>.<\/p>\n

Development and productization of deep learning systems for cyber defense require large volumes of data, computations, resources, and engineering effort. Microsoft Defender ATP combines data collected from millions of endpoints with Microsoft computational resources and algorithms to provide industry-leading protection against attacks.<\/p>\n

Stronger detection of malicious PowerShell scripts and other threats on endpoints using deep learning mean richer and better-informed security through Microsoft Threat Protection<\/a>, which provides comprehensive security for identities, endpoints, email and data, apps, and infrastructure.<\/p>\n

 <\/p>\n

Shay Kels<\/strong> and Amir Rubin<\/strong><\/em>
\nMicrosoft Defender ATP team<\/em><\/p>\n

 <\/p>\n

Additional references:<\/h4>\n