-
Notifications
You must be signed in to change notification settings - Fork 304
add shToElf handler #567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add shToElf handler #567
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
| 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 not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its super gross