diff options
author | Jakob Kaivo <jkk@ung.org> | 2019-08-01 10:10:52 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2019-08-01 10:10:52 -0400 |
commit | b73bf969e6538e03cc0d9761a62f3ffbd494eb6c (patch) | |
tree | 529f8a9457bf2e6adee532790d7af0c90389b3be | |
parent | 16606c9e5f296b2fea0df38b54334673506209f3 (diff) |
initial commitruncode
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | README.md | 58 | ||||
-rw-r--r-- | runcode.c | 38 |
4 files changed, 103 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d995580 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +runcode +*.o diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5544d3a --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +.POSIX: + +runcode: runcode.c + +clean: + rm -f *.o runcode @@ -1,3 +1,59 @@ # runcode -execute raw binary code from a file
\ No newline at end of file +execute raw binary code from a file + +# Synopsis + +`runcode file` + +# Description + +The `runcode` utility loads an arbitrary binary file into memory and attempts +to execute it. This is useful for testing shellcode used in penetration +testing. + +# Options + +None. + +# Operands + +`file` A file containing executable instructions. + +# STDIN + +Not used by `runcode` itself. May be used by the loaded program. + +# Input Files + +The input file must be contain valid executable instructions appropriate +to architecture `runcode` is being executed on. + +# STDOUT + +Not used by `runcode` itself. May be used by the loaded program. + +# STDERR + +Used for diagnostic messages. + +# Output Files + +None. + +# Extended Description + +None. + +# Exit Status + +0 Successful completion. +>0 An error occurred. + +# Consequence of Errors + +If the file specified cannot be opened for reading or mapped for execution, +a diagnostic message will be printed to standard error. Behavior is undefined +if the file specified does not contain an executable instruction at its first +byte. + diff --git a/runcode.c b/runcode.c new file mode 100644 index 0000000..5a69ea1 --- /dev/null +++ b/runcode.c @@ -0,0 +1,38 @@ +#define _POSIX_C_SOURCE 199309 +#include <fcntl.h> +#include <stdio.h> +#include <sys/mman.h> +#include <sys/stat.h> + +int main(int argc, char *argv[]) +{ + if (argc != 2) { + fprintf(stderr, "Usage: %s file\n", argv[0]); + return 1; + } + + int fd = open(argv[1], O_RDONLY); + if (fd == -1) { + perror("open"); + return 1; + } + + struct stat st; + if (fstat(fd, &st) == -1) { + perror("fstat"); + return 1; + } + + void *code = mmap(NULL, st.st_size, PROT_EXEC | PROT_READ | PROT_WRITE, + MAP_PRIVATE, fd, 0); + + if (code == MAP_FAILED) { + perror("mmap"); + return 1; + } + + void (*fn)(void) = (void (*)(void))code; + fn(); + + return 0; +} |