5 use RFID::Libnfc::Reader;
6 use RFID::Libnfc::Constants;
8 use Digest::MD5 qw(md5_hex);
9 use Getopt::Long::Descriptive;
11 use Data::Dump qw(dump);
13 my ($opt,$usage) = describe_options(
14 '%c %c [dump_with_keys]',
15 [ 'write=s', 'write dump to card' ],
16 [ 'debug|d', 'show debug dumps' ],
17 [ 'help|h', 'usage' ],
19 print $usage->text, exit if $opt->help;
21 my $debug = $ENV{DEBUG} || 0;
22 my $keyfile = shift @ARGV;
24 my $r = RFID::Libnfc::Reader->new(debug => $debug);
26 warn "reader: %s\n", $r->name;
27 my $tag = $r->connect(IM_ISO14443A_106);
36 my $uid = sprintf "%02x%02x%02x%02x", @{ $tag->uid };
38 my $card_key_file = "cards/$uid.key";
39 $keyfile ||= $card_key_file;
42 warn "# loading keys from $keyfile";
43 $tag->load_keys($keyfile);
44 warn "## _keys = ", dump($tag->{_keys}) if $debug;
47 $tag->select if ($tag->can("select"));
51 print STDERR "reading $uid blocks ";
52 for (my $i = 0; $i < $tag->blocks; $i++) {
53 if (my $data = $tag->read_block($i)) {
54 # if we are dumping an ultralight token,
55 # we receive 16 bytes (while a block is 4bytes long)
56 # so we can skip next 3 blocks
57 $i += 3 if ($tag->type eq "ULTRA");
60 } elsif ( $tag->error =~ m/auth/ ) {
61 warn $tag->error,"\n";
63 # disconnect from reader so we can run mfoc
64 RFID::Libnfc::nfc_disconnect($r->{_pdi});
66 print "Dump this card with mfoc? [y] ";
67 my $yes = <STDIN>; chomp $yes;
68 exit unless $yes =~ m/y/i || $yes eq '';
70 my $file = "cards/$uid.keys";
72 warn "# finding keys for card $uid with: mfoc -O $file\n";
73 exec "mfoc -O $file" || die $!;
78 print STDERR "done\n";
80 # re-insert keys into dump
81 my $keys = $tag->{_keys} || die "can't find _keys";
82 foreach my $i ( 0 .. $#$keys ) {
83 my $o = $i * 0x40 + 0x30;
84 last if $o > length($card);
86 = substr($card, 0, $o) . $keys->[$i]->[0]
87 . substr($card, $o+6, 4) . $keys->[$i]->[1]
88 . substr($card, $o+16)
90 warn "# sector $i keys re-inserted at $o\n" if $debug;
93 if ( my $padding = 4096 - length($card) ) {
94 warn "# add $padding bytes up to 4k dump (needed for keys loading)\n" if $debug;
95 $card .= "\x00" x $padding;
98 my $md5 = md5_hex($card);
99 my $out_file = "cards/$uid.$md5";
100 if ( -e $out_file ) {
101 warn "$out_file allready exists, not overwriting\n";
103 write_file $out_file, $card;
104 warn "$out_file ", -s $out_file, " bytes key: $card_key_file\n";
105 if ( ! -e $card_key_file ) {
106 $out_file =~ s{^cards/}{} || die "can't strip directory from out_file";
107 symlink $out_file, $card_key_file || die "$card_key_file: $!";
108 warn "$card_key_file symlink created as default key for $uid\n";
113 my $card = read_file $opt->write;
114 print STDERR "writing $uid block ";
115 foreach my $block ( 0 .. $tag->blocks ) {
116 my $offset = 0x10 * $block;
117 $tag->write_block( $block, substr($card,$offset,0x10) );
118 print STDERR "$block ";
120 print STDERR "done\n";
124 system "./mifare-mad.pl $out_file > $out_file.txt";
125 $ENV{MAD} && system "vi $out_file.txt";