Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

3 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ›ก๏ธ AI-Powered IDS + DNS Anomaly Detector

Two-Layer Network Defense System โ€” Combining Intrusion Detection with DNS Threat Analysis, powered by Machine Learning and Claude AI.

Python Scikit-Learn Streamlit Claude AI License


๐Ÿ“Œ Overview

Most modern cyberattacks touch DNS first โ€” C2 beacons, malware callbacks, and data exfiltration all leverage DNS before or alongside network-level attacks. This project fuses two detection layers into a unified threat intelligence pipeline:

Layer Engine Detects
๐Ÿ”ด IDS Isolation Forest + Random Forest Port scans, DDoS, Brute force, Lateral movement
๐ŸŸ  DNS Isolation Forest + Random Forest DNS tunneling, DGA, Typosquatting, Fast-flux, Amplification
๐Ÿ”— Correlator Rule-based fusion engine Cross-layer threats with unified severity scoring
๐Ÿค– Claude AI claude-sonnet-4-6 Natural language explanations + MITRE ATT&CK mapping

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚               NETWORK TRAFFIC CAPTURE                โ”‚
โ”‚              (Simulated / Live pcap)                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
               โ”‚                  โ”‚
       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
       โ”‚  IDS ENGINE  โ”‚   โ”‚  DNS ENGINE      โ”‚
       โ”‚              โ”‚   โ”‚                  โ”‚
       โ”‚ โ€ข Flow stats โ”‚   โ”‚ โ€ข Query freq     โ”‚
       โ”‚ โ€ข Packet sizeโ”‚   โ”‚ โ€ข Domain entropy โ”‚
       โ”‚ โ€ข Port scan  โ”‚   โ”‚ โ€ข DGA detection  โ”‚
       โ”‚ โ€ข DDoS flags โ”‚   โ”‚ โ€ข Tunneling      โ”‚
       โ”‚ โ€ข Brute forceโ”‚   โ”‚ โ€ข Typosquatting  โ”‚
       โ”‚              โ”‚   โ”‚ โ€ข Fast-flux      โ”‚
       โ”‚ Isolation    โ”‚   โ”‚ Isolation        โ”‚
       โ”‚ Forest + RF  โ”‚   โ”‚ Forest + RF      โ”‚
       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
               โ”‚                  โ”‚
       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
       โ”‚         CORRELATION ENGINE           โ”‚
       โ”‚  Cross-link IDS alerts โ†” DNS events  โ”‚
       โ”‚  Assign unified threat score (0-10)  โ”‚
       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                       โ”‚
       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
       โ”‚         CLAUDE AI LAYER             โ”‚
       โ”‚  โ€ข Natural language threat reports  โ”‚
       โ”‚  โ€ข MITRE ATT&CK TTP mapping         โ”‚
       โ”‚  โ€ข Prioritised remediation steps    โ”‚
       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                       โ”‚
       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
       โ”‚       STREAMLIT DASHBOARD           โ”‚
       โ”‚  โ€ข Real-time threat feed            โ”‚
       โ”‚  โ€ข DNS query heatmap                โ”‚
       โ”‚  โ€ข Traffic anomaly graphs           โ”‚
       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐ŸŽฏ Detected Threats

IDS Layer

Attack Description
port_scan Sequential SYN probing to map open services
ddos High-volume traffic flood exhausting host resources
brute_force Repeated login attempts on SSH / RDP / FTP
lateral_movement Internal host pivoting via SMB / RDP / WMI

DNS Layer

Attack Description
dns_tunneling Data exfiltration encoded in DNS subdomains
dga Domain Generation Algorithm โ€” malware C2 beaconing
typosquatting Look-alike phishing domains
fast_flux Rapid IP rotation to evade blocklists (botnets)
dns_amplification DNS reflection DDoS amplification

Correlated (Cross-Layer) Threats

Combination Severity Meaning
lateral_movement + dns_tunneling ๐Ÿ”ด CRITICAL Active C2 channel with internal pivoting
brute_force + dga ๐Ÿ”ด CRITICAL Compromised host beaconing to C2
ddos + dns_amplification ๐Ÿ”ด CRITICAL Coordinated volumetric attack
port_scan + dga ๐ŸŸ  HIGH Malware reconnaissance phase
brute_force + typosquatting ๐ŸŸ  HIGH Credential phishing campaign

๐Ÿ“Š Model Performance

Metric IDS Model DNS Model
RF Weighted F1 1.0 1.0
IF Attack Recall ~93% ~86%
Features 23 24
Algorithms Isolation Forest + Random Forest Isolation Forest + Random Forest

๐Ÿ› ๏ธ Tech Stack

Component Technology
ML Models scikit-learn (Isolation Forest, Random Forest)
Feature Engineering pandas, numpy, scipy
DNS Analysis dnslib, dnspython
AI Explanations anthropic (claude-sonnet-4-6) with prompt caching
Dashboard streamlit, plotly
Terminal UI rich
Traffic Simulation Custom Python simulator

๐Ÿ“ Project Structure

