-
Notifications
You must be signed in to change notification settings - Fork 9
/
shannon_names.py
196 lines (139 loc) · 6.79 KB
/
shannon_names.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
#!/bin/python3
# Samsung Shannon Modem Loader - Name Reconstruction
# This script is autoamtically scheduled by the loader
# Alexander Pick 2024-2025
import idc
import ida_bytes
import ida_nalt
import idautils
import idaapi
import ida_segment
#import ida_search
import ida_ua
import ida_funcs
import re
import os
import shannon_generic
import shannon_funcs
def restore_cpp_names():
idc.msg("[i] trying to reconstruct cpp names from strings\n")
sc = idautils.Strings()
for i in sc:
# step 1 - find a function name
regex = "([a-z]*::[A-Za-z_]*::[A-Za-z_]*)"
if (re.match(regex, str(i))):
shannon_generic.create_name(i.ea, str(i))
# restores the function names of SS related functions from a macro created function structure
def restore_ss_names():
idc.msg("[i] trying to reconstruct ss names from function macros\n")
# step 1 - find a function name
# search only in main to avoid unnecessary long runtimes
seg_t = ida_segment.get_segm_by_name("MAIN_file")
ss_offset = shannon_generic.search_text(
seg_t.start_ea, seg_t.end_ea, "ss_DecodeGmmFacilityMsg")
if (ss_offset != idaapi.BADADDR):
# step 2 - find xrefs to this name, essentially should be just one xref
for xref in idautils.XrefsTo(ss_offset, 0):
# sanity check - validate that xref target is a function, or next
if (idc.get_func_attr(xref.frm, idc.FUNCATTR_START) == idaapi.BADADDR):
continue
# step 3 - iterate over the next instructions until we find a function call
xref_str = None
tries = 0
prev_offset = xref.frm
while (tries < 5):
# forward search, max 5 instructions
xref_str_tmp = idc.next_head(prev_offset)
opcode = ida_ua.ua_mnem(xref_str_tmp)
if (opcode == None):
tries += 1
continue
if ("BL" in opcode):
# shannon_generic.DEBUG("[d] found BL at %x\n" % xref_str_tmp)
# docs said this is a list, but seems to be a generator?
xref_str = next(
idautils.CodeRefsFrom(xref_str_tmp, 0))
break
else:
prev_offset = xref_str_tmp
tries += 1
if (xref_str == None):
continue # abort if not foudn or emptyt list
idc.msg("[i] found verbose ss_ name function: %x\n" % xref_str)
check_failed = 0
# step 4 - iterate over the all calls to this function
for xref_func in idautils.XrefsTo(xref_str, 0):
tries = 0
prev_offset = xref_func.frm
while (tries < 5):
cur_offset = idc.prev_head(prev_offset)
opcode = ida_ua.ua_mnem(cur_offset)
if (opcode == None):
tries += 1
continue
# Thanks John Doe
if ("DR" in opcode and idc.get_operand_value(cur_offset, 0) == 0x0):
# shannon_generic.DEBUG("[d] found LDR at %x\n" % cur_offset)
break
else:
prev_offset = cur_offset
tries += 1
# get LDR param which is the function name
str_addr = idc.get_operand_value(cur_offset, 1)
# read string
func_name = idc.get_strlit_contents(str_addr)
# sanity checks
if (func_name == None):
# shannon_generic.DEBUG("[d] %x: failed sanity check (None)\n" % str_addr)
check_failed = 1
# this in elif to avoid err with undefined bla
elif (len(func_name.decode()) < 8):
# shannon_generic.DEBUG("[d] %x: failed sanity check (length)\n" % str_addr)
check_failed = 1
if check_failed:
# try to resolve ref
func_name = shannon_generic.resolve_ref(str_addr)
if (func_name == None):
# idc.msg(
# "[e] %x: function name not defined\n" % str_addr)
continue
func_name_str = func_name.decode()
# shannon_generic.DEBUG("[d] %x: found function name %s\n" % (str_addr, func_name_str))
if ("ss_" not in func_name_str):
idc.msg("[e] %x: failed to find function name for %x, found '%s' instead\n" % (
str_addr, xref_func.frm, func_name))
continue
# create a string at string offfset
ida_bytes.create_strlit(str_addr, 0, ida_nalt.STRTYPE_C)
func_start = idc.get_func_attr(
xref_func.frm, idc.FUNCATTR_START)
if (func_start != idaapi.BADADDR):
if (len(func_name_str) > 8):
func_name = shannon_funcs.function_find_name(func_name_str)
idaapi.set_name(func_start, func_name)
else:
idc.msg("[e] %x: function name too short: %s" %
(func_start, func_name_str))
else:
# shannon_generic.DEBUG("[d] not a function, searching for start\n")
cur_offset = xref_func.frm
prev_offset = 0
# find func boundaries
tries = 150
while tries:
# possible bailout
tries -= 1
flags = idc.get_func_flags(cur_offset)
opcode = ida_ua.ua_mnem(cur_offset)
if ((flags == -1) and (opcode != None)):
prev_offset = cur_offset
cur_offset = idc.prev_head(cur_offset)
else:
ida_funcs.add_func(
prev_offset, idc.prev_head(str_addr))
idaapi.set_name(
prev_offset, shannon_funcs.function_find_name(func_name_str))
break
#for debugging purpose export SHANNON_WORKFLOW="NO"
if (os.environ.get('SHANNON_WORKFLOW') == "NO"):
idc.msg("[i] running names in standalone mode")