pastebin - collaborative debugging tool
rovema.kpaste.net RSS


sidunixuser1.c
Posted by Anonymous on Wed 4th Oct 2023 21:06
raw | new post
view followups (newest first): sidunixuser1.c by Anonymous
modification of post by Anonymous (view diff)

  1.  
  2. #include <windows.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <sddl.h>
  6.  
  7. BOOL AllocateUnixUserSID(unsigned long uid, PSID *pSid)
  8. {
  9.     // S-1-22-1-1
  10. #define SECURITY_SAMBA_UNIX_AUTHORITY {0,0,0,0,0,22}
  11.     SID_IDENTIFIER_AUTHORITY sid_id_auth = SECURITY_SAMBA_UNIX_AUTHORITY;
  12.    
  13.     /*
  14.      *
  15.      * $ getent passwd Unix_User+0
  16.      * Unix_User+0:*:4278190080:4278190080:U-Unix_User\0,S-1-22-1-0:/:/sbin/nologin
  17.      *
  18.      */
  19.     // Create a new SID with the given ID authority and no sub-authorities
  20.     if (AllocateAndInitializeSid(&sid_id_auth, 2, 1, (DWORD)uid, 0, 0, 0, 0, 0, 0, pSid)) {
  21.         DWORD sid_len = GetLengthSid(*pSid);
  22.         PSID malloced_sid;
  23.         (void)printf("AllocateUnixUserSID(): Allocated Unix_User+%lu: success, len=%ld\n",
  24.             uid, (long)sid_len);
  25.         malloced_sid = malloc(sid_len);
  26.         CopySid(sid_len, malloced_sid, *pSid);
  27.         FreeSid(*pSid);
  28.         *pSid = malloced_sid;
  29.         return TRUE;
  30.     }
  31.     else
  32.     {
  33.         (void)printf("AllocateUnixUserSID(): Failed to allocate Unix_User+%lu: error code %d\n",
  34.             uid, GetLastError());
  35.         return FALSE;
  36.     }
  37. }
  38.  
  39. int fput_sidstr(FILE *f, PSID *sid)
  40. {
  41.     LPSTR string_sid = NULL;
  42.     int res;
  43.     if (!ConvertSidToStringSidA(*sid, &string_sid)) {
  44.         (void)printf("fput_sidstr: Failed to convert to string: error code %d\n", GetLastError());
  45.         return -1;
  46.     }
  47.     res = fputs(string_sid, f);
  48.     LocalFree(string_sid);
  49.     return res;
  50. }
  51.  
  52. int main(int ac, char *av[])
  53. {
  54.     // S-1-22-1-1
  55.     PSID sid = 0;
  56.     PSID new_sid = 0;
  57.     LPSTR string_sid = NULL;
  58.    
  59. #if 1
  60.     // Create a new SID with the given ID authority and no sub-authorities
  61.     if (!AllocateUnixUserSID(1616, &sid)) {
  62.         (void)printf("Failed to allocate SID: error code %d\n", GetLastError());
  63.         return 1;
  64.     }
  65.    
  66.     (void)printf("sid='");
  67.     (void)fput_sidstr(stdout, &sid);
  68.     (void)printf("'\n");
  69.  
  70.     // Stringify and print
  71.     if (!ConvertSidToStringSidA(sid, &string_sid)) {
  72.         printf("Failed to convert to string: error code %d\n", GetLastError());
  73.         FreeSid(sid);
  74.         return 2;
  75.     }
  76.     (void)printf("string_sid='%s'\n", string_sid);
  77.  
  78.     // Destringify and print
  79.     if (ConvertStringSidToSidA(string_sid, &new_sid)) {
  80.         (void)printf("Success\n");
  81.     }
  82.     else {
  83.         (void)printf("Failed: error code %d\n", GetLastError());
  84.     }
  85. #endif
  86.     /* Part 2 */
  87.     (void)printf("# Part 2, custom ID\n");
  88.  
  89.     LocalFree(string_sid);
  90.     LocalFree(new_sid);
  91.     free(sid);
  92.     sid = NULL;
  93.     new_sid = NULL;    
  94.     string_sid = NULL;
  95.    
  96.     char sid_unix_user_buf[256];
  97.     snprintf(sid_unix_user_buf, sizeof(sid_unix_user_buf), "S-1-22-1-%lu", 1616UL);
  98.  
  99.     if (ConvertStringSidToSidA(sid_unix_user_buf, &sid)) {
  100.         (void)printf("Success\n");
  101.     }
  102.     else {
  103.         (void)printf("ConvertStringSidToSidA() Failed error code %d\n", GetLastError());
  104.     }
  105.     if (!ConvertSidToStringSidA(sid, &string_sid)) {
  106.         (void)printf("ConvertSidToStringSidA() failed error code %d\n", GetLastError());
  107.         FreeSid(sid);
  108.         return 2;
  109.     }
  110.     (void)printf("string_sid='%s'\n", string_sid);
  111.  
  112.     // Destringify and print
  113.     if (ConvertStringSidToSidA(string_sid, &new_sid)) {
  114.         (void)printf("Success\n");
  115.     }
  116.     else {
  117.         (void)printf("Failed: error code %d\n", GetLastError());
  118.     }
  119.  
  120.     if (!ConvertSidToStringSidA(new_sid, &string_sid)) {
  121.         (void)printf("ConvertSidToStringSidA() failed error code %d\n", GetLastError());
  122.         FreeSid(sid);
  123.         return 2;
  124.     }
  125.     (void)printf("string_sid='%s'\n", string_sid);
  126.  
  127.     // Clean up
  128.     LocalFree(string_sid);
  129.     LocalFree(new_sid);
  130.     FreeSid(sid);
  131. }

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