<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>The Blog of Words</title>
    <description>Welcome to The Blog of Words -- The wordiest words that ever worded. A blog about tech, and occasionally other things.
</description>
    <link>https://archenoth.com/</link>
    <atom:link href="https://archenoth.com/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Wed, 11 Mar 2026 21:25:46 +0000</pubDate>
    <lastBuildDate>Wed, 11 Mar 2026 21:25:46 +0000</lastBuildDate>
    <generator>Jekyll v3.10.0</generator>
    
      <item>
        <title>Hacking ADOM to work on Android</title>
        <description>&lt;p&gt;&lt;img src=&quot;/img/Oshavictory.png&quot; alt=&quot;Oshawott standing on tippy toes, showing a phone with a victory hand gesture emoji on it&quot; title=&quot;Success!&quot; class=&quot;imageRight short&quot; /&gt;&lt;/p&gt;

&lt;p&gt;When I last left off, I wrote a post about &lt;a href=&quot;/tech/2020/05/29/Roguelikes-in-Termux.html&quot;&gt;getting as many traditional roguelikes to work on Android as I could!&lt;/a&gt; And while I was successful in accomplishing this for &lt;a href=&quot;https://github.com/termux/termux-packages/pull/10916&quot;&gt;a&lt;/a&gt; &lt;a href=&quot;https://github.com/termux/termux-packages/pull/5740&quot;&gt;few&lt;/a&gt; &lt;a href=&quot;https://github.com/termux/termux-packages/pull/10923&quot;&gt;games&lt;/a&gt; I eventually packaged for Termux, there was still the weirdness that was ADOM, a closed-source game that I wrote a &lt;a href=&quot;/tech/2020/05/29/Roguelikes-in-Termux.html#enter-ptrace&quot;&gt;ptrace wrapper&lt;/a&gt; in order to play without needing to rely on emulation.&lt;/p&gt;

&lt;p&gt;However, it turns out that getting a version of ADOM working on Android without a wrapper not only is possible, it’s actually pretty easy!&lt;/p&gt;

&lt;!-- more --&gt;

&lt;h1 id=&quot;adom-and-the-problem&quot;&gt;ADOM, and the problem&lt;/h1&gt;
&lt;p&gt;&lt;a href=&quot;https://www.adom.de/home/index.html&quot;&gt;ADOM&lt;/a&gt; is an excellent traditional roguelike created by Thomas Biskup. It features a large open world you can navigate with an overworld map, a narrative with branching storylines, and lots of other things none of the other popular roguelikes of the time had. But, unlike those other roguelikes, it also wasn’t open source.&lt;/p&gt;

&lt;p&gt;Without source code, how is it possible to play ADOM on Android? Well, cool thing about Android, it actually &lt;em&gt;does&lt;/em&gt; run ELF binaries when asked nicely. This usually just fails because the binaries you use on other Linuxes have two complications:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;They are for the wrong architecture&lt;/li&gt;
  &lt;li&gt;They want to use shared libraries that &lt;em&gt;do not&lt;/em&gt; exist on Android, and that you can’t easily install&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If those two complications aren’t a problem, there’s nothing really stopping you from running programs compiled without Android in mind, right?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Well, almost&lt;/em&gt;, but we’ll get to that.&lt;/p&gt;

&lt;p&gt;What this does mean is that, &lt;em&gt;most&lt;/em&gt; statically linked ARM binaries built for Linux will just kinda…work on Android! For example, because of how it works, most Linux-ARM targeting builds of Go programs can run on most phones without modification, or any special environments.&lt;/p&gt;

&lt;p&gt;Here’s the &lt;a href=&quot;https://github.com/RUB-NDS/Terrapin-Scanner/releases/tag/v1.1.0&quot;&gt;Terrapin Vulnerability Scanner&lt;/a&gt;’s aarch64 build running without modification on my phone’s shell!&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://chitter.xyz/@archenoth/111699043164231729&quot;&gt;&lt;img src=&quot;https://media.chitter.xyz/media_attachments/files/111/699/012/936/463/830/original/5275b34208e10978.png&quot; alt=&quot;A screenshot of a Termux terminal on Android showing the Linux Terrapin scanner is a 64-bit ELF built in Go, and then showing that it runs without errors&quot; title=&quot;It runs!&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With that in mind, there is &lt;a href=&quot;https://www.adom.de/home/downloads.html&quot;&gt;a statically-linked Raspbian version of ADOM&lt;/a&gt; freely available on ADOM’s website; Raspberry Pi’s architecture being &lt;em&gt;ARM&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This apparently ran without modification on Android once upon a time, but &lt;a href=&quot;/tech/2020/05/29/Roguelikes-in-Termux.html#seccomp&quot;&gt;Android introduced seccomp filters&lt;/a&gt; to prevent programs from using syscalls that it doesn’t support. Syscalls ADOM &lt;em&gt;wants&lt;/em&gt; to use.&lt;/p&gt;

&lt;p&gt;And here lies the problem!&lt;/p&gt;

&lt;p&gt;If ADOM calls these syscalls, it dies, but there’s no source to remove them. My previous answer to this problem was to ptrace out the syscall, but it turns out, we can do better!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Much&lt;/em&gt; better.&lt;/p&gt;

&lt;h1 id=&quot;radare2--strace&quot;&gt;radare2 + strace&lt;/h1&gt;
&lt;p&gt;We can just hack out the system call from the binary itself! And &lt;a href=&quot;https://rada.re/n/&quot;&gt;radare2&lt;/a&gt;, an open source reverse engineering toolkit, that’s already installable in Termux means we can do it all on-device! We just need to figure out where the system call is in the binary so we can replicate the strategy of changing seccomp’d syscalls to syscall 0 &lt;a href=&quot;/tech/2020/05/29/Roguelikes-in-Termux.html#enter-ptrace&quot;&gt;from the previous post&lt;/a&gt;–except this time, not at runtime!&lt;/p&gt;

