-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpsx_lib_id_finder.py
207 lines (164 loc) · 4.26 KB
/
psx_lib_id_finder.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
# PSX Lib ID finder
# by tomsons26
#
# Scans though sections of the binary
# attempting to find PSX LIB IDs
# shows a list of found IDs
import ida_kernwin
import idautils
import idc
from ida_kernwin import Choose
chooser = None
class MyChoose(Choose):
def __init__(self):
Choose.__init__(self, 'PSX Libraries', [ ["Address", 30], ["Info", 30] ])
self.items = []
self.n = 0
self.icon = 5
def OnInit(self):
self.items = self.process()
return True
def OnGetSize(self):
return len(self.items)
def OnGetLine(self, n):
return self.items[n]
def OnSelectLine(self, n):
idc.jumpto(int(self.items[n][0], 16))
return (Choose.NOTHING_CHANGED, )
def show(self):
return self.Show(False) >= 0
CURADDRESS = 0x0
def GetByte(self):
b = ida_bytes.get_byte(self.CURADDRESS)
self.CURADDRESS = self.CURADDRESS + 1
return b
def TryDecodeID(self, addr):
mode = 0
decoded = ""
self.CURADDRESS = addr
# list from printver 1.11
PSXLIBNAMES = [
"libapi", # pkver handled these
"libc",
"libc2",
"libcard",
"libcd",
"libcomb",
"libetc",
"libgpu",
"libgs",
"libgte",
"libgun",
"libmath",
"libpad",
"lib???", # 13 NOTE printed as libpio in pkver
"libpress",
"libsio",
"libsnd",
"libspu",
"lib???", # 18 NOTE printed as libstd in pkver
"lib???", # 19 NOTE printed as libstr in pkver
"libtap",
"lib???", # 21 NOTE printed as libcomb2 in pkver
"libds",
"lib???", # 23 NOTE printed as libmdl in pkver
"libmcrd",
"libhmd", # added in printver 1.02
"libmcx", # added in printver 1.11
"mcgui", # added in printver 1.11
"safechk", # added in printver 1.13
"mcgui_e", # added in printver 1.13
]
LIBCOUNT = len(PSXLIBNAMES)
MONTHS = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
]
# skip over Ps identifier that we already know matches
b0 = self.GetByte()
b1 = self.GetByte()
# while loop lets me keep this easy to read
while 1:
# get library index
b2 = self.GetByte()
# this byte must not be 0xFF
if b2 == 0xFF:
break
# must be 0 when masked with 0xC0
if (b2 & 0xC0) != 0:
break
index = (b2 & 0x3F)
# get timestamp
b3 = self.GetByte()
b4 = self.GetByte()
b5 = self.GetByte()
# these 3 bytes must not be 0xFF
if b3 == 0xFF or b4 == 0xFF or b5 == 0xFF:
break
year = 1996 + (b3 >> 0x04)
month = (b3 & 0xF)
day = (b4 >> 0x03)
hour = ((b4 & 0x07) << 0x02) | (b5 >> 0x06)
minute = (b5 & 0x3F)
# get version info
b6 = self.GetByte()
b7 = self.GetByte()
# these bytes must not be 0xFF
if b6 == 0xFF or b7 == 0xFF:
break
major1 = (b6 & 0xF0) >> 0x04
minor1 = (b6 & 0x0F)
minor2 = (b7 & 0xF0) >> 0x04
minor3 = (b7 & 0x0F)
# make sure its a known index
if LIBCOUNT > index:
decoded = decoded + str("(%02d) %s " % (index, PSXLIBNAMES[index]))
else:
decoded = decoded + str("(%02d) %s ", index, "lib???")
decoded = decoded + str("[%x.%x.%x.%x]" % (major1, minor1, minor2, minor3))
# at some point dates were no longer written, this can be detected by checking if day is 0
if day != 0:
decoded = decoded + str(" : %s %02d %04d %02d:%02d" % (MONTHS[month - 1], day, year, hour, minute))
break
return decoded
# thanks hexrays
@staticmethod
def FindWrapper(binary_pattern, ea):
if ida_pro.IDA_SDK_VERSION >= 900:
ea = ida_bytes.find_bytes(binary_pattern, ea, flags=ida_bytes.BIN_SEARCH_FORWARD | ida_bytes.BIN_SEARCH_NOSHOW)
else:
ea = idc.find_binary(ea, ida_search.SEARCH_DOWN | ida_search.SEARCH_NEXT | ida_search.SEARCH_NOSHOW, binary_pattern)
return ea
def process(self):
found_ea = ida_ida.inf_get_min_ea() - 1
end_ea = ida_ida.inf_get_max_ea()
list = []
# letter pattern Ps
pattern = "50 73"
while True:
found_ea = self.FindWrapper(pattern, found_ea+1)
if found_ea == idaapi.BADADDR:
break
decoded = self.TryDecodeID(found_ea)
if decoded != "":
list.append("0x%08X,%s\n" % (found_ea, decoded))
idc.set_cmt(found_ea, decoded, 0)
# turn into showable list
return [line.rstrip().split(',') for line in list]
def gather_info():
global chooser
chooser = MyChoose()
chooser.show()
#if __name__ == '__main__':
# gather_info()
gather_info()