class PixelScraper: """ Scrapes the pixel data back out of the file. """ def __init__(self, path: str): self._pixelManipulator = PixelManipulator(path) def get_header(self, length: int=128) -> (int, bytearray): pixgen = self._pixelManipulator.get_pixel_generator() cleartext = bytearray() for index, pixel in enumerate(pixgen): if index == length: break charvalue = 0 for i in reversed(range(4)): charvalue <<= 2 charvalue += pixel[i] & 3 cleartext.append(charvalue) size = unpack("<I", cleartext[0:4])[0] filename = cleartext[4:].strip(b'\0') return size, filename def get_data(self): size, filename = self.get_header() pixgen = self._pixelManipulator.get_pixel_generator() # skip the header for i in range(128): next(pixgen) cleartext = bytearray() for index, pixel in enumerate(pixgen): if index == size: break charvalue = 0 for i in reversed(range(4)): charvalue <<= 2 charvalue += pixel[i] & 3 cleartext.append(charvalue) return cleartext
def inject_file(imagepath: str, secretpath: str, outputpath: str): secretfilename = basename(secretpath).encode() with open(secretpath, "rb") as f: secretdata = f.read() header = bytearray(b"\0" * 128) header[0:4] = pack("<I", len(secretdata)) header[4:4 + len(secretfilename)] = secretfilename header = header[:128] # cap anything that's too long pixelmanipulator = PixelManipulator(imagepath) maxfilesize = (pixelmanipulator.get_maximum_size() - 128) if maxfilesize < len(secretdata): print( "The target container is too small for the secret. Maximum file size is", maxfilesize, "bytes") sys.exit(1) bitfactory = BitFactory(secretdata) pixelmanipulator.apply_bits(header, bitfactory) pixelmanipulator.save(outputpath)
def inject_file(imagepath: str, secretpath: str, outputpath: str): secretfilename = basename(secretpath).encode() with open(secretpath, "rb") as f: secretdata = f.read() header = bytearray(b"\0" * 128) header[0:4] = pack("<I", len(secretdata)) header[4:4 + len(secretfilename)] = secretfilename header = header[:128] # cap anything that's too long pixelmanipulator = PixelManipulator(imagepath) maxfilesize = (pixelmanipulator.get_maximum_size() - 128) if maxfilesize < len(secretdata): print("The target container is too small for the secret. Maximum file size is", maxfilesize, "bytes") sys.exit(1) bitfactory = BitFactory(secretdata) pixelmanipulator.apply_bits(header, bitfactory) pixelmanipulator.save(outputpath)
def __init__(self, path: str): self._pixelManipulator = PixelManipulator(path)
def get_maximum_size(imagepath: str) -> int: return PixelManipulator(imagepath).get_maximum_size()