&lt;p&gt;We don’t even need to statically analyze it to find out where we need to hack out the call, we get this information for free! We just need the kernel to seccomp kill ADOM while watching it through &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strace&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;So, to do this, we can just install both things:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;radare2 strace
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And then run ADOM through &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strace&lt;/code&gt;!&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;wget https://www.adom.de/home/download/current/adom_linux_arm_3.3.3.tar.gz
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;tar &lt;/span&gt;xvaf adom_linux_arm_3.3.3.tar.gz
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;adom
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;strace ./adom
execve&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;./adom&quot;&lt;/span&gt;, &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;./adom&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;, 0xb400007b385d9c50 /&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; 42 vars &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;/ &amp;lt;unfinished ...&amp;gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt; Process &lt;span class=&quot;nv&quot;&gt;PID&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;25778 runs &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;32 bit mode. &lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
&amp;lt;... execve resumed&amp;gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                   &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0
&lt;span class=&quot;nb&quot;&gt;uname&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;sysname&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Linux&quot;&lt;/span&gt;, &lt;span class=&quot;nv&quot;&gt;nodename&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;localhost&quot;&lt;/span&gt;, ...&lt;span class=&quot;o&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0
brk&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;NULL&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                               &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x265a000
brk&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x265ad08&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                          &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x265ad08
set_tls&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x265a4c0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                      &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0
brk&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x267bd08&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                          &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x267bd08
brk&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;0x267c000&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                          &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0x267c000
getuid32&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;                              &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 10388
setuid32&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;10388&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                         &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 10388
&lt;span class=&quot;nt&quot;&gt;---&lt;/span&gt; SIGSYS &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;si_signo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;SIGSYS, &lt;span class=&quot;nv&quot;&gt;si_code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;SYS_SECCOMP, &lt;span class=&quot;nv&quot;&gt;si_call_addr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0x1caf70, &lt;span class=&quot;nv&quot;&gt;si_syscall&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;__NR_setuid32, &lt;span class=&quot;nv&quot;&gt;si_arch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;AUDIT_ARCH_ARM&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;---&lt;/span&gt;
+++ killed by SIGSYS +++
Bad system call
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now, look closely at this output. Do you see what I see?&lt;/p&gt;

&lt;p&gt;Specifically in this line:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;--- SIGSYS {si_signo=SIGSYS, si_code=SYS_SECCOMP, si_call_addr=0x1caf70, si_syscall=__NR_setuid32, si_arch=AUDIT_ARCH_ARM} ---
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Staring at us right in the face is the address of where the binary died to seccomp! &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;si_call_addr=0x1caf70&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, armed with this information, if we can simply jump to that location in the binary in radare2, and replace the syscall number with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;, we should be golden!&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;r2 &lt;span class=&quot;nt&quot;&gt;-w&lt;/span&gt; ./adom
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-w&lt;/code&gt; means we are allowing ourselves to write to the binary, because by default, radare2 doesn’t want you to accidentally mangle something you simply want to inspect. But after we are in, we can see r2’s extremely minimalist interface.&lt;/p&gt;

&lt;p&gt;Since we know exactly where we want to go (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x1caf70&lt;/code&gt; minus 8 because of the instruction size), we can just &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s&lt;/code&gt;eek to it with&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[0x0000a520]&amp;gt; s 0x1caf70-8
[0x001caf68]&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As you may have noticed, you can just do math inline the r2 CLI, which is nice! But now that we’re here, we may as well switch to something a little more visual!&lt;/p&gt;

&lt;p&gt;There are two other commands we are going to use to navigate r2:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt; command switches us to a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;isual mode, and…&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt; switches the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt;rint mode, which are basically just different visual mode styles (I like to think of them as pages)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We are interested in the disassembly, which is the second default print mode, so in order to do both at the same time we can just run&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[0x001caf68]&amp;gt; Vp
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will switch to Visual mode and immediately switch to the fullscreen disassembly view, and at the top of the screen, you should be able to see:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[0x001caf68 [xAdvc]0 0% 120 ./adom]&amp;gt; pd $r @ entry0+1837640 # 0x1caf68
       ╎╎   0x001caf68      d570a0e3       mov r7, 0xd5
       ╎╎   0x001caf6c      000000ef       svc 0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is perfect! We are exactly positioned where the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r7&lt;/code&gt; register is getting the syscall number.&lt;/p&gt;

&lt;p&gt;We don’t want &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r7&lt;/code&gt; to be &lt;a href=&quot;https://github.com/torvalds/linux/blob/v4.17/arch/arm/tools/syscall.tbl#L230&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xd5&lt;/code&gt;&lt;/a&gt;, since &lt;a href=&quot;https://github.com/aosp-mirror/platform_bionic/blob/2b499046f10487802bfbaaf4429160595d08b22c/libc/SECCOMP_BLACKLIST_APP.TXT#L16&quot;&gt;that’s a filtered syscall&lt;/a&gt; and will die when we &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;svc 0&lt;/code&gt; the next instruction. So we want to zero that out by hitting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i&lt;/code&gt; to insert hex at our location, and entering &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;00&lt;/code&gt; (two of them because 2 hex values is 1 byte).&lt;/p&gt;

&lt;p&gt;It should look like this after you are done:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;       ╎╎   0x001caf68      0070a0e3       mov r7, 0
       ╎╎   0x001caf6c      000000ef       svc 0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If that looks right, we will have just hacked out that syscall! All that remains is to hit ESC to leave Visual mode, and quit with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;If you’ve done this, all you need to do to have working ADOM is to just pass in a little terminal environment, and you can play without any wrappers!&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ TERMINFO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/data/data/com.termux/files/usr/share/terminfo &lt;span class=&quot;nv&quot;&gt;TERM&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;xterm ./adom
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is what it all looks like in one go!&lt;/p&gt;

&lt;div class=&quot;asciinema&quot;&gt;
  &lt;script async=&quot;&quot; id=&quot;asciicast-smgPNPEBZU5qYXbzp40ufL7xS&quot; src=&quot;https://asciinema.org/a/smgPNPEBZU5qYXbzp40ufL7xS.js&quot;&gt;&lt;/script&gt;
&lt;/div&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;With a tiny amount of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strace&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r2&lt;/code&gt;, any statically linked ARM ELF that dies because of seccomp on Android can be fixed!&lt;/p&gt;

&lt;p&gt;This opens up a gigantic amount of possibilities for running things on Android that you might not have expected could otherwise–but, of course, this also means that, with the power of minor acts of hackery, we are also able to play a fun open world roguelike on the go~&lt;/p&gt;

