Intel HEX转ASCII

有时候做刷写篡改,有的固件包无法用hexview工具篡改

image

于是根据Intel HEX的原理(https://blog.csdn.net/qfmzhu/article/details/129817980) 写了个方便看明文的小脚本

为了方便知道篡改哪里,顺便输出明文对应的hex位置

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
def parse_hex_and_map_to_plaintext(file_path, output_path):
positions_and_data = []
with open(file_path, 'r') as file:
for line_num, line in enumerate(file):
'''
记录类型:2 个十六进制字符,表示记录的类型:
00:数据记录
01:文件结束记录
02:扩展段地址记录
04:扩展线性地址记录
'''
if line.startswith(":"):
record_type = line[7:9]
if record_type == "00":
data_length = int(line[1:3], 16)
data_start_index = 9
data_end_index = data_start_index + data_length * 2
data_hex = line[data_start_index:data_end_index]

ascii_text = ""
for i in range(0, len(data_hex), 2):
hex_byte = data_hex[i:i+2]
try:
char = chr(int(hex_byte, 16))
ascii_text += char if 32 <= ord(char) <= 126 else '.'
except ValueError:
continue

positions_and_data.append({
"line_num": line_num + 1,
"data_start": data_start_index,
"data_end": data_end_index,
"ascii_text": ascii_text,
"original_hex": data_hex
})

with open(output_path, 'w') as output_file:
for entry in positions_and_data:
output_file.write(f"Line: {entry['line_num']}, Start: {entry['data_start']}, End: {entry['data_end']}, Text: {entry['ascii_text']}, Original HEX: {entry['original_hex']}\n")

file_path = '需要转换的hex文件'
output_path = 'result.txt'

parse_hex_and_map_to_plaintext(file_path, output_path)

print(f"{output_path}")

效果:

image




声明:
本文章用于学习交流,严禁用于非法操作,出现后果一切自行承担,阅读此文章表示你已同意本声明。

Disclaimer:
This article is for study and communication. It is strictly forbidden to use it for illegal operations. All consequences shall be borne by yourself. Reading this article means that you have agreed to this statement.