├── unhookLdrLoadDll ├── unhookLdrLoadDll.vcxproj.user ├── unhookLdrLoadDll.vcxproj.filters ├── unhookLdrLoadDll.cpp └── unhookLdrLoadDll.vcxproj ├── README.md └── unhookLdrLoadDll.sln /unhookLdrLoadDll/unhookLdrLoadDll.vcxproj.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LdrLoadDll-Unhooking 2 | 3 | This is a proof of concept of bypassing(unhooking) the hook of potential EDRs, in using an unhooked version of LdrLoadDll to load DLLs. 4 | This is basically a reroute of the initial instruction of LdrLoadDll that will be executed in private memory and then reroute back into the normal code of LdrLoadDll, 5 | Which is better for OPSEC since, all the calls looks like they are coming directly from the NTDLL anyway. 6 | Why did I make this? Certain NTDLL export functions do not follow the SYSCALL procedure, hence this specific function like a few others need to be handled in a different manner. 7 | -------------------------------------------------------------------------------- /unhookLdrLoadDll/unhookLdrLoadDll.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /unhookLdrLoadDll.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.31729.503 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "unhookLdrLoadDll", "unhookLdrLoadDll\unhookLdrLoadDll.vcxproj", "{77D06D83-C7E6-4034-996C-D35C90A5174B}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {77D06D83-C7E6-4034-996C-D35C90A5174B}.Debug|x64.ActiveCfg = Debug|x64 17 | {77D06D83-C7E6-4034-996C-D35C90A5174B}.Debug|x64.Build.0 = Debug|x64 18 | {77D06D83-C7E6-4034-996C-D35C90A5174B}.Debug|x86.ActiveCfg = Debug|Win32 19 | {77D06D83-C7E6-4034-996C-D35C90A5174B}.Debug|x86.Build.0 = Debug|Win32 20 | {77D06D83-C7E6-4034-996C-D35C90A5174B}.Release|x64.ActiveCfg = Release|x64 21 | {77D06D83-C7E6-4034-996C-D35C90A5174B}.Release|x64.Build.0 = Release|x64 22 | {77D06D83-C7E6-4034-996C-D35C90A5174B}.Release|x86.ActiveCfg = Release|Win32 23 | {77D06D83-C7E6-4034-996C-D35C90A5174B}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {32F18F81-5722-42D6-AEE9-8A600EEBE260} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /unhookLdrLoadDll/unhookLdrLoadDll.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define OBJ_CASE_INSENSITIVE 0x00000040L 5 | 6 | 7 | typedef struct _UNICODE_STRING { 8 | USHORT Length; 9 | USHORT MaximumLength; 10 | PWSTR Buffer; 11 | } UNICODE_STRING, * PUNICODE_STRING; 12 | 13 | 14 | typedef struct _OBJECT_ATTRIBUTES { 15 | ULONG Length; 16 | HANDLE RootDirectory; 17 | PUNICODE_STRING ObjectName; 18 | ULONG Attributes; 19 | PVOID SecurityDescriptor; 20 | PVOID SecurityQualityOfService; 21 | } OBJECT_ATTRIBUTES, * POBJECT_ATTRIBUTES; 22 | 23 | #define InitializeObjectAttributes( i, o, a, r, s ) { \ 24 | (i)->Length = sizeof( OBJECT_ATTRIBUTES ); \ 25 | (i)->RootDirectory = r; \ 26 | (i)->Attributes = a; \ 27 | (i)->ObjectName = o; \ 28 | (i)->SecurityDescriptor = s; \ 29 | (i)->SecurityQualityOfService = NULL; \ 30 | } 31 | 32 | using pNewLdrLoadDll = NTSTATUS(NTAPI*)(PWCHAR PathToFile, ULONG Flags, PUNICODE_STRING ModuleFileName, PHANDLE ModuleHandle); 33 | 34 | 35 | PVOID CCopyMemory(PVOID Destination, CONST PVOID Source, SIZE_T Length) 36 | { 37 | PBYTE D = (PBYTE)Destination; 38 | PBYTE S = (PBYTE)Source; 39 | 40 | while (Length--) 41 | *D++ = *S++; 42 | 43 | return Destination; 44 | } 45 | 46 | SIZE_T StringLengthW(LPCWSTR String) 47 | { 48 | LPCWSTR String2; 49 | 50 | for (String2 = String; *String2; ++String2); 51 | 52 | return (String2 - String); 53 | } 54 | 55 | VOID RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString) 56 | { 57 | SIZE_T DestSize; 58 | 59 | if (SourceString) 60 | { 61 | DestSize = StringLengthW(SourceString) * sizeof(WCHAR); 62 | DestinationString->Length = (USHORT)DestSize; 63 | DestinationString->MaximumLength = (USHORT)DestSize + sizeof(WCHAR); 64 | } 65 | else 66 | { 67 | DestinationString->Length = 0; 68 | DestinationString->MaximumLength = 0; 69 | } 70 | 71 | DestinationString->Buffer = (PWCHAR)SourceString; 72 | } 73 | 74 | int main() 75 | { 76 | pNewLdrLoadDll LdrLoadrDll; 77 | UNICODE_STRING ldrldll; 78 | OBJECT_ATTRIBUTES objectAttributes = { 0 }; 79 | wchar_t ldrstring[] = L"Wininet.dll"; 80 | 81 | //Obtaining LdrLoadDll Address from loaded NTDLL 82 | RtlInitUnicodeString(&ldrldll, ldrstring); 83 | InitializeObjectAttributes(&objectAttributes, &ldrldll, OBJ_CASE_INSENSITIVE, NULL, NULL); 84 | LPVOID origLdrLoadDll = GetProcAddress(GetModuleHandleA("ntdll.dll"),"LdrLoadDll"); 85 | 86 | //Setting up the structure of the trampoline for the instructions 87 | unsigned char jumpPrelude[] = { 0x49, 0xBB }; 88 | unsigned char jumpAddress[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF }; 89 | unsigned char jumpEpilogue[] = { 0x41, 0xFF, 0xE3, 0xC3 }; 90 | LPVOID jmpAddr = (void*)((char*)origLdrLoadDll + 0x5); 91 | *(void**)(jumpAddress) = jmpAddr; 92 | 93 | //Allocating the memory for the strcture and its instructions 94 | LPVOID trampoline = VirtualAlloc(NULL,19, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); 95 | printf("Address of trampoline at 0x%p\n", trampoline); 96 | printf("Original LdrLoadDll at 0x%p\n",origLdrLoadDll); 97 | printf("Original jmp Address at 0x%p\n", jmpAddr); 98 | 99 | //Copying the original instruction mov qword ptr [rsp+10h],rbx in the trampoline and jumping back to the rest of the execution for LdrLoadDll 100 | CCopyMemory(trampoline,(PVOID)"\x48\x89\x5c\x24\x10", 5); 101 | //Setting up the JMP address in the original LdrLoadDll 102 | CCopyMemory((PBYTE)trampoline+5, jumpPrelude, 2); 103 | CCopyMemory((PBYTE)trampoline + 5 + 2, jumpAddress, sizeof(jumpAddress)); 104 | CCopyMemory((PBYTE)trampoline + 5 + 2 + 8, jumpEpilogue, 4); 105 | 106 | //Making the Allocated memory executable RX 107 | DWORD oldProtect = 0; 108 | VirtualProtect(trampoline,30,PAGE_EXECUTE_READ,&oldProtect); 109 | LdrLoadrDll = (pNewLdrLoadDll)trampoline; 110 | 111 | //Loading Wininet.dll 112 | HANDLE wininetmodule = NULL; 113 | LdrLoadrDll(NULL, 0 , &ldrldll, &wininetmodule); 114 | } 115 | -------------------------------------------------------------------------------- /unhookLdrLoadDll/unhookLdrLoadDll.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 16.0 23 | Win32Proj 24 | {77d06d83-c7e6-4034-996c-d35c90a5174b} 25 | unhookLdrLoadDll 26 | 10.0 27 | 28 | 29 | 30 | Application 31 | true 32 | v142 33 | Unicode 34 | 35 | 36 | Application 37 | false 38 | v142 39 | true 40 | Unicode 41 | 42 | 43 | Application 44 | true 45 | v142 46 | Unicode 47 | 48 | 49 | Application 50 | false 51 | v142 52 | true 53 | Unicode 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | true 75 | 76 | 77 | false 78 | 79 | 80 | true 81 | 82 | 83 | false 84 | 85 | 86 | 87 | Level3 88 | true 89 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 90 | true 91 | 92 | 93 | Console 94 | true 95 | 96 | 97 | 98 | 99 | Level3 100 | true 101 | true 102 | true 103 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 104 | true 105 | 106 | 107 | Console 108 | true 109 | true 110 | true 111 | 112 | 113 | 114 | 115 | Level3 116 | true 117 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 118 | true 119 | 120 | 121 | Console 122 | true 123 | 124 | 125 | 126 | 127 | Level3 128 | true 129 | true 130 | true 131 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 132 | true 133 | 134 | 135 | Console 136 | true 137 | true 138 | true 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | --------------------------------------------------------------------------------