&lt;p&gt;(Speaking of, if you like this game, I feel like the dev would really appreciate your support &lt;a href=&quot;https://store.steampowered.com/app/333300/ADOM_Ancient_Domains_Of_Mystery/&quot;&gt;on Steam&lt;/a&gt;!)&lt;/p&gt;

&lt;p&gt;Happy hacking!&lt;/p&gt;
</description>
        <pubDate>Tue, 23 Jan 2024 00:00:00 +0000</pubDate>
        <link>https://archenoth.com/tech/2024/01/23/Hacking-ADOM-for-Android.html</link>
        <guid isPermaLink="true">https://archenoth.com/tech/2024/01/23/Hacking-ADOM-for-Android.html</guid>
        
        
        <category>tech</category>
        
      </item>
    
      <item>
        <title>Roguelikes in Termux</title>
        <description>&lt;p&gt;The last few months of my time has been on-and-off spent trying to get a number of traditional roguelikes to run on my phone. To me, the idea of being able to possibly play something from a genre of largely-free games that required next-to no battery power and sported a basically limitless amount of replayability on my phone was something of a minor dream! But if you’re anything like me, the idea of trying to control these things with touchscreen controls seemed like it would be &lt;em&gt;painful at best&lt;/em&gt;. But the “potential cool” of being able to have portable &lt;a href=&quot;https://www.adom.de/home/index.html&quot;&gt;ADOM&lt;/a&gt; outweighed the negatives. So it was at least worth a try!&lt;/p&gt;

&lt;p&gt;In this post, I’ll talk a little about my adventures in getting these little RPGs working on an unrooted Android phone!&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/img/OshaAndroidRL.png&quot; alt=&quot;Oshawott playin with a phone&quot; title=&quot;Explorin&apos; the halls of Angband!&quot; /&gt;&lt;/p&gt;
&lt;style&gt;
  img[alt=&quot;Oshawott playin with a phone&quot;]{
    max-width: 60%;
    margin: 50px;
    margin-left: auto;
    margin-right: auto;
  }

  @media only screen and (max-width: 1400px) and (min-width: 600px) {
    img[alt=&quot;Oshawott playin with a phone&quot;]{
      max-width: 80%;
    }
  }
&lt;/style&gt;

&lt;!-- more --&gt;

&lt;p&gt;Now, I’ve seen people play games in emulators on phones before, and the touchscreen semi-transparent control schemes those all used seemed, well…like something I probably couldn’t tolerate for long enough to enjoy any game beyond the initial novelty. Roguelikes also have a habit of using a substantial number of keys on a full keyboard, so layering those over the screen seemed like it would be a major nonstarter. The alternative would be using a portion of the already-tiny screen for all the keys, making the actual game microscopic!&lt;/p&gt;

&lt;p&gt;But on the other hand, &lt;em&gt;could you imagine being able to play ADOM on the go?&lt;/em&gt; It was &lt;em&gt;definitely&lt;/em&gt; still worth trying!&lt;/p&gt;

&lt;h1 id=&quot;termux&quot;&gt;Termux&lt;/h1&gt;

&lt;p&gt;&lt;a href=&quot;https://termux.com/&quot;&gt;Termux&lt;/a&gt; is a shell and Linux environment for Android, and with it, you can use the command line on your phone like you would on most other Linux distributions. You can install packages, compile code, and even run &lt;a href=&quot;https://wiki.termux.com/wiki/PRoot&quot;&gt;entire others Linux distributions in proots&lt;/a&gt; if you run into limitations of the native environment.&lt;/p&gt;

&lt;p&gt;Seriously, it rocks so hard!&lt;/p&gt;

&lt;blockquote class=&quot;twitter-tweet&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;It&amp;#39;s still wild to me that I slept on Termux for so long&lt;br /&gt;&lt;br /&gt;It&amp;#39;s a full Linux environment that works on a phone! And while it does have some limitations, the fact that I have a working version of *ffmpeg* on my phone that I installed by running &amp;quot;apt install ffmpeg&amp;quot; is just bonkers&lt;/p&gt;&amp;mdash; Arch 💕 (@Archenoth) &lt;a href=&quot;https://twitter.com/Archenoth/status/1224795563599597568?ref_src=twsrc%5Etfw&quot;&gt;February 4, 2020&lt;/a&gt;&lt;/blockquote&gt;

&lt;p&gt;Just the fact that you could &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apt install&lt;/code&gt; things that I had previously thought were impossible to run on Android sold me on using Termux basically instantly. And actually, right in the official game repo, you could install &lt;a href=&quot;https://rephial.org/&quot;&gt;Angband&lt;/a&gt;!&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;game-repo
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;apt update
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;angband
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Of course, I did that nearly immediately after discovering I could. It’s our first Roguelike, and it took basically zero effort to get running!&lt;/p&gt;

&lt;p&gt;But how well did it work?&lt;/p&gt;

&lt;h1 id=&quot;controls&quot;&gt;Controls&lt;/h1&gt;

&lt;p&gt;As expected, after starting the game it became immediately apparent that controls were going to become an issue. But I also discovered that I didn’t have nearly as much trouble seeing the game in portrait mode as I initially though I would. And even if I did, I could run Angband with lower terminal row and column counts to make things bigger without the game complaining.&lt;/p&gt;

&lt;p&gt;This meant that, in theory, I could create a custom keyboard to make things a little less annoying to control, and that would get rid of most of my immediate issues with playing the game. So, paying special attention to the keys I used the most that weren’t easily available on gboard, I wrote a small Termux keyboard layout.&lt;/p&gt;

&lt;p&gt;Here is what I eventually came up with:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/Archenoth/0ef3a2eddb1ddebbcadc46aafc500984.js&quot;&gt;&lt;/script&gt;

&lt;p&gt;That configuration makes the following appear above my normal gboard keyboard:
&lt;img src=&quot;/img/AngbandKeys.png&quot; alt=&quot;A set of arrow keys in a numpad layout, with esc, tab, and equals on the left, and single quote, star, and return on the right&quot; title=&quot;Keys that make Angband reasonable to play!&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You might be wondering, why arrow keys instead of all numbers? Turns out, unlike numbers, arrow keys can be held down to hit them multiple times in these custom key layouts! So if I was going to walk long distances in-game, that auto-repeat functionality seemed like it could be handy. (Also, it’s purty :3)&lt;/p&gt;

&lt;p&gt;Turns out, even though I made this key layout for Angband specifically, it ended up working much better than I expected, and I was able to play a lot of other roguelikes with very little modification!&lt;/p&gt;

&lt;p&gt;But, I would need to get them running first! So, what were my options here?&lt;/p&gt;

&lt;h1 id=&quot;compiling&quot;&gt;Compiling&lt;/h1&gt;

&lt;p&gt;If Angband was easy enough to get working on Termux, that means that it must be easy enough to compile, right?&lt;/p&gt;

&lt;p&gt;Since there are a lot of games based &lt;em&gt;on&lt;/em&gt; Angband, and since I already had a working Angband keymap, Angband variants seemed like the logical next step, since a lot of those are very unique and good games themselves.&lt;/p&gt;

&lt;p&gt;In fact, this was what lead me to discover &lt;a href=&quot;http://www.amirrorclear.net/flowers/game/sil/&quot;&gt;Sil&lt;/a&gt;!&lt;/p&gt;

&lt;blockquote class=&quot;twitter-tweet&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;Enemies are more threatening and have interesting AI, but you also get lots of tactical tools to offset that (Though most choices have tradeoffs)&lt;br /&gt;&lt;br /&gt;The setting is a meticulously-researched Tolkien universe, and it&amp;#39;s a significantly shorter game than Angband; the game it came from&lt;/p&gt;&amp;mdash; Arch 💕 (@Archenoth) &lt;a href=&quot;https://twitter.com/Archenoth/status/1224783546587332609?ref_src=twsrc%5Etfw&quot;&gt;February 4, 2020&lt;/a&gt;&lt;/blockquote&gt;

&lt;p&gt;However, it’s also what lead me to discover my first hurdle with compiling games to play on my phone: The C preprocessor!&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Note: I’m using sil-q for the code blocks here because it requires the same process as normal Sil, but it can be fetched with git&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;build-essential ncurses git
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git clone https://github.com/sil-quirk/sil-q.git
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;sil-q
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;nano src/Makefile.std &lt;span class=&quot;c&quot;&gt;# To turn off things like X11, and enable only gcu&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;make &lt;span class=&quot;nt&quot;&gt;-C&lt;/span&gt; src &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; Makefile.std &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Errors with ./h-system.h:40:12: fatal error: &apos;sys/timeb.h&apos; file not found&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;c-macros&quot;&gt;C Macros&lt;/h2&gt;
&lt;p&gt;Turns out that a some C files act on the absence of certain flags that are set in specific and predictable environments.&lt;/p&gt;

&lt;p&gt;In this case, since Termux on Android wasn’t exactly a “predictable environment”, the lack of any flags that were in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#if !defined(THING)&lt;/code&gt; chains would sometimes mean that we got some pretty wild pieces of code that would become active, and I would need to hunt for the resulting places that these were included and remove them.&lt;/p&gt;

&lt;p&gt;If you just follow the compiler errors, this process is a lot easier than it sounds.&lt;/p&gt;

&lt;p&gt;It was especially easy for Sil, I just needed to delete the line that tried to include &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;timeb.h&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;nano src/h-system.h &lt;span class=&quot;c&quot;&gt;# I literally just deleted lines 39-41&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;make &lt;span class=&quot;nt&quot;&gt;-C&lt;/span&gt; src &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; Makefile.std &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The diff was basically&lt;/p&gt;
&lt;div class=&quot;language-diff highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;gd&quot;&gt;-# if !defined(SGI) &amp;amp;&amp;amp; !defined(ULTRIX)
-#  include &amp;lt;sys/timeb.h&amp;gt;
-# endif
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This worked! I had a sil binary! So, aside from this little bump in the road, the compilation was basically just a standard Linux build. (Which again, I need to stress is absolutely bonkers to me since this is &lt;em&gt;on an unrooted phone&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;However, trying to reap the rewards of my compilation, I ran into my second hurdle: &lt;a href=&quot;https://en.wikipedia.org/wiki/Seccomp&quot;&gt;seccomp&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;seccomp&quot;&gt;seccomp&lt;/h2&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;./sil
Bad system call
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;On older Android versions, this program would have run just fine, but since Android Oreo, &lt;a href=&quot;https://android-developers.googleblog.com/2017/07/seccomp-filter-in-android-o.html&quot;&gt;a seccomp filter was introduced into Android&lt;/a&gt; that would guarantee that certain system calls would never be called. A lot of these system calls were things regarding user and group ID setting, which is something that Android doesn’t really support with it’s single-user environment. (&lt;a href=&quot;https://github.com/aosp-mirror/platform_bionic/blob/2b499046f10487802bfbaaf4429160595d08b22c/libc/SECCOMP_BLACKLIST_APP.TXT&quot;&gt;Here’s&lt;/a&gt; a full list of the functions that would be filtered)&lt;/p&gt;

&lt;p&gt;The catch with seccomp is that anything that calls a seccomp-filtered function won’t just skip over it–the program that called it will actually be &lt;em&gt;killed by the kernel&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This of course, isn’t a great thing if you are trying to play a game.&lt;/p&gt;

&lt;p&gt;How did I discover this? It turned out that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strace&lt;/code&gt; (An extremely handy Linux tool that shows you all of the system calls a program makes, along with their results) can be installed from the official repos in Termux! So I just ran Sil through that:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;strace
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;strace ./sil
&amp;lt;output snipped&amp;gt;
rt_tgsigqueueinfo&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;19612, 19612, SIGSYS, &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;si_signo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;SIGSYS, &lt;span class=&quot;nv&quot;&gt;si_code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;SYS_SECCOMP, &lt;span class=&quot;nv&quot;&gt;si_call_addr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0x7e91ce0038, &lt;span class=&quot;nv&quot;&gt;si_syscall&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;__NR_setregid, &lt;span class=&quot;nv&quot;&gt;si_arch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;AUDIT_ARCH_AARCH64&lt;span class=&quot;o&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 0
rt_sigreturn&lt;span class=&quot;o&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;mask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=[&lt;/span&gt;RTMIN]&lt;span class=&quot;o&quot;&gt;})&lt;/span&gt;            &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 10202
&lt;span class=&quot;nt&quot;&gt;---&lt;/span&gt; SIGSYS &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;si_signo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;SIGSYS, &lt;span class=&quot;nv&quot;&gt;si_code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;SYS_SECCOMP, &lt;span class=&quot;nv&quot;&gt;si_call_addr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0x7e91ce0038, &lt;span class=&quot;nv&quot;&gt;si_syscall&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;__NR_io_setup, &lt;span class=&quot;nv&quot;&gt;si_arch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0 /&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; AUDIT_ARCH_??? &lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;/&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;---&lt;/span&gt;
+++ killed by SIGSYS +++
Bad system call
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Googling “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SYS_SECCOMP&lt;/code&gt;” gave me enough hints to eventually discover the above post about the introduction of a seccomp filter in Android, and also the list of functions that were filtered.&lt;/p&gt;

