tools scripts: fix mix of spaces & tabs

This commit is contained in:
Philippe Teuwen 2019-03-09 10:52:45 +01:00
commit bcb33ca5ef
7 changed files with 214 additions and 214 deletions

View file

@ -25,77 +25,77 @@ import os
# invert binary string
def invert(data):
return ''.join('0' if c == '1' else '1' for c in data)
return ''.join('0' if c == '1' else '1' for c in data)
# do the actual search
def search(target,data):
location = data.find(target)
if location >= 0:
print '*** Match at bit %d:' % location,
print '%s<%s>%s' % (data[:location],target,data[location+len(target):])
else:
print 'Not found'
location = data.find(target)
if location >= 0:
print '*** Match at bit %d:' % location,
print '%s<%s>%s' % (data[:location],target,data[location+len(target):])
else:
print 'Not found'
# convert integer to binary string
def binstring(number):
return bin(number)[2:] if number > 0 else ''
return bin(number)[2:] if number > 0 else ''
# reverse string order
def stringreverse(data):
return data[::-1]
return data[::-1]
# match forward, backward and inverted
def domatch(binary,number):
reversed= stringreverse(number)
inverted= invert(binary)
reversed= stringreverse(number)
inverted= invert(binary)
print ' Forward: (%s)' % number,
search(binary,number)
print ' Reverse: (%s)' % reversed,
search(binary,reversed)
print ' Inverse: (%s)' % inverted
print ' Forward: (%s)' % number,
search(inverted,number)
print ' Reverse: (%s)' % reversed,
search(inverted,reversed)
print ' Forward: (%s)' % number,
search(binary,number)
print ' Reverse: (%s)' % reversed,
search(binary,reversed)
print ' Inverse: (%s)' % inverted
print ' Forward: (%s)' % number,
search(inverted,number)
print ' Reverse: (%s)' % reversed,
search(inverted,reversed)
def main():
if(len(sys.argv) < 3):
print
print '\t'+sys.argv[0] + ' - Search bitstream for a known number'
print
print 'Usage: ' + sys.argv[0] + ' <NUMBER> <BITSTREAM>'
print
print '\tNUMBER will be converted to it\'s BINARY equivalent for all valid'
print '\tinstances of BINARY, OCTAL, DECIMAL and HEX, and the bitstream'
print '\tand it\'s inverse will be searched for a pattern match. Note that'
print '\tNUMBER must be specified in BINARY to match leading zeros.'
print
print 'Example:'
print
print '\tfindbits.py 73 0110010101110011'
print
os._exit(True)
if(len(sys.argv) < 3):
print
print '\t'+sys.argv[0] + ' - Search bitstream for a known number'
print
print 'Usage: ' + sys.argv[0] + ' <NUMBER> <BITSTREAM>'
print
print '\tNUMBER will be converted to it\'s BINARY equivalent for all valid'
print '\tinstances of BINARY, OCTAL, DECIMAL and HEX, and the bitstream'
print '\tand it\'s inverse will be searched for a pattern match. Note that'
print '\tNUMBER must be specified in BINARY to match leading zeros.'
print
print 'Example:'
print
print '\tfindbits.py 73 0110010101110011'
print
os._exit(True)
bases= {
2:'BINARY',
8:'OCTAL',
10:'DECIMAL',
16:'HEX',
}
bases= {
2:'BINARY',
8:'OCTAL',
10:'DECIMAL',
16:'HEX',
}
for base, base_name in sorted(bases.iteritems()):
try:
number= int(sys.argv[1],base)
print
print 'Trying', base_name
# do BINARY as specified to preserve leading zeros
if base == 2:
domatch(sys.argv[1],sys.argv[2])
else:
domatch(binstring(number),sys.argv[2])
except:
continue
for base, base_name in sorted(bases.iteritems()):
try:
number= int(sys.argv[1],base)
print
print 'Trying', base_name
# do BINARY as specified to preserve leading zeros
if base == 2:
domatch(sys.argv[1],sys.argv[2])
else:
domatch(binstring(number),sys.argv[2])
except:
continue
if __name__ == '__main__':
main()
main()

