forked from peterclemenko/c_EntropyModule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntropyModule.cpp
444 lines (419 loc) · 17 KB
/
EntropyModule.cpp
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
*
* The Sleuth Kit
*
* Contact: Brian Carrier [carrier <at> sleuthkit [dot] org]
*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* \file EntropyModule.cpp
* Contains the implementation of a file analysis module that calculates the
* entropy of a file's contents.
*
* This sample module shows a basic Sleuth Kit Framework module. It is
* released as public domain and you are free to remove this header, use
* it as a starting point for your module, and choose whatever license that
* you want. Note that the framework itself is NOT public domain.
*/
// TSK Framework includes
#include "TskModuleDev.h"
// Poco includes
// Uncomment this include if using the Poco catch blocks.
//#include "Poco/Exception.h"
// C/C++ library includes
#include <string>
#include <sstream>
#include <math.h>
#include <assert.h>
// More complex modules will likely put functions and variables other than
// the module API functions in separate source files and/or may define various
// C++ classes to perform the work of the module. However, it is possible to simply
// enclose such functions and variables in an anonymous namespace to give them file scope
// instead of global scope, as is done in this module. This replaces the older practice
// of declaring file scope functions and variables using the "static" keyword. An
// anonymous namespace is a more flexible construct, since it is possible to define
// types within it.
//
// NOTE: Linux/OS-X module developers should make sure module functions
// other than the module API functions are either uniquely named or bound at module link time.
// Placing these functions in an anonymous namespace to give them static-linkage is one way to
// accomplish this.
//
// CAVEAT: Static data can be incompatible with multithreading, since each
// thread will get its own copy of the data.
namespace
{
const char *MODULE_NAME = "Entropy";
const char *MODULE_DESCRIPTION = "Performs an entropy calculation for the contents of a given file";
const char *MODULE_VERSION = "1.0.0";
/**
* Calculates the entropy of a file.
*
* @param pFile A TskFile object corrersponding to a file.
* @return The entropy of the file.
*/
double calculateEntropy(TskFile *pFile)
{
const uint32_t FILE_BUFFER_SIZE = 8193;
uint8_t byte = 0;
long byteCounts[256];
memset(byteCounts, 0, sizeof(long) * 256);
long totalBytes = 0;
char buffer[FILE_BUFFER_SIZE];
ssize_t bytesRead = 0;
do
{
memset(buffer, 0, FILE_BUFFER_SIZE);
bytesRead = pFile->read(buffer, FILE_BUFFER_SIZE);
if (bytesRead > 0)
{
for (int i = 0; i < bytesRead; ++i)
{
byte = static_cast<uint8_t>(buffer[i]);
byteCounts[byte]++;
}
totalBytes += bytesRead;
}
}
while (bytesRead > 0);
double entropy = 0.0;
for (int i = 0; i<256; ++i)
{
double p = static_cast<double>(byteCounts[i]) / static_cast<double>(totalBytes);
if (p > 0.0)
{
entropy -= p * (log(p) / log(2.0));
}
}
return entropy;
}
}
extern "C"
{
/**
* Module identification function.
*
* CAVEAT: This function is intended to be called by TSK Framework only.
* Linux/OS-X modules should *not* call this function within the module
* unless appropriate compiler/linker options are used to bind all
* library-internal symbols at link time.
*
* @return The name of the module.
*/
TSK_MODULE_EXPORT const char *name()
{
return MODULE_NAME;
}
/**
* Module identification function.
*
* CAVEAT: This function is intended to be called by TSK Framework only.
* Linux/OS-X modules should *not* call this function within the module
* unless appropriate compiler/linker options are used to bind all
* library-internal symbols at link time.
*
* @return A description of the module.
*/
TSK_MODULE_EXPORT const char *description()
{
return MODULE_DESCRIPTION;
}
/**
* Module identification function.
*
* CAVEAT: This function is intended to be called by TSK Framework only.
* Linux/OS-X modules should *not* call this function within the module
* unless appropriate compiler/linker options are used to bind all
* library-internal symbols at link time.
*
* @return The version of the module.
*/
TSK_MODULE_EXPORT const char *version()
{
return MODULE_VERSION;
}
/**
* Module initialization function. Receives a string of initialization arguments,
* typically read by the caller from a pipeline configuration file.
* Returns TskModule::OK or TskModule::FAIL. Returning TskModule::FAIL indicates
* the module is not in an operational state.
*
* CAVEAT: This function is intended to be called by TSK Framework only.
* Linux/OS-X modules should *not* call this function within the module
* unless appropriate compiler/linker options are used to bind all
* library-internal symbols at link time.
*
* @param args a string of initialization arguments.
* @return TskModule::OK if initialization succeeded, otherwise TskModule::FAIL.
*/
TskModule::Status TSK_MODULE_EXPORT initialize(const char* arguments)
{
// The TSK Framework convention is to prefix error messages with the
// name of the module/class and the function that emitted the message.
std::ostringstream msgPrefix;
msgPrefix << MODULE_NAME << "::initialize : ";
// Well-behaved modules should catch and log all possible exceptions
// and return an appropriate TskModule::Status to the TSK Framework.
try
{
// If this module required initialization, the initialization code would
// go here.
return TskModule::OK;
}
catch (TskException &ex)
{
std::ostringstream msg;
msg << msgPrefix.str() << "TskException: " << ex.message();
LOGERROR(msg.str());
return TskModule::FAIL;
}
// Uncomment this catch block and the #include of "Poco/Exception.h" if using Poco.
//catch (Poco::Exception &ex)
//{
// std::ostringstream msg;
// msg << msgPrefix.str() << "Poco::Exception: " << ex.displayText();
// LOGERROR(msg.str());
// return TskModule::FAIL;
//}
catch (std::exception &ex)
{
std::ostringstream msg;
msg << msgPrefix.str() << "std::exception: " << ex.what();
LOGERROR(msg.str());
return TskModule::FAIL;
}
// Uncomment this catch block and add necessary .NET references if using C++/CLI.
//catch (System::Exception ^ex)
//{
// std::ostringstream msg;
// msg << msgPrefix.str() << "System::Exception: " << Maytag::systemStringToStdString(ex->Message);
// LOGERROR(msg.str());
// return TskModule::FAIL;
//}
catch (...)
{
LOGERROR(msgPrefix.str() + "unrecognized exception");
return TskModule::FAIL;
}
}
/**
* Module execution function for file analysis modules.
* Receives a pointer to a file the module is to process. The file is
* represented by a TskFile interface from which both file content and file
* metadata can be retrieved. Returns TskModule::OK, TskModule::FAIL, or
* TskModule::STOP. Returning TskModule::FAIL indicates the module
* experienced an error processing the file.
*
* CAVEAT: This function is intended to be called by TSK Framework only.
* Linux/OS-X modules should *not* call this function within the module
* unless appropriate compiler/linker options are used to bind all
* library-internal symbols at link time.
*
* @param pFile A pointer to a file to be processed.
* @returns TskModule::OK on success, TskModule::FAIL on error, or
* TskModule::STOP. Returning TskModule::STOP is a request to terminate
* processing of the file.
*/
TskModule::Status TSK_MODULE_EXPORT run(TskFile *pFile)
{
// The TSK Framework convention is to prefix error messages with the
// name of the module/class and the function that emitted the message.
std::ostringstream msgPrefix;
msgPrefix << MODULE_NAME << "::run : ";
// Well-behaved modules should catch and log all possible exceptions
// and return an appropriate TskModule::Status to the TSK Framework.
try
{
assert(pFile != NULL);
if (pFile == NULL)
{
throw TskException("passed NULL TskFile pointer");
}
// Calculate an entropy value for the file.
double entropy = calculateEntropy(pFile);
// Post the value to the blackboard.
pFile->addGenInfoAttribute(TskBlackboardAttribute(TSK_ENTROPY, MODULE_NAME, "", entropy));
return TskModule::OK;
}
catch (TskException &ex)
{
std::ostringstream msg;
msg << msgPrefix.str() << "TskException: " << ex.message();
LOGERROR(msg.str());
return TskModule::FAIL;
}
// Uncomment this catch block and the #include of "Poco/Exception.h" if using Poco.
//catch (Poco::Exception &ex)
//{
// std::ostringstream msg;
// msg << msgPrefix.str() << "Poco::Exception: " << ex.displayText();
// LOGERROR(msg.str());
// return TskModule::FAIL;
//}
catch (std::exception &ex)
{
std::ostringstream msg;
msg << msgPrefix.str() << "std::exception: " << ex.what();
LOGERROR(msg.str());
return TskModule::FAIL;
}
// Uncomment this catch block and add necessary .NET references if using C++/CLI.
//catch (System::Exception ^ex)
//{
// std::ostringstream msg;
// msg << msgPrefix.str() << "System::Exception: " << Maytag::systemStringToStdString(ex->Message);
// LOGERROR(msg.str());
// return TskModule::FAIL;
//}
catch (...)
{
LOGERROR(msgPrefix.str() + "unrecognized exception");
return TskModule::FAIL;
}
}
// /**
// * Module execution function for post-processing modules.
// *
// * CAVEAT: This function is intended to be called by TSK Framework only.
// * Linux/OS-X modules should *not* call this function within the module
// * unless appropriate compiler/linker options are used to bind all
// * library-internal symbols at link time.
// *
// * @returns TskModule::OK on success, TskModule::FAIL on error
// */
// TskModule::Status TSK_MODULE_EXPORT report()
// {
// // The TSK Framework convention is to prefix error messages with the
// // name of the module/class and the function that emitted the message.
// std::ostringstream msgPrefix;
// msgPrefix << MODULE_NAME << "::report : ";
//
// // Well-behaved modules should catch and log all possible exceptions
// // and return an appropriate TskModule::Status to the TSK Framework.
// try
// {
// // If this module could be used in a post-processing pipeline, the
// // code would go here.
//
// return TskModule::OK;
// }
// catch (TskException &ex)
// {
// std::ostringstream msg;
// msg << msgPrefix.str() << "TskException: " << ex.message();
// LOGERROR(msg.str());
// return TskModule::FAIL;
// }
// // Uncomment this catch block and the #include of "Poco/Exception.h" if using Poco.
// //catch (Poco::Exception &ex)
// //{
// // std::ostringstream msg;
// // msg << msgPrefix.str() << "Poco::Exception: " << ex.displayText();
// // LOGERROR(msg.str());
// // return TskModule::FAIL;
// //}
// catch (std::exception &ex)
// {
// std::ostringstream msg;
// msg << msgPrefix.str() << "std::exception: " << ex.what();
// LOGERROR(msg.str());
// return TskModule::FAIL;
// }
// // Uncomment this catch block and add necessary .NET references if using C++/CLI.
// //catch (System::Exception ^ex)
// //{
// // std::ostringstream msg;
// // msg << msgPrefix.str() << "System::Exception: " << Maytag::systemStringToStdString(ex->Message);
// // LOGERROR(msg.str());
// // return TskModule::FAIL;
// //}
// catch (...)
// {
// LOGERROR(msgPrefix.str() + "unrecognized exception");
// return TskModule::FAIL;
// }
// }
/**
* Module cleanup function. This is where the module should free any resources
* allocated during initialization or execution.
*
* CAVEAT: This function is intended to be called by TSK Framework only.
* Linux/OS-X modules should *not* call this function within the module
* unless appropriate compiler/linker options are used to bind all
* library-internal symbols at link time.
*
* @returns TskModule::OK on success and TskModule::FAIL on error.
*/
TskModule::Status TSK_MODULE_EXPORT finalize()
{
// The TSK Framework convention is to prefix error messages with the
// name of the module/class and the function that emitted the message.
std::ostringstream msgPrefix;
msgPrefix << MODULE_NAME << "::finalize : ";
// Well-behaved modules should catch and log all possible exceptions
// and return an appropriate TskModule::Status to the TSK Framework.
try
{
// If this module required finalization, the finalization code would
// go here.
return TskModule::OK;
}
catch (TskException &ex)
{
std::ostringstream msg;
msg << msgPrefix.str() << "TskException: " << ex.message();
LOGERROR(msg.str());
return TskModule::FAIL;
}
// Uncomment this catch block and the #include of "Poco/Exception.h" if using Poco.
//catch (Poco::Exception &ex)
//{
// std::ostringstream msg;
// msg << msgPrefix.str() << "Poco::Exception: " << ex.displayText();
// LOGERROR(msg.str());
// return TskModule::FAIL;
//}
catch (std::exception &ex)
{
std::ostringstream msg;
msg << msgPrefix.str() << "std::exception: " << ex.what();
LOGERROR(msg.str());
return TskModule::FAIL;
}
// Uncomment this catch block and add necessary .NET references if using C++/CLI.
//catch (System::Exception ^ex)
//{
// std::ostringstream msg;
// msg << msgPrefix.str() << "System::Exception: " << Maytag::systemStringToStdString(ex->Message);
// LOGERROR(msg.str());
// return TskModule::FAIL;
//}
catch (...)
{
LOGERROR(msgPrefix.str() + "unrecognized exception");
return TskModule::FAIL;
}
}
}