]> git.zerfleddert.de Git - proxmark3-svn/blob - tools/findbits.py
84f4670f6cdb858197557852bace5a46b3718841
[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 import string
26
27 # invert binary string
28 def invert(data):
29 return ''.join('0' if c == '1' else '1' for c in data)
30
31 # do the actual search
32 def search(target,data):
33 location= string.find(data,target)
34 if location >= 0:
35 print '*** Match at bit %d:' % location,
36 print '%s<%s>%s' % (data[:location],target,data[location+len(target):])
37 else:
38 print 'Not found'
39
40 # convert integer to binary string
41 def binstring(number):
42 return bin(number)[2:] if number > 0 else ''
43
44 # reverse string order
45 def stringreverse(data):
46 return data[::-1]
47
48 # match forward, backward and inverted
49 def domatch(number,binary):
50 reversed= stringreverse(number)
51 inverted= invert(binary)
52
53 print ' Forward: (%s)' % number,
54 search(binary,number)
55 print ' Reverse: (%s)' % reversed,
56 search(binary,reversed)
57 print ' Inverse: (%s)' % inverted
58 print ' Forward: (%s)' % number,
59 search(inverted,number)
60 print ' Reverse: (%s)' % reversed,
61 search(inverted,reversed)
62
63 def main():
64 if(len(sys.argv) < 3):
65 print
66 print '\t'+sys.argv[0] + ' - Search bitstream for a known number'
67 print
68 print 'Usage: ' + sys.argv[0] + ' <NUMBER> <BITSTREAM>'
69 print
70 print '\tNUMBER will be converted to it\'s BINARY equivalent for all valid'
71 print '\tinstances of BINARY, OCTAL, DECIMAL and HEX, and the bitstream'
72 print '\tand it\'s inverse will be searched for a pattern match. Note that'
73 print '\tNUMBER must be specified in BINARY to match leading zeros.'
74 print
75 print 'Example:'
76 print
77 print '\tfindbits.py 73 0110010101110011'
78 print
79 os._exit(True)
80
81 bases= {
82 2:'BINARY',
83 8:'OCTAL',
84 10:'DECIMAL',
85 16:'HEX',
86 }
87
88 for base in 2,8,10,16:
89 try:
90 number= int(sys.argv[1],base)
91 print
92 print 'Trying', bases[base]
93 # do BINARY as specified to preserve leading zeros
94 if base == 2:
95 domatch(sys.argv[1],sys.argv[2])
96 else:
97 domatch(binstring(number),sys.argv[2])
98 except:
99 continue
100
101 if __name__ == '__main__':
102 main()
Impressum, Datenschutz