X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;ds=sidebyside;f=nfc-card-dumper.pl;h=5397f06c5b0ecb99c5697ae4a8dfa85948de64f4;hb=117ad2e869b1f7ad6485e60dce85f691b301e081;hp=bef1652c2acb40ea5ec122e1ac70129da36f0613;hpb=738087ed59e60ba007b6357e5ec8c60e3fd8afe5;p=perl-Mifare-MAD.git diff --git a/nfc-card-dumper.pl b/nfc-card-dumper.pl index bef1652..5397f06 100755 --- a/nfc-card-dumper.pl +++ b/nfc-card-dumper.pl @@ -6,15 +6,24 @@ 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; my $r = RFID::Libnfc::Reader->new(debug => $debug); if ($r->init()) { - printf ("Reader: %s\n", $r->name); + warn "reader: %s\n", $r->name; my $tag = $r->connect(IM_ISO14443A_106); if ($tag) { @@ -24,10 +33,7 @@ if ($r->init()) { exit -1; } - my $uid = sprintf "%02x%02x%02x%02x", unpack('C4', $tag->{_nai}->abtUid); - # @{ $tag->uid }; # FIXME this doesn't work with tags which have 00 in UID! - - warn "UID: $uid\n"; + my $uid = sprintf "%02x%02x%02x%02x", @{ $tag->uid }; my $card_key_file = "cards/$uid.key"; $keyfile ||= $card_key_file; @@ -35,14 +41,14 @@ if ($r->init()) { if ( -e $keyfile ) { warn "# loading keys from $keyfile"; $tag->load_keys($keyfile); - warn "## _keys = ", dump($tag->{_keys}); + warn "## _keys = ", dump($tag->{_keys}) if $debug; } $tag->select if ($tag->can("select")); my $card; - print STDERR "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, @@ -57,6 +63,10 @@ if ($r->init()) { # disconnect from reader so we can run mfoc RFID::Libnfc::nfc_disconnect($r->{_pdi}); + print "Dump this card with mfoc? [y] "; + my $yes = ; chomp $yes; + exit unless $yes =~ m/y/i || $yes eq ''; + my $file = "cards/$uid.keys"; unlink $file; warn "# finding keys for card $uid with: mfoc -O $file\n"; @@ -77,21 +87,21 @@ if ($r->init()) { . substr($card, $o+6, 4) . $keys->[$i]->[1] . substr($card, $o+16) ; - warn "sector $i keys re-inserted at $o\n"; + warn "# sector $i keys re-inserted at $o\n" if $debug; } if ( my $padding = 4096 - length($card) ) { - warn "add $padding bytes up to 4k dump (needed for keys loading)\n"; + warn "# add $padding bytes up to 4k dump (needed for keys loading)\n" if $debug; $card .= "\x00" x $padding; } my $md5 = md5_hex($card); - my $out_file = "cards/$uid.$md5"; + my $out_file = "cards/$uid.$md5.mfd"; if ( -e $out_file ) { - warn "$out_file allready exists, SKIPING\n"; + warn "$out_file allready exists, not overwriting\n"; } else { write_file $out_file, $card; - print "$out_file ", -s $out_file, " bytes key: $card_key_file\n"; + warn "$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: $!"; @@ -99,8 +109,21 @@ if ($r->init()) { } } - # view dump - $ENV{MAD} && system "./mifare-mad.pl $out_file | vi -R -"; - + 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"; + } else { + # view dump + my $txt_file = $out_file; + $txt_file =~ s/\.mfd/.txt/ || die "can't change extension to txt"; + system "./mifare-mad.pl $out_file > $txt_file"; + $ENV{MAD} && system "vi $txt_file"; + } }