From a1568bfed77c1365aab174a0ed5e32c973f69247 Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Wed, 4 Dec 2024 11:05:49 -0800 Subject: [PATCH 1/7] Remove redundant semicolon line endings --- traceKeyExtractor.py | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/traceKeyExtractor.py b/traceKeyExtractor.py index 7684658..5802707 100644 --- a/traceKeyExtractor.py +++ b/traceKeyExtractor.py @@ -22,7 +22,7 @@ pm3Location = None #Calculated. The location of Proxm pm3Command = "bin/pm3" # The command that works to start proxmark3 mfNonceBruteCommand = "share/proxmark3/tools/mf_nonce_brute" # The command to execute mfNonceBrute -trace = ""; #Prompted during runtime. Trace filename that the user provides +trace = "" #Prompted during runtime. Trace filename that the user provides def main(): global pm3Location,dictionaryFilepath,trace @@ -33,9 +33,9 @@ def main(): print("This will extract the keys from a trace file") print("that was saved from sniffing communication between") print("the AMS and RFID tag.") - print(""); + print("") print("Instructions to sniff and save the trace can be found at") - print("https://github.com/Bambu-Research-Group/RFID-Tag-Guide"); + print("https://github.com/Bambu-Research-Group/RFID-Tag-Guide") print("--------------------------------------------------------") print("") @@ -44,7 +44,7 @@ def main(): #Create a dictionary file to store keys that we discover print(f"Creating dictionary file '{dictionaryFilename}'") dictionaryFile = open(dictionaryFilename, "w") - dictionaryFile.close(); + dictionaryFile.close() dictionaryFilepath = os.path.abspath(dictionaryFilename) print(f"Saved dictionary to {dictionaryFilepath}") @@ -72,22 +72,22 @@ def discoverKeys(): print("PROGRAM: ", mfNonceBruteCommand) - keyList = []; + keyList = [] # Run a max of 16 times. for i in range(16): - loopNum = i+1; + loopNum = i+1 print("----------------------") print(f"Loop {loopNum} of 16") #Run PM3 with the trace # -o means run without connecting to PM3 hardware # -c specifies commands within proxmark 3 software - cmd_list = [pm3Location / pm3Command,"-o","-c", f"trace load -f {trace}; trace list -1 -t mf -f {dictionaryFilepath}; exit"]; + cmd_list = [pm3Location / pm3Command,"-o","-c", f"trace load -f {trace}; trace list -1 -t mf -f {dictionaryFilepath}; exit"] print(f"Viewing tracelog with {len(keyList)} discovered keys") print(f"pm3 {' '.join(cmd_list[1:])}") result = subprocess.run(cmd_list, shell=os.name == 'nt',stdout=subprocess.PIPE, stderr=subprocess.PIPE) - output = result.stdout; + output = result.stdout @@ -111,20 +111,20 @@ def discoverKeys(): print("Found line containing a key:") print(f" {line}") # split the line into "words", which are whitespace separated - words = line.split(" "); + words = line.split(" ") key = "" #find the word "key", and then grab the word directly after it for j in range(len(words)-1): - w = words[j]; + w = words[j] if w == "key" or w == "key:": - key = words[j+1]; #Guaranteed to not be out of bounds because we loop to words length - 1 + key = words[j+1] #Guaranteed to not be out of bounds because we loop to words length - 1 break #If we didn't find a key, skip this line if key == "": - continue; + continue #If key ends with a vertical bar |, remove it key = key.replace('|', '') @@ -133,7 +133,7 @@ def discoverKeys(): #Add this key to our keylist if it's new if key in keyList: print(f" Duplicate key, ignoring: {key}") - continue; + continue keyList.append(key) print(f" Found new key: {key}") @@ -151,7 +151,7 @@ def discoverKeys(): if "mf_nonce_brute" in words[j]: #Add the rest of the line to our arguments args = words[j+1:] #can't be out of bounds because we loop to len(words)-1 - break; + break key = bruteForce(args) @@ -159,14 +159,14 @@ def discoverKeys(): key = strip_color_codes(key) if key == "": - continue; + continue key = key.upper() #Add this key to our keylist if it's new if key in keyList: print(f" Duplicate key, ignoring: {key}") - continue; + continue keyList.append(key) print(f" Found new key: {key}") @@ -179,9 +179,9 @@ def discoverKeys(): for j in range(len(keyList)): print(f" {j}: {keyList[j]}") dictionaryFile.write(keyList[j]) - dictionaryFile.write("\n"); + dictionaryFile.write("\n") print() - dictionaryFile.close(); + dictionaryFile.close() #Done! Show results print(f"{len(keyList)} keys saved to file: {dictionaryFilepath}") @@ -208,7 +208,7 @@ def bruteForce(args): print(f" {line}") #Parse out the key from within the brackets - words = line.split(" "); + words = line.split(" ") for i in range(len(words)-1): if words[i] == "[": return words[i+1] @@ -294,6 +294,5 @@ def testCommands(directories, command, arguments = ""): return None #We didn't find any program that worked +main() #Run main program - -main(); #Run main program From e7210f03c24a92758a65511153598af8cc6c9017 Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Wed, 4 Dec 2024 11:06:30 -0800 Subject: [PATCH 2/7] Only run program if entry script --- traceKeyExtractor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/traceKeyExtractor.py b/traceKeyExtractor.py index 5802707..c7413ed 100644 --- a/traceKeyExtractor.py +++ b/traceKeyExtractor.py @@ -294,5 +294,5 @@ def testCommands(directories, command, arguments = ""): return None #We didn't find any program that worked -main() #Run main program - +if __name__ == "__main__": + main() #Run main program From 21cb5a784de5263c9810e131453ff02fd4fea14a Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Wed, 4 Dec 2024 11:08:42 -0800 Subject: [PATCH 3/7] Remove unused pm3_dirs variable Upon further inspection, on Windows, pm3 can be installed literally anywhere the user decides, so there's no point in retaining a list --- traceKeyExtractor.py | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/traceKeyExtractor.py b/traceKeyExtractor.py index c7413ed..51454da 100644 --- a/traceKeyExtractor.py +++ b/traceKeyExtractor.py @@ -8,10 +8,6 @@ if not sys.version_info >= (3, 6): print("Python 3.6 or higher is required!") exit(-1) -# List of possible directories for Proxmark3 to try -# XXX Populate with results for Windows -pm3_dirs = [] - #Global variables #Default name of the dictionary file we create dictionaryFilename = "myKeyDictionary.dic" #Arbitrary filename for storing dictionary file @@ -247,13 +243,7 @@ def get_proxmark3_location(): pm3_location = which_pm3.parent.parent print(f"Found global installation ({pm3_location})!") return pm3_location - - # Check pm3_dirs paths - pm3_dirs_result = testCommands(pm3_dirs, "bin/pm3", "--help") - if pm3_dirs_result: - print(f"Found installation in {pm3_dirs_result}!") - return pm3_dirs_result - + # At this point, we've tried all the paths to find it print("Failed to find working 'pm3' command. You can set the Proxmark3 directory via the 'PROXMARK3_DIR' environment variable.") exit(-1) # Halt program From 1af755b0e7045c276e0cb290deeeb7b603cbb09c Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Wed, 4 Dec 2024 11:09:15 -0800 Subject: [PATCH 4/7] Bump version number --- traceKeyExtractor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/traceKeyExtractor.py b/traceKeyExtractor.py index 51454da..e46b522 100644 --- a/traceKeyExtractor.py +++ b/traceKeyExtractor.py @@ -24,7 +24,7 @@ def main(): global pm3Location,dictionaryFilepath,trace print("--------------------------------------------------------") - print("RFID Key Extractor v0.2 - Bambu Research Group 2024") + print("RFID Key Extractor v0.2.1 - Bambu Research Group 2024") print("--------------------------------------------------------") print("This will extract the keys from a trace file") print("that was saved from sniffing communication between") @@ -243,7 +243,7 @@ def get_proxmark3_location(): pm3_location = which_pm3.parent.parent print(f"Found global installation ({pm3_location})!") return pm3_location - + # At this point, we've tried all the paths to find it print("Failed to find working 'pm3' command. You can set the Proxmark3 directory via the 'PROXMARK3_DIR' environment variable.") exit(-1) # Halt program From d6b0e0d33143a4a687cf24d9eedd157d1b6948c6 Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Wed, 4 Dec 2024 16:26:00 -0800 Subject: [PATCH 5/7] Allow specifying trace path as command line argument --- traceKeyExtractor.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/traceKeyExtractor.py b/traceKeyExtractor.py index e46b522..5e037af 100644 --- a/traceKeyExtractor.py +++ b/traceKeyExtractor.py @@ -44,17 +44,21 @@ def main(): dictionaryFilepath = os.path.abspath(dictionaryFilename) print(f"Saved dictionary to {dictionaryFilepath}") - #Instruct the user to create a trace file - print() - print("Start by creating a trace file. In the proxmark terminal, execute command `hf 14a sniff -c -r`.") - print("Then, place the Proxmark3 between the RFID reader and spool.") - print("Load in filament and wait for the process to complete, then press the button on the Proxmark3.") - print("Finally, in the proxmark terminal, execute command `trace save -f [FILEPATH]` to create the trace file.") - print("See the GitHub repository for more details.") - print() + if len(sys.argv) > 1: + # If the user included an argument, assume it's the path to the tracefile + trace = os.path.abspath(sys.argv[1]) + else: + #Instruct the user to create a trace file + print() + print("Start by creating a trace file. In the proxmark terminal, execute command `hf 14a sniff -c -r`.") + print("Then, place the Proxmark3 between the RFID reader and spool.") + print("Load in filament and wait for the process to complete, then press the button on the Proxmark3.") + print("Finally, in the proxmark terminal, execute command `trace save -f [FILEPATH]` to create the trace file.") + print("See the GitHub repository for more details.") + print() - #Get the tracename/filepath from user - trace = input("Enter trace name or full trace filepath: ") + #Get the tracename/filepath from user + trace = input("Enter trace name or full trace filepath: ") discoverKeys() From 02577c810d02f1ff9734c508e4d5d8bfadd7fe03 Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Wed, 4 Dec 2024 16:29:07 -0800 Subject: [PATCH 6/7] Make trace filepath an argument instead of a global variable --- traceKeyExtractor.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/traceKeyExtractor.py b/traceKeyExtractor.py index 5e037af..c8e888c 100644 --- a/traceKeyExtractor.py +++ b/traceKeyExtractor.py @@ -18,10 +18,9 @@ pm3Location = None #Calculated. The location of Proxm pm3Command = "bin/pm3" # The command that works to start proxmark3 mfNonceBruteCommand = "share/proxmark3/tools/mf_nonce_brute" # The command to execute mfNonceBrute -trace = "" #Prompted during runtime. Trace filename that the user provides def main(): - global pm3Location,dictionaryFilepath,trace + global pm3Location,dictionaryFilepath print("--------------------------------------------------------") print("RFID Key Extractor v0.2.1 - Bambu Research Group 2024") @@ -60,7 +59,7 @@ def main(): #Get the tracename/filepath from user trace = input("Enter trace name or full trace filepath: ") - discoverKeys() + discoverKeys(trace) print("Keys obtained. Remove the spool from the AMS and place the Proxmark3 on the spool's tag.") print(f"In proxmark terminal, execute command `hf mf fchk -f {dictionaryFilepath} --dump` to create a keyfile from this dictionary.") @@ -68,7 +67,7 @@ def main(): #Loop 16 times to attempt to extract all 16 keys from the tracefile -def discoverKeys(): +def discoverKeys(traceFilepath): print("PROGRAM: ", mfNonceBruteCommand) @@ -83,7 +82,7 @@ def discoverKeys(): #Run PM3 with the trace # -o means run without connecting to PM3 hardware # -c specifies commands within proxmark 3 software - cmd_list = [pm3Location / pm3Command,"-o","-c", f"trace load -f {trace}; trace list -1 -t mf -f {dictionaryFilepath}; exit"] + cmd_list = [pm3Location / pm3Command,"-o","-c", f"trace load -f {traceFilepath}; trace list -1 -t mf -f {dictionaryFilepath}; exit"] print(f"Viewing tracelog with {len(keyList)} discovered keys") print(f"pm3 {' '.join(cmd_list[1:])}") result = subprocess.run(cmd_list, shell=os.name == 'nt',stdout=subprocess.PIPE, stderr=subprocess.PIPE) From 7203a6c5ba7475bbd5565652ab72c7bdff376462 Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Wed, 4 Dec 2024 16:29:57 -0800 Subject: [PATCH 7/7] Move setup commands to its own function --- traceKeyExtractor.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/traceKeyExtractor.py b/traceKeyExtractor.py index c8e888c..982e566 100644 --- a/traceKeyExtractor.py +++ b/traceKeyExtractor.py @@ -18,10 +18,19 @@ pm3Location = None #Calculated. The location of Proxm pm3Command = "bin/pm3" # The command that works to start proxmark3 mfNonceBruteCommand = "share/proxmark3/tools/mf_nonce_brute" # The command to execute mfNonceBrute - -def main(): +def setup(): global pm3Location,dictionaryFilepath + pm3Location = get_proxmark3_location() + + #Create a dictionary file to store keys that we discover + print(f"Creating dictionary file '{dictionaryFilename}'") + dictionaryFile = open(dictionaryFilename, "w") + dictionaryFile.close() + dictionaryFilepath = os.path.abspath(dictionaryFilename) + print(f"Saved dictionary to {dictionaryFilepath}") + +def main(): print("--------------------------------------------------------") print("RFID Key Extractor v0.2.1 - Bambu Research Group 2024") print("--------------------------------------------------------") @@ -34,14 +43,8 @@ def main(): print("--------------------------------------------------------") print("") - pm3Location = get_proxmark3_location() - - #Create a dictionary file to store keys that we discover - print(f"Creating dictionary file '{dictionaryFilename}'") - dictionaryFile = open(dictionaryFilename, "w") - dictionaryFile.close() - dictionaryFilepath = os.path.abspath(dictionaryFilename) - print(f"Saved dictionary to {dictionaryFilepath}") + # Run setup + setup() if len(sys.argv) > 1: # If the user included an argument, assume it's the path to the tracefile