~jkaivo/runcode

b73bf969e6538e03cc0d9761a62f3ffbd494eb6c — Jakob Kaivo 3 years ago 16606c9 runcode
initial commit
4 files changed, 103 insertions(+), 1 deletions(-)

A .gitignore
A Makefile
M README.md
A runcode.c
A .gitignore => .gitignore +2 -0
@@ 0,0 1,2 @@
runcode
*.o

A Makefile => Makefile +6 -0
@@ 0,0 1,6 @@
.POSIX:

runcode: runcode.c

clean:
	rm -f *.o runcode

M README.md => README.md +57 -1
@@ 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.


A runcode.c => runcode.c +38 -0
@@ 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;
}