mirror of
https://github.com/overcuriousity/autopsy-flatpak.git
synced 2025-07-06 21:00:22 +00:00
192 lines
8.6 KiB
Python
192 lines
8.6 KiB
Python
# Sample module in the public domain. Feel free to use this as a template
|
|
# for your modules (and you can remove this header and take complete credit
|
|
# and liability)
|
|
#
|
|
# Contact: Brian Carrier [carrier <at> sleuthkit [dot] org]
|
|
#
|
|
# This is free and unencumbered software released into the public domain.
|
|
#
|
|
# Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
# distribute this software, either in source code form or as a compiled
|
|
# binary, for any purpose, commercial or non-commercial, and by any
|
|
# means.
|
|
#
|
|
# In jurisdictions that recognize copyright laws, the author or authors
|
|
# of this software dedicate any and all copyright interest in the
|
|
# software to the public domain. We make this dedication for the benefit
|
|
# of the public at large and to the detriment of our heirs and
|
|
# successors. We intend this dedication to be an overt act of
|
|
# relinquishment in perpetuity of all present and future rights to this
|
|
# software under copyright law.
|
|
#
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
# OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
# Simple data source-level ingest module for Autopsy.
|
|
# Used as part of Python tutorials from Basis Technology - August 2015
|
|
#
|
|
# Looks for files of a given name, opens then in SQLite, queries the DB,
|
|
# and makes artifacts
|
|
|
|
import jarray
|
|
import inspect
|
|
import os
|
|
from java.lang import Class
|
|
from java.lang import System
|
|
from java.sql import DriverManager, SQLException
|
|
from java.util.logging import Level
|
|
from java.util import Arrays
|
|
from java.io import File
|
|
from org.sleuthkit.datamodel import SleuthkitCase
|
|
from org.sleuthkit.datamodel import AbstractFile
|
|
from org.sleuthkit.datamodel import ReadContentInputStream
|
|
from org.sleuthkit.datamodel import BlackboardArtifact
|
|
from org.sleuthkit.datamodel import BlackboardAttribute
|
|
from org.sleuthkit.autopsy.ingest import IngestModule
|
|
from org.sleuthkit.autopsy.ingest.IngestModule import IngestModuleException
|
|
from org.sleuthkit.autopsy.ingest import DataSourceIngestModule
|
|
from org.sleuthkit.autopsy.ingest import IngestModuleFactoryAdapter
|
|
from org.sleuthkit.autopsy.ingest import IngestMessage
|
|
from org.sleuthkit.autopsy.ingest import IngestServices
|
|
from org.sleuthkit.autopsy.ingest import ModuleDataEvent
|
|
from org.sleuthkit.autopsy.coreutils import Logger
|
|
from org.sleuthkit.autopsy.casemodule import Case
|
|
from org.sleuthkit.autopsy.datamodel import ContentUtils
|
|
from org.sleuthkit.autopsy.casemodule.services import Services
|
|
from org.sleuthkit.autopsy.casemodule.services import FileManager
|
|
from org.sleuthkit.autopsy.casemodule.services import Blackboard
|
|
|
|
|
|
|
|
# Factory that defines the name and details of the module and allows Autopsy
|
|
# to create instances of the modules that will do the analysis.
|
|
class ContactsDbIngestModuleFactory(IngestModuleFactoryAdapter):
|
|
|
|
moduleName = "Contacts Db Analyzer"
|
|
|
|
def getModuleDisplayName(self):
|
|
return self.moduleName
|
|
|
|
def getModuleDescription(self):
|
|
return "Sample module that parses contacts.db"
|
|
|
|
def getModuleVersionNumber(self):
|
|
return "1.0"
|
|
|
|
def isDataSourceIngestModuleFactory(self):
|
|
return True
|
|
|
|
def createDataSourceIngestModule(self, ingestOptions):
|
|
return ContactsDbIngestModule()
|
|
|
|
|
|
# Data Source-level ingest module. One gets created per data source.
|
|
class ContactsDbIngestModule(DataSourceIngestModule):
|
|
|
|
_logger = Logger.getLogger(ContactsDbIngestModuleFactory.moduleName)
|
|
|
|
def log(self, level, msg):
|
|
self._logger.logp(level, self.__class__.__name__, inspect.stack()[1][3], msg)
|
|
|
|
def __init__(self):
|
|
self.context = None
|
|
|
|
# Where any setup and configuration is done
|
|
# 'context' is an instance of org.sleuthkit.autopsy.ingest.IngestJobContext.
|
|
# See: http://sleuthkit.org/autopsy/docs/api-docs/latest/classorg_1_1sleuthkit_1_1autopsy_1_1ingest_1_1_ingest_job_context.html
|
|
def startUp(self, context):
|
|
self.context = context
|
|
|
|
# Where the analysis is done.
|
|
# The 'dataSource' object being passed in is of type org.sleuthkit.datamodel.Content.
|
|
# See: http://www.sleuthkit.org/sleuthkit/docs/jni-docs/latest/interfaceorg_1_1sleuthkit_1_1datamodel_1_1_content.html
|
|
# 'progressBar' is of type org.sleuthkit.autopsy.ingest.DataSourceIngestModuleProgress
|
|
# See: http://sleuthkit.org/autopsy/docs/api-docs/latest/classorg_1_1sleuthkit_1_1autopsy_1_1ingest_1_1_data_source_ingest_module_progress.html
|
|
def process(self, dataSource, progressBar):
|
|
|
|
# we don't know how much work there is yet
|
|
progressBar.switchToIndeterminate()
|
|
|
|
# Use blackboard class to index blackboard artifacts for keyword search
|
|
blackboard = Case.getCurrentCase().getServices().getBlackboard()
|
|
|
|
# Find files named contacts.db, regardless of parent path
|
|
fileManager = Case.getCurrentCase().getServices().getFileManager()
|
|
files = fileManager.findFiles(dataSource, "contacts.db")
|
|
|
|
numFiles = len(files)
|
|
progressBar.switchToDeterminate(numFiles)
|
|
fileCount = 0
|
|
for file in files:
|
|
|
|
# Check if the user pressed cancel while we were busy
|
|
if self.context.isJobCancelled():
|
|
return IngestModule.ProcessResult.OK
|
|
|
|
self.log(Level.INFO, "Processing file: " + file.getName())
|
|
fileCount += 1
|
|
|
|
# Save the DB locally in the temp folder. use file id as name to reduce collisions
|
|
lclDbPath = os.path.join(Case.getCurrentCase().getTempDirectory(), str(file.getId()) + ".db")
|
|
ContentUtils.writeToFile(file, File(lclDbPath))
|
|
|
|
# Open the DB using JDBC
|
|
try:
|
|
Class.forName("org.sqlite.JDBC").newInstance()
|
|
dbConn = DriverManager.getConnection("jdbc:sqlite:%s" % lclDbPath)
|
|
except SQLException as e:
|
|
self.log(Level.INFO, "Could not open database file (not SQLite) " + file.getName() + " (" + e.getMessage() + ")")
|
|
return IngestModule.ProcessResult.OK
|
|
|
|
# Query the contacts table in the database and get all columns.
|
|
try:
|
|
stmt = dbConn.createStatement()
|
|
resultSet = stmt.executeQuery("SELECT * FROM contacts")
|
|
except SQLException as e:
|
|
self.log(Level.INFO, "Error querying database for contacts table (" + e.getMessage() + ")")
|
|
return IngestModule.ProcessResult.OK
|
|
|
|
# Cycle through each row and create artifacts
|
|
while resultSet.next():
|
|
try:
|
|
name = resultSet.getString("name")
|
|
email = resultSet.getString("email")
|
|
phone = resultSet.getString("phone")
|
|
except SQLException as e:
|
|
self.log(Level.INFO, "Error getting values from contacts table (" + e.getMessage() + ")")
|
|
|
|
|
|
# Make an artifact on the blackboard, TSK_CONTACT and give it attributes for each of the fields
|
|
art = file.newDataArtifact(BlackboardArtifact.Type.TSK_CONTACT, Arrays.asList(
|
|
BlackboardAttribute(BlackboardAttribute.Type.TSK_NAME_PERSON,
|
|
ContactsDbIngestModuleFactory.moduleName, name),
|
|
BlackboardAttribute(BlackboardAttribute.Type.TSK_EMAIL,
|
|
ContactsDbIngestModuleFactory.moduleName, email),
|
|
BlackboardAttribute(BlackboardAttribute.Type.TSK_PHONE_NUMBER,
|
|
ContactsDbIngestModuleFactory.moduleName, phone)
|
|
))
|
|
|
|
try:
|
|
# index the artifact for keyword search
|
|
blackboard.postArtifact(art, ContactsDbIngestModuleFactory.moduleName)
|
|
except Blackboard.BlackboardException as e:
|
|
self.log(Level.SEVERE, "Error indexing artifact " + art.getDisplayName())
|
|
|
|
# Clean up
|
|
stmt.close()
|
|
dbConn.close()
|
|
os.remove(lclDbPath)
|
|
|
|
|
|
# After all databases, post a message to the ingest messages in box.
|
|
message = IngestMessage.createMessage(IngestMessage.MessageType.DATA,
|
|
"ContactsDb Analyzer", "Found %d files" % fileCount)
|
|
IngestServices.getInstance().postMessage(message)
|
|
|
|
return IngestModule.ProcessResult.OK
|