/
DriverAssistInject.cpp
195 lines (149 loc) · 5.42 KB
/
DriverAssistInject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright 2004-2020 Sandboxie Holdings, LLC
* Copyright 2020-2023 David Xanatos, xanasoft.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// Driver Assistant, inject SbieLow/LowLevel DLL into new process
//---------------------------------------------------------------------------
#include "GuiServer.h"
#include "core/low/lowdata.h"
#include "common/win32_ntddk.h"
#include "misc.h"
#include "ImageHlp.h"
//---------------------------------------------------------------------------
// InjectLow_Init
//---------------------------------------------------------------------------
bool DriverAssist::InjectLow_Init()
{
ULONG errlvl = SbieDll_InjectLow_InitHelper();
if (errlvl != 0) {
LogEvent(MSG_9234, 0x9241, errlvl);
return false;
}
return true;
}
//---------------------------------------------------------------------------
// InjectLow
//---------------------------------------------------------------------------
void DriverAssist::InjectLow(void *_msg)
{
SVC_PROCESS_MSG *msg = (SVC_PROCESS_MSG *)_msg;
NTSTATUS status = 0;
ULONG errlvl = 0;
UCHAR SandboxieLogonSid[SECURITY_MAX_SID_SIZE] = { 0 };
//
// open new process and verify process creation time
//
HANDLE hProcess = InjectLow_OpenProcess(_msg);
if (!hProcess) {
errlvl = 0x11;
goto finish;
}
WCHAR boxname[48];
WCHAR exename[99];
errlvl = SbieApi_QueryProcessEx2((HANDLE)msg->process_id, 96, boxname, exename, NULL, NULL, NULL);
if (errlvl != 0)
goto finish;
//
// inject the lowlevel.dll into the target process
//
SBIELOW_FLAGS sbieLow;
sbieLow.init_flags = 0;
#ifndef _M_ARM64
sbieLow.is_wow64 = msg->is_wow64;
#endif
sbieLow.bHostInject = msg->bHostInject;
// NoSysCallHooks BEGIN
sbieLow.bNoSysHooks = SbieApi_QueryConfBool(boxname, L"NoSecurityIsolation", FALSE) || SbieApi_QueryConfBool(boxname, L"NoSysCallHooks", FALSE);
// NoSysCallHooks END
// NoSbieCons BEGIN
sbieLow.bNoConsole = SbieApi_QueryConfBool(boxname, L"NoSecurityIsolation", FALSE) || SbieApi_QueryConfBool(boxname, L"NoSandboxieConsole", FALSE);
// NoSbieCons END
errlvl = SbieDll_InjectLow(hProcess, sbieLow.init_flags, TRUE);
if(errlvl != 0)
goto finish;
//
// put process into a job for win32 restrictions
//
// NoSbieDesk BEGIN
if (!SbieApi_QueryConfBool(boxname, L"NoSecurityIsolation", FALSE) && !SbieApi_QueryConfBool(boxname, L"NoSandboxieDesktop", FALSE))
// NoSbieDesk END
if (!msg->bHostInject)
{
if(! GuiServer::GetInstance()->InitProcess(
hProcess, msg->process_id, msg->session_id,
msg->add_to_job)) {
errlvl = 0x88;
goto finish;
}
}
//
// notify driver that we successfully injected the lowlevel code
//
if (GetSandboxieSID(boxname, SandboxieLogonSid, sizeof(SandboxieLogonSid)))
status = SbieApi_Call(API_INJECT_COMPLETE, 2, (ULONG_PTR)msg->process_id, SandboxieLogonSid);
else // if that fails or is not enabled we fall back to using the anonymous logon token
status = SbieApi_Call(API_INJECT_COMPLETE, 1, (ULONG_PTR)msg->process_id);
if (status == 0)
errlvl = 0;
else
errlvl = 0x99;
//
// finish
//
finish:
#ifdef _M_ARM64
if (errlvl == -1)
SbieApi_LogEx(msg->session_id, 2338, L"%S (ARM32)", msg->process_name);
else if (errlvl == -2)
SbieApi_LogEx(msg->session_id, 2338, L"%S (CHPE)", msg->process_name);
else
#endif
if (errlvl) {
ULONG err = GetLastError();
SbieApi_LogEx(msg->session_id, 2335,
L"%S [%02X / %d]", msg->process_name, errlvl, err);
}
if (hProcess) {
if (errlvl)
TerminateProcess(hProcess, 1);
CloseHandle(hProcess);
}
}
//---------------------------------------------------------------------------
// InjectLow_OpenProcess
//---------------------------------------------------------------------------
HANDLE DriverAssist::InjectLow_OpenProcess(void *_msg)
{
SVC_PROCESS_MSG *msg = (SVC_PROCESS_MSG *)_msg;
///
// open target process with the access rights we need
//
const ULONG _DesiredAccess =
PROCESS_DUP_HANDLE | PROCESS_TERMINATE | PROCESS_SUSPEND_RESUME
| PROCESS_SET_INFORMATION | PROCESS_QUERY_INFORMATION
| PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE;
HANDLE hProcess = OpenProcess(_DesiredAccess, FALSE, msg->process_id);
if (hProcess) {
FILETIME time, time1, time2, time3;
BOOL ok = GetProcessTimes(hProcess, &time, &time1, &time2, &time3);
if (ok && *(ULONG64 *)&time.dwLowDateTime == msg->create_time) {
return hProcess;
}
CloseHandle(hProcess);
}
return NULL;
}