Dissecting Windows Malware Series – Understanding Cryptography and Data Encoding – Part 3

Heard of AES? No? Well, alright then. Perhaps about Base64 Encoding??? XOR Encoding? Not that either?

Congratulations, you are about to learn about 3 core ways malware use data encoding to their advantage!

In the previous article https://8ksec.io/dissecting-windows-malware-series-process-injections-part-2/, we introduced the mechanism of Process Injection that malware use to achieve Stealth and Evasion. We saw direct implementation of:

  • Process Injection

  • Process Hollowing

And the use of other interesting Stealth associated mechanism.

So, now you are asking, what’s next?

What's In It For Me❓

We’ll see how malware utilize complex encryption algorithms like AES and “seemingly simpler” encryption methods like Base64 to conceal their functionalities.

Attackers do this to make it harder for Malware Analysts to fully understand what the malware does and how it operates.

An interesting footnote – in many cases, attackers prefer using simpler encryption methods like Base64 Encoding or XOR Encoding because they are:

  1. Space-efficient – they can be used in space-constrained environments and even in various Shellcodes.

  2. Less noticeable to the eye – they do not require calling functions from the cryptographic API of a specific encryption, thus are more “quiet” in the eyes of the Analyst.

  3. Efficient – because these are relatively compact encryption methods that don’t require a large number of code lines and/or calling external API functions, they less affect the performance of the malware during execution.

Following this article, you’ll be able to spot AES encryption, XOR encoding and Base64 encoding mechanisms in the sample you are analyzing, much easier.

A Brief Overview of Encryption Methods

XOR Encoding

Arguably the most by-the-book method used, in one way or another, among malware.

In its simplest form, it uses a character (byte) that serves as a key, for example: 0x3C.

If we perform a XOR operation with the key on each character in our string, we will get a completely different representation of the existing string, meaningless in human eyes.

For example, the address: http[:]//badsite[.]com with the XOR operation, using the key – 0x2d on each character will translate to – VJJN…_ZMWJ[.]QS.

Overall, it doesn’t sound so terrible, right?

The issue becomes complicated when playing around with this method

For instance, given an ASCII string representing an IP address, a domain, or even names of functions themselves used by the malware – we could use the content of the string itself for encryption.

We could start the XOR operation at the beginning of the string, at the end or literally at any position we choose – On a randomly chosen character.

The output character we get from the XOR operation will serve us as a “key” to perform the XOR operation with the next character in the string and so on…

This method opens up a vast array of encoding possibilities for the attacker, using a simple XOR operation, starting from a specific character in the string, using nested loops from a certain position, and so on…

									<img alt="" height="184" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/XOR_encoding.png?fit=504%2C184&amp;ssl=1" width="504" />											Image Reference: Practical Malware Analysis By Michael Sikorski and Andrew Honig
									
						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
		<h3>Base64 Encoding</h3>		</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>Like the previous method, due to the variety of its implementations, it’s one of the encoding mechanisms implemented among malware authors.</p><p>This method essentially converts binary data representation into a set of 64 characters.</p><p>There are several schemes for the different types of Base64 Encoding out there, but most contain the characters: [0-9,a-z,A-Z] and two additional characters, usually + and =, used for padding the string length to fit the format.</p><p>The conversion is done as follows: <strong>every 3 bytes of binary data are converted to 4 bytes of Base64 data</strong>.</p><p>Just to tickle your ears, the string ATT is represented by the following 3 bytes in hexadecimal: <em>0x41</em> <em>0x54</em> <em>0x54</em> (each character in ASCII is 8 bits which is one byte which is two characters in hex).</p><p>If we convert it to binary, we get this representation:</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
									
									<img alt="" height="211" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/base64_decrypting.png?fit=728%2C211&amp;ssl=1" width="728" />											Image Reference: Practical Malware Analysis By Michael Sikorski and Andrew Honig
									
						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>We’ll treat each 6 bits as a single character and convert them to decimal representation, meaning 010000 will be converted to 16 in decimal, 010101 will be converted to 21, and so on…</p><p>Now, if we go to the character table that represents our Base64 format:</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
									
									<img alt="" height="50" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/base64_index_table.png?fit=676%2C50&amp;ssl=1" width="676" />											Image Reference: Practical Malware Analysis By Michael Sikorski and Andrew Honig
									
						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>Look for the indexes – 16, 21, 17, and 20, we will get the characters: QVRT accordingly.</p><p>Also here, the attacker has a wide range of possibilities for implementing Base64 Encoding.</p><p>He could, for instance, change the indexing table to have a different order, which will affect all the indexing and how the conversion is done.</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
									
									<img alt="" height="43" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/base_64_custom_table.png?fit=641%2C43&amp;ssl=1" width="641" />											Image Reference: Practical Malware Analysis By Michael Sikorski and Andrew Honig
									
						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
									
									<img alt="" height="229" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/correct_vs_not_correct_decoding.png?fit=788%2C229&amp;ssl=1" width="788" />											Image Reference: Practical Malware Analysis By Michael Sikorski and Andrew Honig
									
						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
		<h3>Use of Cryptography API Functions</h3>		</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>Most encryption algorithms under this category are based on the fact that:</p><ul><li><p>Performing brute-forcing in an attempt to decrypt the source, given the complexity of the key and the encryption, simply isn’t possible (it would take decades – yes, decades…).</p></li><li><p>The way the algorithm works is publicly accessible for inspection, but without the key, it’s impossible to decrypt the source.</p></li><li><p>As mentioned earlier, using encryption algorithms in this category is usually easier to identify during the analysis of malware. This is reflected in the use of informative constants, aka <em>‘Consts’</em>, importing libraries and functions, and others…</p></li></ul><p>Let’s briefly go over the symmetric encryption algorithm – AES:</p><ol start=""><li><p>It is a symmetric algorithm meaning the same key is used for both encryption and decryption – we’ll remember this when analyzing the malware, aiming to find the key for performing the decoding.</p></li><li><p>The algorithm essentially takes 128 bits of content and turns it into 128 bits of cipher text (encrypted and unreadable content).</p></li><li><p>The key used for encryption can be in sizes of 128 bits, 192 bits, or 256 bits. Each size indicates the number of rounds that will be performed during the algorithm on the content we wish to encrypt (10 rounds, 12 rounds, or 14 rounds accordingly).</p></li><li><p>The algorithm essentially takes 16 bytes (128 bits), arranges them in a 4×4 grid, and performs operations of:</p><p><strong>XOR -&gt; Byte Substitution -&gt; Shifting Rows -&gt; Mixing Columns -&gt; Adding Round Key</strong></p><p>Where each such cycle (except for the XOR stage, which happens at the beginning and relies on a hidden key) is called a round.</p></li></ol>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
												<img alt="" height="457" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/S_box_transformation.png?fit=503%2C457&amp;ssl=1" width="503" />													</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>Side Note:</p><p>In addition, a mechanism called <em>‘Key scheduling’</em> is also used in order not to use the same key in every round, but we won’t get into that right now.</p><p>Generally speaking, most modern encryption algorithms are based on <strong>Substitution Box</strong> and <strong>Permutation Box</strong>, where the previously mentioned Substitution Bytes stage is actually a Substitution Box.</p><p>And a Substitution Box is actually a lookup table where each byte is mapped to another byte based on a specific function.</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
		<h3>For the Math Lovers Among Us<img alt="➕" src="https://s.w.org/images/core/emoji/15.0.3/72x72/2795.png" style="height: 1em;" /></h3>		</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>As a footnote for the math enthusiasts, an S-Box is a non-linear transformation of the form:</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
												<img alt="" height="57" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/non_linear_transformation_pic_1.png?fit=141%2C57&amp;ssl=1" width="141" />													</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>Meaning, each function <em>fi</em> in the field, receives n bits as input and returns <em>fi</em> for each <em>xi</em> in the following manner (meaning m bits).</p><p>Thus, a function in the field would be of the form:</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
												<img alt="" height="94" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/non_linear_transformation_pic_2.png?fit=467%2C94&amp;ssl=1" width="467" />													</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>Where for each <em>f1,f2,…,fm</em> such: <em>fi</em> is actually a Boolean function of the form:</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
												<img alt="" height="48" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/non_linear_transformation_pic_3.png?fit=126%2C48&amp;ssl=1" width="126" />													</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>Meaning, each such Boolean function fi returns us 0 or 1. Therefore, the final output we get will be m bits, or more precisely an m-ordered form of the sort:</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
												<img alt="" height="48" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/non_linear_transformation_pic_4.png?fit=267%2C48&amp;ssl=1" width="267" />													</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>Thus, in essence, the output of the Substitution Box stage for each byte is received.</p><p>In our case, since we talked about AES, the n parameter represents 128 bits. The whole mapping process we presented is actually one round (as mentioned earlier) and according to the algorithm, a number of such rounds will be performed.</p><p>Here comes the importance of the secret key mentioned earlier.</p><p>The key is divided into several “chunks” where each chunk essentially undergoes XOR with the output we received after each round we did – if we hadn’t done that, we could simply decrypt the algorithm by performing the operations we did in reverse order (given access to the sequence of operations themselves and without needing the key) – this stage is called key scheduling.</p><p><em>We won’t get into the Permutation Box stage in this article</em></p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
		<h3>The Data Decoding Checklist<img alt="" src="https://s.w.org/images/core/emoji/15.0.3/72x72/1f4c8.png" style="height: 1em;" /></h3>		</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>Alright, so after “bashing you with some theory” (and a bit of math), we’ll dive into a partial analysis of a malware sample containing some of the principles presented here.</p><p>We’ll see that it is actually based on a custom encoding approach – it uses XOR encryption, Base64, and also AES encryption in the unique way it chose to implement.</p><p>In analyzing encrypted malware, we need to remember that we must identify the following three key principles:</p><ol start=""><li><p>Identify the type of encoding the malware uses (if at all), among them: AES, Base64, XOR Encoding, and others…</p></li><li><p>In case there is use of encoding, what are the functions aimed at performing the encoding.</p></li><li><p>How can we use these functions, the type of encoding and/or the memory addresses where the encoding occurs, to perform the decoding (deciphering) step.</p></li></ol>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
		<h3>Let's Start Reversing<img alt="" src="https://s.w.org/images/core/emoji/15.0.3/72x72/1f60e.png" style="height: 1em;" /></h3>		</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>We started with the Basic Static Analysis stage and saw the existence of the following strings in the data segment of the malware:</p><p><em>CDEFGHIJKLMNOPQRSTUVWXYZABcdefghijklmnopqrstuvwxyzab0123456789+/</em></p><p>This is a Base64 index table (note the change in the order of characters in the index table as mentioned earlier) used for some sort of encoding, the malware will perform.</p><p>In addition, to a number of interesting functions: <em>WriteFile</em>, <em>CreateThread</em>, we can see several error message strings: <em>Data not multiple of block size</em>, <em>Incorrect key length</em>, and <em>Empty key</em>.</p><p>We can guess that it refers to some kind of encryption where the size of the block and the length of the key are important.</p><p>We also see a few strings are displayed as gibberish, which indeed confirms that the malware uses some form of encryption.</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
												<img alt="" height="48" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/cmd_data_encoding_1.png?fit=645%2C48&amp;ssl=1" width="645" />													</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
												<img alt="" height="220" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/cmd_data_encoding_2.png?fit=663%2C220&amp;ssl=1" width="663" />													</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>During the Basic Dynamic Analysis phase, we observed the malware attempting to communicate outward through port: 8910 to a domain we identified in the previous stage.</p><p>Subsequently, we see a <em>cmd.exe</em> window opening, simply waiting – we can infer that the malware might be trying to execute a reverse shell, where commands are actually sent to the malware on the previously seen port, for local execution at the endpoint.</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
		<h4>A Little Side Note</h4>		</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>We can use Netcat to set up a listener on port 8910 and use ApateDNS or INetSim to make the malware redirect all the requests it sends to the suspicious domain, to arrive at 127.0.0.1.</p><div></div><p>We can see that the malware tries to run commands like <em>dir</em> and we can see a lot of encrypted data received through the created connection.</p><p>Using PEiD, specifically with the KANAL Plugin, we can see memory addresses identified with constants and high entropy levels (in a word, entropy indicates memory addresses where the data is more random than usual, pointing to some form of implemented encryption).</p><p>Additionally, we can see the scan detected the use of an S-box structure implemented in various encryption algorithms (displayed as [S] and [S-inv] in the screenshot):</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
												<img alt="" height="263" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/Kanal_analyzer_output.png?fit=315%2C263&amp;ssl=1" width="315" />													</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>RIJNDAEL is another name for the encryption algorithm – AES – confirming our initial hypothesis about the use of some cryptographic algorithm.</p><p>Continue to analyze the addresses in memory characterized by high entropy, we notice that at address <em>0x004120A4</em>, we find the Base64 index table we saw before.</p><p>We can conclude that the malware uses Base64 encoding with a custom index table and AES encryption.</p><p>Running a search for all the <em>XOR</em> commands existing in our assembly code, we see that there are quite a few:</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
												<img alt="" height="770" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/xor_search_results.png?fit=594%2C770&amp;ssl=1" width="594" />													</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>We’ll ignore all commands that belong to functions related to Windows API libraries and commands where register cleanup is performed, namely commands in the form: <em>xor eax,eax</em>.</p><p>We’ll perform cross-reference to the addresses remaining after the filtering with the addresses we found before in the entropy scan and discover that there are 6 main functions containing these commands.</p><p>Here is an example code taken from one of these functions:</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
												<img alt="" height="783" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/XOR_encoding_routine.png?fit=481%2C783&amp;ssl=1" width="481" />													</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>If we start to delve into the details of each assembly instruction in each of these functions, we will waste a lot of time – which is exactly what the malware writer would want us to do.</p><p>We’ll need to understand which function calls, whether they call one another (spoiler: they do) and generally understand the big picture of what is happening here.</p><p>Based on the fact that we expect to see some reference to AES encryption, we will try to understand how the XOR functions we found are related to this.</p><p>After some cross-referencing to the XOR functions, we discover that the main function is the one that calls the function whose name we changed to be <em>xor_func1</em>:</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
												<img alt="" height="784" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/network_connection_establish.png?fit=800%2C784&amp;ssl=1" width="800" />													</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>Since we anticipate references to AES encryption, the observant will notice that the offset of the string, which the function receives as a parameter, not only resembles a key but also comprises 16 characters.</p><p>These 16 characters, corresponding to 16 bytes (with each ASCII character representing one byte, totaling 128 bits), immediately suggest that we are dealing with AES-128 encryption, which is based on a 16-byte-long key.</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
		<h3>Return to the Bigger Picture<img alt="" src="https://s.w.org/images/core/emoji/15.0.3/72x72/1f304.png" style="height: 1em;" /></h3>		</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>We saw earlier, a call to the function <em>xor_func1</em> which receives a number of parameters, among them a string that looks like an AES key.</p><p>After that, we saw API functions calls to <em>WSAStartup</em> and <em>WSASocketA</em> whose purpose is to perform initialization for a network connection.</p><p>Then, the following code segment arrives:</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
												<img alt="" height="718" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/port_n_dns_resolving.png?fit=753%2C718&amp;ssl=1" width="753" />													</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>Several additional calls are made whose purposes are:</p><p>&nbsp; &nbsp;1. Performing DNS resolving for the domain passed as a parameter<br />&nbsp; &nbsp;2. Converting the port (displayed as 8910) from little endian to big endian for the network convention during communication<br />&nbsp; &nbsp;3. Performing connect to start the communication<br />&nbsp; &nbsp;4. And calling another function *sub_4015B7*.</p><p>Starting to analyze the function *sub_4015B7*, we discover several interesting things:</p><p>&nbsp; &nbsp;5. The malware creates a process of *cmd.exe*:</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
												<img alt="" height="360" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/process_create.png?fit=622%2C360&amp;ssl=1" width="622" />													</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<ol start="6"><li><p>Two threads are created (we’ll discover later that each is intended for a different purpose):</p></li></ol>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
												<img alt="" height="665" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/thread_creation.png?fit=794%2C665&amp;ssl=1" width="794" />													</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<ol start="7"><li><p>Later on, we see a creation and linking of pipes to standard output, input, error as is commonly done when creating communication between standard input to output:</p></li></ol>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
												<img alt="" height="437" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/pipe_creation.png?fit=682%2C437&amp;ssl=1" width="682" />													</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>Given the fact that we found a Base64 index table, we can find the location in memory where it is saved (in the data segment of course) and perform cross-reference to see where it is used.</p><p>We will discover that it is referenced in the function <em>sub_40103F</em> which is called from <em>sub_401082</em> and this in turn contains a nested loop.</p><p>Finally, we discover that the function <em>sub_401082</em> is passed as a parameter to one of the threads we saw earlier (at the start address that the thread will run).</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
		<h3>Importance of Understanding the Chronological Order<img alt="" src="https://s.w.org/images/core/emoji/15.0.3/72x72/1f50e.png" style="height: 1em;" /></h3>		</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>If we delve a bit into the code and the important addresses we saw, we will discover that:</p><ol start=""><li><p>There is a function that performs the decoding stage based on the Base64 index table called right after the call to CreateProcess that creates cmd.exe. Then, a socket connection is created with the calls (<em>WSAStartup</em>, <em>WSASocketA</em>, and additional functions from the library Ws2_32.dll) to create communication to the malicious domain.</p></li><li><p>A pipe is created that will pass the input/output from the created socket, to cmd.exe.</p></li><li><p>Analysis of the calls related to AES encryption reveals that the function responsible for performing the AES encryption is passed as a parameter to the second thread we saw, and this happens after the discussed process – cmd.exe has already started running.</p></li><li><p>Finally, we witness a call to the API function <em>WriteFile</em> in order to write to the cmd console – which in turn will perform a redirect of the output back through the created socket, out to the network.</p></li></ol><p>When dealing with data encoding mechanisms, understanding the chronological order of the various encoding and decoding components is crucial. That’s the key to successfully decoding the encoded data!</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
									
									<img alt="" height="299" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/chronological_order.png?fit=779%2C299&amp;ssl=1" width="779" />											Image Reference: Practical Malware Analysis By Michael Sikorski and Andrew Honig
									
						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
		<h3>What Have We Seen<img alt="" src="https://s.w.org/images/core/emoji/15.0.3/72x72/1f60e.png" style="height: 1em;" /></h3>		</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<ul><li><p>The malware we saw created a reverse shell, where the data it sends back to the C2 server undergoes encryption.</p></li><li><p>In terms of the chronological order and the big picture, all the commands sent from the C2 to the reverse shell are sent encoded using Base64 and undergo decoding using the custom index table we found.</p></li><li><p>Diving deeper, performing analysis and cross-referencing, we found out that all the responses sent back to the C2 server undergo AES encryption using the key:</p><p><em>ijklmnopqrstuvwx</em></p><p>right before being sent back.</p></li><li><p>We reviewed the way AES encryption works: how does it work, what is it based on, a bit of mathematical explanation, and how it is displayed in the assembly code of the malware sample.</p></li><li><p><strong>Most importantly</strong> , we saw what the workflow we want to implement when investigating malware that use encryptions of various kinds:</p></li></ul><ol start=""><li><p>We want to identify what kind of encryption/encoding the malware is using.</p></li><li><p>Discover which functions perform the encryption/encoding and which additional memory addresses are related to the process (for example, strings in the data segment).</p></li><li><p>How to perform pivoting around an address/function or any other piece of information we found in order to advance with understanding the full functionality of the malware.</p></li></ol>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
		<h3>What Haven't We Seen (And Probably Will)<img alt="" src="https://s.w.org/images/core/emoji/15.0.3/72x72/1f440.png" style="height: 1em;" /></h3>		</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>Last but not least, we did not manage to present here the *Decoding stage itself*.<br />The decoding can be performed in several ways, the main ones are:</p><p>1. Manual Decoding – in this approach, we identify the functions that perform the decoding, run the malware in a debugger, and see the strings decoded in real-time. If we fail to identify these functions, or if the malware does not perform decoding at any stage during the run – we’re out of luck…</p><p>2. Decoding Using Coding – we can write a script that will perform the decoding for us.</p><p>With that being said, we’ll need to give it all the necessary information for performing the decoding.</p><p>For example, if it’s Base64, we will need to give it the index table, if it’s AES or DES, we will need to give it the password (which we will have to extract from the assembly code) and the mode of encryption (for instance, AES contains several modes: CBC or EBC, etc…) – in other words, there’s quite a bit of data that we first need to extract from the assembly code, so that our code can run and perform what we command it to.</p><p>3. Instrumentation – Once we identify the functions responsible for performing the decoding and encoding and the parameters they receive, we can change the flow of the program’s execution so that it will perform the decoding for us – with the data we provide it.</p><p>We achieve this using plugins we write for OllyDbg, ImmDbg,x32/64Dbg that will perform modifications to the values of the registers and to memory addresses in order to perform the decoding for us.</p><p>This requires a fairly deep understanding of what happens behind the scenes, to allocate buffers from which we will write and read, changing the value of the EIP register (and other registers accordingly), and more…</p><p>It’s not the easiest to implement but once you understand it deeply, it can be applied to a significant number of malware families out there – definitely a skill worth acquiring for the long run.</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
												<img alt="" height="570" src="https://i0.wp.com/8ksec.io/wp-content/uploads/2024/03/decoding_with_ollDbg.png?fit=800%2C570&amp;ssl=1" width="800" />													</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
		<h3>A Side Note<img alt="" src="https://s.w.org/images/core/emoji/15.0.3/72x72/1f4c3.png" style="height: 1em;" /></h3>		</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>We can implement this partially in the debugger itself.</p><p>In cases the encryption used by the malware is reversible (the same algorithm for encryption and decoding) we can simply give the function that performs the encoding, the encoded data and receive it decoded at the end of its run…</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
		<h3>What's Next<img alt="" src="https://s.w.org/images/core/emoji/15.0.3/72x72/1f60f.png" style="height: 1em;" /></h3>		</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<p>Since we already discussed the major objectives of malware, the vastly used Process Injection techniques and even got a glimpse of some User-Space Rootkit, the next step is exploring one of the most important aspects of malware – Network Traffic!</p><p>Malware have different purposes, but all of them are made to infiltrate data in one way or another.</p><p>Thus, we need to be able to research and analyze network traffic.</p><p>BUT, before we dive to the world of Network Traffic Analysis, we’ll do a short detour.</p><p>As you sure have noticed, reverse engineering malware requires some understanding of Low-Level knowledge and OS Internals.</p><p>Since this aspect is so important, we’ll review the difference between the main CPU architecture types – RISC and CISC. How does they differ from one another, and how does it affect the way malware are written on each architecture.</p><p>Once again, keep learning and Have Fun Reversing!</p>						</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
		<h3>References</h3>		</div>
			</div>
				</div>
			</div>
	<div>
				<div>
			<div>
			<div>
						<ol start=""><li><p>The following resources are taken from an amazing book called:</p><p><em>Practical Malware Analysis By Michael Sikorski and Andrew Honig</em></p><ul><li><p>The Malware Sample</p></li><li><p>The graph chart with the XOR and AES functions call image</p></li><li><p>The Base64 Index Table example image</p></li><li><p>The XOR initial explanatory image</p></li><li><p>The unsuccessful vs successful base64 decoding’ image</p></li></ul></li><li><p>All other resources were generated through the analysis process</p></li></ol>						</div>
			</div>
				</div>
			</div>
			</div><p>The post <a href="https://8ksec.io/dissecting-windows-malware-series-understanding-cryptography-and-data-encoding-part-3/" rel="noreferrer" target="_blank">Dissecting Windows Malware Series – Understanding Cryptography and Data Encoding – Part 3</a> first appeared on <a href="https://8ksec.io" rel="noreferrer" target="_blank">8kSec</a>.</p>

Article Link: Dissecting Windows Malware Series – Understanding Cryptography and Data Encoding – Part 3 - 8kSec