<?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 Arka on Medium]]></title>
        <description><![CDATA[Stories by Arka on Medium]]></description>
        <link>https://medium.com/@arkaghosh08?source=rss-bfc17c043cc3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*uwsMCjNYwJldusGjS-J-zw.png</url>
            <title>Stories by Arka on Medium</title>
            <link>https://medium.com/@arkaghosh08?source=rss-bfc17c043cc3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 15:53:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@arkaghosh08/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[How To Start Reverse Engineering — A Guide]]></title>
            <link>https://medium.com/@arkaghosh08/how-to-start-reverse-engineering-a-guide-b50b6c8112cf?source=rss-bfc17c043cc3------2</link>
            <guid isPermaLink="false">https://medium.com/p/b50b6c8112cf</guid>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[reverse-engineering]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <dc:creator><![CDATA[Arka]]></dc:creator>
            <pubDate>Sat, 30 Sep 2023 16:51:45 GMT</pubDate>
            <atom:updated>2025-03-16T13:58:12.364Z</atom:updated>
            <content:encoded><![CDATA[<h3>How To Start Reverse Engineering — A Guide</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rIB19v7nj0vr00QgD_bVrg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@markusspiske?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Markus Spiske</a> on <a href="https://unsplash.com/photos/iar-afB0QQw?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>As the name suggests, reverse engineering is finding out how something works, bottom-up. Reverse engineering in <em>computer science</em> is finding out how a program works. It might come in handy in the real world for various reasons. From wanting to recreate software with enhancements and simply trying to understand how something works to more professional uses like malware analysis, everything uses reverse engineering.</p><h3>The Ground Basics</h3><p>Now that the idea of reverse engineering is covered, let’s briefly cover some concepts.</p><p>The first step in understanding reverse engineering involves knowing how programs work. Most software we use daily is written in compiled languages. Apart from compiled languages (C, C++), some languages are interpreted (Python, Javascript), or use slightly different mechanisms (like Java).</p><p>Here, we are going to focus only on reversing compiled programs.</p><h3>Compilers</h3><p>Compilers are computer software that convert code written in some higher-level language to lower-level machine instructions. This bundle of instructions (called an executable binary, executable, or simply a binary) can then be executed by the computer.</p><figure><img alt="Compiler scheme (various stages of a compiler)" src="https://cdn-images-1.medium.com/max/399/1*BiKCk6VtxT8VV6jBuNxcvQ.png" /><figcaption>Seda Kh., CC BY-SA 4.0 &lt;<a href="https://creativecommons.org/licenses/by-sa/4.0">https://creativecommons.org/licenses/by-sa/4.0</a>&gt;, via Wikimedia Commons</figcaption></figure><p>With reverse engineering, our goal is to attempt to understand how the binary works.</p><h4>For example,</h4><p>A very simple snippet of code in C might look like the following</p><pre>int a = 12, b = 5;<br>int c = a + b;</pre><p>This snippet can be translated to x86–64 Assembly in the following way</p><pre>; Assume the stack pointer has been moved for 3 int32 numbers (12B)<br><br>; Move the numbers into memory<br>mov DWORD PTR [rsp+0x08], 12<br>mov DWORD PTR [rsp+0x04],  5<br><br>; Move the numbers into registers for the calculation<br>mov eax, DWORD PTR [rsp+0x08]<br>mov ebx, DWORD PTR [rsp+0x04]<br><br>; Carry on the addition<br>add eax, ebx<br><br>; Move the result back to memory<br>mov DWORD PTR [rsp], eax<br><br>; Instructions to restore the stack pointer</pre><p>This, when further assembled to machine instructions (AMD64 Linux), might look like the following (hexadecimal representation of a sequence of bytes)</p><pre>C7 44 24 08 0C 00 00 00 C7 44 24 04 05 00 00 00 8B 44 24 08 8B 5C 24 04 01 D8 89 04 24</pre><p>If you open any binary in a hex editor, the sequence of bytes represented as hexadecimal would look pretty similar.</p><h3>Some Useful Tools</h3><p>Now, imagine that you are given an executable. Viewing the hex dump alone wouldn’t take us anywhere.</p><figure><img alt="Code for a hello world program" src="https://cdn-images-1.medium.com/max/627/1*W0GoUxF0Hsov5o5pzxgybw.png" /><figcaption>A sample Hello World program in C</figcaption></figure><figure><img alt="Hexdump of hello world program" src="https://cdn-images-1.medium.com/max/883/1*akRylfOMJago-16-k4lQ6w.png" /><figcaption>Glimpse of a hex dump of the above code</figcaption></figure><p>As seen in the above pictures, a simple Hello World program can become hundreds of bytes of instructions. For instance, this simple 7-line program had 997 lines in its hex dump!</p><h4>Disassemblers</h4><p>To make things more readable, we use disassemblers. Just as assemblers convert assembly code to machine instructions, disassemblers do the opposite.</p><p><a href="https://www.sourceware.org/gdb/">GDB</a> is one of the oldest and most powerful tools that can be used as a disassembler. However, using GDB can be hard as it’s primarily a terminal-based program and without a thorough knowledge of its commands, using GDB can be a very rudimentary experience. So, there are other tools like <a href="https://ghidra-sre.org/">Ghidra</a>, <a href="https://hex-rays.com/ida-free/">Ida</a>, or <a href="https://binary.ninja/">Binary Ninja</a>, which make working with binaries more interactive and easier.</p><figure><img alt="Disassembly for main() of the hello world program" src="https://cdn-images-1.medium.com/max/792/1*lKUW2ZlV2z_g23s_3KfveQ.png" /><figcaption>Disassembly of main() in GDB</figcaption></figure><p>Now, this is certainly better than trying to read binary outright. However, reading assembly code directly can still be challenging and confusing, especially for more complicated programs.</p><h4>Decompilers</h4><p>Enter decompilers. Again, going by the name, you might’ve guessed what they do. They take the assembly generated by disassemblers and output readable (well, most of the time) code in a desired language.</p><p>Softwares like Ghidra, Ida, etc, come with their decompiler plugins and are very capable. However, one important thing to note is that unlike disassemblers, which produce a one-to-one accurate translation of machine code to assembly, decompiled code may not be a full translation from assembly.</p><p>Decompiling is a much harder process, especially as coding concepts get harder (code involving classes and objects, for instance). The resultant code must only be treated as pseudo-code.</p><figure><img alt="Ghidra interface" src="https://cdn-images-1.medium.com/max/1024/1*vKAgwRR43WtFwiEVm4KWiA.png" /><figcaption>View of Ghidra interface (with the sample Hello World program loaded)</figcaption></figure><h3>A Review of a CTF Rev Challenge</h3><p>CTF (Capture The Flag) competitions are a great way to practice your cybersecurity skills ethically. The competitions include challenges from various categories of cybersecurity. Each challenge involves unearthing a “flag”, which is usually a string in a particular format, for example, flag{...}. The contents inside the curly brackets are, of course, unique to the challenge.</p><p>Reverse engineering is a very popular category and often a part of most CTFs.</p><h4>Csaw CTF 2023 / Rebug 2</h4><p><a href="https://ctf.csaw.io/">Csaw CTF</a>, which was hosted around mid-September, had some beginner-friendly challenges. Rebug 2 is such a challenge from the reverse category.</p><p>The challenge description was the following</p><figure><img alt="Screenshot of Rebug 2 challenge description from csaw 2023" src="https://cdn-images-1.medium.com/max/901/1*wG8nk98WQgjJkfIbxG1gYA.png" /><figcaption>Screenshot of <a href="https://ctf.csaw.io/">csaw</a>/<a href="https://ctf.csaw.io/challenges#Rebug%202-9">Rebug 2</a> challenge description</figcaption></figure><p>On downloading the file, the first step I did (and what usually is done) was to run the file command.</p><figure><img alt="Output for file command" src="https://cdn-images-1.medium.com/max/1024/1*Ph3JRIuojlEdW6Zt1NJ-gQ.png" /></figure><p>This gives us some very basic but useful information about the file. We can mainly infer that the file is a 64-bit ELF executable, along with a few other details.</p><p>The next step is to run the strings command.</p><p>Running it on this binary doesn’t give me anything useful. But sometimes, you can find crucial leads using the strings command.</p><p>There are some other basic Linux commands like strace and ltrace that you can run.</p><h4>Decompilation</h4><p>I opened the binary using Ghidra. After running the initial analysis that Ghidra does, I started looking at the decompilation of the main function.</p><figure><img alt="Decompiled main() function of the challenge" src="https://cdn-images-1.medium.com/max/665/1*VK4vqDx_giGXfndnPcNdnQ.png" /></figure><p>The code might not look like regular C, but upon close inspection, it’s quite the same.</p><p>There’s a single function call present in the main function, and it’s obviously of interest. Double-clicking on the function takes us to the decompilation of the function itself.</p><figure><img alt="Decompiled printbinchar() from the challenge" src="https://cdn-images-1.medium.com/max/552/1*zx4xd44bpkfgQte1stkRGQ.png" /></figure><p>Again, there’s a bunch of code that almost looks like gibberish. But yet again, there’s a function call to xoring() that is of interest. Navigating to xoring() get the following.</p><figure><img alt="Decompiled xoring() from the challenge" src="https://cdn-images-1.medium.com/max/642/1*pv10NnOufe6mCPSIstvg9w.png" /></figure><p>Immediately, what caught my eye was the array called flag. Remember that the challenge description also mentions that we need to find the flag in the binary.</p><p>It’s pretty clear from the code around the variable that this function is “creating” the flag.</p><p>Our goal at this point is to obtain the contents of flag. A point to note is that within the main function, printbinchar() is getting called inside a for-loop. Hence, to get the full value of the flag, we’d need to obtain the contents of the array <em>after</em> the control breaks out of the loop.</p><h4>Dynamic Analysis</h4><p>Until this point, whatever we had been doing was static analysis. Static analysis is mainly used to figure out the logical flow of a program.</p><p>Now that we know how the code works, and need to find out the contents of its memory during run-time, we need to switch to dynamic analysis.</p><p>There are various tools for dynamic analysis, the most basic albeit powerful one again being GDB. However, because of the aforementioned reasons, GDB alone can prove to be a difficult tool to work with.</p><p>Certain enhancements like pwndbg, peda or GEF are added to GDB to make it more friendly. Another alternative is <a href="https://rada.re/n/radare2.html">Radare</a>.</p><p>We launch Radare2 using the following command</p><figure><img alt="Radare2 launch command" src="https://cdn-images-1.medium.com/max/259/1*BGTcis4TWAewW9yjmQ1bQA.png" /></figure><p>The two arguments used are -d for debugging mode and -AA for a complete analysis of the binary.</p><h4>Final Bit</h4><p>To figure out the contents of the flag variable, we need to follow three simple steps:-</p><ol><li>Find the memory address of flag.</li><li>Halt the program <em>right</em> after it exits from the loop in main().</li><li>View the contents of flag from memory using the previously found address.</li></ol><h4>1. Memory Address</h4><p>Fortunately, in the case of this binary, we don’t <em>need</em> the memory address of flag. This is because if we look at the corresponding assembly instructions to the array operations</p><figure><img alt="Code line 25, depicting array operation on flag" src="https://cdn-images-1.medium.com/max/236/1*68Dz9gqrnccRssg40Rcp5g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/332/1*5zyZBeX-UaJjOh1_mSyCKA.png" /></figure><p>We can see that the memory address of flag was loaded into the RDX register. If we also observe all the code following this, RDX isn’t used anywhere else. This means instead of keeping track of the address of flag we can, at any point, get the address by checking the RDX register.</p><h4>2. Halt Execution</h4><p>One of the best features of dynamic analysis is setting breakpoints. Breakpoints allow you to halt the execution of code at almost any point and then give manual control of executing instructions after that.</p><p>To set the breakpoint, you need to access the main() function in Radare. You can do this by typing s main to reach the main() function, followed by pdf for disassembling it.</p><figure><img alt="Radare command to move to main()" src="https://cdn-images-1.medium.com/max/345/1*hoffshdx-F52iAfXDtTlNw.png" /></figure><figure><img alt="Radare command to disassemble a function" src="https://cdn-images-1.medium.com/max/797/1*Jk7LxuMiV_Ed0yYOIbAbzg.png" /><figcaption>Glimpse of disassembly of the main function</figcaption></figure><p>I set a breakpoint right before the program quit, as the for-loop is the last piece of code in the main function.</p><figure><img alt="Instruction for reference to set breakpoint" src="https://cdn-images-1.medium.com/max/547/1*WG6WSmzeiezHdoXchEjJjg.png" /></figure><figure><img alt="Radare command to set breakpoint" src="https://cdn-images-1.medium.com/max/238/1*2KiHkFwb42BMggdJkiDAxw.png" /></figure><p>db is used to set a breakpoint. It requires the address of the instruction where the breakpoint is to be placed.</p><h4>Obtain the Flag</h4><p>Next, we need to run the binary. We can run it by using dc.</p><figure><img alt="Radare command to run the program" src="https://cdn-images-1.medium.com/max/376/1*-J_svT4P9E8IsBTffOVGLg.png" /></figure><p>The program executes and halts at the set breakpoint. Now, we need to look at the memory where flag is located. We can do this by using the pxw command. The p stands for print, x for hex, w for word. So, it will print the memory contents in 16-bit chunks and in hexadecimal. The memory we want to access is stored in RDX so, we use the dr command to get the contents of RDX.</p><p>The final command becomes:-</p><figure><img alt="Radare command to view contents of memory" src="https://cdn-images-1.medium.com/max/162/1*xTcEqK7ZyDWtB-uyWIBs6A.png" /></figure><p>The backticks used to surround dr rdx are to execute that command first, then use <em>its</em> output in executing the pxw command.</p><p>The output obtained is</p><figure><img alt="Output of the memory viewing function" src="https://cdn-images-1.medium.com/max/918/1*AYT1iqdyt93fl_JMSaQRjQ.png" /></figure><p>The yellow text is the ASCII representation of the memory contents. This is what we’re looking for.</p><p>And that&#39;s it, we have successfully obtained the flag!</p><p>Putting it in the format specified in the challenge description, we get csawctf{01011100010001110000}.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b50b6c8112cf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>