Autopsy 4.22.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 */
27package org.sleuthkit.autopsy.recentactivity;
28
29import java.io.File;
30import java.io.FileInputStream;
31import java.io.FileNotFoundException;
32import java.io.IOException;
33import java.util.HashMap;
34import java.util.Scanner;
35import java.util.logging.Level;
36import org.sleuthkit.autopsy.coreutils.Logger;
37
38import org.openide.util.NbBundle;
39import org.sleuthkit.autopsy.coreutils.PlatformUtil;
40
45class UsbDeviceIdMapper {
46
47 private static final Logger logger = Logger.getLogger(UsbDeviceIdMapper.class.getName());
48 private HashMap<String, USBInfo> devices;
49 private static final String DataFile = "USB_DATA.txt"; //NON-NLS
50
51 public UsbDeviceIdMapper() {
52 try {
53 loadDeviceMap();
54 } catch (FileNotFoundException ex) {
55 logger.log(Level.SEVERE, "Could not find file " + DataFile + ".", ex); //NON-NLS
56 devices = null;
57 } catch (IOException ex) {
58 logger.log(Level.SEVERE, "Unknown IO error occurred in method devices.", ex); //NON-NLS
59 }
60 }
61
70 public USBInfo parseAndLookup(String dev) {
71 String[] dtokens = dev.split("[_&]");
72 String vID = dtokens[1].toUpperCase();
73 String pID;
74 if (dtokens.length < 4 || dtokens[3].length() < 4) {
75 pID = "0000";
76 } else {
77 pID = dtokens[3];
78 }
79 pID = pID.toUpperCase();
80
81 // first try the full key
82 String key = vID + pID;
83 if (devices.containsKey(key)) {
84 return devices.get(key);
85 }
86
87 // try just the vendor ID -> In case database doesn't know about this specific product
88 key = vID + "0000";
89 if (devices.containsKey(key)) {
90 USBInfo info = devices.get(key);
91 return new USBInfo(info.getVendor(),
92 NbBundle.getMessage(this.getClass(), "UsbDeviceIdMapper.parseAndLookup.text", pID));
93 }
94
95 return new USBInfo(null, null);
96 }
97
104 private void loadDeviceMap() throws FileNotFoundException, IOException {
105 devices = new HashMap<>();
106 PlatformUtil.extractResourceToUserConfigDir(this.getClass(), DataFile, false);
107 try (Scanner dat = new Scanner(new FileInputStream(new java.io.File(PlatformUtil.getUserConfigDirectory() + File.separator + "USB_DATA.txt")))) {
108 /*
109 * Syntax of file:
110 *
111 * # vendor vendor_name # device device_name <-- single tab #
112 * interface interface_name <-- two tabs
113 */
114 String line = dat.nextLine();
115 while (dat.hasNext()) {
116
117 // comments
118 if ((line.startsWith("#")) || (line.equals(""))) {
119 line = dat.nextLine();
120 continue;
121 }
122
123 // stop once we've hitten the part of the file that starts to talk about class types
124 if (line.startsWith("C 00")) { //NON-NLS
125 return;
126 }
127
128 String dvc = "";
129 String[] tokens = line.split("[\\t\\s]+"); //NON-NLS
130 String vID = tokens[0];
131 for (int n = 1; n < tokens.length; n++) {
132 dvc += tokens[n] + " ";
133 }
134
135 // make an entry with just the vendor ID
136 String pID = vID + "0000";
137 pID = pID.toUpperCase();
138 USBInfo info = new USBInfo(dvc, null);
139 devices.put(pID, info);
140
141 // parseAndLookup the later lines that have specific products
142 line = dat.nextLine();
143 if (line.startsWith("\t")) {
144 while (dat.hasNext() && line.startsWith("\t")) {
145 tokens = line.split("[\\t\\s]+"); //NON-NLS
146
147 // make key based on upper case version of vendor and product IDs
148 pID = vID + tokens[1];
149 pID = pID.toUpperCase();
150
151 String device = "";
152 line = dat.nextLine();
153 for (int n = 2; n < tokens.length; n++) {
154 device += tokens[n] + " ";
155 }
156
157 info = new USBInfo(dvc, device);
158
159 // store based on the previously generated key
160 devices.put(pID, info);
161 }
162 }
163 }
164 }
165 }
166
170 public class USBInfo {
171
172 private final String vendor;
173 private final String product;
174
175 private USBInfo(String vend, String prod) {
176 vendor = vend;
177 product = prod;
178 }
179
185 public String getVendor() {
186 return vendor;
187 }
188
194 public String getProduct() {
195 return product;
196 }
197
198 @Override
199 public String toString() {
200 return vendor + product;
201 }
202 }
203}

Copyright © 2012-2024 Sleuth Kit Labs. Generated on:
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.