<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:media="http://search.yahoo.com/mrss/">
  <channel>

    <title>Cryptography on less on sec</title>
    <link>https://lessonsec.com/tags/cryptography/</link>
    <description>
      Recent content in Cryptographyon less on sec
    </description>

    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <lastBuildDate>Sun, 03 Oct 2021 12:41:44 &#43;0200</lastBuildDate>
    <atom:link href="https://lessonsec.com/tags/cryptography/index.xml" rel="self" type="application/rss&#43;xml" /><item>
        <title>Walkthrough: CryptoHack CTF</title>
        <link>https://lessonsec.com/posts/walkthrough-cryptohack-ctf/</link>
        <guid isPermaLink="true">https://lessonsec.com/posts/walkthrough-cryptohack-ctf/</guid>
        <pubDate>Sun, 03 Oct 2021 12:41:44 &#43;0200</pubDate><description>Recently I&amp;rsquo;ve been meaning to get into cryptography more seriously, and to be honest I&amp;rsquo;ve also been postponing it for a while too, so I figured it was time I wrote this article to get motivated!</description>
	<content:encoded>&lt;p&gt;Recently I&amp;rsquo;ve been meaning to get into cryptography more seriously, and to be honest I&amp;rsquo;ve also been postponing it for a while too, so I figured it was time I wrote this article to get motivated!&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m approaching this cryptography deep dive with &lt;a href=&#34;https://cryptohack.org/&#34;&gt;https://cryptohack.org/&lt;/a&gt;.
Cryptohack it&amp;rsquo;s website offering CTF style challenges to understand and try to break modern cryptography. I really like this gamified approach so I decided to give it a shot.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Disclaimer&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;You always need to be extra careful when sharing CTFs solutions online. That&amp;rsquo;s the reason why I&amp;rsquo;m strictly following cryptohack&amp;rsquo;s guidelines.
As requested in the website&amp;rsquo;s &lt;a href=&#34;https://cryptohack.org/faq/#solutions&#34;&gt;FAQ&lt;/a&gt; I&amp;rsquo;m only sharing solutions for challenges worth 10 points or less.
These challenges are pretty basic, but I felt like it would be useful to have this kind of content published for those who are not familiar with basic cryptography or with the coding tools and technologies needed to solve the challenges. Each challenge solution will be explained but no flag will be available in this article.&lt;/p&gt;
&lt;p&gt;Cryptohack also has a functionality to share the solution once you get the flag for the challenge. Solutions to more complex challenges are to be shared exclusively there. The solutions are however only available for the solvers of the relative challenge.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Make sure to download the python notebook with the code snippets from this article &lt;a href=&#34;https://lessonsec.com/resources/cryptohack_walkthrough/cryptohack_walkthrough.ipynb&#34;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&#34;page-index&#34;&gt;Page index&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#setup&#34;&gt;Setup&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#introduction-challenges&#34;&gt;Introduction Challenges&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#finding-flags-2-pts&#34;&gt;Finding Flags&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#great-snakes-3-pts&#34;&gt;Great Snakes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#network-attacks-5-pts&#34;&gt;Network Attacks&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#general-challenges&#34;&gt;General Challenges&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#ascii-5-pts&#34;&gt;ASCII&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#hex-5-pts&#34;&gt;Hex&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#base64-10-pts&#34;&gt;Base64&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#bytes-and-big-integers-10-pts&#34;&gt;Bytes and Big Integers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#xor-starter-10-pts&#34;&gt;XOR Starter&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#mathematics&#34;&gt;Mathematics&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#vectors-10-pts&#34;&gt;Vectors&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#symmetric-ciphers&#34;&gt;Symmetric Ciphers&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#keyed-permutations-5-pts&#34;&gt;Keyed Permutations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#resisting-bruteforce-10-pts&#34;&gt;Resisting Bruteforce&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#rsa&#34;&gt;RSA&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#rsa-starter-1-10-pts&#34;&gt;RSA Starter 1&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#diffie-hellman&#34;&gt;Diffie-Hellman&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#diffie-hellman-starter-1-10-pts&#34;&gt;Diffie-Hellman Starter 1&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#crypto-on-the-web&#34;&gt;Crypto On The Web&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#token-appreciation-5-pts&#34;&gt;Token Appreciation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#jwt-sessions-10-pts&#34;&gt;JWT Sessions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://lessonsec.com/posts/walkthrough-cryptohack-ctf/#conclusions&#34;&gt;Conclusions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id=&#34;setup&#34;&gt;Setup&lt;/h2&gt;
&lt;p&gt;Before starting I suggest getting the official docker image provided in the FAQs.
You simply need to pull &lt;code&gt;hyperreality/cryptohack:latest&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To run the container simply run the provided command: &lt;code&gt;docker run -p 127.0.0.1:8888:8888 -it hyperreality/cryptohack:latest&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This will start a Jupyter Notebook server reachable at &lt;code&gt;localhost:8888&lt;/code&gt;.
If you don&amp;rsquo;t want to use notebooks to solve the challenges but still want to use the container because of dependencies, you can overwrite the entrypoint of the image with the following command: &lt;code&gt;docker run -it --entrypoint /bin/bash -p 127.0.0.1:8888:8888 -v hyperreality/cryptohack:latest&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Once the docker situation is under control, we can start working on the challenges.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&#34;introduction-challenges&#34;&gt;Introduction Challenges&lt;/h2&gt;
&lt;p&gt;These challenges are basically tutorials to get familiar with how the challenges on this website works.
They show the flag format, how to work with the challenge scripts and how to approach the network based attacks.&lt;/p&gt;
&lt;h4 id=&#34;finding-flags-2-pts&#34;&gt;Finding Flags (2 pts.)&lt;/h4&gt;
&lt;p&gt;Simply follow the instructions and copy-paste the flag in the text field.&lt;/p&gt;
&lt;h4 id=&#34;great-snakes-3-pts&#34;&gt;Great Snakes (3 pts.)&lt;/h4&gt;
&lt;p&gt;For this one you need to execute the provided python script, that will print out the flag.&lt;/p&gt;
&lt;h4 id=&#34;network-attacks-5-pts&#34;&gt;Network Attacks (5 pts.)&lt;/h4&gt;
&lt;p&gt;We need to interact with a TCP server using JSON messages.
The website suggests using python and &lt;code&gt;telnetlib&lt;/code&gt; to do so. It also provides an example showing how to interact with the server of this challenge.&lt;/p&gt;
&lt;p&gt;What we need to do to get the flag is to play around a little bit with the server and find the correct request to &lt;em&gt;buy&lt;/em&gt; a &lt;em&gt;flag&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Solution&lt;/u&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# import the libraries needed for the challenge&lt;/span&gt;
&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; telnetlib
&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; json

