-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstegpy.py
executable file
·222 lines (191 loc) · 7.33 KB
/
stegpy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python
#Stegpy - a simple steganalysis script
#Copyright (C) 2013 Nicolas Oberli
#
#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import os
import sys
import argparse
from Tkinter import Tk , StringVar, OptionMenu, Label
import tkSimpleDialog
import tkFileDialog
from PIL import Image, ImageTk
class Viewer():
def __init__(self, image):
self.original = image
self.image = image.copy()
self.window = Tk()
self.genViews()
self.currentView = StringVar(self.window)
self.currentView.set('Original')
options = self.filters.keys();options.sort()
self.views = OptionMenu(self.window, self.currentView,
*options, command=self.applyFilter)
self.views.pack()
self.tkImage = ImageTk.PhotoImage(image)
self.lblImage = Label(image=self.tkImage)
self.lblImage.bind('<Button-1>', self.displayInfos)
self.lblImage.bind('<Button-3>', self.save)
self.lblImage.image = self.tkImage
self.lblImage.pack()
self.status = StringVar()
self.lblStatus = Label(textvariable=self.status, justify='right')
self.lblStatus.pack()
self.window.mainloop()
def displayInfos(self, event):
"""
Displays the coordinates in the status bar
"""
x = int((event.x-0.1)/args.scalefactor)
y = int((event.y-0.1)/args.scalefactor)
pixel = orig.getpixel((x, y))
self.setStatus("Coordinates : (%s:%s) - Pixel value : %s" %
(x, y, str(pixel)))
def setStatus(self, text):
"""
Changes the text in the status bar
"""
self.status.set(text)
def save(self, event):
"""
Saves the filtered image to a file
"""
options = {'filetypes':[('PNG','.png'),('GIF','.gif')]}
outfile = tkFileDialog.asksaveasfilename(**options)
if outfile == '':
return
else:
self.image.save(outfile)
return
def genViews(self):
"""
Generates filters based on the source image
"""
self.filters = {}
for plug in viewPlugins:
self.filters.update({plug.name:viewPlugins.index(plug)})
def applyFilter(self, view):
"""
Applies a filter to the image
"""
view = self.filters[self.currentView.get()]
plugin = viewPlugins[view]
if plugin.parameters:
for param in plugin.parameters.keys():
a = tkSimpleDialog.askinteger(
'Question', plugin.parameters[param])
if a is not None:
setattr(viewPlugins[view], param, a)
self.image = viewPlugins[view].process(self.original)
self.showImage(self.currentView.get(), self.image)
self.setStatus("")
return
def showImage(self, title, image):
"""
Updates the image in the window
"""
self.tkImage = ImageTk.PhotoImage(image)
self.lblImage.configure(image=self.tkImage)
self.lblImage.image = self.tkImage
viewPlugins = []
commandPlugins = []
plugins = []
def loadPlugins():
sys.path.insert(0,'plugins/')
plugs = []
for filename in os.listdir('plugins/'):
name, ext = os.path.splitext(filename)
if ext.endswith(".py"):
plugs.append(__import__(name))
for plug in plugs:
plugin = plug.register()
if plugin.mode == 'visual':
viewPlugins.append(plugin)
elif plugin.mode == 'command':
commandPlugins.append(plugin)
else:
plugins.append(plugin)
def parseVisualParameters(parent = None):
params = argparse.ArgumentParser( parents=[parent],
description="Visual mode parameters")
params.add_argument('-ts', '--thumbnail-size', dest='thumbsize', type=int,
metavar='SIZE', default=0,
help='Use a thumbnail of maximum SIZE pixels to view generated images')
params.add_argument('-sf', '--scale-factor', dest='scalefactor', type=float,
metavar='FACTOR', default=1,
help='Scale the image to FACTOR. can be positive or a fraction')
return params
def parseCommandParameters( parent=None):
params = argparse.ArgumentParser( parents=[parent], add_help=False,
description="Command mode parameters")
params.add_argument('-p', '--plugin', dest='plugin',
choices = [plugin.name for plugin in commandPlugins],
help='Use the following plugin (unset to have a list)')
return params
if __name__ == '__main__':
loadPlugins()
parser = argparse.ArgumentParser(
description='Analyzes an image to find steganography data.',
add_help=False)
parser.add_argument('filename', metavar='FILE', type=file,
help='file to analyze')
applicationMode = parser.add_mutually_exclusive_group(required=True)
applicationMode.add_argument('-V', '--visual', dest='visual',
action='store_true', help='Starts visual mode')
applicationMode.add_argument('-C', '--command', dest='command',
action='store_true', help='Starts command mode')
args = parser.parse_known_args()[0]
try:
orig = Image.open(args.filename)
except IOError, e:
print e.message
sys.exit(1)
#Enter visual mode
if args.visual:
parser = parseVisualParameters(parser)
args = parser.parse_known_args()[0]
#Creating a thumbnail to work with
if args.thumbsize:
thumb = orig.copy()
thumb.thumbnail((args.thumbsize, args.thumbsize), Image.NEAREST)
v = Viewer(thumb)
#If scale factor is used
elif args.scalefactor:
image = orig.resize(tuple([int(args.scalefactor*x) for x in orig.size]), Image.NEAREST)
v = Viewer(image)
else:
v = Viewer(orig)
sys.exit(0)
elif args.command:
commandParser = parseCommandParameters(parser)
args = commandParser.parse_known_args()[0]
if args.plugin is None:
commandParser.print_help()
else:
#Get the right plugin
for plugin in commandPlugins:
if plugin.name == args.plugin:
break
if plugin.parameters is not None:
pluginParser = plugin.get_argParser()
pluginParser.parents = [parser, commandParser]
args = pluginParser.parse_known_args()[0]
for param in plugin.parameters.keys():
try:
setattr(plugin, param, getattr(args, param))
except AttributeError, e:
pass
print plugin.process(orig)
pass