-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gdbnew.py
226 lines (202 loc) · 8.71 KB
/
.gdbnew.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
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
import gdb
import struct
import re
class TISCommand(gdb.Command):
"""Display stack or heap in a custom format."""
COLORS = [
'\033[93m', # Yellow
'\033[92m', # Green
'\033[94m', # Blue
'\033[91m', # Red
'\033[95m', # Magenta
'\033[96m', # Cyan
]
RESET = '\033[0m'
def __init__(self):
super(TISCommand, self).__init__("tis", gdb.COMMAND_USER)
def invoke(self, arg, from_tty):
if arg:
args = gdb.string_to_argv(arg)
if len(args) == 1:
if args[0] == "heap":
self.display_heap(50) # Default to 40 lines if no number is provided
return
else:
try:
lines = int(args[0])
rsp_val = int(gdb.parse_and_eval("$rsp"))
start_address = max(rsp_val, 0) # Minimum address is 0
self.display_memory(start_address-0x30, lines)
return
except ValueError:
gdb.write("show with another format:\n")
start_address = self.parse_address(args[0])
if start_address is None:
gdb.write("Invalid address format.\n")
return
lines = 12 # Default count
elif len(args) == 2:
if args[0] == "heap":
try:
lines = int(args[1])
self.display_heap(lines)
except ValueError:
gdb.write("Invalid number of lines.\n")
return
start_address = self.parse_address(args[0])
if start_address is None:
gdb.write("Invalid address format.\n")
return
try:
lines = int(args[1])
except ValueError:
gdb.write("Invalid number of lines.\n")
return
else:
gdb.write("Usage: tis <address> [lines] or tis heap [lines]\n")
return
else:
rsp_val = int(gdb.parse_and_eval("$rsp"))
start_address = max(rsp_val, 0) # Minimum address is 0
lines = 12
try:
gdb.selected_inferior().read_memory(start_address-0x30, 0x10)
self.display_memory(start_address-0x30, lines)
return
except gdb.MemoryError as e:
gdb.selected_inferior().read_memory(start_address, 0x10)
self.display_memory(start_address, lines)
return
except gdb.MemoryError as e:
gdb.write(f"Error reading memory at 0x{start_address:016x}: {e}\n")
return
def display_memory(self, start_address, lines):
block_color_index = 0
for i in range(lines):
addr = start_address + i * 0x10
offset = addr - start_address
try:
address_str = ""
data = gdb.selected_inferior().read_memory(addr, 0x10)
values = struct.unpack("QQ", bytes(data))
ascii_rep = ''.join(chr(b) if 32 <= b <= 126 else '.' for b in bytes(data))
register_name = self.get_register_name(addr)
if register_name:
address_str += f" -> {register_name}"
address_str = address_str.ljust(8)
if addr == start_address+0x30:
address_str += f" <- here"
hex_str = f"0x{values[0]:016x}\t0x{values[1]:016x}{self.RESET}"
ascii_str = f"{ascii_rep}{self.RESET}"
offaddr = f"0x{offset:05x} | {hex(addr)}"
else:
hex_str = f"{self.COLORS[block_color_index]}0x{values[0]:016x}\t0x{values[1]:016x}{self.RESET}"
ascii_str = f"{self.COLORS[block_color_index]}{ascii_rep}{self.RESET}"
offaddr = f"{self.COLORS[block_color_index]}0x{offset:05x} | {hex(addr)}"
gdb.write(f"{offaddr} | {hex_str}\t{ascii_str}{self.RESET}{address_str}\n")
except gdb.MemoryError as e:
gdb.write(f"Error reading memory at 0x{addr:016x}: {e}\n")
break
except Exception as e:
gdb.write(f"Unexpected error: {e}\n")
break
if (i + 1) % 5 == 0:
block_color_index = (block_color_index + 1) % len(self.COLORS)
def display_heap(self, lines=40):
try:
# Assume the heap boundaries can be determined from the heap segment in the memory mappings
mappings = gdb.execute("info proc mappings", to_string=True)
for line in mappings.splitlines():
if "[heap]" in line:
parts = line.split()
start_address = int(parts[0], 16)
end_address = int(parts[1], 16)
actual_lines = min((end_address - start_address) // 0x10, lines)
self.display_memory(start_address, actual_lines)
# self.count_heap_chunks(start_address, start_address + actual_lines * 0x10)
return
gdb.write("Heap segment not found.\n")
except gdb.error as e:
gdb.write(f"Error retrieving heap segment: {e}\n")
# def count_heap_chunks(self, start_address, end_address):
# chunk_count = 0
# addr = start_address
# try:
# while addr < end_address:
# data = gdb.selected_inferior().read_memory(addr, 0x8)
# chunk_size = struct.unpack("Q", bytes(data))[0] & ~0x7 # Mask out flags in the size
# if chunk_size == 0:
# break
# chunk_count += 1
# addr += chunk_size
# gdb.write(f"Number of heap chunks: {chunk_count}\n")
# except gdb.MemoryError as e:
# gdb.write(f"Error reading memory at 0x{addr:016x}: {e}")
# except Exception as e:
# gdb.write(f"Unexpected error: {e}")
#
def get_register_name(self, address):
frame = gdb.selected_frame()
arch = frame.architecture()
if arch is None:
return None
for reg_desc in arch.registers():
try:
reg_val = frame.read_register(reg_desc.name)
if int(reg_val) == address:
return reg_desc.name
except gdb.error:
continue
return None
def parse_address(self, arg):
try:
if '+' in arg:
base, offset = arg.split('+')
base_val = int(gdb.parse_and_eval(base))
offset_val = int(gdb.parse_and_eval(offset))
return base_val + offset_val
else:
return int(gdb.parse_and_eval(arg))
except gdb.error:
return None
except ValueError:
return None
TISCommand()
class NeCommand(gdb.Command):
def __init__(self):
super(NeCommand, self).__init__("ne", gdb.COMMAND_USER)
def Nefunction(self):
try:
count = 10 # Number of instructions to disassemble
frame = gdb.selected_frame()
arch = frame.architecture()
pc = frame.pc()
instructions = []
current_pc = pc
point = ["jmp", "je", "jne", "jl", "jle", "jg", "jge", "call","cmp","ret"]
# Disassemble until we have enough instructions
while len(instructions) < count:
next_instructions = arch.disassemble(current_pc, current_pc + 16)
if not next_instructions:
break
instructions.extend(next_instructions)
current_pc = next_instructions[-1]['addr'] + next_instructions[-1]['length']
for i in range(count-1):
nexti = instructions[i+1]['asm'].split()[0]
# print(instructions[i]['asm'])
if nexti in point:
gdb.execute(f"ni {i+1}")
# print(i+1)
# print(instructions[i+1]['asm'])
return
gdb.execute(f"ni")
except:
gdb.write("program is stopped !!\n")
# for ins in instructions[1:count+1]: # Start from the second instruction and limit to 'count' instructions
# asm_parts = ins['asm'].split()
# asm = asm_parts[0] if asm_parts else "" # Extract the mnemonic instruction
# marker = "=>" if ins['addr'] == pc else " "
# gdb.write(f"{marker} {asm}\n")
def invoke(self, arg, from_tty):
self.Nefunction()
NeCommand()