&lt;p&gt;Armed with this new knowledge, I saw that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__NR_setregid&lt;/code&gt; was the function that tripped the filter and killed the game. So I just grepped for it, and deleted those parts of the code (Since again, they were just dead calls on Android’s single-user environment anyway), and recompiled.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git &lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;setregid&apos;&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# It&apos;s interesting how the matched .o below has `x86_64` in it&apos;s name, since this was very much running on `aarch64`&lt;/span&gt;
src/files.c:    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;setregid&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;getegid&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;, getgid&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; 0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
src/files.c:            quit&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;setregid(): cannot set permissions correctly!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
src/files.c:    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;setregid&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;getegid&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;, getgid&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; 0&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
src/files.c:            quit&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;setregid(): cannot set permissions correctly!&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
Binary file src/sil.o.x86_64 matches
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;nano src/files.c &lt;span class=&quot;c&quot;&gt;# To delete the things that matched above&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;make &lt;span class=&quot;nt&quot;&gt;-C&lt;/span&gt; src &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; Makefile.std &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;./sil
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It worked!&lt;/p&gt;

&lt;blockquote class=&quot;twitter-tweet&quot; data-conversation=&quot;none&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;I mostly stumbled across it because I&amp;#39;ve been looking for games I can compile and run on my phone, and I&amp;#39;m happy to say that Sil works excellently on there!&lt;br /&gt;&lt;br /&gt;Actually, roguelikes in general seem to have a knack for working much better than they should with a touchscreen in Termux &lt;a href=&quot;https://t.co/ASGjFlk7qC&quot;&gt;pic.twitter.com/ASGjFlk7qC&lt;/a&gt;&lt;/p&gt;&amp;mdash; Arch 💕 (@Archenoth) &lt;a href=&quot;https://twitter.com/Archenoth/status/1224790926339362816?ref_src=twsrc%5Etfw&quot;&gt;February 4, 2020&lt;/a&gt;&lt;/blockquote&gt;

&lt;p&gt;And with that hurdle out of the way, it turned out that I could get all kinds of Angband variants working with this knowledge. That included things like &lt;a href=&quot;https://github.com/sulkasormi/frogcomposband&quot;&gt;FrogComposband&lt;/a&gt;, which is a large Angband variant with quests, an overworld, and generally a very significant amount of content.&lt;/p&gt;