View file

@ -4,65 +4,65 @@ from itertools import imap
import unittest, sys, findbits
class TestFindBits(unittest.TestCase):
def setUp(self):
self.old_stdout = sys.stdout
sys.stdout = OutputBuffer()
def setUp(self):
self.old_stdout = sys.stdout
sys.stdout = OutputBuffer()
def tearDown(self):
sys.stdout = self.old_stdout
def tearDown(self):
sys.stdout = self.old_stdout
INVERT_CASES = [
('10', '01'),
('', ''),
]
def test_invert(self):
self.commutative_test(findbits.invert, self.INVERT_CASES)
INVERT_CASES = [
('10', '01'),
('', ''),
]
def test_invert(self):
self.commutative_test(findbits.invert, self.INVERT_CASES)
SEARCH_CASES = [
('1111', '10111101', ['Match at bit 2', '0<1111>0']),
('00', '10111101', ['Not found']),
]
def test_search(self):
for target, data, expected_fragments in self.SEARCH_CASES:
sys.stdout.clear_buffer()
findbits.search(target, data)
for fragment in expected_fragments:
self.assertIn(fragment, sys.stdout.content)
SEARCH_CASES = [
('1111', '10111101', ['Match at bit 2', '0<1111>0']),
('00', '10111101', ['Not found']),
]
def test_search(self):
for target, data, expected_fragments in self.SEARCH_CASES:
sys.stdout.clear_buffer()
findbits.search(target, data)
for fragment in expected_fragments:
self.assertIn(fragment, sys.stdout.content)
BINSTRING_CASES = [
(42, '101010'),
(1, '1'),
(0, ''),
]
def test_binstring(self):
self.unary_operation_test(findbits.binstring, self.BINSTRING_CASES)
BINSTRING_CASES = [
(42, '101010'),
(1, '1'),
(0, ''),
]
def test_binstring(self):
self.unary_operation_test(findbits.binstring, self.BINSTRING_CASES)
REVERSE_CASES = [
('abc', 'cba'),
('', ''),
]
def test_stringreverse(self):
self.commutative_test(findbits.stringreverse, self.REVERSE_CASES)
REVERSE_CASES = [
('abc', 'cba'),
('', ''),
]
def test_stringreverse(self):
self.commutative_test(findbits.stringreverse, self.REVERSE_CASES)
def commutative_test(self, operation, cases):
self.unary_operation_test(operation, cases)
self.unary_operation_test(operation, imap(reversed, cases))
def commutative_test(self, operation, cases):
self.unary_operation_test(operation, cases)
self.unary_operation_test(operation, imap(reversed, cases))
def unary_operation_test(self, operation, cases):
for case_in, case_out in cases:
self.assertEqual(operation(case_in), case_out)
def unary_operation_test(self, operation, cases):
for case_in, case_out in cases:
self.assertEqual(operation(case_in), case_out)
class OutputBuffer(object):
def __init__(self):
self.clear_buffer()
def __init__(self):
self.clear_buffer()
def clear_buffer(self):
self.content = ''
def clear_buffer(self):
self.content = ''
def write(self, data):
self.content += data
def write(self, data):
self.content += data
if __name__ == '__main__':
unittest.main()
unittest.main()

View file

@ -14,16 +14,16 @@ if [ "${1}" = "" ]; then
echo "Syntax: ${0} </installation/target/directory> [download & build directory (default ${PWD})]"
exit 1
else
DESTDIR="${1}"
DESTDIR="${1}"
fi
# Where do you want to build the tools. This is where the log files
# will be written (which you can monitor with 'tail' during compilation).
# You can delete this directory after everything is done.
if [ ! "${2}" = "" ]; then
SRCDIR="${2}"
SRCDIR="${2}"
else
SRCDIR="${PWD}"
SRCDIR="${PWD}"
fi
BUILDDIR=${SRCDIR}/build-gnuarm4

