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

View file

@ -4,65 +4,65 @@ from itertools import imap
import unittest, sys, findbits import unittest, sys, findbits
class TestFindBits(unittest.TestCase): class TestFindBits(unittest.TestCase):
def setUp(self): def setUp(self):
self.old_stdout = sys.stdout self.old_stdout = sys.stdout
sys.stdout = OutputBuffer() sys.stdout = OutputBuffer()
def tearDown(self): def tearDown(self):
sys.stdout = self.old_stdout sys.stdout = self.old_stdout
INVERT_CASES = [ INVERT_CASES = [
('10', '01'), ('10', '01'),
('', ''), ('', ''),
] ]
def test_invert(self): def test_invert(self):
self.commutative_test(findbits.invert, self.INVERT_CASES) self.commutative_test(findbits.invert, self.INVERT_CASES)
SEARCH_CASES = [ SEARCH_CASES = [
('1111', '10111101', ['Match at bit 2', '0<1111>0']), ('1111', '10111101', ['Match at bit 2', '0<1111>0']),
('00', '10111101', ['Not found']), ('00', '10111101', ['Not found']),
] ]
def test_search(self): def test_search(self):
for target, data, expected_fragments in self.SEARCH_CASES: for target, data, expected_fragments in self.SEARCH_CASES:
sys.stdout.clear_buffer() sys.stdout.clear_buffer()
findbits.search(target, data) findbits.search(target, data)
for fragment in expected_fragments: for fragment in expected_fragments:
self.assertIn(fragment, sys.stdout.content) self.assertIn(fragment, sys.stdout.content)
BINSTRING_CASES = [ BINSTRING_CASES = [
(42, '101010'), (42, '101010'),
(1, '1'), (1, '1'),
(0, ''), (0, ''),
] ]
def test_binstring(self): def test_binstring(self):
self.unary_operation_test(findbits.binstring, self.BINSTRING_CASES) self.unary_operation_test(findbits.binstring, self.BINSTRING_CASES)
REVERSE_CASES = [ REVERSE_CASES = [
('abc', 'cba'), ('abc', 'cba'),
('', ''), ('', ''),
] ]
def test_stringreverse(self): def test_stringreverse(self):
self.commutative_test(findbits.stringreverse, self.REVERSE_CASES) self.commutative_test(findbits.stringreverse, self.REVERSE_CASES)
def commutative_test(self, operation, cases): def commutative_test(self, operation, cases):
self.unary_operation_test(operation, cases) self.unary_operation_test(operation, cases)
self.unary_operation_test(operation, imap(reversed, cases)) self.unary_operation_test(operation, imap(reversed, cases))
def unary_operation_test(self, operation, cases): def unary_operation_test(self, operation, cases):
for case_in, case_out in cases: for case_in, case_out in cases:
self.assertEqual(operation(case_in), case_out) self.assertEqual(operation(case_in), case_out)
class OutputBuffer(object): class OutputBuffer(object):
def __init__(self): def __init__(self):
self.clear_buffer() self.clear_buffer()
def clear_buffer(self): def clear_buffer(self):
self.content = '' self.content = ''
def write(self, data): def write(self, data):
self.content += data self.content += data
if __name__ == '__main__': 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})]" echo "Syntax: ${0} </installation/target/directory> [download & build directory (default ${PWD})]"
exit 1 exit 1
else else
DESTDIR="${1}" DESTDIR="${1}"
fi fi
# Where do you want to build the tools. This is where the log files # 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). # will be written (which you can monitor with 'tail' during compilation).
# You can delete this directory after everything is done. # You can delete this directory after everything is done.
if [ ! "${2}" = "" ]; then if [ ! "${2}" = "" ]; then
SRCDIR="${2}" SRCDIR="${2}"
else else
SRCDIR="${PWD}" SRCDIR="${PWD}"
fi fi
BUILDDIR=${SRCDIR}/build-gnuarm4 BUILDDIR=${SRCDIR}/build-gnuarm4

View file

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

View file

@ -16,20 +16,20 @@ if ($search =~ /^[01]+$/) { }
# decimal # decimal
elsif ($search =~ /^\d+$/) elsif ($search =~ /^\d+$/)
{ {
$search = unpack("B*", pack("N", $search)); $search = unpack("B*", pack("N", $search));
$search =~ s/^0*//; $search =~ s/^0*//;
} }
# hex # hex
elsif ($search =~ /^[\da-fA-F]+$/) elsif ($search =~ /^[\da-fA-F]+$/)
{ {
$search = unpack("B*", pack("H*", $search)); $search = unpack("B*", pack("H*", $search));
$search =~ s/^0*//; $search =~ s/^0*//;
} }
# ascii # ascii
else else
{ {
$search = unpack("B*", $search); $search = unpack("B*", $search);
$search =~ s/^0*//; $search =~ s/^0*//;
} }
@ -44,12 +44,12 @@ $data =~ s/\s//g;
if ($data =~ /^[01]+$/) { } if ($data =~ /^[01]+$/) { }
elsif ($data =~ /^[\da-fA-F]+$/) elsif ($data =~ /^[\da-fA-F]+$/)
{ {
$data = unpack("B*", pack("H*", $data)); $data = unpack("B*", pack("H*", $data));
$search =~ s/^0*//; $search =~ s/^0*//;
} }
else 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; my $last = 0;
for (my $i = 1; $i < @bits; $i++) for (my $i = 1; $i < @bits; $i++)
{ {
# if we changed, flip our bit # if we changed, flip our bit
if ($bits[$i-1] == 1) if ($bits[$i-1] == 1)
{ {
$last ^= 1; $last ^= 1;
} }
$man .= $last; $man .= $last;
} }
print "Testing with manchester demodulation...\n"; print "Testing with manchester demodulation...\n";
@ -83,37 +83,37 @@ test_all($man, $search, 1);
sub test_all sub test_all
{ {
my ($data, $search, $flip) = @_; 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 if ($flip)
foreach my $parity (4, 8) {
{ $data =~ s/(.)/$1 ^ 1/eg;
# 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; # first just see if our data is in the stream
foreach (1 .. $parity) if ($data =~ /$search/)
{ {
my $test = $tmp; print "Found $search in our stream ($data)\n";
$test =~ s/(.{$parity})./$1/g; }
if ($test =~ /$search/) # try removing parity every 4 and 8 bits
{ foreach my $parity (4, 8)
print "Found $search with parity every " . ($parity + 1) . "th bit, round $_ out of $parity ($test)\n"; {
} # 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
# chop of a bit to change our bit position next round my $tmp = $data;
$tmp =~ s/^.//; 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/^.//;
}
}
} }

View file

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

View file

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