Skip to content

Commit 50d495e

Browse files
authored
Add files via upload
1 parent 7e89241 commit 50d495e

17 files changed

Lines changed: 7037 additions & 0 deletions

SbieKernelPatch.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36408.4 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SbieKernelPatch", "SbieKernelPatch\SbieKernelPatch.vcxproj", "{3129E8DD-7654-4A1F-A305-57CBB984EDD7}"
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+
{3129E8DD-7654-4A1F-A305-57CBB984EDD7}.Debug|x64.ActiveCfg = Debug|x64
17+
{3129E8DD-7654-4A1F-A305-57CBB984EDD7}.Debug|x64.Build.0 = Debug|x64
18+
{3129E8DD-7654-4A1F-A305-57CBB984EDD7}.Debug|x86.ActiveCfg = Debug|Win32
19+
{3129E8DD-7654-4A1F-A305-57CBB984EDD7}.Debug|x86.Build.0 = Debug|Win32
20+
{3129E8DD-7654-4A1F-A305-57CBB984EDD7}.Release|x64.ActiveCfg = Release|x64
21+
{3129E8DD-7654-4A1F-A305-57CBB984EDD7}.Release|x64.Build.0 = Release|x64
22+
{3129E8DD-7654-4A1F-A305-57CBB984EDD7}.Release|x86.ActiveCfg = Release|Win32
23+
{3129E8DD-7654-4A1F-A305-57CBB984EDD7}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {D358B296-3E83-4D17-A51F-44666C4D0189}
30+
EndGlobalSection
31+
EndGlobal

SbieKernelPatch/DriverInterface.h

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include <iostream>
2+
#include "DriverTypes.h"
3+
4+
class DriverInterface {
5+
public:
6+
HANDLE hDevice;
7+
8+
// On instantiation, get a handle to the driver and execute our first IOCTL call.
9+
DriverInterface() {
10+
hDevice = CreateFileA(
11+
"\\\\.\\EchoDrv",
12+
GENERIC_READ | GENERIC_WRITE,
13+
FILE_SHARE_READ | FILE_SHARE_WRITE,
14+
NULL,
15+
OPEN_EXISTING,
16+
NULL,
17+
NULL
18+
);
19+
20+
//If driver handle failed to open print message and return
21+
if (hDevice == INVALID_HANDLE_VALUE) {
22+
std::cout << "Invalid handle on CreateFileA!" << std::endl;
23+
//Get the last error from windows for CreateFile
24+
std::cout << "Error code: " << GetLastError() << std::endl;
25+
}
26+
27+
// Yes, this buffer seems useless - but without it the driver BSOD's the PC.
28+
//Create a buffer to have data returned to.
29+
void* buf = (void*)malloc(4096);
30+
31+
//Call IOCTL that sets the PID variable and gets past the DWORD check
32+
//0x9e6a0594 - IOCTL Code
33+
BOOL success = DeviceIoControl(hDevice, 0x9e6a0594, NULL, NULL, buf, 4096, NULL, NULL);
34+
if (!success) {
35+
std::cout << "DeviceIOControl 0x9e6a0594 failed!" << std::endl;
36+
std::cout << "Error code: " << GetLastError() << std::endl;
37+
38+
CloseHandle(hDevice);
39+
return;
40+
}
41+
42+
//We don't need that buffer anymore
43+
free(buf);
44+
}
45+
46+
~DriverInterface() {
47+
CloseHandle(hDevice);
48+
}
49+
50+
// Next, get a HANDLE to the desired process through the driver.
51+
HANDLE get_handle_for_pid(DWORD pid) {
52+
// IOCTL Code - 0xe6224248
53+
54+
k_get_handle param{};
55+
// Process ID to get handle for
56+
param.pid = pid;
57+
58+
// Access to be granted on the returned handle
59+
param.access = GENERIC_ALL;
60+
61+
// Do DeviceIoControl call
62+
BOOL success = DeviceIoControl(hDevice, 0xe6224248, &param, sizeof(param), &param, sizeof(param), NULL, NULL);
63+
if (!success) {
64+
std::cout << "DeviceIOControl 0xe6224248 failed!" << std::endl;
65+
std::cout << "Error code: " << GetLastError() << std::endl;
66+
return INVALID_HANDLE_VALUE;
67+
}
68+
69+
// Return the handle given by the driver.
70+
return param.handle;
71+
}
72+
73+
// A simple function to read memory using the driver.
74+
BOOL read_memory_raw(void* address, void* buf, size_t len, HANDLE targetProcess) {
75+
k_param_readmem req{};
76+
req.fromAddress = (void*)address;
77+
req.length = len;
78+
req.targetProcess = targetProcess;
79+
req.toAddress = (void*)buf;
80+
81+
BOOL success = DeviceIoControl(hDevice, 0x60a26124, &req, sizeof(k_param_readmem), &req, sizeof(k_param_readmem), NULL, NULL);
82+
return success;
83+
}
84+
85+
void Shutdown() {
86+
CloseHandle(hDevice);
87+
}
88+
};

