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