From 5cf0e5764e70da772a835ffa0cf98b46e2224fe2 Mon Sep 17 00:00:00 2001 From: g3gg0 Date: Fri, 22 Mar 2024 22:33:30 +0100 Subject: [PATCH] fixed proxmark3 operations with latest firmwares --- TeddyBench/Proxmark3.cs | 76 ++++++++++++++++++++------------ TeddyBench/TeddyMain.Designer.cs | 8 ++-- TeddyBench/TeddyMain.cs | 6 +-- 3 files changed, 54 insertions(+), 36 deletions(-) diff --git a/TeddyBench/Proxmark3.cs b/TeddyBench/Proxmark3.cs index 9ff7baf..bb00541 100644 --- a/TeddyBench/Proxmark3.cs +++ b/TeddyBench/Proxmark3.cs @@ -91,7 +91,9 @@ public enum eCommandType : short ISO15693_EML_SETMEM = 0x0331, MeasureAntennaTuning = 0x400, - WTX = 0x116 + WTX = 0x116, + Nack = 0xFE, + Ack = 0xFF } public struct OldArgs @@ -106,6 +108,8 @@ public class Pm3UsbCommand public Pm3UsbCommandStructLegacy data = new Pm3UsbCommandStructLegacy(); + public Pm3UsbCommand(eCommandType cmd, byte[] payload) : this(cmd, 0, 0, 0, payload) { } + public Pm3UsbCommand(eCommandType cmd, ulong arg0 = 0, ulong arg1 = 0, ulong arg2 = 0, byte[] payload = null) { data = new Pm3UsbCommandStructLegacy(); @@ -636,12 +640,19 @@ private bool UnlockTag(uint pass, ref int reason) { return false; } - bool supported = false; - Pm3UsbCommand cmd = new Pm3UsbCommand(eCommandType.ISO15693_SLIX_DISABLE_PRIVACY, pass); + + byte[] key = new byte[4]; + key[0] = (byte)((pass >> 0) & 0xFF); + key[1] = (byte)((pass >> 8) & 0xFF); + key[2] = (byte)((pass >> 16) & 0xFF); + key[3] = (byte)((pass >> 24) & 0xFF); + Pm3UsbCommand cmd = new Pm3UsbCommand(eCommandType.ISO15693_SLIX_DISABLE_PRIVACY, key); LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] UnlockTag: Send request for pass 0x" + pass.ToString("X8")); cmd.Write(Port); + int retries = 0; + while (true) { Pm3UsbResponse response = new Pm3UsbResponse(Port); @@ -650,7 +661,6 @@ private bool UnlockTag(uint pass, ref int reason) { case eCommandType.ISO15693_SLIX_DISABLE_PRIVACY: { - supported = true; if (response.Status == 0) { reason = 0; @@ -668,15 +678,14 @@ private bool UnlockTag(uint pass, ref int reason) if (!HandleDefault(response)) { - break; + if (retries++ > 3) + { + break; + } } } - if(!supported) - { - throw new NotSupportedException(); - } - return false; + throw new NotSupportedException(); } private string UIDToString(byte[] uid) @@ -719,15 +728,12 @@ private byte[] SendCommand(byte[] command) } byte[] ret = null; - byte[] buf = CreateIso15CommandBuffer(Iso15Command.ISO15_CONNECT | Iso15Command.ISO15_HIGH_SPEED | Iso15Command.ISO15_READ_RESPONSE, command); - - Pm3UsbCommand cmd = new Pm3UsbCommand(eCommandType.ISO15693_COMMAND, (byte)buf.Length, 1, 1, buf); + Pm3UsbCommand cmd = new Pm3UsbCommand(eCommandType.ISO15693_COMMAND, (byte)command.Length, 1, 1, command); LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] GetResponse: Send " + BitConverter.ToString(command).Replace("-", "")); cmd.Write(Port); - int timeouts = 0; - int successes = 0; + int retries = 0; while (true) { @@ -740,8 +746,6 @@ private byte[] SendCommand(byte[] command) { LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] GetResponse: ACK, returning data (" + response.DataLength + ")"); - if (successes++ == 1) - { ret = new byte[response.DataLength]; Array.Copy(response.DataPtr, ret, response.DataLength); @@ -752,20 +756,20 @@ private byte[] SendCommand(byte[] command) return null; } return ret; - } - - continue; } else { - LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] GetResponse: no tag answered, continue"); - continue; + LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] GetResponse: no tag answered"); + return null; } } if (!HandleDefault(response)) { - break; + if (retries++ > 3) + { + break; + } } } @@ -868,7 +872,7 @@ private void EmulateTagInternal(byte[] data) } } - private bool HandleDefault(Pm3UsbResponse response) + private bool HandleDefault(Pm3UsbResponse response, string comment = "") { switch (response.Cmd) { @@ -884,7 +888,7 @@ private bool HandleDefault(Pm3UsbResponse response) case eCommandType.NoData: case eCommandType.Timeout: - LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] timeout, returning"); + LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] timeout " + comment); return false; default: @@ -984,7 +988,7 @@ private bool MeasureAntennaInternal(MeasurementResult result, eMeasurementType t return true; } - if (!HandleDefault(response)) + if (!HandleDefault(response, "(measurement takes a while)")) { if (timeouts++ > 10) { @@ -1335,6 +1339,10 @@ private bool Flash(List segments, bool bootloader) { Pm3UsbCommand cmdStart = new Pm3UsbCommand(eCommandType.StartFlash, bootloader ? FlashStart : BootloaderEnd, FlashEnd, bootloader ? 0x54494f44UL : 0UL); cmdStart.Write(Port); + if (!ReadLegacyAck()) + { + return false; + } foreach (var seg in segments) { @@ -1377,12 +1385,12 @@ private bool WriteBlock(uint address, byte[] data, int offset, int length) byte[] memBuf = Enumerable.Repeat((byte)0xFF, 0x100).ToArray(); Array.Copy(data, offset, memBuf, 0, length); - //LogWindow.Log(LogWindow.eLogLevel.Debug, "[Flash] Block 0x" + address.ToString("X8") + "..." ); + LogWindow.Log(LogWindow.eLogLevel.Debug, "[Flash] Block 0x" + address.ToString("X8") + "..." ); Pm3UsbCommand finish = new Pm3UsbCommand(eCommandType.FinishWrite, address); Array.Copy(memBuf, finish.data.d, memBuf.Length); finish.Write(Port); - if (!ReadAck()) + if (!ReadLegacyAck()) { return false; } @@ -1390,6 +1398,18 @@ private bool WriteBlock(uint address, byte[] data, int offset, int length) return true; } + private bool ReadLegacyAck() + { + Pm3UsbResponse response = new Pm3UsbResponse(Port); + + if (!response.ResponseLegacy || response.respLegacy.cmd != (int)eCommandType.Ack) + { + LogWindow.Log(LogWindow.eLogLevel.Debug, "[PM3] ReadAck: did not reply with ACK"); + return false; + } + return true; + } + private bool ReadAck() { Pm3UsbResponse res = new Pm3UsbResponse(Port); diff --git a/TeddyBench/TeddyMain.Designer.cs b/TeddyBench/TeddyMain.Designer.cs index 35e25b9..72c4489 100644 --- a/TeddyBench/TeddyMain.Designer.cs +++ b/TeddyBench/TeddyMain.Designer.cs @@ -197,7 +197,6 @@ private void InitializeComponent() // // flashFirmwareToolStripMenuItem // - this.flashFirmwareToolStripMenuItem.Enabled = false; this.flashFirmwareToolStripMenuItem.Name = "flashFirmwareToolStripMenuItem"; this.flashFirmwareToolStripMenuItem.Size = new System.Drawing.Size(226, 22); this.flashFirmwareToolStripMenuItem.Text = "Flash Firmware"; @@ -205,7 +204,6 @@ private void InitializeComponent() // // flashBootloaderToolStripMenuItem // - this.flashBootloaderToolStripMenuItem.Enabled = false; this.flashBootloaderToolStripMenuItem.Name = "flashBootloaderToolStripMenuItem"; this.flashBootloaderToolStripMenuItem.Size = new System.Drawing.Size(226, 22); this.flashBootloaderToolStripMenuItem.Text = "Flash Bootloader"; @@ -337,7 +335,7 @@ private void InitializeComponent() this.splitContainer1.Panel2.Controls.Add(this.btnDelete); this.splitContainer1.Panel2.Controls.Add(this.btnAdd); this.splitContainer1.Size = new System.Drawing.Size(788, 401); - this.splitContainer1.SplitterDistance = 693; + this.splitContainer1.SplitterDistance = 696; this.splitContainer1.SplitterWidth = 1; this.splitContainer1.TabIndex = 1; // @@ -361,7 +359,7 @@ private void InitializeComponent() // splitContainer2.Panel2 // this.splitContainer2.Panel2.Controls.Add(this.lstTonies); - this.splitContainer2.Size = new System.Drawing.Size(693, 401); + this.splitContainer2.Size = new System.Drawing.Size(696, 401); this.splitContainer2.SplitterDistance = 36; this.splitContainer2.SplitterWidth = 1; this.splitContainer2.TabIndex = 1; @@ -443,7 +441,7 @@ private void InitializeComponent() this.lstTonies.Location = new System.Drawing.Point(0, 0); this.lstTonies.Name = "lstTonies"; this.lstTonies.ShowItemToolTips = true; - this.lstTonies.Size = new System.Drawing.Size(693, 364); + this.lstTonies.Size = new System.Drawing.Size(696, 364); this.lstTonies.TabIndex = 0; this.lstTonies.TileSize = new System.Drawing.Size(64, 64); this.lstTonies.UseCompatibleStateImageBehavior = false; diff --git a/TeddyBench/TeddyMain.cs b/TeddyBench/TeddyMain.cs index d6b85ce..30cadfb 100644 --- a/TeddyBench/TeddyMain.cs +++ b/TeddyBench/TeddyMain.cs @@ -316,7 +316,7 @@ private void Proxmark3_FlashResult(object sender, bool e) if (e) { - MessageBox.Show("Flashing the device succeeded, it will reconnect now", "Flashing Proxmark3 done"); + MessageBox.Show("Flashing the device succeeded, will reconnect now", "Flashing Proxmark3 done"); } else { @@ -405,8 +405,8 @@ private void Proxmark3_DeviceFound(object sender, string e) reportProxmarkAnToolStripMenuItem.Enabled = true; reportNFCTagToolStripMenuItem.Enabled = true; - flashBootloaderToolStripMenuItem.Enabled = false; // (RfidReader.DeviceInfo & RfidReaderBase.eDeviceInfo.BootromPresent) != 0; - flashFirmwareToolStripMenuItem.Enabled = false; // (RfidReader.DeviceInfo & RfidReaderBase.eDeviceInfo.BootromPresent) != 0; + flashBootloaderToolStripMenuItem.Enabled = (RfidReader.DeviceInfo & RfidReaderBase.eDeviceInfo.BootromPresent) != 0; + flashFirmwareToolStripMenuItem.Enabled = (RfidReader.DeviceInfo & RfidReaderBase.eDeviceInfo.BootromPresent) != 0; } }