Autopsy  4.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
PhotoRecCarverOutputParser.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2014 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.modules.photoreccarver;
20 
21 import java.io.File;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.logging.Level;
34 import org.sleuthkit.datamodel.AbstractFile;
35 import org.sleuthkit.datamodel.LayoutFile;
36 import org.sleuthkit.datamodel.CarvedFileContainer;
37 import org.sleuthkit.datamodel.TskCoreException;
38 import org.sleuthkit.datamodel.TskFileRange;
39 import org.w3c.dom.Document;
40 import org.w3c.dom.Element;
41 import org.w3c.dom.NodeList;
42 
47 class PhotoRecCarverOutputParser {
48 
49  private final Path basePath;
50  private static final Logger logger = Logger.getLogger(PhotoRecCarverFileIngestModule.class.getName());
51 
52  PhotoRecCarverOutputParser(Path base) {
53  basePath = base;
54  }
55 
73  List<LayoutFile> parse(File xmlInputFile, long id, AbstractFile af) throws FileNotFoundException, IOException {
74  try {
75  final Document doc = XMLUtil.loadDoc(PhotoRecCarverOutputParser.class, xmlInputFile.toString());
76  if (doc == null) {
77  return null;
78  }
79 
80  Element root = doc.getDocumentElement();
81  if (root == null) {
82  logger.log(Level.SEVERE, "Error loading config file: invalid file format (bad root)."); //NON-NLS
83  return null;
84  }
85 
86  NodeList fileObjects = root.getElementsByTagName("fileobject"); //NON-NLS
87  final int numberOfFiles = fileObjects.getLength();
88 
89  if (numberOfFiles == 0) {
90  return null;
91  }
92  String fileName;
93  Long fileSize;
94  NodeList fileNames;
95  NodeList fileSizes;
96  NodeList fileRanges;
97  Element entry;
98  Path filePath;
99  FileManager fileManager = Case.getCurrentCase().getServices().getFileManager();
100 
101  // create and initialize the list to put into the database
102  List<CarvedFileContainer> carvedFileContainer = new ArrayList<>();
103 
104  for (int fileIndex = 0; fileIndex < numberOfFiles; ++fileIndex) {
105  entry = (Element) fileObjects.item(fileIndex);
106  fileNames = entry.getElementsByTagName("filename"); //NON-NLS
107  fileSizes = entry.getElementsByTagName("filesize"); //NON-NLS
108  fileRanges = entry.getElementsByTagName("byte_run"); //NON-NLS
109 
110  fileSize = Long.parseLong(fileSizes.item(0).getTextContent());
111  fileName = fileNames.item(0).getTextContent();
112  filePath = Paths.get(fileName);
113  if (filePath.startsWith(basePath)) {
114  fileName = filePath.getFileName().toString();
115  }
116 
117  List<TskFileRange> tskRanges = new ArrayList<>();
118  for (int rangeIndex = 0; rangeIndex < fileRanges.getLength(); ++rangeIndex) {
119 
120  Long img_offset = Long.parseLong(((Element) fileRanges.item(rangeIndex)).getAttribute("img_offset")); //NON-NLS
121  Long len = Long.parseLong(((Element) fileRanges.item(rangeIndex)).getAttribute("len")); //NON-NLS
122 
123  // Verify PhotoRec's output
124  long fileByteStart = af.convertToImgOffset(img_offset);
125  if (fileByteStart == -1) {
126  // This better never happen... Data for this file is corrupted. Skip it.
127  logger.log(Level.INFO, "Error while parsing PhotoRec output for file {0}", fileName); //NON-NLS
128  continue;
129  }
130 
131  // check that carved file is within unalloc block
132  long fileByteEnd = img_offset + len;
133  if (fileByteEnd > af.getSize()) {
134  long overshoot = fileByteEnd - af.getSize();
135  if (fileSize > overshoot) {
136  fileSize = fileSize - overshoot;
137  } else {
138  // This better never happen... Data for this file is corrupted. Skip it.
139  continue;
140  }
141  }
142 
143  tskRanges.add(new TskFileRange(fileByteStart, len, rangeIndex));
144  }
145 
146  if (!tskRanges.isEmpty()) {
147  carvedFileContainer.add(new CarvedFileContainer(fileName, fileSize, id, tskRanges));
148  }
149  }
150  return fileManager.addCarvedFiles(carvedFileContainer);
151  } catch (NumberFormatException | TskCoreException ex) {
152  logger.log(Level.SEVERE, "Error parsing PhotoRec output and inserting it into the database: {0}", ex); //NON-NLS
153  }
154 
155  List<LayoutFile> empty = Collections.emptyList();
156  return empty;
157  }
158 }

Copyright © 2012-2015 Basis Technology. Generated on: Wed Apr 6 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.