<?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 pwnPH0fun on Medium]]></title>
        <description><![CDATA[Stories by pwnPH0fun on Medium]]></description>
        <link>https://medium.com/@pwnPHOfun?source=rss-4fdd6e2ce87e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*3IPPNgdoJ6rsIJCvpthGdQ.jpeg</url>
            <title>Stories by pwnPH0fun on Medium</title>
            <link>https://medium.com/@pwnPHOfun?source=rss-4fdd6e2ce87e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 06:01:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pwnPHOfun/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[Heap exploitation journey #1 — Tcache attack]]></title>
            <link>https://infosecwriteups.com/heap-exploitation-journey-1-tcache-attack-5b38fb0c19b0?source=rss-4fdd6e2ce87e------2</link>
            <guid isPermaLink="false">https://medium.com/p/5b38fb0c19b0</guid>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[pwn]]></category>
            <category><![CDATA[defcon]]></category>
            <category><![CDATA[heap]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <dc:creator><![CDATA[pwnPH0fun]]></dc:creator>
            <pubDate>Fri, 01 Oct 2021 04:28:26 GMT</pubDate>
            <atom:updated>2022-09-29T20:07:35.758Z</atom:updated>
            <content:encoded><![CDATA[<h3>Heap exploitation #1 — Tcache attack</h3><p>Hi guys. It’s been half a year since I last played CTFs. Now I’m back at it to learn about heap exploitation. I will be making blogs like this about the techniques and the sample challenges to demonstrate them.</p><p>In this first episode, we will explore the poisoning of the pointers in the tcache linked list to allocate an arbitrary chunk (chunk that starts at any address we want).</p><p>To illustrate the concept, I’m gonna walk you through the challenge <strong>babyheap</strong> in <strong>DEFCON19 Qualifiers Round</strong>. Link to the challenge can be found <a href="https://github.com/guyinatuxedo/nightmare/tree/master/modules/29-tcache/dcquals19_babyheap"><strong>here</strong></a>.</p><blockquote><a href="https://github.com/guyinatuxedo/nightmare/tree/master/modules/29-tcache/dcquals19_babyheap">https://github.com/guyinatuxedo/nightmare/tree/master/modules/29-tcache/dcquals19_babyheap</a></blockquote><h3>Recon</h3><p>As usual, the first steps I always do are to <em>checksec</em> and decompile the binary in <em>ghidra</em>.</p><p>We can see that all protections are enabled:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oN5Dmf8XqCUNMbej4gG9gQ.png" /></figure><p>Let’s also find out the version of libc:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OIdWUpJBpwrtvV0eMeMCxw.png" /></figure><p>Fire up ghidra, we can then see the function which handles the main menu:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1006/1*0vFvTGsNV7BOZV3lPC1t7g.png" /></figure><p>Nothing interesting. Let’s check out the <strong>make</strong> function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1006/1*nvojg1Xk1jwKoCx4TX-_1w.png" /></figure><p>We can see from the “index_in_array” variable that we are only allowed to allocate at most 10 chunks. Also, this variable denotes the index given to the current chunk.</p><p>We can allocate only 0xf8 chunks and 0x178 chunks. Then, using global variables, the allocated chunk pointers and their corresponding <em>input sizes (not the size of the allocated chunk, which is 0x100 if you check the chunk header) </em>are stored in memory:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FtIKYsZYSyv0B1RyLuKXVA.png" /></figure><p>Note that in the <em>first while loop</em> above, which is :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/756/1*Iel145jHFu5VaIiuOz3_Ig.png" /></figure><p>Because plVar1 is a pointer, +2 is executed as pointer math, which means it increases the address that plVar1 is holding by 16 (since 64-bit binary, so 1 address is 8 bytes).</p><p>This segment of code also signifies that the <em>index given to the current chunk</em> relies on the first non-null size in “size_addr”.</p><p>Next up:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0V6efX0n8PqaUzGgSC4eiA.png" /></figure><p>Then it reads the user data into the allocated chunk until a null byte or newline is encountered. Because the counter variable “uVar3” is incremented after the read, this allows us to scan in “input size + 1” bytes. So we have a <strong><em>one-byte overflow</em></strong> here.</p><p>Next, let’s check out the <strong>destroy</strong> function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XKtlojmSngX9DW-S3rhZTg.png" /></figure><p>It first check if the pointer to the allocated chunk is already null to avoid double-free. Then it sets the pointers, sizes, and content to null and free the memory. So nothing that we can exploit here.</p><p>Finally, the <strong>show</strong> function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x1h5rHg8R6CuLAEFVKyMWw.png" /></figure><p>Hmm, <strong><em>puts</em></strong> might allow us to print beyond user data? Other than that, nothing special here.</p><p>Now since the libc version is ≥ 2.26 (see above), there’s <strong><em>tcache</em></strong>, which is:</p><blockquote>Each thread has a per-thread cache (called the tcache) containing a small collection of chunks which can be accessed without needing to lock an arena. These chunks are stored as <strong>an array of singly-linked lists</strong>, like fastbins, but with links pointing to the payload (user area) not the chunk header. <strong>Each bin contains one size chunk</strong>, so the array is indexed (indirectly) by chunk size. Unlike fastbins, the tcache is <strong>limited in how many chunks are allowed in each bin</strong> (tcache_count). If the tcache bin is empty for a given requested size, the next larger sized chunk is not used (could cause internal fragmentation), instead the fallback is to use the normal malloc routines i.e. locking the thread’s arena and working from there.<br><a href="https://sourceware.org/glibc/wiki/MallocInternals#Thread_Local_Cache_.28tcache.29">https://sourceware.org/glibc/wiki/MallocInternals#Thread_Local_Cache_.28tcache.29</a></blockquote><p>The heap manager does the following strategy <strong>when allocating chunks, in order</strong>:</p><blockquote><strong>Try the <em>fastbin/smallbin </em>recycling strategy</strong></blockquote><blockquote>- If a corresponding <em>fast bin</em> exists, try and find a chunk from there (and also opportunistically prefill the <em>tcache</em> with entries from the fast bin).</blockquote><blockquote>- Otherwise, if a corresponding <em>small bin</em> exists, allocate from there (opportunistically prefilling the <em>tcache</em> as we go).</blockquote><blockquote><strong>Resolve all the deferred frees</strong></blockquote><blockquote>- Otherwise “truly free” the entries in the fast-bins and move their consolidated chunks to the <em>unsorted </em>bin.</blockquote><blockquote>- Go through each entry in the <em>unsorted </em>bin. If it is suitable, stop. Otherwise, put the unsorted entry on its corresponding small/large bin as we go (possibly promoting small entries to the <em>tcache </em>as we go)<br><a href="https://azeria-labs.com/heap-exploitation-part-2-glibc-heap-free-bins/">https://azeria-labs.com/heap-exploitation-part-2-glibc-heap-free-bins/</a></blockquote><p>And it does the following strategy <strong>when we free chunks, in order</strong>:</p><blockquote>4. If the chunk fits into a <em>tcache </em>bin, store it there.</blockquote><blockquote>5. If the chunk has the <em>M </em>bit set, give it back to the operating system via <em>munmap</em>.</blockquote><blockquote>6. Otherwise we obtain the arena heap lock and then:</blockquote><blockquote>- If the chunk fits into a fastbin, put it on the corresponding fastbin, and we’re done.</blockquote><blockquote>- If the chunk is &gt; 64KB, consolidate the fastbins immediately and put the resulting merged chunks on the unsorted bin.</blockquote><blockquote>- Merge the chunk backwards and forwards with neighboring freed chunks in the small, large, and unsorted bins.</blockquote><blockquote>- If the resulting chunk lies at the top of the heap, merge it into the top of the heap rather than storing it in a bin.</blockquote><blockquote>- Otherwise store it in the <em>unsorted bin</em>. (<em>Malloc</em> will later do the work to put entries from the unsorted bin into the small or large bins).</blockquote><p>So our chunks will end up in the tcache. Now each tcache bin can hold a <strong>maximum of 7 chunks of the same size</strong>. We can free/allocate at most 10 chunks, so where do the last 3 free chunks go ?</p><p><em>As stated in the last dot point, it goes into the unsorted bin. The interesting thing is that if a freed chunk is </em><strong><em>the only chunk in unsorted_bin</em></strong><em>, then it will </em><strong><em>hold a pointer to an arena</em></strong><em>. But in our simple program, there’s only 1 arena — the main arena. So we get a </em><strong><em>pointer to some fixed offset in libc.</em></strong><em>(</em><a href="https://drive.google.com/file/d/1eJskblBnGMOM-lKyDKcqVFh8EQG1GB48/view"><em>https://drive.google.com/file/d/1eJskblBnGMOM-lKyDKcqVFh8EQG1GB48/view</em></a><em>)</em></p><p>To see how this looks like in gdb, allocate 8 chunks ≤ 0xf8 bytes, free them, allocate 8 chunks ≤ 0xf8 bytes again with content being “AAAAAAAA”, then show the content of the 8th chunk. Then we just have to grab the last 8 bytes of the output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SgNp930_67zDwvTn2DRYkA.png" /></figure><p>Then find libc base address with vmmap, and subtract “0x00007ffff7fedca0” from it to get the offset:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1die2RxoP4qlTQs6ReaLEA.png" /></figure><p>Since we are able to leak libc, this gives us a hint that maybe we should try to call one execve gadget?</p><h3>Exploitation</h3><p>So far we have:</p><ul><li>A one-byte overflow to the size of the next chunk.</li><li>A libc leak.</li></ul><p>As mention above the tcache bins are linked lists, so if we can modify the pointer (of a free chunk in tcache) to the next free chunk, then we should be able to allocate an arbitrary chunk?</p><p>But where do we modify it to? We don’t have any useful function in the binary to redirect to. However, we can modify it to allocate a chunk at “<strong>__malloc_hook</strong>”, a custom function that will be automatically called when “malloc” is called. Then we fill “_malloc_hook” with our one_gadget, so when we allocate a new chunk, we will pop a shell!!!</p><p>BOOM!!! Sounds like a good plan to me. Now we just have to figure out a way to modify the pointer of a free chunk in tcache.</p><p>Recall earlier in the recon section that we have a one-byte overflow to the size of the next chunk. Let’s assume that <strong>the next chunk is allocated</strong>. So if we <em>modify its siz</em>e to be 0x181 then <em>free</em> it, it will <em>end up in the 0x180-bytes tcache bin</em> rather than the 0xf8-bytes tcache bin. Then if we allocate another &gt; 0xf8-bytes chunk, we will be trying to allocate a 0x178 chunk due to how the program’s algorithm. So we get this same chunk back, but this time we are allowed to fill ≥ 0xf9 bytes into its 0xf8-bytes memory area !!</p><p>BOOM!!! We get an arbitrary write into the chunk after the modified one. If that chunk is freed and is in tcache bin, then we can overwrite its pointer to point to “<strong>__malloc_hook</strong>”. After that, we just keep allocating chunks in that tcache bin back until we reach “__malloc_hook”, then we fill into it our address to one_gadget. Finally, we allocate another chunk to trigger “__malloc_hook” and pop a shell !!!!!!!</p><p>One important thing to remember is that we must be careful with our input indices representing the chunks: In the <strong>make </strong>function, the algorithm traverses the global variable (which contains the pointers to the chunks) until a null pointer is encountered, then assign the current chunk that index. Also, because<strong><em> tcache is LIFO, we get back the chunks in reverse order to how we free them</em></strong>.</p><p>Another important thing to remember is we can’t provide null-bytes in our input since the make function only reads until ‘\0’ or ‘\n’ is encountered.</p><h3>To summary our attack plan</h3><ol><li>Allocate 10 chunks of 0xf8 bytes. Free the first 8. Reallocate 8 chunks with content “A”*8.</li><li>Show the content of the 8th chunk. Unpack the last 8 bytes, subtract from offset, and set libc base address.</li><li>Free the 0xf8 chunks in such a way that when we <strong><em>reallocate them, we get the chunks in this order</em></strong>: a chunk at the front to prevent coalescing backwards, the chunk whose size will be overwritten, the chunk that will perform the size overwrite, the chunk whose pointer will be overwritten to “__malloc_hook”, final chunk to prevent coalescing forwards.</li><li>Reallocate the chunks in the order above, the 3rd chunk should have content “A”*0xf8 + chr(0x81).</li><li>Free the 2nd chunk, it will end up in 0x180-bytes bin since its size was overwritten by the 3rd chunk’s content.</li><li>Free the 4th chunk, it will end up in 0xf8-bytes bin.</li><li>Allocate a 0x178-bytes chunk, we will get back the old 2nd chunk. Fill it with “A”*0xf8 + “B”*8 + address of “__malloc_hook”. Now the 4th chunk’s pointer is set to “__malloc_hook”.</li><li>Allocate a 0xf8-bytes chunk, we will get back the 4th chunk.</li><li>Allocate another 0xf8-bytes chunk, we will get back “__malloc_hook” . Fill it with the address of one_gadget.</li><li>Allocate another chunk to trigger “__malloc_hook” and hence, one_gadget.</li><li>ENJOY YOUR SHELL!! :)</li></ol><p>Here’s the link to my python code for the exploit: <a href="https://github.com/wechicken456/nightmare_solutions/blob/main/dcquals19_babyheap/my_exploit.py">https://github.com/wechicken456/nightmare_solutions/blob/main/dcquals19_babyheap/my_exploit.py</a></p><p>WOOSHH, that was pretty interesting. Such a small bug like a one-byte overflow can cause such catastrophic effect.</p><p>Please feel free to comment or dm me on Discord if you have any questions!</p><p>Discord: tuz#9881</p><p>Github: <a href="https://github.com/wechicken456">https://github.com/wechicken456</a></p><p>Happy pwning! Until the next time..</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5b38fb0c19b0" width="1" height="1" alt=""><hr><p><a href="https://infosecwriteups.com/heap-exploitation-journey-1-tcache-attack-5b38fb0c19b0">Heap exploitation journey #1 — Tcache attack</a> was originally published in <a href="https://infosecwriteups.com/">InfoSec Write-ups</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Codeforces problems #1–1471D]]></title>
            <link>https://medium.com/@pwnPHOfun/codeforces-problems-1-1471d-44b0544541?source=rss-4fdd6e2ce87e------2</link>
            <guid isPermaLink="false">https://medium.com/p/44b0544541</guid>
            <category><![CDATA[modular-arithmetic]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[codeforces]]></category>
            <dc:creator><![CDATA[pwnPH0fun]]></dc:creator>
            <pubDate>Thu, 02 Sep 2021 11:27:26 GMT</pubDate>
            <atom:updated>2022-06-05T13:41:22.261Z</atom:updated>
            <content:encoded><![CDATA[<p>Codeforces problems #1–1471D</p><h3>I’M BACK!</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Ftenor.com%2Fembed%2F14795798&amp;display_name=Tenor&amp;url=https%3A%2F%2Ftenor.com%2Fview%2Fim-back-i-am-back-arnold-schwarzenegger-arnie-arnold-gif-14795798&amp;image=https%3A%2F%2Fc.tenor.com%2FLKh5OdPVawYAAAAC%2Fim-back-i-am-back.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=tenor" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0739521c60fc53c6d22e58607e6a6c0/href">https://medium.com/media/f0739521c60fc53c6d22e58607e6a6c0/href</a></iframe><p>Wassssssssss upppppppppppppppp guysss? <br>It’s been a long time since I last blogged. The main reason for this is that I took a break from security stuffs and picked up programming again.</p><p>Just like CTFs in security, I’ve been solving problems on competitive programming platforms such as Codeforces, Atcoder, and Australian Informatics Olympiad (AIO). Today I solved such an intersting problem that I decided to start a random programming blog :D</p><h3>First post in the series: 1471D or 1470B — Strange Definition</h3><blockquote>The link to the problem: <a href="https://codeforces.com/contest/1471/problem/D">https://codeforces.com/contest/1471/problem/D</a></blockquote><h3>Problem analysis</h3><p>Let’s start with common things first. The relationship between <strong>gcd</strong> and <strong>lcm</strong> can be modelled by the equation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/338/1*ro4ZDeemuw41aHj_qVgBVA.png" /><figcaption><a href="https://en.wikipedia.org/wiki/Greatest_common_divisor">https://en.wikipedia.org/wiki/Greatest_common_divisor</a></figcaption></figure><p>Replace a,b with x,y to fit the context of the problem. It follows from the problem statement that:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/506/1*ciRAxR42yjUNE2WG62-xkw.png" /></figure><p>where n is an integer.</p><p>Because (gcd(x, y))²n² =( gcd(x,y)n )² =|xy|, it follows that |xy| must be a perfect square. But what are we going to do with this?</p><p><strong><em>Here comes the interesting part. Think about it, if xy = n² is a perfect square, then what happens to the product of the factors of x and y?</em></strong></p><p>You guessed it! Each final factors (after multiplying) must have an even power, cause that way when we square root it, we will get an <em>integer.</em></p><blockquote>For example, if xy = 18 = 2.3.3, how do we know if it’s not a perfect square? We can tell because there is only one 2 in the factorization — when we square root xy, the 2 becomes sqrt(2), and we get an irrational number.</blockquote><p>Let’s representing as x and y as a product of powers of its <strong>prime</strong> factors:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/1*xD9el7LbZYPsg_NRHc-PcQ.png" /></figure><p>Then it follows that in |xy|, for each i from 1 to infinity, (cnt1 + cnt2) % 2 = 0:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/1*_pUSqO7R52Lfz0jxqBz7Fw.png" /></figure><p>Then, it follows that <strong><em>cnt1 and cnt2 must have the same parity mod 2.</em></strong></p><p>Because only the parity/power of the factors determine if |xy| is a perfect square or not, we can represent each number as a product of its factor % 2:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jxvmgOIwTUhvfbi1Na7i-A.png" /></figure><p>After this replacement, factors with even powers are replaced by 1. They don’t change the logic because when we square root them we will get integers anyway.</p><p>So we are left with factors of power of 1. This implies that<strong> only equal numbers (numbers after the replacement) are <em>adjacent</em></strong>. Because that’s the only way for all of the sums of the powers of the factors can add up to 2, otherwise some sums would be odd and the numbers are not adjacent. So let’s <strong>group the equal numbers together</strong>.</p><p>Let’s consider what happens <strong><em>after the first second</em></strong>/operation: Each number is replaced by the product of themselves with their adjacent numbers. Then there are 2 scenarios for each group:</p><ul><li>If the number of integers in a group is even, then the sum of the powers will be <strong><em>even. By representing these powers as mod 2 like above, the numbers become 1</em></strong>. In this case, we <strong><em>group these 1s from a group with the other 1s in other groups, and the total number of adjacent numbers increase to equal to the numbers of 1s.</em></strong></li><li>Otherwise, the sum of the powers will be <strong><em>odd, and their mod 2 representation remain unchanged.</em></strong></li></ul><p>In the first case, the 1s don’t change if we apply the same logic, so the total number of adjacent numbers remain unchanged after the first opertaion. In the second case, nothing changes.</p><p>Since nothing changes after the first operation, the answer is the same for every query q &gt; 0:</p><ul><li>q = 0. The answer is <strong><em>the group with the most numbers in it.</em></strong></li><li>q &gt; 0. The answer is max(<strong><em>the group with the most numbers in it at 0th second, the total number of 1s</em></strong>).</li></ul><p>My C++ solution: <a href="https://github.com/wechicken456/codeforces/blob/master/1470B.cpp">https://github.com/wechicken456/codeforces/blob/master/1470B.cpp</a></p><p>Checkout my solutions to other problems (some have comments explaining what’s happening): <a href="https://github.com/wechicken456/codeforces">https://github.com/wechicken456/codeforces</a></p><h3>That’s all for today’s post</h3><p>That was a lot of simple maths.. But I think the solution is very elegant. It’s one of the problems that I find excited to blog about.</p><p>If you have any questions or suggesstions please feel free to comment down below or text me (links are below)!</p><p>Happy hacking! I mean, coding! Old habits I guess xD</p><p>Twitter: <a href="https://twitter.com/tuz283427619">https://twitter.com/tuz283427619</a></p><p>Instagram: <a href="https://www.instagram.com/hieunghiavuong/">https://www.instagram.com/hieunghiavuong/</a></p><p>Discord: tuz#3918</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=44b0544541" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[damCTF 2020 writeup — allokay]]></title>
            <link>https://medium.com/@pwnPHOfun/damctf-2020-writeup-allokay-5d4c4b8dca00?source=rss-4fdd6e2ce87e------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d4c4b8dca00</guid>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <category><![CDATA[damctf]]></category>
            <category><![CDATA[binary-exploitation]]></category>
            <category><![CDATA[pwn]]></category>
            <dc:creator><![CDATA[pwnPH0fun]]></dc:creator>
            <pubDate>Wed, 14 Oct 2020 08:00:32 GMT</pubDate>
            <atom:updated>2020-10-14T08:00:32.288Z</atom:updated>
            <content:encoded><![CDATA[<h3>damCTF 2020 writeup — allokay</h3><p>Hi everyone! It’s been a long time. Today I will walk you through the <em>pwn</em> challenge <strong>allokay</strong> in damCTF 2020.</p><p>Some basic information about the binary we are given:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-zRaS6HfQymo31Ex772l3A.png" /><figcaption>checksec and file</figcaption></figure><p>Open it up in ghidra:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c25AxENuXPVIHK6l0CNf_Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mLC-tWT_omlARcRaAG7tbw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AqhJFlSH8oeZJph2JrE5qA.png" /></figure><p>So the goal seems to be calling <em>win</em> with the parameter we control.</p><p>So in main we can see that we gives the program how many numbers (<strong>n</strong>) we can enter, then it does bound-checking, then call <em>get_input()</em> which scans in <strong>n</strong> numbers.</p><p>We can see it’s doing some arithmetics in <em>get_input()</em> before we can enter the numbers. Let’s go into gdb to figure it out!</p><p>Set a breakpoint before <em>scanf()</em> to figure out the address we’re writing into:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sXHQXnUzbqyG5e7NkUj7uQ.png" /></figure><p>Interesting, so we’re scanning into the top of the stack (notice <strong>$rsi</strong>). Let’s enter the number 1 then continue:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DA0a1o_V0rdQzBIgBcqMqg.png" /></figure><p>As you can see <strong>$rsi </strong>increments by 8 =&gt; So we’re making our way back to RBP and RIP:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lSjlk6VZslcGgMnL_CJqZw.png" /></figure><p>So <strong>RIP</strong> is 0x7fffffffe528–0x00007fffffffe4d0=88 bytes away from the 1st number we enter — that makes 88/8=11 longs. However if we keep going, our program exits:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TvIJdgZ1xvdBRntNShN_cg.png" /></figure><p>So what the hell happened there? Well we actually <em>overwrote the iterable variable and the amount of numbers we can scan. </em>Let’s look at the stack layout before we scan in the numbers (I entered the size to be 11 numbers):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MQiIBvX1HPlrEmR8JffvPQ.png" /></figure><p>Ghidra very kindly tells us that the index variable is located at <strong>$RIP-0x1c </strong>(hence how it is named). After entering the 1st number we should expect it to be 1 right? Because its type is long, but it is casted to int in comparison with <strong>input_size</strong>, so its value actually <strong><em>lies in the upper-half of the previous address</em></strong>. Also, its address is not a multiple of 8 so that’s one part of why it is accessed this way:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oAq7F8nBzI8Qa_fEIBC4tw.png" /></figure><p>When we’re scanning in numbers, we are actually scanning in <strong>longs not ints </strong>so that changes the entire 8-bytes — which means our <strong>index</strong> variable might be reset to 0 every time we enter a number less than 0xffffffff. But that should makes our program run longer and not exit right? Well, our <strong>input_size</strong> lies at <strong>$RIP-0x24</strong> which is also in our way and has share the same properties with our <strong>index</strong> variable:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ImfQH3a0sVA_dXyukBPMrA.png" /></figure><p>That explains it: Because the numbers are scanned as longs, their value affect the entire 8-bytes. So during scanning in we might accidentally reset the input_size to 0.</p><p>EXPLOITATION</p><p>We can easily overcome this by scanning in a number &gt; 0xffffffff. This will results in the upper-half holding a value. We know that <strong>$RIP</strong> is 11 longs away from our 1st number. We just have to calculate the offsets to our <strong>index</strong> variable and the <strong>input_size</strong> so that we can avoid crashing them. Also since we can control our <strong>index</strong> variable, we can set it to the offset to <strong>$RIP</strong> without worrying about the <strong>canary</strong>. Then we just have to call <em>win(ptr to “/bin/sh”) </em>with the gadget <strong>pop rdi; ret</strong>. Finally, where to store our string “/bin/sh\x00” — we can just include it in our name input like <strong>“14” + “a”*6 + “/bin/sh\x00”</strong>. Because <em>atoi()</em> stops when encountered a non-digit character, we append <strong>“a”*6 </strong>for alignment and <strong>“/bin/sh\x00” </strong>will be at <strong>name+8</strong>. Putting it all together:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uDte8N3W0GhSrzhMxUIyCA.png" /><figcaption><a href="https://github.com/wechicken456/damCTF-writeup">https://github.com/wechicken456/damCTF-writeup</a></figcaption></figure><p>Flag: dam{4Re_u_A11_0cK4y}</p><p>Thanks for reading! I hope this helps you! Stay tuned for more posts :)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1/1*xBHqAlD29bANLAJBl6O6AA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d4c4b8dca00" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DEFCON 2016 Quals writeup — feedme]]></title>
            <link>https://infosecwriteups.com/defcon-2016-quals-writeup-feedme-4d90167b448d?source=rss-4fdd6e2ce87e------2</link>
            <guid isPermaLink="false">https://medium.com/p/4d90167b448d</guid>
            <category><![CDATA[pwn]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <category><![CDATA[defcon]]></category>
            <category><![CDATA[reverse-engineering]]></category>
            <category><![CDATA[ctf]]></category>
            <dc:creator><![CDATA[pwnPH0fun]]></dc:creator>
            <pubDate>Mon, 10 Aug 2020 10:07:59 GMT</pubDate>
            <atom:updated>2022-09-29T20:06:32.379Z</atom:updated>
            <content:encoded><![CDATA[<h3>DEFCON 2016 Quals writeup — feedme</h3><p>Hi guys and welcome back to another CTF writeup. This time I will show you how I solved the pwn challenge feedme from DEFCON 2016 Quals.</p><p>A little bit of history first: After solving most pwn challenges from picoCTF2019 <strong><em>except</em></strong> for Ghost Diary, Sice-scream and Zero-to-hero, I got bored. Mostly because I’m a complete noob :/ — so I can’t solve them… yet. Then an idea came across my mind: Let’s try to solve 1 pwn challenge from past DEFCON tasks— just ONE is enough. So I searched for past DEFCON tasks and came across this: <a href="https://github.com/guyinatuxedo/nightmare/tree/master/modules/07-bof_static/dcquals16_feedme">https://github.com/guyinatuxedo/nightmare/tree/master/modules/07-bof_static/dcquals16_feedme</a></p><blockquote>This repo gathers a lot of pwn challenges from past events ranging from beginner to advanced. I’m so glad I found this. So shout-out to <br><strong><em>guyinatuxedo, noopnoop, digital cold </em></strong>and<strong><em> you neards </em></strong>for this wonderful resource<strong><em>.</em></strong></blockquote><p>Without any further delay, let’s get started.</p><p>Running it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mvz9yD_W3zK0AXjcc2kRwA.png" /></figure><p>We can give it 2 input. Apparently we smashed the stack canary but <em>pwntools couldn’t detect the canary :(</em>... It says the process terminated and child exit. Then I kept “<em>smashing</em>” :) and it kept spawning another process. So my guess is the program is forking over and over again.</p><p>It seems to be a stack <strong>bof</strong> (buffer overflow) vulnerability. Because it prints out our input again, I tried using the format string specifiers to leak the canary but it doesn’t work:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mJiI5u38-BizpGAPmp5Mew.png" /></figure><p>Let’s open it in Ghidra and see what’s going on:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MK1D0uHTqiZzCV4kWJwM9Q.png" /></figure><p>We get a lot of FUN.. functions. We can just go through all of them and see which one prints out the string <em>“FEED ME” </em>or we can just search for references to it:</p><ul><li>Go to Search =&gt; Memory =&gt; Choose String then enter FEED ME:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hWRuu1AEwHyCt6bRrP_1ow.png" /></figure><ul><li>Then double-click the location in the table. Select References =&gt; Show references to Address:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iHvh7XWRN37y3rxLDaG8rg.png" /></figure><p>It leads us to the function we’re looking for:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b1l1bsi5Rw0KSTZgUaUWXQ.png" /></figure><p>I renamed the functions to make it easier to understand. The first function just prints out “FEED ME!” so I guess that it’s <strong><em>puts</em></strong>. After that, by using some dynamic analysis, I figured out that the first one allows us to enter the <strong>size of input for the second line = ASCII code of the first character</strong> in that input. So if we enter “123456” then the size of our input for the second line would be 49 — which is “1” in the ASCII table. Then we feed the program the second input. After that I guess the next function is just returning the pointer to the first 16 characters of our second input since it’s being used with the format specifier “<em>%s</em>” in the next line.</p><p>So far we know that we can feed it 2 inputs, the size is in our control and we have a stack canary.</p><blockquote>For Canary, although the Canary is different each time the same process restarts (the same as GS, GS is restarted), but the Canary of each thread in the same process is identical. Also, there is a class that opens the child process interaction through the fork function, <strong>because the fork function directly copies the memory of the parent process, so the Canary of each child process created is the same</strong>. We can use this feature to completely blast Canary byte by byte.</blockquote><blockquote><a href="https://ctf-wiki.github.io/ctf-wiki/pwn/linux/mitigation/canary/">https://ctf-wiki.github.io/ctf-wiki/pwn/linux/mitigation/canary/</a></blockquote><p>Okay so we can bruteforce the canary. I wonder if there’s a limit to how many tries we have. Let’s see who’s calling <strong>feed_me_func. </strong>Right click the function =&gt; show references to:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0evBC8AbcnpQsd0LlyjuHA.png" /></figure><p>I’m a noob, so based on the while loop I guess that we have 800 tries :D Also we din’t see the line “<em>YUM, got blah blah blah</em>” in our output before. This is because as the program finds out that we smashed the canary, it terminated. So if we didn’t smash the canary, we would see that line in our output. This is quite helpful for us to know if we guessed the correct byte (for the canary) in our bruteforce.</p><p>We know that <strong><em>NX is enabled</em></strong> so our best shot would be ROP. Note that we have to give the program the correct input size because <em>the process only continues if we fed it the exact amount of bytes we said we would</em>, otherwise it would just wait for us to feed it more bytes. It took my dead ass 5 minutes to realise that :D</p><p>Anyway so the plan is to do a simple bruteforce for the canary byte-by-byte. This takes us 256*3 = 768 tries maximum (in 32-bit the last byte of the canary will always be \x00 =&gt; the canary looks like XXXXXX00), and we have 800 tries (I guess xD). Then overwrite EIP with our ROP chain. Since the binary is statically linked, we can search in it for ROP gadgets with ROPgadgets.</p><blockquote>When you <strong>statically</strong> link a file into an executable, the contents of that file are included at link time. In other words, the contents of the file are physically inserted into the executable that you will run.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v0gxfzFwFcImZj9PbFj2-A.png" /><figcaption>Bruteforcing the canary</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o3Ejo9r0otvgSKF8TvJwQQ.png" /><figcaption>ROP Chain</figcaption></figure><p>Let’s run it and hope that it works :D</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TrJm75DkrMvmNeLhYdEXQg.png" /></figure><p>Oof — Never thought a noob like me could solve a single challenge from DEFCON even just qualifier rounds.</p><p>Anyway this was a great experience and I learnt a lot from it. Many thanks to the authors of <strong>Nightmare</strong> for creating such a wonderful resource. Hope this was helpful to you in any way and thanks for reading :)</p><p>If you have any questions just please feel free to dm me (links are below)! Happy hacking!</p><p>Twitter: <a href="https://twitter.com/pwnphofun">https://twitter.com/pwnphofun</a></p><p>Github: <a href="https://github.com/wechicken456">https://github.com/wechicken456</a></p><p>Discord: tuz#9881</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4d90167b448d" width="1" height="1" alt=""><hr><p><a href="https://infosecwriteups.com/defcon-2016-quals-writeup-feedme-4d90167b448d">DEFCON 2016 Quals writeup — feedme</a> was originally published in <a href="https://infosecwriteups.com/">InfoSec Write-ups</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[csictf 2020 writeup — secret-society]]></title>
            <link>https://medium.com/@pwnPHOfun/csictf-2020-writeup-secret-society-c47a3122c7a9?source=rss-4fdd6e2ce87e------2</link>
            <guid isPermaLink="false">https://medium.com/p/c47a3122c7a9</guid>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[reverse-engineering]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[pwn]]></category>
            <dc:creator><![CDATA[pwnPH0fun]]></dc:creator>
            <pubDate>Wed, 22 Jul 2020 07:38:52 GMT</pubDate>
            <atom:updated>2020-07-22T07:38:52.004Z</atom:updated>
            <content:encoded><![CDATA[<h3>csictf 2020 writeup — secret-society</h3><p>Hello everyone and welcome back to another CTF writeup! This post will be about the <strong>secret-society</strong> <em>pwn</em> challenge from csictf 2020.</p><p>Running it gives us:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/535/1*d0yyAcdac4k89ISgFYNnuA.png" /></figure><p>No matter what input we give it, it will always return that phrase.</p><p>Let’s open it in Ghidra:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FOtwsJph3o9ZM7yhbT4L6w.png" /><figcaption>Original main</figcaption></figure><p>I made some changes to variable names so that it’s easier to understand and follow:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xlq_hiu79B_-mxElaBOrWg.png" /><figcaption>Modified main</figcaption></figure><p>It takes 0x80 characters from stdin which just fits the size of the allocated memory (i<strong>nput_str</strong>). Then it appends the phrase “, we are everywhere.” to input_str. After that it reads the content of flag.txt to <strong>flag</strong>. Finally it prints out our <strong>input_str</strong>.</p><p>Hmm not much right?</p><p>But hold on, let’s check out where input_str and flag are.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b0q3bwNzxgebRd2fSZN1qw.png" /><figcaption>input_str</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3f1kXthK93sFaj4LpAONeg.png" /><figcaption>flag</figcaption></figure><p>0xd0–0x80 = 0x50 :)</p><p>So our <strong><em>input_str</em></strong> is right next to the <strong><em>flag</em></strong>. So if we fill our buffer with 0x80 bytes, then <em>fgets()</em> won’t append ‘<strong>\0</strong>’ at the end of <strong>input_str</strong>, which when passed to<em> puts()</em> will link with the <strong>flag</strong> as there’s no ‘<strong>\0</strong>’ character to terminate <strong>input_str</strong></p><p>There we go, simple as that :D</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YWK8p858rEMhfgeu5GYSWQ.png" /></figure><p>Flag: csivit{Bu!!er_e3pl01ts_ar5_5asy}</p><p>Hope this helps! Stay tuned for more posts :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c47a3122c7a9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[csictf 2020 writeup — RickNMorty]]></title>
            <link>https://medium.com/@pwnPHOfun/csictf-2020-writeup-ricknmorty-a057c5226431?source=rss-4fdd6e2ce87e------2</link>
            <guid isPermaLink="false">https://medium.com/p/a057c5226431</guid>
            <category><![CDATA[reverse-engineering]]></category>
            <category><![CDATA[pwn]]></category>
            <category><![CDATA[rev]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <category><![CDATA[ctf]]></category>
            <dc:creator><![CDATA[pwnPH0fun]]></dc:creator>
            <pubDate>Wed, 22 Jul 2020 06:27:21 GMT</pubDate>
            <atom:updated>2020-07-22T06:29:31.062Z</atom:updated>
            <content:encoded><![CDATA[<h3>csictf 2020 writeup — RickNMorty</h3><p>Welcome back to another CTF writeup! In this post I will clearly walk your through the <strong>RickNMorty </strong><em>reverse engineering</em> challenge in csictf 2020.</p><p>We were given a binary. Runing it prints out 2 numbers, then we can provide input, then the same process repeats for a period of time. After that, it prints out the time that loop takes to finish:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*9LIeu9PG3bDwJMFCPGZ7yw.png" /></figure><p>Open it in Ghidra. The first part is just declaring variables:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/309/1*GVNhSJ7TUr1LEy0_knfAiw.png" /><figcaption>1st part</figcaption></figure><p>The second part is the logic:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/430/1*ZXQIKGS9flXPjTwlxnVVSA.png" /></figure><p>I’ve renamed it to make it easier to understand and follow.</p><p>First, the loop generates random numbers, does some math, passes the result to 2 variables <strong><em>a </em></strong>and <strong><em>b </em></strong>then prints them out.</p><p>After that, <strong><em>a </em></strong>is passed to<em> function1</em>. Let’s check it out:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*RVh-sAlVvm0sExad89QOCw.png" /><figcaption>function1</figcaption></figure><p>Hmm, weird, it takes 2 parameters but we only pass 1 to it. Let’s go to gdb and figure out what’s going on. Set a breakpoint before calling function1 then run the program:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/529/1*f6FSoNq4SITYK3p1tnZSbQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*Zal0LdpUlXDEhPz3A1kbIA.png" /></figure><p>13 in hex is 0xd and 12 in hex is 0xc. We can see that these values are passed to <strong><em>rsi </em></strong>and <strong><em>rdi </em></strong>as arguments to function1 (<a href="https://unix.stackexchange.com/questions/421910/why-did-the-system-call-registers-and-order-change-from-intel-32bit-to-64bit#:~:text=The%20system%20call%20convention%20is,needs%20%25rcx%20for%20other%20purposes.">https://unix.stackexchange.com/questions/421910/why-did-the-system-call-registers-and-order-change-from-intel-32bit-to-64bit#:~:text=The%20system%20call%20convention%20is,needs%20%25rcx%20for%20other%20purposes.</a>). So all clear now — function1 takes 2 arguments which are <strong><em>a </em></strong>and <strong><em>b</em></strong>.</p><p>Then it loops from<strong><em> i=1</em></strong> to <strong><em>max(param_1, param_2)</em></strong>. It then checks if both param_1 and param_2 are both divisible by i, or in other words, if i is a factor of both param_1 and param_2. If they are then assign local_c = i. So this function is just returning the greatest common factor of a and b.</p><p>Look back to main:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/176/1*4yFSFHQybJY9GP9bqWmOFA.png" /></figure><p>The result+3 is passed to <em>function2</em>. Let’s check it out:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/273/1*h2izkIA-oB6amNvbkEtyEQ.png" /><figcaption>function2</figcaption></figure><p>Simple enough, you can obviously figure what this does by yourself. It’s just returning the factorial of the parameter.</p><p>Back to main:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/1*VBkkEm6aFKXN92e_bXbLuw.png" /></figure><p>The result after these 2 functions are compared to the input value. If they aren’t equal then set local_c = 1. Our goal is to keep local_c the same throughout the whole loop while still finishing the loop within 30s.</p><p>So we just have to find greatest common factor <em>c = gcd(a,b)</em> then send back <em>factorial(c+3)</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uMN1voGeN5bedLvhgkgXNA.png" /><figcaption>solve.py</figcaption></figure><p>Output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t5NcYPY5UdTywyIqYnm0Dw.png" /></figure><p>Flag: csictf{h3_7u2n3d_h1m531f_1n70_4_p1ck13}</p><p>Hope this helps! Stay tuned for more posts :D</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a057c5226431" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[rgbCTF writeup — Advanced Reversing Mechanics 2]]></title>
            <link>https://medium.com/@pwnPHOfun/rgbctf-writeup-advanced-reversing-mechanics-2-37a7d4d15534?source=rss-4fdd6e2ce87e------2</link>
            <guid isPermaLink="false">https://medium.com/p/37a7d4d15534</guid>
            <category><![CDATA[pwn]]></category>
            <category><![CDATA[reverse-engineering]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <category><![CDATA[ctf]]></category>
            <dc:creator><![CDATA[pwnPH0fun]]></dc:creator>
            <pubDate>Thu, 16 Jul 2020 09:34:37 GMT</pubDate>
            <atom:updated>2020-07-16T09:34:37.507Z</atom:updated>
            <content:encoded><![CDATA[<h3>rgbCTF writeup — Advanced Reversing Mechanics 2</h3><p>Welcome back to another CTF writeup! This is a pwn/rev challenge from rgbCTF 2020. In this challenge we are given an array of bytes and a binary:</p><blockquote>0A, FB, F4, 88, DD, 9D, 7D, 5F, 9E, A3, C6, BA, F5, 95, 5D, 88, 3B, E1, 31, 50, C7, FA, F5, 81, 99, C9, 7C, 23, A1, 91, 87, B5, B1, 95, E4</blockquote><p>Similar to the first part, the binary is ARM. Opening it in Ghidra gives us:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/519/1*c7lW0Kht4Z7WWVPM2z0fPA.png" /></figure><p>Not much stuff, it just calls the <strong>encryptFlag </strong>function then print out the ciphertext.</p><p>The decompilation of <strong>encryptFlag</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/304/1*H9fJCriDJFrppwQiy6FNqw.png" /><figcaption>First part</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/690/1*tgXKQCyvvr3O_GxcZUA2kw.png" /><figcaption>Second part</figcaption></figure><p>The first part is just declaring variables and check whether the parameter passed to the function is null. The second part is the actually encryption algorithm. We can try to figure out this whole arithmetic process or we can jut bruteforce it :D</p><p>Since we already know that all the characters in the flag are <strong>printable charcters</strong> — their ASCII values range from 32 to 128. So I passed all the characters to the encryptFlag function and whatever returns that matches the corresponding element in the array give, we append it to our flag. I was too lazy to rewrite it in python so I just copied the decompilation code and added some bruteforce lines in C. :D</p><p>It looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PUd8wHSqka0kP_LwWdgMFA.png" /><figcaption>solve.c</figcaption></figure><p>Sorry for the bad quality image, I don’t know what’s wrong it snipping tool.. Since we knew that our flag starts with <em>rgbCTF{</em> we can start with the position 7. Then we create test_flag — a copy of our current flag, assign the last character to be a printable character, then we encrypt the test_flag. We check if the encrypted byte (the last character) matches with the corresponding one in expected. If it matches, we append it to our flag then exit the inner loop to reduce the runtime. Very basic and easy bruteforce right?</p><blockquote>Note: remember to declare the type <strong><em>byte </em></strong>and <strong><em>uint </em></strong>since C doesn’t know that they mean yet.</blockquote><p>I’ll put the solve script on my github: <a href="https://github.com/wechicken456/rgbCTF-2020-writeups/blob/master/Advanced_Reversing_Mechanics_2.c">https://github.com/wechicken456/rgbCTF-2020-writeups/blob/master/Advanced_Reversing_Mechanics_2.c</a></p><p>Output of the script:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/537/1*EBM01hc8kZAndNK6wUB3Gw.png" /></figure><p>Flag: flag: rgbCTF{ARM_ar1thm3t1c_r0cks_fad961}</p><p>If you approached this challenge differently, please share it! Anyway hope this helps! Stay tuned for more posts :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=37a7d4d15534" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[rgbCTF — otp]]></title>
            <link>https://medium.com/@pwnPHOfun/rgbctf-otp-b6346fe4a1eb?source=rss-4fdd6e2ce87e------2</link>
            <guid isPermaLink="false">https://medium.com/p/b6346fe4a1eb</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[capture-the-flag]]></category>
            <category><![CDATA[crypto]]></category>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <dc:creator><![CDATA[pwnPH0fun]]></dc:creator>
            <pubDate>Wed, 15 Jul 2020 07:23:33 GMT</pubDate>
            <atom:updated>2020-07-15T07:28:57.777Z</atom:updated>
            <content:encoded><![CDATA[<h3>rgbCTF writeup — otp</h3><p>In this challenge we were given a python script which seems to be the encryption algorithm:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qn65SCNCZFljin48rMHk9g.png" /></figure><p>It first generates the seed from the <strong>time()</strong> function. The 10 numbers don’t seem to have any effect on the encryption algorithm so we ignore it. Then it generate 40 random bytes, these are then XORed with the corresponding bytes in <strong>g</strong> (which is our flag). On GeeksforGeeks there’s a description of the <strong>seed()</strong> in python:</p><blockquote>Seed function is used to save the state of a random function, so that it can generate same random numbers on multiple executions of the code on the same machine or on different machines (for a specific seed value).</blockquote><p>So if we write a script that generates the same seed we should know the 40 bytes that are used to XORed with the flag. I used the same python script with some modifications to print out the 40 random bytes and<strong> n (ciphertext)</strong> :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wicGfHKKftDL5urBdNmUNQ.png" /></figure><p>This gives us the following output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8bQnz9QyNB-AVhML-mNrPQ.png" /></figure><p>Now we just have to XOR <strong>n</strong> with <strong>b</strong> to get our flag. Note that <strong>bytearray()</strong> in the encryption algorithm produces the ciphertext in little endian order — it is in reverse. So we will have to reverse it to get the flag:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3QNtJxDRdjEXJzwM8pEfkA.png" /></figure><p>Summary: <a href="https://github.com/wechicken456/rgbCTF-2020-writeups/blob/master/otp.txt">https://github.com/wechicken456/rgbCTF-2020-writeups/blob/master/otp.txt</a></p><p>Flag: rgbCTF{random_is_not_secure}</p><p>Hope this helps! Stay tuned for more posts :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b6346fe4a1eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[rgbCTF writeup — e]]></title>
            <link>https://medium.com/@pwnPHOfun/rgbctf-writeup-e-188d563d0948?source=rss-4fdd6e2ce87e------2</link>
            <guid isPermaLink="false">https://medium.com/p/188d563d0948</guid>
            <category><![CDATA[rsa]]></category>
            <category><![CDATA[blog]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <category><![CDATA[crypto]]></category>
            <category><![CDATA[ctf]]></category>
            <dc:creator><![CDATA[pwnPH0fun]]></dc:creator>
            <pubDate>Tue, 14 Jul 2020 08:38:04 GMT</pubDate>
            <atom:updated>2020-07-14T08:38:04.246Z</atom:updated>
            <content:encoded><![CDATA[<p>rgbCTF writeup — e</p><p>This is the <em>crypto </em>challenge <strong>e </strong>from rgbCTF 2020.</p><p>We were given a text file:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-4tATUj3XEm3a9Sb_nIOSg.png" /><figcaption>e.txt</figcaption></figure><p>It is RSA obviously. The plaintext in RSA is encrypted by using this formula: <strong>(m^e) mod n = c</strong>.</p><p>The ciphertext is decrypted by using this formula: <strong>(c ^d) mod totient_n</strong>. But does it always have to be this way?</p><p>In this challenge, I noticed that <strong>n </strong>is much more larger than <strong>c</strong>. So I thought that what if <strong>m^e &lt; n</strong>? Then “<strong>mod n</strong>” in the encryption is not in effect =&gt; <strong>m^e = c</strong>.</p><p>So I took the <strong>eth </strong>root of <strong>c </strong>to get <strong>m</strong>. Use the <strong>gmpy2</strong> module in python to be get as much precision as possible:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dDt4UT6TADqOpxPz1MQ6gg.png" /><figcaption>Python solve script</figcaption></figure><p>Hexdecode it using <strong>unhexlify </strong>from <strong><em>binascii</em></strong>. The flag is in reverse so reverse it.</p><p>Flag: rgbCTF{lucky_number_13}</p><p>Hope this helps! Stay tuned for more posts :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=188d563d0948" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[picoCTF2019 reverse_cipher]]></title>
            <link>https://medium.com/@pwnPHOfun/picoctf2019-reverse-cipher-10bfc4b1d1c2?source=rss-4fdd6e2ce87e------2</link>
            <guid isPermaLink="false">https://medium.com/p/10bfc4b1d1c2</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[reverse-engineering]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <category><![CDATA[ctf]]></category>
            <dc:creator><![CDATA[pwnPH0fun]]></dc:creator>
            <pubDate>Wed, 01 Jul 2020 06:40:23 GMT</pubDate>
            <atom:updated>2020-07-15T07:29:47.707Z</atom:updated>
            <content:encoded><![CDATA[<h3>picoCTF2019 writeup — reverse_cipher</h3><p>This is a 300 points reverse engineering challenge from picoCTF2019. We were given a binary and a text file. Open the binary in Ghidra. Skip the first few parts cause we’re only interested in the encryption algorithms:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*bsNDyZsgZLaBY5QgzMoZWA.png" /></figure><p>I renamed some variables so that it’s easier to recognize. The first loop doesn’t change any character of the flag so we know that it is picoCTF{</p><p>Inside the second loop, the condition is checking whether local_14 is an even number. If it is, then local_9 = flag[local_14] + ‘\x05’. Because of implicit conversion in C, flag[local_14]+0x5 takes the ASCII value of flag[local_14] then add 5 from it, then local_9 is assigned the character representation of that result. If it isn’t then local_9 = flag[local_14] — 2. Again, flag[local_14]-2 takes the ASCII value of flag[local_14] then subtract 2 from it, then local_9 is assigned the character representation of that result. Then local_9 is written into the output file.</p><p>So for every character at even positions in the encrypted text we just have to minus 5 from that ASCII value and for every odd position we just have to add 2 to it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*EetOCXl-JDqQ-pa6ewfqVg.png" /></figure><p>Remember to wrap the flag with picoCTF{}</p><p>Flag: picoCTF{r3v3rs369806a41}</p><p>Hope this help!! :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10bfc4b1d1c2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>