forked from bl4de/security-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhexview.py
More file actions
executable file
·245 lines (204 loc) · 8.22 KB
/
Copy pathhexview.py
File metadata and controls
executable file
·245 lines (204 loc) · 8.22 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/python
"""
hexview.py - hex dump of any file
based on: "Tutorial: Making your own Hex Dump Program" by DrapsTV
https://www.youtube.com/watch?v=B8nRrw_M_nk&index=1&list=WL
"""
import argparse
import os
import itertools
ASCII = 'ascii'
CTRL = 'ctrl'
OTHER = 'other'
COLORS = {
"black": '\33[0;30m\33[40m',
"white": '\33[0;37m\33[40m',
"red": '\33[0;31m\33[40m',
"green": '\33[0;32m\33[40m',
"green_bg": '\33[1;32m\33[41m',
"yellow": '\33[0;33m\33[40m',
"yellow_bg": '\33[1;33m\33[41m',
"blue": '\33[0;34m\33[40m',
"magenta": '\33[0;35m\33[40m',
"magenta_bg": '\33[1;35m\33[41m',
"cyan": '\33[0;36m\33[40m',
"grey": '\33[0;90m\33[40m',
"lightgrey": '\33[0;37m\33[40m',
"lightblue": '\33[0;94m\33[40m'
}
def char_type(c):
"""
Returns char type depends on its ASCII code
"""
if 32 < ord(c) < 128:
return ASCII
if ord(c) <= 16:
return CTRL
return OTHER
def make_color(c, df_c=False):
"""
Formats color for byte depends on if it's printable ASCII
"""
# if character is \0 added for padding return immediately as black
if c == "\0":
return "{}{:02X}{}".format(
COLORS['black'], ord(c), COLORS['white'])
# for file diff - if characters are different, use bg color for char:
diff = (c != df_c) if df_c != False else False
# printable ASCII:
if char_type(c) == ASCII:
if diff:
retval = "{}{:02X}{}".format(
COLORS['green_bg'], ord(c), COLORS['white'])
else:
retval = "{}{:02X}{}".format(
COLORS['green'], ord(c), COLORS['white'])
if char_type(c) == OTHER:
if diff:
retval = "{}{:02X}{}".format(
COLORS['yellow_bg'], ord(c), COLORS['white'])
else:
retval = "{}{:02X}{}".format(
COLORS['yellow'], ord(c), COLORS['white'])
if char_type(c) == CTRL:
if diff:
retval = "{}{:02X}{}".format(
COLORS['magenta_bg'], ord(c), COLORS['white'])
else:
retval = "{}{:02X}{}".format(
COLORS['magenta'], ord(c), COLORS['white'])
return retval
def format_text(c):
"""
Formats color for character depends on if it's printable ASCII
"""
if char_type(c) == ASCII:
retval = "{}{}{}".format(COLORS['lightblue'], c, COLORS['white'])
if char_type(c) == CTRL:
retval = "{}.{}".format(COLORS['magenta'], COLORS['white'])
if char_type(c) == OTHER:
retval = "{}.{}".format(COLORS['yellow'], COLORS['white'])
return retval
def format_chunk(chunk, start, stop, df_chunk=False, dec=False):
"""
Formats one full chunk (byte)
"""
# add padding if chunk is shorter than 16 characters, same for df_chunk
if len(chunk) < 16:
chunk += "\0" * (16 - len(chunk))
if df_chunk and len(df_chunk) < 16:
df_chunk += "\0" * (16 - len(df_chunk))
if dec:
if df_chunk:
return " ".join("{}:{}{:#04}{} ".format(make_color(c, df_c), COLORS['grey'],
ord(c), COLORS['white']) for c, df_c in itertools.izip(chunk[start:stop], df_chunk[start:stop]))
return " ".join("{}:{}{:#04}{} ".format(make_color(c), COLORS['grey'],
ord(c), COLORS['white']) for c in chunk[start:stop])
else:
if df_chunk:
return " ".join("{} ".format(make_color(c, df_c)) for c, df_c in itertools.izip(chunk[start:stop],df_chunk[start:stop]))
return " ".join("{} ".format(make_color(c)) for c in chunk[start:stop])
def extract_shellcode(start, end, read_binary):
"""
Extract shellcode in hex format, eg. for C exploits, from given range of bytes
"""
read_binary.seek(start)
shellcode = ""
s = read_binary.read(end - start)
for c in s:
if ord(c) == 0:
shellcode = shellcode + "{}".format(COLORS['red']) + str(
hex(ord(c))).replace("0x", "\\x") + "{}".format(COLORS['white'])
else:
shellcode = shellcode + "{}".format(COLORS['yellow']) + str(
hex(ord(c))).replace("0x", "\\x") + "{}".format(COLORS['white'])
print "\n{}[+] Shellcode extracted from byte(s) {:#08x} to {:#08x}:{}".format(COLORS['cyan'], start, end, COLORS['white'])
print "\n{}\n".format(shellcode)
if __name__ == "__main__":
"""
main program routine
"""
__FROM = 0
__TO = 0
parser = argparse.ArgumentParser()
parser.add_argument("file", help="Specify a file")
parser.add_argument(
"-d", "--decimal", help="Display DEC values with HEX", action="store_true")
parser.add_argument(
"-s", "--start", help="Start byte")
parser.add_argument(
"-e", "--end", help="End byte")
parser.add_argument(
"-D", "--diff", help="Perform diff with FILENAME")
parser.add_argument(
"-S", "--shellcode", help="Extract shellcode (-s and -e has to be passed)", action="store_true")
args = parser.parse_args()
b = 16
# for -D / --diff - second file has to be opened
if args.diff:
diff_file = open(args.diff, 'rb')
if args.file:
with open(args.file, 'rb') as infile:
if args.start > -1 and args.end and (int(args.start, 16) > -1 and int(args.end, 16) > int(args.start, 16)):
__FROM = int(args.start, 16)
__TO = int(args.end, 16)
else:
__TO = os.path.getsize(args.file)
if args.shellcode and __FROM > -1 and __TO:
extract_shellcode(__FROM, __TO, infile)
infile.seek(__FROM)
if args.diff:
diff_file.seek(__FROM)
offset = __FROM
print "{}[+] Hex dump: {}\n".format(COLORS['cyan'], COLORS['white'])
while offset < __TO:
chunk = infile.read(b)
if args.diff:
df_chunk = diff_file.read(b)
else:
df_chunk = False
if len(chunk) == 0:
break
text = str(chunk)
text = ''.join([format_text(i) for i in text])
output = "{}{:#08x}{}".format(
COLORS['cyan'], offset, COLORS['white']) + ": "
output += format_chunk(chunk, 0, 4,
df_chunk, args.decimal) + " | "
output += format_chunk(chunk, 4, 8,
df_chunk, args.decimal) + " | "
output += format_chunk(chunk, 8, 12,
df_chunk, args.decimal) + " | "
output += format_chunk(chunk, 12, 16, df_chunk, args.decimal)
if args.diff:
df_text = str(df_chunk)
df_text = ''.join([format_text(i) for i in df_text])
df_output = " " + \
format_chunk(df_chunk, 0, 4, chunk,
args.decimal) + " | "
df_output += format_chunk(df_chunk,
4, 8, chunk, args.decimal) + " | "
df_output += format_chunk(df_chunk,
8, 12, chunk, args.decimal) + " | "
df_output += format_chunk(df_chunk, 12,
16, chunk, args.decimal) + df_text
if len(chunk) % b != 0:
if args.decimal:
output += " " * (((b * 2) - 4 - len(chunk))) + text
if args.diff:
df_output += " " * \
(((b * 2) - 4 - len(df_chunk))) + df_text
else:
output += " " * (b + 4 - len(chunk)) + text
if args.diff:
df_output += " " * \
(b + 4 - len(df_chunk)) + df_text
else:
output += " " + text
if args.diff:
output += df_output
print output
offset += 16
print
else:
print parser.usage