<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Gautam Singh on Medium]]></title>
        <description><![CDATA[Stories by Gautam Singh on Medium]]></description>
        <link>https://medium.com/@brist0l?source=rss-43b60c46d388------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*EZFyRZkXTexeQJVd</url>
            <title>Stories by Gautam Singh on Medium</title>
            <link>https://medium.com/@brist0l?source=rss-43b60c46d388------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 01:06:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@brist0l/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Pythonoic exploits]]></title>
            <link>https://brist0l.medium.com/pythonoic-exploits-7ca1fab7c05c?source=rss-43b60c46d388------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ca1fab7c05c</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[python-programming]]></category>
            <dc:creator><![CDATA[Gautam Singh]]></dc:creator>
            <pubDate>Sat, 11 Jul 2026 15:43:12 GMT</pubDate>
            <atom:updated>2026-07-11T15:43:12.263Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JdC44zvut-bSfbPhzszAsg.png" /><figcaption>Bunch of cafe babes</figcaption></figure><p>Python is a language simple enough to understand (simple , not easy) . So why not make exploits in it ?</p><p>I obviously will be shamelessly demonstrating this on a CTF that I made, the <a href="https://github.com/Brist0l/baby-CTF">baby_CTF</a> — 2 .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d81IJSoLiIK5IlHads29vA.png" /><figcaption>The baby-2 CTF</figcaption></figure><p>CTFs generally need more than one iterations to be solved , so instead of giving the same payload manually we can automate the process with python.</p><p>Now Upon reading the description it’s clear that we need to access the 0x0 “unreadable” memory and the ISA has also been provided.</p><p>Let’s input anything just to see how the machine reacts and then design the exploit on the basis of it’s behavior.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YswsjHkwn4t9NLJhQ9sX4Q.png" /><figcaption>Machine’s state after “12345” as the input</figcaption></figure><p>So now let’s look at the debug logs . It starts with “[*] Writing 49 bytes.” . Why 49 ? Why such a contrived number ? Why not 59 bytes or 69 ? We only typed &quot;12345&quot;, so where did 49 come from ?Upon referring the ASCII table , it’s clear that “1” has the decimal value 49. So from this we can infer that the first byte which we input , it controls the number of bytes we can write.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/573/1*hZpXrYyBZ3QxOnzoGbc13w.png" /><figcaption>Ascii of “1”</figcaption></figure><p>Now if we compare the the states of the machine before giving the input and after giving the input we can see that the values at the address 0x10 to 0x41 have been set to 0 and are yellow in color. 0x41 — 0x10 is 0x31 which is 49. Now the bytes at 0x10 are 0x3132 , 0x3334 , 0x3500 . This is a very “predictable” pattern. The input which we had entered was “12345” , and guess what each of these character’s ASCII value is,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/493/1*WaugSlCZ5olfEi_zt6nxhQ.png" /><figcaption>ASCII’s 🙏</figcaption></figure><p>So now the “big-endianess” also makes sense, “12345” has just been stored as is in the memory.</p><p>Now looking on the next debug log,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/879/1*Q4IxcoBhJykoyXZ1m_v-pw.png" /></figure><p>Hmmmmm , so it’s expecting a sum of 0x69 but we are inputting such values which make it 0x99 .With all the input we have given and the current sum it’s clear that there are 3 values which give such a sum.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*U6AOgVdbQIMEzK2029YYRg.png" /><figcaption>sum of the 2nd,3rd and 4th byte</figcaption></figure><p>Now we need to make 0x69. What’s the easiest triplet which will yield this result ? duh , it’s 0x69 + 0x0 + 0x0 . But now how do we even freaking input 0x0 . We need the bytes</p><pre>69 00 00</pre><p>0x69 is quite easy, we can just input i.</p><p>The problem is the two 0x00 bytes.</p><p>Your terminal won’t let you type a NUL character.</p><p>So… are we stuck?</p><p>Not really. This is the point where I would recommend you to contemplate on your life decisions and why did you read this article…….yea time up.</p><p>Hint : Read the title.</p><p>Ok so if you followed the hint you may have concluded that we will use python, congrats ! You are competent enough to follow my hints.</p><p>So yea , we will be using python to input such characters. But now the question still remains on how to we input 0x0 using python.</p><p>We use sys and in that we just use a specific function sys.stdout.buffer.write() . The main difference between this and print() is that print works on text data(str) and appends a new line automatically where as the other function works on byte data and writes only whats given.</p><p>So writing a code like</p><pre>import sys<br><br>bt = b&quot;\x69\x00\00&quot;<br><br>sys.stdout.buffer.write(bt)</pre><p>will give us output like ,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/822/1*_3PNedgVkHRa_BuofZFitA.png" /></figure><p>Ok so the first part of the CTF is solved , lets set the first byte to a big enough value and the 2nd , 3rd and 4th bytes to this “sum value”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sb9PTXVoJHeJg8lQrXWX0Q.png" /></figure><p>Let’s try piping it’s output to the binary , using python3 ok.py | ./baby_2 .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WQrcG1GXXnGE8LezN7j35Q.png" /><figcaption>solved the sum part</figcaption></figure><p>Ok so now this is a progression. We can see that the registers have been unlocked but still we haven’t solved it.</p><p>Now let’s read the next hint, “ You can’t ‘directly’ access 0x00, you must jump beyond 0x15 or below 0x10&quot; . So that’s an interesting log , I guess ? It doesn’t say much and nor is it clear . When am I even trying to access 0x00?Ig let’s try changing the input again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nf949s84sHM6p-Lzhf38IA.png" /><figcaption>test payload</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3FGWiOMYRPsORdlj2CaQcA.png" /><figcaption>output of the test payload</figcaption></figure><p>Ok so now it says that you can’t access 0x50 , that implies that the byte after the sum_byte is the address at which we can jump at. But now the hint also specifies that it needs to be between 0x10 and 0x15 . So now that means that we can jump anywhere between those addresses.</p><p>We have already filled 0x10, 0x11, 0x12, 0x13 and we will fill in 0x14 with the jump address . We only have one place to jump , that’s 0x15 . So lets set it to 0x15 .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QF5n5-k03JKECBI76lz-dg.png" /><figcaption>Set the jump byte in the payload</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mKKe5tOHz-VMB_tkWNMPgg.png" /><figcaption>something different</figcaption></figure><p>Ok well now wtf is this 😃<br>Oh….sorry….I went off-script. So now It says that it is randomizing the register values. Is it seriously doing that ? Let’s run it again :)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8nkvdPftI-FO1YKgmEe0BA.png" /><figcaption>rerun</figcaption></figure><p>Well yea it surely does randomize it. An interesting thing to note is that we had set the “jump_byte” to 0x15 but the IP is pointing to some random location.</p><p>So now to point the IP clearly through this thick fog of randomization we will use something known as “NOP slide”. Basically we will inject in a bunch of NOPs and let the CPU execute them , so if we inject in say 70 of these NOP instructions starting from 0x16 and say that because of the randomization the IP points to 0x30 , it will find a NOP there and it will <em>slide </em>down the NOP instructions till they end. So with this we can tackle the randomization.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g--IgSHGWcAOKQ9sTr5v3w.png" /><figcaption>screenshot from the boot description of the CTF</figcaption></figure><p>0x90 is the instruction for NOP. We will be adding about 70 of these so we would also need to change the amount of bytes we can write , so hence I will also change “first_byte” to 0x77 (so we can write into the whole RAM)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3iwzgHKLuuGh1615Q8VgmQ.png" /><figcaption>payload with NOP sled</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*89aPNUXdtvcFCrUPe7i-XQ.png" /><figcaption>new hint (ig)</figcaption></figure><p>And it’s working like expected , we reach the last NOP and after it there is a 0x00 instruction which isn’t defined. Let’s inject some code after the NOP sled which would allow us to jump to 0x00 . This code is referred to as shellcode in modern lingo.</p><p>Now we just want to jump to 0x00 and the JMP is 0xff , so if we write 0xff 0x00 in the memory it will be interpreted as JMP 0x00 .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lbu6LpGb7hyBqs4CM5uMOg.png" /><figcaption>payload with shellcode</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/753/1*n0Qba5BJIR2eWQVPIfLJTw.png" /><figcaption>output of new payload</figcaption></figure><p>Well Now It says that there is a difference of 0x11 and there in register 0 and 0x08 in register 1.</p><p>So let’s calculate the actual values,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/1*nQRfdrBx5LhA5LkRwzHflw.png" /><figcaption>The original values</figcaption></figure><p>So we need to set register 0 to 0x3 and register 1 to 0x31 . So it’s asm will be 0x01 0x01 0x31 to set 0x31 to the first register . So now the payload is,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K40nl13XChmuNysLX_rzlw.png" /><figcaption>i hope this is the final payload setting</figcaption></figure><p>Let’s take a deep breath first and again contemplate your decision on reasoning on why you decided to read this article . Well you don’t have any so yea….</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eNeYyxODQmJT0xnAgXh18w.png" /></figure><p>Yay it worked ! So well ig now solve more CTFs or maybe read more of my articles or well maybe hit me a DM on discord at brist0l or maybe eat a Doughnut . I personally would add brist0l on discord first and then read his articles and then solve a CTF while eating a doughnut.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ca1fab7c05c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Full UG-level COA for exams (100% clickbait), Part-2]]></title>
            <link>https://brist0l.medium.com/full-ug-level-coa-for-exams-100-clickbait-part-2-fef7fd7a8a45?source=rss-43b60c46d388------2</link>
            <guid isPermaLink="false">https://medium.com/p/fef7fd7a8a45</guid>
            <category><![CDATA[emulator]]></category>
            <category><![CDATA[chip-8]]></category>
            <category><![CDATA[computer-organization]]></category>
            <category><![CDATA[computer-science]]></category>
            <dc:creator><![CDATA[Gautam Singh]]></dc:creator>
            <pubDate>Thu, 15 Jan 2026 06:06:10 GMT</pubDate>
            <atom:updated>2026-01-15T06:06:10.161Z</atom:updated>
            <content:encoded><![CDATA[<p>So in the first part we saw the basic architectural terms, now let’s dive deeper into the logic.</p><p>To make the computer do stuff there are opcodes. So now our job is to read these instructions from the RAM , check what’s it doing, do what’s it saying. In formal COA this process is known as the fetch-decode-execute cycle.</p><p>The fetch stage reads the instruction from the RAM and then copies it to the MDR (Memory Data register) . The MDR is the one of the two register which faces both the CPU as well the components outside the CPU. All the data which either needs to get inside the CPU or outside the CPU it has to go through this. Now it has a counterpart known as the MAR (memory address register), it holds the address of the data which has to fetched from the RAM , it goes to address and then that data is copied into the MDR and from the MDR it is moved into the other registers for further processing depending on the instruction.</p><p>Now we are building an emulator and we don’t need a 1-to-1 mapping of the hardware . The MAR and MDR are such registers which control data flow and in C we “technically” have easier control over the data so we can just store the instruction in variables.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0Zs9DARQue6yIrG-DjRNUQ.png" /><figcaption>Fetching the instruction in C</figcaption></figure><p>Our instruction is 2 bytes long so we need a datatype which is 2 bytes long (purely 2 bytes , so hence we used unsigned , if signed short was used then we wouldn’t have access to the most significant bit because of computer storing the data in 2’s compliment form).</p><p>In order to “reconstruct” the instruction (we need to “reconstruct” it because we are fetching 1 byte at a time and the instruction is 2 byte long). So our MSB (most significant bits) will have the first half of the instruction, from “where” we are fetching the instruction is dictated by the PC which is initialized on 0x200.</p><p>Let’s take an example to illustrate what the piece of code is actually doing , assuming that the instruction stored at the memory address 0x200 till 0x202 is “6005” (remember that it’s in hexadecimal). It’s binary equivalent is 0110-0000-0000-0101. The value stored in MSB would be 0000–0000–0110–0000. So we want the “right-half” of this instruction towards the most significant side, so we left shift it by 8. Now LSB contains 0000–0000–0000–0101, so now we just need to “join” these two and store it . So we store 0 in opcode and “OR” all three together to a get the opcode. Then we increment the PC by 2 to reach 0x202.</p><p>Now we have the opcode but we don’t know what it actually does , so here the decode part comes into play.</p><p>Knowing what’s happening inside the computer is always a good thing so to generate some logs let’s write a function which does exactly that . We want to know which function is running and how the register values have actually changed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qftGMlgwCBqoiTw3dOtPoA.png" /><figcaption>Defines the start of the function</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lKhB1toDqEvSj9lC1N4TlA.png" /><figcaption>Show register states</figcaption></figure><p>So what it looks like is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5RtHAZhe6m793P_Ttqoiyg.png" /><figcaption>logs for the fetch phase</figcaption></figure><p>Now for decoding the instruction we must understand how the instruction’s bits have been arranged. The instruction’s nibbles(4 bits) have different meaning.</p><ol><li>The first nibble tells what category of instruction is it.</li><li>The 2&#39;nd Nibble is `X` which tells which register needs to be used (where `X` is the number. i.e. Vx)</li><li>The 3&#39;rd Nibble is `Y` which tells which is the 2nd register to be used(where `Y` is the number. i.e. Vy) . Note that both `X` and `Y` can range from 0-F.</li><li>The 4&#39;th Nibble is `N` and it’s a const number</li><li>`NN` The 2nd byte (i.e. 3rd and 4th nibble) is a const number.</li><li>`NNN` The 2nd,3rd and 4th nibbles are an address (i.e. a 12 bit memory address).</li></ol><p>So the instruction which we have been taking as an example , 6005 , can be written as 6XNN .</p><p>An example of the instructions,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jdnpMc1oX_9xzxFlASYrSw.png" /><figcaption>taken from <a href="https://en.wikipedia.org/wiki/CHIP-8">https://en.wikipedia.org/wiki/CHIP-8</a></figcaption></figure><p>So now our next task is to get these values , i.e. “decode” the instructions. We will use bit manipulation to get a hold of these values. So we can make a variable with a value of 0xF000 and “AND” it with the instruction to get the first 4 bits , then right shift it by 12 to get the “actual” value as the rest of the 8 bits on the LSB side will be 0 (it will be 0 for sure as we will used unsigned data types and we needn’t worry about the sign extension stuff of the 2’s compliment).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/894/1*mcRCOcrYRSTEq1R4i0oDsA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/552/1*wtGDJxxsGyZOXiedcVqOaA.png" /></figure><p>We also need the values of N , NN, NNN , X and Y . So that too can be interpreted in the similar fashion.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/1*c0mUvXq2OfDnOWfL0J5i9g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/517/1*69xNBs8Q1BvzSp51xZh3uQ.png" /></figure><p>And so we get the “decoded” instruction . Again C has made it really easy , we didn’t need to simulate any “AND” circuits or “Left/Right Shift” circuits to get the result.</p><p>Now we need to “execute” these instructions, We have all the nibbles available so we can just use switch cases . Let’s implement the instruction 6XNN . We can simply write it like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7GEmDCSTE0gy8YqPnjZK5A.png" /></figure><p>At this point, the switch-case logic <em>is</em> the Control Unit(CU). Each case corresponds to a control decision that hardware would normally encode using gates or microcode.</p><p>The 00EE and the 2NNN instructions are antonyms in the “software sense”. The 00EE instruction is a return instruction and the 2NNN is a call instruction. These are basically the function mechanisms . Another instruction is 1NNN , which is a “jump” instruction . There is a subtle difference between a “call” and a “jump”, in “call” the address of the current PC will be pushed in the stack and when the function ends (i.e. returned) the value will be popped of the stack, whilst in a “jump” the PC will just be set to NNN .</p><p>What 2NNN is :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/832/1*TBxExzjChecCt2_zp-xwVA.png" /></figure><p>What 00EE is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*pRMe2kIBOMOnEhwUAQxCbg.png" /></figure><p>What 1NNN is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*1T8NXNRe6P4uTJU4W09Tyw.png" /></figure><p>So forth and so on the rest of the instructions can be implemented and then you can have the true fun in life , i.e. play tetris.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wcqjpmsNCxxwlMoGjUYQaw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fef7fd7a8a45" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Full UG-level COA for exams (100% clickbait),  Part-1]]></title>
            <link>https://brist0l.medium.com/full-ug-level-coa-for-exams-100-clickbait-part-1-bc66a8d892ea?source=rss-43b60c46d388------2</link>
            <guid isPermaLink="false">https://medium.com/p/bc66a8d892ea</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[chip-8]]></category>
            <category><![CDATA[computer-architecture]]></category>
            <category><![CDATA[gaming-emulators]]></category>
            <dc:creator><![CDATA[Gautam Singh]]></dc:creator>
            <pubDate>Thu, 08 Jan 2026 05:01:11 GMT</pubDate>
            <atom:updated>2026-01-08T05:01:11.869Z</atom:updated>
            <content:encoded><![CDATA[<h3>Full UG-level COA for exams (100% clickbait), Part-1</h3><p>COA as a subject is purely a love-or-hate for the people who study it and the ones who teach it (ironically) alike . My university is uniquely famous for setting such papers for their semesters which can make you question your own existence whilst giving the exam . So in order not to go full-on <em>Aristotle </em>in the exam , I decided (drum-rolls please) to………study! Yep , I know , I just used <em>Aristotlian brain.</em></p><p>Assuming you aren’t a 40 year old , it’s highly probable that you wouldn’t have seen such “computers” (I personally haven’t seen such computers , but that just might be due to the fact that I am not a 40 year old man…..yet):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/0*5s4BNBuj2OnDP2Wx.jpg" /><figcaption>An RCA COSMAC-VIP from the late 1970&#39;s</figcaption></figure><p>An advantage of studying such computers in contrast to the modern day computers is that they are relatively easier to understand ( this badboy’s RAM can be extendable upto 4kb, yep , 4-K-B).</p><p>How much easier you might be asking , well that’s a really good question. It’s as easy as understanding this Von Neumann architecture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*mTcn5-9rOvLKlOKY.png" /></figure><p>When seen in contrast with it’s “rival” , the Harvard architecture, the Von Neumann architecture just has “one” memory unit. “one” over here doesn’t count the number of RAM sticks , it refers to number of contiguous blocks of memory. So in this case it means that there is just one big pool of memory available for both storing the data (temporarily) as well as the program instructions. The Harvard architecture has “two” separate memory units , one for the program instruction and one for data.</p><p>Dividing the memory into two parts solves problems such as “Control Hazards” ( I will touch upon this later) but increases the cost of the computer. Although with both of these architecture a subtle problem problems remains, and that is a “processor-memory bottleneck” . What it means is that in both the architectures the data and instructions can’t be brought to the CPU ‘simultaneously’.</p><p>More on all that “theoretical” stuff later , let’s see the COSMAC-VIP running some CHIP-8 programs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YbcO5H9PrVIPDlj7Vv47PQ.png" /><figcaption>A tribute to the one and only IBM</figcaption></figure><p>CHIP-8 is an interpreted programming language which is “emulated” on the COSMAC-VIP hardware.</p><p>Emulated over here basically means that you are running another computer in your computer , I personally believe that it’s a really reasonable opportunity to learn COA.</p><p>There are 4 blocks in the Von Neumann architecture diagram. The input , output , CPU and the memory. So let’s clearly see what it exactly means in terms of the COSMAC-VIP</p><p>Input : Input is done with a hex keyboard that has 16 keys ranging 0 to F. (So we can just map out our computer’s keys to this emulator’s input)</p><p>Output: The original CHIP-8 display resolution is 64×32 pixels , and its color is monochrome. ( I am sure we can sacrifice some of our screen for this emulator’s output)</p><p>Now jumping to the meat of the emulator , the CPU and the memory:</p><p>CPU : It has 16 8-bit registers ranging from V0 to VF. An address register which is denoted by “I” and it’s 12-bit wide and obviously a “PC” which again is 12-bit wide.</p><p>Registers are pieces of hardware which store data (and not to mention that they are faster than the guys in the wild-wild west). There are 2 kinds of registers, a general purpose register and a special purpose registers.</p><p>The special purpose register registers are similar to the general purpose registers but with a subtle difference that the “meaning” of the data stored in the special purpose registers is always known. For eg. a general purpose register as well as a special purpose register(say a SP[stack pointer] in this case), both can store a value like 0x1000 but for the special purpose register it’s the address for the stack and for the general purpose register it can be literally “anything” , it can be the amount of times you have failed in an exam , the input supplied by the user , the amount of times you have disappointed your parents.</p><p>So lets start off by building some registers, so they hold data , that’s what variables also do in a programming language (and obviously we are going to be using C).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7fKXC2LzXzK-mr3MxT3_uA.png" /></figure><p>uint8_t is just an unsigned int which is 8 bytes wide.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AZxsuLox8TxT2XfsKR6ARw.png" /><figcaption>output of “man uint8_t”</figcaption></figure><p>_BitInt(x) is used to specify a datatype of width “x” , in this case 12 bytes.</p><p>Registers are just “registers” , there are no “signed” or “unsigned” registers. The signed/unsigned is a software level construct. All registers are “technically” unsigned because they just hold the data and don’t dictate what’s the orientation of the data(i.e. if it’s 2’s compliment or not).</p><p>In “formal” COA , there are two special purpose registers which are stressed upon . The PC and the IR , the Program counter and the Instruction counter respectively.</p><p>Program Counter: This holds the memory address of the <strong><em>next instruction </em></strong>to be executed <strong><em>next.</em></strong></p><p>Instruction Register: This holds the instruction that has been fetched from the the memory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*4fmOqzY-tZr8z-Ea9kd71Q.png" /></figure><p>“ADD R1,R2” is just an english representation of the bytes being stored in the IR register. Dissecting a real life program:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0q8D1XyMcSPKi2UsnEqhWA.png" /><figcaption>objdump of a random executable I made</figcaption></figure><p>The left most blocks are the addresses where the instructions are stored on the RAM ( they aren’t “true” addresses because of the precedence of virtual memory , again that’s a story for another day).</p><p>The extreme right contains the data stored at the specific address, and the block in middle is the hexadecimal representation of the instructions. It’s interesting to see that each address isn’t incremented by the same value ( again that’s just intel being intel [although space efficient but can get confusing whilst understanding what’s going on under the hood]) . This is happening because the size of each instruction is not the same.</p><p>So PC stores the left block and the IR stores the middle/right block.</p><p>Now another plot twist , many computers don’t have an IR. Even CHIP-8 doesn’t have one.</p><p>Memory: It has 0x1000 memory spaces and each of those memory space can hold 8 bits. But now interestingly enough the CHIP-8 interpreter itself occupies the first 0x200 spaces of the memory , so the program start from 0x200.</p><p>For storing the return address of the subroutines a 16 2-byte call stack is used.</p><p>In C it can be written as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pk4RAoDUdcrOt2q6GxNQBw.png" /></figure><p>So well technically if we compile this , we have got ourselves a COSMAC-VIP ( without the CPU logic [the ALU and CU] and the busses) . So our next step is to implement them.</p><p>The Arithmetic Logic Unit , or more conveniently known as the ALU is a bunch of electronic components arranged in a special way (which, coming from someone who can’t stand gate-level electronics, might as well be mumbo-jumbo) to perform basic operations like addition, subtraction , bit shift , logical and etc.</p><p>We needn’t simulate the mumbo-jumbo because C will already take care of that as it already has built in operators like “+”, “-” , “~” , “^” etc . So our job is that we need to map the correct operator to the correct instructions.</p><p>The Control Unit, or more conveniently known as the CU is again a more complex set of electronic components arranged in a special way to command the CPU on what and when to do. The CU is responsible for telling your CPU to “get the instruction from this address and put it in register” or “go to the ALU and add 4 to the register V2”.</p><p>Again we needn’t simulate the physical connections of the CU . We just need the “logic”. The major part in this emulator which we will be dealing is the fetch-decode(barely)-execute cycle . This cycle is governed by the CU.</p><p>From the debug logs of the CHIP-8 , it is evident that CU is doing “something really heavy” , let’s dissect this piece by piece.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f5gMzkq3vslWLB_0wwW6lQ.png" /><figcaption>debug log of the emulator</figcaption></figure><p>The fetch-decode-execute cycle is used make the computer do what you want. To see this in action we need to inject some instructions in the RAM. Recall that the user program starts at the memory location 0x200 .</p><p>Although CHIP-8 documentation states that memory below 0x200 is reserved for the interpreter, in modern emulators the interpreter exists outside the emulated memory, making this region unused except for conventionally placed data such as font sprites.</p><p>Now before even injecting the instructions we need to know two important aspects , one being that “what” instructions are valid and the other being “how” are they stored in the RAM.</p><p>In formal CS , the “how” is referred to as <strong>endianness</strong>. It is the order in which the bytes of a<em> word </em>data-type (over here it refers to any processor design’s natural unit of data, it can be 2 bytes , 4 bytes , 8 bytes etc etc.) are stored/transferred in a computer . The word size for CHIP-8 is 2 bytes.</p><p>Endianness is primarily expressed as <strong>big-endian</strong> or <strong>little-endian . </strong>A big-endian system stores the most significant byte (MSB) of a word at the smallest memory address and the least significant byte (LSB) at the largest. A little-endian system, in contrast, stores the LSB at the smallest address. Of the two, big-endian is thus closer to the way the digits of numbers are written left-to-right in English, comparing digits to bytes and assuming addresses increase from left to right.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*M_84QJ7t36Cqds70" /><figcaption>credit: <a href="https://commons.wikimedia.org/wiki/User:Aeroid">https://commons.wikimedia.org/wiki/User:Aeroid</a> , source : <a href="https://commons.wikimedia.org/wiki/File:32bit-Endianess.svg">https://commons.wikimedia.org/wiki/File:32bit-Endianess.svg</a></figcaption></figure><p>CHIP-8 is big endian and it’s instruction size is of 2 bytes. Instructions are also known as operation-codes ( or opcodes for short). Each block of memory in the CHIP-8 can hold upto 1 byte of data , so to store 1 opcode atleast two memory locations need to be used.</p><p>The opcodes for the CHIP-8 have already been defined by <a href="https://en.wikipedia.org/wiki/Joseph_Weisbecker">Joseph Weisbecker</a>, an example of a simple opcode is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/927/1*4JTgOfURxJ9NnsR8Hzo7_A.png" /><figcaption>borrowed from the wiki (<a href="https://en.wikipedia.org/wiki/CHIP-8">https://en.wikipedia.org/wiki/CHIP-8</a>)</figcaption></figure><p>The opcodes are in hexadecimal and with the following symbols:</p><ul><li>NNN: address</li><li>NN: 8-bit constant</li><li>N: 4-bit constant</li><li>X and Y: 4-bit register identifier</li><li>PC : Program Counter</li><li>I : 12bit register (For memory address) (Similar to void pointer);</li><li>VN: One of the 16 available variables. N may be 0 to F (hexadecimal);</li></ul><p>So the opcode 6XNN , it has 4 hexadecimal digits . One hexadecimal digit is 4 bits long , so this whole instruction is 16 bits long (i.e. 2 bytes). What is does is that it sets a register X to a 4 bit constant.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a7knErF3j47RZfiVvX2S5Q.png" /></figure><p>So a code like this will load the instruction 6005 into the memory. It would be read as , “setting the register V0 to 0x05” . Obviously if we “run” it , nothing will happen because we haven’t set up the logic for modifying the register values or fetching the instructions or anything of sorts.</p><p>The part-2 will elaborate about the CPU design and logic.</p><p>references:</p><ol><li><a href="https://en.wikipedia.org/wiki/CHIP-8">https://en.wikipedia.org/wiki/CHIP-8</a></li><li><a href="https://github.com/brist0l/chip-8">https://github.com/brist0l/chip-8</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bc66a8d892ea" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Function Prologue: an assembliatic approach (for a 64 bit CPU)]]></title>
            <link>https://brist0l.medium.com/function-prologue-an-assembliatic-approach-for-a-64-bit-cpu-2e737527af1e?source=rss-43b60c46d388------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e737527af1e</guid>
            <dc:creator><![CDATA[Gautam Singh]]></dc:creator>
            <pubDate>Thu, 06 Nov 2025 18:12:16 GMT</pubDate>
            <atom:updated>2025-11-06T18:12:16.610Z</atom:updated>
            <content:encoded><![CDATA[<h3>assembliatic /ə-sĕm′-blē-ăt′ĭk/, adjective :</h3><ol><li>Relating to assembly work.</li><li>A piece of code which will mainly be analyzed in assembly.</li></ol><p>Function prologue , as the name suggests has something to do with the function in the starting (duh). Function prologue is a name which is given to a set of instructions which are responsible for setting the registers as well as the stack when the function is called.</p><p>Similarly a “function epilogue” is name given to a set of instructions which destroy the stack and “exits” out of the function.</p><p>The most common “function” and the start point point of any C program is the ‘main’ function (i.e. until and unless you haven’t configured the linker to your liking). So after compiling any C program your compiler will spit out an ELF ( if you are on linux ).</p><p>An ELF is a binary file format. It describes how the program should be loaded(program/segment headers) and stores metadata describing program components(section headers).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S199WZ9s_yxQXTAaKF5sCQ.png" /><figcaption>double checking to make sure the program is an ELF or not (you can never trust the internet)</figcaption></figure><p>What ok_2 is a really simple C program , which goes as follows:</p><pre>#include&lt;stdio.h&gt;<br><br>int main(){<br> int a = 0xF;<br> printf(&quot;%d\n&quot;,a);<br>}</pre><p>This well , prints out 15 (ik , quite surprising).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*-awvKvXw3WgStQYPa6RIQQ.png" /></figure><p>To start ./ok_2 the computer doesn’t directly call the ‘main’ function. The first thing that is called is a function which is linked to every program which is named _start. Now the question arises that how do we even reach _start . Upon strace-ing the program:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y7fEX8fgotP6wo-KY3N7hA.png" /></figure><p>The first syscall is which is being called is an execve . execve as defined by the manual executes the program given to it and setups up it’s newly initialized stack and push the “argc” , “argv” and “envp” . It will also setup the heap, and (initialized and uninitialized) data segments. After the setup if there aren’t any errors then _start is called.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2FS3VUAP52Da0L8VLi628Q.png" /><figcaption>output of “man 2 execve”</figcaption></figure><p>As the program which we are executing is an ELF , we can have a look into it also:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1XkxoOc2SnRbIApHVbRZvA.png" /></figure><p>Every ELF file starts with the sequence 7f 45 4c 46 . Which translates into “7f ELF” in ASCII.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/521/1*7BVBseDyaAxzCykcALIabQ.png" /></figure><p>The ELF autopsy also reveals how data is stored in my CPU (in this case it’s in 2’s compliment form , little endian).</p><p>The “entry point address” shows the address of the entry point (duh) , but what exactly is in that address ? To find that out let’s dissect the binary down.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IY9dgVbFLE8CQVJVkWNr3Q.png" /><figcaption>The output of “objdump -d -M intel ok_2”</figcaption></figure><p>and et voila , 0x1040 contains the “_start” function.</p><p>Analyzing the first half of the function:</p><pre>0000000000001040 &lt;_start&gt;:<br>    1040: f3 0f 1e fa           endbr64<br>    1044: 31 ed                 xor    ebp,ebp<br>    1046: 49 89 d1              mov    r9,rdx<br>    1049: 5e                    pop    rsi<br>    104a: 48 89 e2              mov    rdx,rsp<br>    104d: 48 83 e4 f0           and    rsp,0xfffffffffffffff0<br>    1051: 50                    push   rax<br>    1052: 54                    push   rsp<br>    1053: 45 31 c0              xor    r8d,r8d<br>    1056: 31 c9                 xor    ecx,ecx<br>    1058: 48 8d 3d da 00 00 00  lea    rdi,[rip+0xda]        # 1139 &lt;main&gt;<br>    105f: ff 15 5b 2f 00 00     call   QWORD PTR [rip+0x2f5b]        # 3fc0 &lt;__libc_start_main@GLIBC_2.34&gt;<br>    1065: f4                    hlt</pre><p>The first instruction is endbr64 , which stands for “end branch 64” . It acts like a safety feature , if a hacker was able to change the destination address of an indirect jump, the program is very likely to terminate even if the destination is legal assembly code. The instruction is otherwise treated as a NOP . Basically it’s a safety feature and nothing “relating” to _start itself.</p><p>The next instructions <strong>XOR</strong>’s ebp with ebp , basically setting ebp to 0. In ABI (application binary interface) terms it means that it’s the starting of the stack frame.</p><p>Recall that we had “argc”, “argv” and “envp” on the stack . So the instruction pop rsi set’s rsi to “argc”. Now , after popping “argc” , the rsp will point to “argv”.</p><p>The next instruction mov rdx,rsp is basically moving “argv” into rdx without shifting the stack pointer. The next instruction <strong>AND</strong>’s a bitmask with rsp for alignment reasons , it will just change the lower 4 bits of the stack pointer.</p><p>The instructions below this are used for setting up the arguments which need to be passed in __libc_start_main . __libc_start_main is a function which looks like:</p><pre>int __libc_start_main(int (*main) (int, char * *, char * *), <br>                      int argc,<br>                      char * * ubp_av, <br>                      void (*init) (void), <br>                      void (*fini) (void), <br>                      void (*rtld_fini) (void), <br>                      void (* stack_end));</pre><p>Arguments are successively passed in rdi, rsi, rdx, r10, r8, r9, this is the convention adopted.</p><p>So obviously the first argument is a function pointer to the “actual” ‘main’ function and it’s value should be stored in rdi . rdi is modified in at 0x1058 with the instruction lea rdi,[rip+0xda] . That just means that 0x1139 is stored in rdi (which is the address of the starting of the main function).</p><p>Recall that ”argc” had been popped into rsi , so this is the 2&#39;nd argument which is being passed.</p><p>rdx was already pointing to “argv” so the 3&#39;rd argument has also been taken care of. ( The 3&#39;rd argument wants the content of “argv”). Rest of the registers are already set when execve was called as it loads the auxiliary vectors (kernel info) into the registers.</p><p>The __libc_start_main function is defined in libc.so.6. These libraries are part of GCC.</p><p>The 4th argument is void (*init) (void) , it basically points to a function __libc_csu_init , it’s the constructor of the program . It runs before the “main” function and void (*fini) (void) is the destructor of this program and points to a function __libc_csu_fini , it runs after the “main” function and cleans up the mess. __libc_csu_fini is linked into our code from glibc, and lives in the source tree in csu/elf-init.c. It’s our program’s C level destructor.</p><p>Loader started at _start which then called __libc_start_main which then called __libc_csu_init and which calls _init .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dPA0ySzXNtKVtwhbfDwDcg.png" /></figure><p>We grab the address of “__gmon_start” . If it’s zero then we don’t call it, instead we jump past it. Otherwise, we call it to set up profiling. After this function runs then it returns back to __libc_start_main and now “main” is finally executed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UlVEaLz2OEXLD91JwKc2mQ.png" /><figcaption>disassembly of main</figcaption></figure><p>The first 3 lines of asm are called the “function prologue”.</p><p>So before the first instruction even is executed the stack looks something like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/830/1*6ABwrf_DGXF9EbOpFOZgfw.png" /></figure><p>Basically it’s pointing to a stack frame in which we aren’t interested in. We need to build a new stack frame. Those 3 instructions exactly do that.</p><p>The first instruction push rbp , so now the stack looks something like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/778/1*T5l3uf2Nzo0n813mR58PEw.png" /></figure><p>The next instruction being mov rbp,rsp . It basically sets rsp to rbp , So now rsp and rbp point to the same address. Now the stack looks something like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/1*299dQ5lUUaFuzrI4lXOqpw.png" /></figure><p>And the 3rd instruction sub rsp,0x10 . This just shifts the address of rsp by 0x10 , so now a new stack frame has been created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FXTTDeB_rxF-C5XtMY-yuA.png" /></figure><p>The next instruction stores 0xf at rbp — 0x4 . So now the stack looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ygkVkhuiVh-otsJue2vAbQ.png" /></figure><p>The next few instructions setup the registers for the “printf” call.</p><p>Before returning ( i.e. the ret instruction) to __libc_start_main which reroutes it to exit function , there is an instruction in “main” which is called leave (this is the function epilogue of “main”) . What this instruction is just the opposite of the first 3 instructions of “main” .</p><p>So what leave is basically:</p><pre>mov   rsp, rbp     # rsp = rbp<br>pop   rbp</pre><p>So after mov rsp,rbp get’s executed the stack looks something like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/825/1*AIllhF0B0f2rhcVv9vxIsA.png" /></figure><p>And the next instruction pops the address of the location where rbp was pointing at in the starting of the function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/885/1*dpBlOXpKULWmopIE4szCHA.png" /></figure><p>Hence, the original stack frame has been retrieved.</p><p>This was a very simplified and high level overview of how a C program loads and how a function actually “builds”.</p><p>References:</p><ol><li><a href="https://refspecs.linuxbase.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/baselib---libc-start-main-.html">https://refspecs.linuxbase.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/baselib---libc-start-main-.html</a></li><li><a href="https://dbp-consulting.com/tutorials/debugging/linuxProgramStartup.html">https://dbp-consulting.com/tutorials/debugging/linuxProgramStartup.html</a></li><li><a href="https://shikaan.github.io/assembly/x86/guide/2024/09/26/x86-64-functions.html">https://shikaan.github.io/assembly/x86/guide/2024/09/26/x86-64-functions.html</a></li><li><a href="https://en.wikipedia.org/wiki/Application_binary_interface">https://en.wikipedia.org/wiki/Application_binary_interface</a></li><li><a href="https://stackoverflow.com/questions/1753602/what-are-the-names-of-the-new-x86-64-processors-registers">https://stackoverflow.com/questions/1753602/what-are-the-names-of-the-new-x86-64-processors-registers</a></li><li><a href="https://www.tortall.net/projects/yasm/manual/html/arch-x86-registers.html">https://www.tortall.net/projects/yasm/manual/html/arch-x86-registers.html</a></li><li><a href="https://x64.syscall.sh/">https://x64.syscall.sh/</a></li><li><a href="https://stackoverflow.com/questions/29790175/assembly-x86-leave-instruction">https://stackoverflow.com/questions/29790175/assembly-x86-leave-instruction</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e737527af1e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How the order of declaring variables affect a C binary]]></title>
            <link>https://brist0l.medium.com/how-the-order-of-declaring-variables-affect-a-c-binary-5100bd5bc2c6?source=rss-43b60c46d388------2</link>
            <guid isPermaLink="false">https://medium.com/p/5100bd5bc2c6</guid>
            <dc:creator><![CDATA[Gautam Singh]]></dc:creator>
            <pubDate>Wed, 23 Jul 2025 11:40:25 GMT</pubDate>
            <atom:updated>2025-07-24T03:09:51.870Z</atom:updated>
            <content:encoded><![CDATA[<p>To demonstrate this fact , lets consider the following block of code.</p><pre>#include&lt;stdio.h&gt;<br>#include&lt;string.h&gt;<br><br>int main(){<br> int show_password = 0;<br> char input_password[5];<br><br> scanf(&quot;%s&quot;,input_password);<br><br> if(strcmp(input_password,&quot;ok&quot;) == 0)<br>  show_password = 1;<br><br> if(show_password)<br>  printf(&quot;password&quot;);<br>}</pre><p>The code is quite straightforward , it takes input into “small” array and then if the string is equal to “ok” then it will turn on the show_password flag which then is checked by the next if condition.</p><p>The “small” array is used for a deliberate cause. Upon compiling this program with a gcc ok.c -fno-stack-protector -g -o ok, we will get a binary with the name okwhich works as expected. The -fno-stack-protector flag is used so that no guard variable is inserted on the stack by the compiler. By default this flag is turned on but in some linux distributions this isn’t the case.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/633/1*fmZQHCN1XTm8QK2sr_QPkQ.png" /></figure><p>And upon supplying a string which is greater in length than the “small” array we still get the “password”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/758/1*mZq34GsbNjGqvKgLIPDePQ.png" /></figure><p>Now what actually is happening behind this buffer overflow and how is related to the ordering of the deceleration of variables ?</p><p>To look inside the binary let’s fire up gdb with</p><p>gdb -q ./ok which opens up the ./ok binary and suppresses the introductory message which gdb gives. We had used the -g flag so that we can we can debug the binary easily , it gives up access to the variable names and other useful stuff but it also increases the size of the binary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V7STpE6SI6lxXBz50L7RLQ.png" /></figure><p>Using the list command we can see the source code and we can decide where do we have to have to set breakpoints in the code so that we can analyze it later.</p><p>We are interested in input_password and show_password so it is quite logical to break after the string has been inputted and just before the program prints the password (i.e. if it will). So to add breakpoints we can use the break i command of gdb which sets a breakpoint at i .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/926/1*pGsvO8Y8Tej6larIqXJanQ.png" /></figure><p>So now if we run the program, it should stop at line 10. I have typed in a random string greater than 5 bytes , “kfafassfas”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KUZt0PwA354CWsKAz_HaXQ.png" /></figure><p>So the input_password variable is an array of char. We can get it’s address in the memory using the &amp; operator .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/785/1*urDhdSbJ4dN5QWJiAkQy2w.png" /></figure><p>In my computer it is stored at 0x7fffffffe6d7 , that means that the first character will be stored in this address and the next one in the next byte (i.e. 0x7fffffffe6d8 ) and so on and so forth . 5 bytes are reserved like this. If it were an array of ints like int a[5] then 20 bytes would have been reserved for it because an int is worth 4 bytes (in most computers).</p><p>We can use the x command in gdb which is the shorthand for “examine” to see what does that . It can be paired with other flags to make it behave the way you want. Like x/c memwill print out whats at that memory address mem in character form.</p><p>x is for printing it in hexadecimal.</p><p>d is for printing it in decimal.</p><p>A number can be used to print out contiguous blocks of memory like</p><p>x/4c mem will print out 4 bytes starting from the address mem in character form.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fU_ZMMmeMgcrxf20vOSd7A.png" /></figure><p>A shorthand for printing strings is to use x/s mem , so we can also write x/s 0x7fffffffe6d7 to get it in strings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/693/1*pAe1D2IDijuj8jTeXt0hDA.png" /></figure><p>But on printing the variable input_password we don’t get the same result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*GM34NFkEQP92rmrgkX9TPA.png" /></figure><p>This is just storing “kfafa” where has the rest of it gone ? So lets inspect the memory address after the last byte of input_password using x</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1011/1*x0yWc3YnaDASK8GSep-7Ww.png" /></figure><p>So it has started writing in “0x7fffffffe6dc” and (un)ironically our show_password also starts from there. We can again use &amp; to see that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/678/1*Z_qom20Si81ujmj4qJpcqw.png" /></figure><p>So show_password is an int so it would be covering 4 bytes. Before those bytes contained a 0 but now they have been overwritten by our buffer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xozqPrCSlNC-Ev59srIi-g.png" /></figure><p>My computer is of little endian architecture so the values are stored <em>backwards . </em>So<em> </em>what actually is stored is 0x61667373 , this is 4 bytes long. So that is stored in the show_password , if we convert it to decimal we get “1634104179” . We can check this using our debugger also, but before that I need to introduce another feature of x .</p><p>x/u mem , u is the unit size</p><p>The unit size is any of</p><p>bBytes.</p><p>hHalfwords (two bytes).</p><p>wWords (four bytes).</p><p>gGiant words (eight bytes).</p><p>So these unit values will compute the data in a contiguous blocks of memory.</p><p>x/dw &amp;show_password will show 4 bytes of show_password in decimal form.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/697/1*OZ9vpCulXLMh4DvcIvayWw.png" /></figure><p>So using c to move to the next breakpoint . As show_password is a non 0 value it will be executed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*jDU_FvxZ92YZiE7CwW5GnQ.png" /></figure><p>And as expected:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5DgjKYu77D6h_h6EwYtE0Q.png" /></figure><p>This type of memory layout is occurring due to the fact that the stack<br>moves from a higher memory address to a lower memory address . So lets <br>assume that “input_password” is at 990 and show_password is at 1000, so<br>we first fill input_password in the which extends to 990,991,992….,<br>hence this causes the buffer overflow to happen.<br>This is quite a contrived example that depends on the layout of memory.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5100bd5bc2c6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What “pseudo force” looks from the ground frame ?]]></title>
            <link>https://brist0l.medium.com/what-pseudo-force-looks-from-the-ground-frame-c0d4acb666f9?source=rss-43b60c46d388------2</link>
            <guid isPermaLink="false">https://medium.com/p/c0d4acb666f9</guid>
            <dc:creator><![CDATA[Gautam Singh]]></dc:creator>
            <pubDate>Thu, 01 Aug 2024 14:21:37 GMT</pubDate>
            <atom:updated>2024-08-01T14:21:37.974Z</atom:updated>
            <content:encoded><![CDATA[<h3>What “pseudo force” looks from the ground frame ?</h3><p>We are all familiar with the second law of motion stated by newton Which states that, “Force is the rate of change of momentum”. It can be written mathematically as:</p><pre>F = dp/dt , where p is the momentum</pre><p>Before further going in depth of the second law we must understand what momentum is . momentum can be intuitively thought as “ the degree of difficulty which would be required to stop a moving object ”. It is mathematically defined as :</p><pre>p = mv, note that momentum is a vector quantity</pre><p>If we plug the “momentum equation” in the “force equation” , we would be getting:</p><pre>F = d(mv)/dt <br><br>on diffrentiating this equation<br><br>F = v dm/dt + m dv/dt         (By applying the chain rule)<br><br>If we consider that the mass doesn&#39;t of an object doesn&#39;t change with time,<br>we can conclude that dm/dt (rate of change of mass with time) = 0<br><br>So we can re write the equation as:<br><br>F = m dv/dt                   (Note that this equation is still vector because<br>                                momentum was vector)</pre><p>dv/dt is the rate of change of velocity with time , which is acceleration (which is denoted by an ‘a’). So again rewriting the equation we will get:</p><pre>F = ma       (It&#39;s a vector quantity)</pre><p>Now the force which is being applied on a body might be different for different frame of references .</p><p>For example; when a force F is applied in the (-)x-direction to a body kept on a table , from the ground frame it would look as if the body is moving from right to left , from the body frame of reference it would look as if the body isn’t moving at all, from the moon frame of reference it would look as if the body is performing a circular motion(due to the motion of the Earth) + Linear motion ( due to the force F ).</p><p>Let us imagine a pendulum which is suspended in an accelerating car . If we are in the car frame of reference ( which is an accelerating frame of reference) , we would say that the pendulum swung back because of pseudo force . But if we analyze this situation from ground frame of reference , we can’t intuitively see a force which is holding the pendulum back. To help us with we can use the conclusion of Newton’s first law.</p><p>Newton’s first law states that “If a body is at rest it will remain at rest until acted upon by an external force ,If a body is in motion it will remain in motion until acted upon by an external force”. From this law we can conclude that a body will be stay in the state it is in until external forces act upon it.</p><p>So at t = 0 when the car was at rest , the pendulum was also at rest . A little while later the engine of car produces a torque on tires , which in turn will generate friction between the road and that friction will push the car to go forward. Now the car will move forward ( it goes from a state of rest to a state of motion due to an external force , i.e. friction due to the road), but there is no external force which would be acting on the pendulum ( assuming there is no air resistance). As there is no external force acting on the pendulum , it would “want”(due to newton’s first law) to stay in the position it originally was. The car is accelerating forward whilst the pendulum would want to stay in the position it originally was. From the ground frame it would look as if the pendulum is going backward. It would move backward till a certain point until it will come to rest ( when all the forces will balance out , like tension and gravity) and from then on the tension due to the string will accelerate it in the direction of the motion of the car (The position of the pendulum is static from the car frame but the position from the ground frame is accelerating).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c0d4acb666f9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Playing Games on Linux still hard without steam ??]]></title>
            <link>https://brist0l.medium.com/playing-games-on-linux-still-hard-without-steam-50af13169ad7?source=rss-43b60c46d388------2</link>
            <guid isPermaLink="false">https://medium.com/p/50af13169ad7</guid>
            <category><![CDATA[games]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[emulator]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Gautam Singh]]></dc:creator>
            <pubDate>Sun, 11 Jul 2021 11:51:59 GMT</pubDate>
            <atom:updated>2025-04-29T14:01:16.847Z</atom:updated>
            <content:encoded><![CDATA[<h3>Playing Games on Linux still hard without steam ?</h3><p>It’s been a while since people have started comparing windows and Linux in terms of gaming . Where windows supports native compatibility you sure do need some good piece of hardware to run most of the games , whereas Linux which is more secure and is best suited for lower hardware does not support native compatibility with most of the games.</p><p>But there are software’s which allow games which are meant to be run on windows to run on Linux.</p><p>Like :</p><ol><li>Lutris</li><li>Wine</li><li>Proton</li></ol><p>And there are many more.</p><p>Many people shift from Windows to Linux just because they have weak hardware or are developers . After shifting to Linux you can’t play titles like GTA vice city and all which don’t require strong hardware . And Many people suggest to buy it from steam and run it using a compatibility layer like proton . And Proton does a great job at running the games on Linux .</p><p>But what about the people who own a CD or just have the game files . I got a perfect solution for that .</p><p>P.S : I am going to be using apt (packet manager) for this demonstration</p><ol><li>We first need to install wine , and we can do that by typing the following command sudo apt install wine (the download size is around is 450mb)</li><li>Then cd into the directory where the files of the game’s are. eg : cd Gta/</li><li>Games have a file which installs the whole game but the problem is that they are in an .exe(executable) format , over here wine comes in play. Type wine {installer}.exe in the {installer} the file name goes .</li><li>Thats it just go with the default setting</li></ol><p>I hope you found this useful or atleast entertaining.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=50af13169ad7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>