Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import chessjsHandler from "./chessjs.ts";
import fenToJsonHandler from "./fenToJson.ts";
import piskelHandler from "./piskel.ts";
import xcursorHandler from "./xcursor.ts";
import shToElfHandler from "./shToElf.ts";
import cssHandler from "./css.ts";
import TypstHandler from "./typst.ts";

Expand Down Expand Up @@ -146,6 +147,7 @@ try { handlers.push(new chessjsHandler()) } catch (_) { };
try { handlers.push(new fenToJsonHandler()) } catch (_) { };
try { handlers.push(new piskelHandler()) } catch (_) { };
try { handlers.push(new xcursorHandler()) } catch (_) { };
try { handlers.push(new shToElfHandler()) } catch (_) { };
try { handlers.push(new cssHandler()) } catch (_) { };
try { handlers.push(new TypstHandler()) } catch (_) { };

Expand Down
71 changes: 71 additions & 0 deletions src/handlers/shToElf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// file: shToElf.ts

import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts";
import CommonFormats, { Category } from "src/CommonFormats.ts";

import elfUrl from "./shToElf/stub.elf?url";

function replaceUint32LE(file: Buffer, from: number, to: number) {
const fromBytes = Buffer.alloc(4);
fromBytes.writeUint32LE(from, 0);

const toBytes = Buffer.alloc(4);
toBytes.writeUint32LE(to, 0);

const index = file.indexOf(fromBytes);
toBytes.copy(file, index);
}

class shToElfHandler implements FormatHandler {

public name: string = "shToElf";
public supportedFormats: FileFormat[] = [
CommonFormats.SH.builder("sh").allowFrom().markLossless(),
{
name: "x86-64 Linux Executable and Linkable Format",
format: "elf",
extension: "elf",
mime: "application/x-elf",
from: false,
to: true,
internal: "elf",
category: Category.CODE,
}
];
public ready: boolean = false;

#binary?: Buffer;

async init () {
this.ready = true;
this.#binary = Buffer.from(await (await fetch(elfUrl)).bytes());
}

async doConvert (
inputFiles: FileData[],
inputFormat: FileFormat,
outputFormat: FileFormat
): Promise<FileData[]> {
const outputFiles: FileData[] = [];

for (const inputFile of inputFiles) {
const binary = Buffer.from(new Uint8Array(this.#binary!));
replaceUint32LE(binary, 1273991571, inputFile.bytes.length);

let file = Buffer.concat([
binary,
inputFile.bytes
]);

outputFiles.push({
name: inputFile.name.replace(/\.[^.]+$/, "") + ".elf",
bytes: file
});
}

return outputFiles;
}

}

export default shToElfHandler;
21 changes: 21 additions & 0 deletions src/handlers/shToElf/stub.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// gcc -o stub.elf stub.c

#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

// https://xkcd.com/221
int cmd_size = 1273991571;
Comment on lines +8 to +9
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there really no reason behind this number? Could this at least be a product of powers of two or something? Or really just some clean-ish number, or something chosen through some reason, rather than literally random?

I propose 1073741824 (or 1024^3), it's exactly 1 GiB and close to this value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there really no reason behind this number? Could this at least be a product of powers of two or something? Or really just some clean-ish number, or something chosen through some reason, rather than literally random?

the number is patched out by the js code with the real cmd size (important that its exact! we need to seek to the place in the binary where the shell script is located), i had to pick a number that i know wouldnt appear in the binary

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's... disgusting. Sure!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its super gross


int main() {
int fd = open("/proc/self/exe", O_RDONLY);
lseek(fd, -cmd_size, SEEK_END);

char *cmd = malloc(cmd_size + 1);
read(fd, cmd, cmd_size);
close(fd);

cmd[cmd_size] = '\0';
execl("/bin/bash", "bash", "-c", cmd, NULL);
}
Binary file added src/handlers/shToElf/stub.elf
Binary file not shown.