<div align="center">
  
  [![Pre-release](https://img.shields.io/github/v/release/lukeswitz/PathShield?include_prereleases&label=pre-release&color=orange)](https://github.com/lukeswitz/PathShield/releases)
[![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/lukeswitz/PathShield)](https://github.com/lukeswitz/AntiHunter/tree/main/PathShield/)
  
<img height="500" alt="image" src="https://github.com/user-attachments/assets/0dac6a9f-32a8-4b05-b6a1-b59fc3762f51" />

PathShield is an RF awareness tool for M5StickC Plus 1.1 and Plus 2. It uses BLE/WiFi scanning to detect nearby devices, alerting on those following you.
</div>


---

> [!CAUTION]
> **ETHICAL USE ONLY**
> Users are solely responsible for: Compliance with all applicable laws. Respecting reasonable expectations of privacy


## Table of Contents

1. [Features](#features)
2. [Installation](#installation)
3. [Controls](#controls)
4. [Display Guide](#display-guide)
5. [Detection Algorithm](#detection-algorithm)
6. [Customization](#customization)
7. [Troubleshooting](#troubleshooting)
8. [Known Limitations](#known-limitations)
9. [Credits](#credits)
10. [License](#license)


## Features

- **Dual-Band Scanning**: Alternates WiFi and BLE detection, displays MAC, vendor, SSID, channel, hit count & RSSI
- **Persistence Scoring**: Multi-factor algorithm reduces false positives. Allowlist for known devices
- **Real-Time Alerts**: Visual notifications for detected trackers and user-specified MAC targets
- **Tracker Detection**: AirTag, Tile, SmartTag, Chipolo, Google FMDN identified by protocol
- **Known Device Ring Buffer**: Stable-RSSI devices promoted to compact storage, freeing active slots
- **24,500+ MAC Database**: Offline manufacturer identification
- **Hardware Adaptive**: Auto-detects CPlus2 PSRAM for 2x device tracking capacity
- **Dynamic Memory Scaling**: Device limits scale to available heap at boot

![image](https://github.com/user-attachments/assets/fade8692-0052-4e00-b244-b068992c8772)


> [!TIP]
> Modify `allowlistMacs` to ignore known devices.
>
> Change the `specialMacs` to your own target devices (default detects Flock and Axon Taser cameras).

## Installation

### Web Flasher
[Install PathShield](https://lukeswitz.github.io/PathShield/)

1. Open link in Chrome, Edge, or Opera (not Safari/Firefox)
2. Select your hardware (Plus 1.1 or Plus 2)
3. Connect device via USB-C
4. Click "Deploy Firmware"
5. Select serial port, wait ~2 minutes

### From Source (Arduino IDE)

**M5StickC Plus 1.1:**
1. Board: **M5StickCPlus**
2. PSRAM: **Disabled**
3. Partition: **Huge APP (3MB No OTA/1MB SPIFFS)**

**M5StickC Plus 2:**
1. Board: **M5StickCPlus2**
2. PSRAM: **Enabled**
3. Partition: **8M with spiffs (3MB APP/1.5MB SPIFFS)**

Both require M5Unified and NimBLE-Arduino libraries.


## Controls

### Normal Scanning Mode
```
Button A:   Pause scanning
Button B:   Toggle name filter
A+B (hold): Settings menu
```

### Paused 
```
Button A:      Scroll up
Button B:      Scroll down (tap)
Button B hold: Resume (hold 1 second)
```

### Settings Menu
```
Button A:  Navigate options (up/down)
Button B:  Select option
A+B hold:  Exit menu
```

**Available Settings:**
- **Brightness**: Low/High (saves battery on low brightness)
- **Clear Devices**: Clears all tracked devices from memory
- **Display Timeout**: How long before screen turns off when idle (10-300 seconds)
- **Shutdown Device**: Power off the device

## Display Guide

### Top Status Bar
- **Scan Mode**: `SCAN` (green = actively scanning), `PAUSE` (red = paused)
- **WiFi/BLE Indicator**: Current scan mode (WiFi or Bluetooth)
- **Battery Bar**: Device battery percentage (0-100%)
- **Memory Bar**: Available RAM in KB (green = good, yellow = warning, red = critical)

### Device List
Each device shows:
```
Device Name (BLE) or SSID (WiFi)
Manufacturer (identified from MAC)
Detection Count + Signal Strength (RSSI)
```

### Color Codes
```
CYAN    = WiFi networks / Normal Bluetooth devices
ORANGE  = User-defined tracker (special MAC)
RED     = Suspected tracker detected (high persistence score)
YELLOW  = Manufacturer name
GREEN   = Scan active, status messages
```

### Filter Mode
- Press **Button B** to toggle between "Show All" (all devices) and "Named Only" (only named devices)
- Useful for cutting through noise when there are many unnamed devices

### Footer
- **Page counter**: Shows which page you're viewing (e.g., "1-3/23")
- **Scroll hint**: When paused, shows navigation instructions

## Detection Algorithm

### How Tracker Identification Works

PathShield uses a **multi-layer packet inspection approach** to reliably identify trackers from BLE advertisement data. Since modern trackers use standardized Bluetooth identifiers, this method catches them regardless of MAC randomization.

**Layer 1: Manufacturer Data (Company IDs)**

BLE packets include manufacturer-specific data identified by a 16-bit company ID (stored little-endian). Known trackers advertise predictable company IDs:

- **AirTag & Find My Devices**: Company ID `0x004C` (Apple Inc.)
  - Must have ≥27 bytes of payload
  - Payload type byte at offset 2 must be `0x12` or `0x07` (Find My authentication)
  - Also catches: Chipolo ONE Spot, Eufy cameras in Find My mode

- **Samsung SmartTag**: Company ID `0x0075` (Samsung Electronics)

- **Chipolo (native mode)**: Company ID `0x0133` (Chipolo d.o.o.)

- **Google FMDN**: Company ID `0x00E0` (Google Find My Device Network)

**Layer 2: Service UUIDs**

Some trackers advertise standard BLE service UUIDs instead of (or in addition to) manufacturer data:

- **Tile**: Service UUIDs `0xFD51` or `0xFD52`
- **Samsung SmartTag (alt)**: Service UUID `0xFD6F` (SmartThings/Find)

**Layer 3: Device Name (Fallback)**

If manufacturer data and service UUIDs don't match, PathShield falls back to case-insensitive name matching:
- Device name contains "tile" → Tile tracker
- Device name contains "smarttag" → Samsung SmartTag
- Device name contains "chipolo" → Chipolo

This layered approach works because trackers must advertise these identifiers to function — they can't hide their identity without breaking their intended purpose. The combination of multiple detection methods eliminates false negatives.

### Persistence Scoring (0.0 - 1.0)

**Factor 1: Detection Frequency (0.25 max)**
- Requires minimum 8 detections
- More frequent = higher score

**Factor 2: Time Window Distribution (0.30 max)**
- Tracks across 4 windows: 5/10/15/20 minutes
- Must appear in 3+ windows
- Consistent presence = tracking behavior

**Factor 3: ε-Connectedness (0.25 max)**
- No gaps >3 minutes between detections
- Continuous tracking pattern
- Distinguishes stalking from coincidence

**Factor 4: RSSI Pattern (0.20 max)**
- Signal strength variations
- Movement correlation

**Alert Threshold: ≥ 0.65**



## Customization

### Known Tracker MACs

Edit `specialMacs[]` in PathShield.ino:
```cpp
const char *specialMacs[] = {
  // Apple OUIs (AirTags use rotating addresses from Apple's OUI space)
  "AC:DE:48",  // Apple Inc.
  "F0:98:9D",  // Apple Inc.
  "BC:92:6B",  // Apple Inc.
  
  // Tile
  "C4:AC:05",  // Tile Inc.
  "E0:00:00",  // Tile Inc. (some models)
  
  // Samsung SmartTag
  "E4:5F:01",  // Samsung Electronics
  "74:5C:4B",  // Samsung Electronics
  "E8:50:8B",  // Samsung Electronics (additional)
  
  // Chipolo
  "EC:81:93",  // Chipolo d.o.o.
};
```

### Allowlist (Trusted Devices)

Add your own trusted devices to `allowlistMacs[]` to prevent false alerts:

```cpp
const char *allowlistMacs[] = {
  "AA:BB:CC",  // Your trusted device 1
  "DD:EE:FF",  // Your trusted device 2
  // Add MAC prefixes of devices you own
};
```

Allowlisted devices are completely ignored during scanning and will never trigger tracker alerts.

### Adjust Sensitivity

**More Sensitive (more alerts):**
```cpp
#define PERSISTENCE_THRESHOLD 0.50  // Lower threshold
#define MIN_DETECTIONS 5            // Fewer detections needed
#define MIN_WINDOWS 2               // Fewer time windows
```

**Less Sensitive (fewer false positives):**
```cpp
#define PERSISTENCE_THRESHOLD 0.75  // Higher threshold
#define MIN_DETECTIONS 12           // More detections needed
#define MIN_WINDOWS 4               // More time windows
```

**Scan Timing:**
```cpp
#define SCAN_SWITCH_INTERVAL 3000   // 3 seconds each (WiFi/BLE)
// Change to 2000 for faster scanning
// Change to 5000 for slower, battery-saving
```

### Display Customization

**Max devices shown:**
```cpp
const int maxDisplay = 3;  // Change to 2 or 4
```

**Color scheme (in displayTrackedDevices):**
```cpp
M5.Display.setTextColor(CYAN);     // Change to GREEN, BLUE, etc.
M5.Display.drawFastHLine(0, 0, SCREEN_WIDTH, MAGENTA);  // Border color
```

## Troubleshooting

### No Alerts for Known Tracker

**Solution:**
1. Lower threshold temporarily:
```cpp
#define PERSISTENCE_THRESHOLD 0.40
```
2. Check Serial Monitor (115200 baud) for detection counts
3. Verify tracker is powered on and advertising

### Too Many False Positives

**Quick fix: Use name filter**
- Press **Button B** to show only named devices
- Hides random MAC addresses and noise

**Persistent false positives: Add to allowlist**
1. Note the MAC address from the display
2. Add to `allowlistMacs[]` in PathShield.ino
3. Re-upload and restart

**Fine-tune sensitivity (advanced)**
```cpp
#define PERSISTENCE_THRESHOLD 0.75  // Raise to be more strict
#define MIN_DETECTIONS 12           // Require more detections
```

### Cannot Resume from Pause

Hold Button B for 1 full second (not just tap).

### Device Crashes / Resets

Watch the memory bar on screen — red means critically low. Device limits scale dynamically at boot based on available heap.

### SPIFFS Format on First Boot

Normal on first flash. The device formats SPIFFS automatically (~30 seconds), then boots normally.

### Button Not Responding

- Screen may be dimmed (press any button to wake)
- Wait 200ms between presses (debounce)
- For menu: hold both buttons 300ms+


## Hardware

| | M5StickC Plus 1.1 | M5StickC Plus 2 |
|---|---|---|
| **SoC** | ESP32-PICO-D4 | ESP32-PICO-V3-02 |
| **Flash** | 4MB | 8MB |
| **PSRAM** | None | 2MB |
| **Device Limits** | ~50 BLE, ~50 WiFi | ~70 BLE, ~50 WiFi |

Separate firmware builds required per board (different PSRAM/partition configs).

## Known Limitations

1. **MAC Randomization**: Modern phones randomize MACs — use name filter
2. **Range Limited**: BLE/WiFi ranges vary by environment
3. **No GPS**: Detects proximity only, not location
4. **Battery**: Continuous dual-band scanning drains battery in 4-6 hours

## Credits

Detection algorithms based on:
- [Chasing-Your-Tail-NG](https://github.com/ArgeliusLabs/Chasing-Your-Tail-NG) - Persistence tracking
- [BLE-Doubt](https://arxiv.org/abs/2205.12235) - Topological classification
- [CreepDetector](https://github.com/AlexLynd/CreepDetector) - Original concept

## License

MIT License - Use at your own risk. Developers provide no warranty and accept no liability for unlawful or
unethical use. Review local regulations before deployment.

## Contributing

Issues and pull requests welcome. Test thoroughly before submitting.

## Support

- GitHub Issues: Bug reports and feature requests
- Serial Monitor: Enable debugging (115200 baud)
- [Web Flasher](https://lukeswitz.github.io/PathShield/)
