Clicky

20160801

Embedded binaries in VBscript

As I wrote before, VBscript is boo, boo and should be banned from the earth.

However, you can still do a lot of productive work with it. E.g. answer a long standing question: can I embed binary code, in this example an icon file, in my VBscript?

Yes you can! This is the method:

Step 1: Create a Base64 file from the icon file with the Windows build-in CERTUTIL command:

C:\> CERTUTIL -encode icon.ico icon.b64

The result will be a Base64 encoded "certificate" file. The "certificate" headers are used by CERTUTIL to recognize the start and end of the Base64 code.

Step 2: Append the Base64 code to your VBscript. Each line must be REMmed out to let the script run correctly. In this case the VBscript standard `' ` is used. So the code would look something like this:

' -----BEGIN CERTIFICATE-----
' AAABAAYAEBAAAAAACABoBQAAZgAAACAgAAAAAAgAqAgAAM4FAAAwMAAAAAAIAKgO
' AAB2DgAAEBAAAAAAIABoBAAAHh0AACAgAAAAACAAqBAAAIYhAAAwMAAAAAAgAKgl
' AAAuMgAAKAAAABAAAAAgAAAAAQAIAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAOLv

' ..
' ..
' AAAAHwAA/AAAAAA/AAD+AAAAAH8AAP+AAAAA/wAA/8AAAAP/AAD/4AAAB/8AAP/4
' AAAf/wAA//4AAH//AAD//8AD//8AAA==
' -----END CERTIFICATE-----


Step 3: Now we have to add some code to extract and decode the Base64 data from the VBscript:

    dim fso : set fso=CreateObject("scripting.FileSystemObject")
    dim wsh : set wsh=CreateObject("wscript.shell")
   
    '--- Extract ICO file in the %TEMP% folder...
    iconFile=fso.GetSpecialFolder(2) & "\icon"
    set f=fso.OpenTextFile(WScript.ScriptFullName)
    s=replace(f.ReadAll,"' ","")
    f.close
    set f=fso.OpenTextFile(iconFile & ".tmp",2,TRUE)
    f.writeline(s)
    f.close
    wsh.run "certutil -decode " & iconFile & ".tmp" & " " & iconFile & ".ico",0,true


This code will read the VBscript, strips the REM code, writes the result in the TEMP folder and uses CERTUTIL to decode the Base64 data in the icon (binary) file in the TEMP folder:

C:\>dir %temp%\icon.ico
 Volume in drive C has no label.
 Volume Serial Number is 7AC3-9DD3

 Directory of C:\Users\thisuser\AppData\Local\Temp

08/01/2016  06:40 AM            22,486 icon.ico
               2 File(s)        
22,486 bytes
               0 Dir(s)  25,406,103,552 bytes free


Now you can piggyback any binary inside your VBscript. Enjoy! 





No comments :

Post a Comment

Real Time Web Analytics