-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathProcess.cpp
1446 lines (1253 loc) · 44.1 KB
/
Process.cpp
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
8888888b. 888 888 d8b
888 Y88b 888 888 Y8P
888 888 888 888
888 d88P 888d888 .d88b. Y88b d88P 888 .d88b. 888 888 888
8888888P" 888P" d88""88b Y88b d88P 888 d8P Y8b 888 888 888
888 888 888 888 Y88o88P 888 88888888 888 888 888
888 888 Y88..88P Y888P 888 Y8b. Y88b 888 d88P
888 888 "Y88P" Y8P 888 "Y8888 "Y8888888P"
PE Editor & Dissasembler & File Identifier
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Written by Shany Golan.
In januar, 2003.
I have investigated P.E. file format as thoroughly as possible,
But I cannot claim that I am an expert yet, so some of its information
May give you wrong results.
Language used: Visual C++ 6.0
Date of creation: July 06, 2002
Date of first release: unknown ??, 2003
You can contact me: e-mail address: [email protected]
Copyright (C) 2011. By Shany Golan.
Permission is granted to make and distribute verbatim copies of this
Program provided the copyright notice and this permission notice are
Preserved on all copies.
File: Process.cpp (main)
This program was written by Shany Golan, Student at :
Ruppin, department of computer science and engineering University.
*/
#include <windows.h>
#include "resource\resource.h"
#include <commctrl.h>
#include "process.h"
#include "functions.h"
#include <tlhelp32.h>
#include <vdmdbg.h>
#include <psapi.h>
#include "Resize\AnchorResizing.h"
// ================================================================
// ======================== STRUCTS =============================
// ================================================================
typedef struct {
DWORD dwPID;
PROCENUMPROC lpProc;
DWORD lParam;
BOOL bEnd;
} EnumInfoStruct;
// ================================================================
// ===================== GLOBAL VARIABLES =======================
// ================================================================
HWND hWnd;
HWND ProcessWindow;
HWND ListModules;
HWND hwnd;
DWORD FileSize=0;
HBITMAP hMenuBitmap; // bitmap handler for menu
HINSTANCE Original_Hinst; // main window handler
HINSTANCE hInstLib; // psapi dll handler
DWORD_PTR SelectedRow; // selected row
int ModuleIndex; // print order
int ProcessIndex; // print order
bool processflag;
bool Win9x=false; // OS Check
bool PartialDump=false; // partial dump active/not active
char Text[MAX_PATH]="";
// ================================================================
// ===================== PROTOTYPES =============================
// ================================================================
BOOL ( WINAPI *lpfEnumProcessModules) ( HANDLE, HMODULE*, DWORD, LPDWORD );
DWORD ( WINAPI *lpfGetModuleFileNameEx) ( HANDLE, HMODULE , LPTSTR, DWORD );
DWORD ( WINAPI *lpfGetModuleInformation)( HANDLE, HMODULE , LPMODULEINFO, DWORD );
// ================================================================
// ===================== Process Variable Update ==================
// ================================================================
void GetData(HWND Original, HINSTANCE hinst)
{
// Get original handlers of the main window from the main functino
// this will help us to do PE Editor linking
hwnd=Original;
Original_Hinst=hinst;
}
// ================================================================
// ===================== Process dialog window ====================
// ================================================================
BOOL CALLBACK ProcessDlgProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
// This Window Message will close the dialog //
//============================================//
case WM_CLOSE:
{
// Free bitmap handler
DeleteObject(hMenuBitmap);
// free loaded DLL
FreeLibrary(hInstLib);
// free some mem by deleting all info
SendMessage(ProcessWindow,LVM_DELETEALLITEMS,0,0); // delete all info
SendMessage(ListModules,LVM_DELETEALLITEMS,0,0); // delete all info
// reIntialize the controls on the main window
InitializeResizeControls(hwnd);
EndDialog(hWnd,0);
}
break;
// Window Resize Intialize
case WM_SHOWWINDOW:
{
InitializeResizeControls(hWnd);
}
break;
// Window's Controls Resize Intialize
case WM_SIZE:
{
ResizeControls(hWnd);
}
break;
// Playing with the window
case WM_LBUTTONDOWN:
{
ReleaseCapture();
SendMessage(hWnd,WM_NCLBUTTONDOWN,HTCAPTION,0);
}
break;
// This Window Message is the heart of the dialog //
//================================================//
case WM_INITDIALOG:
{
LVCOLUMN LvCol; // Make Coluom struct
OSVERSIONINFO OSVersion; // Operating System struct
char namebuff[255]={0}; // Temp var
char stat[255]={0}; // Temp var
Win9x=false; // are we running on Win9x ?
ModuleIndex=0; // reset index of listview counter
ProcessIndex=0; // reset index of listview counter
// Inialize controls on which point the will be anchor too
SetWindowLongPtr(GetDlgItem(hWnd,IDC_LISTPROC), GWL_USERDATA,ANCHOR_RIGHT | ANCHOR_BOTTOM);
SetWindowLongPtr(GetDlgItem(hWnd,IDC_MODULES), GWL_USERDATA,ANCHOR_RIGHT | ANCHOR_BOTTOM | ANCHOR_NOT_TOP);
// get process listview handle
ProcessWindow=GetDlgItem(hWnd,IDC_LISTPROC); // get the ID of the ListView
// get module listview handle
ListModules=GetDlgItem(hWnd,IDC_MODULES); // get the ID of the ListView
// Set listview properties/Styles
SendMessage(ProcessWindow,LVM_SETEXTENDEDLISTVIEWSTYLE,0,LVS_EX_FULLROWSELECT|LVS_EX_FLATSB|LVS_EX_ONECLICKACTIVATE|LVS_EX_GRIDLINES); // Full row select
SendMessage(ListModules,LVM_SETEXTENDEDLISTVIEWSTYLE,0,LVS_EX_FULLROWSELECT|LVS_EX_FLATSB|LVS_EX_ONECLICKACTIVATE|LVS_EX_GRIDLINES); // Full row select
// Here we put the info on the Coulom headers
// this is not data, only name of each header we like
memset(&LvCol,0,sizeof(LvCol)); // set 0
LvCol.mask=LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM; // Type of mask
LvCol.cx=0x165; // width between each coloum
LvCol.pszText="Loaded Process"; // First Header
SendMessage(ProcessWindow,LVM_INSERTCOLUMN,0,(LPARAM)&LvCol);// Insert/Show the coloum
LvCol.cx=0x43; // width of header
LvCol.pszText="Process ID"; // Next coloum
SendMessage(ProcessWindow,LVM_INSERTCOLUMN,1,(LPARAM)&LvCol); // ...
LvCol.cx=0x48; // width of header
LvCol.pszText="Priority"; // Next coloum
SendMessage(ProcessWindow,LVM_INSERTCOLUMN,2,(LPARAM)&LvCol); // ...
// delete all items at process listview
SendMessage(ProcessWindow,LVM_DELETEALLITEMS,0,0); // delete all info
// intialize the module's ListView
memset(&LvCol,0,sizeof(LvCol)); // set 0
LvCol.mask=LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM;
LvCol.cx=0x100;
LvCol.pszText="Loaded Module";
SendMessage(ListModules,LVM_INSERTCOLUMN,0,(LPARAM)&LvCol); // Insert/Show the coloum
LvCol.cx=0x50; // width of header
LvCol.pszText="Address";
SendMessage(ListModules,LVM_INSERTCOLUMN,1,(LPARAM)&LvCol);
LvCol.cx=0x57; // width of header
LvCol.pszText="Size Of Image";
SendMessage(ListModules,LVM_INSERTCOLUMN,2,(LPARAM)&LvCol);
LvCol.cx=0x49; // width of header
LvCol.pszText="Entry Point";
SendMessage(ListModules,LVM_INSERTCOLUMN,3,(LPARAM)&LvCol);
// Delete all items at module listview
SendMessage(ListModules,LVM_DELETEALLITEMS,0,0);
// Get Operating Sytem Information
OSVersion.dwOSVersionInfoSize = sizeof(OSVersion);
GetVersionEx(&OSVersion);
// check for Win2k and above system
if(OSVersion.dwMajorVersion==5 || OSVersion.dwMajorVersion==6 && OSVersion.dwPlatformId==VER_PLATFORM_WIN32_NT)
{
// Try load Psapi.Dll
hInstLib = LoadLibraryA("PSAPI.DLL");
// Dll Not found
if( hInstLib == NULL )
{
MessageBox(hWnd,"Couldn't Load Psapi.dll","Error Loading DLL",MB_OK|MB_ICONINFORMATION);
FreeLibrary( hInstLib );
return false;
}
// load up the function from the dll
lpfEnumProcessModules = (BOOL(WINAPI *)(HANDLE, HMODULE *,
DWORD, LPDWORD)) GetProcAddress( hInstLib,
"EnumProcessModules" ) ;
// load up the function from the dll
lpfGetModuleFileNameEx =(DWORD (WINAPI *)(HANDLE, HMODULE,
LPTSTR, DWORD )) GetProcAddress( hInstLib,
"GetModuleFileNameExA" ) ;
// load up the function from the dll
lpfGetModuleInformation =(DWORD (WINAPI *)(HANDLE, HMODULE,
LPMODULEINFO, DWORD )) GetProcAddress( hInstLib,
"GetModuleInformation" ) ;
// Win2K is set
Win9x=false;
}
else // Win9x
if(OSVersion.dwMajorVersion==4 && OSVersion.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
Win9x=true; // Win9x is set
// Show Process Information
MainProcess(hWnd,ProcessWindow);
return true; // Always True
}
break;
case WM_NOTIFY:
{
switch(LOWORD(wParam))
{
case IDC_LISTPROC:
{
// Create a menu while clicking right button of mouse
if(((LPNMHDR)lParam)->code == NM_RCLICK)
{
// get selected item
SelectedRow=SendMessage(ProcessWindow,LVM_GETNEXTITEM,(WPARAM)-1,LVNI_FOCUSED); // return item selected
if(SelectedRow!=-1)
{
DWORD PID;
// get Process Id
PID=GetProcessPID((DWORD)SelectedRow);
// delete all items
SendMessage(ListModules,LVM_DELETEALLITEMS,0,0);
ModuleIndex=0; // reset the order
PrintModules(PID); // lets get the modules and show them
processflag=1;
// load the menu and store handle
HMENU hMenu = LoadMenu (NULL, MAKEINTRESOURCE (IDR_PROCESSES));
// we will change onlt the sub menu
HMENU hPopupMenu = GetSubMenu (hMenu, 0);
POINT pt; // pointr struct
// load bitmap to menu item
hMenuBitmap = LoadBitmap(Original_Hinst, MAKEINTRESOURCE(IDB_PROCESS_KILL));
SetMenuItemBitmaps(hPopupMenu, 0, MF_BYPOSITION, hMenuBitmap, hMenuBitmap);
// load bitmap to menu item
hMenuBitmap = LoadBitmap(Original_Hinst, MAKEINTRESOURCE(IDB_PROCESS_DUMP));
SetMenuItemBitmaps(hPopupMenu, 2, MF_BYPOSITION, hMenuBitmap, hMenuBitmap);
// load bitmap to menu item
hMenuBitmap = LoadBitmap(Original_Hinst, MAKEINTRESOURCE(IDB_PARTIAL_DUMP));
SetMenuItemBitmaps(hPopupMenu, 3, MF_BYPOSITION, hMenuBitmap, hMenuBitmap);
// load bitmap to menu item
hMenuBitmap = LoadBitmap(Original_Hinst, MAKEINTRESOURCE(IDB_PROCESS_PRIORITY));
SetMenuItemBitmaps(hPopupMenu, 5, MF_BYPOSITION, hMenuBitmap, hMenuBitmap);
// load bitmap to menu item
hMenuBitmap = LoadBitmap(Original_Hinst, MAKEINTRESOURCE(IDB_PROCESS_REFRESH));
SetMenuItemBitmaps(hPopupMenu, 6, MF_BYPOSITION, hMenuBitmap, hMenuBitmap);
// cerate a default item (bold text)
SetMenuDefaultItem (hPopupMenu, 0, TRUE);
// get mouse position
GetCursorPos (&pt);
// menu will be over all window's running
SetForegroundWindow (hWnd);
// track the menu with the mouse pointers and show the menu/submenu
TrackPopupMenu (hPopupMenu, TPM_LEFTALIGN, pt.x, pt.y, 0, hWnd, NULL);
// menu will be over all window's running
SetForegroundWindow (hWnd);
// kill menu/submenu after we showed it
DestroyMenu (hPopupMenu);
DestroyMenu (hMenu);
}
}
if(((LPNMHDR)lParam)->code == NM_CLICK)
{
// left click on a process
DWORD PID;
// get selected item
SelectedRow=SendMessage(ProcessWindow,LVM_GETNEXTITEM,(WPARAM)-1,(LPARAM)LVNI_FOCUSED); // return item selected
if(SelectedRow!=-1)
{
// get Process ID
PID=GetProcessPID((DWORD)SelectedRow);
if(PID!=0)
{
// show all modules of the process
SendMessage(ListModules,LVM_DELETEALLITEMS,0,0);
ModuleIndex=0; // reset the order
PrintModules(PID); // lets get the modules
}
}
}
}
break;
}
}
// This Window Message will control the dialog //
//==============================================//
case WM_COMMAND:
{
switch(LOWORD(wParam)) // what we press on?
{
case ID_PROCESSES_REFRESHVIEW:
{
// Refresh the entire Processes SnapShot and redraw
// set indexes
ProcessIndex=0;
ModuleIndex=0;
// delete all items
SendMessage(ProcessWindow,LVM_DELETEALLITEMS,0,0); // delete all info
SendMessage(ListModules,LVM_DELETEALLITEMS,0,0);
// start redrawing
MainProcess(hWnd,ProcessWindow);
}
break;
// Set priority to REAL
case ID_PROCESS_REAL:
{
DWORD PID;
HANDLE hProcess;
ProcessIndex=0;
ModuleIndex=0;
PID=GetProcessPID((DWORD)SelectedRow); // Get PID
hProcess = OpenProcess(PROCESS_SET_INFORMATION, false, PID);
if(hProcess!=NULL)
{
// Set the new Piority
SetPriorityClass(hProcess, REALTIME_PRIORITY_CLASS);
}
CloseHandle(hProcess);
SendMessage(ProcessWindow,LVM_DELETEALLITEMS,0,0);
MainProcess(hWnd,ProcessWindow);
}
break;
// Set priority to HIGH
case ID_PROCESS_HIGH:
{
DWORD PID;
HANDLE hProcess;
ProcessIndex=0;
ModuleIndex=0;
PID=GetProcessPID((DWORD)SelectedRow);
hProcess = OpenProcess(PROCESS_SET_INFORMATION, false, PID);
if(hProcess!=NULL)
{
// Set the new Piority
SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS);
}
CloseHandle(hProcess);
SendMessage(ProcessWindow,LVM_DELETEALLITEMS,0,0);
MainProcess(hWnd,ProcessWindow);
}
break;
// Set priority to NORMAL
case ID_PROCESS_NORMAL:
{
DWORD PID;
HANDLE hProcess;
ProcessIndex=0;
ModuleIndex=0;
PID=GetProcessPID((DWORD)SelectedRow);
hProcess = OpenProcess(PROCESS_SET_INFORMATION, false, PID);
if(hProcess!=NULL)
{
// Set the new Priority
SetPriorityClass(hProcess, NORMAL_PRIORITY_CLASS);
}
CloseHandle(hProcess);
SendMessage(ProcessWindow,LVM_DELETEALLITEMS,0,0);
MainProcess(hWnd,ProcessWindow);
}
break;
// Set priority to IDLE
case ID_PROCESS_IDLE:
{
DWORD PID;
HANDLE hProcess;
ProcessIndex=0;
ModuleIndex=0;
PID=GetProcessPID((DWORD)SelectedRow);
hProcess = OpenProcess(PROCESS_SET_INFORMATION, false, PID);
if(hProcess!=NULL)
{
// Set the new Priority
SetPriorityClass(hProcess, IDLE_PRIORITY_CLASS);
}
CloseHandle(hProcess);
SendMessage(ProcessWindow,LVM_DELETEALLITEMS,0,0);
MainProcess(hWnd,ProcessWindow);
}
break;
case ID_KILL_PROCESS:
{
// Killing a process by its PID
DWORD PID=0;
HANDLE hpro=0;
if(!processflag)
{
MessageBox(hWnd,"Select process to kill","Error",MB_OK);
return 0;
}
PID=GetProcessPID((DWORD)SelectedRow); // Get PID
if(PID!=0)
{
// check process access rights
if(!OpenProcess(PROCESS_ALL_ACCESS, 1, PID))
{
MessageBox(hWnd,"Couldn't Open Process For Killing!","Error",MB_OK);
return 0;
}
else
{
// open the process with all access
hpro=OpenProcess(PROCESS_ALL_ACCESS, 1, PID);
// kill the running process
TerminateProcess(hpro, 0);
// clear the process handle
CloseHandle(hpro);
}
}
processflag=0;
// Reset the Process View
SendMessage(ProcessWindow,LVM_DELETEALLITEMS,0,0); // delete all info
SendMessage(ListModules,LVM_DELETEALLITEMS,0,0);
Sleep(2);
// Reset ListView Indexes
ProcessIndex=0;
ModuleIndex=0;
// ReGet all Process and modules
MainProcess(hWnd,ProcessWindow);
}
break;
case ID_DUMP_PROCESS:
{
// set full dump active
PartialDump=false;
// Full Dump, no address/size specified
DumpFull(0,0);
}
break;
case ID_PARTIAL_DUMP:
{
DialogBox(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_PARTIAL_DUMP), hWnd, (DLGPROC)Partial_Dump);
}
break;
}
}
break;
default:
{
return FALSE;
}
}
return TRUE;
}
//==============================================================
//=================== Partial Process Dumper====================
//==============================================================
// Dump only part of a process,
// Size and address are user defined!
BOOL CALLBACK Partial_Dump(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) // what are we doing ?
{
case WM_INITDIALOG:
{
LVITEM LvItem;
HWND WinList;
// Mask the controls to input only user defined chars
MaskEditControl(GetDlgItem(hWnd,IDC_BASE_ADDRESS), "0123456789abcdef\b",TRUE);
MaskEditControl(GetDlgItem(hWnd,IDC_DUMP_SIZE), "0123456789abcdef\b",TRUE);
// Limit the number of chars in the edit box
SendDlgItemMessage(hWnd,IDC_BASE_ADDRESS, EM_SETLIMITTEXT, (WPARAM)8,0);
SendDlgItemMessage(hWnd,IDC_DUMP_SIZE, EM_SETLIMITTEXT, (WPARAM)8,0);
// InitLvItem
memset(&LvItem,0,sizeof(LvItem));
LvItem.mask=LVIF_TEXT;
LvItem.iSubItem=0;
LvItem.pszText=Text;
LvItem.cchTextMax=256;
LvItem.iItem=0;
// Check operating System
if(Win9x==false) // WinNT/2000/XP Partial Dumping
{
WinList=ListModules; // Win2k++ [ read first loaded module]
// Win2K | WinXP Dump Style
SendMessage(WinList,LVM_GETITEMTEXT, 0, (LPARAM)&LvItem);
wsprintf(Text,"%s",LvItem.pszText);
LvItem.iSubItem=2;
SendMessage(WinList,LVM_GETITEMTEXT, 0, (LPARAM)&LvItem);
wsprintf(Text,"%s",LvItem.pszText);
FileSize=StringToDword(Text);
LvItem.iSubItem=1;
SendMessage(WinList,LVM_GETITEMTEXT, 0, (LPARAM)&LvItem);
wsprintf(Text,"%s",LvItem.pszText);
SetDlgItemText(hWnd,IDC_BASE_ADDRESS,Text);
wsprintf(Text,"%08X",FileSize);
SetDlgItemText(hWnd,IDC_DUMP_SIZE,Text);
GetDlgItemText(hWnd,IDC_BASE_ADDRESS,Text,9);
}
else // Win9x Partial Dump
{
LVFINDINFO lvi; // struct for item findinf
DWORD_PTR Index; // return index
// zero the struct
memset(&lvi,0,sizeof(lvi));
LvItem.iSubItem=0;
// get string to search from the process window
SendMessage(ProcessWindow,LVM_GETITEMTEXT, SelectedRow, (LPARAM)&LvItem);
wsprintf(Text,"%s",LvItem.pszText); // get the name of the process
lvi.psz=Text;
LvItem.iSubItem=1;
// search in loaded modules window
Index=SendMessage(ListModules,LVM_FINDITEM, (WPARAM)-1, (LPARAM)&lvi);
SendMessage(ListModules,LVM_GETITEMTEXT, Index, (LPARAM)&LvItem);
SetDlgItemText(hWnd,IDC_BASE_ADDRESS,LvItem.pszText);
DWORD ProcessAddress=StringToDword(LvItem.pszText);
LvItem.iSubItem=2;
SendMessage(ListModules,LVM_GETITEMTEXT, Index, (LPARAM)&LvItem);
SetDlgItemText(hWnd,IDC_DUMP_SIZE,LvItem.pszText);
FileSize=StringToDword(LvItem.pszText);
GetDlgItemText(hWnd,IDC_BASE_ADDRESS,Text,9); // for the partial dumping checking
}
return true;
}
break;
case WM_CLOSE:
{
// Set dump task to full
PartialDump=false;
EndDialog(hWnd,0);
}
break;
case WM_LBUTTONDOWN:
{
ReleaseCapture();
SendMessage(hWnd,WM_NCLBUTTONDOWN,HTCAPTION,0);
}
break;
case WM_PAINT:
{
return false;
}
break;
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case ID_PARTIAL_DUMP_EXIT:
{
// Set dump task to full
PartialDump=false;
EndDialog(hWnd,0);
}
break;
case ID_PARTIAL_DUMP_SAVE:
{
DWORD Size,Addr,OriginalAddr;
char Fsize[9],Address[9];
// get new information from the edit boxes
GetDlgItemText(hWnd,IDC_DUMP_SIZE,Fsize,9);
GetDlgItemText(hWnd,IDC_BASE_ADDRESS,Address,9);
// covnert text info to dwords
OriginalAddr=StringToDword(Text);
Size=StringToDword(Fsize);
Addr=StringToDword(Address);
// check if the dump size is out of process's limits
if(Size>FileSize || Size==0)
{
MessageBox(hWnd,"Size mismatches process's boundery","Notice",MB_OK|MB_ICONINFORMATION);
break;
}
else // check if we are accessing wrong process address
if(Addr<OriginalAddr || Addr>=(OriginalAddr+FileSize))
{
MessageBox(hWnd,"Address mismatches process's boundery","Notice",MB_OK|MB_ICONINFORMATION);
break;
}
else
{
PartialDump=true; // setting the partial dump active
DumpFull(Size,Addr); // dump process with parameters
PartialDump=false; // setting partial dump inactive
EndDialog(hWnd,0); // close the dailog
}
}
break;
}
break;
}
break;
}
return 0;
}
// =============================================================
// ================= Find Running Process ======================
// =============================================================
BOOL WINAPI EnumProcs(PROCENUMPROC lpProc, LPARAM lParam)
{
// The EnumProcs function takes a pointer to a callback function
// that will be called once per process with the process filename
// and process ID.
//
// lpProc -- Address of callback routine.
//
// lParam -- A user-defined LPARAM value to be passed to
// the callback routine.
//
// Callback function definition:
// BOOL CALLBACK Proc(DWORD dw, WORD w, LPCSTR lpstr, LPARAM lParam);
DWORD dwSize;
DWORD dwSize2;
DWORD dwIndex;
LPDWORD lpdwPIDs = NULL;
OSVERSIONINFO osver;
HINSTANCE hInstLib = NULL;
HINSTANCE hInstLib2 = NULL;
HANDLE hSnapShot = NULL;
PROCESSENTRY32 procentry;
HMODULE hMod;
HANDLE hProcess;
EnumInfoStruct sInfo;
char szFileName[MAX_PATH];
BOOL bFlag;
//char szFilePath[MAX_PATH];
// define ToolHelp Function Pointers.
HANDLE (WINAPI *lpfCreateToolhelp32Snapshot)(DWORD, DWORD);
BOOL (WINAPI *lpfProcess32First)(HANDLE, LPPROCESSENTRY32);
BOOL (WINAPI *lpfProcess32Next)(HANDLE, LPPROCESSENTRY32);
// define PSAPI Function Pointers.
BOOL (WINAPI *lpfEnumProcesses)(DWORD *, DWORD, DWORD *);
BOOL (WINAPI *lpfEnumProcessModules)(HANDLE, HMODULE *, DWORD,
LPDWORD);
DWORD (WINAPI *lpfGetModuleBaseName)(HANDLE, HMODULE, LPTSTR, DWORD);
// VDMDBG Function Pointers.
INT (WINAPI *lpfVDMEnumTaskWOWEx)(DWORD, TASKENUMPROCEX, LPARAM);
// Retrieve the OS version
osver.dwOSVersionInfoSize = sizeof(osver);
if (!GetVersionEx(&osver))
return FALSE;
// If Windows NT == 4.0
// If Windows 2000 == 5.0
if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT
&& osver.dwMajorVersion == 4) {
// start exception handler macro
__try {
// Get the procedure addresses explicitly. We do
// this so we don't have to worry about modules
// failing to load under OSes other than Windows NT 4.0
// because references to PSAPI.DLL can't be resolved.
hInstLib = LoadLibraryA("PSAPI.DLL"); // Load Handle to PSAPI DLL
if (hInstLib == NULL)
__leave;
hInstLib2 = LoadLibraryA("VDMDBG.DLL");// Load Handle to VDMDBG DLL
if (hInstLib2 == NULL)
__leave;
// Get procedure addresses.
lpfEnumProcesses = (BOOL (WINAPI *)(DWORD *, DWORD, DWORD*))
GetProcAddress(hInstLib, "EnumProcesses");
lpfEnumProcessModules = (BOOL (WINAPI *)(HANDLE, HMODULE *,
DWORD, LPDWORD)) GetProcAddress(hInstLib,
"EnumProcessModules");
lpfGetModuleBaseName = (DWORD (WINAPI *)(HANDLE, HMODULE,
LPTSTR, DWORD)) GetProcAddress(hInstLib,
"GetModuleBaseNameA");
lpfVDMEnumTaskWOWEx = (INT (WINAPI *)(DWORD, TASKENUMPROCEX,
LPARAM)) GetProcAddress(hInstLib2, "VDMEnumTaskWOWEx");
// check if procedures are not NULL
if (lpfEnumProcesses == NULL
|| lpfEnumProcessModules == NULL
|| lpfGetModuleBaseName == NULL
|| lpfVDMEnumTaskWOWEx == NULL)
__leave;
//
// Call the PSAPI function EnumProcesses to get all of the
// ProcID's currently in the system.
//
// NOTE: In the documentation, the third parameter of
// EnumProcesses is named cbNeeded, which implies that you
// can call the function once to find out how much space to
// allocate for a buffer and again to fill the buffer.
// This is not the case. The cbNeeded parameter returns
// the number of PIDs returned, so if your buffer size is
// zero cbNeeded returns zero.
//
// NOTE: The "HeapAlloc" loop here ensures that we
// actually allocate a buffer large enough for all the
// PIDs in the system.
//
dwSize2 = 256 * sizeof(DWORD);
do {
if (lpdwPIDs) {
HeapFree(GetProcessHeap(), 0, lpdwPIDs);
dwSize2 *= 2;
}
// allocate a buffer large enough for all PIDs
lpdwPIDs = (LPDWORD) HeapAlloc(GetProcessHeap(), 0, dwSize2);
// check if vald
if (lpdwPIDs == NULL)
__leave;
// Enumerate Processes
if (!lpfEnumProcesses(lpdwPIDs, dwSize2, &dwSize))
__leave;
} while (dwSize == dwSize2);
// How many ProcID's did we get?
dwSize /= sizeof(DWORD);
// Loop through each ProcID.
for (dwIndex = 0; dwIndex < dwSize; dwIndex++) {
szFileName[0] = 0;
// Open the process (if we can... security does not
// permit every process in the system to be opened).
hProcess = OpenProcess(
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, lpdwPIDs[dwIndex]);
if (hProcess != NULL) {
// Here we call EnumProcessModules to get only the
// first module in the process. This will be the
// EXE module for which we will retrieve the name.
if (lpfEnumProcessModules(hProcess, &hMod,
sizeof(hMod), &dwSize2)) {
// Get the module name
if (!lpfGetModuleBaseName(hProcess, hMod,
szFileName, sizeof(szFileName)))
szFileName[0] = 0;
}
CloseHandle(hProcess);
}
// Regardless of OpenProcess success or failure, we
// still call the enum func with the ProcID.
if (!lpProc(lpdwPIDs[dwIndex], 0, szFileName, lParam))
break;
// Did we just bump into an NTVDM?
if (_stricmp(szFileName, "NTVDM.EXE") == 0) {
// Fill in some info for the 16-bit enum proc.
sInfo.dwPID = lpdwPIDs[dwIndex];
sInfo.lpProc = lpProc;
sInfo.lParam = (DWORD) lParam;
sInfo.bEnd = FALSE;
// Enum the 16-bit stuff.
lpfVDMEnumTaskWOWEx(lpdwPIDs[dwIndex],
(TASKENUMPROCEX) Enum16, (LPARAM) &sInfo);
// Did our main enum func say quit?
if (sInfo.bEnd)
break;
}
}
} __finally {
if (hInstLib) // free dll handler
FreeLibrary(hInstLib);
if (hInstLib2) // free dll handler
FreeLibrary(hInstLib2);
if (lpdwPIDs) // free allocated memory (heap)
HeapFree(GetProcessHeap(), 0, lpdwPIDs);
}
// If any OS other than Windows NT 4.0. -> 2000/XP
} else if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
|| (osver.dwPlatformId == VER_PLATFORM_WIN32_NT
&& osver.dwMajorVersion > 4)) {
__try {
hInstLib = LoadLibraryA("Kernel32.DLL"); // handler to the kernel32.dll
if (hInstLib == NULL)
__leave;
// If NT-based OS, load VDMDBG.DLL.
/*
if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT) {
hInstLib2 = LoadLibraryA("VDMDBG.DLL");
if (hInstLib2 == NULL){
DWORD Error = GetLastError();
__leave;
}
}
*/
// Get procedure addresses. We are linking to
// these functions explicitly, because a module using
// this code would fail to load under Windows NT,
// which does not have the Toolhelp32
// functions in KERNEL32.DLL.
lpfCreateToolhelp32Snapshot =
(HANDLE (WINAPI *)(DWORD,DWORD))
GetProcAddress(hInstLib, "CreateToolhelp32Snapshot");
lpfProcess32First =
(BOOL (WINAPI *)(HANDLE,LPPROCESSENTRY32))
GetProcAddress(hInstLib, "Process32First");
lpfProcess32Next =
(BOOL (WINAPI *)(HANDLE,LPPROCESSENTRY32))
GetProcAddress(hInstLib, "Process32Next");
if (lpfProcess32Next == NULL
|| lpfProcess32First == NULL
|| lpfCreateToolhelp32Snapshot == NULL)
__leave;
/*
if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT) {
lpfVDMEnumTaskWOWEx = (INT (WINAPI *)(DWORD, TASKENUMPROCEX,
LPARAM)) GetProcAddress(hInstLib2, "VDMEnumTaskWOWEx");
if (lpfVDMEnumTaskWOWEx == NULL)
__leave;
}
*/
// Get a handle to a Toolhelp snapshot of all processes.
hSnapShot = lpfCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapShot == INVALID_HANDLE_VALUE) {
FreeLibrary(hInstLib);
return FALSE;
}
// Get the first process' information.
procentry.dwSize = sizeof(PROCESSENTRY32);
bFlag = lpfProcess32First(hSnapShot, &procentry);
// While there are processes, keep looping.
while (bFlag) {
// Call the enum func with the filename and ProcID.
if (lpProc(procentry.th32ProcessID, 0,
procentry.szExeFile, lParam)) {
// Did we just bump into an NTVDM?
if (_stricmp(procentry.szExeFile, "NTVDM.EXE") == 0) {
// Fill in some info for the 16-bit enum proc.
sInfo.dwPID = procentry.th32ProcessID;
sInfo.lpProc = lpProc;
sInfo.lParam = (DWORD) lParam;
sInfo.bEnd = FALSE;
// Enum the 16-bit stuff.
lpfVDMEnumTaskWOWEx(procentry.th32ProcessID,
(TASKENUMPROCEX) Enum16, (LPARAM) &sInfo);
// Did our main enum func say quit?
if (sInfo.bEnd)
break;
}
procentry.dwSize = sizeof(PROCESSENTRY32);
bFlag = lpfProcess32Next(hSnapShot, &procentry);
} else
bFlag = FALSE;
}
} __finally {
if (hInstLib)// free dll handler
FreeLibrary(hInstLib);
if (hInstLib2)// free dll handler
FreeLibrary(hInstLib2);
}
} else
return FALSE;
// Free the library.
FreeLibrary(hInstLib);
return TRUE;
}
BOOL WINAPI Enum16(DWORD dwThreadId, WORD hMod16, WORD hTask16,
PSZ pszModName, PSZ pszFileName, LPARAM lpUserDefined) {
BOOL bRet;
EnumInfoStruct *psInfo = (EnumInfoStruct *)lpUserDefined;
bRet = psInfo->lpProc(psInfo->dwPID, hTask16, pszFileName,
psInfo->lParam);
if (!bRet)
psInfo->bEnd = TRUE;
return !bRet;
}
BOOL CALLBACK MyProcessEnumerator(DWORD dwPID, WORD wTask, LPCSTR szProcess, LPARAM lParam)
{
struct processes proc; // Processes Struct
// Win9x || Win2k
if (wTask == 0)
{
HANDLE hProcess; // Process handler
DWORD hPriority; // Priority