Skip to content

Commit

Permalink
verifier fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
czy committed Jun 30, 2024
1 parent 8b064b3 commit dd43d66
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 22 deletions.
6 changes: 3 additions & 3 deletions HadSvc/knetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ void KNetWork::ReLoadIpPortConnectRule()

if (!pDenyRule || !pConnectRule || !pDnsRule)
return;

// Clear
NetNdrRuleClear();
RtlSecureZeroMemory(pDenyRule, sizeof(DENY_RULE) * g_MaxRuleCounter);
RtlSecureZeroMemory(pConnectRule, sizeof(REDIRECT_RULE) * g_MaxRuleCounter);
RtlSecureZeroMemory(pDnsRule, sizeof(DNS_RULE) * g_MaxRuleCounter);
Expand All @@ -53,9 +56,6 @@ void KNetWork::ReLoadIpPortConnectRule()
ConfigNetWorkYamlRuleParsing(pDenyRule, &iDenyCounter, pConnectRule, &iConnectCounter, g_MaxRuleCounter);
ConfigNetWorkYamlDnsRuleParsing(pDnsRule, &iDnsCounter, g_MaxRuleCounter);

// Clear
NetNdrRuleClear();

// DENY
{
for (int i = 0; i < iDenyCounter; ++i)
Expand Down
6 changes: 3 additions & 3 deletions HadSvc/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
#include <DirectoryRuleAssist.h>

// Debug调试
static bool kerne_mon = true; // kernel采集
static bool kerne_mon = false; // kernel采集
static bool kerne_rootkit = false; // rootkit接口
static bool user_mod = true; // user接口
static bool etw_mon = true; // user采集
static bool user_mod = false; // user接口
static bool etw_mon = false; // user采集

static bool gpip_send = false; // pip上报
static char g_chNameGuid[64] = { 0 }; // agentid
Expand Down
4 changes: 2 additions & 2 deletions HadSvc/mcfilter.rc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ IDI_ICON1 ICON "firearms.ico"
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 23,10,12,1
FILEVERSION 23,6,30,1
PRODUCTVERSION 1,0,0,1
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
Expand All @@ -79,7 +79,7 @@ BEGIN
BEGIN
VALUE "CompanyName", "theSecHunter"
VALUE "FileDescription", "HadesWin"
VALUE "FileVersion", "23.10.12.1"
VALUE "FileVersion", "23.6.30.1"
VALUE "InternalName", "HadesSvc.exe"
VALUE "LegalCopyright", "Copyright (C) 2022"
VALUE "OriginalFilename", "HadesSvc.exe"
Expand Down
1 change: 1 addition & 0 deletions HadesContrl/HadesContrl.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\HadesSdk\hpsocket\Include\HPSocket\HPSocket.h" />
<ClInclude Include="..\HadesSdk\include\sysinfo.h" />
<ClInclude Include="crashreport.h" />
<ClInclude Include="framework.h" />
<ClInclude Include="HadesContrl.h" />
Expand Down
3 changes: 3 additions & 0 deletions HadesContrl/HadesContrl.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
<ClInclude Include="Interface.h">
<Filter>Interface</Filter>
</ClInclude>
<ClInclude Include="..\HadesSdk\include\sysinfo.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="HadesContrl.cpp">
Expand Down
4 changes: 2 additions & 2 deletions MonitorEvent/netdrv/NetDrv.rc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ END
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 24,6,23,1
FILEVERSION 24,6,30,1
PRODUCTVERSION 1,0,0,1
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
Expand All @@ -69,7 +69,7 @@ BEGIN
BEGIN
VALUE "CompanyName", "Hades"
VALUE "FileDescription", "Hades For Windows NetDrv"
VALUE "FileVersion", "24.6.23.1"
VALUE "FileVersion", "24.6.30.1"
VALUE "InternalName", "NetDrv.sys"
VALUE "LegalCopyright", "Copyright (C) 2024"
VALUE "OriginalFilename", "NetDrv.sys"
Expand Down
55 changes: 54 additions & 1 deletion MonitorEvent/netdrv/devctrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,60 @@ NTSTATUS devctrl_create(PIRP irp, PIO_STACK_LOCATION irpSp)

return status;
}
VOID devctrl_freeSharedMemoryThread(_In_ PVOID Parameter)
{
PSHARED_MEMORY pSharedMemory = (PSHARED_MEMORY)Parameter;
if (pSharedMemory) {
if (pSharedMemory->mdl)
{
__try
{
if (pSharedMemory->userVa)
{
MmUnmapLockedPages(pSharedMemory->userVa, pSharedMemory->mdl);
}
if (pSharedMemory->kernelVa)
{
MmUnmapLockedPages(pSharedMemory->kernelVa, pSharedMemory->mdl);
}
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
}

MmFreePagesFromMdl(pSharedMemory->mdl);
IoFreeMdl(pSharedMemory->mdl);
pSharedMemory->mdl = NULL;
memset(pSharedMemory, 0, sizeof(SHARED_MEMORY));
}
}
PsTerminateSystemThread(STATUS_SUCCESS);
}
void devctrl_freeSharedMemory(PSHARED_MEMORY pSharedMemory)
{
if (KeGetCurrentIrql() > APC_LEVEL)
{
HANDLE threadHandle = NULL;
NTSTATUS status = STATUS_SUCCESS;
status = PsCreateSystemThread(
&threadHandle,
THREAD_ALL_ACCESS,
NULL,
NULL,
NULL,
devctrl_freeSharedMemoryThread,
pSharedMemory
);

if (NT_SUCCESS(status) && threadHandle)
{
KPRIORITY priority = HIGH_PRIORITY;
ZwSetInformationThread(threadHandle, ThreadPriority, &priority, sizeof(priority));
ZwClose(threadHandle);
}
return;
}

if (pSharedMemory->mdl)
{
__try
Expand All @@ -101,6 +153,7 @@ void devctrl_freeSharedMemory(PSHARED_MEMORY pSharedMemory)

MmFreePagesFromMdl(pSharedMemory->mdl);
IoFreeMdl(pSharedMemory->mdl);
pSharedMemory->mdl = NULL;

memset(pSharedMemory, 0, sizeof(SHARED_MEMORY));
}
Expand Down Expand Up @@ -1086,7 +1139,7 @@ NTSTATUS devctrl_init(PDRIVER_OBJECT pDriverObject)
InitializeListHead(&g_IoQueryHead);
VerifiExInitializeNPagedLookasideList(&g_IoQueryList, NULL, NULL, 0, sizeof(NF_QUEUE_ENTRY), 'NFQU', 0);
KeInitializeSpinLock(&g_sIolock);

// Init I/O handler Thread
KeInitializeEvent(
&g_ioThreadEvent,
Expand Down
16 changes: 8 additions & 8 deletions MonitorEvent/netdrv/hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,35 @@ void hash_table_free(PHASH_TABLE pTable)
}

int ht_add_entry(PHASH_TABLE pTable, PHASH_TABLE_ENTRY pEntry)
{
{
if (pTable == NULL || (!pTable))
return 0;
if (pEntry == NULL || (!pEntry))
return 0;
UINT64 hash = pEntry->id % pTable->size;

if (ht_find_entry(pTable, pEntry->id))
return 0;

pEntry->pNext = pTable->pEntries[hash];
pTable->pEntries[hash] = pEntry;

return 1;
}


PHASH_TABLE_ENTRY ht_find_entry(PHASH_TABLE pTable, UINT64 id)
{
PHASH_TABLE_ENTRY pEntry;
if (pTable == NULL || (!pTable))
return 0;

PHASH_TABLE_ENTRY pEntry = NULL;
pEntry = pTable->pEntries[id % pTable->size];

while (pEntry)
{
if (pEntry->id == id)
{
return pEntry;
}

pEntry = pEntry->pNext;
}

return NULL;
}

Expand Down
51 changes: 51 additions & 0 deletions MonitorEvent/sysmondrv/devctrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,59 @@ NTSTATUS devctrl_DrDevEnum(PDEVICE_OBJECT DeviceObject, PIRP irp, PIO_STACK_LOCA
}

// ☆ Share Memory MDL
VOID devctrl_freeSharedMemoryThread(_In_ PVOID Parameter)
{
PSHARED_MEMORY pSharedMemory = (PSHARED_MEMORY)Parameter;
if (pSharedMemory) {
if (pSharedMemory->mdl)
{
__try
{
if (pSharedMemory->userVa)
{
MmUnmapLockedPages(pSharedMemory->userVa, pSharedMemory->mdl);
}
if (pSharedMemory->kernelVa)
{
MmUnmapLockedPages(pSharedMemory->kernelVa, pSharedMemory->mdl);
}
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
}

MmFreePagesFromMdl(pSharedMemory->mdl);
IoFreeMdl(pSharedMemory->mdl);
pSharedMemory->mdl = NULL;
memset(pSharedMemory, 0, sizeof(SHARED_MEMORY));
}
}
PsTerminateSystemThread(STATUS_SUCCESS);
}
void devctrl_freeSharedMemory(PSHARED_MEMORY pSharedMemory)
{
if (KeGetCurrentIrql() > APC_LEVEL)
{
HANDLE threadHandle = NULL;
NTSTATUS status = STATUS_SUCCESS;
status = PsCreateSystemThread(
&threadHandle,
THREAD_ALL_ACCESS,
NULL,
NULL,
NULL,
devctrl_freeSharedMemoryThread,
pSharedMemory
);

if (NT_SUCCESS(status) && threadHandle)
{
KPRIORITY priority = HIGH_PRIORITY;
ZwSetInformationThread(threadHandle, ThreadPriority, &priority, sizeof(priority));
ZwClose(threadHandle);
}
return;
}
if (pSharedMemory->mdl)
{
__try
Expand Down
2 changes: 1 addition & 1 deletion MonitorEvent/sysmonuserlib/sysmonuser.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
<LinkIncremental>true</LinkIncremental>
<OutDir>..\..\HadesSdk\sysmonuser\lib</OutDir>
<TargetName>$(ProjectName)_d</TargetName>
<IncludePath>D:\Hades\Hades-Windows\HadesSdk\include;$(IncludePath)</IncludePath>
<IncludePath>..\..\HadesSdk\include;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
Expand Down
4 changes: 2 additions & 2 deletions RuleEngineSvc/RuleEngineSvc.rc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ END
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 23,10,12,1
FILEVERSION 24,6,30,1
PRODUCTVERSION 1,0,0,1
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
Expand All @@ -67,7 +67,7 @@ BEGIN
BEGIN
BLOCK "080404b0"
BEGIN
VALUE "FileVersion", "23.10.12.1"
VALUE "FileVersion", "24.6.30.1"
VALUE "InternalName", "RuleEngi.dll"
VALUE "LegalCopyright", "Copyright (C) 2023"
VALUE "OriginalFilename", "RuleEngi.dll"
Expand Down
51 changes: 51 additions & 0 deletions UnitTest/UntsRule.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#include "UntsRule.h"
#include <NetWorkRuleAssist.h>
#include <sysinfo.h>
#include <NetApi.h>

static const int g_MaxRuleCounter = 100;

UntsRule::UntsRule()
{
Expand All @@ -10,5 +15,51 @@ UntsRule::~UntsRule()

void UntsRule::UnTs_ReLoadIpPortConnectRule()
{
PDENY_RULE pDenyRule = nullptr;
PREDIRECT_RULE pConnectRule = nullptr;
PDNS_RULE pDnsRule = nullptr;

pDenyRule = (PDENY_RULE)new DENY_RULE[g_MaxRuleCounter];
pConnectRule = (PREDIRECT_RULE)new REDIRECT_RULE[g_MaxRuleCounter];
pDnsRule = (PDNS_RULE)new DNS_RULE[g_MaxRuleCounter];

if (!pDenyRule || !pConnectRule || !pDnsRule)
return;

// Clear
NetNdrRuleClear();
RtlSecureZeroMemory(pDenyRule, sizeof(DENY_RULE) * g_MaxRuleCounter);
RtlSecureZeroMemory(pConnectRule, sizeof(REDIRECT_RULE) * g_MaxRuleCounter);
RtlSecureZeroMemory(pDnsRule, sizeof(DNS_RULE) * g_MaxRuleCounter);

int iDenyCounter = 0; int iConnectCounter = 0; int iDnsCounter = 0;
ConfigNetWorkYamlRuleParsing(pDenyRule, &iDenyCounter, pConnectRule, &iConnectCounter, g_MaxRuleCounter);
ConfigNetWorkYamlDnsRuleParsing(pDnsRule, &iDnsCounter, g_MaxRuleCounter);

// DENY
{
for (int i = 0; i < iDenyCounter; ++i)
NetNdrSetDenyRule(pDenyRule[i].strRuleName, pDenyRule[i].strIpAddress, pDenyRule[i].strProtocol, pDenyRule[i].strPorts, pDenyRule[i].strAction);
}

// REDIRECT
{
for (int i = 0; i < iConnectCounter; ++i)
NetNdrSetRediRectRule(pConnectRule[i].strRuleName, pConnectRule[i].strRedirectIp, pConnectRule[i].RedrectPort, pConnectRule[i].strProtocol, pConnectRule[i].strProcessName);
}

// DNS
{
for (int i = 0; i < iDnsCounter; ++i)
NetNdrSetDnsRule(pDnsRule[i].strRuleName, pDnsRule[i].strProtocol, pDnsRule[i].sDnsName.c_str(), pDnsRule[i].strAction);
}

if (pDenyRule) {
delete[] pDenyRule;
pDenyRule = nullptr;
}
if (pConnectRule) {
delete[] pConnectRule;
pConnectRule = nullptr;
}
}
2 changes: 2 additions & 0 deletions UnitTest/unitts.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
<SpectreMitigation>false</SpectreMitigation>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<SpectreMitigation>false</SpectreMitigation>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down

0 comments on commit dd43d66

Please sign in to comment.