Skip to main content

Detect backdoors in Python, JS, PHP and TypeScript code

Project description

🛡️ bac_detect

PyPI version License: MIT Python Versions Build Status

A powerful tool for detecting potential backdoors and vulnerabilities in Python, JavaScript, PHP, and TypeScript source code

🚀 Installation🔍 Features📊 Usage Examples⚙️ Configuration👥 Contributing


📦 Installation

pip install bac_detect

🚀 Quick Start

# Scan a directory or single file
bac_detect path/to/your/code

# Enable extra Python checks via Pylint
bac_detect --use-pylint path/to/your/code

# Show only HIGH severity issues
bac_detect --min-severity high path/to/your/code

# Export results to JSON file
bac_detect --output-format json --output-file results.json path/to/your/code

# Use multiple CPU cores for faster scanning
bac_detect path/to/your/code  # Multi-threading is enabled by default

# Disable dependency checking
bac_detect --no-check-dependencies path/to/your/code

🔍 Features

  • Multi-language scanning: Analysis of .py, .js, .php, and .ts files
  • Combined approach:
    • Abstract Syntax Tree (AST) analysis using Bandit for Python and Esprima for JavaScript/TypeScript
    • Deep AST analysis for PHP code
    • Advanced regex-based scanning
    • Detection of suspicious constructs and potential backdoors
  • Machine Learning: Initial implementation of anomaly detection for identifying unusual code patterns
  • Smart classification: All found issues are categorized by severity levels (HIGH, MEDIUM, LOW)
  • Customizability: All detection rules can be configured in the patterns.json file
  • REST API: For integration with other security tools and CI/CD pipelines
  • Performance optimizations:
    • Multi-threaded scanning for faster processing
    • Selective file processing with ignore patterns
  • Obfuscated code detection: Identifies common obfuscation techniques across different languages
  • Dependencies security: Checks requirements.txt, package.json, and composer.json for known malicious packages
  • Flexible reporting: Export results to JSON format
  • CI/CD integration: Returns non-zero exit status when issues are detected

📊 Usage Examples

Basic Scanning

# Scan a directory recursively through all subdirectories
bac_detect /path/to/project

# Scan only specific file types
bac_detect --include "*.py,*.js" /path/to/project

# Exclude specific directories
bac_detect --exclude "tests/,vendor/" /path/to/project

Advanced Options

# Output results to JSON
bac_detect --output-format json --output-file results.json /path/to/project

# Set minimum severity level to display
bac_detect --min-severity medium /path/to/project

# Scan using a custom patterns file
bac_detect --patterns custom_patterns.json /path/to/project

# Use a custom ignore file
bac_detect --ignore-file .custom_ignore /path/to/project

# Disable multi-threading
bac_detect --no-multiprocessing /path/to/project

# Set maximum number of threads
bac_detect --max-workers 4 /path/to/project

REST API Usage (New in v1.3.0)

# Start the API server
bac_detect-api --port 8080

# From another terminal or application
curl -X POST http://localhost:8080/scan -F "file=@/path/to/file.py"

Machine Learning Integration (New in v1.3.0)

# Train the anomaly detection model on your codebase
bac_detect --train-model /path/to/clean/codebase

# Use the trained model for detection
bac_detect --use-ml /path/to/scan --model-path ./my_model.pkl

Output Example

[HIGH] Potential backdoor found: eval with dynamic content
        File: backend/utils.py, Line: 42
        Code: eval(request.params.get('cmd'))
        
[MEDIUM] Unsafe SQL query handling
        File: backend/models.py, Line: 78
        Code: cursor.execute("SELECT * FROM users WHERE id = " + user_id)
        
[LOW] Deprecated security function usage
        File: backend/auth.py, Line: 156
        Code: md5(password).hexdigest()

⚙️ Configuration

Detection Pattern Setup

All regex rules are located in the file:

bac_detect/patterns.json

You can edit existing or add new patterns:

{
  "python": {
    "high": [
      {
        "pattern": "eval\\s*\\(.*\\)",
        "description": "Dangerous use of eval()"
      },
      {
        "pattern": "os\\.system\\s*\\(.*\\$.*\\)",
        "description": "Shell command execution with external variables"
      }
    ],
    "medium": [
      ...
    ]
  },
  "javascript": {
    ...
  }
}

Ignoring Files and Patterns

Create a .bac_detectignore file in your project root:

# This is a comment
# Ignore specific patterns
pattern:eval_usage
pattern:base64_decode

# Ignore files/directories (regex format)
tests/.*
vendor/.*
.*\.min\.js$

Configuration File

You can also create a .bac_detectrc configuration file in your project root:

[DEFAULT]
exclude = tests/,docs/,vendor/
include = *.py,*.js,*.php,*.ts
min-severity = medium
use-pylint = true

👥 Contributing

  1. Fork this repository
  2. Create a feature branch:
    git checkout -b feature/my-new-feature
    
  3. Make your changes and commit:
    git add .
    git commit -m "Add support for XYZ"
    
  4. Push to your fork and open a Pull Request against the main branch

📄 License

This project is licensed under the MIT License. See the LICENSE file for details.

📍 Repository

👉 https://github.com/WaiperOK/bac_detect

Test Detect

A test package for demonstrating PyPI uploads.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bac_detect-1.4.0.tar.gz (35.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bac_detect-1.4.0-py3-none-any.whl (37.4 kB view details)

Uploaded Python 3

File details

Details for the file bac_detect-1.4.0.tar.gz.

File metadata

  • Download URL: bac_detect-1.4.0.tar.gz
  • Upload date:
  • Size: 35.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.4 Windows/11

File hashes

Hashes for bac_detect-1.4.0.tar.gz
Algorithm Hash digest
SHA256 4c6ba9713c75218ac7629ef736c83a12f8ab952a391651bafb7f252ce4b025be
MD5 76cb4528855e3452f325b6ac693b2c82
BLAKE2b-256 195b232d3e9e97740fab93898346291b5a143fba7fd741c3103401cf1f116f49

See more details on using hashes here.

File details

Details for the file bac_detect-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: bac_detect-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 37.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.4 Windows/11

File hashes

Hashes for bac_detect-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4929d1bcc6c285fa50d04c7a7faa653064536582f7d980ab28ae2caf15429bee
MD5 bba5bb51d26804c74ca9ecb7a3bf98bd
BLAKE2b-256 ae46227414cf621d1903289507faced1d8cc67e6ff3f231b29a9af0140155057

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page