476{
478 NTSTATUS NtStatus;
480 ULONG ReturnedLength;
482 char SystemRoot[MAX_PATH] = {0};
483 char ModuleSymbolPath[MAX_PATH] = {0};
484 char TempPath[MAX_PATH] = {0};
488 ULONG SysModuleInfoBufferSize = 0;
489 UINT32 ModuleDetailsSize = 0;
494
495
496
497
499
500
501
502
503 if (GetSystemDirectoryA(SystemRoot, MAX_PATH) == NULL)
504 {
505 ShowMessages(
"err, unable to get system directory (%x)\n",
506 GetLastError());
507
509 }
510
511 string SystemRootString(SystemRoot);
512
513
514
515
516 transform(SystemRootString.begin(),
517 SystemRootString.end(),
518 SystemRootString.begin(),
519 [](unsigned char c) { return std::tolower(c); });
520
521
522
523
524 Replace(SystemRootString,
"\\system32",
"");
525
526
527
528
529
530
531
532
533
534
536
537
538
539
541 NULL,
542 SysModuleInfoBufferSize,
543 MEM_COMMIT | MEM_RESERVE,
544 PAGE_READWRITE);
545
546 if (!ModuleInfo)
547 {
548 ShowMessages(
"err, unable to allocate memory for module list (%x)\n",
549 GetLastError());
551 }
552
553 if (!NT_SUCCESS(
555 ModuleInfo,
556 SysModuleInfoBufferSize,
557 NULL)))
558 {
559 ShowMessages(
"err, unable to query module list (%#x)\n", NtStatus);
560
561 VirtualFree(ModuleInfo, 0, MEM_RELEASE);
563 }
564
565
566
567
568
569
570
571
572
573
574
575 do
576 {
577
578
579
581 {
582 break;
583 }
584
585
586
587
588 ModuleCountRequest.
ProcessId = UserProcessId;
590
591
592
593
594 Status = DeviceIoControl(
597
598 &ModuleCountRequest,
600 &ModuleCountRequest,
602
603 &ReturnedLength,
604 NULL
605 );
606
607 if (!Status)
608 {
609 break;
610 }
611
612
613
614
616 {
618
619
620
623
625
626 if (ModuleDetailsRequest == NULL)
627 {
628 break;
629 }
630
631 RtlZeroMemory(ModuleDetailsRequest, ModuleDetailsSize);
632
633
634
635
636 ModuleDetailsRequest->
ProcessId = UserProcessId;
638
639
640
641
642 Status = DeviceIoControl(
645
646 ModuleDetailsRequest,
648 ModuleDetailsRequest,
649 ModuleDetailsSize,
650
651 &ReturnedLength,
652 NULL
653 );
654
655 if (!Status)
656 {
657 free(ModuleDetailsRequest);
658 break;
659 }
660
661
662
663
665 {
666
667
668
671
672
673
674
675 if (Modules == NULL)
676 {
677 ModulesCount = 0;
678 free(ModuleDetailsRequest);
679 break;
680 }
681 }
682 else
683 {
685 free(ModuleDetailsRequest);
686 break;
687 }
688
689
690
691
692 IsFreeUsermodeModulesBuffer =
TRUE;
693 }
694 else
695 {
697 break;
698 }
699
701
702
703
704
705
706
707
708
711
712 if (ModuleSymDetailArray == NULL)
713 {
714 ShowMessages(
"err, unable to allocate memory for module list (%x)\n",
715 GetLastError());
716
717 if (IsFreeUsermodeModulesBuffer)
718 {
719 free(ModuleDetailsRequest);
720 }
722 }
723
724
725
726
728
729
730
731
732
733
734
735 if (ModulesCount != 0)
736 {
737 for (
UINT32 i = 0; i < ModulesCount; i++)
738 {
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753 RtlZeroMemory(ModuleSymbolPath, sizeof(ModuleSymbolPath));
754 RtlZeroMemory(TempPath, sizeof(TempPath));
755 RtlZeroMemory(ModuleSymbolGuidAndAge, sizeof(ModuleSymbolGuidAndAge));
756
757
758
759
760 wcstombs(TempPath, Modules[i].FilePath, MAX_PATH);
761
763 TempPath,
764 ModuleSymbolPath,
765 ModuleSymbolGuidAndAge,
766 ModuleDetailsRequest->
Is32Bit))
767 {
768 IsSymbolPdbDetailAvailable =
TRUE;
769
770
771 }
772 else
773 {
774 IsSymbolPdbDetailAvailable =
FALSE;
775
776
777 }
778
779
780
781
785
786 memcpy(ModuleSymDetailArray[i].FilePath, TempPath, strlen(TempPath));
787
788 if (IsSymbolPdbDetailAvailable)
789 {
792 memcpy(ModuleSymDetailArray[i].ModuleSymbolPath, ModuleSymbolPath, MAX_PATH);
793
794
795
796
797 string ModuleSymbolPathString(ModuleSymbolPath);
798 if (ModuleSymbolPathString.find(":\\") != std::string::npos)
800 else
802 }
803 else
804 {
806 }
807
808
809
810
811
812
813 if (SendOverSerial)
814 {
816 }
817 }
818 }
819
820
821
822
823
824
825
827 {
828 UINT32 IndexInSymbolBuffer = ModulesCount + i;
829
830 auto PathName = ModuleInfo->Modules[i].FullPathName + ModuleInfo->Modules[i].OffsetToFileName;
831
832
833
834
835 RtlZeroMemory(ModuleSymbolPath, sizeof(ModuleSymbolPath));
836 RtlZeroMemory(ModuleSymbolGuidAndAge, sizeof(ModuleSymbolGuidAndAge));
837
838 string ModuleFullPath((const char *)ModuleInfo->Modules[i].FullPathName);
839
840 if (ModuleFullPath.rfind("\\SystemRoot\\", 0) == 0)
841 {
842
843
844
845
846
847 Replace(ModuleFullPath,
"\\SystemRoot", SystemRootString);
848 }
849
850
851
852
854 ModuleSymbolPath,
855 ModuleSymbolGuidAndAge,
857 {
858 IsSymbolPdbDetailAvailable =
TRUE;
859
860
861
862
863 }
864 else
865 {
866 IsSymbolPdbDetailAvailable =
FALSE;
867
868
869
870
871 }
872
873
874
875
876 ModuleSymDetailArray[IndexInSymbolBuffer].
BaseAddress = (
UINT64)ModuleInfo->Modules[i].ImageBase;
877 memcpy(ModuleSymDetailArray[IndexInSymbolBuffer].FilePath, ModuleFullPath.c_str(), ModuleFullPath.size());
878
879 if (IsSymbolPdbDetailAvailable)
880 {
882 memcpy(ModuleSymDetailArray[IndexInSymbolBuffer].ModuleSymbolGuidAndAge, ModuleSymbolGuidAndAge,
MAXIMUM_GUID_AND_AGE_SIZE);
883 memcpy(ModuleSymDetailArray[IndexInSymbolBuffer].ModuleSymbolPath, ModuleSymbolPath, MAX_PATH);
884
885
886
887
888 string ModuleSymbolPathString(ModuleSymbolPath);
889 if (ModuleSymbolPathString.find(":\\") != std::string::npos)
891 else
893 }
894 else
895 {
897 }
898
899
900
901
902
903
904 if (SendOverSerial)
905 {
907 }
908 }
909
910
911
912
913
914
915
916
917 *BufferToStoreDetails = ModuleSymDetailArray;
919
920 VirtualFree(ModuleInfo, 0, MEM_RELEASE);
921
922 if (IsFreeUsermodeModulesBuffer)
923 {
924 free(ModuleDetailsRequest);
925 }
926
928}
UCHAR BOOLEAN
Definition BasicTypes.h:39
unsigned __int64 UINT64
Definition BasicTypes.h:21
unsigned int UINT32
Definition BasicTypes.h:48
unsigned long ULONG
Definition BasicTypes.h:37
#define MAXIMUM_GUID_AND_AGE_SIZE
maximum size for GUID and Age of PE @detail It seems that 33 bytes is enough but let's have more spac...
Definition Constants.h:491
#define DEBUGGER_OPERATION_WAS_SUCCESSFUL
General value to indicate that the operation or request was successful.
Definition ErrorCodes.h:23
@ SystemModuleInformation
Definition Hooks.h:116
enum _SYSTEM_INFORMATION_CLASS SYSTEM_INFORMATION_CLASS
System information class.
UINT32 StoredLength
Definition HyperDbgScriptImports.h:65
#define IOCTL_GET_USER_MODE_MODULE_DETAILS
ioctl, to get user mode modules details
Definition Ioctls.h:240
struct _USERMODE_LOADED_MODULE_SYMBOLS * PUSERMODE_LOADED_MODULE_SYMBOLS
struct _USERMODE_LOADED_MODULE_DETAILS USERMODE_LOADED_MODULE_DETAILS
struct _USERMODE_LOADED_MODULE_DETAILS * PUSERMODE_LOADED_MODULE_DETAILS
struct _RTL_PROCESS_MODULES * PRTL_PROCESS_MODULES
BOOL Replace(std::string &str, const std::string &from, const std::string &to)
general replace all function
Definition common.cpp:73
BOOLEAN ShowErrorMessage(UINT32 Error)
shows the error message
Definition debugger.cpp:38
HANDLE g_DeviceHandle
Holds the global handle of device which is used to send the request to the kernel by IOCTL,...
Definition globals.h:471
NULL()
Definition test-case-generator.py:530
BOOLEAN ScriptEngineConvertFileToPdbFileAndGuidAndAgeDetailsWrapper(const char *LocalFilePath, char *PdbFilePath, char *GuidAndAgeDetails, BOOLEAN Is32BitModule)
ScriptEngineConvertFileToPdbFileAndGuidAndAgeDetails wrapper.
Definition script-engine-wrapper.cpp:245
BOOLEAN IsUserMode
Definition Symbols.h:29
BOOLEAN Is32Bit
Definition Symbols.h:30
UINT64 BaseAddress
Definition Symbols.h:31
BOOLEAN IsLocalSymbolPath
Definition Symbols.h:26
BOOLEAN IsSymbolDetailsFound
Definition Symbols.h:25
ULONG NumberOfModules
Definition Windows.h:34
UINT32 Result
Definition Symbols.h:52
UINT32 ModulesCount
Definition Symbols.h:51
UINT32 ProcessId
Definition Symbols.h:48
BOOLEAN Is32Bit
Definition Symbols.h:50
BOOLEAN OnlyCountModules
Definition Symbols.h:49
UINT64 BaseAddress
Definition Symbols.h:40
BOOLEAN SymbolDeleteSymTable()
Delete and free structures and variables related to the symbols.
Definition symbol.cpp:433