Merge pull request #2593 from Akury83/master
[RRG-proxmark3.git] / client / pyscripts / pm3_nfc2eml.py
blobe47ff846464c06fe6849c252b8b6ae98e20f7ebc
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
4 """
5 Script to convert amiibo nfc Flipper Zero files to eml files to be used with Proxmark3
6 Date: 18/05/2023
7 Script Author: OscarAkaElvis
8 Tested on: Linux and Windows
9 OscarAkaElvis - https://twitter.com/OscarAkaElvis
10 """
12 import re
13 import argparse
14 import os
15 from os import path
16 from sys import argv
18 # Vars
19 script_name = path.basename(argv[0])
20 script_version = "1.0"
22 # Function to print script's version
23 def print_version():
24 print()
25 return 'v{}'.format(script_version)
27 # Check if the input file is a text file based on its content
28 def is_text_file(file_path):
29 with open(file_path, 'rb') as file:
30 try:
31 file_content = file.read().decode('utf-8')
32 return all(ord(char) < 128 for char in file_content)
33 except UnicodeDecodeError:
34 return False
36 # Main script code
37 def main():
38 # Text help data
39 description_text = "Script to convert amiibo nfc Flipper Zero files to eml files to be used with Proxmark3."
40 epilog_text = 'Example:\n python3 ' + script_name + ' -i file.nfc -o output.eml'
42 # Create an argument parser
43 parser = argparse.ArgumentParser(exit_on_error=False, description=description_text, formatter_class=argparse.RawDescriptionHelpFormatter, epilog=epilog_text)
44 # Add the input file argument
45 parser.add_argument("-i", "--input", required=True, help="Path to the input nfc file.")
46 # Add the output file argument
47 parser.add_argument("-o", "--output", required=True, help="Name of the output eml file.")
48 # Add the version argument
49 parser.add_argument('-v', '--version', action='version', version=print_version(), help="Show script's version number and exit")
51 # Parse the command-line arguments
53 args = parser.parse_args()
55 # Extract the file paths from the command-line arguments
56 file_path = args.input
57 output_file = args.output
59 # Check if the target file exists
60 if not os.path.isfile(file_path):
61 print(f"[!] The input file '{file_path}' does not exist.")
62 exit(0)
64 # Validation to check if the input file is a text file based on its content
65 if not is_text_file(file_path):
66 print("[!] The input file does not appear to be a nfc text file.")
68 # Check if the input file name has the .nfc extension
69 if not file_path.lower().endswith('.nfc'):
70 print("[+] Warning. The input file should have the '.nfc' extension.")
72 # Get the absolute path of the output file
73 output_file_path = os.path.abspath(output_file)
75 # Check if the directory of the output file is writable
76 output_dir = os.path.dirname(output_file_path)
77 if not os.access(output_dir, os.W_OK):
78 print(f"[!] The output directory '{output_dir}' is not writable or doesn't exist.")
80 # Check if the output file name has the .eml extension
81 if not output_file.lower().endswith('.eml'):
82 print("[+] Warning. The output file should have the '.eml' extension.")
84 # Read the target file
85 with open(file_path, 'r') as file:
86 file_content = file.read()
88 # Extract the data from each "Page X" line
89 matches = re.findall(r'Page \d+:(.*?)$', file_content, re.MULTILINE | re.DOTALL)
91 if matches:
92 # Remove spaces and convert to lowercase for each match
93 extracted_data = [re.sub(r'\s', '', match).lower() for match in matches]
95 # Join the extracted data with line feeds
96 result = '\n'.join(extracted_data)
98 # Write the extracted data to the output file
99 with open(output_file_path, 'w', newline='\n') as output_file:
100 output_file.write(result)
101 print(f"[*] File converted successfully. Output eml file written as '{os.path.basename(output_file.name)}'.")
102 else:
103 # If the needed data is not there
104 print("[!] Provided input file seems to not be a valid nfc file to work with.")
106 # Application entrypoint
107 if __name__ == '__main__':
108 try:
109 main()
110 except:
111 pass