danielbohannon / Invoke-Obfuscation

PowerShell Obfuscator
Apache License 2.0
3.7k stars 765 forks source link

Obfuscated code broken #57

Closed jamarir closed 3 years ago

jamarir commented 3 years ago

Here is a PowerShell version of Invoke-Mimikatz: https://raw.githubusercontent.com/BC-SECURITY/Empire/master/empire/server/data/module_source/credentials/Invoke-Mimikatz.ps1

Launching it normally is OK (as admin and without antiviruses):

Import-Module .\Invoke-Mimikatz.ps1
Invoke-Mimikatz -Command "sekurlsa::logonPasswords"
    Hostname: *** / -

      .#####.   mimikatz 2.2.0 (x64) #19041 Oct  4 2020 10:28:51
     .## ^ ##.  "A La Vie, A L'Amour" - (oe.eo)
     ## / \ ##  /*** Benjamin DELPY `gentilkiwi` ( benjamin@gentilkiwi.com )
     ## \ / ##       > https://blog.gentilkiwi.com/mimikatz
     '## v ##'       Vincent LE TOUX             ( vincent.letoux@gmail.com )
      '#####'        > https://pingcastle.com / https://mysmartlogon.com ***/

But when I try to obfuscate it using TOKEN\ARGUMENT\1 :

Import-Module -Global -Force ..\..\Invoke-Obfuscation\Invoke-Obfuscation.psd1
Invoke-Obfuscation -ScriptPath .\Invoke-Mimikatz.ps1 -command "TOKEN\\ARGUMENT\\2" -Quiet |Out-File -encoding ascii obfuscated.ps1
Import-Module -Global -Force .\obfuscated.ps1
Invoke-Mimikatz -Command "sekurlsa::logonPasswords"

I always have the following error

Exception lors de l'appel de « GetDelegateForFunctionPointer » avec « 2 » argument(s) : « La valeur ne peut pas être null.
Nom du paramètre : ptr »
Au caractère obfuscated.ps1:489 : 9
+         $VirtualAlloc = [System.Runtime.InteropServices.Marshal]::Get ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException

Exception lors de l'appel de « GetDelegateForFunctionPointer » avec « 2 » argument(s) : « La valeur ne peut pas être null.
Nom du paramètre : ptr »
Au caractère obfuscated.ps1:494 : 9
+         $VirtualAllocEx = [System.Runtime.InteropServices.Marshal]::G ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException

Exception lors de l'appel de « GetDelegateForFunctionPointer » avec « 2 » argument(s) : « La valeur ne peut pas être null.
Nom du paramètre : ptr »
Au caractère obfuscated.ps1:499 : 9
+         $memcpy = [System.Runtime.InteropServices.Marshal]::GetDelega ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentNullException
(...)

Also, an error sometimes happens with TOKEN\STRING\1 or TOKEN\STRING\2.

That's the same with any AST\*\1 (but here, the code can't be ofuscated)

Import-Module -Global -Force .\Invoke-Obfuscation\Invoke-Obfuscation.psd1
Invoke-Obfuscation -ScriptPath .\Invoke-Mimikatz.ps1 -command "AST\\ALL\\1" -Quiet |Out-File -encoding ascii obfuscated.ps1
Import-Module -Global -Force .\obfuscated.ps1
Invoke-Mimikatz -Command "sekurlsa::logonPasswords"

For example, when chosing AST\ALL\1 :

Can't find process 0
Au caractère obfuscated.ps1:2540 : 17
+                 Throw "Can't find process $ProcName"
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (Can't find process 0:String) [], RuntimeException
    + FullyQualifiedErrorId : Can't find process 0

Or AST\HashtableAst\1

Exception lors de l'appel de « Substring » avec « 2 » argument(s) : « La longueur ne peut pas être inférieure à zéro.
Nom du paramètre : length »
Au caractère Invoke-Obfuscation\Out-ObfuscatedAst.ps1:5738 : 17
+ ...             $ObfuscatedExtent = [String] $ObfuscatedExtent.Substring( ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException

Exception lors de l'appel de « Substring » avec « 1 » argument(s) : « StartIndex ne peut pas être inférieur à zéro.
Nom du paramètre : startIndex »
Au caractère Invoke-Obfuscation\Out-ObfuscatedAst.ps1:5745 : 17
+ ...             $ObfuscatedExtent += [String] $AbstractSyntaxTree.Extent. ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException

Exception lors de l'appel de « Substring » avec « 2 » argument(s) : « La longueur ne peut pas être inférieure à zéro.
Nom du paramètre : length »
Au caractère Invoke-Obfuscation\Out-ObfuscatedAst.ps1:5738 : 17
+ ...             $ObfuscatedExtent = [String] $ObfuscatedExtent.Substring( ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException

Exception lors de l'appel de « Substring » avec « 1 » argument(s) : « StartIndex ne peut pas être inférieur à zéro.
Nom du paramètre : startIndex »
Au caractère Invoke-Obfuscation\Out-ObfuscatedAst.ps1:5745 : 17
+ ...             $ObfuscatedExtent += [String] $AbstractSyntaxTree.Extent. ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException

Or AST\CommandAst\1 :

Exception lors de l'appel de « Substring » avec « 2 » argument(s) : « La longueur ne peut pas être inférieure à zéro.
Nom du paramètre : length »
Au caractère Invoke-Obfuscation\Out-ObfuscatedAst.ps1:5738 : 17
+ ...             $ObfuscatedExtent = [String] $ObfuscatedExtent.Substring( ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException

Exception lors de l'appel de « Substring » avec « 1 » argument(s) : « StartIndex ne peut pas être inférieur à zéro.
Nom du paramètre : startIndex »
Au caractère Invoke-Obfuscation\Out-ObfuscatedAst.ps1:5745 : 17
+ ...             $ObfuscatedExtent += [String] $AbstractSyntaxTree.Extent. ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException

Exception lors de l'appel de « Substring » avec « 2 » argument(s) : « La longueur ne peut pas être inférieure à zéro.
Nom du paramètre : length »
Au caractère Invoke-Obfuscation\Out-ObfuscatedAst.ps1:5738 : 17
+ ...             $ObfuscatedExtent = [String] $ObfuscatedExtent.Substring( ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException

Exception lors de l'appel de « Substring » avec « 1 » argument(s) : « StartIndex ne peut pas être inférieur à zéro.
Nom du paramètre : startIndex »
Au caractère Invoke-Obfuscation\Out-ObfuscatedAst.ps1:5745 : 17
+ ...             $ObfuscatedExtent += [String] $AbstractSyntaxTree.Extent. ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException

Therefore, I suppose AST is not supported by this version of Invoke-Mimikatz.ps1. But do you have any idea why TOKEN\STRING\1 and TOKEN\STRING\2 may sometimes break Invoke-Mimikatz.ps1, while TOKEN\ARGUMENT\1 breaks it everytime ?

Is there any table referencing these incompatibilities, or is it specific to the script we try to obfuscate ?