forked from rfidtool/ESP-RFID-Tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaba-decode.php
149 lines (122 loc) · 3.88 KB
/
aba-decode.php
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/*
USAGE:
Command Line: /usr/bin/php aba-decode.php 1101000001100000100011001001001010101101111000001010011101101111100010
Web: www.LegacySecurityGroup.com/aba-decode.php?binary=1101000001100000100011001001001010101101111000001010011101101111100010
*/
/* Decode Track 2 data from binary */
if (defined('STDIN')) {
$binary = filter_var($argv[1], FILTER_SANITIZE_NUMBER_INT);
define( "LINEBREAK", PHP_EOL);
} else {
if(isset($_POST['submit'])) {
$binary = filter_input(INPUT_POST, 'binary', FILTER_SANITIZE_NUMBER_INT);
}
else {
$binary = filter_input(INPUT_GET, 'binary', FILTER_SANITIZE_NUMBER_INT);
}
define( "LINEBREAK", "<br>");
}
if (empty($binary)) {
$binary = "1101000001100000100011001001001010101101111000001010011101101111100010";
}
echo "https://github.com/rfidtool/ESP-RFID-Tool/blob/master/Magstripe/aba-decode.php" . LINEBREAK;
echo "For converting Track 2 Magstripe ABA Binary data to ASCII" . LINEBREAK . LINEBREAK;
echo "Original script by: AndrewMohawk" . LINEBREAK;
// andrew@andrewmohawk.com
echo "http://www.andrewmohawk.com" . LINEBREAK . LINEBREAK;
echo "Modified slightly by: Corey Harding" . LINEBREAK;
echo "www.LegacySecurityGroup.com / www.Exploit.Agency" . LINEBREAK . LINEBREAK;
if (!defined('STDIN')) {
?>
<html>
<body>
<form action="<?php basename(__FILE__, '.php'); ?>" method="post">
<input type='text' name="binary" value="<?php echo $binary; ?>" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
<?php
}
// this function by mtroy dot student at gmail dot com taken from http://php.net/manual/en/function.strpos.php
function strpos_r($haystack, $needle)
{
if(strlen($needle) > strlen($haystack))
trigger_error(sprintf("%s: length of argument 2 must be <= argument 1", __FUNCTION__), E_USER_WARNING);
$seeks = array();
while($seek = strrpos($haystack, $needle))
{
array_push($seeks, $seek);
$haystack = substr($haystack, 0, $seek);
}
return $seeks;
}
function processBinary($binary)
{
$AsciiOutput = "";
//find start sentinel
$start_sentinel = strpos($binary,"11010");
if($start_sentinel === false)
{
echo "Could not find start sentinel" . LINEBREAK;
return false;
}
//find end sentinel
$end_sentinel = false;
$end_sentinel = strrpos($binary,"11111");
if(count($end_sentinel) == 0)
{
echo "Could not find end sentinel" . LINEBREAK;
return false;
}
//Lets decode the data:
$bit_length = 5; // 4 bits for data, 1 bit for odd-parity or LRC checking
$data = substr($binary,$start_sentinel,($end_sentinel-$start_sentinel+5));
$currentBits = "";
$currentNum = 0;
$finalString = "";
for($i=0;$i<strlen($data);$i++)
{
if(strlen($currentBits) < $bit_length)
{
$currentBits .= $data[$i];
}
if(strlen($currentBits) == $bit_length)
{
$parityBit = $currentBits[4];
$dataBits = substr($currentBits,0,4);
$asciiChar = 0;
for($x=0;$x<4;$x++)
{
$currentNum += $dataBits[$x];
}
$dec = bindec($dataBits);
$dec = str_pad($dec, 2, "0", STR_PAD_LEFT); // just so output is nice
$asciiChar = chr(bindec(strrev($dataBits))+48); // reverse the binary (since its LSB first) then convert to dec, add 48 and then take it to ASCII
echo "$currentBits - Data ($dataBits) Parity($parityBit) Decimal ($dec) Ascii($asciiChar)";
if(($currentNum + $parityBit) % 2 == false)
{
echo " __ Parity: Invalid";
}
else
{
echo " __ Parity: Valid";
}
$AsciiOutput .= $asciiChar;
echo LINEBREAK;
$currentBits = "";
$currentNum = 0;
}
}
echo LINEBREAK . LINEBREAK . "Total Out (ascii): $AsciiOutput" . LINEBREAK;
}
echo "Decoding " . $binary . LINEBREAK . LINEBREAK;
echo "Trying One way:" . LINEBREAK . LINEBREAK;
if (processBinary($binary) == false)
{
//reverse.
echo LINEBREAK . LINEBREAK;
echo "Trying The Reverse:" . LINEBREAK . LINEBREAK;
processBinary(strrev($binary));
}