Windows Service in C++: AdjustTokenPrivileges, Accessing UFEI in service











up vote
-1
down vote

favorite












I implemented a console application which can access the UFEI by using the API AdjustTokenPrivilages and GetFirmwareEnvironmentVariable. This app works from a command line opened as administrator.



EDIT: adding portion of the code which is working for console app. Same APIs are used in service too.



//function to update privilages
int privilageUpdate ()
{
TOKEN_PRIVILEGES tp;
HANDLE hToken;
LUID luid;
DWORD err;

if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES,
&hToken)) {
err = GetLastError();
return (-1 * err);
}

if (!LookupPrivilegeValue(NULL, SE_SYSTEM_ENVIRONMENT_NAME, &luid)) {
err = GetLastError();
return (-1*err);
}

tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE/*TRUE*/, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
if (GetLastError() != ERROR_SUCCESS) {
err = GetLastError();
return (-1*err);
}
return ERROR_SUCCESS;
}

INT readUFEI () {
//Reading
INT32 data;
INT success = GetFirmwareEnvironmentVariable(L"VAR"
, L"{xxyyzzaa-xxyy-xxyy-aabb-bbccddeeffgg}"
, &data
, sizeof(data));
if (0 == success) {
err = GetLastError();
return (-1 * err)
}
return data;
}


I am working on a C++ service (since, I've to make use of the C++ API & .dlls and other Windows APIs which are available as C++ APIs) which needs to access the UFEI. The service runs but the API AdjustTokenPrivilages is failing with 1300 ERROR_NOT_ALL_ASSIGNED, GetFirmwareEnvironmentVariable is failing with error code 1314 ERROR_PRIVILEGE_NOT_HELD (A required privilege is not held by the client).



I am currently logged in with username xyz and I have admin access rights.
As I understand the services are run differently from the users.



I've tried the following but did not work




  1. running with command line with sc start servicename in administrator mode

  2. running the services app as admin and then right clicking to start service.


So my question is




  1. Is AdjustTokenPrivilages has any effect on services ? If not, how to achieve this ?

  2. Is this something which can be solved by changing the Log On tab ?


Please note that the service is not used by any apps.
I am new to the Windows development and any help would be appriciated.










share|improve this question
























  • Is AdjustTokenPrivilages has effect on any token. service here unrelated.
    – RbMm
    Nov 10 at 15:37












  • @RbMm If you're saying that it is not related to service, why that it works in console app but not when moved to service ?
    – neeru
    Nov 11 at 3:05










  • of course AdjustTokenPrivilages is absolute unrelated to service. and you not show any code, what is "working" or not
    – RbMm
    Nov 11 at 5:55










  • @RbMm Thanks for the feedback. I updated the code.
    – neeru
    Nov 11 at 11:34










  • you forget close hToken. except this code is correct. (only why you negative returned error code ? return it as is). if AdjustTokenPrivilages return ERROR_NOT_ALL_ASSIGNED this mean that your process token have not SE_SYSTEM_ENVIRONMENT_PRIVILEGE. for be sure - enumerate and dump current privileges from your process token (or use some utils for this). under which account your service run ? if under LocalSystem you must have SeSystemEnvironmentPrivilege
    – RbMm
    Nov 11 at 12:06















up vote
-1
down vote

favorite












I implemented a console application which can access the UFEI by using the API AdjustTokenPrivilages and GetFirmwareEnvironmentVariable. This app works from a command line opened as administrator.



EDIT: adding portion of the code which is working for console app. Same APIs are used in service too.



//function to update privilages
int privilageUpdate ()
{
TOKEN_PRIVILEGES tp;
HANDLE hToken;
LUID luid;
DWORD err;

if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES,
&hToken)) {
err = GetLastError();
return (-1 * err);
}

if (!LookupPrivilegeValue(NULL, SE_SYSTEM_ENVIRONMENT_NAME, &luid)) {
err = GetLastError();
return (-1*err);
}

tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE/*TRUE*/, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
if (GetLastError() != ERROR_SUCCESS) {
err = GetLastError();
return (-1*err);
}
return ERROR_SUCCESS;
}

INT readUFEI () {
//Reading
INT32 data;
INT success = GetFirmwareEnvironmentVariable(L"VAR"
, L"{xxyyzzaa-xxyy-xxyy-aabb-bbccddeeffgg}"
, &data
, sizeof(data));
if (0 == success) {
err = GetLastError();
return (-1 * err)
}
return data;
}


