README
¶
DNS Egress Control with eBPF
A project that implements DNS-based egress control using eBPF. This system intercepts DNS queries, resolves domains, and dynamically allows outbound traffic to the resolved IPs for the duration of the DNS TTL.
Features
- DNS Interception: Captures DNS queries and resolves them through upstream DNS servers
- TTL-based Whitelisting: Automatically allows IPs for the duration of their DNS TTL
- eBPF Filtering: Uses XDP (eXpress Data Path) to filter outbound traffic at the network level
- Automatic Cleanup: Removes expired IPs from the whitelist when their TTL expires
- Domain Monitoring: Periodically refreshes DNS records for configured domains
Architecture
┌─────────────────────────────────────────────────────────────┐
│ DNS Egress Control System │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────┐ │
│ │ DNS Server │ │ Domain Cache │ │ eBPF Manager │ │
│ │ │ │ │ │ │ │
│ │ - Listens on │ │ - Stores DNS │ │ - Manages XDP │ │
│ │ port 53 │───▶│ records │───▶│ programs │ │
│ │ - Handles │ │ - Tracks TTL│ │ - Updates maps │ │
│ │ queries │ │ - Auto-clean│ │ - Filters traffic│ │
│ └─────────────┘ └─────────────┘ └─────────────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────────┐│
│ │ eBPF/XDP Filter ││
│ │ - Intercepts all outbound traffic ││
│ │ - Checks destination IP against allowed list ││
│ │ - Allows traffic to whitelisted IPs ││
│ │ - Drops traffic to non-whitelisted IPs ││
│ └─────────────────────────────────────────────────────────┘│
└─────────────────────────────────────────────────────────────┘
How It Works
- DNS Query Handling: When a client makes a DNS query, our DNS server intercepts it
- DNS Resolution: The server resolves the domain through an upstream DNS server
- IP Whitelisting: The resolved IPs are added to the eBPF map with their TTL
- Traffic Filtering: The eBPF/XDP program checks all outbound packets against the whitelist
- TTL Management: When the TTL expires, the IPs are automatically removed from the whitelist
Installation
Prerequisites
- Linux kernel 4.18+ (for XDP support)
- Go 1.21+
- clang and llvm (for eBPF compilation)
- libelf-dev
- Network interface that supports XDP
Building
# Clone the repository
git clone https://github.com/HasseJohansen/egress-domain-whitelist.git
cd egress-domain-whitelist
# Build the project
make build
# Or build with eBPF program compilation
make build-ebpf
Using Docker
# Build the Docker image
docker build -t egress-domain-whitelist .
# Run the container with necessary privileges
docker run --rm --name egress-control \
--cap-add=NET_ADMIN \
--cap-add=SYS_ADMIN \
--network=host \
-e INTERFACE=eth0 \
-e UPSTREAM_DNS=8.8.8.8:53 \
egress-domain-whitelist
Configuration
Environment Variables
INTERFACE: Network interface to attach XDP program to (default:eth0)UPSTREAM_DNS: Upstream DNS server to use for resolution (default:8.8.8.8:53)PORT: Port to listen on for DNS queries (default:53)REFRESH_INTERVAL: How often to refresh DNS records (default:300s)
Command Line Arguments
./egress-domain-whitelist \
-interface eth0 \
-upstream-dns 8.8.8.8:53 \
-port 53 \
-domains example.com,google.com
Usage
Basic Usage
-
Start the service:
sudo ./egress-domain-whitelist -interface eth0 -
Configure your system to use this DNS server (e.g., in
/etc/resolv.conf):nameserver 127.0.0.1 -
Now when you access any domain, its IPs will be automatically whitelisted for the DNS TTL duration
Pre-whitelisting Domains
You can pre-whitelist specific domains by specifying them in the configuration:
sudo ./egress-domain-whitelist -interface eth0 -domains example.com,google.com
This will resolve these domains immediately and keep their IPs whitelisted, refreshing them periodically.
Implementation Details
eBPF/XDP Program
The eBPF program (bpf_program.c) implements an XDP filter that:
- Parses Ethernet headers to identify IP packets
- Extracts source and destination IP addresses
- Checks if the IP is in the allowed list (eBPF map)
- Allows or drops the packet based on the check
DNS Server
The DNS server:
- Listens for DNS queries on UDP port 53
- Resolves domains through upstream DNS servers
- Caches DNS responses with their TTL
- Updates the eBPF maps with allowed IPs
- Returns DNS responses to clients
Domain Cache
The domain cache:
- Stores DNS records with their TTL
- Automatically removes expired records
- Provides thread-safe access to cached records
IP Management
The system:
- Adds IPs to the eBPF maps when they are resolved
- Removes IPs when their TTL expires
- Periodically refreshes DNS records for configured domains
- Updates eBPF maps when IPs change
Security Considerations
- Privileges: The application requires
CAP_NET_ADMINandCAP_SYS_ADMINcapabilities to load eBPF programs - Network Interface: Only attach to interfaces where you want to filter traffic
- DNS Spoofing: Ensure your upstream DNS server is trustworthy
- Performance: XDP processing happens at the network layer, so it's very efficient
Limitations
- Currently only supports IPv4 (IPv6 support is in the eBPF program but not fully implemented in Go)
- Requires Linux kernel with XDP support
- Needs root privileges to load eBPF programs
- DNS server only handles A records (IPv4 addresses)
Future Enhancements
- Full IPv6 support
- TCP DNS support
- DNSSEC validation
- More sophisticated IP management (CIDR ranges, etc.)
- Metrics and monitoring
- Configuration file support
- Multiple upstream DNS servers with failover
License
This project is licensed under the GPL License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues.
Added README note
Documentation
¶
There is no documentation for this package.
Directories
¶
| Path | Synopsis |
|---|---|
|
config-server
module
|
|
|
Package ebpf provides eBPF-based DNS egress control
|
Package ebpf provides eBPF-based DNS egress control |