Autopsy  3.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
UsbDeviceIdMapper.java
Go to the documentation of this file.
1  /*
2  *
3  * Autopsy Forensic Browser
4  *
5  * Copyright 2012-2014 Basis Technology Corp.
6  *
7  * Copyright 2012 42six Solutions.
8  * Contact: aebadirad <at> 42six <dot> com
9  * Project Contact/Architect: carrier <at> sleuthkit <dot> org
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
27 package org.sleuthkit.autopsy.recentactivity;
28 
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.util.HashMap;
34 import java.util.Scanner;
35 import java.util.logging.Level;
37 
38 import org.openide.util.NbBundle;
40 
44 class UsbDeviceIdMapper {
45  private static final Logger logger = Logger.getLogger(UsbDeviceIdMapper.class.getName());
46  private HashMap<String, USBInfo> devices;
47  private static final String DataFile = "USB_DATA.txt"; //NON-NLS
48 
49  public UsbDeviceIdMapper() {
50  try {
51  loadDeviceMap();
52  } catch (FileNotFoundException ex) {
53  logger.log(Level.SEVERE, "Could not find file " + DataFile + ".", ex); //NON-NLS
54  devices = null;
55  } catch (IOException ex) {
56  logger.log(Level.SEVERE, "Unknown IO error occurred in method devices.", ex); //NON-NLS
57  }
58  }
59 
66  public USBInfo parseAndLookup(String dev) {
67  String[] dtokens = dev.split("[_&]");
68  String vID = dtokens[1].toUpperCase();
69  String pID;
70  if (dtokens.length < 4 || dtokens[3].length() < 4) {
71  pID = "0000";
72  } else {
73  pID = dtokens[3];
74  }
75  pID = pID.toUpperCase();
76 
77  // first try the full key
78  String key = vID + pID;
79  if (devices.containsKey(key)) {
80  return devices.get(key);
81  }
82 
83  // try just the vendor ID -> In case database doesn't know about this specific product
84  key = vID + "0000";
85  if (devices.containsKey(key)) {
86  USBInfo info = devices.get(key);
87  return new USBInfo(info.getVendor(),
88  NbBundle.getMessage(this.getClass(), "UsbDeviceIdMapper.parseAndLookup.text", pID));
89  }
90 
91  return new USBInfo(null, null);
92  }
93 
100  private void loadDeviceMap() throws FileNotFoundException, IOException {
101  devices = new HashMap<>();
102  PlatformUtil.extractResourceToUserConfigDir(this.getClass(), DataFile, false);
103  try (Scanner dat = new Scanner(new FileInputStream(new java.io.File(PlatformUtil.getUserConfigDirectory() + File.separator + "USB_DATA.txt")))) {
104  /* Syntax of file:
105  *
106  * # vendor vendor_name
107  * # device device_name <-- single tab
108  * # interface interface_name <-- two tabs
109  */
110  String line = dat.nextLine();
111  while (dat.hasNext()) {
112 
113  // comments
114  if ((line.startsWith("#")) || (line.equals(""))) {
115  line = dat.nextLine();
116  continue;
117  }
118 
119  // stop once we've hitten the part of the file that starts to talk about class types
120  if (line.startsWith("C 00")) { //NON-NLS
121  return;
122  }
123 
124  String dvc = "";
125  String[] tokens = line.split("[\\t\\s]+"); //NON-NLS
126  String vID = tokens[0];
127  for (int n = 1; n < tokens.length; n++) {
128  dvc += tokens[n] + " ";
129  }
130 
131  // make an entry with just the vendor ID
132  String pID = vID + "0000";
133  pID = pID.toUpperCase();
134  USBInfo info = new USBInfo(dvc, null);
135  devices.put(pID, info);
136 
137  // parseAndLookup the later lines that have specific products
138  line = dat.nextLine();
139  if (line.startsWith("\t")) {
140  while (dat.hasNext() && line.startsWith("\t")) {
141  tokens = line.split("[\\t\\s]+"); //NON-NLS
142 
143  // make key based on upper case version of vendor and product IDs
144  pID = vID + tokens[1];
145  pID = pID.toUpperCase();
146 
147  String device = "";
148  line = dat.nextLine();
149  for (int n = 2; n < tokens.length; n++) {
150  device += tokens[n] + " ";
151  }
152 
153  info = new USBInfo(dvc, device);
154 
155  // store based on the previously generated key
156  devices.put(pID, info);
157  }
158  }
159  }
160  }
161  }
162 
166  public class USBInfo {
167  private final String vendor;
168  private final String product;
169 
170  private USBInfo(String vend, String prod) {
171  vendor = vend;
172  product = prod;
173  }
174 
179  public String getVendor() {
180  return vendor;
181  }
182 
187  public String getProduct() {
188  return product;
189  }
190 
191  @Override
192  public String toString() {
193  return vendor + product;
194  }
195  }
196 }

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.