feat: add spoofer
- spoofer redirects reads to /proc to ./proc
This commit is contained in:
12
Makefile
12
Makefile
@@ -32,6 +32,8 @@ HOST_SRC_EXTRA := src/elf.c src/hwinfo.c src/util.c tiny-AES-c/aes.c
|
||||
HOST_SRC := src/main.c $(HOST_SRC_EXTRA)
|
||||
HOST_SRC_ENCRYPTOR := src/encryptor.c $(HOST_SRC_EXTRA)
|
||||
HOST_INCLUDE := -I src -I tinyriscv/src -I tiny-AES-c
|
||||
HOST_SRC_SPOOFER := src/spoof/spoofer.c
|
||||
|
||||
|
||||
ifeq ($(OS),Windows_NT)
|
||||
HOST_EXE := build/main.exe
|
||||
@@ -45,6 +47,8 @@ else
|
||||
HOST_EXE_ENCRYPTOR := build/encryptor
|
||||
endif
|
||||
|
||||
HOST_EXE_SPOOFER := build/spoofer
|
||||
|
||||
|
||||
# Disassembly
|
||||
RISCV_OBJDUMP ?= $(patsubst %-gcc,%-objdump,$(RISCV_CC))
|
||||
@@ -55,16 +59,20 @@ _DISAS_FILES := $(if $(_DISAS_TARGETS),$(filter %.elf %.o,$(MAKECMDGOALS)))
|
||||
|
||||
.PHONY: all emulated-riscv host clean disas-risc
|
||||
|
||||
all: $(ZIP_FILE)
|
||||
all: $(ZIP_FILE) $(HOST_EXE_SPOOFER)
|
||||
|
||||
emulated-riscv: $(EMU_OBJ_LICENSE)
|
||||
|
||||
host: $(HOST_EXE)
|
||||
host: $(HOST_EXE) $(HOST_EXE_SPOOFER)
|
||||
|
||||
# Build the main challenge executable
|
||||
$(HOST_EXE): $(HOST_SRC) | build
|
||||
$(HOST_CC) $(HOST_CFLAGS) $(HOST_LDFLAGS) $(HOST_INCLUDE) $(HOST_SRC) -o $@
|
||||
|
||||
# Build the spoofer
|
||||
$(HOST_EXE_SPOOFER): $(HOST_SRC_SPOOFER) | build
|
||||
$(HOST_CC) $(HOST_CFLAGS) $(HOST_LDFLAGS) -shared -fPIC $^ -o $@ -ldl
|
||||
|
||||
# Build the encryptor
|
||||
$(HOST_EXE_ENCRYPTOR): $(HOST_SRC_ENCRYPTOR) | build
|
||||
$(HOST_CC) $(HOST_CFLAGS) $(HOST_LDFLAGS) $(HOST_INCLUDE) $(HOST_SRC_ENCRYPTOR) -o $@
|
||||
|
||||
39
src/spoof/spoofer.c
Normal file
39
src/spoof/spoofer.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
static FILE *(*real_fopen)(const char *path, const char *mode) = NULL;
|
||||
|
||||
static void init_real_fopen(void) {
|
||||
if (!real_fopen) {
|
||||
real_fopen = dlsym(RTLD_NEXT, "fopen");
|
||||
if (!real_fopen) {
|
||||
fprintf(stderr, "proc_redirect: dlsym(fopen) failed: %s\n", dlerror());
|
||||
abort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FILE *fopen(const char *path, const char *mode) {
|
||||
init_real_fopen();
|
||||
|
||||
if (path && strncmp(path, "/proc/", 6) == 0) {
|
||||
const char *rel_suffix = path + 1;
|
||||
size_t len = strlen(rel_suffix) + 3;
|
||||
char *new_path = malloc(len);
|
||||
if (!new_path) {
|
||||
fprintf(stderr, "proc_redirect: malloc failed\n");
|
||||
abort();
|
||||
}
|
||||
new_path[0] = '.';
|
||||
new_path[1] = '/';
|
||||
strcpy(new_path + 2, rel_suffix);
|
||||
|
||||
FILE *f = real_fopen(new_path, mode);
|
||||
free(new_path);
|
||||
return f;
|
||||
}
|
||||
|
||||
return real_fopen(path, mode);
|
||||
}
|
||||
Reference in New Issue
Block a user