diff --git a/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/03-rwslotmachine3/deploy/Dockerfile b/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/03-rwslotmachine3/deploy/Dockerfile new file mode 100644 index 0000000..3ccbd2f --- /dev/null +++ b/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/03-rwslotmachine3/deploy/Dockerfile @@ -0,0 +1,55 @@ +FROM debian:11 AS build + +ARG CHALLENGE_NAME="03-rwslotmachine" +ARG USER_NAME="rwslotmachine3" + +RUN useradd -m -d /home/${USER_NAME} -s /bin/bash ${USER_NAME} +RUN mkdir /home/${USER_NAME}/${CHALLENGE_NAME} + +COPY sol/rwslotmachine3.c /home/${USER_NAME}/${CHALLENGE_NAME}/ +COPY sol/Makefile /home/${USER_NAME}/${CHALLENGE_NAME}/ + +RUN dpkg --add-architecture i386 && \ + apt-get update && \ + apt-get install -y gcc-multilib make libc6-dev:i386 + +WORKDIR /home/${USER_NAME}/${CHALLENGE_NAME} +RUN make build + +RUN chown -R root:rwslotmachine3 /home/rwslotmachine3 +RUN chmod 750 /home/rwslotmachine3 +RUN chmod 750 /home/rwslotmachine3/${CHALLENGE_NAME} +RUN chmod 750 /home/rwslotmachine3/${CHALLENGE_NAME}/rwslotmachine3 +RUN chmod 750 /home/rwslotmachine3/${CHALLENGE_NAME}/Makefile + +FROM debian:11 AS runtime + +ARG CHALLENGE_NAME="03-rwslotmachine" +ARG USER_NAME="rwslotmachine3" + +RUN useradd -m -d /home/${USER_NAME} -s /bin/bash ${USER_NAME} +RUN mkdir /home/${USER_NAME}/${CHALLENGE_NAME} + +RUN apt-get update && \ + apt-get install -y make && \ + apt-get install -y procps && rm -rf /var/lib/apt/lists/* && \ + apt-get update && apt-get install -y iproute2 + +COPY --from=build /home/rwslotmachine3/03-rwslotmachine/rwslotmachine3 \ + /home/rwslotmachine3/03-rwslotmachine/ +COPY --from=build /home/rwslotmachine3/03-rwslotmachine/Makefile \ + /home/rwslotmachine3/03-rwslotmachine/ + +COPY src/ld-linux.so.2 /home/${USER_NAME}/${CHALLENGE_NAME}/ld-linux.so.2 +COPY src/libc.so.6 /home/${USER_NAME}/${CHALLENGE_NAME}/libc.so.6 + +WORKDIR /home/${USER_NAME}/${CHALLENGE_NAME} +RUN make check_binary + +RUN chown -R root:rwslotmachine3 /home/rwslotmachine3 +RUN chmod 750 /home/rwslotmachine3 +RUN chmod 750 /home/rwslotmachine3/${CHALLENGE_NAME} +RUN chmod 750 /home/rwslotmachine3/${CHALLENGE_NAME}/rwslotmachine3 +RUN chmod 750 /home/rwslotmachine3/${CHALLENGE_NAME}/Makefile + +CMD ["./ld-linux.so.2", "--library-path", ".", "./rwslotmachine3", "31346"] diff --git a/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/03-rwslotmachine3/deploy/Makefile b/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/03-rwslotmachine3/deploy/Makefile new file mode 100644 index 0000000..5b81045 --- /dev/null +++ b/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/03-rwslotmachine3/deploy/Makefile @@ -0,0 +1,37 @@ +PORT_BUILD ?= 32000 +PORT_RUNTIME ?= 32001 +IMG_BUILD ?= rwslotmachine3_build +IMG_RUNTIME ?= rwslotmachine3_runtime +CONT_BUILD ?= $(IMG_BUILD)-cnt +CONT_RUNTIME ?= $(IMG_RUNTIME)-cnt + +create_build: + docker build -t $(IMG_BUILD) --target=build -f Dockerfile .. + +create_runtime: + docker build -t $(IMG_RUNTIME) --target=runtime -f Dockerfile .. + +run_build: + docker run -d --rm -p $(PORT_BUILD):31346 --name $(CONT_BUILD) -t $(IMG_BUILD) + +run_runtime: + docker run -d --rm -p $(PORT_RUNTIME):31346 --name $(CONT_RUNTIME) -t $(IMG_RUNTIME) + +stop_build: + -docker stop $(CONT_BUILD) + +stop_runtime: + -docker stop $(CONT_RUNTIME) + +stop_all: stop_build stop_runtime + +clean_build: stop_build + docker rm $(CONT_BUILD) + +clean_runtime: stop_runtime + docker rm $(CONT_RUNTIME) + +clean_all: clean_build clean_runtime + +.PHONY: create_build create_runtime run_build run_runtime \ + stop_all stop_build stop_runtime clean clean_build clean_runtime diff --git a/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/03-rwslotmachine3/deploy/README.md b/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/03-rwslotmachine3/deploy/README.md new file mode 100644 index 0000000..7bb76e2 --- /dev/null +++ b/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/03-rwslotmachine3/deploy/README.md @@ -0,0 +1,36 @@ +#Build, Run and Stop the container + +- Build docker images for build and runtime stages: + + ```bash + make create_build + make create_runtime + ``` + +- Run the containers: + + ```bash + make run_build + make run_runtime + ``` + +- Stop the containers: + + ```bash + make stop_build + make stop_runtime + ``` + +- Clean + + ```bash + make clean_all + ``` + +#Use the container and the executable + +Build and run the runtime container and from host run: + + ```bash + nc 127.0.0.1 32001 + ``` diff --git a/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/03-rwslotmachine3/sol/Makefile b/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/03-rwslotmachine3/sol/Makefile new file mode 100644 index 0000000..d2619c3 --- /dev/null +++ b/chapters/mitigations-and-defensive-strategies/defense-mechanisms/activities/03-rwslotmachine3/sol/Makefile @@ -0,0 +1,19 @@ +CFLAGS = -m32 -fno-stack-protector -Wall -Wno-format-security +LDFLAGS = -no-pie +CC = gcc + +.PHONY: build check_binary check_port + +build: + $(CC) $(CFLAGS) $(LDFLAGS) rwslotmachine3.c -o rwslotmachine3 + +check_binary: + [ -f rwslotmachine3 ] && chmod +x rwslotmachine3 \ + || echo "error: executable file not found" + +check_port: + if ss -tulnp | grep 31346 | grep -q ld-linux.so.2; then \ + echo "executable listens or operates on port 31346."; \ + else \ + echo "error: executable doesn't operate on port 31346."; \ + fi