SecureDNS is a DNS middleware that enhances DNS resolution with additional security features, such as VirusTotal integration for domain reputation checks, caching using Redis, and logging DNS queries to a SQLite database. It also supports DNS resolution using Google and Cloudflare DNS-over-HTTPS (DoH) services.
This project is built to be used in a Raspberry Pi.
- VirusTotal Integration: Checks domains against VirusTotal to identify malicious domains.
- Redis Caching: Caches DNS query results and VirusTotal responses to improve performance.
- SQLite Logging: Logs DNS queries and middleware events for auditing and analysis.
- DNS-over-HTTPS (DoH): Resolves domains using Google and Cloudflare DoH services.
- Customizable Search Suffixes: Strips specific suffixes from domain names for internal use.
- NXDOMAIN Handling: Returns NXDOMAIN for unresolved domains.
- The
DNSHandlerclass indns_handler.pyprocesses incoming DNS queries. - It parses the DNS request, extracts the domain name, and checks if it matches specific suffixes or is a reverse DNS lookup.
- The middleware checks if the domain is flagged as malicious using VirusTotal.
- The
check_virustotalfunction invirustotal.pyqueries VirusTotal's API to check if a domain is flagged as malicious. - Results are cached in Redis for faster subsequent lookups.
- Redis is used to cache:
- VirusTotal results (
vt:<domain>). - Resolved IP addresses (
dns:<domain>).
- VirusTotal results (
- Cached results reduce the need for repeated external API calls and DNS queries.
- If a domain is not flagged as malicious, the middleware resolves it using:
- Local DNS resolution (
forward_dns_queryindns_utils.py). - Google DoH (
get_ip_addresses_from_googleindns_utils.py). - Cloudflare DoH (
get_ip_addresses_from_cloudflareindns_utils.py).
- Local DNS resolution (
- Resolved IP addresses are verified across multiple sources for accuracy.
- All DNS queries and middleware events are logged to a SQLite database (
dns_queriesandmiddleware_logstables). - The
log_queryandlog_eventfunctions indatabase.pyhandle logging.
- The middleware starts and stops gracefully, logging events to the database and log file.
- Signal handlers (
SIGINTandSIGTERM) ensure proper shutdown.
- Python 3.8 or higher.
- Redis installed and running on
localhost:6379. - SQLite (pre-installed with Python).
digcommand-line tool (for local DNS resolution).
-
Clone the repository:
git clone https://github.com/your-repo/SecureDNS.git cd SecureDNS -
Install dependencies:
pip install -r requirements.txt
-
Configure the
.envfile:- Add your VirusTotal API key in the
.envfile:VT_API_KEY = "your_virustotal_api_key"
- Add your VirusTotal API key in the
-
Initialize the SQLite database:
python database.py
-
Start the middleware:
sudo python main.py
-
Start the Middleware:
- Run
main.pyto start the DNS middleware. - The middleware listens on port 53 for DNS queries.
- Run
-
Query a Domain:
- Use a DNS client or tool (e.g.,
dig) to query the middleware:dig @127.0.0.1 example.com
- Use a DNS client or tool (e.g.,
-
Check Logs:
- View logs in the SQLite database (
/var/log/dns_queries.db) or the log file (/var/log/dns_middleware.log).
- View logs in the SQLite database (
main.py: Entry point for the middleware. Sets up logging, initializes the database, and starts the DNS server.dns_handler.py: Handles incoming DNS queries, integrates VirusTotal, and resolves domains.virustotal.py: Queries VirusTotal API and caches results in Redis.dns_utils.py: Provides DNS resolution using Google and Cloudflare DoH services.database.py: Manages SQLite database for logging queries and events..env: Stores environment variables (e.g., VirusTotal API key).
- VirusTotal API Key: Set in the
.envfile. - Redis Configuration: Default host is
localhost, port is6379, and database is0. Modify invirustotal.pyif needed. - Log File: Default path is
/var/log/dns_middleware.log. Modify inmain.pyif needed. - Database Path: Default path is
/var/log/dns_queries.db. Modify indatabase.pyif needed.
- A DNS query for
example.comis received. - The middleware checks Redis for cached results.
- If not cached, it queries VirusTotal to check if the domain is malicious.
- If safe, it resolves the domain using local DNS, Google DoH, or Cloudflare DoH.
- The resolved IP address is cached in Redis and returned to the client.
- The query is logged in the SQLite database.
-
Redis Connection Issues:
- Ensure Redis is running on
localhost:6379. - Check the Redis configuration in
virustotal.py.
- Ensure Redis is running on
-
Database Errors:
- Ensure the SQLite database file is writable.
- Check the database path in
database.py.
-
Port 53 Binding Issues:
- Run the middleware with
sudoto bind to port 53.
- Run the middleware with
This project is licensed under the MIT License. See the LICENSE file for details.
- Developed by Dhivijit Koppuravuri and Mokshagna Bhuvan.