<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Malware Analysis on Kartone's Reversing Garage</title><link>https://blog.kartone.ninja/tags/malware-analysis/</link><description>Recent content in Malware Analysis on Kartone's Reversing Garage</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Thu, 29 Oct 2020 08:59:13 +0000</lastBuildDate><atom:link href="https://blog.kartone.ninja/tags/malware-analysis/index.xml" rel="self" type="application/rss+xml"/><item><title>Project Sodinokibi</title><link>https://blog.kartone.ninja/project-sodinokibi/</link><pubDate>Thu, 29 Oct 2020 08:59:13 +0000</pubDate><guid>https://blog.kartone.ninja/project-sodinokibi/</guid><description>&lt;img src="https://blog.kartone.ninja/images/2020/10/Sodinokibi-Ransom-Note-2.png" alt="Featured image of post Project Sodinokibi" /&gt;&lt;h3 id="learning-python"&gt;Learning Python
&lt;/h3&gt;&lt;p&gt;Python is the language I always wanted to learn. I tried but failed every single time, don&amp;rsquo;t know exactly why. This time was different though, I knew from the first line of code. So, with a little push of a dear friend of mine (thanks &lt;a class="link" href="https://twitter.com/elioxyz" target="_blank" rel="noopener"
&gt;Elio&lt;/a&gt;!), I tried to investigate how to decode Sodinokibi ransomware configurations for hundreds, maybe thousands, of samples. I intended to understand, using powerful insights from VirusTotal Enterprise API, if there are relationships between Threat Actor, mapped inside the ransomware configuration, and the country visible from the VirusTotal sample submission.&lt;br&gt;
I am perfectly aware that it&amp;rsquo;s not as easy as it seems: the ransomware sample submission&amp;rsquo;s country, visible from VirusTotal, may not be the country affected by the ransomware itself. But, in one case of another, I think there could be somehow a link between the two parameters: maybe from the Incident Response perspective.&lt;/p&gt;
&lt;h3 id="getting-the-samples"&gt;Getting the samples
&lt;/h3&gt;&lt;p&gt;My first step was to get as many samples as I could. My first thought was to use VirusTotal API: I&amp;rsquo;m lucky enough to have an Enterprise account, but the results were overwhelming and, due to the fact I was experimenting with Python, the risk of running too many requests and consume my threshold was too high. So I opted to use another excellent malware sharing platform: &lt;a class="link" href="https://bazaar.abuse.ch/browse/" target="_blank" rel="noopener"
&gt;Malware Bazaar by Abuse.ch&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;All the code is available &lt;a class="link" href="https://github.com/kartone/sodinokibiTA" target="_blank" rel="noopener"
&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;downloaded_samples = []
data = { &amp;#39;query&amp;#39;: &amp;#39;get_taginfo&amp;#39;, &amp;#39;tag&amp;#39;: args.tag_sample, &amp;#39;limit&amp;#39;: 1000 }
response = requests.post(&amp;#39;https://mb-api.abuse.ch/api/v1/&amp;#39;, data = data, timeout=10)
maldata = response.json()
print(&amp;#34;[+] Retrieving the list of downloaded samples...&amp;#34;)
for file in glob.glob(SAMPLES_PATH+&amp;#39;*&amp;#39;):
filename = ntpath.basename(os.path.splitext(file)[0])
downloaded_samples.append(filename)
print(&amp;#34;[+] We have a total of %s samples&amp;#34; % len(downloaded_samples))
for i in range(len(maldata[&amp;#34;data&amp;#34;])):
if &amp;#34;Decryptor&amp;#34; not in maldata[&amp;#34;data&amp;#34;][i][&amp;#34;tags&amp;#34;]:
for key in maldata[&amp;#34;data&amp;#34;][i].keys():
if key == &amp;#34;sha256_hash&amp;#34;:
value = maldata[&amp;#34;data&amp;#34;][i][key]
if value not in downloaded_samples:
print(&amp;#34;[+] Downloading sample with &amp;#34;, key, &amp;#34;-&amp;gt;&amp;#34;, value)
if args.get_sample:
get_sample(value)
if args.clean_sample:
housekeeping(EXT_TO_CLEAN)
else:
print(&amp;#34;[+] Skipping the sample because of Tag: Decryptor&amp;#34;)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This block of code essentially builds the request for the back-end API where the tag to search for comes from the command line parameter. I defaulted it to &lt;strong&gt;Sodinokibi&lt;/strong&gt;. It then creates a list of samples already present in the &lt;code&gt;./samples&lt;/code&gt; directory not to download them again. Interestingly, because there are many Sodinokibi decryptors executables on the Malware Bazaar platform, I needed some sort of sanitization not to download them. When it founds a sample not present inside the local directory, It then calls the function to download it.&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;def get_sample(hash):
headers = { &amp;#39;API-KEY&amp;#39;: KEY }
data = { &amp;#39;query&amp;#39;: &amp;#39;get_file&amp;#39;, &amp;#39;sha256_hash&amp;#39;: hash }
response = requests.post(&amp;#39;https://mb-api.abuse.ch/api/v1/&amp;#39;, data=data, timeout=15, headers=headers, allow_redirects=True)
with open(SAMPLES_PATH+hash+&amp;#39;.zip&amp;#39;, &amp;#39;wb&amp;#39;) as f:
f.write(response.content)
print(&amp;#34;[+] Sample downloaded successfully&amp;#34;)
with pyzipper.AESZipFile(SAMPLES_PATH+hash+&amp;#39;.zip&amp;#39;) as zf:
zf.extractall(path=SAMPLES_PATH, pwd=ZIP_PASSWORD)
print(&amp;#34;[+] Sample unpacked successfully&amp;#34;)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;A straightforward function: builds the API call, gets the zipped sample, unpack, and saves it inside the directory &lt;code&gt;./samples&lt;/code&gt;. &lt;strong&gt;Note that the sample filenames are always their SHA-256 hash.&lt;/strong&gt; After unpacking it, I made a small housekeeping function to get rid of the zip files.&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;def housekeeping(ext):
try:
for f in glob.glob(SAMPLES_PATH+&amp;#39;*.&amp;#39;+ext):
os.remove(f)
except OSError as e:
print(&amp;#34;Error: %s - %s &amp;#34; % (e.filename, e.strerror))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;a class="link" href="https://asciinema.org/a/356798" target="_blank" rel="noopener"
&gt;This is what happens when you run the script.&lt;/a&gt;&lt;/p&gt;
&lt;h3 id="getting-insights-on-ransomware-configuration"&gt;Getting insights on ransomware configuration
&lt;/h3&gt;&lt;p&gt;Now it&amp;rsquo;s time to analyze these samples to get the pieces of information we need. The plan is to extract the configuration from an RC4 encrypted configuration stored inside a PE file section. Save &lt;strong&gt;ActorID&lt;/strong&gt;, &lt;strong&gt;CampaignID&lt;/strong&gt;, and executable hash. With the latter, we then query VirusTotal API to get insights for the sample submission: the &lt;strong&gt;City&lt;/strong&gt; and the &lt;strong&gt;Country&lt;/strong&gt; from where the sample was submitted and when there was the submission. As I wanted to map these pieces of information on a map, with &lt;a class="link" href="https://opencagedata.com" target="_blank" rel="noopener"
&gt;OpenCage&lt;/a&gt; API I then obtained cities coordinates of the submissions.&lt;/p&gt;
&lt;p&gt;The code to build the API calls and parse the response JSON is rough, shallow and straightforward I would not go with it. I&amp;rsquo;m sure there are plenty of better ways to do its job, but&amp;hellip;it&amp;rsquo;s my first time with Python! So bear with me, please. What I think it&amp;rsquo;s interesting is the function that extracts and decrypts the configuration from the ransomware executable PE file. These are the lines of code that do this task:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;excluded_sections = [&amp;#39;.text&amp;#39;, &amp;#39;.rdata&amp;#39;, &amp;#39;.data&amp;#39;, &amp;#39;.reloc&amp;#39;, &amp;#39;.rsrc&amp;#39;, &amp;#39;.cfg&amp;#39;]
def arc4(key, enc_data):
var = ARC4.new(key)
dec = var.decrypt(enc_data)
return dec
def decode_sodinokibi_configuration(f):
filename = os.path.join(&amp;#39;./samples&amp;#39;, f)
filename += &amp;#39;.exe&amp;#39;
with open(filename, &amp;#34;rb&amp;#34;) as file:
bytes = file.read()
str_hash = hashlib.sha256(bytes).hexdigest()
pe = pefile.PE(filename)
for section in pe.sections:
section_name = section.Name.decode().rstrip(&amp;#39;\x00&amp;#39;)
if section_name not in excluded_sections:
data = section.get_data()
enc_len = struct.unpack(&amp;#39;I&amp;#39;, data[0x24:0x28])[0]
dec_data = arc4(data[0:32], data[0x28:enc_len + 0x28])
parsed = json.loads(dec_data[:-1])
return str_hash, parsed[&amp;#39;pid&amp;#39;], parsed[&amp;#39;sub&amp;#39;]
#print(&amp;#34;Sample SHA256 Hash: &amp;#34;, str_hash)
#print(&amp;#34;Actor ID: &amp;#34;, parsed[&amp;#39;pid&amp;#39;])
#print(&amp;#34;Campaign ID: &amp;#34;, parsed[&amp;#39;sub&amp;#39;])
#print(&amp;#34;Attacker&amp;#39;s Public Encryption Key: &amp;#34;, parsed[&amp;#39;pk&amp;#39;])
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Disclaimer: these lines are, obviously, not mine. I modified the script provided by the guys of &lt;a class="link" href="https://blogs.blackberry.com/en/2019/07/threat-spotlight-sodinokibi-ransomware" target="_blank" rel="noopener"
&gt;BlackBerry ThreatVector&lt;/a&gt;. I invite you to read where they explain how the configuration is stored within the section, where&amp;rsquo;s the RC4 encryption key and how to decrypt it.&lt;/p&gt;
&lt;p&gt;In my version of the script, it runs on Python3 and uses a standard library for the RC4 algorithm. Also, it&amp;rsquo;s worth to mention that &lt;strong&gt;this script fails if input samples are packed&lt;/strong&gt;. It expects the existence of the particular section with the saved encrypted configuration; it fails otherwise. I added some controls to handle miserable crashes, but there are unmanaged cases still: I&amp;rsquo;m so new to Python!&lt;/p&gt;
&lt;p&gt;In the end, we have a dear old CSV file enriched with a bunch of information: &lt;strong&gt;Country, City, Latitude, Longitude, ActorID, CampaignID, Hash, Timestamp.&lt;/strong&gt; We&amp;rsquo;re ready to map it.&lt;/p&gt;
&lt;h3 id="understanding-the-data"&gt;Understanding the data
&lt;/h3&gt;&lt;p&gt;Our data is described inside a &lt;code&gt;data.csv&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2020/08/image.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Field &lt;code&gt;aid&lt;/code&gt; (ActorID) is changed, during the months, from an integer number, like ActorID: 39 to a hash representation. For now, we have only 174 samples where we managed to extract the configuration. We can now group the data by aid field and count the submissions.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2020/08/image-1.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;From what I see, I can understand that the samples related to ThreatActor with the ID 39 have nine submissions from the city of Ashburn US. &lt;strong&gt;I have to comprehend why this city has so many submissions related to Sodinokibi&lt;/strong&gt;. I hope that someone that reads this post would help me to understand and shed some light.&lt;/p&gt;
&lt;p&gt;If we map the ThreatActorID vs the City of the submission, we can easily see the data.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2020/08/image-2.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;ThreatActors vs Submissions City&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2020/08/image-3.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Submissions City vs Submissions count&lt;/p&gt;
&lt;p&gt;Next steps would be acquiring as many samples as I can. The best choice would be using VirusTotal API to retrieve the samples and this is what I&amp;rsquo;m going to do. Hopefully I won&amp;rsquo;t burn my entire Company API limit.&lt;/p&gt;
&lt;p&gt;All the scripts used in this post, the data and the Jupiter notebook used to map the data is available &lt;a class="link" href="https://github.com/kartone/sodinokibiTA" target="_blank" rel="noopener"
&gt;here&lt;/a&gt;.&lt;/p&gt;</description></item><item><title>The p0sT5n1F3r Backdoor</title><link>https://blog.kartone.ninja/the-p0st5n1f3r-backdoor/</link><pubDate>Wed, 16 Oct 2019 19:20:00 +0000</pubDate><guid>https://blog.kartone.ninja/the-p0st5n1f3r-backdoor/</guid><description>&lt;img src="https://blog.kartone.ninja/images/2025/08/postsniffer.jpg" alt="Featured image of post The p0sT5n1F3r Backdoor" /&gt;&lt;p&gt;How does a malicious backdoor designed to sniff sensitive HTTPS traffic go completely undetected?&lt;/p&gt;
&lt;p&gt;During an IR case, we found and dissected a highly targeted malware sample, a custom Apache module we call&lt;code&gt;p0sT5n1F3r&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This threat was specifically engineered for its target&amp;rsquo;s environment and was rated 100% clean by all major security vendors due to its extensive use of custom encryption.&lt;/p&gt;
&lt;p&gt;This report details the reverse engineering journey, from the initial static analysis to the critical breakthrough: cracking its custom RC4 encryption scheme. This discovery allowed us to unveil its true purpose—intercepting financial transaction data—and even uncover a hidden HTML interface used by the attackers.&lt;/p&gt;
&lt;p&gt;Read the &lt;a class="link" href="https://blog.kartone.ninja/files/report.pdf" &gt;full technical deep dive&lt;/a&gt; to learn how this threat was unmasked.&lt;/p&gt;</description></item><item><title>WannaCry, two years later: a deep look into its code</title><link>https://blog.kartone.ninja/malware-analysis-a-wannacry-sample-found-in-the-wild-2/</link><pubDate>Thu, 23 May 2019 09:17:00 +0000</pubDate><guid>https://blog.kartone.ninja/malware-analysis-a-wannacry-sample-found-in-the-wild-2/</guid><description>&lt;img src="https://blog.kartone.ninja/images/2019/05/wannacry-screencap_thumb800-2.jpg" alt="Featured image of post WannaCry, two years later: a deep look into its code" /&gt;&lt;p&gt;My own technical analysis of the malware that, in 2017, spread like wildfire encrypting thousands of computers, using one of the tools leaked from the National Security Agency by the group named ShadowBrokers.&lt;/p&gt;
&lt;p&gt;Almost two years passed after that weekend of May 2017, when the crypto-worm WannaCry infested the net thanks to the EternalBlue exploit. In roughly two days, WannaCry spread itself all over the world infecting almost 230.000 computers in over 150 countries:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/Countries_initially_affected_in_WannaCry_ransomware_attack.svg"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;By TheAwesomeHwyh&lt;/p&gt;
&lt;p&gt;At that time, working as an Information Security Officer, with my colleagues, especially the guys from IT Infrastructure dept., worked hard to keep the entire Company perimeter safe. Luckily for us, we were not hit by the ransomware, but a lot of effort was spent explaining to the rest of the Company what happened.&lt;/p&gt;
&lt;h3 id="flash-forward-to-2019"&gt;Flash forward to 2019
&lt;/h3&gt;&lt;p&gt;Since this January, I&amp;rsquo;ve been running my own Dionaea honeypot that keeps catching a huge number of WannaCry samples. Just to give you some numbers, within two months, the 445 port was hit almost half a million times and I was able to collect roughly 18.000 of its samples at the rate of almost 300 samples per day.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image.png"
loading="lazy"
&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-1.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;If you notice from the file size, all these samples are all the same, and everyone of them is a WannaCry sample, delivered right to the 445 port in a DLL fashion.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-2.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Just to make a contribution to the WannaCry story, though small and useless, I thought it would be fun to analyze the internals of this malware as I wasn&amp;rsquo;t able to do it back in the days. I will concentrate the analysis on its various layers and the most important parts of the code that make this malware unique.&lt;/p&gt;
&lt;h2 id="peeling-the-onion"&gt;Peeling the onion
&lt;/h2&gt;&lt;p&gt;First look at one of these samples, confirms that we&amp;rsquo;re dealing with a malicious DLL and it&amp;rsquo;s worth to note its compilation timestamp. Let&amp;rsquo;s call this as &lt;code&gt;launcher.dll&lt;/code&gt; because of the evidence found in a string inside the code.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-3.png"
loading="lazy"
&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-12.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Luckily for us, this sample is not packed. We can check its &lt;strong&gt;Import and Export Address Table&lt;/strong&gt; to get an idea of what this sample is able to do.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-4.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Easily enough, checking the imported API, we can assume that the malware uses something in its &lt;strong&gt;resource&lt;/strong&gt; &lt;strong&gt;section&lt;/strong&gt; and supposedly create a file and run a process. Commonly, DLL malware exports functionalities to the outside via its &lt;strong&gt;Export Address Table&lt;/strong&gt;. We can see only one exported function and it&amp;rsquo;s called &lt;strong&gt;PlayGame&lt;/strong&gt;*:*&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-5.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;As noted above, malware imported some specific APIs to manage its resource section, like &lt;code&gt;FindResourceA&lt;/code&gt; and &lt;code&gt;LoadResource&lt;/code&gt;. We can easily recognize the &lt;em&gt;magic numbers&lt;/em&gt; of a Portable Executable file - a Windows executable file - stored inside this section. We can dump it easily with tools like ResourceHacker:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-6.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;But before analyzing it, we need to get rid of &lt;em&gt;some&lt;/em&gt; bytes in the header, we&amp;rsquo;ll come to these bytes later.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-7.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;So now, we can open it and check its sections like we just did with the aforementioned DLL. Interestingly this new dumped executable seems 7 years older than the first one, its compile timestamp is dated November 2010 but, be aware that this date can be easily fake.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-8.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;We can get an idea of what its purpose is by checking out the imported libraries:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-10.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;We have to expect much more complexity in this stage than the DLL. We have a bunch of standard libraries like &lt;code&gt;KERNEL32.dll&lt;/code&gt; or &lt;code&gt;WININET.dll&lt;/code&gt; and &lt;code&gt;iphlpapi.dll&lt;/code&gt;. This DLL was unknown for me so I found, from &lt;a class="link" href="https://docs.microsoft.com/en-us/windows/desktop/iphlp/ip-helper-start-page" target="_blank" rel="noopener"
&gt;MSDN&lt;/a&gt;, that:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Purpose&lt;/em&gt;&lt;br&gt;
The Internet Protocol Helper (IP Helper) API enables the retrieval and modification of network configuration settings for the local computer.&lt;br&gt;
The IP Helper API is applicable in any computing environment where programmatically manipulating network and TCP/IP configuration is useful. Typical applications include IP routing protocols and Simple Network Management Protocol (SNMP) agents.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-9.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;A quick look suggests that this executable operates with Windows services configuration, manages files and resources and also, has network capabilities:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-11.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;h3 id="the-plan"&gt;The Plan
&lt;/h3&gt;&lt;p&gt;My plan is to give a deep look inside all various stages that the malware extracts during its execution, analyzing its code and how it interacts with internal Windows subsystems.&lt;/p&gt;
&lt;p&gt;For this reason, we&amp;rsquo;re now stepping back to analyze and understand how the DLL extracts this executable in the first place. Then we&amp;rsquo;ll give a look inside the debugger to see how things happen in realtime and then, we will analyze and try to understand what this executable is going to do once it infects the system.&lt;/p&gt;
&lt;h3 id="analysis-of-the-first-layer-launcherdll"&gt;Analysis of the first layer: launcher.dll
&lt;/h3&gt;&lt;p&gt;The purpose of this DLL is exactly what we supposed thanks to the analysis of the imported libraries. The only exported function &lt;code&gt;PlayGame&lt;/code&gt; is easily disassembled by IDAPro.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-13.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;The first call to &lt;code&gt;sprintf&lt;/code&gt; compose the &lt;code&gt;Dest&lt;/code&gt; string as &lt;code&gt;C:\WINDOWS\mssecsvc.exe&lt;/code&gt;. Then it calls two functions, &lt;code&gt;sub_10001016&lt;/code&gt; that extracts, from its resource section, the executable we dumped before and then, saves it into a new file named as &lt;code&gt;Dest&lt;/code&gt; string; after that &lt;code&gt;sub_100010AB&lt;/code&gt; runs the file. Notice that we have just gained our first &lt;strong&gt;host-based indicator&lt;/strong&gt;: &lt;strong&gt;&lt;code&gt;C:\WINDOWS\MSSECSVC.EXE&lt;/code&gt;&lt;/strong&gt; for this malware detection.&lt;/p&gt;
&lt;h4 id="function-sub_10001016-aka-extractandcreate"&gt;Function &lt;code&gt;sub_10001016&lt;/code&gt; aka &lt;code&gt;ExtractAndCreate&lt;/code&gt;
&lt;/h4&gt;&lt;p&gt;For better reading and understanding this function, we can rename it as &lt;code&gt;ExtractAndCreate&lt;/code&gt; and we can split it into two parts: the &lt;em&gt;extract&lt;/em&gt; part and the &lt;em&gt;create file&lt;/em&gt; part.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-14.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Disassembled extract part&lt;/p&gt;
&lt;p&gt;During this phase, the malware uses four API calls, that are completely covered inside the MSDN.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;FindResourceA&lt;/code&gt;: &lt;em&gt;Determines the location of a resource with the specified type and name in the specified module.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;LoadResource&lt;/code&gt;: &lt;em&gt;Retrieves a handle that can be used to obtain a pointer to the first byte of the specified resource in memory.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;LockResource&lt;/code&gt;: &lt;em&gt;Retrieves a pointer to the specified resource in memory.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;SizeOfResource&lt;/code&gt;: &lt;em&gt;Retrieves the size, in bytes, of the specified resource.&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That being said, we can now analyze step by step this simple four blocks of code. First function prototype is:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;HRSRC FindResourceA(
HMODULE hModule,
LPCSTR lpName,
LPCSTR lpType
);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We have three function parameters that, as per calling convention, must be pushed in reverse order, so:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;push offset Type ; &amp;#34;W&amp;#34;
push 65h ; lpName
push hModule ; hModule
call ds:FindResourceA
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Parameter &lt;code&gt;hModule&lt;/code&gt; is being populated inside the &lt;code&gt;DLLMain&lt;/code&gt; method, and is equals to variable &lt;code&gt;hinstDLL&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-15.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;hinstDLL&lt;/code&gt;: &lt;em&gt;A handle to the DLL module. The value is the &lt;strong&gt;base address of the DLL&lt;/strong&gt;. The HINSTANCE of a DLL is the same as the HMODULE of the DLL, so hinstDLL can be used in calls to functions that require a module handle.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;lpName&lt;/code&gt;: &lt;em&gt;The name of the resource.&lt;/em&gt; In this case, name is &lt;code&gt;0x65&lt;/code&gt; or &lt;code&gt;101&lt;/code&gt; in decimal value. If you look, name is confirmed by analyzing the DLL with ResourceHacker:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-16.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;lpType&lt;/code&gt;: &lt;em&gt;The resource type.&lt;/em&gt; Can be also noticed in the screenshot above.&lt;/p&gt;
&lt;p&gt;From &lt;a class="link" href="https://docs.microsoft.com/it-it/windows/desktop/api/winbase/nf-winbase-findresourcea" target="_blank" rel="noopener"
&gt;MSDN&lt;/a&gt;: &lt;em&gt;If the function succeeds, the return value is a handle to the specified resource&amp;rsquo;s information block. To obtain a handle to the resource, pass this handle to the &lt;code&gt;LoadResource&lt;/code&gt; function. If the function fails, the return value is &lt;code&gt;NULL&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Coming back to the disassembly, this handle is returned into &lt;code&gt;EAX&lt;/code&gt; and then moved inside &lt;code&gt;EDI&lt;/code&gt;, where is being tested to check if it&amp;rsquo;s &lt;code&gt;null&lt;/code&gt;. If it&amp;rsquo;s not, the handle is pushed, as the second argument, to the next API call to &lt;code&gt;LoadResource&lt;/code&gt;. Quoting &lt;a class="link" href="https://msdn.microsoft.com/en-us/library/ms648046%28v=VS.85%29.aspx" target="_blank" rel="noopener"
&gt;MSDN&lt;/a&gt;: &lt;em&gt;it&lt;/em&gt; &lt;em&gt;retrieves a handle that can be used to obtain a pointer to the first byte of the specified resource in memory.&lt;/em&gt; It also suggests:&amp;quot;&amp;hellip;&lt;em&gt;to obtain a pointer to the first byte of the resource data, call the &lt;a class="link" href="https://msdn.microsoft.com/en-us/library/ms648047%28v=vs.85%29.aspx" target="_blank" rel="noopener"
&gt;LockResource&lt;/a&gt; function; to obtain the size of the resource, call &lt;a class="link" href="https://msdn.microsoft.com/en-us/library/ms648048%28v=vs.85%29.aspx" target="_blank" rel="noopener"
&gt;SizeofResource&lt;/a&gt;&amp;quot;.&lt;/em&gt;&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;HGLOBAL WINAPI LoadResource(
_In_opt_ HMODULE hModule,
_In_ HRSRC hResInfo
);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;hModule&lt;/code&gt;: &lt;em&gt;A handle to the module whose executable file contains the resource.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;hResInfo&lt;/code&gt;: &lt;em&gt;A handle to the resource to be loaded.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The same approach applies with the other two API calls: &lt;code&gt;LockResource&lt;/code&gt; and &lt;code&gt;SizeofResource&lt;/code&gt;. The interesting thing to note here is that the return value from this last call, stored inside &lt;code&gt;EAX&lt;/code&gt; register as &lt;code&gt;500000&lt;/code&gt;, won&amp;rsquo;t be used at all:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/13.PNG"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;So now, looking in the debugger, we have:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;EAX&lt;/code&gt; = &lt;code&gt;500000&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ESI&lt;/code&gt; = &lt;code&gt;10004060&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;code&gt;ESI&lt;/code&gt; register contains the pointer to the memory region referred to the resource section that contains the executable itself. You can notice it thanks to the &lt;code&gt;MZ&lt;/code&gt; header in the memory dump. Remember the &lt;code&gt;4 bytes&lt;/code&gt; that were been removed with hex editor before? According to &lt;a class="link" href="https://docs.microsoft.com/en-us/windows/desktop/menurc/resource-file-formats" target="_blank" rel="noopener"
&gt;MSDN&lt;/a&gt; this DWORD is the actual size of raw data inside the resource section of the binary itself. So, this value &lt;code&gt;0x0038D000&lt;/code&gt;is moved into &lt;code&gt;EBX&lt;/code&gt; and then pushed as &lt;code&gt;lpBuffer&lt;/code&gt; to the &lt;code&gt;WriteFile&lt;/code&gt; function. Pretty standard call here: &lt;code&gt;CreateFileA&lt;/code&gt; will create a file with specific attributes. Parameter &lt;code&gt;dwFlagsAndAttributes&lt;/code&gt;, according to MSDN, a value of &lt;code&gt;0x4&lt;/code&gt;stands for: &lt;em&gt;&amp;ldquo;The file is part of or used exclusively by an operating system&amp;rdquo;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-17.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;After the call to &lt;code&gt;WriteFile&lt;/code&gt;, we have our executable saved and ready to run. The interesting parameters for this call are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;lpBuffer&lt;/code&gt;: equals to &lt;code&gt;ESI&lt;/code&gt;, is the value returned by the call to &lt;code&gt;LockResource&lt;/code&gt; and is a pointer to the buffer to write into the file. Basically is a pointer to the binary inside the resource section.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nNumberOfBytesToWrite&lt;/code&gt;: as we said earlier, this parameter is the value pointed by the &lt;code&gt;ESI&lt;/code&gt; to a &lt;code&gt;DWORD&lt;/code&gt; inside of resource header. Its value represent the size of the binary data.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So now, we can enable a breakpoint right after the &lt;code&gt;WriteFile&lt;/code&gt; call and get the freshly created executable.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/18.PNG"
loading="lazy"
&gt;&lt;/p&gt;
&lt;h4 id="function-sub_100010ab-aka-runthefile"&gt;Function &lt;code&gt;sub_100010AB&lt;/code&gt; aka &lt;code&gt;RunTheFile&lt;/code&gt;
&lt;/h4&gt;&lt;p&gt;Here we&amp;rsquo;re dealing with a very simple API call to &lt;code&gt;CreateProcessA&lt;/code&gt;, nothing fancy to add. I&amp;rsquo;d prefer not to dig inside all these parameters, it&amp;rsquo;s completely covered inside the MSDN.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-20.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;h4 id="conclusion-after-the-first-layer"&gt;Conclusion after the first layer
&lt;/h4&gt;&lt;p&gt;What I would show here is my own study process: be aware, sometimes it can be very, very time-consuming but it gives me a big, complete and deep look inside Windows internals and how malware uses them. This proceeding, for me as a novice, helped a lot.&lt;/p&gt;
&lt;h3 id="analysis-of-the-second-layer-mssecsvcexe"&gt;Analysis of the second layer: mssecsvc.exe
&lt;/h3&gt;&lt;p&gt;This will differs from the DLL file. As we noted initially, this executable is way more complex: we&amp;rsquo;ll deal with various libraries and functionalities. But all start with a (Win)main function, right?&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-21.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Do you remember the kill-switch? Do you remember the &lt;a class="link" href="https://en.wikipedia.org/wiki/WannaCry_ransomware_attack" target="_blank" rel="noopener"
&gt;story&lt;/a&gt; behind? Give it a read, it&amp;rsquo;s very interesting.&lt;/p&gt;
&lt;p&gt;In general terms, the main function of a Windows program is named &lt;code&gt;WinMain&lt;/code&gt;, this is the first function that will be called when the program starts. We see a very strange url inside this code. Exactly the string is: &lt;code&gt;http://www.iuqerfsodp9ifjaposdfjhgosurijfaewrwergwea.com&lt;/code&gt; and is referred through the &lt;code&gt;EDI&lt;/code&gt; register. After that, the &lt;code&gt;WinINet&lt;/code&gt; subsystem is initialized using the call to &lt;code&gt;InternetOpenA&lt;/code&gt;, this function &lt;em&gt;returns a valid handle that the application passes to subsequent WinINet functions.&lt;/em&gt; Next, there&amp;rsquo;s a call to &lt;code&gt;InternetOpenUrlA&lt;/code&gt; that &lt;em&gt;opens a resource specified by a complete FTP or HTTP URL.&lt;/em&gt; After that the handle is closed and a new function is called: &lt;code&gt;sub_408090&lt;/code&gt;, we&amp;rsquo;ll name it &lt;code&gt;ServiceStuff&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-22.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;In the first block of code, according to MSDN: &lt;code&gt;GetModuleFileNameA&lt;/code&gt; &lt;em&gt;retrieves the fully qualified path for the file that contains the specified module. The module must have been loaded by the current process, first parameter&lt;/em&gt; &lt;code&gt;hModule&lt;/code&gt; &lt;em&gt;is the handle to the loaded module whose path is being requested. If this parameter is &lt;strong&gt;NULL&lt;/strong&gt;,&lt;/em&gt; &lt;code&gt;GetModuleFileNameA&lt;/code&gt; &lt;em&gt;retrieves the path of the executable file of the current process.&lt;/em&gt; Here the value is set to &lt;code&gt;NULL&lt;/code&gt;, so it retrieves the name of the executable itself:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-23.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;We then find a check on the number of arguments: if there are arguments the &lt;code&gt;TRUE&lt;/code&gt; path will be taken. Because, in our case, we&amp;rsquo;re debugging without any argument, the &lt;code&gt;FALSE&lt;/code&gt; path is taken and a new function &lt;code&gt;sub_407F20&lt;/code&gt; is called. This is a simple function that calls other two, so let&amp;rsquo;s call it &lt;code&gt;FunctionCaller&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-24.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Simple enough &lt;code&gt;sub_407C40&lt;/code&gt; create a new service and then starts it, so we name it &lt;code&gt;CreateAndStartService&lt;/code&gt;. Service will be run with command line &lt;code&gt;mssecsvc.exe -m security&lt;/code&gt; and with a display name as &lt;code&gt;&amp;quot;Microsoft Security Center (2.0) Service&amp;quot;&lt;/code&gt; defined as &lt;code&gt;&amp;quot;mssecsvc2.0&amp;quot;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-25.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;When we move then to &lt;code&gt;sub_407cE0&lt;/code&gt;, things start to become fun. For the sake of simplicity, we&amp;rsquo;ll analyze this function in four parts. The first part is easy because the malware dynamically resolve some APIs:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-26.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Nothing too much complicated here: it uses &lt;code&gt;GetProcAddress&lt;/code&gt; to populate some variables with the address of specific APIs, so it can call them in the next lines of code. After that, the second part will manage the resource section, just like the way we analyzed in the DLL &lt;code&gt;launcher.dll&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-27.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;This is confirmed into the debugger:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-28.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;The return value from &lt;code&gt;LockResource&lt;/code&gt;, as we know, is the pointer to the resource section into the binary and we can notice the &lt;code&gt;MZ&lt;/code&gt; header into the memory dump. We then reach another interesting piece of code:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-29.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Two distinct string: &lt;code&gt;Dest&lt;/code&gt; and &lt;code&gt;NewFileName&lt;/code&gt;, are created using &lt;code&gt;sprintf&lt;/code&gt; function. This two evidence are others good host-based indicators:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Dest&lt;/code&gt; = &lt;code&gt;C:\WINDOWS\tasksche.exe&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;NewFileName&lt;/code&gt; = &lt;code&gt;C:\WINDOWS\qeriuwjhrf&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;After that, the &lt;em&gt;old&lt;/em&gt; file &lt;code&gt;tasksche.exe&lt;/code&gt; is moved into the new file &lt;code&gt;qeriuwjhrf&lt;/code&gt; and a new &lt;code&gt;tasksche.exe&lt;/code&gt; is created. Now, I found myself lost into somehow obscure code: I got that &lt;code&gt;WriteFile&lt;/code&gt; will dump the &lt;strong&gt;R&lt;/strong&gt; resource into the created file &lt;code&gt;tasksche.exe&lt;/code&gt; and runs it at the end. What&amp;rsquo;s inside the middle part, for me, remains in the dark.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/Schermata-2019-05-17-alle-16.30.51.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;In situations like this, I prefer to view the code inside the debugger because viewing the code during runtime maybe can help to shed some light. Indeed, seems like It created the command line for the incoming &lt;code&gt;CreateProcessA&lt;/code&gt; call.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-31.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;To recap: this function dumps its resource data inside a new executable file named &lt;code&gt;tasksche.exe&lt;/code&gt;, making a copy inside another file named &lt;code&gt;qeriuwjhrf&lt;/code&gt;, and then run &lt;code&gt;tasksche.exe /i&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Stepping back to &lt;code&gt;ServiceStuff&lt;/code&gt; function, there&amp;rsquo;s the other path to analyze: when there are the arguments &lt;code&gt;&amp;quot;-m security&amp;quot;&lt;/code&gt;, it enters into service mode. After its initialization, it changes service config:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-32.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;According to &lt;a class="link" href="https://docs.microsoft.com/en-us/windows/desktop/api/winsvc/nf-winsvc-changeserviceconfig2a" target="_blank" rel="noopener"
&gt;MSDN&lt;/a&gt;, &lt;em&gt;it changes the config so that failure actions occur if the service exits without entering a SERVICE_STOPPED state&lt;/em&gt;. After that, it executes its &lt;code&gt;ServiceFunction&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-33.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;This function setup the handles and starts exploiting the &lt;code&gt;MS17-010&lt;/code&gt; vulnerability into the reachable networks. Note that it exits after 24h. Here, I renamed this function &lt;code&gt;ExecuteEternalBlue&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-34.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;This call starts a number of events that let the infection to happen. First thing, Winsock subsystem is initialized and a CryptoContext is generated:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-35.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Next, the malware will load a DLL into the memory - the very same &lt;code&gt;launcher.dll&lt;/code&gt; we analyzed before - and then run it. Networks attacks happen inside two new threads. This flow can be easily observed if we decompile this function:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-36.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;The first thread, involving the function &lt;code&gt;sub_407720&lt;/code&gt;, will enumerates local network adapters and generates IP addresses compatible for those networks. For every IP, it tries to connect to port 445 and, if successful, launch the attack. Second thread, involving function &lt;code&gt;sub_407840&lt;/code&gt;, will run 128 times with 2 seconds (hex &lt;code&gt;7D0&lt;/code&gt;) delay between each run. It will generates random IP address and tries to connect on port 445, if connection is successful, malware will launch the EternalBlue attack. It&amp;rsquo;s a pretty big chunk of code, but one interesting block of code is this:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-37.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Basically the malware, with the random IP placed into the &lt;code&gt;Dest&lt;/code&gt; string converted into the proper format, calls &lt;code&gt;sub_407480&lt;/code&gt; aka &lt;code&gt;CreateSocketAndConnect&lt;/code&gt; to try a connection to the 445 port, if the connection is successful, real attack is launched within the function &lt;code&gt;sub_407540&lt;/code&gt; aka &lt;code&gt;SMBAttack&lt;/code&gt;.&lt;/p&gt;
&lt;h4 id="conclusion-after-the-second-layer"&gt;Conclusion after the second layer
&lt;/h4&gt;&lt;p&gt;So, until now, we got a DLL - &lt;code&gt;launcher.dll&lt;/code&gt; - that loads and runs a binary stored inside its resource section,&lt;code&gt;mssecsvc.exe&lt;/code&gt;. The very first time, a new service is created to achieve persistence and after that it scans the networks (local and random remote) launching the EternalBlue exploits against 445 ports. In its stand-alone version, it dumps another binary from its resource section and runs it. What&amp;rsquo;s the purpose of this third binary? Let&amp;rsquo;s give a look.&lt;/p&gt;
&lt;h3 id="analysis-of-the-third-layer-taskscheexe"&gt;Analysis of the third layer: tasksche.exe
&lt;/h3&gt;&lt;p&gt;Remember that this executable come from the resource section of previous file, &lt;code&gt;mssecsvc.exe&lt;/code&gt;. When it runs as service, locates its resource section and writes it to the disk creating &lt;code&gt;tasksche.exe&lt;/code&gt;. When it starts, it first generates a random string based on computer name, then checks if there are some command line arguments, in particular, if there&amp;rsquo;s &lt;code&gt;/i&lt;/code&gt; as argument. We have now two branches to analyze:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If there&amp;rsquo;s &lt;code&gt;/i&lt;/code&gt; argument: it creates specific directories and copies the file over it, like &lt;code&gt;C:\ProgramData\somerandomstring\tasksche.exe&lt;/code&gt; and runs it from there.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-39.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If there&amp;rsquo;s no &lt;code&gt;/i&lt;/code&gt; argument: it locates its resource section, named &lt;strong&gt;XIA&lt;/strong&gt;, storing and extracting it onto disk. What&amp;rsquo;s interesting to note here that this resource is a compressed &lt;em&gt;password protected&lt;/em&gt; archive. Luckily for us, password is hardcoded in clear text.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-40.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s give a look inside the archive knowing the password: &lt;strong&gt;WNcry@2ol7&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-41.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;We can recognize the magic numbers for a ZIP file that we can dump directly and extract.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-43.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;b.wnry&lt;/code&gt; is the bitmap image of the ransomware. Basically what you see as wallpaper when the computer is infected.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-44.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;c.wnry&lt;/code&gt; is the configuration file in clear text, we can see some &lt;code&gt;onion&lt;/code&gt; servers and the archive containing the TOR browser.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-45.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;r.wnry&lt;/code&gt; contains some text ransom note.&lt;/p&gt;
&lt;p&gt;Inside the &lt;code&gt;msg&lt;/code&gt; folder there are some localized ransom note:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2019/05/image-46.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;h4 id="conclusion-after-the-third-layer"&gt;Conclusion after the third layer
&lt;/h4&gt;&lt;p&gt;This new executable seems pretty interesting because basically, it manages all the crypto actions involved within the ransomware. I won&amp;rsquo;t go into this analysis because it&amp;rsquo;s beyond my actual skills and also because, there are plenty of resources available on the internet, from amazing guys that are way better than me. For example, &lt;a class="link" href="https://www.fireeye.com/blog/threat-research/2017/05/wannacry-malware-profile.html" target="_blank" rel="noopener"
&gt;this&lt;/a&gt; technical analysis by FireEye was published only few days aftermath and is complete, deep and detailed. I used it a lot to better understand many pieces of obscure code.&lt;/p&gt;
&lt;h2 id="conclusion"&gt;Conclusion
&lt;/h2&gt;&lt;p&gt;I have learned a lot from this research: I learned how malware interacts with their resource section to hide, dump and create files; I learned how malware interacts with Windows service manager and how they actually load DLLs in memory, how they scans networks and how EternalBlue actually works. Also, having available such complete and detailed technical analysis, on this very specific malware, helped me to not loose the direction when I went too deep inside the assembly code. It was very fun and I hope this research will be helpful to someone at least as it was for me.&lt;/p&gt;</description></item><item><title>Pony stealer: a malware analysis - The sample analysis - Part three</title><link>https://blog.kartone.ninja/pony-stealer-a-malware-analysis-the-sample-analysis-part-three/</link><pubDate>Mon, 10 Sep 2018 07:30:00 +0000</pubDate><guid>https://blog.kartone.ninja/pony-stealer-a-malware-analysis-the-sample-analysis-part-three/</guid><description>&lt;p&gt;After the first two parts &lt;a class="link" href="https://blog.kartone.ninja/pony-stealer-malware-analysis/" &gt;here&lt;/a&gt; and &lt;a class="link" href="https://blog.kartone.ninja/pony-stealer-a-malware-analysis-analyzing-the-sample/" &gt;here&lt;/a&gt;, we can move forward giving the sample a run inside a disassembler to look what&amp;rsquo;s inside and, eventually, into a debugger to see it live.&lt;/p&gt;
&lt;p&gt;IDA has some difficulties to analyze the sample due to the facts it heavily uses anti-disassembly trick:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/08/image-6.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Note that the conditional jump to &lt;code&gt;41062E&lt;/code&gt; never gonna happens. We can patch those bytes &lt;code&gt;\xF8\x72\x01&lt;/code&gt; with &lt;code&gt;NOP&lt;/code&gt; instruction or leave them alone knowing the fact that IDA can be fooled during analysis. Also at &lt;code&gt;41062F&lt;/code&gt; the sample delays its execution, invoking &lt;code&gt;GetTickCount&lt;/code&gt; function and dividing the remainder of the &lt;code&gt;DIV&lt;/code&gt; instruction by a predefined constant. So until the &lt;code&gt;CMP&lt;/code&gt; instruction is satisfied it will run this bunch of code a pseudo-random number of times. It appears that this technique can trick some antivirus heuristic controls.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/08/image-7.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;After condition is verified, the flow reaches the &lt;code&gt;CALL&lt;/code&gt; instruction at &lt;code&gt;4105c3&lt;/code&gt;, we see another anti-disassembly technique, the misaligned &lt;code&gt;PUSH&lt;/code&gt; instruction.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/08/image-8.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Clearly the misaligned &lt;code&gt;PUSH&lt;/code&gt; at &lt;code&gt;4105c7&lt;/code&gt; is there to fool the disassembler and we need to fix it if we want to have a better look on that piece of code. By defining manually that byte at &lt;code&gt;4105d0&lt;/code&gt;, IDA can now better analyze the code:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/08/image-9.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Now it&amp;rsquo;s clear what this piece of code does: it pushes the address of the function at &lt;code&gt;4105a2&lt;/code&gt; onto the stack. This pointer will be the argument of &lt;code&gt;SetUnhandledExceptionFilter&lt;/code&gt; function that, in the end, will exit from the process in case of unhandled exception.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/08/image-13.png"
loading="lazy"
&gt;&lt;img src="https://blog.kartone.ninja/images/2018/08/image-10.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s focus on what happens at address &lt;code&gt;410508&lt;/code&gt;, because it&amp;rsquo;s where the fun starts:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/08/image-15.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;After some studies I tried to interpret that code and the results are shown below.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/08/image-14.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Basically malware is starting its activities: first it loads libraries with the &lt;code&gt;OleInitialize&lt;/code&gt; and &lt;code&gt;LoadLibraries&lt;/code&gt; calls, after it fires up a delayer routine that, in malware intentions, will fool the heuristic controls of Kaspersky Antivirus. After that it enable some required privileges with the fourth call:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/08/image-16.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;This routine will cycle through and enable all these privileges:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/08/image-17.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;And after that it tries to get if the process is running within &lt;code&gt;LocalSystem&lt;/code&gt; or not. In both cases it will impersonate or the &lt;code&gt;LocalSystemUser&lt;/code&gt; or the &lt;code&gt;LocalUser&lt;/code&gt; using the API call to &lt;code&gt;GetUserNameA&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In the next session we&amp;rsquo;ll go deeper into the analysis trying to better understand its codebase.&lt;/p&gt;</description></item><item><title>Pony stealer: a malware analysis - The sample dry run - Part two</title><link>https://blog.kartone.ninja/pony-stealer-a-malware-analysis-analyzing-the-sample/</link><pubDate>Fri, 03 Aug 2018 07:00:00 +0000</pubDate><guid>https://blog.kartone.ninja/pony-stealer-a-malware-analysis-analyzing-the-sample/</guid><description>&lt;p&gt;After we were able to unpack the sample like we did in the &lt;a class="link" href="https://blog.kartone.ninja/pony-stealer-malware-analysis/" &gt;previous&lt;/a&gt; post, it&amp;rsquo;s time to understand what the malware is intended to do. The very first thing that I normally do is to give the sample a dry run inside a dedicated virtual machine, just to observe its behavior and monitoring its API calls. These calls can be monitored with a little tool called ApiLogger - that can be found &lt;a class="link" href="https://www.aldeid.com/wiki/SysAnalyzer/ApiLogger" target="_blank" rel="noopener"
&gt;here&lt;/a&gt; and it&amp;rsquo;s automatically installed by the &lt;a class="link" href="https://github.com/fireeye/flare-vm" target="_blank" rel="noopener"
&gt;Flare-vm&lt;/a&gt; script.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The API logger works by injecting a DLL into the target process. Once loaded, the DLL will insert a series of detours style hooks into specific API calls. When these API are accessed by any code in the process, they will trigger a notification message which gets sent to the main interface.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It&amp;rsquo;s clear that malware tries to steal informations (probably credentials) of various software via calls to &lt;strong&gt;RegOpenKeyA&lt;/strong&gt; and &lt;strong&gt;RegOpenKeyExA:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/08/image.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;And at the end of the run, it tries to connect to the domain &lt;strong&gt;singatradeing.com&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/08/image-1.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;We can catch this connection with another great tool from FireEye, &lt;strong&gt;&lt;a class="link" href="https://github.com/fireeye/flare-fakenet-ng" target="_blank" rel="noopener"
&gt;FakeNet-NG&lt;/a&gt;&lt;/strong&gt; that will capture and fake responses to all the queries DNS and HTTP  requests, saving all activities into a pcap file that could be analyzed with Wireshark:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/08/image-2.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;We can see that the malware resolved the domain name &lt;strong&gt;singatradeing.com&lt;/strong&gt; with a query DNS (that is faked by FakeNet-NG):&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/08/image-3.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;And sent an http GET request to: &lt;strong&gt;&lt;a class="link" href="http://singatradeing.com/espnphp/coreserver/shit.exe" target="_blank" rel="noopener"
&gt;http://singatradeing.com/espnphp/coreserver/shit.exe&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/08/image-4.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Our fake response served an executable file that was run by the malware:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/08/image-5.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;After that, the malware deleted itself. For this reason, remember to make a copy of the sample executable.&lt;/p&gt;
&lt;p&gt;I wasn&amp;rsquo;t able to download the real executable (shit.exe) but I&amp;rsquo;m sure it will be easy to find it.&lt;/p&gt;
&lt;p&gt;More informations related to that domain can be found &lt;a class="link" href="https://www.virustotal.com/en/domain/SINGATRADEING.COM/information/" target="_blank" rel="noopener"
&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id="recap"&gt;Recap
&lt;/h3&gt;&lt;p&gt;The malware sample, when it runs, tries to steal credentials from the registry keys, tries to download another executable and run it, deleting itself at the end.&lt;/p&gt;</description></item><item><title>Pony stealer: a malware analysis - Unpacking the sample - Part one</title><link>https://blog.kartone.ninja/pony-stealer-malware-analysis/</link><pubDate>Mon, 23 Jul 2018 07:00:00 +0000</pubDate><guid>https://blog.kartone.ninja/pony-stealer-malware-analysis/</guid><description>&lt;h1 id=""&gt;
&lt;/h1&gt;&lt;p&gt;During my day by day job, I had the chance to came across a mail that was blocked by an antispam platform. Attached to this mail there was a sample recognized as a variant of &lt;a class="link" href="https://blog.malwarebytes.com/detections/spyware-pony/" target="_blank" rel="noopener"
&gt;Pony Stealer malware&lt;/a&gt;. Since I&amp;rsquo;ve been greatly interested into malware analysis in the last few months, I thought it would be fun, and also a useful exercise, to apply all the notions I&amp;rsquo;ve been reading so far and writing this post, maybe, would help me in fixing methodologies and concepts. I hope this will be a two parts blog post: during this first part I will focus on unpacking the malware, during the second one I&amp;rsquo;ll try to analyze its real behavior. Let&amp;rsquo;s the journey begin.&lt;/p&gt;
&lt;h2 id="noob-alert"&gt;Noob alert
&lt;/h2&gt;&lt;p&gt;First things first: I&amp;rsquo;m no expert at all. This is my first experience in reversing malware - and also in blogging something - and so expect a lot of shady things and confused assumptions. Learning something new is always a good idea and I hope that digging into malware analysis will allow me to glue together some skills that I&amp;rsquo;m trying to learn in the last couple of  years. Also, do not rely on the memory addresses in the screenshots. As this post was written during various sessions, memory addresses changed every time due to operating system memory protections (ASLR).&lt;/p&gt;
&lt;h2 id="lab-setup"&gt;Lab setup
&lt;/h2&gt;&lt;p&gt;You can find great tutorials online on how to setup a professional and secure lab to test all malicious sample you get. I&amp;rsquo;d like to point you out to these useful resources:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/" target="_blank" rel="noopener"
&gt;Microsoft Windows 7 and 10 images&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/fireeye/flare-vm" target="_blank" rel="noopener"
&gt;Flare-vm&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://oalabs.openanalysis.net/2018/07/16/oalabs_malware_analysis_virtual_machine/" target="_blank" rel="noopener"
&gt;Malware Analysis Virtual Machine&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="running-the-sample-into-online-sandbox"&gt;Running the sample into online sandbox
&lt;/h2&gt;&lt;p&gt;Since, right now, I don&amp;rsquo;t have a working setup of &lt;a class="link" href="https://cuckoosandbox.org" target="_blank" rel="noopener"
&gt;Cuckoo sandbox&lt;/a&gt; on my behalf, the very first thing I did was uploading the sample into a freely usable sandbox online with these results: &lt;a class="link" href="http://tinyurl.com/y9gspzmt" target="_blank" rel="noopener"
&gt;http://tinyurl.com/y9gspzmt&lt;/a&gt;. As you can see, it labels the sample as a variant of the &lt;em&gt;VBObfus.g&lt;/em&gt; family. I didn&amp;rsquo;t find  a lot of informations about this malware family, but dynamic analysis shows me very few indicators:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;No evidence of malware activity into screenshot.&lt;/li&gt;
&lt;li&gt;No network activity.&lt;/li&gt;
&lt;li&gt;Every string is almost obfuscated.&lt;/li&gt;
&lt;li&gt;No extracted files.&lt;/li&gt;
&lt;li&gt;No evidence of process injection.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Important to note, although no clear evidences, the sample is classified as malicious with threat level as 71/100. Pretty strange, uh?&lt;/p&gt;
&lt;p&gt;&lt;a class="link" href="https://www.hybrid-analysis.com" target="_blank" rel="noopener"
&gt;Hybrid Analysis&lt;/a&gt; has this great feature: if you click on the sample filename, in this case &lt;strong&gt;SKMBT_C36018060720040_pdf.exe&lt;/strong&gt;*,* it shows a bunch of useful informations such as API calls used by the executable, registry keys it gets and/or sets during its runtime, filesystem activity, handles opened to files, operating system modules and other kind of libraries it uses. With all these informations we should have a proper level of confidence of what happens during the sandbox run. Let&amp;rsquo;s dig into some of them.&lt;/p&gt;
&lt;p&gt;First thing I looked at, was the activity on the filesystem:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/07/image.jpeg"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;No files saved and the infamous msvbvm60.dll caught my attention, but we will deal with this later. Nothing too much interesting into registry section too:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/07/image-1.jpeg"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;There&amp;rsquo;s a possibility to filter the operations (Query, Open, Write and Delete) but I didn&amp;rsquo;t find anything related to write or delete operations.&lt;/p&gt;
&lt;p&gt;The most interesting section is the API calls section. To understand the malware behavior during its run inside the sandbox, it&amp;rsquo;s necessary to analyze what API this sample calls. Following &lt;a class="link" href="https://www.symantec.com/connect/articles/windows-anti-debug-reference" target="_blank" rel="noopener"
&gt;this&lt;/a&gt; and &lt;a class="link" href="https://bitbucket.org/NtQuery/scyllahide/downloads/ScyllaHide.pdf" target="_blank" rel="noopener"
&gt;this&lt;/a&gt; useful resources, I started checking API calls, trying to find any evidence of anti-debug or anti-vm techniques, mainly because there&amp;rsquo;s no evidence of process injection and nowadays process injection is a very, very common technique. After checking all API anti-debug calls found in documentation I was clearly missing something because I wasn&amp;rsquo;t able to find any of them. So it&amp;rsquo;s time to give it a run into my lab and observe its behavior.&lt;/p&gt;
&lt;h2 id="static-analysis"&gt;Static analysis
&lt;/h2&gt;&lt;p&gt;Before give it a run, let&amp;rsquo;s check with some basic tools how&amp;rsquo;s the file is built:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/07/image-2.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;So, really we&amp;rsquo;re dealing with a VisualBasic 5/6 executable file.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/07/image-3.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s dig into more details with the executable:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/07/image-4.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;With this great tool we can find some initial informations:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;File Version Info Size=1548 -&amp;gt; 060Ch&lt;br&gt;
Translations : 040904B0     Language : English (U.S.)  -  ( 0 4 0 9 )&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;CompanyName  =  NIrSOft&lt;br&gt;
FileDescription  =  ELEctrum&lt;br&gt;
FileVersion  =  6.02&lt;br&gt;
InternalName  =  Bulbotuber&lt;br&gt;
LegalCopyright  =  LAVasoft&lt;br&gt;
LegalTradeMarks  =  THE ERAser PROject&lt;br&gt;
OriginalFilename  =  Bulbotuber.exe&lt;br&gt;
ProductName  =  ASUs&lt;br&gt;
ProductVersion  =  6.02&lt;br&gt;
Comments  =  Pwa, INA.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Don&amp;rsquo;t know how useful these informations are but, anyway, it&amp;rsquo;s always better to have informations rather than nothing. Assumed that it&amp;rsquo;s a VB5/6 executable file and I don&amp;rsquo;t know how to deal with it inside IDAPro, my next action will be to run it inside my Analysis VM, with the intent to understand better its behavior.&lt;/p&gt;
&lt;h2 id="dynamic-analysis"&gt;Dynamic analysis
&lt;/h2&gt;&lt;p&gt;Interestingly it seems to me that, after some sort of unpacking in memory, there is clearly a process injection:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/07/pexplorer.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Apparently there must be in place some sort of anti-debug and/or anti-vm tricks. Easily enough in x32dbg there is a life-saving plugin, named ScyllaHide, that is capable of doing some black magic to hide the debugger from malware. We can avoid the process crashing during its run inside the debugger.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/07/image-8.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;We can observe that the sample creates another process with the same name - a copy of itself - and this is typically an indication of the &lt;strong&gt;&lt;a class="link" href="http://www.autosectools.com/Process-Hollowing.pdf" target="_blank" rel="noopener"
&gt;process hollowing&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I won&amp;rsquo;t dig into describing the process injection because there are some great guys that have created very complete and clear tutorials on how to approach this technique. I can suggest &lt;a class="link" href="https://oalabs.openanalysis.net" target="_blank" rel="noopener"
&gt;this&lt;/a&gt; site maintained by this great guy: &lt;a class="link" href="https://twitter.com/herrcore" target="_blank" rel="noopener"
&gt;Sergey&lt;/a&gt; and also his Youtube channel &lt;a class="link" href="https://www.youtube.com/channel/UC--DwaiMV-jtO-6EvmKOnqg" target="_blank" rel="noopener"
&gt;here&lt;/a&gt;. I strongly suggest to follow all of his videos and tutorials: they are a great way to learn malware analysis and unpacking.&lt;/p&gt;
&lt;h2 id="unpacking-the-malware"&gt;Unpacking the malware
&lt;/h2&gt;&lt;p&gt;To unpack the malware we&amp;rsquo;ll focus mainly on these three API calls:&lt;/p&gt;
&lt;p&gt;&lt;a class="link" href="https://docs.microsoft.com/it-it/windows/desktop/api/processthreadsapi/nf-processthreadsapi-createprocessa" target="_blank" rel="noopener"
&gt;kernel32.CreateProcessW&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a class="link" href="https://undocumented.ntinternals.net" target="_blank" rel="noopener"
&gt;ntdll.NtWriteVirtualMemory&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a class="link" href="https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-resumethread" target="_blank" rel="noopener"
&gt;ntdll.NtResumeThread&lt;/a&gt;&lt;/p&gt;
&lt;h3 id="new-process-creation"&gt;New process creation
&lt;/h3&gt;&lt;p&gt;First API call to breakpoint into debugger is kernel32.CreateProcessW, that &lt;em&gt;creates a new process and its primary thread (cit. Microsoft)&lt;/em&gt;. We&amp;rsquo;re interested in its syntax:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;BOOL CreateProcessA(
LPCSTR lpApplicationName,
LPSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCSTR lpCurrentDirectory,
LPSTARTUPINFOA lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation
);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And more interestingly, its structure on the stack when its called:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/07/image-9.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;In accordance with the calling convention the function parameters are pushed on the stack in reverse order.  At address &lt;strong&gt;0x0018F460&lt;/strong&gt; there&amp;rsquo;s the function fifth parameter &lt;em&gt;dwCreationFlags,&lt;/em&gt; with the value of &lt;strong&gt;0x00000004.&lt;/strong&gt; This value means &lt;strong&gt;CREATE_SUSPENDED;&lt;/strong&gt; we have reached the start of the hollowing process: a new copy of the process has been created in suspended mode.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/07/image-10.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;We can confirm its PID &lt;strong&gt;2660,&lt;/strong&gt; running the function CreateProcessW until it returns and checking in memory dump the value of the first parameter pushed on the stack at address &lt;strong&gt;0x0018F470&lt;/strong&gt; with the value of &lt;strong&gt;0x0643008C:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/07/image-11.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;The new process PID is at address &lt;strong&gt;0x06430094&lt;/strong&gt;: &lt;strong&gt;0x0A64&lt;/strong&gt; that translate into decimal in &lt;strong&gt;2660.&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id="running-the-newly-created-process"&gt;Running the newly created process
&lt;/h3&gt;&lt;p&gt;We won&amp;rsquo;t bother too much with &lt;strong&gt;ntResumeThread&lt;/strong&gt; API call. Just note that when you reach breakpoint on this call, you know that the malware is ready to run itself (the new copy of itself actually) and, for this reason, you need to be very caution. Just don&amp;rsquo;t let this call run because you&amp;rsquo;re executing the malware itself.&lt;/p&gt;
&lt;h3 id="unpacking-the-malware-1"&gt;Unpacking the malware
&lt;/h3&gt;&lt;p&gt;The interesting part: this API call let us to dump the hidden payload stored inside the malware. So, breakpoint on it and let the malware run until it reaches the breakpoint. As before:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;NtWriteVirtualMemory(
IN HANDLE ProcessHandle,
IN PVOID BaseAddress,
IN PVOID Buffer,
IN ULONG NumberOfBytesToWrite,
OUT PULONG NumberOfBytesWritten OPTIONAL );
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Basically we&amp;rsquo;re interested in two arguments, in particular: the &lt;em&gt;BaseAddress&lt;/em&gt; andthe &lt;em&gt;Buffer.&lt;/em&gt; These two parameters tell us where the buffer (the malware payload) will be written inside the newly created child&amp;rsquo;s memory. During its run, the malware makes a lot of calls to this function and I single stepped all of them: when breakpoint is reached, analyze the stack:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/07/image-12.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Focus on the third argument: &lt;strong&gt;0x064B6000&lt;/strong&gt; and follow it into the memory dump:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/07/image-13.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;It seems we found something interesting, uh? :-)&lt;/p&gt;
&lt;p&gt;We found that a PE file will be copied inside a memory address. Easy to dump it, right now: right click on to that address and follow it into memory map and after that dump that segment into a file.&lt;/p&gt;
&lt;p&gt;So we have dumped an entire segment into a bin file. We can open it with an hex editor, scroll down until we reach the start of PE file (MZ magic bytes) and clear all junk from MZ to the beginning of the dump. Save to a new exe file and we&amp;rsquo;re ready to open it with another great tool made by &lt;a class="link" href="https://twitter.com/hasherezade" target="_blank" rel="noopener"
&gt;hasherazade&lt;/a&gt;: &lt;strong&gt;PE Bear&lt;/strong&gt;. Luckily for us, IAT (Import Address Table) was not corrupted and we can see all the API the (real)malware calls when it runs.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/07/image-14.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;Basically we have unpacked the malware.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.kartone.ninja/images/2018/07/image-16.png"
loading="lazy"
&gt;&lt;/p&gt;
&lt;p&gt;I will try to update this post with the second part as soon as I&amp;rsquo;ll figure it out. :-)&lt;/p&gt;</description></item></channel></rss>