-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileExtractorCLI.py
executable file
·169 lines (149 loc) · 6.59 KB
/
FileExtractorCLI.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
#!/usr/bin/env python
"""
Command Line interface for the FileExtractor. Initialises and starts the fileextractor core.
This module can start the application. It is checking for the call of the __main__ function and
will in case of activate the core.
FileExtractor 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 3 of the License, or
(at your option) any later version.
FileExtractor 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 FileExtractor. If not, see <http://www.gnu.org/licenses/>.
"""
import FileExtractorCore
import tools
import signatures
import sys
from ExecutionSettings import ExecutionSettings
from ExecutionSettings import ExecutionStatus
from ExecutionSettings import def_digits
def usage(programname):
"""
Displays the usage text to standard output.
@param programname: Name of the program (usually first argument of the CLI argument list)
"""
print ""
print "Usage: %s [-sX] [-eX] [-dX] [-nX] [-fS] [-o{1|2|3}] filename" %programname
print ""
print "Parameters"
print "\t-sX\tStart position inside file for searching (default 0)"
print "\t-eX\tEnd position inside file for searching (default filesize)"
print "\t-dX\tNumber of digits for numbering the files (default %d)" %def_digits
print "\t-nX\tNumber of first file to create (default 0)"
print "\t-fS\tDestination folder for extracted files (default current directory)"
print "\t-oX\tOutput level (1-progress; 2-percentage/occurences; 3-full debug) - (default 2)"
print "\t-gX\tOutput frequency - depending on the size: filesize/X (default 100)"
print "\t-iY\tSignature operations"
print "\t\t-is\tShow available signaturs"
print "\t\t-idS\tDisable Signature with name S"
print ""
def handleArguments(args):
"""
Processes a list of arguments.
Creates a new L{ExecutionSettings} and a new L{ExecutionStatus} object.
Iterates the list of arguments and assigns any known argument value to the given
settings or status instance.
@param args: List of arguments to be processed (usually the CLI arguments list)
@return: Info about success: 0 - not successful, 1 - Signature information is requested,
2 - successful; The settings applied to the Settings / Status instances
@rtype: C{int}; L{ExecutionStatus}
"""
if len(args) < 2:
usage(args[0])
return 0, None
settings = ExecutionSettings(signatures = signatures.getCopyOfAllSignauteres())
status = ExecutionStatus(settings)
disabled_signs = []
for arg in args:
if arg[0] != '-':
continue
if arg[1] == 's':
status.file_start = int(arg[2:])
elif arg[1] == 'e':
status.file_end = int(arg[2:])
elif arg[1] == 'd':
settings.digits = int(arg[2:])
elif arg[1] == 'n':
settings.counterstart_global = int(arg[2:])
elif arg[1] == 'f':
settings.dest_folder = arg[2:]
elif arg[1] == 'o':
settings.output_level = int(arg[2:])
elif arg[1] =='g':
settings.output_frequency = int(arg[2:])
elif arg[1] =='i':
if arg[2] == 's':
return 1, status
elif arg[2] == 'd':
disabled_signs.append(arg[3:])
else:
print ('Unrecognised option for Signature operations: %c [Ignored]' %(arg[2]))
else:
print ('Unrecognised option: %c [Ignored]' %(arg[1]))
settings.dest_folder = tools.checkDestfolder(settings.dest_folder)
settings.sourceFiles.append(args[len(args)-1])
settings.disableSignatureWithNames(disabled_signs)
return 2, status
def printHeader(status):
"""
Display information about the settings for the current application execution on standard sutput.
@param status: Information container for current execution.
@type status: L{ExecutionStatus}
"""
signs = status.settings.signatures
binfilename = status.getCurrentFile()
size = status.getCurrentSize()
print "\nSearching for Files inside Files"
print "--------------------------------"
print "The following signatures are activated:"
for sig in signs:
print ('\t%s\t: %s' %(sig[signatures.name], sig[signatures.description]))
print ('\nFile to search in: %s (%d Bytes)\n' % (binfilename, size))
print "Settings:"
print "\tRead from: 0x%x to: 0x%x" %(status.file_start, status.file_end)
print "\tDigits for running number: %d" %status.settings.digits
print "\tStart counting from: %d" %status.settings.counterstart_global
print "\tTarget Directory: %s" %status.settings.dest_folder
print "\tOutput Level: %d" %status.settings.output_level
print ""
def printResults(signs, counter, time_passed):
"""
Displays the results for the performed execution to standard output.
@param signs: Signatures which were active for this execution.
@type signs: C{dict}
@param counter: Number of found files for each signature for the last execution.
@type counter: C{dict}
@param time_passed: Time passing during the execution.
@type time_passed: C{float}
"""
print "--------------------------------"
print "\nSearch finished - results:"
print "\nThe following number of files were found and stored:"
for sig in signs:
print ('\t%s\t: %d File(s)' %(sig[signatures.name], counter[sig[signatures.name]]))
print ('Overall processing time: %f seconds' %time_passed)
def startCLI(argv):
"""
Starts the CLI application
An L{ExecutionSettings} object is intialised and passed to the method L{handleArguments}, which
assigns the appropriate values. Afterwards, the FileExtractor core is initialised and
started with the given source file.
@param argv: List of arguments processed by the application (CL arguments)
"""
ret, status = handleArguments(argv)
if ret==0:
sys.exit()
if ret == 1:
signatures.printSignatures(status.settings.getActiveSignatures())
sys.exit()
if FileExtractorCore.init(status) < 0:
sys.exit()
printHeader(status)
signs, counter = FileExtractorCore.startSearch(status)
printResults(signs, counter, status.getRunTimeForNumber(0))
if __name__ == "__main__":
startCLI(sys.argv)