&lt;p&gt;Cool! With bands out of the way, what else can we get working?&lt;/p&gt;

&lt;h2 id=&quot;android-specific-code&quot;&gt;Android-specific code&lt;/h2&gt;

&lt;p&gt;My next target was &lt;a href=&quot;https://crawl.develz.org/&quot;&gt;Dungeon Crawl: Stone Soup&lt;/a&gt;, which had a very interesting problem: &lt;a href=&quot;https://play.google.com/store/apps/details?id=com.newtzgames.dcssonline&quot;&gt;&lt;em&gt;It actually has an Android version&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That might seem like a good thing, but that meant there was a lot of Android-specific code that would try to do its thing when I tried to compile it. This turned out to be a headache because Crawl made some assumptions about what an Android version of Crawl &lt;em&gt;should&lt;/em&gt; look like. (Perfectly reasonable assumptions, but I’m not coming at this from a perfectly reasonable angle)&lt;/p&gt;

&lt;p&gt;The most apparent one was that doing an Android build would try to generate an APK, which my phone was not at all equipped to do. It also tried to build a tiles version by default, which of course wouldn’t fly with the purely-textual terminal I was sporting.&lt;/p&gt;

&lt;p&gt;I ended up going through &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;syscalls.cc&lt;/code&gt; and inverting all the checks for Android, then tried compiling it as if it were a normal POSIX application so I could sidestep most of the Android-specific code.&lt;/p&gt;

&lt;p&gt;This &lt;em&gt;almost&lt;/em&gt; worked:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git clone https://github.com/crawl/crawl.git
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;crawl/crawl-ref/source
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;nano syscalls.cc &lt;span class=&quot;c&quot;&gt;# I replaced all &quot;#ifdef __ANDROID__&quot; with &quot;#ifndef __ANDROID__&quot;&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git submodule update &lt;span class=&quot;nt&quot;&gt;--init&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;make &lt;span class=&quot;c&quot;&gt;# &quot;undefined reference to `__android_log_print&apos;&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The reason for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;syscalls.cc&lt;/code&gt; modification was that, even if I acted like I was just a normal non-Android POSIX box, there was still some residual detection in the build that would mean that Android-specific stuff would attempt to build–and most of these cases existed in that one file.&lt;/p&gt;

&lt;p&gt;Luckily, the remaining Android conditional stuff was only complaining about the lack of logging functions to link to, which unlike some other Android libraries, &lt;em&gt;could be linked without issue on-device&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So, I just added it to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LIBS&lt;/code&gt; environment variable. (Not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LDFLAGS&lt;/code&gt; because the Makefile seemed to mess with that a whole bunch before it got to the linking bit)&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ LIBS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;-llog&quot;&lt;/span&gt; make
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;./crawl
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Luckily enough, this worked! Non-Android DCSS worked in Termux!&lt;/p&gt;

&lt;h1 id=&quot;proot&quot;&gt;Proot&lt;/h1&gt;
&lt;p&gt;So, I hinted at the beginning of this post that I wanted to get ADOM running natively on my phone. But how could I go about doing that? It’s a closed-source game, so the usual tricks wouldn’t work here.&lt;/p&gt;

&lt;p&gt;Well, during my travels, I read about being able to run the Raspberry Pi version of ADOM on Android. This was because the Raspberry Pi version was a 32-bit statically linked ARM ELF, which turns out, was able to run without modification on ARM Android devices!&lt;/p&gt;

&lt;p&gt;The keyword here is “was”.&lt;/p&gt;

&lt;p&gt;32-bit statically linked ARM ELF binaries &lt;em&gt;can&lt;/em&gt; still run on my &lt;a href=&quot;https://en.wikipedia.org/wiki/Aarch64&quot;&gt;aarch64&lt;/a&gt; (64-bit ARM) Android phone, but we have an old friend to contend with first: seccomp.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;wget https://www.adom.de/home/download/current/adom_linux_arm_3.3.3.tar.gz
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;tar &lt;/span&gt;xvaf adom_linux_arm_3.3.3.tar.gz
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;adom
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;./adom
Bad system call
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;stracing the game like we did with Sil reveals that the syscall that’s killing the game for us is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;setuid32&lt;/code&gt;, however, this time we couldn’t just change the source to get around it. So, what’s an Android-based ADOM-player to do?&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;strace ./adom
setuid32&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;10202&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                         &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 10202
&lt;span class=&quot;nt&quot;&gt;---&lt;/span&gt; SIGSYS &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;si_signo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;SIGSYS, &lt;span class=&quot;nv&quot;&gt;si_code&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;SYS_SECCOMP, &lt;span class=&quot;nv&quot;&gt;si_call_addr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0x1caf70, &lt;span class=&quot;nv&quot;&gt;si_syscall&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;__NR_setuid32, &lt;span class=&quot;nv&quot;&gt;si_arch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;AUDIT_ARCH_ARM&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;---&lt;/span&gt;
+++ killed by SIGSYS +++
Bad system call
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Usually, if I wanted to change a call in a binary I couldn’t modify, I would try to &lt;a href=&quot;https://jvns.ca/blog/2014/11/27/ld-preload-is-super-fun-and-easy/&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LD_PRELOAD&lt;/code&gt; a custom library that would intercept the bad call&lt;/a&gt;, but alas, this is a &lt;em&gt;statically linked&lt;/em&gt; binary, so trying to change something that would be loaded dynamically would have no affect here.&lt;/p&gt;

&lt;p&gt;Enter proot and it’s magic.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;proot
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;proot ./adom
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This…actually kinda worked! What the heck??&lt;/p&gt;

&lt;p&gt;At the very least, it didn’t instantly get killed by the kernel. However, looking at strace output again, it also seemed that it didn’t know what kind of terminal I was using, or have any environment, which seemed to be messing things up.&lt;/p&gt;

&lt;p&gt;So I passed in the environment that I thought it wanted:&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ TERMINFO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/data/data/com.termux/files/usr/share/terminfo &lt;span class=&quot;nv&quot;&gt;TERM&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;xterm proot ./adom
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And boom! Playable ADOM on Android!&lt;/p&gt;

&lt;blockquote class=&quot;twitter-tweet&quot; data-conversation=&quot;none&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;ADOM was probably the most tricky to figure out since there&amp;#39;s no source code, and the only ARM version is a statically linked binary that immediately uses a syscall that SECCOMP kills the process for&lt;br /&gt;&lt;br /&gt;But it turns out you can just proot it and pass along some curses environment?? &lt;a href=&quot;https://t.co/8ODBwJ0608&quot;&gt;pic.twitter.com/8ODBwJ0608&lt;/a&gt;&lt;/p&gt;&amp;mdash; Arch 💕 (@Archenoth) &lt;a href=&quot;https://twitter.com/Archenoth/status/1245796847790112768?ref_src=twsrc%5Etfw&quot;&gt;April 2, 2020&lt;/a&gt;&lt;/blockquote&gt;

