GithubHelp home page GithubHelp logo

klezvirus / chameleon Goto Github PK

View Code? Open in Web Editor NEW
469.0 11.0 89.0 102 KB

PowerShell Script Obfuscator

License: MIT License

Python 100.00%
powershell obfuscation bypass-antivirus amsi-evasion amsi-bypass

chameleon's Introduction

Chameleon

Chameleon is yet another PowerShell obfuscation tool designed to bypass AMSI and commercial antivirus solutions.

Chameleon

Official Discord Channel (Inceptor)

Come hang out on Discord!

Inceptor

Overview

The tool has been developed as a Python port of the Chimera project, by tokioneon_. As such, it uses mostly the same techniques to evade common detection signatures, such as:

  • comment deletion/substitution
  • string substitution (variables, functions, data-types)
  • variable concatenation
  • indentation randomization
  • semi-random backticks insertion
  • case randomization
  • encoding

Why porting it

Chimera was indeed a shiny project, so why did I decide to port it to Python and why you should use chameleon? Well, there are several reasons why I decided to build Chameleon. I wrote a more detailed post about them here. I've also listed below the most important ones.

Reliability

As the author of Chimera states in the readme, the chimera script can successfully obfuscate scripts that the author tested personally, which are contained in the shells directory. However, the tool is not very reliable with other, untested, scripts. Quoting the author:

there's no telling how untested scripts will reproduce with Chimera...

This alone was a good reason to attempt to make the tool a bit more reliable, and also capable to obfuscate more complex scripts.

Speed

