Autopsy  4.18.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
OsAccountDataPanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2021 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.contentviewers.osaccount;
20 
21 import java.awt.BorderLayout;
22 import java.awt.GridBagConstraints;
23 import java.awt.GridBagLayout;
24 import java.awt.Insets;
25 import java.text.SimpleDateFormat;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.HashMap;
29 import java.util.List;
30 import static java.util.Locale.US;
31 import java.util.Map;
32 import java.util.Optional;
33 import java.util.concurrent.ExecutionException;
34 import java.util.logging.Level;
35 import java.util.logging.Logger;
36 import javax.swing.Box;
37 import javax.swing.JLabel;
38 import javax.swing.JPanel;
39 import javax.swing.SwingWorker;
40 import javax.swing.border.EmptyBorder;
41 import org.openide.util.NbBundle.Messages;
45 import org.sleuthkit.datamodel.BlackboardAttribute;
46 import org.sleuthkit.datamodel.DataSource;
47 import org.sleuthkit.datamodel.Host;
48 import org.sleuthkit.datamodel.OsAccount;
49 import org.sleuthkit.datamodel.OsAccount.OsAccountAttribute;
50 import org.sleuthkit.datamodel.OsAccountInstance;
51 import org.sleuthkit.datamodel.OsAccountManager;
52 import org.sleuthkit.datamodel.OsAccountRealm;
53 import org.sleuthkit.datamodel.SleuthkitCase;
54 
58 public class OsAccountDataPanel extends JPanel {
59 
60  private static final long serialVersionUID = 1L;
61  final private static Logger logger = Logger.getLogger(OsAccountDataPanel.class.getName());
62 
63  private static final int KEY_COLUMN = 0;
64  private static final int VALUE_COLUMN = 1;
65 
66  private final static Insets FIRST_HEADER_INSETS = new Insets(0, 0, 0, 0);
67  private final static Insets HEADER_INSETS = new Insets(ContentViewerDefaults.getSectionSpacing(), 0, ContentViewerDefaults.getLineSpacing(), 0);
70  private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MMM dd yyyy", US);
71 
72  private PanelDataFetcher dataFetcher = null;
73 
74  // Panel constructor.
76  initialize();
77  }
78 
82  private void initialize() {
83  this.setLayout(new GridBagLayout());
84  this.setBorder(new EmptyBorder(ContentViewerDefaults.getPanelInsets()));
85  }
86 
93  void setOsAccountId(Long osAccountId) {
94  removeAll();
95  revalidate();
96 
97  if (osAccountId != null) {
98  setLayout(new BorderLayout());
99  add(new JLabel("Loading OsAccount Data..."), BorderLayout.NORTH);
100 
101  if (dataFetcher != null && !dataFetcher.isDone()) {
102  dataFetcher.cancel(true);
103  }
104 
105  dataFetcher = new PanelDataFetcher(osAccountId);
106  dataFetcher.execute();
107  }
108  }
109 
110  void setOsAccount(OsAccount account) {
111  removeAll();
112  revalidate();
113 
114  if (account != null) {
115  setLayout(new BorderLayout());
116  add(new JLabel("Loading OsAccount Data..."), BorderLayout.NORTH);
117 
118  if (dataFetcher != null && !dataFetcher.isDone()) {
119  dataFetcher.cancel(true);
120  }
121 
122  dataFetcher = new PanelDataFetcher(account);
123  dataFetcher.execute();
124  }
125  }
126 
133  private void addDataComponents(List<SectionData> panelData) {
134  int rowCnt = 0;
135  for (SectionData section : panelData) {
136  addTitle(section.getTitle(), rowCnt++);
137 
138  for (RowData<String, String> rowData : section) {
139  String key = rowData.getKey();
140  String value = rowData.getValue();
141 
142  addPropertyName(key, rowCnt);
143  addPropertyValue(value, rowCnt++);
144  }
145  }
146 
147  // Generate the constraints for a Vertical Glue to fill the space, if
148  // any at the bottom of the panel.
149  GridBagConstraints constraints = new GridBagConstraints();
150  constraints.gridx = 0;
151  constraints.gridy = rowCnt;
152  constraints.gridwidth = 2;
153  constraints.fill = GridBagConstraints.BOTH;
154  constraints.weightx = 1;
155  constraints.weighty = 1;
156  add(Box.createVerticalGlue(), constraints);
157  }
158 
159  @Messages({
160  "OsAccountDataPanel_basic_title=Basic Properties",
161  "OsAccountDataPanel_basic_login=Login",
162  "OsAccountDataPanel_basic_fullname=Full Name",
163  "OsAccountDataPanel_basic_address=Address",
164  "OsAccountDataPanel_basic_admin=Administrator",
165  "OsAccountDataPanel_basic_type=Type",
166  "OsAccountDataPanel_basic_creationDate=Creation Date",})
167 
175  private SectionData buildBasicProperties(OsAccount account) {
176  SectionData data = new SectionData(Bundle.OsAccountDataPanel_basic_title());
177 
178  Optional<String> optional = account.getLoginName();
179  data.addData(Bundle.OsAccountDataPanel_basic_login(),
180  optional.isPresent() ? optional.get() : "");
181 
182  optional = account.getFullName();
183  data.addData(Bundle.OsAccountDataPanel_basic_fullname(),
184  optional.isPresent() ? optional.get() : "");
185 
186  data.addData(Bundle.OsAccountDataPanel_basic_address(),
187  account.getName() == null || account.getName().isEmpty() ? "" : account.getName());
188 
189 
190  data.addData(Bundle.OsAccountDataPanel_basic_type(),
191  account.getOsAccountType().isPresent() ? account.getOsAccountType().get().getName() : "");
192 
193  Optional<Long> crTime = account.getCreationTime();
194  if (crTime.isPresent()) {
195  data.addData(Bundle.OsAccountDataPanel_basic_creationDate(), DATE_FORMAT.format(new Date(crTime.get() * 1000)));
196  } else {
197  data.addData(Bundle.OsAccountDataPanel_basic_creationDate(), "");
198  }
199 
200  return data;
201  }
202 
203  @Messages({
204  "OsAccountDataPanel_realm_title=Realm Properties",
205  "OsAccountDataPanel_realm_name=Name",
206  "OsAccountDataPanel_realm_address=Address",
207  "OsAccountDataPanel_realm_confidence=Confidence",
208  "OsAccountDataPanel_realm_unknown=Unknown",
209  "OsAccountDataPanel_realm_scope=Scope",})
210 
218  private SectionData buildRealmProperties(OsAccountRealm realm) {
219  SectionData data = new SectionData(Bundle.OsAccountDataPanel_realm_title());
220 
221  String realmName = realm.getRealmNames().isEmpty() ? Bundle.OsAccountDataPanel_realm_unknown() : realm.getRealmNames().get(0);
222  data.addData(Bundle.OsAccountDataPanel_realm_name(), realmName);
223 
224  Optional<String> optional = realm.getRealmAddr();
225  data.addData(Bundle.OsAccountDataPanel_realm_address(),
226  optional.isPresent() ? optional.get() : "");
227 
228  data.addData(Bundle.OsAccountDataPanel_realm_scope(),
229  realm.getScope().getName());
230 
231  data.addData(Bundle.OsAccountDataPanel_realm_confidence(),
232  realm.getScopeConfidence().getName());
233 
234  return data;
235  }
236 
237  @Messages({
238  "# {0} - hostName",
239  "OsAccountDataPanel_host_section_title={0} Details",
240  "OsAccountDataPanel_host_count_title=Login Count",
241  "OsAccountDataPanel_data_accessed_title=Last Login",
242  "OsAccountDataPanel_administrator_title=Administrator"
243  })
244  private SectionData buildHostData(Host host, List<OsAccountAttribute> attributeList) {
245  SectionData data = new SectionData(Bundle.OsAccountDataPanel_host_section_title(host.getName()));
246  for (OsAccountAttribute attribute : attributeList) {
247  String displayName = attribute.getAttributeType().getDisplayName();
248  String value = attribute.getDisplayString();
249 
250  if(attribute.getAttributeType().getTypeID() == BlackboardAttribute.ATTRIBUTE_TYPE.TSK_COUNT.getTypeID()) {
251  displayName = Bundle.OsAccountDataPanel_host_count_title();
252  } else if(attribute.getAttributeType().getTypeID() == BlackboardAttribute.ATTRIBUTE_TYPE.TSK_IS_ADMIN.getTypeID()) {
253  displayName = Bundle.OsAccountDataPanel_administrator_title();
254  if(attribute.getValueInt() == 0) {
255  value = "False";
256  } else {
257  value = "True";
258  }
259  } else if(attribute.getAttributeType().getTypeID() == BlackboardAttribute.ATTRIBUTE_TYPE.TSK_DATETIME_ACCESSED.getTypeID()) {
260  displayName = Bundle.OsAccountDataPanel_data_accessed_title();
261  }
262 
263  data.addData(displayName, value);
264  }
265 
266  return data;
267  }
268 
275  private void addTitle(String title, int row) {
276  JLabel label = new JLabel(title);
277  // Make the title bold.
278  label.setFont(ContentViewerDefaults.getHeaderFont());
279  add(label, getTitleContraints(row));
280  }
281 
288  private void addPropertyName(String key, int row) {
289  JLabel label = new JLabel(key + ":");
290  add(label, getPropertyNameContraints(row));
291  }
292 
299  private void addPropertyValue(String value, int row) {
300  JLabel label = new JLabel(value);
301  add(label, getPropertyValueContraints(row));
302  }
303 
311  private GridBagConstraints getTitleContraints(int row) {
312  GridBagConstraints constraints = new GridBagConstraints();
313 
314  constraints.gridx = 0;
315  constraints.gridy = row;
316  constraints.gridwidth = 2; // The title goes across the other columns
317  constraints.gridheight = 1;
318  constraints.anchor = GridBagConstraints.NORTHWEST;
319  constraints.fill = GridBagConstraints.HORIZONTAL;
320  constraints.weightx = 1;
321  constraints.insets = (row == 0)
322  ? FIRST_HEADER_INSETS
323  : HEADER_INSETS;
324 
325  return constraints;
326  }
327 
335  private GridBagConstraints getPropertyNameContraints(int row) {
336  GridBagConstraints constraints = new GridBagConstraints();
337 
338  constraints.gridx = KEY_COLUMN;
339  constraints.gridy = row;
340  constraints.gridwidth = 1; // The title goes across the other columns
341  constraints.gridheight = 1;
342  constraints.anchor = GridBagConstraints.WEST;
343  constraints.insets = KEY_COLUMN_INSETS;
344 
345  return constraints;
346  }
347 
355  private GridBagConstraints getPropertyValueContraints(int row) {
356  GridBagConstraints constraints = new GridBagConstraints();
357 
358  constraints.gridx = VALUE_COLUMN;
359  constraints.gridy = row;
360  constraints.gridwidth = 1; // The title goes across the other columns
361  constraints.gridheight = 1;
362  constraints.fill = GridBagConstraints.HORIZONTAL;
363  constraints.insets = VALUE_COLUMN_INSETS;
364  constraints.weightx = 1;
365 
366  return constraints;
367  }
368 
372  private class PanelDataFetcher extends SwingWorker<WorkerResults, Void> {
373 
374  private final Long accountId;
375  private OsAccount account;
376 
382  PanelDataFetcher(Long accountId) {
383  this.accountId = accountId;
384  this.account = null;
385  }
386 
387  PanelDataFetcher(OsAccount account) {
388  this.account = account;
389  this.accountId = null;
390  }
391 
392  @Override
393  protected WorkerResults doInBackground() throws Exception {
394  Map<Host, List<OsAccountAttribute>> hostMap = new HashMap<>();
395  Map<Host, DataSource> instanceMap = new HashMap<>();
396  SleuthkitCase skCase = Case.getCurrentCase().getSleuthkitCase();
397  OsAccountManager osAccountManager = skCase.getOsAccountManager();
398 
399  if(account == null) {
400  account = osAccountManager.getOsAccountByObjectId(accountId);
401  }
402 
403  OsAccountRealm realm = skCase.getOsAccountRealmManager().getRealmByRealmId(account.getRealmId());
404 
405  List<Host> hosts = osAccountManager.getHosts(account);
406  List<OsAccountAttribute> attributeList = account.getExtendedOsAccountAttributes();
407 
408  if (attributeList != null) {
409  if (hosts != null) {
410  // Organize the attributes by hostId
411  Map<Long, List<OsAccountAttribute>> idMap = new HashMap<>();
412  for (OsAccountAttribute attribute : attributeList) {
413  List<OsAccountAttribute> atList = null;
414  Optional<Long> optionalId = attribute.getHostId();
415  Long key = null;
416  if (optionalId.isPresent()) {
417  key = optionalId.get();
418  }
419 
420  atList = idMap.get(key);
421 
422  if (atList == null) {
423  atList = new ArrayList<>();
424  idMap.put(key, atList);
425  }
426 
427  atList.add(attribute);
428  }
429 
430  // Add attribute lists to the hostMap
431  for (Host host : hosts) {
432  List<OsAccountAttribute> atList = idMap.get(host.getHostId());
433  if (atList != null) {
434  hostMap.put(host, atList);
435  }
436 
437  }
438  List<OsAccountAttribute> atList = idMap.get(null);
439  if (atList != null) {
440  hostMap.put(null, atList);
441  }
442 
443  // Store both the host and the dataSource so that we get
444  // all of the calls to the db done in the thread.
445  for (OsAccountInstance instance : account.getOsAccountInstances()) {
446  instanceMap.put(instance.getDataSource().getHost(), instance.getDataSource());
447  }
448 
449  } else {
450  hostMap.put(null, attributeList);
451  }
452  }
453 
454  return new WorkerResults(hostMap, instanceMap, realm);
455  }
456 
457  @Override
458  protected void done() {
459  WorkerResults results = null;
460 
461  try {
462  if (this.isCancelled()) {
463  return;
464  } else {
465  results = get();
466  }
467  } catch (ExecutionException | InterruptedException ex) {
468  logger.log(Level.SEVERE, String.format("Failed to retrieve data for OsAccount (%d)", account.getId()), ex);
469  }
470 
471  if (results != null) {
472  removeAll();
473  setLayout(new GridBagLayout());
474 
475  List<SectionData> data = new ArrayList<>();
476  data.add(buildBasicProperties(account));
477  Map<Host, List<OsAccountAttribute>> hostDataMap = results.getAttributeMap();
478  if (hostDataMap != null && !hostDataMap.isEmpty()) {
479  hostDataMap.forEach((K, V) -> data.add(buildHostData(K, V)));
480  }
481 
482  OsAccountRealm realm = results.getRealm();
483  if (realm != null) {
484  data.add(buildRealmProperties(realm));
485  }
486 
487 // Removing the instance section for now. Leaving code here for
488 // future use.
489 // Map<Host, DataSource> instanceMap = results.getDataSourceMap();
490 // if (!instanceMap.isEmpty()) {
491 // SectionData instanceSection = new SectionData("Instances");
492 // instanceMap.forEach((K, V) -> instanceSection.addData(K.getName(), V.getName()));
493 //
494 // data.add(instanceSection);
495 // }
496 
497  addDataComponents(data);
498 
499  revalidate();
500  repaint();
501  }
502  }
503  }
504 
509  private final class WorkerResults {
510 
511  private final Map<Host, List<OsAccountAttribute>> attributeMap;
512  private final Map<Host, DataSource> instanceMap;
513  private final OsAccountRealm realm;
514 
523  WorkerResults(Map<Host, List<OsAccountAttribute>> attributeMap, Map<Host, DataSource> instanceMap, OsAccountRealm realm) {
524  this.attributeMap = attributeMap;
525  this.instanceMap = instanceMap;
526  this.realm = realm;
527  }
528 
536  Map<Host, List<OsAccountAttribute>> getAttributeMap() {
537  return attributeMap;
538  }
539 
545  Map<Host, DataSource> getDataSourceMap() {
546  return instanceMap;
547  }
548 
549  OsAccountRealm getRealm() {
550  return realm;
551  }
552  }
553 }
SectionData buildHostData(Host host, List< OsAccountAttribute > attributeList)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2021 Basis Technology. Generated on: Thu Jul 8 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.