// get current config block from tag BOOL get_config_block(BYTE *out, BYTE tagtype) { int block; if(config_block_number(&block, tagtype)) return read_tag(out, block, block); return FALSE; }
// create a VTAG from UID BOOL vtag_create_from_uid(BYTE *target_tagtype, BYTE source_tagtype, BYTE *uid) { BYTE tag; unsigned int config_block_no, user_block_no; // set target tag type if (strlen(target_tagtype) == 0) { if(RFIDlerVTag.TagType == TAG_TYPE_NONE) tag= source_tagtype; else tag= RFIDlerVTag.TagType; } else if(!(tag= tag_get_type(target_tagtype))) return FALSE; // if tag & vtag are the same, just copy if(RFIDlerConfig.TagType == tag) { vtag_set_tag_from_type(tag); RFIDlerVTag.EmulatedTagType= TAG_TYPE_NONE; } else { // otherwise, set up emulation // check target is capable of emulating source if(config_block(TmpBits, source_tagtype, tag)) { vtag_set_tag_from_type(tag); RFIDlerVTag.EmulatedTagType= source_tagtype; } else return FALSE; } // create raw UID if(!tag_uid_to_hex(RFIDlerVTag.UID, uid, source_tagtype)) return FALSE; // get config & user data block numbers if required if(config_block_number(&config_block_no, tag)) { // get & store config block if (!config_block(&RFIDlerVTag.Data[HEXDIGITS(RFIDlerVTag.BlockSize * config_block_no)], source_tagtype, tag)) return FALSE; // copy raw hex UID to data blocks if(config_user_block(&user_block_no, tag)) memcpy(&RFIDlerVTag.Data[HEXDIGITS(RFIDlerVTag.BlockSize * user_block_no)], RFIDlerVTag.UID, strlen(RFIDlerVTag.UID)); } return TRUE; }
// reset tag to default by writing config block BOOL tag_write_default_config(BYTE tagtype, BYTE *password) { unsigned int tmp; if(!config_block_number(&tmp, tagtype)) return FALSE; // hitag2 always needs login/auth if(strlen(password) || tagtype == TAG_TYPE_HITAG2) if(!(tag_login(tmp, TmpBuff, password) || tag_auth(tmp, TmpBuff, password))) return FALSE; // emulation block for own tag type is default config block if(!config_block(TmpBuff, tagtype, tagtype)) return FALSE; return write_tag(tmp, TmpBuff, VERIFY); }
/// show contents of vtag void vtag_dump(void) { BYTE tmp[MAXBLOCKSIZE + 1], interpret; unsigned int i; UserMessage(" Type: %s", (BYTE *) TagTypes[RFIDlerVTag.TagType]); UserMessage("\r\n Emulating: %s", (BYTE *) TagTypes[RFIDlerVTag.EmulatedTagType]); UserMessage("\r\n Raw UID: %s", RFIDlerVTag.UID); // show interpreted UID if(RFIDlerVTag.EmulatedTagType == TAG_TYPE_NONE) interpret= RFIDlerVTag.TagType; else interpret= RFIDlerVTag.EmulatedTagType; UserMessage("\r\n UID: %s", interpret_uid(tmp, RFIDlerVTag.UID, interpret) ? tmp : (BYTE *) "invalid!"); // show config block if present if(config_block_number(&i, RFIDlerVTag.TagType)) { UserMessage("%s","\r\n\r\n"); config_block_show(&RFIDlerVTag.Data[HEXDIGITS(RFIDlerVTag.BlockSize * i)], RFIDlerVTag.TagType); } if(RFIDlerVTag.DataBlocks == 0) { UserMessage("%s", "\r\n\r\n"); return; } UserMessage("%s", "\r\n Data:"); tmp[HEXDIGITS(RFIDlerVTag.BlockSize)]= '\0'; for(i= 0 ; i < RFIDlerVTag.DataBlocks ; ++i) { memcpy(tmp, &RFIDlerVTag.Data[HEXDIGITS(RFIDlerVTag.BlockSize * i)], HEXDIGITS(RFIDlerVTag.BlockSize)); if(tmp[0]) { UserMessageNum("\r\n %d: ", i); UserMessage("%s", tmp); } } UserMessage("%s", "\r\n\r\n"); }
BOOL vtag_write_to_tag(BYTE *pass) { unsigned int block, config_block_no; BYTE tmp[MAXBLOCKSIZE + 1]; BOOL auth= FALSE; StoredConfig tmptag; // preserve tag type memcpy(&tmptag, &RFIDlerConfig, sizeof(RFIDlerConfig)); // set real tag to vtag type if not already the same if(RFIDlerConfig.TagType != RFIDlerVTag.TagType) if(!tag_set(RFIDlerVTag.TagType)) { memcpy(&RFIDlerConfig, &tmptag, sizeof(RFIDlerConfig)); return FALSE; } // reset target tag, but don't care if we get UID as it may not be in a valid mode get_tag_uid(tmp); // re-auth if(!tag_login(block, tmp, pass)) tag_auth(block, tmp, pass); // initialise target in default mode // get config block number if(!config_block_number(&config_block_no, RFIDlerConfig.TagType)) return FALSE; // get default config block data tmp[HEXDIGITS(RFIDlerVTag.BlockSize)]= '\0'; if (!config_block(tmp, RFIDlerConfig.TagType, RFIDlerConfig.TagType)) { memcpy(&RFIDlerConfig, &tmptag, sizeof(RFIDlerConfig)); return FALSE; } // write default config if(!write_tag(config_block_no, tmp, VERIFY)) { memcpy(&RFIDlerConfig, &tmptag, sizeof(RFIDlerConfig)); return FALSE; } // reset tag again get_tag_uid(tmp); // write all VTAG blocks with valid data in them // but avoid writing config block until last as tag may stop responding tmp[HEXDIGITS(RFIDlerVTag.BlockSize)]= '\0'; for(block= 0 ; block < RFIDlerVTag.DataBlocks ; ++block) if(block != config_block_no && RFIDlerVTag.Data[HEXDIGITS(RFIDlerVTag.BlockSize * block)]) { // try to login/auth in case target tag requires it // don't care if we fail if(!(auth= tag_login(block, tmp, pass))) auth= tag_auth(block, tmp, pass); memcpy(tmp, &RFIDlerVTag.Data[HEXDIGITS(RFIDlerVTag.BlockSize * block)], HEXDIGITS(RFIDlerVTag.BlockSize)); UserMessageNum("\r\n%d: ", block); UserMessage("%s", tmp); // failure allowed as we may be trying to write locked blocks if(!write_tag(block, tmp, VERIFY)) { UserMessage("%s", " Failed!"); if(!auth) UserMessage("%s", " (Auth/Login)"); } } // write config block (no verify as some tags stop talking after config change) if(!tag_login(block, tmp, pass)) tag_auth(block, tmp, pass); tmp[HEXDIGITS(RFIDlerVTag.BlockSize)]= '\0'; memcpy(tmp, &RFIDlerVTag.Data[HEXDIGITS(RFIDlerVTag.BlockSize * config_block_no)], HEXDIGITS(RFIDlerVTag.BlockSize)); UserMessageNum("\r\n\r\n%d: ", config_block_no); UserMessage("%s", tmp); if(!write_tag(config_block_no, tmp, NO_VERIFY)) { memcpy(&RFIDlerConfig, &tmptag, sizeof(RFIDlerConfig)); return FALSE; } memcpy(&RFIDlerConfig, &tmptag, sizeof(RFIDlerConfig)); return TRUE; }
// copy real tag to vtag or set up emulation BOOL vtag_copy_from_tag(BYTE *tagtype, BYTE *pass) { BYTE tmp[MAXUID + 1], tag, copy= FALSE, i; unsigned int config_block_no, user_block_no; // set target tag type if (strlen(tagtype) == 0) { if(RFIDlerVTag.TagType == TAG_TYPE_NONE) tag= RFIDlerConfig.TagType; else tag= RFIDlerVTag.TagType; } else if(!(tag= tag_get_type(tagtype))) return FALSE; // check we've got a tag to copy if(!get_tag_uid(tmp)) return FALSE; // set vtag to desired type vtag_set_tag_from_type(tag); // if tag & vtag are the same, just copy if(RFIDlerConfig.TagType == tag) { RFIDlerVTag.EmulatedTagType= TAG_TYPE_NONE; // auth if(!tag_login(0, TmpBits, pass)) tag_auth(0, TmpBits, pass); // copy UID strcpy(RFIDlerVTag.UID, tmp); // if no data to copy, we're done. if(RFIDlerVTag.DataBlocks == 0) return TRUE; // copy data blocks for(i= 0 ; i < RFIDlerVTag.DataBlocks ; ++i) { if (!read_tag(RFIDlerVTag.Data + HEXDIGITS(i * RFIDlerVTag.BlockSize), i, i)) UserMessageNum("%d: (fail)\r\n", i); else copy= TRUE; } return copy; } // otherwise, set up emulation RFIDlerVTag.EmulatedTagType= RFIDlerConfig.TagType; strcpy(RFIDlerVTag.UID, tmp); // get config & user data block numbers if(!config_block_number(&config_block_no, tag) || !config_user_block(&user_block_no, tag)) return FALSE; // get & store config block if (!config_block(&RFIDlerVTag.Data[HEXDIGITS(RFIDlerVTag.BlockSize * config_block_no)], RFIDlerConfig.TagType, tag)) return FALSE; // copy raw hex UID to data blocks memcpy(&RFIDlerVTag.Data[HEXDIGITS(RFIDlerVTag.BlockSize * user_block_no)], RFIDlerVTag.UID, strlen(RFIDlerVTag.UID)); return TRUE; }