summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-08-01 10:10:52 -0400
committerJakob Kaivo <jkk@ung.org>2019-08-01 10:10:52 -0400
commitb73bf969e6538e03cc0d9761a62f3ffbd494eb6c (patch)
tree529f8a9457bf2e6adee532790d7af0c90389b3be
parent16606c9e5f296b2fea0df38b54334673506209f3 (diff)
initial commitruncode
-rw-r--r--.gitignore2
-rw-r--r--Makefile6
-rw-r--r--README.md58
-rw-r--r--runcode.c38
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
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 <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;
+}