Exemplo n.º 1
0
def decode(openPath, mapSeed, password="", zeroTerm=True, file=None):
	"""decode data from an text file using homoglyphs

	Args:
		openPath (string): path to the stego-text file to decode
		mapSeed (string): seed to generate the lsb map
		password (str, optional): password to encrypt the data with. Defaults to "".
		zeroTerm (boolean, optional): stop decoding on \x00 (NUL). Defaults to True.
		file (<file>, optional): file pointer. Defaults to None.

	Returns:
		bytes: data from the text file
	"""
	with open(openPath, encoding="utf-8") as openData:
		fileData = openData.read()
	position = 0
	data = []
	decodeMap = getMap(fileData, mapSeed)
	for _char in fileData:
		byte = 0
		shift = 0
		while shift < 8:
			if decodeMap[position] > 0:
				byte, shift = decodeGlyph(fileData, position, byte, shift)
			position += 1
		if byte == 0 and zeroTerm:
			break
		data.append(byte)
	result = otp(bytes(data), password, False)
	return toFile(result, file) if file else result
Exemplo n.º 2
0
    def decode(self, mapSeed, password="", zeroTerm=True, file=None):
        """decode data from an array using lsb steganography

		Args:
		mapSeed (string): seed to generate the lsb map
		password (str, optional): password to encrypt the data with. Defaults to "".
		zeroTerm (boolean, optional): stop decoding on \x00 (NUL). Defaults to True.
		file (<file>, optional): file pointer. Defaults to None.

		Returns:
		bytes: data from the image
		"""
        lsbMap = getMap(self.array, mapSeed)
        data = []
        while self.pointer in range(self.arrayLen):
            byte = 0
            shift = 0
            while shift < 8:
                if lsbMap[self.pointer] > 0:
                    bit = self.getLsb()  # Little endian
                    byte += bit << shift
                    shift += 1
                else:
                    self.pointer += 1  # Increment pointer anyway
            if byte == 0 and zeroTerm:
                break
            data.append(byte)
        result = otp(bytes(data), password, False)
        return toFile(result, file) if file else result
Exemplo n.º 3
0
def encode(openPath, writePath, data, mapSeed, password=""):
	"""encode a text file with data using homoglyphs

	Args:
		openPath (string): path to the original text file to open
		writePath (string): path to write the stego-text file
		data (string): data to encode
		mapSeed (string): seed to generate the lsb map
		password (str, optional): password to encrypt the data with. Defaults to "".
	"""
	with open(openPath, encoding="utf-8") as openData:
		fileData = openData.read()
	position = 0
	output = []
	data = otp(toBin(data), password) + b"\x00"
	encodeMap = getMap(fileData, mapSeed)
	systemRandom = SystemRandom()
	for char in data:
		shift = 0
		while shift < 8:
			if encodeMap[position] > 0:
				result, shift = encodeGlyph(fileData, position, char, shift)
			else:
				result, _shift = encodeGlyph(fileData, position,
				systemRandom.randint(0, 1) << shift, shift)
			output.append(result)
			position += 1
	output.append(fileData[position:])
	with open(writePath, "w", encoding="utf-8") as writeData:
		writeData.write("".join(output))
Exemplo n.º 4
0
def encode(openPath, writePath, data, mapSeed, password="", safe=True):
    """encode a text file with data using zero width chars

	Args:
		openPath (string): path to the original text file to open
		writePath (string): path to write the stego-text file
		data (string|bytes|<file>): data to encode
		mapSeed (string): seed to generate the lsb map
		password (str, optional): password to encrypt the data with. Defaults to "".
		safe (boolean, optional): use a reduced set of chars to show in fewer
		editors. Defaults to True.
	"""
    with open(openPath, "rb") as openData:
        fileData = openData.read()
    position = 0
    pointer = 0
    zwcMap = getMap(fileData, mapSeed)
    encodeData = otp(toBin(data), password) + b"\x00"
    while pointer < len(encodeData):
        if zwcMap[position] > 0:
            position, fileData = encodeCharZero(fileData, position,
                                                encodeData[pointer], safe)
            pointer += 1
        else:
            position += getUtf8Size(fileData,
                                    position)[0]  # increment by char size
    with open(writePath, "wb") as writeData:
        writeData.write(fileData)
Exemplo n.º 5
0
def encode(openPath, writePath, appendData, password=""):
    """encode a file with data by appending binary after the end of the file

	Args:
		openPath (string): path to the original file to open
		writePath (string): path to write the stego-file
		appendData (string|bytes|<file>): data to encode
		password (str, optional): password to encrypt the data with. Defaults to "".
	"""
    data, fileExt = openFile(openPath)
    imageWriteData = data[:data.find(endKeys[fileExt]) + len(endKeys[fileExt])]
    writeFile(writePath, imageWriteData + otp(toBin(appendData), password))
Exemplo n.º 6
0
def decodeFile(openPath, password="", filePointer=None):
    """ decode data as a file """
    # Look for "/*.[* not .xml]" or "/application.xml"
    with ZipFile(openPath, "r", compression=ZIP_DEFLATED) as zipFile:
        files = []
        for file in zipFile.namelist():
            if not file.endswith(
                (".xml", "mimetype")) or file.endswith("application.xml"):
                files.append(file)
        with zipFile.open(files[0], "r") as dataFile:
            data = otp(dataFile.read(), password, False)
        return toFile(data, filePointer) if filePointer else data
