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
|
/*
* The Sleuth Kit
*
* Contact: Brian Carrier [carrier <at> sleuthkit [dot] org]
* Copyright (c) 2011-2012 Basis Technology Corporation. All Rights
* reserved.
*
* This software is distributed under the Common Public License 1.0
*/
/** \file ExifExtractModule.cpp
* Contains the implementation of the EXIF data extraction file analysis module.
*/
// System includes
#include <string>
#include <sstream>
#include <string.h>
// Framework includes
#include "tsk/framework/utilities/TskModuleDev.h"
// libexif includes
#include "libexif/exif-loader.h"
#include "Poco/DateTime.h"
#include "Poco/DateTimeParser.h"
namespace
{
const char *MODULE_NAME = "tskLibExifModule";
const char *MODULE_DESCRIPTION = "Stores extracted EXIF data to the image database";
const char *MODULE_VERSION = "1.0.0";
// JFIF signature
static unsigned char jfifSig[] = { 0xFF, 0xD8, 0xFF, 0xE0 };
// EXIF signature
static unsigned char exifSig[] = { 0xFF, 0xD8, 0xFF, 0xE1 };
// We process the file 8k at a time
static const uint32_t FILE_BUFFER_SIZE = 8192;
std::map<ExifTag, TSK_ATTRIBUTE_TYPE> initializeTagMap()
{
std::map<ExifTag, TSK_ATTRIBUTE_TYPE> retval;
retval.insert(pair<ExifTag, TSK_ATTRIBUTE_TYPE>(EXIF_TAG_MAKE, TSK_DEVICE_MAKE));
retval.insert(pair<ExifTag, TSK_ATTRIBUTE_TYPE>(EXIF_TAG_MODEL, TSK_DEVICE_MODEL));
retval.insert(pair<ExifTag, TSK_ATTRIBUTE_TYPE>((ExifTag)EXIF_TAG_GPS_LATITUDE, TSK_GEO_LATITUDE));
retval.insert(pair<ExifTag, TSK_ATTRIBUTE_TYPE>((ExifTag)EXIF_TAG_GPS_LONGITUDE, TSK_GEO_LONGITUDE));
retval.insert(pair<ExifTag, TSK_ATTRIBUTE_TYPE>((ExifTag)EXIF_TAG_GPS_ALTITUDE, TSK_GEO_ALTITUDE));
retval.insert(pair<ExifTag, TSK_ATTRIBUTE_TYPE>((ExifTag)EXIF_TAG_GPS_MAP_DATUM, TSK_GEO_MAPDATUM));
retval.insert(pair<ExifTag, TSK_ATTRIBUTE_TYPE>((ExifTag)EXIF_TAG_GPS_SPEED, TSK_GEO_VELOCITY));
retval.insert(pair<ExifTag, TSK_ATTRIBUTE_TYPE>((ExifTag)EXIF_TAG_DATE_TIME_ORIGINAL, TSK_DATETIME));
retval.insert(pair<ExifTag, TSK_ATTRIBUTE_TYPE>((ExifTag)EXIF_TAG_XP_AUTHOR, TSK_NAME_PERSON));
retval.insert(pair<ExifTag, TSK_ATTRIBUTE_TYPE>((ExifTag)EXIF_TAG_TIME_ZONE_OFFSET,TSK_DATETIME));
return retval;
}
static std::map<ExifTag, TSK_ATTRIBUTE_TYPE> tagMap = initializeTagMap();
/**
* Extracts GPS coordinates from the given tag_data and converts them
* into decimal degrees.
*/
float getDecimalDegrees(char * tag_data)
{
char * token;
char * deg;
char * min;
char * sec;
// Tokenize the data
token = strtok(tag_data, " , ");
// Caputure degrees, minutes, seconds
if (token)
{
deg = token;
token = strtok(NULL, " , ");
}
if (token)
{
min = token;
token = strtok(NULL, " , ");
}
if (token)
{
sec = token;
token = strtok(NULL, " , ");
}
// Formula to convert to decimal
return (atof(deg) + (atof(min)/60) + (atof(sec)/3600));
}
/* Extracts GPS speed and returns speed as float value */
float getGPSSpeed(char *tag_data)
{
char * token;
char * wholeNum;
char * decimal;
// Tokenize the data
token = strtok(tag_data, ".");
//Get the whole number value
if (token)
{
wholeNum = token;
token = strtok(NULL, ".");
}
if (token)
{
decimal = token;
token = strtok(NULL, " , ");
}
return (atof(wholeNum) + atof(decimal));
}
}
extern "C"
{
/**
* Module identification function.
*
* @return The name of the module as a const char *.
*/
TSK_MODULE_EXPORT const char* name()
{
return MODULE_NAME;
}
/**
* Module identification function.
*
* @return A description of the module as a const char *.
*/
TSK_MODULE_EXPORT const char* description()
{
return MODULE_DESCRIPTION;
}
/**
* Module identification function.
*
* @return The version of the module as a const char *.
*/
TSK_MODULE_EXPORT const char* version()
{
return MODULE_VERSION;
}
/* Function to populate TSK Blackboard exif related attributes */
void extractExifData(ExifData * exifData, TskFile * pFile)
{
std::map<ExifTag, TSK_ATTRIBUTE_TYPE>::iterator it;
std::vector<TskBlackboardAttribute> attrs;
std::string datetime = "";
int timezone = 0;
for (it = tagMap.begin(); it != tagMap.end(); ++it)
{
ExifEntry * exifEntry = exif_data_get_entry(exifData, it->first);
char tag_data[256];
if (exifEntry == NULL)
continue;
if (it->first == EXIF_TAG_GPS_LATITUDE ||
it->first == EXIF_TAG_GPS_LONGITUDE)
{
// Check for the EXIF_IFD_GPS image file directory to avoid interoperability value
ExifIfd ifd = exif_entry_get_ifd(exifEntry);
if (ifd != EXIF_IFD_GPS)
continue;
exif_entry_get_value(exifEntry, tag_data, 256);
float decDegrees = getDecimalDegrees(tag_data);
char refValue[2];
if (it->first == EXIF_TAG_GPS_LATITUDE)
{
// Get the latitude reference value; used to determine if positive or negative decimal value
ExifEntry * latitudeRef = exif_data_get_entry(exifData, it->first);
exif_entry_get_value(latitudeRef, refValue,2);
if (strcmp(refValue, "S") == 0)
decDegrees *= -1;
}
else
{
// Get the longitude reference value; used to determine if positive or negative decimal value
ExifEntry * longitudeRef = exif_data_get_entry(exifData, it->first);
exif_entry_get_value(longitudeRef, refValue,2);
if (strcmp(refValue, "W") == 0)
decDegrees *= -1;
}
TskBlackboardAttribute attr(it->second, name(), "", decDegrees);
attrs.push_back(attr);
}
else if (it->first == EXIF_TAG_GPS_SPEED)
{
// Check for the EXIF_IFD_GPS image file directory to avoid interoperability value
ExifIfd ifd = exif_entry_get_ifd(exifEntry);
if (ifd != EXIF_IFD_GPS)
continue;
//Get the GPS speed value
exif_entry_get_value(exifEntry, tag_data, 256);
float speed = getGPSSpeed(tag_data);
char refValue[2];
//Get the GPS speed reference value
ExifEntry * speedRef = exif_data_get_entry(exifData, it->first);
exif_entry_get_value(speedRef, refValue,2);
//Convert Kilometers per hour to meters per second
if (strcmp(refValue, "K") == 0)
{
speed *= 0.277778;
}
//Convert Miles per hour to meters per second
if (strcmp(refValue, "M") == 0)
{
speed *= 0.44704;
}
//Convert Knots to meters per second
if (strcmp(refValue, "N") == 0)
{
speed *= 0.514444;
}
TskBlackboardAttribute attr(it->second, name(), "", speed);
attrs.push_back(attr);
}
else if (it->first == EXIF_TAG_DATE_TIME_ORIGINAL)
{
exif_entry_get_value(exifEntry, tag_data, 256);
datetime = std::string(tag_data);
}
else if(it->first == EXIF_TAG_TIME_ZONE_OFFSET){
exif_entry_get_value(exifEntry, tag_data, 256);
timezone = atoi(tag_data);
}
else
{
// Get the tag's data
exif_entry_get_value(exifEntry, tag_data, 256);
// Add tag data to blackboard
TskBlackboardAttribute attr(it->second, name(), "", tag_data);
attrs.push_back(attr);
}
}
if(!datetime.empty()){
Poco::DateTime parsedDT;
int tzd;
Poco::DateTimeParser::tryParse(datetime, parsedDT, tzd);
if(timezone)
parsedDT.makeUTC(timezone);
else
parsedDT.makeUTC(tzd);
TskBlackboardAttribute attr(TSK_DATETIME, name(), "", (uint64_t)parsedDT.utcTime());
attrs.push_back(attr);
}
if(attrs.size() > 0){
TskBlackboardArtifact art = pFile->createArtifact(TSK_METADATA_EXIF);
for(size_t i = 0; i < attrs.size(); i++){
art.addAttribute(attrs[i]);
}
}
}
/**
* Module initialization function. This module does not take any arguments.
* @return TskModule::OK
*/
TskModule::Status TSK_MODULE_EXPORT initialize(const char* arguments)
{
return TskModule::OK;
}
/**
* Module execution function. Receives a pointer to a file the module is to
* process. The file is represented by a TskFile interface which is used
* to read the contents of the file and post extracted EXIF data to the
* database.
*
* @param pFile A pointer to a file.
* @returns TskModule::OK on success, TskModule::FAIL on error.
*/
TskModule::Status TSK_MODULE_EXPORT run(TskFile * pFile)
{
if (pFile == NULL)
{
LOGERROR("ExifExtractModule: passed NULL file pointer.");
return TskModule::FAIL;
}
try
{
char buffer[FILE_BUFFER_SIZE];
int bytesRead = 0;
memset(buffer, 0, FILE_BUFFER_SIZE);
bytesRead = pFile->read(buffer, FILE_BUFFER_SIZE);
if (bytesRead < 4)
return TskModule::OK;
// Check the first 4 bytes to see if this is a JPEG file.
// We check for both the JFIF and EXIF signatures.
if (memcmp(buffer, jfifSig, sizeof(jfifSig)) != 0 &&
memcmp(buffer, exifSig, sizeof(exifSig)) != 0)
{
// It's not a JPEG file so we skip it.
return TskModule::OK;
}
ExifLoader * exifLoader = exif_loader_new();
if (exifLoader == NULL)
{
LOGERROR("ExifExtractModule - Received NULL ExifLoader pointer");
return TskModule::FAIL;
}
// Feed the file content into libexif
while (bytesRead > 0)
{
exif_loader_write(exifLoader, reinterpret_cast<unsigned char *>(buffer), bytesRead);
memset(buffer, 0, FILE_BUFFER_SIZE);
bytesRead = pFile->read(buffer, FILE_BUFFER_SIZE);
}
ExifData * exifData = exif_loader_get_data(exifLoader);
// exifData will be NULL if there is no EXIF data in the image
if (exifData != NULL)
{
// For debugging, exif_data_dump writes all exif data to stdout
//exif_data_dump(exifData);
extractExifData(exifData, pFile);
exif_data_unref(exifData);
}
// Free the loader
exif_loader_unref(exifLoader);
}
catch (TskException& tskEx)
{
std::stringstream msg;
msg << "ExifExtractModule - Error processing file id " << pFile->getId() << ": " << tskEx.message();
LOGERROR(msg.str());
return TskModule::FAIL;
}
catch (std::exception& ex)
{
std::stringstream msg;
msg << "ExifExtractModule - Error processing file id " << pFile->getId() << ": " << ex.what();
LOGERROR(msg.str());
return TskModule::FAIL;
}
return TskModule::OK;
}
/**
* Module cleanup function. This module does not need to free any
* resources allocated during initialization or execution.
*
* @returns TskModule::OK
*/
TskModule::Status TSK_MODULE_EXPORT finalize()
{
return TskModule::OK;
}
}
|