Merge remote-tracking branch 'mjesec/master'
[librfid] / utils / mifare-tool.c
index 7c3a51e..ab6d216 100644 (file)
@@ -13,7 +13,7 @@
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
-//#include <libgen.h>
+
+#ifndef __MINGW32__
+#include <libgen.h>
+#endif
 
 #define _GNU_SOURCE
 #include <getopt.h>
@@ -47,7 +50,8 @@ static void help(void)
                " -r    --read          Read a mifare sector\n"
                " -l    --loop-read     Loop reading a mifare sector\n"
                " -w    --write         Write a mifare sector\n"
-               " -k    --key           Specify mifare access key (in hex tuples)\n");
+               " -k    --key           Specify mifare access key (in hex tuples)\n"
+               " -b    --brute-force n Brute Force read sector n\n");
 }
 
 static struct option mifare_opts[] = {
@@ -56,6 +60,7 @@ static struct option mifare_opts[] = {
        { "loop-read", 1, 0, 'l' },
        { "write", 1 ,0, 'w' },
        { "help", 0, 0, 'h' },
+       { "brute-force", 1, 0, 'b' },
        { 0, 0, 0, 0 }
 };
 
@@ -117,12 +122,25 @@ int main(int argc, char **argv)
        }
 
        while (1) {
-               c = getopt_long(argc, argv, "k:r:l:w:", mifare_opts,
+               c = getopt_long(argc, argv, "k:r:l:w:b:h", mifare_opts,
                                &option_index);
                if (c == -1)
                        break;
 
                switch (c) {
+                       int i;
+               case 'b':
+                       page = atoi(optarg);
+                       printf("key: %s\n", hexdump(key, MIFARE_CL_KEY_LEN));
+                       len = MIFARE_CL_PAGE_SIZE;
+                       mifare_l3();
+                       for (i = 0; i <= 0xff; i++) {
+                               key[MIFARE_CL_KEY_LEN-1]=i;
+                               if (mifare_cl_auth(key, page) >= 0)
+                                       printf("KEY: %s\n",hexdump(key, MIFARE_CL_KEY_LEN));
+                       }
+
+                       break;
                case 'k':
                        hexread(key, optarg, strlen(optarg));
                        printf("key: %s\n", hexdump(key, MIFARE_CL_KEY_LEN));