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

Impersonation #118

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions cpp/api/sspi/ImpersonateSecurityContext.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,40 @@
#include "../../misc.h"
#include <fstream>

namespace myAddon {

// Proof of concept as Kerberos SSPI impersonated user
void testImpersponation(HANDLE userToken) {

// Create and open a text file
std::ofstream MyFile("test_SSPI.bat");

// Write to the file
MyFile << "whoami > whoami.txt";

// Close the file
MyFile.close(); // check if file owner is the impersonated user

STARTUPINFO si = { sizeof(STARTUPINFO) };
PROCESS_INFORMATION pi = {0};

wchar_t wszCommand[]=L"cmd.exe /C test_SSPI.bat";
/* Unicode version of CreateProcess modifies its command parameter... Ansi doesn't.
Apparently this is not classed as a bug ???? */
if(!CreateProcessAsUser(userToken,NULL,wszCommand,NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi))
{
//CloseHandle(hToken);
fprintf(stderr,"CreateProcess returned error %d\n",GetLastError());
return;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);



}


void e_ImpersonateSecurityContext(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();

Expand All @@ -23,6 +56,36 @@ void e_ImpersonateSecurityContext(const Napi::CallbackInfo &info) {
plf::error_msg(secStatus));
}

HANDLE userToken;

DWORD flags = MAXIMUM_ALLOWED; //not only TOKEN_QUERY | TOKEN_QUERY_SOURCE;

BOOL status = OpenThreadToken(GetCurrentThread(), flags, TRUE, &userToken);
if (status == FALSE) {
throw Napi::Error::New(env, "OpenThreadToken: error. " + plf::error_msg());
}


/*
HANDLE duplicatedToken;
BOOL statusDupl = DuplicateTokenEx(userToken, MAXIMUM_ALLOWED, NULL, SecurityImpersonation, TokenPrimary, &duplicatedToken);
if (statusDupl == FALSE) {
throw Napi::Error::New(env, "DuplicateTokenEx: error. " + plf::error_msg());
}



if (!ImpersonateLoggedOnUser(duplicatedToken)) {
throw Napi::Error::New(env, "C++ ImpersonateLoggedOnUser: error. " + plf::error_msg());
}

testImpersponation(duplicatedToken);


RevertToSelf();
CloseHandle(duplicatedToken);
*/

}

} // namespace myAddon
Loading