Exemplo n.º 7
0
def decode(openPath, password="", file=None):
    """decode data from a file by extracting data after end of file

	Args:
		openPath (string): path to the stego-file to decode
		password (str, optional): password to encrypt the data with. Defaults to "".
		file (<file>, optional): file pointer. Defaults to None.

	Returns:
		bytes: data from the image
	"""
    """ decode an image with data """
    data, fileExt = openFile(openPath)
    readData = data[data.find(endKeys[fileExt]) + len(endKeys[fileExt]):]
    result = otp(readData, password, False)
    return toFile(result, file) if file else result
Exemplo n.º 8
0
    def encode(self, mapSeed, password=""):
        """encode an array with data using lsb steganography

		Args:
		mapSeed (string): seed to generate the lsb map
		password (str, optional): password to encrypt the data with. Defaults to "".
		"""
        data = otp(self.data, password) + b"\x00"
        lsbMap = getMap(self.array, mapSeed)
        systemRandom = SystemRandom()
        for char in data:
            shift = 0
            while shift < 8:
                if lsbMap[self.pointer] > 0:
                    self.setLsb(char >> shift & 1)
                    shift += 1
                else:
                    self.setLsb(systemRandom.randint(0, 1))
        return self.array
Exemplo n.º 9
0
def decodeFile(openPath, password="", filePointer=None):
    """decode data from a microsoft office file by extracting the file

	Args:
		openPath (string): path to the stego-document to decode
		password (str, optional): password to encrypt the data with. Defaults to "".
		filePointer (<file>, optional): pointer to the file. Defaults to None.

	Returns:
		bytes: data from the image
	"""
    # Look for "/docProps/*.[* not .xml]" or "/docProps/application.xml"
    with ZipFile(openPath, "r", compression=ZIP_DEFLATED) as zipFile:
        files = []
        for file in zipFile.namelist():
            if file.startswith("docProps") and (not file.endswith(
                (".xml", ".rels")) or file.endswith("application.xml")):
                files.append(file)
        with zipFile.open(files[0], "r") as dataFile:
            data = otp(dataFile.read(), password, False)
        return toFile(data, filePointer) if filePointer else data
Exemplo n.º 10
0
def encodeFile(openPath,
               writePath,
               file,
               fileName="application.xml",
               password=""):
    """ encode data as a file """
    # Add one of the following:
    # <Override PartName="/docProps/<file>" ContentType="application/octet-stream"/>
    # <Override PartName="/docProps/application.xml"
    # ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>
    copyfile(openPath, writePath)
    with MutableZipFile(writePath, "a", compression=ZIP_DEFLATED) as zipFile:
        zipFile.writestr("docProps/" + fileName, otp(toBin(file), password))
        with zipFile.open("[Content_Types].xml", "r") as xmlFile:
            lines = [line.strip() for line in xmlFile.readlines()]
        lines[1] = lines[1].replace(
            b"</Types>", b"<Override PartName=\"/docProps/" +
            fileName.encode("utf-8") + b"\" ContentType=\"application/" +
            (b"vnd.openxmlformats-officedocument.extended-properties+xml"
             if fileName == "application.xml" else b"octet-stream") +
            b"\"/></Types>")
        zipFile.writestr("[Content_Types].xml", b"\n".join(lines))
Exemplo n.º 11
0
def encodeFile(openPath,
               writePath,
               file,
               fileName="application.xml",
               password=""):
    """ encode data as a file """
    # Add one of the following: <manifest:manifest></manifest:manifest>
    # <manifest:file-entry manifest:full-path="<file>" manifest:media-type="application/octet-stream"/>
    # <manifest:file-entry manifest:full-path="/application.xml"
    # manifest:media-type="text/xml"/>
    copyfile(openPath, writePath)
    with MutableZipFile(writePath, "a", compression=ZIP_DEFLATED) as zipFile:
        zipFile.writestr(fileName, otp(toBin(file), password))
        with zipFile.open("META-INF/manifest.xml", "r") as xmlFile:
            lines = [line.strip() for line in xmlFile.readlines()]
        lines[1] = lines[1].replace(
            b"</manifest:manifest>",
            b"<manifest:file-entry manifest:full-path=\"" +
            fileName.encode("utf-8") + b"\" manifest:media-type=\"" +
            (b"text/xml" if fileName == "application.xml" else
             b"application/octet-stream") + b"\"/></manifest:manifest>")
        zipFile.writestr("META-INF/manifest.xml", b"\n".join(lines))
Exemplo n.º 12
0
def decode(openPath,
           mapSeed,
           password="",
           zeroTerm=True,
           file=None,
           safe=True):
    """decode data from a text file using zero width chars

	Args:
		openPath (string): path to the stego-text file to decode
		mapSeed (string): seed to generate the lsb map
		password (str, optional): password to encrypt the data with. Defaults to "".
		zeroTerm (boolean, optional): stop decoding on \x00 (NUL). Defaults to True.
		file (<file>, optional): file pointer. Defaults to None.
		safe (boolean, optional): use a reduced set of chars to show in fewer
		editors. Defaults to True.

	Returns:
		bytes: data from the text file
	"""
    with open(openPath, "rb") as openData:
        fileData = openData.read()
    position = 0
    data = []
    zwcMap = getMap(fileData, mapSeed)
    for _char in fileData:
        if zwcMap[position] > 0:
            position, byte = decodeCharZero(fileData, position, safe)
            if byte == 0 and zeroTerm:
                break
            data.append(byte)
        else:
            position += getUtf8Size(fileData,
                                    position)[0]  # increment by char size
    result = otp(bytes(data), password, False)
    return toFile(result, file) if file else result