&lt;p&gt;However, there was a catch. Savefiles didn’t seem to work, and for some reason, using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qemu-arm&lt;/code&gt; with proot seemed to fix it, but in a pretty resource-intensive way.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;qemu-user-arm
&lt;span class=&quot;nv&quot;&gt;$ TERMINFO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/data/data/com.termux/files/usr/share/terminfo &lt;span class=&quot;nv&quot;&gt;TERM&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;xterm proot &lt;span class=&quot;nt&quot;&gt;-q&lt;/span&gt; qemu-arm ./adom
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So, while this worked, it was both heavy, and also no longer technically being run “natively”. At that point, I was mostly just messing around with proot options, seeing if I could coerce it into working somehow without qemu, but with little luck.&lt;/p&gt;

&lt;p&gt;But…if proot allowed us to get past the initial seccomp hurdle, that meant that we could likely make something that would work too! Since no code is &lt;em&gt;actually&lt;/em&gt; magic, that meant we could &lt;em&gt;definitely&lt;/em&gt; do better than we had if we can learn proot’s secrets. So, what can we learn from proot? How is it letting things work?&lt;/p&gt;

&lt;h2 id=&quot;enter-ptrace&quot;&gt;Enter ptrace!&lt;/h2&gt;

&lt;p&gt;Diving a little into how proot works, I discovered that the way it allows for intercepting such low-level execution is because of a nifty little library called “&lt;a href=&quot;https://en.wikipedia.org/wiki/Ptrace&quot;&gt;ptrace&lt;/a&gt;”. I’d heard of ptrace before since that’s what debuggers use–and it’s also what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;strace&lt;/code&gt; uses to peek into what a program is doing as it runs.&lt;/p&gt;

&lt;p&gt;From my understanding, proot is a version of chroot that is capable of running without root because it intercepts all the privileged calls with ptrace, and instead uses its own implementations. This allows for the program it calls to “think” it’s doing privileged things, when in actuality, it’s just working within a special environment.&lt;/p&gt;

&lt;p&gt;Also, with ptrace, you can’t “technically” stop a syscall from happening, but you can change every part of it before it executes–including the syscall number! So that meant if we wanted to stop a system call from happening, we just needed to change the system call number to be a syscall that was allowed by seccomp. And lucky for us, there is, in fact, a syscall that fits the bill perfectly: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-1&lt;/code&gt;, the “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;invalid syscall&lt;/code&gt;” syscall. This would turn our target syscall into what is essentially a nop.&lt;/p&gt;

&lt;p&gt;That’s exactly what we’re looking for!&lt;/p&gt;

&lt;p&gt;So, all we had to do to get ADOM working was write a simple program that could ptrace ADOM and intercept our rogue syscall. There was a little snag though: since I was working on aarch64, none of the documentation I could find gave information that worked on it.&lt;/p&gt;

