forked from RfidResearchGroup/proxmark3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfidtest.pl
executable file
·119 lines (98 loc) · 2.47 KB
/
rfidtest.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/perl
# -samy kamkar, [email protected]
use strict;
die "usage: $0 <file with data> <binary to search for>\n" unless @ARGV == 2;
my ($file, $search) = @ARGV;
$search =~ s/\s//g;
# sure, these aren't perfect, but simplifies usability if you know what you're doing
# if in doubt, use binary
# binary, cool
if ($search =~ /^[01]+$/) { }
# decimal
elsif ($search =~ /^\d+$/)
{
$search = unpack("B*", pack("N", $search));
$search =~ s/^0*//;
}
# hex
elsif ($search =~ /^[\da-fA-F]+$/)
{
$search = unpack("B*", pack("H*", $search));
$search =~ s/^0*//;
}
# ascii
else
{
$search = unpack("B*", $search);
$search =~ s/^0*//;
}
# read file contents
open(F, "<$file") || die "Can't read $file: $!";
my $data = join("", <F>);
close(F);
# convert to binary
$data =~ s/\s//g;
# binary, great
if ($data =~ /^[01]+$/) { }
elsif ($data =~ /^[\da-fA-F]+$/)
{
$data = unpack("B*", pack("H*", $data));
$search =~ s/^0*//;
}
else
{
die "Seriously. What sort of data is this file? Binary or hex only please.\n";
}
# search every method we know how
print "Testing normally...\n";
test_all($data, $search);
print "Testing with flipped bits...\n";
test_all($data, $search, 1);
# now try manchester demodulating
my @bits = split(//, $data);
my $man;
my $last = 0;
for (my $i = 1; $i < @bits; $i++)
{
# if we changed, flip our bit
if ($bits[$i-1] == 1)
{
$last ^= 1;
}
$man .= $last;
}
print "Testing with manchester demodulation...\n";
test_all($man, $search);
print "Testing with flipped manchester demodulation...\n";
test_all($man, $search, 1);
sub test_all
{
my ($data, $search, $flip) = @_;
if ($flip)
{
$data =~ s/(.)/$1 ^ 1/eg;
}
# first just see if our data is in the stream
if ($data =~ /$search/)
{
print "Found $search in our stream ($data)\n";
}
# try removing parity every 4 and 8 bits
foreach my $parity (4, 8)
{
# try removing a parity bit every $parity bits
# test by cutting off a bit at a time in case we're in the wrong bit position
my $tmp = $data;
foreach (1 .. $parity)
{
my $test = $tmp;
$test =~ s/(.{$parity})./$1/g;
if ($test =~ /$search/)
{
print "Found $search with parity every " . ($parity + 1) . "th bit, round $_ out of $parity ($test)\n";
}
# chop of a bit to change our bit position next round
$tmp =~ s/^.//;
}
}
}