-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathexefilter_minigui.py
263 lines (209 loc) · 8.1 KB
/
exefilter_minigui.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
"""
exefilter_minigui.py
A minimalistic GUI (graphical user interface) for ExeFilter.
This file is part of the ExeFilter project.
Project website: U{http://www.decalage.info/en/exefilter}
@author: U{Philippe Lagadec<mailto:decalage(a)laposte.net>}
@contact: U{Philippe Lagadec<mailto:decalage(a)laposte.net>}
@license: CeCILL (open-source, GPL compatible)
see attached file LICENCE.txt
@version: 0.01
@status: alpha
"""
#==============================================================================
__docformat__ = 'epytext en'
__date__ = "2010-05-02"
__version__ = "0.01"
#------------------------------------------------------------------------------
# CHANGELOG:
# 2010-05-02 v0.01 PL: - initial version
#------------------------------------------------------------------------------
# TODO:
#------------------------------------------------------------------------------
# REFERENCES:
# http://easygui.sourceforge.net/
#=== IMPORTS ==================================================================
import os.path
import ExeFilter as xf
import Politique
from thirdparty.plx import plx
from thirdparty.easygui import *
#=== CONSTANTS ================================================================
MAIN_TITLE = 'ExeFilter v%s miniGUI' % xf.XF_VERSION
MODE_FILE = 'file'
MODE_DIR = 'directory'
FILE = 'A - choose a File to be analyzed'
DIR = 'D - choose a Directory or Removable Device to be analyzed'
POLICY = 'E - load/edit Policy'
LAUNCH = 'L - Launch ExeFilter'
EXIT = 'X - Exit'
main_menu_choices = [
FILE,
DIR,
POLICY,
LAUNCH,
EXIT,
]
POLICY_EDIT = 'E - Edit policy'
POLICY_LOAD = 'L - Load policy from file'
POLICY_SAVE = 'S - Save policy to file'
POLICY_HTML = 'T - Create HTML file describing the policy'
POLICY_EXIT = 'X - return to main menu'
policy_menu_choices = [
POLICY_EDIT,
POLICY_LOAD,
POLICY_SAVE,
POLICY_HTML,
POLICY_EXIT,
]
#=== GLOBAL VARIABLES =========================================================
mode = MODE_DIR
source_dir = os.path.abspath('demo_files')
dest_dir = os.path.abspath('demo_output')
source_file = os.path.join(source_dir, '*')
dest_file = os.path.join(dest_dir, '*')
config_file = None
# default policy:
policy = Politique.Politique()
#=== FUNCTIONS ================================================================
def edit_param(section, param):
title = 'Edit %s.%s' % (section, param.code)
msg = '%s.%s: %s\n\n%s\n\nDefault value: %s' % (section,
param.code, param.nom, param.description, param.valeur_defaut)
value = enterbox(msg, title, default=param.valeur)
if value is not None:
param.valeur = value
def edit_section (section, params):
msg = "Select policy parameter to be edited:"
choices2params = {}
choices = [POLICY_EXIT]
for param in params.values():
choice = '%s: %s' % (param.code, param.nom)
choices2params[choice] = param
choices.append(choice)
while True:
choice = choicebox(msg=msg, title=section,
choices=choices)
if not choice or choice == POLICY_EXIT:
break
param = choices2params[choice]
edit_param(section, param)
def policy_menu():
global config_file, policy
while True:
choice = choicebox(msg='Edit the filtering policy', title=MAIN_TITLE,
choices=policy_menu_choices)
if choice == POLICY_LOAD:
title = "Load policy file"
msg = "Select policy file to be loaded:"
f = fileopenbox(msg, title, default='*.ini')
if f:
config_file = str(f)
# start with default policy:
policy = Politique.Politique()
# load policy from file
policy.lire_config(config_file)
if choice == POLICY_EDIT:
title = "Edit policy"
msg = "Select policy section to be edited:"
choices = [Politique.SECTION_EXEFILTER, POLICY_EXIT]
filter_params = {}
for filter in policy.filtres:
choices.append(filter.nom_classe)
filter_params[filter.nom_classe] = filter.parametres
while True:
choice = choicebox(msg=msg, title=title,
choices=choices)
if not choice or choice == POLICY_EXIT:
break
if choice == Politique.SECTION_EXEFILTER:
params = policy.parametres
else:
params = filter_params[choice]
section = choice
edit_section(section, params)
if choice == POLICY_SAVE:
title = "Save policy file"
msg = "Select policy file to be saved:"
if config_file:
default_name = str(config_file)
else:
default_name = 'policy.ini'
f = filesavebox(msg, title, default=default_name)
if f:
config_file = str(f)
# save policy to file
policy.ecrire_fichier(config_file)
if choice == POLICY_HTML:
title = "Create HTML file describing the policy"
msg = "Select HTML file to create:"
if config_file:
default_name = str(config_file+'.html')
else:
default_name = 'policy.html'
f = filesavebox(msg, title, default=default_name)
if f:
# create HTML file
policy.ecrire_html(f)
plx.display_html_file(f)
if not choice or choice == POLICY_EXIT:
break
#=== MAIN =====================================================================
try:
while True:
if mode == MODE_DIR:
source, dest = source_dir, dest_dir
else:
source, dest = source_file, dest_file
status = """ExeFilter v%s
mode: %s
source dir or file: %s
destination dir or file: %s
config/policy file: %s
""" % (xf.XF_VERSION, mode, source, dest, config_file)
choice = choicebox(msg=status, title=MAIN_TITLE, choices=main_menu_choices)
if not choice:
break
if choice == FILE:
title = "Source file"
msg = "Select source file to be analyzed:"
f = fileopenbox(msg, title, default=source_file)
if f:
mode = MODE_FILE
# convert to string because easygui does not like unicode...
source_file = str(f)
# dest file = source file + _cleaned by default:
filename, ext = os.path.splitext(f)
dest_file = str(filename + '_cleaned' + ext)
title = "Destination file"
msg = "Select destination file to store the sanitized version:"
f = filesavebox(msg, title, default=dest_file)
if f:
dest_file = str(f)
if choice == DIR:
title = "Source directory"
msg = "Select source directory (or removable device) to be analyzed:"
d = diropenbox(msg, title, default=source_dir)
if d:
mode = MODE_DIR
source_dir = d
title = "Destination directory"
msg = "Select destination directory where to copy sanitized files:"
#TODO: create dest dir if not there?
d = diropenbox(msg, title, default=dest_dir)
if d:
dest_dir = d
if choice == POLICY:
policy_menu()
if choice == LAUNCH:
if mode == MODE_DIR:
xf.transfert([source_dir], dest_dir, pol=policy)
else:
xf.transfert([source_file], dest_file, pol=policy, dest_is_a_file=True)
xf.display_html_report()
if choice == EXIT:
break
except:
exceptionbox()