poolset.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <nfc/nfc.h>
  5. #include <freefare.h>
  6. #include "color.h"
  7. MifareClassicKey SecretKey[] = { 0x00, 0x01, 0x31, 0xB9, 0x3F, 0x28 };
  8. MifareClassicBlock data_read4;
  9. MifareClassicBlock data_read5;
  10. void show_menu(){
  11. printf("How it works? So easy !\n");
  12. printf(" --read\tRead credits and date\n");
  13. printf(" --write\tWrite <credit> <buy_date> <expire_date>\n");
  14. printf("\t\tEx: ./poolset --write 10 25/12/16 25/06/16\n\n");
  15. }
  16. void rw_tag(int coins, char *buy_date, char *expire_date) {
  17. nfc_context *context; //init libnfc
  18. nfc_device *pnd; //trouver un lecteur
  19. MifareTag *tags = NULL; //stockage des tags
  20. int i, nbsect;
  21. nfc_init(&context);
  22. if(context == NULL) {
  23. printf("[x] Unable to init libnfc !\n");
  24. exit(EXIT_FAILURE);
  25. }
  26. pnd = nfc_open(context, NULL);
  27. if (pnd == NULL) {
  28. printf("[x] Unable to open NFC device !\n");
  29. exit(EXIT_FAILURE);
  30. }
  31. tags = freefare_get_tags(pnd);
  32. printf("NFC reader: "ANSI_COLOR_GREEN"%s"ANSI_COLOR_RESET" opened and waiting for a tag\n", nfc_device_get_name(pnd));
  33. while(*tags == NULL) {
  34. printf(".");
  35. fflush(stdout);
  36. sleep(1);
  37. tags = freefare_get_tags(pnd);
  38. }
  39. printf("\n");
  40. for(i=0; tags[i]; i++) {
  41. switch(freefare_get_tag_type(tags[i])) {
  42. case MIFARE_CLASSIC_1K:
  43. 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]) );
  44. break;
  45. case MIFARE_MINI:
  46. 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]) );
  47. break;
  48. default:
  49. printf(ANSI_COLOR_RED"[x] Wrong tag Mr Dugland !\n"ANSI_COLOR_RESET);
  50. break;
  51. exit(EXIT_FAILURE);
  52. }
  53. 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]);
  54. //Check for a good tag type
  55. if(mifare_classic_connect(tags[i]) == OPERATION_OK) {
  56. printf(ANSI_COLOR_GREEN"=> Mifare tag connected !\n"ANSI_COLOR_RESET);
  57. }
  58. else printf(ANSI_COLOR_RED"Non ca chie dans la colle mais grave, ton tag est moisi\n"ANSI_COLOR_RESET);
  59. //Authenticating the tag
  60. if(mifare_classic_authenticate(tags[i], 5, SecretKey[0], MFC_KEY_A) == OPERATION_OK) {
  61. printf(ANSI_COLOR_GREEN"=> Tag authenticated !\n"ANSI_COLOR_RESET);
  62. }
  63. else printf(ANSI_COLOR_RED"Non ca chie dans la colle mais grave, check ta clé\n"ANSI_COLOR_RESET);
  64. //Check of arguements in rw_tag() for read only or write/read
  65. if((coins != 0 || coins == 0) && buy_date != NULL && expire_date != NULL) {
  66. char *token;
  67. token = strtok (buy_date,"/");
  68. int j=0;
  69. while (token != NULL) {
  70. printf ("%s ", token);
  71. data_read5[j] = atoi(token);
  72. j++;
  73. token = strtok (NULL, "/");
  74. }
  75. token = strtok (expire_date, "/");
  76. while (token != NULL) {
  77. printf ("%s ", token);
  78. data_read5[j] = atoi(token);
  79. j++;
  80. token = strtok (NULL, "/");
  81. }
  82. data_read5[6] = 0;
  83. data_read5[7] = coins;
  84. printf("=> Writing new datas...\n");
  85. if(mifare_classic_write(tags[i], 5, &data_read5) == OPERATION_OK) {
  86. if(mifare_classic_read(tags[i], 5, &data_read5) == OPERATION_OK) {
  87. printf("=> "ANSI_COLOR_GREEN"%.2d"ANSI_COLOR_RESET" credits found now !\n", data_read5[7]);
  88. }
  89. }
  90. printf("Reading a second time to check...\n");
  91. }
  92. if( (mifare_classic_read(tags[i], 5, &data_read5) == OPERATION_OK)
  93. &&
  94. (mifare_classic_read(tags[i], 4, &data_read4) == OPERATION_OK) )
  95. {
  96. printf(ANSI_COLOR_YELLOW"=-=-=-=-=-=-=- INFOS -=-=-=-=-=-=-=-=\n"ANSI_COLOR_RESET);
  97. 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]);
  98. printf(" => Credits :\t\t"ANSI_COLOR_YELLOW"%.2d\n"ANSI_COLOR_RESET, data_read5[7]);
  99. printf(" => Buy date :\t\t"ANSI_COLOR_YELLOW"%.2d/%.2d/%.2d\n"ANSI_COLOR_RESET, data_read5[0], data_read5[1], data_read5[2]);
  100. printf(" => Expiration date :\t"ANSI_COLOR_YELLOW"%.2d/%.2d/%.2d\n"ANSI_COLOR_RESET, data_read5[3], data_read5[4], data_read5[5]);
  101. if (data_read5[7] == 0) {
  102. printf(" => Type : "ANSI_COLOR_YELLOW"\t\tFull access tag (Probably)\n"ANSI_COLOR_RESET);
  103. printf(" => Last validation :\t"ANSI_COLOR_YELLOW"%.2d/%.2d/%.2d at %.2d:%.2d\n"ANSI_COLOR_RESET,
  104. data_read5[8], data_read5[9], data_read5[10], data_read5[11], data_read5[12]);
  105. }
  106. else
  107. printf(" => Type : "ANSI_COLOR_YELLOW"\t\tCredited card\n"ANSI_COLOR_RESET);
  108. //printf(" => Card keeped :\t");
  109. }
  110. mifare_classic_disconnect(tags[i]);
  111. break;
  112. }
  113. freefare_free_tags(tags);
  114. nfc_close(pnd);
  115. nfc_exit(context);
  116. exit(EXIT_SUCCESS);
  117. }
  118. int main(int argc, const char **argv) {
  119. printf("=========================\n");
  120. printf("RFID Poolset Read/Write !\n");
  121. printf("=========================\n");
  122. if (argc <= 1) {
  123. show_menu();
  124. exit(1);
  125. }
  126. if (strcmp(argv[1], "--read") == 0)
  127. rw_tag(NULL, NULL, NULL);
  128. if (strcmp(argv[1], "--write") == 0)
  129. rw_tag(atoi(argv[2]), argv[3], argv[4]);
  130. exit(0);
  131. }