<?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 Felipe Cavalcanti on Medium]]></title>
        <description><![CDATA[Stories by Felipe Cavalcanti on Medium]]></description>
        <link>https://medium.com/@felipejfc?source=rss-45753e521624------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*iVF6eOpnHQMuUSdElaPxxA.png</url>
            <title>Stories by Felipe Cavalcanti on Medium</title>
            <link>https://medium.com/@felipejfc?source=rss-45753e521624------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 04:14:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@felipejfc/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[Reverse Engineering a Cocos2dx-JS Game]]></title>
            <link>https://felipejfc.medium.com/reverse-engineering-a-cocos2dx-js-game-6cecc1c08f28?source=rss-45753e521624------2</link>
            <guid isPermaLink="false">https://medium.com/p/6cecc1c08f28</guid>
            <category><![CDATA[cocos2dx]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[games]]></category>
            <category><![CDATA[hacking]]></category>
            <category><![CDATA[reverse-engineering]]></category>
            <dc:creator><![CDATA[Felipe Cavalcanti]]></dc:creator>
            <pubDate>Mon, 17 Feb 2025 01:36:24 GMT</pubDate>
            <atom:updated>2025-02-17T16:15:51.336Z</atom:updated>
            <content:encoded><![CDATA[<p>Reverse engineering is often seen as a dark art, reserved for hackers and security researchers. But for me, it’s a hobby — a way to satisfy my curiosity, learn new techniques, and improve my own software by understanding how others protect theirs. Recently, I embarked on a quest to reverse engineer a Cocos2d-x JS game. This article will walk you through my process, the tools I used, and the lessons I learned along the way.</p><h3>The Challenge: A Non-Conventional Cocos2d-x JS Game</h3><p>The game in question, which I will not name not to expose it, was built using a custom version of Cocos2d-x JS, a popular framework for mobile game development. Unlike standard builds, this game didn’t facilitate discovery of the encryption key, several articles pointed out that the key could be found by either looking for a certain string in the libcocos2d.so lib using a hex editor, or disassembling it and then looking for the xxtea_decrypt method:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Kmyj1exX6GADUJfR" /></figure><p>In this case however, nor the key was to be found near the string pointed by the tutorials, nor we could look for this method by name as names were mangled. So what I knew at that point:</p><ol><li><strong>Stripped or moved Decryption Key</strong>: The decryption key for the JS files was removed from the code or moved to an unknown location, making it super hard to extract using conventional methods.</li><li><strong>Mangled Method Names</strong>: All method names were obfuscated, rendering online guides and tools useless.</li></ol><p>This made the game a perfect candidate for reverse engineering, as it presented unique challenges that required a combination of static and dynamic analysis.</p><h3>Tools of the Trade</h3><p>To tackle this challenge, I used a variety of tools:</p><ul><li><a href="https://github.com/frida/frida"><strong>Frida</strong></a>: For runtime hooking and dynamic analysis.</li><li><a href="https://hex-rays.com/ida-pro"><strong>IDA Pro</strong></a>: For disassembling the binary and understanding the code flow. <a href="https://github.com/NationalSecurityAgency/ghidra"><strong>Ghidra</strong></a> can also be used as a free open source alternative.</li><li><a href="https://apktool.org/"><strong>APKTool</strong></a>: To unpack and repackage the game’s APK.</li><li><strong>Android Emulator</strong>: For testing and debugging. I eventually switched to a iOS due to anti-debugging protections on Android.</li><li><strong>Jailbroken iPad M1:</strong> For dynamic analysis as well. I’ve used <a href="https://ellekit.space/dopamine/">Domanine</a> as the JB method. For people with ARM (m1/2/3..) Macs there’s also a flashy method to run iOS apps on it that can eliminate the need for a jailbroken device, but describing the steps would require an article of its own: <a href="https://jhftss.github.io/Debug-any-iOS-Apps-on-M1-Mac/">reference 1</a>, <a href="https://googleprojectzero.blogspot.com/2021/05/fuzzing-ios-code-on-macos-at-native.html">reference 2</a>.</li><li><strong>Custom Scripts</strong>: I used <a href="https://github.com/TeamRocketIst/cocos2d-dec">cocos2d-dec</a> to decrypt the JS files and wrote custom Python scripts to handle image decryption.</li></ul><p>My setup was a Mac with an M3 processor, but the process is platform-agnostic and can be replicated on Windows or Linux.</p><h3>The Process: From Static Analysis to Runtime Hooking</h3><h3>Step 1: Understanding the Encryption</h3><p>Cocos2d-x games typically compress and encrypt JS files using the XXTEA algorithm. However, in this case, the decryption key was either not stored in the binary, or moved to an unknown location, and all method names were mangled. This meant I had to find the key dynamically.</p><h3>Step 2: Static Analysis with IDA Pro</h3><p>I started by loading the cocos2d generated binary (<strong>libcocos2djs.so</strong> if Android, or <strong>app binary</strong> if iOS) into IDA Pro and searching for obvious strings like “encrypt” and “decrypt.” One string stood out: “can’t decrypt code ...”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*esxwjD3BoLqzHEzOkRSrng.png" /></figure><p>By tracing references to this string, I found an if block that I assumed was working with decryption result. The method preceding this block was likely the function I was looking for, with arguments for the encrypted content and the key.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WsWPDXwvZKU4wjiJgep3BA.png" /><figcaption>Method that referenced the failed to decrypt string, decompiled using IDA Pro.</figcaption></figure><p>If you take a look at the first image in the article again, you’ll see that this function call seems to match exactly the signature of xxtea_decrypt method that we found online, this plus the fact that we see that error as a possible result of its return made me very confident that this was the function I was looking for, and if I were right, the key would be passed as its third argument.</p><h3>Step 3: Dynamic Analysis with Frida</h3><p>With this hypothesis, I used Frida to hook the suspected decryption method at runtime.</p><p>The process to hook the function with Frida was the following, in IDA the offset of the function, relative to the module’s (libcocos2djs.so) base was 0x06D84AC</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iqmGlo50Cnxu0XIPHBTgtg.png" /></figure><p>So what I needed to do was to run the game with Frida with a hook file attached, that would intercept this function call and print the key. Which can be achieved with the following code:</p><pre>Java.perform(function() {<br>    // Function to find and hook the module<br>    function hookModule() {<br>        // Get the base address of libcocos2djs.so<br>        var baseAddr = Module.findBaseAddress(&#39;libcocos2djs.so&#39;);<br>        if (baseAddr === null) {<br>            console.log(&#39;Module not loaded yet, retrying...&#39;);<br>            return false;<br>        }<br><br>        // Calculate the target address by adding the offset to base address<br>        var targetAddr = baseAddr.add(0x6D84AC);<br><br>        Interceptor.attach(targetAddr, {<br>            onEnter: function(args) {<br>                // args[0] = a1<br>                // args[1] = a2<br>                // args[2] = a3 (string pointer)<br>                // args[3] = a4<br>                // args[4] = a5<br><br>                // Read the string from the pointer (a3)<br>                var length = args[3].toInt32();<br>                var stringPtr = args[2];<br>                <br>                // Read the bytes and create a string<br>                var bytes = Memory.readByteArray(stringPtr, length);<br>                // Convert ArrayBuffer to regular array before using map<br>                var byteArray = Array.from(new Uint8Array(bytes));<br>                console.log(&#39;String content (hex):&#39;, byteArray.map(b =&gt; b.toString(16).padStart(2, &#39;0&#39;)).join(&#39; &#39;));<br>                <br>                // Try to read as UTF-8 string<br>                try {<br>                    var str = Memory.readUtf8String(stringPtr, length);<br>                    console.log(&#39;String content (UTF-8):&#39;, str);<br>                } catch (e) {<br>                    console.log(&#39;Failed to read as UTF-8 string:&#39;, e);<br>                }<br>            }<br>        });<br>        <br>        console.log(&#39;Hook installed at offset 0x6D84AC&#39;);<br>        return true;<br>    }<br><br>    // Try to hook immediately<br>    if (!hookModule()) {<br>        // If failed, set up an interval to keep trying<br>        var intervalId = setInterval(function() {<br>            if (hookModule()) {<br>                clearInterval(intervalId);<br>            }<br>        }, 50);<br>    }<br>});</pre><p>Run the game with Frida, passing the hook file so it runs right when the app starts:</p><pre>frida -U -l hook.js -f com.the.game</pre><p>And… bingo!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Uyfp-bCTc_ZHST77mJMogA.png" /></figure><p>By printing the method’s arguments, I was able to capture the decryption key. This was a breakthrough moment — I now had the key to decrypt the JS files.</p><h3>Step 4: Decrypting the JS Files</h3><p>Using the <a href="https://github.com/TeamRocketIst/cocos2d-dec">cocos2d-dec</a> tool, I decrypted the JS files and dumped their plaintext contents. This gave me access to the game’s logic and allowed me to understand how it worked under the hood.</p><p>Usage was pretty straight forward, in the case of this game, xxtea had an additional xor step, so I also needed to use -xs and -xk arguments in the tool, passing to it the magic header of the xxtea-encrypted files, which you can identify by opening them. It will be basically the initial content and the only readable thing in the file.</p><p>I pointed the tool at the path in which apktool decompressed the apk file, it will recurse finding .jsc files and decrypting them, creating the .js file in the same folder.</p><pre>python3 js_xxtea_decrypt.py -k xxxxxxxx-8161-xx -xs &quot;$(echo -en &#39;xxxxxx&#39; | xxd -p)&quot; -xk xxxxxx  ~/dev/the-game/game-root-folder</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4Xjck1IOQm3r4hUZ8yXIqQ.png" /><figcaption>File before and after decryption</figcaption></figure><h3>Step 5: Decrypting the PNG Assets</h3><p>As an additional step, I also wanted to dump all of the game’s images, and they were also encrypted using XXTEA, but with a twist: the key was dynamically generated based on the first 16 bytes of the file name. The process to discover that was the following, first I tried to find methods with obvious names containing “png” or “image” in it like “decrypt_image”, “decrypt_png” etc, but didn’t find any, they were also probably mangled such as the other ones. I then tried an approach similar to the one that helped with finding the method that decrypted files, but couldn’t find suggestive strings also, so I changed strategy and decided to work backwards…</p><p>I knew that for the images to be rendered, they first need to be decrypted, and for this to happen, they first need to be loaded from the disk in their encrypted format, so I’ve put a breakpoint in fopen function using IDA Pro and traced the call stack to find the decryption routine. This strategy is very common in RE, we work with the assumption that an interesting flow will need to use a standard library API and put a breakpoint there to find caller method. This was the method that called fopen and my findings:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5z-Vi0pLJn9vaiiK9n-jcg.png" /><figcaption>routine that contained call to fopen that I found tracing back the call</figcaption></figure><p>Ok, there’s some interesting things there, first the fopen call that I definitely only found because I traced back from my break point given its not referenced directly, then on the bottom we can see a call to <strong>sub_6D84AC, </strong>bingo!</p><p>This is our xxtea_decryt function, same as we found while looking how to decrypt js files, however, we can see there’s some logic around defining the key (3rd) argument in the call, <strong>v16.</strong> The logic is simple, it’s basically checking if the length of the name of the file being decrypted is &lt; 17, if it is, it uses the default key which is in plain text there for us to see, otherwise, it uses another value, at that point I basically copy pasted the whole method in Cursor and asked my friend Claude to tell me what this value is and it was spot on: it’s the first 16 chars of the name of the file.</p><p>With the help of good old Claude again, I wrote a Python script to replicate the same decryption logic and successfully decrypted all game’s PNG files.</p><h3>Challenges and Breakthroughs</h3><p>Setting up the RE environment itself is a challenge. Jailbreaking an iOS device is no easy task these days, one needs to have some specific device on some specific range of iOS versions. Also, anti-debugging protections can made it difficult to attach a debugger, I couldn’t do it in a rooted Android environment, so I switched to a jailbroken M1 iPad, which worked seamlessly. Another challenge is understanding the assembly code, but this part has come a long way since when I started reverse engineering games to hack them around 2007, tools like IDA Pro and LLMs made this process much easier.</p><p>The breakthrough came when I successfully hooked the decryption method with Frida and captured the key. This opened the door to decrypting both the JS files and the PNG assets.</p><h3>Results: A Fully Decrypted Game</h3><p>By the end of the process, I had access to all the game’s decrypted JS files and PNG assets. In a second moment, I also wrote a Frida hook to the “evalString” function in libcocos2djs.so that allowed me to modify the game’s JS code in my local computer and replace what was loaded in runtime, making it easy to debug the non-native part of the game.</p><h3>Ethical Considerations</h3><p>It’s important to note that this project was solely for educational purposes. Reverse engineering can be a powerful tool for learning, but it should never be used to steal or copy someone else’s work. Instead, it’s an opportunity to learn from others and improve your own software.</p><h3>Lessons Learned</h3><ol><li><strong>Combining Static and Dynamic Analysis</strong>: Static analysis alone wasn’t enough — I needed runtime hooking to capture the decryption key.</li><li><strong>The Power of Frida</strong>: Frida is an incredibly versatile tool for dynamic analysis and runtime manipulation.</li><li><strong>LLMs as Reverse Engineering Assistants</strong>: Tools like Claude 3.5 Sonnet and DeepSeek R1 made it easier to understand assembly code and write custom hook scripts.</li><li><strong>Anti-Debugging Protections</strong>: Be prepared to find these, overcoming them can be a BIG challenge sometimes, but if you’re lucky, something as simple as switch platforms or devices can help you with that.</li></ol><h3>Empowering Others</h3><p>My goal with this article is to empower you to tackle similar challenges. Whether you’re a reverse engineering enthusiast, a game developer, or a security researcher, the techniques and tools I’ve shared can help you achieve your goals. Remember, the key to success is persistence, curiosity, and a willingness to learn.</p><p>If you’re interested in reverse engineering, I encourage you to dive in and start experimenting. Use the tools and techniques I’ve shared, and look for more resources online, a tip is that there’s a LOT of good information about this subject in Chinese websites, so Baidu + Google translator can be a big ally. And most importantly, always approach reverse engineering with respect for others’ work and a commitment to ethical practices.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6cecc1c08f28" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Power Usage Comparison of Communication Protocols on the ESP32: Wi-Fi, Zigbee, and ESP NOW]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://felipejfc.medium.com/power-usage-comparison-of-communication-protocols-on-the-esp32-wi-fi-zigbee-and-esp-now-247a3c720355?source=rss-45753e521624------2"><img src="https://cdn-images-1.medium.com/max/1245/1*Ya5mvBDLzg06je_-_uwB7Q.png" width="1245"></a></p><p class="medium-feed-snippet">Introduction:
In the realm of IoT devices, maximizing battery life is paramount. Choosing the right communication protocol can&#x2026;</p><p class="medium-feed-link"><a href="https://felipejfc.medium.com/power-usage-comparison-of-communication-protocols-on-the-esp32-wi-fi-zigbee-and-esp-now-247a3c720355?source=rss-45753e521624------2">Continue reading on Medium »</a></p></div>]]></description>
            <link>https://felipejfc.medium.com/power-usage-comparison-of-communication-protocols-on-the-esp32-wi-fi-zigbee-and-esp-now-247a3c720355?source=rss-45753e521624------2</link>
            <guid isPermaLink="false">https://medium.com/p/247a3c720355</guid>
            <category><![CDATA[zigbee]]></category>
            <category><![CDATA[battery]]></category>
            <category><![CDATA[electronics]]></category>
            <category><![CDATA[esp32]]></category>
            <category><![CDATA[iot]]></category>
            <dc:creator><![CDATA[Felipe Cavalcanti]]></dc:creator>
            <pubDate>Thu, 30 May 2024 21:01:41 GMT</pubDate>
            <atom:updated>2024-05-30T21:03:57.643Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[HDMI Forced Output on Linux Systems: A Guide to Custom EDID without physical display attached]]></title>
            <link>https://felipejfc.medium.com/hdmi-forced-output-on-linux-systems-a-guide-to-custom-edid-without-physical-display-attached-8cde490adb89?source=rss-45753e521624------2</link>
            <guid isPermaLink="false">https://medium.com/p/8cde490adb89</guid>
            <category><![CDATA[headless-server]]></category>
            <category><![CDATA[remote-desktop]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[stream]]></category>
            <category><![CDATA[servers]]></category>
            <dc:creator><![CDATA[Felipe Cavalcanti]]></dc:creator>
            <pubDate>Wed, 08 May 2024 22:15:54 GMT</pubDate>
            <atom:updated>2024-05-08T22:15:54.409Z</atom:updated>
            <content:encoded><![CDATA[<p>Have you ever wanted to access a headless server remotely, only to find that without a physical display attached, Linux systems won’t initialize GPU display capabilities? Fear not! With the power of custom EDID, you can force HDMI output and unlock a world of remote desktop possibilities.</p><p><strong>Understanding the Challenge</strong></p><p>By default, Linux systems refrain from initializing GPU display capabilities when no physical display is attached. This limitation prevents users from utilizing remote desktop software to access headless machines with video output.</p><p><strong>Enter Custom EDID</strong></p><p>Extended Display Identification Data (EDID) plays a crucial role in this scenario. Without a valid EDID, Linux will default to a low resolution upon boot, rendering remote access impractical. However, with a custom EDID, users can specify desired resolutions and refresh rates, such as 4K@60fps or 2K@120fps, ensuring optimal performance even without a physical display.</p><p><strong>Practical Applications</strong></p><p>The applications of this capability are myriad, particularly for headless servers. Imagine being able to access your server remotely via applications like Moonlight, Sunshine Streaming, or VNC, regardless of whether it has a physical monitor attached.</p><p><strong>Implementation Guide</strong></p><p>Here’s how it can be achieved on your Linux system:</p><ol><li>Obtain a valid EDID file. This can be achieved by connecting a physical HDMI display to the computer and copying the file from /sys/class/drm/*/edid. Alternatively, other methods for obtaining an EDID file can be found online.</li><li>Place the EDID file (custom_edid.bin, for example) inside /usr/lib/firmware/edid.</li><li>Edit the kernel command line to include the following options:</li></ol><pre>video=HDMI-A-1:D drm.edid_firmware=VGA-1:edid/custom_edid.bin</pre><p>The first option forces display on HDMI-A even if there’s no physical display attached, while the second specifies the EDID file to be used by the system.</p><p>If you’re using <a href="https://wiki.archlinux.org/title/Kernel_mode_setting#Early_KMS_start">Early KMS</a>, ensure the EDID file is included in your initramfs image. For example, with Dracut, you can create the following override</p><pre>$ cat /etc/dracut.conf.d/99-local.conf<br>install_items+=&quot; /usr/lib/firmware/edid/custom_edid.bin&quot;</pre><p>Rebuild the initramfs and verify the inclusion of the EDID file with lsinitrd.</p><p>Reboot your system, and enjoy the benefits of forced HDMI output with custom EDID!</p><p><strong>Conclusion</strong></p><p>With the ability to force HDMI output and specify custom EDID, Linux users can seamlessly access headless servers and other remote systems, opening up many possibilities for remote desktop applications. Say goodbye to the need for physical HDMI displays or dummy plugs, and embrace the flexibility and convenience of remote access on Linux systems.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8cde490adb89" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Running OpenWRT on the Odroid N2 —  And pretty much any other unsupported ARM device that can run…]]></title>
            <link>https://felipejfc.medium.com/running-openwrt-on-the-odroid-n2-and-pretty-much-any-other-unsupported-arm-device-that-can-run-e8174008df04?source=rss-45753e521624------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8174008df04</guid>
            <category><![CDATA[router]]></category>
            <category><![CDATA[arm]]></category>
            <category><![CDATA[networking]]></category>
            <category><![CDATA[odroid]]></category>
            <category><![CDATA[openwrt]]></category>
            <dc:creator><![CDATA[Felipe Cavalcanti]]></dc:creator>
            <pubDate>Sun, 22 Nov 2020 22:56:32 GMT</pubDate>
            <atom:updated>2022-10-26T01:37:24.541Z</atom:updated>
            <content:encoded><![CDATA[<h3>Running OpenWRT on the Odroid N2 — And pretty much any other unsupported ARM device that can run Linux</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*np5mf9CZ4tKzPmB6N7eHMQ.png" /><figcaption>Luci running on the Odroid N2 with 5.9.6 kernel</figcaption></figure><h3><strong>Why?</strong></h3><p>People usually run OpenWRT in home routers, however, it’s not every router that is supported by it, also, the majority of the routers destined for home usage don’t have potent hardware, so they are not a good fit for processing-intensive tasks, like encryption of a big amount of data if you intend to put all your network traffic behind a VPN running on it for example.</p><p>Routing your network with more robust hardware, like the Odroid N2, you will have sufficient power for putting all your network traffic behind a VPN while still maintaining your download/upload speed to the maximum that is provided by your ISP (unless you have insanely fast internet, hehe).</p><p>Say that, like me, you have a good home router with great Wi-Fi capabilities but that lacks OpenWRT support. You can, for example, run OpenWRT into your SBC and then turn your router into a Dumb AP, leveraging its gigabit ports and also its Wireless Radio, so you get the best of both worlds. You have a potent router running OpenWRT while also maintaining your great Wireless capabilities.</p><h3><strong>Requirements</strong></h3><ul><li>A working Linux distro for the ARM device with kernel ≥ 4.14</li><li>If you want to do actual routing you will need 2 network interfaces, e.g. 2 ethernet ports or 1 ethernet port + 1 wifi device. I use 2 ethernet ports, the one embedded in the device and a USB3-&gt;1GB Ethernet adapter from Ugreen.</li><li>Some Linux knowledge</li></ul><h3><strong>How?</strong></h3><p><em>TLDR; get the boot partition, kernel modules, and firmware from the working Linux distro and merge it with OpenWrt&#39;s armvirt rootfs, either 64 or 32 bits depending on your device.</em></p><p>The process consists of getting the kernel modules, boot partition and firmware from a working Linux distro for your Arm device and merging it with openwrt’s armvirt rootfs and it should work just fine. I’ve tested it with both Linux kernel 4.9 and also the newer kernel 5.9, they worked just fine.</p><p>I’ll be using Armbian’s last distro containing 5.9 kernel, which can be downloaded from Armbian’s page <a href="https://www.armbian.com/odroid-n2/">here</a>.</p><p>Now, Let’s begin!</p><h4>1. Flash the working Linux distro image to an SD card or eMMC module</h4><p>Armbian’s tutorial suggests that we use <a href="https://www.balena.io/etcher/">etcher</a> for flashing the image to the sd/eMMC, I’ve used it a couple of times now and it works very well, so I suggest you do the same. Verify that the flashed module boots on your device, if so, continue to the next step, if not, continue trying step 1 until you manage to get a bootable device.</p><h4>2. Backup boot, Linux modules, and firmware</h4><p>In this section, this cd /path/to/rootfs means entering the SD/eMMC root, the one you just flashed with the Linux distro.</p><p>To start, let’s organize the Linux modules. Generally, the /lib/modulesdirectory structure of a Linux distribution looks like this:</p><pre>/lib/modules/&lt;kernel-version&gt;/<br>├── kernel<br>│   ├── arch<br>│   ├── crypto<br>│   ├── drivers<br>│   ├── fs<br>│   ├── lib<br>│   ├── mm<br>│   ├── net<br>│   ├── security<br>│   ├── sound<br>│   └── virt<br>├── modules.alias<br>├── modules.alias.bin<br>├── modules.builtin<br>├── modules.builtin.bin<br>├── modules.dep<br>├── modules.dep.bin<br>├── modules.devname<br>├── modules.order<br>├── modules.softdep<br>├── modules.symbols<br>└── modules.symbols.bin</pre><p>In contrast, OpenWrt expects that we store all the modules directly into the /lib/modules/&lt;kernel-version&gt; directory, so let’s move them manually with the following commands:</p><pre># you will probably need root going forward<br>cd /path/to/rootfs<br><br># enter the modules dir<br>cd ./lib/modules/&lt;kernel-version&gt;/<br><br># exclude everything apart from the kernel directory<br>sudo rm -f * 2&gt;/dev/null<br><br># recursivelly move all modules to the current directory<br>sudo mv $(find kernel -type f) .<br><br># remove the kernel dir<br>sudo rm -r kernel</pre><p>Now that we dealt with the modules, lets backup the stuff we will need before deleting everything and extracting OpenWrt’s rootfs.</p><p>In armbian’s image, there’s only one partition and boot is a directory inside it, for some other distros you’ll have a boot partition instead, which is fine, just don’t touch it as you’ll be extracting the rootfs to the other partition.</p><p>We will be creating a backup dir and then copying all the necessary stuff there with the following commands:</p><pre># create the backup dir<br>mkdir -p /tmp/backup<br><br># enter the rootfs path in your sd or emmc<br>cd /path/to/rootfs<br><br># copy boot dir to backup dir, not necessary if boot partition<br>cp -ra ./boot /tmp/backup/boot<br><br># backup modules and firmware<br>cp -ra ./lib/modules /tmp/backup/<br>cp -ra ./lib/firmware /tmp/backup/</pre><pre># backup run dir (needed in latest versions of armbian+openwrt I tried)</pre><pre>cp -ra ./run /tmp/backup/</pre><pre># remove everything since we backed-up the necessary already<br>sudo rm -rf *<br><br># extract openwrt&#39;s rootfs to the sd<br>sudo tar -xvf /path/to/openwrt/rootfs.tar.gz<br><br># remove openwrt&#39;s boot dir<br>sudo rm -rf ./boot<br><br># restore the backup<br>sudo mv -f /tmp/backup/boot .<br>sudo mv -f /tmp/backup/modules/&lt;kernel-version&gt; ./lib/modules<br>sudo mv -f /tmp/backup/firmware ./lib<br>sudo mv -f /tmp/backup/run .</pre><p>You must end up with the following tree:</p><pre>sd-root<br>  - boot (copied from backup)<br>  - lib<br>    - modules (copied from backup)<br>      - kernel version<br>        - bunch of .ko files (copied from working distro)<br>    - firmware (copied from backup)<br>  - ... (all the other directoried from openwrt rootfs)</pre><p>By now you should already have a bootable OpenWrt distro for your device, however, we need to configure networking first so that you’re able to access it and configure it.</p><h4>3. Configure network</h4><p>Edit or create the file /etc/config/network and insert the following:</p><p>For finishing up configuring your device, you now have 2 options, configuring a new LAN already, or, using DHCP for connecting the odroid into an existing router that has DHCP enabled.</p><p>Option 1: New Lan (This is my preferred one, I’ll tell you why soon)</p><pre>config interface &#39;lan&#39;<br>        option type &#39;bridge&#39;<br>        option ifname &#39;eth0&#39;<br>        option proto &#39;static&#39;<br>        option ipaddr &#39;10.0.0.1&#39;<br>        option netmask &#39;255.255.255.0&#39;</pre><p>Option 2: DHCP</p><pre>config interface &#39;lan&#39;<br>        option ifname &#39;eth0&#39;<br>        option proto &#39;dhcp&#39;</pre><p>The reason I prefer creating the new LAN is because of predictability, I already know the IP my device will be listing to, I don’t need to go to the issuing DHCP device and figure out what IP was assigned to it.</p><p>You may have noticed that I configured only one LANinterface, the embedded one, we still need to configure the second one, we will make it work soon, for now, bear with me.</p><h4>4. Booting</h4><p>You may now try to boot your device, you will have no image display but you must have networking working by now.</p><p>If you did the new LAN option, connect a PC directly to it using an ethernet cable and configure the PC IP settings manually as follows:</p><pre>ip 10.0.0.2<br>netmask 255.255.255.0<br>gateway 10.0.0.1</pre><p>You should be able to access your device now on the IP addr 10.0.0.1.</p><p>If you did the DHCP option you need to figure out what IP was issued to your device.</p><p>Either way, you then should be able to either access Luci on a web browser or ssh to it using root user.</p><p>If you have used DHCP you should have internet access working already, try to opkg update. If it’s not possible to access the internet, try to ping something, say 8.8.8.8 (Google’s DNS), if it’s working you have working connectivity so it might have failed because you have wrong DNS config, check /etc/resolv.conf file.</p><p>If you did Lan option, let’s move forward now and configure your second eth interface as your Wan interface, move to the next section.</p><h4>5. Talking about kmods and making final adjustments</h4><p>If you have experience with openwrt you know that a kmod is a kernel module and that they’re usually installed using opkg, we need to load one for example to make our USB Ethernet adapter usable.</p><p>Here I have 2 news for you, a good one and a bad one.</p><p>The bad one is, because you’re not using OpenWRT’s supported kernel, you will not be able to install kmod packages using the default OpenWRT opkg repository. You might now be asking yourself what was the point of all this if you would not be able to use kmods, again, bear with me, I would not make you go through all that if using kernel modules would not be possible, continue reading.</p><p>The good news is you are able to use all the kernel modules that came with the Linux distribution you based your build in, and not only it probably contain 98% of the kmods available to be downloaded through OpenWRT repository, it also contain much more, also, please note that apart from kmods, every other package in the official OpenWRT repo is still usable! You can install and start using them!</p><p>Let’s make your second ethernet usable (if it’s not already). Here, I’m using a Gigabit Ethernet to USB3 adapter, the ax88179_178a one. I needed to load a kernel module containing it’s driver to use it.</p><p>How do you do it? As simple as that:</p><pre>modprobe ax88179_178a</pre><p>I’m able to do so because inside my /lib/modules/&lt;kernel-version&gt; directory, there’s a kernel module named ax88179_178a.ko.</p><p>Let’s make the module load in boot time, one way to do it is to create a file inside the directory /etc/modules.d/, for example, /etc/modules.d/ax88179 in the content you put the name of the kernel module, without the .ko extension. In my case, only: ax88179_178a.</p><p>So I end up with a file, /etc/modules.d/ax88179 with the content:</p><pre>ax88179_178a</pre><p>With the kernel module loaded, I will now configure my eth1, then I end up with the following in /etc/config/network:</p><pre>config interface &#39;lan&#39;<br> option type &#39;bridge&#39;<br> option ifname &#39;eth0&#39;<br> option proto &#39;static&#39;<br> option ipaddr &#39;10.0.0.1&#39;<br> option netmask &#39;255.255.255.0&#39;</pre><pre>config interface &#39;wan&#39;<br> option ifname &#39;eth1&#39;<br> option proto &#39;dhcp&#39;</pre><p>Restart network with /etc/init.d/network restart and everything should now be working! You have made your SCB into a fully working OpenWRT router!</p><h4>6. Enjoy!</h4><p>That’s it, thanks for reading! If you have any question feel free to leave it in the comments.</p><p>Best!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8174008df04" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The ultimate guide for live debugging apps on jailbroken iOS 12]]></title>
            <link>https://felipejfc.medium.com/the-ultimate-guide-for-live-debugging-apps-on-jailbroken-ios-12-4c5b48adf2fb?source=rss-45753e521624------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c5b48adf2fb</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[reverse-engineering]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[jailbreak]]></category>
            <category><![CDATA[hacking]]></category>
            <dc:creator><![CDATA[Felipe Cavalcanti]]></dc:creator>
            <pubDate>Wed, 21 Aug 2019 16:32:08 GMT</pubDate>
            <atom:updated>2025-02-17T02:00:25.309Z</atom:updated>
            <content:encoded><![CDATA[<h3>The missing guide to debug third party apps on iOS</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kwxrpyEu4nqygk2pTQUJxg.png" /></figure><p>I always liked doing reverse engineering for fun. Back in the days when I had time to play more games on my old Windows XP PC and study random stuff, I had fun using ollydbg for doing reverse engineering on games and hack them. The interest in doing that came back when my bank’s app refused to run on my jailbroken iPhone “for my own security”, other apps do the same. This was the trigger for me to want to do reverse engineering on iOS apps.</p><p>Then, some time ago I found out that it was possible to live debug apps on iOS devices as well and tried doing it some times but I always struggled to get gdb or lldb to work. There were a lot of different versions, some specifically to arm64, others to x86 and neither worked well on newer iOS versions, and ended up giving up every time. Also, even when I eventually got it to work, I only got a working debug session using gdb or lldb on my terminal. I couldn’t get a more appropriate, more visual disassembler/debugger like <a href="https://www.hex-rays.com/products/ida/">IDA Pro</a> to work.</p><p>Happily, I recently found that it’s already possible to get everything working, even a working debugging session on IDA Pro, however, finding all the information I needed to get it to work was quite a challenge. To find all the “parts” I needed to get it to work I needed to grab information from github, iOS hacking forums and even chinese websites, so I decided to document everything here, maybe it helps someone.</p><p>So, let’s start!</p><h4>You need to be jailbroken</h4><p>First things first, you will need to jailbreak your iDevice. By the time of writing there’s a working jailbreak for the latest and currently signed iOS version: 12.4. It matters to tell that this is not common and happens rarely these days. I’ll not enter into the details on how to jailbreak your iDevice but you can easily find a tutorial with a quick google search, just search for unc0ver.</p><p>Also, install some ssh server on your iDevice, like openssh for example. You will need it to run commands remotely on the device.</p><p><strong>Important:</strong> Remember to change the default ssh password for both <em>mobile</em> and <em>root</em> users the moment you install the ssh server.</p><p>For the best experience, I suggest you <a href="https://iphonedevwiki.net/index.php/SSH_Over_USB">SSH over USB</a>.</p><h4>Setup Debugserver</h4><p>Debugserver is the binary that we will use to attach to the running iOS app that you want to debug. It can be found on your mac if you have Xcode installed.</p><p>On your mac run:</p><pre>$ hdiutil attach /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport/12.1\ \(16B5059d\)/DeveloperDiskImage.dmg</pre><pre>$ cp /Volumes/DeveloperDiskImage/usr/bin/debugserver ./</pre><p>I “lipoed” debugserver because I’ve read somewhere that it was necessary, but I’m not sure.</p><pre>$ lipo -thin arm64 debugserver  -output debugserver_arm64</pre><p>Now you have a thin binary with only arm64 architecture, scp it to your iDevice.</p><p>If you’re using unc0ver jailbreak, then the author <a href="https://twitter.com/Pwn20wnd">@Pwn20wnd</a> has written a script to modify debugserver entitlements and copy it to /usr/bin, you can find it at /usr/local/bin/debugserver here is it’s content:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa5bcad2a419271b0aa172a789800d36/href">https://medium.com/media/aa5bcad2a419271b0aa172a789800d36/href</a></iframe><p>The important part in this script is the one where it uses ldid to modify the entitlements of debugserver with the ones in this file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa92a2c89dff6878bb3bb718f069c781/href">https://medium.com/media/fa92a2c89dff6878bb3bb718f069c781/href</a></iframe><p>So, let’s do this step ourselves on the debugserver_arm64 we scp’ed to the device and then move it to where the script expects it to be.</p><pre>$ ldid -S/usr/share/entitlements/debugserver.xml debugserver_arm64<br>$ mv debugserver_arm64 /usr/bin/debugserver</pre><p>Now, test it! If it worked, it should output something like that:</p><pre>$ /usr/bin/debugserver<br>debugserver-@(#)PROGRAM:LLDB  PROJECT:lldb-900.3.86<br> for arm64.<br>Usage:<br>  debugserver host:port [program-name program-arg1 program-arg2 ...]<br>  debugserver /path/file [program-name program-arg1 program-arg2 ...]<br>  debugserver host:port --attach=&lt;pid&gt;<br>  debugserver /path/file --attach=&lt;pid&gt;<br>  debugserver host:port --attach=&lt;process_name&gt;<br>  debugserver /path/file --attach=&lt;process_name&gt;</pre><p>debugserver is now ready!</p><p>If it exits with Killed:9 then you probably messed up with the modification of entitlements, try again.</p><h4>Prepare the app you want to debug</h4><p>Apps downloaded from the appstore are encrypted in your iDevice, so, before starting to debug we need to decrypt them. For that you have plenty options, I’ve read somewhere that the most famous one (Clutch) has some problems with iOS 12, I’ve tested bfdecrypt and it works fine, here’s how:</p><p>Add level3tjg’s repo on cydia and install bfdecrypt from it. Repo: <a href="https://level3tjg.github.io/"><strong>https://level3tjg.github.io/</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/1*oCdUzNpdag52-uMhcPE9zw.png" /></figure><p>After installing, you can find bfdecrypt in settings, select on it the app you want to dump and decrypt.</p><p>After selecting it, open the app, it will show a message that bfdecrypt is decrypting the app and after it’s done you can netcat it to your pc (if you choose yes in bfdecrypt popup)with nc *your_device_ip* 31336 &gt; decrypted.ipa or grab the decrypted ipa should be found at /var/mobile/Containers/Data/Application/*Your App*/Documents/ and copy it to your computer with scp.</p><p><strong>Important:</strong> You need to re-sign and <strong>reinstall the app</strong> using the decrypted ipa. You can do that entirely on your iDevice by using <em>ipapatcher </em>(available on level3tjg’s repo) to re-sign it and install it using <em>Filza</em> or just use <a href="http://www.cydiaimpactor.com/">Cydia Impactor</a> on your mac.</p><p>The app is now ready to be debugged.</p><h4>Debugging the app</h4><p>Now that you have working debugserver and your decrypted app is installed on your iDevice we should be ready to debug it, follow these steps:</p><p>On your iDevice, start the remote debugger with debugserver:</p><pre>$ debugserver localhost:1111 -a *ExecutableName*<br>-- or<br>$ debugserver localhost:1111 /var/containers/Bundle/Application/.../*AppName**.app/**AppName**</pre><p>I used localhost:1111 because I’m using iProxy to proxy 1111 port on my iDevice to 1111 port on my mac.</p><p>On your mac, connect lldb to debugserver</p><pre>$ lldb<br>$ process connect connect://127.0.0.1:1111</pre><p>The output should be something like that:</p><pre>(lldb) process connect connect://127.0.0.1:1111<br>Process 5572 stopped<br>* thread #1, queue = &#39;com.apple.main-thread&#39;, stop reason = signal SIGSTOP<br>    frame #0: 0x000000020a0ac0f4 libsystem_kernel.dylib`mach_msg_trap + 8<br>libsystem_kernel.dylib`mach_msg_trap:<br>-&gt;  0x20a0ac0f4 &lt;+8&gt;: ret</pre><pre>libsystem_kernel.dylib`mach_msg_overwrite_trap:<br>    0x20a0ac0f8 &lt;+0&gt;: mov    x16, #-0x20<br>    0x20a0ac0fc &lt;+4&gt;: svc    #0x80<br>    0x20a0ac100 &lt;+8&gt;: ret<br>Target 0: (App) stopped.<br>(lldb)</pre><p><em>Note: If your lldb shows a python traceback about weakref stuff, it is fine. This is probably due to you having python2 installed by brew. Still, if you want to get rid of it, two possible solutions are: unlinking brew’s python2 or putting the one in /usr/bin/ first on your path (e.g. export PATH=/usr/bin/:$PATH).</em></p><p>Thats it, you are ready to start debugging the app! You can read some lldb tips on lldb’s <a href="https://lldb.llvm.org/use/tutorial.html">page</a>.</p><p>If you want to setup IDA Pro continue reading the next section.</p><h4>Using IDA Pro remote iOS debugger</h4><ul><li><strong>Open the binary from the decrypted ipa on IDA</strong></li></ul><p>An ipa is just a zip file with the ipa extension, so just extract it:</p><pre>$ unzip decrypted.ipa</pre><p>Inside it’s contents, there must be a binary at Payload/*Application*.app/*Application*</p><p>The file doesn’t have an extension, so select “All Files (*)” in the “Select file to disassemble” window in IDA.</p><ul><li><strong>Activate Remote iOS Debugger</strong></li></ul><p>After IDA finishes analysing the binary, select Remote iOS Debugger in Debugger&gt;Switch Debugger menu.</p><ul><li><strong>(Optional) Setup symbols</strong></li></ul><p>IDA relies heavily on the use of symbol files in order to achieve fast and detailed debugging (as does Xcode — it usually stores symbols in ~/Library/Developer/Xcode/iOS DeviceSupport/&lt;iOS Version&gt;/Symbols when you first connect your device).</p><p>In IDA, copy this path to ‘Symbol path’ in Debugger&gt;Debugger options&gt;Set specific options:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zVRMsQYzdKz73QUa7WoDXg.png" /></figure><ul><li><strong>Connecting to the debugserver using ida’s remote ios debugger</strong></li></ul><p>Here you have 2 options, launch the app you want to debug in your iDevice and attach to it using debugserver as we done before:</p><pre>$ debugserver localhost:1111 -a *ExecutableName*</pre><p>After that you can go ahead and attach to the process using IDA:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ENwxuuL-PyU9Tm8Z8Fa_eg.png" /></figure><p>If you have attached the debugserver to an already running process on you iDevice, then it doesn’t really matter what you put at “Application” and “Input file” text boxes, it will just attach to it. Alternativelly you could run the debugserver on the iDevice only listening:</p><pre>$ debugserver localhost:1111</pre><p>And have IDA launch the process. For that you have to set both paths above to the path on your device where the binary resides, it’s usually inside a path like this:</p><pre>/var/containers/Bundle/Application/*Application UUID*/*Application*.app/*Application*</pre><p>That’s it, if you done everything right IDA will attach to the debugserver and after it ends analysing the binary you will be able to set breakpoints and start debugging the app!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EopaX-1IHpgb06HwWeMDMA.png" /></figure><h4>More Informations</h4><p>If you hit problems, here are additional sources of information where I grabbed most of the knowledge to make this post:</p><p><a href="http://iosre.com/t/ios12-debugserver-lldb/14429">Random Chinese post</a> (I suggest using google translator)</p><p><a href="https://www.hex-rays.com/products/ida/support/tutorials/ios_debugger_tutorial.pdf">Hex-Rays documentation</a></p><p><a href="https://github.com/pwn20wndstuff/Undecimus/issues/531">Github Issue 1</a> | <a href="https://github.com/pwn20wndstuff/Undecimus/issues/728">Github Issue 2</a></p><p>Happy hacking!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c5b48adf2fb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>