I have done a ton more research since I first posted this question and I think I had a few terms goofed up as well.
Dilemma: My company's Information Security team has flagged certutil.exe
as a potentially dangerous application to be used after a recent phishing attack. This really stinks because we LOVE certutil.exe
for the lightning fast and dead-nuts accurate conversion of hex files into ascii files. These ascii files have to be converted exactly as certutil.exe -decodehex
performs in order to be parsed by another program to read and interpret various pieces of data produced by a separate in-house program.
I have a PowerShell
script which seems to convert Hex
to ASCII
very accurately, BUT, it never seems to "finish" as though maybe the while loop is dorked up. Additionally, because of how the stream is is broken down, there are far too many line-breaks in the resulting file. The files are typically 7 MB a piece, roughly 7-8 billion characters long, and roughly 11 line breaks.
The script I mention is below and is an adaptation of the work presented in this link. Instead of converting the data stream into a Hex
representation of the Hex
data, I convert it to ASCII
.
$bufferSize = 65536
$ASCIIFile = "C:\FooBar.dat"
$stream = [System.IO.File]::OpenRead(
"C:\FixedOutput.dat")
while ( $stream.Position -lt $stream.Length ) {
#BEGIN CALLOUT A
$buffer = new-object Byte[] $bufferSize
$bytesRead = $stream.Read($buffer, 0, $bufferSize)
#END CALLOUT A
for ( $line = 0; $line -lt [Math]::Floor($bytesRead /
16); $line++ ) {
$slice = $buffer[($line * 16)..(($line * 16) + 15)]
$bytes=[System.Text.Encoding]::ASCII.GetString($slice)
$asc = -join($bytes-split"(..)"|?{$_}|%{[char][convert]::ToByte($_,16)})
$asc | Write-Host >> $ASCIIFile -NoNewline
}
#BEGIN CALLOUT B
if ( $bytesRead % 16 -ne 0 ) {
$slice = $buffer[($line * 16)..($bytesRead - 1)]
$output = ""
foreach ( $byte in $slice ) {
$output=[System.Text.Encoding]::ASCII.GetString($byte)
}
$output | ADD-Content $Asciifile
#END CALLOUT B
}
}
$stream.Close()
Additionally, I had adapted the PowerShell
code from this S.O. article in addition to the presumably previous duplicate question and answer. The problem with this set of code, is the output still took 15 minutes or so, but the output is not identical to certutil.exe -decodehex
so the information cannot be parsed by our in-house program!
Additionally, I can literally copy and paste the hex data from the original file, paste it into a hex editor and then save the output as a new file to get what I need.
Problem is, we often have 30 - 40 of these files at once and we need a lightning fast solution. . .
I've looked for VB.net
(my 2nd most familiar language) solutions, but they are comparable in methods as the PowerShell
methods I have found, and nothing adequately takes an entire file and puts it to ASCII
with relative ease or Accuracy.
UPDATE:
In addition to re-formatting the question, I have also put to test the very well-detailed answer below from TheMadTechnician and this brought magnificently glorious tears to my eyes. If I could reach through the silicon and kiss you, I probably would. TEXTBOOK MATCH. LIGHTNING FAST. BEAUTIFUL.
Now. . . . let us hope my I.S. Dept. doesn't flag this methodology as well and make noise about it. . .
I modified the -join
statement since I'm concatenating the files prior to calling PowerShell
from within a Batch
script, but this would work beautifully within PowerShell
as well.
Lastly, since our I.S. Dept. restricts the usage of .ps1
scripts, a while back, I found an awesome option to embed complex commands as Base64 strings instead and then calling this using Start /MIN powershell -encodedcommand _insertEncodedCommandHere_
Again - I cannot thank you enough! If I ever get a working method utilizing this same crypt32.dll
library via VB.Net, I'll come back and post it as an answer as well, but YOU have won the prize!