GitHunt
MI

micccon/pi-blocker-c

7-layer OSI network security stack in C using raw sockets on a Raspberry Pi Zero 2 W. Implements MITRE D3FEND defensive techniques at every layer. Includes full MITRE ATT&CK attack simulation.

Pi-Blocker ๐Ÿ›ก๏ธ

A 7-layer OSI network security stack built in C using raw sockets, running on a Raspberry Pi Zero 2 W. Implements MITRE D3FEND defensive techniques at every OSI layer โ€” from physical link monitoring up to DNS and HTTP application filtering. Includes a full MITRE ATT&CK attack simulation documenting what the stack catches and what it misses.

Started as a DNS ad blocker. Became something more.


What It Does

Layer Protocol D3FEND Technique What It Defends Against
L7 DNS + HTTP D3-DNSDL, D3-HTTPA C2 domains, ad networks, HTTP-based malware
L6 TLS D3-TLSIC Deprecated TLS, missing SNI, C2 tunneling
L5 TCP D3-CSLL SYN flood DoS, connection exhaustion
L4 TCP D3-NTCD Port scans (SYN, NULL, XMAS, FIN)
L3 IP D3-ITF Known malicious IPs, botnet C2 servers
L2 ARP D3-AAF ARP spoofing, MITM attacks
L1 Physical D3-NTA Physical taps, link state tampering

Architecture

Incoming Traffic
      โ†“
[L1] Netlink socket โ€” link state monitoring
[L2] AF_PACKET ETH_P_ARP โ€” ARP reply inspection
[L3] AF_PACKET ETH_P_IP โ€” IP reputation filtering
[L4] Raw TCP โ€” port scan detection + RST injection
[L5] Raw TCP โ€” SYN flood detection
[L6] Raw TCP โ€” TLS ClientHello policy engine
[L7] UDP port 53 โ€” DNS denylisting
[L7] TCP port 8080 โ€” HTTP proxy + blocklist
      โ†“
common/enforce.c โ€” shared iptables PI_BLOCKER chain
common/reputation.c โ€” IP threat intel feeds
common/blocklist.c โ€” domain blocklist (70k+ entries)

Every layer is independently threaded. Every decision is logged with inline MITRE technique tags:

[2026-03-06 15:39:21] [LAYER_4] [PORT] [BLOCKED] src=10.0.0.131 dst_port=587 unique_ports=18 d3fend=D3-NTCD attck=T1046
[2026-03-06 15:39:21] [LAYER_7] [DNS] [BLOCKED] domain=doubleclick.net client=10.0.0.5 d3fend=D3-DNSDL attck=T1071.004
[2026-03-06 15:39:21] [LAYER_6] [TLS] [BLOCKED (deprecated TLS)] host=example.com tls_ver=0x0301 d3fend=D3-TLSIC attck=T1573

Quick Start

git clone https://github.com/micccon/pi-blocker-c.git
cd pi-blocker

# Build all layers
make

# Run all layers at once (requires root)
sudo ./start_layer_all.sh

That's it. The startup script launches all 8 processes (DNS, HTTP proxy, TLS inspector, session tracker, port filter, IP filter, ARP monitor, link monitor) and initializes the shared PI_BLOCKER iptables chain.

Run individual layers manually:

sudo ./layer_7/start_layer7.sh    # DNS + HTTP proxy
sudo ./layer_6/start_layer6.sh    # TLS inspector
sudo ./layer_5/start_layer_5.sh   # Session tracker
sudo ./layer_4/start_layer4.sh    # Port filter
sudo ./layer_3/start_layer3.sh    # IP filter
sudo ./layer_2/start_layer2.sh    # ARP monitor
sudo ./layer_1/start_layer1.sh    # Link monitor

Run tests:

cd layer_tests
sudo ./run_all.sh                 # Run all layer tests
sudo ./layer_4_test.sh            # Run individual layer test

Project Structure

pi-blocker/
โ”œโ”€โ”€ Makefile                        โ€” builds all layers
โ”œโ”€โ”€ start_layer_all.sh              โ€” launches all layers at once
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ D3FEND.md                       โ€” D3FEND technique mapping per layer
โ”œโ”€โ”€ ATT&CK.md                       โ€” ATT&CK attack simulation writeup
โ”œโ”€โ”€ common/
โ”‚   โ”œโ”€โ”€ enforce.c / enforce.h       โ€” shared iptables enforcement (PI_BLOCKER chain)
โ”‚   โ”œโ”€โ”€ reputation.c / reputation.h โ€” IP threat intel feed loading + CIDR matching
โ”‚   โ”œโ”€โ”€ blocklist.c / blocklist.h   โ€” domain blocklist + binary search
โ”‚   โ””โ”€โ”€ net_hdrs.h                  โ€” packed protocol headers (IP, TCP, UDP, DNS, TLS, ARP)
โ”œโ”€โ”€ layer_7/
โ”‚   โ”œโ”€โ”€ dns/                        โ€” DNS sinkhole (D3-DNSDL)
โ”‚   โ”‚   โ”œโ”€โ”€ dns.c / dns.h
โ”‚   โ”‚   โ”œโ”€โ”€ main.c
โ”‚   โ”‚   โ””โ”€โ”€ Makefile
โ”‚   โ”œโ”€โ”€ http/                       โ€” HTTP proxy + CONNECT handler (D3-HTTPA)
โ”‚   โ”‚   โ”œโ”€โ”€ proxy.c / proxy.h
โ”‚   โ”‚   โ”œโ”€โ”€ main.c
โ”‚   โ”‚   โ””โ”€โ”€ Makefile
โ”‚   โ”œโ”€โ”€ start_layer7.sh
โ”‚   โ”œโ”€โ”€ Makefile
โ”‚   โ””โ”€โ”€ layer_7.md
โ”œโ”€โ”€ layer_6/                        โ€” TLS ClientHello policy engine (D3-TLSIC)
โ”‚   โ”œโ”€โ”€ tls_inspector.c / tls_inspector.h
โ”‚   โ”œโ”€โ”€ main.c
โ”‚   โ”œโ”€โ”€ start_layer6.sh
โ”‚   โ”œโ”€โ”€ Makefile
โ”‚   โ””โ”€โ”€ layer_6.md
โ”œโ”€โ”€ layer_5/                        โ€” SYN flood detection (D3-CSLL)
โ”‚   โ”œโ”€โ”€ session.c / session.h
โ”‚   โ”œโ”€โ”€ main.c
โ”‚   โ”œโ”€โ”€ start_layer_5.sh
โ”‚   โ”œโ”€โ”€ Makefile
โ”‚   โ””โ”€โ”€ layer_5.md
โ”œโ”€โ”€ layer_4/                        โ€” Port scan detection + RST injection (D3-NTCD)
โ”‚   โ”œโ”€โ”€ filter.c / filter.h
โ”‚   โ”œโ”€โ”€ main.c
โ”‚   โ”œโ”€โ”€ start_layer4.sh
โ”‚   โ””โ”€โ”€ Makefile
โ”œโ”€โ”€ layer_3/                        โ€” IP reputation filtering (D3-ITF)
โ”‚   โ”œโ”€โ”€ ip_filter.c / ip_filter.h
โ”‚   โ”œโ”€โ”€ main.c
โ”‚   โ”œโ”€โ”€ start_layer3.sh
โ”‚   โ”œโ”€โ”€ Makefile
โ”‚   โ””โ”€โ”€ layer_3.md
โ”œโ”€โ”€ layer_2/                        โ€” ARP spoofing detection (D3-AAF)
โ”‚   โ”œโ”€โ”€ arp_monitor.c / arp_monitor.h
โ”‚   โ”œโ”€โ”€ main.c
โ”‚   โ”œโ”€โ”€ start_layer2.sh
โ”‚   โ”œโ”€โ”€ Makefile
โ”‚   โ””โ”€โ”€ layer_2.md
โ”œโ”€โ”€ layer_1/                        โ€” Physical link state monitoring (D3-NTA)
โ”‚   โ”œโ”€โ”€ link_monitor.c / link_monitor.h
โ”‚   โ”œโ”€โ”€ main.c
โ”‚   โ”œโ”€โ”€ start_layer1.sh
โ”‚   โ””โ”€โ”€ Makefile
โ”œโ”€โ”€ layer_tests/
โ”‚   โ”œโ”€โ”€ run_all.sh
โ”‚   โ”œโ”€โ”€ layer_1_test.sh through layer_7_test.sh
โ”œโ”€โ”€ reputation/
โ”‚   โ””โ”€โ”€ reputation.txt              โ€” combined Feodo Tracker + Emerging Threats feed
โ”œโ”€โ”€ hostnames/
โ”‚   โ”œโ”€โ”€ blocklist.txt               โ€” 70k+ ad + malicious domains (sorted)
โ”‚   โ”œโ”€โ”€ random-domains-dnsperf.txt  โ€” benchmark dataset
โ”‚   โ””โ”€โ”€ random_domains.txt
โ””โ”€โ”€ images/

Layer Details

Layer 7 โ€” DNS Blocker (D3-DNSDL)

  • Raw UDP socket on port 53
  • 70,000+ domain blocklist, binary search O(log n)
  • RFC 1035 compliant parsing โ€” pointer-based name decompression
  • Subdomain matching โ€” blocking evil.com blocks sub.evil.com
  • Returns REFUSED for blocked domains
  • Counters: T1071.004

Performance on Pi Zero 2 W:

Queries/sec:     747.59
Avg latency:     79.7ms
Memory:          ~15MB with 70k domains

Layer 7 โ€” HTTP Proxy (D3-HTTPA)

  • TCP socket on port 8080, pthread per connection
  • Parses Host header, checks against blocklist
  • Returns 403 Forbidden for blocked domains
  • CONNECT tunneling for HTTPS โ€” with destination validation (loopback + RFC 1918 blocked)
  • Counters: T1071.001

