Created
August 22, 2018 01:33
-
-
Save nornagon/3461c811005b34c4436f66ad731756a9 to your computer and use it in GitHub Desktop.
What good is a programmer if they can't destroy software?
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
#!/bin/sh | |
tail +3 "$0" | clang -x c -o "$0" - && exec "$0" | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include <sys/mman.h> | |
#include <mach-o/dyld.h> | |
const char what_good_is_a_writer[] = | |
"What good is a writer if he can’t destroy literature?\n" | |
"And us... what good are we if we don’t help as much\n" | |
"as we can in that destruction?\n"; | |
int main() { | |
write(0, what_good_is_a_writer, sizeof(what_good_is_a_writer)); | |
uint32_t path_length = 0; | |
_NSGetExecutablePath(NULL, &path_length); | |
char* path = mmap(0, path_length, PROT_WRITE|PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); | |
_NSGetExecutablePath(path, &path_length); | |
int fd = open(path, O_RDWR); | |
off_t size = lseek(fd, 0, SEEK_END); | |
uint8_t* self = mmap(0, size, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0); | |
int r = ((int)&main) >> 12; | |
off_t byte_to_flip = r % size; | |
off_t bit_to_flip = (r >> 8) % 8; | |
self[byte_to_flip] ^= 1 << bit_to_flip; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment