<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/">
  <channel>
    <title>Kimi · Jared Lynskey</title>
    <link>https://jared.lynskey.co.nz/en/tags/kimi/</link>
    <description>Short stories of value on software engineering and engineering leadership — performance reviews, hiring and scaling teams, CI/CD, DevOps monitoring and agentic AI. Written from Seoul.</description>
    <generator>Hugo</generator>
    <language>en</language>
    <copyright>© 2026 Jared Lynskey</copyright>
    <lastBuildDate>Sat, 21 Nov 2026 00:00:00 +1300</lastBuildDate>
    <atom:link href="https://jared.lynskey.co.nz/en/tags/kimi/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Kimi for Reverse Engineering, Claude for Modernisation: Python to Rust</title>
      <link>https://jared.lynskey.co.nz/en/posts/2026/kimi-claude-python-rust-modernization/</link>
      <guid isPermaLink="true">https://jared.lynskey.co.nz/en/posts/2026/kimi-claude-python-rust-modernization/</guid>
      <pubDate>Sat, 21 Nov 2026 00:00:00 +1300</pubDate>
      <dc:creator>Jared Lynskey</dc:creator>
      <category>Kimi</category>
      <category>Claude Code</category>
      <category>AI Agents</category>
      <category>Reverse Engineering</category>
      <category>Rust</category>
      <category>Python</category>
      <category>Software Modernisation</category>
      <description>How I used Kimi to work out proprietary localhost protocols, proved them in Python, then used Claude to rewrite it in Rust without changing its behaviour.</description>
      <content:encoded><![CDATA[<p>Some Korean banking websites still expect a handful of proprietary security programs to be running on your computer. The page doesn&rsquo;t reach them over the internet. It calls them on fixed localhost ports and asks for device information, installation status, a stamp for each request and an encrypted log of the machine. That works until the programs no longer support the operating system you use.</p>
<p>I wanted a small, maintainable compatibility service that could answer those calls without shipping anybody else&rsquo;s binaries, certificates or private keys. It ended up as one Rust program that listens only on <code>127.0.0.1</code> and gives the bank pages the requests and responses they expect. I run it on macOS and Linux. The Windows build passes the same tests in CI, but I haven&rsquo;t tried it against a bank page on a real Windows machine yet.</p>
<p>I used two AI tools, for two different jobs. Kimi helped me work out the protocols and prove a Python implementation against the real sites. Claude helped turn that working Python into the Rust program. Neither tool stood in for the other, and neither stood in for testing. The reverse engineering produced a specification, and the rewrite had to keep to it.</p>

<h2 class="relative group">The rules came first
    <div id="the-rules-came-first" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#the-rules-came-first" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>This was an interoperability project on my own machine, not an attempt to defeat a bank&rsquo;s controls. These are the rules the program keeps:</p>
<ul>
<li>It contains no proprietary executables, no captured private keys and no vendor certificates.</li>
<li>The device facts it reports are the current machine&rsquo;s real ones, and it generates fresh values for each request.</li>
<li>Every service stays on loopback, behind a local certificate authority that belongs to one user and can sign only for the loopback names.</li>
<li>Anything it doesn&rsquo;t implement is refused. There are no convincing placeholder successes.</li>
<li>The bank&rsquo;s server is the authority on what&rsquo;s acceptable. A payload I can decrypt locally is a unit test, and it proves nothing about compatibility.</li>
</ul>
<p>The rules made the project simpler as well as safer. I never needed a server to accept arbitrary data. I needed a client correct enough that the server would accept true data through the normal flow.</p>

<h2 class="relative group">Mapping the protocol with Kimi
    <div id="mapping-the-protocol-with-kimi" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#mapping-the-protocol-with-kimi" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>Before writing a replacement, I needed to know what the browser really does.</p>
<p>Kimi read the login pages and their JavaScript, traced the localhost requests and grouped the calls by role. One program handles transaction stamps. Another collects device and network facts. Others report installation status, keyboard support or the result of an environment scan. Some calls are JSONP over HTTPS and others are WebSockets. The port numbers and response shapes can&rsquo;t move, because the bank&rsquo;s JavaScript already depends on them.</p>
<p>What came out of this stage was a protocol map, which is a lot more useful than a pile of captured traffic:</p>
<table>
	<thead>
			<tr>
					<th>Role</th>
					<th>What the page expects from it</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>Stamp</td>
					<td>A handshake, a stamp for each request, certificate setup and an encrypted log of the machine</td>
			</tr>
			<tr>
					<td>Collector</td>
					<td>Device and network facts, over HTTPS and WebSockets</td>
			</tr>
			<tr>
					<td>Manager</td>
					<td>Which of the required programs are installed, and their versions</td>
			</tr>
			<tr>
					<td>Keyboard and keyguard</td>
					<td>Version, health and keepalive replies</td>
			</tr>
			<tr>
					<td>Scanner</td>
					<td>Replies to an environment scan</td>
			</tr>
	</tbody>
</table>
<p>For each operation I wrote down the request parameters, the response envelope, the content type, the callback behaviour and what a failure looks like. Then each observation was reduced to a small pure function: a salted encoding here, a cipher layer there, an ordered list of device fields somewhere else.</p>
<p>Keeping protocol facts apart from implementation choices paid off later. Port numbers and field names are fixed by the protocol. The Python modules, the Rust crates and the error types were mine to choose.</p>

<h2 class="relative group">Python as the executable specification
    <div id="python-as-the-executable-specification" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#python-as-the-executable-specification" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>Python was a good first language for this. Its cryptography libraries are convenient, byte manipulation is quick, and you can change a response and try the bank page again straight away. So the first working version was never a throwaway mock. It held everything I&rsquo;d learned, in a form I could run.</p>
<p>Each protocol rule got a local test with synthetic inputs. For one bank&rsquo;s encrypted device log, the tests generated a throwaway key and certificate, ran the encoder, and decrypted the result to check the field order and values. That caught plenty of encoding mistakes without going near a real account.</p>
<p>Then one failure showed why passing locally isn&rsquo;t enough.</p>

<h3 class="relative group">The request ID that was only right locally
    <div id="the-request-id-that-was-only-right-locally" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#the-request-id-that-was-only-right-locally" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>The log response looked right and decrypted correctly in the tests, but the bank rejected it with a security-program validation error. Rather than keep spending login attempts, Kimi compared fresh localhost responses from the bank&rsquo;s own installed program with the ones my Python produced.</p>
<p>The outer envelope had the same shape in both, and so did the encrypted key. The request ID didn&rsquo;t. The native program encrypted the ID with the session key before encoding it. Mine encrypted the device log but only encoded the ID.</p>
<p>With evidence that clear, the fix was small. The regression test now decrypts the request ID with the generated key, as it already did for the log. On the next real attempt the bank stopped returning the validation error and went on to check the ID and password. A later attempt completed the login.</p>
<p>That&rsquo;s the loop I want an agent to follow when it reverse engineers something: observe, state one precise hypothesis, change one thing, test it locally, and only then make a single end-to-end check. It gets a lot further than asking for ten speculative fixes at once.</p>

<h2 class="relative group">From observations to a Rust design with Claude
    <div id="from-observations-to-a-rust-design-with-claude" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#from-observations-to-a-rust-design-with-claude" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>Once the Python worked, the job changed, and I changed tools with it. Claude had nothing to discover. Its job was to keep the behaviour exactly as it was while the structure and the packaging improved.</p>
<p>The Rust code is split along the protocol&rsquo;s own boundaries: a module for each role, one for the machine&rsquo;s real operating-system and network facts, one for the cryptography and the local certificate authority, and one for the command line. Tokio, Hyper and Rustls do the async server and TLS work. Explicit result types mean a malformed certificate, a bad parameter or an unsupported operation fails on purpose. A lockfile pins the dependencies, and a toolchain file pins the compiler the releases are built with.</p>
<p>Rust also gives me one native executable for each operating system, so nobody needs a Python runtime or a PyInstaller bundle. The bundles had been the fragile part. The Intel Mac build wouldn&rsquo;t start because the cryptography package no longer ships an Intel wheel: pip compiled it from source against a different OpenSSL from the one PyInstaller bundles. For people who already work in Python, a small PyO3 package exposes the same Rust program as a module and a command, and it doesn&rsquo;t keep a second copy of the protocol logic.</p>
<p>I should be honest about what &ldquo;modernised&rdquo; means here. This wasn&rsquo;t a benchmark-driven rewrite, and I&rsquo;m not claiming a speed-up I haven&rsquo;t measured. The goals were smaller artefacts, clearer ownership of concurrency and errors, reproducible native builds and less packaging to carry around. Python was the right tool for discovery. Rust is easier for me to ship and maintain.</p>

<h2 class="relative group">The contract: new implementation, old behaviour
    <div id="the-contract-new-implementation-old-behaviour" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#the-contract-new-implementation-old-behaviour" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>A rewrite is only safe if you can say what mustn&rsquo;t change. I checked that at three levels.</p>
<figure class="diagram">
  <div role="img" aria-label="Diagram: four stages from top to bottom. The vendors&#39; programs, which the bank pages already expect, are observed to produce a Python reference checked against the real sites. Golden bytes and a 37-request corpus carry that behaviour into the Rust program. Socket tests and a smoke test of the built binary carry it into the release, one file for each operating system."><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 700 508" aria-hidden="true" focusable="false">
<rect class="d-panel" x="2" y="2" width="696" height="84" rx="14"></rect>
<text class="d-label" x="24" y="38" font-size="20">The vendors' programs</text>
<text class="d-sub" x="24" y="66" font-size="17">what the bank pages already expect</text>
<path class="d-line" style="fill:none;stroke-width:2" d="M48,86 V132"></path>
<path class="d-head" d="M48,142 l-6.5,-11 h13 z"></path>
<text class="d-sub" x="72" y="120" font-size="17">observed: what the pages ask, and what the programs answer</text>
<rect class="d-box" x="2" y="142" width="696" height="84" rx="14"></rect>
<text class="d-label" x="24" y="178" font-size="20">Python reference</text>
<text class="d-sub" x="24" y="206" font-size="17">checked against the real sites</text>
<path class="d-line" style="fill:none;stroke-width:2" d="M48,226 V272"></path>
<path class="d-head" d="M48,282 l-6.5,-11 h13 z"></path>
<text class="d-sub" x="72" y="260" font-size="17">golden bytes, and a 37-request corpus answered the same way</text>
<rect class="d-box" x="2" y="282" width="696" height="84" rx="14"></rect>
<text class="d-label" x="24" y="318" font-size="20">Rust program</text>
<text class="d-sub" x="24" y="346" font-size="17">same bytes, same refusals</text>
<path class="d-line" style="fill:none;stroke-width:2" d="M48,366 V412"></path>
<path class="d-head" d="M48,422 l-6.5,-11 h13 z"></path>
<text class="d-sub" x="72" y="400" font-size="17">socket tests, then a smoke test of the built binary</text>
<rect class="d-box" x="2" y="422" width="696" height="84" rx="14"></rect>
<text class="d-label" x="24" y="458" font-size="20">Release</text>
<text class="d-sub" x="24" y="486" font-size="17">one file for each operating system</text>
</svg>
</div><figcaption>Each arrow is a set of checks the next stage has to pass.</figcaption>
</figure>


<h3 class="relative group">Golden bytes from Python
    <div id="golden-bytes-from-python" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#golden-bytes-from-python" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>A folder of test fixtures holds output from the Python version: an encoded device record, its compressed form, some cipher output, a certificate authority it minted and a few stamps. The Rust tests compare their own results with those bytes.</p>
<p>Fixtures like these answer questions that ordinary unit tests can quietly redefine. Does the compressed stream match what the C library behind the Python version produced, byte for byte? Is the field order intact? Does a certificate authority made by the earlier version still work when the new one renews the server certificate?</p>

<h3 class="relative group">A request corpus across every role
    <div id="a-request-corpus-across-every-role" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#a-request-corpus-across-every-role" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>I ran the Python and Rust servers side by side against a corpus of 37 requests that covers every role and transport, refusals included. Similar JSON wasn&rsquo;t enough to pass. Status lines, CORS headers, content types and bodies all had to be identical.</p>
<p>The refusals belong in that corpus. A replacement that returns success where the reference rejects a request has changed the security behaviour, and I don&rsquo;t count that as compatible.</p>

<h3 class="relative group">Real sockets and the built program
    <div id="real-sockets-and-the-built-program" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#real-sockets-and-the-built-program" aria-label="Anchor">#</a>
    </span>
    
</h3>
<p>On top of the unit tests there are socket tests and a smoke test of the release binary. The smoke test starts the real program, then requires its status command to find every fixed port answering with the new local certificate. CI lints and tests the Rust code on macOS, Linux and Windows, and builds the Python wheel as a separate job. The release workflow runs the tests again on each target, builds, smoke-tests what it built, and only then packages it.</p>

<h2 class="relative group">Hardening is part of compatibility
    <div id="hardening-is-part-of-compatibility" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#hardening-is-part-of-compatibility" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>A local service that hands out device information is sensitive even though it never sees a password. Any website can name a localhost port in a script tag or a WebSocket, and CORS stops neither.</p>
<p>So the program checks the caller on every role and transport. It reads <code>Origin</code>, <code>Referer</code> and <code>Sec-Fetch-Site</code>, refuses an unexpected <code>Host</code>, and answers only the bank origins on its allow-list. The certificate authority can sign only for <code>127.0.0.1</code>, <code>localhost</code> and the one hostname a stamp page calls. Its private key stays in the user&rsquo;s data folder, and a throwaway container run never writes the key to disk at all.</p>
<p>Keyboard encryption and certificate signing aren&rsquo;t implemented, so they return explicit errors. Nothing fabricates encrypted output, and nothing pretends an install or an update succeeded.</p>
<p>None of that was bolted on after the rewrite. It was already behaviour, so the rewrite had to carry it over.</p>

<h2 class="relative group">The workflow, if you want to reuse it
    <div id="the-workflow-if-you-want-to-reuse-it" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#the-workflow-if-you-want-to-reuse-it" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>The split worked because each phase had its own definition of done.</p>
<ol>
<li>Map before you implement. Have the first agent read the browser code, the localhost calls and the request sequences that carry state, and keep what was observed apart from what&rsquo;s still a guess.</li>
<li>Build a small reference implementation. Python suited me: fast iteration, readable transforms and cryptographic round trips on synthetic data.</li>
<li>Go to the real service sparingly. Compare fresh responses, isolate one mismatch, and don&rsquo;t retry blind against something live.</li>
<li>Freeze the behaviour as fixtures before you change languages: golden bytes, a request corpus and the failure cases, none of them secret.</li>
<li>Let the second agent refactor against that contract. Ask it for module boundaries, typed errors, concurrency, packaging and CI, and don&rsquo;t ask it to reinterpret the protocol.</li>
<li>Test what you ship. That means the release binary on every target, which a library import or a clean compile won&rsquo;t tell you about.</li>
<li>Read the diff yourself. An agent can hold a lot of context, but it can&rsquo;t decide your legal scope, your appetite for risk or what compatibility has to mean.</li>
</ol>
<p>Separate git worktrees help when the exploring and the refactoring run in parallel. Without them, an exploratory edit and a structural rewrite can overwrite each other before the tests ever run. I wrote about that in <a href="https://jared.lynskey.co.nz/en/posts/2026/claude-code-git-worktrees/" >Claude Code Worktrees</a>.</p>

<h2 class="relative group">What I would do again
    <div id="what-i-would-do-again" class="anchor"></div>
    
    <span
        class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none">
        <a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#what-i-would-do-again" aria-label="Anchor">#</a>
    </span>
    
</h2>
<p>The prompts that worked best weren&rsquo;t &ldquo;reverse engineer this product&rdquo; or &ldquo;rewrite this in Rust&rdquo;. They were a run of narrower jobs:</p>
<ul>
<li>Recover this request&rsquo;s exact encoding and prove it with a local round trip.</li>
<li>Explain the difference between the native response and the reference one, without guessing.</li>
<li>Keep these bytes while replacing the implementation.</li>
<li>Make the release fail if any behaviour changes.</li>
</ul>
<p>That order also told me where a failure belonged. A protocol mismatch went back to discovery, a broken fixture to the rewrite, and a packaging failure to the release. Both tools were at their most useful when the evidence, and not their confidence, decided what happened next. Python sat between the two and gave each of them something concrete to keep to.</p>
]]></content:encoded>
      <media:content url="https://jared.lynskey.co.nz/en/posts/2026/kimi-claude-python-rust-modernization/featured.jpg" medium="image" type="image/jpeg" />
    </item>
  </channel>
</rss>