Layer 6 โ€” TLS Inspector (D3-TLSIC)

  • Raw socket monitors ports 443 and 8080
  • Inspects TLS ClientHello before handshake completes
  • Policy checks: TLS version (min 1.2), SNI presence, ALPN value, extension count, ClientHello size
  • TCP RST injection on policy violation
  • Counters: T1573

Layer 5 โ€” Session Tracker (D3-CSLL)

  • Tracks SYN packets per source IP in tumbling 60s window
  • Hash table (1021 buckets, prime, chaining) โ€” O(1) lookup
  • Threshold: 20 SYNs โ†’ block via iptables
  • Mutex-protected, thread-safe
  • Counters: T1499

Layer 4 โ€” Port Filter (D3-NTCD)

  • Detects SYN, NULL, XMAS, FIN scan types by TCP flag inspection
  • Circular buffer tracks unique destination ports per source IP in 10s window
  • Threshold: 16 unique ports โ†’ block + RST inject
  • Counters: T1046

Layer 3 โ€” IP Filter (D3-ITF)

  • AF_PACKET raw socket โ€” sees forwarded traffic
  • Loads Feodo Tracker (botnet C2) + Emerging Threats feeds
  • CIDR + single IP matching, up to 4096 entries
  • Auto-updated via reputation/update.sh
  • Counters: T1590

Layer 2 โ€” ARP Monitor (D3-AAF)

  • AF_PACKET ETH_P_ARP socket, monitors ARP replies only
  • Maintains IPโ†’MAC table with 300s stale entry pruning
  • Alerts when MAC changes for known IP
  • Counters: T1557.002
  • AF_NETLINK NETLINK_ROUTE socket, RTMGRP_LINK group
  • Detects carrier loss (IFF_RUNNING drops)
  • Tracks flap count per interface with 10s alert cooldown
  • Counters: T1200

Shared Infrastructure

common/enforce.c โ€” All layers use a single enforcement library:

  • Dedicated PI_BLOCKER iptables chain โ€” clean flush on exit
  • block_ip(), block_port(), block_proto() โ€” deduplicated via hash tables
  • rst_inject() โ€” TCP RST with RFC 793 pseudo-header checksum
  • pthread_once init, mutex-protected throughout

common/net_hdrs.h โ€” Packed protocol headers for zero-copy parsing:

  • struct ip_hdr, struct tcp_hdr, struct udp_hdr
  • struct dns_hdr, struct tls_record_hdr, struct tls_handshake_hdr
  • struct eth_hdr, struct arp_pkt

Attack Simulation

After building the stack, I attacked it using Kali Linux, Metasploit, Burp Suite, and nmap โ€” treating the Pi as a black-box target.

What the stack caught:

Attack Tool Layer Result
Port scan nmap -sV L4 Blocked after 16th unique port
SYN flood hping3 --flood L5 Blocked after SYN threshold
ARP spoofing arpspoof L2 Alerted immediately
IP reputation hping3 -a <bad-ip> L3 Blocked before connection
DNS C2 dig @pi evil.com L7 REFUSED

What the stack missed:

A slow nmap scan (--scan-delay 15s) bypassed Layer 4's 10s detection window, revealing:

  • Port 22: OpenSSH 10.0p2
  • Port 8080: Open HTTP proxy

The HTTP proxy accepted CONNECT 127.0.0.1:22 without destination validation โ€” tunneling directly to SSH on localhost. This SSRF vulnerability allowed routing Metasploit's ssh_login module through the proxy, brute forcing credentials with a targeted Raspberry Pi default credential list, and obtaining a full interactive shell via proxychains โ€” all without a single log entry across all 7 layers.

Fix applied: CONNECT destination validation โ€” loopback and RFC 1918 ranges are now blocked before tunneling.

Full writeup: ATT&CK.md
D3FEND mapping: D3FEND.md


Known Limitations

Layer Limitation Planned Fix
L7 HTTP/1.0 CONNECT without Host header required parser fix Fixed
L6 Packet-based TLS inspection only โ€” fragmented ClientHello bypasses TCP stream reassembly
L5 Per-IP SYN threshold โ€” distributed floods bypass Subnet-level aggregate tracking
L4 Fixed 10s window โ€” slow scans bypass Adaptive/cumulative scoring
L3 Linear reputation scan O(n) Binary search

Update Threat Intel Feeds

# Pull fresh Feodo Tracker + Emerging Threats feeds
cd reputation
chmod +x update.sh
sudo ./update.sh

# Update domain blocklist
curl -o hosts.txt https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
grep "^0.0.0.0" hosts.txt | awk '{print $2}' | grep -v "^0.0.0.0$" | grep -v "^localhost$" \
  > hostnames/blocklist.txt
sort -u hostnames/blocklist.txt -o hostnames/blocklist.txt

Requirements

  • Raspberry Pi Zero 2 W (or any Linux system)
  • Root access (raw sockets require CAP_NET_RAW)
  • iptables installed
  • GCC + POSIX threads (-lpthread)
  • Build: make in any layer directory or root

Acknowledgments


License: MIT