+int usage_lf_awid_brute(void){
+ PrintAndLog("Enables bruteforce of AWID reader with specified facility-code.");
+ PrintAndLog("This is a attack against reader. if cardnumber is given, it starts with it and goes up / down one step");
+ PrintAndLog("if cardnumber is not given, it starts with 1 and goes up to 65535");
+ PrintAndLog("");
+ PrintAndLog("Usage: lf awid brute [h] a <format> f <facility-code> c <cardnumber> d <delay>");
+ PrintAndLog("Options:");
+ PrintAndLog(" h : This help");
+ PrintAndLog(" a <format> : format length 26|50");
+ PrintAndLog(" f <facility-code> : 8|16bit value facility code");
+ PrintAndLog(" c <cardnumber> : (optional) cardnumber to start with, max 65535");
+ PrintAndLog(" d <delay> : delay betweens attempts in ms. Default 1000ms");
+ PrintAndLog("");
+ PrintAndLog("Samples:");
+ PrintAndLog(" lf awid brute a 26 f 224");
+ PrintAndLog(" lf awid brute a 50 f 2001 d 2000");
+ PrintAndLog(" lf awid brute a 50 f 2001 c 200 d 2000");
+ return 0;
+}
+
+static int sendPing(void){
+ UsbCommand ping = {CMD_PING, {1, 2, 3}};
+ SendCommand(&ping);
+ SendCommand(&ping);
+ SendCommand(&ping);
+ clearCommandBuffer();
+ UsbCommand resp;
+ if (WaitForResponseTimeout(CMD_ACK, &resp, 1000))
+ return 0;
+ return 1;
+}
+
+static bool sendTry(uint8_t fmtlen, uint32_t fc, uint32_t cn, uint32_t delay, uint8_t *bs, size_t bs_len){
+
+ PrintAndLog("Trying FC: %u; CN: %u", fc, cn);
+ if ( !getAWIDBits(fmtlen, fc, cn, bs)) {
+ PrintAndLog("Error with tag bitstream generation.");
+ return FALSE;
+ }
+
+ uint64_t arg1 = (10<<8) + 8; // fcHigh = 10, fcLow = 8
+ uint64_t arg2 = 50; // clk RF/50 invert=0
+ UsbCommand c = {CMD_FSK_SIM_TAG, {arg1, arg2, bs_len}};
+ memcpy(c.d.asBytes, bs, bs_len);
+ clearCommandBuffer();
+ SendCommand(&c);
+ msleep(delay);
+ sendPing();
+ return TRUE;
+}
+
+
+int CmdAWIDDemodFSK(const char *Cmd) {
+ int findone = 0;
+ if (Cmd[0] == 'h' || Cmd[0] == 'H') return usage_lf_awid_fskdemod();
+ if (Cmd[0] == '1') findone = 1;
+
+ UsbCommand c = {CMD_AWID_DEMOD_FSK, {findone, 0, 0}};
+ clearCommandBuffer();
+ SendCommand(&c);
+ return 0;
+}
+
+//refactored by marshmellow
+int getAWIDBits(uint8_t fmtlen, uint32_t fc, uint32_t cn, uint8_t *bits) {
+
+ // the return bits, preamble 0000 0001
+ bits[7] = 1;
+
+ uint8_t pre[66];
+ memset(pre, 0, sizeof(pre));
+
+ // add formatlength
+ num_to_bytebits(fmtlen, 8, pre);
+
+ // add facilitycode, cardnumber and wiegand parity bits
+ if ( fmtlen == 26 ) {
+ uint8_t wiegand[24];
+ num_to_bytebits(fc, 8, wiegand);
+ num_to_bytebits(cn, 16, wiegand+8);
+ wiegand_add_parity(pre+8, wiegand, sizeof(wiegand));
+ } else {
+ uint8_t wiegand[48];
+ num_to_bytebits(fc, 16, wiegand);
+ num_to_bytebits(cn, 32, wiegand+16);
+ wiegand_add_parity(pre+8, wiegand, sizeof(wiegand));
+ }
+
+ // add AWID 4bit parity
+ size_t bitLen = addParity(pre, bits+8, 66, 4, 1);
+
+ if (bitLen != 88) return 0;
+ return 1;