2011-11-06 19:19:07 +00:00
|
|
|
/* packet-rfid-mifare.c
|
|
|
|
* Dissector for the NXP MiFare Protocol
|
|
|
|
*
|
|
|
|
* References:
|
|
|
|
* http://code.google.com/p/nfc-tools/source/browse/trunk/libfreefare/libfreefare/mifare_classic.c
|
|
|
|
* http://www.nxp.com/documents/data_sheet/MF1S703x.pdf
|
|
|
|
* http://www.nxp.com/documents/application_note/AN1304.pdf
|
2011-11-14 20:04:04 +00:00
|
|
|
*
|
2011-11-06 19:19:07 +00:00
|
|
|
* Copyright 2011, Tyson Key <tyson.key@gmail.com>
|
|
|
|
*
|
2011-11-06 20:20:14 +00:00
|
|
|
* $Id$
|
2011-11-06 19:19:07 +00:00
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
*/
|
2011-11-14 20:04:04 +00:00
|
|
|
|
2011-11-06 19:19:07 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <epan/packet.h>
|
|
|
|
|
|
|
|
static int proto_mifare = -1;
|
|
|
|
|
|
|
|
static int hf_mifare_command = -1;
|
|
|
|
static int hf_mifare_block_address = -1;
|
|
|
|
static int hf_mifare_key_a = -1;
|
|
|
|
static int hf_mifare_key_b = -1;
|
|
|
|
static int hf_mifare_uid = -1;
|
|
|
|
static int hf_mifare_operand = -1;
|
|
|
|
|
2011-11-14 20:04:04 +00:00
|
|
|
#define AUTH_A 0x60
|
|
|
|
#define AUTH_B 0x61
|
|
|
|
#define READ 0x30
|
|
|
|
#define WRITE 0xA0
|
|
|
|
#define TRANSFER 0xB0
|
2011-11-06 19:19:07 +00:00
|
|
|
#define DECREMENT 0xC0
|
|
|
|
#define INCREMENT 0xC1
|
2011-11-14 20:04:04 +00:00
|
|
|
#define RESTORE 0xC2
|
2011-11-06 19:19:07 +00:00
|
|
|
|
|
|
|
static const value_string hf_mifare_commands[] = {
|
2011-11-14 20:04:04 +00:00
|
|
|
{AUTH_A, "AUTH_A"},
|
|
|
|
{AUTH_B, "AUTH_B"},
|
|
|
|
{READ, "READ"},
|
|
|
|
{WRITE, "WRITE"},
|
|
|
|
{TRANSFER, "TRANSFER"},
|
2011-11-06 19:19:07 +00:00
|
|
|
{DECREMENT, "DECREMENT"},
|
|
|
|
{INCREMENT, "INCREMENT"},
|
2011-11-14 20:04:04 +00:00
|
|
|
{RESTORE, "RESTORE"},
|
2011-11-06 19:19:07 +00:00
|
|
|
|
|
|
|
/* End of commands */
|
|
|
|
{0x00, NULL}
|
|
|
|
};
|
|
|
|
|
2011-11-14 20:04:04 +00:00
|
|
|
static dissector_handle_t data_handle;
|
2012-01-13 21:44:13 +00:00
|
|
|
static dissector_table_t mifare_dissector_table;
|
2011-11-06 19:19:07 +00:00
|
|
|
|
|
|
|
/* Subtree handles: set by register_subtree_array */
|
|
|
|
static gint ett_mifare = -1;
|
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
static void
|
|
|
|
dissect_mifare(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
|
2011-11-06 19:19:07 +00:00
|
|
|
{
|
|
|
|
proto_item *item;
|
|
|
|
proto_tree *mifare_tree;
|
2012-01-13 21:44:13 +00:00
|
|
|
guint8 cmd;
|
|
|
|
tvbuff_t *next_tvb = NULL;
|
2011-11-06 19:19:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
col_set_str(pinfo->cinfo, COL_PROTOCOL, "MiFare");
|
|
|
|
col_set_str(pinfo->cinfo, COL_INFO, "MiFare Packet");
|
|
|
|
|
|
|
|
if (tree) {
|
|
|
|
/* Start with a top-level item to add everything else to */
|
|
|
|
|
|
|
|
item = proto_tree_add_item(tree, proto_mifare, tvb, 0, -1, ENC_NA);
|
|
|
|
mifare_tree = proto_item_add_subtree(item, ett_mifare);
|
|
|
|
|
|
|
|
proto_tree_add_item(mifare_tree, hf_mifare_command, tvb, 0, 1, ENC_NA);
|
|
|
|
cmd = tvb_get_guint8(tvb, 0);
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
|
|
|
|
case AUTH_A:
|
2012-01-13 21:44:13 +00:00
|
|
|
proto_tree_add_item(mifare_tree, hf_mifare_block_address, tvb, 1, 1, ENC_BIG_ENDIAN);
|
|
|
|
proto_tree_add_item(mifare_tree, hf_mifare_key_a, tvb, 2, 6, ENC_BIG_ENDIAN);
|
|
|
|
proto_tree_add_item(mifare_tree, hf_mifare_uid, tvb, 8, 4, ENC_BIG_ENDIAN);
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
col_set_str(pinfo->cinfo, COL_INFO, "Authenticate with Key A");
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
break;
|
2011-11-06 19:19:07 +00:00
|
|
|
|
|
|
|
case AUTH_B:
|
2012-01-13 21:44:13 +00:00
|
|
|
proto_tree_add_item(mifare_tree, hf_mifare_block_address, tvb, 1, 1, ENC_BIG_ENDIAN);
|
|
|
|
proto_tree_add_item(mifare_tree, hf_mifare_key_b, tvb, 2, 6, ENC_BIG_ENDIAN);
|
|
|
|
proto_tree_add_item(mifare_tree, hf_mifare_uid, tvb, 8, 4, ENC_BIG_ENDIAN);
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
col_set_str(pinfo->cinfo, COL_INFO, "Authenticate with Key B");
|
2011-11-14 20:04:04 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
break;
|
2011-11-06 19:19:07 +00:00
|
|
|
|
|
|
|
case READ:
|
2012-01-13 21:44:13 +00:00
|
|
|
proto_tree_add_item(mifare_tree, hf_mifare_block_address, tvb, 1, 1, ENC_BIG_ENDIAN);
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
col_set_str(pinfo->cinfo, COL_INFO, "Read");
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
break;
|
2011-11-06 19:19:07 +00:00
|
|
|
|
|
|
|
case WRITE:
|
2012-01-13 21:44:13 +00:00
|
|
|
col_set_str(pinfo->cinfo, COL_INFO, "Write");
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
/* LibNFC and the TouchATag-branded reader don't expose the 2-byte CRC
|
|
|
|
or 4-bit NAK, as per MF1S703x, so we pretend that they don't exist.
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
I've never seen traces with those data structures before, either... */
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
proto_tree_add_item(mifare_tree, hf_mifare_block_address, tvb, 1, 1, ENC_BIG_ENDIAN);
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
/* Because we don't know what the user will write, just let Data have away
|
|
|
|
with the rest of the packet's contents for now. */
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
next_tvb = tvb_new_subset_remaining(tvb, 2);
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
call_dissector(data_handle, next_tvb, pinfo, tree);
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
break;
|
2011-11-06 19:19:07 +00:00
|
|
|
|
|
|
|
case TRANSFER:
|
2012-01-13 21:44:13 +00:00
|
|
|
proto_tree_add_item(mifare_tree, hf_mifare_block_address, tvb, 1, 1, ENC_BIG_ENDIAN);
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
col_set_str(pinfo->cinfo, COL_INFO, "Transfer");
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
break;
|
2011-11-06 19:19:07 +00:00
|
|
|
|
|
|
|
case DECREMENT:
|
2012-01-13 21:44:13 +00:00
|
|
|
proto_tree_add_item(mifare_tree, hf_mifare_block_address, tvb, 1, 1, ENC_BIG_ENDIAN);
|
|
|
|
proto_tree_add_item(mifare_tree, hf_mifare_operand, tvb, 2, 4, ENC_BIG_ENDIAN);
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
col_set_str(pinfo->cinfo, COL_INFO, "Decrement");
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
break;
|
2011-11-06 19:19:07 +00:00
|
|
|
|
|
|
|
case INCREMENT:
|
2012-01-13 21:44:13 +00:00
|
|
|
proto_tree_add_item(mifare_tree, hf_mifare_block_address, tvb, 1, 1, ENC_BIG_ENDIAN);
|
|
|
|
proto_tree_add_item(mifare_tree, hf_mifare_operand, tvb, 2, 4, ENC_BIG_ENDIAN);
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
col_set_str(pinfo->cinfo, COL_INFO, "Increment");
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
break;
|
2011-11-06 19:19:07 +00:00
|
|
|
|
|
|
|
case RESTORE:
|
2012-01-13 21:44:13 +00:00
|
|
|
proto_tree_add_item(mifare_tree, hf_mifare_block_address, tvb, 1, 1, ENC_BIG_ENDIAN);
|
|
|
|
proto_tree_add_item(mifare_tree, hf_mifare_operand, tvb, 2, 4, ENC_BIG_ENDIAN);
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
col_set_str(pinfo->cinfo, COL_INFO, "Restore");
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
break;
|
2011-11-06 19:19:07 +00:00
|
|
|
|
|
|
|
default:
|
2012-01-13 21:44:13 +00:00
|
|
|
col_set_str(pinfo->cinfo, COL_INFO, "Unknown");
|
2011-11-06 19:19:07 +00:00
|
|
|
|
2012-01-13 21:44:13 +00:00
|
|
|
break;
|
2011-11-06 19:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
proto_register_mifare(void)
|
|
|
|
{
|
|
|
|
static hf_register_info hf[] = {
|
|
|
|
|
|
|
|
{&hf_mifare_command,
|
|
|
|
{ "Command", "mifare.cmd", FT_UINT8, BASE_HEX,
|
2011-11-06 20:20:14 +00:00
|
|
|
VALS(hf_mifare_commands), 0x0, NULL, HFILL }},
|
2012-01-13 21:44:13 +00:00
|
|
|
{&hf_mifare_block_address,
|
|
|
|
{ "Block Address", "mifare.block.addr", FT_UINT8, BASE_DEC,
|
|
|
|
NULL, 0x0, NULL, HFILL }},
|
|
|
|
{&hf_mifare_key_a,
|
|
|
|
{ "Key A", "mifare.key.a", FT_UINT64, BASE_HEX,
|
|
|
|
NULL, 0x0, NULL, HFILL }},
|
|
|
|
{&hf_mifare_key_b,
|
|
|
|
{ "Key B", "mifare.key.b", FT_UINT64, BASE_HEX,
|
|
|
|
NULL, 0x0, NULL, HFILL }},
|
|
|
|
{&hf_mifare_uid,
|
|
|
|
{ "UID", "mifare.uid", FT_UINT32, BASE_HEX,
|
|
|
|
NULL, 0x0, NULL, HFILL }},
|
|
|
|
{&hf_mifare_operand,
|
|
|
|
{ "Operand", "mifare.operand", FT_INT32, BASE_DEC,
|
|
|
|
NULL, 0x0, NULL, HFILL }}
|
2011-11-06 19:19:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static gint *ett[] = {
|
|
|
|
&ett_mifare
|
|
|
|
};
|
|
|
|
|
|
|
|
proto_mifare = proto_register_protocol("NXP MiFare", "MiFare", "mifare");
|
|
|
|
proto_register_field_array(proto_mifare, hf, array_length(hf));
|
|
|
|
proto_register_subtree_array(ett, array_length(ett));
|
|
|
|
|
|
|
|
mifare_dissector_table = register_dissector_table("mifare.payload",
|
2012-01-13 21:44:13 +00:00
|
|
|
"MiFare Payload", FT_UINT8, BASE_DEC);
|
2011-11-06 19:19:07 +00:00
|
|
|
|
|
|
|
register_dissector("mifare", dissect_mifare, proto_mifare);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handler registration */
|
|
|
|
void
|
|
|
|
proto_reg_handoff_mifare(void)
|
|
|
|
{
|
2011-11-14 20:04:04 +00:00
|
|
|
data_handle = find_dissector("data");
|
2011-11-06 19:19:07 +00:00
|
|
|
}
|
|
|
|
/*
|
2012-01-13 21:44:13 +00:00
|
|
|
* Editor modelines - http://www.wireshark.org/tools/modelines.html
|
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* ex: set shiftwidth=4 tabstop=8 expandtab:
|
|
|
|
* :indentSize=4:tabSize=8:noTabs=true:
|
|
|
|
*/
|