<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: SJ W</title>
    <description>The latest articles on DEV Community by SJ W (@7jw92nvd1klaq1).</description>
    <link>https://dev.to/7jw92nvd1klaq1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1112371%2F1fe2f758-04a3-47f5-8def-e7b2c9271d52.png</url>
      <title>DEV Community: SJ W</title>
      <link>https://dev.to/7jw92nvd1klaq1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/7jw92nvd1klaq1"/>
    <language>en</language>
    <item>
      <title>picoCTF "homework" Walkthrough</title>
      <dc:creator>SJ W</dc:creator>
      <pubDate>Sat, 05 Jul 2025 06:18:24 +0000</pubDate>
      <link>https://dev.to/7jw92nvd1klaq1/picoctf-homework-walkthrough-384a</link>
      <guid>https://dev.to/7jw92nvd1klaq1/picoctf-homework-walkthrough-384a</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Today, we are going to go over the picoCTF challenge I have completed a few days ago, called &lt;strong&gt;homework&lt;/strong&gt;, a challenge in the category of binary exploitation. It has been a while since I posted anything in this blog, and before I start the walkthrough, I would like to briefly talk about my future career in cybersecurity. Recently, I made up my mind to fully pursue a career in cybersecurity. Fully transitioning into the field of cybersecurity feels like a daunting task, but at the same time, I believe that it will help me lead a fulfilling life that I have always envisioned. Evident in my blog, the vast majority of posts are related to various CTF challenges I have completed so far, and simply, it is the most enjoyable thing for me to do in life. Along the journey, there will be a plenty of challenges and obstacles that will, at times, make me feel doubtful about accomplishing my goal, but I believe that, with strong willpower and commitment, I can overcome those and keep moving forward for the ultimate goal I set in life. Enough with my gibberish, let's go ahead with the challenge.&lt;/p&gt;

&lt;p&gt;Unlike all the challenges I have completed and posted in this blog, this challenge is of the category of Binary Exploitation, which involves a slightly different approach than Reverse Engineering. On top of understanding how a binary works, challenges in this category require you to carefully manipulate a binary into doing something that wasn't expected of it to do. Usually in this category, there are challenges involving heap and stack overflow that alters the behaviors of a program in unexpected ways.&lt;/p&gt;

&lt;p&gt;This challenge basically involves providing a series of strings that are fed to a custom interpreter, which executes the custom instructions that a user provides. Ultimately, what we need the binary to do is, with our carefully crafted instructions, to output the flag that we need to submit in order to complete this challenge.&lt;/p&gt;

&lt;h2&gt;
  
  
  Analysis
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7pzm5oprk8xl3goxzp3i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7pzm5oprk8xl3goxzp3i.png" alt="Ghidra Analysis Options" width="800" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just as usual, it all starts with statically analyzing a binary with the help of Ghidra. Let's see what it contains.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl32rofdfap6gu8o4yzci.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl32rofdfap6gu8o4yzci.png" alt="main function" width="800" height="387"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the completion of the analysis, we wind up right at the function &lt;strong&gt;main&lt;/strong&gt;, which is the starting point of the binary. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftjathswvu1grk71p4lk4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftjathswvu1grk71p4lk4.png" alt="main function decompilation" width="800" height="810"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Looking at the function &lt;strong&gt;main&lt;/strong&gt;, we notice a few lines that seem pretty interesting. In &lt;strong&gt;lines 12 and 13&lt;/strong&gt;, the function apparently copies the content of a file &lt;strong&gt;flag.txt&lt;/strong&gt; to a memory address using the &lt;strong&gt;scanf&lt;/strong&gt; function. The &lt;strong&gt;lines 16 and 17&lt;/strong&gt; contain variables named &lt;strong&gt;rows&lt;/strong&gt; and &lt;strong&gt;cols&lt;/strong&gt;, which seem to indicate that it has to do with a matrix of &lt;strong&gt;0x32&lt;/strong&gt; rows and &lt;strong&gt;0x16&lt;/strong&gt; columns (&lt;strong&gt;50 rows and 22 columns&lt;/strong&gt;). &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhjaz4nnjut19xlmac93.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhjaz4nnjut19xlmac93.png" alt="Disassembly of the variable board part 1" width="800" height="471"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnw9v0nveiqwyfdhu3njc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnw9v0nveiqwyfdhu3njc.png" alt="Disassembly of the variable board part 2" width="800" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the variable &lt;strong&gt;board&lt;/strong&gt; mentioned in &lt;strong&gt;line 19&lt;/strong&gt; encompasses &lt;strong&gt;1100&lt;/strong&gt; bytes (&lt;strong&gt;50 * 22&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;And then, we encounter a for-loop that repeats the total of &lt;strong&gt;4&lt;/strong&gt; times maximum, which does the followings:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Accept an input of up to &lt;strong&gt;22&lt;/strong&gt; bytes and store the input at the following address.

