|
18 | 18 | #include "protocols.h" // defines
|
19 | 19 | #include "cliparser.h"
|
20 | 20 |
|
| 21 | +#include "graph.h" // MAX_GRAPH_TRACE_LEN |
| 22 | +#include "lfdemod.h" |
| 23 | +#include "cmddata.h" // setDemodBuff |
| 24 | + |
| 25 | + |
21 | 26 | static int CmdHelp(const char *Cmd);
|
22 | 27 |
|
23 | 28 | static const char *getHitagTypeStr(uint32_t uid) {
|
@@ -883,6 +888,71 @@ static int CmdLFHitag2Dump(const char *Cmd) {
|
883 | 888 | return PM3_SUCCESS;
|
884 | 889 | }
|
885 | 890 |
|
| 891 | +static int CmdLFHitag2PWMDemod(const char *Cmd) { |
| 892 | + |
| 893 | + CLIParserContext *ctx; |
| 894 | + CLIParserInit(&ctx, "lf hitag pwmdemod", |
| 895 | + "Demodulate the data in the GraphBuffer and output binary\n", |
| 896 | + "lf hitag pwmdemod -t 65 --> specify first wave index\n" |
| 897 | + "lf hitag pwmdemod" |
| 898 | + ); |
| 899 | + |
| 900 | + void *argtable[] = { |
| 901 | + arg_param_begin, |
| 902 | + arg_int0("t", "start", "<dec>", "first wave index"), |
| 903 | + arg_param_end |
| 904 | + }; |
| 905 | + |
| 906 | + CLIExecWithReturn(ctx, Cmd, argtable, true); |
| 907 | + uint32_t start_idx = (uint32_t)arg_get_int_def(ctx, 1, 0); |
| 908 | + CLIParserFree(ctx); |
| 909 | + |
| 910 | + uint8_t *bits = calloc(MAX_GRAPH_TRACE_LEN, sizeof(uint8_t)); |
| 911 | + if (bits == NULL) { |
| 912 | + PrintAndLogEx(INFO, "failed to allocate memory"); |
| 913 | + return PM3_EMALLOC; |
| 914 | + } |
| 915 | + |
| 916 | + size_t size = getFromGraphBuf(bits); |
| 917 | + |
| 918 | + PrintAndLogEx(DEBUG, "DEBUG: (Hitag2PWM) #samples from graphbuff: %zu", size); |
| 919 | + |
| 920 | + if (size < 255) { |
| 921 | + free(bits); |
| 922 | + return PM3_ESOFT; |
| 923 | + } |
| 924 | + |
| 925 | + // TODO autodetect |
| 926 | + uint8_t fchigh = 29; |
| 927 | + uint8_t fclow = 20; |
| 928 | + |
| 929 | + size = HitagPWMDemod(bits, size, &fchigh, &fclow, &start_idx, g_DemodBitRangeBuffer); |
| 930 | + PrintAndLogEx(DEBUG, "DEBUG: start_idx=%d, size=%d", start_idx, size); |
| 931 | + if (size > 0) { |
| 932 | + setDemodBuff2(bits, size, 0, g_DemodBitRangeBuffer); |
| 933 | + setClockGrid(32, start_idx); |
| 934 | + uint32_t total = 0; |
| 935 | + for (int i=0; i<size; i++) { |
| 936 | + total += g_DemodBitRangeBuffer[i]; |
| 937 | + PrintAndLogEx(SUCCESS, "%d", g_DemodBitRangeBuffer[i]); |
| 938 | + } |
| 939 | + PrintAndLogEx(SUCCESS, "total %d", total); |
| 940 | + } |
| 941 | + |
| 942 | + if (size == 0) { |
| 943 | + PrintAndLogEx(DEBUG, "DEBUG: (Hitag2PWM) No wave detected"); |
| 944 | + free(bits); |
| 945 | + return PM3_ESOFT; |
| 946 | + } |
| 947 | + |
| 948 | + |
| 949 | + PrintAndLogEx(SUCCESS, _YELLOW_("HITAG/PWM") " - decoded bitstream"); |
| 950 | + PrintAndLogEx(INFO, "--------------------------------------"); |
| 951 | + printDemodBuff(0, false, false, false); |
| 952 | + |
| 953 | + free(bits); |
| 954 | + return PM3_SUCCESS; |
| 955 | +} |
886 | 956 |
|
887 | 957 | // Annotate HITAG protocol
|
888 | 958 | void annotateHitag1(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool is_response) {
|
@@ -943,16 +1013,17 @@ void annotateHitagS(char *exp, size_t size, uint8_t *cmd, uint8_t cmdsize, bool
|
943 | 1013 | }
|
944 | 1014 |
|
945 | 1015 | static command_t CommandTable[] = {
|
946 |
| - {"help", CmdHelp, AlwaysAvailable, "This help"}, |
947 |
| - {"eload", CmdLFHitagEload, IfPm3Hitag, "Load Hitag dump file into emulator memory"}, |
948 |
| - {"list", CmdLFHitagList, AlwaysAvailable, "List Hitag trace history"}, |
949 |
| - {"info", CmdLFHitagInfo, IfPm3Hitag, "Hitag2 tag information"}, |
950 |
| - {"reader", CmdLFHitagReader, IfPm3Hitag, "Act like a Hitag reader"}, |
951 |
| - {"sim", CmdLFHitagSim, IfPm3Hitag, "Simulate Hitag transponder"}, |
952 |
| - {"sniff", CmdLFHitagSniff, IfPm3Hitag, "Eavesdrop Hitag communication"}, |
953 |
| - {"writer", CmdLFHitagWriter, IfPm3Hitag, "Act like a Hitag writer"}, |
954 |
| - {"dump", CmdLFHitag2Dump, IfPm3Hitag, "Dump Hitag2 tag"}, |
955 |
| - {"cc", CmdLFHitagCheckChallenges, IfPm3Hitag, "Test all challenges"}, |
| 1016 | + {"help", CmdHelp, AlwaysAvailable, "This help"}, |
| 1017 | + {"eload", CmdLFHitagEload, IfPm3Hitag, "Load Hitag dump file into emulator memory"}, |
| 1018 | + {"list", CmdLFHitagList, AlwaysAvailable, "List Hitag trace history"}, |
| 1019 | + {"info", CmdLFHitagInfo, IfPm3Hitag, "Hitag2 tag information"}, |
| 1020 | + {"reader", CmdLFHitagReader, IfPm3Hitag, "Act like a Hitag reader"}, |
| 1021 | + {"sim", CmdLFHitagSim, IfPm3Hitag, "Simulate Hitag transponder"}, |
| 1022 | + {"sniff", CmdLFHitagSniff, IfPm3Hitag, "Eavesdrop Hitag communication"}, |
| 1023 | + {"writer", CmdLFHitagWriter, IfPm3Hitag, "Act like a Hitag writer"}, |
| 1024 | + {"dump", CmdLFHitag2Dump, IfPm3Hitag, "Dump Hitag2 tag"}, |
| 1025 | + {"pwmdemod", CmdLFHitag2PWMDemod, AlwaysAvailable, "PWM Hitag2 reader message demodulation"}, |
| 1026 | + {"cc", CmdLFHitagCheckChallenges, IfPm3Hitag, "Test all challenges"}, |
956 | 1027 | { NULL, NULL, 0, NULL }
|
957 | 1028 | };
|
958 | 1029 |
|
|
0 commit comments