Autopsy  4.6.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
HashDbSearchPanel.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.awt.Color;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.awt.event.KeyAdapter;
25 import java.awt.event.KeyEvent;
26 import java.util.ArrayList;
27 
28 import org.openide.util.NbBundle;
30 import javax.swing.JOptionPane;
31 import javax.swing.table.DefaultTableModel;
32 import javax.swing.text.AttributeSet;
33 import javax.swing.text.BadLocationException;
34 import javax.swing.text.PlainDocument;
37 
41 class HashDbSearchPanel extends javax.swing.JPanel implements ActionListener {
42 
43  private static final Logger logger = Logger.getLogger(HashDbSearchPanel.class.getName());
44  private static HashDbSearchPanel instance;
45 
49  public static HashDbSearchPanel getDefault() {
50  if (instance == null) {
51  instance = new HashDbSearchPanel();
52  }
53  return instance;
54  }
55 
59  public void refresh() {
60  boolean running = IngestManager.getInstance().isIngestRunning();
61  if (running) {
62  titleLabel.setForeground(Color.red);
63  titleLabel.setText(NbBundle.getMessage(this.getClass(), "HashDbSearchPanel.titleText.ingestOngoing"));
64  } else {
65  titleLabel.setForeground(Color.black);
66  titleLabel.setText(NbBundle.getMessage(this.getClass(), "HashDbSearchPanel.titleLabel.text"));
67  }
68  hashField.setEditable(!running);
69  searchButton.setEnabled(!running);
70  addButton.setEnabled(!running);
71  removeButton.setEnabled(!running);
72  hashTable.setEnabled(!running);
73  hashLabel.setEnabled(!running);
74  saveBox.setEnabled(!running);
75  }
76 
80  private HashDbSearchPanel() {
81  setName(HashDbPanelSearchAction.ACTION_NAME);
82  initComponents();
83  customInit();
84  }
85 
86  final void customInit() {
87  addButton.addActionListener(this);
88  removeButton.addActionListener(this);
89  errorField.setVisible(false);
90  hashField.requestFocus();
91  // Don't let the user input more characters than in an MD5 hash
92  hashField.setDocument(new PlainDocument() {
93  @Override
94  public void insertString(int offset, String str, AttributeSet a) throws BadLocationException {
95  if ((this.getLength() + str.length()) <= 32) {
96  super.insertString(offset, str, a);
97  }
98  }
99  });
100  // Pressing enter adds the hash
101  hashField.addKeyListener(new KeyAdapter() {
102  @Override
103  public void keyPressed(KeyEvent e) {
104  if (e.getKeyChar() == KeyEvent.VK_ENTER) {
105  addButton.doClick();
106  }
107  }
108  });
109  // Pressing delete removes the selected rows
110  hashTable.addKeyListener(new KeyAdapter() {
111  @Override
112  public void keyPressed(KeyEvent e) {
113  if (e.getKeyChar() == KeyEvent.VK_DELETE) {
114  removeButton.doClick();
115  }
116  }
117  });
118  }
119 
120  void addSearchActionListener(ActionListener l) {
121  for (ActionListener al : searchButton.getActionListeners()) {
122  searchButton.removeActionListener(al);
123  }
124  searchButton.addActionListener(l);
125  }
126 
127  void addCancelActionListener(ActionListener l) {
128  cancelButton.addActionListener(l);
129  }
130 
136  @SuppressWarnings("rawtypes")
137  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
138  private void initComponents() {
139 
140  jScrollPane1 = new javax.swing.JScrollPane();
141  hashTable = new javax.swing.JTable();
142  hashField = new javax.swing.JTextField();
143  addButton = new javax.swing.JButton();
144  hashLabel = new javax.swing.JLabel();
145  searchButton = new javax.swing.JButton();
146  removeButton = new javax.swing.JButton();
147  jSeparator1 = new javax.swing.JSeparator();
148  titleLabel = new javax.swing.JLabel();
149  errorField = new javax.swing.JLabel();
150  saveBox = new javax.swing.JCheckBox();
151  cancelButton = new javax.swing.JButton();
152 
153  hashTable.setModel(new javax.swing.table.DefaultTableModel(
154  new Object [][] {
155 
156  },
157  new String [] {
158  "MD5 Hashes"
159  }
160  ) {
161  Class[] types = new Class [] {
162  java.lang.String.class
163  };
164  boolean[] canEdit = new boolean [] {
165  false
166  };
167 
168  public Class getColumnClass(int columnIndex) {
169  return types [columnIndex];
170  }
171 
172  public boolean isCellEditable(int rowIndex, int columnIndex) {
173  return canEdit [columnIndex];
174  }
175  });
176  jScrollPane1.setViewportView(hashTable);
177  if (hashTable.getColumnModel().getColumnCount() > 0) {
178  hashTable.getColumnModel().getColumn(0).setHeaderValue(org.openide.util.NbBundle.getMessage(HashDbSearchPanel.class, "HashDbSearchPanel.hashTable.columnModel.title0")); // NOI18N
179  }
180 
181  hashField.setText(org.openide.util.NbBundle.getMessage(HashDbSearchPanel.class, "HashDbSearchPanel.hashField.text")); // NOI18N
182 
183  org.openide.awt.Mnemonics.setLocalizedText(addButton, org.openide.util.NbBundle.getMessage(HashDbSearchPanel.class, "HashDbSearchPanel.addButton.text")); // NOI18N
184 
185  org.openide.awt.Mnemonics.setLocalizedText(hashLabel, org.openide.util.NbBundle.getMessage(HashDbSearchPanel.class, "HashDbSearchPanel.hashLabel.text")); // NOI18N
186 
187  org.openide.awt.Mnemonics.setLocalizedText(searchButton, org.openide.util.NbBundle.getMessage(HashDbSearchPanel.class, "HashDbSearchPanel.searchButton.text")); // NOI18N
188 
189  org.openide.awt.Mnemonics.setLocalizedText(removeButton, org.openide.util.NbBundle.getMessage(HashDbSearchPanel.class, "HashDbSearchPanel.removeButton.text")); // NOI18N
190 
191  org.openide.awt.Mnemonics.setLocalizedText(titleLabel, org.openide.util.NbBundle.getMessage(HashDbSearchPanel.class, "HashDbSearchPanel.titleLabel.text")); // NOI18N
192 
193  errorField.setForeground(new java.awt.Color(255, 0, 0));
194  org.openide.awt.Mnemonics.setLocalizedText(errorField, org.openide.util.NbBundle.getMessage(HashDbSearchPanel.class, "HashDbSearchPanel.errorField.text")); // NOI18N
195 
196  org.openide.awt.Mnemonics.setLocalizedText(saveBox, org.openide.util.NbBundle.getMessage(HashDbSearchPanel.class, "HashDbSearchPanel.saveBox.text")); // NOI18N
197  saveBox.addActionListener(new java.awt.event.ActionListener() {
198  public void actionPerformed(java.awt.event.ActionEvent evt) {
199  saveBoxActionPerformed(evt);
200  }
201  });
202 
203  org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(HashDbSearchPanel.class, "HashDbSearchPanel.cancelButton.text")); // NOI18N
204 
205  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
206  this.setLayout(layout);
207  layout.setHorizontalGroup(
208  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
209  .addGroup(layout.createSequentialGroup()
210  .addContainerGap()
211  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
212  .addComponent(jScrollPane1)
213  .addGroup(layout.createSequentialGroup()
214  .addComponent(hashLabel)
215  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
216  .addComponent(hashField))
217  .addComponent(jSeparator1)
218  .addGroup(layout.createSequentialGroup()
219  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
220  .addComponent(titleLabel)
221  .addGroup(layout.createSequentialGroup()
222  .addGap(61, 61, 61)
223  .addComponent(addButton)
224  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
225  .addComponent(removeButton)
226  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
227  .addComponent(saveBox)))
228  .addGap(0, 0, Short.MAX_VALUE))
229  .addGroup(layout.createSequentialGroup()
230  .addComponent(errorField)
231  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
232  .addComponent(searchButton)
233  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
234  .addComponent(cancelButton)))
235  .addContainerGap())
236  );
237  layout.setVerticalGroup(
238  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
239  .addGroup(layout.createSequentialGroup()
240  .addContainerGap()
241  .addComponent(titleLabel)
242  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
243  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
244  .addGap(18, 18, 18)
245  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
246  .addComponent(hashLabel)
247  .addComponent(hashField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
248  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
249  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
250  .addComponent(addButton)
251  .addComponent(removeButton)
252  .addComponent(saveBox))
253  .addGap(18, 18, 18)
254  .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
255  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
256  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
257  .addComponent(searchButton)
258  .addComponent(errorField)
259  .addComponent(cancelButton))
260  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
261  );
262  }// </editor-fold>//GEN-END:initComponents
263 
264  private void saveBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBoxActionPerformed
265  // TODO add your handling code here:
266  }//GEN-LAST:event_saveBoxActionPerformed
267 
268  // Variables declaration - do not modify//GEN-BEGIN:variables
269  private javax.swing.JButton addButton;
270  private javax.swing.JButton cancelButton;
271  private javax.swing.JLabel errorField;
272  private javax.swing.JTextField hashField;
273  private javax.swing.JLabel hashLabel;
274  private javax.swing.JTable hashTable;
275  private javax.swing.JScrollPane jScrollPane1;
276  private javax.swing.JSeparator jSeparator1;
277  private javax.swing.JButton removeButton;
278  private javax.swing.JCheckBox saveBox;
279  private javax.swing.JButton searchButton;
280  private javax.swing.JLabel titleLabel;
281  // End of variables declaration//GEN-END:variables
282 
283  @Override
284  public void actionPerformed(ActionEvent e) {
285  if (e.getSource().equals(addButton)) {
286  add();
287  } else if (e.getSource().equals(removeButton)) {
288  remove();
289  }
290  }
291 
296  @NbBundle.Messages ({
297  "HashDbSearchPanel.noOpenCase.errMsg=No open case available."
298  })
299  boolean search() {
300  // Check if any hashed have been entered
301  if (hashTable.getRowCount() != 0) {
302  // Make sure at least 1 file has an md5 hash
303  try {
304  if (HashDbSearcher.countFilesMd5Hashed() > 0) {
305  return doSearch();
306  } else {
307  JOptionPane.showMessageDialog(this,
308  NbBundle.getMessage(this.getClass(),
309  "HashDbSearchPanel.noFilesHaveMD5HashMsg"),
310  NbBundle.getMessage(this.getClass(), "HashDbSearchPanel.dlgMsg.title"),
311  JOptionPane.ERROR_MESSAGE);
312  return false;
313  }
314  } catch (NoCurrentCaseException ex) {
315  JOptionPane.showMessageDialog(this,
316  Bundle.HashDbSearchPanel_noOpenCase_errMsg(),
317  NbBundle.getMessage(this.getClass(), "HashDbSearchPanel.dlgMsg.title"),
318  JOptionPane.ERROR_MESSAGE);
319  return false;
320  }
321  } else {
322  errorField.setText(NbBundle.getMessage(this.getClass(), "HashDbSearchPanel.errorText.noHashesAddedMsg"));
323  errorField.setVisible(true);
324  return false;
325  }
326  }
327 
328  private boolean doSearch() {
329  errorField.setVisible(false);
330  // Get all the rows in the table
331  int numRows = hashTable.getRowCount();
332  ArrayList<String> hashes = new ArrayList<String>();
333  for (int i = 0; i < numRows; i++) {
334  hashes.add((String) hashTable.getValueAt(i, 0));
335  }
336  // Start a new thread and find the hashes
337  HashDbSearchThread hashThread = new HashDbSearchThread(hashes);
338  hashThread.execute();
339  return true;
340  }
341 
345  void add() {
346  errorField.setVisible(false);
347  DefaultTableModel model = (DefaultTableModel) hashTable.getModel();
348  String hash = hashField.getText();
349  if (!hash.equals("")) {
350  if (hash.matches("[a-fA-F0-9]{32}")) {
351  for (int i = 0; i < model.getRowCount(); i++) {
352  if (model.getValueAt(i, 0).equals(hashField.getText())) {
353  hashField.setText("");
354  errorField.setText(
355  NbBundle.getMessage(this.getClass(), "HashDbSearchPanel.errorText.hashAlreadyAddedMsg"));
356  errorField.setVisible(true);
357  errorField.setVisible(true);
358  return;
359  }
360  }
361  model.addRow(new String[]{hash});
362  hashField.setText(""); // wipe the field
363  } else {
364  errorField.setText(NbBundle.getMessage(this.getClass(), "HashDbSearchPanel.errorText.invalidMD5HashMsg"));
365  errorField.setVisible(true);
366  }
367  }
368  hashField.requestFocus(); // select the field to type in
369  }
370 
374  void remove() {
375  DefaultTableModel model = (DefaultTableModel) hashTable.getModel();
376  int rows[] = hashTable.getSelectedRows();
377  // Loop backwards to delete highest row index first, otherwise
378  // index numbers change and the wrong rows are deleted
379  for (int i = rows.length - 1; i >= 0; i--) {
380  model.removeRow(rows[i]);
381  }
382  }
383 
387  void clear() {
388  if (!saveBox.isSelected()) {
389  DefaultTableModel model = (DefaultTableModel) hashTable.getModel();
390  int numRows = hashTable.getRowCount();
391  for (int i = numRows - 1; i >= 0; i--) {
392  model.removeRow(i);
393  }
394  }
395  errorField.setVisible(false);
396  hashField.setText("");
397  }
398 }

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.