-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrySearchForm.cpp
2025 lines (1757 loc) · 69.4 KB
/
CrySearchForm.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
// Nessecary dialog includes and IML provider for imaging.
#include "CrySearchForm.h"
#include "CrySettingsDialog.h"
#include "CrySearchAboutDialog.h"
#include "CryProcessEnumeratorForm.h"
#include "CryNewScanForm.h"
#include "CryAllocateMemoryWindow.h"
#include "CryFillMemoryWindow.h"
#include "CryMemoryDissectionWindow.h"
#include "CryProcessEnvironmentBlockWindow.h"
#include "CrySystemHandleInformationWindow.h"
#include "CryHeapWalkDialog.h"
#include "CodeCaveScannerWindow.h"
#include "CryPointerScanWindow.h"
#include "CryPluginsWindow.h"
#include "CryBruteforcePIDWindow.h"
#include "ImlProvider.h"
#include "UIUtilities.h"
// Global source IML file declaration. Imaging in the GUI depends on this.
#define IMAGECLASS CrySearchIml
#define IMAGEFILE "CrySearch.iml"
#include <Draw/iml_source.h>
// Time callback type definitions.
#define MEMORY_SCANNER_COMPLETION_TIMECALLBACK 5
#define ADDRESS_TABLE_UPDATE_TIMECALLBACK 10
#define HOTKEY_TIMECALLBACK 20
#define UPDATE_RESULTS_TIMECALLBACK 21
#define PROCESS_TERMINATION_TIMECALLBACK 30
// ---------------------------------------------------------------------------------------------
// Global declaration of the memory scanner class instance which technically runs the application.
MemoryScanner* mMemoryScanner;
// Global declaration of the plugin system class.
PluginSystem* mPluginSystem;
// Address table instance that provides the user access to address tables.
AddressTable loadedTable;
bool viewAddressTableValueHex = false;
// Global declaration of the module manager class.
ModuleManager* mModuleManager;
// Global PE methodic class instance, nessecary for over half of the application.
PortableExecutable* mPeInstance;
// Global Debugger class instance, necessary for debugging the remote process.
CryDebugger* mDebugger;
// Stored process PE information.
Win32PEInformation LoadedProcessPEInformation;
// ---------------------------------------------------------------------------------------------
// Subwindows or controls that are managed by the main window class may be needed outside. A globally defined pointer is necessary.
CrySearchWindowManager* mCrySearchWindowManager;
// ---------------------------------------------------------------------------------------------
// Gets the string representation of the address of a search result.
String GetAddress(const int index)
{
#ifdef _WIN64
return FormatInt64HexUpper(CachedAddresses[index].Address);
#else
return FormatHexadecimalIntSpecial(CachedAddresses[index].Address);
#endif
}
// Gets the string representation of the value of a search result.
String GetValue(const int index)
{
// Do not read the values if a scan is running.
if (!mMemoryScanner->IsScanRunning())
{
const int dataSize = GlobalScanParameter->GlobalScanValueType == CRYDATATYPE_AOB ? GlobalScanParameter->ValueSize
: CachedAddresses[index].StringLength;
Byte readBuffer[STRING_MAX_UNTIL_NULL * sizeof(wchar_t)];
return mMemoryScanner->Peek(CachedAddresses[index].Address, dataSize ? min(dataSize, (int)(STRING_MAX_UNTIL_NULL * sizeof(wchar_t)))
: sizeof(__int64), readBuffer) ? ValueAsStringInternal(readBuffer, GlobalScanParameter->GlobalScanValueType
, dataSize, GlobalScanParameter->CurrentScanHexValues) : "???";
}
// The value of the search result could not be read. The presented value is therefore unknown.
return "???";
}
// Gets the description of an address table entry.
String GetAddressTableDescription(const int index)
{
return loadedTable[index]->Description;
}
// Gets the string representation of the address of an address table entry.
String GetAddressTableAddress(const int index)
{
#ifdef _WIN64
return FormatInt64HexUpper(loadedTable[index]->Address);
#else
return FormatHexadecimalIntSpecial(loadedTable[index]->Address);
#endif
}
// Gets the string representation of the value of an address table entry.
String GetAddressTableValue(const int index)
{
// Only read the value of address table entries if a process is opened.
AddressTableEntry* const entry = loadedTable[index];
if (mMemoryScanner->GetProcessId())
{
// If this address should be treated as a pointer, we should first calculate the final address.
SIZE_T calcAddr = entry->Address;
if (entry->IsPointer)
{
// Add all offsets with intermediate pointer reading.
const int oCount = entry->OffsetsList.GetCount() - 1;
for (int i = 0; i < oCount; ++i)
{
// We read out all pointer values except the last one, we don't need to read
// the pointer value, but the actual value instead.
if (!mMemoryScanner->Peek(calcAddr + entry->OffsetsList[i], mMemoryScanner->IsX86Process() ? sizeof(DWORD) : sizeof(__int64), &calcAddr))
{
// Failed to read at this address, the value can by anything from this point on.
entry->Value = "???";
return entry->Value;
}
}
// Add the last offset without reading.
calcAddr += entry->OffsetsList[entry->OffsetsList.GetCount() - 1];
}
// Read the value at the final address.
Byte readBuffer[STRING_MAX_UNTIL_NULL * sizeof(wchar_t)];
if (mMemoryScanner->Peek(calcAddr, entry->Size ? min(entry->Size, (int)(STRING_MAX_UNTIL_NULL * sizeof(wchar_t))) : sizeof(__int64), readBuffer))
{
// Properly format the final value.
entry->Value = ValueAsStringInternal(readBuffer, entry->ValueType, entry->Size, viewAddressTableValueHex);
}
else
{
entry->Value = "???";
}
return entry->Value;
}
// The value of the address table entry could not be read. The presented value is therefore unknown.
entry->Value = "???";
return entry->Value;
}
// Gets the valuetype of an address table entry.
String GetAddressTableValueType(const int index)
{
return GetCrySearchDataTypeRepresentation(loadedTable[index]->ValueType);
}
// ---------------------------------------------------------------------------------------------
// If CrySearch was opened using a file association, open the file straight away.
// If CrySearch was opened regularly, pass NULL as parameter.
CrySearchForm::CrySearchForm(const char* fn)
{
this->processLoaded = false;
this->wndTitleRandomized = false;
this->lowerPaneHidden = false;
this->mWindowManager.SetParentWindow(this);
DWORD wndTitle[] = {0x53797243, 0x63726165, 0x654d2068, 0x79726f6d, 0x61635320, 0x72656e6e, 0x0}; //"CrySearch Memory Scanner"
this->Title((char*)wndTitle).Icon(CrySearchIml::CrySearch()).Sizeable().Zoomable().SetRect(0, 0, 800, 600);
this->SetMinSize(Size(640, 480));
this->AddFrame(mMenuStrip);
this->mMenuStrip.Set(THISBACK(MainMenu));
this->AddFrame(mToolStrip);
this->mToolStrip.Set(THISBACK(ToolStrip));
this->mScanResults.CryAddRowNumColumn("Address").SetConvert(Single<IndexBasedValueConvert<GetAddress>>());
this->mScanResults.CryAddRowNumColumn("Value").SetConvert(Single<IndexBasedValueConvert<GetValue>>());
this->mScanResults.WhenLeftDouble = THISBACK(SearchResultDoubleClicked);
this->mScanResults.WhenBar = THISBACK(SearchResultWhenBar);
this->mUserAddressList.CryAddRowNumColumn("Description").SetConvert(Single<IndexBasedValueConvert<GetAddressTableDescription>>());
this->mUserAddressList.CryAddRowNumColumn("Address").SetConvert(Single<IndexBasedValueConvert<GetAddressTableAddress>>());
this->mUserAddressList.CryAddRowNumColumn("Value").SetConvert(Single<IndexBasedValueConvert<GetAddressTableValue>>());
this->mUserAddressList.CryAddRowNumColumn("Type").SetConvert(Single<IndexBasedValueConvert<GetAddressTableValueType>>());
this->mUserAddressList.WhenBar = THISBACK(UserDefinedEntryWhenBar);
this->mUserAddressList.WhenLeftDouble = THISBACK(UserDefinedEntryWhenDoubleClicked);
this->mUserAddressList.RemovalRoutine = THISBACK(AddressTableRemovalRoutine);
this->mSearchResultsPanel
<< this->mSearchResultCount.SetLabel("Search Results: 0").HSizePosZ(5, 5).TopPos(5, 20)
<< this->mScanningProgress.RightPos(5, 120).TopPos(5, 20)
<< this->mScanResults.MultiSelect().HSizePosZ(5, 5).VSizePosZ(30, 0)
;
this->mUserAddressPanel << this->mUserAddressList.MultiSelect().HSizePos(5, 5).VSizePos(5);
this->mScanningProgress.Hide();
this->mTabbedDataWindows.WhenSet = THISBACK(ActiveTabWindowChanged);
*this
<< this->mMainSplitter.Vert(this->mInputScanSplitter.Horz(this->mSearchResultsPanel, this->mUserAddressPanel)
, this->mTabbedDataWindows.SizePos())
;
// Sets the position and resize thresholds for the main window splitting controls.
this->SetMainSplitterPosition();
this->mMainSplitter.SetMinPixels(0, 100);
this->mMainSplitter.SetMinPixels(1, 100);
this->mInputScanSplitter.SetMinPixels(0, 300);
this->mInputScanSplitter.SetMinPixels(1, 250);
// If settings configuration file is not found, create a new one using default settings.
if (!SettingsFile::ConfigFileExists() || !SettingsFile::GetInstance()->Initialize())
{
//Prompt("Settings Error", CtrlImg::exclamation(), "The settings file was not found or corrupt, and has been overwritten with the defaults. If this is your first run, you can ignore this warning.", "OK");
SettingsFile::GetInstance()->DefaultSettings();
}
// Initiate the memory scanner class, the most important part of CrySearch.
mMemoryScanner->ErrorOccured = THISBACK(ScannerErrorOccured);
mMemoryScanner->UpdateScanningProgress = THISBACK(ScannerUserInterfaceUpdate);
mMemoryScanner->ScanStarted = THISBACK(ScannerScanStarted);
// Initialize the plugin system.
mPluginSystem = PluginSystem::GetInstance();
mPluginSystem->RetrieveAndLoadAllPlugins();
// Validate plugin-defined routine indices and act accordingly.
const int opr = SettingsFile::GetInstance()->GetOpenProcessRoutine();
const int rpm = SettingsFile::GetInstance()->GetReadMemoryRoutine();
const int wpm = SettingsFile::GetInstance()->GetWriteMemoryRoutine();
const int pm = SettingsFile::GetInstance()->GetProtectMemoryRoutine();
const int pluginCount = mPluginSystem->GetPluginCount();
bool changed = false;
// If the settings-saved routine index is out of the current bounds, a previously used routine-plugin
// may have failed at this moment, or the designated plugin has been removed from the plugins directory.
// We take no chance and set the default routine for use.
Vector<CrySearchPlugin> overrideFuncs;
mPluginSystem->GetPluginsByType(CRYPLUGIN_COREFUNC_OVERRIDE, overrideFuncs);
if ((opr > 1 || rpm > 1 || wpm > 1 || pm > 1) && (max(opr, rpm, wpm, pm) - 2 >= overrideFuncs.GetCount()))
{
SettingsFile::GetInstance()->SetOpenProcessRoutine();
SettingsFile::GetInstance()->SetReadMemoryRoutine();
SettingsFile::GetInstance()->SetWriteMemoryRoutine();
SettingsFile::GetInstance()->SetProtectMemoryRoutine();
changed = true;
}
// If the value was changed, let the user know.
if (changed)
{
SettingsFile::GetInstance()->Save();
Prompt("Warning", CtrlImg::exclamation(), "The settings file contained core invalid routine indices. The invalid ones have been restored to default.", "OK");
}
// The settings file saves some routines too. Set the correct routines.
CrySearchRoutines.InitializeRoutines();
// If one of more NTDLL functions were not succesfully retrieved, notify the user about it.
if (CrySearchRoutines.ErrorOccured())
{
Prompt("Behavioral Warning", CtrlImg::exclamation(), Format("Some NTDLL functions were not loaded succesfully. %s may behave unpredictable from here.", String((char*)wndTitle, 9)), "OK");
}
// Make sure the module manager is initialized.
mModuleManager = ModuleManager::GetInstance();
// Set timer that runs keeping track of hotkeys.
SetTimeCallback(100, THISBACK(CheckKeyPresses), HOTKEY_TIMECALLBACK);
// Set timer callback that runs the address list update sequence.
SetTimeCallback(SettingsFile::GetInstance()->GetAddressTableUpdateInterval(), THISBACK(AddressValuesUpdater), ADDRESS_TABLE_UPDATE_TIMECALLBACK);
// Set timer callback that runs the search results update sequence.
SetTimeCallback(1000, THISBACK(SearchResultListUpdater), UPDATE_RESULTS_TIMECALLBACK);
// Assign proper callback functions to configured hotkeys.
this->LinkHotkeysToActions();
// Wind up UI debugger error event. When attaching fails, the debug window must be closed at once.
this->mWindowManager.GetDebuggerWindow()->DebugErrorOccured = THISBACK(DebugWindowErrorOccured);
// If an address table file was opened using file association, load it and display it.
if (fn)
{
AddressTable::CreateAddressTableFromFile(loadedTable, fn);
this->mUserAddressList.SetVirtualCount(loadedTable.GetCount());
}
}
// The main window destructor.
CrySearchForm::~CrySearchForm()
{
// Stop the timer callbacks that are running.
KillTimeCallback(ADDRESS_TABLE_UPDATE_TIMECALLBACK);
KillTimeCallback(HOTKEY_TIMECALLBACK);
KillTimeCallback(PROCESS_TERMINATION_TIMECALLBACK);
}
// ---------------------------------------------------------------------------------------------
// Populates the main application window menu strip.
void CrySearchForm::MainMenu(Bar& pBar)
{
pBar.Add("File", THISBACK(FileMenu));
pBar.Add("Edit", THISBACK(EditMenu));
pBar.Add("Tools", THISBACK(ToolsMenu));
// Some menu items should only be added when a process has been opened.
if (this->processLoaded && mModuleManager->GetModuleCount())
{
pBar.Add("Debugger", THISBACK(DebuggerMenu));
}
pBar.Add("Window", THISBACK(WindowMenu));
pBar.Add("Help", THISBACK(HelpMenu));
// When the window title is randomized the opened process should be listed in the label below the bar.
pBar.MenuGapRight();
pBar.Add(this->mOpenedProcess.SetAlign(ALIGN_RIGHT), 200);
}
// Populates the main application window toolstrip.
void CrySearchForm::ToolStrip(Bar& pBar)
{
pBar.Add("Open Process", CrySearchIml::AttachToProcessMenu(), THISBACK(OpenProcessMenu));
pBar.Add(this->processLoaded && !mMemoryScanner->IsScanRunning(), "Search", CrySearchIml::SearchMemoryMenu(), THISBACK(MemorySearch));
pBar.Add(this->processLoaded && !mMemoryScanner->IsScanRunning() && mScanResults.GetCount() > 0, "Refresh search results", CrySearchIml::NextScanMenu(), THISBACK(RefreshSearchResults));
}
// Populates the file menu bar.
void CrySearchForm::FileMenu(Bar& pBar)
{
pBar.Add("Open Process", CrySearchIml::AttachToProcessMenu(), THISBACK(OpenProcessMenu));
pBar.Add(this->processLoaded, "Close Process", THISBACK(CloseProcessMenu));
pBar.Separator();
pBar.Add("Open File", CrySearchIml::OpenFile(), THISBACK(OpenFileMenu));
if (loadedTable.GetFileName().IsEmpty())
{
pBar.Add(false, "Save File", CrySearchIml::SaveFile(), THISBACK(SaveFileMenu));
}
else
{
pBar.Add(true, "Save File", CrySearchIml::SaveFile(), THISBACK(SaveFileMenu));
}
pBar.Add("Save File As", THISBACK(SaveFileAsMenu));
pBar.Separator();
pBar.Add("Exit", CrySearchIml::ExitApplication(), THISBACK(ExitApplication));
}
// Populates the menu bar for data editing operations.
void CrySearchForm::EditMenu(Bar& pBar)
{
pBar.Add((this->mScanResults.GetCount() > 0), "Clear Scan Results", THISBACK(ClearScanResultsWithoutWarning));
pBar.Add((this->mUserAddressList.GetCount() > 0), "Clear Address List", THISBACK(ClearAddressList));
pBar.Separator();
pBar.Add("Settings", CrySearchIml::SettingsButton(), THISBACK(SettingsButtonClicked));
}
// Populates the menu bar for tools.
void CrySearchForm::ToolsMenu(Bar& pBar)
{
if (this->processLoaded)
{
pBar.Add("View PEB", CrySearchIml::AboutButton(), THISBACK(ViewPEBButtonClicked));
pBar.Add(!mMemoryScanner->IsReadOnlyOperationMode(), "View Handles", CrySearchIml::ViewHandlesButton(), THISBACK(ViewSystemHandlesButtonClicked));
pBar.Separator();
pBar.Add(!mMemoryScanner->IsReadOnlyOperationMode(), "Allocate Memory", CrySearchIml::AllocateMemoryButton(), THISBACK(AllocateMemoryButtonClicked));
pBar.Add(!mMemoryScanner->IsReadOnlyOperationMode(), "Fill Memory", THISBACK(FillMemoryButtonClicked));
pBar.Add("Memory Dissection", CrySearchIml::MemoryDissection(), THISBACK(MemoryDissectionButtonClicked));
pBar.Add("View Heap Information", CrySearchIml::HeapWalkSmall(), THISBACK(HeapWalkMenuClicked));
pBar.Add("Scan for Code Caves", CrySearchIml::CodeCaveSmall(), THISBACK(CodeCaveMenuClicked));
// Disabled pointer scan implementation as we first fix bugs...
pBar.Add("Pointer Scan", CrySearchIml::PointerScanSmall(), THISBACK(PointerScanMenuClicked));
}
// These menu items can be added regardless of the program state.
pBar.Add(!this->processLoaded, "Brute-Force PID", CrySearchIml::BruteForceSmall(), THISBACK(BruteForcePIDClicked));
pBar.Separator();
pBar.Add("Plugins", CrySearchIml::PluginsMenuSmall(), THISBACK(PluginsMenuClicked));
}
// // Populates the menu bar for debugger settings.
void CrySearchForm::DebuggerMenu(Bar& pBar)
{
// If the modules in the opened process could not be retrieved, we can't display this menu.
if (this->processLoaded)
{
const bool isAttached = mDebugger && mDebugger->IsDebuggerAttached();
const bool isReadOnly = mMemoryScanner->IsReadOnlyOperationMode();
pBar.Add(!isAttached && !isReadOnly, "Attach", CrySearchIml::DebuggerAttach(), THISBACK(DebuggerAttachMenu));
pBar.Add(isAttached, "Detach", THISBACK(DebuggerDetachMenu));
}
}
// Populates the menu bar for window visibility settings.
void CrySearchForm::WindowMenu(Bar& pBar)
{
pBar.Add("Always on top", THISBACK(ToggleAlwaysOnTop)).Check(this->IsTopMost());
pBar.Add("Randomize window title", THISBACK(RandomizeWindowTitle)).Check(this->wndTitleRandomized);
pBar.Add("Hide lower pane", THISBACK(HideLowerPaneButtonClicked)).Check(this->lowerPaneHidden);
if (this->processLoaded)
{
pBar.Separator();
pBar.Add("General", CrySearchIml::ViewGeneralButton(), THISBACK(ViewGeneralButtonClicked)).Check(IsTabPageOpened(this->mTabbedDataWindows, "General") >= 0);
pBar.Add("Disassembly", CrySearchIml::DisassemblyIcon(), THISBACK(ShowHideDisasmWindow)).Check(IsTabPageOpened(this->mTabbedDataWindows, "Disassembly") >= 0);
pBar.Add("Imports", CrySearchIml::ViewImportsButton(), THISBACK(ViewImportsButtonClicked)).Check(IsTabPageOpened(this->mTabbedDataWindows, "Imports") >= 0);
pBar.Add("Threads", CrySearchIml::ViewThreadsButton(), THISBACK(ViewThreadsButtonClicked)).Check(IsTabPageOpened(this->mTabbedDataWindows, "Threads") >= 0);
pBar.Add("Modules", CrySearchIml::ViewModulesButton(), THISBACK(ViewModulesButtonClicked)).Check(IsTabPageOpened(this->mTabbedDataWindows, "Modules") >= 0);
}
}
// Populates the help menu bar.
void CrySearchForm::HelpMenu(Bar& pBar)
{
pBar.Add("About", CrySearchIml::AboutButton(), THISBACK(AboutCrySearch));
}
// Populates the menu bar for changing properties of address table entries.
void CrySearchForm::ChangeRecordSubMenu(Bar& pBar)
{
pBar.Add("Description", THISBACK1(AddressListChangeProperty, CRDM_DESCRIPTION));
pBar.Add(this->mUserAddressList.GetSelectCount() == 1, "Address", THISBACK1(AddressListChangeProperty, CRDM_ADDRESS));
pBar.Add(!mMemoryScanner->IsReadOnlyOperationMode(), "Value", THISBACK1(AddressListChangeProperty, CRDM_VALUE));
pBar.Add("Type", THISBACK1(AddressListChangeProperty, CRDM_TYPE));
}
// Populates the menu bar for copying the value of a field in the address table to the clipboard.
void CrySearchForm::CopyAddressTableValueMenu(Bar& pBar)
{
pBar.Add("Description", THISBACK(CopyAddressTableEntryDescription));
pBar.Add("Address", THISBACK(CopyAddressTableEntryAddress));
pBar.Add("Value", THISBACK(CopyAddressTableEntryValue));
pBar.Add("Type", THISBACK(CopyAddressTableEntryType));
}
// Copies the description of the currently selected address table entry to the clipboard.
void CrySearchForm::CopyAddressTableEntryDescription()
{
const int cursor = this->mUserAddressList.GetCursor();
if (cursor >= 0 && loadedTable.GetCount() > 0)
{
WriteClipboardText(loadedTable[cursor]->Description);
}
}
// Copies the address of the currently selected address table entry to the clipboard.
void CrySearchForm::CopyAddressTableEntryAddress()
{
const int cursor = this->mUserAddressList.GetCursor();
if (cursor >= 0 && loadedTable.GetCount() > 0)
{
WriteClipboardText(FormatInt64HexUpper(loadedTable[cursor]->Address));
}
}
// Copies the value of the currently selected address table entry to the clipboard.
void CrySearchForm::CopyAddressTableEntryValue()
{
const int cursor = this->mUserAddressList.GetCursor();
if (cursor >= 0 && loadedTable.GetCount() > 0)
{
WriteClipboardText(loadedTable[cursor]->Value);
}
}
// Copies the type of the currently selected address table entry to the clipboard.
void CrySearchForm::CopyAddressTableEntryType()
{
const int cursor = this->mUserAddressList.GetCursor();
if (cursor >= 0 && loadedTable.GetCount() > 0)
{
WriteClipboardText(GetCrySearchDataTypeRepresentation(loadedTable[cursor]->ValueType));
}
}
// Executed when the user right-clicks an address in the address table.
void CrySearchForm::UserDefinedEntryWhenBar(Bar& pBar)
{
pBar.Add("Manually add address", CrySearchIml::AddToAddressList(), THISBACK(ManuallyAddAddressToTable));
const int row = this->mUserAddressList.GetCursor();
if (row >= 0 && loadedTable.GetCount() > 0)
{
pBar.Add("Dissect memory", CrySearchIml::MemoryDissection(), THISBACK(AddressListEntryMemoryDissection));
pBar.Separator();
if (loadedTable[row]->Frozen)
{
pBar.Add(!mMemoryScanner->IsReadOnlyOperationMode(), "Thaw", CrySearchIml::ThawIconSmall(), THISBACK(ToggleAddressTableFreezeThaw));
}
else
{
pBar.Add(!mMemoryScanner->IsReadOnlyOperationMode(), "Freeze", CrySearchIml::FreezeAddressSmall(), THISBACK(ToggleAddressTableFreezeThaw));
}
// Add decimal/hexadecimal toggle button.
pBar.Add(viewAddressTableValueHex ? "View as decimal" : "View as hexadecimal", THISBACK(ToggleAddressTableValueView)).Check(viewAddressTableValueHex);
const bool canDbg = (mDebugger && mDebugger->IsDebuggerAttached()) && this->mUserAddressList.GetSelectCount() == 1;
if (mDebugger && mDebugger->FindBreakpoint(loadedTable[row]->Address) == -1)
{
pBar.Add(canDbg, "Set Breakpoint", CrySearchIml::SetBreakpoint(), THISBACK(SetDataBreakpointMenu));
}
else
{
pBar.Add(canDbg, "Remove Breakpoint", CrySearchIml::DeleteButton(), THISBACK(RemoveBreakpointMenu));
}
pBar.Add("Copy", THISBACK(CopyAddressTableValueMenu));
pBar.Add("Change Record", CrySearchIml::ChangeRecordIcon(), THISBACK(ChangeRecordSubMenu));
pBar.Separator();
pBar.Add("Delete\tDEL", CrySearchIml::DeleteButton(), THISBACK(DeleteUserDefinedAddress));
}
}
// Populates the menu bar for setting breakpoints.
void CrySearchForm::SetDataBreakpointMenu(Bar& pBar)
{
pBar.Add("Read", THISBACK(SetDataBreakpointOnRead));
pBar.Add("Write", THISBACK(SetDataBreakpointOnReadWrite));
pBar.Add("Execute", THISBACK(SetDataBreakpointOnExecute));
}
// Executed when the user right-clicks a search result.
void CrySearchForm::SearchResultWhenBar(Bar& pBar)
{
if (this->mScanResults.GetCursor() >= 0 && mMemoryScanner->GetScanResultCount() > 0)
{
pBar.Add("Add to address list", CrySearchIml::AddToAddressList(), THISBACK(SearchResultDoubleClicked));
pBar.Add("View as hexadecimal", THISBACK(ToggleSearchResultViewAs)).Check(GlobalScanParameter->CurrentScanHexValues);
}
}
// ---------------------------------------------------------------------------------------------
// Checks key presses across all controls. Consider it a global key event function.
void CrySearchForm::CheckKeyPresses()
{
// If hotkeys are enabled, execute the hotkeys procedure.
if (SettingsFile::GetInstance()->GetEnableHotkeys())
{
this->HotkeysProcedure();
}
// Reinstate the callback for the next key check.
SetTimeCallback(100, THISBACK(CheckKeyPresses), HOTKEY_TIMECALLBACK);
}
// Called regularly to update the search results currently visible.
void CrySearchForm::SearchResultListUpdater()
{
// Refresh the address table ArrayCtrl to force updating of the values.
this->mScanResults.Refresh();
// Reinstate the callback for the next iteration.
SetTimeCallback(1000, THISBACK(SearchResultListUpdater), UPDATE_RESULTS_TIMECALLBACK);
}
// Called regularly to update entries currently in the address table.
void CrySearchForm::AddressValuesUpdater()
{
// If CrySearch is operating in read only mode, nothing may be written to the target process.
if (!mMemoryScanner->IsReadOnlyOperationMode())
{
// Handle frozen addresses.
const int addrTableCount = loadedTable.GetCount();
for (int i = 0; i < addrTableCount; ++i)
{
// If we are currently looking at a frozen entry, we need to write its value there.
AddressTableEntry* const curEntry = loadedTable[i];
if (curEntry->Frozen)
{
// Read the current values into local variables.
const int curIntValue = ScanInt(curEntry->FrozenValue, NULL, 10);
const double curDoubleValue = StrDbl(curEntry->FrozenValue);
// Get the correct data size for writing.
switch (curEntry->ValueType)
{
case CRYDATATYPE_BYTE:
mMemoryScanner->Poke(curEntry->Address, &curIntValue, sizeof(Byte));
break;
case CRYDATATYPE_2BYTES:
mMemoryScanner->Poke(curEntry->Address, &curIntValue, sizeof(short));
break;
case CRYDATATYPE_4BYTES:
mMemoryScanner->Poke(curEntry->Address, &curIntValue, sizeof(int));
break;
case CRYDATATYPE_8BYTES:
{
const __int64 curLongValue = ScanInt64(curEntry->Value, NULL, 10);
mMemoryScanner->Poke(curEntry->Address, &curLongValue, sizeof(__int64));
}
break;
case CRYDATATYPE_FLOAT:
{
const float fValue = (float)curDoubleValue;
mMemoryScanner->Poke(curEntry->Address, &fValue, sizeof(float));
}
break;
case CRYDATATYPE_DOUBLE:
mMemoryScanner->Poke(curEntry->Address, &curDoubleValue, sizeof(double));
break;
case CRYDATATYPE_AOB:
{
ArrayOfBytes curAobValue = StringToBytes(curEntry->FrozenValue);
mMemoryScanner->PokeB(curEntry->Address, curAobValue);
curEntry->Size = curAobValue.Size;
}
break;
case CRYDATATYPE_STRING:
mMemoryScanner->PokeA(curEntry->Address, curEntry->FrozenValue);
break;
case CRYDATATYPE_WSTRING:
mMemoryScanner->PokeW(curEntry->Address, curEntry->FrozenValue.ToWString());
break;
}
}
}
}
// Refresh the address table ArrayCtrl to force the values to update.
this->mUserAddressList.Refresh();
// Reinstate timer queue callback to ensure timer keeps running.
SetTimeCallback(SettingsFile::GetInstance()->GetAddressTableUpdateInterval(), THISBACK(AddressValuesUpdater), ADDRESS_TABLE_UPDATE_TIMECALLBACK);
}
// This callback checks whether the process is still running, if one is opened.
// If the opened process terminated somehow, CrySearch will close it internally.
void CrySearchForm::CheckProcessTermination()
{
if (mMemoryScanner->GetProcessId() > 0)
{
if (!IsProcessActive(mMemoryScanner->GetHandle()))
{
this->ProcessTerminated = true;
this->ScannerErrorOccured(PROCESSWASTERMINATED);
// Kill the callback, otherwise errors will keep coming.
KillTimeCallback(PROCESS_TERMINATION_TIMECALLBACK);
}
}
SetTimeCallback(250, THISBACK(CheckProcessTermination), PROCESS_TERMINATION_TIMECALLBACK);
}
// ---------------------------------------------------------------------------------------------
// Handles the removal of items from the address table.
void CrySearchForm::AddressTableRemovalRoutine(const Vector<int>& items)
{
// Remove breakpoint from data if necessary.
const int count = items.GetCount();
for (int i = 0; i < count; ++i)
{
if (mDebugger && mDebugger->IsDebuggerAttached())
{
mDebugger->RemoveBreakpoint(loadedTable[items[i]]->Address);
}
}
// Remove the items from the address table and refresh the control.
loadedTable.Remove(items);
this->mUserAddressList.Clear();
this->mUserAddressList.SetVirtualCount(loadedTable.GetCount());
}
// Hides or shows the lower window pane.
void CrySearchForm::HideLowerPaneButtonClicked()
{
Rect r = this->GetRect();
if (this->lowerPaneHidden)
{
this->mMainSplitter.Add(this->mTabbedDataWindows.SizePos());
this->SetMinSize(Size(640, 480));
r.bottom += 220;
this->SetRect(r);
this->SetMainSplitterPosition();
}
else
{
this->mMainSplitter.Remove(this->mTabbedDataWindows);
this->SetMinSize(Size(640, 220));
const int remaining = r.bottom - r.top;
r.bottom = remaining < 220 ? r.bottom - remaining : 220;
this->SetRect(r);
this->SetMainSplitterPosition();
}
this->mMainSplitter.SetMinPixels(0, 100);
this->mMainSplitter.SetMinPixels(1, 100);
this->lowerPaneHidden = !this->lowerPaneHidden;
}
// Adjusts the position of the main window splitter control.
void CrySearchForm::SetMainSplitterPosition()
{
const Rect r = this->mMainSplitter.GetRect();
const int total = r.bottom - r.top;
this->mMainSplitter.SetPos(((total / 2) * 10000 / total) - 600);
}
// Opens up memory dissection window with new dissection dialog opened and selected address filled in.
void CrySearchForm::AddressListEntryMemoryDissection()
{
// Retrieve a pointer to the selected address table entry.
const AddressTableEntry* const pEntry = loadedTable[this->mUserAddressList.GetCursor()];
// Execute the memory dissection window using the retrieved address table entry pointer.
CryMemoryDissectionWindow* cmdw = new CryMemoryDissectionWindow(pEntry);
cmdw->Run();
delete cmdw;
}
// Toggles CrySearch's main window to be always on top or not.
void CrySearchForm::ToggleAlwaysOnTop()
{
this->TopMost(!this->IsTopMost());
}
// Executed when the tab window currently active has changed.
void CrySearchForm::ActiveTabWindowChanged()
{
const int index = ~this->mTabbedDataWindows;
if (index >= 0)
{
// This situation needs to be handled separately because the imports window needs redrawal.
TabCtrl::Item& newtab = this->mTabbedDataWindows.GetItem(index);
if (newtab.GetText() == "Imports")
{
this->mWindowManager.GetImportsWindow()->ModuleRedraw();
}
}
}
// Randomizes the window title and sets CrySearch to use menubar label to display the opened process.
void CrySearchForm::RandomizeWindowTitle()
{
if (this->wndTitleRandomized)
{
DWORD wndTitle[] = {0x53797243, 0x63726165, 0x654d2068, 0x79726f6d, 0x61635320, 0x72656e6e, 0x0}; //"CrySearch Memory Scanner"
String windowTitle = this->processLoaded ? Format("%s - (%i) %s", (char*)wndTitle, mMemoryScanner->GetProcessId(), mMemoryScanner->GetProcessName()) : (char*)wndTitle;
this->Title(SettingsFile::GetInstance()->GetEnableReadOnlyMode() ? Format("%s - (Read-Only)", windowTitle) : windowTitle);
this->mOpenedProcess.SetLabel("");
}
else
{
this->Title(GenerateRandomWindowTitle());
// Set the label in the menu bar to be utilized.
this->mOpenedProcess.SetLabel(this->processLoaded ? Format("(%i) %s ", mMemoryScanner->GetProcessId(), mMemoryScanner->GetProcessName()) : "");
}
this->mMenuStrip.Set(THISBACK(MainMenu));
this->wndTitleRandomized = !this->wndTitleRandomized;
}
// Executes the heap walk dialog.
void CrySearchForm::HeapWalkMenuClicked()
{
CryHeapWalkDialog* chwd = new CryHeapWalkDialog(CrySearchIml::HeapWalkSmall());
chwd->Execute();
delete chwd;
}
// Executes the code cave scanner dialog.
void CrySearchForm::CodeCaveMenuClicked()
{
CodeCaveScannerWindow* ccsw = new CodeCaveScannerWindow(CrySearchIml::CodeCaveSmall());
ccsw->Execute();
delete ccsw;
}
// Executes the pointer scan dialog.
void CrySearchForm::PointerScanMenuClicked()
{
CryPointerScanWindow* cpsw = new CryPointerScanWindow(CrySearchIml::PointerScanSmall());
cpsw->Execute();
delete cpsw;
}
// Sets a hardware breakpoint on an address.
void CrySearchForm::SetBreakpointMenuFunction(const HWBP_TYPE type)
{
const int cursor = this->mUserAddressList.GetCursor();
HWBP_SIZE size = HWBP_SIZE_4;
// Get breakpoint-wise correct size of data.
switch (GetDataSizeFromValueType(loadedTable[cursor]->ValueType))
{
case 1:
size = HWBP_SIZE_1;
break;
case 2:
size = HWBP_SIZE_2;
break;
case 4:
size = HWBP_SIZE_4;
break;
case 8:
size = HWBP_SIZE_8;
break;
}
// Let's refresh the threads list once more to be sure we have every thread currently active.
mCrySearchWindowManager->GetThreadWindow()->ClearList();
mCrySearchWindowManager->GetThreadWindow()->Initialize();
// Set breakpoint on data in each thread in the process.
mDebugger->SetHardwareBreakpoint(mThreadsList, loadedTable[cursor]->Address, size, type);
}
// Sets a read breakpoint on the selected data address (in the address table).
void CrySearchForm::SetDataBreakpointOnRead()
{
this->SetBreakpointMenuFunction(HWBP_TYPE_READWRITE);
}
// Sets a read/write breakpoint on the selected data address (in the address table).
void CrySearchForm::SetDataBreakpointOnReadWrite()
{
this->SetBreakpointMenuFunction(HWBP_TYPE_WRITE);
}
// Sets a breakpoint on the selected address.
void CrySearchForm::SetDataBreakpointOnExecute()
{
this->SetBreakpointMenuFunction(HWBP_TYPE_EXECUTE);
}
// Removes a breakpoint from the selected address.
void CrySearchForm::RemoveBreakpointMenu()
{
this->mWindowManager.GetDebuggerWindow()->Cleanup();
mDebugger->RemoveBreakpoint(loadedTable[this->mUserAddressList.GetCursor()]->Address);
}
// Executes operations to brute force PIDs (Process ID's) to find hidden processes.
void CrySearchForm::BruteForcePIDClicked()
{
CryBruteforcePIDWindow* cbfpidw = new CryBruteforcePIDWindow();
if (cbfpidw->Execute() == 10)
{
// If the dialog result is 10, the user requested to open a brute-forced process.
this->WhenProcessOpened(cbfpidw->GetSelectedProcess(), true);
}
delete cbfpidw;
}
// Executes the plugins window.
void CrySearchForm::PluginsMenuClicked()
{
CryPluginsWindow* cpw = new CryPluginsWindow();
cpw->Execute();
delete cpw;
}
// Opens an address table file to be loaded into memory.
void CrySearchForm::OpenFileMenu()
{
const DWORD appname[] = {0x53797243, 0x63726165, 0x68}; //"CrySearch"
FileSel* fs = new FileSel();
String filter = (char*)appname;
filter += " Address Tables\t*.csat";
fs->Types(filter);
if (fs->ExecuteOpen("Open file..."))
{
if (loadedTable.GetCount() > 0 && !Prompt("Are you sure?", CtrlImg::exclamation()
, "The address table contains addresses. Do you want to clear them and open a file?", "Yes", "No"))
{
delete fs;
return;
}
String filename = fs->Get();
if (!filename.IsEmpty())
{
AddressTable::CreateAddressTableFromFile(loadedTable, filename);
this->mUserAddressList.SetVirtualCount(loadedTable.GetCount());
}
}
delete fs;
}
// Executed when the user double clicks an address table entry.
void CrySearchForm::UserDefinedEntryWhenDoubleClicked()
{
const int row = this->mUserAddressList.GetCursor();
const int column = this->mUserAddressList.GetClickColumn();
if (row >= 0 && loadedTable.GetCount() > 0)
{
Vector<int> singleRowInput = { row };
switch (column)
{
#ifdef _WIN64
case 0: // description
CryChangeRecordDialog(loadedTable, singleRowInput, CRDM_DESCRIPTION).Execute();
break;
case 1: // address
CryChangeRecordDialog(loadedTable, singleRowInput, CRDM_ADDRESS).Execute();
break;
case 2: // value
CryChangeRecordDialog(loadedTable, singleRowInput, mMemoryScanner->IsReadOnlyOperationMode() ? CRDM_DESCRIPTION : CRDM_VALUE).Execute();
break;
case 3: // type
CryChangeRecordDialog(loadedTable, singleRowInput, CRDM_TYPE).Execute();
break;
#else
case 0: // description
CryChangeRecordDialog(loadedTable, singleRowInput, CRDM_DESCRIPTION).Execute();
break;
case 1: // address
CryChangeRecordDialog(loadedTable, singleRowInput, CRDM_ADDRESS).Execute();
break;
case 2: // value
CryChangeRecordDialog(loadedTable, singleRowInput, mMemoryScanner->IsReadOnlyOperationMode() ? CRDM_DESCRIPTION : CRDM_VALUE).Execute();
break;
case 3: // type
CryChangeRecordDialog(loadedTable, singleRowInput, CRDM_TYPE).Execute();
break;
#endif
}
}
}
// Freezes addresses that are thawn and thaws frozen addresses.
void CrySearchForm::ToggleAddressTableFreezeThaw()
{
loadedTable[this->mUserAddressList.GetCursor()]->Frozen = !loadedTable[this->mUserAddressList.GetCursor()]->Frozen;
}
// Toggles whether entries in the address table are currently shown in hexadecimal format or decimal format.
void CrySearchForm::ToggleAddressTableValueView()
{
viewAddressTableValueHex = !viewAddressTableValueHex;
}
// Toggles whether the search results are currently shown in hexadecimal format or decimal format.
void CrySearchForm::ToggleSearchResultViewAs()
{
GlobalScanParameter->CurrentScanHexValues = !GlobalScanParameter->CurrentScanHexValues;
}
// Open a dialog to enable the user to manually add an address to the address table.
void CrySearchForm::ManuallyAddAddressToTable()
{
CryChangeRecordDialog(loadedTable, Vector<int>(), CRDM_MANUALNEW).Execute();
this->mUserAddressList.SetVirtualCount(loadedTable.GetCount());
}
// Change a property of a selected address table entry (address, description, value or type).
void CrySearchForm::AddressListChangeProperty(ChangeRecordDialogMode mode)
{
const int row = this->mUserAddressList.GetCursor();
const int totalCount = loadedTable.GetCount();
if (row >= 0 && totalCount > 0)
{
// Get selected rows.
Vector<int> selectedRows;
for (int r = 0; r < totalCount; ++r)
{
if (this->mUserAddressList.IsSelected(r))
{
selectedRows << r;
}
}
// Open the record changing dialog corresponding to the selected mode.
switch (mode)
{
case CRDM_DESCRIPTION:
#ifdef _WIN64
CryChangeRecordDialog(loadedTable, selectedRows, CRDM_DESCRIPTION).Execute();
#else
CryChangeRecordDialog(loadedTable, selectedRows, CRDM_DESCRIPTION).Execute();
#endif
break;
case CRDM_ADDRESS:
#ifdef _WIN64
CryChangeRecordDialog(loadedTable, selectedRows, CRDM_ADDRESS).Execute();
#else