Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
name: Build
name: Build and Test

on: [push, pull_request]

jobs:
compile:
verify:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install GCC
- name: Install build tools
run: |
sudo apt-get update
sudo apt-get install -y gcc
sudo apt-get install -y gcc make

- name: Compile all lab programs
run: |
gcc -Wall -Wextra -Werror src/vuln_buffer_overflow.c -o vuln_buffer_overflow
gcc -Wall -Wextra -Werror src/safe_input_demo.c -o safe_input_demo
gcc -Wall -Wextra -Werror src/stack_layout_demo.c -o stack_layout_demo
gcc -Wall -Wextra -Werror src/overflow_behavior_demo.c -o overflow_behavior_demo
gcc -Wall -Wextra -Werror src/control_flow_simulation.c -o control_flow_simulation
- name: Run project verification
run: make check
29 changes: 24 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,38 @@ CC = gcc
CFLAGS = -Wall -Wextra -g -O0
VERIFY_CFLAGS = -Wall -Wextra -Werror
SRC_DIR = src
TEST_DIR = tests
PROGRAMS = vuln_buffer_overflow safe_input_demo stack_layout_demo overflow_behavior_demo control_flow_simulation
TEST_PROGRAM = demo_runtime_checks
TEST_SOURCES = $(TEST_DIR)/$(TEST_PROGRAM).c $(addprefix $(SRC_DIR)/,$(addsuffix .c,$(PROGRAMS)))

.PHONY: all verify clean
ifeq ($(OS),Windows_NT)
RUN_TEST = .\$(TEST_PROGRAM).exe
CLEAN_CMD = powershell -NoProfile -Command "$(foreach file,$(PROGRAMS) $(addsuffix .exe,$(PROGRAMS)) $(TEST_PROGRAM) $(TEST_PROGRAM).exe,Remove-Item -ErrorAction SilentlyContinue '$(file)';) exit 0"
else
RUN_TEST = ./$(TEST_PROGRAM)
CLEAN_CMD = rm -f $(PROGRAMS) $(addsuffix .exe,$(PROGRAMS)) $(TEST_PROGRAM) $(TEST_PROGRAM).exe
endif

.PHONY: all verify test check clean

all: $(PROGRAMS)

$(PROGRAMS): %: $(SRC_DIR)/%.c
$(PROGRAMS): %: $(SRC_DIR)/%.c $(SRC_DIR)/demo_programs.h
$(CC) $(CFLAGS) -o $@ $<

verify: $(addprefix verify-,$(PROGRAMS))
verify: $(addprefix verify-,$(PROGRAMS)) verify-tests

verify-%: $(SRC_DIR)/%.c
verify-%: $(SRC_DIR)/%.c $(SRC_DIR)/demo_programs.h
$(CC) $(VERIFY_CFLAGS) -o $* $<

verify-tests: $(TEST_SOURCES) $(SRC_DIR)/demo_programs.h
$(CC) $(VERIFY_CFLAGS) -DDEMO_NO_MAIN -I$(SRC_DIR) -o $(TEST_PROGRAM) $(TEST_SOURCES)

test: verify-tests
$(RUN_TEST)

check: verify test

clean:
-$(RM) $(PROGRAMS) $(addsuffix .exe,$(PROGRAMS))
-$(CLEAN_CMD)
184 changes: 111 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,114 +1,152 @@
# Exploit Lab
# Exploit Lab (C Memory Safety Demos)

Hands-on C demos for observing unsafe input, stack layout, overflow behavior, and control-flow concepts in a controlled, educational environment.
A beginner-friendly C lab for observing unsafe input, safer input handling, stack layout, deterministic overflow side effects, and conceptual control-flow changes.

## Overview

This repository is a memory-safety learning lab built around small, focused C programs. It exists to show how unsafe input handling behaves, how stack memory is laid out at runtime, how undefined behavior can appear after an out-of-bounds write, and how function-pointer reassignment changes execution flow in a safe, explicit way.
This project is built for learning, not exploitation. Each demo is small enough to read quickly and focused enough to show one memory-safety idea at a time.

The project is relevant to application security, systems programming, and debugging workflows. It is designed to help developers and learners understand why bounded input, runtime inspection, and defensive coding practices matter in real programs.
Core demos:

## Features
- `vuln_buffer_overflow`: shows why `scanf("%s", ...)` is dangerous with fixed-size buffers.
- `safe_input_demo`: contrasts that behavior with bounded input using `fgets(...)`.
- `stack_layout_demo`: prints local and parameter addresses so you can inspect stack-frame layout.
- `overflow_behavior_demo`: safely simulates how an unbounded copy can spill past a buffer into adjacent bytes.
- `control_flow_simulation`: demonstrates how changing a function pointer changes executed code.

- Demonstrates unsafe input handling with `scanf("%s", ...)` in a small stack-buffer example.
- Demonstrates safe input handling with `fgets(...)` and newline cleanup.
- Prints stack-frame addresses for local variables and function parameters.
- Shows overflow-related instability in a controlled demo that warns when input exceeds the buffer capacity.
- Simulates control-flow redirection with a function pointer reassignment.
- Includes GDB-oriented walkthroughs for inspecting locals, registers, and stack state.
- Documents runtime protections such as ASLR, NX, and stack canaries at a conceptual level.
- Provides captured screenshots and notes for the demos in `docs/images/`.
- Includes a GitHub Actions workflow that compiles all active demos on push and pull request.
## What Improved

## Tech Stack
The project now behaves like a real teaching lab instead of a compile-only collection of demos:

