An AI-powered system for detecting insider threats using machine learning. This project combines unsupervised anomaly detection (Isolation Forest) with supervised classification (XGBoost) for comprehensive threat identification and explainability.
This system analyzes user behavior within an organization to identify potential insider threats. It leverages:
- Hybrid ML Approach: Combines unsupervised anomaly detection with supervised classification
- Explainability: XAI integration for transparent threat explanations
- Real-time Inference: FastAPI-based REST API for live threat analysis
- Production Ready: Deployment-ready with containerization support
insider_threat_detection_system/
βββ data_transformation_and_preprocessing/ # ETL Pipeline
β βββ 01_ingest_and_compress/ # CSV to Parquet conversion
β βββ 02_load_to_duckdb/ # Data loading into DuckDB
β βββ 03_aggregate_features/ # Feature aggregation by user
β βββ 04_unsupervised_data_preprocessing/ # Unsupervised data prep & EDA
β βββ 05_supervised_data_preprocessing/ # Supervised data prep & EDA
βββ model_training_and_evaluation/ # Model training notebooks
β βββ supervised_model_XGBClassifier/ # XGBoost training & evaluation
β βββ unsupervised_model_isolation_forest/ # Isolation Forest training
βββ inference/ # Inference pipelines
β βββ 01_fake_data_generation/ # Test data generation
β βββ 02_inference_hybrid_approach/ # Combined inference pipeline
β βββ 03_XAI_Integration/ # Explainability integration
βββ deployment/ # Production deployment
β βββ server.py # FastAPI application
β βββ inference_with_XAI_package.py # Unified inference engine
β βββ requirements.txt # Deployment dependencies
βββ requirements.txt # Core dependencies
βββ .gitignore # Git ignore patterns
- Python 3.8+
- pip or conda
-
Clone the repository
git clone https://github.com/AmanJ4588/insider_threat_detection_ai cd insider_threat_detection_system -
Install dependencies
pip install -r requirements.txt
-
Download the CERT R4.2 Dataset
- Download from kilthub.cmu.edu
- Place the dataset in the
dataset/directory following the existing structure
Start the FastAPI server for real-time inference:
cd deployment
python server.pyThe API will be available at http://localhost:8000
API Endpoints:
GET /- Health checkPOST /analyze- Analyze user data for threats
Example Request:
curl -X POST "http://localhost:8000/analyze" \
Β -H "Content-Type: application/json" \
Β -d "{
\"user_id\": \"string\",
\"total_logon_events\": integer,
\"logon_unique_pcs\": integer,
\"logon_after_hours_ratio\": float,
\"unique_urls_visited\": integer,
\"file_access_count\": integer,
\"is_weekend\": integer_or_boolean,
\"unique_recipients\": integer,
\"total_emails\": integer,
\"agreeableness\": float,
\"total_http_events\": integer,
\"conscientiousness\": float,
\"extraversion\": float,
\"openness\": float,
\"neuroticism\": float,
\"unique_roles\": integer,
\"http_unique_pcs\": integer,
... (all remaining key-value pairs)
}"Response:
{
"user_id": "string",
"risk_score": "float",
"anomaly_score": "float",
"verdict": "string",
"threshold_used": "float",
"explanation": {
"risk_drivers": [
{
"feature_id": "string",
"display_name": "string",
"impact_score": "float",
"raw_value": "float_or_int",
"human_value": "string",
"narrative": "string"
},
"..."
],
"anomaly_drill_down": "string"
},
"action": "string"
}- Converts raw CSV files to Parquet format for efficiency
- Reduces storage footprint while maintaining data integrity
- Loads Parquet files into DuckDB for fast querying
- Creates structured database tables for analysis
- Scripts:
parquet_to_duckDB.py,answers_to_duckDB.py
- Aggregates events by user to create behavioral features
- Generates datasets for both supervised and unsupervised learning
- Notebooks:
tables_aggregation_by_user.ipynb,unsupervised_tables_export.ipynb
- EDA on unlabeled user data
- Feature scaling and normalization
- Outlier detection and handling
- Labels insider threat data using ground truth
- EDA on labeled dataset
- Handles class imbalance
- Purpose: Detect anomalous user behavior patterns
- Training:
model_training_and_evaluation/unsupervised_model_isolation_forest/ - Output: Anomaly scores (0-100) indicating deviation from normal behavior
- Advantages: Doesn't require labeled data, detects novel attack patterns
- Purpose: Classify users as insider threats based on labeled examples
- Training:
model_training_and_evaluation/supervised_model_XGBClassifier/ - Components:
hyperparameter_tuning.ipynb- Hyperparameter optimizationmodel_training_and_evaluation.ipynb- Training and evaluationbest_threshold.ipynb- Optimal decision threshold selection
- Output: Risk scores (0-100) and threat classification
- Combines both models for comprehensive threat detection
- Unsupervised scores identify behavioral anomalies
- Supervised scores provide labeled threat classification
- Final decision based on both signals
User Data
β
[Unsupervised] β Isolation Forest β Anomaly Score
β
[Feature Processing]
β
[Supervised] β XGBoost Classifier β Risk Score
β
[XAI Integration]
β
Threat Assessment + Explanation
- SHAP values for model explainability
- Feature importance analysis
- Transparent threat explanations
- scikit-learn (1.7.2) - Machine learning algorithms
- xgboost (3.1.1) - Gradient boosting classifier
- pandas (2.3.3) - Data manipulation
- numpy (2.3.3) - Numerical computing
- duckdb (1.4.0) - Fast SQL database
- pyarrow (17.0.0) - Columnar data format (required for Parquet operations)
- fastapi (0.119.0) - REST API framework
- uvicorn (0.34.0) - ASGI server
- gunicorn (23.0.0) - WSGI HTTP server (optional)
- shap (0.50.0) - SHAP values for interpretability
- tensorboard (2.20.0) - Training visualization (optional)
- jupyter (1.1.1) - Interactive notebooks
See requirements.txt for full dependency list.
Generate test data for inference and validation:
python inference/01_fake_data_generation/fake_data_generator.pyExecute the hybrid inference pipeline:
python inference/02_inference_hybrid_approach/hybrid_inference.pyThis repository does not include a Dockerfile. The commands below are example placeholders β add a Dockerfile at the repository root or adjust the build context before running them.
# Example (requires a Dockerfile at repository root)
docker build -t insider-threat-detection:latest .
docker run -p 8000:8000 insider-threat-detection:latestcd deployment
pip install -r requirements.txt
# Recommended: use Gunicorn with the Uvicorn worker for FastAPI
gunicorn -k uvicorn.workers.UvicornWorker deployment.server:app --bind 0.0.0.0:8000 --workers 4
# Or run directly with Uvicorn:
uvicorn deployment.server:app --host 0.0.0.0 --port 8000 --workers 4Models are evaluated on:
- Precision - Minimize false positives
- Recall - Catch actual threats
- F1-Score - Balance precision and recall
- Confusion matrix (TP / FP / TN / FN) - summarizing the performance of the model
- PrecisionβRecall AUC (AUPRC) - measures the model's overall trade-off between Precision and Recall across all classification thresholds
See evaluation notebooks in model_training_and_evaluation/ for detailed metrics.
This project uses the CERT R4.2 Insider Threat Dataset:
- Realistic synthetic user behavior data
- Multiple insider threat scenarios
- Ground truth labels for evaluation
- Includes file access, email, and web browsing events
Note: The dataset is not included in this repository. Download from kilthub.cmu.edu and place in the dataset/ directory.
Key configuration files:
requirements.txt- Python dependenciesdeployment/requirements.txt- Deployment-specific dependencies- Model paths defined in inference scripts
The inference and deployment scripts expect trained model files at the following paths (relative to the repository root):
models/unsupervised/iforest_model.pklmodels/supervised/XGBClassifier_model.pkl
Place your trained model pickle files at these locations, or update the paths in deployment/server.py and inference/02_inference_hybrid_approach/hybrid_inference.py.
Note: Notebooks and training scripts use DuckDB files for faster access. For example, several notebooks expect dataset/supervised_dataset.duckdb and specific table names (e.g., labeled_training_data_new_approach). Use the scripts in data_transformation_and_preprocessing/02_load_to_duckdb/ and the preprocessing notebooks to generate the DuckDB files and tables before running the training notebooks.
All analysis and training uses Jupyter notebooks:
jupyter notebookNavigate to:
- Data preprocessing:
data_transformation_and_preprocessing/ - Model training:
model_training_and_evaluation/
- Update preprocessing pipeline in
data_transformation_and_preprocessing/ - Retrain models in
model_training_and_evaluation/ - Update inference pipeline in
inference/
- CERT Insider Threat Dataset: kilthub.cmu.edu
- SHAP Documentation: github.com/slundberg/shap
- XGBoost Documentation: xgboost.readthedocs.io
- Create a feature branch
- Make your changes
- Run tests and validation
- Submit a pull request
Check dataset/answers/license.txt for dataset licensing information.
This system is designed for research and organizational security purposes. Results should be validated by security experts before taking any action against users.
Last Updated: December 2025
For questions or issues, please contact the development team or open an issue on the repository.