def store_file(self, file_object): if file_object.binary is None: logging.error('Cannot store binary! No binary data specified') else: destination_path = self.generate_path(file_object) write_binary_to_file(file_object.binary, destination_path, overwrite=False) file_object.set_file_path(destination_path)
def unpack_function(file_path, tmp_dir): container = ExtendedDskOne(file_path) write_binary_to_file(container.decoded_payload, os.path.join(tmp_dir, "b64decoded_payload")) return container.get_meta_dict()
def test_delete_file(self): file_path = os.path.join(self.tmp_dir.name, "test_folder", "test_file") write_binary_to_file(b'this is a test', file_path) self.assertTrue(os.path.exists(file_path), "file not created") delete_file(file_path) self.assertFalse(os.path.exists(file_path)) # Test delete none existing file delete_file(file_path)
def _extract_file_from_upgrade(binary_data, upgrade_command, extraction_dir): file_name = _get_name_of_upgrade(binary_data, upgrade_command) file_binary = _get_binary_of_upgrade(binary_data, upgrade_command, file_name) if file_name: file_name = '{}.bin'.format(get_safe_name(file_name, max_size=80)) else: file_name = '{}.bin'.format(upgrade_command['begin_offset']) file_path = Path(extraction_dir, file_name) write_binary_to_file(file_binary, str(file_path), overwrite=False, file_copy=True)
def unpack_function(file_path, tmp_dir): """ file_path specifies the input file. tmp_dir should be used to store the extracted files. Optional: Return a dict with meta information """ raw_data = get_binary_from_file(file_path) decompressed_data = zlib.decompress(raw_data) output_file_path = os.path.join(tmp_dir, "zlib_decompressed") write_binary_to_file(decompressed_data, output_file_path) return {}
def extract_names(yara_file_path=SIGNATURE_PATH, target_path=TARGET_PATH): stashed_directory = os.getcwd() os.chdir(os.path.dirname(os.path.abspath(__file__))) software_names = get_software_names(yara_file_path) with suppress(FileExistsError): os.mkdir(os.path.dirname(target_path)) binary_string = 'OS_LIST = {}\n'.format(json.dumps(software_names)) write_binary_to_file(file_binary=binary_string.encode(), file_path=target_path, overwrite=True) os.chdir(stashed_directory)
def test_fail_safe_write_file(self): file_path = os.path.join(self.tmp_dir.name, "test_folder", "test_file") write_binary_to_file(b'this is a test', file_path) self.assertTrue(os.path.exists(file_path), "file not created") read_binary = get_binary_from_file(file_path) self.assertEqual(read_binary, b'this is a test', "written data not correct") # Test not overwrite flag write_binary_to_file(b'do not overwrite', file_path, overwrite=False) read_binary = get_binary_from_file(file_path) self.assertEqual(read_binary, b'this is a test', "written data not correct") # Test overwrite flag write_binary_to_file(b'overwrite', file_path, overwrite=True) read_binary = get_binary_from_file(file_path) self.assertEqual(read_binary, b'overwrite', "written data not correct") # Test copy_file_flag write_binary_to_file(b'second_overwrite', file_path, file_copy=True) self.assertTrue(os.path.exists("{}-1".format(file_path)), "new file copy does not exist") read_binary_original = get_binary_from_file(file_path) self.assertEqual(read_binary_original, b'overwrite', "original file no longer correct") read_binary_new = get_binary_from_file("{}-1".format(file_path)) self.assertEqual(read_binary_new, b'second_overwrite', "binary of new file not correct")
def _store_blacklist(blacklist_uids, out_file): output = '\n'.join(blacklist_uids).encode(encoding='utf_8', errors='replace') write_binary_to_file(output, out_file)
def _write_password_file(password_set, file_path): content = '\n'.join(list(password_set)).encode('utf-8') write_binary_to_file(content, file_path)
def _store_files(payloads, tmp_dir): counter = 0 for item in payloads: write_binary_to_file(item, os.path.join(tmp_dir, 'payload_{}.bin'.format(counter))) counter += 1
def unpack_function(file_path, tmp_dir): output_file_path = Path(tmp_dir, 'dahua_firmware.zip') original = get_binary_from_file(file_path) fixed = b'PK' + original[2:] write_binary_to_file(fixed, str(output_file_path)) return {'output': 'zip header fixed'}
def unpack_function(file_path, tmp_dir): """ file_path specifies the input file. tmp_dir should be used to store the extracted files. """ tpwr702n = TPWR702N(file_path) write_binary_to_file(tpwr702n.get_container_header(), '{}/container_header.hdr'.format(tmp_dir)) write_binary_to_file(tpwr702n.get_tpimg0_header(), '{}/img0.hdr'.format(tmp_dir)) write_binary_to_file(tpwr702n.get_bootloader(), '{}/bootloader.7z'.format(tmp_dir)) write_binary_to_file(tpwr702n.get_os_and_fs(), '{}/main.img'.format(tmp_dir)) write_binary_to_file(tpwr702n.get_os(), '{}/main.7z'.format(tmp_dir)) write_binary_to_file(tpwr702n.get_fs(), '{}/main.owfs'.format(tmp_dir)) remaining = tpwr702n.get_remaining_blocks() for offset in remaining: write_binary_to_file(remaining[offset], '{}/{}_unknown.bin'.format(tmp_dir, offset)) return tpwr702n.get_meta_dict()
def test_get_dir_of_file_absolute_path(self): test_file_path = os.path.join(self.tmp_dir.name, 'test_file') write_binary_to_file('test', test_file_path) absolute_file_path_result = get_dir_of_file(test_file_path) self.assertEqual(absolute_file_path_result, self.tmp_dir.name)
def extract_fingerprint(input_data, tmp_dir): fingerprint = _get_file_fingerprint(input_data) if fingerprint: store_path = Path(tmp_dir, 'fingerprint.txt') write_binary_to_file(fingerprint, str(store_path), overwrite=False, file_copy=True)