<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by 0xMar$ on Medium]]></title>
        <description><![CDATA[Stories by 0xMar$ on Medium]]></description>
        <link>https://medium.com/@0xMars890?source=rss-7ddecf970067------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*hQshJxaVLfKZdrzHOn7ISw.jpeg</url>
            <title>Stories by 0xMar$ on Medium</title>
            <link>https://medium.com/@0xMars890?source=rss-7ddecf970067------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 11:19:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@0xMars890/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[CyCTF Luxor Finals (Mobile)]]></title>
            <link>https://medium.com/@0xMars890/cyctf-luxor-finals-mobile-49326620c26c?source=rss-7ddecf970067------2</link>
            <guid isPermaLink="false">https://medium.com/p/49326620c26c</guid>
            <category><![CDATA[mobile-writeups]]></category>
            <category><![CDATA[ast3roids]]></category>
            <category><![CDATA[cyctf-luxor-finals-mobile]]></category>
            <category><![CDATA[cyctf-26-finals]]></category>
            <category><![CDATA[cyctf-luxor-finals]]></category>
            <dc:creator><![CDATA[0xMar$]]></dc:creator>
            <pubDate>Tue, 31 Mar 2026 14:31:55 GMT</pubDate>
            <atom:updated>2026-03-31T20:30:19.205Z</atom:updated>
            <content:encoded><![CDATA[<p>AndroLock — NativeVault Mobile</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/349/1*A9Fo7lLA_ky1wnDogHSGDw.png" /></figure><h3>ِAndroLock</h3><h3>Challenge Overview</h3><p>The application initially presents a fake login screen and does not expose any obvious functionality. The challenge description states that the real flag can be obtained either by submitting a valid ADB command or by creating an exploit APK.<br>In this case, static analysis was enough to identify an exported activity that could be abused to reach a hidden internal activity.</p><h3>Reviewing the Manifest</h3><p>The first step was to inspect <strong>AndroidManifest.xml </strong>in <strong>JADX</strong>. This reveals the package name, the declared activities, and whether any of them are externally reachable.</p><p>Important findings from the manifest:</p><blockquote>Package name: com.ctf.androlock</blockquote><blockquote>SecretActivity is <strong>not</strong> exported</blockquote><blockquote>SecondActivity is exported</blockquote><blockquote>SecondActivity also has an intent filter for VIEW</blockquote><blockquote>MainActivity is exported</blockquote><pre>    package=&quot;com.ctf.androlock&quot;<br>&lt;activity<br>            android:name=&quot;com.ctf.androlock.SecretActivity&quot;<br>            android:exported=&quot;false&quot;/&gt;<br>&lt;activity<br>    android:name=&quot;com.ctf.androlock.SecondActivity&quot;<br>    android:exported=&quot;true&quot;&gt;<br><br>&lt;intent-filter&gt;<br>    &lt;action android:name=&quot;android.intent.action.VIEW&quot;/&gt;<br>    &lt;category android:name=&quot;android.intent.category.DEFAULT&quot;/&gt;<br>    &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot;/&gt;<br>    &lt;data<br>        android:scheme=&quot;ctf&quot;<br>        android:host=&quot;androlock&quot;/&gt;<br>&lt;/intent-filter&gt;<br><br>&lt;activity<br>    android:name=&quot;com.ctf.androlock.MainActivity&quot;<br>    android:exported=&quot;true&quot;&gt;</pre><p>This immediately makes <strong>SecondActivity </strong>interesting, because any Activity marked with <strong>exported=true </strong>can be invoked from outside the app using <strong>adb shell am start</strong>.</p><h3>Analyzing SecondActivity</h3><pre>public class SecondActivity extends AppCompatActivity {<br>    @Override // androidx.fragment.app.FragmentActivity, androidx.activity.ComponentActivity, androidx.core.app.ComponentActivity, android.app.Activity<br>    protected void onCreate(Bundle savedInstanceState) {<br>        super.onCreate(savedInstanceState);<br>        String next = getIntent().getStringExtra(&quot;next&quot;);<br>        if (&quot;secret&quot;.equals(next)) {<br>            Intent secretIntent = new Intent(this, (Class&lt;?&gt;) SecretActivity.class);<br>            PendingIntent pi = PendingIntent.getActivity(this, 0, secretIntent, 167772160);<br>            try {<br>                pi.send();<br>                finish();<br>            } catch (PendingIntent.CanceledException e) {<br>                e.printStackTrace();<br>            }<br>        }<br>    }<br>}</pre><h4>From this code, we can extract the full exploit logic:</h4><blockquote>SecondActivity reads an intent extra called next</blockquote><blockquote>It checks whether the value equals “secret”</blockquote><blockquote>If the condition is true, it creates an internal intent to SecretActivity</blockquote><blockquote>It then launches that internal activity using PendingIntent.send()</blockquote><h3>Analyzing SecretActivity</h3><pre>public class SecretActivity extends AppCompatActivity {<br>    @Override // androidx.fragment.app.FragmentActivity, androidx.activity.ComponentActivity, androidx.core.app.ComponentActivity, android.app.Activity<br>    protected void onCreate(Bundle savedInstanceState) {<br>        super.onCreate(savedInstanceState);<br>        TextView tv = new TextView(this);<br>        tv.setTextSize(22.0f);<br>        tv.setText(&quot;CyCTF{fake_flag_don&#39;t_submit}&quot;);<br>        Log.d(&quot;CTF&quot;, &quot;FLAG: CyCTF{fake_flag_don&#39;t_submit}&quot;);<br>        setContentView(tv);<br>    }<br>}</pre><blockquote><em>The string hardcoded inside the APK is only a fake flag and is not the real submission flag.</em></blockquote><blockquote><strong><em>SecretActivity</em></strong><em> cannot be launched directly because it is marked as android:</em><strong><em>exported=false</em></strong><em>. Therefore, the exploit must go through </em><strong><em>SecondActivity</em></strong><em>, which is externally reachable.</em></blockquote><h3>Building the ADB Exploit</h3><p>Since <strong>SecondActivity</strong> is exported and expects the <strong>extra next=secret</strong>, the correct <strong>ADB payload </strong>is straightforward:</p><pre>adb shell am start -n com.ctf.androlock/.SecondActivity --es next secret</pre><blockquote>adb shell am start launches an activity on the device</blockquote><blockquote>-n com.ctf.androlock/.SecondActivity specifies the target component</blockquote><blockquote>— es next secret sends a string extra named <strong>(next)</strong> with the value <strong>(secret)</strong></blockquote><h3><strong>Final Flag</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Aqi2a85_gfSVhdSiocF0Rw.png" /></figure><p>CyCTF{00oops_Wh0_S3N7_7H15_InT3nT???}</p><h3>NativeVault [1 solve] 🔥</h3><h3>Challenge Overview</h3><p><strong>NativeVault</strong> is an Android reversing challenge built around a very small UI, a text field, a CHECK FLAG button, and a result label that prints either <strong>ACCESS GRANTED </strong>or <strong>ACCESS DENIED</strong></p><p>At first glance, the APK looks simple, but the challenge description already hints at the real trick, all validation logic lives inside a<strong> native library</strong></p><p>That immediately changes the solving strategy</p><p>Instead of looking for a plain string comparison inside the Java code, the goal becomes tracing the Java flow into JNI, reversing the native verifier inside libnative-lib.so, understanding how the app uses the bundled asset file <strong>enc_data</strong>, and recovering the real flag from the native logic</p><h3><strong>APK Triage</strong></h3><p>I started by unpacking the APK and checking the obvious moving parts first</p><p>The files that mattered immediately were</p><blockquote>assets/enc_data<br> lib/arm64-v8a/libnative-lib.so</blockquote><p>That already suggested the intended direction</p><p>The Java layer would likely only act as a wrapper, while the real validation logic would live inside the native library, and the bundled asset file would probably contain encrypted or structured data consumed by the native checker</p><h3><strong>Tracing the Java Side in JADX</strong></h3><p>Before touching IDA, I wanted to understand the Java side first</p><p>This is usually the cleanest way to recover the full application flow before dealing with native assembly</p><pre>public final native boolean verifyAsset(byte[] fileData, String candidate);<br><br>    static {<br>        System.loadLibrary(&quot;native-lib&quot;);<br>    }</pre><p>This immediately tells us two important things</p><p>First, the app explicitly loads libnative-lib.so</p><p>Second, the native method takes both the raw asset bytes and the user supplied candidate string, which means the checker does not validate the input in isolation, it validates the input against external data loaded from the app bundle</p><pre>InputStream inputStreamOpen = this$0.getAssets().open(&quot;enc_data&quot;);<br>byte[] bytes = ByteStreamsKt.readBytes(inputStream);<br>str = this$0.verifyAsset(bytes, string) ? &quot;ACCESS GRANTED&quot; : &quot;ACCESS DENIED&quot;;</pre><p>The click handler confirms the full high level flow</p><blockquote>read user input<br>trim it<br>open the asset file <strong>enc_data</strong><br>read it into memory as raw bytes<br>pass both the file bytes and the input string into <strong>verifyAsset</strong><br>show <strong>ACCESS GRANTED</strong> only if the native function returns true</blockquote><p>At this point, the Java layer is fully understood, and the real target becomes the JNI export inside libnative-lib.so</p><h3><strong>Moving Into the Native Library</strong></h3><p>After that, I moved into lib/arm64-v8a/libnative-lib.so and searched for the JNI export corresponding to the Java native method</p><p>In IDA, the function appears as</p><p><strong>Java_com_example_nativevault_MainActivity_verifyAsset</strong></p><pre>__int64 __fastcall Java_com_example_nativevault_MainActivity_verifyAsset(<br>        __int64 a1,<br>        __int64 a2,<br>        __int64 a3,<br>        __int64 a4)</pre><p>Even before renaming variables, the JNI calling pattern makes the arguments easy to interpret</p><blockquote>a1 is the JNIEnv pointer<br>a2 is the Java object instance<br>a3 is the byte array containing enc_data<br>a4 is the user supplied string</blockquote><h3><strong>Reversing the Native Verifier</strong></h3><h4>Asset header validation</h4><pre>  v7 = (*(__int64 (__fastcall **)(__int64, __int64))(*(_QWORD *)a1 + 1368LL))(a1, a3);<br>  v8 = (_BYTE *)(*(__int64 (__fastcall **)(__int64, __int64, _QWORD))(*(_QWORD *)a1 + 1472LL))(a1, a3, 0);<br>  v9 = v8;<br>  if ( v7 &lt; 72 || *v8 != 78 || v8[1] != 86 || v8[2] != 1 || v8[3] )<br>  {<br>    (*(void (__fastcall **)(__int64, __int64, _BYTE *, __int64))(*(_QWORD *)a1 + 1536LL))(a1, a3, v8, 2);<br>    return 0;<br>  }</pre><p>The verifier begins by asking JNI for the length of the byte array and a pointer to the raw bytes</p><p>Then it validates the asset format</p><p>The checks are straightforward</p><blockquote>the file must be at least 72 bytes long<br>byte 0 must be 78, which is 0x4E, or N<br>byte 1 must be 86, which is 0x56, or V<br>byte 2 must be 1<br>byte 3 must be 0</blockquote><p>So, the file starts with the header</p><pre>4E 56 01 00</pre><p>This proves that <strong>enc_data</strong> is not just arbitrary ciphertext, it has a custom format that the native code understands</p><h4>The embedded constant tables</h4><pre>v76[0] = -91;<br>v11 = 0;<br>v76[1] = 60;<br>v76[2] = 127;<br>v76[3] = -71;<br>v76[4] = 46;<br>v76[5] = 97;<br>v76[6] = -40;<br>v76[7] = 20;<br>v76[8] = -100;<br>v76[9] = 85;<br>v76[10] = 106;<br>v76[11] = -25;<br>v76[12] = 3;<br>v76[13] = -12;<br>v76[14] = -62;<br>v76[15] = -120;<br><br>v75[0] = -17;<br>v75[1] = 67;<br>v75[2] = 78;<br>v75[3] = 53;<br>v75[4] = -76;<br>v75[5] = -38;<br>v75[6] = 91;<br>v75[7] = -105;<br>v75[8] = -4;<br>v75[9] = 4;<br>v75[10] = 102;<br>v75[11] = -114;<br>v75[12] = 59;<br>v75[13] = 49;<br>v75[14] = -61;<br>v75[15] = -85;</pre><p>The next interesting part is that the verifier builds two 16-byte tables on the stack</p><p>Converting those signed byte assignments into hex gives</p><p>Table 1, v75<br>EF 43 4E 35 B4 DA 5B 97 FC 04 66 8E 3B 31 C3 AB</p><p>Table 2, v76<br>A5 3C 7F B9 2E 61 D8 14 9C 55 6A E7 03 F4 C2 88</p><p>These values are not the flag and they are not the IV</p><p>They are hardcoded key material used to derive the real 16-byte key at runtime</p><h4>Key derivation loop</h4><pre>v12 = 7;<br>do<br>{<br>  v13 = v75[v12 &amp; 0xF];<br>  v12 += 3;<br>  *(&amp;v59 + v11) = ((v13 ^ v76[v11]) &lt;&lt; ((unsigned __int8)v11 % 5u + 1))<br>                | ((unsigned __int8)(v13 ^ v76[v11]) &gt;&gt; (((unsigned __int8)v11 % 5u) ^ 7));<br>  ++v11;<br>}<br>while ( v11 != 16 );</pre><p>This loop is where the real key is reconstructed</p><p>For each of the 16 output bytes, the code</p><p>- takes one byte from v75 using a rotating index<br>- XORs it with the corresponding byte from v76<br>- applies a rotation based on the loop index<br>- stores the result in the local buffer beginning at v59</p><p>Rewriting the logic in cleaner pseudocode gives something like</p><p>for i in range(16):<br> a = table1[(7 + 3 * i) &amp; 0xF]<br> b = table2[i]<br> t = a ^ b<br> r = (i % 5) + 1<br> out[i] = rol8(t, r)</p><h4>Script to GenerateKey</h4><pre>def rol8(x, r):<br>    x &amp;= 0xff<br>    return ((x &lt;&lt; r) | (x &gt;&gt; (8 - r))) &amp; 0xff<br><br>table1 = bytes.fromhex(&quot;EF434E35B4DA5B97FC04668E3B31C3AB&quot;)<br>table2 = bytes.fromhex(&quot;A53C7FB92E61D8149C556AE703F4C288&quot;)<br><br>out = []<br><br>idx = 7<br>for i in range(16):<br>    a = table1[idx &amp; 0xF]<br>    b = table2[i]<br>    t = a ^ b<br>    r = (i % 5) + 1<br>    out.append(rol8(t, r))<br>    idx += 3<br><br>key = bytes(out)<br><br>print(&quot;key hex:&quot;, key.hex())<br>print(&quot;key ascii:&quot;, key.decode())</pre><h4>Then the result is</h4><p>key hex: <strong>64697265637473797363616c6c733078</strong><br>key ascii: <strong>directsyscalls0x</strong></p><p>That means the verifier derives the final 16-byte key <strong>directsyscalls0x</strong></p><h4>The decryption helper call</h4><pre>sub_1CC3C((__int64 *)&amp;v56, (_DWORD)v8 + 8, (_DWORD)v8 + 24, (unsigned int)v7 - 24LL);</pre><p>This helper is where the encrypted asset payload gets processed</p><p>Even without fully reversing every internal operation inside <strong>sub_1CC3C</strong>, the argument pattern already tells us something important</p><p>The function receives data starting at <strong>asset + 8</strong>, another pointer at <strong>asset + 24</strong>, and a payload length of <strong>asset_len — 24</strong></p><p>That strongly suggests that the asset contains structured encrypted data rather than a direct plaintext flag</p><p>I used this call as the bridge into the crypto analysis, where the exact file layout, IV, ciphertext, and decryption mode become much clearer</p><h4>Final input comparison</h4><pre>v15 = (*(__int64 (__fastcall **)(__int64, __int64, _QWORD))(*(_QWORD *)a1 + 1352LL))(a1, a4, 0);<br>v16 = (*(__int64 (__fastcall **)(__int64, __int64))(*(_QWORD *)a1 + 1344LL))(a1, a4);<br><br>if ( v57 - (_BYTE *)v56 != v16 )<br>{<br>  v26 = 0;<br>  goto LABEL_41;<br>}</pre><p>The rest of the function obtains the UTF chars for the Java string, checks the length against the decrypted buffer, and then compares both byte sequences</p><p>The comparison is optimized with vector operations for longer inputs, but logically it is just checking whether</p><p><strong>decrypted_plaintext == user_input</strong></p><p>At this point, the full verifier logic is understood</p><blockquote>validate the asset header<br>derive a 16-byte key from two embedded constant tables<br>decrypt the payload stored in <strong>enc_data</strong><br>compare the plaintext with the user supplied string<br>return true only on exact match</blockquote><h3><strong>Crypto Analysis</strong></h3><p>Once the key derivation was recovered, the rest of the challenge shifted from pure reversing into a small crypto problem</p><p>At this point, I already knew that the native verifier was reconstructing a 16-byte key at runtime, and that the final derived key was</p><p><strong>directsyscalls0x</strong></p><p>The next step was to understand how this key was actually used against the bundled asset file <strong>enc_data</strong></p><p>The answer comes directly from the JNI verifier itself, specifically this call inside <strong>Java_com_example_nativevault_MainActivity_verifyAsset</strong></p><pre>sub_1CC3C((__int64 *)&amp;v56, (_DWORD)v8 + 8, (_DWORD)v8 + 24, (unsigned int)v7 - 24LL);</pre><p>This line is extremely important because it tells us how the native code splits the asset in memory</p><p><strong>v8</strong> is the pointer returned by <strong>GetByteArrayElements</strong>, so it points to the beginning of <strong>enc_data</strong></p><p><strong>v7</strong> is the total file length returned by <strong>GetArrayLength</strong></p><p>That means the arguments passed into <strong>sub_1CC3C </strong>can be interpreted</p><blockquote>v8 + 8 means start reading at offset 0x08</blockquote><blockquote>v8 + 24 means start reading at offset 0x18</blockquote><blockquote>v7–24 means process everything from offset 0x18 until the end of the file</blockquote><p>From this alone, the internal layout of <strong>enc_data</strong> becomes much clearer</p><blockquote>bytes 0..3 are the custom file header</blockquote><blockquote>bytes 4..7 are additional header bytes</blockquote><blockquote>bytes 8..23 form a 16-byte block</blockquote><blockquote>bytes 24..end form the encrypted payload</blockquote><h4>The first four bytes had already been validated earlier in the function by this check</h4><pre>if ( v7 &lt; 72 || *v8 != 78 || v8[1] != 86 || v8[2] != 1 || v8[3] )</pre><h4>This corresponds to the header</h4><pre>4E 56 01 00</pre><p>So, the first part of the file is clearly metadata rather than ciphertext</p><p>At this point, the 16-byte block at offset 0x08 became the natural IV candidate, while the bytes starting at offset 0x18 became the ciphertext candidate</p><p>This also makes sense from a cryptographic perspective</p><blockquote>the recovered key is 16 bytes long, which matches AES-128</blockquote><blockquote>the block at offset 0x08 is also 16 bytes long, which fits a standard AES IV</blockquote><blockquote>the encrypted payload length is <strong>72–24 = 48 bytes</strong>, which is exactly 3 AES blocks</blockquote><p>That is a strong structural hint that the asset is being decrypted with AES-CBC</p><p>To verify that assumption, I extracted the bytes directly from <strong>enc_data</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/924/1*L6icWMXu6ckoAsvDGSWDzQ.png" /></figure><pre>00000000: 4e 56 01 00 0a e2 ee 9f 36 6e e5 81 85 be 69 ab<br>00000010: 84 e9 61 a6 76 1d 79 da 11 df a5 e8 12 55 8f 7d<br>00000020: 4a ac a1 ff 62 d6 26 93 e5 7f 51 5a f2 7e 50 de<br>00000030: a6 f2 fc ed 88 22 43 2e 6d 80 10 94 60 91 e5 c5<br>00000040: 52 d5 d0 76 11 de bb c6</pre><p>Using the offsets recovered from the native code, the file layout is easiest to describe in decimal</p><blockquote>Bytes 8..23 are the IV<br>Bytes 24..71 are the ciphertext</blockquote><p>If we look at the same ranges in the hex dump, they correspond to offsets 0x08..0x17 for the IV and 0x18..0x47 for the ciphertext</p><p>The 16-byte IV extracted from bytes 8..23 is</p><pre>36 6E E5 81 85 BE 69 AB 84 E9 61 A6 76 1D 79 DA</pre><p>The ciphertext extracted from bytes 24..71 is</p><pre>11 DF A5 E8 12 55 8F 7D 4A AC A1 FF 62 D6 26 93<br>E5 7F 51 5A F2 7E 50 DE A6 F2 FC ED 88 22 43 2E<br>6D 80 10 94 60 91 E5 C5 52 D5 D0 76 11 DE BB C6</pre><p>I verified this with a short Python script</p><pre>from pathlib import Path<br><br>data = Path(&quot;enc_data&quot;).read_bytes()<br><br>iv = data[8:24]<br>ct = data[24:]<br><br>print(&quot;IV =&quot;, iv.hex())<br>print(&quot;Ciphertext =&quot;, ct.hex())</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*wdYNvpMXK6KO3t8gl4PqBQ.jpeg" /></figure><p>The decrypt script to reach the flag</p><pre>from pathlib import Path<br>from Crypto.Cipher import AES<br><br>data = Path(&quot;enc_data&quot;).read_bytes()<br>key = bytes.fromhex(&quot;64697265637473797363616c6c733078&quot;)  <br>iv = data[8:24]<br>ct = data[24:]<br><br>pt = AES.new(key, AES.MODE_CBC, iv=iv).decrypt(ct)<br>print(pt)<br>print(pt[:-pt[-1]].decode())</pre><h3><strong>Final Flag</strong></h3><p>cyctf{symbols_stripped_secrets_kept}</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=49326620c26c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CyCTF Luxor Finals (reverse)]]></title>
            <link>https://medium.com/@0xMars890/cyctf-luxur-finals-reverse-7476478f8635?source=rss-7ddecf970067------2</link>
            <guid isPermaLink="false">https://medium.com/p/7476478f8635</guid>
            <category><![CDATA[cyctf-luxur-finals]]></category>
            <category><![CDATA[cyctf-26]]></category>
            <category><![CDATA[cyctf-luxur-finals-rev]]></category>
            <category><![CDATA[ast3roids]]></category>
            <dc:creator><![CDATA[0xMar$]]></dc:creator>
            <pubDate>Tue, 31 Mar 2026 02:02:42 GMT</pubDate>
            <atom:updated>2026-03-31T13:51:22.650Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>mix — Reverse Engineering [1 solve] 🔥</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/242/1*Zh_Mp9WQYTQCRJn99GJ0kw.png" /></figure><p><strong>Challenge idea</strong></p><p><strong>mix </strong>is a binary reversing challenge built around a decoy execution path<br>The visible main only prints a harmless message, while the real logic runs before main through a hidden constructor referenced in .<strong>init_array</strong></p><p>The solve path involves reversing a hex parser, understanding a validator that checks a 16-byte key, identifying an embedded key inside the data section, and reconstructing a rotate-xor routine to decrypt the final payload</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/819/1*EBiQUUPC7e1Le74ymJPFQQ.png" /><figcaption>The most useful strings were</figcaption></figure><p><strong>The Decoy Main</strong><br>The string This is a normal program leads to a tiny function at <strong>0x1120</strong><br>IDA shows that this function does nothing except load the string into rdi, call puts, and return<br>That makes it a convincing visible main, but it clearly does not contain the real challenge logic</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/268/1*GMmWxucMtFZkkbaz8eLKZQ.png" /></figure><p>To confirm that <strong>0x1120 </strong>is indeed the visible main, I checked the entry point around <strong>0x1140</strong><br>The startup code passes <strong>0x1120 </strong>to <strong>__libc_start_main</strong>, which means the normal execution path really does begin in that tiny function<br>At that point it becomes clear that the real logic must be hidden elsewhere</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/445/1*cLCByc9f1AcLjK5Ol9U_mQ.png" /><figcaption>The visible main at 0x1120 only prints a harmless message and immediately returns</figcaption></figure><p><strong>The Real Logic Before main</strong><br>The real behavior appears in <strong>0x10C0 </strong>rather than in the visible main<br>Cross references show that <strong>0x10C0 </strong>is referenced from .init_array, which means it runs as a constructor before main<br>Unlike the decoy entry point, this function reads the user input, parses the hex string, and then passes the result to the validator at <strong>0x1320</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*CPX2uoiJe76Y13MkVMl-tg.png" /><figcaption>IDA shows that 0x10C0 is referenced from .init_array, so it executes before visible main</figcaption></figure><blockquote><strong>The function at 0x14E0 is responsible for parsing the user input before it reaches the validator</strong></blockquote><pre>puts(&quot;Enter encryption key (hex format e.g., deadbeefcafebabe...):&quot;);<br>printf(&quot;&gt; &quot;);</pre><blockquote><strong>It reads the input as a string using scanf</strong></blockquote><pre>if ((unsigned int)__isoc23_scanf(&quot;%255s&quot;, s) == 1 )</pre><blockquote><strong>It checks that the input length is even which is required for a valid hex string</strong></blockquote><pre>v4 = strlen(s);<br>if ((v4 &amp; 1) == 0 )</pre><blockquote><strong>The loop then converts every two hex characters into one byte using strtol with base 16</strong></blockquote><pre>v8 = *(_WORD *)v3;<br>v11 = 0;<br>v10 = v8;<br>*v2 = __isoc23_strtol(&amp;v10, &amp;v12, 16);</pre><blockquote><strong>If parsing fails, the routine aborts and the constructor eventually reports an invalid key</strong></blockquote><pre>if (*v12 )<br>  break;<br>printf(&quot;Invalid Key!&quot;);<br>return 0;</pre><blockquote>The validator at 0x1320 works on the parsed bytes rather than on the original text input<br>At a high level, it treats the first 16 bytes as four big-endian 32-bit words and checks a set of arithmetic and bitwise relations between them</blockquote><blockquote><strong>The first check ensures that at least 16 bytes were parsed</strong></blockquote><pre>v2 = a2 &lt;= 0xF;<br>v3 = 0;<br>if ( v2 )<br>  return v3;</pre><blockquote><strong>The routine then treats the first 16 bytes as four 32-bit big endian words</strong></blockquote><pre>x0 = be32(key[0:4])<br>x1 = be32(key[4:8])<br>x2 = be32(key[8:12])<br>x3 = be32(key[12:16])</pre><blockquote><strong>The first condition checks an xor relation between the first two words</strong></blockquote><pre>((x0 ^ x1 ^ 0x80DCD95F) &amp; 0xFFFF) == 0x85D4</pre><blockquote><strong>The next check constrains the low byte of the third and fourth words</strong></blockquote><pre>if ((_BYTE)v8 + (_BYTE)v7 != 0xD7 )<br>  return v3;</pre><blockquote><strong>The final validation step combines addition masking bitwise OR and a masked low word check</strong></blockquote><pre>(((x1 + x3) &amp; 0xFD52D47A) | 0xFF000000) == 0xFF42946A<br>(x0 | x2) == 0xF9BFFE9C<br>(((x3 &amp; x1) - 0x5DCD090A) &amp; 0xFF00) == 0xF700</pre><h3><strong>Finding the Embedded Key in .data</strong></h3><p>Once the validator was clear, the next step was to inspect the data section<br>The interesting blob starts at file offset <strong>0x3060</strong>, which appears in IDA at virtual address <strong>0x4060</strong><br>This blob is <strong>0x50 </strong>bytes long, and its final <strong>0x10 </strong>bytes are located at <strong>0x40A0</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/613/1*Ssrd-q7Gp-WoQYmFDywjDw.png" /><figcaption>The interesting data blob begins at virtual address 0x4060</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*-Vlxz-lifGwqZcguQkIu5g.png" /><figcaption>The last 16 bytes of the blob are stored at 0x40A0</figcaption></figure><blockquote><strong>As a single hex string, this becomes</strong></blockquote><blockquote>f13d729c71062e1729978e005cdc90d7</blockquote><blockquote>At this point I tested whether these embedded bytes already formed a valid key<br>They did<br>All validator checks passed, which confirmed that the binary already stores the correct 16-byte key inside its own data section</blockquote><h3><strong>Understanding the Mixer at 0x1240</strong></h3><p>At this point the remaining task was to understand how the blob was transformed<br>Function 0x1240 looks much more complicated in assembly than it really is because the compiler optimized the % 3 arithmetic into several low level instructions<br>After simplifying the routine, its behavior becomes much clearer<br>It walks over the buffer byte by byte, reuses the key cyclically, and <strong>xors</strong> each byte with a rotated key byte whose rotation amount depends on<strong> i % 3</strong></p><pre>buf[i] ^= (key[i % keylen] &lt;&lt; (i % 3)) &amp; 0xff<br>buf[i] ^= key[i % keylen] &gt;&gt; (8 - (i % 3))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/784/1*WwRfonJ5sST_rrKnggC9fg.png" /><figcaption>The visible branch of 0x1240 computes key[i % keylen], derives a shift from i % 3, and xors the transformed byte into the buffer</figcaption></figure><blockquote><em>The visible branch of </em><strong>0x1240<em> </em></strong><em>computes </em><strong><em>key[i % keylen]</em></strong><em>, derives a shift from </em><strong><em>i % 3</em></strong><em>, and </em><strong><em>xors </em></strong><em>the transformed byte into the buffer<br>The second branch at </em><strong><em>loc_12C9 </em></strong><em>handles the complementary right-shift case<br>Together both branches reduce to the cleaner high-level form used in the solver</em></blockquote><p><strong>Early exit</strong></p><blockquote>This block prepares the routine before the loop begins<br>If the buffer length is zero the function returns immediately<br>After that it saves the main arguments into working registers<br><strong>r9</strong> becomes the buffer pointer<br><strong>r10 </strong>stores the buffer length<br><strong>r11 </strong>stores the key pointer<br>and <strong>rdi</strong> is used as the key length<br>The loop counter is then initialized in <strong>rsi</strong></blockquote><pre>test    rsi, rsi<br>jz      locret_1310<br>mov     r9, rdi<br>mov     r10, rsi<br>mov     r11, rdx<br>mov     rbx, 0AAAAAAAAAAAAAAABh<br>mov     rdi, rcx<br>xor     esi, esi</pre><p><strong>Branch selection</strong></p><blockquote>The routine splits into two branches here<br>The branch visible in the screenshot is the left shift case<br>The second branch at <strong>loc_12C9 </strong>handles the complementary right shift case</blockquote><pre>cmp     r8d, 1<br>jz      short loc_12C9</pre><p><strong>Computing the key index</strong></p><blockquote>This block computes <strong>i % keylen</strong><br>On x86 64 the div instruction uses<strong> RDX:RAX</strong> implicitly as the dividend and stores the remainder in <strong>RDX</strong><br>That remainder is the index of the current key byte</blockquote><pre>mov     rax, rsi<br>xor     edx, edx<br>mov     rcx, rsi<br>div     rdi</pre><p><strong>Loading the key byte</strong></p><blockquote>Once the remainder has been computed the routine loads the current key byte from <strong>key[i % keylen]</strong><br>At this point <strong>r8d </strong>holds the byte that will be transformed and applied to the buffer</blockquote><pre>mov     rax, rsi<br>movzx   r8d, byte ptr [r11+rdx]</pre><p><strong>Deriving the shift amount</strong></p><blockquote>This sequence is the compiler generated way of computing <strong>i % 3</strong><br>Instead of using a <strong>direct division by 3</strong>, the compiler uses the <strong>magic constant 0xAAAAAAAAAAAAAAAB </strong>and a few arithmetic steps to derive the same remainder more efficiently<br>By the end of this block <strong>rcx </strong>contains a value equivalent to <strong>i % 3</strong></blockquote><pre>mul     rbx<br>mov     rax, rdx<br>and     rdx, 0FFFFFFFFFFFFFFFEh<br>shr     rax, 1<br>add     rdx, rax<br>sub     rcx, rdx</pre><p><strong>Applying the transformed key byte</strong></p><blockquote>Now the loaded key byte is shifted left by the amount stored in <strong>cl</strong><br>Since <strong>cl</strong> comes from the previous block this means the shift amount depends on <br><strong>i % 3</strong> The transformed byte is then <strong>xored </strong>into the current buffer position</blockquote><pre>shl     r8d, cl<br>xor     [r9+rsi], r8b</pre><p>This branch therefore corresponds to</p><pre>buf[i] ^= (key[i % keylen] &lt;&lt; (i % 3)) &amp; 0xff</pre><p><strong>Looping over the buffer</strong></p><blockquote>After processing one byte, the function increments the position and continues until the entire buffer has been transformed</blockquote><pre>add     rsi, 1<br>cmp     r10, rsi<br>jnz     short loc_1280</pre><p><strong>Decrypting the Payload</strong></p><p>At this point the blob can be split into two parts</p><p>ciphertext = first 0x40 bytes<br>key = last 0x10 bytes</p><p>Using the embedded key f13d729c71062e1729978e005cdc90d7</p><p>Using the embedded key and the recovered rotate-xor logic, the decrypted plaintext immediately began with CyCTF{</p><p><strong>Solver</strong></p><blockquote>To verify the result cleanly, I wrapped the extracted logic in a short Python solver<br>The script reads the blob from the binary, validates the embedded key using the same constraints recovered from 0x1320, applies the rotate-xor transformation, and extracts the flag from the decrypted plaintext</blockquote><pre>from pathlib import Path<br><br><br>BINARY_PATH = Path(__file__).with_name(&quot;mix&quot;)<br>BLOB_OFFSET = 0x3060<br>BLOB_SIZE = 0x50<br>KEY_SIZE = 16<br><br><br>def u32be(chunk: bytes) -&gt; int:<br>    return int.from_bytes(chunk, &quot;big&quot;)<br><br><br>def validate_key(key: bytes) -&gt; bool:<br>    if len(key) != KEY_SIZE:<br>        return False<br><br>    a = u32be(key[0:4])<br>    d = u32be(key[4:8])<br>    r8 = u32be(key[8:12])<br>    c = u32be(key[12:16])<br><br>    return (<br>        ((a ^ d ^ 0x80DCD95F) &amp; 0xFFFF) == 0x85D4<br>        and ((c + r8 + 0x4A94EB98) &amp; 0xFF) == 0x6F<br>        and ((((d + c) &amp; 0xFD52D47A) | 0xFF000000) == 0xFF42946A)<br>        and ((a | r8) == 0xF9BFFE9C)<br>        and ((((c &amp; d) - 0x5DCD090A) &amp; 0xFF00) == 0xF700)<br>    )<br><br><br>def rol8(value: int, shift: int) -&gt; int:<br>    if shift == 0:<br>        return value<br>    return ((value &lt;&lt; shift) &amp; 0xFF) | (value &gt;&gt; (8 - shift))<br><br><br>def mix_rol(ciphertext: bytes, key: bytes) -&gt; bytes:<br>    out = bytearray(ciphertext)<br>    for i in range(len(out)):<br>        out[i] ^= rol8(key[i % len(key)], i % 3)<br>    return bytes(out)<br><br><br>def main() -&gt; None:<br>    data = BINARY_PATH.read_bytes()<br>    blob = data[BLOB_OFFSET:BLOB_OFFSET + BLOB_SIZE]<br><br>    key = blob[-KEY_SIZE:]<br>    if not validate_key(key):<br>        raise SystemExit(&quot;embedded key did not pass validation&quot;)<br><br>    plaintext = mix_rol(blob[:-KEY_SIZE], key)<br><br>    try:<br>        start = plaintext.index(b&quot;CyCTF{&quot;)<br>        end = plaintext.index(b&quot;}&quot;, start) + 1<br>    except ValueError as exc:<br>        raise SystemExit(f&quot;flag not found in decrypted payload: {plaintext!r}&quot;) from exc<br><br>    flag = plaintext[start:end].decode(&quot;ascii&quot;)<br>    print(f&quot;key:  {key.hex()}&quot;)<br>    print(f&quot;flag: {flag}&quot;)<br><br><br>if __name__ == &quot;__main__&quot;:<br>    main()</pre><p><strong>Final Flag</strong></p><p>CyCTF{5d6c82de7bef5d92fd7ad7c2e2fcd222eeb674ecc9220d24031c4d5}</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7476478f8635" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>