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