Skip to content

Commit

Permalink
fixed proxmark3 operations with latest firmwares
Browse files Browse the repository at this point in the history
  • Loading branch information
g3gg0 committed Mar 22, 2024
1 parent 501d33b commit 5cf0e57
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 36 deletions.
76 changes: 48 additions & 28 deletions TeddyBench/Proxmark3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
Expand All @@ -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;
}
}
}

Expand Down Expand Up @@ -868,7 +872,7 @@ private void EmulateTagInternal(byte[] data)
}
}

private bool HandleDefault(Pm3UsbResponse response)
private bool HandleDefault(Pm3UsbResponse response, string comment = "")
{
switch (response.Cmd)
{
Expand All @@ -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:
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -1335,6 +1339,10 @@ private bool Flash(List<MemSegment> 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)
{
Expand Down Expand Up @@ -1377,19 +1385,31 @@ 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;
}

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);
Expand Down
8 changes: 3 additions & 5 deletions TeddyBench/TeddyMain.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions TeddyBench/TeddyMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
}

Expand Down

0 comments on commit 5cf0e57

Please sign in to comment.