&lt;ul&gt;
&lt;li&gt;Address of the variable &lt;strong&gt;board&lt;/strong&gt; + (&lt;strong&gt;0x16&lt;/strong&gt; * &lt;strong&gt;local_14&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;The reason why the function does (&lt;strong&gt;0x16&lt;/strong&gt; * &lt;strong&gt;local_14&lt;/strong&gt;) is that there are &lt;strong&gt;22&lt;/strong&gt; columns in each row, and multiplying it to &lt;strong&gt;local_14&lt;/strong&gt; results in the very first column of a certain row. Basically, &lt;strong&gt;local_14&lt;/strong&gt; acts as a row number.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Check if the input is not accepted or the first byte of an input is the ASCII value of &lt;strong&gt;0x52&lt;/strong&gt;.

&lt;ul&gt;
&lt;li&gt;If either one evaluates to True, then terminate the for-loop prematurely.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Calculate the length of an input.&lt;/li&gt;
&lt;li&gt;Replace the very last character of an input with a null byte.

&lt;ul&gt;
&lt;li&gt;So, let's say you provided an input of &lt;strong&gt;22&lt;/strong&gt; bytes. The process replaces the 22nd byte with a null byte, which turns into an input of &lt;strong&gt;21&lt;/strong&gt; bytes. &lt;strong&gt;Essentially, each row is only able to contain up to an input of 21 bytes&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, what we can gather from above is the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;There is a &lt;strong&gt;board of 50 row and 22 cols&lt;/strong&gt; that will be placed with our input. Each row contains &lt;strong&gt;22&lt;/strong&gt; columns.&lt;/li&gt;
&lt;li&gt;You provide an input &lt;strong&gt;4&lt;/strong&gt; times total. If the max length of each line is &lt;strong&gt;21&lt;/strong&gt; bytes, then &lt;strong&gt;you can provide inputs of up to 84 bytes&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Okay, so you place inputs to a board. What does each input do?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqmmn6b4eowltp83hqjzq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqmmn6b4eowltp83hqjzq.png" alt="step function" width="800" height="73"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The "step" function
&lt;/h2&gt;

&lt;p&gt;Once it exits the for-loop, it enters a do...while-loop, which seems to repeatedly call the function &lt;strong&gt;step&lt;/strong&gt;. Once the function returns the value of &lt;strong&gt;0&lt;/strong&gt;, it exits the loop, thus terminating the process. Let's see what it does.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmhibnvvvasc2jw47f6ke.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmhibnvvvasc2jw47f6ke.png" alt="Step function part1" width="800" height="887"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6pojimm03ec4neqze306.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6pojimm03ec4neqze306.png" alt="Step function part2" width="800" height="887"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flktawo9gg7u72k7iebww.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flktawo9gg7u72k7iebww.png" alt="Step function part3" width="800" height="887"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The function is just essentially a switch statement with a bunch of cases, each of which represents a unique instruction that a process executes. Notably, many of these instructions utilize the variable called &lt;strong&gt;stack&lt;/strong&gt;, which is used to store data needed to execute instructions. To save some time on trying to figure out what each instruction does, I copied the entire thing and asked an LLM to tell me what each of them does. Here is a response from it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Here is a breakdown of what each case in the switch statement does, identified by its hexadecimal value and corresponding ASCII character:

case 0x21 ('!') - Logical NOT:
Pops the top value from the stack. If the value is 0, it pushes 1 onto the stack. Otherwise, it pushes 0.

case 0x24 ('$') - Pop:
Discards the value at the top of the stack.

case 0x25 ('%') - Modulo:
Pops two values from the stack (let's call them a and b, where a is the top value). It then calculates b % a and pushes the result back onto the stack.

case 0x2a ('*') - Multiplication:
Pops two values (a and b) from the stack, multiplies them (b * a), and pushes the result onto the stack.

case 0x2b ('+') - Addition:
Pops two values (a and b) from the stack, adds them (b + a), and pushes the result onto the stack.

case 0x2c (',') - Output Character:
Pops a value from the stack and prints it to the console as its corresponding ASCII character.

case 0x2d ('-') - Subtraction:
Pops two values (a and b) from the stack, subtracts them (b - a), and pushes the result onto the stack.

case 0x2e ('.') - Output Integer:
Pops a value from the stack and prints it to the console as a decimal integer.

case 0x2f ('/') - Division:
Pops two values (a and b) from the stack, divides them (b / a), and pushes the integer result onto the stack.

case 0x3a (':') - Duplicate:
Duplicates the value at the top of the stack. If the stack is empty, this will result in an error.

case 0x3c ('&amp;lt;') - Go Left:
Sets the direction of program flow to the left. The program counter will now move left in the next step.

case 0x3e ('&amp;gt;') - Go Right:
Sets the direction of program flow to the right.

case 0x40 ('@') - End Program:
Terminates the program by returning 0 from the step function.

case 0x5c ('\') - Swap:
Swaps the top two values on the stack.

case 0x5e ('^') - Go Up:
Sets the direction of program flow upwards.

case 0x5f ('_') - Horizontal If:
Pops a value from the stack. If the value is 0, the direction of flow is set to the right ('&amp;gt;'). Otherwise, it is set to the left ('&amp;lt;').

case 0x60 ('') - Greater Than: 
Pops two values (aandb) from the stack. It checks if b &amp;gt; a. If true, it pushes 1 onto the stack; otherwise, it pushes 0.

case 0x67 ('g') - Get:
Pops a y-coordinate and then an x-coordinate from the stack. It retrieves the ASCII value of the character at that (x, y) position on the board and pushes it onto the stack.

case 0x70 ('p') - Put:
Pops a y-coordinate, an x-coordinate, and a value from the stack. It then modifies the board by placing the character corresponding to the value's ASCII code at the specified (x, y) position.

case 0x76 ('v') - Go Down:
Sets the direction of program flow downwards.

case 0x7c ('|') - Vertical If:
Pops a value from the stack. If the value is 0, the direction of flow is set downwards ('v'). Otherwise, it is set upwards ('^').

default:
This block handles any characters not explicitly covered by the other cases. It specifically checks if the character on the board is '0' (ASCII 0x30). If it is, it pushes the integer value 0 onto the stack. For any other character (e.g., '1' through '9' or other symbols not defined), it does nothing and the program counter simply moves to the next position.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, to summarize what you can do with these instructions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You can move around the board. Wherever you land at is exactly an instruction that an interpreter will execute.&lt;/li&gt;
&lt;li&gt;You can add, subtract, multiply, and divide the top two values on the stack.&lt;/li&gt;
&lt;li&gt;You can output what is on the stack, either in the form of an ASCII character or an integer.&lt;/li&gt;
&lt;li&gt;You can duplicate the top value or swap the top two values on the stack, essentially changing their positions.&lt;/li&gt;
&lt;li&gt;You can flip the value on the stack either from &lt;strong&gt;0 to 1&lt;/strong&gt; or any non-zero value to &lt;strong&gt;0&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Lastly, you can insert a value to a certain cell, and retrieve a value from a certain cell on the board and push it to the stack. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzuzd8x7m6lfz16b3nfor.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzuzd8x7m6lfz16b3nfor.png" alt="Instruction 0x67" width="800" height="507"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Mind you that, for the number 6, there is a huge caveat that limits which cell you can insert a value to or get a value from: with the help of the variable &lt;strong&gt;rows&lt;/strong&gt; and &lt;strong&gt;cols&lt;/strong&gt;, it forbids you to access values off the board. For example, the very logic is implemented in the if-statement of the instruction &lt;strong&gt;0x67&lt;/strong&gt; (an instruction for pushing to the stack the value at a certain cell) from the line 162 to 167. If the top value on the stack exceeds the value of the variable &lt;strong&gt;rows&lt;/strong&gt;, which is &lt;strong&gt;0x32 (50)&lt;/strong&gt;, or the 2nd top value on the stack exceeds the value of the variable &lt;strong&gt;cols&lt;/strong&gt;, which is &lt;strong&gt;0x16 (22)&lt;/strong&gt;, it terminates a process early.&lt;/p&gt;

&lt;p&gt;So, like I said earlier, our goal is to somehow make it output the flag for the completion of the challenge. &lt;strong&gt;Being aware of the fact that we can't access values off the board and that the flag we are trying to output is not on the board, how can we potentially access values off the board?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Like I said, the caveat is that both &lt;strong&gt;rows&lt;/strong&gt; and &lt;strong&gt;cols&lt;/strong&gt; are used to prevent access to values off the board. &lt;strong&gt;Can we perhaps modify either of the values in order to access values off the board?&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Where Are Rows and Cols?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp2l69340bij8gclhd9w8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp2l69340bij8gclhd9w8.png" alt="Rows and Cols Disassembly" width="800" height="370"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Going through the disassembly, I discovered that both &lt;strong&gt;rows&lt;/strong&gt; and &lt;strong&gt;cols&lt;/strong&gt; are located right after the &lt;strong&gt;board&lt;/strong&gt; variable. Both &lt;strong&gt;rows&lt;/strong&gt; and &lt;strong&gt;cols&lt;/strong&gt; occupy &lt;strong&gt;4&lt;/strong&gt; bytes each. Modifying either of those values to a value that exceeds their original values allows us to access values off the board! Let's see if we can modify them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1z9gl2lvhwpf92vt6xf7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1z9gl2lvhwpf92vt6xf7.png" alt="Instruction 0x70" width="800" height="79"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above is a screenshot of the instruction &lt;strong&gt;0x70&lt;/strong&gt;, which is used to insert a value at a certain cell. The mechanism with which it chooses a cell on the &lt;strong&gt;board&lt;/strong&gt; is the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The address of the variable &lt;strong&gt;board&lt;/strong&gt; + (&lt;strong&gt;0x16&lt;/strong&gt; * &lt;strong&gt;row&lt;/strong&gt;) + &lt;strong&gt;col&lt;/strong&gt;.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;row&lt;/strong&gt; is the very top value on the stack. It can be any value from 0 to 50.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;col&lt;/strong&gt; is the 2nd top value on the stack. It can be any value from 0 to 22.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Okay, so ideally, if we were to choose a cell on the end of the board, we would need both &lt;strong&gt;row&lt;/strong&gt; and &lt;strong&gt;col&lt;/strong&gt; to be their respective max values. So, for (&lt;strong&gt;0x16&lt;/strong&gt; * &lt;strong&gt;row&lt;/strong&gt;), let's pretend that the value of &lt;strong&gt;row&lt;/strong&gt; is &lt;strong&gt;50&lt;/strong&gt;. &lt;strong&gt;(0x16(22) * 50)&lt;/strong&gt; results in the value &lt;strong&gt;1100&lt;/strong&gt;. If we were to pretend that the value of &lt;strong&gt;col&lt;/strong&gt; is &lt;strong&gt;22&lt;/strong&gt;, adding it to &lt;strong&gt;1100&lt;/strong&gt; results in the value &lt;strong&gt;1122&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If there are only &lt;strong&gt;1100&lt;/strong&gt; cells on the board and we can access up to &lt;strong&gt;1122&lt;/strong&gt; cells, we can access up to &lt;strong&gt;22&lt;/strong&gt; extra bytes off the board, and since both &lt;strong&gt;rows&lt;/strong&gt; and &lt;strong&gt;cols&lt;/strong&gt; are located right after the board and within those &lt;strong&gt;22&lt;/strong&gt; extra bytes, we can modify them!&lt;/p&gt;

&lt;h2&gt;
  
  
  Where Is The Flag?
&lt;/h2&gt;

&lt;p&gt;Okay, since we have confirmed that the values of both &lt;strong&gt;rows&lt;/strong&gt; and &lt;strong&gt;cols&lt;/strong&gt; can indeed be modified, it's time to find out where the flag is located.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fil28rpxgtzoy8km5dhia.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fil28rpxgtzoy8km5dhia.png" alt="Flag disassembly part 1" width="800" height="522"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fypzujxccnrwen36ahnci.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fypzujxccnrwen36ahnci.png" alt="Flag disassembly part 2" width="800" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The flag is located somewhere after &lt;strong&gt;cols&lt;/strong&gt;, and the memory for the flag spans &lt;strong&gt;104&lt;/strong&gt; bytes. The very first address of the flag is &lt;strong&gt;0x1056e0&lt;/strong&gt;, and the very last address of the flag is &lt;strong&gt;0x105747&lt;/strong&gt;. To calculate how many bytes we need in order to access the flag, all we have to do is to subtract the very last address of the variable &lt;strong&gt;board&lt;/strong&gt; from &lt;strong&gt;0x105747&lt;/strong&gt;. The very last address of the &lt;strong&gt;board&lt;/strong&gt; is &lt;strong&gt;0x1056ab&lt;/strong&gt;, so it would be (&lt;strong&gt;0x105747&lt;/strong&gt; - &lt;strong&gt;0x1056ab&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0untscpsg57nfj8ynaxj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0untscpsg57nfj8ynaxj.png" alt="Calculation" width="800" height="293"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;We need to modify the variable cols to contain the value of 156 at least in order to access the entirety of the flag.&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Crafting Input
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frhx4c5g369rssz4xzqyc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frhx4c5g369rssz4xzqyc.png" alt="For-loop for providing inputs" width="800" height="143"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Like I said previously, we can provide up to &lt;strong&gt;4&lt;/strong&gt; lines in total, each of which may contain up to &lt;strong&gt;21&lt;/strong&gt; bytes. &lt;strong&gt;So the maximum amount of instructions that we can provide to the process amounts to 84 bytes.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Okay, so here is what we have to do basically:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Set the variable &lt;strong&gt;cols&lt;/strong&gt; to a value that exceeds &lt;strong&gt;156&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Repeatedly, retrieve and push to the stack each character of the flag, and output each character.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The very first instruction that the interpreter executes is the very first byte of the first input a user provides. By default, once it executes an instruction, it moves to the next cell on the right and executes the next instruction. Unless modifying the direction, once it reaches the last instruction of a row, with the help of modulus, it goes back to the first instruction of the same row.&lt;/p&gt;

&lt;p&gt;So, since we have only &lt;strong&gt;84&lt;/strong&gt; instructions at max to print out the entirety of the flag, it would make sense to make the most out of it by doing the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmpvdh8re3exj6r2s6z9t.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmpvdh8re3exj6r2s6z9t.jpg" alt="Instructions" width="800" height="725"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above is how I decided to go about crafting the inputs in order to use as many instructions as possible, given our limitation of being able to use up to &lt;strong&gt;84&lt;/strong&gt; instructions. Since the first instruction that the interpreter executes is the one on top-left corner, let it go over the first row until the very last column, which moves down to the next row of the same column. And then change the direction to the left and let it also cover the entirety of the row in reverse. The same pattern continues until the very last row.&lt;/p&gt;

&lt;p&gt;In order to change the value of the variable &lt;strong&gt;cols&lt;/strong&gt;, we have to calculate the exact row and column that we have to target. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp2l69340bij8gclhd9w8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp2l69340bij8gclhd9w8.png" alt="Rows and Cols Disassembly" width="800" height="370"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see in a screenshot above, right after the &lt;strong&gt;board&lt;/strong&gt; is &lt;strong&gt;rows&lt;/strong&gt;, which is followed by &lt;strong&gt;cols&lt;/strong&gt;. So, accessing the last cell of the &lt;strong&gt;board&lt;/strong&gt; requires us to set the very top value on the stack to &lt;strong&gt;50&lt;/strong&gt; and the 2nd top value to &lt;strong&gt;0&lt;/strong&gt;, since &lt;strong&gt;(50 * 0x16) + 0&lt;/strong&gt; results in &lt;strong&gt;1100&lt;/strong&gt;. Since the &lt;strong&gt;cols&lt;/strong&gt; is located 4 bytes after the last cell of the &lt;strong&gt;board&lt;/strong&gt;, all we have to do is to make the 2nd top value on the stack hold the value of &lt;strong&gt;4&lt;/strong&gt;, thus resulting in &lt;strong&gt;(50 * 0x16) + 4&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So, let's get to crafting the first line of the input for pushing &lt;strong&gt;50&lt;/strong&gt; to the stack.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;create_50_on_the_stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\x30\x21\x3a\x2b\x3a\x3a\x3a\x3a\x2b\x2b\x2b\x2b\x3a\x3a\x3a\x3a\x2b\x2b\x2b\x2b\x76&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;line_1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;create_50_on_the_stack&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is a breakdown of the instructions of the line 1 above:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;\x30\x21\x3a\x2b&lt;/code&gt; - Push &lt;strong&gt;2&lt;/strong&gt; to the stack&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\x3a\x3a\x3a\x3a\x2b\x2b\x2b\x2b&lt;/code&gt; - (&lt;strong&gt;0x3a&lt;/strong&gt;) Duplicate the top value on the stack &lt;strong&gt;4&lt;/strong&gt; times and (&lt;strong&gt;0x2b&lt;/strong&gt;) add the top 2 values on the stack &lt;strong&gt;4&lt;/strong&gt; times. This means &lt;strong&gt;2&lt;/strong&gt; becomes &lt;strong&gt;10&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\x3a\x3a\x3a\x3a\x2b\x2b\x2b\x2b&lt;/code&gt; - Same as the one above, except &lt;strong&gt;10&lt;/strong&gt; becomes &lt;strong&gt;50&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\x76&lt;/code&gt; - Move down to the same column of the next row.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Upon the executions of instructions in the first row, there is only one value on the stack, which is &lt;strong&gt;50&lt;/strong&gt;. Let's craft the 2nd line of the input.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;insert_200_to_cols_1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\x3c\x30\x30\x70\x30\x30\x67\x3a\x3a\x3a\x2b\x2b\x2b\x30\x30\x67\x30\x21\x3a\x2b\x76&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;line_2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;insert_200_to_cols_1&lt;/span&gt;&lt;span class="p"&gt;[::&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is a breakdown of the instructions of the line 2 above:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;\x3c&lt;/code&gt; - Change the direction to the left, thus moving leftwards.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\x30\x30\x70&lt;/code&gt; - (&lt;strong&gt;0x30&lt;/strong&gt;) Push &lt;strong&gt;0&lt;/strong&gt; twice to the stack and (&lt;strong&gt;0x70&lt;/strong&gt;) write the 3rd top value on the stack to the board using the very top value on the stack as the row number and the 2nd top value on the stack as the column number.

&lt;ul&gt;
&lt;li&gt;At this point, upon pushing &lt;strong&gt;0&lt;/strong&gt; twice to the stack, the stack has &lt;strong&gt;3&lt;/strong&gt; items - &lt;strong&gt;(50 (last), 0, 0 (top))&lt;/strong&gt;. What this means is that we are writing &lt;strong&gt;50&lt;/strong&gt; to the first cell of the board in the top left corner. Those values used to write to the board are popped, and the stack becomes empty.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\x30\x30\x67&lt;/code&gt; - (&lt;strong&gt;0x30&lt;/strong&gt;) Push &lt;strong&gt;0&lt;/strong&gt; twice to the stack and (&lt;strong&gt;0x67&lt;/strong&gt;) retrieve the value from the board and push it to the stack.

&lt;ul&gt;
&lt;li&gt;So, right before this, we inserted the value &lt;strong&gt;50&lt;/strong&gt; to the first cell of the board at &lt;strong&gt;(0, 0)&lt;/strong&gt;. We retrieve &lt;strong&gt;50&lt;/strong&gt; from the top left corner of the board and push it to the stack, so there is only &lt;strong&gt;50&lt;/strong&gt; on the stack at this point.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\x3a\x3a\x3a\x2b\x2b\x2b&lt;/code&gt; - (&lt;strong&gt;0x3a&lt;/strong&gt;) Duplicate the top value on the stack &lt;strong&gt;3&lt;/strong&gt; times and (&lt;strong&gt;0x2b&lt;/strong&gt;) add the top 2 values on the stack &lt;strong&gt;3&lt;/strong&gt; times. This means &lt;strong&gt;50&lt;/strong&gt; becomes &lt;strong&gt;200&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\x30\x30\x67&lt;/code&gt; - Same as the 3rd one, retrieve &lt;strong&gt;50&lt;/strong&gt; from the first cell of the board and push it to the stack.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\x30\x21\x3a\x2b&lt;/code&gt; - Push &lt;strong&gt;2&lt;/strong&gt; to the stack.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\x76&lt;/code&gt; - Move down to the same column of the next row.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Upon the executions of instructions in the second row, there are three values on the stack, &lt;strong&gt;(200 (last), 50, 2)&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;insert_200_to_cols_2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\x3e\x3a\x2b\x5c\x70\x30&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\x3e&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\x76&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;line_3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;insert_200_to_cols_2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is a breakdown of the instructions of the line 3 above:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;\x3e&lt;/code&gt; - Change the direction to the right, thus moving rightward.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\x3a\x2b&lt;/code&gt; - Duplicate the top value (&lt;strong&gt;2&lt;/strong&gt;) on the stack and add the top two values (&lt;strong&gt;2 + 2&lt;/strong&gt;), resulting in the previous top value of &lt;strong&gt;2&lt;/strong&gt; turning into &lt;strong&gt;4&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\x5c&lt;/code&gt; - Swap the top two values on the stack.

&lt;ul&gt;
&lt;li&gt;So it goes from &lt;strong&gt;(200 (last), 50, 4)&lt;/strong&gt; to &lt;strong&gt;(200 (last), 4, 50)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\x70&lt;/code&gt; - Write the 3rd top value on the stack to the board using the very top value on the stack as the row number and the 2nd top value on the stack as the column number.

&lt;ul&gt;
&lt;li&gt;What this does is inserting &lt;strong&gt;200&lt;/strong&gt; to the &lt;strong&gt;(50 * 0x16 + 4)&lt;/strong&gt;, which is where &lt;strong&gt;cols&lt;/strong&gt; is located! After this, &lt;strong&gt;cols&lt;/strong&gt; holds the value of &lt;strong&gt;200&lt;/strong&gt;!&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\x30&lt;/code&gt; - Push &lt;strong&gt;0&lt;/strong&gt; to the stack.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\x3e&lt;/code&gt; (&lt;strong&gt;14&lt;/strong&gt; times) - Change the direction to the right. Basically, this doesn't do anything, and the reason why we are inserting this is that we are ready to start printing out characters with the help of the instructions on the last row.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Upon the executions of instructions in the third row, there is only one value on the stack, which is &lt;strong&gt;0&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;print_each_char&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\x3c\x3a\x30\x30\x67\x67\x2c&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\x30\x21\x2b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\x3c&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;
&lt;span class="n"&gt;line_4&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;print_each_char&lt;/span&gt;&lt;span class="p"&gt;[::&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last row is where you start printing out characters. Unlike other rows, it doesn't involve moving down to the next row since there is no instruction on the next row. Using the concept of a for-loop here, we repeatedly print out characters consecutively starting from the last cell of the &lt;strong&gt;board&lt;/strong&gt; to every one of the next 200 extra cells after, which include the flag we need.&lt;/p&gt;

&lt;p&gt;Here is a breakdown of the instructions of the line 4 above:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;\x3c&lt;/code&gt; - Change the direction to the left, thus moving leftwards.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\x3a&lt;/code&gt; - Duplicate the very top value on the stack. The very top value of the stack at this point is the value of the &lt;strong&gt;column&lt;/strong&gt; that we will use to print out each character. With the output of each character, it increments by &lt;strong&gt;1&lt;/strong&gt;. Currently, at the outset, it holds &lt;strong&gt;0&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\x30\x30\x67&lt;/code&gt; - Retrieve &lt;strong&gt;50&lt;/strong&gt; from the first cell of the board and push it to the stack.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\x67&lt;/code&gt; - Retrieve the value from &lt;strong&gt;(50 * 0x16 + column)&lt;/strong&gt; and push it to the stack.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\x2c&lt;/code&gt; - Print out the value at the top of the stack in the form of ASCII character.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\x30\x21\x2b&lt;/code&gt; - Push &lt;strong&gt;1&lt;/strong&gt; to the stack and increment the column by &lt;strong&gt;1&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\x3c&lt;/code&gt; (&lt;strong&gt;11&lt;/strong&gt; times) - Skip the rest of the row to the next iteration of the for-loop.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is a full script that you can use to print out the flag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pwn&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;

&lt;span class="c1"&gt;# Set the target host and port
&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;mars.picoctf.net&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;31689&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Establishes a remote connection to the specified host and port
    using pwntools, receives initial data, and then drops into
    an interactive shell.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Connecting to &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# Establish a remote connection
&lt;/span&gt;        &lt;span class="c1"&gt;# The 'remote()' function returns a 'tube' object, which can be
&lt;/span&gt;        &lt;span class="c1"&gt;# used to send and receive data.
&lt;/span&gt;        &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;remote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Connection established!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="c1"&gt;# Receive and print initial data from the server.
&lt;/span&gt;        &lt;span class="c1"&gt;# This is useful to see any banners or prompts the binary sends
&lt;/span&gt;        &lt;span class="c1"&gt;# right after connection.
&lt;/span&gt;        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Receiving initial data...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;initial_output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;recvline&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# Adjust if the initial output is multi-line or not line-terminated
&lt;/span&gt;        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Initial output: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;initial_output&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Dropping into interactive mode. Use Ctrl+D to exit.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;create_50_on_the_stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\x30\x21\x3a\x2b\x3a\x3a\x3a\x3a\x2b\x2b\x2b\x2b\x3a\x3a\x3a\x3a\x2b\x2b\x2b\x2b\x76&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
        &lt;span class="n"&gt;line_1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;create_50_on_the_stack&lt;/span&gt;

        &lt;span class="n"&gt;insert_200_to_cols_1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\x3c\x30\x30\x70\x30\x30\x67\x3a\x3a\x3a\x2b\x2b\x2b\x30\x30\x67\x30\x21\x3a\x2b\x76&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
        &lt;span class="n"&gt;line_2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;insert_200_to_cols_1&lt;/span&gt;&lt;span class="p"&gt;[::&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

        &lt;span class="n"&gt;insert_200_to_cols_2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\x3e\x3a\x2b\x5c\x70\x30&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\x3e&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\x76&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
        &lt;span class="n"&gt;line_3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;insert_200_to_cols_2&lt;/span&gt;

        &lt;span class="n"&gt;print_each_char&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\x3c\x3a\x30\x30\x67\x67\x2c&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\x30\x21\x2b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\x3c&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;
        &lt;span class="n"&gt;line_4&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;print_each_char&lt;/span&gt;&lt;span class="p"&gt;[::&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

        &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line_1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line_2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line_3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;line_4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;interactive&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Failed to connect or interact: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;# In case of an error, ensure the connection is closed if it was opened
&lt;/span&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;locals&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connected&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;finally&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# Ensure the connection is closed when the script finishes or exits
&lt;/span&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;r&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;locals&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;connected&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Connection closed.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9gd7692f49cw3uz7e0g8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9gd7692f49cw3uz7e0g8.png" alt="Flag" width="800" height="307"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Upon executing the script, we get the flag we need for the completion of the challenge!&lt;/p&gt;

&lt;p&gt;It wasn't as bad in my thought overall, except for crafting the inputs part, where I struggled only a bit. Thank you for reading this walkthrough, and will post more walkthroughs of other challenges as well soon. Thank you!&lt;/p&gt;

</description>
      <category>picoctf</category>
      <category>ctf</category>
      <category>ghidra</category>
      <category>cybersecurity</category>
    </item>
    <item>
      <title>picoCTF "Breadth" Walkthrough</title>
      <dc:creator>SJ W</dc:creator>
      <pubDate>Tue, 10 Dec 2024 13:10:33 +0000</pubDate>
      <link>https://dev.to/7jw92nvd1klaq1/picoctf-breadth-walkthrough-3oo6</link>
      <guid>https://dev.to/7jw92nvd1klaq1/picoctf-breadth-walkthrough-3oo6</guid>
      <description>&lt;p&gt;Long time no see! It has been a while since the last time I posted basically anything in my blog, lol. For the last 3 months or so, I have been extremely busy with life in general, and it may sound like an excuse, but as a result, I didn't come up with time to take on any of the challenges. However, for the first time in a while, life finally allowed me some free time to enjoy spending some time doing what I love to do, and here I am, back with another walkthrough of one of the challenges in picoCTF. Unlike the last time I completed the challenge MATRIX, they no longer award a user with points for completing a challenge, and instead, opted to go with marking each challenge with their level of difficulty. The challenge I completed this time is called "Breadth", and I was surprised by how simple this challenge was, even though it was marked as &lt;strong&gt;Hard&lt;/strong&gt;. Just with any other RE challenges, this involves decompiling binaries using Ghidra, comparing and going through the code to ultimately find the flag that we need for the completion of the challenge. Without further ado, let's go pwn this challenge!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Examining the Binaries
&lt;/h2&gt;

&lt;p&gt;Unlike the previous challenges we have completed so far, this challenge has two binaries that a user is tasked with analyzing. Let's decompile these two binaries using Ghidra.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft0mpnfp1imihhqroon4q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft0mpnfp1imihhqroon4q.png" alt="Importing binaries" width="800" height="562"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2x75irbb0bwod0sizdyx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2x75irbb0bwod0sizdyx.png" alt="Imported binaries" width="800" height="624"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6nzx0pah5f2fmsj6fv9f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6nzx0pah5f2fmsj6fv9f.png" alt="Image description" width="800" height="519"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One of the first steps I always take when it comes to RE is to go through a list of function calls at face value, as in, trying to see if there are any interesting functions, including the function "main." &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6f9rv3kbid41n9klmwo6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6f9rv3kbid41n9klmwo6.png" alt="Image description" width="800" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flcju8yi81iq330ezujuk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flcju8yi81iq330ezujuk.png" alt="Image description" width="800" height="849"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, there are a lot of randomly named functions without giving any clue to their functionalities? Randomly clicking one of those functions and checking out its content show that there is only one line, which is "return"; however, in the viewer that shows both hexadecimal values and their translations in Assembly language, we are able to see some interesting strings.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo4o5ov8fr4g1mo10ejya.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo4o5ov8fr4g1mo10ejya.png" alt="potential flag" width="800" height="119"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It basically loads the address of the string starting with &lt;strong&gt;picoCTF{TjVxTcVux2adLBDDDFJ6FMs&lt;/strong&gt; to the register EDI, which is going to be used as the first argument for the function "puts." Every flag needed to complete a challenge in picoCTF starts with &lt;strong&gt;picoCTF{&lt;/strong&gt; followed by a series of random characters and a closed curly bracket. It's not just this function that that has a potential flag; basically, there are tens of thousands of the randomly named functions that have a string starting with &lt;strong&gt;picoCTF&lt;/strong&gt;. &lt;strong&gt;This led to believe that what we are trying to do is to sort of comparing and ascertaining the differences between these two binaries, in order to ultimately find the function that was changed between two versions.&lt;/strong&gt; So, let's try to compare them and find out what has been changed!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feqhyu13g4blvtnd5tgcg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feqhyu13g4blvtnd5tgcg.png" alt="main function" width="800" height="221"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before moving on, I had to check out the function &lt;strong&gt;main&lt;/strong&gt;, to cover all the bases to make sure that I didn't miss out on anything that might give a clue to solving this challenge. Upon inspection, I realized that there was basically nothing interesting inside the &lt;strong&gt;main&lt;/strong&gt; function, besides calling itself "Dead code".&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Comparing the Binaries
&lt;/h2&gt;

&lt;p&gt;To compare the binaries, I decided to opt for the program called &lt;strong&gt;&lt;a href="https://book.rada.re/tools/radiff2/intro.html" rel="noopener noreferrer"&gt;radiff2&lt;/a&gt;&lt;/strong&gt;, which is part of the CLI-based RE tool, &lt;strong&gt;radare2&lt;/strong&gt;. I later found out that Ghidra has a function for comparing two binaries, but at the time of tackling this challenge, I wasn't aware of its existence.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe5zpsqklzp0wpftb2091.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe5zpsqklzp0wpftb2091.png" alt="the differences between binaries" width="800" height="82"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Running the tool made me realize that there aren't as many differences between the binaries as I had initially expected. Providing no option to the tool, as you can see, doesn't yield the information on which line is added to and removed from the version 2 of the binary. To do so, we provide the option &lt;strong&gt;-u&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F34ylez0yof479wqq64nz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F34ylez0yof479wqq64nz.png" alt="the diff output" width="800" height="62"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can confirm that the changes in binaries took place in the following addresses:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;0x000002d4&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;0x0009504e&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The lines in the red show the hexadecimal values that are removed from the version 2 of the binary, and the green lines show the hex values that have replaced their removed counterparts. Let's go back to Ghidra and search for the series of hexadecimal values of &lt;strong&gt;43 52 e5 d7 4f 75 9f f9 9c 57 06 0c 2b c2 df 27 51 a7 dd a9&lt;/strong&gt; located at the address &lt;strong&gt;0x000002d4&lt;/strong&gt; to see if it is anything interesting.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbpovp122iwy8u8206xd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhbpovp122iwy8u8206xd.png" alt="Searching for hex values" width="800" height="780"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2q45ge9cm94ep4hsqr1k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2q45ge9cm94ep4hsqr1k.png" alt="Changed hex string 1" width="800" height="461"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Searching for the series of hexadecimal values results in the following section of the code. We are basically at the very beginning of the binary, and the viewer seems to show the metadata of the binary, and doesn't seem to yield anything that has to do what we need at the moment. Let's move on to the next candidate, which is the address &lt;strong&gt;0x0009504e&lt;/strong&gt;, with the series of hexadecimal values of &lt;strong&gt;48 3d 3e c7 1b 04 74 0a c3 66 0f 1f 84 00&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fikykq8uu6p0fauekxcwg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fikykq8uu6p0fauekxcwg.png" alt="memory search" width="800" height="778"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl35m2nvoqhma8fgg9n2p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl35m2nvoqhma8fgg9n2p.png" alt="potential answer" width="800" height="275"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Okay, searching for those hexadecimal values leads to the following function of the code, with the code viewer showing the potential flag, &lt;strong&gt;picoCTF{VnDB2LUf1VFJkdfDJtdYtFlMexPxXS6X}&lt;/strong&gt;. At this point, I am kind of thinking that there is no way this is a flag we need to complete the challenge. Regardless, let's proceed with submitting this to check if this is indeed the right flag. &lt;/p&gt;

&lt;p&gt;It was the right flag, and we completed the challenge. It was really simple compared to other challenges that we did, and I personally think this should be marked either &lt;strong&gt;Easy&lt;/strong&gt; and &lt;strong&gt;Medium&lt;/strong&gt;. Thank you for reading another walkthrough, and I will make sure to come back with something both interesting and challenging for the next walkthrough!&lt;/p&gt;

</description>
      <category>security</category>
      <category>reverse</category>
      <category>ctf</category>
      <category>picoctf</category>
    </item>
    <item>
      <title>Using Snippets in Visual Studio Code</title>
      <dc:creator>SJ W</dc:creator>
      <pubDate>Mon, 10 Jun 2024 14:52:53 +0000</pubDate>
      <link>https://dev.to/7jw92nvd1klaq1/using-snippets-in-visual-studio-code-1j97</link>
      <guid>https://dev.to/7jw92nvd1klaq1/using-snippets-in-visual-studio-code-1j97</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Compared to the posts I have uploaded so far, this will be super short and brief, so I will briefly go through the topic of &lt;strong&gt;Snippets&lt;/strong&gt; and how you should utilize it in Visual Studio Code, to ultimately enhance your experience as a developer! I am writing this as an assignment of the coding bootcamp that I am currently attending right now, so like I previously mentioned, it will be very concise and right to the point.&lt;/p&gt;

&lt;h2&gt;
  
  
  Snippets?
&lt;/h2&gt;

&lt;p&gt;Snippets are simply bits of code that you frequently utilize in your daily coding lives, that you tend to sort of copy and paste in so many parts of your projects. For example, to create an HTML document, you mostly start with the very line, which is &lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/code&gt; followed by various tags, such as &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt;, etc. Quickly, having to repeat the patterns of typing all those manually become somewhat tedious, and you want to find the solution to simply not having to repeat those manually, every time you create a new page for your website. That's where Snippets come in. By using a functionality that simply include the code at your whim, you tend to use less time having to set up the basic structure of code, which simply allows you to save your time and let you work towards your goal from the get-go.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use it?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuys1pl2pgeoxgdhca34l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuys1pl2pgeoxgdhca34l.png" alt="Screenshot of the download page" width="800" height="263"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I am currently writing this in context of using Visual Studio Code by Microsoft, since it seems to be the most convenient and advanced form of IDE currently. To enable the functionality of &lt;strong&gt;Snippets&lt;/strong&gt; in VSC, we first have to download the extension called &lt;strong&gt;Snippet Generator&lt;/strong&gt; by the user called &lt;strong&gt;fiore&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I will use part of what I am currently working on as an example, in an effort to showcase the usage of the extension we have downloaded. To give you context, as part of the project, I am currently creating the frontend of the bookshop project that I have been working on for some time, using TypeScript and React. In React, there is a thing called a component, which is basically a piece of reusable code that may be used throughout a project. To create a component, you tend to repeat the same process: You first have to create a function, import the same libraries, etc. Like I said, it starts feeling really tedious and annoying. I will show you in an example below, alongside &lt;strong&gt;how to register and use snippets&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Regardless of the nature of the language and the project you are working on, as long as you feel like you tend to repeat a lot of the same code over and over again, throughout your experience as a developer, it's going to come very useful.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import styled from "styled-components";
import { useForm } from "react-hook-form";
import InputText from "../components/common/InputText";
import useAuth from "../hooks/useAuth";


const Login = () =&amp;gt; {
  const { userLogin, error } = useAuth();
  const { handleSubmit, register } = useForm&amp;lt;LoginFormProps&amp;gt;();

  return (
    &amp;lt;LoginStyle&amp;gt;
        &amp;lt;form onSubmit={handleSubmit(userLogin)}&amp;gt;
            &amp;lt;InputText placeholder="Email" inputType="text" {...register("email")} /&amp;gt;
            &amp;lt;InputText placeholder="Password" inputType="password" {...register("password")} /&amp;gt;
            &amp;lt;button type="submit"&amp;gt;Login&amp;lt;/button&amp;gt;
        &amp;lt;/form&amp;gt;
        {error &amp;amp;&amp;amp; &amp;lt;p&amp;gt;{error}&amp;lt;/p&amp;gt;}
    &amp;lt;/LoginStyle&amp;gt;
  );
};


const LoginStyle = styled.div`
  padding: ${props =&amp;gt; props.theme.layout.medium.padding};
  background-color: ${props =&amp;gt; props.theme.color.background};
  border-radius: 0.25rem;
`;

export default Login;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above is a file for containing the component called &lt;code&gt;Login&lt;/code&gt;, which is rendered and displayed on the screen, when a user decides to log in to the website. Some of the notable lines in the code block includes the followings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;import styled from "styled-components";&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;styled&lt;/strong&gt; is used in every page component. Definitely a part worth being a part of the snippet.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;const Login&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;You may replace the word &lt;strong&gt;Login&lt;/strong&gt;, with any name that you would like to call your component with. Regardless of whatever page component you create, you always have to name a component, so it is something that gets repeated every time.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;const LoginStyle&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;This is also a bit of code that gets used for every page component. Just with the example above replacing the name of the component, we may replace the substring &lt;strong&gt;Login&lt;/strong&gt; with the same name that the component is named after, so it is definitely worth making this part of the snippet for creating a page component.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;export default Login;&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Lastly, using the component &lt;strong&gt;Login&lt;/strong&gt; in other files requires it being exported. This line gets used in every page component, and should be a part of the snippet. Just with the variable &lt;strong&gt;LoginStyle&lt;/strong&gt;, this contains the name of the component, so you know what to do.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Identifying bits of lines that may be incorporated and used in other files that contain a page component, we end up with the following code block:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import styled from "styled-components";

const Login = () =&amp;gt; {
  return (
    &amp;lt;LoginStyle&amp;gt;

    &amp;lt;/LoginStyle&amp;gt;
  );
};

const LoginStyle = styled.div``;

export default Login;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay, so let's get to turning the code above into the snippet!&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Replace the name of the component
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Before we turn those into a snippet that we would like to use over and over again, we have to prep them into being used as one. Like I said, in order to reuse the bits of code above, all we have to do is to replace the name of the component, which in this case is &lt;strong&gt;Login&lt;/strong&gt;, with its respective name. Replace its name with &lt;code&gt;$1&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import styled from "styled-components";

const $1 = () =&amp;gt; {
  return (
    &amp;lt;$1Style&amp;gt;

    &amp;lt;/$1Style&amp;gt;
  );
};

const $1Style = styled.div``;

export default $1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;As you can see, I have replaced the substring &lt;strong&gt;Login&lt;/strong&gt; with the substring &lt;strong&gt;$1&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Highlight, Right-click them, and choose an option for creating a snippet
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8duw1jldas5y5g35yxst.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8duw1jldas5y5g35yxst.png" alt="Image description" width="800" height="798"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have installed the extension I mentioned at the beginning, once you right-click the highlighted section of the code, you should see the option called &lt;strong&gt;Generate snippet&lt;/strong&gt; in the options.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Choose the programming language
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6n7fg73ofbbky2dl3443.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6n7fg73ofbbky2dl3443.png" alt="Image description" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What programming language is the code based on? If you are writing a snippet for C language, then choose C. Since I am trying to use this snippet to automate the initial process of creating a page component, I will choose the option &lt;strong&gt;typescriptreact&lt;/strong&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  4. Name a snippet
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ww0821c4q8wf1p9s1sq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ww0821c4q8wf1p9s1sq.png" alt="Image description" width="800" height="81"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Name it how you like!&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Choose the prefix
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3w3qlnyndwhe37arwah3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3w3qlnyndwhe37arwah3.png" alt="Image description" width="800" height="81"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The prefix is basically used to generate the snippet in your code. &lt;strong&gt;The naming convention seems to be that it should always start with the underscore character&lt;/strong&gt;, for the fact that it may be more distinct and may not be confused with other items in the autocompletion feature that VSC provides us with. &lt;/p&gt;

&lt;h3&gt;
  
  
  6. (Optional) Provide a description
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqymshgam1jqryjs5p853.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqymshgam1jqryjs5p853.png" alt="Image description" width="800" height="81"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Maybe you would like to explain to yourself what it is used for, for future references. This step is completely optional, so you may skip this step by simply pressing the &lt;strong&gt;ENTER&lt;/strong&gt; on your keyboard!&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Let's use this
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqvjh7icndw689f0f9gj6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqvjh7icndw689f0f9gj6.png" alt="Image description" width="800" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft234yy57coyrpc9h3tzj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft234yy57coyrpc9h3tzj.png" alt="Image description" width="800" height="252"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Typing the prefix of the snippet that I just created, I get prompted with the following autocomplete. By pressing &lt;strong&gt;ENTER&lt;/strong&gt;, I end up with the following snippet of code, with which I can start working on making a new page component right away!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>picoCTF "MATRIX" Walkthrough (Caution: an extremely lengthy post)</title>
      <dc:creator>SJ W</dc:creator>
      <pubDate>Fri, 24 May 2024 17:21:12 +0000</pubDate>
      <link>https://dev.to/7jw92nvd1klaq1/picoctf-matrix-walkthrough-caution-a-lengthy-post-2cci</link>
      <guid>https://dev.to/7jw92nvd1klaq1/picoctf-matrix-walkthrough-caution-a-lengthy-post-2cci</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;For this post, we are going to tackle one of the hardest challenges when it comes to the category of Reverse Engineering on picoCTF that is worth 500 points. Certainly, it was a lot harder and took more time to understand and ultimately obtain the flag than two previous challenges I completed, but it was a pretty great learning experience I would say, which taught me a good deal about the mindset and technique I should use to approach reverse engineering in general. Since the challenge is a lot harder than others, the post may be a bit longer than the usual to explain some of the findings, but I will do my best to explain things concisely and right to the point. Without further ado, let's dive in to it!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Analyzing the Binary
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Start
&lt;/h3&gt;

&lt;p&gt;Just with two previous challenges, this requires one to download the binary and examine the content inside. Let's download and import it to Ghidra for analysis.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frp2haie6sghswkl8mb1q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frp2haie6sghswkl8mb1q.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl9iwb6thzsv3oen50svm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl9iwb6thzsv3oen50svm.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Here are various details regarding the binary. In some cases, there may be some noteworthy details that might aid one in finding out what one is working with, but I don't really see anything too interesting in it, so we move on to the next phase where Ghidra commences analysis.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fue3kt3rhk500ecbxbfdd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fue3kt3rhk500ecbxbfdd.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Let's use the default options for the analysis of the binary. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F091eh5c4xjniq7w69w7s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F091eh5c4xjniq7w69w7s.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Here we are in the part of Ghidra for examining the innards of the binary. You can check the bottom-right corner to check the progress of an analysis. Once it's done, we start analyzing it ourselves!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  "Main" Function
&lt;/h3&gt;

&lt;p&gt;So, the very first thing I do is to check a list of functions that might give away some hints on how we should approach the challenge. Let's check out what kind of functions it has.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5fxrsf2rr9jdxqh1wfer.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5fxrsf2rr9jdxqh1wfer.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyum6tu328lbl2qioflwc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyum6tu328lbl2qioflwc.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I don't even see anything too noteworthy that piques my interest, and in fact, there is no &lt;code&gt;main&lt;/code&gt; function that we usually encounter in a binary written in C. Hmm, I think this binary is written in C and compiled with GCC, but I don't see any function called &lt;code&gt;main&lt;/code&gt;. However, there is a function called &lt;code&gt;__libc_start_main&lt;/code&gt;, which is called inside the function &lt;code&gt;entry&lt;/code&gt;. Let's check out the function &lt;code&gt;entry&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbc087e5ucr1v6e99dr19.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbc087e5ucr1v6e99dr19.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The function &lt;code&gt;__libc_start_main&lt;/code&gt; is provided with the total 7 arguments. There are a few interesting arguments worth checking out. In Ghidra, any function that cannot be named, due to it being stripped during the compilation process or for some reason, gets assigned a random name that starts with the substring &lt;code&gt;FUN_&lt;/code&gt;. As you can see, there are a few arguments whose name starts with the substring &lt;code&gt;FUN_&lt;/code&gt;. So what one can deduce is that one of the functions passed to the function &lt;code&gt;__libc_start_main&lt;/code&gt; is called when the program starts? Rummaging through the Internet, I come across this help page for the function &lt;code&gt;__libc_start_main&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fohgnoaukewzdyhilkaug.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fohgnoaukewzdyhilkaug.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;a href="https://refspecs.linuxbase.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/baselib---libc-start-main-.html" rel="noopener noreferrer"&gt;page&lt;/a&gt;&lt;/strong&gt; basically states that the function &lt;code&gt;__libc_start_main&lt;/code&gt; is called to initialize the environment for a process, prior to executing the main function we call. &lt;strong&gt;The very first argument passed to the function is actually a function pointer for the main function that will be executed, once the preparation for running a process is done.&lt;/strong&gt; The name of the function we must check out is &lt;strong&gt;&lt;code&gt;FUN_001010d0&lt;/code&gt;&lt;/strong&gt;. Let's move on to checking out the very function &lt;strong&gt;&lt;code&gt;FUN_001010d0&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6f6frpn45l7uqsdspbr0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6f6frpn45l7uqsdspbr0.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw4xy4lgddwbjy76cjanw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw4xy4lgddwbjy76cjanw.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Seems like we are at the right place to start figuring out how things work! &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the screenshot right above this paragraph, &lt;strong&gt;line 39&lt;/strong&gt; outputs the string &lt;strong&gt;&lt;code&gt;Have a flag!&lt;/code&gt;&lt;/strong&gt;, which subsequently in the next line outputs the flag we need to complete the challenge. Since we are in the initial stage of analysis, it is still very unclear what we should do to lead the binary to output the flag in general. Let's investigate the function for more clues.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;From line 32 to 34&lt;/strong&gt;, it repeatedly calls the function &lt;strong&gt;&lt;code&gt;FUN_00101350&lt;/code&gt;&lt;/strong&gt; inside the do...while block. The function &lt;strong&gt;&lt;code&gt;FUN_00101350&lt;/code&gt;&lt;/strong&gt; is provided with total two arguments, &lt;strong&gt;&lt;code&gt;&amp;amp;local_168&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;local_16c&lt;/code&gt;&lt;/strong&gt;. &lt;strong&gt;In line 37&lt;/strong&gt;, it checks the first value of &lt;strong&gt;&lt;code&gt;local_16c&lt;/code&gt;&lt;/strong&gt; to see if it amounts to a NULL byte (0), and once it checks that it is indeed a NULL byte, it proceeds to the next line to check whether the value of the variable &lt;strong&gt;&lt;code&gt;local_16a&lt;/code&gt;&lt;/strong&gt; is a NULL byte as well. Based on the information I laid out, you can presume that the variable &lt;code&gt;local_16c&lt;/code&gt; is passed to the function &lt;strong&gt;&lt;code&gt;FUN_00101350&lt;/code&gt;&lt;/strong&gt;, and somehow gets modified after each execution. The character &lt;strong&gt;&lt;code&gt;&amp;amp;&lt;/code&gt;&lt;/strong&gt; indicates that it accepts the address of the variable &lt;strong&gt;&lt;code&gt;local_168&lt;/code&gt;&lt;/strong&gt;. So what does the variable &lt;strong&gt;&lt;code&gt;&amp;amp;local_168&lt;/code&gt;&lt;/strong&gt;, the first argument of the function &lt;strong&gt;&lt;code&gt;FUN_00101350&lt;/code&gt;&lt;/strong&gt;, contain? &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmhbk17z4s3qcsyhn6mcu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmhbk17z4s3qcsyhn6mcu.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The variable &lt;strong&gt;&lt;code&gt;local_16a&lt;/code&gt;&lt;/strong&gt; gets assigned the address of the data &lt;strong&gt;&lt;code&gt;DAT_001020f0&lt;/code&gt;&lt;/strong&gt;. Since its name starts with the substring &lt;code&gt;DAT_&lt;/code&gt;, we can presume that it may be pointing to a constant data that is located somewhere within the binary. Let's check out what data it contains.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffs5u2uqvvtr5dfsm4rgf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffs5u2uqvvtr5dfsm4rgf.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Okay, we end up at the address of &lt;strong&gt;&lt;code&gt;0x1020f0&lt;/code&gt;&lt;/strong&gt;, and it contains a byte, which is equal to the value of &lt;code&gt;0x81&lt;/code&gt;. And in the next bytes, the values &lt;code&gt;0x75&lt;/code&gt;, &lt;code&gt;0x00&lt;/code&gt;, &lt;code&gt;0x80&lt;/code&gt; are laid out. Still not sure about its purpose. After further investigating and scrolling around, I find something interesting.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foukox5xdzozf9rwi9khn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foukox5xdzozf9rwi9khn.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Let's read this thing from bottom to top: &lt;strong&gt;m a k e  i t  o u t  a l i v e ?&lt;/strong&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F82tojx8kog46dzwbvsgj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F82tojx8kog46dzwbvsgj.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Another interesting artifacts that read like the following, when you read it from bottom to top: &lt;strong&gt;Congratulations,&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Actually, let's run the binary in a VM to check out how it works at face value.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fojgamlpg0y1yxfnarkr7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fojgamlpg0y1yxfnarkr7.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Okay, at the outset, it actually outputs the message &lt;code&gt;WELCOME to the M A T R I X \n Can you make it out alive?&lt;/code&gt;. Did you notice that at the last part of the output, it contains the substring &lt;strong&gt;make it out alive?&lt;/strong&gt; that we found during the analysis of the binary? &lt;/li&gt;
&lt;li&gt;Subsequently, I input a random string to see what happens, and press ENTER, and am instantly greeted with the message &lt;strong&gt;&lt;code&gt;You were eaten by a grue&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;So, I basically have to provide the right password to the binary to eventually obtain the flag. What's the right password? Let's go back to Ghidra and investigate further statically.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Exploring the function "FUN_00101350"
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F57ci7j1co9qag7y8i8vf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F57ci7j1co9qag7y8i8vf.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Back to Ghidra! Since the function &lt;strong&gt;&lt;code&gt;FUN_00101350&lt;/code&gt;&lt;/strong&gt; seems to be the one that dictates the overall flow of the binary, it's time to check out its contents.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fghhvhnmx5aut8fjywzap.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fghhvhnmx5aut8fjywzap.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwu7he5u8hr1tebo2y0fe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwu7he5u8hr1tebo2y0fe.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Okay, so this is quite a lot to digest, with nearly 200 lines of code welcoming us right before our eyes. Nonetheless, we have to go over the entirety of the function, in order to figure out what's going on.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6cylfqcxsmaowd2endff.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6cylfqcxsmaowd2endff.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here are some references that you should know prior to continuing. These will be referenced throughout the post, and will greatly help you understand how things work basically&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;param_1[0]&lt;/strong&gt; (&lt;code&gt;param_1 + 0 bytes&lt;/code&gt; == &lt;strong&gt;&lt;code&gt;0x00007fffffffdfc0&lt;/code&gt;&lt;/strong&gt;) - &lt;strong&gt;holds the address &lt;code&gt;0x1020f0&lt;/code&gt; (&lt;code&gt;0x00005555555560f0&lt;/code&gt;)&lt;/strong&gt;, from which the real program starts. When the function &lt;code&gt;FUN_00101350&lt;/code&gt; gets invoked, the binary passes the address &lt;strong&gt;&lt;code&gt;0x00007fffffffdfc0&lt;/code&gt;&lt;/strong&gt; to the &lt;code&gt;FUN_00101350&lt;/code&gt; as the first argument.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;param_1[1]&lt;/strong&gt; (&lt;code&gt;param_1 + 8 bytes&lt;/code&gt; == &lt;strong&gt;&lt;code&gt;0x00007fffffffdfc8&lt;/code&gt;&lt;/strong&gt;) - &lt;strong&gt;holds the offset of two bytes&lt;/strong&gt;, with which the address, at which the current command is located, is calculated, by adding &lt;code&gt;0x1020f0&lt;/code&gt; to the offset.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;param_1[2]&lt;/strong&gt; (&lt;code&gt;param_1 + 16 bytes&lt;/code&gt; == &lt;strong&gt;&lt;code&gt;0x00007fffffffdfd0&lt;/code&gt;&lt;/strong&gt;) - &lt;strong&gt;holds the address of the top of one of the makeshift stacks.&lt;/strong&gt; This stack gets used the most often. The starting address of the stack is at&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;param_1[3]&lt;/strong&gt; (&lt;code&gt;param_1 + 24 bytes&lt;/code&gt; == &lt;strong&gt;&lt;code&gt;0x00007fffffffdfd8&lt;/code&gt;&lt;/strong&gt;) - &lt;strong&gt;holds the address of the top of one of the makeshift stacks.&lt;/strong&gt; This stack gets used the least often and is mainly for temporarily storing values needed in another stack.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzii3urpcgh4qiyavbmse.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzii3urpcgh4qiyavbmse.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The register &lt;code&gt;r12&lt;/code&gt; stores the lowest address of the stack at &lt;strong&gt;param_1[3]&lt;/strong&gt;, which is &lt;strong&gt;&lt;code&gt;0x0000555555559ab0&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The register &lt;code&gt;r13&lt;/code&gt; stores the lowest address of the stack at &lt;strong&gt;param_1[2]&lt;/strong&gt;, which is &lt;strong&gt;&lt;code&gt;0x00005555555592a0&lt;/code&gt;&lt;/strong&gt;.

&lt;ul&gt;
&lt;li&gt;Every item on the stack is 2 bytes worth, which means that an item occupies 4 digits at once. And also, considering the fact that the binary is in little-endian, the value starts from the right, so you can read like &lt;strong&gt;&lt;code&gt;0x0001|0001|0000|0061&lt;/code&gt;&lt;/strong&gt;. In the screenshot above, the address at &lt;code&gt;0x00005555555592a6&lt;/code&gt; holds the value &lt;strong&gt;0061&lt;/strong&gt;, which is basically the hexadecimal value of &lt;code&gt;a&lt;/code&gt; in ASCII. &lt;code&gt;a&lt;/code&gt; is the very first character of the input I provided to the binary. So I assume that every character gets placed at the exact address of &lt;code&gt;0x00005555555592a6&lt;/code&gt; for processing. What about &lt;strong&gt;&lt;code&gt;0x0001|0001|0000|&lt;/code&gt;&lt;/strong&gt; in the front? We will see what they represent.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnbtu0e9iscb8gc4dxkd3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnbtu0e9iscb8gc4dxkd3.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At this point, all we know is that the function receives two arguments, the first one being a pointer to another pointer - &lt;strong&gt;&lt;code&gt;&amp;amp;local_168&lt;/code&gt;&lt;/strong&gt; gets passed as the first argument, and &lt;strong&gt;&lt;code&gt;&amp;amp;&lt;/code&gt;&lt;/strong&gt; indicates the memory address of the variable &lt;code&gt;local_168&lt;/code&gt;. &lt;code&gt;local_168&lt;/code&gt;, by the way, is assigned an address of &lt;code&gt;0x1020f0&lt;/code&gt; prior to entering this function - &lt;strong&gt;&lt;code&gt;local_168 = &amp;amp;DAT_001020f0;&lt;/code&gt;&lt;/strong&gt; - so we know for sure that it is a pointer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In line 17&lt;/strong&gt;, the binary dereferences the pointer &lt;code&gt;param_1&lt;/code&gt;, whose value is &lt;strong&gt;&lt;code&gt;0x00007fffffffdfc0&lt;/code&gt;&lt;/strong&gt;, and assigns it to the variable &lt;code&gt;lVar4&lt;/code&gt;. &lt;strong&gt;&lt;code&gt;lVar4&lt;/code&gt; ends up with the address of &lt;code&gt;0x1020f0&lt;/code&gt;.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In line 18&lt;/strong&gt;, it adds &lt;code&gt;1&lt;/code&gt; to the argument &lt;code&gt;param_1&lt;/code&gt;, which becomes &lt;strong&gt;&lt;code&gt;0x00007fffffffdfc8&lt;/code&gt;&lt;/strong&gt;. Since &lt;code&gt;param_1&lt;/code&gt; is a pointer of the &lt;code&gt;long&lt;/code&gt; type (8 bytes), when you add 1 to it, it is equal to adding 8. And then it change its type from &lt;code&gt;long *&lt;/code&gt; to &lt;code&gt;ushort *&lt;/code&gt;, which means that it turns into a pointer that can reference up to two bytes only. And lastly, it dereferences the pointer - the first two bytes from the address of &lt;code&gt;param_1 + 1&lt;/code&gt; (&lt;strong&gt;&lt;code&gt;0x00007fffffffdfc8&lt;/code&gt;&lt;/strong&gt;) - and assigns it to the variable &lt;strong&gt;&lt;code&gt;uVar2&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In line 19&lt;/strong&gt;, the binary adds the value of the variable &lt;strong&gt;&lt;code&gt;uVar2&lt;/code&gt;&lt;/strong&gt; to 1, and assigns it to the variable &lt;strong&gt;&lt;code&gt;uVar9&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In line 20&lt;/strong&gt;, the binary assigns the value of &lt;strong&gt;&lt;code&gt;uVar9&lt;/code&gt;&lt;/strong&gt; to the address &lt;code&gt;param_1 + 1&lt;/code&gt; (&lt;strong&gt;&lt;code&gt;0x00007fffffffdfc8&lt;/code&gt;&lt;/strong&gt;). The value at &lt;code&gt;param_1 + 1&lt;/code&gt; increments.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In line 21&lt;/strong&gt;, it adds the address &lt;code&gt;0x1020f0&lt;/code&gt; (&lt;strong&gt;&lt;code&gt;0x00005555555560f0&lt;/code&gt;&lt;/strong&gt;) stored in the variable &lt;code&gt;lVar4&lt;/code&gt; to the value of &lt;code&gt;uVar2&lt;/code&gt;, which results in the address bigger than &lt;code&gt;0x1020f0&lt;/code&gt;, subsequently converts its type from &lt;code&gt;long *&lt;/code&gt; to &lt;code&gt;byte *&lt;/code&gt;, and lastly dereference it to fetch a byte from the calculated address.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Summary&lt;/strong&gt; - &lt;strong&gt;The variable &lt;code&gt;uVar2&lt;/code&gt; in line 18 holds the offset, which in line 21 is added to the address &lt;code&gt;0x1020f0&lt;/code&gt; and subsequently dereferenced for a command that needs to be executed.&lt;/strong&gt; Let's see what kind of commands there are by going through the screenshots below.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2chhi7tku95p81altudj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2chhi7tku95p81altudj.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyejo1o4yyjl4fj1678ig.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyejo1o4yyjl4fj1678ig.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm8gf8f545uvkdi2xzd1u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm8gf8f545uvkdi2xzd1u.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx7j7tk2duagv3v1muwj9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx7j7tk2duagv3v1muwj9.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb5qgyp48h7mm1arr9pw3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb5qgyp48h7mm1arr9pw3.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Going through the screenshots, you may notice that there are some notable &lt;code&gt;case&lt;/code&gt; clauses with the following values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;0 (0x00)&lt;/li&gt;
&lt;li&gt;1 (0x01)&lt;/li&gt;
&lt;li&gt;0x10 - 0x14&lt;/li&gt;
&lt;li&gt;0x20 - 0x21&lt;/li&gt;
&lt;li&gt;0x30 - 0x34&lt;/li&gt;
&lt;li&gt;default - 0x80 - 0x81, 0xc0 - 0xc1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each case clause performs different functions for the binary, but one thing in common is that the most of them use the line at the beginning to fetch the address of the top of the stack at &lt;code&gt;param_1[2]&lt;/code&gt;. &lt;strong&gt;The binary actually utilizes two makeshift &lt;a href="https://medium.com/huawei-developers/stack-vs-heap-understanding-memory-allocation-in-programming-a83a54901416" rel="noopener noreferrer"&gt;stacks&lt;/a&gt; to store various data needed in calculation. The memory address at &lt;code&gt;param_1[2]&lt;/code&gt; stores the top address of one of the makeshift stacks that the binary uses.&lt;/strong&gt; &lt;strong&gt;Stacks used in this binary consist of values up to 2 bytes each, so when the binary pushes or pops the stack, the address of the top of the stack increases or decreases by two bytes.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Explaining what each value does in detail may be way too time-consuming, so instead, I will give you the brief explanations of what each does.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;0&lt;/strong&gt; - It doesn't do anything&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;1&lt;/strong&gt; - Executing this results in the end of the binary. This is where the binary determines whether you have successfully finished the challenge.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;0x10&lt;/strong&gt; - Copies the value at the top of the stack (-0x2) and &lt;a href="https://www.programiz.com/dsa/stack" rel="noopener noreferrer"&gt;pushes&lt;/a&gt; to the stack at &lt;code&gt;param_1[2]&lt;/code&gt;, basically duplicating the value. The value at &lt;code&gt;param_1[2]&lt;/code&gt; increases by 2.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;0x11&lt;/strong&gt; - Pops the stack, whose value at &lt;code&gt;param_1[2]&lt;/code&gt; decreases by 2.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;0x12&lt;/strong&gt; - Adds the value at the top of the stack (-0x2) to the value of the item right below (-0x4), and store it at the offset (-0x4). Then pops the stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;0x13&lt;/strong&gt; - Subtracts the value at the top of the stack (-0x2) to the value of the item right after (-0x4), and store it at the offset (-0x4). Then pops the stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;0x14&lt;/strong&gt; - Swap the value at the top of the stack (-0x2) with one right after (-0x4). &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;0x20&lt;/strong&gt; - This involves two stacks - the one at &lt;code&gt;param_1[2]&lt;/code&gt; and &lt;code&gt;param_1[3]&lt;/code&gt;. The top of the stack &lt;code&gt;param_1[2]&lt;/code&gt; pops and pushes to the stack at &lt;code&gt;param_1[3]&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;0x21&lt;/strong&gt; - This involves two stacks - the one at &lt;code&gt;param_1[2]&lt;/code&gt; and &lt;code&gt;param_1[3]&lt;/code&gt;. The top of the stack &lt;code&gt;param_1[3]&lt;/code&gt; pops and pushes to the stack at &lt;code&gt;param_1[2]&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;[&lt;em&gt;&lt;strong&gt;0x30&lt;/strong&gt;&lt;/em&gt;] - &lt;strong&gt;Pops the top of the stack at &lt;code&gt;param_1[2]&lt;/code&gt; and puts the offset at &lt;code&gt;param_1[1]&lt;/code&gt;.&lt;/strong&gt; This is where we increments and stores the value in line 20 of the function. It basically changes the flow of the program.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;[&lt;em&gt;&lt;strong&gt;0x31&lt;/strong&gt;&lt;/em&gt;] - Pops the top of the stack at &lt;code&gt;param_1[2]&lt;/code&gt; TWICE. If the second popped item has the value of &lt;code&gt;0x0000&lt;/code&gt;, then &lt;strong&gt;change the offset at &lt;code&gt;param_1[1]&lt;/code&gt; to that of the first popped item.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;0x80&lt;/strong&gt; - Pushes the value of a byte to the stack &lt;code&gt;param_1[2]&lt;/code&gt;. &lt;strong&gt;Increases the offset at &lt;code&gt;param_1[1]&lt;/code&gt; by 2.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;0x81&lt;/strong&gt; - Pushes the value of two bytes to the stack &lt;code&gt;param_1[2]&lt;/code&gt;. &lt;strong&gt;Increases the offset at &lt;code&gt;param_1[1]&lt;/code&gt; by 3.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;0xc0&lt;/strong&gt; - Accepts the input from a user.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;0xc1&lt;/strong&gt; - Outputs whatever is on the stack &lt;code&gt;param_1[2]&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most of the commands above only increment the offset at &lt;code&gt;param_1[1]&lt;/code&gt; by a number from one to three at most, but ones that are surrounded by square brackets (0x30 and 0x31) drastically change the offset using the value stored on the stack &lt;code&gt;param_1[2]&lt;/code&gt;, which means that they kind of act like the &lt;strong&gt;&lt;a href="https://en.wikipedia.org/wiki/JMP_(x86_instruction)" rel="noopener noreferrer"&gt;JMP&lt;/a&gt;&lt;/strong&gt; instruction in Assembly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Okay, so basically the offset at &lt;code&gt;param_1[1]&lt;/code&gt; starts with the value &lt;code&gt;0&lt;/code&gt;, which means that if you add the value &lt;code&gt;0&lt;/code&gt; to the base address &lt;code&gt;0x1020f0&lt;/code&gt;, it results in the address of &lt;code&gt;0x1020f0&lt;/code&gt;.&lt;/strong&gt; The very first command located at that address is the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fchbwxhbn64ua36cpac89.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fchbwxhbn64ua36cpac89.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There is a byte value of &lt;code&gt;0x81&lt;/code&gt; at the address &lt;code&gt;0x1020f0&lt;/code&gt;. Revisiting the explanation on the command related to the byte &lt;code&gt;0x81&lt;/code&gt;, we know that it has to do with pushing the value of two bytes to the stack &lt;code&gt;param_1[2]&lt;/code&gt;. So which value does it push to the stack? It combines the next two bytes &lt;strong&gt;(0x1020f1 =&amp;gt; 0x75, 0x1020f2 =&amp;gt; 0x00)&lt;/strong&gt; after the current address &lt;code&gt;0x1020f0&lt;/code&gt;, pushes the very combined value &lt;code&gt;0x0075&lt;/code&gt; to the stack &lt;code&gt;param_1[2]&lt;/code&gt;, and lastly, increases the offset at &lt;code&gt;param_1[1]&lt;/code&gt; by 3, which results in the next command at the address &lt;code&gt;0x1020f3 =&amp;gt; 0x80&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The very next command &lt;code&gt;0x80&lt;/code&gt; at &lt;code&gt;0x1020f3&lt;/code&gt; is also really similar to that of &lt;code&gt;0x81&lt;/code&gt;, albeit it pushes only the value of one byte located right after the given address to the top of the stack, and increases the offset at &lt;code&gt;param_1[1]&lt;/code&gt; by 2. So why does it try to achieve from pushing a bunch of bytes to the stack? When you run the binary, it actually outputs the intro message before you have to input an answer. Basically, it is preparing to output the message &lt;code&gt;Welcome to the M A T R I X\nCan you make it out alive?&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4bef4egzutteklxia8h8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4bef4egzutteklxia8h8.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once it fully pushes all the necessary bytes to the stack, it eventually reaches the address &lt;code&gt;0x102161&lt;/code&gt;, which holds the byte &lt;code&gt;0x81&lt;/code&gt;. It pushes the value &lt;code&gt;0x013b&lt;/code&gt; to the top of the stack, increases the offset by 3, and proceeds to the next command &lt;code&gt;0x30&lt;/code&gt; at &lt;code&gt;0x102164&lt;/code&gt;. At this point, the very value at the top of the stack at &lt;code&gt;param_1[2]&lt;/code&gt; is &lt;code&gt;0x013b&lt;/code&gt;. The command &lt;code&gt;0x30&lt;/code&gt; pops the stack at &lt;code&gt;param_1[2]&lt;/code&gt; to &lt;code&gt;param_1[1]&lt;/code&gt;. The next command after this will be calculated by adding &lt;code&gt;0x013b&lt;/code&gt; at &lt;code&gt;param_1[1]&lt;/code&gt; to the base address &lt;code&gt;0x1020f0&lt;/code&gt;, which results in the address &lt;code&gt;0x10222b&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Having learned how the binary works, one thing that comes to my mind is basically this: At which address does it accept our input? Let's go back to the dynamic analysis of the binary and check the address at which we wind up for providing our answer to the binary. &lt;/p&gt;

&lt;p&gt;One way to figure out the offset at which the command for accepting an input from a user is to stop the process upon the invocation of the function that accepts the input! Upon going through Ghidra, we discover the following code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmklwpmy5fwlr6zpmfvni.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmklwpmy5fwlr6zpmfvni.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inside the main function &lt;code&gt;FUN_001010d0&lt;/code&gt;, we discover the lines above, at which the pointers to some unknown functions are passed to the variables. Let's click each function and where we end up at.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frc2dl5uerwc1xd9dedgd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frc2dl5uerwc1xd9dedgd.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Clicking the function &lt;code&gt;FUN_00101320&lt;/code&gt;, we end up at the address &lt;code&gt;0x101320&lt;/code&gt;. Inside it, we discover the line that calls the function &lt;code&gt;getc&lt;/code&gt; whose argument is &lt;code&gt;stdin&lt;/code&gt;. The function &lt;code&gt;getc&lt;/code&gt; alongside &lt;code&gt;stdin&lt;/code&gt; as the argument causes the program to stop and accept an input from a user! So what we have to do right now is to stop the program upon the process calling the function &lt;code&gt;getc&lt;/code&gt;. And then skipping a few steps, we end up finding out the exact address, at which the command for accepting an input from a user gets invoked.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feto6bc9qonc6ktd64o74.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feto6bc9qonc6ktd64o74.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F35rkvj9yvv5buf05ts9w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F35rkvj9yvv5buf05ts9w.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The process stops upon entering the function &lt;code&gt;getc&lt;/code&gt;. With that, I type the GDB command &lt;code&gt;next&lt;/code&gt; multiple times, until the program accepts an input from a user. I type in &lt;code&gt;asdfasdfasdfasdf&lt;/code&gt; and press ENTER to continue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxu2c90nyz1vtzfp3gyqe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxu2c90nyz1vtzfp3gyqe.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Eventually, the process exits the function &lt;code&gt;getc&lt;/code&gt; and returns back to the function &lt;code&gt;FUN_00101320&lt;/code&gt;. One thing to note is that the instruction for calling the function &lt;code&gt;getc&lt;/code&gt; is located at &lt;code&gt;0x55555555532b&lt;/code&gt; inside the function &lt;code&gt;FUN_00101320&lt;/code&gt;. Let's briefly go back to check out the line that is responsible for calling the function &lt;code&gt;FUN_00101320&lt;/code&gt;. In order to do that, we go back to GDB and type the command &lt;code&gt;si&lt;/code&gt; a few times, until we exit the function &lt;strong&gt;&lt;code&gt;FUN_00101320&lt;/code&gt;&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy8pa7080ufztdkmps7f5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy8pa7080ufztdkmps7f5.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We wind up at the address &lt;code&gt;0x5555555555c5&lt;/code&gt;. Going back to Ghidra and searching for any address that ends with &lt;code&gt;5c5&lt;/code&gt;, we find the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq5uhh2djlodokyxikc30.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq5uhh2djlodokyxikc30.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The last two screenshots contain the identical instructions, and upon clicking the instruction at the address of &lt;code&gt;001015c5&lt;/code&gt; in Ghidra, we learn that the command &lt;code&gt;c0&lt;/code&gt; is responsible for accepting an input from a user.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff23rbrxbe7lnxijd5bjd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff23rbrxbe7lnxijd5bjd.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw9be79odaw86h0sg8dud.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw9be79odaw86h0sg8dud.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnktv7wl61h9fzchcp26l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnktv7wl61h9fzchcp26l.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0eey4jy9hzwc81s0yah1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0eey4jy9hzwc81s0yah1.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The very next offset at &lt;code&gt;param_1[1]&lt;/code&gt; after accepting an input from a user is &lt;code&gt;0x7c&lt;/code&gt;, and it is stored in the &lt;code&gt;rax&lt;/code&gt; register.&lt;/strong&gt; We know this for sure, for the fact that inside the function &lt;strong&gt;FUN_00101350&lt;/strong&gt;, it passes the offset to the variable &lt;code&gt;uVar2&lt;/code&gt;. When you click the variable &lt;code&gt;uVar2&lt;/code&gt; and checks the assembly code to see which line is associated with the line, you see that the instruction at the address &lt;code&gt;00101357&lt;/code&gt; passes the value to the &lt;code&gt;rax&lt;/code&gt; register.&lt;/p&gt;

&lt;h3&gt;
  
  
  Processing input from a user
&lt;/h3&gt;

&lt;p&gt;Adding the offset &lt;code&gt;0x7c&lt;/code&gt; to the base address &lt;code&gt;0x1020f0&lt;/code&gt; results in the address &lt;code&gt;0x10216c&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgg0wq3gxidnwev702dm3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgg0wq3gxidnwev702dm3.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's basically go through what is going on above. &lt;strong&gt;The only valid characters that the binary accepts for an input are the followings:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;u&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;d&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;l&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;r&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Providing a character other than those above results in the binary outputting the message &lt;strong&gt;&lt;code&gt;You were eaten by a grue&lt;/code&gt;&lt;/strong&gt;. This is how it works.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;After accepting an input, the binary pushes the first character of your input to the stack at &lt;code&gt;param_1[2]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It first pushes the value &lt;code&gt;0x75&lt;/code&gt;, a hex value for the character &lt;code&gt;u&lt;/code&gt;, to the top of the stack at &lt;code&gt;param_1[2]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Using the command &lt;code&gt;0x13&lt;/code&gt;, it subtracts the first character of your input from the value at the top of the stack, which is &lt;code&gt;0x75&lt;/code&gt;, pops the stack, and replaces the value at the top of the stack with the calculation result.&lt;/li&gt;
&lt;li&gt;It then pushes the new offset value that may potentially replace the offset at &lt;code&gt;param_1[1]&lt;/code&gt; to the stack.&lt;/li&gt;
&lt;li&gt;Using the command &lt;code&gt;0x31&lt;/code&gt;, if the calculation result is &lt;code&gt;0&lt;/code&gt;, it replaces the offset at &lt;code&gt;param_1[1]&lt;/code&gt; with the new offset value that was pushed to the stack at &lt;code&gt;param_1[2]&lt;/code&gt; in the previous step. Lastly, the stack at &lt;code&gt;param_1[2]&lt;/code&gt; pops twice. If the calculation result is not zero, proceeds to the next step:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;u&lt;/code&gt;&lt;/strong&gt; --&amp;gt; &lt;strong&gt;0x102190&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;d&lt;/code&gt;&lt;/strong&gt; --&amp;gt; &lt;strong&gt;0x10219a&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;l&lt;/code&gt;&lt;/strong&gt; --&amp;gt; &lt;strong&gt;0x1021a4&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;r&lt;/code&gt;&lt;/strong&gt; --&amp;gt; &lt;strong&gt;0x1021b0&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;The steps 2-5 repeat until a character matches &lt;code&gt;0x64 == d&lt;/code&gt;, &lt;code&gt;0x6c == l&lt;/code&gt;, or &lt;code&gt;0x72 == r&lt;/code&gt;. If not, you fail the challenge, and are greeted with the message &lt;strong&gt;&lt;code&gt;You were eaten by a grue&lt;/code&gt;&lt;/strong&gt;. &lt;strong&gt;Once the binary pushes the value &lt;code&gt;0x00FB&lt;/code&gt; to the stack at &lt;code&gt;param_1[2]&lt;/code&gt;, you can assure that you are doomed to fail the challenge&lt;/strong&gt;, because the offset &lt;code&gt;0xFB&lt;/code&gt; added to the base address &lt;code&gt;0x1020f0&lt;/code&gt; is the very address for printing out the message for the failure of the challenge!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;A series of characters consisting of those characters? &lt;code&gt;u&lt;/code&gt; means UP, &lt;code&gt;d&lt;/code&gt; means DOWN, &lt;code&gt;l&lt;/code&gt; means LEFT, and &lt;code&gt;r&lt;/code&gt; means RIGHT. Yes, you are basically navigating the maze that a single wrong step leads to your demise.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating the script that simulates the binary
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fchbwxhbn64ua36cpac89.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fchbwxhbn64ua36cpac89.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Focrtv1h7koufslbghtkz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Focrtv1h7koufslbghtkz.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2pqa2jiervy5kt3vhwxn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2pqa2jiervy5kt3vhwxn.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So far, we have extensively covered the binary. It's really grueling to manually go through every single command to fully keep track of how the binary works. Starting at the base address &lt;code&gt;0x1020f0&lt;/code&gt;, it ends at the address &lt;code&gt;0x1026c1&lt;/code&gt;, which means that there are about a thousand of the commands to go through to fully understand the inner-workings of the binary. Going through this manually might take forever to cover every little detail it contains. What about creating a Python script that simulates the inner-workings of the binary? That's what I set out to do.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why creating a script that replicates the inner-workings of the binary? I can fully control and observe how the state of both stacks at &lt;code&gt;param_1[2]&lt;/code&gt; and &lt;code&gt;param_1[3]&lt;/code&gt; changes after processing each character, without the help of GDB, outside the VM.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In order to replicate the inner-workings of the binary, we have to sort of understand how the binary works right after it accepts an input from a user. Some of the things we will take into considerations are the followings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The state of the stack at &lt;code&gt;param_1[2]&lt;/code&gt; - With every character being processed by the binary, the binary produces a sort of accumulative results and saved them to the stack for future calculations.&lt;/li&gt;
&lt;li&gt;The state of the stack at &lt;code&gt;param_1[3]&lt;/code&gt;, which is temporarily used for storing data from the stack at &lt;code&gt;param_1[2]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;How each command manipulates those two stacks above - The commands we discussed above directly manipulates the stack at &lt;code&gt;param_1[2]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is how I came up with the script that simulates the binary:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://github.com/7jw92nVd1kLaq1/picoCTF-MATRIX-scripts/blob/main/matrix_commands_json.py" rel="noopener noreferrer"&gt;Parse all the available commands&lt;/a&gt;&lt;/strong&gt; starting from the address of &lt;code&gt;0x1020f0&lt;/code&gt; to the address of &lt;code&gt;0x1026c1&lt;/code&gt;, since those are what the function &lt;strong&gt;FUN_00101350&lt;/strong&gt; executes into &lt;strong&gt;&lt;a href="https://github.com/7jw92nVd1kLaq1/picoCTF-MATRIX-scripts/blob/main/matrix_commands.json" rel="noopener noreferrer"&gt;the JSON file&lt;/a&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Create a &lt;strong&gt;&lt;a href="https://github.com/7jw92nVd1kLaq1/picoCTF-MATRIX-scripts/blob/main/matrix.py" rel="noopener noreferrer"&gt;script&lt;/a&gt;&lt;/strong&gt; that simulates the binary.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Creating a map of the maze inside the binary
&lt;/h3&gt;

&lt;p&gt;The only allowed characters are &lt;code&gt;u&lt;/code&gt;, &lt;code&gt;d&lt;/code&gt;, &lt;code&gt;l&lt;/code&gt;, and &lt;code&gt;r&lt;/code&gt;, as an input, and we can presume that those are characters used to navigate the maze that somehow leads to the exit. So, if there is the maze, can we actually map it? &lt;strong&gt;One thing that we have to take into consideration is that any step that leads the offset at &lt;code&gt;param_1[1]&lt;/code&gt; to become &lt;code&gt;0x00FB&lt;/code&gt; is to be avoided!&lt;/strong&gt; Let's go back to Ghidra and go through the list of commands a bit to find all the the addresses with the value of &lt;code&gt;fb&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fak574ba8i0xpzb1xl8y1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fak574ba8i0xpzb1xl8y1.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foof0cr0okolw14j0nxr5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foof0cr0okolw14j0nxr5.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There are a lot of &lt;code&gt;0xfb&lt;/code&gt; in a list of commands from the base address &lt;code&gt;0x1020f0&lt;/code&gt; to &lt;code&gt;0x1026c1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The first address with &lt;code&gt;0xfb&lt;/code&gt; is &lt;code&gt;0x0010218d&lt;/code&gt;. This address actually is not that relevant to the maze, since &lt;code&gt;0xfb&lt;/code&gt; at this address is pushed to the stack at &lt;code&gt;param_1[2]&lt;/code&gt; upon finding out that one of the characters of an input of a user is not &lt;code&gt;u&lt;/code&gt;, &lt;code&gt;d&lt;/code&gt;, &lt;code&gt;l&lt;/code&gt;, &lt;code&gt;r&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The really interesting address with the value of &lt;code&gt;0xfb&lt;/code&gt; starts at the very next address &lt;code&gt;0x102265&lt;/code&gt;&lt;/strong&gt;. The next values of &lt;code&gt;0xfb&lt;/code&gt; are found at the address &lt;code&gt;0x102269&lt;/code&gt;, &lt;code&gt;0x10226d&lt;/code&gt;, &lt;code&gt;0x102271&lt;/code&gt;, etc. &lt;code&gt;0xfb&lt;/code&gt; is placed next to each other at the offset of 4 bytes consistently, which continues until the address of &lt;code&gt;0x102667&lt;/code&gt;, the one with the last value of &lt;code&gt;0xfb&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ya043m8ecvgngq4w72r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ya043m8ecvgngq4w72r.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Starting from the first relevant address &lt;code&gt;0x102265&lt;/code&gt; that contains the value of &lt;code&gt;0xfb&lt;/code&gt; and scrolling down the list of commands, you notice that every value &lt;code&gt;0xfb&lt;/code&gt; is preceded by the value of &lt;code&gt;0x81&lt;/code&gt;, which makes sense since the command &lt;code&gt;0x81&lt;/code&gt; is used to push the value of two bytes located right next to it. Basically, &lt;code&gt;0x81&lt;/code&gt; pushes the value of &lt;code&gt;0x00fb&lt;/code&gt; to the stack, prior to the command &lt;code&gt;0x30&lt;/code&gt; changing the value of the offset at &lt;code&gt;param_1[1]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwr9ipqmmno3zocectfqv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwr9ipqmmno3zocectfqv.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Okay, so what we are sure about is that the actual map may be starting from the address of &lt;code&gt;0x102264&lt;/code&gt;, which holds the value of &lt;code&gt;0x81&lt;/code&gt;. Increasing the address in increments of 4, you notice that each address ends up with either the value of &lt;code&gt;0x81&lt;/code&gt; or &lt;code&gt;0x30&lt;/code&gt;. &lt;code&gt;0x30&lt;/code&gt;'s are definitely safe, for the fact that it doesn't replace the offset at &lt;code&gt;param_1[1]&lt;/code&gt; with the value of &lt;code&gt;0xFB&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fivnr163ey2gfgwm7srs7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fivnr163ey2gfgwm7srs7.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F901i0ftmavw45hnz93f8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F901i0ftmavw45hnz93f8.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The address &lt;code&gt;0x102664&lt;/code&gt; is the last address in increments of 4 starting from the address &lt;code&gt;0x102264&lt;/code&gt;, before the last value of  &lt;code&gt;0xFB&lt;/code&gt; at the address &lt;code&gt;0x102267&lt;/code&gt;. &lt;strong&gt;So if I were to subtract &lt;code&gt;0x102264&lt;/code&gt; from &lt;code&gt;0x102664&lt;/code&gt;, I get the value of 1024 in decimal. Considering the fact that the binary increases the address in increments of 4, dividing 1024 by 4 results in the value of 256. If you multiply 16 by 16, you get 256. &lt;em&gt;Are we dealing with a 16 * 16 grid?&lt;/em&gt;&lt;/strong&gt; With that very conjecture, let's create a &lt;strong&gt;&lt;a href="https://github.com/7jw92nVd1kLaq1/picoCTF-MATRIX-scripts/blob/main/matrix_map_v1.py" rel="noopener noreferrer"&gt;simple Python script&lt;/a&gt;&lt;/strong&gt; for visualizing the maze.&lt;/p&gt;

&lt;p&gt;Here is what I did:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a two dimensional array for storing 16 arrays of 16 elements.&lt;/li&gt;
&lt;li&gt;Starting from the address at &lt;code&gt;0x102264&lt;/code&gt;, you evaluate the followings:

&lt;ul&gt;
&lt;li&gt;If the value at the address is &lt;code&gt;0x81&lt;/code&gt;, and the value at the very next address is &lt;code&gt;0xFB&lt;/code&gt;, you are dead. Mark it with &lt;code&gt;-&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;If the value at the address is &lt;code&gt;0x81&lt;/code&gt;, and the value at the very next address is not &lt;code&gt;0xFB&lt;/code&gt;, you are alive. Mark it with &lt;code&gt;O&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If the value at the address is &lt;code&gt;0x30&lt;/code&gt;, you are alive. Mark it with &lt;code&gt;O&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Increase the address in increments of 4, and repeat the step 2, until the last address of &lt;code&gt;0x102664&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When you run the script above and output the grid, you get the following result.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd81lz8nd0b0da46qin77.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd81lz8nd0b0da46qin77.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F78oknaa28ijho8hhy2rx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F78oknaa28ijho8hhy2rx.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That looks like a breakthrough! So, where do we actually start the maze from? It's got to be the very first corner at the top left of the maze, and our goal is to get out of the maze through the only exit at the bottom right corner.&lt;/p&gt;

&lt;p&gt;If we were to start from the top left, we can technically exit the maze with the following: &lt;strong&gt;rrrddrrrrrrddddddlllllddrrrrdddrruuuruuuuuuurrddddddddlddrd&lt;/strong&gt;. Let's see if it works or not.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgzpjck2z4en6vd1ctpze.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgzpjck2z4en6vd1ctpze.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is wrong! Frustrating. Let's check out why we got it wrong and how we can fix this, by analyzing the binary. The key to analyzing why we got it wrong is checking the states of those two stacks at &lt;code&gt;param_1[2]&lt;/code&gt; amd &lt;code&gt;param_1[3]&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Analysis of the first failed attempt
&lt;/h3&gt;

&lt;p&gt;Let's modify the script that simulates the binary for debugging purposes, by having a look at the stacks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm2iex4ol3cs4w2tt14eg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm2iex4ol3cs4w2tt14eg.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F00cqovl2qsz7dkq13s7e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F00cqovl2qsz7dkq13s7e.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftvld4iksq5vhsr5m9b06.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftvld4iksq5vhsr5m9b06.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl44hvduxj1ftqo7v7e1l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl44hvduxj1ftqo7v7e1l.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F06jn2oa4rsb68c4c1yz5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F06jn2oa4rsb68c4c1yz5.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So moving right three times seems perfectly okay, and interestingly increments the value at the lowest item on the stack at &lt;code&gt;param_1[2]&lt;/code&gt; by 1 with each move to the right. The problem occurs when we try to move downwards. Let's see what is being pushed at the very address that seems to be causing the problem. Starting from the very first item in the 2-D array we created for visualizing the grid, we wind up at the address &lt;code&gt;0x1022F4&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsxf59b65mj8wuuyor65l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsxf59b65mj8wuuyor65l.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The address &lt;code&gt;0x1022F4&lt;/code&gt; holds the value of &lt;code&gt;0x81&lt;/code&gt;, and it seems to push the value of &lt;code&gt;0x0574&lt;/code&gt; and ultimately replace the offset at &lt;code&gt;param_1[1]&lt;/code&gt; with it. Are there several addresses with the value of &lt;code&gt;0x0574&lt;/code&gt; in the list of commands? &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbnc050gczwa7lqvebje6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbnc050gczwa7lqvebje6.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are about 5 addresses between the address of &lt;code&gt;0x102264&lt;/code&gt; and &lt;code&gt;0x102664&lt;/code&gt; with the value of &lt;code&gt;0x74&lt;/code&gt;. Let's factor that into the map, fix the &lt;strong&gt;&lt;a href="https://github.com/7jw92nVd1kLaq1/picoCTF-MATRIX-scripts/blob/main/matrix_map.py" rel="noopener noreferrer"&gt;script&lt;/a&gt;&lt;/strong&gt;, and see the difference between the previous one and the new one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxnkht09u669v5mpjn3n8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxnkht09u669v5mpjn3n8.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I marked every address that pushes &lt;code&gt;0x574&lt;/code&gt; with the plus sign, and yes, they are all placed in the path to the exit. So, how do we possibly pass this? In midst of trying various methods, I find something very, very interesting.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhba334q82iq41roo3g71.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhba334q82iq41roo3g71.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So I know for sure that, from the starting point, I can move to the right the five times max, and when I do that, the third value from the lowest on the stack at &lt;code&gt;param_1[2]&lt;/code&gt; increments by 1. &lt;strong&gt;Is the third value from the lowest on the stack the key to passing through the plus sign that we discovered earlier?&lt;/strong&gt; Let's go back to the starting point, and see if we can move downward this time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcfd3xdz9v2q9cfdr84s3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcfd3xdz9v2q9cfdr84s3.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I was able to move downward with the input &lt;strong&gt;&lt;code&gt;rrrrrlllllrrrdd&lt;/code&gt;!&lt;/strong&gt; The third value from the lowest on the stack decrements by 1, whenever you go past the plus sign! &lt;strong&gt;So, since there are total five plus signs that we have to get past, in order to exit the maze, that means that the third value from the lowest on the stack must be at least 5.&lt;/strong&gt; With that in mind, let's create the input and check if it works.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvxsdssfpqan2sah9n2ud.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvxsdssfpqan2sah9n2ud.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I provide the input &lt;strong&gt;&lt;code&gt;rrrrrlllllrrrrrlllllrrrrrlllllrrrrrlllllrrrrrlllllrrrddrrrrrrddddddlllllddrrrrdddrruuuruuuuuuurrddddddddlddrd&lt;/code&gt;&lt;/strong&gt;, and yes, it seems like I got it right! Let's try it on the binary being run on the server by picoCTF, and obtain the flag!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fljxlazgiwx2388sidyyj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fljxlazgiwx2388sidyyj.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yay! I got the flag, and submit it for the whopping 500 points!&lt;/p&gt;

&lt;p&gt;This is the end of the walkthrough, but I feel like I didn't explain about how I came to the conclusions in some parts. I will review this post and revise and fill in some of the missing parts from time to time! Thanks for reading!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/7jw92nVd1kLaq1/picoCTF-MATRIX-scripts/tree/main" rel="noopener noreferrer"&gt;Link to the scripts I made for this challenge&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>picoctf</category>
      <category>security</category>
      <category>ctf</category>
      <category>learning</category>
    </item>
    <item>
      <title>TailwindCSS vs Semantic CSS</title>
      <dc:creator>SJ W</dc:creator>
      <pubDate>Thu, 23 May 2024 17:09:08 +0000</pubDate>
      <link>https://dev.to/7jw92nvd1klaq1/tailwindcss-vs-semantic-css-411j</link>
      <guid>https://dev.to/7jw92nvd1klaq1/tailwindcss-vs-semantic-css-411j</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Currently, I am participating in the bootcamp for the full-stack web development, and started and finished the simple project using the React Framework, and in order to make your website look presentable, you cannot forgo using the HTML's best friend, CSS. Even though I could have chosen the path of creating the separate CSS files and defining various CSS rules there, I chose to go with one of the most well-used CSS frameworks at the moment, which is &lt;strong&gt;&lt;a href="https://tailwindcss.com/docs/installation"&gt;TailwindCSS&lt;/a&gt;&lt;/strong&gt;. So, at the very outset of my journey to web development, I was utterly hopeless when it comes to designing the website, and for that fact, TailwindCSS has basically been my go-to when it comes to styling my website. Despite its reputation for being verbose - to achieve something complex, you end up with an insanely long string of various classes - I gave it a shot and was instantly hooked on its usefulness and quickness for being able to come up with the somehow decent-looking website. Thus, I was no longer hopeless, and found creating the website much more enjoyable.&lt;/p&gt;

&lt;p&gt;Recently, however, watching the instructor use some sort of the CSS-related library from NPM that resembles the plain CSS, it got me questioning whether what I am doing - using the approach of TailwindCSS - is really the right thing to do or not. So I started googling basically something along the line of &lt;code&gt;Is TailwindCSS bad?&lt;/code&gt; and wound up in the thread that was wildly varying in opinions on which one is better. To get myself really acquainted with both the pros and cons of what TailwindCSS is doing versus the basic plain CSS - Semantic CSS, I thought that it would be good to do due diligence spending some time learning and comparing both pros and cons of both approaches, especially for those that are stuck in making a choice between those two.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For this post, I am going to go over each, and present both their pros and cons for the readers to equip with knowledge for making a right decision for themselves.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Semantic CSS
&lt;/h2&gt;

&lt;p&gt;Semantic CSS refers to so-called, the vanilla CSS that we first approach when we learn CSS. There are often three ways of styling various HTML elements using the plain CSS:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use the inline style for an element that you want to apply designs to.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; HTML tags inside an HTML page, for specifying the CSS rules for a specific page.&lt;/li&gt;
&lt;li&gt;Use an external file whose name ends with &lt;code&gt;.css&lt;/code&gt;. There, you may specify a bunch of rules in the same way as the approach above; however, you may import those files to one or more pages, and apply the styles to the HTML elements simultaneously.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is usually how we use the plain CSS, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;head&amp;gt;
&amp;lt;style&amp;gt;
.flex-container {
  display: flex;
  justify-content: center;
  align-items: center;
  color: red;
}
&amp;lt;/style&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
&amp;lt;div class="flex-container"&amp;gt;
  &amp;lt;div&amp;gt;Container 1&amp;lt;/div&amp;gt;
  &amp;lt;div&amp;gt;Container 2&amp;lt;/div&amp;gt;
  &amp;lt;div&amp;gt;Container 3&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The approach above is what we do with the approach number two. Basically, it requires one to create a CSS rule with a name - in the example above, it defines a rule named &lt;code&gt;.flex-container&lt;/code&gt; that must be used with the &lt;code&gt;class&lt;/code&gt; attribute in one or more elements. You are not limited to creating rules with mere classes, instead, you may even go further, by separately designing each element based on the value of its &lt;code&gt;id&lt;/code&gt; attribute by prepending it with &lt;code&gt;#&lt;/code&gt;, as well as the name of the HTML tag, which requires no special letter to be prepended. &lt;/p&gt;

&lt;p&gt;Some of the advantages for the plain CSS include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Customizable&lt;/strong&gt; - Obviously, this is the biggest advantage, but for those that must be in complete control over how the design of the website should be, then this is for you. Using the vanilla CSS makes much more sense for those that want to create something complex that involves animations and transitions. With the right amount of creativity and know-how, the sky is the limit when it comes to coming up with what you like!&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Succinct&lt;/strong&gt; - As opposed to forcing the source code of the HTML page with the bombardment of the inline classes like TailwindCSS, you can separately define CSS rules in external files ending with &lt;code&gt;.css&lt;/code&gt;, and import them to the HTML pages. Like I said, this may be a major advantage to those that prefer not seeing the source code filled with the bunch of classes. It looks way cleaner!&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fast Rendering Speed&lt;/strong&gt; - Not sure if it really matters, since there is about a 0.4-second difference when it comes to it, but based on the metrics of both LCP and FCP. FCP (first contentful paint) is a metric for how long it takes for a user to see the very first item on the page, whereas LCP (largest contentful paint) is a metric for how long it takes for the page to adequately render to be usable by a user. The vanilla CSS has apparently the advantage, which kind of makes sense, I guess due to the fact that the files of TailwindCSS are a lot bigger than that of the vanilla CSS?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Learning&lt;/strong&gt; - Actually, there is no better way to learn CSS at a much deeper level than trying to use it without the help of frameworks. You can definitely achieve a lot of insanely cool stuffs with vanilla CSS, and if that is what you are looking for and you want to learn more about CSS in general, you should at least try to stick with vanilla CSS, before moving on to frameworks, such as TailwindCSS, that may or may not make your life easier.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Tailwind CSS
&lt;/h2&gt;

&lt;p&gt;TailwindCSS is the CSS framework that provides a set of utility classes, which basically allow a user to design without creating a custom CSS file. All you have to do to use TailwindCSS is to download all the necessary files via CDN or NPM, depending on the medium you are working on your project, and simply include the available classes to HTML elements in a page. Let me show you an example below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div className="grow w-full text-left px-10 flex flex-col justify-center items-center text-white text-xl gap-5"&amp;gt;
    &amp;lt;h1&amp;gt;Select a board :(&amp;lt;/h1&amp;gt;
    &amp;lt;h1&amp;gt;Or create one!&amp;lt;/h1&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yes, it may look extremely verbose and confusing to many people. Since you have to be aware of what each class does to the looks of the element, there may be a bit of a learning curve for those that first try this, but once you get used to its class-based approach and functionalities, it becomes less of an issue moving forwards. Some of the advantages of using TailwindCSS include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Development Speed&lt;/strong&gt; - The ease of applying a predefined set of classes to elements greatly increases the web development. Also, you don't have to name each CSS rule, for the fact that everything is already named for your convenience.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintainability&lt;/strong&gt; - Imagine the situation where you have to revisit the old HTML code that has been sitting on your computer for a long, long time. The consistency in the name of every class throughout its evolution allows one to easily revisit and understand the code with ease. Imagine hiring a new developer that knows TailwindCSS and goes through your company's source code, that would greatly save time from having to educate a new developer on the design system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Frequent Update&lt;/strong&gt; - One thing that I didn't think about, but if you are frequently updating the pages of your website, then it may be better to approach using TailwindCSS. The reason is that the frequent update of the website may make keeping up with CSS rules much more arduous, than being able to directly adjust the looks of the element individually by incrementally changing the classes of each element?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consistency&lt;/strong&gt; - Using a predefined set of classes that are readily available for you throughout the entirety of the application provides the website with much more consistent looks. People who came up with this framework definitely took into considerations the need for a consistent design system. If you are not a designer, but somehow need to come up with a decent-looking UI, a gigantic mess of inline classes, with which TailwindCSS is associated, becomes less of a big deal.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Responsive&lt;/strong&gt; - I guess this is one of the biggest selling points of TailwindCSS, but yes, you can easily implement the &lt;strong&gt;&lt;a href="https://tailwindcss.com/docs/responsive-design"&gt;responsive design&lt;/a&gt;&lt;/strong&gt; of the website, by using what we call the breakpoint. Here is an example:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div className="w-full md:w-[300px] ...&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The element above has two classes, &lt;code&gt;w-full&lt;/code&gt; and &lt;code&gt;w-[300px]&lt;/code&gt;. As you can see, in front of &lt;code&gt;w-[300px]&lt;/code&gt;, it has the &lt;code&gt;md:&lt;/code&gt;, which is a breakpoint that tells the browser to resize the width of the element to &lt;code&gt;300px&lt;/code&gt;, upon the width of the viewport exceeding &lt;code&gt;768px&lt;/code&gt;. The breakpoint &lt;code&gt;md&lt;/code&gt; is associated with &lt;code&gt;768px&lt;/code&gt; by default, and if you would like to map the breakpoint with a different value, you are free to do so with the use of the config file! &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What's better?
&lt;/h3&gt;

&lt;p&gt;There is no clear, right answer to this, because both have its own strength that make each stand out over one another. I guess it all comes down to this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;If you are working on designs that are highly specific and require granular control over how it should look, then Semantic CSS may be the choice for you.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;If you are working on a project that requires fast prototyping and very little of animations and transitions, you may easily get away with TailwindCSS.&lt;/strong&gt; Not merely limited to its predefined set of utility classes, you can easily &lt;strong&gt;&lt;a href="https://tailwindcss.com/docs/adding-custom-styles"&gt;customize&lt;/a&gt;&lt;/strong&gt; it to your likings, just as well as Semantic CSS.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If anything, you can always mix the both approaches and simply take advantage of each approach! Like I said, there is simply no right or wrong answer. Both approaches warrant use cases, and you can't go wrong with any one of them. If not, then try things like &lt;strong&gt;&lt;a href="https://sass-lang.com/"&gt;SASS&lt;/a&gt;&lt;/strong&gt;, which seems like another method for utilizing CSS in a fancy way.&lt;/p&gt;

&lt;p&gt;I will continuously update this post, since I didn't spend too much time on organizing my thoughts on this topic and its structure. This post feels way too incomplete and horribly thought-out, so I will keep updating this. Thank you.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>picoCTF "Classic Crackme 0x100" Walkthrough</title>
      <dc:creator>SJ W</dc:creator>
      <pubDate>Sun, 12 May 2024 16:32:42 +0000</pubDate>
      <link>https://dev.to/7jw92nvd1klaq1/picoctf-classic-crackme-0x100-walkthrough-4d16</link>
      <guid>https://dev.to/7jw92nvd1klaq1/picoctf-classic-crackme-0x100-walkthrough-4d16</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;In this post, we are going to go through the walkthrough for the challenge of &lt;strong&gt;Classic Crackme 0x100&lt;/strong&gt; on picoCTF. The last post was also about one of the challenges hosted by picoCTF, but this one is a lot harder for the fact that it awards much more points than the last one (+200 points more)! Indeed, it felt a lot harder than the last one, for the fact that it is more complex and involves a bit more thinking on understanding the mechanism by which this binary works; however, just like the last one, this challenges involves decompiling the binary and finding the flag for the challenge. To get the flag, I used tools such as Ghidra and a custom Python script, and was able to solve it after a few tries for some time. Let's go and pwn this!&lt;/p&gt;

&lt;h1&gt;
  
  
  Classic Crackme 0x100
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz3chuqzr19wpoik9k751.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz3chuqzr19wpoik9k751.png" alt="challenge description" width="800" height="630"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just like the last challenge I completed, we need to download the binary and examine its content to find the flag we need to submit for the completion of the challenge. Let's download it and examine it with the tool Ghidra.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Examine the binary
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy9vay6ibb0j60b2z408b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy9vay6ibb0j60b2z408b.png" alt="Ghidra asking for permission to analyze the binary" width="800" height="340"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The name of the binary is &lt;strong&gt;crackme100&lt;/strong&gt;. It is basically a binary that can be run in x86_64 Linux systems.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxcr095n6dgqusvilot6h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxcr095n6dgqusvilot6h.png" alt="Binary Analysis Options" width="800" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This shows a list of options that you may use to examine a binary. Let's just leave it as it is and analyze the binary on default options.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0puchf0zetin8l9lscuo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0puchf0zetin8l9lscuo.png" alt="Ghidra screen" width="800" height="477"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Okay, we entered the screen that displays the result of a decompiled binary. The first thing I do is to check out a list of functions that exist within a binary and see if there is anything interesting. Let's check out a list of its functions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flvhpx2cs2r2jc6e5hq9b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flvhpx2cs2r2jc6e5hq9b.png" alt="a list of functions in the binary" width="800" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Checking out the list of its functions, I couldn't find any function with an interesting name, which naturally led me to examining the function &lt;code&gt;main&lt;/code&gt;, which is the entry point for every binary that is written in C.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg9mdqapvs8txcgbkhrhe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg9mdqapvs8txcgbkhrhe.png" alt="decompiled main function" width="800" height="1078"&gt;&lt;/a&gt;&lt;br&gt;
Upon checking out the function, we notice a few interesting points that hints at capturing the flag.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In line 33, it has a function called &lt;code&gt;printf&lt;/code&gt; displaying the text to the screen for instructing a user to enter the secret password. So, with that, you can easily assume that it has something to do with a user providing its input and checking if it is indeed the password that the binary is looking for.&lt;/li&gt;
&lt;li&gt;From line 51 to line 55, it seems to compare the contents of two different pointers, which made me assume that they are  both pointing to addresses containing the string. Once the comparison is made, when both match with one another, the binary jumps to line 53, which displays the following string to a screen: &lt;code&gt;SUCCESS! Here is your flag: %s\n","picoCTF{sample_flag}&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Okay, so what is the exact password that one needs to provide, in order to complete this challenge?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From line 25 to line 31, there are five variables holding the hexadecimal values of the size of 8 bytes each, one variable holding a hexadecimal value of the size of 7 bytes, and lastly, another variable holding a hexadecimal value of the size of 3 bytes. Let's see if they are placed adjacent to one another, to see if they together form a giant string.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Finding the right password
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67npt454pnpodyenru7w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67npt454pnpodyenru7w.png" alt="Stack memory layout" width="800" height="209"&gt;&lt;/a&gt;&lt;br&gt;
According to memory layout of the function &lt;code&gt;main&lt;/code&gt;, the very first variable (&lt;code&gt;local_68&lt;/code&gt;) with a hexadecimal value is placed at the address of EBP minus the offset of 0x68, and the variable after this (&lt;code&gt;local_60&lt;/code&gt;) gets placed right after the variable &lt;code&gt;local_68&lt;/code&gt;, which basically confirms that they together form a string when the variable &lt;code&gt;local_68&lt;/code&gt; is referenced using its pointer. From &lt;code&gt;local_68&lt;/code&gt; to &lt;code&gt;local_48&lt;/code&gt; holds a string the size of 40 bytes total, and the very last variable in the screenshot above, which is &lt;code&gt;local_40&lt;/code&gt;, holds a string the size of 11 bytes. So when you add 11 to 40, it results in total 51 bytes. So is the password a string of total 51 bytes? No, it is actually 51 bytes minus 1 byte, because a string in C should always end with a NULL byte to mark the end of the string, so the very last byte is likely to be a NULL byte, which doesn't count as part of a string in C. So, the password is likely to be of the size of 50 bytes. So what's the password? Let's actually run the binary in a VM and see what the password is. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs90eiq7btlxymn39vb6v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs90eiq7btlxymn39vb6v.png" alt="the binary being run in a Linux VM" width="800" height="116"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Here is a screenshot of running the binary. It basically accepts a password from a user, and compare it to the password that it holds. In this instance, inputting the random string fails and exits the process. Let's check out the password that our password is being compared to using the GNU Debugger.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvoq5uyw0mcwxwkkmqkcb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvoq5uyw0mcwxwkkmqkcb.png" alt="Image description" width="800" height="33"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzx1cwo60l8ftenbxfk0g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzx1cwo60l8ftenbxfk0g.png" alt="Image description" width="800" height="28"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;So, in order to analyze the running binary, we have to first set the breakpoint, at which we can see the string itself. A breakpoint is essentially the point at which we can completely stop the program from executing further. At line 34 is where the program stops to accept a password from a user using the function called &lt;code&gt;scanf&lt;/code&gt;. And the very second argument passed to the function &lt;code&gt;scanf&lt;/code&gt; is a variable &lt;code&gt;local_a8&lt;/code&gt;, a pointer to an array of type &lt;code&gt;char&lt;/code&gt; where our input is going to be stored. In line 51, the binary compares the alleged password &lt;code&gt;local_68&lt;/code&gt; with our input &lt;code&gt;local_a8&lt;/code&gt;, using the function called &lt;code&gt;memcmp&lt;/code&gt;. So we can set the breakpoint at where &lt;code&gt;memcmp&lt;/code&gt; is called, and check out what the alleged password is. Let's go ahead and try.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffnfvqqk5uey1g85slstt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffnfvqqk5uey1g85slstt.png" alt="Image description" width="800" height="108"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We set the breakpoint at where the function &lt;code&gt;memcmp&lt;/code&gt; is called, by typing &lt;code&gt;b memcmp&lt;/code&gt;. And we run the binary, by typing in &lt;code&gt;run&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8jy6qvrxclw8km6kt3ts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8jy6qvrxclw8km6kt3ts.png" alt="Image description" width="800" height="114"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The process is created, and it asks for the password again, just like the first time we ran the binary. I type in &lt;code&gt;asdfasdfasdfasdfasdf&lt;/code&gt; and press ENTER.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmx3pdsxtqdi4u9a7m4kn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmx3pdsxtqdi4u9a7m4kn.png" alt="Image description" width="800" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The screenshot above is a screen upon invoking the function &lt;code&gt;memcmp&lt;/code&gt;, and as you can see, we can observe what it does, by watching the contents of its registers, stacks, flags, etc. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F94lypvhap7wtvmhk9oa5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F94lypvhap7wtvmhk9oa5.png" alt="Image description" width="800" height="149"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the code section shows the exact instruction that the code is currently running. The highlighted line in green text is the next instruction that the process is planning on running when you decide to proceed, and its angle brackets(&amp;lt;&amp;gt;) has the word &lt;code&gt;memcmp+0&lt;/code&gt; inside to show the function that it is about to run, so we are basically about to run the function &lt;code&gt;memcmp&lt;/code&gt;. Let's move on to executing next instructions, by typing in &lt;code&gt;si&lt;/code&gt; and &lt;code&gt;next&lt;/code&gt; until we stumble upon something interesting.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuoluc9a1r0qp1jw36jq8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuoluc9a1r0qp1jw36jq8.png" alt="Image description" width="800" height="128"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F37q7vtux7h1r16hf1nf3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F37q7vtux7h1r16hf1nf3.png" alt="Image description" width="800" height="76"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;After executing a number of instructions, we stumble upon some weird strings on stack memory. Those strings consist of 50 characters each, and we are currently in the code where we are comparing two strings with one another. That means, one of them has to be the password that we have to match! One thing to note is that the string we provided (&lt;code&gt;asdfasdfasdfasdfasdf&lt;/code&gt;) at the start of the process is no longer to be seen, and transformed into one of those two strings. So which one is the right password, and which one is our input? Let's go back to Ghidra and find out.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvdzs1ypmjf2qu785yhik.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvdzs1ypmjf2qu785yhik.png" alt="Image description" width="738" height="344"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hovering a cursor over the hexadecimal value of &lt;code&gt;local_68&lt;/code&gt;, the very start of the right password to match our input against, we see the information above in which we can see how they are converted to either a decimal or a series of characters. Right next to &lt;code&gt;char[]&lt;/code&gt;, we see the following string &lt;strong&gt;jhpnnkpm&lt;/strong&gt;. The one thing we learned from solving the last challenge is that the string is stored in little-endian (reversed), so if we reverse the string, it becomes &lt;strong&gt;mpknnphj&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5chdsjua5shhfcy4gx1l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5chdsjua5shhfcy4gx1l.png" alt="Image description" width="800" height="31"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;So the string that starts with &lt;strong&gt;mpknnphj&lt;/strong&gt; is the password, and here is that very string! So the one that starts in &lt;strong&gt;avgl&lt;/strong&gt; is the string we have provided (&lt;code&gt;asdfasdfasdfasdfasdf&lt;/code&gt;), and it has turned into something completely new. That means there must be a section of code that converts our string into something brand new, and our job is to find the string that, after conversion, turns into the exact string that starts with &lt;strong&gt;mpknnphj&lt;/strong&gt;! Let's go back to Ghidra and see how its encryption works.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Understanding how its encryption works
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftrtp3fgbl5m803ng167i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftrtp3fgbl5m803ng167i.png" alt="Image description" width="800" height="357"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is an excerpt of code relevant for the encryption of an input we provide to the binary. The code is really hard to read in general, due to a lot of variables whose names are prefixed with &lt;code&gt;local&lt;/code&gt;. So, we will have to sort of modify the code, by changing the names of those variables into something readable, and understand how it works.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F91370bx9vunarbbnfg6p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F91370bx9vunarbbnfg6p.png" alt="Image description" width="800" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's go over the mechanism by which its encryption works, by going through the code above.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;In line 7&lt;/strong&gt;, the process determines the length of the variable &lt;code&gt;right_password&lt;/code&gt; (&lt;code&gt;local_68&lt;/code&gt;), the address of the right password, and subsequently, &lt;strong&gt;in line 8&lt;/strong&gt;, assigns its length to a variable &lt;code&gt;right_password_length&lt;/code&gt; (&lt;code&gt;local_14&lt;/code&gt;). &lt;strong&gt;In line 15&lt;/strong&gt;, the process iterates over the for-loop exactly 50 times by comparing its counter to &lt;code&gt;right_password_length&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;From line 14 to line 15&lt;/strong&gt;, we see the nested for-loops, and &lt;strong&gt;line 14&lt;/strong&gt; shows that it exactly repeats the process 3 times. With every iteration in a for-loop in &lt;strong&gt;line 14&lt;/strong&gt;, the for-loop at &lt;strong&gt;line 15&lt;/strong&gt; goes over every character of the password provided by a user. Since the counter involved in it goes from 0 to 49 (50 times), it goes over and transform each character of our input into something new, by the method with which it encrypts a string. &lt;/li&gt;
&lt;li&gt;&lt;strong&gt;So, the conclusion is that it repeats the encryption process the total three times, with every iteration transforming each letter of our input.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ferut5oqv8egoreoosx4b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ferut5oqv8egoreoosx4b.png" alt="Image description" width="734" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5kmxlqp6act30k21njsj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5kmxlqp6act30k21njsj.png" alt="Image description" width="800" height="142"&gt;&lt;/a&gt;&lt;br&gt;
Time to delve into how it transforms each character of our input! The second screenshot above from this shows a list of variables used in the encryption process. They are basically nothing but numbers used to modify each character; however, the screenshot very above this is where all the magic happen.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In line 16, it executes the line in sequential order. The variable &lt;code&gt;local_18&lt;/code&gt; holds the constant value of 0x55 (85).

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;(password_index % 0xff &amp;gt;&amp;gt; 1 &amp;amp; local_18) // Let's call this "Operand 1"&lt;/code&gt; - &lt;strong&gt;1. Two operands on modulus&lt;/strong&gt; --&amp;gt; &lt;strong&gt;2. Shift bits to right once&lt;/strong&gt; --&amp;gt; &lt;strong&gt;3. Do AND bitwise operation&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(password_index % 0xff &amp;amp; local_18) // "Operand 2"&lt;/code&gt; - &lt;strong&gt;1. Two operands on modulus&lt;/strong&gt; --&amp;gt; &lt;strong&gt;2. Do AND bitwise operation&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;local_28 = (Operand 1) + (Operand 2)&lt;/code&gt; - Add both and assign the result to the variable &lt;code&gt;local_28&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;In line 18, it utilizes the variable &lt;code&gt;local_28&lt;/code&gt; from line 16 for further computation. The variable &lt;code&gt;local_1c&lt;/code&gt; holds the hexadecimal value of 0x33 (51).

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;((int)local_28 &amp;gt;&amp;gt; 2 &amp;amp; local_1c) // "Operand 1"&lt;/code&gt; - &lt;strong&gt;1. Shift the bits of &lt;code&gt;local_28&lt;/code&gt; to right twice&lt;/strong&gt; --&amp;gt; &lt;strong&gt;2. Do AND bitwise operation&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(local_1c &amp;amp; local_28) // Operand 2&lt;/code&gt; - &lt;strong&gt;1. Do AND bitwise operation&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;local_2c = (Operand 1) + (Operand 2)&lt;/code&gt; - The result of addition of Operand 1 and Operand 2 gets assigned to the variable &lt;code&gt;local_2c&lt;/code&gt;, which is going to get utilized in the very subsequent line of code.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;For both the line 16 and 18, what I noticed is that they don't involve the input of a user at all. All they require is an index at which a character is located, so since they will always retain the constant values depending on a very index&lt;/strong&gt;, and are definitely used for encrypting each character of our input, let's make a note of this and move on to the next line, which is line 20.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In line 20, it utilizes the variable &lt;code&gt;local_2c&lt;/code&gt; from line 18 for further computation. The variable &lt;code&gt;local_20&lt;/code&gt; holds the hexadecimal value of 0xf (15).

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;((int)local_2c &amp;gt;&amp;gt; 4 &amp;amp; local_20)&lt;/code&gt; - &lt;strong&gt;1. Shift the bits of &lt;code&gt;local_2c&lt;/code&gt; to right four times&lt;/strong&gt; --&amp;gt; &lt;strong&gt;2. Do AND bitwise operation with the value 0xf&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;((int)our_input[password_index] - (int)character_a)&lt;/code&gt; - &lt;strong&gt;1. Subtract the decimal value of a character in ASCII with the decimal value of the character "a", which is 97&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(local_20 &amp;amp; local_2c)&lt;/code&gt; - &lt;strong&gt;1. Do AND bitwise operation&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;iVar1 = (Operand 1) + (Operand 2) + (Operand 3)&lt;/code&gt; - The result of the addition of Operand 1, Operand 2, and Operand 3 gets assigned to the variable &lt;code&gt;iVar1&lt;/code&gt;, which is going to get utilized in the very subsequent line of code.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;The last line is where a newly encrypted character gets placed in our input rightfully at its index. Let's briefly check out how it works, and see what we should do to somehow reverse the process of encryption.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;In line 23, it utilizes the variable &lt;code&gt;iVar1&lt;/code&gt; from line 18 for further computation.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;our_input[password_index] = character_a + (char)iVar1 + (char) (iVar1 / 0x1a) * -0x1a;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;1. Divide the value of &lt;code&gt;iVar1&lt;/code&gt; with 0x1a, which is 26 in decimal&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;2. Multiply it with -0x1a (-26)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;3. Add the result of the step 2 with the value of &lt;code&gt;iVar1&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;4. Lastly, add the result of the step 3 with the decimal value of the character "a" (97)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Summary &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From line 16 to 18, they don't involve the input of a user at all, so the only value that holds sway over the values of the variables &lt;code&gt;local_28&lt;/code&gt; and &lt;code&gt;local_2c&lt;/code&gt; is the counter (which runs from 0 to 49, since the right password is of 50 characters) in the for-loop in line 15. The variable &lt;code&gt;local_28&lt;/code&gt; is used in calculating the value of &lt;code&gt;local_2c&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Line 20 is where it involves the input of a user for calculating the value of the variable &lt;code&gt;iVar1&lt;/code&gt;. In the very next line, &lt;code&gt;iVar1&lt;/code&gt; is then used to transform a character of an input of a user, and a transformed character replaces an original character in the input of a user.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;So, that's pretty much it when it comes to encryption. At this point, &lt;strong&gt;since we are equipped with adequate information on how the program works basically, it's time to actually figure out the actual input, which will be transformed to the string &lt;code&gt;mpknnphjngbhgzydttvkahppevhkmpwgdzxsykkokriepfnrdm&lt;/code&gt; after three rounds of encryption.&lt;/strong&gt; Let's say if we were to reverse the following encryption method that consists of four steps above, the logical way to approach solving this would be to completely go backwards from the point at which we end up with the right password. &lt;/p&gt;

&lt;h2&gt;
  
  
  2. Reversing the encryption process
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5kmxlqp6act30k21njsj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5kmxlqp6act30k21njsj.png" alt="Image description" width="800" height="142"&gt;&lt;/a&gt;&lt;br&gt;
Since we learned that three rounds of encryption using the method above wind up with the following &lt;strong&gt;mpknnphjngbhgzydttvkahppevhkmpwgdzxsykkokriepfnrdm&lt;/strong&gt;, we are going to iterate over the string from its very last character to its first and eventually work our way towards the answer we need. &lt;/p&gt;

&lt;p&gt;So, beginning with the letter &lt;code&gt;m&lt;/code&gt; at the index of 49 in the string above, let's start from line 23.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;m = character_a + (char)iVar1 + (char) (iVar1 / 0x1a) * -0x1a;&lt;/code&gt; 

&lt;ul&gt;
&lt;li&gt;The only value we are missing right now is &lt;code&gt;iVar1&lt;/code&gt;. There may be more than one number that can take place of &lt;code&gt;iVar1&lt;/code&gt;, so we kind of have to guess to find out what &lt;code&gt;iVar1&lt;/code&gt; is, by placing a random value from 0 to 50 to see if we end up with the letter &lt;code&gt;m&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;iVar1 = ((int)local_2c &amp;gt;&amp;gt; 4 &amp;amp; local_20) + ((int)&lt;strong&gt;our_input[password_index]&lt;/strong&gt; - (int)character_a) + (local_20 &amp;amp; local_2c)&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Once we learn what &lt;code&gt;iVar1&lt;/code&gt; is, we move on to line 20, where  the only value missing is a character, &lt;strong&gt;our_input[password_index]&lt;/strong&gt;. We already know in advance what the values of both &lt;code&gt;local_2c&lt;/code&gt; and &lt;code&gt;local_20&lt;/code&gt; are going to be, with the realization that the only factor that controls the values of &lt;code&gt;local_28&lt;/code&gt; and &lt;code&gt;local_2c&lt;/code&gt; in line 16 and 18 is an index of our input at which a character is located. Once we guess a right character, with which we end up with the same value of &lt;code&gt;iVar1&lt;/code&gt;, we replace a letter in the string with a very letter we find.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Let's create a script for finding the right input!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feyrrrgnng0cwq0b30gme.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feyrrrgnng0cwq0b30gme.png" alt="Image description" width="800" height="519"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffxsfy9xq729hj7fh1z36.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffxsfy9xq729hj7fh1z36.png" alt="Image description" width="800" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are going to go over the code in the screenshot right above this paragraph, to see what's going on. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Remember that the only factor that controls the values of &lt;code&gt;local_28&lt;/code&gt; and &lt;code&gt;local_2c&lt;/code&gt; in line 16 and 18 is an index of a letter, at which a character is located in our input? &lt;strong&gt;The first for-loop in the screenshot basically calculates the values of &lt;code&gt;local_28&lt;/code&gt; and &lt;code&gt;local_2c&lt;/code&gt; using an index, saves them in the list &lt;code&gt;wowow&lt;/code&gt;, and puts the list &lt;code&gt;wowow&lt;/code&gt; in the list whose name is &lt;code&gt;keys&lt;/code&gt;.&lt;/strong&gt; Since the answer consists of 50 letters, the list &lt;code&gt;keys&lt;/code&gt; will have the total 50 items. Those values will be used in the helper function called &lt;code&gt;find_previous_letter&lt;/code&gt;, to find a previous letter before encryption.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The second for-loop is a rendition of the reversed process of the encryption process we saw above, and nested, for the fact that the encryption is done for three rounds. In its nested loop, it goes over from an index 49 to 0 (50 times) and uses one helper function called &lt;code&gt;find_iVar&lt;/code&gt; to guess a right number that can take place of &lt;code&gt;iVar1&lt;/code&gt;, and uses a number to find a previous letter to reverse encryption.&lt;/strong&gt; The function &lt;code&gt;find_iVar&lt;/code&gt; actually returns an integer value, which subsequently gets converted to a ASCII letter associated with the very value, so its name isn't really accurate.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo2hz9uxcd6i00dudj9ml.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo2hz9uxcd6i00dudj9ml.png" alt="Image description" width="800" height="344"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's go over two helper functions that are instrumental in finding the right answer. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the function &lt;code&gt;find_iVar&lt;/code&gt; does the job of finding the right candidates for a value that may be used as that of &lt;code&gt;iVar1&lt;/code&gt;. Once it identifies a potential value of &lt;code&gt;iVar1&lt;/code&gt;, it passes a value to the function &lt;code&gt;find_previous_letter&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The function &lt;code&gt;find_previous_letter&lt;/code&gt; accepts the potential value of &lt;code&gt;iVar1&lt;/code&gt; and an index, at which a character is located in the input, as arguments. It is the function for finding the right character (&lt;strong&gt;our_input[password_index]&lt;/strong&gt;) in the line &lt;code&gt;iVar1 = ((int)local_2c &amp;gt;&amp;gt; 4 &amp;amp; local_20) + ((int)our_input[password_index] - (int)character_a) + (local_20 &amp;amp; local_2c)&lt;/code&gt;. Occasionally, the function &lt;code&gt;find_iVar&lt;/code&gt; may find and pass a wrong candidate to the function &lt;code&gt;find_previous_letter&lt;/code&gt;, which results in not matching a single character from 'a' to 'z' - an early exit. To avoid the script from exiting early with an exception, I use the "try...except" block that skips the wrong candidate for &lt;code&gt;iVar1&lt;/code&gt;, and the script continues finding a next candidate for the value of &lt;code&gt;iVar1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Running the script outputs the following answer:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc83yzuwxcqj2rk2ifuku.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc83yzuwxcqj2rk2ifuku.png" alt="Image description" width="800" height="39"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;mmhhkjbakavyaqprqnpbuygdymyyddkratrjsbbceizsgtbcxd&lt;/strong&gt; is what the script says an answer is basically. Let's see if it spits out the right flag with the submission of the answer. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foouvufzie9g717dlhxzi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foouvufzie9g717dlhxzi.png" alt="Image description" width="800" height="63"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yay! We got the flag! I submit the very flag and get 300 points!&lt;/p&gt;

&lt;p&gt;Overall, it was a pretty interesting challenge to solve, albeit it was kind of challenging compared to the previous one I did. In the near future, I will complete more challenges and post their walkthroughs from time to time. Thank you for reading. &lt;/p&gt;

</description>
      <category>ctf</category>
      <category>security</category>
    </item>
    <item>
      <title>picoCTF "GDB Test Drive" Walkthrough</title>
      <dc:creator>SJ W</dc:creator>
      <pubDate>Thu, 02 May 2024 15:20:06 +0000</pubDate>
      <link>https://dev.to/7jw92nvd1klaq1/picoctf-gdb-test-drive-1mim</link>
      <guid>https://dev.to/7jw92nvd1klaq1/picoctf-gdb-test-drive-1mim</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;In this post, we are going to delve into something entirely different from what you usually stumble upon in my blog. A few years ago, I was sort of getting into what is known as binary exploitation, a form of hacking that finds vulnerabilities in various binaries, which essentially are executables whose extension starts with .exe in Windows OS for example. My first step into the world of binary exploitation started with going over the book called &lt;strong&gt;&lt;a href="https://www.amazon.com/Hacking-Art-Exploitation-Jon-Erickson/dp/1593271441"&gt;Hacking: The Art of Exploitation&lt;/a&gt;&lt;/strong&gt; by Jon Erickson, which basically revolves around the topic of the hacking technique called buffer overflow. For the next post right after this, I will go over some of the examples of buffer overflow by going over the CTF challenge that involves the ROP chain, but it basically initiated my fascination with the world of binary exploitation. However, due to some circumstances in life, my interest in it kind of waned eventually, and recently, after reading and watching various contents associated with it kind of rejuvenated my interest in the topic of binary exploitation again. For that exact reason, I decided to get back into it, by actually learning how to write a program in NASM (x86 assembly in Intel syntax), decompiling binaries using tools, such as Ghidra and Radare2, and completing various CTF challenges that have to do with reverse engineering in general.&lt;/p&gt;

&lt;p&gt;In this post, we are going to try the CTF challenge called &lt;strong&gt;&lt;a href="https://play.picoctf.org/practice/challenge/273?category=3&amp;amp;page=2"&gt;GDB Test Drive&lt;/a&gt;&lt;/strong&gt; on the website called picoCTF, a website that hosts a multitude of CTF challenges for anyone to try for free. I love feeling challenged in general, and feel like there is no better way to learn something other than actually doing and struggling with it.&lt;/p&gt;

&lt;h1&gt;
  
  
  GDB Test Drive
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqv6siz70z9o2ru3p37d6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqv6siz70z9o2ru3p37d6.png" alt="Challenge Description" width="800" height="772"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Okay, so here is how this challenge works: download a binary that must be examined with the tools of your choice, find and submit the flag that is hidden within the binary in order to complete the challenge. In picoCTF, every challenge works differently from another, but the one thing that remains the same is that you have to submit a flag for completing a challenge. Anyway, let's download the binary to see what's going on inside.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Examine the binary
&lt;/h2&gt;

&lt;p&gt;Let's use the tool called Ghidra to decompile the binary we downloaded for the challenge. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzzzxsmip0qxhj2omv9wa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzzzxsmip0qxhj2omv9wa.png" alt="Importing the binary" width="800" height="521"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The author of this challenge clearly instructs one to run the  binary with the GNU Debugger (GDB), but I just decided not to do so, since I felt lazy. I thought that using Ghidra would be enough to complete the challenge.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5livrkljk86fwya82hpd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5livrkljk86fwya82hpd.png" alt="Ghidra asking whether it should analyze the binary" width="800" height="344"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4uuwb4e7mf7li2mgzfac.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4uuwb4e7mf7li2mgzfac.png" alt="Showing options prior to decompiling the binary" width="800" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ghidra is showing various options for decompiling a binary, but I decided to go with the default choices, since it seems to be the safest basically. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpnzytr1igmhtj2zegfzk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpnzytr1igmhtj2zegfzk.png" alt="Image description" width="800" height="477"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we are! Now, let's get to the task of analyzing the binary and finding the flag hidden within it. To understand how the binary works, I decided to go through a list of functions that exist within the binary, to see if there is anything interesting.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnig4cz3vhm4s2uq403ba.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnig4cz3vhm4s2uq403ba.png" alt="Image description" width="800" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcotsbxkyd3f5vgit67gf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcotsbxkyd3f5vgit67gf.png" alt="Image description" width="800" height="72"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So in C, every program starts with a function called &lt;strong&gt;main&lt;/strong&gt;, and in the list of functions above, right alongside the function &lt;strong&gt;main&lt;/strong&gt;, I have discovered a function called &lt;strong&gt;rotate_encrypt&lt;/strong&gt;. The first thing that came to my mind when I saw it was that it might have something to do with encrypting a string into something illegible to our eyes, basically. Let's check out both functions of &lt;strong&gt;main&lt;/strong&gt; and &lt;strong&gt;rotate_encrypt&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Inside the function "main"
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fem98txfa4aniueb3566h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fem98txfa4aniueb3566h.png" alt="main function decompiled" width="800" height="1093"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the screenshot above, we are basically seeing a decompiled version of the function &lt;strong&gt;main&lt;/strong&gt;, and it is unclear what each variable is for, since every variable is replaced with the name that is prefixed by the word &lt;code&gt;local&lt;/code&gt;. However, as you can see, about midway through the function, that it invokes the function &lt;strong&gt;rotate_encrypt&lt;/strong&gt; with two arguments, to encrypt something that is passed to it, and one of the arguments is of a pointer, which I assume is an address to the first letter of the string. Let's see what gets passed to the function!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffyr1eidiy8jzs00jkeh2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffyr1eidiy8jzs00jkeh2.png" alt="a string that get passed to the function rotate_encrypt" width="764" height="226"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjpzg7ne5qzf5fs8q29ij.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjpzg7ne5qzf5fs8q29ij.png" alt="positions of variables within the stack" width="734" height="176"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There are total four variables that get assigned with the 8 bytes worth of data each, and we know that they are located right next to one another, for the fact that &lt;code&gt;local_38&lt;/code&gt; is located 8 bytes before &lt;code&gt;local_30&lt;/code&gt;, which is also placed next to &lt;code&gt;local_28&lt;/code&gt;, and &lt;code&gt;local_20&lt;/code&gt;. &lt;code&gt;local_38&lt;/code&gt; is located at the address of EBP plus the offset of -0x38 bytes (when the offset is lower, it gets placed higher), and since it occupies 8 bytes starting from the address, it stores the data from the address of -0x38 to -0x31. And below &lt;code&gt;local_38&lt;/code&gt; is &lt;code&gt;local_30&lt;/code&gt; whose location is the address of EBP plus the offset of -0x30 bytes. For &lt;code&gt;local_30&lt;/code&gt;, it occupies the address of -0x30 to -0x29. You can assume the same for the last two variables: &lt;code&gt;local_28&lt;/code&gt; and &lt;code&gt;local_20&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;local_38 = 0x4c75257240343a41&lt;/code&gt;: The binary assigns the hexadecimal value of &lt;code&gt;0x4c75257240343a41&lt;/code&gt; to a local variable of &lt;code&gt;local_38&lt;/code&gt; of &lt;code&gt;undefined8&lt;/code&gt; (I assume you can store the data of the size of 8 bytes). When a computer stores a string in memory (RAM), it usually stores a string in a series of numbers in succession, and each character is mapped with a certain number. Each cell in memory can hold up to a byte worth of data, and the data may represent anything from an integer of 0 to 255 (0x00-0xff). Since the number (&lt;code&gt;0x4c75257240343a41&lt;/code&gt;) we are seeing above represents eight bytes worth of data, we can split the hexadecimal value into a byte worth of data (&lt;code&gt;0x4c75257240343a41 --&amp;gt; 0x4c, 0x75, 0x25, 0x72, 0x40, 0x34, 0x3a, 0x41&lt;/code&gt;) and convert each to a character according to ASCII encoding, which results in the following string: &lt;strong&gt;Lu%r@4:A&lt;/strong&gt;. In ASCII, &lt;strong&gt;0x4c&lt;/strong&gt; is mapped with a character &lt;strong&gt;L&lt;/strong&gt;, &lt;strong&gt;0x75&lt;/strong&gt; is mapped with a character &lt;strong&gt;u&lt;/strong&gt;, and so on.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;local_30 = 0x4362383846336235&lt;/code&gt;: Let's apply the same logic to this as the example above. Converting the hexadecimal numbers of &lt;code&gt;0x4362383846336235&lt;/code&gt; to ASCII string results in the following string: &lt;strong&gt;Cb88F3b5&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;local_28 = 0x6630624760433530&lt;/code&gt;: Converting the hexadecimal numbers of &lt;code&gt;0x6630624760433530&lt;/code&gt; to ASCII string results in the following string: &lt;strong&gt;f0bG`C50&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;local_20 = 0x4e64646267353361&lt;/code&gt;: Converting the hexadecimal numbers of &lt;code&gt;0x4e64646267353361&lt;/code&gt; to ASCII string results in the following string: &lt;strong&gt;Nddbg53a&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you concatenate each one of them, it becomes the following string: &lt;strong&gt;Lu%r@4:ACb88F3b5f0bG`C50Nddbg53a&lt;/strong&gt;. So at this point, I would like to assume that the string gets converted to the flag we have to submit for the challenge. Let's see what the function &lt;strong&gt;rotate_encrypt&lt;/strong&gt; does with the string we have discovered.&lt;/p&gt;

&lt;h3&gt;
  
  
  Inside the function "rotate_encrypt"
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkexq4tw4opt9xxiw8l06.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkexq4tw4opt9xxiw8l06.png" alt="decompiled function of rotate_encrypt" width="800" height="1093"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Carefully going through the contents of the function, I have noticed the followings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It copies the content of the string (&lt;strong&gt;Lu%r@4:ACb88F3b5f0bG`C50Nddbg53a&lt;/strong&gt;) to a new pointer, with the help of the function called &lt;code&gt;strdup&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;In line 11, it stores the length of the copied string.&lt;/li&gt;
&lt;li&gt;In line 12, it iterates over the string using a for-loop.&lt;/li&gt;
&lt;li&gt;In line 13, there is a conditional logic that checks the followings: &lt;code&gt;(' ' &amp;lt; __s[local_20]) &amp;amp;&amp;amp; (__s[local_20] != '\x7f')&lt;/code&gt;. First, it evaluates whether a character in the string is bigger than ' ', which in ASCII evaluates to an integer of 32. Second, it checks whether a character in the string is not equal to &lt;code&gt;\x7f&lt;/code&gt;, which in ASCII evaluates to an integer of 127. If a character evaluates to "true" in both cases, it enters the line 14, else move on to a next character of the string.&lt;/li&gt;
&lt;li&gt;In line 14, it adds an integer of 47 to a character, and assigns it to the variable &lt;code&gt;cVar1&lt;/code&gt;. For example, if I add ')' - in ASCII, it is 41 - with 47, it becomes 88 ('X'). &lt;/li&gt;
&lt;li&gt;In line 15, it checks if the variable &lt;code&gt;cVar1&lt;/code&gt; is smaller than an integer of 127. If it evaluates to "true", then a character is replaced with the content of the variable &lt;code&gt;cVar1&lt;/code&gt;. If not then, it subtracts an integer of 94 from &lt;code&gt;cVar1&lt;/code&gt;, and replaces a character with it. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since we pretty much figure out how the program works, we can try to replicate the code with the help of Python!&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Exploit
&lt;/h2&gt;

&lt;p&gt;Since we know how the program works, we should be able to figure out the flag needed to complete the challenge!&lt;/p&gt;

&lt;h3&gt;
  
  
  Failure to submit the right flag
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu8cpjixzo72dfctfp8jf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu8cpjixzo72dfctfp8jf.png" alt="failed code" width="800" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is my first attempt at writing the python code that replicates the function &lt;strong&gt;rotate_encrypt&lt;/strong&gt; in the binary we analyzed. What it does is that it iterates over the string, converts each character in the string to a number according to ASCII encoding, adds 47 to it, and checks whether it evaluates to being bigger than an integer of 127. If it does, then it subtracts 94 from a number, and lastly, converts the number back to a character, which gets appended to a new string. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzqqcq051pi1qn1b2ez4d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzqqcq051pi1qn1b2ez4d.png" alt="What is this? it is broken" width="800" height="58"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Running the script results in the following string: &lt;strong&gt;{FTCocipr3ggub3d7_3v1rd_}5538db2&lt;/strong&gt;. Hmm, looks a bit broken, since the flag is supposed in the format of &lt;strong&gt;picoCTF{flagssss}&lt;/strong&gt;. Anyway, let's try to submit it and see if it is an answer. Surely, it is not an answer! Still though, I find some solace in the fact that reversing the first 8 characters of the string above results in the following: &lt;strong&gt;picoCTF{&lt;/strong&gt;. After learning that very important fact, I come up with a new script to rectify the situation. &lt;/p&gt;

&lt;h3&gt;
  
  
  Finding the right flag
&lt;/h3&gt;

&lt;p&gt;Since reversing the first 8 characters results in the right result of &lt;strong&gt;picoCTF{&lt;/strong&gt;, which is how a flag should start, we can reverse each data stored in each variable in the following manner:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Lu%r@4:A&lt;/strong&gt; --&amp;gt; &lt;strong&gt;A:4@r%uL&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cb88F3b5&lt;/strong&gt; --&amp;gt; &lt;strong&gt;5b3F88bC&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;f0bG`C50&lt;/strong&gt; --&amp;gt; &lt;strong&gt;05C`Gb0f&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nddbg53a&lt;/strong&gt; --&amp;gt; &lt;strong&gt;a35gbddN&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you concatenate each reversed substring, it results in the string &lt;strong&gt;A:4@r%uL5b3F88bC05C`Gb0fa35gbddN&lt;/strong&gt;. Let's run the script to see what it outputs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv8ap94hgr1e9osti0qzq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv8ap94hgr1e9osti0qzq.png" alt="Image description" width="800" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fub3zm2b1z9kma2mbs1m5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fub3zm2b1z9kma2mbs1m5.png" alt="Image description" width="800" height="36"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is still weird. Maybe, I should reverse it? Let's fix the code and output it in reverse.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9dqc78see4okfv3uaolh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9dqc78see4okfv3uaolh.png" alt="Image description" width="800" height="77"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3cdtef0ullhx3aibvjbr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3cdtef0ullhx3aibvjbr.png" alt="Image description" width="800" height="36"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Seems like the right flag! Let's submit it and see how it works! It is right, which means that I have successfully finished the challenge!&lt;/p&gt;

&lt;p&gt;It was a fun challenge overall to ultimately test my knowledge in reverse engineering. From time to time, I will post walkthroughs of different challenges that I come across in picoCTF from time to time. Thank you for reading this post!&lt;/p&gt;

</description>
      <category>ctf</category>
      <category>hacking</category>
    </item>
    <item>
      <title>How to avoid CSRF?</title>
      <dc:creator>SJ W</dc:creator>
      <pubDate>Thu, 25 Apr 2024 16:54:49 +0000</pubDate>
      <link>https://dev.to/7jw92nvd1klaq1/how-to-avoid-csrf-976</link>
      <guid>https://dev.to/7jw92nvd1klaq1/how-to-avoid-csrf-976</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Hello, in this post, we will briefly go over what CSRF is and how you can possibly avoid your users from being victims of the such attack. Recently, I have been working on the project for creating the backend of the website for selling books. While learning about how to basically handle authentication for safely allowing users to access the sensitive information, instead of using a cookie to store the credentials of a user, the instructor decided to go for a route of using HTTP headers for sending JSON web tokens in a request. When she went for that method, I couldn't initially understand why she would ditch a cookie for the storage of tokens for manually storing tokens somewhere in the browser - such as localStorage or sessionStorage, which may be vulnerable to XSS attacks. And then I realized there must be a reason for that, which led me down the path to search for an answer as to why a cookie may not be a good candidate for storing sensitive information. That's when I came across the topic of CSRF and how it may be used to make users perform unwanted actions. &lt;/p&gt;

&lt;h2&gt;
  
  
  CSRF?
&lt;/h2&gt;

&lt;p&gt;CSRF is essentially an attack that tricks users into unknowingly performing nefarious actions wanted by an attacker. An attacker utilizes the credentials stored in the cookies of the browser of a user, and its malicious website, to potentially trick users into submitting requests to a legitimate website using its credentials. For example, a legitimate website issues and sends tokens to a user via cookies, which get stored in the user's browser for its subsequent requests. With the tokens saved in the browser, an attacker somehow leads a victim to its website, creates a form for submitting information to a legitimate website and makes a user submit the form for whatever purposes, such as changing one's password or making a purchase. As you can see, allowing such an action to an attacker is often very catastrophic and may lead to the complete compromise of the website itself - imagine an attacker somehow tricking a user with admin privileges to perform actions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7t3iv4mvaodtc7e7k31w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7t3iv4mvaodtc7e7k31w.png" alt="How CSRF works" width="800" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is how it works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A user logs in to a legitimate website for whatever it intends to do.&lt;/li&gt;
&lt;li&gt;In response, a website issues tokens for allowing users to make subsequent requests to the various pages of the website. The purpose of the tokens is to allow users not to have to log in to the website every time it tries to access some pages that are only meant for authenticated users. A website issues tokens to a user via the cookies, which are stored in the browser of a user for making subsequent requests to the website.&lt;/li&gt;
&lt;li&gt;Unknowingly, an attacker lures a user to a page of the malicious website of an attacker that looks awfully similar to that of the legitimate website, and provides the form for making a POST request to the endpoint of a legitimate website. The form is meant to send data to the endpoint for changing the password of a certain user. Here is an example:
&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjpll24exwjfvtevfbegt.png" alt="Image description" width="800" height="205"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;My First Web Page&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;Welcome to my first web page!&amp;lt;/h1&amp;gt;
    &amp;lt;p&amp;gt;This is a paragraph of text&amp;lt;/p&amp;gt;
    &amp;lt;form action="https://targetWebsite.com/api/change-password" method="post"&amp;gt;
        &amp;lt;input type="hidden" id="password" name="password" value="attackProvidedPassword1234"&amp;gt;
        &amp;lt;input type="submit" value="Click Here!"&amp;gt;
    &amp;lt;/form&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;A user willfully clicks the button above to see what happens out of curiosity. At this point, it doesn't matter what information a user provides to the form, since an attacker is completely free to change the contents of the form via JavaScript, and may provide the new password of its choice - &lt;code&gt;attackProvidedPassword1234&lt;/code&gt; - on behalf of a user.&lt;/li&gt;
&lt;li&gt;A user ultimately sends a form to a legitimate website. In our example above, it sends the form to the website at the URL of a &lt;code&gt;https://targetWebsite.com/change-password&lt;/code&gt;. A legitimate website handles the form, as if a user has willfully submitted the form, and proceeds to change the password of a user with the new password provided by an attacker.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The example above is kind of a simple example of what an attacker can do to a victim with CSRF attacks, but it is still an entirely feasible scenario that may result in catastrophic results. If an attacker manages to trick an admin into performing actions vital to the management of the system itself, it may result in the complete takedown of the system. So, since we know the mechanism of the attack, how do we exactly defend our users against falling victims to such an attack?&lt;/p&gt;

&lt;h2&gt;
  
  
  Solutions
&lt;/h2&gt;

&lt;p&gt;One thing we have to know is that problems lie in the way the system is designed, so there is no way you can deflect some of the blames to a user when this attack occurs. There is no one-size-fits-all solution for completely mitigating the CSRF attacks, but rather it requires a combination of various measures that, when used together, provides a robust defense for end users. Here are some of the solutions you may implement in your system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Avoid GET requests for making changes to data&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stick with methods other than that of the GET for making changes to existing information in the system. The GET method is strictly used for retrieving the information, and it shouldn't be used for anything other than that. Imagine if there was a URL for sending money to a certain user like this, &lt;code&gt;http://bank.com/transfer.do?acct=MARIA&amp;amp;amount=100000&lt;/code&gt;. The URL accepts two query parameters, acct and amount, and an attacker may easily construct a URL for sending a certain amount of money to itself by replacing the acct with its value, and may attack it to a link, such as &lt;code&gt;&amp;lt;a href="http://bank.com/transfer.do?acct=MARIA&amp;amp;amount=100000"&amp;gt;Click Here&amp;lt;/a&amp;gt;&lt;/code&gt;. All it is left at this point is for a user to click that link and send the money to an attacker. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Use CORS&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CORS allows the system to block requests from all the domains other than the ones that it is configured to accept requests from. The system often utilizes the HTTP methods such as POST and PUT to introduce or make changes to the data, and CORS essentially protects a user by filtering requests that are from the domains that are not approved by the system itself. &lt;strong&gt;Thankfully, the browsers nowadays are set up by default to avoid making the cross-origin requests, unless the target server allows it without any restriction.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Use CSRF Token&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Provide a single-use token to a user before every POST or PUT request a user makes. For example, when a user makes changes to its password, it submits its new password alongside the token provided by the system prior to changing the password. Once the token is used, it becomes invalid and prevents itself from being reused again. In my personal opinion, I don't know how effective this method is, when frontend and backend are decoupled, but I assume this may be very useful in a case where backend serves the HTML pages. Here is how you may use it to secure your webpage from attacks: &lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Assign a session token to a user upon accessing the website. &lt;/li&gt;
&lt;li&gt;With a session token provided to a user, generate a CSRF token, which is essentially mapped to a session token of a user. &lt;/li&gt;
&lt;li&gt;A user uses it to submit a POST or PUT request to submit information, and the system verifies the CSRF token against a session token of a user to make sure that it is indeed from a user.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9kogs7yxi15or5sdtvog.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9kogs7yxi15or5sdtvog.png" alt="Image description" width="800" height="214"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Online Bookshop Project (Part 3)</title>
      <dc:creator>SJ W</dc:creator>
      <pubDate>Thu, 25 Apr 2024 10:04:18 +0000</pubDate>
      <link>https://dev.to/7jw92nvd1klaq1/online-bookshop-project-part-3-5hf5</link>
      <guid>https://dev.to/7jw92nvd1klaq1/online-bookshop-project-part-3-5hf5</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Back at it again with the project for developing an e-commerce website for selling books. In the previous post, we went over the phase of designing the database schema for the system using the E-R diagram. Through the process, we were able to identify a few key entities - User, Book and Order - pivotal in storing data that we may need for the system. And each entity shares relationships with various additional entities whose existence came about with the help of the concept of normalization, to avoid the duplication of the data. For this post, there are going to be two main things that are going to be covered: the DB initialization and creating the project using Express.js. Since we have completed the schema for the DB in the last post, we will move on to actually implementing them using MariaDB, by creating the new DB and a number of its tables. And also, we are finally going to get into the real fun of the project, which is coding! Since the bootcamp I attend is fully based on JavaScript from frontend to backend, we will go over some of the endpoints that I identified, and start implementing the API endpoints using Express.js and various third-party libraries from Node.js.&lt;/p&gt;

&lt;h2&gt;
  
  
  Identifying API Endpoints
&lt;/h2&gt;

&lt;p&gt;An API is essentially an interface that allows two different programs to talk to one another. Think of it as a bridge that connects one or more independent programs to facilitate the data exchanges. For example, the system I am creating is a facilitator of the data exchange between the browser and the database for the necessary data for buying books. Through the system, a user may be able to create, fetch, update and delete various information that it has access to. The system exposes a various number of "bridges" designed for the specific functionalities that they try to perform for end users. &lt;/p&gt;

&lt;p&gt;Since I am trying to separate the frontend and backend using the different technologies - Express.js and React.js, the backend will return data to users in the form of JSON for the most part, which is parsed by the frontend, and ultimately rendered and viewed as a component for display for the users. The backend system will adhere to various principles in regard to what we call REST. REST is an architectural style for distributing data using HTTP endpoints. Offering the guidelines rather than a set of rules and laws that one must strictly adhere to, it provides developers freedom to implement the system in a variety of ways. There are various principles that one must conform to for the system to be considered RESTful, and if you are interested in what makes the system RESTful, please check out the &lt;strong&gt;&lt;a href="https://restfulapi.net/"&gt;link here&lt;/a&gt;&lt;/strong&gt; for more information. &lt;/p&gt;

&lt;p&gt;With my mind in REST :), It is time to create each URL for every endpoint that gives off the vibes of REST. &lt;strong&gt;An endpoint is where the system and a user meet to interact with one another. There are two main important aspects when it comes to each HTTP endpoint - name and method.&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Name&lt;/strong&gt; - &lt;strong&gt;Since it represents a specific functionality or the resource that one intends to exchange or perform, the URL of an endpoint must be both simple and descriptive at face value.&lt;/strong&gt; For example, If I create an endpoint for returning the information of all the books in the system at the moment, it might be better to stick with something like "/books". Just from the name "/books", you can easily tell that it has something to do with books. To go further, if you want an endpoint that has to do with a specific book, you may utilize the URL, such as "/books/{:book_id}" - replace the curly braces and its content with a unique ID of a book you need.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Method&lt;/strong&gt; - In addition to the mere name of an endpoint, each endpoint may have one or more methods that further describe the actions to undertake for the resource you are trying to access. &lt;strong&gt;For communication, the REST style uses the HTTP protocol, and it has four main methods that achieve what we call CRUD functions (Create, Read, Update, and Delete): POST, GET, PUT, DELETE.&lt;/strong&gt; Let's say an endpoint "/users" allows the HTTP methods of POST, GET. With the POST method often being associated with submitting the information, we can use the endpoint "/users" of POST for creating a new user. And since the method GET is associated with fetching the existing information stored in the system, we can use the GET method to retrieve the information of all the users in a system.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As you can see in what I said, there is a hierarchy in each URL. For this project, the RESTful API endpoints will have their names starting with the most generic thing that you can have in the system - since we are making the backend for selling books online, books, orders, and users come to mind - and become more detailed as it goes further and gets appended by slashes (/). Here are the types of API endpoints that I identified and implemented as a result.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Authentication / Authorization&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Authors&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Books&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Carts&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Categories&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Orders&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Users&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To run the e-commerce website for selling books, you can kind of guess the types of functionalities needed based on the list above. For example, at the very basic level, we need to control who can have access to various information at various levels. To control the access to information, we will have &lt;strong&gt;Authentication / Authorization,&lt;/strong&gt; which represent functionalities that have to do with accessing the system in general. Some of the example include logging in and out, password change, etc. For the rest of the main entities, it will have the following functionalities, such as creating, retrieving, updating, and deleting objects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Structure
&lt;/h2&gt;

&lt;p&gt;Currently, the project is structured in various files and modules to keep it largely modular. Here is how I structured my project:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fheb7i4m9l8aqytlttdx5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fheb7i4m9l8aqytlttdx5.png" alt="Project Structure" width="622" height="1006"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's try to go over some of the files and folders that are integral to the system.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;controllers&lt;/strong&gt; - &lt;strong&gt;This is the directory for various controller modules that contain various callback functions directly associated with API endpoints.&lt;/strong&gt; Essentially, each file in this directory holds various functions that get invoked whenever a user makes a request to the system with a specific URL. Each callback function is directly tied to a specific endpoint. To learn more about what the controller is, check out this &lt;strong&gt;&lt;a href="https://www.geeksforgeeks.org/mvc-design-pattern/"&gt;link here&lt;/a&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;middlewares&lt;/strong&gt; - This directory stores various middleware modules. S*&lt;em&gt;imply, middleware is a function that gets invoked before the request makes its way to a function in one of the functions in the modules in the directory of controllers.&lt;/em&gt;* There are many different use cases for middleware, but it's really useful for deciding whether a user is authorized to access the resource or not. By checking the credentials provided by a user inside the request object, you can determine whether one has rights to access the certain information.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;models&lt;/strong&gt; - &lt;strong&gt;&lt;a href="https://sequelize.org/docs/v6/core-concepts/model-basics/"&gt;Sequelize&lt;/a&gt;&lt;/strong&gt; uses a class called model to represent each table in DB. Since the system involves about more than 20 different tables of the DB for storing various critical information, creating a separate directory as a module for storing various models related to the project just makes sense, and most importantly, makes the project look a lot cleaner.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;routes&lt;/strong&gt; - &lt;strong&gt;The directory stores various files that store the API endpoints, from which the interaction between the system and users may occur.&lt;/strong&gt; Each endpoint accepts the URL pattern and the callback functions that we define in files inside the directory of &lt;strong&gt;controllers&lt;/strong&gt; as arguments.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;services&lt;/strong&gt; - &lt;strong&gt;The directory contains files which store the domain-specific logic.&lt;/strong&gt; What I mean by domain is the entity, in which the system basically revolves around. For example, there is a service file just for handling the users-related logic, including the fetching of the information of a user, registration of a new user, etc. Also, since the system is all about selling books to users, you can easily infer that the system may contain logic for books as well. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;db.js&lt;/strong&gt; - A file for storing the information on the DB for the system. It initializes the instances of the modules of both MySQL and Sequelize. The system currently makes uses of both for the DB-related activities, but plans on making a full transition to Sequelize in the near future.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;app.js&lt;/strong&gt; - A file that starts the web server for accepting connections from users. This is where all the routes and middlewares get converged and merged into the server instance. To start the server, we run the following command in the project directory: "node ./app.js".&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the next blog post, we will go over each file and directory mentioned, and see how they interact with another in unison to form the backend of the e-commerce website for books!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Online Bookshop Project (Part 2)</title>
      <dc:creator>SJ W</dc:creator>
      <pubDate>Mon, 15 Apr 2024 11:40:25 +0000</pubDate>
      <link>https://dev.to/7jw92nvd1klaq1/online-bookshop-project-part-2-3l8a</link>
      <guid>https://dev.to/7jw92nvd1klaq1/online-bookshop-project-part-2-3l8a</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In the previous post, I went over the process of analysis for the project I had been tasked with, by going over the data flow diagram that I constructed to visualize the comprehensive view of the system's architecture. With the overall understanding of what the system consist of under our belt, we will move on to the next step of the analysis phase, by focusing on the database design to fully accommodate our needs for storing various data in an efficient, standardized manner. Storing data is the single most important aspect of the system, and for that simple reason, it is very imperative not to neglect getting things right. The proper design of the system results in various upsides, such as the elimination of data redundancy, high efficiency in queries, thus improving the performance of the system. In order to kick-start the process of the DB design, we will go over the E-R diagram for the system. &lt;/p&gt;

&lt;h2&gt;
  
  
  E-R Diagram
&lt;/h2&gt;

&lt;p&gt;An E-R diagram is a tool that provides a template you can use for designing the DB of the system. It allows you to visualize the overall looks of the system by picturing various "things" that we need for the system, and drawing the relationships between those "things". &lt;strong&gt;There are three main components that comprise an E-R diagram: Entity, Attributes, and Relationships.&lt;/strong&gt; &lt;strong&gt;An entity in a diagram represents something in real life that is either tangible or intangible.&lt;/strong&gt; When we talk about something tangible, some of its examples would be Human, Bike, Car, etc. When it comes to the intangibles, the examples are Order, Review, Roles. Those are something that can neither be touched nor seen, while all we can do is to sort of picture how they may seem in our brain. In order to identify entities necessary for the system, we have to focus on what the system is all about. For example, since the system I am working on is the e-commerce website for books, you can easily infer that I need to store the data related to books. And also, in order to keep track of the orders made by each customer, you can easily infer that I need an entity for both customers and orders they make. Every entity we identify will always be mapped to one of the tables of the database, and used to store various data related to it. &lt;strong&gt;Each entity has a distinct set of attributes that uniquely represent the relevant aspects of itself for the system to operate seamlessly.&lt;/strong&gt; It is our job to fully identify and specify the types of data we are trying to store in relation to each entity. &lt;strong&gt;To establish a relationship between two or more objects, you simply draw a line that connects one to another, and specify further by defining associations and dependencies that occur between objects.&lt;/strong&gt; For example, each customer in the system may have one or more orders at any given moment. And each order may be associated with one or more books, and each book may be associated with one or more orders, which makes it a "Many-to-Many Relationship." Each element in an E-R diagram serves to enhance the understanding of how the system should operate and how it may be improved by spotting the glaring problems that we may identify from each iteration of a diagram. With that being said, let's look at the E-R diagram I came up with.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzj5zikw90htj49ndaye3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzj5zikw90htj49ndaye3.png" alt="Image description" width="800" height="613"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9qr47oncyf3dgtuexcj8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9qr47oncyf3dgtuexcj8.png" alt="Image description" width="800" height="561"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, there are various entities that comprehensively represent the distinct part of the data of the system, and shares relationships with various entities. Let's briefly delve into the diagram by using a few key entities that the system will primarily deal with. &lt;strong&gt;The User entity represents a table for storing the data of users for the system.&lt;/strong&gt; As you can see, I added various attributes, such as email, password, nickname, etc. to distinguish each user from another. Every email that a user provides for registration must be completely unique, as in, there should be no other users with the same email at the time of registration. Besides those attributes I already defined, there could be various attributes that I may add in the near future, depending on the needs arising from various circumstances, but what I already defined would suffice for now. &lt;strong&gt;There is a Book entity&lt;/strong&gt;, which is not directly related to the User entity, but they are kind of intertwined with each other based on the Order entity. Just with the User entity, the Book entity is adequately structured for the system as of now, and it shares relationships with various entities in a way that aligns with the concept of &lt;strong&gt;&lt;a href="https://www.javatpoint.com/dbms-normalization"&gt;normalization&lt;/a&gt;&lt;/strong&gt;. &lt;strong&gt;The Order entity basically gives away the type of data it stores&lt;/strong&gt;, but they are directly related to both the User and Book entity. It directly stores the "id" attribute of the User entity, and indirectly stores a number of books via the Order_Books entity. There are other entities that greatly enhance the quality of the system, but the User, Book, Order are essentially the core that ultimately enable the system I envision.&lt;/p&gt;

&lt;p&gt;With that being out of the way, I think it is about time to start designing and implementing the system, based on two previous artifacts. I could potentially take more time analyzing and identifying more needs for the system before I move on to the designing phase of the project, but since this is meant to be relatively simple, rather than too complex, I believe that those will suffice as the foundation for developing the project without too many issues. If anything, I can always go back and modify them or add a few artifacts to meet the needs that I may have yet to discover and resolve. For the next post, I will go over the basic design of the API endpoints that I came up with, prior to actually implementing them using JavaScript.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Online Bookshop Project (Part 1)</title>
      <dc:creator>SJ W</dc:creator>
      <pubDate>Tue, 09 Apr 2024 15:45:09 +0000</pubDate>
      <link>https://dev.to/7jw92nvd1klaq1/online-bookshop-project-part-1-3f9l</link>
      <guid>https://dev.to/7jw92nvd1klaq1/online-bookshop-project-part-1-3f9l</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this post, I will showcase the process of working on the first e-commerce backend project for the bootcamp that I am currently attending. What I get to work on right now is not actually the first project I worked on in the bootcamp so far, but it is the first project with some difficulty far greater than others we did, since we are actually tasked with creating a first backend that sort of emulates the real-world e-commerce website for selling books. Compared to the first one, it is more challenging, with a far more number of APIs for various entities and being tasked with coming up with the designs of the SW as well. I feel like the structure of the project is still basic at best, since I didn't get to spend a lot of time thinking through the project thoroughly, but I still intend to improve upon it with each iteration for the foreseeable future.&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;Prior to bootcamp, I had a few experiences of creating a website from scratch, but in every single case, to my dismay, I abandoned them prematurely since it became way too big of a monstrosity to deal with at the end. The main culprit for why I always wound up ruining the project lies in the fact that I simply lacked the knowledge of how to create the SW in a right way. Whenever some interesting ideas come about, regardless of the sheer difficulty of a project, I impulsively took on each idea by facing it head-on and powering through the great deal of the unknown, which caused a lot of headaches, rather than carefully planning everything from the start and learning about various techniques and tools for creating the SW efficiently. Through these unfinished projects and a great deal of frustration, I learned a great deal about how vital it is to systematically approach building the SW from scratch, encompassing every important step in the process of the SW Engineering, from the stage of analysis to maintenance. What I plan on doing in this project is to apply almost all the things I learned about SW Engineering so far from the start to finish, thus actually completing the project. &lt;/p&gt;

&lt;h2&gt;
  
  
  Data Flow Diagram
&lt;/h2&gt;

&lt;p&gt;I decided to begin the project with DFD. Even though I should have come up with a list of requirements prior to creating a DFD for the SW, I remember learning that DFD is actually a great tool to reveal some of the missing requirements of the SW, by creating a diagram and visualizing how data moves from one to another. Anyway, one of the classes I am taking right now at school recently went over the topic of DFD and emphasized it as an integral part of the SW Engineering. Taking into consideration many different aspects I learned of DFD, I created the first DFD of level 0 intended to showcase the high-level overview of how the system being developed intends to operate.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2lv058burmy7hngqqk4i.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2lv058burmy7hngqqk4i.jpg" alt="Image description" width="800" height="694"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's go over what I did with the DFD above. I am sorry if it is way too fuzzy and unrecognizable, but you can see a number of symbols, whose shape sort of imply the different roles they play in the diagram. In DFD, there are total four symbols that are used - rectangle, circle, open symbol, and an arrow, and each plays an important role that in combination, aids the overall understanding of the system.&lt;/p&gt;

&lt;p&gt;Every data in the DFD starts from external entities - a rectangular symbol. &lt;strong&gt;External entities refer to anything that is outside the system, and essentially represent a starting point, from the which data originates.&lt;/strong&gt; For example, in the DFD above, a user makes a request to the system, in order to retrieve information on a certain user. To start the process, it utilizes the web browser to make a GET request with the certain URL and various information needed for inquiring about a user to the system.&lt;/p&gt;

&lt;p&gt;There are about eight different groups of functions that I described as processes - you use a circle to indicate a process in DFD. &lt;strong&gt;Processes are simply responsible for taking and processing an input, and passing the output to either one of the following types: external entity, data store or process.&lt;/strong&gt; Let's use one of the processes in the DFD to go over what I had in mind when it comes to designing the system. When an external entity called &lt;em&gt;User&lt;/em&gt; sends a request to the system, the first destination it ends up is the process called &lt;em&gt;Authentication Management&lt;/em&gt;, which encompasses any logic related to authentication and authorization. Making sure that each user accesses information that it is rightfully authorized to have access to, this process contains the logic of checking the role of a user for every request a user sends to the system, so it is extremely vital for keeping the system secure from being misused by a malicious attacker. Once a user is verified to have access to a certain API endpoint, the request gets passed to another process in the system - if the request is intended to fetch the information about a certain user in the system, then it is appropriate to direct the request to the process of &lt;em&gt;User Management&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;With each process taking a data flow as an input, a data flow gets transformed into another data flow as an output with a different name.&lt;/strong&gt; One of the caveats of DFD is that you can't re-use the same name on two or more data flows, in order to avoid ambiguity in the meaning of the data. Even if the data flow contains the same kind of data after it gets processed, it should still change its name to point out the fact that it has been processed without changes taking place. For example, in the DFD above, after a request from a User entity gets processed by the process of &lt;em&gt;Authentication Management&lt;/em&gt; to verify and check the user's identity to allow access to certain resources, it then gets passed to another process, and the process of &lt;em&gt;Authentication Management&lt;/em&gt; does not necessarily change the structure of the request, since all it does it to make sure that a user is authorized to have access to certain sensitive materials that the system stores.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data Store represents a storage that holds the information originating from the process. It retains and returns the information that may be used at some point in the future - for both the long-term and short-term - for various purposes.&lt;/strong&gt; Let's take the situation in which the system registers a user, with the information provided by a user using the registration form. When the process of &lt;em&gt;User Management&lt;/em&gt; receives a request from the user for registration, it first makes sure that the email provided by a new user is not used by another user by going through a list of users. Once it is fully verified that the email provided by a new user is unique, then the system registers a new user by saving the information of a new user, sending the data to the data store called &lt;em&gt;User Store&lt;/em&gt;, a storage intended for storing anything related to users. Now, whenever a user provides the credentials used for its registration, the process of &lt;em&gt;User Management&lt;/em&gt; verifies them against the existing information of a user and gives a signal whether the right credentials have been provided.&lt;/p&gt;

&lt;p&gt;Even though it would be lovely to delve into every aspect of the DFD possible, it would simply take way too long to go over every aspect of the DFD above, so I will stop going over the DFD part of the project, and move on to covering other things I did for the project. &lt;strong&gt;For the next post regarding this series, I will go over the E-R diagram and various other artifacts I generated for showcasing and understanding the system in a much more detailed manner.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Design Patterns (Creational)</title>
      <dc:creator>SJ W</dc:creator>
      <pubDate>Fri, 29 Mar 2024 10:25:41 +0000</pubDate>
      <link>https://dev.to/7jw92nvd1klaq1/design-pattern-part-1-3ohc</link>
      <guid>https://dev.to/7jw92nvd1klaq1/design-pattern-part-1-3ohc</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In the fifth post of my blog, I will go over all the design patterns that can be applied as solutions to various problems that we commonly face during the design process of the SW. The proper design of the SW is extremely crucial, for the fact that a poorly designed SW is an absolute nightmare to deal with as a developer and a customer. For developers, it can lead to issues of SW vulnerabilities, scalability, and the bad reputation of a company, and for customers, performance, UX, etc. What's a better way to mitigate those issues than to properly design the SW from scratch? By utilizing the well-known design strategies used by developers all across the programming industries, you can simply save others from having to spend an enormous amount of time fixing bugs and trying to make sense of the code you wrote, and ultimately make everyone who comes into contact with the SW happy. I am not trying to say that these are some sort of bulletproof, one-size-fits-all solutions that may completely eliminate all the problems that the SW may have later down the line; however, we can mitigate some of the unnecessary issues arising from haphazardly approaching the design process by incorporating the well-proven, brilliant techniques into the SW. &lt;/p&gt;

&lt;p&gt;Prior to writing this, I have to remind you first that I do not have expertise in this domain of knowledge. The primary purpose of writing this post is to deepen my understanding of design patterns by writing about what I learn and know in order to improve my skill set as a programmer. If you see any errors or mistakes in my writing about pretty much anything you see, feel free to point them out by commenting below so that I can fix them! &lt;/p&gt;

&lt;h2&gt;
  
  
  What are Design Patterns?
&lt;/h2&gt;

&lt;p&gt;Since the inception of the very first computer, the ever-shifting landscape of the computing world has seen an incomprehensible amount of software that ultimately changed the history of mankind forever. The combination of physical components that together make up the backbone of what we know as the computer is basically nothing without the SW. For example, an operating system provides us various tools that enable us to gracefully and effectively control this marvel of the invention without having to directly communicate with various pieces of hardware. It also provides us with a virtual platform on which we run various pieces of application software that aid us in conveniently managing and solving our daily tasks. In fact, you are using an application called Web Browser on your smartphone or computer to read this very post right now to learn more about Design Pattern, and the very web browser you are using is made by a group of people intent on helping people connect to the ever-increasing world of the Internet. &lt;/p&gt;

&lt;p&gt;Creating well-run, maintained software requires one to carefully plan everything from the start to make sure that it has a good foundational background from which things can flourish. Unfortunately, people who came to invent the SW at the outset of the computing world did not have the luxury of developing the SW with such knowledge. Since it was something that was so new, they had to pave ways for us to get to where we are today by experimenting and trying various things that gave birth to brilliant solutions that we resort to to this day, but it was not expected to be without issues. Without all the tools and methodologies at their disposal, creating the SW was not really efficient and structured. Some of the problems they faced included failing to meet the requirements, being overdue, budgeting, etc. Recognizing the need to address those issues, people decided to come up with the discipline of &lt;strong&gt;SW Engineering&lt;/strong&gt; which focused on processes, methodologies, and guidelines to develop the SW as efficiently and methodically as possible.&lt;/p&gt;

&lt;p&gt;Design patterns aim to make the design process of the software's development much more pleasant and, most importantly, efficient. During the design process, we often come across instances where we may have no idea how to come up with fundamentally sound solutions to mitigate the unforeseen issues that can arise in the future. &lt;strong&gt;Design patterns provide you with a set of ideas or guidelines on how to think about approaching the issues from a high-level view.&lt;/strong&gt; I would like to think of it as the template from which we can standardize approaches to coming up with robust solutions to all the recurring problems we often face. It does not necessarily give you a set of concrete solutions that may be incorporated right into the SW or encompasses only a few handfuls of situations to which design patterns could be applied, but sort of guides you towards where you may find the solutions. &lt;strong&gt;Why racking your brains trying to reinvent the wheel when you can simply resort to the proven-to-work guidance from the brilliance of those that came before us on the implementation of the SW?&lt;/strong&gt; You also get to reap the benefits of efficiency, scalability, flexibility, etc.!&lt;/p&gt;

&lt;p&gt;Currently, there are about 22 design patterns that are well-used to this day and are grouped into three different types: &lt;strong&gt;Creational, structural, and behavioral.&lt;/strong&gt; Each category aims to address different types of problems, and their names simply give away which to use for the types of problems you are trying to resolve. &lt;strong&gt;Creational Patterns&lt;/strong&gt; deal with the creation of objects. &lt;strong&gt;Structural patterns&lt;/strong&gt; deal with merging two or more objects into compositional structures. &lt;strong&gt;Behavioral patterns&lt;/strong&gt; streamline communication and delegate various responsibilities to objects in situations. As the first part of the series, I am going to cover creational patterns first—their definition, description, and how we may apply each pattern to instances that we may encounter in the future—and structural patterns and behavioral patterns subsequently. &lt;/p&gt;

&lt;h2&gt;
  
  
  Creational Patterns
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Click patterns below to navigate&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://dev.to/7jw92nvd1klaq1/design-pattern-part-1-3ohc#factory-method"&gt;Factory Method&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://dev.to/7jw92nvd1klaq1/design-pattern-part-1-3ohc#abstract-factory"&gt;Abstract Factory&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://dev.to/7jw92nvd1klaq1/design-pattern-part-1-3ohc#builder"&gt;Builder&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://dev.to/7jw92nvd1klaq1/design-pattern-part-1-3ohc#prototype"&gt;Prototype&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://dev.to/7jw92nvd1klaq1/design-pattern-part-1-3ohc#singleton"&gt;Singleton&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  My Definition
&lt;/h3&gt;

&lt;p&gt;As the name suggests, Creational Patterns mainly deal with creating objects. Here are some of the concepts that you will often encounter while going through each pattern:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Abstraction&lt;/li&gt;
&lt;li&gt;Reusability&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Abstraction
&lt;/h4&gt;

&lt;p&gt;While going through and researching various aspects of each pattern of this category, I noticed that some of the patterns use some sort of an abstract class that is used to create other subclasses. I guess, since Design Patterns are for OOP in general, any concept related to OOP is often utilized. So how does an abstract class give you an advantage? One of the biggest advantages you can have from this is that you can simply add new subclasses, without having to introduce the new code to many different parts of the system. Here is an example: let's say you are creating an application for reading the documents of various types. Regardless of the type of document, you can assume that they all share the following functionalities: you must be able to open, write, save and close. Let's say that you decide to create a function for creating a document. Instead of having to create functions for opening each type of document tediously, you can simply create a function that accepts any object that inherits the abstract class, and invoke "open()" of its abstract class. Since you can add more concrete classes that represent the new types of document, from now on, as a result, your system your system becomes more flexible!&lt;/p&gt;

&lt;h4&gt;
  
  
  Economical
&lt;/h4&gt;

&lt;p&gt;Some of the Creational Patterns we are going to delve into put a huge emphasis on the economical use of the objects. What I mean by economical is that it aims to reduce the unnecessary creation and use of the objects. One of the examples would be a pattern called &lt;strong&gt;Singleton&lt;/strong&gt;, which ensures that there is one object of a specific class that is created and gets shared by other objects. There is also a pattern called &lt;strong&gt;Prototype&lt;/strong&gt;, which deals with cloning an object, entirely avoiding the process of generating a new object - sometimes, it's just more time-consuming to generate a new object from the direct instantiation of an object than to clone an object that already exists in the memory. We will go deeper into those patterns mentioned above, so make sure to read it until the end!&lt;/p&gt;

&lt;h3&gt;
  
  
  Factory Method
&lt;/h3&gt;

&lt;p&gt;The Factory Method is a design pattern that emphasizes using an abstract Factory class to provide an interface for creating an object, with its concrete subclasses determining the type of object to create. Let's delve into this pattern with a simple example:&lt;/p&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Scenario&lt;/strong&gt;: Your business has a Payment Processor class that initially accepts only credit cards as a valid payment method. However, as your business grows, there is a demand to also accept PayPal as a payment method. Since your existing Payment Processor class is strictly for accepting credit cards, you would need to create a separate Payment Processor class for PayPal. While creating a separate Payment Processor class for each payment method might not always be overly complicated, in a large system, this approach could require various parts of the code to be updated to start accepting new payment methods. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Solution&lt;/strong&gt;: To resolve this issue, an abstract factory class can be used to provide interfaces for creating objects, although the actual object creation implementation may vary depending on its subclasses. This allows for having the client code that accepts any subclass of an abstract Factory class. The client code creates a specific payment method object, using the Factory object, and then processes a payment with it. Check out the code below or the &lt;strong&gt;&lt;a href="https://github.com/7jw92nVd1kLaq1/designPatternsExamples/blob/main/CreationalPatterns/factory-method.js"&gt;GitHub link&lt;/a&gt;&lt;/strong&gt; here
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Abstract class called PaymentMethod
class PaymentMethod {
    constructor() {
        if (this.constructor === PaymentMethod) {
            throw new Error("Cannot instantiate abstract class!");
        }
    }
    connectToGateway() {
        throw new Error("This method must be overwritten!");
    }
    pay(amount) {
        throw new Error("This method must be overwritten!");
    }
}

// Concrete class called CreditCard
class CreditCard extends PaymentMethod {
    #gateway;
    constructor(cardNumber, cardHolder, expiryDate) {
        super();
        this.#gateway = this.connectToGateway(cardNumber, cardHolder, expiryDate);
    }
    connectToGateway(cardNumber, cardHolder, expiryDate) {
        console.log('Connecting to payment gateway...');
    }
    pay(amount) {
        console.log(`Paying $${amount} using Credit Card`);
    }
}

// Concrete class called PayPal
class PayPal extends PaymentMethod {
    #gateway;
    constructor(email, password) {
        super();
        this.#gateway = this.connectToGateway(email, password);
    }
    connectToGateway(email, password) {
        console.log('Connecting to payment gateway...');
    }
    pay(amount) {
        console.log(`Paying $${amount} using PayPal`);
    }
}

// Concrete class called BitPay
class BitPay extends PaymentMethod {
    #gateway;
    constructor(email, password) {
        super();
        this.#gateway = this.connectToGateway(email, password);
    }
    connectToGateway(email, password) {
        console.log('Connecting to payment gateway...');
    }
    pay(amount) {
        console.log(`Paying $${amount} using BitPay`);
    }
}

// Abstract factory method to create payment methods
class PaymentMethodFactory {
    constructor() {
        if (this.constructor === PaymentMethodFactory) {
            throw new Error("Cannot instantiate abstract class!");
        }
    }
    createPaymentMethod() {
        throw new Error("This method must be overwritten!");
    }
}

// Concrete factory method to create CreditCard payment methods
class CreditCardPaymentMethodFactory extends PaymentMethodFactory {
    #cardNumber;
    #cardHolder;
    #expiryDate;
    constructor(cardNumber, cardHolder, expiryDate) {
        super();
        this.#cardNumber = cardNumber;
        this.#cardHolder = cardHolder;
        this.#expiryDate = expiryDate;
    }
    createPaymentMethod() {
        return new CreditCard(this.#cardNumber, this.#cardHolder, this.#expiryDate);
    }
}

// Concrete factory method to create PayPal payment methods
class PayPalPaymentMethodFactory extends PaymentMethodFactory {
    #email;
    #password;
    constructor(email, password) {
        super();
        this.#email = email;
        this.#password = password;
    }
    createPaymentMethod() {
        return new PayPal(this.#email, this.#password);
    }
}

// Concrete factory method to create BitPay payment methods
class BitPayPaymentMethodFactory extends PaymentMethodFactory {
    #email;
    #password;
    constructor(email, password) {
        super();
        this.#email = email;
        this.#password = password;
    }
    createPaymentMethod() {
        return new BitPay(this.#email, this.#password);
    }
}


// Class that will use the factory method to create payment methods
class PaymentProcessor {
    #paymentMethodFactory;
    #paymentMethod;
    constructor(paymentMethodFactory) {
        this.#paymentMethodFactory = paymentMethodFactory;
    }
    processPayment(amount) {
        this.#paymentMethod = this.#paymentMethodFactory.createPaymentMethod();
        this.#paymentMethod.pay(amount);
    }
}

// Usage
const creditCardPaymentMethodFactory = new CreditCardPaymentMethodFactory('123123', 'John Doe', '12/23');
const paymentProcessor = new PaymentProcessor(creditCardPaymentMethodFactory);
paymentProcessor.processPayment(100);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, there are two abstract classes that essentially enable this pattern: &lt;strong&gt;PaymentMethod&lt;/strong&gt; and &lt;strong&gt;PaymentMethodFactory&lt;/strong&gt;. The &lt;strong&gt;PaymentMethod&lt;/strong&gt; class represents an interface, with which the various types of payment method are implemented, and the &lt;strong&gt;PaymentMethodFactory&lt;/strong&gt; class represents an abstract class, with which various subclasses are created depending on the types of objects they intend to generate. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PaymentMethod&lt;/strong&gt; class has one abstract function called "pay" that needs to be overridden by its subclasses. The reason why is that, regardless of the type of payment method you use, the only purpose of payment method is to allow users to pay for items with their choices of payment method. That means that every payment method should commonly share the function of "pay" a user may use to make a payment; however, the exact implementation of every subclass may vary. To allow the system to accept payments using credit cards, PayPal and BitPay(Cryptocurrency), the business decides to implement each payment method, creating a subclass inheriting the &lt;strong&gt;PaymentMethod&lt;/strong&gt; class and uniquely implementing its own version of the function "pay."&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PaymentMethodFactory&lt;/strong&gt; class, just like the &lt;strong&gt;PaymentMethod&lt;/strong&gt; class, provides an interface for a subclass generating an object of a single type of payment method. Since each subclass that extends the &lt;strong&gt;PaymentMethod&lt;/strong&gt; class is responsible for creating a single type of object, we can assume that we will have to create a subclass that extends the &lt;strong&gt;PaymentMethodFactory&lt;/strong&gt; class for each payment method available. The only job that a Factory subclass performs is providing an interface for generating an object of a certain payment method. Every Factory subclass has to implement its own version of a function called "createPaymentMethod", because the way an object of each class gets generated may differ. In order to create an object of the class &lt;strong&gt;CreditCard,&lt;/strong&gt; you have to provide the card number, name of the holder, and expiration date; however, in order to create an object of the class &lt;strong&gt;PayPal,&lt;/strong&gt; all you have to provide is the credentials used to log in to one's PayPal account. Regardless, all we have to ensure is that it has to return an object of the &lt;strong&gt;PaymentMethod&lt;/strong&gt; variant.&lt;/li&gt;
&lt;li&gt;Lastly, the instatiation of an object of &lt;strong&gt;PaymentProcessor&lt;/strong&gt; class - we'll call this &lt;strong&gt;client code&lt;/strong&gt;  - requires any subclass that extends the &lt;strong&gt;PaymentMethodFactory&lt;/strong&gt; Class. Inside the constructor function of &lt;strong&gt;PaymentProcessor&lt;/strong&gt; class, you can see that it assigns the class you provide it with to the private variable called #paymentMethodFactory - In JavaScript, prepending a variable with "#" results in it becoming a private instance variable. Finally, when you invoke its function called "processPayment," it generates an object of a subclass extending the &lt;strong&gt;PaymentMethod&lt;/strong&gt; class, by utilizing the Factory subclass you provided at the time of its instantiation, and proceeds to make a payment based on the argument you provide to the function.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  When to Use This?
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;When you do not have a clear idea of the types of object that could be created&lt;/strong&gt; - Considering the situation above, you can tell that the system may or may not be extended at some point, by providing more payment methods on demand; however, you have no idea exactly which payment methods will get implemented later down the line. I don't know about you, but for me, it feels a lot cleaner to have the function accept an object of any subclass extending the &lt;strong&gt;PaymentMethodFactory&lt;/strong&gt; class.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When you want to create an object inside the factory class&lt;/strong&gt; - Honestly, I had a hard time coming to terms with this statement, when I first learned about it - like why do you have to use a Factory method to generate an object when you can directly generate it without the Factory class outside the client code; however, when the creation of an object is often complex and requires some crazy preparation logic, then it is probably better for the system to delegate the responsibilities of creating an object to another class. Providing an Factory interface, whose sole responsiblity is generating an object, to the client code promotes decoupling and flexibility. Decoupling makes the system a lot modular.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How about not altering the client code?&lt;/strong&gt; - Adding new classes by extending the abstract class, without altering the content of the client code whenever you introduce new payment method, the system becomes more flexible and maintainable!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://dev.to/7jw92nvd1klaq1/design-pattern-part-1-3ohc#creational-patterns"&gt;Back To List&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Abstract Factory
&lt;/h3&gt;

&lt;p&gt;Another pattern that involves the word &lt;strong&gt;Factory&lt;/strong&gt; in its name, you can easily assume that it might work in a similar manner as its Factory Method counterpart; however, unlike Factory Method pattern, it can generate more than one type of object! &lt;strong&gt;Abstract Factory is a creational design pattern that let you generate families of related objects without specifying their concrete classes.&lt;/strong&gt; Let's explore what it is all about, by going through the example below.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Scenario&lt;/strong&gt;: You are a lazy developer that tries to develop the comprehensive system toolkit for the maintenance of various OS. The tookit has the following functionalities: Task Manager, Disk Manager, and Network Manager. Since every OS works differently, even though they work fundamentally similar, the implementation of the toolkit for each OS may differ from one another, and you initially think of creating a separate SW for each OS; however, you are too lazy for that approach and start thinking that it would be nice if you can just have one program completely compatible with the operating systems of many different types. Instead of having the toolkit working for just one specific OS, it would be neat if the program detects the type of OS, in which the program runs, and functions accordingly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Solution&lt;/strong&gt;: Create an abstract class for each tool in the toolkit, and an abstract Factory class for generating those tools in the toolkit based on the type of OS, in which the toolkit runs. Since the toolkit you are trying to develop, regardless of the type of OS, should have the same functionalities as one another, you can simply create an abstract class for each functionality that is going to be integrated into the SW. For each OS, you can simply implement its own version of the functionality inheriting the abstract class. The abstract Factory class provides the interfaces for generating the tools included in the toolkit. For each OS, extend the abstract Factory class! Check out this &lt;strong&gt;&lt;a href="https://github.com/7jw92nVd1kLaq1/designPatternsExamples/blob/main/CreationalPatterns/abstract-factory.js"&gt;GitHub link&lt;/a&gt;&lt;/strong&gt; or the code below for clarification!
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Abstract class for creating a factory for various toolkits for various OS
class ToolkitFactory {
    constructor() {
        if (this.constructor === ToolkitFactory) {
            throw new Error("Cannot instantiate abstract class!");
        }
    }
    createTaskManager() {
        throw new Error("This method must be overwritten!");
    }
    createDiskManager() {
        throw new Error("This method must be overwritten!");
    }
    createNetworkManager() {
        throw new Error("This method must be overwritten!");
    }
}

// Concrete class for creating a factory for a Windows toolkit
class WindowsToolkitFactory extends ToolkitFactory {
    createTaskManager() {
        return new WindowsTaskManager();
    }
    createDiskManager() {
        return new WindowsDiskManager();
    }
    createNetworkManager() {
        return new WindowsNetworkManager();
    }
}

// Concrete class for creating a factory for a Linux toolkit
class LinuxToolkitFactory extends ToolkitFactory {
    createTaskManager() {
        return new LinuxTaskManager();
    }
    createDiskManager() {
        return new LinuxDiskManager();
    }
    createNetworkManager() {
        return new LinuxNetworkManager();
    }
}

// Abstract class for a TaskManager
class TaskManager {
    constructor() {
        if (this.constructor === TaskManager) {
            throw new Error("Cannot instantiate abstract class!");
        }
    }
    list() {
        throw new Error("This method must be overwritten!");
    }
    start() {
        throw new Error("This method must be overwritten!");
    }
    stop() {
        throw new Error("This method must be overwritten!");
    }
}

// Concrete class for a Windows TaskManager
class WindowsTaskManager extends TaskManager {
    list() {
        console.log("Listing Windows tasks...");
    }
    start() {
        console.log("Starting Windows task...");
    }
    stop() {
        console.log("Stopping Windows task...");
    }
}

// Concrete class for a Linux
class LinuxTaskManager extends TaskManager {
    list() {
        console.log("Listing Linux tasks...");
    }
    start() {
        console.log("Starting Linux task...");
    }
    stop() {
        console.log("Stopping Linux task...");
    }
}

// Abstract class for a DiskManager
class DiskManager {
    constructor() {
        if (this.constructor === DiskManager) {
            throw new Error("Cannot instantiate abstract class!");
        }
    }
    list() {
        throw new Error("This method must be overwritten!");
    }
    format() {
        throw new Error("This method must be overwritten!");
    }
}

// Concrete class for a Windows DiskManager
class WindowsDiskManager extends DiskManager {
    list() {
        console.log("Listing Windows disks...");
    }
    format() {
        console.log("Formatting Windows disk...");
    }
}

// Concrete class for a Linux
class LinuxDiskManager extends DiskManager {
    list() {
        console.log("Listing Linux disks...");
    }
    format() {
        console.log("Formatting Linux disk...");
    }
}

// Abstract class for a NetworkManager
class NetworkManager {
    constructor() {
        if (this.constructor === NetworkManager) {
            throw new Error("Cannot instantiate abstract class!");
        }
    }
    list() {
        throw new Error("This method must be overwritten!");
    }
    connect() {
        throw new Error("This method must be overwritten!");
    }
    disconnect() {
        throw new Error("This method must be overwritten!");
    }
}

// Concrete class for a Windows NetworkManager
class WindowsNetworkManager extends NetworkManager {
    list() {
        console.log("Listing Windows networks...");
    }
    connect() {
        console.log("Connecting to Windows network...");
    }
    disconnect() {
        console.log("Disconnecting from Windows network...");
    }
}

// Concrete class for a Linux
class LinuxNetworkManager extends NetworkManager {
    list() {
        console.log("Listing Linux networks...");
    }
    connect() {
        console.log("Connecting to Linux network...");
    }
    disconnect() {
        console.log("Disconnecting from Linux network...");
    }
}

// Client code for returning the correct toolkit factory
function getToolkitFactory(os) {
    if (os === 'Windows') {
        return new WindowsToolkitFactory();
    } else if (os === 'Linux') {
        return new LinuxToolkitFactory();
    } else {
        throw new Error("Unsupported OS");
    }
}

// Client code for using the toolkit factory
function useToolkit(toolkitFactory) {
    const taskManager = toolkitFactory.createTaskManager();
    const diskManager = toolkitFactory.createDiskManager();
    const networkManager = toolkitFactory.createNetworkManager();

    while (1) {
        console.log('1. List tasks');
        console.log('2. Start task');
        console.log('3. Stop task');
        console.log('4. List disks');
        console.log('5. Format disk');
        console.log('6. List networks');
        console.log('7. Connect to network');
        console.log('8. Disconnect from network');
        console.log('9. Exit');
        const choice = prompt('Enter your choice: ');
        switch (choice) {
            case '1':
                taskManager.list();
                break;
            case '2':
                taskManager.start();
                break;
            case '3':
                taskManager.stop();
                break;
            case '4':
                diskManager.list();
                break;
            case '5':
                diskManager.format();
                break;
            case '6':
                networkManager.list();
                break;
            case '7':
                networkManager.connect();
                break;
            case '8':
                networkManager.disconnect();
                break;
            case '9':
                return;
            default:
                console.log('Invalid choice');
        }
    }
}

// Usage
const windowsToolkitFactory = getToolkitFactory('Windows');
useToolkit(windowsToolkitFactory);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, we see four abstract classes: &lt;strong&gt;ToolkitFactory, NetworkManager, DiskManager, and TaskManager&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ToolkitFactory&lt;/strong&gt; - Your goal is to create the toolkit for only two types of OS: Windows and Linux. Since the &lt;strong&gt;ToolkitFactory&lt;/strong&gt; class provides the interface for generating the available tools whose name end with &lt;strong&gt;Manager&lt;/strong&gt;, you can extend this class by creating a Factory subclass for each OS.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NetworkManager, DiskManager, and TaskManager&lt;/strong&gt; - The toolkit provides the functionalities for managing one's computer, but since it consists of many tools, whose implementation may vary depending on the OS, you simply extend them as well by creating a subclass for each class, and let the concrete Factory class instantiate an object using the subclass, just like how we did in the example of Factory Method pattern.&lt;/li&gt;
&lt;li&gt;The client code consists of two functions: one for generating the toolkit, and another for interacting with the toolkit. The function "getToolkitFactory" creates an object of a concrete Factory class based on the type of OS the program runs in. The function "useToolkit" accepts the Factory class as its argument, generates an object of each tool, and provides a prompt for a user to use the tools.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  When to Use This?
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;When you do not have a clear idea of which families of objects to create&lt;/strong&gt; - The only difference between this pattern and &lt;strong&gt;Factory Method&lt;/strong&gt; pattern is that you can generate objects of more than one type. If you group the related items into several groups based on the families they belong to, then I guess you can use this pattern.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Supporting multiple variants of products&lt;/strong&gt;: If you develop an application like above where you have to support multiple variants of products without knowing the specifics of each variant, then this pattern may be for you.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://dev.to/7jw92nvd1klaq1/design-pattern-part-1-3ohc#creational-patterns"&gt;Back To List&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Builder
&lt;/h3&gt;

&lt;p&gt;The Builder pattern is a creational design pattern that provides a flexible approach for constructing a complex object. In this pattern, the builder provides the interface for making a modification to an object it intends to generate, and the client code updates an object by invoking the functions of a builder class successively. The definition might be hard to understand unless you have a look at the example below:&lt;/p&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Scenario&lt;/strong&gt;: You are a developer tasked with creating a simple script for making an HTTP request to a server. There is a number of aspects that you have to consider when it comes to an HTTP request: you may include in an HTTP request various properties, such as URL, method, cookies, headers, etc. Here is a thing though: you do not always have to provide every one of those properties I mentioned above for each request. For example, if you are making a simple request for fetching information from a website that doesn't require a log-in, then you can simply provide "URL" and "method" parameter with arguments. Or let's say that you want to log in to the website, then the most common approach is to provide "POST" for the "method" parameter, login information for the "data" parameter, and an URL for the "URL" parameter. Every instance is different, and you should allow a user to configure it however you want based on one's circumstances. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Solution&lt;/strong&gt;: How about providing a class with various functions for configuring an object that you intend to generate. Invoking a function of the class alters a certain aspect of an object, and you can invoke or skip them based on your needs at any given point, providing you with flexibility. When you are finally done with the configuration of an object, you can simply return the object, by invoking a function called "build". Check out this &lt;strong&gt;&lt;a href="https://github.com/7jw92nVd1kLaq1/designPatternsExamples/blob/main/CreationalPatterns/builder.js"&gt;GitHub link&lt;/a&gt;&lt;/strong&gt; or the code below for clarification!
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class HTTPRequest {
    constructor() {
        this.url = null;
        this.method = 'GET'; // Default method
        this.headers = {};
        this.cookies = {};
        this.data = null;
    }

    toString() {
        return `HTTPRequest(${this.method} ${this.url}, Headers: ${JSON.stringify(this.headers)}, Cookies: ${JSON.stringify(this.cookies)}, Data: ${this.data})`;
    }

    send() {
        console.log(`Sending request to ${this.url} using method ${this.method}`);
    }
}

// Abstract class for a builder
class HTTPRequestBuilder {
    constructor() {
        if (this.constructor === HTTPRequestBuilder) {
            throw new Error("Cannot instantiate abstract class!");
        }
    }

    setURL(url) {
        this.request.url = url;
        return this;
    }

    setMethod(method) {
        this.request.method = method;
        return this;
    }

    setHeaders(headers) {
        this.request.headers = headers;
        return this;
    }

    setCookies(cookies) {
        this.request.cookies = cookies;
        return this;
    }

    setData(data) {
        this.request.data = data;
        return this;
    }

    build() {
        return this.request;
    }
}

// Concrete class for a GET request builder
class GetRequestBuilder extends HTTPRequestBuilder {
    constructor() {
        super();
        this.request = new HTTPRequest();
        this.request.method = 'GET';
    }
}

// Concrete class for a POST request builder
class PostRequestBuilder extends HTTPRequestBuilder {
    constructor() {
        super();
        this.request = new HTTPRequest();
        this.request.method = 'POST';
    }
}

// Director class to build requests (optional)
class RequestDirector {
    constructor(builder) {
        this.builder = builder;
    }

    construct(url, data = {}, headers = {}, cookies = {}) {
        return this.builder
            .setURL(url)
            .setHeaders(headers)
            .setCookies(cookies)
            .setData(data)
            .build();
    }
}

// Usage
const director = new RequestDirector(new PostRequestBuilder());
// Prompts the user to enter data
const request = director.construct('http://example.com', { name: 'John Doe', age: 42 });
console.log(request.toString()); // HTTPRequest(POST http://example.com, Headers: {}, Cookies: {}, Data: {"name":"John Doe","age":42})
request.send(); // Sends the request to the server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We have one abstract class called &lt;strong&gt;HTTPRequestBuilder&lt;/strong&gt; which has various functions for altering a request object depending on the situational context. Its only responsibility is to provide the abstract interface for configuring an object flexibly. Each function it provides, except for the function "build", returns an object itself, which allows an object to chain itself, every time it modifies the request. Lastly, you invoke the function "build" to finally get the object of &lt;strong&gt;HTTPRequest&lt;/strong&gt; that you modify. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using the &lt;strong&gt;HTTPRequestBuilder&lt;/strong&gt; class, two concrete builder classes - &lt;strong&gt;GetRequestBuilder and PostRequestBuilder&lt;/strong&gt; - are created. Usually in HTTP, you often differentiate the types of the requests based on their method - GET is often used to fetch the information, whereas POST is used for submitting information of a user, etc. At the time of instantiation, each concrete Builder class instantiates an object of &lt;strong&gt;HTTPRequest&lt;/strong&gt; whose properties are going to be modified by the builder itself, and immediately assigns the method to it accordingly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;RequestDirector&lt;/strong&gt; class is completely optional and performs the task of modifying the request to one's liking, by receiving an object of a concrete builder class and invoking its various functions.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  When to Use This?
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;When you have to provide a lot of different arguments to a class for the instantiation of an object&lt;/strong&gt; - A class having a large amount of parameters for generating an object is usually a bad programming approach, so one way to mitigate this is to provide a builder class that modifies gradually by invoking each function specific for altering only an aspect of an object.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When there are multiple ways of instantiating an object&lt;/strong&gt; - I think, in Java and multiple programming languages - you can simply provide as many constructor functions as possible to provide as many different ways to create an object. But it may often look bad if you don't do it right, and I feel like an approach of builder class that promotes the gradual change of an object is suited for the readability of the code and the customization of an object.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://dev.to/7jw92nvd1klaq1/design-pattern-part-1-3ohc#creational-patterns"&gt;Back To List&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Prototype
&lt;/h3&gt;

&lt;p&gt;The Prototype pattern is a creational design pattern that lets you create objects without having to instantiate them using classes. It is often used when it takes a while to instantiate an object via a class, saving computing and temporal resources. Let's check out the example for understanding it better!&lt;/p&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Scenario&lt;/strong&gt;: You are developing a graphical video game and are tasked with creating an NPC that is going to be spawned in huge numbers. However, due to the way generating a character works, you find out that it takes quite a while to generate a single character using a standard approach of the instantiation of an NPC via a class, which makes it really inefficient and a waste of time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Solution&lt;/strong&gt;: Eventually, you figure out the cause of the issue: the texture processing of an NPC. The process of loading the texture files of an NPC and applying them to a character proves to be really inefficient. What if you could just generate a single NPC and copy its processed texture to other NPCs by cloning it? Check out the excerpt of the code below or this &lt;strong&gt;&lt;a href="https://github.com/7jw92nVd1kLaq1/designPatternsExamples/blob/main/CreationalPatterns/prototype.js"&gt;GitHub link&lt;/a&gt;&lt;/strong&gt; for source code.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Abstract class for a game NPC
class NPC {
    constructor() {
        if (this.constructor === NPC) {
            throw new Error("Cannot instantiate abstract class!");
        }
    }
    clone() {
        throw new Error("This method must be overwritten!");
    }
}

// Concrete class for a game NPC with a lot of properties, including textures
class Orc extends NPC {
    #name;
    #health;
    #attack;
    #defense;
    #textures;
    constructor(name, health, attack, defense, textures) {
        super();
        this.#name = name;
        this.#health = health;
        this.#attack = attack;
        this.#defense = defense;
        if (Array.isArray(textures))
            this.loadTextures(textures);
        else
            this.#textures = textures;
    }
    processTextures(textures) {
        console.log("Processing textures...");
        return textures;
    }
    loadTextures(textures) {
        console.log("Loading textures...");
        this.#textures = this.processTextures(textures);
    }
    clone() {
        const clone = structuredClone(this);
        clone.#health = 100;
        return clone;
    }
}

// Usage
// Create an Orc NPC
const orc = new Orc("Orc", 100, 20, 10, ["orc.png", "orc.3d"]);
const orc2 = orc.clone();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The abstract class called &lt;strong&gt;NPC&lt;/strong&gt; is an abstract class providing the interface for implementing a class that represents a type of &lt;strong&gt;NPC&lt;/strong&gt;. It has one function called "clone," and delegates the responsiblity of cloning to objects. &lt;/li&gt;
&lt;li&gt;The instantiation of an object of a concrete class called &lt;strong&gt;Orc&lt;/strong&gt;, which is going to be one of the NPCs of the game, launches the initialization process that runs the complex code for loading and processing the texture files of the character for rendering it on the screen. Instead of generating a new object using the &lt;strong&gt;Orc&lt;/strong&gt; class, you invoke the function "clone" of an already existing object and make a hard copy of it. That way, you simply bypass the process of loading and processing the texture files needed to render an NPC on the screen.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  When to Use This?
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;When instantiating an object is resource-intensive&lt;/strong&gt;: If instantiating an object is simply too inefficient, compared to that of cloning, then you know that this might be a better choice than creating it from scratch.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Avoiding subclassing&lt;/strong&gt;: Apparently, subclassing excessively can make your code complex and hard to maintain  in many situations. If you are in a situation where you have way too many subclasses, then it is probably better to clone an object than to follow a rigid class hierarchy. For example, In a graphics editing application, different shapes might share the same set of functions but differ in their properties, such as color, size, or position. Using the Prototype Pattern, you can clone a shape and adjust its properties, avoiding the need for a subclass for each shape variant.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://dev.to/7jw92nvd1klaq1/design-pattern-part-1-3ohc#creational-patterns"&gt;Back To List&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Singleton
&lt;/h3&gt;

&lt;p&gt;The Singleton pattern is a creational design pattern that emphasizes using one object for a class, providing a single access point for controlling an object. If you need only one object of a class for an application, then this may be what you need. Let's check out the example below to see how it may be used.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Scenario&lt;/strong&gt;: You develop an online document-editing application with a functionality that allows multiple people to work on a single document simulataneously in real-time. Your job is to ensure that only one person modifies a document at a time, which essentially prevents others from doing so.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Solution&lt;/strong&gt;: Instead of making multiple copies of a document for each user in a session, you generate a single object representing a document, and let others take turns editing it. To allow a single user to edit a document while others wait, you may implement the lock feature that gives only one person access to the editing feature of the document; However, to prevent a person from locking a document indefinitely, you implement a timeout feature, which revokes one's access to a lock after a certain time. Check out this &lt;strong&gt;&lt;a href="https://github.com/7jw92nVd1kLaq1/designPatternsExamples/blob/main/CreationalPatterns/singleton.js"&gt;GitHub link&lt;/a&gt;&lt;/strong&gt; or the code below for clarification!
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Document {
    static #lock = null;
    static #content = {
        title: "Document",
        body: "This is a document",
        footer: "End of document"
    };

    constructor() {
        throw new Error("Cannot instantiate this class!");
    }

    static acquireLock(name) {
        if (Document.#isLockExpired()) {
            Document.releaseLock();
        }
        if (!Document.isLocked()) {
            Document.#lock = {
                name: name,
                timestamp: Date.now()
            };
        } else {
            throw new Error("Document is already locked!");
        }
    }

    static releaseLock() {
        Document.#lock = null;
    }

    static isLocked() {
        return Boolean(Document.#lock);
    }

    static #isLockExpired() {
        return Document.#lock &amp;amp;&amp;amp; (Date.now() - Document.#lock.timestamp &amp;gt; 5000);
    }

    static modifyContent(key, content) {
        if (!Document.isLocked() || Document.#lock.name !== key)
            throw new Error("Document is locked or you do not hold the lock!");
        Document.#content = content;
        Document.releaseLock();
    }

    static getContent() {
        return Document.#content;
    }
}

// Usage
const key = 'John Doe';
Document.acquireLock(key);
console.log(Document.isLocked()); // true
console.log(Document.getContent());
Document.modifyContent(key, {
    title: "New Document",
    body: "This is a new document",
    footer: "End of new document"
});
console.log(Document.getContent());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In the example, there is a class called &lt;strong&gt;Document&lt;/strong&gt;. The class cannot be instatiated, since it intends to provides a single access point for everyone that access the application. &lt;/li&gt;
&lt;li&gt;It has two static private variables: lock and content. The lock holds the key of a user and timestamp for recording when the lock was acquired by the user. Modifying the content of the &lt;strong&gt;Document&lt;/strong&gt; class requires one to acquire a lock by invoking the static function called "acquireLock." The function essentially checks if the lock is already acquired by someone, and if it does, then determines if the lock is already expired or not. A user modifies the content of the &lt;strong&gt;Document&lt;/strong&gt; class, by invoking the function "modifyContent", which accepts two arguments: key, content. A user provides its key that one uses to generate a lock, and the system verifies to make sure that the user is the one that has acquired the lock by comparing the user key to that of "lock" static variable. The &lt;strong&gt;Document&lt;/strong&gt; class replaces its "content" static variable with what a user provides, and lastly, releases a lock, which allows others to acquire it for modifying the Document.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  When to Use This?
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;When you have to manage a shared resource&lt;/strong&gt;: I feel like this is often used in a situation where various objects need to share a single object. Sharing a configuration object, a connection to DB, and such come to mind.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Controlled access and modification&lt;/strong&gt; - When you have to make sure that only one person may modify the content and such at a time, like in the example above, then you should consider appplying this pattern to the application, to prevent concurrent modifications. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://dev.to/7jw92nvd1klaq1/design-pattern-part-1-3ohc#creational-patterns"&gt;Back To List&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;For this blog post, we went over the Design Patterns: why they matter, the types of patterns, and most importantly, the patterns of the Creational variant. Emphasizing the economical and inheritable approaches, the creational design patterns provide the effective ways of creating objects. &lt;strong&gt;Factory Method&lt;/strong&gt; pattern provides an interface for creating an object of a certain type. &lt;strong&gt;Abstract Factory&lt;/strong&gt; pattern provides an interface for creating the objects of families. &lt;strong&gt;Builder&lt;/strong&gt; pattern provides an interface for creating a complex object flexibly. &lt;strong&gt;Prototype&lt;/strong&gt; pattern provides you a efficient way of creating an object with the concept of cloning. &lt;strong&gt;Singleton&lt;/strong&gt; pattern utilizes a single access point to resource, providing a way to share resource concurrently while preventing race conditions. They are all well effective in decoupling the system, making it more modular and manageable! For the next blog, I will go over the rest of the Structural Patterns. Thank you for reading this long post!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>webdev</category>
      <category>designpatterns</category>
    </item>
  </channel>
</rss>
