X-Git-Url: http://cvs.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/4c36581b96dfb81c85f34a4229d82e51edeaf499..b62cbadb6198fcfcf7b6787acac01b5b7262eea6:/client/cmdlfhid.c diff --git a/client/cmdlfhid.c b/client/cmdlfhid.c index ff25d07e..fd0f6eba 100644 --- a/client/cmdlfhid.c +++ b/client/cmdlfhid.c @@ -8,64 +8,62 @@ // Low frequency HID commands //----------------------------------------------------------------------------- -#include -#include -#include "proxmark3.h" -#include "ui.h" -#include "graph.h" -#include "cmdparser.h" #include "cmdlfhid.h" -#include "util.h" -#include "cmdmain.h" -#include "sleep.h" static int CmdHelp(const char *Cmd); int usage_lf_hid_wiegand(void){ - PrintAndLog("Usage: lf hid wiegand [h] [oem] [FacilityCode] [cardnumber]"); - PrintAndLog("This command converts FC/Cardnum to wiegand code"); + PrintAndLog("This command converts facility code/card number to Wiegand code"); + PrintAndLog("Usage: lf hid wiegand [h] [OEM] [FC] [CN]"); + PrintAndLog("Options:"); - PrintAndLog(" h - This help"); - PrintAndLog(" oem - Oem number"); - PrintAndLog(" facilitynum - Facility number"); - PrintAndLog(" cardnum - Card number"); + PrintAndLog(" h - This help"); + PrintAndLog(" OEM - OEM number / site code"); + PrintAndLog(" FC - facility code"); + PrintAndLog(" CN - card number"); PrintAndLog("Examples:"); PrintAndLog(" lf hid wiegand 0 101 2001"); return 0; } +int usage_lf_hid_sim(void){ + PrintAndLog("HID Tag simulator"); + PrintAndLog(""); + PrintAndLog("Usage: lf hid sim [h] [ID]"); + PrintAndLog("Options:"); + PrintAndLog(" h - This help"); + PrintAndLog(" ID - HID id"); + PrintAndLog("Examples:"); + PrintAndLog(" lf hid sim 224"); + return 0; +} +int usage_lf_hid_clone(void){ + PrintAndLog("Clone HID to T55x7. Tag must be on antenna. "); + PrintAndLog(""); + PrintAndLog("Usage: lf hid clone [h] [ID] "); + PrintAndLog("Options:"); + PrintAndLog(" h - This help"); + PrintAndLog(" ID - HID id"); + PrintAndLog(" L - 84bit ID"); + PrintAndLog("Examples:"); + PrintAndLog(" lf hid clone 224"); + PrintAndLog(" lf hid clone 224 L"); + return 0; +} int usage_lf_hid_brute(void){ - PrintAndLog("Enables bruteforce of HID readers with specified facility-code."); + PrintAndLog("Enables bruteforce of HID readers with specified facility code."); PrintAndLog("Different formatlength is supported"); PrintAndLog("This is a incremental attack against reader."); PrintAndLog(""); - PrintAndLog("Usage: lf hid brute "); + PrintAndLog("Usage: lf hid brute [h] "); PrintAndLog("Options :"); - PrintAndLog(" : 26|33|34|35|37|40|44|84 "); - PrintAndLog(" : 8-bit value HID facility code"); + PrintAndLog(" h - This help"); + PrintAndLog(" - 26|33|34|35|37|40|44|84"); + PrintAndLog(" - 8-bit value HID facility code"); PrintAndLog(""); PrintAndLog("Sample : lf hid brute 26 224"); return 0; } -/* -int CmdHIDDemod(const char *Cmd) -{ - if (GraphTraceLen < 4800) { - PrintAndLog("too short; need at least 4800 samples"); - return 0; - } - - GraphTraceLen = 4800; - for (int i = 0; i < GraphTraceLen; ++i) { - if (GraphBuffer[i] < 0) { - GraphBuffer[i] = 0; - } else { - GraphBuffer[i] = 1; - } - } - RepaintGraphWindow(); - return 0; -} -*/ + int CmdHIDDemodFSK(const char *Cmd) { int findone = ( Cmd[0] == '1' ) ? 1 : 0; UsbCommand c = {CMD_HID_DEMOD_FSK, {findone, 0 , 0}}; @@ -78,6 +76,9 @@ int CmdHIDSim(const char *Cmd) { unsigned int hi = 0, lo = 0; int n = 0, i = 0; + uint8_t ctmp = param_getchar(Cmd, 0); + if ( strlen(Cmd) == 0 || ctmp == 'H' || ctmp == 'h' ) return usage_lf_hid_sim(); + while (sscanf(&Cmd[i++], "%1x", &n ) == 1) { hi = (hi << 4) | (lo >> 28); lo = (lo << 4) | (n & 0xf); @@ -93,10 +94,14 @@ int CmdHIDSim(const char *Cmd) { } int CmdHIDClone(const char *Cmd) { + unsigned int hi2 = 0, hi = 0, lo = 0; int n = 0, i = 0; UsbCommand c; + uint8_t ctmp = param_getchar(Cmd, 0); + if ( strlen(Cmd) == 0 || ctmp == 'H' || ctmp == 'h' ) return usage_lf_hid_clone(); + if (strchr(Cmd,'l') != 0) { while (sscanf(&Cmd[i++], "%1x", &n ) == 1) { hi2 = (hi2 << 4) | (hi >> 28); @@ -128,171 +133,199 @@ int CmdHIDClone(const char *Cmd) { SendCommand(&c); return 0; } - -static void getParity26(uint32_t *hi, uint32_t *lo){ - uint32_t result = 0; - int i; - // even parity - for (i = 24;i >= 13;i--) - result ^= (*lo >> i) & 1; - // even parity 26th bit - *lo |= result << 25; - - // odd parity - result = 0; - for (i = 12;i >= 1;i--) - result ^= (*lo >> i) & 1; - *lo |= !result; -} -static void getParity33(uint32_t *hi, uint32_t *lo){ - -} -static void getParity34(uint32_t *hi, uint32_t *lo){ - uint32_t result = 0; - int i; - - // even parity - for (i = 7;i >= 0;i--) - result ^= (*hi >> i) & i; - for (i = 31;i >= 24;i--) - result ^= (*lo >> i) & 1; - - *hi |= result << 2; - - // odd parity bit - result = 0; - for (i = 23;i >= 1;i--) - result ^= (*lo >> i) & 1; - - *lo |= !result; -} -static void getParity35(uint32_t *hi, uint32_t *lo){ -} -static void getParity37S(uint32_t *hi,uint32_t *lo){ - uint32_t result = 0; - int i; - - // even parity - for (i = 4; i >= 0; i--) - result ^= (*hi >> i) & 1; +// struct to handle wiegand +typedef struct { + uint8_t FormatLen; + uint8_t SiteCode; + uint8_t FacilityCode; + uint8_t CardNumber; + uint8_t* Wiegand; + size_t Wiegand_n; + } wiegand_t; + +// static void addHIDMarker(uint8_t fmtlen, uint8_t *out) { - for (i = 31; i >= 20; i--) - result ^= (*lo >> i) & 1; - - *hi |= result; - - // odd parity - result = 0; - for (i = 19; i >= 1; i--) - result ^= (*lo >> i) & 1; - - *lo |= result; -} -static void getParity37H(uint32_t *hi, uint32_t *lo){ - uint32_t result = 0; - int i; - - // even parity - for (i = 4;i >= 0;i--) - result ^= (*hi >> i) & 1; - for (i = 31;i >= 20;i--) - result ^= (*lo >> i) & 1; - *hi |= result << 4; - - // odd parity - result = 0; - for (i = 19;i >= 1;i--) - result ^= (*lo >> i) & 1; - *lo |= result; -} - -static void calc26(uint16_t fc, uint32_t cardno, uint32_t *hi, uint32_t *lo){ - *lo = ((cardno & 0xFFFF) << 1) | ((fc & 0xFF) << 17) | (1 << 26); - *hi = (1 << 5); -} -static void calc33(uint16_t fc, uint32_t cardno, uint32_t *hi, uint32_t *lo){ - -} -static void calc34(uint16_t fc, uint32_t cardno, uint32_t *hi, uint32_t *lo){ - // put card number first bit 1 .. 20 // - *lo = ((cardno & 0X000F7FFF) << 1) | ((fc & 0XFFFF) << 17); - // set bit format for less than 37 bit format - *hi = (1 << 5) | (fc >> 15); -} -static void calc35(uint16_t fc, uint32_t cardno, uint32_t *hi, uint32_t *lo){ - *lo = ((cardno & 0xFFFFF) << 1) | fc << 21; - *hi = (1 << 5) | ((fc >> 11) & 1); -} -static void calc37S(uint16_t fc, uint32_t cardno, uint32_t *hi, uint32_t *lo){ - // FC 2 - 17 - 16 bit - // cardno 18 - 36 - 19 bit - // Even P1 1 - 19 - // Odd P37 19 - 36 - - fc = fc & 0xFFFF; - *lo = ((fc << 20) | (cardno & 0x7FFFF) << 1); - *hi = (fc >> 12); -} -static void calc37H(uint64_t cardno, uint32_t *hi, uint32_t *lo){ - // SC NONE - // cardno 1-35 34 bits - // Even Parity 0th bit 1-18 - // Odd Parity 36th bit 19-35 - cardno = (cardno & 0x00000003FFFFFFFF); - *lo = (cardno << 1); - *hi = (cardno >> 31); -} -static void calc40(uint64_t cardno, uint32_t *hi, uint32_t *lo){ - cardno = (cardno & 0xFFFFFFFFFF); - *lo = ((cardno & 0xFFFFFFFF) << 1 ); - *hi = (cardno >> 31); +// } +//static void getParity26(uint32_t *hi, uint32_t *lo){ + // uint32_t result = 0; + // int i; + // // even parity + // for (i = 24;i >= 13;i--) + // result ^= (*lo >> i) & 1; + // // even parity 26th bit + // *lo |= result << 25; + + // // odd parity + // result = 0; + // for (i = 12;i >= 1;i--) + // result ^= (*lo >> i) & 1; + // *lo |= !result; +//} + +// static void getParity33(uint32_t *hi, uint32_t *lo){ + +// } +// static void getParity34(uint32_t *hi, uint32_t *lo){ + // uint32_t result = 0; + // int i; + + // // even parity + // for (i = 7;i >= 0;i--) + // result ^= (*hi >> i) & i; + // for (i = 31;i >= 24;i--) + // result ^= (*lo >> i) & 1; + + // *hi |= result << 2; + + // // odd parity bit + // result = 0; + // for (i = 23;i >= 1;i--) + // result ^= (*lo >> i) & 1; + + // *lo |= !result; +// } +// static void getParity35(uint32_t *hi, uint32_t *lo){ +// } +// static void getParity37S(uint32_t *hi,uint32_t *lo){ + // uint32_t result = 0; + // int i; + + // // even parity + // for (i = 4; i >= 0; i--) + // result ^= (*hi >> i) & 1; + + // for (i = 31; i >= 20; i--) + // result ^= (*lo >> i) & 1; + + // *hi |= result; + + // // odd parity + // result = 0; + // for (i = 19; i >= 1; i--) + // result ^= (*lo >> i) & 1; + + // *lo |= result; +// } +// static void getParity37H(uint32_t *hi, uint32_t *lo){ + // uint32_t result = 0; + // int i; + + // // even parity + // for (i = 4;i >= 0;i--) + // result ^= (*hi >> i) & 1; + // for (i = 31;i >= 20;i--) + // result ^= (*lo >> i) & 1; + // *hi |= result << 4; + + // // odd parity + // result = 0; + // for (i = 19;i >= 1;i--) + // result ^= (*lo >> i) & 1; + // *lo |= result; +// } + +//static void calc26(uint16_t fc, uint32_t cardno, uint32_t *hi, uint32_t *lo){ +void calc26(uint16_t fc, uint32_t cardno, uint8_t *out){ + + uint8_t wiegand[24]; + num_to_bytebits(fc, 8, wiegand); + num_to_bytebits(cardno, 16, wiegand+8); + wiegand_add_parity(out, wiegand, sizeof(wiegand) ); + +// *out |= (1 << 26); // why this? +// *out |= (1 << 37); // bit format for hid? } +// static void calc33(uint16_t fc, uint32_t cardno, uint32_t *hi, uint32_t *lo){ + +// } +// static void calc34(uint16_t fc, uint32_t cardno, uint32_t *hi, uint32_t *lo){ + // // put card number first bit 1 .. 20 // + // *lo = ((cardno & 0X000F7FFF) << 1) | ((fc & 0XFFFF) << 17); + // // set bit format for less than 37 bit format + // *hi = (1 << 5) | (fc >> 15); +// } +// static void calc35(uint16_t fc, uint32_t cardno, uint32_t *hi, uint32_t *lo){ + // *lo = ((cardno & 0xFFFFF) << 1) | fc << 21; + // *hi = (1 << 5) | ((fc >> 11) & 1); +// } +// static void calc37S(uint16_t fc, uint32_t cardno, uint32_t *hi, uint32_t *lo){ + // // FC 2 - 17 - 16 bit + // // cardno 18 - 36 - 19 bit + // // Even P1 1 - 19 + // // Odd P37 19 - 36 + + // fc = fc & 0xFFFF; + // *lo = ((fc << 20) | (cardno & 0x7FFFF) << 1); + // *hi = (fc >> 12); +// } +// static void calc37H(uint64_t cardno, uint32_t *hi, uint32_t *lo){ + // // SC NONE + // // cardno 1-35 34 bits + // // Even Parity 0th bit 1-18 + // // Odd Parity 36th bit 19-35 + // cardno = (cardno & 0x00000003FFFFFFFF); + // *lo = (cardno << 1); + // *hi = (cardno >> 31); +// } +// static void calc40(uint64_t cardno, uint32_t *hi, uint32_t *lo){ + // cardno = (cardno & 0xFFFFFFFFFF); + // *lo = ((cardno & 0xFFFFFFFF) << 1 ); + // *hi = (cardno >> 31); +// } + +static void calcWiegand(uint8_t fmtlen, uint16_t fc, uint64_t cardno, uint8_t *bits){ + + // uint32_t hi = 0, lo = 0; + // uint32_t cn32 = (cardno & 0xFFFFFFFF); + // switch ( fmtlen ) { + // case 26 : { + // calc26(fc, cn32, bits); + // addHIDFormatMarker(fmtlen, bits); + // break; + // } + // case 33 : { + // // calc33(fc, cn32, hi, lo); + // // getParity33(hi, lo); + // break; + // } + // case 34 : { + // calc34(fc, cn32, hi, lo); + // getParity34(hi, lo); + // break; + // } + // case 35 : { + // calc35(fc, cn32, hi, lo); + // getParity35(hi, lo); + // break; + // } + // case 37 : { + // calc37S(fc, cn32, hi, lo); + // getParity37S(hi, lo); + // break; + // } + // case 38 : { + // calc37H(cn32, hi, lo); + // getParity37H(hi, lo); + // break; + // } + // case 40 : calc40(cardno, hi, lo); break; + // case 44 : { break; } + // case 84 : { break; } + // } -static void calcWiegand(uint8_t fmtlen, uint16_t fc, uint64_t cardno, uint32_t *hi, uint32_t *lo){ - - uint32_t cn = (cardno & 0xFFFFFFFF); - switch ( fmtlen ) { - case 26 : { - calc26(fc, cn, hi, lo); - getParity26(hi, lo); - break; - } - case 33 : { - calc33(fc, cn, hi, lo); - getParity33(hi, lo); - break; - } - case 34 : { - calc34(fc, cn, hi, lo); - getParity34(hi, lo); - break; - } - case 35 : { - calc35(fc, cn, hi, lo); - getParity35(hi, lo); - break; - } - case 37 : { - calc37S(fc, cn, hi, lo); - getParity37S(hi, lo); - break; - } - case 38 : { - calc37H(cn, hi, lo); - getParity37H(hi, lo); - break; - } - case 40 : calc40(cardno, hi, lo); break; - case 44 : { break; } - case 84 : { break; } - } } int CmdHIDWiegand(const char *Cmd) { - uint32_t oem; - uint32_t fc, lo = 0, hi = 0; + uint32_t oem = 0, fc = 0; uint64_t cardnum = 0; + uint32_t blocks[2] = {0,0}; + uint32_t wiegand[2] = {0,0}; + + uint8_t bits[96]; + uint8_t *bs = bits; + memset(bs, 0, sizeof(bits)); + uint8_t ctmp = param_getchar(Cmd, 0); if ( strlen(Cmd) == 0 || strlen(Cmd) < 3 || ctmp == 'H' || ctmp == 'h' ) return usage_lf_hid_wiegand(); @@ -300,11 +333,27 @@ int CmdHIDWiegand(const char *Cmd) { fc = param_get32ex(Cmd, 1, 0, 10); cardnum = param_get64ex(Cmd, 2, 0, 10); + // uint8_t ftmlen[] = {26,33,34,35,37,38,40}; + + PrintAndLog("HID | OEM | FC | CN | Wiegand | HID Formatted"); + PrintAndLog("----+-----+-----+-------+-----------+--------------------"); for (uint8_t i = 0; i < sizeof(ftmlen); i++){ - calcWiegand( ftmlen[i], fc, cardnum, &hi, &lo); - PrintAndLog("HID %d bit | FC: %d CN: %llu | Wiegand Code: %08X%08X", ftmlen[i], fc, cardnum, hi, lo); + calcWiegand( ftmlen[i], fc, cardnum, bs); + blocks[0] = bytebits_to_byte(bs,32); + blocks[1] = bytebits_to_byte(bs+32,32); + PrintAndLog(" %d | %d | %d | %llu | %08X%08X | %08X%08X ", + ftmlen, + oem, + fc, + cardnum, + wiegand[0], + wiegand[1], + blocks[0], + blocks[1] + ); } + PrintAndLog("----+-----+-----+-------+-----------+--------------------"); return 0; } @@ -312,7 +361,10 @@ int CmdHIDBrute(const char *Cmd){ bool error = TRUE; uint8_t fc = 0, fmtlen = 0; - uint32_t hi = 0, lo = 0; + + uint8_t bits[96]; + uint8_t *bs = bits; + memset(bs, 0, sizeof(bits)); UsbCommand c = {CMD_HID_SIM_TAG, {0, 0, 0}}; @@ -332,7 +384,7 @@ int CmdHIDBrute(const char *Cmd){ fc = param_get8(Cmd, 1); if ( fc == 0) return usage_lf_hid_brute(); - PrintAndLog("Bruteforceing HID Reader"); + PrintAndLog("Brute-forcing HID reader"); PrintAndLog("Press pm3-button to abort simulation or run another command"); for ( uint16_t cn = 1; cn < 0xFFFF; ++cn){ @@ -347,10 +399,10 @@ int CmdHIDBrute(const char *Cmd){ return 1; } - calcWiegand( fmtlen, fc, cn, &hi, &lo); + calcWiegand( fmtlen, fc, cn, bs); - c.arg[0] = hi; - c.arg[1] = lo; + c.arg[0] = bytebits_to_byte(bs,32); + c.arg[1] = bytebits_to_byte(bs+32,32); clearCommandBuffer(); SendCommand(&c); @@ -362,12 +414,12 @@ int CmdHIDBrute(const char *Cmd){ } static command_t CommandTable[] = { - {"help", CmdHelp, 1, "This help"}, - //{"demod", CmdHIDDemod, 1, "Demodulate HID Prox Card II (not optimal)"}, - {"fskdemod", CmdHIDDemodFSK, 0, "['1'] Realtime HID FSK demodulator (option '1' for one tag only)"}, - {"sim", CmdHIDSim, 0, " -- HID tag simulator"}, - {"clone", CmdHIDClone, 0, " ['l'] -- Clone HID to T55x7 (tag must be in antenna)(option 'l' for 84bit ID)"}, - {"wiegand", CmdHIDWiegand, 1, " -- convert facilitycode, cardnumber to Wiegand code"}, + {"help", CmdHelp, 1, "This help"}, + {"fskdemod",CmdHIDDemodFSK, 0, "[1] Realtime HID FSK demodulator (option '1' for one tag only)"}, + {"sim", CmdHIDSim, 0, " -- HID tag simulator"}, + {"clone", CmdHIDClone, 0, " [L] -- Clone HID to T55x7"}, + {"wiegand", CmdHIDWiegand, 0, " -- convert facility code/card number to Wiegand code"}, + {"brute", CmdHIDBrute, 0, " -- brute force card number"}, {NULL, NULL, 0, NULL} };