- #define UNICODE
- #define _UNICODE
- #include <windows.h>
- #include <ntsecapi.h>
- #include <ntstatus.h>
- #include <sddl.h>
- #include <wchar.h>
- #include <stdio.h>
- void InitLsaString(PLSA_UNICODE_STRING LsaString, LPWSTR String)
- {
- DWORD StringLength;
- if (String == NULL) {
- LsaString->Buffer = NULL;
- LsaString->Length = 0;
- LsaString->MaximumLength = 0;
- return;
- }
- LsaString->Buffer = String;
- LsaString->Length = (USHORT)StringLength * sizeof(WCHAR);
- LsaString->MaximumLength = (USHORT)(StringLength + 1) * sizeof(WCHAR);
- }
- NTSTATUS OpenPolicy(LPWSTR ServerName, DWORD DesiredAccess, PLSA_HANDLE PolicyHandle)
- {
- LSA_OBJECT_ATTRIBUTES ObjectAttributes;
- LSA_UNICODE_STRING ServerString;
- PLSA_UNICODE_STRING Server = NULL;
- //
- // Always initialize the object attributes to all zeroes.
- //
- ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
- if (ServerName != NULL) {
- //
- // Make a LSA_UNICODE_STRING out of the LPWSTR passed in
- //
- InitLsaString(&ServerString, ServerName);
- Server = &ServerString;
- }
- //
- // Attempt to open the policy.
- //
- return LsaOpenPolicy(
- Server,
- &ObjectAttributes,
- DesiredAccess,
- PolicyHandle
- );
- }
- NTSTATUS SetPrivilegeOnAccount(LSA_HANDLE PolicyHandle, PSID AccountSid, LPWSTR PrivilegeName, BOOL bEnable)
- {
- LSA_UNICODE_STRING PrivilegeString;
- //
- // Create a LSA_UNICODE_STRING for the privilege name.
- //
- InitLsaString(&PrivilegeString, PrivilegeName);
- //
- // grant or revoke the privilege, accordingly
- //
- if (bEnable) {
- return LsaAddAccountRights(
- PolicyHandle, // open policy handle
- AccountSid, // target SID
- &PrivilegeString, // privileges
- 1 // privilege count
- );
- }
- else {
- return LsaRemoveAccountRights(
- PolicyHandle, // open policy handle
- AccountSid, // target SID
- FALSE, // do not disable all rights
- &PrivilegeString, // privileges
- 1 // privilege count
- );
- }
- }
- int main(int ac, char *av[])
- {
- HANDLE hToken = NULL;
- if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
- {
- return -1;
- }
- DWORD dwBufferSize = 0;
- // Probe the buffer size reqired for PTOKEN_USER structure
- if (!GetTokenInformation(hToken, TokenUser, NULL, 0, &dwBufferSize) &&
- (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
- {
- // Cleanup
- CloseHandle(hToken);
- hToken = NULL;
- return -1;
- }
- // Retrieve the token information in a TOKEN_USER structure
- if (!GetTokenInformation(
- hToken,
- TokenUser,
- pTokenUser,
- dwBufferSize,
- &dwBufferSize))
- {
- // Cleanup
- CloseHandle(hToken);
- hToken = NULL;
- return -1;
- }
- // Print SID string
- LPWSTR strsid;
- ConvertSidToStringSid(pTokenUser->User.Sid, &strsid);
- // Cleanup
- CloseHandle(hToken);
- hToken = NULL;
- NTSTATUS status;
- LSA_HANDLE policyHandle;
- if (status = OpenPolicy(NULL, POLICY_CREATE_ACCOUNT | POLICY_LOOKUP_NAMES, &policyHandle))
- {
- }
- #if 0
- // Add new privelege to the account
- if (status = SetPrivilegeOnAccount(policyHandle, pTokenUser->User.Sid, SE_LOCK_MEMORY_NAME, TRUE))
- {
- }
- #endif
- // Enable this priveledge for the current process
- hToken = NULL;
- TOKEN_PRIVILEGES tp;
- if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &hToken))
- {
- return -1;
- }
- tp.PrivilegeCount = 1;
- tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
- if (!LookupPrivilegeValue(NULL, SE_LOCK_MEMORY_NAME, &tp.Privileges[0].Luid))
- {
- return -1;
- }
- BOOL result = AdjustTokenPrivileges(hToken, FALSE, &tp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
- DWORD error = GetLastError();
- if (!result || (error != ERROR_SUCCESS))
- {
- return -1;
- }
- // Cleanup
- CloseHandle(hToken);
- hToken = NULL;
- SIZE_T pageSize = GetLargePageMinimum();
- #define N_PAGES_TO_ALLOC 4
- // Finally allocate the memory
- char *largeBuffer = VirtualAlloc(NULL, pageSize * N_PAGES_TO_ALLOC, MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
- if (largeBuffer)
- {
- }
- else
- {
- }
- }
Win32: Get large MMU pages for memory
Posted by Anonymous on Tue 16th Apr 2024 02:20
raw | new post
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.