<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>naftpink - bytecode</title>
    <link>https://naft.pink</link>
    <description>Notes tagged bytecode on naftpink.</description>
    <language>en</language>
    <atom:link href="https://naft.pink/blog/tags/bytecode/feed.xml" rel="self" type="application/xml" />
    <item>
      <title>the only honest test of an obfuscator is writing the deobfuscator</title>
      <link>https://naft.pink/blog/obfuscator-and-deobfuscator</link>
      <guid isPermaLink="true">https://naft.pink/blog/obfuscator-and-deobfuscator</guid>
      <pubDate>Sun, 26 Jul 2026 00:00:00 GMT</pubDate>
      <description>I built a Java obfuscator and the tool that undoes it, both on ASM. Writing the second one is what told me which parts of the first were protection and which were just mess.</description>
      <content:encoded><![CDATA[

<p>Years ago I wrote an <a href="https://naft.pink/projects/java-obfuscator">obfuscator for Java programs and a deobfuscator that
undoes it</a>, both built on ASM, both working on
compiled bytecode rather than source. This was 2021/2022 and I&#x27;m writing about
it from memory rather than from the code, so treat the specifics below as
roughly-remembered rather than a transcript - the lesson is the part I&#x27;m sure
of.</p>
<p>Building the second one is the only reason I learned anything from the first.</p>
<h2 id="an-obfuscator-always-looks-like-it-works">an obfuscator always looks like it works</h2>
<p>This is the trap, and it&#x27;s structural rather than a mistake anyone makes out of
carelessness.</p>
<p>You run your tool. You open the output in a decompiler. It&#x27;s unreadable - names
are gone, the control flow is soup, the strings aren&#x27;t there. It looks like
exactly what you set out to build, and every instinct says you&#x27;re done.</p>
<p>But &quot;I can&#x27;t read this&quot; and &quot;this is expensive to reverse&quot; are different claims,
and only the second one matters. The first is guaranteed by construction: you
scrambled it, so of course it looks scrambled to you. You&#x27;re the least qualified
person to judge, because you&#x27;re reading output you already know the shape of and
grading it on how alien it feels.</p>
<p>The gap between those two claims only becomes visible when somebody sits down
and actually tries to undo it. The fastest way to arrange that is to be them.</p>
<h2 id="bytecode-not-source">bytecode, not source</h2>
<p>Worth saying what layer this operates at, because it changes what&#x27;s available.</p>
<p>The tool doesn&#x27;t rewrite Java and recompile. It reads the <code>.class</code> files
<code>javac</code> already produced and rewrites those directly. ASM is a visitor over the
class format - you hand it a reader, it walks the structure, and you intercept
the parts you care about:</p>
<figure data-rehype-pretty-code-figure=""><pre tabindex="0" data-language="java" data-theme="rose-pine-moon"><code data-language="java" data-theme="rose-pine-moon"><span data-line=""><span style="color:#3E8FB0">class</span><span style="color:#9CCFD8"> Renamer</span><span style="color:#3E8FB0"> extends</span><span style="color:#C4A7E7;font-style:italic"> ClassVisitor</span><span style="color:#908CAA"> {</span></span>
<span data-line=""><span style="color:#908CAA">    @</span><span style="color:#3E8FB0">Override</span></span>
<span data-line=""><span style="color:#3E8FB0">    public</span><span style="color:#3E8FB0"> MethodVisitor</span><span style="color:#EA9A97"> visitMethod</span><span style="color:#908CAA">(</span></span>
<span data-line=""><span style="color:#3E8FB0">            int</span><span style="color:#C4A7E7;font-style:italic"> access</span><span style="color:#908CAA">,</span><span style="color:#3E8FB0"> String</span><span style="color:#C4A7E7;font-style:italic"> name</span><span style="color:#908CAA">,</span><span style="color:#3E8FB0"> String</span><span style="color:#C4A7E7;font-style:italic"> desc</span><span style="color:#908CAA">,</span><span style="color:#3E8FB0"> String</span><span style="color:#C4A7E7;font-style:italic"> sig</span><span style="color:#908CAA">,</span><span style="color:#3E8FB0"> String</span><span style="color:#908CAA">[]</span><span style="color:#C4A7E7;font-style:italic"> ex</span><span style="color:#908CAA">)</span><span style="color:#908CAA"> {</span></span>
<span data-line=""><span style="color:#3E8FB0">        return</span><span style="color:#E0DEF4;font-style:italic"> super</span><span style="color:#908CAA">.</span><span style="color:#EA9A97">visitMethod</span><span style="color:#908CAA">(</span><span style="color:#E0DEF4">access</span><span style="color:#908CAA">,</span><span style="color:#EA9A97"> rename</span><span style="color:#908CAA">(</span><span style="color:#E0DEF4">name</span><span style="color:#908CAA">),</span><span style="color:#E0DEF4"> desc</span><span style="color:#908CAA">,</span><span style="color:#E0DEF4"> sig</span><span style="color:#908CAA">,</span><span style="color:#E0DEF4"> ex</span><span style="color:#908CAA">);</span></span>
<span data-line=""><span style="color:#908CAA">    }</span></span>
<span data-line=""><span style="color:#908CAA">}</span></span></code></pre></figure>
<p>Two things follow from working down here. The first is that the tool never needs
source, which is the entire premise of the deobfuscator - if you had the source
you wouldn&#x27;t be running it. The second is that the JVM will happily execute
class files <code>javac</code> would never emit. There are shapes down there that no Java
file can describe, and a source-level tool simply cannot reach them.</p>
<h2 id="what-the-reverser-kept-killing">what the reverser kept killing</h2>
<p>The pattern that repeated: I&#x27;d add a transformation, admire how bad the output
looked, then write the undo for it and find out it was a lot less work than the
obfuscation had been.</p>
<p>That&#x27;s the useful signal. A transformation that a mechanical pass reverses
completely isn&#x27;t protection - it&#x27;s <em>noise</em>. It costs you real things: bigger
artefacts, slower loading, sometimes a genuine behaviour change you don&#x27;t notice
for weeks. And it buys nothing against anyone with the patience to write the
reversal.</p>
<p>Renaming is the clearest case. Stripping every identifier down to <code>a</code>, <code>b</code>, <code>aa</code>
feels devastating and mostly isn&#x27;t, because names were never load-bearing - the
structure survives untouched, and a reverser doesn&#x27;t need your names, only
consistent ones they can assign themselves. It raises the cost of <em>reading</em>
without raising the cost of <em>understanding</em>, and the second is the one an
attacker is paying.</p>
<p>What survived my own reversing attempts were the transformations where undoing
them meant reasoning about what the program actually does, not just pattern
matching on what it looks like. Anything a visitor could spot and rewrite
mechanically, a visitor did.</p>
<h2 id="the-part-that-generalises">the part that generalises</h2>
<p>I stopped working on the JVM a long time ago and this is still the project I
think about most, because the lesson wasn&#x27;t about bytecode at all:</p>
<p><strong>You learn almost nothing about a defence by admiring it. You learn everything
by attacking it.</strong></p>
<p>Anyone can build the half that looks strong. The half that tells you the truth
is the one that tries to break it, and it&#x27;s the half people skip, because it&#x27;s
less fun and it mostly produces bad news about work you already did.</p>
<p>That instinct is the thing that carried forward. It&#x27;s why I care about the race
in an <a href="https://naft.pink/projects/art-gallery">auction system</a> where two bids land in the same
instant, and about the <a href="https://naft.pink/projects/dog-grooming">booking slot</a> two people click
at once - not because those failures showed up in testing, but because the
interesting case is the adversarial one and you only find it by going looking on
purpose.</p>
<p>Nobody reports the double-booking bug. They just don&#x27;t come back.</p>
<hr/>
<p>The concrete version of all this - what happened when the deobfuscator was
pointed at <a href="https://naft.pink/blog/string-encryption-ships-the-key">ProGuard and Allatori</a>, and
why encrypted strings hand you the key along with the lock - is a separate note.</p>]]></content:encoded>
      <category>java</category>
      <category>jvm</category>
      <category>bytecode</category>
    </item>
    <item>
      <title>string encryption ships you the key</title>
      <link>https://naft.pink/blog/string-encryption-ships-the-key</link>
      <guid isPermaLink="true">https://naft.pink/blog/string-encryption-ships-the-key</guid>
      <pubDate>Sun, 26 Jul 2026 00:00:00 GMT</pubDate>
      <description>My Java deobfuscator targeted ProGuard and Allatori jars. This is old enough - 2021/2022 - that I&apos;m writing it from memory, not from the code, and I&apos;ve had to correct details I got backwards the first time I wrote this up.</description>
      <content:encoded><![CDATA[

<p>The <a href="https://naft.pink/projects/java-obfuscator">deobfuscator half</a> of that project had two real
targets: ProGuard and Allatori. Upfront caveat, because it matters more than
usual here: this is a 2021/2022 project, I&#x27;m writing from memory rather than
from the code, and an earlier draft of this post had the two swapped in a way
that didn&#x27;t match what actually happened. What follows is the corrected
version, and I&#x27;d still hold it loosely.</p>
<h2 id="proguard-was-the-more-annoying-one">proguard was the more annoying one</h2>
<p>As best I remember it, ProGuard-obfuscated jars were the side that actually
made me do work. I ran into something like five different methods for deriving
or decrypting a key, and which one a given jar used didn&#x27;t seem consistent from
jar to jar - possibly tied to a version or a build setting, but I never
actually isolated which.</p>
<p>That&#x27;s the honest shape of that half: not one algorithm, a catalogue. You
recognise a pattern, write the reversal, and go looking for the next variant.
I don&#x27;t have a clean account of why there were around five rather than three or
eight - that&#x27;s just how many distinct methods I ran into before I stopped
looking.</p>
<h2 id="allatoris-obstacle-wasnt-the-encryption">allatori&#x27;s obstacle wasn&#x27;t the encryption</h2>
<p>Allatori&#x27;s actual hurdle was different in kind, and simpler than I originally
described it: a deliberately malformed class, built to crash decompilers like
Luyten rather than to resist a targeted tool. It&#x27;s a cheap trick - it does
nothing against something that isn&#x27;t a general-purpose decompiler - but it&#x27;s
effective against exactly the tool most people would reach for first.</p>
<p>Once that class was out of the way, decrypting the strings underneath it wasn&#x27;t
the hard part. Which is worth sitting with, because it&#x27;s the general shape of
this problem regardless of which tool you&#x27;re pointed at:</p>
<blockquote>
<p><strong>You cannot ship someone a program that runs on their machine and also
withhold from them what the program needs in order to run.</strong></p>
</blockquote>
<p>A program that has to decrypt a string to use it has to carry the routine, the
key, and the call site into that decryption, all in the same artefact it hands
you. The string isn&#x27;t protected. It&#x27;s <em>inconvenienced</em> - one indirection away
from plaintext, with the indirection included in the download. That&#x27;s true of
string encryption generally, independent of which specific tool I was pointed
at, and it&#x27;s the part of this whole project I&#x27;m actually confident about.</p>
<h2 id="what-im-not-confident-about-anymore">what I&#x27;m not confident about anymore</h2>
<p>I&#x27;d previously ranked name mangling, string encryption, and control-flow
obfuscation by how much they resisted the deobfuscator, with control flow as
&quot;the one that actually cost me.&quot; I don&#x27;t have a solid memory of testing control
flow obfuscation specifically as part of this project, so I&#x27;m dropping that
ranking rather than keep repeating a claim I can&#x27;t back up.</p>
<p>What I&#x27;ll stand behind: name mangling destroys information rather than hiding
it, so there&#x27;s nothing to reverse, only to relabel. String encryption is
reversible in principle for the reason above. Past that, I&#x27;d rather say &quot;I
don&#x27;t remember clearly&quot; than restate specifics that didn&#x27;t hold up the first
time I wrote them down.</p>
<h2 id="the-part-i-still-use">the part I still use</h2>
<p>I wrote about <a href="https://naft.pink/blog/obfuscator-and-deobfuscator">why building both halves was the point</a>
separately, and the lesson from that post is the one part of this project I&#x27;d
still defend without hedging: you cannot tell which of your own transformations
are strong by admiring the output. You find out by writing the thing that comes
at it from the other side - and, apparently, you should also double-check your
own memory before writing it up years later.</p>]]></content:encoded>
      <category>java</category>
      <category>jvm</category>
      <category>bytecode</category>
    </item>
  </channel>
</rss>
