Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic steganography support #41

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 108 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,109 @@
.idea
__pycache__
*.pyc
*.egg-info
### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio

*.iml

## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
# .idea/gradle.xml
# .idea/libraries

# Mongo Explorer plugin:
# .idea/mongoSettings.xml

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties

.gitignore

115 changes: 85 additions & 30 deletions mimic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# coding=utf-8
from __future__ import print_function

from collections import namedtuple
from itertools import chain
from random import random, randrange
from sys import version_info

from mimic.steganography import Steganography

Hgs = namedtuple('Hgs', ('ascii', 'fwd', 'rev'))

if version_info >= (3,):
unichr = chr
unicode = lambda s, e: s
Expand All @@ -11,6 +19,9 @@
# Surrounding field for printing clarity
field = u'\u2591'

# source file
FILE = None

# List of all homoglyphs - named tuples with 'ascii' char, 'fwd' alternatives string for forward mimic mode, and 'rev'
# string of potentially non-universally-printable chars that should still be able to check or reverse back to ASCII
all_hgs = []
Expand All @@ -35,9 +46,6 @@ def fill_homoglyphs():
If a character is deemed unprintable on some systems, don't delete it - move it from the fwd string to rev.
"""

from collections import namedtuple
Hgs = namedtuple('Hgs', ('ascii', 'fwd', 'rev'))

all_hgs.extend(Hgs(*t) for t in (
(' ', u'\u00A0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F', u'\u3000'),
('!', u'\uFF01\u01C3\u2D51\uFE15\uFE57', u'\u119D'),
Expand Down Expand Up @@ -158,14 +166,6 @@ def get_writer():
return getwriter(stdout.encoding or 'utf-8')(stdout)


def read_line():
from sys import stdin

if version_info >= (3,):
return input()
return raw_input().decode(stdin.encoding or 'utf-8')


def listing():
"""
Show a list of all known homoglyphs
Expand Down Expand Up @@ -265,11 +265,13 @@ def search():
break


def pipe(replace):
def pipe(read_line, replace, stego):
"""
Pipe from input to output
End with ctrl+C or EOF
:param read_line: A function which returns the next line of input
:param replace: A function to replace each char
:param stego: A StegoHelper instance to manage the steganography
"""

out = get_writer()
Expand All @@ -281,24 +283,37 @@ def pipe(replace):
line = read_line()
except EOFError:
return
if line == '':
return
for c in line:
out.write(replace(c))
out.write('\n')
if isinstance(c, int):
c = chr(c)
replacement = replace(c, stego)
out.write(replacement)

if not line.endswith("\n"):
out.write('\n')

def pipe_mimic(hardness):

def pipe_mimic(read_line, hardness, stego):
"""
Pipe from input to output, replacing chars with homoglyphs
:param read_line: function to procide the next line of text to mimick
:param hardness: Percent probability to replace a char
:param stego: Steganography module to encode data in the mimicking
"""
from itertools import chain
from random import random, randrange

def replace(c):
def replace(c, s):
if isinstance(c, int):
c = chr(c)
if random() > hardness / 100. or c not in hg_index:
return c
hms = hg_index[c]

# If there is a stego object, use that to choose the next character
if s:
return s.stego_encode(hms)

# hms contains the current character. We've already decided, above, that this character should be replaced, so
# we need to try and avoid that. Loop through starting at a random index.
fwd = hms.ascii + hms.fwd
Expand All @@ -308,20 +323,22 @@ def replace(c):
return fwd[index]
return c

pipe(replace)
pipe(read_line, replace, stego)


def replace_reverse(c):
def replace_reverse(c, stego):
"""
Undo the damage to c
"""
hgs = hg_index.get(c)
if hgs:
if stego:
stego.stego_decode(c, hgs)
return hgs.ascii
return c


def replace_check(c):
def replace_check(c, stego):
"""
Replace non-ASCII chars with their code point
"""
Expand Down Expand Up @@ -353,6 +370,8 @@ def parse():
help="show a char's homoglyphs")
parser.add_option('-l', '--list', action='store_true',
help='show all homoglyphs')
parser.add_option('-s', '--source', dest='source_file',
help='mimic or demimic a source file instead of stdin')

(options, args) = parser.parse_args()

Expand All @@ -377,12 +396,12 @@ def check_opts(opt, compat=None, req=None):
'req': tuple(req)
})

check_opts('forward', {'chance', 'source_steg_file'})
check_opts('reverse', {'dest_steg_file'})
check_opts('check', {'dest_steg_file'})
check_opts('source_steg_file', {'forward', 'chance'}, {'forward'})
check_opts('dest_steg_file', req={'reverse', 'check'})
check_opts('chance', {'forward', 'source_steg_file'}, {'forward'})
check_opts('forward', {'chance', 'source_steg_file', 'source_file'})
check_opts('reverse', {'dest_steg_file', 'source_file'})
check_opts('check', {'dest_steg_file', 'source_file'})
check_opts('source_steg_file', {'forward', 'chance', 'source_file'}, {'forward'})
check_opts('dest_steg_file', {'reverse', 'check', 'source_file'}, req={'reverse', 'check'})
check_opts('chance', {'forward', 'source_steg_file', 'source_file'}, {'forward'})
check_opts('explain_char')
check_opts('list')

Expand All @@ -394,15 +413,51 @@ def check_opts(opt, compat=None, req=None):
return options, args


def read_line_stdin():
"""
read_line implementation drawing from stdin (default usage)
:return: Next line of input as a string
"""
from sys import stdin

if version_info >= (3,):
return input() + "\n"
return raw_input().decode(stdin.encoding or 'utf-8') + "\n"


def create_read_line_file(file_name):
"""
read_line implementation drawing from a file

:param file_name: The name of the file to read
:return: The next line of the file
"""
global FILE
FILE = open(file_name, "rb")
def read_line_file():
return FILE.readline().decode("utf-8")
return read_line_file


def main():
try:
(options, args) = parse()

reader = read_line_stdin
if options.source_file:
reader = create_read_line_file(options.source_file)

if options.forward:
pipe_mimic(options.chance)
stego = Steganography(source_file=options.source_steg_file)
pipe_mimic(reader, options.chance, stego)
stego.close()
elif options.reverse:
pipe(replace_reverse)
stego = Steganography(dest_file=options.dest_steg_file)
pipe(reader, replace_reverse, stego)
stego.close()
elif options.check:
pipe(replace_check)
stego = Steganography()
pipe(reader, replace_check, stego)
elif options.explain_char:
explain(unicode(options.explain_char, 'utf-8'))
elif options.list:
Expand Down
Loading