pastebin - collaborative debugging tool
rovema.kpaste.net RSS


sidunixuser1.cc
Posted by Anonymous on Wed 4th Oct 2023 09:17
raw | new post
view followups (newest first): sidunixuser1.c by Anonymous

  1. #include <iostream>
  2. #include <windows.h>
  3. #include <sddl.h>
  4.  
  5. int main()
  6. {
  7.     // S-1-22-1-1
  8. #define SECURITY_SAMBA_UNIX_AUTHORITY {0,0,0,0,0,22}
  9.     SID_IDENTIFIER_AUTHORITY sid_id_auth = SECURITY_SAMBA_UNIX_AUTHORITY;
  10.     PSID sid = 0;
  11.     PSID new_sid = 0;
  12.     LPWSTR string_sid = NULL;
  13.    
  14.     /*
  15.      *
  16.      * $ getent passwd Unix_User+0
  17.      * Unix_User+0:*:4278190080:4278190080:U-Unix_User\0,S-1-22-1-0:/:/sbin/nologin
  18.      *
  19.      */
  20. #if 0
  21.     // Create a new SID with the given ID authority and no sub-authorities
  22.     if (!AllocateAndInitializeSid(&sid_id_auth, 2, 1, 0, 0, 0, 0, 0, 0, 0, &sid)) {
  23.         std::cout << "Failed to allocate SID: error code " << GetLastError() << std::endl;
  24.         return 1;
  25.     }
  26.  
  27.     // Stringify and print
  28.     if (!ConvertSidToStringSidW(sid, &string_sid)) {
  29.         std::cout << "Failed to convert to string: error code " << GetLastError() << std::endl;
  30.         FreeSid(sid);
  31.         return 2;
  32.     }
  33.     std::wcout << string_sid << std::endl;
  34.  
  35.     // Destringify and print
  36.     if (ConvertStringSidToSidW(string_sid, &new_sid)) {
  37.         std::cout << "Success" << std::endl;
  38.     }
  39.     else {
  40.         std::cout << "Failed: error code " << GetLastError() << std::endl;
  41.     }
  42. #endif
  43.     /* Part 2 */
  44.     std::wcout << "# Part 2, custom ID" << std::endl;
  45.  
  46.     LocalFree(string_sid);
  47.     LocalFree(new_sid);
  48.     FreeSid(sid);
  49.     sid = NULL;
  50.     new_sid = NULL;    
  51.     string_sid = NULL;
  52.    
  53.     char sid_unix_user_buf[256];
  54.     snprintf(sid_unix_user_buf, sizeof(sid_unix_user_buf), "S-1-22-1-%lu", 1616UL);
  55.  
  56.     if (ConvertStringSidToSidA(sid_unix_user_buf, &sid)) {
  57.         std::cout << "Success" << std::endl;
  58.     }
  59.     else {
  60.         std::cout << "Failed: error code " << GetLastError() << std::endl;
  61.     }
  62.     if (!ConvertSidToStringSidW(sid, &string_sid)) {
  63.         std::cout << "Failed to convert to string: error code " << GetLastError() << std::endl;
  64.         FreeSid(sid);
  65.         return 2;
  66.     }
  67.     std::wcout << string_sid << std::endl;
  68.  
  69.     // Destringify and print
  70.     if (ConvertStringSidToSidW(string_sid, &new_sid)) {
  71.         std::cout << "Success" << std::endl;
  72.     }
  73.     else {
  74.         std::cout << "Failed: error code " << GetLastError() << std::endl;
  75.     }
  76.  
  77.     if (!ConvertSidToStringSidW(new_sid, &string_sid)) {
  78.         std::cout << "Failed to convert to string: error code " << GetLastError() << std::endl;
  79.         FreeSid(sid);
  80.         return 2;
  81.     }
  82.     std::wcout << string_sid << std::endl;
  83.  
  84.     // Clean up
  85.     LocalFree(string_sid);
  86.     LocalFree(new_sid);
  87.     FreeSid(sid);
  88. }

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.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at