View file

@ -32,28 +32,28 @@ my $commandGIT = "env which git";
if ( defined($commandGIT) ) {
my $githistory = `git fetch --all`;
# now avoiding the "fatal: No names found, cannot describe anything." error by fallbacking to abbrev hash in such case
my $gitversion = `git describe --dirty --always`;
my $gitbranch = `git rev-parse --abbrev-ref HEAD`;
$clean = $gitversion =~ '-dirty' ? 0 : 1;
my $githistory = `git fetch --all`;
# now avoiding the "fatal: No names found, cannot describe anything." error by fallbacking to abbrev hash in such case
my $gitversion = `git describe --dirty --always`;
my $gitbranch = `git rev-parse --abbrev-ref HEAD`;
$clean = $gitversion =~ '-dirty' ? 0 : 1;
if ( defined($gitbranch) and defined($gitversion) ) {
$fullgitinfo = $fullgitinfo.'/'. $gitbranch . '/' . $gitversion;
if ( defined($gitbranch) and defined($gitversion) ) {
$fullgitinfo = $fullgitinfo.'/'. $gitbranch . '/' . $gitversion;
my @compiletime = localtime();
$compiletime[4] += 1;
$compiletime[5] += 1900;
$ctime = sprintf("%6\$04i-%5\$02i-%4\$02i %3\$02i:%2\$02i:%1\$02i", @compiletime);
} else {
$fullgitinfo = $fullgitinfo.'/master/release (git)';
}
my @compiletime = localtime();
$compiletime[4] += 1;
$compiletime[5] += 1900;
$ctime = sprintf("%6\$04i-%5\$02i-%4\$02i %3\$02i:%2\$02i:%1\$02i", @compiletime);
} else {
$fullgitinfo = $fullgitinfo.'/master/release (git)';
}
} else {
$fullgitinfo = $fullgitinfo.'/master/release (no_git)';
my @dl_time = localtime( (stat('../README.md'))[10] );
$dl_time[4] += 1;
$dl_time[5] += 1900;
$ctime = sprintf("%6\$04i-%5\$02i-%4\$02i %3\$02i:%2\$02i:%1\$02i", @dl_time);
$fullgitinfo = $fullgitinfo.'/master/release (no_git)';
my @dl_time = localtime( (stat('../README.md'))[10] );
$dl_time[4] += 1;
$dl_time[5] += 1900;
$ctime = sprintf("%6\$04i-%5\$02i-%4\$02i %3\$02i:%2\$02i:%1\$02i", @dl_time);
}
$fullgitinfo =~ s/(\s)//g;
@ -66,11 +66,11 @@ print <<EOF
#include "proxmark3.h"
/* Generated file, do not edit */
const struct version_information __attribute__((section(".version_information"))) version_information = {
VERSION_INFORMATION_MAGIC,
1,
1,
$clean,
"$fullgitinfo",
"$ctime",
VERSION_INFORMATION_MAGIC,
1,
1,
$clean,
"$fullgitinfo",
"$ctime",
};
EOF

View file