HOST &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;socket.cryptohack.org&amp;#34;&lt;/span&gt;
PORT &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;11112&lt;/span&gt;

&lt;span style=&#34;color:#75715e&#34;&gt;# initialize the connection&lt;/span&gt;
tn &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; telnetlib&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;Telnet(HOST, PORT)

&lt;span style=&#34;color:#75715e&#34;&gt;# define functions to receive and send JSON payloads over TCP&lt;/span&gt;
&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;readline&lt;/span&gt;():
    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; tn&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;read_until(&lt;span style=&#34;color:#e6db74&#34;&gt;b&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;\n&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;)

&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;json_recv&lt;/span&gt;():
    line &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; readline()
    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; json&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;loads(line&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;decode())

&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;json_send&lt;/span&gt;(hsh):
    request &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; json&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;dumps(hsh)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;encode()
    tn&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;write(request)
  
&lt;span style=&#34;color:#75715e&#34;&gt;# reads the banner printed by the server&lt;/span&gt;
print(readline())
print(readline())
print(readline())
print(readline())

&lt;span style=&#34;color:#75715e&#34;&gt;# ------ Request example ------&lt;/span&gt;
&lt;span style=&#34;color:#75715e&#34;&gt;# Compose a request for the server&lt;/span&gt;
request &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; {&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;buy&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;clothes&amp;#34;&lt;/span&gt;}

&lt;span style=&#34;color:#75715e&#34;&gt;# Sends the request&lt;/span&gt;
json_send(request)

&lt;span style=&#34;color:#75715e&#34;&gt;# Gets the response&lt;/span&gt;
response &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; json_recv()

print(response) &lt;span style=&#34;color:#75715e&#34;&gt;# {&amp;#39;error&amp;#39;: &amp;#39;Sorry! All we have to sell are flags.&amp;#39;}&lt;/span&gt;

&lt;span style=&#34;color:#75715e&#34;&gt;# ------ Real request ------&lt;/span&gt;
&lt;span style=&#34;color:#75715e&#34;&gt;# mhhh flags you say?&lt;/span&gt;
request &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; {&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;buy&amp;#34;&lt;/span&gt;: &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;flag&amp;#34;&lt;/span&gt;}

&lt;span style=&#34;color:#75715e&#34;&gt;# Sends the request&lt;/span&gt;
json_send(request)

&lt;span style=&#34;color:#75715e&#34;&gt;# Gets the response&lt;/span&gt;
response &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; json_recv()

print(response)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;hr&gt;
&lt;h2 id=&#34;general-challenges&#34;&gt;General Challenges&lt;/h2&gt;
&lt;h3 id=&#34;uencodingu&#34;&gt;&lt;u&gt;Encoding&lt;/u&gt;&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;For these challenges it&amp;rsquo;s not really necessary to write any code. While writing your own scripts can help getting familiar with tools and techniques, a deeper understanding of encodings can be obtained solving the challenges in different ways.&lt;/p&gt;
&lt;p&gt;A super-versatile and commonly used tool for this kind of task is &lt;a href=&#34;https://gchq.github.io/CyberChef/&#34;&gt;CyberChef&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4 id=&#34;ascii-5-pts&#34;&gt;ASCII (5 pts.)&lt;/h4&gt;
&lt;p&gt;We are given a 7-bit ASCII encoded string and we need to decode it to get the flag.
The challenge hint suggests that we use the python &lt;code&gt;chr()&lt;/code&gt; function to do to.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Solution&lt;/u&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# ASCII values to decode&lt;/span&gt;
values &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; [&lt;span style=&#34;color:#ae81ff&#34;&gt;99&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;114&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;121&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;112&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;116&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;111&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;123&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;65&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;83&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;67&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;73&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;73&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;95&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;112&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;114&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;49&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;110&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;116&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;52&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;98&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;108&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;51&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;125&lt;/span&gt;]

solution &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;
&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; v &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; values:
    solution &lt;span style=&#34;color:#f92672&#34;&gt;+=&lt;/span&gt; chr(v)
    
print(solution)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id=&#34;hex-5-pts&#34;&gt;Hex (5 pts.)&lt;/h4&gt;
&lt;p&gt;In this challenge we are provided with an hex encoded string we need to decode.
This time the challenge hint suggests using the &lt;code&gt;bytes.fromhex()&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Solution&lt;/u&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# HEX values to decode&lt;/span&gt;
values &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;63727970746f7b596f755f77696c6c5f62655f776f726b696e675f776974685f6865785f737472696e67735f615f6c6f747d&amp;#34;&lt;/span&gt;

solution &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; bytes&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;fromhex(values)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;decode(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;utf-8&amp;#39;&lt;/span&gt;)

print(solution)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id=&#34;base64-10-pts&#34;&gt;Base64 (10 pts.)&lt;/h4&gt;
&lt;p&gt;For this challenge we are given an hex encoded string, to be decoded and then encoded in base64 to be used as flag.
In this case we will be using the &lt;code&gt;base64&lt;/code&gt; python module, in particular the &lt;code&gt;base64.b64encode()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Solution&lt;/u&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; base64

&lt;span style=&#34;color:#75715e&#34;&gt;# HEX values to decode&lt;/span&gt;
values &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;72bca9b68fc16ac7beeb8f849dca1d8a783e8acf9679bf9269f7bf&amp;#34;&lt;/span&gt;

&lt;span style=&#34;color:#75715e&#34;&gt;# Decoded values&lt;/span&gt;
tmp &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; bytes&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;fromhex(values)
&lt;span style=&#34;color:#75715e&#34;&gt;# print(tmp)&lt;/span&gt;
solution &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; base64&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;b64encode(tmp)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;decode(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;utf-8&amp;#39;&lt;/span&gt;)

print(solution)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h4 id=&#34;bytes-and-big-integers-10-pts&#34;&gt;Bytes and Big Integers (10 pts.)&lt;/h4&gt;
&lt;p&gt;Some cryptosystems like RSA work only when applied to numbers. We need to encode our messages as numbers in order to work with these cryptosystems.
One method to do so is to represent the data as bytes and convert these in a base-16 or base-10 number.&lt;/p&gt;
&lt;p&gt;In this challenge we are provided with a message encoded in this way and we need to get the original message out.&lt;/p&gt;
&lt;p&gt;For this challenge the PyCryptodome library it needed, we can work with this encoding using the functions: &lt;code&gt;Crypto.Util.number.bytes_to_long()&lt;/code&gt; and &lt;code&gt;Crypto.Util.number.long_to_bytes()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Solution&lt;/u&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;color:#f92672&#34;&gt;from&lt;/span&gt; Crypto.Util.number &lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; long_to_bytes

&lt;span style=&#34;color:#75715e&#34;&gt;# Message encoded as number&lt;/span&gt;
values &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;11515195063862318899931685488813747395775516287289682636499965282714637259206269&lt;/span&gt;

solution &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; long_to_bytes(values)&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;decode(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;utf-8&amp;#39;&lt;/span&gt;)

print(solution)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;hr&gt;
&lt;h3 id=&#34;uxoru&#34;&gt;&lt;u&gt;XOR&lt;/u&gt;&lt;/h3&gt;
&lt;h4 id=&#34;xor-starter-10-pts&#34;&gt;XOR Starter (10 pts.)&lt;/h4&gt;
&lt;p&gt;In this challenge we need to XOR the value 13 to each character of the provided string, then we need to put the result in the cyber{flag} format.
The hint suggests that it&amp;rsquo;s possible to use the &lt;code&gt;xor()&lt;/code&gt; function from &lt;code&gt;pwntools&lt;/code&gt; but it&amp;rsquo;s just as easy to do the same in pure python.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Solution&lt;/u&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Provided string&lt;/span&gt;
values &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;label&amp;#34;&lt;/span&gt;

solution &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&amp;#34;&lt;/span&gt;
&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; v &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; values:
    solution &lt;span style=&#34;color:#f92672&#34;&gt;+=&lt;/span&gt; chr(ord(v) &lt;span style=&#34;color:#f92672&#34;&gt;^&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;13&lt;/span&gt;)
    
&lt;span style=&#34;color:#75715e&#34;&gt;# The {{{var}}} syntax is needed to excape curly braces in python f-strings&lt;/span&gt;
print(&lt;span style=&#34;color:#e6db74&#34;&gt;f&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;crypto&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;{{&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;{&lt;/span&gt;solution&lt;span style=&#34;color:#e6db74&#34;&gt;}&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;}}&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;hr&gt;
&lt;h2 id=&#34;mathematics&#34;&gt;Mathematics&lt;/h2&gt;
&lt;h3 id=&#34;ulatticesu&#34;&gt;&lt;u&gt;Lattices&lt;/u&gt;&lt;/h3&gt;
&lt;h4 id=&#34;vectors-10-pts&#34;&gt;Vectors (10 pts.)&lt;/h4&gt;
&lt;p&gt;In this challenge we are asked to perform operations on a three dimensional vector space.
If this sounds new to you make sure to carefully read the challenge description and check the suggested materials.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Solution&lt;/u&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;v &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;6&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;)
w &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;)
u &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; (&lt;span style=&#34;color:#ae81ff&#34;&gt;7&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;7&lt;/span&gt;,&lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;)

&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;vector_minus&lt;/span&gt;(a, b):
   &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; [x &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt; y &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; x, y &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; zip(a,b)]

&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;vector_dot&lt;/span&gt;(a,b):
    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; sum([x &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; y &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; x, y &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; zip(a,b)])
    
&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;scalar_times&lt;/span&gt;(a, times):
    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; list(map( &lt;span style=&#34;color:#66d9ef&#34;&gt;lambda&lt;/span&gt; x: x &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; times , a))

&lt;span style=&#34;color:#75715e&#34;&gt;# calculate 3*(2*v - w) ∙ 2*u&lt;/span&gt;
vector_dot(scalar_times(vector_minus(scalar_times(v, &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;), w), &lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;), scalar_times(u, &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;hr&gt;
&lt;h2 id=&#34;symmetric-ciphers&#34;&gt;Symmetric Ciphers&lt;/h2&gt;
&lt;p&gt;&lt;u&gt;&lt;strong&gt;How AES Works&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;
&lt;h4 id=&#34;keyed-permutations-5-pts&#34;&gt;Keyed Permutations (5 pts.)&lt;/h4&gt;
&lt;p&gt;In this challenge we are asked to answer a question: &lt;em&gt;What is the mathematical term for a one-to-one correspondence?&lt;/em&gt;
Google is your friend for this one!&lt;/p&gt;
&lt;h4 id=&#34;resisting-bruteforce-10-pts&#34;&gt;Resisting Bruteforce (10 pts.)&lt;/h4&gt;
&lt;p&gt;This time we are asked: &lt;em&gt;What is the name for the best single-key attack against AES?&lt;/em&gt;                &lt;br&gt;
Just make sure you carefully read the challenge description and you&amp;rsquo;re good to go!&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&#34;rsa&#34;&gt;RSA&lt;/h2&gt;
&lt;p&gt;&lt;u&gt;&lt;strong&gt;Starter&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;
&lt;h4 id=&#34;rsa-starter-1-10-pts&#34;&gt;RSA Starter 1 (10 pts.)&lt;/h4&gt;
&lt;p&gt;The basis of RSA encryption is modular exponentiation. In this challenge we are asked to use such technique to create a &amp;ldquo;trapdoor function&amp;rdquo; (a function easy to calculate but hard to reverse).
This can be done using the &lt;code&gt;pow()&lt;/code&gt; function that python provides.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Solution&lt;/u&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# Calculate 101^17 mod 22663&lt;/span&gt;
pow(&lt;span style=&#34;color:#ae81ff&#34;&gt;101&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;17&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;22663&lt;/span&gt;)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;hr&gt;
&lt;h2 id=&#34;diffie-hellman&#34;&gt;Diffie-Hellman&lt;/h2&gt;
&lt;p&gt;&lt;u&gt;&lt;strong&gt;Starter&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;
&lt;h4 id=&#34;diffie-hellman-starter-1-10-pts&#34;&gt;Diffie-Hellman Starter 1 (10 pts.)&lt;/h4&gt;
&lt;p&gt;The Diffie-Hellman algorithm works with finite fields and modular exponentiation to allow to parties to exchange a shared secret.
If you&amp;rsquo;re not familiar with this algorithm or with the math behind it I would suggest to check out the &lt;a href=&#34;https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange#Cryptographic_explanation&#34;&gt;Wikipedia page&lt;/a&gt; to get started.&lt;/p&gt;
&lt;p&gt;In this challenge we are asked to find an inverse element given the prime number and the modulo.&lt;/p&gt;
&lt;p&gt;&lt;u&gt;Solution&lt;/u&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;g &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;209&lt;/span&gt;
p &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;991&lt;/span&gt;
fc &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;

&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; x &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; range(&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;, p):    
    &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (g &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt; x) &lt;span style=&#34;color:#f92672&#34;&gt;%&lt;/span&gt; p &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; fc:         
        print(x)        
        &lt;span style=&#34;color:#66d9ef&#34;&gt;break&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;hr&gt;
&lt;h2 id=&#34;crypto-on-the-web&#34;&gt;Crypto On The Web&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;&lt;u&gt;JSON web tokens&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h4 id=&#34;token-appreciation-5-pts&#34;&gt;Token Appreciation (5 pts.)&lt;/h4&gt;
&lt;p&gt;&lt;a href=&#34;https://datatracker.ietf.org/doc/html/rfc7519&#34;&gt;JWTs or JSON Web Tokens&lt;/a&gt; are a standard method to safely represent claims between two parties.
This kind of token is not encrypted by default, and this is the reason why it&amp;rsquo;s possible to reverse the encoding and extract the original message.&lt;/p&gt;
&lt;p&gt;We are given the token:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmbGFnIjoiY3J5cHRve2p3dF9jb250ZW50c19j&lt;/code&gt;
&lt;code&gt;YW5fYmVfZWFzaWx5X3ZpZXdlZH0iLCJ1c2VyIjoiQ3J5cHRvIE1jSGFjayIsImV4cCI6MjAwNT&lt;/code&gt;
&lt;code&gt;AzMzQ5M30.shKSmZfgGVvd2OSB2CGezzJ3N6WAULo3w9zCl_T47K&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Now, there are a few ways to solve this challenge, the suggested one is to use Python&amp;rsquo;s &lt;a href=&#34;https://pyjwt.readthedocs.io/en/stable/&#34;&gt;PyJWT&lt;/a&gt; library, but since it&amp;rsquo;s not installed in the docker container we are using, it&amp;rsquo;s easier to use an online tool like &lt;a href=&#34;https://gchq.github.io/CyberChef&#34;&gt;CyberChef&lt;/a&gt; or &lt;a href=&#34;https://jwt.io/&#34;&gt;jwt.io&lt;/a&gt;.&lt;/p&gt;
&lt;h4 id=&#34;jwt-sessions-10-pts&#34;&gt;JWT Sessions (10 pts.)&lt;/h4&gt;
&lt;p&gt;In this challenge we are given some information about the use of JWT tokens, now we are asked the &lt;em&gt;HTTP header used by the browser to send JWTs to the server&lt;/em&gt;. Once again Google is your friend!&lt;/p&gt;
&lt;p&gt;If you want to solve this challenge on your own, take out the developer tools in your browser, go to the network tab and start looking around for HTTP headers that could refer to the use of JWT tokens. You&amp;rsquo;re &lt;em&gt;authorized&lt;/em&gt; to do that!&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&#34;conclusions&#34;&gt;Conclusions&lt;/h2&gt;
&lt;p&gt;Hope this article is useful to anyone who&amp;rsquo;s meaning to get into cryptography or CTFs in general.
Writing this article allowed me to go back and review my knowledge of basic cryptography as well as exploring a bit out of my comfort zone (when it came to more complex challenges not included in the writeup).&lt;/p&gt;
</content:encoded>

      </item>

  </channel>
</rss>