- Language: C
- Compiler: GCC
- Debugger: GDB
- Build tool: Make
- CI: GitHub Actions
- Target platforms: Linux and Windows with MinGW-compatible builds
- The overflow demo is deterministic and no longer relies on real undefined behavior just to explain overwrite effects.
- Each demo exposes a reusable `run_*` entry point through [`src/demo_programs.h`](/d:/GitHub%20Repo/exploit-lab-cpp/src/demo_programs.h:1), which makes the demos testable without changing their CLI behavior.
- A runtime smoke-test harness in [`tests/demo_runtime_checks.c`](/d:/GitHub%20Repo/exploit-lab-cpp/tests/demo_runtime_checks.c:1) validates the expected output of every stable demo.
- The Makefile now supports `make check`, and CI uses the same project-owned verification path instead of duplicating compile commands by hand.

## Project Structure

- `src/` -> active lab programs that build into the five demo binaries
- `vuln_buffer_overflow.c` -> unsafe input demo using `scanf("%s", ...)`
- `safe_input_demo.c` -> bounded input demo using `fgets(...)`
- `stack_layout_demo.c` -> prints stack addresses for local values and a parameter
- `overflow_behavior_demo.c` -> shows undefined behavior risk after oversized input
- `control_flow_simulation.c` -> safe function-pointer reassignment demo
- `docs/` -> guided learning material and reference notes
- `demo.md` -> quick walkthrough and sample outputs
- `gdb-guide.md` -> debugging workflow for the active binaries
- `lab-exercises.md` -> step-by-step beginner exercises
- `memory-layout.md` -> stack and buffer basics
- `protections.md` -> ASLR, NX, and stack canaries overview
- `advanced-concepts.md` -> conceptual theory only
- `images/` -> screenshots and supporting visual assets
- `archive/` -> deprecated placeholder files retained for compatibility
- `main.c` -> inactive placeholder
- `stack_behavior_demo.c` -> inactive placeholder
- `.github/workflows/` -> CI workflow that compiles the demo programs
- `Makefile` -> build, verify, and clean targets
- `README.md` -> project overview and setup guide

## Setup & Installation

Clone the repository:
```text
exploit-lab-cpp/
|- src/
| |- demo_programs.h
| |- vuln_buffer_overflow.c
| |- safe_input_demo.c
| |- stack_layout_demo.c
| |- overflow_behavior_demo.c
| |- control_flow_simulation.c
|- tests/
| |- demo_runtime_checks.c
|- docs/
| |- demo.md
| |- gdb-guide.md
| |- lab-exercises.md
| |- memory-layout.md
| |- protections.md
| |- advanced-concepts.md
|- archive/
|- .github/workflows/
|- Makefile
|- project.md
|- README.md
```

## Build

Prerequisites:

- GCC
- Make or `mingw32-make`
- Optional: GDB

Linux/macOS:

```bash
git clone https://github.com/urvalkheni/exploit-lab-cpp.git
cd exploit-lab-cpp
make
```

Install the required tools:
Windows (MinGW):

- Linux: `gcc`, `make`, and optionally `gdb`
- Windows: MinGW or another GCC-compatible toolchain, plus `make`
```powershell
mingw32-make
```

## Verify

Build all demos:
Strict compile verification:

```bash
make
make verify
```

Run a demo:
End-to-end compile plus runtime verification:

```bash
./safe_input_demo
make check
```

Windows (MinGW):

```powershell
.\safe_input_demo.exe
mingw32-make check
```

If you want to verify compilation without using the default build target, run:
## Run The Demos

Linux/macOS:

```bash
make verify
./vuln_buffer_overflow
./safe_input_demo
./stack_layout_demo
./overflow_behavior_demo
./control_flow_simulation
```

Windows:

```powershell
.\vuln_buffer_overflow.exe
.\safe_input_demo.exe
.\stack_layout_demo.exe
.\overflow_behavior_demo.exe
.\control_flow_simulation.exe
```

## Available Demos
## Example Output

`overflow_behavior_demo`

```text
[overflow_behavior_demo] Deterministic overflow impact demo
Value of x before input: 10
Enter input: AAAAAAAAAAAAAAAAAAAAAA
Input length: 22
WARNING: Input length (22) exceeds buffer capacity (15).
Simulated bytes written past buffer: 4
Bytes that reached adjacent int: 4 of 4
Adjacent int bytes after simulation: 41 41 41 41
Value of x after simulated copy: 1094795585
Buffer preview: AAAAAAAAAAAAAAAA
```

`control_flow_simulation`

- `vuln_buffer_overflow` -> unsafe input behavior demonstration
- `safe_input_demo` -> bounded input demonstration
- `stack_layout_demo` -> runtime stack address observation
- `overflow_behavior_demo` -> overflow effect observation with a warning
- `control_flow_simulation` -> conceptual control-flow redirection via a function pointer
```text
[control_flow_simulation] Conceptual control-flow demo
Calling through function pointer (before change):
safe_function(): normal control flow path.

## Helpful Docs
Simulating conceptual pointer corruption by manual reassignment...
Calling through function pointer (after change):
target_function(): alternate control flow path.
```

- [Demo guide](docs/demo.md)
- [Lab exercises](docs/lab-exercises.md)
- [GDB guide](docs/gdb-guide.md)
- [Memory layout basics](docs/memory-layout.md)
- [Runtime protections](docs/protections.md)
- [Advanced concepts](docs/advanced-concepts.md)
## Why This Matters

## Safety and Scope
- You can compare unsafe and safe input paths directly.
- You can study memory layout concepts without needing exploit code.
- You can reproduce the same learning signals in CI and on contributor machines.

This project is educational and defensive in scope. It does not include exploit payloads or a real exploitation walkthrough. The focus is on understanding memory behavior, debugging, and safer programming patterns.
## Disclaimer

Do not run these experiments on systems you do not own or have explicit permission to analyze.
This repository is for educational and defensive learning only.
Loading
Loading