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