]> git.zerfleddert.de Git - proxmark3-svn/blob - tools/findbits.py
minor fixes
[proxmark3-svn] / tools / findbits.py
1 #!/usr/bin/python
2
3 # findbits.py - find Binary, Octal, Decimal or Hex number in bitstream
4 #
5 # Adam Laurie <adam@algroup.co.uk>
6 # http://rfidiot.org/
7 #
8 # This code is copyright (c) Adam Laurie, 2009, All rights reserved.
9 # For non-commercial use only, the following terms apply - for all other
10 # uses, please contact the author:
11 #
12 # This code is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16 #
17 # This code is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22
23 import sys
24 import os
25
26 # invert binary string
27 def invert(data):
28 return ''.join('0' if c == '1' else '1' for c in data)
29
30 # do the actual search
31 def search(target,data):
32 location = data.find(target)
33 if location >= 0:
34 print '*** Match at bit %d:' % location,
35 print '%s<%s>%s' % (data[:location],target,data[location+len(target):])
36 else:
37 print 'Not found'
38
39 # convert integer to binary string
40 def binstring(number):
41 return bin(number)[2:] if number > 0 else ''
42
43 # reverse string order
44 def stringreverse(data):
45 return data[::-1]
46
47 # match forward, backward and inverted
48 def domatch(binary,number):
49 reversed= stringreverse(number)
50 inverted= invert(binary)
51
52 print ' Forward: (%s)' % number,
53 search(binary,number)
54 print ' Reverse: (%s)' % reversed,
55 search(binary,reversed)
56 print ' Inverse: (%s)' % inverted
57 print ' Forward: (%s)' % number,
58 search(inverted,number)
59 print ' Reverse: (%s)' % reversed,
60 search(inverted,reversed)
61
62 def main():
63 if(len(sys.argv) < 3):
64 print
65 print '\t'+sys.argv[0] + ' - Search bitstream for a known number'
66 print
67 print 'Usage: ' + sys.argv[0] + ' <NUMBER> <BITSTREAM>'
68 print
69 print '\tNUMBER will be converted to it\'s BINARY equivalent for all valid'
70 print '\tinstances of BINARY, OCTAL, DECIMAL and HEX, and the bitstream'
71 print '\tand it\'s inverse will be searched for a pattern match. Note that'
72 print '\tNUMBER must be specified in BINARY to match leading zeros.'
73 print
74 print 'Example:'
75 print
76 print '\tfindbits.py 73 0110010101110011'
77 print
78 os._exit(True)
79
80 bases= {
81 2:'BINARY',
82 8:'OCTAL',
83 10:'DECIMAL',
84 16:'HEX',
85 }
86
87 for base, base_name in sorted(bases.iteritems()):
88 try:
89 number= int(sys.argv[1],base)
90 print
91 print 'Trying', base_name
92 # do BINARY as specified to preserve leading zeros
93 if base == 2:
94 domatch(sys.argv[1],sys.argv[2])
95 else:
96 domatch(binstring(number),sys.argv[2])
97 except:
98 continue
99
100 if __name__ == '__main__':
101 main()
Impressum, Datenschutz