]> git.zerfleddert.de Git - proxmark3-svn/blob - tools/findbits.py
simplified findbits.invert()
[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 out= ''
43 while number > 0:
44 out += chr(0x30 + (number & 0x01))
45 number= number >> 1
46 return stringreverse(out)
47
48 # reverse string order
49 def stringreverse(data):
50 out= ''
51 for x in range(len(data) -1,-1,-1):
52 out += data[x]
53 return out
54
55 # match forward, backward and inverted
56 def domatch(number,binary):
57 reversed= stringreverse(number)
58 inverted= invert(binary)
59
60 print ' Forward: (%s)' % number,
61 search(binary,number)
62 print ' Reverse: (%s)' % reversed,
63 search(binary,reversed)
64 print ' Inverse: (%s)' % inverted
65 print ' Forward: (%s)' % number,
66 search(inverted,number)
67 print ' Reverse: (%s)' % reversed,
68 search(inverted,reversed)
69
70 def main():
71 if(len(sys.argv) < 3):
72 print
73 print '\t'+sys.argv[0] + ' - Search bitstream for a known number'
74 print
75 print 'Usage: ' + sys.argv[0] + ' <NUMBER> <BITSTREAM>'
76 print
77 print '\tNUMBER will be converted to it\'s BINARY equivalent for all valid'
78 print '\tinstances of BINARY, OCTAL, DECIMAL and HEX, and the bitstream'
79 print '\tand it\'s inverse will be searched for a pattern match. Note that'
80 print '\tNUMBER must be specified in BINARY to match leading zeros.'
81 print
82 print 'Example:'
83 print
84 print '\tfindbits.py 73 0110010101110011'
85 print
86 os._exit(True)
87
88 bases= {
89 2:'BINARY',
90 8:'OCTAL',
91 10:'DECIMAL',
92 16:'HEX',
93 }
94
95 for base in 2,8,10,16:
96 try:
97 number= int(sys.argv[1],base)
98 print
99 print 'Trying', bases[base]
100 # do BINARY as specified to preserve leading zeros
101 if base == 2:
102 domatch(sys.argv[1],sys.argv[2])
103 else:
104 domatch(binstring(number),sys.argv[2])
105 except:
106 continue
107
108 if __name__ == '__main__':
109 main()
Impressum, Datenschutz