May 24, 2018

PowerShell :: Split a long string

PowerShell ISE logo
(Last Updated On: 14th September 2016)

Hi,

When I was writing some VBA for a macro malware project, I soon realised that there was a limit on the length of a string declaration in VBA which meant I had to break up a large Base64 string into multiple concatenations.  This PowerShell scripts automates that process and could be easily modified for other languages as it simply reads each character of a file, discards any formatting characters and then outputs a fixed length line to the output file.  You will need to define the $inputFilePath and $outputFilePath.

$inputFilePath = "C:\Base64.txt"
$outputFilePath = "C:\Output.txt"

$variableName = "StringVariable"
$lineSize = 200

$encodedCommand = [IO.File]::ReadAllText($inputFilePath)
$y = 0
$formattingCharacters = "`n","`r","`t"

write-output '' > $outputFilePath
while($y -lt $encodedCommand.Length)
{
$x=0;
[string]$buffer='';
while($x -lt $lineSize)
{
if ($encodedCommand[$y] -notin $formattingCharacters)
{
$buffer=$buffer+$encodedcommand[$y]
$x++
}
$y++
}
$line = "$variableName = $variableName & `""+$buffer+"`""
write-output $line >> $outputFilePath
}

Hope you find it useful,

Simon

Previous «
Next »

Simon is a sysadmin for Local Government in North Yorkshire with a real passion for security and coding.

1 Comment

  1. Pingback: an intro to macro malware – Synack

Leave a Reply

Subscribe to SYNACK via Email

%d bloggers like this: