2020-02-18 07:49:29 +08:00
#!/usr/bin/env bash
2017-10-24 06:15:02 +08:00
2019-09-09 18:58:11 +08:00
# Usage: run option -h to get help
2020-04-29 21:15:25 +08:00
# BT auto detection
2020-04-21 18:01:26 +08:00
# Shall we look for white HC-06-USB dongle ?
FINDBTDONGLE=true
# Shall we look for rfcomm interface ?
2020-04-29 21:15:25 +08:00
FINDBTRFCOMM=true
# Shall we look for registered BT device ? (Linux only)
2020-04-21 18:01:26 +08:00
FINDBTDIRECT=true
2019-07-15 00:20:41 +08:00
PM3PATH=$(dirname "$0")
2019-09-10 01:55:27 +08:00
FULLIMAGE="fullimage.elf"
BOOTIMAGE="bootrom.elf"
2019-08-31 05:35:29 +08:00
# try pm3 dirs in current repo workdir
if [ -d "$PM3PATH/client/" ]; then
CLIENT="$PM3PATH/client/proxmark3"
# try install dir
elif [ -x "$PM3PATH/proxmark3" ]; then
CLIENT="$PM3PATH/proxmark3"
else
# hope it's installed somehow, still not sure where fw images are...
CLIENT="proxmark3"
fi
2019-04-27 17:25:07 +08:00
2019-10-01 00:30:10 +08:00
PM3LIST=()
2020-04-21 18:01:26 +08:00
SHOWLIST=false
2019-10-01 00:30:10 +08:00
function get_pm3_list_Linux {
2020-04-21 23:11:18 +08:00
N=$1
2019-10-01 00:30:10 +08:00
PM3LIST=()
for DEV in $(find /dev/ttyACM* 2>/dev/null); do
2020-05-16 20:24:21 +08:00
if which udevadm >/dev/null; then
if udevadm info -q property -n "$DEV" | grep -q "ID_VENDOR=proxmark.org"; then
PM3LIST+=("$DEV")
if [ ${#PM3LIST[*]} -ge $N ]; then
return
fi
fi
else
if grep -q "PRODUCT=9ac4/4b8f" "/sys/class/tty/${DEV#/dev/}/../../uevent" 2>/dev/null; then
PM3LIST+=("$DEV")
if [ ${#PM3LIST[*]} -ge $N ]; then
return
fi
2020-04-21 23:11:18 +08:00
fi
2019-07-15 00:20:41 +08:00
fi
2019-03-09 18:10:22 +08:00
done
2020-04-21 18:01:26 +08:00
if $FINDBTDONGLE; then
2020-04-21 18:30:33 +08:00
# check if the HC-06-USB white dongle is present (still, that doesn't tell us if it's paired with a Proxmark3...)
2020-04-21 18:01:26 +08:00
for DEV in $(find /dev/ttyUSB* 2>/dev/null); do
2020-04-29 20:07:26 +08:00
if udevadm info -q property -n "$DEV" | grep -q "ID_MODEL=CP2104_USB_to_UART_Bridge_Controller"; then
2020-04-21 18:01:26 +08:00
PM3LIST+=("$DEV")
2020-04-21 23:11:18 +08:00
if [ ${#PM3LIST[*]} -ge $N ]; then
return
fi
2020-04-21 18:01:26 +08:00
fi
done
fi
2020-04-29 21:15:25 +08:00
if $FINDBTRFCOMM; then
2020-04-21 18:30:33 +08:00
# check if the MAC of a Proxmark3 was bound to a local rfcomm interface
2020-04-29 21:15:25 +08:00
# (on OSes without deprecated rfcomm and hcitool, the loop will be simply skipped)
2020-04-29 20:07:26 +08:00
for DEVMAC in $(rfcomm -a 2>/dev/null | grep " 20:19:0[45]" | sed 's/^\(.*\): \([0-9:]*\) .*/\1@\2/'); do
2020-04-21 18:30:33 +08:00
DEV=${DEVMAC/@*/}
MAC=${DEVMAC/*@/}
2020-04-29 21:15:25 +08:00
# check which are Proxmark3 and, side-effect, if they're actually present
2020-04-21 18:30:33 +08:00
if hcitool name "$MAC" | grep -q "PM3"; then
PM3LIST+=("/dev/$DEV")
2020-04-21 23:11:18 +08:00
if [ ${#PM3LIST[*]} -ge $N ]; then
return
fi
2020-04-21 18:30:33 +08:00
fi
2020-04-21 18:01:26 +08:00
done
fi
2020-04-29 21:15:25 +08:00
if $FINDBTDIRECT; then
# check if the MAC of a Proxmark3 was registered in the known devices
MACS=()
2020-04-29 21:29:34 +08:00
for MAC in $(dbus-send --system --print-reply --type=method_call --dest='org.bluez' '/' org.freedesktop.DBus.ObjectManager.GetManagedObjects 2>/dev/null|\
2020-04-29 21:15:25 +08:00
awk '/"Address"/{getline;gsub(/"/,"",$3);a=$3}/Name/{getline;if (/PM3_RDV4/) print a}'); do
PM3LIST+=("bt:$MAC")
done
# we don't probe the device so there is no guarantee the device is actually present
fi
2017-10-24 06:15:02 +08:00
}
2019-10-01 00:30:10 +08:00
function get_pm3_list_macOS {
2020-04-21 23:11:18 +08:00
N=$1
2019-10-01 00:30:10 +08:00
PM3LIST=()
2020-04-29 20:07:26 +08:00
for DEV in $(ioreg -r -c "IOUSBHostDevice" -l | awk -F '"' '
2019-11-18 20:11:28 +08:00
$2=="USB Vendor Name"{b=($4=="proxmark.org")}
b==1 && $2=="IODialinDevice"{print $4}'); do
2019-10-01 00:30:10 +08:00
PM3LIST+=("$DEV")
2020-04-21 23:11:18 +08:00
if [ ${#PM3LIST[*]} -ge $N ]; then
return
fi
2019-03-09 18:10:22 +08:00
done
2018-08-16 22:22:54 +08:00
}
2019-10-01 00:30:10 +08:00
function get_pm3_list_Windows {
2020-04-21 23:11:18 +08:00
N=$1
2019-10-01 00:30:10 +08:00
PM3LIST=()
2020-04-29 20:07:26 +08:00
# Need to look for this first, the call to Win32_serialport "crashes" then native bt serial port. Don't ask why.
#BT direct SERIAL PORTS (COM)
2020-04-29 21:15:25 +08:00
if $FINDBTRFCOMM; then
2020-04-29 20:07:26 +08:00
for DEV in $(powershell.exe -command "Get-CimInstance -ClassName Win32_PnPEntity | Where-Object Caption -like 'Standard Serial over Bluetooth link (COM*' | Select Name" 2> /dev/null | awk '$0 ~ /COM/{print substr($6,2,4)}'); do
DEV=${DEV/ */}
PM3LIST+=("$DEV")
if [ ${#PM3LIST[*]} -ge $N ]; then
return
fi
done
fi
2020-04-29 18:00:49 +08:00
# Normal SERIAL PORTS (COM)
2020-04-29 20:07:26 +08:00
for DEV in $(powershell.exe -command "Get-CimInstance -ClassName Win32_serialport | Where-Object PNPDeviceID -like '*VID_9AC4&PID_4B8F*' | Select DeviceID" 2>/dev/null | awk '/^COM/{print $1}'); do
2019-10-01 00:30:10 +08:00
DEV=${DEV/ */}
PM3LIST+=("$DEV")
2020-04-21 23:11:18 +08:00
if [ ${#PM3LIST[*]} -ge $N ]; then
return
fi
2019-07-03 20:16:16 +08:00
done
2020-04-29 20:07:26 +08:00
2020-04-29 18:00:49 +08:00
#white BT dongle SERIAL PORTS (COM)
if $FINDBTDONGLE; then
2020-04-29 20:07:26 +08:00
for DEV in $(powershell.exe -command "Get-CimInstance -ClassName Win32_serialport | Where-Object PNPDeviceID -like '*VID_10C4&PID_EA60*' | Select DeviceID" 2>/dev/null | awk '/^COM/{print $1}'); do
2020-04-29 18:00:49 +08:00
DEV=${DEV/ */}
PM3LIST+=("$DEV")
if [ ${#PM3LIST[*]} -ge $N ]; then
return
fi
done
fi
2020-04-29 20:07:26 +08:00
}
function get_pm3_list_WSL {
N=$1
PM3LIST=()
2020-04-29 18:00:49 +08:00
2020-04-29 20:07:26 +08:00
# Need to look for this first, the call to Win32_serialport "crashes" then native bt serial port. Don't ask why.
#BT direct SERIAL PORTS (COM)
2020-04-29 21:15:25 +08:00
if $FINDBTRFCOMM; then
2020-04-29 18:00:49 +08:00
for DEV in $(powershell.exe -command "Get-CimInstance -ClassName Win32_PnPEntity | Where-Object Caption -like 'Standard Serial over Bluetooth link (COM*' | Select Name" 2> /dev/null | awk '$0 ~ /COM/{print substr($6,2,4)}'); do
2020-04-29 20:07:26 +08:00
2020-04-29 18:00:49 +08:00
DEV=${DEV/ */}
2020-04-29 20:07:26 +08:00
DEV="/dev/ttyS${DEV#COM}"
# ttyS counterpart takes some more time to appear
if [ -e "$DEV" ]; then
PM3LIST+=("$DEV")
if [ ! -w "$DEV" ]; then
echo "[!!] Let's give users read/write access to $DEV"
sudo chmod 666 "$DEV"
fi
if [ ${#PM3LIST[*]} -ge $N ]; then
return
fi
2020-04-29 18:00:49 +08:00
fi
2020-04-29 20:07:26 +08:00
done
2020-04-29 18:00:49 +08:00
fi
2019-07-03 20:16:16 +08:00
2020-04-29 18:00:49 +08:00
# Normal SERIAL PORTS (COM)
2020-04-29 20:07:26 +08:00
for DEV in $(powershell.exe -command "Get-CimInstance -ClassName Win32_serialport | Where-Object PNPDeviceID -like '*VID_9AC4&PID_4B8F*' | Select DeviceID" 2>/dev/null | awk '/^COM/{print $1}'); do
2019-10-01 00:30:10 +08:00
DEV=${DEV/ */}
DEV="/dev/ttyS${DEV#COM}"
# ttyS counterpart takes some more time to appear
if [ -e "$DEV" ]; then
PM3LIST+=("$DEV")
if [ ! -w "$DEV" ]; then
echo "[!!] Let's give users read/write access to $DEV"
sudo chmod 666 "$DEV"
fi
2020-04-21 23:11:18 +08:00
if [ ${#PM3LIST[*]} -ge $N ]; then
return
fi
2019-07-13 06:06:19 +08:00
fi
done
2020-04-29 18:00:49 +08:00
2020-04-29 20:07:26 +08:00
#white BT dongle SERIAL PORTS (COM)
2020-04-29 18:00:49 +08:00
if $FINDBTDONGLE; then
2020-04-29 20:07:26 +08:00
for DEV in $(powershell.exe -command "Get-CimInstance -ClassName Win32_serialport | Where-Object PNPDeviceID -like '*VID_10C4&PID_EA60*' | Select DeviceID" 2>/dev/null | awk '/^COM/{print $1}'); do
2020-04-29 18:00:49 +08:00
DEV=${DEV/ */}
2020-04-29 20:07:26 +08:00
DEV="/dev/ttyS${DEV#COM}"
2020-04-29 18:00:49 +08:00
# ttyS counterpart takes some more time to appear
if [ -e "$DEV" ]; then
PM3LIST+=("$DEV")
if [ ! -w "$DEV" ]; then
echo "[!!] Let's give users read/write access to $DEV"
sudo chmod 666 "$DEV"
fi
if [ ${#PM3LIST[*]} -ge $N ]; then
return
fi
fi
done
fi
2019-07-13 06:06:19 +08:00
}
2019-04-27 17:25:07 +08:00
SCRIPT=$(basename -- "$0")
2018-08-16 22:22:54 +08:00
2019-08-31 05:35:29 +08:00
if [ "$SCRIPT" = "pm3" ]; then
CMD() { $CLIENT "$@"; }
2019-09-09 18:58:11 +08:00
HELP() {
cat << EOF
2019-10-01 00:30:10 +08:00
Quick helper script for proxmark3 client when working with a Proxmark3 device connected via USB
2019-09-09 18:58:11 +08:00
Description:
The usage is the same as for the proxmark3 client, with the following differences:
* 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).
2019-10-01 00:30:10 +08:00
You can also specify a first option -n N to access the Nth Proxmark3 connected on USB.
2019-09-09 18:58:11 +08:00
Don't use this script if you want to work offline or with the BT addon.
2020-04-21 18:05:47 +08:00
To see a list of available ports, use --list.
2019-09-09 18:58:11 +08:00
Usage:
2019-10-01 00:30:10 +08:00
$SCRIPT [-n <N>] [-f] [-c <command>]|[-l <lua_script_file>]|[-s <cmd_script_file>] [-i]
2020-04-21 18:05:47 +08:00
$SCRIPT --list
2019-09-09 18:58:11 +08:00
See "$CLIENT -h" for more details on options.
EOF
}
2019-09-09 07:07:46 +08:00
elif [ "$SCRIPT" = "pm3-flash" ]; then
2020-04-21 18:01:26 +08:00
FINDBTDONGLE=false
2020-04-29 21:15:25 +08:00
FINDBTRFCOMM=false
2020-04-21 18:01:26 +08:00
FINDBTDIRECT=false
2019-09-09 07:07:46 +08:00
CMD() {
2019-10-01 00:30:10 +08:00
ARGS=("--port" "$1" "--flash")
2019-09-09 07:07:46 +08:00
shift;
while [ "$1" != "" ]; do
if [ "$1" == "-b" ]; then
ARGS+=("--unlock-bootloader")
else
ARGS+=("--image" "$1")
fi
shift;
done
$CLIENT ${ARGS[@]};
}
2019-09-09 18:58:11 +08:00
HELP() {
cat << EOF
Quick helper script for flashing a Proxmark device via USB
Description:
The usage is similar to the old proxmark3-flasher binary, except that the correct port name will be automatically guessed.
2019-10-01 00:30:10 +08:00
You can also specify a first option -n N to access the Nth Proxmark3 connected on USB.
2019-09-09 18:58:11 +08:00
If this doesn't work, you'll have to use manually the proxmark3 client, see "$CLIENT -h".
2020-04-21 18:05:47 +08:00
To see a list of available ports, use --list.
2019-09-09 18:58:11 +08:00
Usage:
2019-10-01 00:30:10 +08:00
$SCRIPT [-n <N>] [-b] image.elf [image.elf...]
2020-04-21 18:05:47 +08:00
$SCRIPT --list
2019-09-09 18:58:11 +08:00
Options:
-b Enable flashing of bootloader area (DANGEROUS)
Example:
2019-09-09 19:24:45 +08:00
$SCRIPT -b bootrom.elf fullimage.elf
2019-09-09 18:58:11 +08:00
EOF
}
2019-08-31 05:35:29 +08:00
elif [ "$SCRIPT" = "pm3-flash-all" ]; then
2020-04-21 18:01:26 +08:00
FINDBTDONGLE=false
2020-04-29 21:15:25 +08:00
FINDBTRFCOMM=false
2020-04-21 18:01:26 +08:00
FINDBTDIRECT=false
2019-10-01 00:30:10 +08:00
CMD() { $CLIENT "--port" "$1" "--flash" "--unlock-bootloader" "--image" "$BOOTIMAGE" "--image" "$FULLIMAGE"; }
2019-09-09 18:58:11 +08:00
HELP() {
cat << EOF
Quick helper script for flashing a Proxmark device via USB
Description:
The correct port name will be automatically guessed and the stock bootloader and firmware image will be flashed.
2019-10-01 00:30:10 +08:00
You can also specify a first option -n N to access the Nth Proxmark3 connected on USB.
2019-09-09 18:58:11 +08:00
If this doesn't work, you'll have to use manually the proxmark3 client, see "$CLIENT -h".
2020-04-21 18:05:47 +08:00
To see a list of available ports, use --list.
2019-09-09 18:58:11 +08:00
Usage:
2019-10-01 00:30:10 +08:00
$SCRIPT [-n <N>]
2020-04-21 18:05:47 +08:00
$SCRIPT --list
2019-09-09 18:58:11 +08:00
EOF
}
2019-08-31 05:35:29 +08:00
elif [ "$SCRIPT" = "pm3-flash-fullimage" ]; then
2020-04-21 18:01:26 +08:00
FINDBTDONGLE=false
2020-04-29 21:15:25 +08:00
FINDBTRFCOMM=false
2020-04-21 18:01:26 +08:00
FINDBTDIRECT=false
2019-10-01 00:30:10 +08:00
CMD() { $CLIENT "--port" "$1" "--flash" "--image" "$FULLIMAGE"; }
2019-09-09 18:58:11 +08:00
HELP() {
cat << EOF
Quick helper script for flashing a Proxmark device via USB
Description:
The correct port name will be automatically guessed and the stock firmware image will be flashed.
2019-10-01 00:30:10 +08:00
You can also specify a first option -n N to access the Nth Proxmark3 connected on USB.
2019-09-09 18:58:11 +08:00
If this doesn't work, you'll have to use manually the proxmark3 client, see "$CLIENT -h".
2020-04-21 18:05:47 +08:00
To see a list of available ports, use --list.
2019-09-09 18:58:11 +08:00
Usage:
2019-10-01 00:30:10 +08:00
$SCRIPT [-n <N>]
2020-04-21 18:05:47 +08:00
$SCRIPT --list
2019-09-09 18:58:11 +08:00
EOF
}
2019-08-31 05:35:29 +08:00
elif [ "$SCRIPT" = "pm3-flash-bootrom" ]; then
2020-04-21 18:01:26 +08:00
FINDBTDONGLE=false
2020-04-29 21:15:25 +08:00
FINDBTRFCOMM=false
2020-04-21 18:01:26 +08:00
FINDBTDIRECT=false
2019-10-01 00:30:10 +08:00
CMD() { $CLIENT "--port" "$1" "--flash" "--unlock-bootloader" "--image" "$BOOTIMAGE"; }
2019-09-09 18:58:11 +08:00
HELP() {
cat << EOF
Quick helper script for flashing a Proxmark device via USB
Description:
The correct port name will be automatically guessed and the stock bootloader will be flashed.
2019-10-01 00:30:10 +08:00
You can also specify a first option -n N to access the Nth Proxmark3 connected on USB.
2019-09-09 18:58:11 +08:00
If this doesn't work, you'll have to use manually the proxmark3 client, see "$CLIENT -h".
2020-04-21 18:05:47 +08:00
To see a list of available ports, use --list.
2019-09-09 18:58:11 +08:00
Usage:
2019-10-01 00:30:10 +08:00
$SCRIPT [-n <N>]
2020-04-21 18:05:47 +08:00
$SCRIPT --list
2019-09-09 18:58:11 +08:00
EOF
}
2019-04-27 17:25:07 +08:00
else
2019-08-13 04:31:27 +08:00
echo "[!!] Script ran under unknown name, abort: $SCRIPT"
2019-04-27 17:25:07 +08:00
exit 1
fi
2019-09-09 18:58:11 +08:00
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
HELP
exit 0
fi
2019-10-01 00:30:10 +08:00
# 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
2020-04-21 18:01:26 +08:00
if [ "$1" == "--list" ]; then
shift
if [ "$1" != "" ]; then
echo "Option --list must be used alone"
exit 1
fi
SHOWLIST=true
fi
2019-10-01 00:30:10 +08:00
# 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
2019-04-27 17:25:07 +08:00
HOSTOS=$(uname | awk '{print toupper($0)}')
if [ "$HOSTOS" = "LINUX" ]; then
2019-07-13 06:06:19 +08:00
if uname -a|grep -q Microsoft; then
2019-10-01 00:30:10 +08:00
# 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
2019-07-13 06:06:19 +08:00
else
2019-10-01 00:30:10 +08:00
GETPM3LIST=get_pm3_list_Linux
2019-07-13 06:06:19 +08:00
fi
2019-04-27 17:25:07 +08:00
elif [ "$HOSTOS" = "DARWIN" ]; then
2019-10-01 00:30:10 +08:00
GETPM3LIST=get_pm3_list_macOS
2019-07-03 20:16:16 +08:00
elif [[ "$HOSTOS" =~ MINGW(32|64)_NT* ]]; then
2019-10-01 00:30:10 +08:00
GETPM3LIST=get_pm3_list_Windows
2019-04-27 17:25:07 +08:00
else
2019-08-13 04:31:27 +08:00
echo "[!!] Host OS not recognized, abort: $HOSTOS"
2019-04-27 17:25:07 +08:00
exit 1
2018-08-16 22:22:54 +08:00
fi
2019-10-01 00:30:10 +08:00
2020-04-21 18:01:26 +08:00
if $SHOWLIST; then
2020-04-21 23:11:18 +08:00
# Probe for up to 9 devs
$GETPM3LIST 9
2020-04-21 18:01:26 +08:00
if [ ${#PM3LIST} -lt 1 ]; then
echo "[!!] No port found"
exit 0
fi
n=1
for DEV in ${PM3LIST[@]}
do
echo "$n: $DEV"
n=$((n+1))
done
exit 0
fi
2019-10-01 00:30:10 +08:00
# Wait till we get at least N proxmark3 devices
2020-04-21 23:11:18 +08:00
$GETPM3LIST $N
2020-04-21 18:01:26 +08:00
if [ ${#PM3LIST} -lt $N ]; then
echo >&2 "[=] Waiting for Proxmark3 to appear..."
fi
2019-10-01 00:30:10 +08:00
while true; do
if [ ${#PM3LIST[*]} -ge $N ]; then
break
fi
sleep .1
2020-04-21 23:11:18 +08:00
$GETPM3LIST $N
2019-10-01 00:30:10 +08:00
done
if [ ${#PM3LIST} -lt $N ]; then
echo "[!!] No port found, abort"
2019-04-27 17:25:07 +08:00
exit 1
fi
2019-10-01 00:30:10 +08:00
CMD "${PM3LIST[$((N-1))]}" "$@"
2019-04-27 17:25:07 +08:00
exit $?