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