So as you all know the rave for the last ~7 years has been hobbyist micro-controllers. If you have one that acts like a human interface device(i.e. a keyboard or mouse) then you can inject text into a modern operating system and make it do stuff(hands-free) ->
https://www.youtube.com/watch?v=RZiVwJG5HeE.
The purpose of my script is to encode a binary to a text format(base64) so that it can be used by another script later on. If you understand what is special about the Arduino Leonardo or the Teensy micro-controller boards, then you understand why you would want to do this. These particular micro-controllers can act like a keyboard & mouse.
Later on, the newly created script(created on the fly, using programmed keystrokes from the microcontroller) will write out and decode the encoded text from my script into bytes, write those bytes to a binary file, and run the file. For Windows, this would be Powershell or VBscript; for Linux, you can use whatever is available from the command line(I'm thinking Python b/c I know that it has Base64 decoding in it's standard lib).
The goal is to use a native scripting environment to decode and run the encoded binary provided by my script. Surprisingly, VBscript has native reading and writing of binary files; this is called an ADODB Stream object and this functionality can disabled in the registry. If the 'decode-and-run' script is made in Powershell, you would have to remove Powershell to mitigate this. Powershell does not use ADODB Stream objects but rather .NET class libraries(System.IO.*).
I do note that in the above video the guy is running the encoded script as such:
powershell.exe -noprofile -windowstyle hidden -encodedcommand <base64 powershell commands>
This suggests that he is in fact using an encoded powershell script that types out an already encoded binary, decodes it into bytes, writes it to a file, and runs it. I know the sequence of events is confusing but it is worth noting that there are a few different ways to accomplish this, some of which involve using the micro-controller as a hard disk or reading data from an attached sd card.
Note that '-encodedcommand' will not run base64 encoded binary files. Only base64 encoded powershell scripts. Anyways enough with the side tracking, here's my helper script.
#!/usr/bin/env python
##
### ascii-encode.py - ascii encode a binary file
##
#
import base64,sys
def banner():
print " ascii-encode.py - ascii encode a binary file"
print " usage: python ascii-encode.py <filename>"
print ""
def main():
if len(sys.argv) < 2:
banner()
exit()
filename = sys.argv[1]
try:
inFile = open(filename, "rb")
except IOError:
banner()
print "[!] Error: no such file or directory"
exit()
banner()
data = inFile.read()
inFile.close()
encoded = base64.b64encode(data)
print "[+] " + str(len(encoded)) + " bytes encoded"
print "[-] Writing file 'encoded.txt'.."
outFile = open("encoded.txt", "w")
outFile.write(encoded)
outFile.close()
print "[+] Done"
exit()
if __name__ == "__main__":
main()