<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Vm on pugachev.io</title><link>https://pugachev.io/tags/vm/</link><description>Recent content in Vm on pugachev.io</description><generator>Hugo</generator><language>en-US</language><copyright>© Maxim Pugachev</copyright><lastBuildDate>Fri, 04 Dec 2026 13:37:37 +0100</lastBuildDate><atom:link href="https://pugachev.io/tags/vm/index.xml" rel="self" type="application/rss+xml"/><item><title>"Hexecution" by Hack The Box</title><link>https://pugachev.io/2025/11/03/hexecution-by-hack-the-box/</link><pubDate>Mon, 03 Nov 2025 15:54:17 +0100</pubDate><guid>https://pugachev.io/2025/11/03/hexecution-by-hack-the-box/</guid><description>&lt;p&gt;Another day, another toy virtual machine challenge from HTB. It doesn't seem complicated, so I'll keep my story short.&lt;/p&gt;&#10;&lt;p&gt;This challenge even provides the source code as a text file, so there's no need to search for it in the binary. It looks strange at first glance, but it's the same assembly as always: &lt;code&gt;mov&lt;/code&gt;, &lt;code&gt;xor&lt;/code&gt;, &lt;code&gt;cmp&lt;/code&gt;, and so on, just &amp;quot;encrypted&amp;quot; with funny words like &lt;code&gt;BOIL&lt;/code&gt; or &lt;code&gt;QUICKMAFFS&lt;/code&gt;. It reminded me of my school days, when I found a cracked version of MS Visual Basic translated into Russian, including the programming language itself. So I had to literally write Visual Basic code using Russian words. Strange experience, but it was fun.&lt;/p&gt;&#10;&lt;p&gt;The virtual machine itself has 12 instructions (one of which is not used in the provided source code), 6 registers, and an unusual &amp;quot;stack&amp;quot; implementation, which I'll leave for you to figure out. To get you started with the internal structure, I'll say that the stack grows up, and the SP register is &amp;quot;hardcoded&amp;quot; as the last element of the array used for emulation. This is funny in itself because you could try to write an exploit that breaks either the virtual stack or the underlying array, since neither is bounds-checked.&lt;/p&gt;&#10;&lt;p&gt;As usual, I wrote a small disassembler, but now I call it a &amp;quot;translator.&amp;quot; Because I didn't disassemble anything, right? I'm translating a strange language into my strange assembly code. And, as usual, I recommend trying to solve the challenge first, and only using my code when you get stuck.&lt;/p&gt;&#10;&lt;p&gt;The resulting translated program doesn't look very complicated: it contains a hardcoded flag encrypted using 2 simple permutations that can be reversed in two minutes.&lt;/p&gt;&#10;&lt;p&gt;Have fun!&lt;/p&gt;&#10;&lt;p&gt;The challenge can be found &lt;a href="https://app.hackthebox.com/challenges/Hexecution" target="_blank" rel="noopener"&gt;here&lt;/a&gt; and my awesome translator with the source code &lt;a href="https://github.com/itwaseasy/crackmes-solutions/tree/master/hackTheBox/hexecution" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item><item><title>"vvm" by Hack The Box</title><link>https://pugachev.io/2025/04/11/vvm-by-hack-the-box/</link><pubDate>Fri, 11 Apr 2025 17:15:44 +0100</pubDate><guid>https://pugachev.io/2025/04/11/vvm-by-hack-the-box/</guid><description>&lt;p&gt;This is not the first HTB challenge I've solved that uses a virtual machine, and I have to say that &lt;a href="https://pugachev.io/2024/04/14/vmcrack-by-hack-the-box/"&gt;vmcrack&lt;/a&gt; was much more interesting, challenging, and consistent in implementation than this one. In any case, it was quite fun to solve because of the various stack and bitwise exercises you had to deal with.&lt;/p&gt;&#10;&lt;p&gt;But let's start from the beginning. It's a custom virtual machine implementation with 29 different opcodes, and everything is stack-based. There are no registers as such, and the stack is used to store the results of calculations.&lt;/p&gt;&#10;&lt;p&gt;Each opcode is 4 bytes long and can have 0 to 2 operands, which are also 4 bytes long. So the encoding is pretty simple: no need to deal with bits, different sizes, conditions and so on. Just read the first 4 bytes and then decide, based on that number, how many operands to read. But the flip side of this simplicity is that each opcode can do anything from a simple jump or a push/pop to crazy things like allocating a block of memory and reading a line from stdin, or (my favorite) deleting a newline from a buffer.&lt;/p&gt;&#10;&lt;p&gt;To make things easier, I wrote a small disassembler that decodes the VM code into what I call &amp;quot;pseudo-assembly&amp;quot;, so don't try to compile it back. I used the &lt;code&gt;r1&lt;/code&gt;, &lt;code&gt;r2&lt;/code&gt;, and &lt;code&gt;sp&lt;/code&gt; registers to make it more readable, but again, the original code doesn't have registers.&lt;/p&gt;&#10;&lt;p&gt;As for the inconsistencies, please note that the &lt;code&gt;jmp&lt;/code&gt; instruction works a little weirdly: it counts the bytes to the destination from its operand, not from the next instruction. So if you decode it like this:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-nasm" data-lang="nasm"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;0&lt;/span&gt;&lt;span class="nf"&gt;b&lt;/span&gt; &lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="mi"&gt;00&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;jmp&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mh"&gt;0x10&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;the 4 bytes &lt;code&gt;0x00000010&lt;/code&gt; will be included in the offset.&lt;/p&gt;&#10;&lt;p&gt;Another funny thing is that, according to the HTB changelog, the author updated the challenge. I had the previous version, and it is a bit more complex at the last step of checking the flag. Well, it happens.&lt;/p&gt;&#10;&lt;p&gt;The opcode logic is encrypted in the binary, but the xor (of course) code is the same everywhere, so you can decrypt it with the following IDC script:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current_byte&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x2a4d21210b2a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;function_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_screen_ea&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nf"&gt;get_screen_ea&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;function_size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xFF&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;patch_byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You can read the disassembler output I left on GitHub to get the full picture (I even commented it a bit), but basically, it looks like this:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Read the flag from stdin.&lt;/li&gt;&#10;&lt;li&gt;Check the size of the input data.&lt;/li&gt;&#10;&lt;li&gt;Compute 8 integers (4 bytes each) using not-so-complicated bitwise logic. Each of these integers contains 4 characters from the original input, encoded in some way.&lt;/li&gt;&#10;&lt;li&gt;Validate that the resulting integers match the numbers hardcoded into the virtual machine code (or calculated dynamically in a previous version).&lt;/li&gt;&#10;&lt;li&gt;Print the validation result to stdout.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;The fun begins with checking the size of the input data. The virtual machine tries to validate it using the following complex mathematical equation:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;(((((((x * 3) - 6) * 2 + 4) / 2) * 8) + 24) / 10) = 76&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;but in fact, you can skip it simply because before that, it allocates a buffer exactly the same size as the input should be.&lt;/p&gt;&#10;&lt;p&gt;It then encodes groups of 4 characters using the following logic:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Take 4 characters from the input string. For example, the first integer will contain characters from positions &lt;code&gt;0x18&lt;/code&gt;, &lt;code&gt;0x0E&lt;/code&gt;, &lt;code&gt;0x1B&lt;/code&gt; and &lt;code&gt;0x0F&lt;/code&gt;.&lt;/li&gt;&#10;&lt;li&gt;Group them into a single integer using standard bitwise logic: &lt;code&gt;(c0 | c1 &amp;lt;&amp;lt; 0x08) | (c2 &amp;lt;&amp;lt; 0x10) | (c3 &amp;lt;&amp;lt; 0x18)&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;For each group, rotate the result left by &lt;code&gt;Z&lt;/code&gt; bits: &lt;code&gt;(previous_step_value &amp;lt;&amp;lt; Z) | (previous_step_value &amp;gt;&amp;gt; (0x20 - Z))&lt;/code&gt;, where &lt;code&gt;Z&lt;/code&gt; is chosen for each group separately.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;So, for example, for the group &lt;code&gt;ABCD&lt;/code&gt; the following will be done:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;step1: group1 = &amp;#39;ABCD&amp;#39;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;step2: round1 = (&amp;#39;A&amp;#39; | &amp;#39;B&amp;#39; &amp;lt;&amp;lt; 0x08) | (&amp;#39;C&amp;#39; &amp;lt;&amp;lt; 0x10) | (&amp;#39;D&amp;#39; &amp;lt;&amp;lt; 0x18)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;step3: Z = 0x05&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;step4: round2 = (round1 &amp;lt;&amp;lt; 0x05) | (round1 &amp;gt;&amp;gt; 0x1B)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and then it compares &lt;code&gt;round2&lt;/code&gt; with the hardcoded value to check if the input is correct.&lt;/p&gt;&#10;&lt;p&gt;To reverse the algorithm, as usual, you need to go in the opposite direction:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Get the hardcoded value (which is our &lt;code&gt;round2&lt;/code&gt;) and &lt;code&gt;Z&lt;/code&gt; from the VM code.&lt;/li&gt;&#10;&lt;li&gt;Restore &lt;code&gt;round1&lt;/code&gt; by rotating the value right by &lt;code&gt;Z&lt;/code&gt; bits.&lt;/li&gt;&#10;&lt;li&gt;Put the 4 characters from &lt;code&gt;round1&lt;/code&gt; back into their original positions.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;Well, and that's all.&lt;/p&gt;&#10;&lt;p&gt;The challenge can be found &lt;a href="https://app.hackthebox.com/challenges/vvm" target="_blank" rel="noopener"&gt;here&lt;/a&gt; and my awesome IDA-killer and the VM code can be found &lt;a href="https://github.com/itwaseasy/crackmes-solutions/tree/master/hackTheBox/vvm" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item><item><title>"vmcrack" by Hack The Box</title><link>https://pugachev.io/2024/04/14/vmcrack-by-hack-the-box/</link><pubDate>Sun, 14 Apr 2024 15:18:36 +0100</pubDate><guid>https://pugachev.io/2024/04/14/vmcrack-by-hack-the-box/</guid><description>&lt;p&gt;It seems that custom virtual machines are the trend in CTF challenges now, and this task has one too. The only thing is that the crackmes I've seen so far did this, let's say, at an amateur level. But this example is really cool: it's a full implementation that supports and interprets a subset of the x86 architecture using 65 opcodes, 32/16/8-bit registers, various addressing modes including full Base/Index/Scale/Displacement mode, and more. It can even use the &lt;code&gt;fs&lt;/code&gt; segment register!&lt;/p&gt;&#10;&lt;p&gt;To make life a bit more interesting, the challenge has a few anti-debugging techniques, so be prepared if you're going to debug it. I didn't, because I usually prefer to do things statically, but it's interesting to see all these ideas implemented in one binary anyway. I counted 9 different ways to prevent you from debugging it, but there are probably more:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;a href="https://unprotect.it/technique/tls-callback/" target="_blank" rel="noopener"&gt;TLS Callbacks&lt;/a&gt;. There are 3 of them, but they have the same idea: the code finds the address of the desired function using the &amp;quot;PEB technique&amp;quot; and calls it. The function names are hidden using xor with a hardcoded constant that is different for each callback.&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;a href="https://unprotect.it/technique/checkremotedebuggerpresent/" target="_blank" rel="noopener"&gt;CheckRemoteDebuggerPresent&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://unprotect.it/technique/ntqueryinformationprocess/" target="_blank" rel="noopener"&gt;NtQueryInformationProcess&lt;/a&gt;, but with the &lt;code&gt;ProcessDebugObjectHandle&lt;/code&gt; class&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://unprotect.it/technique/ntsetinformationthread/" target="_blank" rel="noopener"&gt;NtSetInformationThread&lt;/a&gt; with the &lt;code&gt;ThreadHideFromDebugger&lt;/code&gt; class&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://unprotect.it/technique/ice-0xf1/" target="_blank" rel="noopener"&gt;icebp instruction&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;PEB's &lt;a href="https://unprotect.it/technique/isdebugged-flag/" target="_blank" rel="noopener"&gt;BeingDebugged&lt;/a&gt; flag&lt;/li&gt;&#10;&lt;li&gt;PEB's &lt;a href="https://unprotect.it/technique/ntglobalflag/" target="_blank" rel="noopener"&gt;NtGlobalFlag&lt;/a&gt; field&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://unprotect.it/technique/heap-flag/" target="_blank" rel="noopener"&gt;ProcessHeap&lt;/a&gt; flags&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://unprotect.it/technique/trap-flag/" target="_blank" rel="noopener"&gt;Trap Flag&lt;/a&gt; in the &lt;code&gt;EFLAGS&lt;/code&gt; register&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://anti-debug.checkpoint.com/techniques/assembly.html#int3" target="_blank" rel="noopener"&gt;int3&lt;/a&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Speaking of TLS callbacks, there is another one that is called first and uses the &lt;code&gt;IsWow64Process2&lt;/code&gt; function to check that the process is running under &lt;a href="https://learn.microsoft.com/en-us/windows/win32/winprog64/running-32-bit-applications" target="_blank" rel="noopener"&gt;WOW64&lt;/a&gt;. If it isn't, or the machine isn't &lt;code&gt;IMAGE_FILE_MACHINE_I386&lt;/code&gt;/&lt;code&gt;AMD64&lt;/code&gt;, it will not start. By the way, &lt;code&gt;vmcrack&lt;/code&gt; is a 32-bit binary that emulates x86, so this check kind of makes sense.&lt;/p&gt;&#10;&lt;p&gt;Now let's dive into the actual implementation of the virtual machine. The whole code uses a single function to run it, and it receives only 2 parameters: the bytecode to be executed and a pointer to the memory block that the machine will use. The memory block size is always the same: &lt;code&gt;0x10000&lt;/code&gt; for the memory itself and &lt;code&gt;0x2C&lt;/code&gt; for the registers.&lt;/p&gt;&#10;&lt;p&gt;The internal implementation is confusing, and I'm guessing intentionally: there are no separate functions that emulate instructions. It's more like a &lt;a href="https://en.wikipedia.org/wiki/Stack_machine" target="_blank" rel="noopener"&gt;stack machine&lt;/a&gt; that performs individual steps during emulation, such as resolving the opcode, parsing the instruction parameters, handling the addressing modes, and so on. Each step gets data from the stack and returns the result to the stack so that the next one can do its job too. Eventually, when parameter checking and resolution are complete (which are more or less the same across instructions), the logic of the instruction is executed. Each callback gets its parameters in registers (yes, no more stack), typically like this: &lt;code&gt;eax=dst ecx=src edx=size&lt;/code&gt;, where &lt;code&gt;dst/src&lt;/code&gt; is either a register or memory, and &lt;code&gt;size&lt;/code&gt; specifies the size of the operands. I say &amp;quot;typically&amp;quot; because some instructions have special meaning, but most of the time the logic is the same.&lt;/p&gt;&#10;&lt;p&gt;The virtual machine has &lt;a href="https://homepage.cs.uiowa.edu/~dwjones/arch/notes/30varlen.html" target="_blank" rel="noopener"&gt;variable-length instructions&lt;/a&gt;: the smallest instruction is encoded using 2 bytes, and the largest takes up to 15 bytes. Some instructions can perform non-trivial logic, so the VM implements a kind of &lt;a href="https://en.wikipedia.org/wiki/Complex_instruction_set_computer" target="_blank" rel="noopener"&gt;CISC&lt;/a&gt; architecture. The general encoding for &lt;em&gt;standard&lt;/em&gt; instructions (there are also non-standard ones) is as follows:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;General encoding:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;- opcode: 1 byte&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;- first argument&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - operand_type: 1 byte&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - (optional) operand_size: 1 byte&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - operand: 1 to 4 bytes&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - (optional) index_register: 1 byte&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - (optional) scale: 1 byte&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - (optional) displacement: 4 bytes&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;- (optional) second argument&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - operand_type: 1 byte&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - (optional) operand_size: 1 byte&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - operand: 1 to 4 bytes&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - (optional) index_register: 1 byte&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - (optional) scale: 1 byte&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - (optional) displacement: 4 bytes&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Operand types:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - 0x01: register&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - 0x02: memory&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - 0x03: immediate&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Operand sizes:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - 0x01: byte&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - 0x02: word&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - 0x03: dword&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Registers:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x04: eax/ax/al&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x08: ecx/cx/cl&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x0C: edx/dx/dl&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x10: ebx/bx/bl&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x14: esp/sp&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x18: ebp/bp&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x1C: esi/si&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; 0x20: edi/di&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Argument rules:&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; if &amp;#39;operand_type&amp;#39; is `register`, argument format is &amp;#39;operand_type operand_size register&amp;#39;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; if &amp;#39;operand_type&amp;#39; is `memory`, argument format is &amp;#39;operand_type operand_size base_register index_register scale displacement/4 bytes&amp;#39;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; if &amp;#39;operand_type&amp;#39; is `immediate`, argument format is &amp;#39;operand_type immediate_value/4 bytes&amp;#39;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here are some examples:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;1D 01 03 18&lt;/code&gt;:&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;1D=push&lt;/code&gt; &lt;code&gt;01=register&lt;/code&gt; &lt;code&gt;03=dword&lt;/code&gt; &lt;code&gt;18=ebp&lt;/code&gt; =&amp;gt; &lt;code&gt;push ebp&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;1A 01 03 18 01 03 14&lt;/code&gt;:&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;1A=mov&lt;/code&gt; &lt;code&gt;01=register&lt;/code&gt; &lt;code&gt;03=dword&lt;/code&gt; &lt;code&gt;18=ebp&lt;/code&gt; &lt;code&gt;01=register&lt;/code&gt; &lt;code&gt;03=dword&lt;/code&gt; &lt;code&gt;14=esp&lt;/code&gt; =&amp;gt; &lt;code&gt;mov ebp, esp&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;09 02 01 20 0C 04 BE BA FE CA 03 7C 00 00 00&lt;/code&gt;:&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;09=add&lt;/code&gt; &lt;code&gt;02=memory&lt;/code&gt; &lt;code&gt;01=byte&lt;/code&gt; &lt;code&gt;20=edi&lt;/code&gt; &lt;code&gt;0C=edx&lt;/code&gt; &lt;code&gt;04=scale&lt;/code&gt; &lt;code&gt;BE BA FE CA=displacement&lt;/code&gt; &lt;code&gt;03=immediate&lt;/code&gt; &lt;code&gt;7C 00 00 00=immediate_value&lt;/code&gt; =&amp;gt; &lt;code&gt;add byte ptr [edi+edx*4+0xCAFEBABE], 0x7C&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;To make my life a little easier, I wrote a &lt;a href="https://github.com/itwaseasy/crackmes-solutions/tree/master/hackTheBox/vmcrack/disassembler" target="_blank" rel="noopener"&gt;small disassembler&lt;/a&gt;. All the rules are there, so it may be easier to understand the logic of the virtual machine by reading the code instead of this text.&lt;/p&gt;&#10;&lt;p&gt;These are exceptions to the rules described earlier:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;opcodes &lt;code&gt;0x33&lt;/code&gt;-&lt;code&gt;0x35&lt;/code&gt; have the format &lt;code&gt;opcode immediate/2 bytes&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;opcodes &lt;code&gt;0x36&lt;/code&gt;-&lt;code&gt;0x40&lt;/code&gt; have the format &lt;code&gt;opcode operand_type&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Please refer to the disassembler sources to see which opcode maps to which x86 instruction. I won't describe them here, except for the one I called &lt;code&gt;mov_wtf&lt;/code&gt;. The name says it all: I was struggling to understand what it does until I saw it used in the bytecode and found this &lt;a href="https://web.archive.org/web/20261114205946/http://blog.rewolf.pl/blog/?p=102" target="_blank" rel="noopener"&gt;blog post&lt;/a&gt;. Honestly, I didn't even know that this was possible, but it does exactly what the article describes: it switches from 32-bit mode to 64-bit mode and back! It's really cool.&lt;/p&gt;&#10;&lt;p&gt;Okay, I hope that my chaotic description of the VM architecture was more or less clear (or that you at least read the disassembler source code), so let's move on to the task itself.&lt;/p&gt;&#10;&lt;p&gt;First of all, every time you do something wrong, it shows some weird text and exits. For example, if you run it under a debugger and hit one of the anti-debugging checks, that's what happens. If your input is incorrect, it shows you something else, and so on. In fact, every wrong move has its own text, so this is also a kind of way to debug it :)&lt;/p&gt;&#10;&lt;p&gt;These messages are stored in the binary, but they are encrypted. To decrypt and print them, the crackme runs the virtual machine with special bytecode. If you disassemble the challenge, you will see the following pattern:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;something_is_wrong&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;bytecode_decrypt_and_print_message&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;encrypted_text_offset&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;binary_blob_with_encrypted_text_ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;bytecode_decrypt_and_print_message&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;encrypted_text_key_offset&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xF00DCAFE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// or any other key&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;memset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm_memory&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm_memory&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;run_vm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm_memory&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bytecode_decrypt_and_print_message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;memset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm_memory&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm_memory&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;run_vm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm_memory&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bytecode_exit&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As you can see, the same bytecode is used all the time, but to print different messages, the necessary pointers and keys are patched into the bytecode, so when the virtual machine starts, it decrypts and prints the right message. On top of that, the addresses of the &lt;code&gt;puts&lt;/code&gt; and &lt;code&gt;exit&lt;/code&gt; functions are patched in the same way, but the code does so in the TLS callback responsible for the &lt;code&gt;WOW64&lt;/code&gt; check.&lt;/p&gt;&#10;&lt;p&gt;The string encryption algorithm is quite simple, so I used the following IDC script to view the text in IDA:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current_byte&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0xF00DCAFE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_screen_ea&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="nf"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xFF&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;patch_byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mh"&gt;0x7C&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;current_byte&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Once you get past half of the anti-debugging tricks, the virtual machine executes the bytecode that reads and validates the input. The &lt;code&gt;run_vm&lt;/code&gt; call with this bytecode is protected by another trick, and some of the tricks are in the bytecode itself, so have fun if you're still trying to debug it. For this bytecode to work, the pointers to &lt;code&gt;fputs&lt;/code&gt;/&lt;code&gt;fgets&lt;/code&gt; and &lt;code&gt;stdin&lt;/code&gt;/&lt;code&gt;stdout&lt;/code&gt; are also patched into it, directly in the &lt;code&gt;main&lt;/code&gt; function.&lt;/p&gt;&#10;&lt;p&gt;Once the input is read from standard input, the bytecode encrypts it with a set of instructions that shuffle the bits back and forth, and then &lt;code&gt;memcmp&lt;/code&gt; checks that the input is correct. If not, you get another strange message from the AI, and it exits. Fortunately, the algorithm is easily reversible, and the correct input can be decoded using the following code:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="nf"&gt;decrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;xor_byte&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;uint8_t&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kr"&gt;__asm&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;mov&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;xor&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;xor_byte&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;inc&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;rol&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x1d&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;sub&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x3e&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;neg&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;xor&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0xc3&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;not&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x0e&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;ror&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x9f&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;dec&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;mov&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;al&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;where &lt;code&gt;c&lt;/code&gt; is the current encrypted character and &lt;code&gt;xor_byte&lt;/code&gt; is the previous one.&lt;/p&gt;&#10;&lt;p&gt;This is probably all you need if your goal is to get the flag, because the challenge shows it when the input is correct. But I was more curious and decided to figure out where the flag was and how it was encrypted.&lt;/p&gt;&#10;&lt;p&gt;Unsurprisingly, this uses yet another piece of bytecode, and it's actually quite fun. If you've been analyzing the binary from the beginning, you've probably noticed that the &lt;code&gt;main&lt;/code&gt; function also initializes some global variables with pointers to various functions. Some of them are obvious: they directly use the Windows API to calculate SHA-256 and MD5 hashes. Another one isn't as obvious, but if you know how CRC32 works, you'll recognize it right away. The last one took me a while to figure out, until I found a table that also reminded me of something. It turned out that this is an &lt;a href="https://en.wikipedia.org/wiki/Rijndael_S-box" target="_blank" rel="noopener"&gt;AES S-box&lt;/a&gt; table, and the function performs AES decryption. All of these are used in the final bytecode that decrypts the flag, embedded in gorgeous ASCII art. To make it a little funnier, the text is also hidden using the same method used for other string data in the binary. So, to know that the user's input was correct, the challenge tries to decrypt the flag and checks that the CRC32 of the decrypted blob is equal to &lt;code&gt;0x4da2c9f6&lt;/code&gt;, which is the CRC32 of the correct data.&lt;/p&gt;&#10;&lt;p&gt;To summarize, the flag can be decrypted using the following code:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;sha&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SHA256&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;sha&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;correct_input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;md5&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;MD5&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;md5&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;correct_input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;cipher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;AES&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sha&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="no"&gt;AES&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;MODE_CBC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;md5&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;decrypted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# decrypt the text&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;decrypted&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;decrypted&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="mh"&gt;0xc1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mh"&gt;0x7c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xff&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="s1"&gt;c&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The challenge can be found &lt;a href="https://app.hackthebox.com/challenges/vmcrack" target="_blank" rel="noopener"&gt;here&lt;/a&gt;, and my disassembler and the disassembled bytecode can be found &lt;a href="https://github.com/itwaseasy/crackmes-solutions/tree/master/hackTheBox/vmcrack" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item><item><title>"Virtually Mad" by Hack The Box</title><link>https://pugachev.io/2024/03/26/virtually-mad-by-hack-the-box/</link><pubDate>Tue, 26 Mar 2024 16:22:19 +0100</pubDate><guid>https://pugachev.io/2024/03/26/virtually-mad-by-hack-the-box/</guid><description>&lt;p&gt;This challenge implements a simple virtual machine with only 5 instructions. Your task is to write a program for this VM that satisfies some requirements, and the flag is the bytecode of this program. Pretty interesting!&lt;/p&gt;&#10;&lt;p&gt;Each instruction is 4 bytes long and has the following format:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;nibbles 7 and 6 encode the opcode&lt;/li&gt;&#10;&lt;li&gt;nibble 5 should always be 1&lt;/li&gt;&#10;&lt;li&gt;nibble 4 encodes the destination register&lt;/li&gt;&#10;&lt;li&gt;nibble 3 is a flag: if set to 1, nibble 2 encodes the source register, otherwise, nibbles 0-2 encode the immediate value&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;I'll write the instructions I used like this: &lt;code&gt;opcode=01 always=1 dst_reg=0 irflag=0 imm_or_src_reg=100&lt;/code&gt;, where &lt;code&gt;irflag&lt;/code&gt; is the &amp;quot;immediate or register&amp;quot; flag. The other fields are self-explanatory.&lt;/p&gt;&#10;&lt;p&gt;The VM has only 4 registers, encoded using their index in the VM state structure: &lt;code&gt;0=reg_a, 1=reg_b, 2=reg_c, 3=reg_d&lt;/code&gt;. There is also a &lt;code&gt;flags&lt;/code&gt; register, which can only have one of 2 values: &lt;code&gt;0&lt;/code&gt; or &lt;code&gt;0x10000000&lt;/code&gt;. It is used to store the result of the &lt;code&gt;cmp&lt;/code&gt; instruction: if the values are equal, the flags register will have the value &lt;code&gt;0x10000000&lt;/code&gt;, otherwise, it will be &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;These are the opcodes implemented by the virtual machine (where the number is the value of the opcode): &lt;code&gt;1=mov, 2=add, 3=sub, 4=cmp, 5=exit&lt;/code&gt;. Every opcode except &lt;code&gt;exit&lt;/code&gt; checks &lt;code&gt;irflag&lt;/code&gt; to see where the source value comes from. For example, &lt;code&gt;add&lt;/code&gt; in pseudocode looks like this:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;irflag&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vm_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;registers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;imm_or_src_reg&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;else&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;imm_or_src_reg&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;vm_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;registers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dst_reg&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The same logic applies to both the &lt;code&gt;sub&lt;/code&gt; and &lt;code&gt;mov&lt;/code&gt; opcodes. The only exception is &lt;code&gt;cmp&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;irflag&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vm_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;registers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;imm_or_src_reg&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;else&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;imm_or_src_reg&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;vm_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;registers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dst_reg&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;vm_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;flags&lt;/span&gt; &lt;span class="o"&gt;|=&lt;/span&gt; &lt;span class="mh"&gt;0x10000000&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;else&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;vm_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;flags&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;=&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="mh"&gt;0x10000000&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And that's all this virtual machine can do. Now the task is to write a program that ends with the following state:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;&lt;code&gt;reg_a&lt;/code&gt; should be equal to &lt;code&gt;0x200&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;reg_b&lt;/code&gt; should be equal to &lt;code&gt;-1&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;reg_c&lt;/code&gt; should be equal to &lt;code&gt;-1&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;reg_d&lt;/code&gt; should be equal to &lt;code&gt;0&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;flags&lt;/code&gt; should be set to &lt;code&gt;0x10000000&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;The total number of instructions in the program must be equal to &lt;code&gt;5&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;There are many ways to achieve these goals, so to narrow down the options (and subsequently check the flag), the crackme places the following restrictions on your program. Each instruction passes if at least one of its two conditions is true:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;The first instruction should be &lt;code&gt;add&lt;/code&gt;, or its &lt;code&gt;dst_reg&lt;/code&gt; should be &lt;code&gt;reg_a&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;The second instruction should be &lt;code&gt;add&lt;/code&gt;, or its immediate value should be &lt;code&gt;0x100&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;The third instruction should be &lt;code&gt;sub&lt;/code&gt;, or its &lt;code&gt;dst_reg&lt;/code&gt; should be &lt;code&gt;reg_b&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;The fourth instruction should be &lt;code&gt;mov&lt;/code&gt;, or its &lt;code&gt;dst_reg&lt;/code&gt; should be &lt;code&gt;reg_c&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;The fifth instruction should be &lt;code&gt;cmp&lt;/code&gt;, or its &lt;code&gt;dst_reg&lt;/code&gt; should be &lt;code&gt;reg_d&lt;/code&gt;&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;The intended program is the following:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;0: opcode=02 always=1 dst_reg=0(a) irflag=0 imm_or_src_reg=100 add a, 256&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;1: opcode=02 always=1 dst_reg=0(a) irflag=0 imm_or_src_reg=100 add a, 256&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;2: opcode=03 always=1 dst_reg=1(b) irflag=0 imm_or_src_reg=001 sub b, 1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;3: opcode=01 always=1 dst_reg=2(c) irflag=1 imm_or_src_reg=100 mov c, b&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;4: opcode=04 always=1 dst_reg=3(d) irflag=0 imm_or_src_reg=000 cmp d, 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So, if you concatenate the encoded instructions into one line, you get the correct answer.&lt;/p&gt;&#10;&lt;p&gt;The challenge can be found &lt;a href="https://app.hackthebox.com/challenges/Virtually%20Mad" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item><item><title>"MATRIX" by picoCTF</title><link>https://pugachev.io/2024/01/11/matrix-by-picoctf/</link><pubDate>Thu, 11 Jan 2024 18:30:36 +0100</pubDate><guid>https://pugachev.io/2024/01/11/matrix-by-picoctf/</guid><description>&lt;p&gt;I always thought picoCTF was the simplest CTF, but it seems like &amp;quot;it depends&amp;quot;, as usual. This problem was quite difficult, at least for me, but very interesting to solve!&lt;/p&gt;&#10;&lt;p&gt;Let's start with the logic, which is quite clear: you need to enter something, and the crackme either exits with &amp;quot;You were eaten by a grue&amp;quot; or prints &amp;quot;Have a flag!&amp;quot; and the flag. What's interesting is that the first message is not visible in the binary:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-console" data-lang="shell"&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; strings -a ./matrix &lt;span class="p"&gt;|&lt;/span&gt; grep grue&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line cmd"&gt;&lt;span class="cl"&gt;&lt;span class="gp"&gt;$&lt;/span&gt; strings -a ./matrix &lt;span class="p"&gt;|&lt;/span&gt; grep flag&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Have a flag!&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;flag.txt&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;If you dig into the binary, it turns out that it only has one function, which takes some parameters, including pointers to the &lt;code&gt;getc&lt;/code&gt; and &lt;code&gt;putc&lt;/code&gt; wrappers that do the I/O. After some time, I was surprised to discover that this function is a virtual machine that executes the &amp;quot;code&amp;quot; stored in one of the fields of the structure it gets as a parameter! The easiest CTF, huh? The structure itself is the state of the virtual machine and looks like this:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;VM&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;machine_code&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;short&lt;/span&gt; &lt;span class="n"&gt;instruction_ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;extra_stack&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;getc&lt;/span&gt;&lt;span class="p"&gt;)();&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;putc&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;};&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The virtual machine has only 15 instructions, one of which is &lt;code&gt;nop&lt;/code&gt;, and they are all stack-based. In other words, it implements some stack-oriented programming language. The only thing is that there are two stacks inside: the main one, which is used by almost every instruction, and a second one, which I call the &amp;quot;extra&amp;quot; stack. It's temporary storage, used mainly to save values for later, swap them and so on. The VM operates only on words: any stack value is 2 bytes long.&lt;/p&gt;&#10;&lt;p&gt;Below is a list of opcodes and the instructions that I have chosen for them. It's a little difficult to map one VM opcode to one x86 instruction, so I decided not to come up with a new assembly language (mostly) and decoded each opcode as a sequence of instructions. There are no registers in the &lt;code&gt;VM&lt;/code&gt; structure, but I decided to use &lt;code&gt;r1&lt;/code&gt; and &lt;code&gt;r2&lt;/code&gt; as temporary storage for the data used in these instructions, and &lt;code&gt;sp&lt;/code&gt; as the current stack pointer. Keep in mind that in my case the stack grows up and &lt;code&gt;sp&lt;/code&gt; always points to the next empty slot, so &lt;code&gt;[sp-2]&lt;/code&gt; is the last element pushed.&lt;/p&gt;&#10;&lt;p&gt;To access the &amp;quot;extra&amp;quot; stack, I use two custom instructions, &lt;code&gt;push_e&lt;/code&gt; and &lt;code&gt;pop_e&lt;/code&gt;. Also, a called function returns its result in &lt;code&gt;r1&lt;/code&gt;, and if it needs a parameter, it's either on the stack or in &lt;code&gt;r1&lt;/code&gt;.&lt;/p&gt;&#10;&lt;table&gt;&#10;&#9;&lt;thead&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Opcode&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Length&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Disasm&lt;/th&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/thead&gt;&#10;&#9;&lt;tbody&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0x00&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;1&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;nop&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0x01&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;1&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;pop r1&lt;br&gt;ret&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0x10&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;1&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;push [sp-2]&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0x11&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;1&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;pop r1&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0x12&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;1&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;pop r1&lt;br&gt;add [sp-2], r1&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0x13&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;1&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;pop r1&lt;br&gt;sub [sp-2], r1&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0x14&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;1&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;xchg [sp-4], [sp-2]&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0x20&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;1&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;pop r1&lt;br&gt;push_e r1&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0x21&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;1&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;pop_e r1&lt;br&gt;push r1&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0x30&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;1&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;pop r1&lt;br&gt;jmp r1&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0x31&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;1&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;pop r1&lt;br&gt;pop r2&lt;br&gt;test r2, r2&lt;br&gt;jnz NEXT_INSTRUCTION&lt;br&gt;jmp r1&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0x80&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;2&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;push NUMBER&lt;br&gt;&lt;em&gt;NUMBER is 1 byte long, sign-extended to a word&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0x81&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;3&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;push NUMBER&lt;br&gt;&lt;em&gt;NUMBER is 2 bytes long&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0xC0&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;1&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;call getc&lt;br&gt;push r1&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;0xC1&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;1&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;pop r1&lt;br&gt;call putc&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;p&gt;To make life a little easier, I wrote a small disassembler that converts the bytecode into the instructions above, so that I could read and annotate it. I ran it, opened the listing, and tried to read all 1362 lines it generated. Honestly, this is a tough read because of all these pop/push/push/pop/push sequences that you need to keep track of. My idea was that if a virtual machine is so simple, common logic like loops should follow some templates. Also, it looks like the compiler was very simple, since it generates a lot of unnecessary instructions in blocks that could be greatly simplified. Or is this some kind of obfuscation?&lt;/p&gt;&#10;&lt;p&gt;Anyway, I spent some time cleaning up and commenting the listing, and it ended up being just 307 lines long, including comments and the printed strings, which are also pushed onto the stack character by character. My cleaned-up version doesn't follow the original syntax (I've extended it a bit), but in return, it's much clearer. The next surprise was that this was actually a maze game. A MAZE GAME, omg! Written in a custom assembly language for a custom virtual machine! With traps and character health! Not bad at all.&lt;/p&gt;&#10;&lt;p&gt;Most of the code in the generated listing is the map: it's a 16x16 array where each cell contains code that either allows you to enter that cell or implements other things like walls and traps. Each cell's code is 4 bytes long. There are 5 types of cells:&lt;/p&gt;&#10;&lt;table&gt;&#10;&#9;&lt;thead&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Code&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;Symbol&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;What&lt;/th&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/thead&gt;&#10;&#9;&lt;tbody&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;30000000&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;em&gt;space&lt;/em&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Place where you can walk&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;81FB0030&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;X&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Wall&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;817F0530&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;+&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Get health&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;81740530&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;-&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Trap&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;81850530&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;!&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Exit&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;p&gt;Finding your way on a 16x16 map is easy when you can see it, so I wrote a little utility that draws it. Here's what it looks like:&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="https://pugachev.io/2024/01/11/matrix-by-picoctf/matrix_maze.png" alt="matrix maze"&gt;&lt;/p&gt;&#10;&lt;p&gt;You start at the &amp;quot;1:1&amp;quot; location with 0 health (well, maybe it's not health, but still), and your goal is to find the way out of the maze without dying. To move in any direction, you need to enter &lt;code&gt;u&lt;/code&gt;, &lt;code&gt;d&lt;/code&gt;, &lt;code&gt;l&lt;/code&gt; or &lt;code&gt;r&lt;/code&gt; for &amp;quot;up&amp;quot;, &amp;quot;down&amp;quot;, &amp;quot;left&amp;quot; and &amp;quot;right&amp;quot; respectively. If you step on a &lt;code&gt;-&lt;/code&gt; cell, you lose 1 health. If you step on a &lt;code&gt;+&lt;/code&gt; cell, you gain 1 health.&lt;/p&gt;&#10;&lt;p&gt;If you pay close attention, you'll see that you won't be able to get through the maze easily, because you'll die in the first trap. So, first of all, you need to gain health before stepping on it. To get the flag, you need to enter a sequence of directions as one long string, like &lt;code&gt;rrrrrldu&lt;/code&gt; and so on, and your path needs to include enough &lt;code&gt;+&lt;/code&gt; cells so you don't die.&lt;/p&gt;&#10;&lt;p&gt;That's awesome, really :)&lt;/p&gt;&#10;&lt;p&gt;Get the crackme &lt;a href="https://learn.cylabacademy.org/library/221" target="_blank" rel="noopener"&gt;here&lt;/a&gt; and the solution &lt;a href="https://github.com/itwaseasy/crackmes-solutions/tree/master/picoCTF/matrix" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item></channel></rss>