<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Anti-Debug on pugachev.io</title><link>https://pugachev.io/tags/anti-debug/</link><description>Recent content in Anti-Debug on pugachev.io</description><generator>Hugo</generator><language>en-US</language><copyright>© Maxim Pugachev</copyright><lastBuildDate>Fri, 04 Dec 2026 11:44:14 +0100</lastBuildDate><atom:link href="https://pugachev.io/tags/anti-debug/index.xml" rel="self" type="application/rss+xml"/><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>"PTRACE_NOPEEKING" by Hack The Box</title><link>https://pugachev.io/2024/04/02/ptrace_nopeeking-by-hack-the-box/</link><pubDate>Tue, 02 Apr 2024 17:26:12 +0100</pubDate><guid>https://pugachev.io/2024/04/02/ptrace_nopeeking-by-hack-the-box/</guid><description>&lt;p&gt;Debugging a debugger is probably as much fun as disassembling a disassembler or decompiling a decompiler. But disassembling two debuggers debugging each other at the same time and preventing you from decompiling them is priceless.&lt;/p&gt;&#10;&lt;p&gt;To start simple, there is one binary that forks and checks 2 parts of the flag at the same time. To complicate things a little, these 2 processes behave as both a debugger and a debuggee, controlling each other's execution.&lt;/p&gt;&#10;&lt;p&gt;Each process manages its own state: the flag-checking algorithm is not executed sequentially but is divided into several stages. For example, the first step (state &lt;code&gt;0&lt;/code&gt;) of the parent process is to initialize the global variables used by the check and read the input from standard input. It then moves to the next state and passes control to the child. When the child starts, its first state is also initialization. On its next turn, the parent runs the check logic. Then the child. And it all goes back and forth, so both processes take turns.&lt;/p&gt;&#10;&lt;p&gt;To make things even harder and prevent you from easily reading the C code decompiled for you by IDA, the crackme uses a trick with &lt;code&gt;ud2&lt;/code&gt; &lt;a href="https://www.felixcloutier.com/x86/ud" target="_blank" rel="noopener"&gt;instructions&lt;/a&gt;, and this is where the &amp;quot;debug-each-other&amp;quot; logic comes into play. When the debuggee executes this instruction, the OS wakes up the debugger because the debuggee was stopped by the delivery of a signal. Then, if this signal is &lt;code&gt;SIGILL&lt;/code&gt;, the magic happens: the current debugger checks the 2 bytes following the &lt;code&gt;ud2&lt;/code&gt; instruction and runs additional logic based on them using &lt;code&gt;ptrace&lt;/code&gt;. For example, if &lt;code&gt;SIGILL&lt;/code&gt; was delivered and the next 2 bytes are &lt;code&gt;0x5253&lt;/code&gt;, the debugger runs logic that reads user input. If the bytes are &lt;code&gt;0x4543&lt;/code&gt;, the debugger runs logic that decrypts the current character, and so on. There are 5 such cases that either simply transfer data from a register to memory, or run more complex logic, such as reading from standard input.&lt;/p&gt;&#10;&lt;p&gt;To split the check between the 2 processes, a global array is used:&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="addr"&gt;.data:0000000000202020&lt;/span&gt; &lt;span class="nf"&gt;whose_turn_to_check&lt;/span&gt; &lt;span class="nv"&gt;dd&lt;/span&gt; &lt;span class="mi"&gt;0&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="mi"&gt;1&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&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="mi"&gt;1&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="addr"&gt;.data:0000000000202068&lt;/span&gt; &lt;span class="kd"&gt;dd&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&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="mi"&gt;1&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="mi"&gt;0&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="mi"&gt;1&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;There are 27 integers (one per flag character), each of which indicates which process should check the current character. If it is 0, the parent should do it, otherwise the child is responsible for it. The interesting thing is that if the currently active process does not need to check the character, it still switches its current state to &amp;quot;I'm-going-to-check-this-character&amp;quot;. But instead of checking the actual character, it gets a value from &lt;code&gt;rand()&lt;/code&gt; and marks it, so in the following states, it can understand that the character is &amp;quot;fake&amp;quot; and skip it correctly.&lt;/p&gt;&#10;&lt;p&gt;In any case, once you figure out what's going on, the logic behind the check won't seem very complicated. Each process does the same thing, which can be summarized in 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;int&lt;/span&gt; &lt;span class="n"&gt;encrypted_char&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_char&lt;/span&gt; &lt;span class="o"&gt;-&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;26&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="mi"&gt;44&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;i&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;int&lt;/span&gt; &lt;span class="n"&gt;flag_char&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;whose_turn_to_check&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="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;part_0&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;part0_index&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;part_1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;part1_index&lt;/span&gt;&lt;span class="o"&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;&#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;encrypted_char&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;flag_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="n"&gt;failed&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In other words, the process performs some math on the current character entered by the user, and if the result is not equal to the hardcoded value, the input is invalid. &lt;code&gt;part_0&lt;/code&gt; and &lt;code&gt;part_1&lt;/code&gt; are arrays of expected values that the parent and the child check in turn.&lt;/p&gt;&#10;&lt;p&gt;To recover the flag, you can use 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;int&lt;/span&gt; &lt;span class="n"&gt;part0_index&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;part1_index&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="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&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;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;&amp;lt;=&lt;/span&gt; &lt;span class="mh"&gt;0x1A&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&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;int&lt;/span&gt; &lt;span class="n"&gt;encrypted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;whose_turn_to_check&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="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;part_0&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;part0_index&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;part_1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;part1_index&lt;/span&gt;&lt;span class="o"&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;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encrypted&lt;/span&gt; &lt;span class="o"&gt;^&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="mh"&gt;0x2C&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mh"&gt;0x1A&lt;/span&gt; &lt;span class="o"&gt;+&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="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="nf"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;%c&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&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="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 challenge can be found &lt;a href="https://app.hackthebox.com/challenges/PTRACE_NOPEEKING" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item><item><title>"Bombs Landed" by Hack The Box</title><link>https://pugachev.io/2024/01/16/bombs-landed-by-hack-the-box/</link><pubDate>Tue, 16 Jan 2024 12:32:38 +0100</pubDate><guid>https://pugachev.io/2024/01/16/bombs-landed-by-hack-the-box/</guid><description>&lt;p&gt;This is another example from HTB that implements &amp;quot;decryption&amp;quot; of code at runtime (like the &amp;quot;&lt;a href="https://pugachev.io/2024/01/04/partial-encryption-by-hack-the-box/"&gt;Partial Encryption&lt;/a&gt;&amp;quot; crackme I described earlier). On top of that, it has a classic anti-debugging trick with &lt;code&gt;ptrace&lt;/code&gt; and cleverly hidden code that took me a while to find. That's the &amp;quot;wtf&amp;quot; moment, you know, that doubles and becomes &amp;quot;WTF&amp;quot; when you finally solve the problem. But, as usual, let's start from the beginning.&lt;/p&gt;&#10;&lt;p&gt;The disassembly shows that it starts by checking &lt;code&gt;argc&lt;/code&gt; and some flags that indicate whether the process is under a debugger (or &lt;code&gt;strace&lt;/code&gt;, or any other tool that uses &lt;code&gt;ptrace&lt;/code&gt;). The hidden code runs only if the process isn't traced and &lt;code&gt;argc&lt;/code&gt; is less than 5. Otherwise, if &lt;code&gt;argc&lt;/code&gt; is greater than 3, it will lead you down the wrong path, still asking for a password. Do you want to crash the application? Enter &lt;code&gt;X&lt;/code&gt; as the password. In all other cases, it just prints a nice &amp;quot;Bad luck dude.&amp;quot; message and exits.&lt;/p&gt;&#10;&lt;p&gt;The &lt;code&gt;ptrace&lt;/code&gt; check has been moved to the &lt;code&gt;init&lt;/code&gt; function (&lt;code&gt;__attribute__((constructor))&lt;/code&gt; in GCC), which automatically runs before &lt;code&gt;main&lt;/code&gt; and sets those flags. If you still want to debug or trace it, there are at least 3 ways around it: patch the binary, use the &lt;code&gt;LD_PRELOAD&lt;/code&gt; trick described by everyone, or use the cool &amp;quot;syscall tampering&amp;quot; feature that &lt;code&gt;strace&lt;/code&gt; has:&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; strace -e &lt;span class="nv"&gt;inject&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;ptrace:retval&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt; ./BombsLanded &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;which causes the &lt;code&gt;ptrace&lt;/code&gt; syscall to return 0 when called from an application.&lt;/p&gt;&#10;&lt;p&gt;If everything is OK, the crackme maps executable memory with &lt;code&gt;mmap&lt;/code&gt; and &amp;quot;decrypts&amp;quot; its hidden code there via xor (of course). The hidden code is 0x197 bytes long, and you can decrypt it in IDA using the following 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;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="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="mh"&gt;0x196&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;&#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;0x63&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;This is where the fun begins. The decrypted function looks quite simple, even though it uses xor-encrypted strings for some reason. Double xor = double protection? Maybe. Basically, it asks you for a password, gets a string from the standard input, and compares it with a hardcoded string containing a non-printable character:&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="addr"&gt;LOAD:0804929D&lt;/span&gt; &lt;span class="nf"&gt;push&lt;/span&gt; &lt;span class="mh"&gt;0Ah&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="addr"&gt;LOAD:0804929F&lt;/span&gt; &lt;span class="nf"&gt;lea&lt;/span&gt; &lt;span class="nb"&gt;eax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;ebp&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nv"&gt;password&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="addr"&gt;LOAD:080492A2&lt;/span&gt; &lt;span class="nf"&gt;push&lt;/span&gt; &lt;span class="nb"&gt;eax&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="addr"&gt;LOAD:080492A3&lt;/span&gt; &lt;span class="nf"&gt;lea&lt;/span&gt; &lt;span class="nb"&gt;eax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;ebp&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nv"&gt;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;&lt;span class="addr"&gt;LOAD:080492A6&lt;/span&gt; &lt;span class="nf"&gt;push&lt;/span&gt; &lt;span class="nb"&gt;eax&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="addr"&gt;LOAD:080492A7&lt;/span&gt; &lt;span class="nf"&gt;mov&lt;/span&gt; &lt;span class="nb"&gt;eax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;offset&lt;/span&gt; &lt;span class="nv"&gt;strncmp&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="addr"&gt;LOAD:080492AC&lt;/span&gt; &lt;span class="nf"&gt;call&lt;/span&gt; &lt;span class="nb"&gt;eax&lt;/span&gt; &lt;span class="c1"&gt;; strncmp&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#10;&lt;p&gt;What could go wrong, right? No. If you look at the binary's import table, it doesn't have &lt;code&gt;strncmp&lt;/code&gt;, but for some reason it does contain &lt;code&gt;dlsym&lt;/code&gt;. And if you check where it was called from, then there's this &amp;quot;wtf&amp;quot; moment: the function is a wrapper that checks the &lt;code&gt;n&lt;/code&gt; parameter, and if it is NOT equal to 10, it just calls the original &lt;code&gt;strncmp&lt;/code&gt;. Otherwise, it xors &lt;code&gt;s2&lt;/code&gt; (the password with the non-printable character) with &lt;code&gt;0x0A&lt;/code&gt; and compares the result with &lt;code&gt;s1&lt;/code&gt;, your input. And that's it: just xor the &amp;quot;encrypted&amp;quot; password with &lt;code&gt;0x0A&lt;/code&gt; and you'll get the answer.&lt;/p&gt;&#10;&lt;p&gt;The challenge can be found &lt;a href="https://app.hackthebox.com/challenges/bombs-landed" target="_blank" rel="noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;</description></item></channel></rss>