always create txt MAD dump, but invoke vi only with MAD=1
[perl-Mifare-MAD.git] / nfc-card-dumper.pl
index d6a8af3..41f6691 100755 (executable)
@@ -9,9 +9,10 @@ use Digest::MD5 qw(md5_hex);
 
 use Data::Dump qw(dump);
 
+my $debug = $ENV{DEBUG} || 0;
 my $keyfile = shift @ARGV;
 
-my $r = RFID::Libnfc::Reader->new(debug => 1);
+my $r = RFID::Libnfc::Reader->new(debug => $debug);
 if ($r->init()) {
     printf ("Reader: %s\n", $r->name);
     my $tag = $r->connect(IM_ISO14443A_106);
@@ -28,7 +29,8 @@ if ($r->init()) {
 
        warn "UID: $uid\n";
 
-       $keyfile ||= "cards/$uid.key";
+       my $card_key_file = "cards/$uid.key";
+       $keyfile ||= $card_key_file;
 
        if ( -e $keyfile ) {
                warn "# loading keys from $keyfile";
@@ -40,7 +42,7 @@ if ($r->init()) {
 
        my $card;
 
-       print STDERR "reading";
+       print STDERR "reading blocks ";
     for (my $i = 0; $i < $tag->blocks; $i++) {
         if (my $data = $tag->read_block($i)) {
             # if we are dumping an ultralight token, 
@@ -55,7 +57,7 @@ if ($r->init()) {
                        # disconnect from reader so we can run mfoc
                        RFID::Libnfc::nfc_disconnect($r->{_pdi});
 
-                       my $file = "cards/$uid.key";
+                       my $file = "cards/$uid.keys";
                        unlink $file;
                        warn "# finding keys for card $uid with: mfoc -O $file\n";
                        exec "mfoc -O $file" || die $!;
@@ -78,19 +80,28 @@ if ($r->init()) {
                warn "sector $i keys re-inserted at $o\n";
        }
 
+       if ( my $padding = 4096 - length($card) ) {
+               warn "add $padding bytes up to 4k dump (needed for keys loading)\n";
+               $card .= "\x00" x $padding;
+       }
+
        my $md5 = md5_hex($card);
-       if ( glob "cards/$uid.$md5.*" ) {
-               warn "SKIPPING, same dump allready exits\n";
+       my $out_file = "cards/$uid.$md5";
+       if ( -e $out_file ) {
+               warn "$out_file allready exists, SKIPING\n";
        } else {
-               my $out_file = "cards/$uid.$md5";
                write_file $out_file, $card;
-               print "$out_file ", -s $out_file, " bytes\n";
-               if ( ! -e "cards/$uid.key" ) {
-                       symlink $out_file, "cards/$uid.key" || die "cards/$uid.key: $!";
-                       warn "using keys as default for card $uid\n";
+               print "$out_file ", -s $out_file, " bytes key: $card_key_file\n";
+               if ( ! -e $card_key_file ) {
+                       $out_file =~ s{^cards/}{} || die "can't strip directory from out_file";
+                       symlink $out_file, $card_key_file || die "$card_key_file: $!";
+                       warn "$card_key_file symlink created as default key for $uid\n";
                }
-               system "./mifare-mad.pl $out_file | vi -R -";
        }
 
+       # view dump
+       system "./mifare-mad.pl $out_file > $out_file.txt";
+       $ENV{MAD} && system "vi $out_file.txt";
+
 }