-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitman.py
executable file
·139 lines (123 loc) · 4.71 KB
/
bitman.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/python
##############################################################################
#
# bitman.py - bitwise file viewer
#
# Copyright (c) 2015, Cecill Etheredge, ijsf - http://ijsf.nl/
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the organization nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
##############################################################################
import sys
import argparse
import struct
import colorama
from termcolor import colored
def main(argv):
#
# Prints bits, LSB first
#
# Originally published by user97370 at: http://stackoverflow.com/questions/2576712/using-python-how-can-i-read-the-bits-in-a-byte
#
def bits(f):
bytes = (ord(b) for b in f.read())
for b in bytes:
for i in xrange(8):
yield (b >> i) & 1
def autoint(x):
return int(x or 0, 0)
# Initialize
colorama.init()
# Parse arguments
parser = argparse.ArgumentParser()
parser.add_argument("input", help = "Input file");
parser.add_argument("-o", "--output", help = "Output file");
parser.add_argument("-p", "--pattern", help = "Binary pattern to highlight");
parser.add_argument("-g", "--grid", help = "Enables grid coloring of every 8th bit", action="store_true");
parser.add_argument("-s", "--skip", help = "Amount of bits to skip (decimal or hexadecimal)", type=autoint, default=0);
parser.add_argument("-l", "--length", help = "Amount of bits to print (decimal or hexadecimal)", type=autoint, default=0);
parser.add_argument("-c", "--columns", help = "Column width or amount of bits to print on one line", type=int, default=80);
args = parser.parse_args()
try:
# File handling
fileInput = open(args.input, "rb")
if not fileInput:
raise IOError("Input file '%s' could not be opened" % args.input)
fileOutput = None
if args.output:
fileOutput = open(args.output, "wb")
if not fileOutput:
raise IOError("Output file '%s' could not be opened" % args.output)
# Variables
hl = ''
hlLength = args.pattern and (len(args.pattern) + 1)
p = 0
c = 0
buf = 0
bufCnt = 0
# Consume file, bit by bit
for b in bits(fileInput):
if p >= args.skip and (p < (args.length + args.skip) or args.length == 0):
# Keep pattern matching string
if hlLength:
hl += str(b)
if len(hl) == hlLength:
hl = hl[1:hlLength]
# Check if column width was exceeded
if c >= args.columns:
# New line
sys.stdout.write('\n')
c = 0
# Print location data for every first bit on newline
if c == 0:
sys.stdout.write(colored(str('0x%08X + 0x%04X ' % (p - args.skip, args.skip)), 'white'))
# Color handling
if args.pattern and hl == args.pattern:
# Pattern found, mark last bit with green
sys.stdout.write(colored(str(b), 'green'))
else:
if args.grid and (c % 8) == 0:
# Grid bit, mark with white
sys.stdout.write(colored(str(b), 'white'))
else:
# Regular bit, mark with magenta
sys.stdout.write(colored(str(b), 'magenta'))
# Write to output file, if necessary
if fileOutput:
if bufCnt == 8:
fileOutput.write(struct.pack('B',buf))
buf = 0
bufCnt = 0
buf = buf | (b << bufCnt)
bufCnt += 1
c += 1
p += 1
sys.stdout.write('\n')
finally:
if fileInput:
fileInput.close()
if fileOutput:
fileOutput.close()
if __name__ == "__main__":
main(sys.argv)