Chimera attempts several obfuscation steps, which usually requires the input to be read from a file, and stored back in a file again. While this is a safe approach, because each step is saved to disk (let's say there is an error at step n, we would still have the result of the obfuscation till n - 1), this is not really efficient. The overhead of writing and reading from a file at each time make the tool really slow when operating on large scripts (up to several minutes with the -a option).

Chameleon, instead, performs all obfuscation steps in memory, meaning it is extremely faster.

Portability

Chimera has been developed as a Bash Script, and heavily relies on common Linux utilities to accomplish the obfuscation.

Chameleon, on the other hand, is built with Python, meaning that you can use it wherever Python is installed.

Smart evasion checking

Chimera offers a function to submit scripts to VirusTotal directly. While this might be considered a useful utility, it will expose the obfuscated script to third party threat-intelligence, weakening the obfuscation engine.

To address this issue, Chameleon uses the utility AMSITrigger by RhytmStick, to check if the obfuscated result will indeed bypass AMSI.

Improvements

So far, we've talked about the efficiency and reliability issues of chimera, but what are the real improvements from an obfuscation standpoint? The techniques used by Chameleon are for the most the same as Chimera, with some improvements:

  • "Smart" variable scope identification (function local variables will be replaced "carefully" or left untouched)
  • Random backticks insertion (not just limited to a set of strings)
  • Random case switch (not just limited to a set of strings)
  • Supports an external obfuscation mapping for functions and parameters (TODO)
  • Additional Base64 Encoding wrapping

Chameleon manages to handle function and local parameters by implementing a very minimalist PowerShell "reader", which is capable of distinguish three contexts:

  • Global/Main Scope
  • In-Function Scope
    • Param() Blocks

The reader is still not a real parser, and relies on Dick Language to find relevant areas limits.

Usage

Using the tool is pretty straightforward, as observable from the help:

usage: chameleon.py [-h] [-l {0,1,2,3,4,5}] -o OUTPUT [-v] [-s] [-d] [-n] [-c] [-f] [-b] [--random-backticks] [-r] [-i] [-x] [-j] [-a] [--decimal] [--base64] [-z] [-F FUNCTION_MAPPING] [-K KEYWORDS] [-B BACKTICKS] [-t {r,d,h}] [--safe] [--verbose] [--about]
                    target

Chameleon - PowerShell script obfuscator (Improved Python port of Chimera)

positional arguments:
  target                Script to obfuscate

optional arguments:
  -h, --help            show this help message and exit
  -l {0,1,2,3,4,5}, --level {0,1,2,3,4,5}
                        String manipulation Level (1: MIN, 5: MAX, 0: RANDOM)
  -o OUTPUT, --output OUTPUT
                        Store the payload in a file
  -v, --variables       Enable variable obfuscation
  -s, --strings         Enable string obfuscation
  -d, --data-types      Enable data types obfuscation
  -n, --nishang         Enable Nishang scripts obfuscation
  -c, --comments        Enable comments obfuscation
  -f, --functions       Enable functions obfuscation
  -b, --use-backticks   Enable use of backticks with generated strings
  --random-backticks    Enable use of backticks randomization
  -r, --random-cases    Enable upper/lower randomization
  -i, --random-spaces   Enable indentation randomization
  -x, --hex-ip          Enable indentation randomization
  -j, --true-false-null
                        Try and obfuscate $true, $false and $null (experimental)
  -a, --enable-all      Enable all obfuscation types
  --decimal             Convert obfuscated payload to decimal format
  --base64              Convert obfuscated payload to base64 format
  -z, --check           Check the script against AMSI Trigger (@RythmStick, @rasta-mouse)
  -F FUNCTION_MAPPING, --function-mapping FUNCTION_MAPPING
                        Add custom keywords to obfuscate
  -K KEYWORDS, --keywords KEYWORDS
                        Add custom keywords to obfuscate
  -B BACKTICKS, --backticks BACKTICKS
                        Add a list of words to backtick
  -t {r,d,h}, --randomization-type {r,d,h}
                        Type of randomization (r: Random, d: Dictionary, h: Hybrid)
  --safe                Reduce obfuscation of certain variables
  --verbose             Enable verbose output
  --about               Shows additional information about the tool

Notes

Worth saying that, even if now Chameleon is capable of obfuscate also complex scripts, it's still not comparable with Invoke-Obfuscation, which actually is way more mature and is also backed-up by a fully fledged parser Management.Automation.Language.Parser.

Next steps

Moreover, Chameleon is still not perfect and still needs further development to increase both its accuracy and improve its obfuscation techniques. A non-exhaustive list of planned improvements are below:

  • Upgrade the PowerShell reader
  • Include other encoding schemes
  • Add more obfuscation methods

Contribute

If you want to contribute, just fork the repository. Any PR is well accepted.

Credits

Worth saying that Chameleon would not be a thing without the work of tokioneon_ on Chimera, as the most of the obfuscation process was ported from Bash to Python (of course with some mods).

References

chameleon's People

Contributors

klezvirus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

chameleon's Issues

Functions not obfuscating

Hi!
I am having trouble finding out as to why the functions are not obfuscating in my small script:

test1.ps1---------------
function runmefor-fun {
$variablenopurpose = "no purpose"
Write-Host $variablenopurpose
}
#commment
runmefor-fun
#hi


My command line is:
chameleon.py -l 1 -o OUTPUT.ps1 -v -c -f Test1.ps1

and my output file is this: --------------------------------

function runmefor-fun {
Write-Host "Hey!"
$pretendreallylongobfuscationishortenedfordisplay = "no purpose"
Write-Host $pretendreallylongobfuscationishortenedfordisplay

}
#commentthatislong
runmefor-fun
#commentthatislong

any ideas as to why this will not work?

Error using --base64 Option

Hey,
When I use --base64 to obfuscate a script the resulting script can not be executed.

The error is:

Get-Command : Die Benennung "Get-Command" wurde nicht als Name eines Cmdlet, einer
Funktion, einer Skriptdatei oder eines ausführbaren Programms erkannt. Überprüfen Sie die
Schreibweise des Namens, oder ob der Pfad korrekt ist (sofern enthalten), und wiederholen
Sie den Vorgang.
In Zeile:1 Zeichen:1

As you can see the highlighted section contains some invalid characters. Any reasons why this happens? Or am I doing something wrong?

Adding random lettering in front of a function declaration

My simple statement is: python chameleon.py -o OUTPUT.ps1 -v Test1.ps1 .
The only thing in Test1.ps1 is :
-----------------------------------------------start of file
function runmeforfun {
Write-Host "Hey!"
$variablenopurpose = "no purpose"
Write-Host $variablenopurpose
}
#single line comment
runmeforfun

-------------------------------------------------end of file

Unfortunately when I run the statement above I get this in the OUTPUT.ps1 file:

-------------------------------------------------start of file
function runmeforfun {
Write-Host "Hey!"
$obfuscatedrandomletteringnothereforpostingpurposes= "no purpose"
Write-Host $obfuscatedrandomletteringnothereforpostingpurposes
}

runmeforfun
-------------------------------------------------end of file

Notice the comment is completely gone and there is now random lettering in front of the function, making the ps1 file now with errors and will not run.

Upon further investigation it looks like the "zeroing out comments" section seems to be the problem

Let me know if I have done something wrong or if this is an error with the python code!! Thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.