1 {***************************************************************
\r
3 * Program Name: KolPeEdit
\r
4 * Purpose :Portable Executable File Control Demo
\r
6 * Small analytical tool for dependancies etc.
\r
7 * Author :Thaddy de Koning
\r
8 * The control itself is based on code by
\r
9 * (Cr)Sergi /e!MiNENCE team
\r
10 * History :Version 1.00m 31-10-2002
\r
11 * Remarks :As usual in my case, no mck
\r
12 * This is just a viewer, but the PE control
\r
13 * allows write operations.
\r
14 * This is risky, so i didn't implement it here.
\r
15 * If you know what you're doing, you know where to look,
\r
16 * if you don't, you don't :)
\r
17 ****************************************************************}
\r
20 {Created by KOL project Expert on:31-10-2002 11:59:09}
\r
26 KOLPEEditor in 'KOLPEEditor.pas';
\r
29 App='Kol PE Viewer';
\r
51 PMyEvents=^TMyEvents;
\r
52 TMyEvents=object(Tobj)
\r
54 {Add your eventhandlers here, example:}
\r
55 function DoMessage(var Msg:Tmsg;var Rslt:integer):boolean;
\r
56 procedure ButtonLoadClick(Sender:pObj);
\r
57 procedure Listboxchange(sender:pObj);
\r
58 procedure ListViewSortclick(sender:pControl;Index:integer);
\r
59 procedure TabsClick(sender:pObj);
\r
63 function TMyEvents.DoMessage(var Msg:TMsg;var Rslt:integer):Boolean;
\r
68 function NewEvents:pMyEvents;
\r
73 procedure TmyEvents.ButtonLoadClick(Sender:pObj);
\r
77 Directory:PDirectory;
\r
78 Dialog:pOpenSaveDialog;
\r
81 case toolbar.CurIndex of
\r
85 Dialog:=NewOpenSaveDialog('','',[]);
\r
86 Dialog.Filter:='Executable Files|*.exe;*.dll;*.ocx;*.cpl';
\r
87 if not Refreshing then begin
\r
88 if not Dialog.execute then exit;
\r
89 PEFilePath:=Dialog.FileName;
\r
90 Applet.caption:=App+' - ['+extractfilename(Dialog.Filename)+']';
\r
91 if PEFilePath='' then
\r
93 MsgOk(extractfilename(Dialog.filename)+' is not a valid PE file!');
\r
99 Edit1.Text:=Format('$%.8X', [EntryPoint]);
\r
100 Edit2.Text:=Format('$%.8X', [ImageBase]);
\r
101 Edit3.Text:=Format('$%.8X', [ImageSize]);
\r
108 For i:=0 to SectionList.Count-1 do
\r
110 LvAdd('',0,[],0,0,0);
\r
111 Section:=PSection(SectionList.Items[i]);
\r
112 LvItems[i,0]:=Section.o_name;
\r
113 LvItems[i,1]:=Format('$%.8X', [Section.o_rva]);
\r
114 LvItems[i,2]:=Format('$%.8X', [Section.o_virtual_size]);
\r
115 LvItems[i,3]:=Format('$%.8X', [Section.o_physical_offs]);
\r
116 LvItems[i,4]:=Format('$%.8X', [Section.o_physical_size]);
\r
117 LvItems[i,5]:=Format('$%.8X', [Section.o_flags]);
\r
127 Directory:=PDirectory(DirectoryList.Items[0]);
\r
128 LvAdd('',0,[],0,0,0);
\r
129 LvItems[lvcount-1,0]:='Export table';
\r
130 LvItems[lvcount-1,1]:=Format('$%.8X', [Directory.RVA]);
\r
131 LvItems[lvcount-1,2]:=Format('$%.8X', [Directory.Size]);
\r
133 Directory:=PDirectory(Directorylist.items[1]);
\r
134 LvAdd('',0,[],0,0,0);
\r
135 LvItems[lvcount-1,0]:='Import table';
\r
136 LvItems[lvcount-1,1]:=Format('$%.8X', [Directory.RVA]);
\r
137 LvItems[lvcount-1,2]:=Format('$%.8X', [Directory.Size]);
\r
139 Directory:=PDirectory(Directorylist.items[2]);
\r
140 LvAdd('',0,[],0,0,0);
\r
141 LvItems[lvcount-1,0]:='Resource';
\r
142 LvItems[lvcount-1,1]:=Format('$%.8X', [Directory.RVA]);
\r
143 LvItems[lvcount-1,2]:=Format('$%.8X', [Directory.Size]);
\r
145 Directory:=PDirectory(Directorylist.items[3]);
\r
146 LvAdd('',0,[],0,0,0);
\r
147 LvItems[lvcount-1,0]:='Exception';
\r
148 LvItems[lvcount-1,1]:=Format('$%.8X', [Directory.RVA]);
\r
149 LvItems[lvcount-1,2]:=Format('$%.8X', [Directory.Size]);
\r
151 Directory:=PDirectory(Directorylist.items[4]);
\r
152 LvAdd('',0,[],0,0,0);
\r
153 LvItems[lvcount-1,0]:='Security';
\r
154 LvItems[lvcount-1,1]:=Format('$%.8X', [Directory.RVA]);
\r
155 LvItems[lvcount-1,2]:=Format('$%.8X', [Directory.Size]);
\r
157 Directory:=PDirectory(Directorylist.items[5]);
\r
158 LvAdd('',0,[],0,0,0);
\r
159 LvItems[lvcount-1,0]:='Relocations';
\r
160 LvItems[lvcount-1,1]:=Format('$%.8X', [Directory.RVA]);
\r
161 LvItems[lvcount-1,2]:=Format('$%.8X', [Directory.Size]);
\r
163 Directory:=PDirectory(Directorylist.items[6]);
\r
164 LvAdd('',0,[],0,0,0);
\r
165 LvItems[lvcount-1,0]:='Debug datas';
\r
166 LvItems[lvcount-1,1]:=Format('$%.8X', [Directory.RVA]);
\r
167 LvItems[lvcount-1,2]:=Format('$%.8X', [Directory.Size]);
\r
169 Directory:=PDirectory(Directorylist.items[7]);
\r
170 LvAdd('',0,[],0,0,0);
\r
171 LvItems[lvcount-1,0]:='Description';
\r
172 LvItems[lvcount-1,1]:=Format('$%.8X', [Directory.RVA]);
\r
173 LvItems[lvcount-1,2]:=Format('$%.8X', [Directory.Size]);
\r
175 Directory:=PDirectory(Directorylist.items[8]);
\r
176 LvAdd('',0,[],0,0,0);
\r
177 LvItems[lvcount-1,0]:='Global PTR';
\r
178 LvItems[lvcount-1,1]:=Format('$%.8X', [Directory.RVA]);
\r
179 LvItems[lvcount-1,2]:=Format('$%.8X', [Directory.Size]);
\r
181 Directory:=PDirectory(Directorylist.items[9]);
\r
182 LvAdd('',0,[],0,0,0);
\r
183 LvItems[lvcount-1,0]:='TLS table';
\r
184 LvItems[lvcount-1,1]:=Format('$%.8X', [Directory.RVA]);
\r
185 LvItems[lvcount-1,2]:=Format('$%.8X', [Directory.Size]);
\r
187 Directory:=PDirectory(Directorylist.items[10]);
\r
188 LvAdd('',0,[],0,0,0);
\r
189 LvItems[lvcount-1,0]:='Load config';
\r
190 LvItems[lvcount-1,1]:=Format('$%.8X', [Directory.RVA]);
\r
191 LvItems[lvcount-1,2]:=Format('$%.8X', [Directory.Size]);
\r
193 Directory:=PDirectory(Directorylist.items[11]);
\r
194 LvAdd('',0,[],0,0,0);
\r
195 LvItems[lvcount-1,0]:='Bound import';
\r
196 LvItems[lvcount-1,1]:=Format('$%.8X', [Directory.RVA]);
\r
197 LvItems[lvcount-1,2]:=Format('$%.8X', [Directory.Size]);
\r
199 Directory:=PDirectory(Directorylist.items[12]);
\r
200 LvAdd('',0,[],0,0,0);
\r
201 LvItems[lvcount-1,0]:='Import address table';
\r
202 LvItems[lvcount-1,1]:=Format('$%.8X', [Directory.RVA]);
\r
203 LvItems[lvcount-1,2]:=Format('$%.8X', [Directory.Size]);
\r
206 for i:=0 to ImportedList.count-1 do
\r
207 ListBox1.Add(PImportedLibrary(ImportedList.Items[i]).Name);
\r
214 for i:=0 to ExportedList.count-2 do
\r
216 LVAdd('',0,[],0,0,0);
\r
217 LvItems[i,0]:=Format('$%.8X', [pExportedFunction(ExportedList.Items[i]).Ordinal]);
\r
218 lvItems[i,1]:=pExportedFunction(Exportedlist.Items[i]).Name;
\r
221 Listbox1.OnChange(@Self);
\r
224 1:msgOk('Not implemented');
\r
234 procedure TMyEvents.ListboxChange(sender: pObj);
\r
238 ImportedFunc:PImportedFunction;
\r
240 with Listview3^, PEEdit^ do
\r
242 If Listbox1.curindex<0 then exit;
\r
245 FuncList:=PImportedLibrary(ImportedList.Items[listbox1.curindex]).FunctionList;
\r
246 for i:=0 to FuncList.Count-1 do
\r
248 ImportedFunc:=PImportedFunction(FuncList.Items[i]);
\r
249 Lvadd('',0,[],0,0,0);
\r
250 lvItems[i,0]:=Format('$%.8X', [ImportedFunc.ordinal,4]);
\r
251 LvItems[i,1]:=ImportedFunc.Name;
\r
257 {This is a callback function for the EnumWindows API call}
\r
258 function ListWindows( hWindow: HWnd;wparam:integer): BOOL;stdcall;
\r
260 s: Array[0..255] of Char;
\r
266 Perform(WM_SETREDRAW,0,0);
\r
267 LvAdd('',0,[],0,0,0);
\r
269 LvItems[ LvCount-1,0] := Format('$%.4X', [hWindow]);
\r
270 {Displays Instance}
\r
271 LvItems[ LvCount-1,1] := Format('$%.8X',
\r
272 [GetWindowLong(hWindow, GWL_HINSTANCE)]);
\r
274 LvItems[ LvCount-1,2] := Format('$%.4X',
\r
275 [GetWindowTask(hWindow)]);
\r
277 LvItems[ LvCount-1,3] := Format('$%.4X',
\r
278 [GetParent(hWindow)]);
\r
279 {Obtains and Displays Windows ClassName}
\r
280 GetClassName( HWindow, s, sizeof(s));
\r
282 LvItems[ LvCount-1,4] := s;
\r
284 {Obtains and Displays Window Caption, if any,
\r
285 If there's no caption contenttext is displayed}
\r
286 GetWindowText( HWindow, s, Sizeof(s));
\r
288 LvItems[ LvCount-1,5] := s;
\r
290 {Displays visibility}
\r
291 If IsWindowVisible( Hwindow ) Then
\r
292 LvItems[LvCount-1,6] := 'Yes'
\r
294 LvItems[LvCount-1,6] := 'No';
\r
296 {Displays Iconicity}
\r
297 If IsIconic( Hwindow ) Then
\r
298 LvItems[LvCount-1,7] := 'Yes'
\r
300 LvItems[LvCount-1,7] := 'No';
\r
301 Perform(WM_SETREDRAW,1,0);
\r
306 procedure TMyEvents.ListViewSortclick(sender: pControl; Index: integer);
\r
308 sender.LVSortColumn(index);
\r
311 procedure TMyEvents.TabsClick(sender: pObj);
\r
313 {Update active window list}
\r
314 if Tabcontrol.Curindex=4 then
\r
316 Listview5.beginupdate;
\r
317 EnumWindows( @ListWindows, Applet.handle);
\r
318 Listview5.endupdate;
\r
323 {var 'Applet' is already declared in KOL.pas}
\r
324 Applet:=NewForm(nil,app).SetSize(600,400).CenterOnParent;
\r
325 Applet.font.fontheight:=12;
\r
326 tbpanel:=Newpanel(Applet,esNone).SetAlign(caTop);
\r
327 Toolbar:=NewToolbar(tbpanel,caTop,[tboFlat],HBITMAP(-1),['',''],[7,8]).resizeparent;
\r
328 Toolbar.TBButtonEnabled[1]:=false;
\r
330 Panel:=NewPanel(Applet,esNone).SetAlign(caBottom);
\r
331 Panel.font.fontstyle:=[fsBold];
\r
332 NewLabel(Panel,' PE Header Entry point:').AutoSize(True);
\r
333 Edit1:=NewEditbox(Panel,[]).Placeright.resizeparent;
\r
334 NewLabel(Panel,' Image base:').Placeright.AutoSize(true);
\r
335 Edit2:=NewEditbox(Panel,[]).Placeright;
\r
336 NewLabel(Panel,' Image size:').Placeright.AutoSize(True);
\r
337 Edit3:=NewEditbox(Panel,[]).Placeright;
\r
339 Toolbar.Onclick:=Events.ButtonLoadClick;
\r
340 PeEdit:=NewPeEditor;
\r
341 TabControl:=NewTabControl(Applet,['Sections','Directories','Imports','Exports', 'Active Windows'],[tcoFlat,tcoButtons],nil,0).SetAlign(caClient);
\r
342 TabControl.OnClick:=Events.tabsclick;
\r
343 ListView1:=NewListView(TabControl.pages[0],lvsDetail,[lvoGridlines,lvorowselect],nil,nil,nil).SetAlign(caClient);
\r
344 Listview1.OnColumnClick:=events.ListViewSortclick;
\r
345 with listview1^ do begin
\r
346 LVColAdd('Name',taLeft,Width div 6);
\r
347 LVColAdd('RVA',taLeft,Width div 6);
\r
348 LVColAdd('v Size',taLeft,Width div 6);
\r
349 LVColAdd('p Offset',taLeft,Width div 6);
\r
350 LVColAdd('p Size',taLeft,Width div 6);
\r
351 LvColAdd('ChaRect',taLeft,Width div 6);
\r
354 ListView2:=NewListView(TabControl.pages[1],lvsDetail,[lvoGridlines,lvorowselect],nil,nil,nil).SetAlign(caClient);
\r
355 Listview2.OnColumnClick:=events.ListViewSortclick;
\r
356 with listview2^ do begin
\r
357 LVColAdd('Directory',taLeft,Width div 3);
\r
358 LVColAdd('RVA',taLeft,Width div 3);
\r
359 LVColAdd('Size',taLeft,Width div 3);
\r
362 GroupBox3:=NewGroupBox(Tabcontrol.pages[2],'Libraries').SetAlign(caLeft);
\r
363 ListBox1:=NewListBox(GroupBox3,[]).SetAlign(caClient);
\r
364 GroupBox4:=NewGroupBox(Tabcontrol.Pages[2],'Functions').SetAlign(caClient);
\r
365 ListView3:=NewListView(GroupBox4,lvsDetail,[lvoGridlines,lvorowselect],nil,nil,nil).SetAlign(caClient);
\r
366 Listview3.OnColumnClick:=events.ListViewSortclick;
\r
367 with listview3^ do begin
\r
368 LVColAdd('Ordinal',taLeft,Width div 3);
\r
369 LVColAdd('Name',taLeft,Width - (width div 3));
\r
371 ListBox1.OnChange:=Events.Listboxchange;
\r
373 ListView4:=NewListView(TabControl.Pages[3],lvsDetail,[lvoGridlines,lvorowselect],nil,nil,nil).SetAlign(caClient);
\r
374 Listview4.OnColumnClick:=events.ListViewSortclick;
\r
375 with listview4^ do begin
\r
376 LVColAdd('Ordinal',taLeft,Width div 3);
\r
377 LVColAdd('Name',taLeft,Width - (width div 3));
\r
380 Applet.OnMessage:=Events.DoMessage;
\r
381 ListView5:=NewListView(Tabcontrol.pages[4],lvsdetail,[lvoGridLines,lvoRowSelect],nil,nil,nil).setalign(caClient);
\r
382 Listview1.OnColumnClick:=events.ListViewSortclick;
\r
385 LvColAdd('Handle',taRight,75);
\r
386 LvColAdd('Instance',taRight,75);
\r
387 LvColAdd('Task',taRight,75);
\r
388 LvColAdd('Parent',taright,75);
\r
389 lvColAdd('Class Name',taLeft,200);
\r
390 LvColAdd('Title',taLeft,200);
\r
391 LvColAdd('Visible',taLeft,75);
\r
392 LvColAdd('Iconic',taLeft,75);
\r
394 Listview5.OnColumnClick:=Events.ListViewSortclick;
\r
395 EnumWindows( @ListWindows, Applet.handle);
\r