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 our $keyfile = shift @ARGV;
23 our ( $tag, $uid, $card_key_file );
27 my $r = RFID::Libnfc::Reader->new(debug => $debug);
29 warn "reader: %s\n", $r->name;
30 my $tag = $r->connect(IM_ISO14443A_106);
39 $uid = sprintf "%02x%02x%02x%02x", @{ $tag->uid };
41 $card_key_file = "cards/$uid.key";
42 $keyfile ||= $card_key_file;
45 warn "# loading keys from $keyfile";
46 $tag->load_keys($keyfile);
47 warn "## _keys = ", dump($tag->{_keys}) if $debug;
50 $tag->select if ($tag->can("select"));
54 print STDERR "reading $uid blocks ";
55 for (my $i = 0; $i < $tag->blocks; $i++) {
56 if (my $data = $tag->read_block($i)) {
57 # if we are dumping an ultralight token,
58 # we receive 16 bytes (while a block is 4bytes long)
59 # so we can skip next 3 blocks
60 $i += 3 if ($tag->type eq "ULTRA");
63 } elsif ( $tag->error =~ m/auth/ ) {
64 warn $tag->error,"\n";
66 # disconnect from reader so we can run mfoc
67 RFID::Libnfc::nfc_disconnect($r->{_pdi});
69 print "Dump this card with mfoc? [y] ";
70 my $yes = <STDIN>; chomp $yes;
71 exit unless $yes =~ m/y/i || $yes eq '';
73 my $file = "cards/$uid.key";
75 warn "# finding keys for card $uid with: mfoc -O $file\n";
76 exec "mfoc -O $file" || die $!;
81 print STDERR "done\n";
83 my $out_file = write_card_dump $tag => $card;
86 read_file $opt->write;
87 print STDERR "writing $uid block ";
88 foreach my $block ( 0 .. $tag->blocks ) {
89 my $offset = 0x10 * $block;
90 my $data = substr($card,$offset,0x10);
91 $tag->write_block( $block, $data );
92 print STDERR "$block ";
93 my $verify = $tag->read_block( $block );
94 print STDERR $verify eq $data ? "OK " : "ERROR ";
96 print STDERR "done\n";
97 unlink $card_key_file;
98 $out_file = write_card_dump $tag => $card;
101 my $txt_file = $out_file;
102 $txt_file =~ s/\.mfd/.txt/ || die "can't change extension of $out_file to txt";
103 system "./mifare-mad.pl $out_file > $txt_file";
104 $ENV{MAD} && system "vi $txt_file";
108 sub write_card_dump {
109 my ( $tag, $card ) = @_;
111 # re-insert keys into dump
112 my $keys = $tag->{_keys} || die "can't find _keys";
113 foreach my $i ( 0 .. $#$keys ) {
114 my $o = $i * 0x40 + 0x30;
115 last if $o > length($card);
117 = substr($card, 0, $o) . $keys->[$i]->[0]
118 . substr($card, $o+6, 4) . $keys->[$i]->[1]
119 . substr($card, $o+16)
121 warn "# sector $i keys re-inserted at $o\n" if $debug;
124 if ( my $padding = 4096 - length($card) ) {
125 warn "# add $padding bytes up to 4k dump (needed for keys loading)\n" if $debug;
126 $card .= "\x00" x $padding;
129 my $md5 = md5_hex($card);
130 my $out_file = "cards/$uid.$md5.mfd";
131 if ( -e $out_file ) {
132 warn "$out_file allready exists, not overwriting\n";
134 write_file $out_file, $card;
135 warn "$out_file ", -s $out_file, " bytes key: $card_key_file\n";
138 if ( ! -e $card_key_file ) {
139 my $source = $out_file;
140 $source =~ s{^cards/}{} || die "can't strip directory from out_file";
141 symlink $source, $card_key_file || die "$card_key_file: $!";
142 warn "$card_key_file symlink created as default key for $uid\n";