From b73bf969e6538e03cc0d9761a62f3ffbd494eb6c Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Thu, 1 Aug 2019 10:10:52 -0400 Subject: initial commit --- .gitignore | 2 ++ Makefile | 6 ++++++ README.md | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- runcode.c | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 runcode.c 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 diff --git a/README.md b/README.md index c108733..7d8ba53 100644 --- a/README.md +++ b/README.md @@ -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 +#include +#include +#include + +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; +} -- cgit v1.2.1