Skip to content

Instantly share code, notes, and snippets.

@FrankSpierings
Last active June 4, 2019 10:01
Show Gist options
  • Save FrankSpierings/043be87aaf249a302e4cac9998d692f4 to your computer and use it in GitHub Desktop.
Save FrankSpierings/043be87aaf249a302e4cac9998d692f4 to your computer and use it in GitHub Desktop.
Create a bitmap file which can be used as a cmd/batch file
#!/usr/bin/python3
#
#Based on: https://www.thelacunablog.com/open-command-prompt-ms-paint.html
import struct
from PIL import Image
def imagegen(s, path):
# Fix header
s = '\x00\x00\x0a\x0d\x0a\x0d' + s
# Blocksize
bs = 3
# Calc padding
padnr = bs - (len(s) % bs)
# Pad
s = s + ('\x00' * padnr)
# Create equal size blocks
p = [s[i:i+bs] for i in range(0, len(s), bs)]
# Transform block items from string to bytes (RGB)
p = [struct.unpack('bbb', i[::-1]) for i in p]
# Create new image
img = Image.new('RGB', (len(p),1))
pixels = img.load()
# Walk the coordinates
for x in range(img.size[0]):
for y in range(img.size[1]):
# Set the pixels to the ones we created in blocks
pixels[x,y] = p[x]
filename = '{0}.bmp'.format(path)
print(filename)
img.save(filename)
imagegen('powershell -command "gci"', '/tmp/data/file3')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment