forked from korcankaraokcu/PINCE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptUtils.py
More file actions
117 lines (105 loc) · 3.83 KB
/
Copy pathScriptUtils.py
File metadata and controls
117 lines (105 loc) · 3.83 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
# -*- coding: utf-8 -*-
import gdb
import struct
import sys
# This is some retarded hack
gdbvalue = gdb.parse_and_eval("$PINCE_PATH")
PINCE_PATH = gdbvalue.string()
sys.path.append(PINCE_PATH) # Adds the PINCE directory to PYTHONPATH to import libraries from PINCE
import SysUtils
import type_defs
INDEX_BYTE = type_defs.VALUE_INDEX.INDEX_BYTE
INDEX_2BYTES = type_defs.VALUE_INDEX.INDEX_2BYTES
INDEX_4BYTES = type_defs.VALUE_INDEX.INDEX_4BYTES
INDEX_8BYTES = type_defs.VALUE_INDEX.INDEX_8BYTES
INDEX_FLOAT = type_defs.VALUE_INDEX.INDEX_FLOAT
INDEX_DOUBLE = type_defs.VALUE_INDEX.INDEX_DOUBLE
INDEX_STRING = type_defs.VALUE_INDEX.INDEX_STRING
INDEX_AOB = type_defs.VALUE_INDEX.INDEX_AOB
index_to_valuetype_dict = type_defs.index_to_valuetype_dict
index_to_struct_pack_dict = type_defs.index_to_struct_pack_dict
inferior = gdb.selected_inferior()
pid = inferior.pid
mem_file = "/proc/" + str(pid) + "/mem"
# This function is used to avoid errors in gdb scripts, because gdb scripts stop working when encountered an error
def issue_command(command, error_message=""):
try:
gdb.execute(command)
except:
if error_message:
error_message = str(error_message)
gdb.execute('echo ' + error_message + '\n')
def read_single_address(address, value_type, length=0, unicode=False, zero_terminate=True):
try:
value_type = int(value_type)
except:
print(str(value_type) + " is not a valid value index")
return ""
try:
address = int(address, 16)
except:
print(str(address) + " is not a valid address")
return ""
packed_data = index_to_valuetype_dict.get(value_type, -1)
if value_type is INDEX_STRING:
try:
expected_length = int(length)
except:
print(str(length) + " is not a valid length")
return ""
if unicode:
expected_length = length * 2
elif value_type is INDEX_AOB:
try:
expected_length = int(length)
except:
print(str(length) + " is not a valid length")
return ""
else:
expected_length = packed_data[0]
data_type = packed_data[1]
FILE = open(mem_file, "rb")
try:
FILE.seek(address)
data_read = FILE.read(expected_length)
except IOError:
FILE.close()
print("Can't access the memory at address " + hex(address) + " or offset " + hex(address + expected_length))
return ""
FILE.close()
if value_type is INDEX_STRING:
if unicode:
returned_string = data_read.decode("utf-8", "replace")
else:
returned_string = data_read.decode("ascii", "replace")
if zero_terminate:
if returned_string.startswith('\x00'):
returned_string = '\x00'
else:
returned_string = returned_string.split('\x00')[0]
return returned_string[0:length]
elif value_type is INDEX_AOB:
return " ".join(format(n, '02x') for n in data_read)
else:
return struct.unpack_from(data_type, data_read)[0]
def set_single_address(address, value_index, value):
try:
address = int(address, 16)
except:
print(str(address) + " is not a valid address")
return
write_data = SysUtils.parse_string(value, value_index)
if write_data is None:
return
if value_index is INDEX_STRING:
write_data = bytearray(write_data, "utf-8", "replace")
elif value_index is INDEX_AOB:
write_data = bytearray(write_data)
else:
data_type = index_to_struct_pack_dict.get(value_index, -1)
write_data = struct.pack(data_type, write_data)
FILE = open(mem_file, "rb+")
# Check SetMultipleAddresses class in GDBCommandExtensions.py to see why we moved away the try/except block
FILE.seek(address)
FILE.write(write_data)
FILE.close()