Skip to content

Instantly share code, notes, and snippets.

@diversenok
Last active March 8, 2021 20:23
Show Gist options
  • Select an option

  • Save diversenok/bfb629798dc1cdbdd51db6dc945c0c16 to your computer and use it in GitHub Desktop.

Select an option

Save diversenok/bfb629798dc1cdbdd51db6dc945c0c16 to your computer and use it in GitHub Desktop.
SbieSvc Thread Leak Demo

SbieSvc Thread Leak Demo

The program calls NtImpersonateThread in a loop. Since Sandboxie does not implement this function, each function call triggers it to log a message and leak a thread handle.

You can find the binary below.

program ThreadLeakDemo;
{$APPTYPE CONSOLE}
{$MINENUMSIZE 4}
{$R *.res}
const
ntdll = 'ntdll.dll';
NtCurrentThread: THandle = THandle(-2);
MILLISEC = -10000;
type
NTSTATUS = type Cardinal;
TSecurityImpersonationLevel = (
SecurityAnonymous = 0,
SecurityIdentification = 1,
SecurityImpersonation = 2,
SecurityDelegation = 3
);
TSecurityQualityOfService = record
Length: Cardinal;
ImpersonationLevel: TSecurityImpersonationLevel;
ContextTrackingMode: Boolean;
EffectiveOnly: Boolean;
end;
function NtImpersonateThread(ServerThreadHandle: THandle;
ClientThreadHandle: THandle; const SecurityQos: TSecurityQualityOfService):
NTSTATUS; stdcall; external ntdll;
function NtDelayExecution(Alertable: Boolean; const [ref] DelayInterval:
Int64): NTSTATUS; stdcall; external ntdll;
procedure Main;
var
QS: TSecurityQualityOfService;
begin
QS := Default(TSecurityQualityOfService);
QS.Length := SizeOf(QS);
QS.ImpersonationLevel := SecurityImpersonation;
while True do
begin
writeln(NtImpersonateThread(NtCurrentThread, NtCurrentThread, QS));
NtDelayExecution(False, 100 * MILLISEC);
end;
end;
begin
Main;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment