Merge branch 'main' into mf_nonce_brute-fix

This commit is contained in:
Queen Vinyl Da.i'gyu-Kazotetsu committed 2024-12-04 10:53:34 -08:00
commit 99e0e23c1e
14 files changed
+14539 -222

No files matched your search

+88 -81
View File
@@ -1,41 +1,16 @@
#########################
# CONFIGURATION (Begin) #
#########################
# Change these variables to match the install locations for your specific computer
# Some examples are provided, and you can comment/uncomment specific lines, or
# manually type them in if your computer differs from the examples
import subprocess
import os
import re
import sys
from pathlib import Path
# -----------
# PM3 Program
# -----------
#The main proxmark program, for most installations, proxmark3 can be started
# with the command "pm3" in the terminal.
# If your computer doesn't recognize this, you may need to navigate to the actual
# installation directory
#List of possible commands to try
pm3 = [
#"pm3", #Default installation location
"/usr/local/opt/proxmark3/bin/pm3", #MAC Installataion - using BREW
"" #Windows specific install location TBD
]
# ---------------------------------
# Brute Force Tool (mf_nonce_brute)
# ---------------------------------
# This is a tool that comes with the PM3 program
# It is located in the "tools" directory of your PM3 installation location
mfNonceBrute = [
"/usr/local/opt/proxmark3/share/proxmark3/tools/mf_nonce_brute", #MAC Install, using Brew
"" #Windows TBD
]
#########################
# CONFIGURATION (End) #
#########################
# Do not modify anything beyond this point, unless you're a developer
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
@@ -43,17 +18,14 @@ dictionaryFilename = "myKeyDictionary.dic" #Arbitrary filename for storing dict
dictionaryFilepath = "" #Calculated. Absolute path to dictionary file
dictionaryFile = "" #File object for writing keys
pm3Command = ""; #Calculated. The command that works to start proxmark3
mfNonceBruteCommand = "" #Calculated. The command to execute mfNonceBrute
pm3Location = None #Calculated. The location of Proxmark3
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
import subprocess
import os
import re
def main():
global pm3Command, mfNonceBruteCommand,dictionaryFilepath,trace
global pm3Location,dictionaryFilepath,trace
print("--------------------------------------------------------")
print("RFID Key Extractor v0.1 - Bambu Research Group 2024")
@@ -67,19 +39,7 @@ def main():
print("--------------------------------------------------------")
print("")
# Find a "pm3" command that works from a list of OS-specific possibilities
print("Checking program: pm3")
pm3Command = testCommands(pm3,"--help"); #Execute "pm3 --help" to test if the install location works
if pm3Command is None:
print("Failed to find working PM3 command. Modify the \"pm3\" variable in this program to match your installation location")
return; #Halt program
# Find a "mf_nonce_brute" command that works from a list of OS-specific possibilities
print("Checking program: mf_nonce_brute")
mfNonceBruteCommand = testCommands(mfNonceBrute,""); #Execute "mf_nonce_brute" to test if the install location works
if mfNonceBruteCommand is None:
print("Failed to find working mf_nonce_brute command. Modify the \"mf_nonce_brute\" variable in this program to match your installation location")
return; #Halt program
pm3Location = get_proxmark3_location()
#Create a dictionary file to store keys that we discover
print(f"Creating dictionary file '{dictionaryFilename}'")
@@ -88,16 +48,23 @@ 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()
#Get the tracename/filepath from user
print("");
trace = input("Enter trace name or full trace filepath: ")
discoverKeys();
discoverKeys()
print(f"In proxmark terminal, execute command `hf mf fchk -f {dictionaryFilepath} --dump`")
print(" to create a keyfile from this dictionary")
print()
print("Then execute `hf mf dump` to dump the contents of the RFID tag")
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.")
print("Then, execute `hf mf dump` to dump the contents of the RFID tag.")
#Loop 16 times to attempt to extract all 16 keys from the tracefile
@@ -116,9 +83,9 @@ def discoverKeys():
#Run PM3 with the trace
# -o means run without connecting to PM3 hardware
# -c specifies commands within proxmark 3 software
cmd_list = [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(" ".join(cmd_list))
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;
@@ -222,10 +189,10 @@ def discoverKeys():
#Run the mf_nonce_brute program with the provided arguments to decode a key
#Returns a key on success, "" otherwise
def bruteForce(args):
cmd_list = [mfNonceBruteCommand] + args
cmd_list = [pm3Location / mfNonceBruteCommand] + args
print(" Running bruteforce command:")
print(" ",end = "")
print(" ".join(cmd_list))
print(f"tools/mf_nonce_brute {' '.join(cmd_list[1:])}")
result = subprocess.run(cmd_list, shell=os.name == 'nt', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#Parse out the key
@@ -256,6 +223,52 @@ def strip_color_codes(input_string):
# Use the sub method to replace the escape sequences with an empty string
return ansi_escape.sub('', input_string)
def get_proxmark3_location():
# Find a "pm3" command that works from a list of OS-specific possibilities
print("Checking program: pm3")
# Check PROXMARK3_DIR environment variable
if os.environ.get('PROXMARK3_DIR'):
if run_command(os.environ['PROXMARK3_DIR'] + "/bin/pm3", "--help"):
return os.environ['PROXMARK3_DIR']
else:
print("Warning: PROXMARK3_DIR environment variable points to the wrong folder, ignoring")
# Get Homebrew installation
brew_install = run_command(["brew", "--prefix", "proxmark3"])
if brew_install:
print("Found installation via Homebrew!")
return Path(brew_install)
# Get global installation
which_pm3 = run_command(["which", "pm3"])
if which_pm3:
which_pm3 = Path(which_pm3)
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
def run_command(command):
try:
# On Windows, use the shell=True argument to run the command
result = subprocess.run(command, shell=os.name == 'nt', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Check the return code to determine if the command was successful
if result.returncode == 0 or result.returncode == 1:
return result.stdout.decode("utf-8").strip()
return None
except Exception as e:
return None
# Test a list of commands to see which one works
# This lets us provide a list of OS-specific commands, test them
# and figure out which one works on this specific computer
@@ -264,26 +277,20 @@ def strip_color_codes(input_string):
# - arguments: Optional arguments to be appended to the command. Useful for programs that don't exit on their own
# This returns the command (string) of the first working command we encounter
#
def testCommands(commandList, arguments = ""):
def testCommands(directories, command, arguments = ""):
for command in commandList:
for directory in directories:
if directory is None:
continue
#OPTIONAL: add arguments such as "--help" to help identify programs that don't exit on their own
cmd_list = [command, arguments]
cmd_list = [directory+"/"+command, arguments]
#Test if this program works
print(" Trying:", command, end=" ... ")
try:
# On Windows, use the shell=True argument to run the command
result = subprocess.run(cmd_list, shell=os.name == 'nt', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Check the return code to determine if the command was successful
if result.returncode == 0 or result.returncode == 1:
print(" SUCCESS!")
return command
except Exception as e:
#print(e) #DEBUG
print(" FAIL");
continue
print(" Trying:", directory, end=" ... ")
if run_command(cmd_list):
return Path(directory)
return None #We didn't find any program that worked