&lt;p&gt;Almost all tutorials mentioned using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PTRACE_GETREGS&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PTRACE_SETREGS&lt;/code&gt; ptrace calls to intercept and modify syscalls, which unfortunately didn’t exist on aarch64 yet. (Including &lt;a href=&quot;https://nullprogram.com/blog/2018/06/23/&quot;&gt;this very helpful tutorial&lt;/a&gt; that helped me in a lot of other ways)&lt;/p&gt;

&lt;p&gt;When I looked for alternatives in the list of ptrace calls I could use on this architecture, I found the similarly-named “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PTRACE_GETREGSET&lt;/code&gt;” and “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PTRACE_SETREGSET&lt;/code&gt;” calls which seemed like answer, but trying to figure out how to use that on aarch64 once again lead me to a lot of platform-specific documentation that didn’t include the one I was on.&lt;/p&gt;

&lt;p&gt;Unfortunately, this made sense, because it turns out ptrace syscall modification involved changing the actual registers of the CPU it was running on, so it made sense that it was going to be very platform-specific. I did eventually find a bug report post that vaguely hinted at using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iovec&lt;/code&gt;s, which &lt;em&gt;was a lead&lt;/em&gt;, but also lead me down a pretty lengthy rabbit hole, since:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;I had never used anything like them before.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iovec&lt;/code&gt; required an understanding of what was being put into them. (Which I very much lacked)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Luckily, I eventually found &lt;a href=&quot;https://codereview.chromium.org/1291983003/diff/1/src/common/android/include/sys/user.h#newcode56&quot;&gt;the commit where the register struct was introduced in aarch64 on Android&lt;/a&gt;, which gave me enough insight about how the registers were structured in aarch64 to successfully guess how to implement a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ptrace&lt;/code&gt; wrapper I could use for ADOM, and this was the result:
&lt;script src=&quot;https://gist.github.com/Archenoth/43cf79c858925f21684ababe19a1af33.js&quot;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;It’s not the cleanest code, and it uses a hardcoded full-path for the location of my &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;adom&lt;/code&gt; binary (Which I renamed to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;adom.bin&lt;/code&gt;) so I could call it from anywhere. But after compiling it with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gcc ./syscall_intercept.c -o adom&lt;/code&gt;, I had a light, working ADOM binary I could move anywhere that worked natively on my phone!&lt;/p&gt;

&lt;p&gt;That’s right! After a whole slew of research, I managed to get ADOM to run natively on my phone with ptrace! And if you compile the code above after pointing it at your ADOM binary, you can too!&lt;/p&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;

&lt;blockquote class=&quot;twitter-tweet&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;List of some roguelikes I&amp;#39;ve gotten to run natively on my phone:&lt;br /&gt;&lt;br /&gt;- Angband&lt;br /&gt;- Sil&lt;br /&gt;- FrogComposband&lt;br /&gt;- Crawl&lt;br /&gt;- ADOM&lt;br /&gt;&lt;br /&gt;I may have a problem&lt;/p&gt;&amp;mdash; Arch 💕 (@Archenoth) &lt;a href=&quot;https://twitter.com/Archenoth/status/1245795026757545984?ref_src=twsrc%5Etfw&quot;&gt;April 2, 2020&lt;/a&gt;&lt;/blockquote&gt;

&lt;p&gt;Honestly, I came into this with pretty low expectations. I expected to either not get very far, or to be underwhelmed by the results even if they &lt;em&gt;did&lt;/em&gt; work.&lt;/p&gt;

&lt;p&gt;So I was pleasantly surprised when it turned out that roguelikes in Termux are not only possible, but also surprisingly playable! And unlike a lot of Android games I’ve enjoyed before, they take up next to no battery, so you can play them when you’re on a long haul somewhere without an internet connection.&lt;/p&gt;

&lt;p&gt;It also turns out that the suspend save functionality of most roguelikes lends itself extremely well to being able to quickly start up and play games for a few minutes at a time, even if you have basically no time to do something more involved.&lt;/p&gt;

&lt;p&gt;I’ve ended up sinking no insubstantial number of hours playing Angband, ADOM, and more recently, ToME 2 on my phone during the little bits of downtime I’ve had in my day. I don’t need to sit down and think about playing it, it just kinda “happens” now when I have a few moments to spare and I have nothing else to do.&lt;/p&gt;

&lt;p&gt;I don’t know how many more roguelikes I’ll put on my phones, but I can say that I’m all set entertainment-wise if I ever get stranded on a desert island somewhere. And, while substantial, the effort I put into this little experiment ended up being much more worth it than I could have ever anticipated it being!&lt;/p&gt;

&lt;p&gt;Now, if you’ll excuse me, I have some Chaos Gates to deal with.&lt;/p&gt;

&lt;script async=&quot;&quot; src=&quot;https://platform.twitter.com/widgets.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;

</description>
        <pubDate>Fri, 29 May 2020 00:00:00 +0000</pubDate>
        <link>https://archenoth.com/tech/2020/05/29/Roguelikes-in-Termux.html</link>
        <guid isPermaLink="true">https://archenoth.com/tech/2020/05/29/Roguelikes-in-Termux.html</guid>
        
        
        <category>tech</category>
        
      </item>
    
      <item>
        <title>Mass Rename</title>
        <description>&lt;p&gt;Lets rename a folder of annoying Twitter &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.jpg-orig&lt;/code&gt; files to what they should be–and lets do it in less than five seconds!&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/Mass-Rename.gif&quot; alt=&quot;Renaming of an entire folder in no time at all&quot; title=&quot;Beep boop, renamed!&quot; /&gt;&lt;/p&gt;

&lt;!-- more --&gt;

&lt;h1 id=&quot;whats-this&quot;&gt;What’s this?&lt;/h1&gt;
&lt;p&gt;In the GIF at the top, you can see an entire folder get renamed
seemingly instantly. What’s happening here is the handywork of a
little Emacs tool called “Dired”, Emacs’ built-in file manager.&lt;/p&gt;

&lt;p&gt;Now, Emacs having a built-in-file manager is about as surprising as
the ocean being wet, but it has some really cool features that are a
lot less obvious. The one we are interested in is the part where Dired
allows you to edit folders as if they were text files!&lt;/p&gt;

&lt;p&gt;Because of this, all I did above was simply find and replace “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-orig&lt;/code&gt;”
with nothing, then save. Simple as that!&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/Dired-Rename.gif&quot; alt=&quot;Find and replace&quot; title=&quot;It&apos;s all text!&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;how-can-i-do-this-too&quot;&gt;How can I do this too?&lt;/h1&gt;
&lt;p&gt;In Emacs, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-x C-f&lt;/code&gt; &lt;sup id=&quot;fnref:1&quot; role=&quot;doc-noteref&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot; rel=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; lets you open files and folders… And when you
open a folder, you are placed in “Dired”; the mode you saw me use
above.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/Dired1.png&quot; alt=&quot;Navigating a folder in Dired&quot; title=&quot;Welcome to Dired!&quot; /&gt;&lt;/p&gt;

&lt;p&gt;By default, Dired is read-only to stop you from making sweeping
changes to your folders (Imagine running across this without knowing
what you’re doing!), but you can turn that off with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-x C-q&lt;/code&gt;,
allowing you to perform all kinds of magic.&lt;/p&gt;

&lt;p&gt;When you do this, it indicates so in the bottom line.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/Dired2.png&quot; alt=&quot;The bottom line shows you can edit!&quot; title=&quot;Uh oh&quot; /&gt;&lt;/p&gt;

&lt;p&gt;After that, you can edit the folder as if it were a text file, using
whatever style of editing you prefer. When you are done, you can save
your changes to the folder by simply saving the buffer as if it were a
normal text file–or by hitting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-c C-c&lt;/code&gt; like the text at the bottom
suggests.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/Dired3.png&quot; alt=&quot;Modifications!&quot; title=&quot;Macros work really nicely here too!&quot; /&gt;&lt;/p&gt;

&lt;p&gt;…and that’s it! Now you wield the power of mass-renaming!&lt;/p&gt;

&lt;div class=&quot;footnotes&quot; role=&quot;doc-endnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot; role=&quot;doc-endnote&quot;&gt;
      &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-x C-f&lt;/code&gt; is Emacs-speak for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Ctrl] + [X]&lt;/code&gt;, then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[Ctrl] + [F]&lt;/code&gt;. &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot; role=&quot;doc-backlink&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</description>
        <pubDate>Tue, 11 Jul 2017 01:21:03 +0000</pubDate>
        <link>https://archenoth.com/emacs/2017/07/11/Arsenal-Tier-Emacs-Writable-Dired.html</link>
        <guid isPermaLink="true">https://archenoth.com/emacs/2017/07/11/Arsenal-Tier-Emacs-Writable-Dired.html</guid>
        
        
        <category>emacs</category>
        
      </item>
    
      <item>
        <title>Sneaky Images</title>
        <description>&lt;p&gt;There is something wrong with this image. Can you tell me &lt;em&gt;what’s&lt;/em&gt;
wrong though?
&lt;img src=&quot;/assets/PichuFauxPass.png&quot; alt=&quot;img&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Go ahead, download it, inspect it, try to figure it out.
&lt;!-- more --&gt;&lt;/p&gt;

&lt;p&gt;Give up yet?&lt;/p&gt;

&lt;p&gt;Let me tell you what’s wrong with it. It isn’t just a PNG file, &lt;em&gt;it’s a ZIP archive too.&lt;/em&gt; Or rather, there is a ZIP file hidden inside of it, and some compression programs can understand it. No, really! Try to open it with 7-Zip or some similar program. You can really open it!&lt;/p&gt;

&lt;p&gt;About now, I imagine you are thinking &lt;em&gt;“What is this wizardry?”&lt;/em&gt; or &lt;em&gt;“How can that even work?”&lt;/em&gt;… It’s actually really quite simple. PNG is a format can ignore garbage at the end of a file, and ZIP programs can often ignore garbage at the beginning of ZIP archives. Why is this true? It’s all in the formats.&lt;/p&gt;

&lt;p&gt;PNG files contain image data, followed by an “End of image” token, a small string of characters that tells any program reading the PNG file that it is finished reading all that it should be interested in. ZIP files on the other hand, have a token at the &lt;em&gt;beginning&lt;/em&gt; of their format. So, if we were to put a ZIP file at the end of a PNG file, we can assume that any program that understands to look beyond the beginning of the file for a ZIP archive can find any hidden archive no problem inside of a PNG without affecting the image.&lt;/p&gt;

&lt;p&gt;Pretty nifty, huh?&lt;/p&gt;

