Autopsy  4.9.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
HashLookupModuleSettingsPanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2018 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.hashdatabase;
20 
21 import java.beans.PropertyChangeEvent;
22 import java.beans.PropertyChangeListener;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.logging.Level;
26 import javax.swing.JScrollPane;
27 import javax.swing.JTable;
28 import javax.swing.table.AbstractTableModel;
29 import javax.swing.table.TableColumn;
33 import org.sleuthkit.datamodel.TskCoreException;
35 
36 
40 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
41 public final class HashLookupModuleSettingsPanel extends IngestModuleIngestJobSettingsPanel implements PropertyChangeListener {
42 
43  private static final long serialVersionUID = 1L;
44  private final HashDbManager hashDbManager = HashDbManager.getInstance();
45  private final List<HashSetModel> knownHashSetModels = new ArrayList<>();
46  private final HashSetsTableModel knownHashSetsTableModel = new HashSetsTableModel(knownHashSetModels);
47  private final List<HashSetModel> knownBadHashSetModels = new ArrayList<>();
48  private final HashSetsTableModel knownBadHashSetsTableModel = new HashSetsTableModel(knownBadHashSetModels);
49 
50  HashLookupModuleSettingsPanel(HashLookupModuleSettings settings) {
51  initializeHashSetModels(settings);
52  initComponents();
53  customizeComponents(settings);
54  }
55 
56  private void initializeHashSetModels(HashLookupModuleSettings settings) {
57  initializeHashSetModels(settings, validSetsOnly(hashDbManager.getKnownFileHashSets()), knownHashSetModels);
58  initializeHashSetModels(settings, validSetsOnly(hashDbManager.getKnownBadFileHashSets()), knownBadHashSetModels);
59  }
60 
61  private void initializeHashSetModels(HashLookupModuleSettings settings, List<HashDb> hashDbs, List<HashSetModel> hashSetModels) {
62  hashSetModels.clear();
63  for (HashDb db : hashDbs) {
64  hashSetModels.add(new HashSetModel(db, settings.isHashSetEnabled(db), isHashDbValid(db)));
65  }
66  }
67 
68  private void customizeComponents(HashLookupModuleSettings settings) {
69  customizeHashSetsTable(jScrollPane1, knownHashTable, knownHashSetsTableModel);
70  customizeHashSetsTable(jScrollPane2, knownBadHashTable, knownBadHashSetsTableModel);
71  alwaysCalcHashesCheckbox.setSelected(settings.shouldCalculateHashes());
72  hashDbManager.addPropertyChangeListener(this);
73  alwaysCalcHashesCheckbox.setText("<html>" + org.openide.util.NbBundle.getMessage(HashLookupModuleSettingsPanel.class, "HashLookupModuleSettingsPanel.alwaysCalcHashesCheckbox.text") + "</html>"); // NOI18N NON-NLS
74  }
75 
76  private void customizeHashSetsTable(JScrollPane scrollPane, JTable table, HashSetsTableModel tableModel) {
77  table.setModel(tableModel);
78  table.setTableHeader(null);
79  table.setRowSelectionAllowed(false);
80  final int width1 = scrollPane.getPreferredSize().width;
81  knownHashTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
82  TableColumn column;
83  for (int i = 0; i < table.getColumnCount(); i++) {
84  column = table.getColumnModel().getColumn(i);
85  if (i == 0) {
86  column.setPreferredWidth(((int) (width1 * 0.07)));
87  } else {
88  column.setPreferredWidth(((int) (width1 * 0.92)));
89  }
90  }
91  }
92 
93  @Override
94  public void propertyChange(PropertyChangeEvent event) {
95  if (event.getPropertyName().equals(HashDbManager.SetEvt.DB_ADDED.name())
96  || event.getPropertyName().equals(HashDbManager.SetEvt.DB_DELETED.name())
97  || event.getPropertyName().equals(HashDbManager.SetEvt.DB_INDEXED.name())) {
98  update();
99  }
100  }
101 
102  @Override
104  List<HashDb> enabledHashSets = new ArrayList<>();
105  List<HashDb> disabledHashSets = new ArrayList<>();
106  addHashSets(knownHashSetModels, enabledHashSets, disabledHashSets);
107  addHashSets(knownBadHashSetModels, enabledHashSets, disabledHashSets);
108  return new HashLookupModuleSettings(alwaysCalcHashesCheckbox.isSelected(),
109  enabledHashSets, disabledHashSets);
110  }
111 
112  private void addHashSets(List<HashSetModel> hashSetModels, List<HashDb> enabledHashSets, List<HashDb> disabledHashSets) {
113  for (HashSetModel model : hashSetModels) {
114  if (model.isEnabled() && model.isValid()) {
115  enabledHashSets.add(model.getDatabase());
116  } else {
117  disabledHashSets.add(model.getDatabase());
118  }
119  }
120  }
121 
122  void update() {
123  updateHashSetModels();
124  knownHashSetsTableModel.fireTableDataChanged();
125  knownBadHashSetsTableModel.fireTableDataChanged();
126  }
127 
128  private void updateHashSetModels() {
129  updateHashSetModels(validSetsOnly(hashDbManager.getKnownFileHashSets()), knownHashSetModels);
130  updateHashSetModels(validSetsOnly(hashDbManager.getKnownBadFileHashSets()), knownBadHashSetModels);
131  }
132 
133  private List<HashDb> validSetsOnly(List<HashDb> hashDbs){
134  List<HashDb> validDbs = new ArrayList<>();
135  for(HashDb db:hashDbs){
136  try{
137  if(db.isValid()){
138  validDbs.add(db);
139  }
140  } catch (TskCoreException ex){
141  Logger.getLogger(HashLookupModuleSettingsPanel.class.getName()).log(Level.SEVERE, "Error checking validity for hash set (name = " + db.getHashSetName() + ")", ex); //NON-NLS
142  }
143  }
144  return validDbs;
145  }
146 
147  void updateHashSetModels(List<HashDb> hashDbs, List<HashSetModel> hashSetModels) {
148 
149  List<HashDb> hashDatabases = new ArrayList<>(hashDbs);
150 
151  // Update the hash sets and detect deletions.
152  List<HashSetModel> deletedHashSetModels = new ArrayList<>();
153  for (HashSetModel model : hashSetModels) {
154  boolean foundDatabase = false;
155  for(HashDb db : hashDatabases){
156  if(model.getDatabase().equals(db)){
157  model.setValid(isHashDbValid(db));
158  hashDatabases.remove(db);
159  foundDatabase = true;
160  break;
161  }
162  }
163  if(! foundDatabase){
164  deletedHashSetModels.add(model);
165  }
166  }
167 
168  // Remove the deleted hash sets.
169  for (HashSetModel model : deletedHashSetModels) {
170  hashSetModels.remove(model);
171  }
172 
173  // Add any new hash sets. All new sets are enabled by default.
174  for (HashDb db : hashDatabases) {
175  hashSetModels.add(new HashSetModel(db, true, isHashDbValid(db)));
176  }
177  }
178 
179  void reset(HashLookupModuleSettings newSettings) {
180  initializeHashSetModels(newSettings);
181  alwaysCalcHashesCheckbox.setSelected(newSettings.shouldCalculateHashes());
182  knownHashSetsTableModel.fireTableDataChanged();
183  knownBadHashSetsTableModel.fireTableDataChanged();
184  }
185 
186  private boolean isHashDbValid(HashDb hashDb) {
187  boolean isValid = false;
188  try {
189  isValid = hashDb.isValid();
190  } catch (TskCoreException ex) {
191  Logger.getLogger(HashLookupModuleSettingsPanel.class.getName()).log(Level.SEVERE, "Error checking validity for hash set (name = " + hashDb.getHashSetName() + ")", ex); //NON-NLS
192  }
193  return isValid;
194  }
195 
196  private static final class HashSetModel {
197 
198  private final HashDb db;
199  private boolean valid;
200  private boolean enabled;
201 
202  HashSetModel(HashDb db, boolean enabled, boolean valid) {
203  this.db = db;
204  this.enabled = enabled;
205  this.valid = valid;
206  }
207 
208  HashDb getDatabase(){
209  return db;
210  }
211 
212  String getName() {
213  return db.getDisplayName();
214  }
215 
216  void setEnabled(boolean enabled) {
217  this.enabled = enabled;
218  }
219 
220  boolean isEnabled() {
221  return enabled;
222  }
223 
224  void setValid(boolean valid) {
225  this.valid = valid;
226  }
227 
228  boolean isValid() {
229  return valid;
230  }
231  }
232 
233  private static final class HashSetsTableModel extends AbstractTableModel {
234 
235  private static final long serialVersionUID = 1L;
236  private final List<HashSetModel> hashSets;
237 
238  HashSetsTableModel(List<HashSetModel> hashSets) {
239  this.hashSets = hashSets;
240  }
241 
242  @Override
243  public int getRowCount() {
244  return hashSets.size();
245  }
246 
247  @Override
248  public int getColumnCount() {
249  return 2;
250  }
251 
252  @Override
253  public Object getValueAt(int rowIndex, int columnIndex) {
254  if (columnIndex == 0) {
255  return hashSets.get(rowIndex).isEnabled();
256  } else {
257  return hashSets.get(rowIndex).getName();
258  }
259  }
260 
261  @Override
262  public boolean isCellEditable(int rowIndex, int columnIndex) {
263  return (columnIndex == 0 && hashSets.get(rowIndex).isValid());
264  }
265 
266  @Override
267  public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
268  if (columnIndex == 0) {
269  hashSets.get(rowIndex).setEnabled((Boolean) aValue);
270  }
271  }
272 
273  @Override
274  public Class<?> getColumnClass(int c) {
275  return getValueAt(0, c).getClass();
276  }
277  }
278 
284  @SuppressWarnings("unchecked")
285  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
286  private void initComponents() {
287 
288  jScrollPane1 = new javax.swing.JScrollPane();
289  knownHashTable = new javax.swing.JTable();
290  knownBadHashDbsLabel = new javax.swing.JLabel();
291  knownHashDbsLabel = new javax.swing.JLabel();
292  alwaysCalcHashesCheckbox = new javax.swing.JCheckBox();
293  jScrollPane2 = new javax.swing.JScrollPane();
294  knownBadHashTable = new javax.swing.JTable();
295 
296  setPreferredSize(new java.awt.Dimension(292, 150));
297 
298  jScrollPane1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
299 
300  knownHashTable.setBackground(new java.awt.Color(240, 240, 240));
301  knownHashTable.setShowHorizontalLines(false);
302  knownHashTable.setShowVerticalLines(false);
303  jScrollPane1.setViewportView(knownHashTable);
304 
305  knownBadHashDbsLabel.setText(org.openide.util.NbBundle.getMessage(HashLookupModuleSettingsPanel.class, "HashLookupModuleSettingsPanel.knownBadHashDbsLabel.text")); // NOI18N
306 
307  knownHashDbsLabel.setText(org.openide.util.NbBundle.getMessage(HashLookupModuleSettingsPanel.class, "HashLookupModuleSettingsPanel.knownHashDbsLabel.text")); // NOI18N
308 
309  alwaysCalcHashesCheckbox.setText(org.openide.util.NbBundle.getMessage(HashLookupModuleSettingsPanel.class, "HashLookupModuleSettingsPanel.alwaysCalcHashesCheckbox.text")); // NOI18N
310  alwaysCalcHashesCheckbox.setToolTipText(org.openide.util.NbBundle.getMessage(HashLookupModuleSettingsPanel.class, "HashLookupModuleSettingsPanel.alwaysCalcHashesCheckbox.toolTipText")); // NOI18N
311  alwaysCalcHashesCheckbox.setMaximumSize(new java.awt.Dimension(290, 35));
312  alwaysCalcHashesCheckbox.setMinimumSize(new java.awt.Dimension(290, 35));
313  alwaysCalcHashesCheckbox.setPreferredSize(new java.awt.Dimension(271, 35));
314  alwaysCalcHashesCheckbox.setVerticalAlignment(javax.swing.SwingConstants.TOP);
315  alwaysCalcHashesCheckbox.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
316 
317  jScrollPane2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
318 
319  knownBadHashTable.setBackground(new java.awt.Color(240, 240, 240));
320  knownBadHashTable.setModel(new javax.swing.table.DefaultTableModel(
321  new Object [][] {
322 
323  },
324  new String [] {
325 
326  }
327  ));
328  knownBadHashTable.setShowHorizontalLines(false);
329  knownBadHashTable.setShowVerticalLines(false);
330  jScrollPane2.setViewportView(knownBadHashTable);
331 
332  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
333  this.setLayout(layout);
334  layout.setHorizontalGroup(
335  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
336  .addGroup(layout.createSequentialGroup()
337  .addContainerGap()
338  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
339  .addGroup(layout.createSequentialGroup()
340  .addComponent(knownHashDbsLabel)
341  .addGap(0, 0, Short.MAX_VALUE))
342  .addComponent(knownBadHashDbsLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE)
343  .addGroup(layout.createSequentialGroup()
344  .addGap(10, 10, 10)
345  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
346  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
347  .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
348  .addComponent(alwaysCalcHashesCheckbox, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
349  .addContainerGap())
350  );
351  layout.setVerticalGroup(
352  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
353  .addGroup(layout.createSequentialGroup()
354  .addGap(2, 2, 2)
355  .addComponent(knownHashDbsLabel)
356  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
357  .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
358  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
359  .addComponent(knownBadHashDbsLabel)
360  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
361  .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
362  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
363  .addComponent(alwaysCalcHashesCheckbox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
364  .addGap(0, 0, 0))
365  );
366  }// </editor-fold>//GEN-END:initComponents
367 
368  // Variables declaration - do not modify//GEN-BEGIN:variables
369  private javax.swing.JCheckBox alwaysCalcHashesCheckbox;
370  private javax.swing.JScrollPane jScrollPane1;
371  private javax.swing.JScrollPane jScrollPane2;
372  private javax.swing.JLabel knownBadHashDbsLabel;
373  private javax.swing.JTable knownBadHashTable;
374  private javax.swing.JLabel knownHashDbsLabel;
375  private javax.swing.JTable knownHashTable;
376  // End of variables declaration//GEN-END:variables
377 }
synchronized void addPropertyChangeListener(PropertyChangeListener listener)
void initializeHashSetModels(HashLookupModuleSettings settings, List< HashDb > hashDbs, List< HashSetModel > hashSetModels)
void addHashSets(List< HashSetModel > hashSetModels, List< HashDb > enabledHashSets, List< HashDb > disabledHashSets)
void customizeHashSetsTable(JScrollPane scrollPane, JTable table, HashSetsTableModel tableModel)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2018 Basis Technology. Generated on: Tue Dec 18 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.