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