Skip to main content

Project for identifying executables that have command-line options that can be obfuscated, possibly bypassing detection rules.

Project description

Windows Command-Line Obfuscation

Background

analyse_obfuscation is a python3 module for finding common command-line obfuscation techniques for a given program, as described in this blog post.

By providing one or more commands, analyse_obfuscation will test if the following obfuscation techniques can be applied:

  1. Option Char substitution

    e.g. ping -n 1 localhost == ping /n 1 localhost

  2. Character substitution

    e.g. reg eˣport HKCU out.reg == reg export HKCU out.reg

  3. Character insertion

    e.g. wevtutil gࢯli (…) == wevtutil gli (…)

  4. Quotes insertion

    e.g. netsh ad"vfi"rewall show (…) == netsh advfirewall show (…)

  5. Shorthands

    e.g. powershell /encod (…) == powershell /encodedcommand (…)

Goals

Note that the goal of this project is to show that a given executable/command line can be obfuscated, not to give a complete list of possible obfuscations for a given command. It should however be possible to derive different obfuscation opportunities from analyse_obfuscation's output.

Blue teamers 🔵 may want to use this tool, for example, to check if an executable they have written a detection rule is vulnerable to command-line obfuscation, meaning the rule should be improved or additional rules are needed. Note that in some cases this game is unwinnable - please take a look at the recommendations in the blog post for suggestions on how to tackle this.

Red teamers 🔴 may want to use this tool to find opportunities for bypassing simple detection rules.

Usage

Run

The simplest way to use this project is by running it (without installation).

  • Run script: clone the entire repository, install all dependencies (pip3 install -r requirements.txt) and run via:
    python3 -m analyse_obfuscation.run --help
    

Install

By installing the project, it will be possible to simply call analyse_obfuscation from the command line.

  • Via PyPI: install the application via for example pip:
    pip3 install analyse_obfuscation
    
  • From source: you can install a local version of the module by cloning the entire repository, followed by these commands:
    (note that this requires setuptools to be installed)
    python3 setup.py sdist bdist_wheel
    pip3 install dist/analyse_obfuscation-*-py3-none-any.whl --upgrade
    

Examples

(Screenshot)

Each execution generates a high-level result overview on the stdout, as can be seen in the screenshot. Additionally a .log file providing examples of commands found to be working is created. Sample report files generated by the below commands can be found in the sample_results/ folder.

# Check simple 'ping' command
analyse_obfuscation --command "ping /n 1 localhost"

# Check 'net share' command using {random}, which will be replaced by random string for each execution
analyse_obfuscation --command "net share x=c:\ /remark:{random}"

# Check 'powershell /encodedcommand' command with increased timeout, as executions tend to take long
analyse_obfuscation --command "powershell /encodedcommand ZQBjAGgAbwAgACIAQAB3AGkAZQB0AHoAZQAiAA==" --timeout 5

# Check 'systeminfo' command by only looking at the exit code, not the output - since every output will be different due to (changing) timestamps
analyse_obfuscation --command "systeminfo /s localhost" --timeout 5 --exit_code_only

# Check all commands as specified in sample.json, saving all reports in 'reports/'
analyse_obfuscation --json_file sample/sample.json --report_dir reports/

Note that the results may contain false positives - especially when single-character command-line options are being tested (such as /n in ping /n 1 localhost). In such cases, character insertion (method 3) may contain whitespace characters, which doesn't really 'count' as insertion character as whitespaces between command-line arguments are usually filtered out anyway. Similarly, character substitution (method 2) may change the entire option: e.g. ping /s 1 localhost and ping /r 1 localhost are functionally different, but happen to give the same output.

All options

All command-line options of this project can be requested by using the --help option:

usage: analyse_obfuscation [--threads n] [--verbose] [--report_dir c:\path\to\dir] [--log_file c:\path\to\file.log] [--help] [--command "proc /arg1 /arg2"] [--range {full,educated,ascii,custom}] [--custom_range 0x??..0x?? [0x??..0x?? ...]] [--char_offset n] [--post_command process_name] [--exit_code_only] [--timeout n] [--json_file c:\path\to\file.jsonl]

Tool for identifying executables that have command-line options that can be obfuscated.

required arguments (either is required):
  --command "proc /arg1 /arg2"
                        Single command to test
  --json_file c:\path\to\file.jsonl
                        Path to JSON file (JSON Line formatted) containing commands config

optional --command arguments:
  --range {full,educated,ascii,custom}
                        Character range to scan (default=educated)
  --custom_range 0x??..0x?? [0x??..0x?? ...]
                        Range to scan
  --char_offset n       Character position used for insertion and replacement
  --post_command process_name
                        Command to run unconditionally after each attempt (e.g. to clean up)
  --exit_code_only      Only base success on the exit code (and not the output of the command)
  --timeout n           Number of seconds per execution before timing out.

optional arguments:
  --threads n           Number of threads to use
  --verbose             Increase output verbosity
  --report_dir c:\path\to\dir
                        Path to save report files to
  --log_file c:\path\to\file.log
                        Path to save log to
  --help                Show this help message and exit

Repository Contents

Item Description
analyse_obfuscation/ Code for python3 module, enabling one to analyse executables for common command-line obfuscation techniques.
sample/ Sample config file to analyse built-in Windows executables, as well as related input files. Used to generate results in the above folder.
sample_results/ Report files generated using the JSONL file in the above sample folder.

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

analyse_obfuscation-1.0.0.tar.gz (25.8 kB view details)

Uploaded Source

Built Distribution

analyse_obfuscation-1.0.0-py3-none-any.whl (25.8 kB view details)

Uploaded Python 3

File details

Details for the file analyse_obfuscation-1.0.0.tar.gz.

File metadata

  • Download URL: analyse_obfuscation-1.0.0.tar.gz
  • Upload date:
  • Size: 25.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.10

File hashes

Hashes for analyse_obfuscation-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a6eeeb1f4c3faa60bb6bc189e353bd498ba0e2f0fcd2193884bfb74a0a095a08
MD5 88935206b7308953282bf275700f7b77
BLAKE2b-256 c6dcf42305bf815fa2d4cb762f1bedb70c67b33cd2d31da6bc9f759f8a043677

See more details on using hashes here.

File details

Details for the file analyse_obfuscation-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: analyse_obfuscation-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 25.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.10

File hashes

Hashes for analyse_obfuscation-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d215e7ed233d17c391e14738b3c610b0956ec424d808f0ee306757975aebe1b7
MD5 ceebd61518233520d1b5425aa173bf7a
BLAKE2b-256 2ce00c697028bde04e7b10d0e15301f2b8a238df2738c5531d03e07a0fba0473

See more details on using hashes here.

Supported by

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