Better device detection in pm3 script, add -n option

This commit is contained in:
Philippe Teuwen 2019-09-30 18:30:10 +02:00
commit e978b180b4
2 changed files with 90 additions and 65 deletions

View file

@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file.
This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log...
## [unreleased][unreleased] ## [unreleased][unreleased]
- Add option `-n` to scripts pm3* (@doegox)
- Add `wiegand list/encode/decode` - wiegand format manipulation. Adapted to fit here. (@grauerfuchs) - Add `wiegand list/encode/decode` - wiegand format manipulation. Adapted to fit here. (@grauerfuchs)
- Add `lf t55xx protect` - sets password and enables password protection on t55x7 tag (@iceman1001) - Add `lf t55xx protect` - sets password and enables password protection on t55x7 tag (@iceman1001)
- Chg `lf t55xx wipe` - now accepts user provided configuration block (@iceman1001) - Chg `lf t55xx wipe` - now accepts user provided configuration block (@iceman1001)

154
pm3
View file

@ -16,66 +16,46 @@ else
CLIENT="proxmark3" CLIENT="proxmark3"
fi fi
function wait4proxmark_Linux { PM3LIST=()
echo >&2 "[=] Waiting for Proxmark3 to appear..."
while true; do function get_pm3_list_Linux {
PM3=$(find /dev/pm3-* /dev/ttyACM* 2>/dev/null | head -1) PM3LIST=()
if [[ $PM3 != "" ]]; then for DEV in $(find /dev/ttyACM* 2>/dev/null); do
break if udevadm info -q property -n "$DEV" |grep -q "ID_MODEL=proxmark3"; then
PM3LIST+=("$DEV")
fi fi
sleep .1
done done
echo "$PM3"
} }
function wait4proxmark_macOS { function get_pm3_list_macOS {
echo >&2 "[=] Waiting for Proxmark3 to appear..." PM3LIST=()
while true; do for DEV in $(ioreg -r -n proxmark3 -l|awk -F '"' '/IODialinDevice/{print $4}'); do
PM3=$(find /dev/pm3-* /dev/tty.usbmodem* 2>/dev/null | head -1) PM3LIST+=("$DEV")
if [[ $PM3 != "" ]]; then
break
fi
sleep .1
done done
echo "$PM3"
} }
function wait4proxmark_Windows { function get_pm3_list_Windows {
echo >&2 "[=] Waiting for Proxmark3 to appear..." PM3LIST=()
while true; do for DEV in $(wmic path Win32_SerialPort where "PNPDeviceID like '%VID_9AC4&PID_4B8F%'" get DeviceID,PNPDeviceID 2>/dev/null|awk '/^COM/{print $1}'); do
device=$(wmic path Win32_SerialPort where "PNPDeviceID like '%VID_9AC4&PID_4B8F%'" get DeviceID,PNPDeviceID 2>/dev/null | awk 'NR==2') DEV=${DEV/ */}
if [[ $device != "" ]]; then PM3LIST+=("$DEV")
PM3=${device/ */}
break
fi
sleep .1
done done
echo "$PM3"
} }
function wait4proxmark_WSL { function get_pm3_list_WSL {
# Test presence of wmic PM3LIST=()
wmic.exe computersystem get name >/dev/null 2>&1 for DEV in $(wmic.exe path Win32_SerialPort where "PNPDeviceID like '%VID_9AC4&PID_4B8F%'" get DeviceID,PNPDeviceID 2>/dev/null|awk '/^COM/{print $1}'); do
if [ $? -ne 0 ]; then DEV=${DEV/ */}
echo "[!] Cannot run wmic.exe, are you sure your WSL is authorized to run Windows processes? (cf WSL interop flag)" DEV="/dev/ttyS${DEV#COM}"
exit 1 # ttyS counterpart takes some more time to appear
fi if [ -e "$DEV" ]; then
PM3LIST+=("$DEV")
echo >&2 "[=] Waiting for Proxmark3 to appear..." if [ ! -w "$DEV" ]; then
while true; do echo "[!!] Let's give users read/write access to $DEV"
device=$(wmic.exe path Win32_SerialPort where "PNPDeviceID like '%VID_9AC4&PID_4B8F%'" get DeviceID,PNPDeviceID 2>/dev/null | awk 'NR==2') sudo chmod 666 "$DEV"
if [[ $device != "" ]]; then fi
PM3=${device/ */}
PM3="/dev/ttyS${PM3#COM}"
break
fi fi
sleep .1
done done
if [ -e "$PM3" ] && [ ! -w "$PM3" ]; then
echo "[!!] We need to give current user read/write access to $PM3"
sudo chmod 666 "$PM3"
fi
echo "$PM3"
} }
SCRIPT=$(basename -- "$0") SCRIPT=$(basename -- "$0")
@ -84,23 +64,24 @@ if [ "$SCRIPT" = "pm3" ]; then
CMD() { $CLIENT "$@"; } CMD() { $CLIENT "$@"; }
HELP() { HELP() {
cat << EOF cat << EOF
Quick helper script for proxmark3 client when working with a Proxmark device connected via USB Quick helper script for proxmark3 client when working with a Proxmark3 device connected via USB
Description: Description:
The usage is the same as for the proxmark3 client, with the following differences: The usage is the same as for the proxmark3 client, with the following differences:
* the correct port name will be automatically guessed; * the correct port name will be automatically guessed;
* the script will wait for a Proxmark to be connected (same as option -w of the client). * the script will wait for a Proxmark to be connected (same as option -w of the client).
You can also specify a first option -n N to access the Nth Proxmark3 connected on USB.
Don't use this script if you want to work offline or with the BT addon. Don't use this script if you want to work offline or with the BT addon.
Usage: Usage:
$SCRIPT [-f] [-c <command>]|[-l <lua_script_file>]|[-s <cmd_script_file>] [-i] $SCRIPT [-n <N>] [-f] [-c <command>]|[-l <lua_script_file>]|[-s <cmd_script_file>] [-i]
See "$CLIENT -h" for more details on options. See "$CLIENT -h" for more details on options.
EOF EOF
} }
elif [ "$SCRIPT" = "pm3-flash" ]; then elif [ "$SCRIPT" = "pm3-flash" ]; then
CMD() { CMD() {
ARGS=("$1" "--flash") ARGS=("--port" "$1" "--flash")
shift; shift;
while [ "$1" != "" ]; do while [ "$1" != "" ]; do
if [ "$1" == "-b" ]; then if [ "$1" == "-b" ]; then
@ -118,10 +99,11 @@ Quick helper script for flashing a Proxmark device via USB
Description: Description:
The usage is similar to the old proxmark3-flasher binary, except that the correct port name will be automatically guessed. The usage is similar to the old proxmark3-flasher binary, except that the correct port name will be automatically guessed.
You can also specify a first option -n N to access the Nth Proxmark3 connected on USB.
If this doesn't work, you'll have to use manually the proxmark3 client, see "$CLIENT -h". If this doesn't work, you'll have to use manually the proxmark3 client, see "$CLIENT -h".
Usage: Usage:
$SCRIPT [-b] image.elf [image.elf...] $SCRIPT [-n <N>] [-b] image.elf [image.elf...]
Options: Options:
-b Enable flashing of bootloader area (DANGEROUS) -b Enable flashing of bootloader area (DANGEROUS)
@ -131,45 +113,48 @@ Example:
EOF EOF
} }
elif [ "$SCRIPT" = "pm3-flash-all" ]; then elif [ "$SCRIPT" = "pm3-flash-all" ]; then
CMD() { $CLIENT "$1" "--flash" "--unlock-bootloader" "--image" "$BOOTIMAGE" "--image" "$FULLIMAGE"; } CMD() { $CLIENT "--port" "$1" "--flash" "--unlock-bootloader" "--image" "$BOOTIMAGE" "--image" "$FULLIMAGE"; }
HELP() { HELP() {
cat << EOF cat << EOF
Quick helper script for flashing a Proxmark device via USB Quick helper script for flashing a Proxmark device via USB
Description: Description:
The correct port name will be automatically guessed and the stock bootloader and firmware image will be flashed. The correct port name will be automatically guessed and the stock bootloader and firmware image will be flashed.
You can also specify a first option -n N to access the Nth Proxmark3 connected on USB.
If this doesn't work, you'll have to use manually the proxmark3 client, see "$CLIENT -h". If this doesn't work, you'll have to use manually the proxmark3 client, see "$CLIENT -h".
Usage: Usage:
$SCRIPT $SCRIPT [-n <N>]
EOF EOF
} }
elif [ "$SCRIPT" = "pm3-flash-fullimage" ]; then elif [ "$SCRIPT" = "pm3-flash-fullimage" ]; then
CMD() { $CLIENT "$1" "--flash" "--image" "$FULLIMAGE"; } CMD() { $CLIENT "--port" "$1" "--flash" "--image" "$FULLIMAGE"; }
HELP() { HELP() {
cat << EOF cat << EOF
Quick helper script for flashing a Proxmark device via USB Quick helper script for flashing a Proxmark device via USB
Description: Description:
The correct port name will be automatically guessed and the stock firmware image will be flashed. The correct port name will be automatically guessed and the stock firmware image will be flashed.
You can also specify a first option -n N to access the Nth Proxmark3 connected on USB.
If this doesn't work, you'll have to use manually the proxmark3 client, see "$CLIENT -h". If this doesn't work, you'll have to use manually the proxmark3 client, see "$CLIENT -h".
Usage: Usage:
$SCRIPT $SCRIPT [-n <N>]
EOF EOF
} }
elif [ "$SCRIPT" = "pm3-flash-bootrom" ]; then elif [ "$SCRIPT" = "pm3-flash-bootrom" ]; then
CMD() { $CLIENT "$1" "--flash" "--unlock-bootloader" "--image" "$BOOTIMAGE"; } CMD() { $CLIENT "--port" "$1" "--flash" "--unlock-bootloader" "--image" "$BOOTIMAGE"; }
HELP() { HELP() {
cat << EOF cat << EOF
Quick helper script for flashing a Proxmark device via USB Quick helper script for flashing a Proxmark device via USB
Description: Description:
The correct port name will be automatically guessed and the stock bootloader will be flashed. The correct port name will be automatically guessed and the stock bootloader will be flashed.
You can also specify a first option -n N to access the Nth Proxmark3 connected on USB.
If this doesn't work, you'll have to use manually the proxmark3 client, see "$CLIENT -h". If this doesn't work, you'll have to use manually the proxmark3 client, see "$CLIENT -h".
Usage: Usage:
$SCRIPT $SCRIPT [-n <N>]
EOF EOF
} }
else else
@ -180,25 +165,64 @@ if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
HELP HELP
exit 0 exit 0
fi fi
# if a port is already provided, let's just run the command as such
for ARG in "$@"; do
if [ "$ARG" == "-p" ]; then
CMD "$@"
exit $?
fi
done
# Number of the proxmark3 we're interested in
N=1
if [ "$1" == "-n" ]; then
shift
if [ "$1" -ge 1 ] && [ "$1" -lt 10 ]; then
N=$1
shift
else
echo "Option -n requires a number between 1 and 9, got \"$1\""
exit 1
fi
fi
echo >&2 "[=] Waiting for Proxmark3 to appear..."
HOSTOS=$(uname | awk '{print toupper($0)}') HOSTOS=$(uname | awk '{print toupper($0)}')
if [ "$HOSTOS" = "LINUX" ]; then if [ "$HOSTOS" = "LINUX" ]; then
if uname -a|grep -q Microsoft; then if uname -a|grep -q Microsoft; then
PORT=$(wait4proxmark_WSL) # Test presence of wmic
wmic.exe computersystem get name >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "[!] Cannot run wmic.exe, are you sure your WSL is authorized to run Windows processes? (cf WSL interop flag)"
exit 1
fi
GETPM3LIST=get_pm3_list_WSL
else else
PORT=$(wait4proxmark_Linux) GETPM3LIST=get_pm3_list_Linux
fi fi
elif [ "$HOSTOS" = "DARWIN" ]; then elif [ "$HOSTOS" = "DARWIN" ]; then
PORT=$(wait4proxmark_macOS) GETPM3LIST=get_pm3_list_macOS
elif [[ "$HOSTOS" =~ MINGW(32|64)_NT* ]]; then elif [[ "$HOSTOS" =~ MINGW(32|64)_NT* ]]; then
PORT=$(wait4proxmark_Windows) GETPM3LIST=get_pm3_list_Windows
else else
echo "[!!] Host OS not recognized, abort: $HOSTOS" echo "[!!] Host OS not recognized, abort: $HOSTOS"
exit 1 exit 1
fi fi
if [ "$PORT" = "" ]; then
echo "[!!] No port, abort" # Wait till we get at least N proxmark3 devices
while true; do
$GETPM3LIST $N
if [ ${#PM3LIST[*]} -ge $N ]; then
break
fi
sleep .1
done
if [ ${#PM3LIST} -lt $N ]; then
echo "[!!] No port found, abort"
exit 1 exit 1
fi fi
CMD "$PORT" "$@" CMD "${PM3LIST[$((N-1))]}" "$@"
exit $? exit $?