ai_ids_dns_detector/
โ”œโ”€โ”€ data/
โ”‚   โ”œโ”€โ”€ simulator.py          # Network flow + DNS attack simulator
โ”‚   โ””โ”€โ”€ models/               # Saved ML models (.pkl) โ€” gitignored
โ”œโ”€โ”€ features/
โ”‚   โ”œโ”€โ”€ ids_features.py       # 23 network flow features (static + temporal)
โ”‚   โ””โ”€โ”€ dns_features.py       # 24 DNS features (entropy, DGA score, freq)
โ”œโ”€โ”€ detection/
โ”‚   โ”œโ”€โ”€ ids_model.py          # Isolation Forest + RF for IDS
โ”‚   โ”œโ”€โ”€ dns_model.py          # Isolation Forest + RF for DNS
โ”‚   โ””โ”€โ”€ correlator.py         # Cross-engine threat fusion & scoring
โ”œโ”€โ”€ llm/
โ”‚   โ””โ”€โ”€ explainer.py          # Claude API + rule-based fallback
โ”œโ”€โ”€ utils/
โ”‚   โ””โ”€โ”€ logger.py             # Rich terminal pretty-printing
โ”œโ”€โ”€ app.py                    # Streamlit dashboard (5 tabs)
โ”œโ”€โ”€ main.py                   # CLI entry point
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ .env.example              # API key template
โ””โ”€โ”€ .gitignore

๐Ÿš€ Getting Started

1. Clone & Install

git clone https://github.com/singhananya0043/ai_ids_dns_detector.git
cd ai_ids_dns_detector
pip install -r requirements.txt

2. Configure API Key (optional but recommended)

cp .env.example .env
# Edit .env and add your Anthropic API key:
# ANTHROPIC_API_KEY=sk-ant-...

Without an API key the system falls back to rule-based explanations with MITRE ATT&CK mappings โ€” all detection still works fully.

3. Run

# CLI โ€” simulate traffic, train models, detect & correlate threats
python -X utf8 main.py

# CLI with Claude AI explanations for top threats
python -X utf8 main.py --explain

# Larger dataset
python -X utf8 main.py --n 1000 --explain

# Launch Streamlit dashboard
python -X utf8 main.py --dashboard
# OR
streamlit run app.py

๐Ÿ–ฅ๏ธ Dashboard

The Streamlit dashboard provides 5 interactive tabs:

Tab Contents
๐Ÿšจ Threats Ranked threat table, score distribution, severity pie chart, Claude AI analysis
๐Ÿ“ก IDS Analysis Attack type breakdown, anomaly score histogram, flow timeline
๐ŸŒ DNS Analysis DNS attack types, domain length box plots, payload timeline
๐Ÿ”— Correlation IDS ร— DNS co-occurrence heatmap, confidence scatter plot
๐Ÿ“Š Model Metrics F1 scores, recall, feature importance charts

๐Ÿค– Claude AI Explanations

When ANTHROPIC_API_KEY is set, the system uses claude-sonnet-4-6 with prompt caching to generate:

  • ๐Ÿ“‹ Threat explanation โ€” context-aware, specific to the IDS + DNS signal combination
  • โš”๏ธ Attack vector โ€” how the attack is being carried out
  • ๐Ÿ’ผ Business impact โ€” potential consequences
  • ๐Ÿ”ง Remediation steps โ€” prioritised with time windows (0-15min, 1hr, 24hr, 7 days)
  • ๐ŸŽฏ MITRE ATT&CK โ€” full TTP mapping with sub-techniques

Example Output

๐Ÿ“‹ Threat Analysis for 10.0.9.8  [Claude API]
  Severity   : CRITICAL  |  Score: 10.0/10
  IDS Attack : lateral_movement (95.5% confidence)
  DNS Attack : dns_tunneling (100% confidence)

  Explanation:
    Host is actively pivoting through internal network via SMB/RDP while
    simultaneously exfiltrating data through a covert DNS tunnel to an
    attacker-controlled C2 server...

  Remediation:
    1. [0-15 min]  Isolate host at switch level โ€” do NOT power off
    2. [0-30 min]  Capture RAM dump and full disk image for forensics
    3. [1-4 hrs]   Block outbound DNS; analyse tunneling domain
    4. [4-24 hrs]  Hunt laterally; reset all credentials from this host
    5. [1-7 days]  Deploy DNS security solution; implement segmentation

  MITRE ATT&CK: T1071.004, T1048.001, T1572, T1021.002, T1550.002

โš™๏ธ CLI Options

python -X utf8 main.py [OPTIONS]

Options:
  --n INT         Number of normal traffic samples to simulate (default: 400)
  --seed INT      Random seed for reproducibility (default: 42)
  --explain       Generate Claude AI threat explanations for top threats
  --dashboard     Launch the Streamlit dashboard
  --top INT       Number of top threats to display (default: 10)

๐Ÿ”ฎ Roadmap

  • Live PCAP capture mode (Scapy integration)
  • Real DNS resolver integration (dnspython)
  • LSTM-based DGA classifier
  • Threat intelligence feed integration (VirusTotal, Shodan)
  • Slack / email alerting for CRITICAL events
  • Docker containerisation
  • Export threat reports to PDF

๐Ÿ™ Acknowledgements


๐Ÿ“„ License

This project is licensed under the MIT License.


Built with ๐Ÿ›ก๏ธ and ๐Ÿค– by singhananya0043

About

Two-layer AI defence - Intrusion Detection System + DNS Anomaly Detector using Isolation Forest, Random Forest and Claude AI explanations

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages