def test_open_close(self): """Tests the open and close functions.""" if not unittest.source: return pff_file = pypff.file() # Test open and close. pff_file.open(unittest.source) pff_file.close() # Test open and close a second time to validate clean up on close. pff_file.open(unittest.source) pff_file.close() if os.path.isfile(unittest.source): with open(unittest.source, "rb") as file_object: # Test open_file_object and close. pff_file.open_file_object(file_object) pff_file.close() # Test open_file_object and close a second time to validate clean up on close. pff_file.open_file_object(file_object) pff_file.close() # Test open_file_object and close and dereferencing file_object. pff_file.open_file_object(file_object) del file_object pff_file.close()
def test_open_file_object(self): """Tests the open_file_object function.""" test_source = unittest.source if not test_source: raise unittest.SkipTest("missing source") if not os.path.isfile(test_source): raise unittest.SkipTest("source not a regular file") pff_file = pypff.file() with open(test_source, "rb") as file_object: pff_file.open_file_object(file_object) with self.assertRaises(IOError): pff_file.open_file_object(file_object) pff_file.close() with self.assertRaises(TypeError): pff_file.open_file_object(None) with self.assertRaises(ValueError): pff_file.open_file_object(file_object, mode="w")
def run(self): db = Database() # https://github.com/williballenthin/python-registry file_id = self.request.POST['file_id'] pst_file = db.get_filebyid(file_id) if not pst_file: raise IOError("File not found in DB") try: self.pst = pypff.file() self.pst.open_file_object(pst_file) except Exception as e: raise base_path = u"" root_node = self.pst.get_root_folder() self.email_dict = {} self.recursive_walk_folders(root_node, base_path) # Store in DB Now store_data = {'file_id': file_id, 'pst': self.email_dict} db.create_datastore(store_data) self.render_type = 'file' self.render_data = { 'PSTViewer': { 'email_dict': self.email_dict, 'file_id': file_id } }
def pypff_test_multi_open_close_file(filename, mode): """Tests multiple open and close.""" description = ( "Testing multi open close of: {0:s} with access: {1:s}" "\t").format(get_filename_string(filename), get_mode_string(mode)) print(description, end="") error_string = None result = True try: pff_file = pypff.file() pff_file.open(filename, mode) pff_file.close() pff_file.open(filename, mode) pff_file.close() except Exception as exception: error_string = str(exception) result = False if not result: print("(FAIL)") else: print("(PASS)") if error_string: print(error_string) return result
def pypff_test_single_open_close_file_object_with_dereference( filename, mode): """Tests single file-like object open and close with dereference.""" description = ( "Testing single open close of file-like object with dereference of: " "{0:s} with access: {1:s}\t").format( get_filename_string(filename), get_mode_string(mode)) print(description, end="") error_string = None result = True try: file_object = open(filename, "rb") pff_file = pypff.file() pff_file.open_file_object(file_object, mode) del file_object pff_file.close() except Exception as exception: error_string = str(exception) result = False if not result: print("(FAIL)") else: print("(PASS)") if error_string: print(error_string) return result
def test_open_close(self): """Tests the open and close functions.""" if not unittest.source: return pff_file = pypff.file() # Test open and close. pff_file.open(unittest.source) pff_file.close() # Test open and close a second time to validate clean up on close. pff_file.open(unittest.source) pff_file.close() file_object = open(unittest.source, "rb") # Test open_file_object and close. pff_file.open_file_object(file_object) pff_file.close() # Test open_file_object and close a second time to validate clean up on close. pff_file.open_file_object(file_object) pff_file.close() # Test open_file_object and close and dereferencing file_object. pff_file.open_file_object(file_object) del file_object pff_file.close()
def run(self): db = Database() # https://github.com/williballenthin/python-registry file_id = self.request.POST['file_id'] pst_file = db.get_filebyid(file_id) if not pst_file: raise IOError("File not found in DB") try: self.pst = pypff.file() self.pst.open_file_object(pst_file) except Exception as e: raise base_path = u"" root_node = self.pst.get_root_folder() self.email_dict = {} self.recursive_walk_folders(root_node, base_path) # Store in DB Now store_data = {'file_id': file_id, 'pst': self.email_dict} db.create_datastore(store_data) self.render_type = 'file' self.render_data = {'PSTViewer': {'email_dict': self.email_dict, 'file_id': file_id}}
def __init__(self, file: Union[Path, IOBase, str] = None) -> None: self.filepath = None self._data = pypff.file() self._encodings = ["utf-8", "utf-16"] self._mime_indices = defaultdict(int) if file: self.load(file)
def test_close(self): """Tests the close function.""" if not unittest.source: raise unittest.SkipTest("missing source") pff_file = pypff.file() with self.assertRaises(IOError): pff_file.close()
def test_get_orphan_item(self): """Tests the get_orphan_item function.""" pff_file = pypff.file() with self.assertRaises(IOError): pff_file.get_orphan_item(0) if not unittest.source: return pff_file = pypff.file() pff_file.open(unittest.source) if pff_file.number_of_orphan_items > 0: item = pff_file.get_orphan_item(0) self.assertIsNotNone(item) pff_file.close()
def test_close(self): """Tests the close function.""" if not unittest.source: return pff_file = pypff.file() with self.assertRaises(IOError): pff_file.close()
def open_data_folder(pst_file): try: pff_file = pypff.file() pff_file.open_file_object(pst_file) root_folder = pff_file.get_root_folder() return PSTFolder( 0, root_folder.get_sub_folder(constants.OUTLOOK_DATA_FOLDER_IDX)) except Exception as e: traceback.print_exc() sys.exit("[!] could not open data folder")
def test_get_root_folder(self): """Tests the get_root_folder function.""" pff_file = pypff.file() with self.assertRaises(IOError): pff_file.get_root_folder() with self.assertRaises(IOError): pff_file.root_folder if not unittest.source: return pff_file = pypff.file() pff_file.open(unittest.source) pff_file.get_root_folder() _ = pff_file.root_folder pff_file.close()
def _parse_pstost(self, target_file: TargetFile) -> List: pff_file = pypff.file() result = [] try: pff_file.open(target_file.get_path()) pff_root_folder = pff_file.get_root_folder() result = self._get_recursive(pff_root_folder, target_file) except: traceback.print_exc() finally: pff_file.close() return result
def test_get_message_store(self): """Tests the get_message_store function.""" pff_file = pypff.file() with self.assertRaises(IOError): pff_file.get_message_store() with self.assertRaises(IOError): pff_file.message_store if not unittest.source: return pff_file = pypff.file() pff_file.open(unittest.source) pff_file.get_message_store() _ = pff_file.message_store pff_file.close()
def test_get_name_to_id_map(self): """Tests the get_name_to_id_map function.""" pff_file = pypff.file() with self.assertRaises(IOError): pff_file.get_name_to_id_map() with self.assertRaises(IOError): pff_file.name_to_id_map if not unittest.source: return pff_file = pypff.file() pff_file.open(unittest.source) pff_file.get_name_to_id_map() _ = pff_file.name_to_id_map pff_file.close()
def test_get_number_of_orphan_items(self): """Tests the get_number_of_orphan_items function.""" pff_file = pypff.file() with self.assertRaises(IOError): pff_file.get_number_of_orphan_items() with self.assertRaises(IOError): pff_file.number_of_orphan_items if not unittest.source: return pff_file = pypff.file() pff_file.open(unittest.source) number_of_items = pff_file.get_number_of_orphan_items() self.assertIsNotNone(number_of_items) self.assertIsNotNone(pff_file.number_of_orphan_items) pff_file.close()
def test_get_name_to_id_map(self): """Tests the get_name_to_id_map function and name_to_id_map property.""" if not unittest.source: raise unittest.SkipTest("missing source") pff_file = pypff.file() pff_file.open(unittest.source) _ = pff_file.get_name_to_id_map() _ = pff_file.name_to_id_map pff_file.close()
def test_get_root_folder(self): """Tests the get_root_folder function and root_folder property.""" if not unittest.source: raise unittest.SkipTest("missing source") pff_file = pypff.file() pff_file.open(unittest.source) _ = pff_file.get_root_folder() _ = pff_file.root_folder pff_file.close()
def test_get_message_store(self): """Tests the get_message_store function and message_store property.""" if not unittest.source: raise unittest.SkipTest("missing source") pff_file = pypff.file() pff_file.open(unittest.source) _ = pff_file.get_message_store() _ = pff_file.message_store pff_file.close()
def pypff_test_single_open_close_file(filename, mode): """Tests a single open and close.""" description = ( "Testing single open close of: {0:s} with access: {1:s}" "\t").format(get_filename_string(filename), get_mode_string(mode)) print(description, end="") error_string = None result = True try: pff_file = pypff.file() pff_file.open(filename, mode) pff_file.close() except TypeError as exception: expected_message = ( "{0:s}: unsupported string object type.").format( "pypff_file_open") if not filename and str(exception) == expected_message: pass else: error_string = str(exception) result = False except ValueError as exception: expected_message = ( "{0:s}: unsupported mode: w.").format( "pypff_file_open") if mode != "w" or str(exception) != expected_message: error_string = str(exception) result = False except Exception as exception: error_string = str(exception) result = False if not result: print("(FAIL)") else: print("(PASS)") if error_string: print(error_string) return result
def test_get_size(self): """Tests the get_size function and size property.""" if not unittest.source: raise unittest.SkipTest("missing source") pff_file = pypff.file() pff_file.open(unittest.source) size = pff_file.get_size() self.assertIsNotNone(size) self.assertIsNotNone(pff_file.size) pff_file.close()
def test_get_root_item(self): """Tests the get_root_item function and root_item property.""" test_source = unittest.source if not test_source: raise unittest.SkipTest("missing source") pff_file = pypff.file() pff_file.open(test_source) _ = pff_file.get_root_item() _ = pff_file.root_item pff_file.close()
def test_get_number_of_orphan_items(self): """Tests the get_number_of_orphan_items function and number_of_orphan_items property.""" if not unittest.source: raise unittest.SkipTest("missing source") pff_file = pypff.file() pff_file.open(unittest.source) number_of_orphan_items = pff_file.get_number_of_orphan_items() self.assertIsNotNone(number_of_orphan_items) self.assertIsNotNone(pff_file.number_of_orphan_items) pff_file.close()
def test_get_ascii_codepage(self): """Tests the get_ascii_codepage function and ascii_codepage property.""" if not unittest.source: raise unittest.SkipTest("missing source") pff_file = pypff.file() pff_file.open(unittest.source) ascii_codepage = pff_file.get_ascii_codepage() self.assertIsNotNone(ascii_codepage) self.assertIsNotNone(pff_file.ascii_codepage) pff_file.close()
def test_get_encryption_type(self): """Tests the get_encryption_type function and encryption_type property.""" if not unittest.source: raise unittest.SkipTest("missing source") pff_file = pypff.file() pff_file.open(unittest.source) encryption_type = pff_file.get_encryption_type() self.assertIsNotNone(encryption_type) self.assertIsNotNone(pff_file.encryption_type) pff_file.close()
def findWords(file, results_file, wordsList): # print "pypff version: " + pypff.get_version() pst = pypff.file() try: pst.open(file) # PFFParser.__show__(pst.root_folder.sub_folders) writer = PFFParser.__email_writer__(results_file, file, wordsList) PFFParser.__search__(pst.root_folder.sub_folders, writer, wordsList) finally: pst.close() return
def test_get_content_type(self): """Tests the get_content_type function and content_type property.""" test_source = unittest.source if not test_source: raise unittest.SkipTest("missing source") pff_file = pypff.file() pff_file.open(test_source) content_type = pff_file.get_content_type() self.assertIsNotNone(content_type) self.assertIsNotNone(pff_file.content_type) pff_file.close()
def pypff_test_single_open_close_file(filename, mode): if not filename: filename_string = "None" else: filename_string = filename print("Testing single open close of: {0:s} with access: {1:s}\t".format( filename_string, get_mode_string(mode))) result = True try: pff_file = pypff.file() pff_file.open(filename, mode) pff_file.close() except TypeError as exception: expected_message = ( "{0:s}: unsupported string object type.").format( "pypff_file_open") if not filename and str(exception) == expected_message: pass else: print(str(exception)) result = False except ValueError as exception: expected_message = ( "{0:s}: unsupported mode: w.").format( "pypff_file_open") if mode != "w" or str(exception) != expected_message: print(str(exception)) result = False except Exception as exception: print(str(exception)) result = False if not result: print("(FAIL)") else: print("(PASS)") return result
def get_process_fails(pstlocation): process_fail_list = [] pstFile = open(pstlocation, "rb") pff_file = pypff.file() pff_file.open_file_object(pstFile) for i, x in enumerate(pff_file.get_root_folder().sub_items): for y in x.sub_items: if y.name == "Process Failures": for z in y.sub_items: if z.subject.split(":")[0] != "Process Failure": continue process_fail = parse_email(z) process_fail_list.append(process_fail) return process_fail_list
def pypff_test_single_open_close_file(filename, mode): if not filename: filename_string = "None" else: filename_string = filename print("Testing single open close of: {0:s} with access: {1:s}\t".format( filename_string, get_mode_string(mode))) result = True try: pff_file = pypff.file() pff_file.open(filename, mode) pff_file.close() except TypeError as exception: expected_message = ( "{0:s}: unsupported string object type.").format("pypff_file_open") if not filename and str(exception) == expected_message: pass else: print(str(exception)) result = False except ValueError as exception: expected_message = ( "{0:s}: unsupported mode: w.").format("pypff_file_open") if mode != "w" or str(exception) != expected_message: print(str(exception)) result = False except Exception as exception: print(str(exception)) result = False if not result: print("(FAIL)") else: print("(PASS)") return result
def test_open(self): """Tests the open function.""" if not unittest.source: return pff_file = pypff.file() pff_file.open(unittest.source) with self.assertRaises(IOError): pff_file.open(unittest.source) pff_file.close() with self.assertRaises(TypeError): pff_file.open(None) with self.assertRaises(ValueError): pff_file.open(unittest.source, mode="w")
def test_open(self): """Tests the open function.""" test_source = unittest.source if not test_source: raise unittest.SkipTest("missing source") pff_file = pypff.file() pff_file.open(test_source) with self.assertRaises(IOError): pff_file.open(test_source) pff_file.close() with self.assertRaises(TypeError): pff_file.open(None) with self.assertRaises(ValueError): pff_file.open(test_source, mode="w")
def test_set_ascii_codepage(self): """Tests the set_ascii_codepage function.""" supported_codepages = ( "ascii", "cp874", "cp932", "cp936", "cp949", "cp950", "cp1250", "cp1251", "cp1252", "cp1253", "cp1254", "cp1255", "cp1256", "cp1257", "cp1258") pff_file = pypff.file() for codepage in supported_codepages: pff_file.set_ascii_codepage(codepage) unsupported_codepages = ( "iso-8859-1", "iso-8859-2", "iso-8859-3", "iso-8859-4", "iso-8859-5", "iso-8859-6", "iso-8859-7", "iso-8859-8", "iso-8859-9", "iso-8859-10", "iso-8859-11", "iso-8859-13", "iso-8859-14", "iso-8859-15", "iso-8859-16", "koi8_r", "koi8_u") for codepage in unsupported_codepages: with self.assertRaises(RuntimeError): pff_file.set_ascii_codepage(codepage)
def launchstuff(pstfile): if os.path.isfile(pstfile): print "\n\n[+] Begining to Process PST: {}\n".format(pstfile) pst = pypff.file() pst.open(pstfile) rootfolder = pst.get_root_folder() iteratefolder(rootfolder) pst.close() counter = 0 for p_id, p_info in emailproccessor.items(): print "[+] Email ID: {}".format(p_id) counter += 1 if counter == nummess: print "\n[+] Complete: Meta Extracted for all {} messages\n\n".format( counter) else: print "[*] Error in Extraction only {} message of {} were extracted\n".format( counter, nummess) else: print "[*] PST file does not exist" return emailproccessor
def pypff_test_single_open_close_file_object(filename, mode): print(("Testing single open close of file-like object of: {0:s} " "with access: {1:s}\t").format(filename, get_mode_string(mode))) result = True try: file_object = open(filename, "rb") pff_file = pypff.file() pff_file.open_file_object(file_object, mode) pff_file.close() except Exception as exception: print(str(exception)) result = False if not result: print("(FAIL)") else: print("(PASS)") return result
def pypff_test_multi_open_close_file(filename, mode): print("Testing multi open close of: {0:s} with access: {1:s}\t".format( filename, get_mode_string(mode))) result = True try: pff_file = pypff.file() pff_file.open(filename, mode) pff_file.close() pff_file.open(filename, mode) pff_file.close() except Exception as exception: print(str(exception)) result = False if not result: print("(FAIL)") else: print("(PASS)") return result
def test_open_file_object(self): """Tests the open_file_object function.""" if not unittest.source: return file_object = open(unittest.source, "rb") pff_file = pypff.file() pff_file.open_file_object(file_object) with self.assertRaises(IOError): pff_file.open_file_object(file_object) pff_file.close() # TODO: change IOError into TypeError with self.assertRaises(IOError): pff_file.open_file_object(None) with self.assertRaises(ValueError): pff_file.open_file_object(file_object, mode="w")
def get_pst(path_on_disk): pst = pypff.file() pst.open(path_on_disk) return pst
import pypff import sys if len(sys.argv) != 3: print "Need to have 2 arguments: <pst file> <search term>" sys.exit(1) pst_file = sys.argv[1] search_term = sys.argv[2] print "PST file:", pst_file print "Search term:", search_term pst = pypff.file() pst.open(pst_file) print "Size:", pst.get_size() print msg_counter = 0 def search_dir(dir,path): if dir.get_display_name(): new_path = path + u"/" + unicode(dir.get_display_name()) else: new_path = path print "Searching ", new_path for i in range(0, dir.get_number_of_sub_messages()): msg = dir.get_sub_message(i)
def main(): supported_codepages = [ "ascii", "cp874", "cp932", "cp936", "cp949", "cp950", "cp1250", "cp1251", "cp1252", "cp1253", "cp1254", "cp1255", "cp1256", "cp1257", "cp1258"] unsupported_codepages = [ "iso-8859-1", "iso-8859-2", "iso-8859-3", "iso-8859-4", "iso-8859-5", "iso-8859-6", "iso-8859-7", "iso-8859-8", "iso-8859-9", "iso-8859-10", "iso-8859-11", "iso-8859-13", "iso-8859-14", "iso-8859-15", "iso-8859-16", "koi8_r", "koi8_u"] pff_file = pypff.file() result = True for codepage in supported_codepages: print("Testing setting supported ASCII codepage of: {0:s}:\t".format( codepage)), try: pff_file.ascii_codepage = codepage result = True except: result = False if not result: print("(FAIL)") return False print("(PASS)") print("Testing setting supported ASCII codepage of: {0:s}:\t".format( codepage)), try: pff_file.set_ascii_codepage(codepage) result = True except: result = False if not result: print("(FAIL)") return False print("(PASS)") for codepage in unsupported_codepages: print("Testing setting unsupported ASCII codepage of: {0:s}:\t".format( codepage)), result = False try: pff_file.ascii_codepage = codepage except RuntimeError as exception: expected_message = ( "{0:s}: unable to determine ASCII codepage.").format( "pypff_file_set_ascii_codepage_from_string") if str(exception) == expected_message: result = True except: pass if not result: print("(FAIL)") return False print("(PASS)") print("Testing setting unsupported ASCII codepage of: {0:s}:\t".format( codepage)), result = False try: pff_file.set_ascii_codepage(codepage) except RuntimeError as exception: expected_message = ( "{0:s}: unable to determine ASCII codepage.").format( "pypff_file_set_ascii_codepage_from_string") if str(exception) == expected_message: result = True except: pass if not result: print("(FAIL)") return False print("(PASS)") return True
def test_signal_abort(self): """Tests the signal_abort function.""" pff_file = pypff.file() pff_file.signal_abort()