I am working on a C++ service (since, I've to make use of the C++ API & .dlls and other Windows APIs which are available as C++ APIs) which needs to access the UFEI. The service runs but the API AdjustTokenPrivilages is failing with 1300 ERROR_NOT_ALL_ASSIGNED, GetFirmwareEnvironmentVariable is failing with error code 1314 ERROR_PRIVILEGE_NOT_HELD (A required privilege is not held by the client).



I am currently logged in with username xyz and I have admin access rights.
As I understand the services are run differently from the users.



I've tried the following but did not work




  1. running with command line with sc start servicename in administrator mode

  2. running the services app as admin and then right clicking to start service.


So my question is




  1. Is AdjustTokenPrivilages has any effect on services ? If not, how to achieve this ?

  2. Is this something which can be solved by changing the Log On tab ?


Please note that the service is not used by any apps.
I am new to the Windows development and any help would be appriciated.










share|improve this question
























  • Is AdjustTokenPrivilages has effect on any token. service here unrelated.
    – RbMm
    Nov 10 at 15:37












  • @RbMm If you're saying that it is not related to service, why that it works in console app but not when moved to service ?
    – neeru
    Nov 11 at 3:05










  • of course AdjustTokenPrivilages is absolute unrelated to service. and you not show any code, what is "working" or not
    – RbMm
    Nov 11 at 5:55










  • @RbMm Thanks for the feedback. I updated the code.
    – neeru
    Nov 11 at 11:34










  • you forget close hToken. except this code is correct. (only why you negative returned error code ? return it as is). if AdjustTokenPrivilages return ERROR_NOT_ALL_ASSIGNED this mean that your process token have not SE_SYSTEM_ENVIRONMENT_PRIVILEGE. for be sure - enumerate and dump current privileges from your process token (or use some utils for this). under which account your service run ? if under LocalSystem you must have SeSystemEnvironmentPrivilege
    – RbMm
    Nov 11 at 12:06













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I implemented a console application which can access the UFEI by using the API AdjustTokenPrivilages and GetFirmwareEnvironmentVariable. This app works from a command line opened as administrator.



EDIT: adding portion of the code which is working for console app. Same APIs are used in service too.



//function to update privilages
int privilageUpdate ()
{
TOKEN_PRIVILEGES tp;
HANDLE hToken;
LUID luid;
DWORD err;

if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES,
&hToken)) {
err = GetLastError();
return (-1 * err);
}

if (!LookupPrivilegeValue(NULL, SE_SYSTEM_ENVIRONMENT_NAME, &luid)) {
err = GetLastError();
return (-1*err);
}

tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE/*TRUE*/, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
if (GetLastError() != ERROR_SUCCESS) {
err = GetLastError();
return (-1*err);
}
return ERROR_SUCCESS;
}

INT readUFEI () {
//Reading
INT32 data;
INT success = GetFirmwareEnvironmentVariable(L"VAR"
, L"{xxyyzzaa-xxyy-xxyy-aabb-bbccddeeffgg}"
, &data
, sizeof(data));
if (0 == success) {
err = GetLastError();
return (-1 * err)
}
return data;
}


I am working on a C++ service (since, I've to make use of the C++ API & .dlls and other Windows APIs which are available as C++ APIs) which needs to access the UFEI. The service runs but the API AdjustTokenPrivilages is failing with 1300 ERROR_NOT_ALL_ASSIGNED, GetFirmwareEnvironmentVariable is failing with error code 1314 ERROR_PRIVILEGE_NOT_HELD (A required privilege is not held by the client).



I am currently logged in with username xyz and I have admin access rights.
As I understand the services are run differently from the users.



I've tried the following but did not work




  1. running with command line with sc start servicename in administrator mode

  2. running the services app as admin and then right clicking to start service.


So my question is




  1. Is AdjustTokenPrivilages has any effect on services ? If not, how to achieve this ?

  2. Is this something which can be solved by changing the Log On tab ?


Please note that the service is not used by any apps.
I am new to the Windows development and any help would be appriciated.










share|improve this question















I implemented a console application which can access the UFEI by using the API AdjustTokenPrivilages and GetFirmwareEnvironmentVariable. This app works from a command line opened as administrator.



EDIT: adding portion of the code which is working for console app. Same APIs are used in service too.



//function to update privilages
int privilageUpdate ()
{
TOKEN_PRIVILEGES tp;
HANDLE hToken;
LUID luid;
DWORD err;

if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES,
&hToken)) {
err = GetLastError();
return (-1 * err);
}

if (!LookupPrivilegeValue(NULL, SE_SYSTEM_ENVIRONMENT_NAME, &luid)) {
err = GetLastError();
return (-1*err);
}

tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE/*TRUE*/, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
if (GetLastError() != ERROR_SUCCESS) {
err = GetLastError();
return (-1*err);
}
return ERROR_SUCCESS;
}

INT readUFEI () {
//Reading
INT32 data;
INT success = GetFirmwareEnvironmentVariable(L"VAR"
, L"{xxyyzzaa-xxyy-xxyy-aabb-bbccddeeffgg}"
, &data
, sizeof(data));
if (0 == success) {
err = GetLastError();
return (-1 * err)
}
return data;
}