SbieKernelPatch/DriverTypes.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#pragma once
2+
#include <Windows.h>
3+
#include <cstdint>
4+
#include <winternl.h>
5+
6+
struct k_param_readmem {
7+
HANDLE targetProcess;
8+
void* fromAddress;
9+
void* toAddress;
10+
size_t length;
11+
void* padding;
12+
uint32_t returnCode;
13+
};
14+
15+
struct k_param_init {
16+
void* first;
17+
void* second;
18+
void* third;
19+
};
20+
21+
struct k_get_handle {
22+
DWORD pid;
23+
ACCESS_MASK access;
24+
HANDLE handle;
25+
DWORD extra1; // ½ÓסÇý¶¯¶àдµÄ
26+
};
27+
28+
// This is a windows type to fetch NtQuerySystemInformation
29+
// These structures are copied from Process Hacker source code (ntldr.h)
30+
typedef struct _RTL_PROCESS_MODULE_INFORMATION
31+
{
32+
HANDLE Section;
33+
PVOID MappedBase;
34+
PVOID ImageBase;
35+
ULONG ImageSize;
36+
ULONG Flags;
37+
USHORT LoadOrderIndex;
38+
USHORT InitOrderIndex;
39+
USHORT LoadCount;
40+
USHORT OffsetToFileName;
41+
UCHAR FullPathName[256];
42+
} RTL_PROCESS_MODULE_INFORMATION, *PRTL_PROCESS_MODULE_INFORMATION;
43+
44+
typedef struct _RTL_PROCESS_MODULES
45+
{
46+
ULONG NumberOfModules;
47+
RTL_PROCESS_MODULE_INFORMATION Modules[1];
48+
} RTL_PROCESS_MODULES, *PRTL_PROCESS_MODULES;
49+
50+
typedef struct _SYSTEM_HANDLE
51+
{
52+
PVOID Object;
53+
HANDLE UniqueProcessId;
54+
HANDLE HandleValue;
55+
ULONG GrantedAccess;
56+
USHORT CreatorBackTraceIndex;
57+
USHORT ObjectTypeIndex;
58+
ULONG HandleAttributes;
59+
ULONG Reserved;
60+
} SYSTEM_HANDLE, *PSYSTEM_HANDLE;
61+
62+
typedef struct _SYSTEM_HANDLE_INFORMATION_EX
63+
{
64+
ULONG_PTR HandleCount;
65+
ULONG_PTR Reserved;
66+
SYSTEM_HANDLE Handles[1];
67+
} SYSTEM_HANDLE_INFORMATION_EX, *PSYSTEM_HANDLE_INFORMATION_EX;
68+
69+
typedef struct _SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX
70+
{
71+
PVOID Object;
72+
ULONG_PTR UniqueProcessId;
73+
ULONG_PTR HandleValue;
74+
ULONG GrantedAccess;
75+
USHORT CreatorBackTraceIndex;
76+
USHORT ObjectTypeIndex;
77+
ULONG HandleAttributes;
78+
ULONG Reserved;
79+
} SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX, *PSYSTEM_HANDLE_TABLE_ENTRY_INFO_EX;