&lt;p&gt;Now that I’ve described what’s happening in abstract, lets take a closer look at this Pichu Faux Pas by opening the file in Emacs…&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/Pichumacs1.png&quot; alt=&quot;img&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As you can see at the bottom, I can look at this image as text by hitting &lt;em&gt;“C-c C-c”&lt;/em&gt; (Or &lt;em&gt;Ctrl + C, Ctrl + C&lt;/em&gt; for you non-Emacs folks.)
That gives you a textual view of the binary data like you would expect:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/Pichumacs2.png&quot; alt=&quot;img&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header&quot;&gt;According to Wikipedia&lt;/a&gt;, the header of a PNG file is as follows:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Bytes&lt;/th&gt;
      &lt;th&gt;Purpose&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;89&lt;/td&gt;
      &lt;td&gt;Has the high bit set to detect transmission systems that do not support 8 bit data and to reduce the chance that a text file is mistakenly interpreted as a PNG, or vice versa.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;50 4E 47&lt;/td&gt;
      &lt;td&gt;In ASCII, the letters &lt;em&gt;PNG&lt;/em&gt;, allowing a person to identify the format easily if it is viewed in a text editor.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0D 0A&lt;/td&gt;
      &lt;td&gt;A DOS-style line ending (CRLF) to detect DOS-Unix line ending conversion of the data.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1A&lt;/td&gt;
      &lt;td&gt;A byte that stops display of the file under DOS when the command type has been used—the end-of-file character&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;0A&lt;/td&gt;
      &lt;td&gt;A Unix-style line ending (LF) to detect Unix-DOS line ending conversion.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Now you can clearly see the “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\211PNG&lt;/code&gt;” followed by a DOS-style line-ending… But this mode isn’t terribly good at looking at binary data, so switching over to hexl-mode we see the following:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/Pichumacs3.png&quot; alt=&quot;img&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now we can follow along with the format easily, I highlighted the “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;89&lt;/code&gt;” to show that the “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\211&lt;/code&gt;” that we saw earlier is the character described by the Wikipedia table. You can even follow along the file with the header description. We see the “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;89&lt;/code&gt;” to start out with, followed by the “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;50 4E 47&lt;/code&gt;”, which corresponds to the “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PNG&lt;/code&gt;” text on the right of the hexl-mode window. You can continue doing this for the entire header… But why is this important?&lt;/p&gt;

&lt;p&gt;Well, as it turns out, a little lower on the PNG Wikipedia article, it describes &lt;a href=&quot;http://en.wikipedia.org/wiki/Portable_Network_Graphics#Critical_chunks&quot;&gt;“Critical Chunks”&lt;/a&gt;, things like the palette, dimenstions, bit depth, and most importantly, &lt;em&gt;the image end&lt;/em&gt;. Below is a table of the critical chunks from Wikipedia.&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Chunk&lt;/th&gt;
      &lt;th&gt;Purpose&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IHDR&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;must be the first chunk; it contains the image’s width, height, and bit depth.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PLTE&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;contains the palette; list of colors.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IDAT&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;contains the image, which may be split among mtabletiple IDAT chunks. Such splitting increases filesize slightly, but makes it possible to generate a PNG in a streaming manner. The IDAT chunk contains the actual image data, which is the output stream of the compression algorithm.&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IEND&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;marks the image end.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;There is a string in every PNG file to tell the program to stop reading the image file, and that string is “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IEND&lt;/code&gt;”. So, if we search for this string, we should have the end of the file, right?&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/Pichumacs4.png&quot; alt=&quot;img&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Right!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One thing Wikipedia doesn’t mention is that there are actually four bytes following the “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IEND&lt;/code&gt;” before a PNG file ends. Those bytes are the “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ae42 6082&lt;/code&gt;” bytes you see right after the “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IEND&lt;/code&gt;”.&lt;/p&gt;

&lt;p&gt;But wait! Something’s amiss here. Do you see it? There is more to the file after the PNG supposedly ended. Lets first isolate that data into another file so we can work with it.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/Pichumacs5.png&quot; alt=&quot;img&quot; /&gt;&lt;/p&gt;

&lt;p&gt;That header looks familiar, but lucky for us, we don’t need to memorize arbitrary header values when we have commands like “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file&lt;/code&gt;” at our disposal. Lets see if it can tell us what kind of data we are working with…&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/Pichumacs6.png&quot; alt=&quot;img&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Turns out, this is a ZIP file… The header for a ZIP file according to PKWare (The guys who made PKZIP) &lt;a href=&quot;http://www.pkware.com/documents/casestudies/APPNOTE.TXT&quot;&gt;is “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PK&lt;/code&gt;”&lt;/a&gt; (Section 4.2.1). It all seems to check out. So lets save it as a zip file.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/Pichumacs7.png&quot; alt=&quot;img&quot; /&gt;&lt;/p&gt;

&lt;p&gt;And open it…&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/Pichumacs8.png&quot; alt=&quot;img&quot; /&gt;&lt;/p&gt;

&lt;p&gt;What’s this now?&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/Pichumacs9.png&quot; alt=&quot;img&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Looks like we have a text file! Yes, a text file with text we could &lt;em&gt;totally&lt;/em&gt; see in hexl-mode… But that’s not the point.&lt;/p&gt;

&lt;p&gt;The point is that we managed to utilize the specification for two different file formats to sneak one file into the other without damaging the original. You can mix lots of formats and get this result. Windows &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.exe&lt;/code&gt; files commonly have ZIP data inside of them. (Installers in particular utilize this one.) The result is that you have a valid executable that you can open in 7-Zip.&lt;/p&gt;

&lt;p&gt;Now that you know about this spifftacular little hack, you are likely wondering how you can do it yourself. Good news! It’s dead simple to mix different file formats if one can be placed right after the other like PNG and ZIP. You can do it on any platform too! You just need to know how to open a terminal or the Windows Command Prompt and use “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cat&lt;/code&gt;” or “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;copy&lt;/code&gt;” respectively.&lt;/p&gt;

&lt;p&gt;On Linux and Mac OSX, you can open a terminal and just put two files into the same file, one after the other with “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cat&lt;/code&gt;“… You can do this by typing:&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;image.png zipfile.zip &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; hax0rus.png
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Windows has a slightly different method:&lt;/p&gt;

&lt;div class=&quot;language-batch highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;copy&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;image&lt;/span&gt;.png &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;zipfile&lt;/span&gt;.zip &lt;span class=&quot;kd&quot;&gt;hax0rus&lt;/span&gt;.png
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Of course you’ll need to replace the .png and .zip filenames with the files you wanna mix.&lt;/p&gt;

&lt;p&gt;By the way, you aren’t limited to only putting one file after another. If you want to, you could possibly make valid files of a certain type with files in the &lt;em&gt;middle&lt;/em&gt; of another. This technique is used more commonly than a lot of people think, so go ahead… Dig around files, look at specifications, and let your imagination wander with possible combinations.&lt;/p&gt;
</description>
        <pubDate>Mon, 07 Oct 2013 06:49:11 +0000</pubDate>
        <link>https://archenoth.com/tricks/2013/10/07/Sneaky-images.html</link>
        <guid isPermaLink="true">https://archenoth.com/tricks/2013/10/07/Sneaky-images.html</guid>
        
        
        <category>tricks</category>
        
      </item>
    
  </channel>
</rss>
