mvt/mvt/android/modules/backup/base.py

49 lines
1.3 KiB
Python
Raw Normal View History

2022-03-04 09:10:56 +00:00
# Mobile Verification Toolkit (MVT)
# Copyright (c) 2021-2022 Claudio Guarnieri.
2022-03-04 09:10:56 +00:00
# Use of this software is governed by the MVT License 1.1 that can be found at
# https://license.mvt.re/1.1/
import fnmatch
2022-03-10 10:33:54 +00:00
import os
2022-06-22 14:53:29 +00:00
from tarfile import TarFile
2022-03-04 09:10:56 +00:00
from mvt.common.module import MVTModule
class BackupExtraction(MVTModule):
"""This class provides a base for all backup extractios modules"""
ab = None
2022-06-22 14:53:29 +00:00
def from_folder(self, backup_path: str, files: list) -> None:
2022-03-04 09:10:56 +00:00
"""
Get all the files and list them
"""
self.backup_path = backup_path
self.files = files
2022-06-22 14:53:29 +00:00
def from_ab(self, file_path: str, tar: TarFile, files: list) -> None:
2022-03-04 09:10:56 +00:00
"""
Extract the files
"""
self.ab = file_path
self.tar = tar
self.files = files
2022-06-22 14:53:29 +00:00
def _get_files_by_pattern(self, pattern: str) -> list:
2022-03-04 09:10:56 +00:00
return fnmatch.filter(self.files, pattern)
2022-06-22 14:53:29 +00:00
def _get_file_content(self, file_path: str) -> bytes:
2022-03-04 09:10:56 +00:00
if self.ab:
try:
member = self.tar.getmember(file_path)
except KeyError:
return None
handle = self.tar.extractfile(member)
else:
handle = open(os.path.join(self.backup_path, file_path), "rb")
data = handle.read()
handle.close()
2022-06-22 14:53:29 +00:00
2022-03-04 09:10:56 +00:00
return data