ghidraxdbg 12.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
ghidraxdbg/__init__.py ADDED
@@ -0,0 +1,17 @@
1
+ ## ###
2
+ # IP: GHIDRA
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ##
16
+
17
+ from . import util, commands, methods, hooks
ghidraxdbg/arch.py ADDED
@@ -0,0 +1,236 @@
1
+ ## ###
2
+ # IP: GHIDRA
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ ##
16
+ from typing import Dict, List, Optional, Tuple
17
+
18
+ from ghidratrace.client import Address, RegVal
19
+
20
+ from . import util
21
+
22
+
23
+ language_map: Dict[str, List[str]] = {
24
+ 'x86_32': ['x86:LE:32:default'],
25
+ 'x86_64': ['x86:LE:64:default']
26
+ }
27
+
28
+ data64_compiler_map: Dict[Optional[str], str] = {
29
+ None: 'pointer64',
30
+ }
31
+
32
+ x86_compiler_map: Dict[Optional[str], str] = {
33
+ 'windows': 'windows',
34
+ 'Cygwin': 'windows',
35
+ 'default': 'windows',
36
+ }
37
+
38
+ default_compiler_map: Dict[Optional[str], str] = {
39
+ 'windows': 'default',
40
+ }
41
+
42
+ windows_compiler_map: Dict[Optional[str], str] = {
43
+ 'windows': 'windows',
44
+ }
45
+
46
+ compiler_map : Dict[str, Dict[Optional[str], str]]= {
47
+ 'DATA:BE:64:default': data64_compiler_map,
48
+ 'DATA:LE:64:default': data64_compiler_map,
49
+ 'x86:LE:32:default': x86_compiler_map,
50
+ 'x86:LE:64:default': x86_compiler_map
51
+ }
52
+
53
+
54
+ def get_arch() -> str:
55
+ try:
56
+ type = str(util.dbg.get_actual_processor_type())
57
+ except Exception as e:
58
+ print(f"Error getting actual processor type: {e}")
59
+ return "Unknown"
60
+ if type == "32":
61
+ return "x86_32"
62
+ if type == "64":
63
+ return "x86_64"
64
+ if type == None:
65
+ return "x86_64"
66
+ return "Unknown"
67
+
68
+
69
+ def get_endian() -> str:
70
+ parm = util.get_convenience_variable('endian')
71
+ if parm != 'auto':
72
+ return parm
73
+ return 'little'
74
+
75
+
76
+ def get_osabi() -> str:
77
+ parm = util.get_convenience_variable('osabi')
78
+ if not parm in ['auto', 'default']:
79
+ return parm
80
+ try:
81
+ os = "Windows" #util.dbg.cmd("vertarget")
82
+ if "Windows" not in os:
83
+ return "default"
84
+ except Exception:
85
+ print("Error getting target OS/ABI")
86
+ pass
87
+ return "windows"
88
+
89
+
90
+ def compute_ghidra_language() -> str:
91
+ # First, check if the parameter is set
92
+ lang = util.get_convenience_variable('ghidra-language')
93
+ if lang != 'auto':
94
+ return lang
95
+
96
+ # Get the list of possible languages for the arch. We'll need to sift
97
+ # through them by endian and probably prefer default/simpler variants. The
98
+ # heuristic for "simpler" will be 'default' then shortest variant id.
99
+ arch = get_arch()
100
+ endian = get_endian()
101
+ lebe = ':BE:' if endian == 'big' else ':LE:'
102
+ if not arch in language_map:
103
+ return 'DATA' + lebe + '64:default'
104
+ langs = language_map[arch]
105
+ matched_endian = sorted(
106
+ (l for l in langs if lebe in l),
107
+ key=lambda l: 0 if l.endswith(':default') else len(l)
108
+ )
109
+ if len(matched_endian) > 0:
110
+ return matched_endian[0]
111
+ # NOTE: I'm disinclined to fall back to a language match with wrong endian.
112
+ return 'DATA' + lebe + '64:default'
113
+
114
+
115
+ def compute_ghidra_compiler(lang: str) -> str:
116
+ # First, check if the parameter is set
117
+ comp = util.get_convenience_variable('ghidra-compiler')
118
+ if comp != 'auto':
119
+ return comp
120
+
121
+ # Check if the selected lang has specific compiler recommendations
122
+ if not lang in compiler_map:
123
+ print(f"{lang} not found in compiler map")
124
+ return 'default'
125
+ comp_map = compiler_map[lang]
126
+ if comp_map == data64_compiler_map:
127
+ print(f"Using the DATA64 compiler map")
128
+ osabi = get_osabi()
129
+ if osabi in comp_map:
130
+ return comp_map[osabi]
131
+ if None in comp_map:
132
+ return comp_map[None]
133
+ print(f"{osabi} not found in compiler map")
134
+ return 'default'
135
+
136
+
137
+ def compute_ghidra_lcsp() -> Tuple[str, str]:
138
+ lang = compute_ghidra_language()
139
+ comp = compute_ghidra_compiler(lang)
140
+ return lang, comp
141
+
142
+
143
+ class DefaultMemoryMapper(object):
144
+
145
+ def __init__(self, defaultSpace: str) -> None:
146
+ self.defaultSpace = defaultSpace
147
+
148
+ def map(self, proc: int, offset: int) -> Tuple[str, Address]:
149
+ space = self.defaultSpace
150
+ return self.defaultSpace, Address(space, offset)
151
+
152
+ def map_back(self, proc: int, address: Address) -> int:
153
+ if address.space == self.defaultSpace:
154
+ return address.offset
155
+ raise ValueError(f"Address {address} is not in process {proc}")
156
+
157
+
158
+ DEFAULT_MEMORY_MAPPER = DefaultMemoryMapper('ram')
159
+
160
+ memory_mappers: Dict[str, DefaultMemoryMapper] = {}
161
+
162
+
163
+ def compute_memory_mapper(lang: str) -> DefaultMemoryMapper:
164
+ if not lang in memory_mappers:
165
+ return DEFAULT_MEMORY_MAPPER
166
+ return memory_mappers[lang]
167
+
168
+
169
+ class DefaultRegisterMapper(object):
170
+
171
+ def __init__(self, byte_order: str) -> None:
172
+ if not byte_order in ['big', 'little']:
173
+ raise ValueError("Invalid byte_order: {}".format(byte_order))
174
+ self.byte_order = byte_order
175
+
176
+ def map_name(self, proc: int, name: str):
177
+ return name
178
+
179
+ def map_value(self, proc: int, name: str, value: int):
180
+ try:
181
+ # TODO: this seems half-baked
182
+ av = value.to_bytes(8, "big")
183
+ except Exception:
184
+ raise ValueError("Cannot convert {}'s value: '{}', type: '{}'"
185
+ .format(name, value, type(value)))
186
+ return RegVal(self.map_name(proc, name), av)
187
+
188
+ def map_name_back(self, proc: int, name: str) -> str:
189
+ return name
190
+
191
+ def map_value_back(self, proc: int, name: str, value: bytes):
192
+ return RegVal(self.map_name_back(proc, name), value)
193
+
194
+
195
+ class Intel_x86_64_RegisterMapper(DefaultRegisterMapper):
196
+
197
+ def __init__(self):
198
+ super().__init__('little')
199
+
200
+ def map_name(self, proc, name):
201
+ if name is None:
202
+ return 'UNKNOWN'
203
+ if name == 'efl':
204
+ return 'rflags'
205
+ if name.startswith('zmm'):
206
+ # Ghidra only goes up to ymm, right now
207
+ return 'ymm' + name[3:]
208
+ return super().map_name(proc, name)
209
+
210
+ def map_value(self, proc, name, value):
211
+ rv = super().map_value(proc, name, value)
212
+ if rv.name.startswith('ymm') and len(rv.value) > 32:
213
+ return RegVal(rv.name, rv.value[-32:])
214
+ return rv
215
+
216
+ def map_name_back(self, proc, name):
217
+ if name == 'rflags':
218
+ return 'eflags'
219
+
220
+
221
+ DEFAULT_BE_REGISTER_MAPPER = DefaultRegisterMapper('big')
222
+ DEFAULT_LE_REGISTER_MAPPER = DefaultRegisterMapper('little')
223
+
224
+ register_mappers = {
225
+ 'x86:LE:632:default': DEFAULT_LE_REGISTER_MAPPER,
226
+ 'x86:LE:64:default': Intel_x86_64_RegisterMapper()
227
+ }
228
+
229
+
230
+ def compute_register_mapper(lang: str)-> DefaultRegisterMapper:
231
+ if not lang in register_mappers:
232
+ if ':BE:' in lang:
233
+ return DEFAULT_BE_REGISTER_MAPPER
234
+ if ':LE:' in lang:
235
+ return DEFAULT_LE_REGISTER_MAPPER
236
+ return register_mappers[lang]