-
Notifications
You must be signed in to change notification settings - Fork 20
/
KBViewer.pas
372 lines (355 loc) · 10.6 KB
/
KBViewer.pas
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
unit KBViewer;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics,
Controls, Forms, Dialogs, StdCtrls, ExtCtrls;
type
TFKBViewer=class(TForm)
lbKB: TListBox;
lbIDR: TListBox;
Panel3: TPanel;
bPrev: TButton;
bNext: TButton;
btnOk: TButton;
btnCancel: TButton;
lPosition: TLabel;
Splitter1: TSplitter;
edtCurrIdx: TEdit;
lKBIdx: TLabel;
Panel1: TPanel;
cbUnits: TComboBox;
Label1: TLabel;
lblKbIdxs: TLabel;
procedure bPrevClick(Sender : TObject);
procedure bNextClick(Sender : TObject);
procedure btnOkClick(Sender : TObject);
procedure btnCancelClick(Sender : TObject);
procedure FormCreate(Sender : TObject);
procedure edtCurrIdxChange(Sender : TObject);
procedure FormShow(Sender : TObject);
procedure cbUnitsChange(Sender : TObject);
private
{ Private declarations }
UnitsNum:Integer;
CurrIdx:Integer;
CurrAdr:Integer;
public
{ Public declarations }
Position:Integer;
FromIdx:Integer;//First Unit idx
ToIdx:Integer; //Last Unit idx
Procedure ShowCode(adr:Integer; idx:Integer);
end;
Var
FKBViewer:TFKBViewer;
implementation
{$R *.DFM}
Uses Def_know,Def_Disasm,Main,KnowledgeBase,Infos,Heuristic,Misc,Types,Def_main;
procedure TFKBViewer.FormCreate(Sender : TObject);
begin
CurrIdx:=-1;
lbIDR.Canvas.Font.Assign(lbIDR.Font);
lbKB.Canvas.Font.Assign(lbKB.Font);
end;
Procedure TFKBViewer.ShowCode (adr:Integer; idx:Integer);
var
outfixup:Boolean;
n, m, wid, maxwid, row:Integer;
firstProcIdx, lastProcIdx:Integer;
Val, Adres:Integer;
canva:TCanvas;
line,disLine:AnsiString;
DisInfo:TDisInfo;
pInfo:MProcInfo;
_bytes:Integer;
instrLen, _pos:Integer;
curAdr:Integer;
p:PAnsiChar;
fixupType:Char;
fixupOfs:Integer;
len:Word;
fixupName:AnsiString;
Op:Byte;
Begin
CurrIdx := idx;
CurrAdr := adr;
edtCurrIdx.Text := IntToStr(CurrIdx);
lPosition.Caption := IntToStr(CurrIdx - Position);
lbIDR.Clear;
canva := lbIDR.canvas;
maxwid := 0;
for m := 0 To FMain.lbCode.Count-1 do
begin
line := FMain.lbCode.Items[m];
//Ignore first byte (for symbol <)
_bytes := Length(line) - 1;
//If instruction, ignore flags (last byte)
if m<>0 then Dec(_bytes);
//Extract original instruction
line := Copy(line,2, _bytes);
//For first row add prefix IDR:
if m=0 then line := 'IDR:' + line;
lbIDR.Items.Add(line);
wid := canva.TextWidth(line);
if wid > maxwid then maxwid := wid;
End;
lbIDR.ScrollWidth := maxwid + 2;
lbKB.Clear;
row := 0;
maxwid := 0;
if KBase.GetProcInfo(CurrIdx, [INFO_DUMP], pInfo) then
begin
cbUnits.Text := KBase.GetModuleName(pInfo.ModuleID);
KBase.GetProcIdxs(pInfo.ModuleID, @firstProcIdx, @lastProcIdx);
lblKbIdxs.Caption := IntToStr(firstProcIdx) + ' - ' + IntToStr(lastProcIdx);
canva := lbKB.canvas;
line := 'KB:' + pInfo.ProcName;
lbKB.Items.Add(line);
Inc(row);
wid := canva.TextWidth(line);
if wid > maxwid then maxwid := wid;
_pos := 0;
curAdr := adr;
if pInfo.FixupNum<>0 then
begin
p := PAnsiChar(pInfo.Dump) + 2*pInfo.DumpSz;
for n := 0 To pInfo.FixupNum-1 do
begin
fixupType := p^;
Inc(p);
fixupOfs := PInteger(p)^;
Inc(p,4);
len := PWord(p)^;
Inc(p,2);
SetLength(fixupName,len);
StrLCopy(PAnsiChar(fixupName),p,len);
Inc(p,len + 1);
while (_pos <= fixupOfs) and (_pos < pInfo.DumpSz) do
begin
instrLen := frmDisasm.Disassemble(pInfo.Dump + _pos, curAdr, @DisInfo, @disLine);
if instrLen=0 then
begin
lbKB.Items.Add(Val2Str(curAdr,8) + ' ???');
Inc(row);
Inc(_pos);
Inc(curAdr);
continue;
End;
op := frmDisasm.GetOp(DisInfo.Mnem);
line := Val2Str(curAdr,8) + ' ' + disLine;
outfixup := false;
if _pos + instrLen > fixupOfs then
begin
if DisInfo.Call then
begin
line := Val2Str(curAdr,8) + ' call ';
outfixup := true;
end
else if op = OP_JMP then
line := Val2Str(curAdr,8) + ' jmp '
else line:=line + ';';
if not SameText(fixupName, pInfo.ProcName) then line:=line + fixupName
else
begin
Val := PInteger(Code + Adr2Pos(CurrAdr) + fixupOfs)^;
if fixupType = 'J' then
Adres := CurrAdr + fixupOfs + Val + 4
else Adres := Val;
line:=line + Val2Str(Adres,8);
End;
End;
lbKB.Items.Add(line);
if outfixup then lbKB.Selected[row] := true;
Inc(row);
wid := canva.TextWidth(line);
if wid > maxwid then maxwid := wid;
Inc(_pos,instrLen);
Inc(curAdr,instrLen);
End;
End;
End;
while _pos < pInfo.DumpSz do
begin
instrLen := frmDisasm.Disassemble(pInfo.Dump + _pos, curAdr, @DisInfo, @disLine);
if instrLen=0 then
begin
lbKB.Items.Add(Val2Str(curAdr,8) + ' ???');
Inc(_pos);
Inc(curAdr);
continue;
End;
line := Val2Str(curAdr,8) + ' ' + disLine;
lbKB.Items.Add(line);
wid := canva.TextWidth(line);
if wid > maxwid then maxwid := wid;
Inc(_pos,instrLen);
Inc(curAdr,instrLen);
End;
End;
lbKB.ScrollWidth := maxwid + 2;
lbKB.TopIndex := 0;
end;
procedure TFKBViewer.bPrevClick(Sender : TObject);
begin
if CurrIdx <> -1 then ShowCode(CurrAdr, CurrIdx - 1);
end;
procedure TFKBViewer.bNextClick(Sender : TObject);
begin
if CurrIdx <> -1 then ShowCode(CurrAdr, CurrIdx + 1);
end;
procedure TFKBViewer.btnOkClick(Sender : TObject);
Var
m, k1, k2, ap, _pos, pos1, pos2, Idx, val:Integer;
adr:Integer;
pInfo:MProcInfo;
recN:InfoRec;
kbName, idrName, kbLine, idrLine:AnsiString;
use:TWordDynArray;
dat:PProcNode;
begin
if KBase.GetProcInfo(CurrIdx, [INFO_DUMP, INFO_ARGS], pInfo) then
begin
adr := CurProcAdr;
ap := Adr2Pos(adr);
if ap < 0 then Exit;
recN := GetInfoRec(adr);
if Not Assigned(recN) Then Exit;
recN.procInfo.DeleteArgs;
FMain.StrapProc(ap, CurrIdx, @pInfo, false, FMain.EstimateProcSize(CurProcAdr));
//Strap all selected items (in IDR list box)
if lbKB.SelCount = lbIDR.SelCount then
begin
k1 := 0;
k2 := 0;
for m := 0 To lbKB.SelCount-1 do
begin
kbLine := '';
idrLine := '';
while k1 < lbKB.Items.Count do
begin
if lbKB.Selected[k1] then
begin
kbLine := lbKB.Items[k1];
Inc(k1);
break;
End;
Inc(k1);
End;
while k2 < lbIDR.Items.Count do
begin
if lbIDR.Selected[k2] then
begin
idrLine := lbIDR.Items[k2];
Inc(k2);
break;
End;
Inc(k2);
End;
if (kbLine <> '') and (idrLine <> '') then
begin
pos1 := Pos('call',kbLine);
pos2 := Pos('call',idrLine);
if (pos1<>0) and (pos2<>0) then
begin
kbName := Trim(Copy(kbLine,pos1 + 4, Length(kbLine)));
idrName := Trim(Copy(idrLine,pos2 + 4, Length(idrLine)));
_pos := Pos(';',idrName);
if _pos<>0 then idrName := Copy(idrName,1, _pos - 1);
adr := 0;
if TryStrToInt('$' + idrName, val) then adr := val;
ap := Adr2Pos(adr);
if (kbName <> '') and (ap >= 0) then
begin
recN := GetInfoRec(adr);
recN.procInfo.DeleteArgs;
use := KBase.GetModuleUses(KBase.GetModuleID(PAnsiChar(cbUnits.Text)));
Idx := KBase.GetProcIdx(use, PAnsiChar(kbName), Code+ap);
if Idx <> -1 then
begin
Idx := KBase.ProcOffsets[Idx].NamId;
if not KBase.IsUsedProc(Idx) then
if KBase.GetProcInfo(Idx, [INFO_DUMP, INFO_ARGS], pInfo) then
FMain.StrapProc(ap, Idx, @pInfo, true, pInfo.DumpSz);
end
else
begin
Idx := KBase.GetProcIdx(use, PAnsiChar(kbName), Nil);
if Idx <> -1 then
begin
Idx := KBase.ProcOffsets[Idx].NamId;
if Not KBase.IsUsedProc(Idx) then
if KBase.GetProcInfo(Idx, [INFO_DUMP, INFO_ARGS], pInfo) then
FMain.StrapProc(ap, Idx, @pInfo, false, FMain.EstimateProcSize(adr));
End;
End;
End;
End;
End;
End;
End;
FMain.RedrawCode;
dat:=FMain.vtProc.GetNodeData(FMain.vtProc.FocusedNode);
Idx:=0;
if Assigned(dat) then Idx:=Dat.adres;
FMain.ShowUnitItems(FMain.GetUnit(CurUnitAdr), 0{FMain.lbUnitItems.TopIndex}, Idx);
End;
Close;
end;
procedure TFKBViewer.btnCancelClick(Sender : TObject);
begin
Close;
end;
procedure TFKBViewer.edtCurrIdxChange(Sender : TObject);
begin
try
ShowCode(CurrAdr, StrToInt(edtCurrIdx.Text));
Except
on E:Exception do Application.ShowException(E);
end;
end;
procedure TFKBViewer.cbUnitsChange(Sender : TObject);
Var
_moduleID:Word;
k, firstProcIdx, lastProcIdx, idx:Integer;
pInfo:MProcInfo;
begin
Position := -1;
_moduleID := KBase.GetModuleID(PAnsiChar(cbUnits.Text));
if _moduleID <> $FFFF then
begin
if KBase.GetProcIdxs(_moduleID, @firstProcIdx, @lastProcIdx) then
begin
edtCurrIdx.Text := IntToStr(firstProcIdx);
lblKbIdxs.Caption := IntToStr(firstProcIdx) + ' - ' + IntToStr(lastProcIdx);
for k := firstProcIdx To lastProcIdx do
begin
idx := KBase.ProcOffsets[k].ModId;
if not KBase.IsUsedProc(idx) then
if KBase.GetProcInfo(idx, [INFO_DUMP, INFO_ARGS], pInfo) then
if MatchCode(Code + Adr2Pos(CurProcAdr), @pInfo) then
begin
edtCurrIdx.Text := IntToStr(idx);
Position := idx;
break;
End;
End;
End;
End;
if Position = -1 then ShowCode(CurProcAdr, firstProcIdx)
else ShowCode(CurProcAdr, Position);
end;
procedure TFKBViewer.FormShow(Sender : TObject);
Var
n,ID:Integer;
p:PAnsiChar;
begin
if UnitsNum=0 then
for n := 0 To KBase.ModuleCount-1 do
begin
ID := KBase.ModuleOffsets[n].NamId;
p := KBase.GetKBCachePtr(KBase.ModuleOffsets[ID].Offset, KBase.ModuleOffsets[ID].Size);
cbUnits.Items.Add(String(p + 4));
Inc(UnitsNum);
End;
end;
end.