Make style

This commit is contained in:
Bjoern Kerler 2020-04-10 01:18:48 +02:00
commit 82323b14e2
7 changed files with 1859 additions and 295 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...
## [unreleased][unreleased]
- Updated 'legic.lua' and 'legic_clone.lua' script - works with current command set (@Pizza_4u)
- Rewrote `hf mfdes` functions and added apdu debugging (@bkerler)
- Add Mifare Desfire GetDFNames and improve HF MFDES Enum output (@bkerler)
- Fix Mifare Desfire select appid handling (@bkerler)

View file

@ -450,8 +450,7 @@ void MifareDES_Auth1(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)
}
}
break;
case 2:
{
case 2: {
//SendDesfireCommand(AUTHENTICATE_ISO, &arg2, resp);
uint8_t keybytes[16];
uint8_t RndA[8] = {0x00};

View file

@ -64,6 +64,7 @@ static int CmdHelp(const char *Cmd);
*/
int DESFIRESendApdu(bool activate_field, bool leavefield_on, sAPDU apdu, uint8_t *result, int max_result_len, int *result_len, uint16_t *sw) {
*result_len = 0;
if (sw) *sw = 0;
@ -120,8 +121,7 @@ int DESFIRESendApdu(bool activate_field, bool leavefield_on, sAPDU apdu, uint8_t
return PM3_SUCCESS;
}
static char* getstatus(uint16_t * sw)
{
static char *getstatus(uint16_t *sw) {
if (sw == NULL) return "--> sw argument error. This should never happen !";
if (((*sw >> 8) & 0xFF) == 0x91) {
switch (*sw & 0xFF) {
@ -188,8 +188,7 @@ static char* getstatus(uint16_t * sw)
return "Unknown error";
}
static char* GetErrorString(int res,uint16_t* sw)
{
static char *GetErrorString(int res, uint16_t *sw) {
switch (res) {
case PM3_EAPDU_FAIL:
return getstatus(sw);
@ -243,8 +242,7 @@ static char* GetErrorString(int res,uint16_t* sw)
static int send_desfire_cmd(sAPDU *apdu, bool select, uint8_t *dest, int *recv_len, uint16_t *sw, int splitbysize, bool readalldata) {
if (g_debugMode>1)
{
if (g_debugMode > 1) {
if (apdu == NULL) PrintAndLogEx(ERR, "APDU=NULL");
if (dest == NULL) PrintAndLogEx(ERR, "DEST=NULL");
if (sw == NULL) PrintAndLogEx(ERR, "SW=NULL");
@ -267,8 +265,7 @@ static int send_desfire_cmd(sAPDU *apdu, bool select, uint8_t *dest, int *recv_l
}
pos += resplen;
if (!readalldata)
{
if (!readalldata) {
if (*sw == status(MFDES_ADDITIONAL_FRAME)) {
apdu->INS = MFDES_ABORT_TRANSACTION;
apdu->Lc = 0;
@ -327,8 +324,8 @@ static desfire_cardtype_t getCardType(uint8_t major, uint8_t minor) {
//none, verified
static int test_desfire_authenticate() {
uint8_t c = 0x00;
sAPDU apdu = {0x90, MFDES_AUTHENTICATE, 0x00, 0x00, 0x01, &c}; // 0x0A, KEY 0
uint8_t data[] = {0x00};
sAPDU apdu = {0x90, MFDES_AUTHENTICATE, 0x00, 0x00, 0x01, data}; // 0x0A, KEY 0
int recv_len = 0;
uint16_t sw = 0;
return send_desfire_cmd(&apdu, false, NULL, &recv_len, &sw, 0, false);
@ -336,8 +333,8 @@ static int test_desfire_authenticate() {
// none, verified
static int test_desfire_authenticate_iso() {
uint8_t c = 0x00;
sAPDU apdu = {0x90, MFDES_AUTHENTICATE_ISO, 0x00, 0x00, 0x01, &c}; // 0x1A, KEY 0
uint8_t data[] = {0x00};
sAPDU apdu = {0x90, MFDES_AUTHENTICATE_ISO, 0x00, 0x00, 0x01, data}; // 0x1A, KEY 0
int recv_len = 0;
uint16_t sw = 0;
return send_desfire_cmd(&apdu, false, NULL, &recv_len, &sw, 0, false);
@ -345,8 +342,8 @@ static int test_desfire_authenticate_iso() {
//none, verified
static int test_desfire_authenticate_aes() {
uint8_t c = 0x00;
sAPDU apdu = {0x90, MFDES_AUTHENTICATE_AES, 0x00, 0x00, 0x01, &c}; // 0xAA, KEY 0
uint8_t data[] = {0x00};
sAPDU apdu = {0x90, MFDES_AUTHENTICATE_AES, 0x00, 0x00, 0x01, data}; // 0xAA, KEY 0
int recv_len = 0;
uint16_t sw = 0;
return send_desfire_cmd(&apdu, false, NULL, &recv_len, &sw, 0, false);
@ -378,8 +375,7 @@ static int get_desfire_freemem(uint32_t *free_mem) {
// --- GET SIGNATURE, verified
static int desfire_print_signature(uint8_t *uid, uint8_t *signature, size_t signature_len, desfire_cardtype_t card_type) {
if (g_debugMode>1)
{
if (g_debugMode > 1) {
if (uid == NULL) PrintAndLogEx(ERR, "UID=NULL");
if (signature == NULL) PrintAndLogEx(ERR, "SIGNATURE=NULL");
}
@ -435,8 +431,7 @@ static int desfire_print_signature(uint8_t *uid, uint8_t *signature, size_t sign
// init / disconnect, verified
static int get_desfire_signature(uint8_t *signature, size_t *signature_len) {
if (g_debugMode>1)
{
if (g_debugMode > 1) {
if (signature == NULL) PrintAndLogEx(ERR, "SIGNATURE=NULL");
if (signature_len == NULL) PrintAndLogEx(ERR, "SIGNATURE_LEN=NULL");
}
@ -497,8 +492,7 @@ static int desfire_print_keysetting(uint8_t key_settings, uint8_t num_keys) {
// none, verified
static int get_desfire_keysettings(uint8_t *key_settings, uint8_t *num_keys) {
if (g_debugMode>1)
{
if (g_debugMode > 1) {
if (key_settings == NULL) PrintAndLogEx(ERR, "KEY_SETTINGS=NULL");
if (num_keys == NULL) PrintAndLogEx(ERR, "NUM_KEYS=NULL");
}
@ -523,8 +517,7 @@ static int desfire_print_keyversion(uint8_t key_idx, uint8_t key_version) {
// none, verified
static int get_desfire_keyversion(uint8_t curr_key, uint8_t *num_versions) {
if (g_debugMode>1)
{
if (g_debugMode > 1) {
if (num_versions == NULL) PrintAndLogEx(ERR, "NUM_VERSIONS=NULL");
}
if (num_versions == NULL) return PM3_EINVARG;
@ -538,8 +531,7 @@ static int get_desfire_keyversion(uint8_t curr_key, uint8_t *num_versions) {
// init / disconnect, verified
static int get_desfire_appids(uint8_t *dest, uint8_t *app_ids_len) {
if (g_debugMode>1)
{
if (g_debugMode > 1) {
if (dest == NULL) PrintAndLogEx(ERR, "DEST=NULL");
if (app_ids_len == NULL) PrintAndLogEx(ERR, "APP_IDS_LEN=NULL");
}
@ -555,8 +547,7 @@ static int get_desfire_appids(uint8_t *dest, uint8_t *app_ids_len) {
// init, verified
static int get_desfire_dfnames(dfname_t *dest, uint8_t *dfname_count) {
if (g_debugMode>1)
{
if (g_debugMode > 1) {
if (dest == NULL) PrintAndLogEx(ERR, "DEST=NULL");
if (dfname_count == NULL) PrintAndLogEx(ERR, "DFNAME_COUNT=NULL");
}
@ -573,8 +564,7 @@ static int get_desfire_dfnames(dfname_t *dest, uint8_t *dfname_count) {
// init, verified
static int get_desfire_select_application(uint8_t *aid) {
if (g_debugMode>1)
{
if (g_debugMode > 1) {
if (aid == NULL) PrintAndLogEx(ERR, "AID=NULL");
}
if (aid == NULL) return PM3_EINVARG;
@ -592,8 +582,7 @@ static int get_desfire_select_application(uint8_t *aid) {
// none, verified
static int get_desfire_fileids(uint8_t *dest, uint8_t *file_ids_len) {
if (g_debugMode>1)
{
if (g_debugMode > 1) {
if (dest == NULL) PrintAndLogEx(ERR, "DEST=NULL");
if (file_ids_len == NULL) PrintAndLogEx(ERR, "FILE_IDS_LEN=NULL");
}
@ -614,8 +603,7 @@ static int get_desfire_fileids(uint8_t *dest, uint8_t *file_ids_len) {
// none, verified
static int get_desfire_filesettings(uint8_t file_id, uint8_t *dest, int *destlen) {
if (g_debugMode>1)
{
if (g_debugMode > 1) {
if (dest == NULL) PrintAndLogEx(ERR, "DEST=NULL");
if (destlen == NULL) PrintAndLogEx(ERR, "DESTLEN=NULL");
}
@ -1174,8 +1162,7 @@ int getKeySettings(uint8_t *aid) {
}
static void DecodeFileType(uint8_t filetype) {
switch (filetype)
{
switch (filetype) {
case 0x00:
PrintAndLogEx(INFO, " File Type: 0x%02X -> Standard Data File", filetype);
break;
@ -1198,8 +1185,7 @@ static void DecodeFileType(uint8_t filetype){
}
static void DecodeComSet(uint8_t comset) {
switch (comset)
{
switch (comset) {
case 0x00:
PrintAndLogEx(INFO, " Com.Setting: 0x%02X -> Plain", comset);
break;
@ -1215,8 +1201,7 @@ static void DecodeComSet(uint8_t comset){
}
}
static char* DecodeAccessValue(uint8_t value)
{
static char *DecodeAccessValue(uint8_t value) {
char *car = (char *)malloc(255);
memset(car, 0x0, 255);
switch (value) {
@ -1254,8 +1239,7 @@ static int DecodeFileSettings(uint8_t* filesettings, int fileset_len, int maclen
uint8_t comset = filesettings[1];
uint16_t accrights = (filesettings[4] << 8) + filesettings[3];
if (fileset_len==1+1+2+3+maclen)
{
if (fileset_len == 1 + 1 + 2 + 3 + maclen) {
int filesize = (filesettings[7] << 16) + (filesettings[6] << 8) + filesettings[5];
DecodeFileType(filetype);
DecodeComSet(comset);
@ -1287,7 +1271,7 @@ static int CmdHF14ADesEnumApplications(const char *Cmd) {
uint8_t file_ids[33] = {0};
uint8_t file_ids_len = 0;
dfname_t dfnames[255] = {0};
dfname_t dfnames[255];
uint8_t dfname_count = 0;
int res = 0;

View file

@ -91,14 +91,20 @@ CRC1 = crc8 over addr 0x00..0x03+0x07..0x0E (special 'gantner crc8')
CRC2 = MCD + MSB0..2+ addr 0x06 + addr 0x05 + addr 0x07 + Stamp (regular Master-Token-CRC)
--]]
--[[
Known issues; needs to be fixed:
* last byte in last segment is handled incorrectly when it is the last bytes on the card itself (MIM256: => byte 256)
--]]
example = "script run legic"
author = "Mosci"
version = "1.0.3"
author = "Mosci, uhei"
version = "1.0.4"
desc =
[[
This script helps you to read, create and modify Legic Prime Tags (MIM22, MIM256, MIM1024)
The virtual tag (and therefore the file to be saved) is always a MIM1024 tag.
it's kinda interactive with following commands in three categories:
Data I/O Segment Manipulation Token-Data
@ -108,8 +114,8 @@ it's kinda interactive with following commands in three categories:
ed => edit Segment Data tk => toggle KGH-Flag
File I/O rs => remove Segment
----------------- cc => check Segment-CRC
lf => load File ck => check KGH
sf => save File ds => dump Segments
lf => load bin File ck => check KGH
sf => save eml/bin File ds => dump Segments
xf => xor to File
@ -128,8 +134,8 @@ it's kinda interactive with following commands in three categories:
without the need of changing anything - MCD,MSN,MCC will be read from the tag
before and applied to the output.
lf: 'load file' - load a (xored) file from the local Filesystem into the 'virtual inTag'
sf: 'save file' - saves the 'virtual inTag' to the local Filesystem (xored with Tag-MCC)
lf: 'load file' - load a (xored) binary file (*.bin) from the local Filesystem into the 'virtual inTag'
sf: 'save file' - saves the 'virtual inTag' to the local Filesystem as eml and bin (xored with Tag-MCC)
xf: 'xor file' - saves the 'virtual inTag' to the local Filesystem (xored with choosen MCC - use '00' for plain values)
ct: 'copy tag' - copy the 'virtual Tag' to a second 'virtual TAG' - not usefull yet, but inernally needed
@ -242,6 +248,16 @@ function istable(t)
return type(t) == 'table'
end
---
-- To have two char string for a byte
local function padString(str)
if (#str == 1) then
return '0'..str
end
return str
end
---
-- creates a 'deep copy' of a table (a=b only references)
function deepCopy(object)
@ -387,15 +403,15 @@ end
function bytesToTag(bytes, tag)
if istable(tag) == false then return oops("tag is no table in: bytesToTag ("..type(tag)..")") end
tag.MCD =bytes[1];
tag.MSN0=bytes[2];
tag.MSN1=bytes[3];
tag.MSN2=bytes[4];
tag.MCC =bytes[5];
tag.DCFl=bytes[6];
tag.DCFh=bytes[7];
tag.raw =bytes[8];
tag.SSC =bytes[9];
tag.MCD =padString(bytes[1]);
tag.MSN0=padString(bytes[2]);
tag.MSN1=padString(bytes[3]);
tag.MSN2=padString(bytes[4]);
tag.MCC =padString(bytes[5]);
tag.DCFl=padString(bytes[6]);
tag.DCFh=padString(bytes[7]);
tag.raw =padString(bytes[8]);
tag.SSC =padString(bytes[9]);
tag.Type=getTokenType(tag.DCFl);
tag.OLE=bbit("0x"..tag.DCFl,7,1)
tag.WRP=("%d"):format(bbit("0x"..bytes[8],0,4))
@ -500,42 +516,26 @@ function tagToBytes(tag)
return bytes
end
---
--- PM3 I/O ---
---
-- read from pm3 into virtual-tag
function readFromPM3()
local tag, bytes, infile
infile="legic.temp"
-- core.console("hf legic reader")
-- core.console("hf legic esave "..infile)
core.console("hf legic dump o "..infile)
tag=readFile(infile..".bin")
return tag
end
local function padString(str)
if (#str == 1) then
return '0'..str
end
return str
end
---
-- write virtual Tag to real Tag
function writeToTag(tag)
local bytes
local filename = 'MylegicClone.hex'
local taglen = 22
if(utils.confirm(acred.."\nplace the (empty) Tag onto the PM3\nand confirm writing to this Tag: "..acoff) == false) then
local writeDCF = false
if(utils.confirm(acred.."\nPlace the (empty) Tag onto the PM3\nand confirm writing to this Tag: "..acoff) == false) then
return
end
if(utils.confirm(acred.."\nShould the decremental field (DCF) be written?: "..acoff) == true) then
writeDCF = true
end
-- get used bytes / tag-len
if (istable(tag.SEG)) then
if (istable(tag.Bck)) then
for i=0, #tag.SEG do
taglen = taglen + tag.SEG[i] . len + 5
taglen = taglen + tag.SEG[i] . len
end
end
local uid_old = tag.MCD..tag.MSN0..tag.MSN1..tag.MSN2
@ -571,37 +571,32 @@ function writeToTag(tag)
bytes[22] = calcMtCrc(bytes)
end
if (bytes) then
print("write temp-file '"..filename.."'")
print(accyan)
writeFile(bytes, filename..".bin")
print(acoff)
bytes = xorBytes(bytes,tag.MCC)
end
end
-- write data to file
if (taglen > 0) then
WriteBytes = input(acyellow.."enter number of bytes to write?"..acoff, taglen)
-- load file into pm3-buffer
if (type(filename) ~= "string") then
filename = input(acyellow.."filename to load to pm3-buffer?"..acoff, "legic.temp")
end
cmd = 'hf legic eload 2 '..filename
core.console(cmd)
-- write pm3-buffer to Tag
for i=0, WriteBytes do
if (i > 6) then
cmd = ("hf legic write o %x d %s "):format(i, padString(bytes[i]))
for i=1, WriteBytes do
if (i > 7) then
cmd = ("hf legic wrbl o %02x d %s "):format(i-1, padString(bytes[i]))
print(acgreen..cmd..acoff)
core.console(cmd)
core.clearCommandBuffer()
elseif (i == 6) then
elseif (i == 7) then
if (writeDCF) then
-- write DCF in reverse order (requires 'mosci-patch')
cmd = ('hf legic write o 05 d %s%s'):format(padString(bytes[i-1]), padString(bytes[i]))
cmd = ('hf legic wrbl o 05 d %s%s'):format(padString(bytes[i-1]), padString(bytes[i]))
print(acgreen..cmd..acoff)
core.console(cmd)
core.clearCommandBuffer()
elseif (i == 5) then
else
print(acgreen.."skip byte 0x05-0x06 - DCF"..acoff)
end
elseif (i == 6) then
print(acgreen.."skip byte 0x05 - will be written next step"..acoff)
else
print(acgreen.."skip byte 0x00-0x04 - unwritable area"..acoff)
@ -641,12 +636,12 @@ end
local function save_BIN(data, filename)
local outfile
local counter = 1
local ext = filename:match("^.+(%..+)$") or ''
local fn = filename
local ext = ".bin"
local fn = filename..ext
-- Make sure we don't overwrite a file
while file_check(fn) do
fn = filename:gsub(ext, tostring(counter)..ext)
fn = filename..ext:gsub(ext, "-"..tostring(counter)..ext)
counter = counter + 1
end
@ -664,26 +659,27 @@ end
---
-- write bytes to file
function writeFile(bytes, filename)
if (filename ~= 'MylegicClone.hex') then
if (file_check(filename)) then
local answer = confirm("\nthe output-file "..filename.." already exists!\nthis will delete the previous content!\ncontinue?")
local emlext = ".eml"
if (filename ~= 'MyLegicClone') then
if (file_check(filename..emlext)) then
local answer = confirm("\nthe output-file "..filename..emlext.." already exists!\nthis will delete the previous content!\ncontinue?")
if not answer then return print("user abort") end
end
end
local line
local bcnt = 0
local fho, err = io.open(filename, "w")
local fho, err = io.open(filename..emlext, "w")
if err then
return oops("OOps ... failed to open output-file ".. filename)
return oops("OOps ... failed to open output-file ".. filename..emlext)
end
bytes = xorBytes(bytes, bytes[5])
for i = 1, #bytes do
if (bcnt == 0) then
line = bytes[i]
line = padString(bytes[i])
elseif (bcnt <= 7) then
line = line.." "..bytes[i]
line = line.." "..padString(bytes[i])
end
if (bcnt == 7) then
-- write line to new file
@ -699,7 +695,7 @@ function writeFile(bytes, filename)
-- save binary
local fn_bin, fn_bin_num = save_BIN(bytes, filename)
print("\nwrote "..acyellow..(#bytes * 3)..acoff.." bytes to " ..acyellow..filename..acoff)
print("\nwrote "..acyellow..(#bytes * 3)..acoff.." bytes to " ..acyellow..filename..emlext..acoff)
if fn_bin and fn_bin_num then
print("\nwrote "..acyellow..fn_bin_num..acoff.." bytes to BINARY file "..acyellow..fn_bin..acoff)
@ -708,6 +704,21 @@ function writeFile(bytes, filename)
return true
end
---
-- read from pm3 into virtual-tag
function readFromPM3()
local tag, bytes, infile
--infile="legic.temp"
infile=os.tmpname()
core.console("hf legic dump f "..infile)
tag=readFile(infile..".bin")
os.remove(infile)
os.remove(infile..".bin")
os.remove(infile..".eml")
os.remove(infile..".json")
return tag
end
--- Map related ---
---
-- make tagMap
@ -2265,8 +2276,8 @@ function modifyHelp()
ed => edit Segment Data tk => toggle KGH-Flag
File I/O rs => remove Segment
----------------- cc => check Segment-CRC
lf => load File ck => check KGH
sf => save File ds => dump Segments
lf => load bin File ck => check KGH
sf => save eml/bin File ds => dump Segments
xf => xor to File
@ -2352,10 +2363,10 @@ function modifyMode()
-- save values of mainTAG to a file (xored with MCC of mainTAG)
["sf"] = function(x)
if istable(inTAG) then
outfile = input("enter filename:", "legic.temp")
outfile = input("enter filename:", "hf-legic-"..inTAG.MCD..inTAG.MSN0..inTAG.MSN1..inTAG.MSN2)
bytes = tagToBytes(inTAG)
--bytes=xorBytes(bytes, inTAG.MCC)
if bytes then
if (bytes) then
writeFile(bytes, outfile)
end
end
@ -2364,7 +2375,7 @@ function modifyMode()
-- save values of mainTAG to a file (xored with 'specific' MCC)
["xf"] = function(x)
if istable(inTAG) then
outfile = input("enter filename:", "legic.temp")
outfile = input("enter filename:", "hf-legic-"..inTAG.MCD..inTAG.MSN0..inTAG.MSN1..inTAG.MSN2)
crc = input("enter new crc: ('00' for a plain dump)", inTAG.MCC)
print("obfuscate with: "..crc)
bytes=tagToBytes(inTAG)

1569
cmdhfmfdes.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -18,7 +18,7 @@ Always use the latest repository commits from *master* branch. There are always
* [File not found](#file-not-found)
* [Pixmap / pixbuf warnings](#pixmap--pixbuf-warnings)
* [Usb cable](#usb-cable)
* [WSL 2 explorer.exe . doesnt work](WSL-2)
* [WSL 2 explorer.exe . doesnt work](#WSL-2)
## `pm3` or `pm3-flash*` doesn't see my Proxmark