@ -16,20 +16,20 @@ if ($search =~ /^[01]+$/) { }
# decimal
elsif ($search =~ /^\d+$/)
{
$search = unpack("B*", pack("N", $search));
$search =~ s/^0*//;
$search = unpack("B*", pack("N", $search));
$search =~ s/^0*//;
}
# hex
elsif ($search =~ /^[\da-fA-F]+$/)
{
$search = unpack("B*", pack("H*", $search));
$search =~ s/^0*//;
$search = unpack("B*", pack("H*", $search));
$search =~ s/^0*//;
}
# ascii
else
{
$search = unpack("B*", $search);
$search =~ s/^0*//;
$search = unpack("B*", $search);
$search =~ s/^0*//;
}
@ -44,12 +44,12 @@ $data =~ s/\s//g;
if ($data =~ /^[01]+$/) { }
elsif ($data =~ /^[\da-fA-F]+$/)
{
$data = unpack("B*", pack("H*", $data));
$search =~ s/^0*//;
$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";
die "Seriously. What sort of data is this file? Binary or hex only please.\n";
}
@ -66,12 +66,12 @@ 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;
# if we changed, flip our bit
if ($bits[$i-1] == 1)
{
$last ^= 1;
}
$man .= $last;
}
print "Testing with manchester demodulation...\n";
@ -83,37 +83,37 @@ test_all($man, $search, 1);
sub test_all
{
my ($data, $search, $flip) = @_;
my ($data, $search, $flip) = @_;
if ($flip)
{
$data =~ s/(.)/$1 ^ 1/eg;
}
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";
}
# 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;
# 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";
}
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/^.//;
}
}
# chop of a bit to change our bit position next round
$tmp =~ s/^.//;
}
}
}

View file

@ -6,30 +6,30 @@
# Jonathan Westhues, April 2004
if(@ARGV == 0) {
die "usage: $0 file-to-endian-swap.s19 > out.s19\n";
die "usage: $0 file-to-endian-swap.s19 > out.s19\n";
}
while(<>) {
chomp;
chomp;
if(/^S0/) {
next;
}
if(/^S7/) {
print "$_\n";
next;
}
if(/^S0/) {
next;
}
if(/^S7/) {
print "$_\n";
next;
}
if(not /^S3(..)(........)(.*)(..)$/) {
die "bad S record at line $.\n";
}
if(not /^S3(..)(........)(.*)(..)$/) {
die "bad S record at line $.\n";
}
$data = $3;
$checksum = $4;
$data = $3;
$checksum = $4;
print "S3$1$2";
while($data =~ m#(..)(..)(..)(..)#g) {
print "$4$3$2$1";
}
print "$checksum\n";
print "S3$1$2";
while($data =~ m#(..)(..)(..)(..)#g) {
print "$4$3$2$1";
}
print "$checksum\n";
}

View file

@ -24,29 +24,29 @@ import sys
import os
if(len(sys.argv) < 3):
print
print '\t'+sys.argv[0] + ' - Generate final byte for XOR LRC'
print
print 'Usage: ' + sys.argv[0] + ' <ID Byte1> <ID Byte2> ... <LRC>'
print
print '\tSpecifying the bytes of a UID with a known LRC will find the last byte value'
print '\tneeded to generate that LRC with a rolling XOR. All bytes should be specified in HEX.'
print
print 'Example:'
print
print '\txorcheck.py 04 00 80 64 ba'
print
print 'Should produce the output:'
print
print '\tTarget (BA) requires final LRC XOR byte value: 5A'
print
os._exit(True)
print
print '\t'+sys.argv[0] + ' - Generate final byte for XOR LRC'
print
print 'Usage: ' + sys.argv[0] + ' <ID Byte1> <ID Byte2> ... <LRC>'
print
print '\tSpecifying the bytes of a UID with a known LRC will find the last byte value'
print '\tneeded to generate that LRC with a rolling XOR. All bytes should be specified in HEX.'
print
print 'Example:'
print
print '\txorcheck.py 04 00 80 64 ba'
print
print 'Should produce the output:'
print
print '\tTarget (BA) requires final LRC XOR byte value: 5A'
print
os._exit(True)
target= int(sys.argv[len(sys.argv) - 1],16)
lrc= 0x00
for i in range(len(sys.argv) - 1):
lrc ^= int(sys.argv[i + 1],16)
lrc ^= int(sys.argv[i + 1],16)
print
print 'Target (%02X) requires final LRC XOR byte value: %02X' % (target,lrc)
print