# mini_httpd — CTF practice target
# Default build disables modern protections to simulate old router firmware.

CC      = gcc
TARGET  = mini_httpd
SRC     = mini_httpd.c

# === Router-like build (default) ===
# No stack canary, no PIE, partial RELRO, executable stack optional
CFLAGS_ROUTER  = -Wall -g -O0 \
                 -fno-stack-protector \
                 -no-pie \
                 -Wl,-z,norelro \
                 -Wl,-z,execstack \
                 -D_FORTIFY_SOURCE=0

# === Hardened build (for comparison / advanced challenge) ===
CFLAGS_HARD    = -Wall -g -O2 \
                 -fstack-protector-strong \
                 -pie -fPIE \
                 -Wl,-z,relro,-z,now \
                 -D_FORTIFY_SOURCE=2

# === Debug build with AddressSanitizer (find bugs quickly) ===
CFLAGS_ASAN    = -Wall -g -O0 \
                 -fsanitize=address \
                 -fno-omit-frame-pointer

.PHONY: all router hardened asan clean

all: router

router: $(SRC)
	$(CC) $(CFLAGS_ROUTER) -o $(TARGET) $(SRC)
	@echo ""
	@echo "Built with router-like protections (none)."
	@echo "Disable ASLR for deterministic heap layout:"
	@echo "  echo 0 | sudo tee /proc/sys/kernel/randomize_va_space"
	@echo ""
	@echo "Run:  ./$(TARGET)"

hardened: $(SRC)
	$(CC) $(CFLAGS_HARD) -o $(TARGET)_hard $(SRC)
	@echo "Built with modern hardening.  Good luck."

asan: $(SRC)
	$(CC) $(CFLAGS_ASAN) -o $(TARGET)_asan $(SRC)
	@echo "Built with ASAN — will report heap bugs on trigger."

clean:
	rm -f $(TARGET) $(TARGET)_hard $(TARGET)_asan core
