bobbystacksmash / CMD-DeObfuscator

A Node.js module for deobfuscating and expanding DOS/BATCH commands.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CMD De-Obfuscator

ABOUT

Currently under active development

CMD DeObfuscator is a pure JavaScript library written to deobfuscate commands passed to CMD.EXE, presenting malicious commands mostly free of obfuscation characters.

USAGE

const CMD  = require("./index"),
      opts = { expand_inline: true };

// Strip escape sequences:
CMD.parse(`p^o^w^e^r^s^h^e^l^l`);
// => [ "powershell" ]

// Command clean-up:
CMD.parse(`w""sc"r"i"p"t e""vil.js`);
// => [ `"wscript" evil.js ]`

// Expand environment variables with substrings:
CMD.parse(`%comspec:~-16,1%%comspec:~-1%%comspec:~-13,1% foo=bar`, opts);
// => [ "Set foo=bar" ]

// Find/replace known values in environment variables:
CMD.parse(`%comspec:cmd=powershell%`)
// => C:\Windows\System32\powershell.exe

// Flattens nested 'CMD.EXE' instances:
CMD.parse(`cmd cmd cmd cmd calc.exe`);
// => [ "calc.exe" ]

// Handles delayed expansion within nested CMD contexts
CMD.parse(`cmd /V:O "set foo=bar& echo !foo!"`);
// => [ "set foo=bar", "echo bar" ]

API

CMD.parse(cmdstr, [options])

Returns: <string[]>

  • cmdstr <string> the command string to parse
  • options <Object>
    • delayed_expansion <bool> Default: false
    • expand_inline <bool> Default: false
    • vars <Object> Default: {}
CMD.parse(`p^ow^er""she"l"l`);
CMD.parse(`echo !hello!`, { delayed_expansion: true, vars: { hello: "world" } });
CMD.parse(`echo %hello%`, { expand_inline:     true, vars: { hello: "world" } });

Parses a given command string in to individual commands before applying variable expansion and de-obfuscation filters to each identified command. Returns an array of cleaned-up commands.

If delayed_expansion is set to true, the given cmdstr will be evaluated as if CMD.EXE had been started with /V:ON or SETLOCAL EnableDelayedExpansion, thus allowing !foo! to be expanded.

If expand_inline is set to true, environment variables are expanded each time a CMD.EXE command is identified, rather than only once at “parse time”. Useful when using the vars object.

The vars object maps environment variables to their values, for example:

CMD.parse("echo %foo%", { expand_inline: true, vars: { foo: "bar" }));
// => [ "echo bar" ]

EXAMPLES

Variable expansion

Attempts to expand all variables in to their expanded form, making analysis of the whole command clearer:

InputDeobfuscated Output
%COMSPEC%C:\Windows\System32\cmd.exe

Variable find/replace

Replaces all occurances of cmd inside the %COMSPEC% var with the string powershell:

InputDeobfuscated Output
%COMSPEC:cmd=powershell%C:\Windows\System32\powershell.exe

Variable substrings

Fetches the last seven characters within the %COMSPEC% var:

InputDeobfuscated Output
%COMSPEC:~-7%cmd.exe

Escape-sequence stripping

All escape characters (^) are stripped from the command:

InputDeobfuscated Output
CmD /C p^o^w^e^r^s^h^e^l^lCmD /C powershell

Empty string removal

All empty strings are removed from the command:

InputDeobfuscated Output
pow""ersh""ellpowershell

String widening

Obfuscation of a command can be achieved by excessive use of double-quotes, for example: w"s"c"r"i"p"t. The string widening algorithm merges quoted and non-quoted regions together:

InputDeobfuscated Output
w"s"c"r"i"p"t="wscript"=

Path resolver (coming soon)

Any identified paths are resolved in to their absolute form, meaning we transform this:

InputDeobfuscated Output
C:\foo\bar\baz\..\..\..\Windws\System32\cmd.exeC:\Windows\System32\cmd.exe

Reading Material

About

A Node.js module for deobfuscating and expanding DOS/BATCH commands.

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:JavaScript 96.8%Language:Lex 3.2%