Autopsy  4.20.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ExtractArchiveWithPasswordAction.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2013-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.embeddedfileextractor;
20 
21 import java.awt.event.ActionEvent;
22 import java.nio.file.Paths;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.ExecutionException;
25 import java.util.logging.Level;
26 import javax.swing.AbstractAction;
27 import javax.swing.JOptionPane;
28 import javax.swing.SwingWorker;
30 import net.sf.sevenzipjbinding.SevenZipNativeInitializationException;
31 import org.openide.util.NbBundle.Messages;
32 import org.openide.windows.WindowManager;
40 import org.sleuthkit.datamodel.AbstractFile;
41 
47 public class ExtractArchiveWithPasswordAction extends AbstractAction {
48 
49  private static final long serialVersionUID = 1L;
50  private static final Logger logger = Logger.getLogger(ExtractArchiveWithPasswordAction.class.getName());
51  private final AbstractFile archiveFile;
52 
60  @Messages({"ExtractArchiveWithPasswordAction.name.text=Unzip contents with password", "ExtractArchiveWithPasswordAction.prompt.text=Enter Password",
61  "ExtractArchiveWithPasswordAction.prompt.title=Enter Password",
62  "ExtractArchiveWithPasswordAction.extractFailed.title=Failed to Unpack Files, with Password",
63  "# {0} - archiveFile",
64  "ExtractArchiveWithPasswordAction.progress.text=Unpacking contents of archive: {0}"})
65  public ExtractArchiveWithPasswordAction(AbstractFile file) {
66  super(Bundle.ExtractArchiveWithPasswordAction_name_text());
67  archiveFile = file;
68  }
69 
70  @Override
71  public void actionPerformed(ActionEvent e) {
72  String password = getPassword(Bundle.ExtractArchiveWithPasswordAction_prompt_title(), "");
73  if (password != null) {
74  ExtractAndIngestWorker extractWorker = new ExtractAndIngestWorker(password, archiveFile);
75  extractWorker.execute();
76  }
77  }
78 
79  private String getPassword(String title, String oldPassword) {
80  String password = null;
81  Object inputValue = JOptionPane.showInputDialog(WindowManager.getDefault().getMainWindow(), Bundle.ExtractArchiveWithPasswordAction_prompt_text(),
82  title, JOptionPane.PLAIN_MESSAGE, null, null, oldPassword);
83  if (inputValue != null) {
84  password = (String) inputValue;
85  }
86  return password;
87  }
88 
94  private class ExtractAndIngestWorker extends SwingWorker<Boolean, Void> {
95 
96  private final AbstractFile archive;
97  private String password;
98  private final ModalDialogProgressIndicator progress = new ModalDialogProgressIndicator(WindowManager.getDefault().getMainWindow(), "Extracting Archive");
99 
106  private ExtractAndIngestWorker(String pass, AbstractFile file) {
107  archive = file;
108  password = pass;
109  }
110 
111  @Override
112  protected Boolean doInBackground() {
113  boolean done = false;
114  try {
115  String moduleDirRelative = Paths.get(Case.getCurrentCaseThrows().getModuleOutputDirectoryRelativePath(), EmbeddedFileExtractorModuleFactory.getOutputFolderName()).toString();
116  String moduleDirAbsolute = Paths.get(Case.getCurrentCaseThrows().getModuleDirectory(), EmbeddedFileExtractorModuleFactory.getOutputFolderName()).toString();
117  /*
118  * Construct a file type detector.
119  */
120  progress.start(Bundle.ExtractArchiveWithPasswordAction_progress_text(archive.getName()));
121  FileTypeDetector fileTypeDetector;
122  try {
123  fileTypeDetector = new FileTypeDetector();
125  return false;
126  }
127  try {
128  SevenZipExtractor extractor = new SevenZipExtractor(null, fileTypeDetector, moduleDirRelative, moduleDirAbsolute, new FileTaskExecutor(null));
129  done = extractor.unpack(archive, new ConcurrentHashMap<>(), password);
130  } catch (SevenZipNativeInitializationException ex) {
131  IngestServices.getInstance().postMessage(IngestMessage.createWarningMessage(EmbeddedFileExtractorModuleFactory.getModuleName(), "Unable to extract file with password", password));
132  logger.log(Level.INFO, "Unable to extract file with password", ex);
133  return done;
134  }
135  } catch (NoCurrentCaseException ex) {
136  logger.log(Level.SEVERE, "Error getting open case unable to perform extraction action", ex);
137  } finally {
138  progress.finish();
139  }
140  return done;
141  }
142 
143  @Override
144  protected void done() {
145  boolean done = false;
146  try {
147  done = get();
148  while (!done) {
149  password = getPassword(Bundle.ExtractArchiveWithPasswordAction_extractFailed_title(), password);
150  if (password == null) {
151  //allow them to cancel if they don't know the correct password
152  return;
153  }
154  done = doInBackground();
155  }
156  } catch (InterruptedException ex) {
157  logger.log(Level.SEVERE, "Unable to extract archive successfully", ex);
158  } catch (ExecutionException ex) {
159  logger.log(Level.SEVERE, "Execution Exception: Unable to extract archive successfully", ex.getCause());
160  }
161  if (done) {
162  RunIngestModulesAction runIngest = new RunIngestModulesAction(archive);
163  runIngest.actionPerformed(null);
164  }
165  }
166  }
167 }
synchronized void start(String message, int totalWorkUnits)
void postMessage(final IngestMessage message)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static IngestMessage createWarningMessage(String source, String subject, String detailsHtml)
static synchronized IngestServices getInstance()

Copyright © 2012-2022 Basis Technology. Generated on: Tue Aug 1 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.