Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
0xvpr committed Jun 5, 2022
0 parents commit ed87ab4
Show file tree
Hide file tree
Showing 12 changed files with 598 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Tests/ELF/elf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

int main()
{
return puts("Unix\n");
}
Binary file added Tests/ELF/elf.exe
Binary file not shown.
2 changes: 2 additions & 0 deletions Tests/ELF/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc elf.c -o elf.exe
2 changes: 2 additions & 0 deletions Tests/PE/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
x86_64-w64-mingw32-gcc-posix pe.c -o pe.exe
6 changes: 6 additions & 0 deletions Tests/PE/pe.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>

int main()
{
return puts("Windows\n");
}
Binary file added Tests/PE/pe.exe
Binary file not shown.
Binary file added bin/vpr-extract
Binary file not shown.
Binary file added build/main.o
Binary file not shown.
14 changes: 14 additions & 0 deletions compile_flags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-std=c99
--target=x86_64-pc-windows-msvc
-isystem/usr/x86_64-w64-mingw32/include
-isystem/usr/lib/gcc/x86_64-w64-mingw32/9.3-win32/include
-isystem/usr/lib/gcc/x86_64-w64-mingw32/9.3-win32/include-fixed
-Iinclude
-Wall
-Wextra
-Werror
-Wshadow
-Wpedantic
-Wconversion
-fms-compatibility
-fms-extensions
340 changes: 340 additions & 0 deletions f.bin

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
TARGET = vpr-extract

CC = gcc
CFLAGS = -O3 -Wall -Wextra -Werror -Wshadow -Wpedantic -Wconversion

LD = gcc
LDFLAGS = -s

BIN = bin
BUILD = build

SOURCE = src
OBJECT = $(BUILD)
SOURCES = $(wildcard $(SOURCE)/*.c)
OBJECTS = $(patsubst $(SOURCE)/%.c,$(OBJECT)/%.o,$(SOURCES))

INCLUDE = include
INCLUDES = $(addprefix -I,$(INCLUDE))

all: $(TARGET)

$(TARGET): $(BIN) $(BUILD) $(OBJECTS)
$(LD) $(LDFLAGS) $(OBJECTS) -o $(BIN)/$(TARGET)

$(OBJECTS): $(OBJECT)/%.o : $(SOURCE)/%.c
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(BIN):
mkdir -p $@

$(BUILD):
mkdir -p $@

.PHONY: clean
clean:
rm -fr $(BIN)/*
rm -fr $(BUILD)/*

.PHONY: extra-clean
extra-clean:
rm -fr $(BIN)
rm -fr $(BUILD)
186 changes: 186 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#include <stdlib.h>
#include <stdio.h>

static FILE* out_stream;
static char* program;
static char* target;
static int format;

static inline void __attribute((noreturn)) __usage_error(int error_code, char* restrict message)
{
fprintf(stderr,
"Error %d: %s.\n" // error_code, message
"\n"
"Usage:\n"
" %s <path/to/executable> [ optional switches ]\n" // program
"\n"
"positional arguments:\n"
" path/to/executable\n"
"\n"
"optional switches:\n"
" -c,-C\t\toutput as c-style char array\n"
" -s,-S\t\toutput as double quoted string\n"
"example :\n"
" %s path/to/executable\n" // program
,error_code, message, program, program
);

exit(error_code);
}

static inline void verify_requirements(void)
{
if (system("type objdump > /dev/null"))
{
__usage_error(-1, "'objdump' not found");
}
if (system("type grep > /dev/null"))
{
__usage_error(-1, "'grep' not found");
}
if (system("type cut > /dev/null"))
{
__usage_error(-1, "'cut' not found");
}
}

static void handle_command_line(int argc, char** argv)
{
if (argc < 2)
{
__usage_error(1, "Missing positional arguments");
}

for (int i = 2; i < argc; i++)
{
switch (argv[i][0])
{
case '-':
{
switch (argv[i][1])
{
case 'C':
case 'c':
{
format = 'c';
break;
}
case 'S':
case 's':
{
format = 's';
break;
}
case 'O':
case 'o':
{
if (i+1 >= argc)
{
__usage_error(2, "No file specified");
}
if (!(out_stream = fopen(argv[i+1], "w")))
{
char buffer[256] = { 0 };
snprintf(buffer, sizeof(buffer), "Failed to open: %s", argv[i+1]);
__usage_error(4, "Failed to open file");
}
break;
}
default:
{
__usage_error(3, "Unknown option supplied");
}
}
}
}
}

if (!format)
{
format = 'c';
}

if (!out_stream)
{
out_stream = stdout;
}
}

static inline void verify_target_exists(void)
{
FILE* fp;
if (!(fp = fopen(target, "rb")))
{
char buffer[256] = { 0 };
snprintf(buffer, sizeof(buffer), "Failed to find executable '%s'", target);
__usage_error(6, buffer);
}
fclose(fp);
}

int main(int argc, char** argv)
{
FILE* fp = NULL;
char disassembler[256] = { 0 };

program = argv[0];
handle_command_line(argc, argv);

target = argv[1];
verify_requirements();
verify_target_exists();

snprintf(disassembler, sizeof(disassembler), "objdump -d --section=.text %s | grep '^ ' | cut -f2", target);
if (!(fp = popen(disassembler, "r")))
{
fprintf(stderr, "Failed to disassemble %s\n", target);
return 5;
}

if (format == 'c')
{
unsigned size = 0;
for (int c; (c = fgetc(fp)) != EOF;)
{
if (c != ' ' && c != '\n' && c != '\t')
{
++size;
}
}
pclose(fp);

if (!(fp = popen(disassembler, "r")))
{
fprintf(stderr, "Failed to disassemble %s\n", target);
return 5;
}

fprintf(out_stream, "unsigned char shellcode[%u] = {\n ", size);
unsigned position = 0;
for (int c = 0; (c = fgetc(fp)) != EOF;)
{
if (c != ' ' && c != '\n' && c != '\t')
{
++position;
fprintf(out_stream, "0x%2X%c%s", c, (position < size ? ',' : '\0'), (position % 10 == 0 && position != 0 ? "\n " : " "));
}
}
fprintf(out_stream, "\n}\n");
}
else if (format == 's')
{
fputc('"', out_stream);
for (int c = 0; (c = fgetc(fp)) != EOF;)
{
if (c != ' ' && c != '\n' && c != '\t')
{
fprintf(out_stream, "\\x%2X", c);
}
}
fputc('"', out_stream);
pclose(fp);
}

fclose(out_stream);
return 0;
}

0 comments on commit ed87ab4

Please sign in to comment.