SbieKernelPatch/DriverUtil.h

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
#pragma once
2+
/*****************************************************************//**
3+
* \file DriverUtil.h
4+
* \brief Çý¶¯¿ØÖÆÏà¹Øº¯Êý
5+
*********************************************************************/
6+
#include <windows.h>
7+
#include <string>
8+
#include <iostream>
9+
10+
namespace DriverUtil
11+
{
12+
bool InstallDriver(const std::string& driverName, const std::string& driverPath)
13+
{
14+
SC_HANDLE hSCManager = OpenSCManagerA(nullptr, nullptr, SC_MANAGER_CREATE_SERVICE);
15+
if (!hSCManager)
16+
{
17+
std::cerr << "[!] OpenSCManager failed: " << GetLastError() << std::endl;
18+
return false;
19+
}
20+
21+
SC_HANDLE hService = CreateServiceA(
22+
hSCManager,
23+
driverName.c_str(),
24+
driverName.c_str(),
25+
SERVICE_ALL_ACCESS,
26+
SERVICE_KERNEL_DRIVER,
27+
SERVICE_DEMAND_START,
28+
SERVICE_ERROR_NORMAL,
29+
driverPath.c_str(),
30+
nullptr, nullptr, nullptr, nullptr, nullptr);
31+
32+
if (!hService)
33+
{
34+
DWORD err = GetLastError();
35+
if (err == ERROR_SERVICE_EXISTS)
36+
{
37+
std::cout << "[+] Service \'" << driverName << "\' already exists." << std::endl;
38+
}
39+
else
40+
{
41+
std::cerr << "[!] CreateService \'"<< driverName << "\' failed: " << err << std::endl;
42+
CloseServiceHandle(hSCManager);
43+
return false;
44+
}
45+
}
46+
else
47+
{
48+
std::cout << "[+] Service \'" << driverName << "\' created successfully." << std::endl;
49+
CloseServiceHandle(hService);
50+
}
51+
52+
CloseServiceHandle(hSCManager);
53+
return true;
54+
}
55+
56+
bool StartDriver(const std::string& driverName)
57+
{
58+
SC_HANDLE hSCManager = OpenSCManagerA(nullptr, nullptr, SC_MANAGER_CONNECT);
59+
if (!hSCManager)
60+
{
61+
std::cerr << "[!] OpenSCManager failed: " << GetLastError() << std::endl;
62+
return false;
63+
}
64+
65+
SC_HANDLE hService = OpenServiceA(hSCManager, driverName.c_str(), SERVICE_START);
66+
if (!hService)
67+
{
68+
std::cerr << "[!] OpenService \'" << driverName << "\' failed: " << GetLastError() << std::endl;
69+
CloseServiceHandle(hSCManager);
70+
return false;
71+
}
72+
73+
if (!StartServiceA(hService, 0, nullptr))
74+
{
75+
DWORD err = GetLastError();
76+
if (err == ERROR_SERVICE_ALREADY_RUNNING)
77+
{
78+
std::cout << "[+] Service \'" << driverName << "\' is already running." << std::endl;
79+
}
80+
else
81+
{
82+
std::cerr << "[!] StartService \'" << driverName << "\' failed: " << err << std::endl;
83+
CloseServiceHandle(hService);
84+
CloseServiceHandle(hSCManager);
85+
return false;
86+
}
87+
}
88+
else
89+
{
90+
std::cout << "[+] Service \'" << driverName << "\' started successfully." << std::endl;
91+
}
92+
93+
CloseServiceHandle(hService);
94+
CloseServiceHandle(hSCManager);
95+
return true;
96+
}
97+
98+
bool StopDriver(const std::string& driverName)
99+
{
100+
SC_HANDLE hSCManager = OpenSCManagerA(nullptr, nullptr, SC_MANAGER_CONNECT);
101+
if (!hSCManager)
102+
{
103+
std::cerr << "[!] OpenSCManager failed: " << GetLastError() << std::endl;
104+
return false;
105+
}
106+
107+
SC_HANDLE hService = OpenServiceA(hSCManager, driverName.c_str(), SERVICE_STOP | SERVICE_QUERY_STATUS);
108+
if (!hService)
109+
{
110+
std::cerr << "[!] OpenService \'" << driverName << "\' failed: " << GetLastError() << std::endl;
111+
CloseServiceHandle(hSCManager);
112+
return false;
113+
}
114+
115+
SERVICE_STATUS status = {};
116+
if (!ControlService(hService, SERVICE_CONTROL_STOP, &status))
117+
{
118+
DWORD err = GetLastError();
119+
if (err == ERROR_SERVICE_NOT_ACTIVE)
120+
{
121+
std::cout << "[!] Service \'" << driverName << "\' is not running." << std::endl;
122+
}
123+
else
124+
{
125+
std::cerr << "[!] ControlService (STOP) failed: " << err << std::endl;
126+
CloseServiceHandle(hService);
127+
CloseServiceHandle(hSCManager);
128+
return false;
129+
}
130+
}
131+
else
132+
{
133+
std::cout << "[-] Service \'" << driverName << "\' stopped successfully." << std::endl;
134+
}
135+
136+
CloseServiceHandle(hService);
137+
CloseServiceHandle(hSCManager);
138+
return true;
139+
}
140+
141+
bool DeleteDriver(const std::string& driverName)
142+
{
143+
SC_HANDLE hSCManager = OpenSCManagerA(nullptr, nullptr, SC_MANAGER_CONNECT);
144+
if (!hSCManager)
145+
{
146+
std::cerr << "[!] OpenSCManager failed: " << GetLastError() << std::endl;
147+
return false;
148+
}
149+
150+
SC_HANDLE hService = OpenServiceA(hSCManager, driverName.c_str(), DELETE);
151+
if (!hService)
152+
{
153+
std::cerr << "[!] OpenService \'" << driverName << "\' failed: " << GetLastError() << std::endl;
154+
CloseServiceHandle(hSCManager);
155+
return false;
156+
}
157+
158+
if (!DeleteService(hService))
159+
{
160+
std::cerr << "[!] DeleteService \'" << driverName << "\' failed: " << GetLastError() << std::endl;
161+
CloseServiceHandle(hService);
162+
CloseServiceHandle(hSCManager);
163+
return false;
164+
}
165+
166+
std::cout << "[-] Service \'" << driverName << "\' deleted successfully." << std::endl;
167+
CloseServiceHandle(hService);
168+
CloseServiceHandle(hSCManager);
169+
return true;
170+
}
171+
}

SbieKernelPatch/Resource.aps

11.5 KB
Binary file not shown.

SbieKernelPatch/Resource.rc

3.16 KB
Binary file not shown.

0 commit comments

Comments
 (0)