{"id":23345,"date":"2023-11-09T07:21:00","date_gmt":"2023-11-09T15:21:00","guid":{"rendered":"http:\/\/www.palada.net\/index.php\/2023\/11\/09\/news-17075\/"},"modified":"2023-11-09T07:21:00","modified_gmt":"2023-11-09T15:21:00","slug":"news-17075","status":"publish","type":"post","link":"https:\/\/www.palada.net\/index.php\/2023\/11\/09\/news-17075\/","title":{"rendered":"Memory scanning leaves attackers nowhere to hide"},"content":{"rendered":"<p><strong>Credit to Author: Matt Wixey| Date: Thu, 09 Nov 2023 13:46:19 +0000<\/strong><\/p>\n<div class=\"entry-content lg:prose-lg mx-auto prose max-w-4xl\">\n<p>In the first of our new series of technical thought leadership papers, which aim to give readers an in-depth look under-the-hood at some of our technologies and research, we wanted to provide an overview of our memory scanning protection and how it works.<\/p>\n<p>Memory scanning \u2013 searching within a process\u2019s memory (the process image, and\/or suspicious modules, threads, and heap regions) for threats \u2013 can be achieved in a variety of ways by security products, and at a variety of times. It may occur when a new process has been created, or regularly for all or some processes on the system. For example, a behavioral trigger for a memory scan may be malware calling CreateRemoteThread (or variants thereof) when it attempts to execute a malicious payload which has been injected into a process; or various other suspicious API calls which are commonly used in process injection and related techniques, such as VirtualAllocEx and WriteProcessMemory, to allocate memory and copy payloads, respectively. More sophisticated malware may call undocumented API functions, or eschew them altogether in favor of direct syscalls and other techniques; combating these methods requires a slightly different approach to memory scanning. There are various other possible behavioral triggers for a memory scan, including process creation, file reads\/writes, or connecting to an IP address.<\/p>\n<p>For almost a quarter of a century, we\u2019ve devoted a considerable amount of research and effort into developing various forms of memory scanning. This goes right back to the year 2000, when our capabilities included periodic and on-demand scans, evolving to behavioral-based memory scans with HIPS (Host-based Intrusion Prevention Systems), and now employing much more sophisticated behavioral technology which evolves as the threat landscape does. In particular, our capabilities are not reliant on pattern-matching but employ more complex logic, such as a Turing-complete definition language which employs an algorithmic approach.<\/p>\n<h1>Why do we need memory scanning?<\/h1>\n<p>The increasing ubiquity of antivirus and endpoint detection solutions means that threat actors are more cautious than ever about dropping malicious files to disk. From their perspective, doing so incurs the risk not only of that particular attack being thwarted, but also having to retool as their malware is analysed, signatured, and reverse-engineered.<\/p>\n<p>As a result, threat actors are increasingly turning to so-called \u201cfileless\u201d techniques, such as process injection, packers, virtualized code, and crypters, to run malicious payloads. For example, in our recent telemetry, we found that <strong>91% of ransomware samples, and 71% of RAT samples<\/strong>, were either custom-packed or used some kind of code obfuscation.<\/p>\n<p>Crucially, many of these techniques mean that the payload itself, even if it does touch disk, is in an encrypted form, and its true intentions and capabilities are only revealed in memory. This makes it difficult for security solutions to distinguish between clean and malicious files, and countermeasures \u2013 such as unpacking packed files by emulating packer instructions \u2013 often come at considerable computational cost.<\/p>\n<p>Many of these tools and techniques are available in open-source code repositories, or within commercial frameworks designed for legitimate penetration testing; as a result, it is trivial for threat actors to leverage them during attacks, often in slightly modified forms. (In an upcoming blog series, we\u2019ll walk through multiple different process injection techniques, complete with demonstrations, to show just how simple it is for threat actors to use off-the-shelf solutions). More advanced attackers, of course, are capable of finding new techniques, or creating novel combinations of, and refinements to, existing methods.<\/p>\n<p>In-memory attacks provide threat actors with a crucial advantage: they can evade detection by running malicious payloads without writing anything incriminating to disk. Some techniques \u2013 such as certain forms of process injection \u2013 can also complicate post-incident forensics, and enable threat actors to harvest sensitive information like credentials stored in memory, or to escalate their privileges.<\/p>\n<p>However, memory scanning takes advantage of one crucial fact: when it is loaded into memory, malware must reveal itself. It will be unpacked, or deobfuscated, or decrypted, so that it can achieve its end objective. Examining and assessing the region of memory in which this occurs, in real-time, allows us to make a judgment on whether a particular thread or process contains malicious code.<\/p>\n<p>And while memory scanning has historically been a computationally expensive process, particularly when scanning an entire system\u2019s memory, there are various ways in which we can target memory scans based on contextual cues about a given incident and other factors. This allows us to adapt flexibly to the situation and therefore maximize performance.<\/p>\n<h1>Types of memory scan<\/h1>\n<p>Scanning an entire system\u2019s memory can present performance challenges. More to the point, it isn\u2019t always necessary. Because memory scanning is a feature within a larger subset of detection and prevention tools, we often know where we want to scan, or when, and so we can perform a targeted memory scan against a process (or processes) at the time they exhibit a suspicious behavior.<\/p>\n<p>For example, say we\u2019re alerted to malware hijacking a thread within a running legitimate process (such as the Suspend, Inject, Resume, or SIR, attack), or malware launching a legitimate process and injecting a malicious payload into it (as in various forms of process injection). We can simply scan that thread or process, which both limits the performance overhead and makes it easier to focus resources on assessing that particular region of memory.<\/p>\n<p><a href=\"https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-951852\" src=\"https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image1.png\" alt=\"An image showing types of memory scanning, arranged as circular diagrams.\" width=\"640\" height=\"352\" srcset=\"https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image1.png 1265w, https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image1.png?resize=300,165 300w, https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image1.png?resize=768,422 768w, https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image1.png?resize=1024,563 1024w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p><em>Figure 1: An overview of our targeted memory scan types<\/em><\/p>\n<h2>Targeting by &#8216;where&#8217;<\/h2>\n<h3>Parent\/child<\/h3>\n<p>On occasions where a suspicious process spawns another process and injects into it, we can scan both the parent process and the child for malicious code.<\/p>\n<h3>Single thread<\/h3>\n<p>Attackers often target particular processes for injection, such as lsass.exe (which contains sensitive credentials that can be leveraged for privilege escalation) or explorer.exe. Typically, these processes have hundreds of threads. In such cases, it\u2019s not necessary to scan every single thread within the process to locate a malicious payload; instead, we pinpoint a specific thread via its ID \u2013 for example, by identifying threads which are about to be started or resumed via API calls such as CreateRemoteThread \u2013 and scan only that one.<\/p>\n<h2>Targeting by &#8216;when&#8217;<\/h2>\n<h3>Inline<\/h3>\n<p>Here, a scan is triggered by a specific behavior, such as process creation; analysts write behavioral rules based on suspicious behaviors which may not in themselves be sufficient to kill the process, but are reason enough to start a scan. We stop the given behavior from completing, and only allow it to continue once the scan has completed and if all appears well.<\/p>\n<h3>Asynchronous<\/h3>\n<p>An asynchronous scan is for circumstances where we can\u2019t make a decision about a particular behavior until the action is completed and we have more context, so we allow the process to continue while scanning it, while continuously updating the assessment.<\/p>\n<h3>Periodic background<\/h3>\n<p>Some fileless malware sits idle in memory for some time in order to evade defences or when it\u2019s waiting for C2 responses \u2013 sometimes for a few minutes or hours, but sometimes for much longer. To counter this, we can scan memory at regular intervals for malicious behaviors.<\/p>\n<h3>Scheduled<\/h3>\n<p>Here, the user wants to scan all machines at a specific time of day or at particular intervals, so as not to cause a spike in memory consumption.<\/p>\n<h3>Post-detection clean-up<\/h3>\n<p>If a behavioral rule is triggered and we block a process as a result, we also trigger a memory scan, in order to check for remnants of the malicious process in memory. For example, some malware employs a technique called a \u2018watcher thread\u2019, where one thread remains idle and simply monitors the execution of a malicious payload in another. If the primary thread is killed, the watcher thread takes over and resumes the activity. A post-detection clean-up memory scan terminates all associated threads, so that the malware won\u2019t relaunch.<\/p>\n<h1>Memory scanning in action<\/h1>\n<p>To demonstrate some of the memory scanning types we discuss above, we selected a malware sample and ran it in a lab environment protected by Sophos to capture the behavioral protection details reported after several memory scans. In a real-world environment, the product would block execution as soon as the malware triggered any of the below protections.<\/p>\n<p>The malware we\u2019re using for this test is the <a href=\"https:\/\/news.sophos.com\/en-us\/2021\/02\/02\/agent-tesla-amps-up-information-stealing-attacks\/\">Agent Tesla RAT<\/a>, a prolific and common threat often distributed via malicious spam emails. Threat actors use Agent Tesla to steal credentials through screenshots and keylogging, and more recent versions employ a variety of anti-sandbox and anti-analysis techniques.<\/p>\n<p>For convenience, as we discuss the memory scans and protections which fire when executing Agent Tesla, we\u2019ll also detail the corresponding MITRE ATT&amp;CK techniques.<\/p>\n<p><a href=\"https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-951853\" src=\"https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image2.png\" alt=\"An image showing five memory protections against the Agent Tesla RAT\" width=\"640\" height=\"381\" srcset=\"https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image2.png 1335w, https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image2.png?resize=300,178 300w, https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image2.png?resize=768,457 768w, https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image2.png?resize=1024,609 1024w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p><em>Figure 2: An overview of the scans initiated during our laboratory test of an Agent Tesla RAT sample<\/em><\/p>\n<h2>Evade_7a (T1055.012) (first released June 2019)<\/h2>\n<p>This memory scan rule triggers when a suspicious process launches a high-reputation clean process, potentially for process injection. Because the rule is triggered during a ProcessCreate event, the newly-created process hasn\u2019t yet started, so we scan the suspicious process for malicious code. In a real-world environment, Sophos protections would kill the parent and child processes, and remove any associated suspicious files.<\/p>\n<h3>Evade_34b (T1055.012) (first released February 2023)<\/h3>\n<p>This rule is technique-based, focusing specifically on process hollowing. It extrapolates specific process memory characteristics, and evaluates if a target process has been hollowed and injected with malicious content. Because this rule is focused on the technique, rather than specific code, it provides additional behavioral protection and assurance<\/p>\n<h2>Exec_14a (T1055.012) (first released October 2019)<\/h2>\n<p>Here, a memory scan occurs as a result of a specific event which occurs when malicious code is injected into a child process, as part of the SIR sequence referenced previously. This event triggers a protection.<\/p>\n<p><a href=\"https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-951854\" src=\"https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image3.png\" alt=\"A screenshot of computer code, with a memory dump on the left and dnSpy output on the right\" width=\"640\" height=\"248\" srcset=\"https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image3.png 1886w, https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image3.png?resize=300,116 300w, https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image3.png?resize=768,298 768w, https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image3.png?resize=1024,397 1024w, https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image3.png?resize=1536,595 1536w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p><em>Figure 3: The Tesla RAT code which corresponds to part of the SIR workflow, leading to a protection being triggered<\/em><\/p>\n<p>The process being scanned is already marked as a suspicious process, since it was launched by another suspicious process (the parent process in the above section). During a typical process injection attack, we want to block the injected process as early as possible, which we achieve by targeting the process shortly after malicious code has been injected. If the parent process didn\u2019t seem to contain any malicious code during the first scan, this scan is the next step; it allows us to check if the malware has unpacked or deobfuscated any malicious code<\/p>\n<h2>C2_1a (T1071.001 and T1095) (first released February 2020)<\/h2>\n<p>At this point, Agent Tesla makes an outbound connection to a C2 server.<\/p>\n<p><a href=\"https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-951855\" src=\"https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image4.png\" alt=\"A screenshot of computer code\" width=\"640\" height=\"374\" srcset=\"https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image4.png 1072w, https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image4.png?resize=300,175 300w, https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image4.png?resize=768,449 768w, https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image4.png?resize=1024,599 1024w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p><em>Figure 4: Part of the Tesla RAT code responsible for making an outbound C2 connection<\/em><\/p>\n<p>We report two different techniques here, because we also capture the port number; for ports 80 and 443, we report T1071, and for others, we report T1095. This is primarily an asynchronous scan. We don\u2019t intentionally hold process execution here, unlike the previous two scans, but when the memory detection triggers, the process would be immediately terminated.<\/p>\n<h2>Creds_2c (T1555.003) (first released September 2021)<\/h2>\n<p>This rule triggers when a process touches files which hold credentials (such as browser credentials) on disk; we scan the responsible process for any suspicious code. Typically, non-browser processes would not touch these files, so that\u2019s immediately suspicious.<\/p>\n<p><a href=\"https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-951856\" src=\"https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image5.png\" alt=\"A screenshot of computer code\" width=\"640\" height=\"269\" srcset=\"https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image5.png 1521w, https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image5.png?resize=300,126 300w, https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image5.png?resize=768,323 768w, https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/image5.png?resize=1024,430 1024w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p><em>Figure 5: The Tesla RAT looks for credentials in local storage<\/em><\/p>\n<h2>Memory_1b (first released September 2021)<\/h2>\n<p>Finally, this is a periodic background memory scan, which scans all running processes on a system at regular intervals. It provides an extra layer of assurance, ensuring that all processes are scanned even if there are no behavioral triggers.<\/p>\n<p>As shown in this example, having multiple scanning layers for different events and triggers \u2013 complemented by periodic scans across the whole system \u2013 is a key defence against in-memory threats, providing multiple opportunities to terminate malicious processes.<\/p>\n<h1>Conclusion<\/h1>\n<p>While memory scanning is not a panacea for all in-memory attacks, it is an important weapon in the continuing battle against increasingly sophisticated malware. As with any form of protection, memory scanning techniques must constantly adapt and respond to real-world developments, as threat actors develop new methods or build on those which already exist.<\/p>\n<p>As we noted earlier, we\u2019ve been doing this for a long time, and as the threat landscape has shifted and evolved, we\u2019ve continued to adapt our technologies in order to protect against threats, while keeping performance overheads to a minimum and ensuring we build redundancy into our various scan types to provide in-depth protection. These are central tenets of Sophos\u2019 memory scanning capabilities, and our current research reflects this.<\/p>\n<p>For example, one area we\u2019re currently researching is using the data and intelligence we\u2019ve gathered across all of our incidents, research, and analysis to statistically identify certain patterns in memory which are suggestive of a particular class of malware. Various ransomware families, for instance, may have very different codebases and approaches to enumerating and encrypting files \u2013 but, from an in-memory perspective, there are commonalities across many of them which we can use to build in more generic protections. Similarly, RATs and infostealers may be very distinct in themselves, but they often generate predictable sequences of behavior which, at the memory level, can be a good predictor that a particular thread or process has been hijacked by a RAT or infostealer.<\/p>\n<\/p><\/div>\n<p><a href=\"https:\/\/news.sophos.com\/en-us\/2023\/11\/09\/memory-scanning-leaves-attackers-nowhere-to-hide\/\" target=\"bwo\" >http:\/\/feeds.feedburner.com\/sophos\/dgdY<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/news.sophos.com\/wp-content\/uploads\/2023\/10\/shutterstock_2136839167.jpg\"\/><\/p>\n<p><strong>Credit to Author: Matt Wixey| Date: Thu, 09 Nov 2023 13:46:19 +0000<\/strong><\/p>\n<p>Sophos X-Ops takes an in-depth look at memory scanning and why it matters<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[10378,10377],"tags":[129,11299,22060,26777,27030,16771],"class_list":["post-23345","post","type-post","status-publish","format-standard","hentry","category-security","category-sophos","tag-featured","tag-fileless-malware","tag-memory-scanning","tag-process-injection","tag-sophos-x-ops","tag-threat-research"],"_links":{"self":[{"href":"https:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/posts\/23345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/comments?post=23345"}],"version-history":[{"count":0,"href":"https:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/posts\/23345\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/media?parent=23345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/categories?post=23345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.palada.net\/index.php\/wp-json\/wp\/v2\/tags?post=23345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}