I am working on a C++ service (since, I've to make use of the C++ API & .dlls and other Windows APIs which are available as C++ APIs) which needs to access the UFEI. The service runs but the API AdjustTokenPrivilages is failing with 1300 ERROR_NOT_ALL_ASSIGNED, GetFirmwareEnvironmentVariable is failing with error code 1314 ERROR_PRIVILEGE_NOT_HELD (A required privilege is not held by the client).



I am currently logged in with username xyz and I have admin access rights.
As I understand the services are run differently from the users.



I've tried the following but did not work




  1. running with command line with sc start servicename in administrator mode

  2. running the services app as admin and then right clicking to start service.


So my question is




  1. Is AdjustTokenPrivilages has any effect on services ? If not, how to achieve this ?

  2. Is this something which can be solved by changing the Log On tab ?


Please note that the service is not used by any apps.
I am new to the Windows development and any help would be appriciated.







windows visual-c++ service






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 11:26

























asked Nov 10 at 12:50









neeru

127210




127210












  • Is AdjustTokenPrivilages has effect on any token. service here unrelated.
    – RbMm
    Nov 10 at 15:37












  • @RbMm If you're saying that it is not related to service, why that it works in console app but not when moved to service ?
    – neeru
    Nov 11 at 3:05










  • of course AdjustTokenPrivilages is absolute unrelated to service. and you not show any code, what is "working" or not
    – RbMm
    Nov 11 at 5:55










  • @RbMm Thanks for the feedback. I updated the code.
    – neeru
    Nov 11 at 11:34










  • you forget close hToken. except this code is correct. (only why you negative returned error code ? return it as is). if AdjustTokenPrivilages return ERROR_NOT_ALL_ASSIGNED this mean that your process token have not SE_SYSTEM_ENVIRONMENT_PRIVILEGE. for be sure - enumerate and dump current privileges from your process token (or use some utils for this). under which account your service run ? if under LocalSystem you must have SeSystemEnvironmentPrivilege
    – RbMm
    Nov 11 at 12:06


















  • Is AdjustTokenPrivilages has effect on any token. service here unrelated.
    – RbMm
    Nov 10 at 15:37












  • @RbMm If you're saying that it is not related to service, why that it works in console app but not when moved to service ?
    – neeru
    Nov 11 at 3:05










  • of course AdjustTokenPrivilages is absolute unrelated to service. and you not show any code, what is "working" or not
    – RbMm
    Nov 11 at 5:55










  • @RbMm Thanks for the feedback. I updated the code.
    – neeru
    Nov 11 at 11:34










  • you forget close hToken. except this code is correct. (only why you negative returned error code ? return it as is). if AdjustTokenPrivilages return ERROR_NOT_ALL_ASSIGNED this mean that your process token have not SE_SYSTEM_ENVIRONMENT_PRIVILEGE. for be sure - enumerate and dump current privileges from your process token (or use some utils for this). under which account your service run ? if under LocalSystem you must have SeSystemEnvironmentPrivilege
    – RbMm
    Nov 11 at 12:06
















Is AdjustTokenPrivilages has effect on any token. service here unrelated.
– RbMm
Nov 10 at 15:37






Is AdjustTokenPrivilages has effect on any token. service here unrelated.
– RbMm
Nov 10 at 15:37














@RbMm If you're saying that it is not related to service, why that it works in console app but not when moved to service ?
– neeru
Nov 11 at 3:05




@RbMm If you're saying that it is not related to service, why that it works in console app but not when moved to service ?
– neeru
Nov 11 at 3:05












of course AdjustTokenPrivilages is absolute unrelated to service. and you not show any code, what is "working" or not
– RbMm
Nov 11 at 5:55




of course AdjustTokenPrivilages is absolute unrelated to service. and you not show any code, what is "working" or not
– RbMm
Nov 11 at 5:55












@RbMm Thanks for the feedback. I updated the code.
– neeru
Nov 11 at 11:34




@RbMm Thanks for the feedback. I updated the code.
– neeru
Nov 11 at 11:34












you forget close hToken. except this code is correct. (only why you negative returned error code ? return it as is). if AdjustTokenPrivilages return ERROR_NOT_ALL_ASSIGNED this mean that your process token have not SE_SYSTEM_ENVIRONMENT_PRIVILEGE. for be sure - enumerate and dump current privileges from your process token (or use some utils for this). under which account your service run ? if under LocalSystem you must have SeSystemEnvironmentPrivilege
– RbMm
Nov 11 at 12:06




you forget close hToken. except this code is correct. (only why you negative returned error code ? return it as is). if AdjustTokenPrivilages return ERROR_NOT_ALL_ASSIGNED this mean that your process token have not SE_SYSTEM_ENVIRONMENT_PRIVILEGE. for be sure - enumerate and dump current privileges from your process token (or use some utils for this). under which account your service run ? if under LocalSystem you must have SeSystemEnvironmentPrivilege
– RbMm
Nov 11 at 12:06

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239121%2fwindows-service-in-c-adjusttokenprivileges-accessing-ufei-in-service%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239121%2fwindows-service-in-c-adjusttokenprivileges-accessing-ufei-in-service%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)