Writing Binary Data in WSH - Writing binary files with FSO
(Page 4 of 4 )
I used the ADODB.Stream method of creating binary files for quite some time before stumbling across this method. It works effectively, but I wasn’t happy about writing every file twice to get the job done. The ADODB Stream object isn’t particularly inefficient, it just seemed as though I was doing twice as much work as I really should have had to. Enter the FileSystemObject.
The FileSystemObject is not designed to work with binary files. It’s doesn’t provide any binary support at all, as a matter of fact—and only one method of creating a file. Let’s look at the solution.
With CreateObject("Scripting.FileSystemObject").OpenTextFile(strPath, 2, True)
For x = 1 To 44 Step 2
.Write Chr(Clng("&h" & Mid("504B0506" & String(36, "0"),x,2)))
Next
.Close
End With
The code really isn’t all that difficult. The secret is in the use of the OpenTextFile method. The first and second parameters instruct the FileSystemObject to open a text file For Writing. The optional third parameter is a Boolean value that tells the FileSystemObject if the file should be created if it doesn’t exist, and that works perfectly for us.
When the FileSystemObject creates a file, it creates a completely empty file. We can write our binary data directly to it without any problems. We just need to determine how to do that.
I’ve wrapped this code in a For Next loop to simplify it a bit. The secret is in the functions I’m using inside of that loop. I’m starting with my binary information in a string. I move through the string, grabbing those characters two at a time. (Each byte has two hex characters).
I then add a “&h” in front of those two characters. That’s VBScript’s way of denoting a hexadecimal value. Next, we exploit VBScript’s lack of strict data types. Because this string looks like a hexadecimal number, VBS has no qualms when we instruct it to read the string value as a number with the Clng() function. The Clng function converts a value into a numeric value of type Long. This data type is wide enough to house our extremely large decimal values without losing any information.
We have to convert these hex values into decimal values because the Chr function only accepts decimal numbers. As you may recall, it takes a decimal number and returns the ASCII character represented by that decimal. It doesn’t care if the character is actually a non-printable character. That allows use of the FileSystemObject object to write the characters directly to the file without any problems. Closing the File object saves the newly-written binary data exactly the way we intended.
There you have it. Two exploits for writing binary data to files with a language that doesn’t support it, in an environment that won’t allow it. Beware when using file streams, as is it very easy to run into memory issues if you attempt to read or write files that are too large. It’s best to take them in chunks rather than attempting the file as a whole. I hope you have fun with this one. Until next time, keep coding!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |