added --write dump.mfd option
authorDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 2 Feb 2011 11:21:46 +0000 (12:21 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 2 Feb 2011 11:21:46 +0000 (12:21 +0100)
nfc-card-dumper.pl

index ffdecc9..b1e780a 100755 (executable)
@@ -6,9 +6,18 @@ use RFID::Libnfc::Reader;
 use RFID::Libnfc::Constants;
 use File::Slurp;
 use Digest::MD5 qw(md5_hex);
+use Getopt::Long::Descriptive;
 
 use Data::Dump qw(dump);
 
+my ($opt,$usage) = describe_options(
+       '%c %c [dump_with_keys]',
+       [ 'write=s',    'write dump to card' ],
+       [ 'debug|d',    'show debug dumps' ],
+       [ 'help|h',             'usage' ],
+);
+print $usage->text, exit if $opt->help;
+
 my $debug = $ENV{DEBUG} || 0;
 my $keyfile = shift @ARGV;
 
@@ -39,7 +48,7 @@ if ($r->init()) {
 
        my $card;
 
-       print STDERR "$uid reading blocks ";
+       print STDERR "reading $uid blocks ";
     for (my $i = 0; $i < $tag->blocks; $i++) {
         if (my $data = $tag->read_block($i)) {
             # if we are dumping an ultralight token, 
@@ -100,6 +109,17 @@ if ($r->init()) {
                }
        }
 
+       if ( $opt->write ) {
+               my $card = read_file $opt->write;
+               print STDERR "writing $uid block ";
+               foreach my $block ( 0 .. $tag->blocks ) {
+                       my $offset = 0x10 * $block;
+                       $tag->write_block( $block, substr($card,$offset,0x10) );
+                       print STDERR "$block ";
+               }
+               print STDERR "done\n";
+       }
+
        # view dump
        system "./mifare-mad.pl $out_file > $out_file.txt";
        $ENV{MAD} && system "vi $out_file.txt";